| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 1 | #include <linux/config.h> | 
|  | 2 | #include <linux/string.h> | 
|  | 3 | #include <linux/kernel.h> | 
|  | 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 |  | 
|  | 12 | /** | 
|  | 13 | * of_match_device - Tell if an of_device structure has a matching | 
|  | 14 | * of_match structure | 
|  | 15 | * @ids: array of of device match structures to search in | 
|  | 16 | * @dev: the of device structure to match against | 
|  | 17 | * | 
|  | 18 | * Used by a driver to check whether an of_device present in the | 
|  | 19 | * system is in its list of supported devices. | 
|  | 20 | */ | 
|  | 21 | const struct of_device_id *of_match_device(const struct of_device_id *matches, | 
|  | 22 | const struct of_device *dev) | 
|  | 23 | { | 
|  | 24 | if (!dev->node) | 
|  | 25 | return NULL; | 
|  | 26 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { | 
|  | 27 | int match = 1; | 
|  | 28 | if (matches->name[0]) | 
|  | 29 | match &= dev->node->name | 
|  | 30 | && !strcmp(matches->name, dev->node->name); | 
|  | 31 | if (matches->type[0]) | 
|  | 32 | match &= dev->node->type | 
|  | 33 | && !strcmp(matches->type, dev->node->type); | 
|  | 34 | if (matches->compatible[0]) | 
|  | 35 | match &= of_device_is_compatible(dev->node, | 
|  | 36 | matches->compatible); | 
|  | 37 | if (match) | 
|  | 38 | return matches; | 
|  | 39 | matches++; | 
|  | 40 | } | 
|  | 41 | return NULL; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) | 
|  | 45 | { | 
|  | 46 | struct of_device * of_dev = to_of_device(dev); | 
|  | 47 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); | 
|  | 48 | const struct of_device_id * matches = of_drv->match_table; | 
|  | 49 |  | 
|  | 50 | if (!matches) | 
|  | 51 | return 0; | 
|  | 52 |  | 
|  | 53 | return of_match_device(matches, of_dev) != NULL; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | struct of_device *of_dev_get(struct of_device *dev) | 
|  | 57 | { | 
|  | 58 | struct device *tmp; | 
|  | 59 |  | 
|  | 60 | if (!dev) | 
|  | 61 | return NULL; | 
|  | 62 | tmp = get_device(&dev->dev); | 
|  | 63 | if (tmp) | 
|  | 64 | return to_of_device(tmp); | 
|  | 65 | else | 
|  | 66 | return NULL; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | void of_dev_put(struct of_device *dev) | 
|  | 70 | { | 
|  | 71 | if (dev) | 
|  | 72 | put_device(&dev->dev); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 |  | 
|  | 76 | static int of_device_probe(struct device *dev) | 
|  | 77 | { | 
|  | 78 | int error = -ENODEV; | 
|  | 79 | struct of_platform_driver *drv; | 
|  | 80 | struct of_device *of_dev; | 
|  | 81 | const struct of_device_id *match; | 
|  | 82 |  | 
|  | 83 | drv = to_of_platform_driver(dev->driver); | 
|  | 84 | of_dev = to_of_device(dev); | 
|  | 85 |  | 
|  | 86 | if (!drv->probe) | 
|  | 87 | return error; | 
|  | 88 |  | 
|  | 89 | of_dev_get(of_dev); | 
|  | 90 |  | 
|  | 91 | match = of_match_device(drv->match_table, of_dev); | 
|  | 92 | if (match) | 
|  | 93 | error = drv->probe(of_dev, match); | 
|  | 94 | if (error) | 
|  | 95 | of_dev_put(of_dev); | 
|  | 96 |  | 
|  | 97 | return error; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static int of_device_remove(struct device *dev) | 
|  | 101 | { | 
|  | 102 | struct of_device * of_dev = to_of_device(dev); | 
|  | 103 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | 
|  | 104 |  | 
|  | 105 | if (dev->driver && drv->remove) | 
|  | 106 | drv->remove(of_dev); | 
|  | 107 | return 0; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static int of_device_suspend(struct device *dev, pm_message_t state) | 
|  | 111 | { | 
|  | 112 | struct of_device * of_dev = to_of_device(dev); | 
|  | 113 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | 
|  | 114 | int error = 0; | 
|  | 115 |  | 
|  | 116 | if (dev->driver && drv->suspend) | 
|  | 117 | error = drv->suspend(of_dev, state); | 
|  | 118 | return error; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | static int of_device_resume(struct device * dev) | 
|  | 122 | { | 
|  | 123 | struct of_device * of_dev = to_of_device(dev); | 
|  | 124 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | 
|  | 125 | int error = 0; | 
|  | 126 |  | 
|  | 127 | if (dev->driver && drv->resume) | 
|  | 128 | error = drv->resume(of_dev); | 
|  | 129 | return error; | 
|  | 130 | } | 
|  | 131 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 132 | static int node_match(struct device *dev, void *data) | 
|  | 133 | { | 
|  | 134 | struct of_device *op = to_of_device(dev); | 
|  | 135 | struct device_node *dp = data; | 
|  | 136 |  | 
|  | 137 | return (op->node == dp); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | struct of_device *of_find_device_by_node(struct device_node *dp) | 
|  | 141 | { | 
|  | 142 | struct device *dev = bus_find_device(&of_bus_type, NULL, | 
|  | 143 | dp, node_match); | 
|  | 144 |  | 
|  | 145 | if (dev) | 
|  | 146 | return to_of_device(dev); | 
|  | 147 |  | 
|  | 148 | return NULL; | 
|  | 149 | } | 
|  | 150 | EXPORT_SYMBOL(of_find_device_by_node); | 
|  | 151 |  | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 152 | #ifdef CONFIG_PCI | 
|  | 153 | struct bus_type ebus_bus_type = { | 
|  | 154 | .name	= "ebus", | 
|  | 155 | .match	= of_platform_bus_match, | 
|  | 156 | .probe	= of_device_probe, | 
|  | 157 | .remove	= of_device_remove, | 
|  | 158 | .suspend	= of_device_suspend, | 
|  | 159 | .resume	= of_device_resume, | 
|  | 160 | }; | 
| David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 161 | EXPORT_SYMBOL(ebus_bus_type); | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 162 | #endif | 
|  | 163 |  | 
|  | 164 | #ifdef CONFIG_SBUS | 
|  | 165 | struct bus_type sbus_bus_type = { | 
|  | 166 | .name	= "sbus", | 
|  | 167 | .match	= of_platform_bus_match, | 
|  | 168 | .probe	= of_device_probe, | 
|  | 169 | .remove	= of_device_remove, | 
|  | 170 | .suspend	= of_device_suspend, | 
|  | 171 | .resume	= of_device_resume, | 
|  | 172 | }; | 
| David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 173 | EXPORT_SYMBOL(sbus_bus_type); | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 174 | #endif | 
|  | 175 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 176 | struct bus_type of_bus_type = { | 
|  | 177 | .name	= "of", | 
|  | 178 | .match	= of_platform_bus_match, | 
|  | 179 | .probe	= of_device_probe, | 
|  | 180 | .remove	= of_device_remove, | 
|  | 181 | .suspend	= of_device_suspend, | 
|  | 182 | .resume	= of_device_resume, | 
|  | 183 | }; | 
|  | 184 | EXPORT_SYMBOL(of_bus_type); | 
|  | 185 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 186 | static inline u64 of_read_addr(const u32 *cell, int size) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 187 | { | 
|  | 188 | u64 r = 0; | 
|  | 189 | while (size--) | 
|  | 190 | r = (r << 32) | *(cell++); | 
|  | 191 | return r; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | static void __init get_cells(struct device_node *dp, | 
|  | 195 | int *addrc, int *sizec) | 
|  | 196 | { | 
|  | 197 | if (addrc) | 
|  | 198 | *addrc = of_n_addr_cells(dp); | 
|  | 199 | if (sizec) | 
|  | 200 | *sizec = of_n_size_cells(dp); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | /* Max address size we deal with */ | 
|  | 204 | #define OF_MAX_ADDR_CELLS	4 | 
|  | 205 |  | 
|  | 206 | struct of_bus { | 
|  | 207 | const char	*name; | 
|  | 208 | const char	*addr_prop_name; | 
|  | 209 | int		(*match)(struct device_node *parent); | 
|  | 210 | void		(*count_cells)(struct device_node *child, | 
|  | 211 | int *addrc, int *sizec); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 212 | int		(*map)(u32 *addr, const u32 *range, | 
|  | 213 | int na, int ns, int pna); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 214 | unsigned int	(*get_flags)(u32 *addr); | 
|  | 215 | }; | 
|  | 216 |  | 
|  | 217 | /* | 
|  | 218 | * Default translator (generic bus) | 
|  | 219 | */ | 
|  | 220 |  | 
|  | 221 | static void of_bus_default_count_cells(struct device_node *dev, | 
|  | 222 | int *addrc, int *sizec) | 
|  | 223 | { | 
|  | 224 | get_cells(dev, addrc, sizec); | 
|  | 225 | } | 
|  | 226 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 227 | /* Make sure the least significant 64-bits are in-range.  Even | 
|  | 228 | * for 3 or 4 cell values it is a good enough approximation. | 
|  | 229 | */ | 
|  | 230 | static int of_out_of_range(const u32 *addr, const u32 *base, | 
|  | 231 | const u32 *size, int na, int ns) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 232 | { | 
|  | 233 | u64 a = of_read_addr(addr, na); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 234 | u64 b = of_read_addr(base, na); | 
|  | 235 |  | 
|  | 236 | if (a < b) | 
|  | 237 | return 1; | 
|  | 238 |  | 
|  | 239 | b += of_read_addr(size, ns); | 
|  | 240 | if (a >= b) | 
|  | 241 | return 1; | 
|  | 242 |  | 
|  | 243 | return 0; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static int of_bus_default_map(u32 *addr, const u32 *range, | 
|  | 247 | int na, int ns, int pna) | 
|  | 248 | { | 
|  | 249 | u32 result[OF_MAX_ADDR_CELLS]; | 
|  | 250 | int i; | 
|  | 251 |  | 
|  | 252 | if (ns > 2) { | 
|  | 253 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | 
|  | 254 | return -EINVAL; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | 
|  | 258 | return -EINVAL; | 
|  | 259 |  | 
|  | 260 | /* Start with the parent range base.  */ | 
|  | 261 | memcpy(result, range + na, pna * 4); | 
|  | 262 |  | 
|  | 263 | /* Add in the child address offset.  */ | 
|  | 264 | for (i = 0; i < na; i++) | 
|  | 265 | result[pna - 1 - i] += | 
|  | 266 | (addr[na - 1 - i] - | 
|  | 267 | range[na - 1 - i]); | 
|  | 268 |  | 
|  | 269 | memcpy(addr, result, pna * 4); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | return 0; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | static unsigned int of_bus_default_get_flags(u32 *addr) | 
|  | 275 | { | 
|  | 276 | return IORESOURCE_MEM; | 
|  | 277 | } | 
|  | 278 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 279 | /* | 
|  | 280 | * PCI bus specific translator | 
|  | 281 | */ | 
|  | 282 |  | 
|  | 283 | static int of_bus_pci_match(struct device_node *np) | 
|  | 284 | { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 285 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { | 
|  | 286 | /* Do not do PCI specific frobbing if the | 
|  | 287 | * PCI bridge lacks a ranges property.  We | 
|  | 288 | * want to pass it through up to the next | 
|  | 289 | * parent as-is, not with the PCI translate | 
|  | 290 | * method which chops off the top address cell. | 
|  | 291 | */ | 
|  | 292 | if (!of_find_property(np, "ranges", NULL)) | 
|  | 293 | return 0; | 
|  | 294 |  | 
|  | 295 | return 1; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
|  | 301 | static void of_bus_pci_count_cells(struct device_node *np, | 
|  | 302 | int *addrc, int *sizec) | 
|  | 303 | { | 
|  | 304 | if (addrc) | 
|  | 305 | *addrc = 3; | 
|  | 306 | if (sizec) | 
|  | 307 | *sizec = 2; | 
|  | 308 | } | 
|  | 309 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 310 | static int of_bus_pci_map(u32 *addr, const u32 *range, | 
|  | 311 | int na, int ns, int pna) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 312 | { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 313 | u32 result[OF_MAX_ADDR_CELLS]; | 
|  | 314 | int i; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 315 |  | 
|  | 316 | /* Check address type match */ | 
|  | 317 | if ((addr[0] ^ range[0]) & 0x03000000) | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 318 | return -EINVAL; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 319 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 320 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, | 
|  | 321 | na - 1, ns)) | 
|  | 322 | return -EINVAL; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 323 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 324 | /* Start with the parent range base.  */ | 
|  | 325 | memcpy(result, range + na, pna * 4); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 326 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 327 | /* Add in the child address offset, skipping high cell.  */ | 
|  | 328 | for (i = 0; i < na - 1; i++) | 
|  | 329 | result[pna - 1 - i] += | 
|  | 330 | (addr[na - 1 - i] - | 
|  | 331 | range[na - 1 - i]); | 
|  | 332 |  | 
|  | 333 | memcpy(addr, result, pna * 4); | 
|  | 334 |  | 
|  | 335 | return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
|  | 338 | static unsigned int of_bus_pci_get_flags(u32 *addr) | 
|  | 339 | { | 
|  | 340 | unsigned int flags = 0; | 
|  | 341 | u32 w = addr[0]; | 
|  | 342 |  | 
|  | 343 | switch((w >> 24) & 0x03) { | 
|  | 344 | case 0x01: | 
|  | 345 | flags |= IORESOURCE_IO; | 
|  | 346 | case 0x02: /* 32 bits */ | 
|  | 347 | case 0x03: /* 64 bits */ | 
|  | 348 | flags |= IORESOURCE_MEM; | 
|  | 349 | } | 
|  | 350 | if (w & 0x40000000) | 
|  | 351 | flags |= IORESOURCE_PREFETCH; | 
|  | 352 | return flags; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | /* | 
|  | 356 | * SBUS bus specific translator | 
|  | 357 | */ | 
|  | 358 |  | 
|  | 359 | static int of_bus_sbus_match(struct device_node *np) | 
|  | 360 | { | 
|  | 361 | return !strcmp(np->name, "sbus") || | 
|  | 362 | !strcmp(np->name, "sbi"); | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | static void of_bus_sbus_count_cells(struct device_node *child, | 
|  | 366 | int *addrc, int *sizec) | 
|  | 367 | { | 
|  | 368 | if (addrc) | 
|  | 369 | *addrc = 2; | 
|  | 370 | if (sizec) | 
|  | 371 | *sizec = 1; | 
|  | 372 | } | 
|  | 373 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 374 | 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] | 375 | { | 
|  | 376 | return of_bus_default_map(addr, range, na, ns, pna); | 
|  | 377 | } | 
|  | 378 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 379 | static unsigned int of_bus_sbus_get_flags(u32 *addr) | 
|  | 380 | { | 
|  | 381 | return IORESOURCE_MEM; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 |  | 
|  | 385 | /* | 
|  | 386 | * Array of bus specific translators | 
|  | 387 | */ | 
|  | 388 |  | 
|  | 389 | static struct of_bus of_busses[] = { | 
|  | 390 | /* PCI */ | 
|  | 391 | { | 
|  | 392 | .name = "pci", | 
|  | 393 | .addr_prop_name = "assigned-addresses", | 
|  | 394 | .match = of_bus_pci_match, | 
|  | 395 | .count_cells = of_bus_pci_count_cells, | 
|  | 396 | .map = of_bus_pci_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 397 | .get_flags = of_bus_pci_get_flags, | 
|  | 398 | }, | 
|  | 399 | /* SBUS */ | 
|  | 400 | { | 
|  | 401 | .name = "sbus", | 
|  | 402 | .addr_prop_name = "reg", | 
|  | 403 | .match = of_bus_sbus_match, | 
|  | 404 | .count_cells = of_bus_sbus_count_cells, | 
|  | 405 | .map = of_bus_sbus_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 406 | .get_flags = of_bus_sbus_get_flags, | 
|  | 407 | }, | 
|  | 408 | /* Default */ | 
|  | 409 | { | 
|  | 410 | .name = "default", | 
|  | 411 | .addr_prop_name = "reg", | 
|  | 412 | .match = NULL, | 
|  | 413 | .count_cells = of_bus_default_count_cells, | 
|  | 414 | .map = of_bus_default_map, | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 415 | .get_flags = of_bus_default_get_flags, | 
|  | 416 | }, | 
|  | 417 | }; | 
|  | 418 |  | 
|  | 419 | static struct of_bus *of_match_bus(struct device_node *np) | 
|  | 420 | { | 
|  | 421 | int i; | 
|  | 422 |  | 
|  | 423 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | 
|  | 424 | if (!of_busses[i].match || of_busses[i].match(np)) | 
|  | 425 | return &of_busses[i]; | 
|  | 426 | BUG(); | 
|  | 427 | return NULL; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | static int __init build_one_resource(struct device_node *parent, | 
|  | 431 | struct of_bus *bus, | 
|  | 432 | struct of_bus *pbus, | 
|  | 433 | u32 *addr, | 
|  | 434 | int na, int ns, int pna) | 
|  | 435 | { | 
|  | 436 | u32 *ranges; | 
|  | 437 | unsigned int rlen; | 
|  | 438 | int rone; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | ranges = of_get_property(parent, "ranges", &rlen); | 
|  | 441 | if (ranges == NULL || rlen == 0) { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 442 | u32 result[OF_MAX_ADDR_CELLS]; | 
|  | 443 | int i; | 
|  | 444 |  | 
|  | 445 | memset(result, 0, pna * 4); | 
|  | 446 | for (i = 0; i < na; i++) | 
|  | 447 | result[pna - 1 - i] = | 
|  | 448 | addr[na - 1 - i]; | 
|  | 449 |  | 
|  | 450 | memcpy(addr, result, pna * 4); | 
|  | 451 | return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 452 | } | 
|  | 453 |  | 
|  | 454 | /* Now walk through the ranges */ | 
|  | 455 | rlen /= 4; | 
|  | 456 | rone = na + pna + ns; | 
|  | 457 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 458 | if (!bus->map(addr, ranges, na, ns, pna)) | 
|  | 459 | return 0; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 460 | } | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 461 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 462 | return 1; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 463 | } | 
|  | 464 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 465 | static int of_resource_verbose; | 
|  | 466 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 467 | static void __init build_device_resources(struct of_device *op, | 
|  | 468 | struct device *parent) | 
|  | 469 | { | 
|  | 470 | struct of_device *p_op; | 
|  | 471 | struct of_bus *bus; | 
|  | 472 | int na, ns; | 
|  | 473 | int index, num_reg; | 
|  | 474 | void *preg; | 
|  | 475 |  | 
|  | 476 | if (!parent) | 
|  | 477 | return; | 
|  | 478 |  | 
|  | 479 | p_op = to_of_device(parent); | 
|  | 480 | bus = of_match_bus(p_op->node); | 
|  | 481 | bus->count_cells(op->node, &na, &ns); | 
|  | 482 |  | 
|  | 483 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | 
|  | 484 | if (!preg || num_reg == 0) | 
|  | 485 | return; | 
|  | 486 |  | 
|  | 487 | /* Convert to num-cells.  */ | 
|  | 488 | num_reg /= 4; | 
|  | 489 |  | 
|  | 490 | /* Conver to num-entries.  */ | 
|  | 491 | num_reg /= na + ns; | 
|  | 492 |  | 
|  | 493 | for (index = 0; index < num_reg; index++) { | 
|  | 494 | struct resource *r = &op->resource[index]; | 
|  | 495 | u32 addr[OF_MAX_ADDR_CELLS]; | 
|  | 496 | u32 *reg = (preg + (index * ((na + ns) * 4))); | 
|  | 497 | struct device_node *dp = op->node; | 
|  | 498 | struct device_node *pp = p_op->node; | 
|  | 499 | struct of_bus *pbus; | 
|  | 500 | u64 size, result = OF_BAD_ADDR; | 
|  | 501 | unsigned long flags; | 
|  | 502 | int dna, dns; | 
|  | 503 | int pna, pns; | 
|  | 504 |  | 
|  | 505 | size = of_read_addr(reg + na, ns); | 
|  | 506 | flags = bus->get_flags(reg); | 
|  | 507 |  | 
|  | 508 | memcpy(addr, reg, na * 4); | 
|  | 509 |  | 
|  | 510 | /* If the immediate parent has no ranges property to apply, | 
|  | 511 | * just use a 1<->1 mapping. | 
|  | 512 | */ | 
|  | 513 | if (of_find_property(pp, "ranges", NULL) == NULL) { | 
|  | 514 | result = of_read_addr(addr, na); | 
|  | 515 | goto build_res; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | dna = na; | 
|  | 519 | dns = ns; | 
|  | 520 |  | 
|  | 521 | while (1) { | 
|  | 522 | dp = pp; | 
|  | 523 | pp = dp->parent; | 
|  | 524 | if (!pp) { | 
|  | 525 | result = of_read_addr(addr, dna); | 
|  | 526 | break; | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | pbus = of_match_bus(pp); | 
|  | 530 | pbus->count_cells(dp, &pna, &pns); | 
|  | 531 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 532 | if (build_one_resource(dp, bus, pbus, addr, | 
|  | 533 | dna, dns, pna)) | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 534 | break; | 
|  | 535 |  | 
|  | 536 | dna = pna; | 
|  | 537 | dns = pns; | 
|  | 538 | bus = pbus; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | build_res: | 
|  | 542 | memset(r, 0, sizeof(*r)); | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 543 |  | 
|  | 544 | if (of_resource_verbose) | 
|  | 545 | printk("%s reg[%d] -> %llx\n", | 
|  | 546 | op->node->full_name, index, | 
|  | 547 | result); | 
|  | 548 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 549 | if (result != OF_BAD_ADDR) { | 
| David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 550 | r->start = result & 0xffffffff; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 551 | r->end = result + size - 1; | 
| David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 552 | r->flags = flags | ((result >> 32ULL) & 0xffUL); | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 553 | } else { | 
|  | 554 | r->start = ~0UL; | 
|  | 555 | r->end = ~0UL; | 
|  | 556 | } | 
|  | 557 | r->name = op->node->name; | 
|  | 558 | } | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | static struct of_device * __init scan_one_device(struct device_node *dp, | 
|  | 562 | struct device *parent) | 
|  | 563 | { | 
|  | 564 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 565 | struct linux_prom_irqs *intr; | 
|  | 566 | int len, i; | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 567 |  | 
|  | 568 | if (!op) | 
|  | 569 | return NULL; | 
|  | 570 |  | 
|  | 571 | op->node = dp; | 
|  | 572 |  | 
|  | 573 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | 
|  | 574 | (25*1000*1000)); | 
|  | 575 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | 
|  | 576 | if (op->portid == -1) | 
|  | 577 | op->portid = of_getintprop_default(dp, "portid", -1); | 
|  | 578 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 579 | intr = of_get_property(dp, "intr", &len); | 
|  | 580 | if (intr) { | 
|  | 581 | op->num_irqs = len / sizeof(struct linux_prom_irqs); | 
|  | 582 | for (i = 0; i < op->num_irqs; i++) | 
|  | 583 | op->irqs[i] = intr[i].pri; | 
|  | 584 | } else { | 
|  | 585 | unsigned int *irq = of_get_property(dp, "interrupts", &len); | 
|  | 586 |  | 
|  | 587 | if (irq) { | 
|  | 588 | op->num_irqs = len / sizeof(unsigned int); | 
|  | 589 | for (i = 0; i < op->num_irqs; i++) | 
|  | 590 | op->irqs[i] = irq[i]; | 
|  | 591 | } else { | 
|  | 592 | op->num_irqs = 0; | 
|  | 593 | } | 
|  | 594 | } | 
|  | 595 | if (sparc_cpu_model == sun4d) { | 
|  | 596 | static int pil_to_sbus[] = { | 
|  | 597 | 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, | 
|  | 598 | }; | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 599 | struct device_node *io_unit, *sbi = dp->parent; | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 600 | struct linux_prom_registers *regs; | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 601 | int board, slot; | 
|  | 602 |  | 
|  | 603 | while (sbi) { | 
|  | 604 | if (!strcmp(sbi->name, "sbi")) | 
|  | 605 | break; | 
|  | 606 |  | 
|  | 607 | sbi = sbi->parent; | 
|  | 608 | } | 
|  | 609 | if (!sbi) | 
|  | 610 | goto build_resources; | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 611 |  | 
|  | 612 | regs = of_get_property(dp, "reg", NULL); | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 613 | if (!regs) | 
|  | 614 | goto build_resources; | 
|  | 615 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 616 | slot = regs->which_io; | 
|  | 617 |  | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 618 | /* If SBI's parent is not io-unit or the io-unit lacks | 
|  | 619 | * a "board#" property, something is very wrong. | 
|  | 620 | */ | 
|  | 621 | if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) { | 
|  | 622 | printk("%s: Error, parent is not io-unit.\n", | 
|  | 623 | sbi->full_name); | 
|  | 624 | goto build_resources; | 
|  | 625 | } | 
|  | 626 | io_unit = sbi->parent; | 
|  | 627 | board = of_getintprop_default(io_unit, "board#", -1); | 
|  | 628 | if (board == -1) { | 
|  | 629 | printk("%s: Error, lacks board# property.\n", | 
|  | 630 | io_unit->full_name); | 
|  | 631 | goto build_resources; | 
|  | 632 | } | 
|  | 633 |  | 
| David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 634 | for (i = 0; i < op->num_irqs; i++) { | 
|  | 635 | int this_irq = op->irqs[i]; | 
|  | 636 | int sbusl = pil_to_sbus[this_irq]; | 
|  | 637 |  | 
|  | 638 | if (sbusl) | 
|  | 639 | this_irq = (((board + 1) << 5) + | 
|  | 640 | (sbusl << 2) + | 
|  | 641 | slot); | 
|  | 642 |  | 
|  | 643 | op->irqs[i] = this_irq; | 
|  | 644 | } | 
|  | 645 | } | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 646 |  | 
| David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 647 | build_resources: | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 648 | build_device_resources(op, parent); | 
|  | 649 |  | 
|  | 650 | op->dev.parent = parent; | 
|  | 651 | op->dev.bus = &of_bus_type; | 
|  | 652 | if (!parent) | 
|  | 653 | strcpy(op->dev.bus_id, "root"); | 
|  | 654 | else | 
|  | 655 | strcpy(op->dev.bus_id, dp->path_component_name); | 
|  | 656 |  | 
|  | 657 | if (of_device_register(op)) { | 
|  | 658 | printk("%s: Could not register of device.\n", | 
|  | 659 | dp->full_name); | 
|  | 660 | kfree(op); | 
|  | 661 | op = NULL; | 
|  | 662 | } | 
|  | 663 |  | 
|  | 664 | return op; | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | static void __init scan_tree(struct device_node *dp, struct device *parent) | 
|  | 668 | { | 
|  | 669 | while (dp) { | 
|  | 670 | struct of_device *op = scan_one_device(dp, parent); | 
|  | 671 |  | 
|  | 672 | if (op) | 
|  | 673 | scan_tree(dp->child, &op->dev); | 
|  | 674 |  | 
|  | 675 | dp = dp->sibling; | 
|  | 676 | } | 
|  | 677 | } | 
|  | 678 |  | 
|  | 679 | static void __init scan_of_devices(void) | 
|  | 680 | { | 
|  | 681 | struct device_node *root = of_find_node_by_path("/"); | 
|  | 682 | struct of_device *parent; | 
|  | 683 |  | 
|  | 684 | parent = scan_one_device(root, NULL); | 
|  | 685 | if (!parent) | 
|  | 686 | return; | 
|  | 687 |  | 
|  | 688 | scan_tree(root->child, &parent->dev); | 
|  | 689 | } | 
|  | 690 |  | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 691 | static int __init of_bus_driver_init(void) | 
|  | 692 | { | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 693 | int err; | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 694 |  | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 695 | err = bus_register(&of_bus_type); | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 696 | #ifdef CONFIG_PCI | 
|  | 697 | if (!err) | 
|  | 698 | err = bus_register(&ebus_bus_type); | 
|  | 699 | #endif | 
|  | 700 | #ifdef CONFIG_SBUS | 
|  | 701 | if (!err) | 
|  | 702 | err = bus_register(&sbus_bus_type); | 
|  | 703 | #endif | 
| David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 704 |  | 
|  | 705 | if (!err) | 
|  | 706 | scan_of_devices(); | 
|  | 707 |  | 
|  | 708 | return err; | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 709 | } | 
|  | 710 |  | 
|  | 711 | postcore_initcall(of_bus_driver_init); | 
|  | 712 |  | 
| David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 713 | static int __init of_debug(char *str) | 
|  | 714 | { | 
|  | 715 | int val = 0; | 
|  | 716 |  | 
|  | 717 | get_option(&str, &val); | 
|  | 718 | if (val & 1) | 
|  | 719 | of_resource_verbose = 1; | 
|  | 720 | return 1; | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | __setup("of_debug=", of_debug); | 
|  | 724 |  | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 725 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) | 
|  | 726 | { | 
|  | 727 | /* initialize common driver fields */ | 
|  | 728 | drv->driver.name = drv->name; | 
|  | 729 | drv->driver.bus = bus; | 
|  | 730 |  | 
|  | 731 | /* register with core */ | 
|  | 732 | return driver_register(&drv->driver); | 
|  | 733 | } | 
|  | 734 |  | 
|  | 735 | void of_unregister_driver(struct of_platform_driver *drv) | 
|  | 736 | { | 
|  | 737 | driver_unregister(&drv->driver); | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 |  | 
|  | 741 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) | 
|  | 742 | { | 
|  | 743 | struct of_device *ofdev; | 
|  | 744 |  | 
|  | 745 | ofdev = to_of_device(dev); | 
|  | 746 | return sprintf(buf, "%s", ofdev->node->full_name); | 
|  | 747 | } | 
|  | 748 |  | 
|  | 749 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); | 
|  | 750 |  | 
|  | 751 | /** | 
|  | 752 | * of_release_dev - free an of device structure when all users of it are finished. | 
|  | 753 | * @dev: device that's been disconnected | 
|  | 754 | * | 
|  | 755 | * Will be called only by the device core when all users of this of device are | 
|  | 756 | * done. | 
|  | 757 | */ | 
|  | 758 | void of_release_dev(struct device *dev) | 
|  | 759 | { | 
|  | 760 | struct of_device *ofdev; | 
|  | 761 |  | 
|  | 762 | ofdev = to_of_device(dev); | 
|  | 763 |  | 
|  | 764 | kfree(ofdev); | 
|  | 765 | } | 
|  | 766 |  | 
|  | 767 | int of_device_register(struct of_device *ofdev) | 
|  | 768 | { | 
|  | 769 | int rc; | 
|  | 770 |  | 
|  | 771 | BUG_ON(ofdev->node == NULL); | 
|  | 772 |  | 
|  | 773 | rc = device_register(&ofdev->dev); | 
|  | 774 | if (rc) | 
|  | 775 | return rc; | 
|  | 776 |  | 
| Andrew Morton | 6cc8b6f | 2006-07-10 15:28:54 -0700 | [diff] [blame] | 777 | rc = device_create_file(&ofdev->dev, &dev_attr_devspec); | 
|  | 778 | if (rc) | 
|  | 779 | device_unregister(&ofdev->dev); | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 780 |  | 
| Andrew Morton | 6cc8b6f | 2006-07-10 15:28:54 -0700 | [diff] [blame] | 781 | return rc; | 
| David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 782 | } | 
|  | 783 |  | 
|  | 784 | void of_device_unregister(struct of_device *ofdev) | 
|  | 785 | { | 
|  | 786 | device_remove_file(&ofdev->dev, &dev_attr_devspec); | 
|  | 787 | device_unregister(&ofdev->dev); | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | struct of_device* of_platform_device_create(struct device_node *np, | 
|  | 791 | const char *bus_id, | 
|  | 792 | struct device *parent, | 
|  | 793 | struct bus_type *bus) | 
|  | 794 | { | 
|  | 795 | struct of_device *dev; | 
|  | 796 |  | 
|  | 797 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | 
|  | 798 | if (!dev) | 
|  | 799 | return NULL; | 
|  | 800 | memset(dev, 0, sizeof(*dev)); | 
|  | 801 |  | 
|  | 802 | dev->dev.parent = parent; | 
|  | 803 | dev->dev.bus = bus; | 
|  | 804 | dev->dev.release = of_release_dev; | 
|  | 805 |  | 
|  | 806 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); | 
|  | 807 |  | 
|  | 808 | if (of_device_register(dev) != 0) { | 
|  | 809 | kfree(dev); | 
|  | 810 | return NULL; | 
|  | 811 | } | 
|  | 812 |  | 
|  | 813 | return dev; | 
|  | 814 | } | 
|  | 815 |  | 
|  | 816 | EXPORT_SYMBOL(of_match_device); | 
|  | 817 | EXPORT_SYMBOL(of_register_driver); | 
|  | 818 | EXPORT_SYMBOL(of_unregister_driver); | 
|  | 819 | EXPORT_SYMBOL(of_device_register); | 
|  | 820 | EXPORT_SYMBOL(of_device_unregister); | 
|  | 821 | EXPORT_SYMBOL(of_dev_get); | 
|  | 822 | EXPORT_SYMBOL(of_dev_put); | 
|  | 823 | EXPORT_SYMBOL(of_platform_device_create); | 
|  | 824 | EXPORT_SYMBOL(of_release_dev); |