Commit 1ada656f authored by Kevin Cernekee's avatar Kevin Cernekee Committed by Ralf Baechle

MIPS: BMIPS: Rewrite DMA code to use "dma-ranges" property

This is a more standardized way of handling DMA remapping, and it is
suitable for the memory map found on BCM3384.
Signed-off-by: default avatarKevin Cernekee <cernekee@gmail.com>
Cc: f.fainelli@gmail.com
Cc: jaedon.shin@gmail.com
Cc: abrestic@chromium.org
Cc: tglx@linutronix.de
Cc: jason@lakedaemon.net
Cc: jogo@openwrt.org
Cc: arnd@arndb.de
Cc: computersforpeace@gmail.com
Cc: linux-mips@linux-mips.org
Cc: devicetree@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/8850/Signed-off-by: default avatarRalf Baechle <ralf@linux-mips.org>
parent 9c24ce29
...@@ -6,76 +6,112 @@ ...@@ -6,76 +6,112 @@
* Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com> * Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com>
*/ */
#define pr_fmt(fmt) "bmips-dma: " fmt
#include <linux/device.h> #include <linux/device.h>
#include <linux/dma-direction.h> #include <linux/dma-direction.h>
#include <linux/dma-mapping.h> #include <linux/dma-mapping.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/mm.h> #include <linux/io.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/pci.h> #include <linux/printk.h>
#include <linux/slab.h>
#include <linux/types.h> #include <linux/types.h>
#include <dma-coherence.h> #include <dma-coherence.h>
/* /*
* BCM3384 has configurable address translation windows which allow the * BCM338x has configurable address translation windows which allow the
* peripherals' DMA addresses to be different from the Zephyr-visible * peripherals' DMA addresses to be different from the Zephyr-visible
* physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000 * physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000
* *
* If our DT "memory" node has a "dma-xor-mask" property we will enable this * If the "brcm,ubus" node has a "dma-ranges" property we will enable this
* translation using the provided offset. * translation globally using the provided information. This implements a
* very limited subset of "dma-ranges" support and it will probably be
* replaced by a more generic version later.
*/ */
static u32 bcm3384_dma_xor_mask;
static u32 bcm3384_dma_xor_limit = 0xffffffff;
/* struct bmips_dma_range {
* PCI collapses the memory hole at 0x10000000 - 0x1fffffff. u32 child_addr;
* On systems with a dma-xor-mask, this range is guaranteed to live above u32 parent_addr;
* the dma-xor-limit. u32 size;
*/ };
#define BCM3384_MEM_HOLE_PA 0x10000000
#define BCM3384_MEM_HOLE_SIZE 0x10000000
static dma_addr_t bcm3384_phys_to_dma(struct device *dev, phys_addr_t pa) static struct bmips_dma_range *bmips_dma_ranges;
#define FLUSH_RAC 0x100
static dma_addr_t bmips_phys_to_dma(struct device *dev, phys_addr_t pa)
{ {
if (dev && dev_is_pci(dev) && struct bmips_dma_range *r;
pa >= (BCM3384_MEM_HOLE_PA + BCM3384_MEM_HOLE_SIZE))
return pa - BCM3384_MEM_HOLE_SIZE; for (r = bmips_dma_ranges; r && r->size; r++) {
if (pa <= bcm3384_dma_xor_limit) if (pa >= r->child_addr &&
return pa ^ bcm3384_dma_xor_mask; pa < (r->child_addr + r->size))
return pa - r->child_addr + r->parent_addr;
}
return pa; return pa;
} }
dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, size_t size) dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, size_t size)
{ {
return bcm3384_phys_to_dma(dev, virt_to_phys(addr)); return bmips_phys_to_dma(dev, virt_to_phys(addr));
} }
dma_addr_t plat_map_dma_mem_page(struct device *dev, struct page *page) dma_addr_t plat_map_dma_mem_page(struct device *dev, struct page *page)
{ {
return bcm3384_phys_to_dma(dev, page_to_phys(page)); return bmips_phys_to_dma(dev, page_to_phys(page));
} }
unsigned long plat_dma_addr_to_phys(struct device *dev, dma_addr_t dma_addr) unsigned long plat_dma_addr_to_phys(struct device *dev, dma_addr_t dma_addr)
{ {
if (dev && dev_is_pci(dev) && struct bmips_dma_range *r;
dma_addr >= BCM3384_MEM_HOLE_PA)
return dma_addr + BCM3384_MEM_HOLE_SIZE; for (r = bmips_dma_ranges; r && r->size; r++) {
if ((dma_addr ^ bcm3384_dma_xor_mask) <= bcm3384_dma_xor_limit) if (dma_addr >= r->parent_addr &&
return dma_addr ^ bcm3384_dma_xor_mask; dma_addr < (r->parent_addr + r->size))
return dma_addr - r->parent_addr + r->child_addr;
}
return dma_addr; return dma_addr;
} }
static int __init bcm3384_init_dma_xor(void) static int __init bmips_init_dma_ranges(void)
{ {
struct device_node *np = of_find_node_by_type(NULL, "memory"); struct device_node *np =
of_find_compatible_node(NULL, NULL, "brcm,ubus");
const __be32 *data;
struct bmips_dma_range *r;
int len;
if (!np) if (!np)
return 0; return 0;
of_property_read_u32(np, "dma-xor-mask", &bcm3384_dma_xor_mask); data = of_get_property(np, "dma-ranges", &len);
of_property_read_u32(np, "dma-xor-limit", &bcm3384_dma_xor_limit); if (!data)
goto out_good;
len /= sizeof(*data) * 3;
if (!len)
goto out_bad;
/* add a dummy (zero) entry at the end as a sentinel */
bmips_dma_ranges = kzalloc(sizeof(struct bmips_dma_range) * (len + 1),
GFP_KERNEL);
if (!bmips_dma_ranges)
goto out_bad;
for (r = bmips_dma_ranges; len; len--, r++) {
r->child_addr = be32_to_cpup(data++);
r->parent_addr = be32_to_cpup(data++);
r->size = be32_to_cpup(data++);
}
out_good:
of_node_put(np); of_node_put(np);
return 0; return 0;
out_bad:
pr_err("error parsing dma-ranges property\n");
of_node_put(np);
return -EINVAL;
} }
arch_initcall(bcm3384_init_dma_xor); arch_initcall(bmips_init_dma_ranges);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment