PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/linux/linux/arch/mips/jazz/jazzdma.c

https://gitlab.com/envieidoc/tomato
C | 541 lines | 348 code | 77 blank | 116 comment | 63 complexity | e841f484009eae65359779d874a06842 MD5 | raw file
  1. /*
  2. * Mips Jazz DMA controller support
  3. * Copyright (C) 1995, 1996 by Andreas Busse
  4. *
  5. * NOTE: Some of the argument checking could be removed when
  6. * things have settled down. Also, instead of returning 0xffffffff
  7. * on failure of vdma_alloc() one could leave page #0 unused
  8. * and return the more usual NULL pointer as logical address.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/spinlock.h>
  16. #include <asm/mipsregs.h>
  17. #include <asm/jazz.h>
  18. #include <asm/io.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/dma.h>
  21. #include <asm/jazzdma.h>
  22. #include <asm/pgtable.h>
  23. /*
  24. * Set this to one to enable additional vdma debug code.
  25. */
  26. #define CONF_DEBUG_VDMA 0
  27. static spinlock_t jazz_dma_lock = SPIN_LOCK_UNLOCKED;
  28. static unsigned long vdma_pagetable_start;
  29. /*
  30. * Debug stuff
  31. */
  32. #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
  33. static int debuglvl = 3;
  34. /*
  35. * Initialize the pagetable with a one-to-one mapping of
  36. * the first 16 Mbytes of main memory and declare all
  37. * entries to be unused. Using this method will at least
  38. * allow some early device driver operations to work.
  39. */
  40. static inline void vdma_pgtbl_init(void)
  41. {
  42. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  43. unsigned long paddr = 0;
  44. int i;
  45. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  46. pgtbl[i].frame = paddr;
  47. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  48. paddr += VDMA_PAGESIZE;
  49. }
  50. }
  51. /*
  52. * Initialize the Jazz R4030 dma controller
  53. */
  54. void __init vdma_init(void)
  55. {
  56. /*
  57. * Allocate 32k of memory for DMA page tables. This needs to be page
  58. * aligned and should be uncached to avoid cache flushing after every
  59. * update.
  60. */
  61. vdma_pagetable_start = alloc_bootmem_low_pages(VDMA_PGTBL_SIZE);
  62. if (!vdma_pagetable_start)
  63. BUG();
  64. dma_cache_wback_inv(vdma_pagetable_start, VDMA_PGTBL_SIZE);
  65. vdma_pagetable_start = KSEG1ADDR(vdma_pagetable_start);
  66. /*
  67. * Clear the R4030 translation table
  68. */
  69. vdma_pgtbl_init();
  70. r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,
  71. PHYSADDR(vdma_pagetable_start));
  72. r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
  73. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  74. printk("VDMA: R4030 DMA pagetables initialized.\n");
  75. }
  76. /*
  77. * Allocate DMA pagetables using a simple first-fit algorithm
  78. */
  79. unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
  80. {
  81. VDMA_PGTBL_ENTRY *entry = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  82. int first, last, pages, frame, i;
  83. unsigned long laddr, flags;
  84. /* check arguments */
  85. if (paddr > 0x1fffffff) {
  86. if (vdma_debug)
  87. printk("vdma_alloc: Invalid physical address: %08lx\n",
  88. paddr);
  89. return VDMA_ERROR; /* invalid physical address */
  90. }
  91. if (size > 0x400000 || size == 0) {
  92. if (vdma_debug)
  93. printk("vdma_alloc: Invalid size: %08lx\n", size);
  94. return VDMA_ERROR; /* invalid physical address */
  95. }
  96. spin_lock_irqsave(&jazz_dma_lock, flags);
  97. /*
  98. * Find free chunk
  99. */
  100. pages = (size + 4095) >> 12; /* no. of pages to allocate */
  101. first = 0;
  102. while (1) {
  103. while (entry[first].owner != VDMA_PAGE_EMPTY &&
  104. first < VDMA_PGTBL_ENTRIES) first++;
  105. if (first + pages > VDMA_PGTBL_ENTRIES) {
  106. /* nothing free */
  107. spin_unlock_irqrestore(&jazz_dma_lock, flags);
  108. return VDMA_ERROR;
  109. }
  110. last = first + 1;
  111. while (entry[last].owner == VDMA_PAGE_EMPTY
  112. && last - first < pages)
  113. last++;
  114. if (last - first == pages)
  115. break; /* found */
  116. }
  117. /*
  118. * Mark pages as allocated
  119. */
  120. laddr = (first << 12) + (paddr & (VDMA_PAGESIZE - 1));
  121. frame = paddr & ~(VDMA_PAGESIZE - 1);
  122. for (i = first; i < last; i++) {
  123. entry[i].frame = frame;
  124. entry[i].owner = laddr;
  125. frame += VDMA_PAGESIZE;
  126. }
  127. /*
  128. * Update translation table and return logical start address
  129. */
  130. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  131. if (vdma_debug > 1)
  132. printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
  133. pages, laddr);
  134. if (vdma_debug > 2) {
  135. printk("LADDR: ");
  136. for (i = first; i < last; i++)
  137. printk("%08x ", i << 12);
  138. printk("\nPADDR: ");
  139. for (i = first; i < last; i++)
  140. printk("%08x ", entry[i].frame);
  141. printk("\nOWNER: ");
  142. for (i = first; i < last; i++)
  143. printk("%08x ", entry[i].owner);
  144. printk("\n");
  145. }
  146. spin_unlock_irqrestore(&jazz_dma_lock, flags);
  147. return laddr;
  148. }
  149. /*
  150. * Free previously allocated dma translation pages
  151. * Note that this does NOT change the translation table,
  152. * it just marks the free'd pages as unused!
  153. */
  154. int vdma_free(unsigned long laddr)
  155. {
  156. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  157. int i;
  158. i = laddr >> 12;
  159. if (pgtbl[i].owner != laddr) {
  160. printk
  161. ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
  162. laddr);
  163. return -1;
  164. }
  165. while (i < VDMA_PGTBL_ENTRIES && pgtbl[i].owner == laddr) {
  166. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  167. i++;
  168. }
  169. if (vdma_debug > 1)
  170. printk("vdma_free: freed %ld pages starting from %08lx\n",
  171. i - (laddr >> 12), laddr);
  172. return 0;
  173. }
  174. /*
  175. * Map certain page(s) to another physical address.
  176. * Caller must have allocated the page(s) before.
  177. */
  178. int vdma_remap(unsigned long laddr, unsigned long paddr,
  179. unsigned long size)
  180. {
  181. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  182. int first, pages, npages;
  183. if (laddr > 0xffffff) {
  184. if (vdma_debug)
  185. printk("vdma_map: Invalid logical address: %08lx\n",
  186. laddr);
  187. return -EINVAL; /* invalid logical address */
  188. }
  189. if (paddr > 0x1fffffff) {
  190. if (vdma_debug)
  191. printk("vdma_map: Invalid physical address: %08lx\n",
  192. paddr);
  193. return -EINVAL; /* invalid physical address */
  194. }
  195. npages = pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  196. first = laddr >> 12;
  197. if (vdma_debug)
  198. printk("vdma_remap: first=%x, pages=%x\n", first, pages);
  199. if (first + pages > VDMA_PGTBL_ENTRIES) {
  200. if (vdma_debug)
  201. printk("vdma_alloc: Invalid size: %08lx\n", size);
  202. return -EINVAL;
  203. }
  204. paddr &= ~(VDMA_PAGESIZE - 1);
  205. while (pages > 0 && first < VDMA_PGTBL_ENTRIES) {
  206. if (pgtbl[first].owner != laddr) {
  207. if (vdma_debug)
  208. printk("Trying to remap other's pages.\n");
  209. return -EPERM; /* not owner */
  210. }
  211. pgtbl[first].frame = paddr;
  212. paddr += VDMA_PAGESIZE;
  213. first++;
  214. pages--;
  215. }
  216. /*
  217. * Update translation table
  218. */
  219. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  220. if (vdma_debug > 2) {
  221. int i;
  222. pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  223. first = laddr >> 12;
  224. printk("LADDR: ");
  225. for (i = first; i < first + pages; i++)
  226. printk("%08x ", i << 12);
  227. printk("\nPADDR: ");
  228. for (i = first; i < first + pages; i++)
  229. printk("%08x ", pgtbl[i].frame);
  230. printk("\nOWNER: ");
  231. for (i = first; i < first + pages; i++)
  232. printk("%08x ", pgtbl[i].owner);
  233. printk("\n");
  234. }
  235. return 0;
  236. }
  237. /*
  238. * Translate a physical address to a logical address.
  239. * This will return the logical address of the first
  240. * match.
  241. */
  242. unsigned long vdma_phys2log(unsigned long paddr)
  243. {
  244. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  245. int frame, i;
  246. frame = paddr & ~(VDMA_PAGESIZE - 1);
  247. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  248. if (pgtbl[i].frame == frame)
  249. break;
  250. }
  251. if (i == VDMA_PGTBL_ENTRIES)
  252. return ~0UL;
  253. return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
  254. }
  255. /*
  256. * Translate a logical DMA address to a physical address
  257. */
  258. unsigned long vdma_log2phys(unsigned long laddr)
  259. {
  260. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  261. return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
  262. }
  263. /*
  264. * Print DMA statistics
  265. */
  266. void vdma_stats(void)
  267. {
  268. int i;
  269. printk("vdma_stats: CONFIG: %08x\n",
  270. r4030_read_reg32(JAZZ_R4030_CONFIG));
  271. printk("R4030 translation table base: %08x\n",
  272. r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
  273. printk("R4030 translation table limit: %08x\n",
  274. r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
  275. printk("vdma_stats: INV_ADDR: %08x\n",
  276. r4030_read_reg32(JAZZ_R4030_INV_ADDR));
  277. printk("vdma_stats: R_FAIL_ADDR: %08x\n",
  278. r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
  279. printk("vdma_stats: M_FAIL_ADDR: %08x\n",
  280. r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
  281. printk("vdma_stats: IRQ_SOURCE: %08x\n",
  282. r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
  283. printk("vdma_stats: I386_ERROR: %08x\n",
  284. r4030_read_reg32(JAZZ_R4030_I386_ERROR));
  285. printk("vdma_chnl_modes: ");
  286. for (i = 0; i < 8; i++)
  287. printk("%04x ",
  288. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  289. (i << 5)));
  290. printk("\n");
  291. printk("vdma_chnl_enables: ");
  292. for (i = 0; i < 8; i++)
  293. printk("%04x ", r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  294. (i << 5)));
  295. printk("\n");
  296. }
  297. /*
  298. * DMA transfer functions
  299. */
  300. /*
  301. * Enable a DMA channel. Also clear any error conditions.
  302. */
  303. void vdma_enable(int channel)
  304. {
  305. int status;
  306. if (vdma_debug)
  307. printk("vdma_enable: channel %d\n", channel);
  308. /*
  309. * Check error conditions first
  310. */
  311. status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  312. if (status & 0x400)
  313. printk("VDMA: Channel %d: Address error!\n", channel);
  314. if (status & 0x200)
  315. printk("VDMA: Channel %d: Memory error!\n", channel);
  316. /*
  317. * Clear all interrupt flags
  318. */
  319. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  320. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  321. (channel << 5)) | R4030_TC_INTR
  322. | R4030_MEM_INTR | R4030_ADDR_INTR);
  323. /*
  324. * Enable the desired channel
  325. */
  326. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  327. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  328. (channel << 5)) |
  329. R4030_CHNL_ENABLE);
  330. }
  331. /*
  332. * Disable a DMA channel
  333. */
  334. void vdma_disable(int channel)
  335. {
  336. if (vdma_debug) {
  337. int status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  338. (channel << 5));
  339. printk("vdma_disable: channel %d\n", channel);
  340. printk("VDMA: channel %d status: %04x (%s) mode: "
  341. "%02x addr: %06x count: %06x\n",
  342. channel, status,
  343. ((status & 0x600) ? "ERROR" : "OK"),
  344. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  345. (channel << 5)),
  346. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
  347. (channel << 5)),
  348. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
  349. (channel << 5)));
  350. }
  351. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  352. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  353. (channel << 5)) &
  354. ~R4030_CHNL_ENABLE);
  355. /*
  356. * After disabling a DMA channel a remote bus register should be
  357. * read to ensure that the current DMA acknowledge cycle is completed.
  358. */
  359. *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
  360. }
  361. /*
  362. * Set DMA mode. This function accepts the mode values used
  363. * to set a PC-style DMA controller. For the SCSI and FDC
  364. * channels, we also set the default modes each time we're
  365. * called.
  366. * NOTE: The FAST and BURST dma modes are supported by the
  367. * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
  368. * for now.
  369. */
  370. void vdma_set_mode(int channel, int mode)
  371. {
  372. if (vdma_debug)
  373. printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
  374. mode);
  375. switch (channel) {
  376. case JAZZ_SCSI_DMA: /* scsi */
  377. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  378. /* R4030_MODE_FAST | */
  379. /* R4030_MODE_BURST | */
  380. R4030_MODE_INTR_EN |
  381. R4030_MODE_WIDTH_16 |
  382. R4030_MODE_ATIME_80);
  383. break;
  384. case JAZZ_FLOPPY_DMA: /* floppy */
  385. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  386. /* R4030_MODE_FAST | */
  387. /* R4030_MODE_BURST | */
  388. R4030_MODE_INTR_EN |
  389. R4030_MODE_WIDTH_8 |
  390. R4030_MODE_ATIME_120);
  391. break;
  392. case JAZZ_AUDIOL_DMA:
  393. case JAZZ_AUDIOR_DMA:
  394. printk("VDMA: Audio DMA not supported yet.\n");
  395. break;
  396. default:
  397. printk
  398. ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
  399. channel);
  400. }
  401. switch (mode) {
  402. case DMA_MODE_READ:
  403. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  404. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  405. (channel << 5)) &
  406. ~R4030_CHNL_WRITE);
  407. break;
  408. case DMA_MODE_WRITE:
  409. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  410. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  411. (channel << 5)) |
  412. R4030_CHNL_WRITE);
  413. break;
  414. default:
  415. printk
  416. ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
  417. mode);
  418. }
  419. }
  420. /*
  421. * Set Transfer Address
  422. */
  423. void vdma_set_addr(int channel, long addr)
  424. {
  425. if (vdma_debug)
  426. printk("vdma_set_addr: channel %d, addr %lx\n", channel,
  427. addr);
  428. r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
  429. }
  430. /*
  431. * Set Transfer Count
  432. */
  433. void vdma_set_count(int channel, int count)
  434. {
  435. if (vdma_debug)
  436. printk("vdma_set_count: channel %d, count %08x\n", channel,
  437. (unsigned) count);
  438. r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
  439. }
  440. /*
  441. * Get Residual
  442. */
  443. int vdma_get_residue(int channel)
  444. {
  445. int residual;
  446. residual =
  447. r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
  448. if (vdma_debug)
  449. printk("vdma_get_residual: channel %d: residual=%d\n",
  450. channel, residual);
  451. return residual;
  452. }
  453. /*
  454. * Get DMA channel enable register
  455. */
  456. int vdma_get_enable(int channel)
  457. {
  458. int enable;
  459. enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  460. if (vdma_debug)
  461. printk("vdma_get_enable: channel %d: enable=%d\n", channel,
  462. enable);
  463. return enable;
  464. }