David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1 | #include <linux/string.h> |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/mod_devicetable.h> |
| 6 | #include <linux/slab.h> |
| 7 | |
| 8 | #include <asm/errno.h> |
| 9 | #include <asm/of_device.h> |
| 10 | |
| 11 | /** |
| 12 | * of_match_device - Tell if an of_device structure has a matching |
| 13 | * of_match structure |
| 14 | * @ids: array of of device match structures to search in |
| 15 | * @dev: the of device structure to match against |
| 16 | * |
| 17 | * Used by a driver to check whether an of_device present in the |
| 18 | * system is in its list of supported devices. |
| 19 | */ |
| 20 | const struct of_device_id *of_match_device(const struct of_device_id *matches, |
| 21 | const struct of_device *dev) |
| 22 | { |
| 23 | if (!dev->node) |
| 24 | return NULL; |
| 25 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 26 | int match = 1; |
| 27 | if (matches->name[0]) |
| 28 | match &= dev->node->name |
| 29 | && !strcmp(matches->name, dev->node->name); |
| 30 | if (matches->type[0]) |
| 31 | match &= dev->node->type |
| 32 | && !strcmp(matches->type, dev->node->type); |
| 33 | if (matches->compatible[0]) |
| 34 | match &= of_device_is_compatible(dev->node, |
| 35 | matches->compatible); |
| 36 | if (match) |
| 37 | return matches; |
| 38 | matches++; |
| 39 | } |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) |
| 44 | { |
| 45 | struct of_device * of_dev = to_of_device(dev); |
| 46 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); |
| 47 | const struct of_device_id * matches = of_drv->match_table; |
| 48 | |
| 49 | if (!matches) |
| 50 | return 0; |
| 51 | |
| 52 | return of_match_device(matches, of_dev) != NULL; |
| 53 | } |
| 54 | |
| 55 | struct of_device *of_dev_get(struct of_device *dev) |
| 56 | { |
| 57 | struct device *tmp; |
| 58 | |
| 59 | if (!dev) |
| 60 | return NULL; |
| 61 | tmp = get_device(&dev->dev); |
| 62 | if (tmp) |
| 63 | return to_of_device(tmp); |
| 64 | else |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | void of_dev_put(struct of_device *dev) |
| 69 | { |
| 70 | if (dev) |
| 71 | put_device(&dev->dev); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static int of_device_probe(struct device *dev) |
| 76 | { |
| 77 | int error = -ENODEV; |
| 78 | struct of_platform_driver *drv; |
| 79 | struct of_device *of_dev; |
| 80 | const struct of_device_id *match; |
| 81 | |
| 82 | drv = to_of_platform_driver(dev->driver); |
| 83 | of_dev = to_of_device(dev); |
| 84 | |
| 85 | if (!drv->probe) |
| 86 | return error; |
| 87 | |
| 88 | of_dev_get(of_dev); |
| 89 | |
| 90 | match = of_match_device(drv->match_table, of_dev); |
| 91 | if (match) |
| 92 | error = drv->probe(of_dev, match); |
| 93 | if (error) |
| 94 | of_dev_put(of_dev); |
| 95 | |
| 96 | return error; |
| 97 | } |
| 98 | |
| 99 | static int of_device_remove(struct device *dev) |
| 100 | { |
| 101 | struct of_device * of_dev = to_of_device(dev); |
| 102 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 103 | |
| 104 | if (dev->driver && drv->remove) |
| 105 | drv->remove(of_dev); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int of_device_suspend(struct device *dev, pm_message_t state) |
| 110 | { |
| 111 | struct of_device * of_dev = to_of_device(dev); |
| 112 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 113 | int error = 0; |
| 114 | |
| 115 | if (dev->driver && drv->suspend) |
| 116 | error = drv->suspend(of_dev, state); |
| 117 | return error; |
| 118 | } |
| 119 | |
| 120 | static int of_device_resume(struct device * dev) |
| 121 | { |
| 122 | struct of_device * of_dev = to_of_device(dev); |
| 123 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 124 | int error = 0; |
| 125 | |
| 126 | if (dev->driver && drv->resume) |
| 127 | error = drv->resume(of_dev); |
| 128 | return error; |
| 129 | } |
| 130 | |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 131 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) |
| 132 | { |
| 133 | unsigned long ret = res->start + offset; |
David S. Miller | 6bda573 | 2006-10-18 23:00:35 -0700 | [diff] [blame] | 134 | struct resource *r; |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 135 | |
David S. Miller | 6bda573 | 2006-10-18 23:00:35 -0700 | [diff] [blame] | 136 | if (res->flags & IORESOURCE_MEM) |
| 137 | r = request_mem_region(ret, size, name); |
| 138 | else |
| 139 | r = request_region(ret, size, name); |
| 140 | if (!r) |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 141 | ret = 0; |
| 142 | |
| 143 | return (void __iomem *) ret; |
| 144 | } |
| 145 | EXPORT_SYMBOL(of_ioremap); |
| 146 | |
David S. Miller | e3a411a3 | 2006-12-28 21:01:32 -0800 | [diff] [blame] | 147 | void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 148 | { |
David S. Miller | e3a411a3 | 2006-12-28 21:01:32 -0800 | [diff] [blame] | 149 | if (res->flags & IORESOURCE_MEM) |
| 150 | release_mem_region((unsigned long) base, size); |
| 151 | else |
| 152 | release_region((unsigned long) base, size); |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 153 | } |
| 154 | EXPORT_SYMBOL(of_iounmap); |
| 155 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 156 | static int node_match(struct device *dev, void *data) |
| 157 | { |
| 158 | struct of_device *op = to_of_device(dev); |
| 159 | struct device_node *dp = data; |
| 160 | |
| 161 | return (op->node == dp); |
| 162 | } |
| 163 | |
| 164 | struct of_device *of_find_device_by_node(struct device_node *dp) |
| 165 | { |
| 166 | struct device *dev = bus_find_device(&of_bus_type, NULL, |
| 167 | dp, node_match); |
| 168 | |
| 169 | if (dev) |
| 170 | return to_of_device(dev); |
| 171 | |
| 172 | return NULL; |
| 173 | } |
| 174 | EXPORT_SYMBOL(of_find_device_by_node); |
| 175 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_PCI |
| 177 | struct bus_type isa_bus_type = { |
| 178 | .name = "isa", |
| 179 | .match = of_platform_bus_match, |
| 180 | .probe = of_device_probe, |
| 181 | .remove = of_device_remove, |
| 182 | .suspend = of_device_suspend, |
| 183 | .resume = of_device_resume, |
| 184 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 185 | EXPORT_SYMBOL(isa_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 186 | |
| 187 | struct bus_type ebus_bus_type = { |
| 188 | .name = "ebus", |
| 189 | .match = of_platform_bus_match, |
| 190 | .probe = of_device_probe, |
| 191 | .remove = of_device_remove, |
| 192 | .suspend = of_device_suspend, |
| 193 | .resume = of_device_resume, |
| 194 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 195 | EXPORT_SYMBOL(ebus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | #ifdef CONFIG_SBUS |
| 199 | struct bus_type sbus_bus_type = { |
| 200 | .name = "sbus", |
| 201 | .match = of_platform_bus_match, |
| 202 | .probe = of_device_probe, |
| 203 | .remove = of_device_remove, |
| 204 | .suspend = of_device_suspend, |
| 205 | .resume = of_device_resume, |
| 206 | }; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL(sbus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 208 | #endif |
| 209 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 210 | struct bus_type of_bus_type = { |
| 211 | .name = "of", |
| 212 | .match = of_platform_bus_match, |
| 213 | .probe = of_device_probe, |
| 214 | .remove = of_device_remove, |
| 215 | .suspend = of_device_suspend, |
| 216 | .resume = of_device_resume, |
| 217 | }; |
| 218 | EXPORT_SYMBOL(of_bus_type); |
| 219 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 220 | static inline u64 of_read_addr(const u32 *cell, int size) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 221 | { |
| 222 | u64 r = 0; |
| 223 | while (size--) |
| 224 | r = (r << 32) | *(cell++); |
| 225 | return r; |
| 226 | } |
| 227 | |
| 228 | static void __init get_cells(struct device_node *dp, |
| 229 | int *addrc, int *sizec) |
| 230 | { |
| 231 | if (addrc) |
| 232 | *addrc = of_n_addr_cells(dp); |
| 233 | if (sizec) |
| 234 | *sizec = of_n_size_cells(dp); |
| 235 | } |
| 236 | |
| 237 | /* Max address size we deal with */ |
| 238 | #define OF_MAX_ADDR_CELLS 4 |
| 239 | |
| 240 | struct of_bus { |
| 241 | const char *name; |
| 242 | const char *addr_prop_name; |
| 243 | int (*match)(struct device_node *parent); |
| 244 | void (*count_cells)(struct device_node *child, |
| 245 | int *addrc, int *sizec); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 246 | int (*map)(u32 *addr, const u32 *range, |
| 247 | int na, int ns, int pna); |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 248 | unsigned int (*get_flags)(const u32 *addr); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | /* |
| 252 | * Default translator (generic bus) |
| 253 | */ |
| 254 | |
| 255 | static void of_bus_default_count_cells(struct device_node *dev, |
| 256 | int *addrc, int *sizec) |
| 257 | { |
| 258 | get_cells(dev, addrc, sizec); |
| 259 | } |
| 260 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 261 | /* Make sure the least significant 64-bits are in-range. Even |
| 262 | * for 3 or 4 cell values it is a good enough approximation. |
| 263 | */ |
| 264 | static int of_out_of_range(const u32 *addr, const u32 *base, |
| 265 | const u32 *size, int na, int ns) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 266 | { |
| 267 | u64 a = of_read_addr(addr, na); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 268 | u64 b = of_read_addr(base, na); |
| 269 | |
| 270 | if (a < b) |
| 271 | return 1; |
| 272 | |
| 273 | b += of_read_addr(size, ns); |
| 274 | if (a >= b) |
| 275 | return 1; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int of_bus_default_map(u32 *addr, const u32 *range, |
| 281 | int na, int ns, int pna) |
| 282 | { |
| 283 | u32 result[OF_MAX_ADDR_CELLS]; |
| 284 | int i; |
| 285 | |
| 286 | if (ns > 2) { |
| 287 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | |
| 291 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | /* Start with the parent range base. */ |
| 295 | memcpy(result, range + na, pna * 4); |
| 296 | |
| 297 | /* Add in the child address offset. */ |
| 298 | for (i = 0; i < na; i++) |
| 299 | result[pna - 1 - i] += |
| 300 | (addr[na - 1 - i] - |
| 301 | range[na - 1 - i]); |
| 302 | |
| 303 | memcpy(addr, result, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 308 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 309 | { |
| 310 | return IORESOURCE_MEM; |
| 311 | } |
| 312 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 313 | /* |
| 314 | * PCI bus specific translator |
| 315 | */ |
| 316 | |
| 317 | static int of_bus_pci_match(struct device_node *np) |
| 318 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 319 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
David S. Miller | a165b42 | 2007-03-29 01:50:16 -0700 | [diff] [blame] | 320 | const char *model = of_get_property(np, "model", NULL); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 321 | |
| 322 | if (model && !strcmp(model, "SUNW,simba")) |
| 323 | return 0; |
| 324 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 325 | /* Do not do PCI specific frobbing if the |
| 326 | * PCI bridge lacks a ranges property. We |
| 327 | * want to pass it through up to the next |
| 328 | * parent as-is, not with the PCI translate |
| 329 | * method which chops off the top address cell. |
| 330 | */ |
| 331 | if (!of_find_property(np, "ranges", NULL)) |
| 332 | return 0; |
| 333 | |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 338 | } |
| 339 | |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 340 | static int of_bus_simba_match(struct device_node *np) |
| 341 | { |
David S. Miller | a165b42 | 2007-03-29 01:50:16 -0700 | [diff] [blame] | 342 | const char *model = of_get_property(np, "model", NULL); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 343 | |
| 344 | if (model && !strcmp(model, "SUNW,simba")) |
| 345 | return 1; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int of_bus_simba_map(u32 *addr, const u32 *range, |
| 350 | int na, int ns, int pna) |
| 351 | { |
| 352 | return 0; |
| 353 | } |
| 354 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 355 | static void of_bus_pci_count_cells(struct device_node *np, |
| 356 | int *addrc, int *sizec) |
| 357 | { |
| 358 | if (addrc) |
| 359 | *addrc = 3; |
| 360 | if (sizec) |
| 361 | *sizec = 2; |
| 362 | } |
| 363 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 364 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
| 365 | int na, int ns, int pna) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 366 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 367 | u32 result[OF_MAX_ADDR_CELLS]; |
| 368 | int i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 369 | |
| 370 | /* Check address type match */ |
| 371 | if ((addr[0] ^ range[0]) & 0x03000000) |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 372 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 373 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 374 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
| 375 | na - 1, ns)) |
| 376 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 377 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 378 | /* Start with the parent range base. */ |
| 379 | memcpy(result, range + na, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 380 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 381 | /* Add in the child address offset, skipping high cell. */ |
| 382 | for (i = 0; i < na - 1; i++) |
| 383 | result[pna - 1 - i] += |
| 384 | (addr[na - 1 - i] - |
| 385 | range[na - 1 - i]); |
| 386 | |
| 387 | memcpy(addr, result, pna * 4); |
| 388 | |
| 389 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 392 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 393 | { |
| 394 | unsigned int flags = 0; |
| 395 | u32 w = addr[0]; |
| 396 | |
| 397 | switch((w >> 24) & 0x03) { |
| 398 | case 0x01: |
| 399 | flags |= IORESOURCE_IO; |
| 400 | case 0x02: /* 32 bits */ |
| 401 | case 0x03: /* 64 bits */ |
| 402 | flags |= IORESOURCE_MEM; |
| 403 | } |
| 404 | if (w & 0x40000000) |
| 405 | flags |= IORESOURCE_PREFETCH; |
| 406 | return flags; |
| 407 | } |
| 408 | |
| 409 | /* |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 410 | * SBUS bus specific translator |
| 411 | */ |
| 412 | |
| 413 | static int of_bus_sbus_match(struct device_node *np) |
| 414 | { |
| 415 | return !strcmp(np->name, "sbus") || |
| 416 | !strcmp(np->name, "sbi"); |
| 417 | } |
| 418 | |
| 419 | static void of_bus_sbus_count_cells(struct device_node *child, |
| 420 | int *addrc, int *sizec) |
| 421 | { |
| 422 | if (addrc) |
| 423 | *addrc = 2; |
| 424 | if (sizec) |
| 425 | *sizec = 1; |
| 426 | } |
| 427 | |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 428 | /* |
| 429 | * FHC/Central bus specific translator. |
| 430 | * |
| 431 | * This is just needed to hard-code the address and size cell |
| 432 | * counts. 'fhc' and 'central' nodes lack the #address-cells and |
| 433 | * #size-cells properties, and if you walk to the root on such |
| 434 | * Enterprise boxes all you'll get is a #size-cells of 2 which is |
| 435 | * not what we want to use. |
| 436 | */ |
| 437 | static int of_bus_fhc_match(struct device_node *np) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 438 | { |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 439 | return !strcmp(np->name, "fhc") || |
| 440 | !strcmp(np->name, "central"); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 441 | } |
| 442 | |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 443 | #define of_bus_fhc_count_cells of_bus_sbus_count_cells |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * Array of bus specific translators |
| 447 | */ |
| 448 | |
| 449 | static struct of_bus of_busses[] = { |
| 450 | /* PCI */ |
| 451 | { |
| 452 | .name = "pci", |
| 453 | .addr_prop_name = "assigned-addresses", |
| 454 | .match = of_bus_pci_match, |
| 455 | .count_cells = of_bus_pci_count_cells, |
| 456 | .map = of_bus_pci_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 457 | .get_flags = of_bus_pci_get_flags, |
| 458 | }, |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 459 | /* SIMBA */ |
| 460 | { |
| 461 | .name = "simba", |
| 462 | .addr_prop_name = "assigned-addresses", |
| 463 | .match = of_bus_simba_match, |
| 464 | .count_cells = of_bus_pci_count_cells, |
| 465 | .map = of_bus_simba_map, |
| 466 | .get_flags = of_bus_pci_get_flags, |
| 467 | }, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 468 | /* SBUS */ |
| 469 | { |
| 470 | .name = "sbus", |
| 471 | .addr_prop_name = "reg", |
| 472 | .match = of_bus_sbus_match, |
| 473 | .count_cells = of_bus_sbus_count_cells, |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 474 | .map = of_bus_default_map, |
| 475 | .get_flags = of_bus_default_get_flags, |
| 476 | }, |
| 477 | /* FHC */ |
| 478 | { |
| 479 | .name = "fhc", |
| 480 | .addr_prop_name = "reg", |
| 481 | .match = of_bus_fhc_match, |
| 482 | .count_cells = of_bus_fhc_count_cells, |
| 483 | .map = of_bus_default_map, |
| 484 | .get_flags = of_bus_default_get_flags, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 485 | }, |
| 486 | /* Default */ |
| 487 | { |
| 488 | .name = "default", |
| 489 | .addr_prop_name = "reg", |
| 490 | .match = NULL, |
| 491 | .count_cells = of_bus_default_count_cells, |
| 492 | .map = of_bus_default_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 493 | .get_flags = of_bus_default_get_flags, |
| 494 | }, |
| 495 | }; |
| 496 | |
| 497 | static struct of_bus *of_match_bus(struct device_node *np) |
| 498 | { |
| 499 | int i; |
| 500 | |
| 501 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) |
| 502 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 503 | return &of_busses[i]; |
| 504 | BUG(); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static int __init build_one_resource(struct device_node *parent, |
| 509 | struct of_bus *bus, |
| 510 | struct of_bus *pbus, |
| 511 | u32 *addr, |
| 512 | int na, int ns, int pna) |
| 513 | { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 514 | const u32 *ranges; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 515 | unsigned int rlen; |
| 516 | int rone; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 517 | |
| 518 | ranges = of_get_property(parent, "ranges", &rlen); |
| 519 | if (ranges == NULL || rlen == 0) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 520 | u32 result[OF_MAX_ADDR_CELLS]; |
| 521 | int i; |
| 522 | |
| 523 | memset(result, 0, pna * 4); |
| 524 | for (i = 0; i < na; i++) |
| 525 | result[pna - 1 - i] = |
| 526 | addr[na - 1 - i]; |
| 527 | |
| 528 | memcpy(addr, result, pna * 4); |
| 529 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* Now walk through the ranges */ |
| 533 | rlen /= 4; |
| 534 | rone = na + pna + ns; |
| 535 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 536 | if (!bus->map(addr, ranges, na, ns, pna)) |
| 537 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 538 | } |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 539 | |
| 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | static int __init use_1to1_mapping(struct device_node *pp) |
| 544 | { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 545 | const char *model; |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 546 | |
| 547 | /* If this is on the PMU bus, don't try to translate it even |
| 548 | * if a ranges property exists. |
| 549 | */ |
| 550 | if (!strcmp(pp->name, "pmu")) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 551 | return 1; |
| 552 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 553 | /* If we have a ranges property in the parent, use it. */ |
| 554 | if (of_find_property(pp, "ranges", NULL) != NULL) |
| 555 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 556 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 557 | /* If the parent is the dma node of an ISA bus, pass |
| 558 | * the translation up to the root. |
| 559 | */ |
| 560 | if (!strcmp(pp->name, "dma")) |
| 561 | return 0; |
| 562 | |
| 563 | /* Similarly for Simba PCI bridges. */ |
| 564 | model = of_get_property(pp, "model", NULL); |
| 565 | if (model && !strcmp(model, "SUNW,simba")) |
| 566 | return 0; |
| 567 | |
| 568 | return 1; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 569 | } |
| 570 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 571 | static int of_resource_verbose; |
| 572 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 573 | static void __init build_device_resources(struct of_device *op, |
| 574 | struct device *parent) |
| 575 | { |
| 576 | struct of_device *p_op; |
| 577 | struct of_bus *bus; |
| 578 | int na, ns; |
| 579 | int index, num_reg; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 580 | const void *preg; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 581 | |
| 582 | if (!parent) |
| 583 | return; |
| 584 | |
| 585 | p_op = to_of_device(parent); |
| 586 | bus = of_match_bus(p_op->node); |
| 587 | bus->count_cells(op->node, &na, &ns); |
| 588 | |
| 589 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); |
| 590 | if (!preg || num_reg == 0) |
| 591 | return; |
| 592 | |
| 593 | /* Convert to num-cells. */ |
| 594 | num_reg /= 4; |
| 595 | |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 596 | /* Convert to num-entries. */ |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 597 | num_reg /= na + ns; |
| 598 | |
Simon Arlott | e5dd42e | 2007-05-11 13:52:08 -0700 | [diff] [blame^] | 599 | /* Prevent overrunning the op->resources[] array. */ |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 600 | if (num_reg > PROMREG_MAX) { |
| 601 | printk(KERN_WARNING "%s: Too many regs (%d), " |
| 602 | "limiting to %d.\n", |
| 603 | op->node->full_name, num_reg, PROMREG_MAX); |
| 604 | num_reg = PROMREG_MAX; |
| 605 | } |
| 606 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 607 | for (index = 0; index < num_reg; index++) { |
| 608 | struct resource *r = &op->resource[index]; |
| 609 | u32 addr[OF_MAX_ADDR_CELLS]; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 610 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 611 | struct device_node *dp = op->node; |
| 612 | struct device_node *pp = p_op->node; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 613 | struct of_bus *pbus, *dbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 614 | u64 size, result = OF_BAD_ADDR; |
| 615 | unsigned long flags; |
| 616 | int dna, dns; |
| 617 | int pna, pns; |
| 618 | |
| 619 | size = of_read_addr(reg + na, ns); |
| 620 | flags = bus->get_flags(reg); |
| 621 | |
| 622 | memcpy(addr, reg, na * 4); |
| 623 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 624 | if (use_1to1_mapping(pp)) { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 625 | result = of_read_addr(addr, na); |
| 626 | goto build_res; |
| 627 | } |
| 628 | |
| 629 | dna = na; |
| 630 | dns = ns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 631 | dbus = bus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 632 | |
| 633 | while (1) { |
| 634 | dp = pp; |
| 635 | pp = dp->parent; |
| 636 | if (!pp) { |
| 637 | result = of_read_addr(addr, dna); |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | pbus = of_match_bus(pp); |
| 642 | pbus->count_cells(dp, &pna, &pns); |
| 643 | |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 644 | if (build_one_resource(dp, dbus, pbus, addr, |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 645 | dna, dns, pna)) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 646 | break; |
| 647 | |
| 648 | dna = pna; |
| 649 | dns = pns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 650 | dbus = pbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | build_res: |
| 654 | memset(r, 0, sizeof(*r)); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 655 | |
| 656 | if (of_resource_verbose) |
| 657 | printk("%s reg[%d] -> %lx\n", |
| 658 | op->node->full_name, index, |
| 659 | result); |
| 660 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 661 | if (result != OF_BAD_ADDR) { |
David S. Miller | 1815aed | 2006-06-29 19:58:28 -0700 | [diff] [blame] | 662 | if (tlb_type == hypervisor) |
| 663 | result &= 0x0fffffffffffffffUL; |
| 664 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 665 | r->start = result; |
| 666 | r->end = result + size - 1; |
| 667 | r->flags = flags; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 668 | } |
| 669 | r->name = op->node->name; |
| 670 | } |
| 671 | } |
| 672 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 673 | static struct device_node * __init |
| 674 | apply_interrupt_map(struct device_node *dp, struct device_node *pp, |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 675 | const u32 *imap, int imlen, const u32 *imask, |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 676 | unsigned int *irq_p) |
| 677 | { |
| 678 | struct device_node *cp; |
| 679 | unsigned int irq = *irq_p; |
| 680 | struct of_bus *bus; |
| 681 | phandle handle; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 682 | const u32 *reg; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 683 | int na, num_reg, i; |
| 684 | |
| 685 | bus = of_match_bus(pp); |
| 686 | bus->count_cells(dp, &na, NULL); |
| 687 | |
| 688 | reg = of_get_property(dp, "reg", &num_reg); |
| 689 | if (!reg || !num_reg) |
| 690 | return NULL; |
| 691 | |
| 692 | imlen /= ((na + 3) * 4); |
| 693 | handle = 0; |
| 694 | for (i = 0; i < imlen; i++) { |
| 695 | int j; |
| 696 | |
| 697 | for (j = 0; j < na; j++) { |
| 698 | if ((reg[j] & imask[j]) != imap[j]) |
| 699 | goto next; |
| 700 | } |
| 701 | if (imap[na] == irq) { |
| 702 | handle = imap[na + 1]; |
| 703 | irq = imap[na + 2]; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | next: |
| 708 | imap += (na + 3); |
| 709 | } |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 710 | if (i == imlen) { |
| 711 | /* Psycho and Sabre PCI controllers can have 'interrupt-map' |
| 712 | * properties that do not include the on-board device |
| 713 | * interrupts. Instead, the device's 'interrupts' property |
| 714 | * is already a fully specified INO value. |
| 715 | * |
| 716 | * Handle this by deciding that, if we didn't get a |
| 717 | * match in the parent's 'interrupt-map', and the |
| 718 | * parent is an IRQ translater, then use the parent as |
| 719 | * our IRQ controller. |
| 720 | */ |
| 721 | if (pp->irq_trans) |
| 722 | return pp; |
| 723 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 724 | return NULL; |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 725 | } |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 726 | |
| 727 | *irq_p = irq; |
| 728 | cp = of_find_node_by_phandle(handle); |
| 729 | |
| 730 | return cp; |
| 731 | } |
| 732 | |
| 733 | static unsigned int __init pci_irq_swizzle(struct device_node *dp, |
| 734 | struct device_node *pp, |
| 735 | unsigned int irq) |
| 736 | { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 737 | const struct linux_prom_pci_registers *regs; |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 738 | unsigned int bus, devfn, slot, ret; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 739 | |
| 740 | if (irq < 1 || irq > 4) |
| 741 | return irq; |
| 742 | |
| 743 | regs = of_get_property(dp, "reg", NULL); |
| 744 | if (!regs) |
| 745 | return irq; |
| 746 | |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 747 | bus = (regs->phys_hi >> 16) & 0xff; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 748 | devfn = (regs->phys_hi >> 8) & 0xff; |
| 749 | slot = (devfn >> 3) & 0x1f; |
| 750 | |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 751 | if (pp->irq_trans) { |
| 752 | /* Derived from Table 8-3, U2P User's Manual. This branch |
| 753 | * is handling a PCI controller that lacks a proper set of |
| 754 | * interrupt-map and interrupt-map-mask properties. The |
| 755 | * Ultra-E450 is one example. |
| 756 | * |
| 757 | * The bit layout is BSSLL, where: |
| 758 | * B: 0 on bus A, 1 on bus B |
| 759 | * D: 2-bit slot number, derived from PCI device number as |
| 760 | * (dev - 1) for bus A, or (dev - 2) for bus B |
| 761 | * L: 2-bit line number |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 762 | */ |
| 763 | if (bus & 0x80) { |
| 764 | /* PBM-A */ |
| 765 | bus = 0x00; |
| 766 | slot = (slot - 1) << 2; |
| 767 | } else { |
| 768 | /* PBM-B */ |
| 769 | bus = 0x10; |
| 770 | slot = (slot - 2) << 2; |
| 771 | } |
| 772 | irq -= 1; |
| 773 | |
| 774 | ret = (bus | slot | irq); |
| 775 | } else { |
| 776 | /* Going through a PCI-PCI bridge that lacks a set of |
| 777 | * interrupt-map and interrupt-map-mask properties. |
| 778 | */ |
| 779 | ret = ((irq - 1 + (slot & 3)) & 3) + 1; |
| 780 | } |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 781 | |
| 782 | return ret; |
| 783 | } |
| 784 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 785 | static int of_irq_verbose; |
| 786 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 787 | static unsigned int __init build_one_device_irq(struct of_device *op, |
| 788 | struct device *parent, |
| 789 | unsigned int irq) |
| 790 | { |
| 791 | struct device_node *dp = op->node; |
| 792 | struct device_node *pp, *ip; |
| 793 | unsigned int orig_irq = irq; |
| 794 | |
| 795 | if (irq == 0xffffffff) |
| 796 | return irq; |
| 797 | |
| 798 | if (dp->irq_trans) { |
| 799 | irq = dp->irq_trans->irq_build(dp, irq, |
| 800 | dp->irq_trans->data); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 801 | |
| 802 | if (of_irq_verbose) |
| 803 | printk("%s: direct translate %x --> %x\n", |
| 804 | dp->full_name, orig_irq, irq); |
| 805 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 806 | return irq; |
| 807 | } |
| 808 | |
| 809 | /* Something more complicated. Walk up to the root, applying |
| 810 | * interrupt-map or bus specific translations, until we hit |
| 811 | * an IRQ translator. |
| 812 | * |
| 813 | * If we hit a bus type or situation we cannot handle, we |
| 814 | * stop and assume that the original IRQ number was in a |
| 815 | * format which has special meaning to it's immediate parent. |
| 816 | */ |
| 817 | pp = dp->parent; |
| 818 | ip = NULL; |
| 819 | while (pp) { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 820 | const void *imap, *imsk; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 821 | int imlen; |
| 822 | |
| 823 | imap = of_get_property(pp, "interrupt-map", &imlen); |
| 824 | imsk = of_get_property(pp, "interrupt-map-mask", NULL); |
| 825 | if (imap && imsk) { |
| 826 | struct device_node *iret; |
| 827 | int this_orig_irq = irq; |
| 828 | |
| 829 | iret = apply_interrupt_map(dp, pp, |
| 830 | imap, imlen, imsk, |
| 831 | &irq); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 832 | |
| 833 | if (of_irq_verbose) |
| 834 | printk("%s: Apply [%s:%x] imap --> [%s:%x]\n", |
| 835 | op->node->full_name, |
| 836 | pp->full_name, this_orig_irq, |
| 837 | (iret ? iret->full_name : "NULL"), irq); |
| 838 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 839 | if (!iret) |
| 840 | break; |
| 841 | |
| 842 | if (iret->irq_trans) { |
| 843 | ip = iret; |
| 844 | break; |
| 845 | } |
| 846 | } else { |
| 847 | if (!strcmp(pp->type, "pci") || |
| 848 | !strcmp(pp->type, "pciex")) { |
| 849 | unsigned int this_orig_irq = irq; |
| 850 | |
| 851 | irq = pci_irq_swizzle(dp, pp, irq); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 852 | if (of_irq_verbose) |
| 853 | printk("%s: PCI swizzle [%s] " |
| 854 | "%x --> %x\n", |
| 855 | op->node->full_name, |
| 856 | pp->full_name, this_orig_irq, |
| 857 | irq); |
| 858 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | if (pp->irq_trans) { |
| 862 | ip = pp; |
| 863 | break; |
| 864 | } |
| 865 | } |
| 866 | dp = pp; |
| 867 | pp = pp->parent; |
| 868 | } |
| 869 | if (!ip) |
| 870 | return orig_irq; |
| 871 | |
| 872 | irq = ip->irq_trans->irq_build(op->node, irq, |
| 873 | ip->irq_trans->data); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 874 | if (of_irq_verbose) |
| 875 | printk("%s: Apply IRQ trans [%s] %x --> %x\n", |
| 876 | op->node->full_name, ip->full_name, orig_irq, irq); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 877 | |
| 878 | return irq; |
| 879 | } |
| 880 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 881 | static struct of_device * __init scan_one_device(struct device_node *dp, |
| 882 | struct device *parent) |
| 883 | { |
| 884 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 885 | const unsigned int *irq; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 886 | int len, i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 887 | |
| 888 | if (!op) |
| 889 | return NULL; |
| 890 | |
| 891 | op->node = dp; |
| 892 | |
| 893 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", |
| 894 | (25*1000*1000)); |
| 895 | op->portid = of_getintprop_default(dp, "upa-portid", -1); |
| 896 | if (op->portid == -1) |
| 897 | op->portid = of_getintprop_default(dp, "portid", -1); |
| 898 | |
| 899 | irq = of_get_property(dp, "interrupts", &len); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 900 | if (irq) { |
| 901 | memcpy(op->irqs, irq, len); |
| 902 | op->num_irqs = len / 4; |
| 903 | } else { |
| 904 | op->num_irqs = 0; |
| 905 | } |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 906 | |
Simon Arlott | e5dd42e | 2007-05-11 13:52:08 -0700 | [diff] [blame^] | 907 | /* Prevent overrunning the op->irqs[] array. */ |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 908 | if (op->num_irqs > PROMINTR_MAX) { |
| 909 | printk(KERN_WARNING "%s: Too many irqs (%d), " |
| 910 | "limiting to %d.\n", |
| 911 | dp->full_name, op->num_irqs, PROMINTR_MAX); |
| 912 | op->num_irqs = PROMINTR_MAX; |
| 913 | } |
| 914 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 915 | build_device_resources(op, parent); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 916 | for (i = 0; i < op->num_irqs; i++) |
| 917 | op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 918 | |
| 919 | op->dev.parent = parent; |
| 920 | op->dev.bus = &of_bus_type; |
| 921 | if (!parent) |
| 922 | strcpy(op->dev.bus_id, "root"); |
| 923 | else |
David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 924 | sprintf(op->dev.bus_id, "%08x", dp->node); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 925 | |
| 926 | if (of_device_register(op)) { |
| 927 | printk("%s: Could not register of device.\n", |
| 928 | dp->full_name); |
| 929 | kfree(op); |
| 930 | op = NULL; |
| 931 | } |
| 932 | |
| 933 | return op; |
| 934 | } |
| 935 | |
| 936 | static void __init scan_tree(struct device_node *dp, struct device *parent) |
| 937 | { |
| 938 | while (dp) { |
| 939 | struct of_device *op = scan_one_device(dp, parent); |
| 940 | |
| 941 | if (op) |
| 942 | scan_tree(dp->child, &op->dev); |
| 943 | |
| 944 | dp = dp->sibling; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | static void __init scan_of_devices(void) |
| 949 | { |
| 950 | struct device_node *root = of_find_node_by_path("/"); |
| 951 | struct of_device *parent; |
| 952 | |
| 953 | parent = scan_one_device(root, NULL); |
| 954 | if (!parent) |
| 955 | return; |
| 956 | |
| 957 | scan_tree(root->child, &parent->dev); |
| 958 | } |
| 959 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 960 | static int __init of_bus_driver_init(void) |
| 961 | { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 962 | int err; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 963 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 964 | err = bus_register(&of_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 965 | #ifdef CONFIG_PCI |
| 966 | if (!err) |
| 967 | err = bus_register(&isa_bus_type); |
| 968 | if (!err) |
| 969 | err = bus_register(&ebus_bus_type); |
| 970 | #endif |
| 971 | #ifdef CONFIG_SBUS |
| 972 | if (!err) |
| 973 | err = bus_register(&sbus_bus_type); |
| 974 | #endif |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 975 | |
| 976 | if (!err) |
| 977 | scan_of_devices(); |
| 978 | |
| 979 | return err; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | postcore_initcall(of_bus_driver_init); |
| 983 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 984 | static int __init of_debug(char *str) |
| 985 | { |
| 986 | int val = 0; |
| 987 | |
| 988 | get_option(&str, &val); |
| 989 | if (val & 1) |
| 990 | of_resource_verbose = 1; |
| 991 | if (val & 2) |
| 992 | of_irq_verbose = 1; |
| 993 | return 1; |
| 994 | } |
| 995 | |
| 996 | __setup("of_debug=", of_debug); |
| 997 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 998 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
| 999 | { |
| 1000 | /* initialize common driver fields */ |
| 1001 | drv->driver.name = drv->name; |
| 1002 | drv->driver.bus = bus; |
| 1003 | |
| 1004 | /* register with core */ |
| 1005 | return driver_register(&drv->driver); |
| 1006 | } |
| 1007 | |
| 1008 | void of_unregister_driver(struct of_platform_driver *drv) |
| 1009 | { |
| 1010 | driver_unregister(&drv->driver); |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) |
| 1015 | { |
| 1016 | struct of_device *ofdev; |
| 1017 | |
| 1018 | ofdev = to_of_device(dev); |
| 1019 | return sprintf(buf, "%s", ofdev->node->full_name); |
| 1020 | } |
| 1021 | |
| 1022 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); |
| 1023 | |
| 1024 | /** |
| 1025 | * of_release_dev - free an of device structure when all users of it are finished. |
| 1026 | * @dev: device that's been disconnected |
| 1027 | * |
| 1028 | * Will be called only by the device core when all users of this of device are |
| 1029 | * done. |
| 1030 | */ |
| 1031 | void of_release_dev(struct device *dev) |
| 1032 | { |
| 1033 | struct of_device *ofdev; |
| 1034 | |
| 1035 | ofdev = to_of_device(dev); |
| 1036 | |
| 1037 | kfree(ofdev); |
| 1038 | } |
| 1039 | |
| 1040 | int of_device_register(struct of_device *ofdev) |
| 1041 | { |
| 1042 | int rc; |
| 1043 | |
| 1044 | BUG_ON(ofdev->node == NULL); |
| 1045 | |
| 1046 | rc = device_register(&ofdev->dev); |
| 1047 | if (rc) |
| 1048 | return rc; |
| 1049 | |
Andrew Morton | 6cc8b6f | 2006-07-10 15:28:54 -0700 | [diff] [blame] | 1050 | rc = device_create_file(&ofdev->dev, &dev_attr_devspec); |
| 1051 | if (rc) |
| 1052 | device_unregister(&ofdev->dev); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1053 | |
Andrew Morton | 6cc8b6f | 2006-07-10 15:28:54 -0700 | [diff] [blame] | 1054 | return rc; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | void of_device_unregister(struct of_device *ofdev) |
| 1058 | { |
| 1059 | device_remove_file(&ofdev->dev, &dev_attr_devspec); |
| 1060 | device_unregister(&ofdev->dev); |
| 1061 | } |
| 1062 | |
| 1063 | struct of_device* of_platform_device_create(struct device_node *np, |
| 1064 | const char *bus_id, |
| 1065 | struct device *parent, |
| 1066 | struct bus_type *bus) |
| 1067 | { |
| 1068 | struct of_device *dev; |
| 1069 | |
Yan Burman | 982c206 | 2006-11-30 17:13:09 -0800 | [diff] [blame] | 1070 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1071 | if (!dev) |
| 1072 | return NULL; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1073 | |
| 1074 | dev->dev.parent = parent; |
| 1075 | dev->dev.bus = bus; |
| 1076 | dev->dev.release = of_release_dev; |
| 1077 | |
| 1078 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); |
| 1079 | |
| 1080 | if (of_device_register(dev) != 0) { |
| 1081 | kfree(dev); |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | |
| 1085 | return dev; |
| 1086 | } |
| 1087 | |
| 1088 | EXPORT_SYMBOL(of_match_device); |
| 1089 | EXPORT_SYMBOL(of_register_driver); |
| 1090 | EXPORT_SYMBOL(of_unregister_driver); |
| 1091 | EXPORT_SYMBOL(of_device_register); |
| 1092 | EXPORT_SYMBOL(of_device_unregister); |
| 1093 | EXPORT_SYMBOL(of_dev_get); |
| 1094 | EXPORT_SYMBOL(of_dev_put); |
| 1095 | EXPORT_SYMBOL(of_platform_device_create); |
| 1096 | EXPORT_SYMBOL(of_release_dev); |