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 <linux/gfp.h>
  18. #include <asm/mipsregs.h>
  19. #include <asm/jazz.h>
  20. #include <asm/io.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/dma.h>
  23. #include <asm/jazzdma.h>
  24. #include <asm/pgtable.h>
  25. /*
  26. * Set this to one to enable additional vdma debug code.
  27. */
  28. #define CONF_DEBUG_VDMA 0
  29. static VDMA_PGTBL_ENTRY *pgtbl;
  30. static DEFINE_SPINLOCK(vdma_lock);
  31. /*
  32. * Debug stuff
  33. */
  34. #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
  35. static int debuglvl = 3;
  36. /*
  37. * Initialize the pagetable with a one-to-one mapping of
  38. * the first 16 Mbytes of main memory and declare all
  39. * entries to be unused. Using this method will at least
  40. * allow some early device driver operations to work.
  41. */
  42. static inline void vdma_pgtbl_init(void)
  43. {
  44. unsigned long paddr = 0;
  45. int i;
  46. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  47. pgtbl[i].frame = paddr;
  48. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  49. paddr += VDMA_PAGESIZE;
  50. }
  51. }
  52. /*
  53. * Initialize the Jazz R4030 dma controller
  54. */
  55. static int __init vdma_init(void)
  56. {
  57. /*
  58. * Allocate 32k of memory for DMA page tables. This needs to be page
  59. * aligned and should be uncached to avoid cache flushing after every
  60. * update.
  61. */
  62. pgtbl = (VDMA_PGTBL_ENTRY *)__get_free_pages(GFP_KERNEL | GFP_DMA,
  63. get_order(VDMA_PGTBL_SIZE));
  64. BUG_ON(!pgtbl);
  65. dma_cache_wback_inv((unsigned long)pgtbl, VDMA_PGTBL_SIZE);
  66. pgtbl = (VDMA_PGTBL_ENTRY *)CKSEG1ADDR((unsigned long)pgtbl);
  67. /*
  68. * Clear the R4030 translation table
  69. */
  70. vdma_pgtbl_init();
  71. r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,
  72. CPHYSADDR((unsigned long)pgtbl));
  73. r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
  74. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  75. printk(KERN_INFO "VDMA: R4030 DMA pagetables initialized.\n");
  76. return 0;
  77. }
  78. /*
  79. * Allocate DMA pagetables using a simple first-fit algorithm
  80. */
  81. unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
  82. {
  83. int first, last, pages, frame, i;
  84. unsigned long laddr, flags;
  85. /* check arguments */
  86. if (paddr > 0x1fffffff) {
  87. if (vdma_debug)
  88. printk("vdma_alloc: Invalid physical address: %08lx\n",
  89. paddr);
  90. return VDMA_ERROR; /* invalid physical address */
  91. }
  92. if (size > 0x400000 || size == 0) {
  93. if (vdma_debug)
  94. printk("vdma_alloc: Invalid size: %08lx\n", size);
  95. return VDMA_ERROR; /* invalid physical address */
  96. }
  97. spin_lock_irqsave(&vdma_lock, flags);
  98. /*
  99. * Find free chunk
  100. */
  101. pages = VDMA_PAGE(paddr + size) - VDMA_PAGE(paddr) + 1;
  102. first = 0;
  103. while (1) {
  104. while (pgtbl[first].owner != VDMA_PAGE_EMPTY &&
  105. first < VDMA_PGTBL_ENTRIES) first++;
  106. if (first + pages > VDMA_PGTBL_ENTRIES) { /* nothing free */
  107. spin_unlock_irqrestore(&vdma_lock, flags);
  108. return VDMA_ERROR;
  109. }
  110. last = first + 1;
  111. while (pgtbl[last].owner == VDMA_PAGE_EMPTY
  112. && last - first < pages)
  113. last++;
  114. if (last - first == pages)
  115. break; /* found */
  116. first = last + 1;
  117. }
  118. /*
  119. * Mark pages as allocated
  120. */
  121. laddr = (first << 12) + (paddr & (VDMA_PAGESIZE - 1));
  122. frame = paddr & ~(VDMA_PAGESIZE - 1);
  123. for (i = first; i < last; i++) {
  124. pgtbl[i].frame = frame;
  125. pgtbl[i].owner = laddr;
  126. frame += VDMA_PAGESIZE;
  127. }
  128. /*
  129. * Update translation table and return logical start address
  130. */
  131. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  132. if (vdma_debug > 1)
  133. printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
  134. pages, laddr);
  135. if (vdma_debug > 2) {
  136. printk("LADDR: ");
  137. for (i = first; i < last; i++)
  138. printk("%08x ", i << 12);
  139. printk("\nPADDR: ");
  140. for (i = first; i < last; i++)
  141. printk("%08x ", pgtbl[i].frame);
  142. printk("\nOWNER: ");
  143. for (i = first; i < last; i++)
  144. printk("%08x ", pgtbl[i].owner);
  145. printk("\n");
  146. }
  147. spin_unlock_irqrestore(&vdma_lock, flags);
  148. return laddr;
  149. }
  150. EXPORT_SYMBOL(vdma_alloc);
  151. /*
  152. * Free previously allocated dma translation pages
  153. * Note that this does NOT change the translation table,
  154. * it just marks the free'd pages as unused!
  155. */
  156. int vdma_free(unsigned long laddr)
  157. {
  158. int i;
  159. i = laddr >> 12;
  160. if (pgtbl[i].owner != laddr) {
  161. printk
  162. ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
  163. laddr);
  164. return -1;
  165. }
  166. while (i < VDMA_PGTBL_ENTRIES && pgtbl[i].owner == laddr) {
  167. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  168. i++;
  169. }
  170. if (vdma_debug > 1)
  171. printk("vdma_free: freed %ld pages starting from %08lx\n",
  172. i - (laddr >> 12), laddr);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(vdma_free);
  176. /*
  177. * Map certain page(s) to another physical address.
  178. * Caller must have allocated the page(s) before.
  179. */
  180. int vdma_remap(unsigned long laddr, unsigned long paddr, unsigned long size)
  181. {
  182. int first, pages;
  183. if (laddr > 0xffffff) {
  184. if (vdma_debug)
  185. printk
  186. ("vdma_map: Invalid logical address: %08lx\n",
  187. laddr);
  188. return -EINVAL; /* invalid logical address */
  189. }
  190. if (paddr > 0x1fffffff) {
  191. if (vdma_debug)
  192. printk
  193. ("vdma_map: Invalid physical address: %08lx\n",
  194. paddr);
  195. return -EINVAL; /* invalid physical address */
  196. }
  197. pages = (((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);