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