ibmebus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * IBM PowerPC IBM eBus Infrastructure Support.
  3. *
  4. * Copyright (c) 2005 IBM Corporation
  5. * Joachim Fenkes <fenkes@de.ibm.com>
  6. * Heiko J Schick <schickhj@de.ibm.com>
  7. *
  8. * All rights reserved.
  9. *
  10. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  11. * BSD.
  12. *
  13. * OpenIB BSD License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  33. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  34. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/init.h>
  39. #include <linux/export.h>
  40. #include <linux/console.h>
  41. #include <linux/kobject.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/of.h>
  45. #include <linux/slab.h>
  46. #include <linux/stat.h>
  47. #include <linux/of_platform.h>
  48. #include <asm/ibmebus.h>
  49. static struct device ibmebus_bus_device = { /* fake "parent" device */
  50. .init_name = "ibmebus",
  51. };
  52. struct bus_type ibmebus_bus_type;
  53. /* These devices will automatically be added to the bus during init */
  54. static const struct of_device_id ibmebus_matches[] __initconst = {
  55. { .compatible = "IBM,lhca" },
  56. { .compatible = "IBM,lhea" },
  57. {},
  58. };
  59. static void *ibmebus_alloc_coherent(struct device *dev,
  60. size_t size,
  61. dma_addr_t *dma_handle,
  62. gfp_t flag,
  63. unsigned long attrs)
  64. {
  65. void *mem;
  66. mem = kmalloc(size, flag);
  67. *dma_handle = (dma_addr_t)mem;
  68. return mem;
  69. }
  70. static void ibmebus_free_coherent(struct device *dev,
  71. size_t size, void *vaddr,
  72. dma_addr_t dma_handle,
  73. unsigned long attrs)
  74. {
  75. kfree(vaddr);
  76. }
  77. static dma_addr_t ibmebus_map_page(struct device *dev,
  78. struct page *page,
  79. unsigned long offset,
  80. size_t size,
  81. enum dma_data_direction direction,
  82. unsigned long attrs)
  83. {
  84. return (dma_addr_t)(page_address(page) + offset);
  85. }
  86. static void ibmebus_unmap_page(struct device *dev,
  87. dma_addr_t dma_addr,
  88. size_t size,
  89. enum dma_data_direction direction,
  90. unsigned long attrs)
  91. {
  92. return;
  93. }
  94. static int ibmebus_map_sg(struct device *dev,
  95. struct scatterlist *sgl,
  96. int nents, enum dma_data_direction direction,
  97. unsigned long attrs)
  98. {
  99. struct scatterlist *sg;
  100. int i;
  101. for_each_sg(sgl, sg, nents, i) {
  102. sg->dma_address = (dma_addr_t) sg_virt(sg);
  103. sg->dma_length = sg->length;
  104. }
  105. return nents;
  106. }
  107. static void ibmebus_unmap_sg(struct device *dev,
  108. struct scatterlist *sg,
  109. int nents, enum dma_data_direction direction,
  110. unsigned long attrs)
  111. {
  112. return;
  113. }
  114. static int ibmebus_dma_supported(struct device *dev, u64 mask)
  115. {
  116. return mask == DMA_BIT_MASK(64);
  117. }
  118. static u64 ibmebus_dma_get_required_mask(struct device *dev)
  119. {
  120. return DMA_BIT_MASK(64);
  121. }
  122. static const struct dma_map_ops ibmebus_dma_ops = {
  123. .alloc = ibmebus_alloc_coherent,
  124. .free = ibmebus_free_coherent,
  125. .map_sg = ibmebus_map_sg,
  126. .unmap_sg = ibmebus_unmap_sg,
  127. .dma_supported = ibmebus_dma_supported,
  128. .get_required_mask = ibmebus_dma_get_required_mask,
  129. .map_page = ibmebus_map_page,
  130. .unmap_page = ibmebus_unmap_page,
  131. };
  132. static int ibmebus_match_path(struct device *dev, void *data)
  133. {
  134. struct device_node *dn = to_platform_device(dev)->dev.of_node;
  135. return (of_find_node_by_path(data) == dn);
  136. }
  137. static int ibmebus_match_node(struct device *dev, void *data)
  138. {
  139. return to_platform_device(dev)->dev.of_node == data;
  140. }
  141. static int ibmebus_create_device(struct device_node *dn)
  142. {
  143. struct platform_device *dev;
  144. int ret;
  145. dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
  146. if (!dev)
  147. return -ENOMEM;
  148. dev->dev.bus = &ibmebus_bus_type;
  149. dev->dev.dma_ops = &ibmebus_dma_ops;
  150. ret = of_device_add(dev);
  151. if (ret)
  152. platform_device_put(dev);
  153. return ret;
  154. }
  155. static int ibmebus_create_devices(const struct of_device_id *matches)
  156. {
  157. struct device_node *root, *child;
  158. struct device *dev;
  159. int ret = 0;
  160. root = of_find_node_by_path("/");
  161. for_each_child_of_node(root, child) {
  162. if (!of_match_node(matches, child))
  163. continue;
  164. dev = bus_find_device(&ibmebus_bus_type, NULL, child,
  165. ibmebus_match_node);
  166. if (dev) {
  167. put_device(dev);
  168. continue;
  169. }
  170. ret = ibmebus_create_device(child);
  171. if (ret) {
  172. printk(KERN_ERR "%s: failed to create device (%i)",
  173. __func__, ret);
  174. of_node_put(child);
  175. break;
  176. }
  177. }
  178. of_node_put(root);
  179. return ret;
  180. }
  181. int ibmebus_register_driver(struct platform_driver *drv)
  182. {
  183. /* If the driver uses devices that ibmebus doesn't know, add them */
  184. ibmebus_create_devices(drv->driver.of_match_table);
  185. drv->driver.bus = &ibmebus_bus_type;
  186. return driver_register(&drv->driver);
  187. }
  188. EXPORT_SYMBOL(ibmebus_register_driver);
  189. void ibmebus_unregister_driver(struct platform_driver *drv)
  190. {
  191. driver_unregister(&drv->driver);
  192. }
  193. EXPORT_SYMBOL(ibmebus_unregister_driver);
  194. int ibmebus_request_irq(u32 ist, irq_handler_t handler,
  195. unsigned long irq_flags, const char *devname,
  196. void *dev_id)
  197. {
  198. unsigned int irq = irq_create_mapping(NULL, ist);
  199. if (!irq)
  200. return -EINVAL;
  201. return request_irq(irq, handler, irq_flags, devname, dev_id);
  202. }
  203. EXPORT_SYMBOL(ibmebus_request_irq);
  204. void ibmebus_free_irq(u32 ist, void *dev_id)
  205. {
  206. unsigned int irq = irq_find_mapping(NULL, ist);
  207. free_irq(irq, dev_id);
  208. irq_dispose_mapping(irq);
  209. }
  210. EXPORT_SYMBOL(ibmebus_free_irq);
  211. static char *ibmebus_chomp(const char *in, size_t count)
  212. {
  213. char *out = kmalloc(count + 1, GFP_KERNEL);
  214. if (!out)
  215. return NULL;
  216. memcpy(out, in, count);
  217. out[count] = '\0';
  218. if (out[count - 1] == '\n')
  219. out[count - 1] = '\0';
  220. return out;
  221. }
  222. static ssize_t ibmebus_store_probe(struct bus_type *bus,
  223. const char *buf, size_t count)
  224. {
  225. struct device_node *dn = NULL;
  226. struct device *dev;
  227. char *path;
  228. ssize_t rc = 0;
  229. path = ibmebus_chomp(buf, count);
  230. if (!path)
  231. return -ENOMEM;
  232. dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  233. ibmebus_match_path);
  234. if (dev) {
  235. put_device(dev);
  236. printk(KERN_WARNING "%s: %s has already been probed\n",
  237. __func__, path);
  238. rc = -EEXIST;
  239. goto out;
  240. }
  241. if ((dn = of_find_node_by_path(path))) {
  242. rc = ibmebus_create_device(dn);
  243. of_node_put(dn);
  244. } else {
  245. printk(KERN_WARNING "%s: no such device node: %s\n",
  246. __func__, path);
  247. rc = -ENODEV;
  248. }
  249. out:
  250. kfree(path);
  251. if (rc)
  252. return rc;
  253. return count;
  254. }
  255. static BUS_ATTR(probe, 0200, NULL, ibmebus_store_probe);
  256. static ssize_t ibmebus_store_remove(struct bus_type *bus,
  257. const char *buf, size_t count)
  258. {
  259. struct device *dev;
  260. char *path;
  261. path = ibmebus_chomp(buf, count);
  262. if (!path)
  263. return -ENOMEM;
  264. if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  265. ibmebus_match_path))) {
  266. of_device_unregister(to_platform_device(dev));
  267. put_device(dev);
  268. kfree(path);
  269. return count;
  270. } else {
  271. printk(KERN_WARNING "%s: %s not on the bus\n",
  272. __func__, path);
  273. kfree(path);
  274. return -ENODEV;
  275. }
  276. }
  277. static BUS_ATTR(remove, 0200, NULL, ibmebus_store_remove);
  278. static struct attribute *ibmbus_bus_attrs[] = {
  279. &bus_attr_probe.attr,
  280. &bus_attr_remove.attr,
  281. NULL,
  282. };
  283. ATTRIBUTE_GROUPS(ibmbus_bus);
  284. static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
  285. {
  286. const struct of_device_id *matches = drv->of_match_table;
  287. if (!matches)
  288. return 0;
  289. return of_match_device(matches, dev) != NULL;
  290. }
  291. static int ibmebus_bus_device_probe(struct device *dev)
  292. {
  293. int error = -ENODEV;
  294. struct platform_driver *drv;
  295. struct platform_device *of_dev;
  296. drv = to_platform_driver(dev->driver);
  297. of_dev = to_platform_device(dev);
  298. if (!drv->probe)
  299. return error;
  300. of_dev_get(of_dev);
  301. if (of_driver_match_device(dev, dev->driver))
  302. error = drv->probe(of_dev);
  303. if (error)
  304. of_dev_put(of_dev);
  305. return error;
  306. }
  307. static int ibmebus_bus_device_remove(struct device *dev)
  308. {
  309. struct platform_device *of_dev = to_platform_device(dev);
  310. struct platform_driver *drv = to_platform_driver(dev->driver);
  311. if (dev->driver && drv->remove)
  312. drv->remove(of_dev);
  313. return 0;
  314. }
  315. static void ibmebus_bus_device_shutdown(struct device *dev)
  316. {
  317. struct platform_device *of_dev = to_platform_device(dev);
  318. struct platform_driver *drv = to_platform_driver(dev->driver);
  319. if (dev->driver && drv->shutdown)
  320. drv->shutdown(of_dev);
  321. }
  322. /*
  323. * ibmebus_bus_device_attrs
  324. */
  325. static ssize_t devspec_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. struct platform_device *ofdev;
  329. ofdev = to_platform_device(dev);
  330. return sprintf(buf, "%pOF\n", ofdev->dev.of_node);
  331. }
  332. static DEVICE_ATTR_RO(devspec);
  333. static ssize_t name_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct platform_device *ofdev;
  337. ofdev = to_platform_device(dev);
  338. return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
  339. }
  340. static DEVICE_ATTR_RO(name);
  341. static ssize_t modalias_show(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. return of_device_modalias(dev, buf, PAGE_SIZE);
  345. }
  346. static DEVICE_ATTR_RO(modalias);
  347. static struct attribute *ibmebus_bus_device_attrs[] = {
  348. &dev_attr_devspec.attr,
  349. &dev_attr_name.attr,
  350. &dev_attr_modalias.attr,
  351. NULL,
  352. };
  353. ATTRIBUTE_GROUPS(ibmebus_bus_device);
  354. struct bus_type ibmebus_bus_type = {
  355. .name = "ibmebus",
  356. .uevent = of_device_uevent_modalias,
  357. .bus_groups = ibmbus_bus_groups,
  358. .match = ibmebus_bus_bus_match,
  359. .probe = ibmebus_bus_device_probe,
  360. .remove = ibmebus_bus_device_remove,
  361. .shutdown = ibmebus_bus_device_shutdown,
  362. .dev_groups = ibmebus_bus_device_groups,
  363. };
  364. EXPORT_SYMBOL(ibmebus_bus_type);
  365. static int __init ibmebus_bus_init(void)
  366. {
  367. int err;
  368. printk(KERN_INFO "IBM eBus Device Driver\n");
  369. err = bus_register(&ibmebus_bus_type);
  370. if (err) {
  371. printk(KERN_ERR "%s: failed to register IBM eBus.\n",
  372. __func__);
  373. return err;
  374. }
  375. err = device_register(&ibmebus_bus_device);
  376. if (err) {
  377. printk(KERN_WARNING "%s: device_register returned %i\n",
  378. __func__, err);
  379. bus_unregister(&ibmebus_bus_type);
  380. return err;
  381. }
  382. err = ibmebus_create_devices(ibmebus_matches);
  383. if (err) {
  384. device_unregister(&ibmebus_bus_device);
  385. bus_unregister(&ibmebus_bus_type);
  386. return err;
  387. }
  388. return 0;
  389. }
  390. postcore_initcall(ibmebus_bus_init);