jazzdma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/mm.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/spinlock.h>
  17. #include <asm/mipsregs.h>
  18. #include <asm/jazz.h>
  19. #include <asm/io.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/dma.h>
  22. #include <asm/jazzdma.h>
  23. #include <asm/pgtable.h>
  24. /*
  25. * Set this to one to enable additional vdma debug code.
  26. */
  27. #define CONF_DEBUG_VDMA 0
  28. static VDMA_PGTBL_ENTRY *pgtbl;
  29. static DEFINE_SPINLOCK(vdma_lock);
  30. /*
  31. * Debug stuff
  32. */
  33. #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
  34. static int debuglvl = 3;
  35. /*
  36. * Initialize the pagetable with a one-to-one mapping of
  37. * the first 16 Mbytes of main memory and declare all
  38. * entries to be unused. Using this method will at least
  39. * allow some early device driver operations to work.
  40. */
  41. static inline void vdma_pgtbl_init(void)
  42. {
  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. static int __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. pgtbl = (VDMA_PGTBL_ENTRY *)__get_free_pages(GFP_KERNEL | GFP_DMA,
  62. get_order(VDMA_PGTBL_SIZE));
  63. if (!pgtbl)
  64. BUG();
  65. dma_cache_wback_inv((unsigned long)pgtbl, VDMA_PGTBL_SIZE);
  66. pgtbl = (VDMA_PGTBL_ENTRY *)KSEG1ADDR(pgtbl);
  67. /*
  68. * Clear the R4030 translation table
  69. */
  70. vdma_pgtbl_init();
  71. r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE, CPHYSADDR(pgtbl));
  72. r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
  73. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  74. printk(KERN_INFO "VDMA: R4030 DMA pagetables initialized.\n");
  75. return 0;
  76. }
  77. /*
  78. * Allocate DMA pagetables using a simple first-fit algorithm
  79. */
  80. unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
  81. {
  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(&vdma_lock, flags);
  97. /*
  98. * Find free chunk
  99. */
  100. pages = VDMA_PAGE(paddr + size) - VDMA_PAGE(paddr) + 1;
  101. first = 0;
  102. while (1) {
  103. while (pgtbl[first].owner != VDMA_PAGE_EMPTY &&
  104. first < VDMA_PGTBL_ENTRIES) first++;
  105. if (first + pages > VDMA_PGTBL_ENTRIES) { /* nothing free */
  106. spin_unlock_irqrestore(&vdma_lock, flags);
  107. return VDMA_ERROR;
  108. }
  109. last = first + 1;
  110. while (pgtbl[last].owner == VDMA_PAGE_EMPTY
  111. && last - first < pages)
  112. last++;
  113. if (last - first == pages)
  114. break; /* found */
  115. first = last + 1;
  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. pgtbl[i].frame = frame;
  124. pgtbl[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 ", pgtbl[i].frame);
  141. printk("\nOWNER: ");
  142. for (i = first; i < last; i++)
  143. printk("%08x ", pgtbl[i].owner);
  144. printk("\n");
  145. }
  146. spin_unlock_irqrestore(&vdma_lock, flags);
  147. return laddr;
  148. }
  149. EXPORT_SYMBOL(vdma_alloc);
  150. /*
  151. * Free previously allocated dma translation pages
  152. * Note that this does NOT change the translation table,
  153. * it just marks the free'd pages as unused!
  154. */
  155. int vdma_free(unsigned long laddr)
  156. {
  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 (pgtbl[i].owner == laddr && i < VDMA_PGTBL_ENTRIES) {
  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. EXPORT_SYMBOL(vdma_free);
  175. /*
  176. * Map certain page(s) to another physical address.
  177. * Caller must have allocated the page(s) before.
  178. */
  179. int vdma_remap(unsigned long laddr, unsigned long paddr, unsigned long size)
  180. {
  181. int first, pages, npages;
  182. if (laddr > 0xffffff) {
  183. if (vdma_debug)
  184. printk
  185. ("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
  192. ("vdma_map: Invalid physical address: %08lx\n",
  193. paddr);
  194. return -EINVAL; /* invalid physical address */
  195. }
  196. npages = pages =
  197. (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  198. first = laddr >> 12;
  199. if (vdma_debug)
  200. printk("vdma_remap: first=%x, pages=%x\n", first, pages);
  201. if (first + pages > VDMA_PGTBL_ENTRIES) {
  202. if (vdma_debug)
  203. printk("vdma_alloc: Invalid size: %08lx\n", size);
  204. return -EINVAL;
  205. }
  206. paddr &= ~(VDMA_PAGESIZE - 1);
  207. while (pages > 0 && first < VDMA_PGTBL_ENTRIES) {
  208. if (pgtbl[first].owner != laddr) {
  209. if (vdma_debug)
  210. printk("Trying to remap other's pages.\n");
  211. return -EPERM; /* not owner */
  212. }
  213. pgtbl[first].frame = paddr;
  214. paddr += VDMA_PAGESIZE;
  215. first++;
  216. pages--;
  217. }
  218. /*
  219. * Update translation table
  220. */
  221. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  222. if (vdma_debug > 2) {
  223. int i;
  224. pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  225. first = laddr >> 12;
  226. printk("LADDR: ");
  227. for (i = first; i < first + pages; i++)
  228. printk("%08x ", i << 12);
  229. printk("\nPADDR: ");
  230. for (i = first; i < first + pages; i++)
  231. printk("%08x ", pgtbl[i].frame);
  232. printk("\nOWNER: ");
  233. for (i = first; i < first + pages; i++)
  234. printk("%08x ", pgtbl[i].owner);
  235. printk("\n");
  236. }
  237. return 0;
  238. }
  239. /*
  240. * Translate a physical address to a logical address.
  241. * This will return the logical address of the first
  242. * match.
  243. */
  244. unsigned long vdma_phys2log(unsigned long paddr)
  245. {
  246. int i;
  247. int frame;
  248. frame = paddr & ~(VDMA_PAGESIZE - 1);
  249. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  250. if (pgtbl[i].frame == frame)
  251. break;
  252. }
  253. if (i == VDMA_PGTBL_ENTRIES)
  254. return ~0UL;
  255. return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
  256. }
  257. EXPORT_SYMBOL(vdma_phys2log);
  258. /*
  259. * Translate a logical DMA address to a physical address
  260. */
  261. unsigned long vdma_log2phys(unsigned long laddr)
  262. {
  263. return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
  264. }
  265. EXPORT_SYMBOL(vdma_log2phys);
  266. /*
  267. * Print DMA statistics
  268. */
  269. void vdma_stats(void)
  270. {
  271. int i;
  272. printk("vdma_stats: CONFIG: %08x\n",
  273. r4030_read_reg32(JAZZ_R4030_CONFIG));
  274. printk("R4030 translation table base: %08x\n",
  275. r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
  276. printk("R4030 translation table limit: %08x\n",
  277. r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
  278. printk("vdma_stats: INV_ADDR: %08x\n",
  279. r4030_read_reg32(JAZZ_R4030_INV_ADDR));
  280. printk("vdma_stats: R_FAIL_ADDR: %08x\n",
  281. r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
  282. printk("vdma_stats: M_FAIL_ADDR: %08x\n",
  283. r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
  284. printk("vdma_stats: IRQ_SOURCE: %08x\n",
  285. r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
  286. printk("vdma_stats: I386_ERROR: %08x\n",
  287. r4030_read_reg32(JAZZ_R4030_I386_ERROR));
  288. printk("vdma_chnl_modes: ");
  289. for (i = 0; i < 8; i++)
  290. printk("%04x ",
  291. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  292. (i << 5)));
  293. printk("\n");
  294. printk("vdma_chnl_enables: ");
  295. for (i = 0; i < 8; i++)
  296. printk("%04x ",
  297. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  298. (i << 5)));
  299. printk("\n");
  300. }
  301. /*
  302. * DMA transfer functions
  303. */
  304. /*
  305. * Enable a DMA channel. Also clear any error conditions.
  306. */
  307. void vdma_enable(int channel)
  308. {
  309. int status;
  310. if (vdma_debug)
  311. printk("vdma_enable: channel %d\n", channel);
  312. /*
  313. * Check error conditions first
  314. */
  315. status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  316. if (status & 0x400)
  317. printk("VDMA: Channel %d: Address error!\n", channel);
  318. if (status & 0x200)
  319. printk("VDMA: Channel %d: Memory error!\n", channel);
  320. /*
  321. * Clear all interrupt flags
  322. */
  323. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  324. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  325. (channel << 5)) | R4030_TC_INTR
  326. | R4030_MEM_INTR | R4030_ADDR_INTR);
  327. /*
  328. * Enable the desired channel
  329. */
  330. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  331. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  332. (channel << 5)) |
  333. R4030_CHNL_ENABLE);
  334. }
  335. EXPORT_SYMBOL(vdma_enable);
  336. /*
  337. * Disable a DMA channel
  338. */
  339. void vdma_disable(int channel)
  340. {
  341. if (vdma_debug) {
  342. int status =
  343. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  344. (channel << 5));
  345. printk("vdma_disable: channel %d\n", channel);
  346. printk("VDMA: channel %d status: %04x (%s) mode: "
  347. "%02x addr: %06x count: %06x\n",
  348. channel, status,
  349. ((status & 0x600) ? "ERROR" : "OK"),
  350. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  351. (channel << 5)),
  352. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
  353. (channel << 5)),
  354. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
  355. (channel << 5)));
  356. }
  357. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  358. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  359. (channel << 5)) &
  360. ~R4030_CHNL_ENABLE);
  361. /*
  362. * After disabling a DMA channel a remote bus register should be
  363. * read to ensure that the current DMA acknowledge cycle is completed.
  364. */
  365. *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
  366. }
  367. EXPORT_SYMBOL(vdma_disable);
  368. /*
  369. * Set DMA mode. This function accepts the mode values used
  370. * to set a PC-style DMA controller. For the SCSI and FDC
  371. * channels, we also set the default modes each time we're
  372. * called.
  373. * NOTE: The FAST and BURST dma modes are supported by the
  374. * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
  375. * for now.
  376. */
  377. void vdma_set_mode(int channel, int mode)
  378. {
  379. if (vdma_debug)
  380. printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
  381. mode);
  382. switch (channel) {
  383. case JAZZ_SCSI_DMA: /* scsi */
  384. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  385. /* R4030_MODE_FAST | */
  386. /* R4030_MODE_BURST | */
  387. R4030_MODE_INTR_EN |
  388. R4030_MODE_WIDTH_16 |
  389. R4030_MODE_ATIME_80);
  390. break;
  391. case JAZZ_FLOPPY_DMA: /* floppy */
  392. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  393. /* R4030_MODE_FAST | */
  394. /* R4030_MODE_BURST | */
  395. R4030_MODE_INTR_EN |
  396. R4030_MODE_WIDTH_8 |
  397. R4030_MODE_ATIME_120);
  398. break;
  399. case JAZZ_AUDIOL_DMA:
  400. case JAZZ_AUDIOR_DMA:
  401. printk("VDMA: Audio DMA not supported yet.\n");
  402. break;
  403. default:
  404. printk
  405. ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
  406. channel);
  407. }
  408. switch (mode) {
  409. case DMA_MODE_READ:
  410. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  411. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  412. (channel << 5)) &
  413. ~R4030_CHNL_WRITE);
  414. break;
  415. case DMA_MODE_WRITE:
  416. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  417. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  418. (channel << 5)) |
  419. R4030_CHNL_WRITE);
  420. break;
  421. default:
  422. printk
  423. ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
  424. mode);
  425. }
  426. }
  427. EXPORT_SYMBOL(vdma_set_mode);
  428. /*
  429. * Set Transfer Address
  430. */
  431. void vdma_set_addr(int channel, long addr)
  432. {
  433. if (vdma_debug)
  434. printk("vdma_set_addr: channel %d, addr %lx\n", channel,
  435. addr);
  436. r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
  437. }
  438. EXPORT_SYMBOL(vdma_set_addr);
  439. /*
  440. * Set Transfer Count
  441. */
  442. void vdma_set_count(int channel, int count)
  443. {
  444. if (vdma_debug)
  445. printk("vdma_set_count: channel %d, count %08x\n", channel,
  446. (unsigned) count);
  447. r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
  448. }
  449. EXPORT_SYMBOL(vdma_set_count);
  450. /*
  451. * Get Residual
  452. */
  453. int vdma_get_residue(int channel)
  454. {
  455. int residual;
  456. residual = r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
  457. if (vdma_debug)
  458. printk("vdma_get_residual: channel %d: residual=%d\n",
  459. channel, residual);
  460. return residual;
  461. }
  462. /*
  463. * Get DMA channel enable register
  464. */
  465. int vdma_get_enable(int channel)
  466. {
  467. int enable;
  468. enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  469. if (vdma_debug)
  470. printk("vdma_get_enable: channel %d: enable=%d\n", channel,
  471. enable);
  472. return enable;
  473. }
  474. arch_initcall(vdma_init);