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