| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 1 | #include <linux/string.h> | 
 | 2 | #include <linux/kernel.h> | 
| Stephen Rothwell | f85ff30 | 2007-05-01 16:40:36 +1000 | [diff] [blame] | 3 | #include <linux/of.h> | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 4 | #include <linux/init.h> | 
 | 5 | #include <linux/module.h> | 
 | 6 | #include <linux/mod_devicetable.h> | 
 | 7 | #include <linux/slab.h> | 
| Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 8 | #include <linux/errno.h> | 
 | 9 | #include <linux/of_device.h> | 
 | 10 | #include <linux/of_platform.h> | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 11 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 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 |  | 
 | 17 | 	return (op->node == dp); | 
 | 18 | } | 
 | 19 |  | 
 | 20 | struct of_device *of_find_device_by_node(struct device_node *dp) | 
 | 21 | { | 
| Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 22 | 	struct device *dev = bus_find_device(&of_platform_bus_type, NULL, | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 23 | 					     dp, node_match); | 
 | 24 |  | 
 | 25 | 	if (dev) | 
 | 26 | 		return to_of_device(dev); | 
 | 27 |  | 
 | 28 | 	return NULL; | 
 | 29 | } | 
 | 30 | EXPORT_SYMBOL(of_find_device_by_node); | 
 | 31 |  | 
| David S. Miller | 51e0f00 | 2008-08-25 16:44:58 -0700 | [diff] [blame] | 32 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) | 
| David S. Miller | 4426621 | 2008-08-20 16:34:39 -0700 | [diff] [blame] | 33 | { | 
 | 34 | 	struct of_device *op = of_find_device_by_node(node); | 
 | 35 |  | 
 | 36 | 	if (!op || index >= op->num_irqs) | 
| David S. Miller | 51e0f00 | 2008-08-25 16:44:58 -0700 | [diff] [blame] | 37 | 		return 0; | 
| David S. Miller | 4426621 | 2008-08-20 16:34:39 -0700 | [diff] [blame] | 38 |  | 
 | 39 | 	return op->irqs[index]; | 
 | 40 | } | 
 | 41 | EXPORT_SYMBOL(irq_of_parse_and_map); | 
 | 42 |  | 
| David S. Miller | 5059625 | 2008-08-27 04:22:37 -0700 | [diff] [blame] | 43 | /* Take the archdata values for IOMMU, STC, and HOSTDATA found in | 
 | 44 |  * BUS and propagate to all child of_device objects. | 
 | 45 |  */ | 
 | 46 | void of_propagate_archdata(struct of_device *bus) | 
 | 47 | { | 
 | 48 | 	struct dev_archdata *bus_sd = &bus->dev.archdata; | 
 | 49 | 	struct device_node *bus_dp = bus->node; | 
 | 50 | 	struct device_node *dp; | 
 | 51 |  | 
 | 52 | 	for (dp = bus_dp->child; dp; dp = dp->sibling) { | 
 | 53 | 		struct of_device *op = of_find_device_by_node(dp); | 
 | 54 |  | 
 | 55 | 		op->dev.archdata.iommu = bus_sd->iommu; | 
 | 56 | 		op->dev.archdata.stc = bus_sd->stc; | 
 | 57 | 		op->dev.archdata.host_controller = bus_sd->host_controller; | 
 | 58 | 		op->dev.archdata.numa_node = bus_sd->numa_node; | 
 | 59 |  | 
 | 60 | 		if (dp->child) | 
 | 61 | 			of_propagate_archdata(op); | 
 | 62 | 	} | 
 | 63 | } | 
 | 64 |  | 
| Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 65 | struct bus_type of_platform_bus_type; | 
| Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 66 | EXPORT_SYMBOL(of_platform_bus_type); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 67 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 68 | static inline u64 of_read_addr(const u32 *cell, int size) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 69 | { | 
 | 70 | 	u64 r = 0; | 
 | 71 | 	while (size--) | 
 | 72 | 		r = (r << 32) | *(cell++); | 
 | 73 | 	return r; | 
 | 74 | } | 
 | 75 |  | 
 | 76 | static void __init get_cells(struct device_node *dp, | 
 | 77 | 			     int *addrc, int *sizec) | 
 | 78 | { | 
 | 79 | 	if (addrc) | 
 | 80 | 		*addrc = of_n_addr_cells(dp); | 
 | 81 | 	if (sizec) | 
 | 82 | 		*sizec = of_n_size_cells(dp); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | /* Max address size we deal with */ | 
 | 86 | #define OF_MAX_ADDR_CELLS	4 | 
 | 87 |  | 
 | 88 | struct of_bus { | 
 | 89 | 	const char	*name; | 
 | 90 | 	const char	*addr_prop_name; | 
 | 91 | 	int		(*match)(struct device_node *parent); | 
 | 92 | 	void		(*count_cells)(struct device_node *child, | 
 | 93 | 				       int *addrc, int *sizec); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 94 | 	int		(*map)(u32 *addr, const u32 *range, | 
 | 95 | 			       int na, int ns, int pna); | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 96 | 	unsigned long	(*get_flags)(const u32 *addr, unsigned long); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 97 | }; | 
 | 98 |  | 
 | 99 | /* | 
 | 100 |  * Default translator (generic bus) | 
 | 101 |  */ | 
 | 102 |  | 
 | 103 | static void of_bus_default_count_cells(struct device_node *dev, | 
 | 104 | 				       int *addrc, int *sizec) | 
 | 105 | { | 
 | 106 | 	get_cells(dev, addrc, sizec); | 
 | 107 | } | 
 | 108 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 109 | /* Make sure the least significant 64-bits are in-range.  Even | 
 | 110 |  * for 3 or 4 cell values it is a good enough approximation. | 
 | 111 |  */ | 
 | 112 | static int of_out_of_range(const u32 *addr, const u32 *base, | 
 | 113 | 			   const u32 *size, int na, int ns) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 114 | { | 
 | 115 | 	u64 a = of_read_addr(addr, na); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 116 | 	u64 b = of_read_addr(base, na); | 
 | 117 |  | 
 | 118 | 	if (a < b) | 
 | 119 | 		return 1; | 
 | 120 |  | 
 | 121 | 	b += of_read_addr(size, ns); | 
 | 122 | 	if (a >= b) | 
 | 123 | 		return 1; | 
 | 124 |  | 
 | 125 | 	return 0; | 
 | 126 | } | 
 | 127 |  | 
 | 128 | static int of_bus_default_map(u32 *addr, const u32 *range, | 
 | 129 | 			      int na, int ns, int pna) | 
 | 130 | { | 
 | 131 | 	u32 result[OF_MAX_ADDR_CELLS]; | 
 | 132 | 	int i; | 
 | 133 |  | 
 | 134 | 	if (ns > 2) { | 
 | 135 | 		printk("of_device: Cannot handle size cells (%d) > 2.", ns); | 
 | 136 | 		return -EINVAL; | 
 | 137 | 	} | 
 | 138 |  | 
 | 139 | 	if (of_out_of_range(addr, range, range + na + pna, na, ns)) | 
 | 140 | 		return -EINVAL; | 
 | 141 |  | 
 | 142 | 	/* Start with the parent range base.  */ | 
 | 143 | 	memcpy(result, range + na, pna * 4); | 
 | 144 |  | 
 | 145 | 	/* Add in the child address offset.  */ | 
 | 146 | 	for (i = 0; i < na; i++) | 
 | 147 | 		result[pna - 1 - i] += | 
 | 148 | 			(addr[na - 1 - i] - | 
 | 149 | 			 range[na - 1 - i]); | 
 | 150 |  | 
 | 151 | 	memcpy(addr, result, pna * 4); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 152 |  | 
 | 153 | 	return 0; | 
 | 154 | } | 
 | 155 |  | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 156 | static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 157 | { | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 158 | 	if (flags) | 
 | 159 | 		return flags; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 160 | 	return IORESOURCE_MEM; | 
 | 161 | } | 
 | 162 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 163 | /* | 
 | 164 |  * PCI bus specific translator | 
 | 165 |  */ | 
 | 166 |  | 
 | 167 | static int of_bus_pci_match(struct device_node *np) | 
 | 168 | { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 169 | 	if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { | 
 | 170 | 		/* Do not do PCI specific frobbing if the | 
 | 171 | 		 * PCI bridge lacks a ranges property.  We | 
 | 172 | 		 * want to pass it through up to the next | 
 | 173 | 		 * parent as-is, not with the PCI translate | 
 | 174 | 		 * method which chops off the top address cell. | 
 | 175 | 		 */ | 
 | 176 | 		if (!of_find_property(np, "ranges", NULL)) | 
 | 177 | 			return 0; | 
 | 178 |  | 
 | 179 | 		return 1; | 
 | 180 | 	} | 
 | 181 |  | 
 | 182 | 	return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 183 | } | 
 | 184 |  | 
 | 185 | static void of_bus_pci_count_cells(struct device_node *np, | 
 | 186 | 				   int *addrc, int *sizec) | 
 | 187 | { | 
 | 188 | 	if (addrc) | 
 | 189 | 		*addrc = 3; | 
 | 190 | 	if (sizec) | 
 | 191 | 		*sizec = 2; | 
 | 192 | } | 
 | 193 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 194 | static int of_bus_pci_map(u32 *addr, const u32 *range, | 
 | 195 | 			  int na, int ns, int pna) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 196 | { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 197 | 	u32 result[OF_MAX_ADDR_CELLS]; | 
 | 198 | 	int i; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 199 |  | 
 | 200 | 	/* Check address type match */ | 
 | 201 | 	if ((addr[0] ^ range[0]) & 0x03000000) | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 202 | 		return -EINVAL; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 203 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 204 | 	if (of_out_of_range(addr + 1, range + 1, range + na + pna, | 
 | 205 | 			    na - 1, ns)) | 
 | 206 | 		return -EINVAL; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 207 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 208 | 	/* Start with the parent range base.  */ | 
 | 209 | 	memcpy(result, range + na, pna * 4); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 210 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 211 | 	/* Add in the child address offset, skipping high cell.  */ | 
 | 212 | 	for (i = 0; i < na - 1; i++) | 
 | 213 | 		result[pna - 1 - i] += | 
 | 214 | 			(addr[na - 1 - i] - | 
 | 215 | 			 range[na - 1 - i]); | 
 | 216 |  | 
 | 217 | 	memcpy(addr, result, pna * 4); | 
 | 218 |  | 
 | 219 | 	return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 220 | } | 
 | 221 |  | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 222 | static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 223 | { | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 224 | 	u32 w = addr[0]; | 
 | 225 |  | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 226 | 	/* For PCI, we override whatever child busses may have used.  */ | 
 | 227 | 	flags = 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 228 | 	switch((w >> 24) & 0x03) { | 
 | 229 | 	case 0x01: | 
 | 230 | 		flags |= IORESOURCE_IO; | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 231 | 		break; | 
 | 232 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 233 | 	case 0x02: /* 32 bits */ | 
 | 234 | 	case 0x03: /* 64 bits */ | 
 | 235 | 		flags |= IORESOURCE_MEM; | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 236 | 		break; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 237 | 	} | 
 | 238 | 	if (w & 0x40000000) | 
 | 239 | 		flags |= IORESOURCE_PREFETCH; | 
 | 240 | 	return flags; | 
 | 241 | } | 
 | 242 |  | 
 | 243 | /* | 
 | 244 |  * SBUS bus specific translator | 
 | 245 |  */ | 
 | 246 |  | 
 | 247 | static int of_bus_sbus_match(struct device_node *np) | 
 | 248 | { | 
 | 249 | 	return !strcmp(np->name, "sbus") || | 
 | 250 | 		!strcmp(np->name, "sbi"); | 
 | 251 | } | 
 | 252 |  | 
 | 253 | static void of_bus_sbus_count_cells(struct device_node *child, | 
 | 254 | 				   int *addrc, int *sizec) | 
 | 255 | { | 
 | 256 | 	if (addrc) | 
 | 257 | 		*addrc = 2; | 
 | 258 | 	if (sizec) | 
 | 259 | 		*sizec = 1; | 
 | 260 | } | 
 | 261 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 262 | static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 263 | { | 
 | 264 | 	return of_bus_default_map(addr, range, na, ns, pna); | 
 | 265 | } | 
 | 266 |  | 
| David S. Miller | bdba4d6 | 2008-09-10 23:38:51 -0700 | [diff] [blame] | 267 | static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 268 | { | 
 | 269 | 	return IORESOURCE_MEM; | 
 | 270 | } | 
 | 271 |  | 
 | 272 |  | 
 | 273 | /* | 
 | 274 |  * Array of bus specific translators | 
 | 275 |  */ | 
 | 276 |  | 
 | 277 | static struct of_bus of_busses[] = { | 
 | 278 | 	/* PCI */ | 
 | 279 | 	{ | 
 | 280 | 		.name = "pci", | 
 | 281 | 		.addr_prop_name = "assigned-addresses", | 
 | 282 | 		.match = of_bus_pci_match, | 
 | 283 | 		.count_cells = of_bus_pci_count_cells, | 
 | 284 | 		.map = of_bus_pci_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 285 | 		.get_flags = of_bus_pci_get_flags, | 
 | 286 | 	}, | 
 | 287 | 	/* SBUS */ | 
 | 288 | 	{ | 
 | 289 | 		.name = "sbus", | 
 | 290 | 		.addr_prop_name = "reg", | 
 | 291 | 		.match = of_bus_sbus_match, | 
 | 292 | 		.count_cells = of_bus_sbus_count_cells, | 
 | 293 | 		.map = of_bus_sbus_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 294 | 		.get_flags = of_bus_sbus_get_flags, | 
 | 295 | 	}, | 
 | 296 | 	/* Default */ | 
 | 297 | 	{ | 
 | 298 | 		.name = "default", | 
 | 299 | 		.addr_prop_name = "reg", | 
 | 300 | 		.match = NULL, | 
 | 301 | 		.count_cells = of_bus_default_count_cells, | 
 | 302 | 		.map = of_bus_default_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 303 | 		.get_flags = of_bus_default_get_flags, | 
 | 304 | 	}, | 
 | 305 | }; | 
 | 306 |  | 
 | 307 | static struct of_bus *of_match_bus(struct device_node *np) | 
 | 308 | { | 
 | 309 | 	int i; | 
 | 310 |  | 
 | 311 | 	for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | 
 | 312 | 		if (!of_busses[i].match || of_busses[i].match(np)) | 
 | 313 | 			return &of_busses[i]; | 
 | 314 | 	BUG(); | 
 | 315 | 	return NULL; | 
 | 316 | } | 
 | 317 |  | 
 | 318 | static int __init build_one_resource(struct device_node *parent, | 
 | 319 | 				     struct of_bus *bus, | 
 | 320 | 				     struct of_bus *pbus, | 
 | 321 | 				     u32 *addr, | 
 | 322 | 				     int na, int ns, int pna) | 
 | 323 | { | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 324 | 	const u32 *ranges; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 325 | 	unsigned int rlen; | 
 | 326 | 	int rone; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 327 |  | 
 | 328 | 	ranges = of_get_property(parent, "ranges", &rlen); | 
 | 329 | 	if (ranges == NULL || rlen == 0) { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 330 | 		u32 result[OF_MAX_ADDR_CELLS]; | 
 | 331 | 		int i; | 
 | 332 |  | 
 | 333 | 		memset(result, 0, pna * 4); | 
 | 334 | 		for (i = 0; i < na; i++) | 
 | 335 | 			result[pna - 1 - i] = | 
 | 336 | 				addr[na - 1 - i]; | 
 | 337 |  | 
 | 338 | 		memcpy(addr, result, pna * 4); | 
 | 339 | 		return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 340 | 	} | 
 | 341 |  | 
 | 342 | 	/* Now walk through the ranges */ | 
 | 343 | 	rlen /= 4; | 
 | 344 | 	rone = na + pna + ns; | 
 | 345 | 	for (; rlen >= rone; rlen -= rone, ranges += rone) { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 346 | 		if (!bus->map(addr, ranges, na, ns, pna)) | 
 | 347 | 			return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 348 | 	} | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 349 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 350 | 	return 1; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 351 | } | 
 | 352 |  | 
| David S. Miller | 5280267 | 2008-09-03 02:04:41 -0700 | [diff] [blame] | 353 | static int __init use_1to1_mapping(struct device_node *pp) | 
 | 354 | { | 
 | 355 | 	/* If we have a ranges property in the parent, use it.  */ | 
 | 356 | 	if (of_find_property(pp, "ranges", NULL) != NULL) | 
 | 357 | 		return 0; | 
 | 358 |  | 
 | 359 | 	/* Some SBUS devices use intermediate nodes to express | 
 | 360 | 	 * hierarchy within the device itself.  These aren't | 
 | 361 | 	 * real bus nodes, and don't have a 'ranges' property. | 
 | 362 | 	 * But, we should still pass the translation work up | 
 | 363 | 	 * to the SBUS itself. | 
 | 364 | 	 */ | 
 | 365 | 	if (!strcmp(pp->name, "dma") || | 
 | 366 | 	    !strcmp(pp->name, "espdma") || | 
 | 367 | 	    !strcmp(pp->name, "ledma") || | 
 | 368 | 	    !strcmp(pp->name, "lebuffer")) | 
 | 369 | 		return 0; | 
 | 370 |  | 
 | 371 | 	return 1; | 
 | 372 | } | 
 | 373 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 374 | static int of_resource_verbose; | 
 | 375 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 376 | static void __init build_device_resources(struct of_device *op, | 
 | 377 | 					  struct device *parent) | 
 | 378 | { | 
 | 379 | 	struct of_device *p_op; | 
 | 380 | 	struct of_bus *bus; | 
 | 381 | 	int na, ns; | 
 | 382 | 	int index, num_reg; | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 383 | 	const void *preg; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 384 |  | 
 | 385 | 	if (!parent) | 
 | 386 | 		return; | 
 | 387 |  | 
 | 388 | 	p_op = to_of_device(parent); | 
 | 389 | 	bus = of_match_bus(p_op->node); | 
 | 390 | 	bus->count_cells(op->node, &na, &ns); | 
 | 391 |  | 
 | 392 | 	preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | 
 | 393 | 	if (!preg || num_reg == 0) | 
 | 394 | 		return; | 
 | 395 |  | 
 | 396 | 	/* Convert to num-cells.  */ | 
 | 397 | 	num_reg /= 4; | 
 | 398 |  | 
 | 399 | 	/* Conver to num-entries.  */ | 
 | 400 | 	num_reg /= na + ns; | 
 | 401 |  | 
 | 402 | 	for (index = 0; index < num_reg; index++) { | 
 | 403 | 		struct resource *r = &op->resource[index]; | 
 | 404 | 		u32 addr[OF_MAX_ADDR_CELLS]; | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 405 | 		const u32 *reg = (preg + (index * ((na + ns) * 4))); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 406 | 		struct device_node *dp = op->node; | 
 | 407 | 		struct device_node *pp = p_op->node; | 
| David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 408 | 		struct of_bus *pbus, *dbus; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 409 | 		u64 size, result = OF_BAD_ADDR; | 
 | 410 | 		unsigned long flags; | 
 | 411 | 		int dna, dns; | 
 | 412 | 		int pna, pns; | 
 | 413 |  | 
 | 414 | 		size = of_read_addr(reg + na, ns); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 415 |  | 
 | 416 | 		memcpy(addr, reg, na * 4); | 
 | 417 |  | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 418 | 		flags = bus->get_flags(reg, 0); | 
 | 419 |  | 
| David S. Miller | 5280267 | 2008-09-03 02:04:41 -0700 | [diff] [blame] | 420 | 		if (use_1to1_mapping(pp)) { | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 421 | 			result = of_read_addr(addr, na); | 
 | 422 | 			goto build_res; | 
 | 423 | 		} | 
 | 424 |  | 
 | 425 | 		dna = na; | 
 | 426 | 		dns = ns; | 
| David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 427 | 		dbus = bus; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 428 |  | 
 | 429 | 		while (1) { | 
 | 430 | 			dp = pp; | 
 | 431 | 			pp = dp->parent; | 
 | 432 | 			if (!pp) { | 
 | 433 | 				result = of_read_addr(addr, dna); | 
 | 434 | 				break; | 
 | 435 | 			} | 
 | 436 |  | 
 | 437 | 			pbus = of_match_bus(pp); | 
 | 438 | 			pbus->count_cells(dp, &pna, &pns); | 
 | 439 |  | 
| David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 440 | 			if (build_one_resource(dp, dbus, pbus, addr, | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 441 | 					       dna, dns, pna)) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 442 | 				break; | 
 | 443 |  | 
| David S. Miller | e3c71a3 | 2008-08-28 21:02:58 -0700 | [diff] [blame] | 444 | 			flags = pbus->get_flags(addr, flags); | 
 | 445 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 446 | 			dna = pna; | 
 | 447 | 			dns = pns; | 
| David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 448 | 			dbus = pbus; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 449 | 		} | 
 | 450 |  | 
 | 451 | 	build_res: | 
 | 452 | 		memset(r, 0, sizeof(*r)); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 453 |  | 
 | 454 | 		if (of_resource_verbose) | 
 | 455 | 			printk("%s reg[%d] -> %llx\n", | 
 | 456 | 			       op->node->full_name, index, | 
 | 457 | 			       result); | 
 | 458 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 459 | 		if (result != OF_BAD_ADDR) { | 
| David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 460 | 			r->start = result & 0xffffffff; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 461 | 			r->end = result + size - 1; | 
| David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 462 | 			r->flags = flags | ((result >> 32ULL) & 0xffUL); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 463 | 		} | 
 | 464 | 		r->name = op->node->name; | 
 | 465 | 	} | 
 | 466 | } | 
 | 467 |  | 
 | 468 | static struct of_device * __init scan_one_device(struct device_node *dp, | 
 | 469 | 						 struct device *parent) | 
 | 470 | { | 
 | 471 | 	struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 472 | 	const struct linux_prom_irqs *intr; | 
| David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 473 | 	struct dev_archdata *sd; | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 474 | 	int len, i; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 475 |  | 
 | 476 | 	if (!op) | 
 | 477 | 		return NULL; | 
 | 478 |  | 
| David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 479 | 	sd = &op->dev.archdata; | 
 | 480 | 	sd->prom_node = dp; | 
 | 481 | 	sd->op = op; | 
 | 482 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 483 | 	op->node = dp; | 
 | 484 |  | 
 | 485 | 	op->clock_freq = of_getintprop_default(dp, "clock-frequency", | 
 | 486 | 					       (25*1000*1000)); | 
 | 487 | 	op->portid = of_getintprop_default(dp, "upa-portid", -1); | 
 | 488 | 	if (op->portid == -1) | 
 | 489 | 		op->portid = of_getintprop_default(dp, "portid", -1); | 
 | 490 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 491 | 	intr = of_get_property(dp, "intr", &len); | 
 | 492 | 	if (intr) { | 
 | 493 | 		op->num_irqs = len / sizeof(struct linux_prom_irqs); | 
 | 494 | 		for (i = 0; i < op->num_irqs; i++) | 
 | 495 | 			op->irqs[i] = intr[i].pri; | 
 | 496 | 	} else { | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 497 | 		const unsigned int *irq = | 
 | 498 | 			of_get_property(dp, "interrupts", &len); | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 499 |  | 
 | 500 | 		if (irq) { | 
 | 501 | 			op->num_irqs = len / sizeof(unsigned int); | 
 | 502 | 			for (i = 0; i < op->num_irqs; i++) | 
 | 503 | 				op->irqs[i] = irq[i]; | 
 | 504 | 		} else { | 
 | 505 | 			op->num_irqs = 0; | 
 | 506 | 		} | 
 | 507 | 	} | 
 | 508 | 	if (sparc_cpu_model == sun4d) { | 
 | 509 | 		static int pil_to_sbus[] = { | 
 | 510 | 			0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, | 
 | 511 | 		}; | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 512 | 		struct device_node *io_unit, *sbi = dp->parent; | 
| Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 513 | 		const struct linux_prom_registers *regs; | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 514 | 		int board, slot; | 
 | 515 |  | 
 | 516 | 		while (sbi) { | 
 | 517 | 			if (!strcmp(sbi->name, "sbi")) | 
 | 518 | 				break; | 
 | 519 |  | 
 | 520 | 			sbi = sbi->parent; | 
 | 521 | 		} | 
 | 522 | 		if (!sbi) | 
 | 523 | 			goto build_resources; | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 524 |  | 
 | 525 | 		regs = of_get_property(dp, "reg", NULL); | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 526 | 		if (!regs) | 
 | 527 | 			goto build_resources; | 
 | 528 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 529 | 		slot = regs->which_io; | 
 | 530 |  | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 531 | 		/* If SBI's parent is not io-unit or the io-unit lacks | 
 | 532 | 		 * a "board#" property, something is very wrong. | 
 | 533 | 		 */ | 
 | 534 | 		if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) { | 
 | 535 | 			printk("%s: Error, parent is not io-unit.\n", | 
 | 536 | 			       sbi->full_name); | 
 | 537 | 			goto build_resources; | 
 | 538 | 		} | 
 | 539 | 		io_unit = sbi->parent; | 
 | 540 | 		board = of_getintprop_default(io_unit, "board#", -1); | 
 | 541 | 		if (board == -1) { | 
 | 542 | 			printk("%s: Error, lacks board# property.\n", | 
 | 543 | 			       io_unit->full_name); | 
 | 544 | 			goto build_resources; | 
 | 545 | 		} | 
 | 546 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 547 | 		for (i = 0; i < op->num_irqs; i++) { | 
 | 548 | 			int this_irq = op->irqs[i]; | 
 | 549 | 			int sbusl = pil_to_sbus[this_irq]; | 
 | 550 |  | 
 | 551 | 			if (sbusl) | 
 | 552 | 				this_irq = (((board + 1) << 5) + | 
 | 553 | 					    (sbusl << 2) + | 
 | 554 | 					    slot); | 
 | 555 |  | 
 | 556 | 			op->irqs[i] = this_irq; | 
 | 557 | 		} | 
 | 558 | 	} | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 559 |  | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 560 | build_resources: | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 561 | 	build_device_resources(op, parent); | 
 | 562 |  | 
 | 563 | 	op->dev.parent = parent; | 
| Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 564 | 	op->dev.bus = &of_platform_bus_type; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 565 | 	if (!parent) | 
 | 566 | 		strcpy(op->dev.bus_id, "root"); | 
 | 567 | 	else | 
| David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 568 | 		sprintf(op->dev.bus_id, "%08x", dp->node); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 569 |  | 
 | 570 | 	if (of_device_register(op)) { | 
 | 571 | 		printk("%s: Could not register of device.\n", | 
 | 572 | 		       dp->full_name); | 
 | 573 | 		kfree(op); | 
 | 574 | 		op = NULL; | 
 | 575 | 	} | 
 | 576 |  | 
 | 577 | 	return op; | 
 | 578 | } | 
 | 579 |  | 
 | 580 | static void __init scan_tree(struct device_node *dp, struct device *parent) | 
 | 581 | { | 
 | 582 | 	while (dp) { | 
 | 583 | 		struct of_device *op = scan_one_device(dp, parent); | 
 | 584 |  | 
 | 585 | 		if (op) | 
 | 586 | 			scan_tree(dp->child, &op->dev); | 
 | 587 |  | 
 | 588 | 		dp = dp->sibling; | 
 | 589 | 	} | 
 | 590 | } | 
 | 591 |  | 
 | 592 | static void __init scan_of_devices(void) | 
 | 593 | { | 
 | 594 | 	struct device_node *root = of_find_node_by_path("/"); | 
 | 595 | 	struct of_device *parent; | 
 | 596 |  | 
 | 597 | 	parent = scan_one_device(root, NULL); | 
 | 598 | 	if (!parent) | 
 | 599 | 		return; | 
 | 600 |  | 
 | 601 | 	scan_tree(root->child, &parent->dev); | 
 | 602 | } | 
 | 603 |  | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 604 | static int __init of_bus_driver_init(void) | 
 | 605 | { | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 606 | 	int err; | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 607 |  | 
| Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 608 | 	err = of_bus_type_init(&of_platform_bus_type, "of"); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 609 | 	if (!err) | 
 | 610 | 		scan_of_devices(); | 
 | 611 |  | 
 | 612 | 	return err; | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 613 | } | 
 | 614 |  | 
 | 615 | postcore_initcall(of_bus_driver_init); | 
 | 616 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 617 | static int __init of_debug(char *str) | 
 | 618 | { | 
 | 619 | 	int val = 0; | 
 | 620 |  | 
 | 621 | 	get_option(&str, &val); | 
 | 622 | 	if (val & 1) | 
 | 623 | 		of_resource_verbose = 1; | 
 | 624 | 	return 1; | 
 | 625 | } | 
 | 626 |  | 
 | 627 | __setup("of_debug=", of_debug); |