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-direct.h>
20 #include <linux/dma-noncoherent.h>
21 #include <asm/mipsregs.h>
24 #include <linux/uaccess.h>
26 #include <asm/jazzdma.h>
27 #include <asm/pgtable.h>
30 * Set this to one to enable additional vdma debug code.
32 #define CONF_DEBUG_VDMA 0
34 static VDMA_PGTBL_ENTRY *pgtbl;
36 static DEFINE_SPINLOCK(vdma_lock);
41 #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
43 static int debuglvl = 3;
46 * Initialize the pagetable with a one-to-one mapping of
47 * the first 16 Mbytes of main memory and declare all
48 * entries to be unused. Using this method will at least
49 * allow some early device driver operations to work.
51 static inline void vdma_pgtbl_init(void)
53 unsigned long paddr = 0;
56 for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
57 pgtbl[i].frame = paddr;
58 pgtbl[i].owner = VDMA_PAGE_EMPTY;
59 paddr += VDMA_PAGESIZE;
64 * Initialize the Jazz R4030 dma controller
66 static int __init vdma_init(void)
69 * Allocate 32k of memory for DMA page tables. This needs to be page
70 * aligned and should be uncached to avoid cache flushing after every
73 pgtbl = (VDMA_PGTBL_ENTRY *)__get_free_pages(GFP_KERNEL | GFP_DMA,
74 get_order(VDMA_PGTBL_SIZE));
76 dma_cache_wback_inv((unsigned long)pgtbl, VDMA_PGTBL_SIZE);
77 pgtbl = (VDMA_PGTBL_ENTRY *)KSEG1ADDR(pgtbl);
80 * Clear the R4030 translation table
84 r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE, CPHYSADDR(pgtbl));
85 r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
86 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
88 printk(KERN_INFO "VDMA: R4030 DMA pagetables initialized.\n");
91 arch_initcall(vdma_init);
94 * Allocate DMA pagetables using a simple first-fit algorithm
96 unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
98 int first, last, pages, frame, i;
99 unsigned long laddr, flags;
101 /* check arguments */
103 if (paddr > 0x1fffffff) {
105 printk("vdma_alloc: Invalid physical address: %08lx\n",
107 return VDMA_ERROR; /* invalid physical address */
109 if (size > 0x400000 || size == 0) {
111 printk("vdma_alloc: Invalid size: %08lx\n", size);
112 return VDMA_ERROR; /* invalid physical address */
115 spin_lock_irqsave(&vdma_lock, flags);
119 pages = VDMA_PAGE(paddr + size) - VDMA_PAGE(paddr) + 1;
122 while (pgtbl[first].owner != VDMA_PAGE_EMPTY &&
123 first < VDMA_PGTBL_ENTRIES) first++;
124 if (first + pages > VDMA_PGTBL_ENTRIES) { /* nothing free */
125 spin_unlock_irqrestore(&vdma_lock, flags);
130 while (pgtbl[last].owner == VDMA_PAGE_EMPTY
131 && last - first < pages)
134 if (last - first == pages)
140 * Mark pages as allocated
142 laddr = (first << 12) + (paddr & (VDMA_PAGESIZE - 1));
143 frame = paddr & ~(VDMA_PAGESIZE - 1);
145 for (i = first; i < last; i++) {
146 pgtbl[i].frame = frame;
147 pgtbl[i].owner = laddr;
148 frame += VDMA_PAGESIZE;
152 * Update translation table and return logical start address
154 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
157 printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
160 if (vdma_debug > 2) {
162 for (i = first; i < last; i++)
163 printk("%08x ", i << 12);
165 for (i = first; i < last; i++)
166 printk("%08x ", pgtbl[i].frame);
168 for (i = first; i < last; i++)
169 printk("%08x ", pgtbl[i].owner);
173 spin_unlock_irqrestore(&vdma_lock, flags);
178 EXPORT_SYMBOL(vdma_alloc);
181 * Free previously allocated dma translation pages
182 * Note that this does NOT change the translation table,
183 * it just marks the free'd pages as unused!
185 int vdma_free(unsigned long laddr)
191 if (pgtbl[i].owner != laddr) {
193 ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
198 while (i < VDMA_PGTBL_ENTRIES && pgtbl[i].owner == laddr) {
199 pgtbl[i].owner = VDMA_PAGE_EMPTY;
204 printk("vdma_free: freed %ld pages starting from %08lx\n",
205 i - (laddr >> 12), laddr);
210 EXPORT_SYMBOL(vdma_free);
213 * Map certain page(s) to another physical address.
214 * Caller must have allocated the page(s) before.
216 int vdma_remap(unsigned long laddr, unsigned long paddr, unsigned long size)
220 if (laddr > 0xffffff) {
223 ("vdma_map: Invalid logical address: %08lx\n",
225 return -EINVAL; /* invalid logical address */
227 if (paddr > 0x1fffffff) {
230 ("vdma_map: Invalid physical address: %08lx\n",
232 return -EINVAL; /* invalid physical address */
235 pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
238 printk("vdma_remap: first=%x, pages=%x\n", first, pages);
239 if (first + pages > VDMA_PGTBL_ENTRIES) {
241 printk("vdma_alloc: Invalid size: %08lx\n", size);
245 paddr &= ~(VDMA_PAGESIZE - 1);
246 while (pages > 0 && first < VDMA_PGTBL_ENTRIES) {
247 if (pgtbl[first].owner != laddr) {
249 printk("Trying to remap other's pages.\n");
250 return -EPERM; /* not owner */
252 pgtbl[first].frame = paddr;
253 paddr += VDMA_PAGESIZE;
259 * Update translation table
261 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
263 if (vdma_debug > 2) {
265 pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
268 for (i = first; i < first + pages; i++)
269 printk("%08x ", i << 12);
271 for (i = first; i < first + pages; i++)
272 printk("%08x ", pgtbl[i].frame);
274 for (i = first; i < first + pages; i++)
275 printk("%08x ", pgtbl[i].owner);
283 * Translate a physical address to a logical address.
284 * This will return the logical address of the first
287 unsigned long vdma_phys2log(unsigned long paddr)
292 frame = paddr & ~(VDMA_PAGESIZE - 1);
294 for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
295 if (pgtbl[i].frame == frame)
299 if (i == VDMA_PGTBL_ENTRIES)
302 return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
305 EXPORT_SYMBOL(vdma_phys2log);
308 * Translate a logical DMA address to a physical address
310 unsigned long vdma_log2phys(unsigned long laddr)
312 return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
315 EXPORT_SYMBOL(vdma_log2phys);
318 * Print DMA statistics
320 void vdma_stats(void)
324 printk("vdma_stats: CONFIG: %08x\n",
325 r4030_read_reg32(JAZZ_R4030_CONFIG));
326 printk("R4030 translation table base: %08x\n",
327 r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
328 printk("R4030 translation table limit: %08x\n",
329 r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
330 printk("vdma_stats: INV_ADDR: %08x\n",
331 r4030_read_reg32(JAZZ_R4030_INV_ADDR));
332 printk("vdma_stats: R_FAIL_ADDR: %08x\n",
333 r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
334 printk("vdma_stats: M_FAIL_ADDR: %08x\n",
335 r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
336 printk("vdma_stats: IRQ_SOURCE: %08x\n",
337 r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
338 printk("vdma_stats: I386_ERROR: %08x\n",
339 r4030_read_reg32(JAZZ_R4030_I386_ERROR));
340 printk("vdma_chnl_modes: ");
341 for (i = 0; i < 8; i++)
343 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
346 printk("vdma_chnl_enables: ");
347 for (i = 0; i < 8; i++)
349 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
355 * DMA transfer functions
359 * Enable a DMA channel. Also clear any error conditions.
361 void vdma_enable(int channel)
366 printk("vdma_enable: channel %d\n", channel);
369 * Check error conditions first
371 status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
373 printk("VDMA: Channel %d: Address error!\n", channel);
375 printk("VDMA: Channel %d: Memory error!\n", channel);
378 * Clear all interrupt flags
380 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
381 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
382 (channel << 5)) | R4030_TC_INTR
383 | R4030_MEM_INTR | R4030_ADDR_INTR);
386 * Enable the desired channel
388 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
389 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
394 EXPORT_SYMBOL(vdma_enable);
397 * Disable a DMA channel
399 void vdma_disable(int channel)
403 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
406 printk("vdma_disable: channel %d\n", channel);
407 printk("VDMA: channel %d status: %04x (%s) mode: "
408 "%02x addr: %06x count: %06x\n",
410 ((status & 0x600) ? "ERROR" : "OK"),
411 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
413 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
415 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
419 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
420 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
425 * After disabling a DMA channel a remote bus register should be
426 * read to ensure that the current DMA acknowledge cycle is completed.
428 *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
431 EXPORT_SYMBOL(vdma_disable);
434 * Set DMA mode. This function accepts the mode values used
435 * to set a PC-style DMA controller. For the SCSI and FDC
436 * channels, we also set the default modes each time we're
438 * NOTE: The FAST and BURST dma modes are supported by the
439 * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
442 void vdma_set_mode(int channel, int mode)
445 printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
449 case JAZZ_SCSI_DMA: /* scsi */
450 r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
451 /* R4030_MODE_FAST | */
452 /* R4030_MODE_BURST | */
454 R4030_MODE_WIDTH_16 |
455 R4030_MODE_ATIME_80);
458 case JAZZ_FLOPPY_DMA: /* floppy */
459 r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
460 /* R4030_MODE_FAST | */
461 /* R4030_MODE_BURST | */
464 R4030_MODE_ATIME_120);
467 case JAZZ_AUDIOL_DMA:
468 case JAZZ_AUDIOR_DMA:
469 printk("VDMA: Audio DMA not supported yet.\n");
474 ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
480 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
481 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
487 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
488 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
495 ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
500 EXPORT_SYMBOL(vdma_set_mode);
503 * Set Transfer Address
505 void vdma_set_addr(int channel, long addr)
508 printk("vdma_set_addr: channel %d, addr %lx\n", channel,
511 r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
514 EXPORT_SYMBOL(vdma_set_addr);
519 void vdma_set_count(int channel, int count)
522 printk("vdma_set_count: channel %d, count %08x\n", channel,
525 r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
528 EXPORT_SYMBOL(vdma_set_count);
533 int vdma_get_residue(int channel)
537 residual = r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
540 printk("vdma_get_residual: channel %d: residual=%d\n",
547 * Get DMA channel enable register
549 int vdma_get_enable(int channel)
553 enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
556 printk("vdma_get_enable: channel %d: enable=%d\n", channel,
562 static void *jazz_dma_alloc(struct device *dev, size_t size,
563 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
567 ret = dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs);
571 *dma_handle = vdma_alloc(virt_to_phys(ret), size);
572 if (*dma_handle == VDMA_ERROR) {
573 dma_direct_free_pages(dev, size, ret, *dma_handle, attrs);
577 if (!(attrs & DMA_ATTR_NON_CONSISTENT)) {
578 dma_cache_wback_inv((unsigned long)ret, size);
579 ret = (void *)UNCAC_ADDR(ret);
584 static void jazz_dma_free(struct device *dev, size_t size, void *vaddr,
585 dma_addr_t dma_handle, unsigned long attrs)
587 vdma_free(dma_handle);
588 if (!(attrs & DMA_ATTR_NON_CONSISTENT))
589 vaddr = (void *)CAC_ADDR((unsigned long)vaddr);
590 dma_direct_free_pages(dev, size, vaddr, dma_handle, attrs);
593 static dma_addr_t jazz_dma_map_page(struct device *dev, struct page *page,
594 unsigned long offset, size_t size, enum dma_data_direction dir,
597 phys_addr_t phys = page_to_phys(page) + offset;
599 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
600 arch_sync_dma_for_device(dev, phys, size, dir);
601 return vdma_alloc(phys, size);
604 static void jazz_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
605 size_t size, enum dma_data_direction dir, unsigned long attrs)
607 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
608 arch_sync_dma_for_cpu(dev, vdma_log2phys(dma_addr), size, dir);
612 static int jazz_dma_map_sg(struct device *dev, struct scatterlist *sglist,
613 int nents, enum dma_data_direction dir, unsigned long attrs)
616 struct scatterlist *sg;
618 for_each_sg(sglist, sg, nents, i) {
619 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
620 arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
622 sg->dma_address = vdma_alloc(sg_phys(sg), sg->length);
623 if (sg->dma_address == VDMA_ERROR)
625 sg_dma_len(sg) = sg->length;
631 static void jazz_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
632 int nents, enum dma_data_direction dir, unsigned long attrs)
635 struct scatterlist *sg;
637 for_each_sg(sglist, sg, nents, i) {
638 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
639 arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length,
641 vdma_free(sg->dma_address);
645 static void jazz_dma_sync_single_for_device(struct device *dev,
646 dma_addr_t addr, size_t size, enum dma_data_direction dir)
648 arch_sync_dma_for_device(dev, vdma_log2phys(addr), size, dir);
651 static void jazz_dma_sync_single_for_cpu(struct device *dev,
652 dma_addr_t addr, size_t size, enum dma_data_direction dir)
654 arch_sync_dma_for_cpu(dev, vdma_log2phys(addr), size, dir);
657 static void jazz_dma_sync_sg_for_device(struct device *dev,
658 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
660 struct scatterlist *sg;
663 for_each_sg(sgl, sg, nents, i)
664 arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
667 static void jazz_dma_sync_sg_for_cpu(struct device *dev,
668 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
670 struct scatterlist *sg;
673 for_each_sg(sgl, sg, nents, i)
674 arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
677 static int jazz_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
679 return dma_addr == VDMA_ERROR;
682 const struct dma_map_ops jazz_dma_ops = {
683 .alloc = jazz_dma_alloc,
684 .free = jazz_dma_free,
685 .map_page = jazz_dma_map_page,
686 .unmap_page = jazz_dma_unmap_page,
687 .map_sg = jazz_dma_map_sg,
688 .unmap_sg = jazz_dma_unmap_sg,
689 .sync_single_for_cpu = jazz_dma_sync_single_for_cpu,
690 .sync_single_for_device = jazz_dma_sync_single_for_device,
691 .sync_sg_for_cpu = jazz_dma_sync_sg_for_cpu,
692 .sync_sg_for_device = jazz_dma_sync_sg_for_device,
693 .dma_supported = dma_direct_supported,
694 .cache_sync = arch_dma_cache_sync,
695 .mapping_error = jazz_dma_mapping_error,
697 EXPORT_SYMBOL(jazz_dma_ops);