of_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #undef DEBUG
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/slab.h>
  19. #include <linux/pci.h>
  20. #include <asm/errno.h>
  21. #include <asm/dcr.h>
  22. #include <asm/of_device.h>
  23. #include <asm/of_platform.h>
  24. #include <asm/topology.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/ppc-pci.h>
  27. #include <asm/atomic.h>
  28. /*
  29. * The list of OF IDs below is used for matching bus types in the
  30. * system whose devices are to be exposed as of_platform_devices.
  31. *
  32. * This is the default list valid for most platforms. This file provides
  33. * functions who can take an explicit list if necessary though
  34. *
  35. * The search is always performed recursively looking for children of
  36. * the provided device_node and recursively if such a children matches
  37. * a bus type in the list
  38. */
  39. static struct of_device_id of_default_bus_ids[] = {
  40. { .type = "soc", },
  41. { .compatible = "soc", },
  42. { .type = "spider", },
  43. { .type = "axon", },
  44. { .type = "plb5", },
  45. { .type = "plb4", },
  46. { .type = "opb", },
  47. { .type = "ebc", },
  48. {},
  49. };
  50. static atomic_t bus_no_reg_magic;
  51. /*
  52. *
  53. * OF platform device type definition & base infrastructure
  54. *
  55. */
  56. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  57. {
  58. struct of_device * of_dev = to_of_device(dev);
  59. struct of_platform_driver * of_drv = to_of_platform_driver(drv);
  60. const struct of_device_id * matches = of_drv->match_table;
  61. if (!matches)
  62. return 0;
  63. return of_match_device(matches, of_dev) != NULL;
  64. }
  65. static int of_platform_device_probe(struct device *dev)
  66. {
  67. int error = -ENODEV;
  68. struct of_platform_driver *drv;
  69. struct of_device *of_dev;
  70. const struct of_device_id *match;
  71. drv = to_of_platform_driver(dev->driver);
  72. of_dev = to_of_device(dev);
  73. if (!drv->probe)
  74. return error;
  75. of_dev_get(of_dev);
  76. match = of_match_device(drv->match_table, of_dev);
  77. if (match)
  78. error = drv->probe(of_dev, match);
  79. if (error)
  80. of_dev_put(of_dev);
  81. return error;
  82. }
  83. static int of_platform_device_remove(struct device *dev)
  84. {
  85. struct of_device * of_dev = to_of_device(dev);
  86. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  87. if (dev->driver && drv->remove)
  88. drv->remove(of_dev);
  89. return 0;
  90. }
  91. static int of_platform_device_suspend(struct device *dev, pm_message_t state)
  92. {
  93. struct of_device * of_dev = to_of_device(dev);
  94. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  95. int error = 0;
  96. if (dev->driver && drv->suspend)
  97. error = drv->suspend(of_dev, state);
  98. return error;
  99. }
  100. static int of_platform_device_resume(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. int error = 0;
  105. if (dev->driver && drv->resume)
  106. error = drv->resume(of_dev);
  107. return error;
  108. }
  109. struct bus_type of_platform_bus_type = {
  110. .name = "of_platform",
  111. .match = of_platform_bus_match,
  112. .uevent = of_device_uevent,
  113. .probe = of_platform_device_probe,
  114. .remove = of_platform_device_remove,
  115. .suspend = of_platform_device_suspend,
  116. .resume = of_platform_device_resume,
  117. };
  118. EXPORT_SYMBOL(of_platform_bus_type);
  119. static int __init of_bus_driver_init(void)
  120. {
  121. return bus_register(&of_platform_bus_type);
  122. }
  123. postcore_initcall(of_bus_driver_init);
  124. int of_register_platform_driver(struct of_platform_driver *drv)
  125. {
  126. /* initialize common driver fields */
  127. drv->driver.name = drv->name;
  128. drv->driver.bus = &of_platform_bus_type;
  129. /* register with core */
  130. return driver_register(&drv->driver);
  131. }
  132. EXPORT_SYMBOL(of_register_platform_driver);
  133. void of_unregister_platform_driver(struct of_platform_driver *drv)
  134. {
  135. driver_unregister(&drv->driver);
  136. }
  137. EXPORT_SYMBOL(of_unregister_platform_driver);
  138. static void of_platform_make_bus_id(struct of_device *dev)
  139. {
  140. struct device_node *node = dev->node;
  141. char *name = dev->dev.bus_id;
  142. const u32 *reg;
  143. u64 addr;
  144. int magic;
  145. /*
  146. * If it's a DCR based device, use 'd' for native DCRs
  147. * and 'D' for MMIO DCRs.
  148. */
  149. #ifdef CONFIG_PPC_DCR
  150. reg = of_get_property(node, "dcr-reg", NULL);
  151. if (reg) {
  152. #ifdef CONFIG_PPC_DCR_NATIVE
  153. snprintf(name, BUS_ID_SIZE, "d%x.%s",
  154. *reg, node->name);
  155. #else /* CONFIG_PPC_DCR_NATIVE */
  156. addr = of_translate_dcr_address(node, *reg, NULL);
  157. if (addr != OF_BAD_ADDR) {
  158. snprintf(name, BUS_ID_SIZE,
  159. "D%llx.%s", (unsigned long long)addr,
  160. node->name);
  161. return;
  162. }
  163. #endif /* !CONFIG_PPC_DCR_NATIVE */
  164. }
  165. #endif /* CONFIG_PPC_DCR */
  166. /*
  167. * For MMIO, get the physical address
  168. */
  169. reg = of_get_property(node, "reg", NULL);
  170. if (reg) {
  171. addr = of_translate_address(node, reg);
  172. if (addr != OF_BAD_ADDR) {
  173. snprintf(name, BUS_ID_SIZE,
  174. "%llx.%s", (unsigned long long)addr,
  175. node->name);
  176. return;
  177. }
  178. }
  179. /*
  180. * No BusID, use the node name and add a globally incremented
  181. * counter (and pray...)
  182. */
  183. magic = atomic_add_return(1, &bus_no_reg_magic);
  184. snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
  185. }
  186. struct of_device* of_platform_device_create(struct device_node *np,
  187. const char *bus_id,
  188. struct device *parent)
  189. {
  190. struct of_device *dev;
  191. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  192. if (!dev)
  193. return NULL;
  194. memset(dev, 0, sizeof(*dev));
  195. dev->node = of_node_get(np);
  196. dev->dma_mask = 0xffffffffUL;
  197. dev->dev.dma_mask = &dev->dma_mask;
  198. dev->dev.parent = parent;
  199. dev->dev.bus = &of_platform_bus_type;
  200. dev->dev.release = of_release_dev;
  201. dev->dev.archdata.of_node = np;
  202. dev->dev.archdata.numa_node = of_node_to_nid(np);
  203. /* We do not fill the DMA ops for platform devices by default.
  204. * This is currently the responsibility of the platform code
  205. * to do such, possibly using a device notifier
  206. */
  207. if (bus_id)
  208. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  209. else
  210. of_platform_make_bus_id(dev);
  211. if (of_device_register(dev) != 0) {
  212. kfree(dev);
  213. return NULL;
  214. }
  215. return dev;
  216. }
  217. EXPORT_SYMBOL(of_platform_device_create);
  218. /**
  219. * of_platform_bus_create - Create an OF device for a bus node and all its
  220. * children. Optionally recursively instanciate matching busses.
  221. * @bus: device node of the bus to instanciate
  222. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  223. * disallow recursive creation of child busses
  224. */
  225. static int of_platform_bus_create(struct device_node *bus,
  226. struct of_device_id *matches,
  227. struct device *parent)
  228. {
  229. struct device_node *child;
  230. struct of_device *dev;
  231. int rc = 0;
  232. for (child = NULL; (child = of_get_next_child(bus, child)); ) {
  233. pr_debug(" create child: %s\n", child->full_name);
  234. dev = of_platform_device_create(child, NULL, parent);
  235. if (dev == NULL)
  236. rc = -ENOMEM;
  237. else if (!of_match_node(matches, child))
  238. continue;
  239. if (rc == 0) {
  240. pr_debug(" and sub busses\n");
  241. rc = of_platform_bus_create(child, matches, &dev->dev);
  242. } if (rc) {
  243. of_node_put(child);
  244. break;
  245. }
  246. }
  247. return rc;
  248. }
  249. /**
  250. * of_platform_bus_probe - Probe the device-tree for platform busses
  251. * @root: parent of the first level to probe or NULL for the root of the tree
  252. * @matches: match table, NULL to use the default
  253. * @parent: parent to hook devices from, NULL for toplevel
  254. *
  255. * Note that children of the provided root are not instanciated as devices
  256. * unless the specified root itself matches the bus list and is not NULL.
  257. */
  258. int of_platform_bus_probe(struct device_node *root,
  259. struct of_device_id *matches,
  260. struct device *parent)
  261. {
  262. struct device_node *child;
  263. struct of_device *dev;
  264. int rc = 0;
  265. if (matches == NULL)
  266. matches = of_default_bus_ids;
  267. if (matches == OF_NO_DEEP_PROBE)
  268. return -EINVAL;
  269. if (root == NULL)
  270. root = of_find_node_by_path("/");
  271. else
  272. of_node_get(root);
  273. pr_debug("of_platform_bus_probe()\n");
  274. pr_debug(" starting at: %s\n", root->full_name);
  275. /* Do a self check of bus type, if there's a match, create
  276. * children
  277. */
  278. if (of_match_node(matches, root)) {
  279. pr_debug(" root match, create all sub devices\n");
  280. dev = of_platform_device_create(root, NULL, parent);
  281. if (dev == NULL) {
  282. rc = -ENOMEM;
  283. goto bail;
  284. }
  285. pr_debug(" create all sub busses\n");
  286. rc = of_platform_bus_create(root, matches, &dev->dev);
  287. goto bail;
  288. }
  289. for (child = NULL; (child = of_get_next_child(root, child)); ) {
  290. if (!of_match_node(matches, child))
  291. continue;
  292. pr_debug(" match: %s\n", child->full_name);
  293. dev = of_platform_device_create(child, NULL, parent);
  294. if (dev == NULL)
  295. rc = -ENOMEM;
  296. else
  297. rc = of_platform_bus_create(child, matches, &dev->dev);
  298. if (rc) {
  299. of_node_put(child);
  300. break;
  301. }
  302. }
  303. bail:
  304. of_node_put(root);
  305. return rc;
  306. }
  307. EXPORT_SYMBOL(of_platform_bus_probe);
  308. static int of_dev_node_match(struct device *dev, void *data)
  309. {
  310. return to_of_device(dev)->node == data;
  311. }
  312. struct of_device *of_find_device_by_node(struct device_node *np)
  313. {
  314. struct device *dev;
  315. dev = bus_find_device(&of_platform_bus_type,
  316. NULL, np, of_dev_node_match);
  317. if (dev)
  318. return to_of_device(dev);
  319. return NULL;
  320. }
  321. EXPORT_SYMBOL(of_find_device_by_node);
  322. static int of_dev_phandle_match(struct device *dev, void *data)
  323. {
  324. phandle *ph = data;
  325. return to_of_device(dev)->node->linux_phandle == *ph;
  326. }
  327. struct of_device *of_find_device_by_phandle(phandle ph)
  328. {
  329. struct device *dev;
  330. dev = bus_find_device(&of_platform_bus_type,
  331. NULL, &ph, of_dev_phandle_match);
  332. if (dev)
  333. return to_of_device(dev);
  334. return NULL;
  335. }
  336. EXPORT_SYMBOL(of_find_device_by_phandle);
  337. #ifdef CONFIG_PPC_OF_PLATFORM_PCI
  338. /* The probing of PCI controllers from of_platform is currently
  339. * 64 bits only, mostly due to gratuitous differences between
  340. * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
  341. * lacking some bits needed here.
  342. */
  343. static int __devinit of_pci_phb_probe(struct of_device *dev,
  344. const struct of_device_id *match)
  345. {
  346. struct pci_controller *phb;
  347. /* Check if we can do that ... */
  348. if (ppc_md.pci_setup_phb == NULL)
  349. return -ENODEV;
  350. printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
  351. /* Alloc and setup PHB data structure */
  352. phb = pcibios_alloc_controller(dev->node);
  353. if (!phb)
  354. return -ENODEV;
  355. /* Setup parent in sysfs */
  356. phb->parent = &dev->dev;
  357. /* Setup the PHB using arch provided callback */
  358. if (ppc_md.pci_setup_phb(phb)) {
  359. pcibios_free_controller(phb);
  360. return -ENODEV;
  361. }
  362. /* Process "ranges" property */
  363. pci_process_bridge_OF_ranges(phb, dev->node, 0);
  364. /* Setup IO space.
  365. * This will not work properly for ISA IOs, something needs to be done
  366. * about it if we ever generalize that way of probing PCI brigdes
  367. */
  368. pci_setup_phb_io_dynamic(phb, 0);
  369. /* Init pci_dn data structures */
  370. pci_devs_phb_init_dynamic(phb);
  371. /* Register devices with EEH */
  372. #ifdef CONFIG_EEH
  373. if (dev->node->child)
  374. eeh_add_device_tree_early(dev->node);
  375. #endif /* CONFIG_EEH */
  376. /* Scan the bus */
  377. scan_phb(phb);
  378. /* Claim resources. This might need some rework as well depending
  379. * wether we are doing probe-only or not, like assigning unassigned
  380. * resources etc...
  381. */
  382. pcibios_claim_one_bus(phb->bus);
  383. /* Finish EEH setup */
  384. #ifdef CONFIG_EEH
  385. eeh_add_device_tree_late(phb->bus);
  386. #endif
  387. /* Add probed PCI devices to the device model */
  388. pci_bus_add_devices(phb->bus);
  389. return 0;
  390. }
  391. static struct of_device_id of_pci_phb_ids[] = {
  392. { .type = "pci", },
  393. { .type = "pcix", },
  394. { .type = "pcie", },
  395. { .type = "pciex", },
  396. { .type = "ht", },
  397. {}
  398. };
  399. static struct of_platform_driver of_pci_phb_driver = {
  400. .name = "of-pci",
  401. .match_table = of_pci_phb_ids,
  402. .probe = of_pci_phb_probe,
  403. };
  404. static __init int of_pci_phb_init(void)
  405. {
  406. return of_register_platform_driver(&of_pci_phb_driver);
  407. }
  408. device_initcall(of_pci_phb_init);
  409. #endif /* CONFIG_PPC_OF_PLATFORM_PCI */