1 // SPDX-License-Identifier: GPL-2.0
3 * Mips Jazz DMA controller support
4 * Copyright (C) 1995, 1996 by Andreas Busse
6 * NOTE: Some of the argument checking could be removed when
7 * things have settled down. Also, instead of returning 0xffffffff
8 * on failure of vdma_alloc() one could leave page #0 unused
9 * and return the more usual NULL pointer as logical address.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/export.h>
14 #include <linux/errno.h>
16 #include <linux/memblock.h>
17 #include <linux/spinlock.h>
18 #include <linux/gfp.h>
19 #include <linux/dma-map-ops.h>
20 #include <asm/mipsregs.h>
23 #include <linux/uaccess.h>
25 #include <asm/jazzdma.h>
28 * Set this to one to enable additional vdma debug code.
30 #define CONF_DEBUG_VDMA 0
32 static VDMA_PGTBL_ENTRY *pgtbl;
34 static DEFINE_SPINLOCK(vdma_lock);
39 #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
41 static int debuglvl = 3;
44 * Initialize the pagetable with a one-to-one mapping of
45 * the first 16 Mbytes of main memory and declare all
46 * entries to be unused. Using this method will at least
47 * allow some early device driver operations to work.
49 static inline void vdma_pgtbl_init(void)
51 unsigned long paddr = 0;
54 for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
55 pgtbl[i].frame = paddr;
56 pgtbl[i].owner = VDMA_PAGE_EMPTY;
57 paddr += VDMA_PAGESIZE;
62 * Initialize the Jazz R4030 dma controller
64 static int __init vdma_init(void)
67 * Allocate 32k of memory for DMA page tables. This needs to be page
68 * aligned and should be uncached to avoid cache flushing after every
71 pgtbl = (VDMA_PGTBL_ENTRY *)__get_free_pages(GFP_KERNEL | GFP_DMA,
72 get_order(VDMA_PGTBL_SIZE));
74 dma_cache_wback_inv((unsigned long)pgtbl, VDMA_PGTBL_SIZE);
75 pgtbl = (VDMA_PGTBL_ENTRY *)CKSEG1ADDR((unsigned long)pgtbl);
78 * Clear the R4030 translation table
82 r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,
83 CPHYSADDR((unsigned long)pgtbl));
84 r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
85 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
87 printk(KERN_INFO "VDMA: R4030 DMA pagetables initialized.\n");
90 arch_initcall(vdma_init);
93 * Allocate DMA pagetables using a simple first-fit algorithm
95 unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
97 int first, last, pages, frame, i;
98 unsigned long laddr, flags;
100 /* check arguments */
102 if (paddr > 0x1fffffff) {
104 printk("vdma_alloc: Invalid physical address: %08lx\n",
106 return DMA_MAPPING_ERROR; /* invalid physical address */
108 if (size > 0x400000 || size == 0) {
110 printk("vdma_alloc: Invalid size: %08lx\n", size);
111 return DMA_MAPPING_ERROR; /* invalid physical address */
114 spin_lock_irqsave(&vdma_lock, flags);
118 pages = VDMA_PAGE(paddr + size) - VDMA_PAGE(paddr) + 1;
121 while (pgtbl[first].owner != VDMA_PAGE_EMPTY &&
122 first < VDMA_PGTBL_ENTRIES) first++;
123 if (first + pages > VDMA_PGTBL_ENTRIES) { /* nothing free */
124 spin_unlock_irqrestore(&vdma_lock, flags);
125 return DMA_MAPPING_ERROR;
129 while (pgtbl[last].owner == VDMA_PAGE_EMPTY
130 && last - first < pages)
133 if (last - first == pages)
139 * Mark pages as allocated
141 laddr = (first << 12) + (paddr & (VDMA_PAGESIZE - 1));
142 frame = paddr & ~(VDMA_PAGESIZE - 1);
144 for (i = first; i < last; i++) {
145 pgtbl[i].frame = frame;
146 pgtbl[i].owner = laddr;
147 frame += VDMA_PAGESIZE;
151 * Update translation table and return logical start address
153 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
156 printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
159 if (vdma_debug > 2) {
161 for (i = first; i < last; i++)
162 printk("%08x ", i << 12);
164 for (i = first; i < last; i++)
165 printk("%08x ", pgtbl[i].frame);
167 for (i = first; i < last; i++)
168 printk("%08x ", pgtbl[i].owner);
172 spin_unlock_irqrestore(&vdma_lock, flags);
177 EXPORT_SYMBOL(vdma_alloc);
180 * Free previously allocated dma translation pages
181 * Note that this does NOT change the translation table,
182 * it just marks the free'd pages as unused!
184 int vdma_free(unsigned long laddr)
190 if (pgtbl[i].owner != laddr) {
192 ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
197 while (i < VDMA_PGTBL_ENTRIES && pgtbl[i].owner == laddr) {
198 pgtbl[i].owner = VDMA_PAGE_EMPTY;
203 printk("vdma_free: freed %ld pages starting from %08lx\n",
204 i - (laddr >> 12), laddr);
209 EXPORT_SYMBOL(vdma_free);
212 * Translate a physical address to a logical address.
213 * This will return the logical address of the first
216 unsigned long vdma_phys2log(unsigned long paddr)
221 frame = paddr & ~(VDMA_PAGESIZE - 1);
223 for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
224 if (pgtbl[i].frame == frame)
228 if (i == VDMA_PGTBL_ENTRIES)
231 return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
234 EXPORT_SYMBOL(vdma_phys2log);
237 * Translate a logical DMA address to a physical address
239 unsigned long vdma_log2phys(unsigned long laddr)
241 return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
244 EXPORT_SYMBOL(vdma_log2phys);
247 * Print DMA statistics
249 void vdma_stats(void)
253 printk("vdma_stats: CONFIG: %08x\n",
254 r4030_read_reg32(JAZZ_R4030_CONFIG));
255 printk("R4030 translation table base: %08x\n",
256 r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
257 printk("R4030 translation table limit: %08x\n",
258 r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
259 printk("vdma_stats: INV_ADDR: %08x\n",
260 r4030_read_reg32(JAZZ_R4030_INV_ADDR));
261 printk("vdma_stats: R_FAIL_ADDR: %08x\n",
262 r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
263 printk("vdma_stats: M_FAIL_ADDR: %08x\n",
264 r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
265 printk("vdma_stats: IRQ_SOURCE: %08x\n",
266 r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
267 printk("vdma_stats: I386_ERROR: %08x\n",
268 r4030_read_reg32(JAZZ_R4030_I386_ERROR));
269 printk("vdma_chnl_modes: ");
270 for (i = 0; i < 8; i++)
272 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
275 printk("vdma_chnl_enables: ");
276 for (i = 0; i < 8; i++)
278 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
284 * DMA transfer functions
288 * Enable a DMA channel. Also clear any error conditions.
290 void vdma_enable(int channel)
295 printk("vdma_enable: channel %d\n", channel);
298 * Check error conditions first
300 status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
302 printk("VDMA: Channel %d: Address error!\n", channel);
304 printk("VDMA: Channel %d: Memory error!\n", channel);
307 * Clear all interrupt flags
309 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
310 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
311 (channel << 5)) | R4030_TC_INTR
312 | R4030_MEM_INTR | R4030_ADDR_INTR);
315 * Enable the desired channel
317 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
318 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
323 EXPORT_SYMBOL(vdma_enable);
326 * Disable a DMA channel
328 void vdma_disable(int channel)
332 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
335 printk("vdma_disable: channel %d\n", channel);
336 printk("VDMA: channel %d status: %04x (%s) mode: "
337 "%02x addr: %06x count: %06x\n",
339 ((status & 0x600) ? "ERROR" : "OK"),
340 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
342 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
344 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
348 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
349 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
354 * After disabling a DMA channel a remote bus register should be
355 * read to ensure that the current DMA acknowledge cycle is completed.
357 *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
360 EXPORT_SYMBOL(vdma_disable);
363 * Set DMA mode. This function accepts the mode values used
364 * to set a PC-style DMA controller. For the SCSI and FDC
365 * channels, we also set the default modes each time we're
367 * NOTE: The FAST and BURST dma modes are supported by the
368 * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
371 void vdma_set_mode(int channel, int mode)
374 printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
378 case JAZZ_SCSI_DMA: /* scsi */
379 r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
380 /* R4030_MODE_FAST | */
381 /* R4030_MODE_BURST | */
383 R4030_MODE_WIDTH_16 |
384 R4030_MODE_ATIME_80);
387 case JAZZ_FLOPPY_DMA: /* floppy */
388 r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
389 /* R4030_MODE_FAST | */
390 /* R4030_MODE_BURST | */
393 R4030_MODE_ATIME_120);
396 case JAZZ_AUDIOL_DMA:
397 case JAZZ_AUDIOR_DMA:
398 printk("VDMA: Audio DMA not supported yet.\n");
403 ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
409 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
410 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
416 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
417 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
424 ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
429 EXPORT_SYMBOL(vdma_set_mode);
432 * Set Transfer Address
434 void vdma_set_addr(int channel, long addr)
437 printk("vdma_set_addr: channel %d, addr %lx\n", channel,
440 r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
443 EXPORT_SYMBOL(vdma_set_addr);
448 void vdma_set_count(int channel, int count)
451 printk("vdma_set_count: channel %d, count %08x\n", channel,
454 r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
457 EXPORT_SYMBOL(vdma_set_count);
462 int vdma_get_residue(int channel)
466 residual = r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
469 printk("vdma_get_residual: channel %d: residual=%d\n",
476 * Get DMA channel enable register
478 int vdma_get_enable(int channel)
482 enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
485 printk("vdma_get_enable: channel %d: enable=%d\n", channel,
491 static void *jazz_dma_alloc(struct device *dev, size_t size,
492 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
497 if (attrs & DMA_ATTR_NO_WARN)
500 size = PAGE_ALIGN(size);
501 page = alloc_pages(gfp, get_order(size));
504 ret = page_address(page);
505 memset(ret, 0, size);
506 *dma_handle = vdma_alloc(virt_to_phys(ret), size);
507 if (*dma_handle == DMA_MAPPING_ERROR)
509 arch_dma_prep_coherent(page, size);
510 return (void *)(UNCAC_BASE + __pa(ret));
513 __free_pages(page, get_order(size));
517 static void jazz_dma_free(struct device *dev, size_t size, void *vaddr,
518 dma_addr_t dma_handle, unsigned long attrs)
520 vdma_free(dma_handle);
521 __free_pages(virt_to_page(vaddr), get_order(size));
524 static dma_addr_t jazz_dma_map_page(struct device *dev, struct page *page,
525 unsigned long offset, size_t size, enum dma_data_direction dir,
528 phys_addr_t phys = page_to_phys(page) + offset;
530 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
531 arch_sync_dma_for_device(phys, size, dir);
532 return vdma_alloc(phys, size);
535 static void jazz_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
536 size_t size, enum dma_data_direction dir, unsigned long attrs)
538 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
539 arch_sync_dma_for_cpu(vdma_log2phys(dma_addr), size, dir);
543 static int jazz_dma_map_sg(struct device *dev, struct scatterlist *sglist,
544 int nents, enum dma_data_direction dir, unsigned long attrs)
547 struct scatterlist *sg;
549 for_each_sg(sglist, sg, nents, i) {
550 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
551 arch_sync_dma_for_device(sg_phys(sg), sg->length,
553 sg->dma_address = vdma_alloc(sg_phys(sg), sg->length);
554 if (sg->dma_address == DMA_MAPPING_ERROR)
556 sg_dma_len(sg) = sg->length;
562 static void jazz_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
563 int nents, enum dma_data_direction dir, unsigned long attrs)
566 struct scatterlist *sg;
568 for_each_sg(sglist, sg, nents, i) {
569 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
570 arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
571 vdma_free(sg->dma_address);
575 static void jazz_dma_sync_single_for_device(struct device *dev,
576 dma_addr_t addr, size_t size, enum dma_data_direction dir)
578 arch_sync_dma_for_device(vdma_log2phys(addr), size, dir);
581 static void jazz_dma_sync_single_for_cpu(struct device *dev,
582 dma_addr_t addr, size_t size, enum dma_data_direction dir)
584 arch_sync_dma_for_cpu(vdma_log2phys(addr), size, dir);
587 static void jazz_dma_sync_sg_for_device(struct device *dev,
588 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
590 struct scatterlist *sg;
593 for_each_sg(sgl, sg, nents, i)
594 arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
597 static void jazz_dma_sync_sg_for_cpu(struct device *dev,
598 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
600 struct scatterlist *sg;
603 for_each_sg(sgl, sg, nents, i)
604 arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
607 const struct dma_map_ops jazz_dma_ops = {
608 .alloc = jazz_dma_alloc,
609 .free = jazz_dma_free,
610 .map_page = jazz_dma_map_page,
611 .unmap_page = jazz_dma_unmap_page,
612 .map_sg = jazz_dma_map_sg,
613 .unmap_sg = jazz_dma_unmap_sg,
614 .sync_single_for_cpu = jazz_dma_sync_single_for_cpu,
615 .sync_single_for_device = jazz_dma_sync_single_for_device,
616 .sync_sg_for_cpu = jazz_dma_sync_sg_for_cpu,
617 .sync_sg_for_device = jazz_dma_sync_sg_for_device,
618 .mmap = dma_common_mmap,
619 .get_sgtable = dma_common_get_sgtable,
620 .alloc_pages = dma_common_alloc_pages,
621 .free_pages = dma_common_free_pages,
623 EXPORT_SYMBOL(jazz_dma_ops);