dma.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <dma-coherence.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. static dma_addr_t bmips_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. dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, size_t size)
  47. {
  48. return bmips_phys_to_dma(dev, virt_to_phys(addr));
  49. }
  50. dma_addr_t plat_map_dma_mem_page(struct device *dev, struct page *page)
  51. {
  52. return bmips_phys_to_dma(dev, page_to_phys(page));
  53. }
  54. unsigned long plat_dma_addr_to_phys(struct device *dev, dma_addr_t dma_addr)
  55. {
  56. struct bmips_dma_range *r;
  57. for (r = bmips_dma_ranges; r && r->size; r++) {
  58. if (dma_addr >= r->parent_addr &&
  59. dma_addr < (r->parent_addr + r->size))
  60. return dma_addr - r->parent_addr + r->child_addr;
  61. }
  62. return dma_addr;
  63. }
  64. static int __init bmips_init_dma_ranges(void)
  65. {
  66. struct device_node *np =
  67. of_find_compatible_node(NULL, NULL, "brcm,ubus");
  68. const __be32 *data;
  69. struct bmips_dma_range *r;
  70. int len;
  71. if (!np)
  72. return 0;
  73. data = of_get_property(np, "dma-ranges", &len);
  74. if (!data)
  75. goto out_good;
  76. len /= sizeof(*data) * 3;
  77. if (!len)
  78. goto out_bad;
  79. /* add a dummy (zero) entry at the end as a sentinel */
  80. bmips_dma_ranges = kzalloc(sizeof(struct bmips_dma_range) * (len + 1),
  81. GFP_KERNEL);
  82. if (!bmips_dma_ranges)
  83. goto out_bad;
  84. for (r = bmips_dma_ranges; len; len--, r++) {
  85. r->child_addr = be32_to_cpup(data++);
  86. r->parent_addr = be32_to_cpup(data++);
  87. r->size = be32_to_cpup(data++);
  88. }
  89. out_good:
  90. of_node_put(np);
  91. return 0;
  92. out_bad:
  93. pr_err("error parsing dma-ranges property\n");
  94. of_node_put(np);
  95. return -EINVAL;
  96. }
  97. arch_initcall(bmips_init_dma_ranges);