dma.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  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
  4. * for more details.
  5. *
  6. * Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com>
  7. */
  8. #define pr_fmt(fmt) "bmips-dma: " fmt
  9. #include <linux/device.h>
  10. #include <linux/dma-direction.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/printk.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <asm/bmips.h>
  19. /*
  20. * BCM338x has configurable address translation windows which allow the
  21. * peripherals' DMA addresses to be different from the Zephyr-visible
  22. * physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000
  23. *
  24. * If the "brcm,ubus" node has a "dma-ranges" property we will enable this
  25. * translation globally using the provided information. This implements a
  26. * very limited subset of "dma-ranges" support and it will probably be
  27. * replaced by a more generic version later.
  28. */
  29. struct bmips_dma_range {
  30. u32 child_addr;
  31. u32 parent_addr;
  32. u32 size;
  33. };
  34. static struct bmips_dma_range *bmips_dma_ranges;
  35. #define FLUSH_RAC 0x100
  36. dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t pa)
  37. {
  38. struct bmips_dma_range *r;
  39. for (r = bmips_dma_ranges; r && r->size; r++) {
  40. if (pa >= r->child_addr &&
  41. pa < (r->child_addr + r->size))
  42. return pa - r->child_addr + r->parent_addr;
  43. }
  44. return pa;
  45. }
  46. phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dma_addr)
  47. {
  48. struct bmips_dma_range *r;
  49. for (r = bmips_dma_ranges; r && r->size; r++) {
  50. if (dma_addr >= r->parent_addr &&
  51. dma_addr < (r->parent_addr + r->size))
  52. return dma_addr - r->parent_addr + r->child_addr;
  53. }
  54. return dma_addr;
  55. }
  56. void arch_sync_dma_for_cpu_all(struct device *dev)
  57. {
  58. void __iomem *cbr = BMIPS_GET_CBR();
  59. u32 cfg;
  60. if (boot_cpu_type() != CPU_BMIPS3300 &&
  61. boot_cpu_type() != CPU_BMIPS4350 &&
  62. boot_cpu_type() != CPU_BMIPS4380)
  63. return;
  64. /* Flush stale data out of the readahead cache */
  65. cfg = __raw_readl(cbr + BMIPS_RAC_CONFIG);
  66. __raw_writel(cfg | 0x100, cbr + BMIPS_RAC_CONFIG);
  67. __raw_readl(cbr + BMIPS_RAC_CONFIG);
  68. }
  69. static int __init bmips_init_dma_ranges(void)
  70. {
  71. struct device_node *np =
  72. of_find_compatible_node(NULL, NULL, "brcm,ubus");
  73. const __be32 *data;
  74. struct bmips_dma_range *r;
  75. int len;
  76. if (!np)
  77. return 0;
  78. data = of_get_property(np, "dma-ranges", &len);
  79. if (!data)
  80. goto out_good;
  81. len /= sizeof(*data) * 3;
  82. if (!len)
  83. goto out_bad;
  84. /* add a dummy (zero) entry at the end as a sentinel */
  85. bmips_dma_ranges = kcalloc(len + 1, sizeof(struct bmips_dma_range),
  86. GFP_KERNEL);
  87. if (!bmips_dma_ranges)
  88. goto out_bad;
  89. for (r = bmips_dma_ranges; len; len--, r++) {
  90. r->child_addr = be32_to_cpup(data++);
  91. r->parent_addr = be32_to_cpup(data++);
  92. r->size = be32_to_cpup(data++);
  93. }
  94. out_good:
  95. of_node_put(np);
  96. return 0;
  97. out_bad:
  98. pr_err("error parsing dma-ranges property\n");
  99. of_node_put(np);
  100. return -EINVAL;
  101. }
  102. arch_initcall(bmips_init_dma_ranges);