of_device_common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/errno.h>
  8. #include <linux/irq.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_platform.h>
  11. #include "of_device_common.h"
  12. static int node_match(struct device *dev, void *data)
  13. {
  14. struct of_device *op = to_of_device(dev);
  15. struct device_node *dp = data;
  16. return (op->dev.of_node == dp);
  17. }
  18. struct of_device *of_find_device_by_node(struct device_node *dp)
  19. {
  20. struct device *dev = bus_find_device(&platform_bus_type, NULL,
  21. dp, node_match);
  22. if (dev)
  23. return to_of_device(dev);
  24. return NULL;
  25. }
  26. EXPORT_SYMBOL(of_find_device_by_node);
  27. unsigned int irq_of_parse_and_map(struct device_node *node, int index)
  28. {
  29. struct of_device *op = of_find_device_by_node(node);
  30. if (!op || index >= op->archdata.num_irqs)
  31. return 0;
  32. return op->archdata.irqs[index];
  33. }
  34. EXPORT_SYMBOL(irq_of_parse_and_map);
  35. /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
  36. * BUS and propagate to all child of_device objects.
  37. */
  38. void of_propagate_archdata(struct of_device *bus)
  39. {
  40. struct dev_archdata *bus_sd = &bus->dev.archdata;
  41. struct device_node *bus_dp = bus->dev.of_node;
  42. struct device_node *dp;
  43. for (dp = bus_dp->child; dp; dp = dp->sibling) {
  44. struct of_device *op = of_find_device_by_node(dp);
  45. op->dev.archdata.iommu = bus_sd->iommu;
  46. op->dev.archdata.stc = bus_sd->stc;
  47. op->dev.archdata.host_controller = bus_sd->host_controller;
  48. op->dev.archdata.numa_node = bus_sd->numa_node;
  49. if (dp->child)
  50. of_propagate_archdata(op);
  51. }
  52. }
  53. static void get_cells(struct device_node *dp, int *addrc, int *sizec)
  54. {
  55. if (addrc)
  56. *addrc = of_n_addr_cells(dp);
  57. if (sizec)
  58. *sizec = of_n_size_cells(dp);
  59. }
  60. /*
  61. * Default translator (generic bus)
  62. */
  63. void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec)
  64. {
  65. get_cells(dev, addrc, sizec);
  66. }
  67. /* Make sure the least significant 64-bits are in-range. Even
  68. * for 3 or 4 cell values it is a good enough approximation.
  69. */
  70. int of_out_of_range(const u32 *addr, const u32 *base,
  71. const u32 *size, int na, int ns)
  72. {
  73. u64 a = of_read_addr(addr, na);
  74. u64 b = of_read_addr(base, na);
  75. if (a < b)
  76. return 1;
  77. b += of_read_addr(size, ns);
  78. if (a >= b)
  79. return 1;
  80. return 0;
  81. }
  82. int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  83. {
  84. u32 result[OF_MAX_ADDR_CELLS];
  85. int i;
  86. if (ns > 2) {
  87. printk("of_device: Cannot handle size cells (%d) > 2.", ns);
  88. return -EINVAL;
  89. }
  90. if (of_out_of_range(addr, range, range + na + pna, na, ns))
  91. return -EINVAL;
  92. /* Start with the parent range base. */
  93. memcpy(result, range + na, pna * 4);
  94. /* Add in the child address offset. */
  95. for (i = 0; i < na; i++)
  96. result[pna - 1 - i] +=
  97. (addr[na - 1 - i] -
  98. range[na - 1 - i]);
  99. memcpy(addr, result, pna * 4);
  100. return 0;
  101. }
  102. unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
  103. {
  104. if (flags)
  105. return flags;
  106. return IORESOURCE_MEM;
  107. }
  108. /*
  109. * SBUS bus specific translator
  110. */
  111. int of_bus_sbus_match(struct device_node *np)
  112. {
  113. struct device_node *dp = np;
  114. while (dp) {
  115. if (!strcmp(dp->name, "sbus") ||
  116. !strcmp(dp->name, "sbi"))
  117. return 1;
  118. /* Have a look at use_1to1_mapping(). We're trying
  119. * to match SBUS if that's the top-level bus and we
  120. * don't have some intervening real bus that provides
  121. * ranges based translations.
  122. */
  123. if (of_find_property(dp, "ranges", NULL) != NULL)
  124. break;
  125. dp = dp->parent;
  126. }
  127. return 0;
  128. }
  129. void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec)
  130. {
  131. if (addrc)
  132. *addrc = 2;
  133. if (sizec)
  134. *sizec = 1;
  135. }