PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/mips/jazz/jazzdma.c

https://github.com/fzqing/linux-2.6
C | 565 lines | 365 code | 85 blank | 115 comment | 63 complexity | 38fd8bd4942fae330620e57fae0e0263 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, CC-BY-SA-3.0
  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 unsigned long vdma_pagetable_start;
  29. static spinlock_t vdma_lock = SPIN_LOCK_UNLOCKED;
  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. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  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. void __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. vdma_pagetable_start = alloc_bootmem_low_pages(VDMA_PGTBL_SIZE);
  63. if (!vdma_pagetable_start)
  64. BUG();
  65. dma_cache_wback_inv(vdma_pagetable_start, VDMA_PGTBL_SIZE);
  66. vdma_pagetable_start = KSEG1ADDR(vdma_pagetable_start);
  67. /*
  68. * Clear the R4030 translation table
  69. */
  70. vdma_pgtbl_init();
  71. r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,
  72. CPHYSADDR(vdma_pagetable_start));
  73. r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
  74. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  75. printk("VDMA: R4030 DMA pagetables initialized.\n");
  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. VDMA_PGTBL_ENTRY *entry = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  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 = (size + 4095) >> 12; /* no. of pages to allocate */
  102. first = 0;
  103. while (1) {
  104. while (entry[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 (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(&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. VDMA_PGTBL_ENTRY *pgtbl = (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  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 (pgtbl[i].owner == laddr && i < VDMA_PGTBL_ENTRIES) {
  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. VDMA_PGTBL_ENTRY *pgtbl =
  183. (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  184. int first, pages, npages;
  185. if (laddr > 0xffffff) {
  186. if (vdma_debug)
  187. printk
  188. ("vdma_map: Invalid logical address: %08lx\n",
  189. laddr);
  190. return -EINVAL; /* invalid logical address */
  191. }
  192. if (paddr > 0x1fffffff) {
  193. if (vdma_debug)
  194. printk
  195. ("vdma_map: Invalid physical address: %08lx\n",
  196. paddr);
  197. return -EINVAL; /* invalid physical address */
  198. }
  199. npages = pages =
  200. (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  201. first = laddr >> 12;
  202. if (vdma_debug)
  203. printk("vdma_remap: first=%x, pages=%x\n", first, pages);
  204. if (first + pages > VDMA_PGTBL_ENTRIES) {
  205. if (vdma_debug)
  206. printk("vdma_alloc: Invalid size: %08lx\n", size);
  207. return -EINVAL;
  208. }
  209. paddr &= ~(VDMA_PAGESIZE - 1);
  210. while (pages > 0 && first < VDMA_PGTBL_ENTRIES) {
  211. if (pgtbl[first].owner != laddr) {
  212. if (vdma_debug)
  213. printk("Trying to remap other's pages.\n");
  214. return -EPERM; /* not owner */
  215. }
  216. pgtbl[first].frame = paddr;
  217. paddr += VDMA_PAGESIZE;
  218. first++;
  219. pages--;
  220. }
  221. /*
  222. * Update translation table
  223. */
  224. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  225. if (vdma_debug > 2) {
  226. int i;
  227. pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  228. first = laddr >> 12;
  229. printk("LADDR: ");
  230. for (i = first; i < first + pages; i++)
  231. printk("%08x ", i << 12);
  232. printk("\nPADDR: ");
  233. for (i = first; i < first + pages; i++)
  234. printk("%08x ", pgtbl[i].frame);
  235. printk("\nOWNER: ");
  236. for (i = first; i < first + pages; i++)
  237. printk("%08x ", pgtbl[i].owner);
  238. printk("\n");
  239. }
  240. return 0;
  241. }
  242. /*
  243. * Translate a physical address to a logical address.
  244. * This will return the logical address of the first
  245. * match.
  246. */
  247. unsigned long vdma_phys2log(unsigned long paddr)
  248. {
  249. int i;
  250. int frame;
  251. VDMA_PGTBL_ENTRY *pgtbl =
  252. (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  253. frame = paddr & ~(VDMA_PAGESIZE - 1);
  254. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  255. if (pgtbl[i].frame == frame)
  256. break;
  257. }
  258. if (i == VDMA_PGTBL_ENTRIES)
  259. return ~0UL;
  260. return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
  261. }
  262. EXPORT_SYMBOL(vdma_phys2log);
  263. /*
  264. * Translate a logical DMA address to a physical address
  265. */
  266. unsigned long vdma_log2phys(unsigned long laddr)
  267. {
  268. VDMA_PGTBL_ENTRY *pgtbl =
  269. (VDMA_PGTBL_ENTRY *) vdma_pagetable_start;
  270. return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
  271. }
  272. EXPORT_SYMBOL(vdma_log2phys);
  273. /*
  274. * Print DMA statistics
  275. */
  276. void vdma_stats(void)
  277. {
  278. int i;
  279. printk("vdma_stats: CONFIG: %08x\n",
  280. r4030_read_reg32(JAZZ_R4030_CONFIG));
  281. printk("R4030 translation table base: %08x\n",
  282. r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
  283. printk("R4030 translation table limit: %08x\n",
  284. r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
  285. printk("vdma_stats: INV_ADDR: %08x\n",
  286. r4030_read_reg32(JAZZ_R4030_INV_ADDR));
  287. printk("vdma_stats: R_FAIL_ADDR: %08x\n",
  288. r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
  289. printk("vdma_stats: M_FAIL_ADDR: %08x\n",
  290. r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
  291. printk("vdma_stats: IRQ_SOURCE: %08x\n",
  292. r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
  293. printk("vdma_stats: I386_ERROR: %08x\n",
  294. r4030_read_reg32(JAZZ_R4030_I386_ERROR));
  295. printk("vdma_chnl_modes: ");
  296. for (i = 0; i < 8; i++)
  297. printk("%04x ",
  298. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  299. (i << 5)));
  300. printk("\n");
  301. printk("vdma_chnl_enables: ");
  302. for (i = 0; i < 8; i++)
  303. printk("%04x ",
  304. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  305. (i << 5)));
  306. printk("\n");
  307. }
  308. /*
  309. * DMA transfer functions
  310. */
  311. /*
  312. * Enable a DMA channel. Also clear any error conditions.
  313. */
  314. void vdma_enable(int channel)
  315. {
  316. int status;
  317. if (vdma_debug)
  318. printk("vdma_enable: channel %d\n", channel);
  319. /*
  320. * Check error conditions first
  321. */
  322. status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  323. if (status & 0x400)
  324. printk("VDMA: Channel %d: Address error!\n", channel);
  325. if (status & 0x200)
  326. printk("VDMA: Channel %d: Memory error!\n", channel);
  327. /*
  328. * Clear all interrupt flags
  329. */
  330. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  331. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  332. (channel << 5)) | R4030_TC_INTR
  333. | R4030_MEM_INTR | R4030_ADDR_INTR);
  334. /*
  335. * Enable the desired channel
  336. */
  337. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  338. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  339. (channel << 5)) |
  340. R4030_CHNL_ENABLE);
  341. }
  342. EXPORT_SYMBOL(vdma_enable);
  343. /*
  344. * Disable a DMA channel
  345. */
  346. void vdma_disable(int channel)
  347. {
  348. if (vdma_debug) {
  349. int status =
  350. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  351. (channel << 5));
  352. printk("vdma_disable: channel %d\n", channel);
  353. printk("VDMA: channel %d status: %04x (%s) mode: "
  354. "%02x addr: %06x count: %06x\n",
  355. channel, status,
  356. ((status & 0x600) ? "ERROR" : "OK"),
  357. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  358. (channel << 5)),
  359. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
  360. (channel << 5)),
  361. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
  362. (channel << 5)));
  363. }
  364. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  365. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  366. (channel << 5)) &
  367. ~R4030_CHNL_ENABLE);
  368. /*
  369. * After disabling a DMA channel a remote bus register should be
  370. * read to ensure that the current DMA acknowledge cycle is completed.
  371. */
  372. *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
  373. }
  374. EXPORT_SYMBOL(vdma_disable);
  375. /*
  376. * Set DMA mode. This function accepts the mode values used
  377. * to set a PC-style DMA controller. For the SCSI and FDC
  378. * channels, we also set the default modes each time we're
  379. * called.
  380. * NOTE: The FAST and BURST dma modes are supported by the
  381. * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
  382. * for now.
  383. */
  384. void vdma_set_mode(int channel, int mode)
  385. {
  386. if (vdma_debug)
  387. printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
  388. mode);
  389. switch (channel) {
  390. case JAZZ_SCSI_DMA: /* scsi */
  391. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  392. /* R4030_MODE_FAST | */
  393. /* R4030_MODE_BURST | */
  394. R4030_MODE_INTR_EN |
  395. R4030_MODE_WIDTH_16 |
  396. R4030_MODE_ATIME_80);
  397. break;
  398. case JAZZ_FLOPPY_DMA: /* floppy */
  399. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  400. /* R4030_MODE_FAST | */
  401. /* R4030_MODE_BURST | */
  402. R4030_MODE_INTR_EN |
  403. R4030_MODE_WIDTH_8 |
  404. R4030_MODE_ATIME_120);
  405. break;
  406. case JAZZ_AUDIOL_DMA:
  407. case JAZZ_AUDIOR_DMA:
  408. printk("VDMA: Audio DMA not supported yet.\n");
  409. break;
  410. default:
  411. printk
  412. ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
  413. channel);
  414. }
  415. switch (mode) {
  416. case DMA_MODE_READ:
  417. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  418. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  419. (channel << 5)) &
  420. ~R4030_CHNL_WRITE);
  421. break;
  422. case DMA_MODE_WRITE:
  423. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  424. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  425. (channel << 5)) |
  426. R4030_CHNL_WRITE);
  427. break;
  428. default:
  429. printk
  430. ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
  431. mode);
  432. }
  433. }
  434. EXPORT_SYMBOL(vdma_set_mode);
  435. /*
  436. * Set Transfer Address
  437. */
  438. void vdma_set_addr(int channel, long addr)
  439. {
  440. if (vdma_debug)
  441. printk("vdma_set_addr: channel %d, addr %lx\n", channel,
  442. addr);
  443. r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
  444. }
  445. EXPORT_SYMBOL(vdma_set_addr);
  446. /*
  447. * Set Transfer Count
  448. */
  449. void vdma_set_count(int channel, int count)
  450. {
  451. if (vdma_debug)
  452. printk("vdma_set_count: channel %d, count %08x\n", channel,
  453. (unsigned) count);
  454. r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
  455. }
  456. EXPORT_SYMBOL(vdma_set_count);
  457. /*
  458. * Get Residual
  459. */
  460. int vdma_get_residue(int channel)
  461. {
  462. int residual;
  463. residual = r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
  464. if (vdma_debug)
  465. printk("vdma_get_residual: channel %d: residual=%d\n",
  466. channel, residual);
  467. return residual;
  468. }
  469. /*
  470. * Get DMA channel enable register
  471. */
  472. int vdma_get_enable(int channel)
  473. {
  474. int enable;
  475. enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  476. if (vdma_debug)
  477. printk("vdma_get_enable: channel %d: enable=%d\n", channel,
  478. enable);
  479. return enable;
  480. }