2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com>
9 #define pr_fmt(fmt) "bmips-dma: " fmt
11 #include <linux/device.h>
12 #include <linux/dma-direction.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/init.h>
17 #include <linux/printk.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <asm/bmips.h>
23 * BCM338x has configurable address translation windows which allow the
24 * peripherals' DMA addresses to be different from the Zephyr-visible
25 * physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000
27 * If the "brcm,ubus" node has a "dma-ranges" property we will enable this
28 * translation globally using the provided information. This implements a
29 * very limited subset of "dma-ranges" support and it will probably be
30 * replaced by a more generic version later.
33 struct bmips_dma_range {
39 static struct bmips_dma_range *bmips_dma_ranges;
41 #define FLUSH_RAC 0x100
43 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t pa)
45 struct bmips_dma_range *r;
47 for (r = bmips_dma_ranges; r && r->size; r++) {
48 if (pa >= r->child_addr &&
49 pa < (r->child_addr + r->size))
50 return pa - r->child_addr + r->parent_addr;
55 phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dma_addr)
57 struct bmips_dma_range *r;
59 for (r = bmips_dma_ranges; r && r->size; r++) {
60 if (dma_addr >= r->parent_addr &&
61 dma_addr < (r->parent_addr + r->size))
62 return dma_addr - r->parent_addr + r->child_addr;
67 bool bmips_rac_flush_disable;
69 void arch_sync_dma_for_cpu_all(void)
71 void __iomem *cbr = BMIPS_GET_CBR();
74 if (boot_cpu_type() != CPU_BMIPS3300 &&
75 boot_cpu_type() != CPU_BMIPS4350 &&
76 boot_cpu_type() != CPU_BMIPS4380)
79 if (unlikely(bmips_rac_flush_disable))
82 /* Flush stale data out of the readahead cache */
83 cfg = __raw_readl(cbr + BMIPS_RAC_CONFIG);
84 __raw_writel(cfg | 0x100, cbr + BMIPS_RAC_CONFIG);
85 __raw_readl(cbr + BMIPS_RAC_CONFIG);
88 static int __init bmips_init_dma_ranges(void)
90 struct device_node *np =
91 of_find_compatible_node(NULL, NULL, "brcm,ubus");
93 struct bmips_dma_range *r;
99 data = of_get_property(np, "dma-ranges", &len);
103 len /= sizeof(*data) * 3;
107 /* add a dummy (zero) entry at the end as a sentinel */
108 bmips_dma_ranges = kcalloc(len + 1, sizeof(struct bmips_dma_range),
110 if (!bmips_dma_ranges)
113 for (r = bmips_dma_ranges; len; len--, r++) {
114 r->child_addr = be32_to_cpup(data++);
115 r->parent_addr = be32_to_cpup(data++);
116 r->size = be32_to_cpup(data++);
124 pr_err("error parsing dma-ranges property\n");
128 arch_initcall(bmips_init_dma_ranges);