of_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. .probe = of_platform_device_probe,
  113. .remove = of_platform_device_remove,
  114. .suspend = of_platform_device_suspend,
  115. .resume = of_platform_device_resume,
  116. };
  117. EXPORT_SYMBOL(of_platform_bus_type);
  118. static int __init of_bus_driver_init(void)
  119. {
  120. return bus_register(&of_platform_bus_type);
  121. }
  122. postcore_initcall(of_bus_driver_init);
  123. int of_register_platform_driver(struct of_platform_driver *drv)
  124. {
  125. /* initialize common driver fields */
  126. drv->driver.name = drv->name;
  127. drv->driver.bus = &of_platform_bus_type;
  128. /* register with core */
  129. return driver_register(&drv->driver);
  130. }
  131. EXPORT_SYMBOL(of_register_platform_driver);
  132. void of_unregister_platform_driver(struct of_platform_driver *drv)
  133. {
  134. driver_unregister(&drv->driver);
  135. }
  136. EXPORT_SYMBOL(of_unregister_platform_driver);
  137. static void of_platform_make_bus_id(struct of_device *dev)
  138. {
  139. struct device_node *node = dev->node;
  140. char *name = dev->dev.bus_id;
  141. const u32 *reg;
  142. u64 addr;
  143. int magic;
  144. /*
  145. * If it's a DCR based device, use 'd' for native DCRs
  146. * and 'D' for MMIO DCRs.
  147. */
  148. #ifdef CONFIG_PPC_DCR
  149. reg = get_property(node, "dcr-reg", NULL);
  150. if (reg) {
  151. #ifdef CONFIG_PPC_DCR_NATIVE
  152. snprintf(name, BUS_ID_SIZE, "d%x.%s",
  153. *reg, node->name);
  154. #else /* CONFIG_PPC_DCR_NATIVE */
  155. addr = of_translate_dcr_address(node, *reg, NULL);
  156. if (addr != OF_BAD_ADDR) {
  157. snprintf(name, BUS_ID_SIZE,
  158. "D%llx.%s", (unsigned long long)addr,
  159. node->name);
  160. return;
  161. }
  162. #endif /* !CONFIG_PPC_DCR_NATIVE */
  163. }
  164. #endif /* CONFIG_PPC_DCR */
  165. /*
  166. * For MMIO, get the physical address
  167. */
  168. reg = get_property(node, "reg", NULL);
  169. if (reg) {
  170. addr = of_translate_address(node, reg);
  171. if (addr != OF_BAD_ADDR) {
  172. snprintf(name, BUS_ID_SIZE,
  173. "%llx.%s", (unsigned long long)addr,
  174. node->name);
  175. return;
  176. }
  177. }
  178. /*
  179. * No BusID, use the node name and add a globally incremented
  180. * counter (and pray...)
  181. */
  182. magic = atomic_add_return(1, &bus_no_reg_magic);
  183. snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
  184. }
  185. struct of_device* of_platform_device_create(struct device_node *np,
  186. const char *bus_id,
  187. struct device *parent)
  188. {
  189. struct of_device *dev;
  190. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  191. if (!dev)
  192. return NULL;
  193. memset(dev, 0, sizeof(*dev));
  194. dev->node = of_node_get(np);
  195. dev->dma_mask = 0xffffffffUL;
  196. dev->dev.dma_mask = &dev->dma_mask;
  197. dev->dev.parent = parent;
  198. dev->dev.bus = &of_platform_bus_type;
  199. dev->dev.release = of_release_dev;
  200. dev->dev.archdata.of_node = np;
  201. dev->dev.archdata.numa_node = of_node_to_nid(np);
  202. /* We do not fill the DMA ops for platform devices by default.
  203. * This is currently the responsibility of the platform code
  204. * to do such, possibly using a device notifier
  205. */
  206. if (bus_id)
  207. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  208. else
  209. of_platform_make_bus_id(dev);
  210. if (of_device_register(dev) != 0) {
  211. kfree(dev);
  212. return NULL;
  213. }
  214. return dev;
  215. }
  216. EXPORT_SYMBOL(of_platform_device_create);
  217. /**
  218. * of_platform_bus_create - Create an OF device for a bus node and all its
  219. * children. Optionally recursively instanciate matching busses.
  220. * @bus: device node of the bus to instanciate
  221. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  222. * disallow recursive creation of child busses
  223. */
  224. static int of_platform_bus_create(struct device_node *bus,
  225. struct of_device_id *matches,
  226. struct device *parent)
  227. {
  228. struct device_node *child;
  229. struct of_device *dev;
  230. int rc = 0;
  231. for (child = NULL; (child = of_get_next_child(bus, child)); ) {
  232. pr_debug(" create child: %s\n", child->full_name);
  233. dev = of_platform_device_create(child, NULL, parent);
  234. if (dev == NULL)
  235. rc = -ENOMEM;
  236. else if (!of_match_node(matches, child))
  237. continue;
  238. if (rc == 0) {
  239. pr_debug(" and sub busses\n");
  240. rc = of_platform_bus_create(child, matches, &dev->dev);
  241. } if (rc) {
  242. of_node_put(child);
  243. break;
  244. }
  245. }
  246. return rc;
  247. }
  248. /**
  249. * of_platform_bus_probe - Probe the device-tree for platform busses
  250. * @root: parent of the first level to probe or NULL for the root of the tree
  251. * @matches: match table, NULL to use the default
  252. * @parent: parent to hook devices from, NULL for toplevel
  253. *
  254. * Note that children of the provided root are not instanciated as devices
  255. * unless the specified root itself matches the bus list and is not NULL.
  256. */
  257. int of_platform_bus_probe(struct device_node *root,
  258. struct of_device_id *matches,
  259. struct device *parent)
  260. {
  261. struct device_node *child;
  262. struct of_device *dev;
  263. int rc = 0;
  264. if (matches == NULL)
  265. matches = of_default_bus_ids;
  266. if (matches == OF_NO_DEEP_PROBE)
  267. return -EINVAL;
  268. if (root == NULL)
  269. root = of_find_node_by_path("/");
  270. else
  271. of_node_get(root);
  272. pr_debug("of_platform_bus_probe()\n");
  273. pr_debug(" starting at: %s\n", root->full_name);
  274. /* Do a self check of bus type, if there's a match, create
  275. * children
  276. */
  277. if (of_match_node(matches, root)) {
  278. pr_debug(" root match, create all sub devices\n");
  279. dev = of_platform_device_create(root, NULL, parent);
  280. if (dev == NULL) {
  281. rc = -ENOMEM;
  282. goto bail;
  283. }
  284. pr_debug(" create all sub busses\n");
  285. rc = of_platform_bus_create(root, matches, &dev->dev);
  286. goto bail;
  287. }
  288. for (child = NULL; (child = of_get_next_child(root, child)); ) {
  289. if (!of_match_node(matches, child))
  290. continue;
  291. pr_debug(" match: %s\n", child->full_name);
  292. dev = of_platform_device_create(child, NULL, parent);
  293. if (dev == NULL)
  294. rc = -ENOMEM;
  295. else
  296. rc = of_platform_bus_create(child, matches, &dev->dev);
  297. if (rc) {
  298. of_node_put(child);
  299. break;
  300. }
  301. }
  302. bail:
  303. of_node_put(root);
  304. return rc;
  305. }
  306. EXPORT_SYMBOL(of_platform_bus_probe);
  307. static int of_dev_node_match(struct device *dev, void *data)
  308. {
  309. return to_of_device(dev)->node == data;
  310. }
  311. struct of_device *of_find_device_by_node(struct device_node *np)
  312. {
  313. struct device *dev;
  314. dev = bus_find_device(&of_platform_bus_type,
  315. NULL, np, of_dev_node_match);
  316. if (dev)
  317. return to_of_device(dev);
  318. return NULL;
  319. }
  320. EXPORT_SYMBOL(of_find_device_by_node);
  321. static int of_dev_phandle_match(struct device *dev, void *data)
  322. {
  323. phandle *ph = data;
  324. return to_of_device(dev)->node->linux_phandle == *ph;
  325. }
  326. struct of_device *of_find_device_by_phandle(phandle ph)
  327. {
  328. struct device *dev;
  329. dev = bus_find_device(&of_platform_bus_type,
  330. NULL, &ph, of_dev_phandle_match);
  331. if (dev)
  332. return to_of_device(dev);
  333. return NULL;
  334. }
  335. EXPORT_SYMBOL(of_find_device_by_phandle);
  336. #ifdef CONFIG_PPC_OF_PLATFORM_PCI
  337. /* The probing of PCI controllers from of_platform is currently
  338. * 64 bits only, mostly due to gratuitous differences between
  339. * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
  340. * lacking some bits needed here.
  341. */
  342. static int __devinit of_pci_phb_probe(struct of_device *dev,
  343. const struct of_device_id *match)
  344. {
  345. struct pci_controller *phb;
  346. /* Check if we can do that ... */
  347. if (ppc_md.pci_setup_phb == NULL)
  348. return -ENODEV;
  349. printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
  350. /* Alloc and setup PHB data structure */
  351. phb = pcibios_alloc_controller(dev->node);
  352. if (!phb)
  353. return -ENODEV;
  354. /* Setup parent in sysfs */
  355. phb->parent = &dev->dev;
  356. /* Setup the PHB using arch provided callback */
  357. if (ppc_md.pci_setup_phb(phb)) {
  358. pcibios_free_controller(phb);
  359. return -ENODEV;
  360. }
  361. /* Process "ranges" property */
  362. pci_process_bridge_OF_ranges(phb, dev->node, 0);
  363. /* Setup IO space.
  364. * This will not work properly for ISA IOs, something needs to be done
  365. * about it if we ever generalize that way of probing PCI brigdes
  366. */
  367. pci_setup_phb_io_dynamic(phb, 0);
  368. /* Init pci_dn data structures */
  369. pci_devs_phb_init_dynamic(phb);
  370. /* Register devices with EEH */
  371. #ifdef CONFIG_EEH
  372. if (dev->node->child)
  373. eeh_add_device_tree_early(dev->node);
  374. #endif /* CONFIG_EEH */
  375. /* Scan the bus */
  376. scan_phb(phb);
  377. /* Claim resources. This might need some rework as well depending
  378. * wether we are doing probe-only or not, like assigning unassigned
  379. * resources etc...
  380. */
  381. pcibios_claim_one_bus(phb->bus);
  382. /* Finish EEH setup */
  383. #ifdef CONFIG_EEH
  384. eeh_add_device_tree_late(phb->bus);
  385. #endif
  386. /* Add probed PCI devices to the device model */
  387. pci_bus_add_devices(phb->bus);
  388. return 0;
  389. }
  390. static struct of_device_id of_pci_phb_ids[] = {
  391. { .type = "pci", },
  392. { .type = "pcix", },
  393. { .type = "pcie", },
  394. { .type = "pciex", },
  395. { .type = "ht", },
  396. {}
  397. };
  398. static struct of_platform_driver of_pci_phb_driver = {
  399. .name = "of-pci",
  400. .match_table = of_pci_phb_ids,
  401. .probe = of_pci_phb_probe,
  402. .driver = {
  403. .multithread_probe = 1,
  404. },
  405. };
  406. static __init int of_pci_phb_init(void)
  407. {
  408. return of_register_platform_driver(&of_pci_phb_driver);
  409. }
  410. device_initcall(of_pci_phb_init);
  411. #endif /* CONFIG_PPC_OF_PLATFORM_PCI */