dma.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <linux/device.h>
  9. #include <linux/dma-direction.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/of.h>
  14. #include <linux/pci.h>
  15. #include <linux/types.h>
  16. #include <dma-coherence.h>
  17. /*
  18. * BCM3384 has configurable address translation windows which allow the
  19. * peripherals' DMA addresses to be different from the Zephyr-visible
  20. * physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000
  21. *
  22. * If our DT "memory" node has a "dma-xor-mask" property we will enable this
  23. * translation using the provided offset.
  24. */
  25. static u32 bcm3384_dma_xor_mask;
  26. static u32 bcm3384_dma_xor_limit = 0xffffffff;
  27. /*
  28. * PCI collapses the memory hole at 0x10000000 - 0x1fffffff.
  29. * On systems with a dma-xor-mask, this range is guaranteed to live above
  30. * the dma-xor-limit.
  31. */
  32. #define BCM3384_MEM_HOLE_PA 0x10000000
  33. #define BCM3384_MEM_HOLE_SIZE 0x10000000
  34. static dma_addr_t bcm3384_phys_to_dma(struct device *dev, phys_addr_t pa)
  35. {
  36. if (dev && dev_is_pci(dev) &&
  37. pa >= (BCM3384_MEM_HOLE_PA + BCM3384_MEM_HOLE_SIZE))
  38. return pa - BCM3384_MEM_HOLE_SIZE;
  39. if (pa <= bcm3384_dma_xor_limit)
  40. return pa ^ bcm3384_dma_xor_mask;
  41. return pa;
  42. }
  43. dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, size_t size)
  44. {
  45. return bcm3384_phys_to_dma(dev, virt_to_phys(addr));
  46. }
  47. dma_addr_t plat_map_dma_mem_page(struct device *dev, struct page *page)
  48. {
  49. return bcm3384_phys_to_dma(dev, page_to_phys(page));
  50. }
  51. unsigned long plat_dma_addr_to_phys(struct device *dev, dma_addr_t dma_addr)
  52. {
  53. if (dev && dev_is_pci(dev) &&
  54. dma_addr >= BCM3384_MEM_HOLE_PA)
  55. return dma_addr + BCM3384_MEM_HOLE_SIZE;
  56. if ((dma_addr ^ bcm3384_dma_xor_mask) <= bcm3384_dma_xor_limit)
  57. return dma_addr ^ bcm3384_dma_xor_mask;
  58. return dma_addr;
  59. }
  60. static int __init bcm3384_init_dma_xor(void)
  61. {
  62. struct device_node *np = of_find_node_by_type(NULL, "memory");
  63. if (!np)
  64. return 0;
  65. of_property_read_u32(np, "dma-xor-mask", &bcm3384_dma_xor_mask);
  66. of_property_read_u32(np, "dma-xor-limit", &bcm3384_dma_xor_limit);
  67. of_node_put(np);
  68. return 0;
  69. }
  70. arch_initcall(bcm3384_init_dma_xor);