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
*)CKSEG1ADDR((unsigned long)pgtbl
);
80 * Clear the R4030 translation table
84 r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE
,
85 CPHYSADDR((unsigned long)pgtbl
));
86 r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM
, VDMA_PGTBL_SIZE
);
87 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV
, 0);
89 printk(KERN_INFO
"VDMA: R4030 DMA pagetables initialized.\n");
92 arch_initcall(vdma_init
);
95 * Allocate DMA pagetables using a simple first-fit algorithm
97 unsigned long vdma_alloc(unsigned long paddr
, unsigned long size
)
99 int first
, last
, pages
, frame
, i
;
100 unsigned long laddr
, flags
;
102 /* check arguments */
104 if (paddr
> 0x1fffffff) {
106 printk("vdma_alloc: Invalid physical address: %08lx\n",
108 return VDMA_ERROR
; /* invalid physical address */
110 if (size
> 0x400000 || size
== 0) {
112 printk("vdma_alloc: Invalid size: %08lx\n", size
);
113 return VDMA_ERROR
; /* invalid physical address */
116 spin_lock_irqsave(&vdma_lock
, flags
);
120 pages
= VDMA_PAGE(paddr
+ size
) - VDMA_PAGE(paddr
) + 1;
123 while (pgtbl
[first
].owner
!= VDMA_PAGE_EMPTY
&&
124 first
< VDMA_PGTBL_ENTRIES
) first
++;
125 if (first
+ pages
> VDMA_PGTBL_ENTRIES
) { /* nothing free */
126 spin_unlock_irqrestore(&vdma_lock
, flags
);
131 while (pgtbl
[last
].owner
== VDMA_PAGE_EMPTY
132 && last
- first
< pages
)
135 if (last
- first
== pages
)
141 * Mark pages as allocated
143 laddr
= (first
<< 12) + (paddr
& (VDMA_PAGESIZE
- 1));
144 frame
= paddr
& ~(VDMA_PAGESIZE
- 1);
146 for (i
= first
; i
< last
; i
++) {
147 pgtbl
[i
].frame
= frame
;
148 pgtbl
[i
].owner
= laddr
;
149 frame
+= VDMA_PAGESIZE
;
153 * Update translation table and return logical start address
155 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV
, 0);
158 printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
161 if (vdma_debug
> 2) {
163 for (i
= first
; i
< last
; i
++)
164 printk("%08x ", i
<< 12);
166 for (i
= first
; i
< last
; i
++)
167 printk("%08x ", pgtbl
[i
].frame
);
169 for (i
= first
; i
< last
; i
++)
170 printk("%08x ", pgtbl
[i
].owner
);
174 spin_unlock_irqrestore(&vdma_lock
, flags
);
179 EXPORT_SYMBOL(vdma_alloc
);
182 * Free previously allocated dma translation pages
183 * Note that this does NOT change the translation table,
184 * it just marks the free'd pages as unused!
186 int vdma_free(unsigned long laddr
)
192 if (pgtbl
[i
].owner
!= laddr
) {
194 ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
199 while (i
< VDMA_PGTBL_ENTRIES
&& pgtbl
[i
].owner
== laddr
) {
200 pgtbl
[i
].owner
= VDMA_PAGE_EMPTY
;
205 printk("vdma_free: freed %ld pages starting from %08lx\n",
206 i
- (laddr
>> 12), laddr
);
211 EXPORT_SYMBOL(vdma_free
);
214 * Map certain page(s) to another physical address.
215 * Caller must have allocated the page(s) before.
217 int vdma_remap(unsigned long laddr
, unsigned long paddr
, unsigned long size
)
221 if (laddr
> 0xffffff) {
224 ("vdma_map: Invalid logical address: %08lx\n",
226 return -EINVAL
; /* invalid logical address */
228 if (paddr
> 0x1fffffff) {
231 ("vdma_map: Invalid physical address: %08lx\n",
233 return -EINVAL
; /* invalid physical address */
236 pages
= (((paddr
& (VDMA_PAGESIZE
- 1)) + size
) >> 12) + 1;
239 printk("vdma_remap: first=%x, pages=%x\n", first
, pages
);
240 if (first
+ pages
> VDMA_PGTBL_ENTRIES
) {
242 printk("vdma_alloc: Invalid size: %08lx\n", size
);
246 paddr
&= ~(VDMA_PAGESIZE
- 1);
247 while (pages
> 0 && first
< VDMA_PGTBL_ENTRIES
) {
248 if (pgtbl
[first
].owner
!= laddr
) {
250 printk("Trying to remap other's pages.\n");
251 return -EPERM
; /* not owner */
253 pgtbl
[first
].frame
= paddr
;
254 paddr
+= VDMA_PAGESIZE
;
260 * Update translation table
262 r4030_write_reg32(JAZZ_R4030_TRSTBL_INV
, 0);
264 if (vdma_debug
> 2) {
266 pages
= (((paddr
& (VDMA_PAGESIZE
- 1)) + size
) >> 12) + 1;
269 for (i
= first
; i
< first
+ pages
; i
++)
270 printk("%08x ", i
<< 12);
272 for (i
= first
; i
< first
+ pages
; i
++)
273 printk("%08x ", pgtbl
[i
].frame
);
275 for (i
= first
; i
< first
+ pages
; i
++)
276 printk("%08x ", pgtbl
[i
].owner
);
284 * Translate a physical address to a logical address.
285 * This will return the logical address of the first
288 unsigned long vdma_phys2log(unsigned long paddr
)
293 frame
= paddr
& ~(VDMA_PAGESIZE
- 1);
295 for (i
= 0; i
< VDMA_PGTBL_ENTRIES
; i
++) {
296 if (pgtbl
[i
].frame
== frame
)
300 if (i
== VDMA_PGTBL_ENTRIES
)
303 return (i
<< 12) + (paddr
& (VDMA_PAGESIZE
- 1));
306 EXPORT_SYMBOL(vdma_phys2log
);
309 * Translate a logical DMA address to a physical address
311 unsigned long vdma_log2phys(unsigned long laddr
)
313 return pgtbl
[laddr
>> 12].frame
+ (laddr
& (VDMA_PAGESIZE
- 1));
316 EXPORT_SYMBOL(vdma_log2phys
);
319 * Print DMA statistics
321 void vdma_stats(void)
325 printk("vdma_stats: CONFIG: %08x\n",
326 r4030_read_reg32(JAZZ_R4030_CONFIG
));
327 printk("R4030 translation table base: %08x\n",
328 r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE
));
329 printk("R4030 translation table limit: %08x\n",
330 r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM
));
331 printk("vdma_stats: INV_ADDR: %08x\n",
332 r4030_read_reg32(JAZZ_R4030_INV_ADDR
));
333 printk("vdma_stats: R_FAIL_ADDR: %08x\n",
334 r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR
));
335 printk("vdma_stats: M_FAIL_ADDR: %08x\n",
336 r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR
));
337 printk("vdma_stats: IRQ_SOURCE: %08x\n",
338 r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE
));
339 printk("vdma_stats: I386_ERROR: %08x\n",
340 r4030_read_reg32(JAZZ_R4030_I386_ERROR
));
341 printk("vdma_chnl_modes: ");
342 for (i
= 0; i
< 8; i
++)
344 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE
+
347 printk("vdma_chnl_enables: ");
348 for (i
= 0; i
< 8; i
++)
350 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
356 * DMA transfer functions
360 * Enable a DMA channel. Also clear any error conditions.
362 void vdma_enable(int channel
)
367 printk("vdma_enable: channel %d\n", channel
);
370 * Check error conditions first
372 status
= r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5));
374 printk("VDMA: Channel %d: Address error!\n", channel
);
376 printk("VDMA: Channel %d: Memory error!\n", channel
);
379 * Clear all interrupt flags
381 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5),
382 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
383 (channel
<< 5)) | R4030_TC_INTR
384 | R4030_MEM_INTR
| R4030_ADDR_INTR
);
387 * Enable the desired channel
389 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5),
390 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
395 EXPORT_SYMBOL(vdma_enable
);
398 * Disable a DMA channel
400 void vdma_disable(int channel
)
404 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
407 printk("vdma_disable: channel %d\n", channel
);
408 printk("VDMA: channel %d status: %04x (%s) mode: "
409 "%02x addr: %06x count: %06x\n",
411 ((status
& 0x600) ? "ERROR" : "OK"),
412 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE
+
414 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR
+
416 (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT
+
420 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5),
421 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
426 * After disabling a DMA channel a remote bus register should be
427 * read to ensure that the current DMA acknowledge cycle is completed.
429 *((volatile unsigned int *) JAZZ_DUMMY_DEVICE
);
432 EXPORT_SYMBOL(vdma_disable
);
435 * Set DMA mode. This function accepts the mode values used
436 * to set a PC-style DMA controller. For the SCSI and FDC
437 * channels, we also set the default modes each time we're
439 * NOTE: The FAST and BURST dma modes are supported by the
440 * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
443 void vdma_set_mode(int channel
, int mode
)
446 printk("vdma_set_mode: channel %d, mode 0x%x\n", channel
,
450 case JAZZ_SCSI_DMA
: /* scsi */
451 r4030_write_reg32(JAZZ_R4030_CHNL_MODE
+ (channel
<< 5),
452 /* R4030_MODE_FAST | */
453 /* R4030_MODE_BURST | */
455 R4030_MODE_WIDTH_16
|
456 R4030_MODE_ATIME_80
);
459 case JAZZ_FLOPPY_DMA
: /* floppy */
460 r4030_write_reg32(JAZZ_R4030_CHNL_MODE
+ (channel
<< 5),
461 /* R4030_MODE_FAST | */
462 /* R4030_MODE_BURST | */
465 R4030_MODE_ATIME_120
);
468 case JAZZ_AUDIOL_DMA
:
469 case JAZZ_AUDIOR_DMA
:
470 printk("VDMA: Audio DMA not supported yet.\n");
475 ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
481 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5),
482 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
488 r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5),
489 r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+
496 ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
501 EXPORT_SYMBOL(vdma_set_mode
);
504 * Set Transfer Address
506 void vdma_set_addr(int channel
, long addr
)
509 printk("vdma_set_addr: channel %d, addr %lx\n", channel
,
512 r4030_write_reg32(JAZZ_R4030_CHNL_ADDR
+ (channel
<< 5), addr
);
515 EXPORT_SYMBOL(vdma_set_addr
);
520 void vdma_set_count(int channel
, int count
)
523 printk("vdma_set_count: channel %d, count %08x\n", channel
,
526 r4030_write_reg32(JAZZ_R4030_CHNL_COUNT
+ (channel
<< 5), count
);
529 EXPORT_SYMBOL(vdma_set_count
);
534 int vdma_get_residue(int channel
)
538 residual
= r4030_read_reg32(JAZZ_R4030_CHNL_COUNT
+ (channel
<< 5));
541 printk("vdma_get_residual: channel %d: residual=%d\n",
548 * Get DMA channel enable register
550 int vdma_get_enable(int channel
)
554 enable
= r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE
+ (channel
<< 5));
557 printk("vdma_get_enable: channel %d: enable=%d\n", channel
,
563 static void *jazz_dma_alloc(struct device
*dev
, size_t size
,
564 dma_addr_t
*dma_handle
, gfp_t gfp
, unsigned long attrs
)
568 ret
= dma_direct_alloc_pages(dev
, size
, dma_handle
, gfp
, attrs
);
572 *dma_handle
= vdma_alloc(virt_to_phys(ret
), size
);
573 if (*dma_handle
== VDMA_ERROR
) {
574 dma_direct_free_pages(dev
, size
, ret
, *dma_handle
, attrs
);
578 if (!(attrs
& DMA_ATTR_NON_CONSISTENT
)) {
579 dma_cache_wback_inv((unsigned long)ret
, size
);
580 ret
= (void *)UNCAC_ADDR(ret
);
585 static void jazz_dma_free(struct device
*dev
, size_t size
, void *vaddr
,
586 dma_addr_t dma_handle
, unsigned long attrs
)
588 vdma_free(dma_handle
);
589 if (!(attrs
& DMA_ATTR_NON_CONSISTENT
))
590 vaddr
= (void *)CAC_ADDR((unsigned long)vaddr
);
591 dma_direct_free_pages(dev
, size
, vaddr
, dma_handle
, attrs
);
594 static dma_addr_t
jazz_dma_map_page(struct device
*dev
, struct page
*page
,
595 unsigned long offset
, size_t size
, enum dma_data_direction dir
,
598 phys_addr_t phys
= page_to_phys(page
) + offset
;
600 if (!(attrs
& DMA_ATTR_SKIP_CPU_SYNC
))
601 arch_sync_dma_for_device(dev
, phys
, size
, dir
);
602 return vdma_alloc(phys
, size
);
605 static void jazz_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
606 size_t size
, enum dma_data_direction dir
, unsigned long attrs
)
608 if (!(attrs
& DMA_ATTR_SKIP_CPU_SYNC
))
609 arch_sync_dma_for_cpu(dev
, vdma_log2phys(dma_addr
), size
, dir
);
613 static int jazz_dma_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
614 int nents
, enum dma_data_direction dir
, unsigned long attrs
)
617 struct scatterlist
*sg
;
619 for_each_sg(sglist
, sg
, nents
, i
) {
620 if (!(attrs
& DMA_ATTR_SKIP_CPU_SYNC
))
621 arch_sync_dma_for_device(dev
, sg_phys(sg
), sg
->length
,
623 sg
->dma_address
= vdma_alloc(sg_phys(sg
), sg
->length
);
624 if (sg
->dma_address
== VDMA_ERROR
)
626 sg_dma_len(sg
) = sg
->length
;
632 static void jazz_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
633 int nents
, enum dma_data_direction dir
, unsigned long attrs
)
636 struct scatterlist
*sg
;
638 for_each_sg(sglist
, sg
, nents
, i
) {
639 if (!(attrs
& DMA_ATTR_SKIP_CPU_SYNC
))
640 arch_sync_dma_for_cpu(dev
, sg_phys(sg
), sg
->length
,
642 vdma_free(sg
->dma_address
);
646 static void jazz_dma_sync_single_for_device(struct device
*dev
,
647 dma_addr_t addr
, size_t size
, enum dma_data_direction dir
)
649 arch_sync_dma_for_device(dev
, vdma_log2phys(addr
), size
, dir
);
652 static void jazz_dma_sync_single_for_cpu(struct device
*dev
,
653 dma_addr_t addr
, size_t size
, enum dma_data_direction dir
)
655 arch_sync_dma_for_cpu(dev
, vdma_log2phys(addr
), size
, dir
);
658 static void jazz_dma_sync_sg_for_device(struct device
*dev
,
659 struct scatterlist
*sgl
, int nents
, enum dma_data_direction dir
)
661 struct scatterlist
*sg
;
664 for_each_sg(sgl
, sg
, nents
, i
)
665 arch_sync_dma_for_device(dev
, sg_phys(sg
), sg
->length
, dir
);
668 static void jazz_dma_sync_sg_for_cpu(struct device
*dev
,
669 struct scatterlist
*sgl
, int nents
, enum dma_data_direction dir
)
671 struct scatterlist
*sg
;
674 for_each_sg(sgl
, sg
, nents
, i
)
675 arch_sync_dma_for_cpu(dev
, sg_phys(sg
), sg
->length
, dir
);
678 static int jazz_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
680 return dma_addr
== VDMA_ERROR
;
683 const struct dma_map_ops jazz_dma_ops
= {
684 .alloc
= jazz_dma_alloc
,
685 .free
= jazz_dma_free
,
686 .map_page
= jazz_dma_map_page
,
687 .unmap_page
= jazz_dma_unmap_page
,
688 .map_sg
= jazz_dma_map_sg
,
689 .unmap_sg
= jazz_dma_unmap_sg
,
690 .sync_single_for_cpu
= jazz_dma_sync_single_for_cpu
,
691 .sync_single_for_device
= jazz_dma_sync_single_for_device
,
692 .sync_sg_for_cpu
= jazz_dma_sync_sg_for_cpu
,
693 .sync_sg_for_device
= jazz_dma_sync_sg_for_device
,
694 .dma_supported
= dma_direct_supported
,
695 .cache_sync
= arch_dma_cache_sync
,
696 .mapping_error
= jazz_dma_mapping_error
,
698 EXPORT_SYMBOL(jazz_dma_ops
);