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