of_device_common.c 3.9 KB

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