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 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 (dn->full_name &&
  136. (strcasecmp((char *)data, dn->full_name) == 0));
  137. }
  138. static int ibmebus_match_node(struct device *dev, void *data)
  139. {
  140. return to_platform_device(dev)->dev.of_node == data;
  141. }
  142. static int ibmebus_create_device(struct device_node *dn)
  143. {
  144. struct platform_device *dev;
  145. int ret;
  146. dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
  147. if (!dev)
  148. return -ENOMEM;
  149. dev->dev.bus = &ibmebus_bus_type;
  150. dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
  151. ret = of_device_add(dev);
  152. if (ret)
  153. platform_device_put(dev);
  154. return ret;
  155. }
  156. static int ibmebus_create_devices(const struct of_device_id *matches)
  157. {
  158. struct device_node *root, *child;
  159. struct device *dev;
  160. int ret = 0;
  161. root = of_find_node_by_path("/");
  162. for_each_child_of_node(root, child) {
  163. if (!of_match_node(matches, child))
  164. continue;
  165. dev = bus_find_device(&ibmebus_bus_type, NULL, child,
  166. ibmebus_match_node);
  167. if (dev) {
  168. put_device(dev);
  169. continue;
  170. }
  171. ret = ibmebus_create_device(child);
  172. if (ret) {
  173. printk(KERN_ERR "%s: failed to create device (%i)",
  174. __func__, ret);
  175. of_node_put(child);
  176. break;
  177. }
  178. }
  179. of_node_put(root);
  180. return ret;
  181. }
  182. int ibmebus_register_driver(struct platform_driver *drv)
  183. {
  184. /* If the driver uses devices that ibmebus doesn't know, add them */
  185. ibmebus_create_devices(drv->driver.of_match_table);
  186. drv->driver.bus = &ibmebus_bus_type;
  187. return driver_register(&drv->driver);
  188. }
  189. EXPORT_SYMBOL(ibmebus_register_driver);
  190. void ibmebus_unregister_driver(struct platform_driver *drv)
  191. {
  192. driver_unregister(&drv->driver);
  193. }
  194. EXPORT_SYMBOL(ibmebus_unregister_driver);
  195. int ibmebus_request_irq(u32 ist, irq_handler_t handler,
  196. unsigned long irq_flags, const char *devname,
  197. void *dev_id)
  198. {
  199. unsigned int irq = irq_create_mapping(NULL, ist);
  200. if (!irq)
  201. return -EINVAL;
  202. return request_irq(irq, handler, irq_flags, devname, dev_id);
  203. }
  204. EXPORT_SYMBOL(ibmebus_request_irq);
  205. void ibmebus_free_irq(u32 ist, void *dev_id)
  206. {
  207. unsigned int irq = irq_find_mapping(NULL, ist);
  208. free_irq(irq, dev_id);
  209. irq_dispose_mapping(irq);
  210. }
  211. EXPORT_SYMBOL(ibmebus_free_irq);
  212. static char *ibmebus_chomp(const char *in, size_t count)
  213. {
  214. char *out = kmalloc(count + 1, GFP_KERNEL);
  215. if (!out)
  216. return NULL;
  217. memcpy(out, in, count);
  218. out[count] = '\0';
  219. if (out[count - 1] == '\n')
  220. out[count - 1] = '\0';
  221. return out;
  222. }
  223. static ssize_t ibmebus_store_probe(struct bus_type *bus,
  224. const char *buf, size_t count)
  225. {
  226. struct device_node *dn = NULL;
  227. struct device *dev;
  228. char *path;
  229. ssize_t rc = 0;
  230. path = ibmebus_chomp(buf, count);
  231. if (!path)
  232. return -ENOMEM;
  233. dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  234. ibmebus_match_path);
  235. if (dev) {
  236. put_device(dev);
  237. printk(KERN_WARNING "%s: %s has already been probed\n",
  238. __func__, path);
  239. rc = -EEXIST;
  240. goto out;
  241. }
  242. if ((dn = of_find_node_by_path(path))) {
  243. rc = ibmebus_create_device(dn);
  244. of_node_put(dn);
  245. } else {
  246. printk(KERN_WARNING "%s: no such device node: %s\n",
  247. __func__, path);
  248. rc = -ENODEV;
  249. }
  250. out:
  251. kfree(path);
  252. if (rc)
  253. return rc;
  254. return count;
  255. }
  256. static BUS_ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe);
  257. static ssize_t ibmebus_store_remove(struct bus_type *bus,
  258. const char *buf, size_t count)
  259. {
  260. struct device *dev;
  261. char *path;
  262. path = ibmebus_chomp(buf, count);
  263. if (!path)
  264. return -ENOMEM;
  265. if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  266. ibmebus_match_path))) {
  267. of_device_unregister(to_platform_device(dev));
  268. put_device(dev);
  269. kfree(path);
  270. return count;
  271. } else {
  272. printk(KERN_WARNING "%s: %s not on the bus\n",
  273. __func__, path);
  274. kfree(path);
  275. return -ENODEV;
  276. }
  277. }
  278. static BUS_ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove);
  279. static struct attribute *ibmbus_bus_attrs[] = {
  280. &bus_attr_probe.attr,
  281. &bus_attr_remove.attr,
  282. NULL,
  283. };
  284. ATTRIBUTE_GROUPS(ibmbus_bus);
  285. static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
  286. {
  287. const struct of_device_id *matches = drv->of_match_table;
  288. if (!matches)
  289. return 0;
  290. return of_match_device(matches, dev) != NULL;
  291. }
  292. static int ibmebus_bus_device_probe(struct device *dev)
  293. {
  294. int error = -ENODEV;
  295. struct platform_driver *drv;
  296. struct platform_device *of_dev;
  297. drv = to_platform_driver(dev->driver);
  298. of_dev = to_platform_device(dev);
  299. if (!drv->probe)
  300. return error;
  301. of_dev_get(of_dev);
  302. if (of_driver_match_device(dev, dev->driver))
  303. error = drv->probe(of_dev);
  304. if (error)
  305. of_dev_put(of_dev);
  306. return error;
  307. }
  308. static int ibmebus_bus_device_remove(struct device *dev)
  309. {
  310. struct platform_device *of_dev = to_platform_device(dev);
  311. struct platform_driver *drv = to_platform_driver(dev->driver);
  312. if (dev->driver && drv->remove)
  313. drv->remove(of_dev);
  314. return 0;
  315. }
  316. static void ibmebus_bus_device_shutdown(struct device *dev)
  317. {
  318. struct platform_device *of_dev = to_platform_device(dev);
  319. struct platform_driver *drv = to_platform_driver(dev->driver);
  320. if (dev->driver && drv->shutdown)
  321. drv->shutdown(of_dev);
  322. }
  323. /*
  324. * ibmebus_bus_device_attrs
  325. */
  326. static ssize_t devspec_show(struct device *dev,
  327. struct device_attribute *attr, char *buf)
  328. {
  329. struct platform_device *ofdev;
  330. ofdev = to_platform_device(dev);
  331. return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
  332. }
  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 ssize_t modalias_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
  344. buf[len] = '\n';
  345. buf[len+1] = 0;
  346. return len+1;
  347. }
  348. static struct device_attribute ibmebus_bus_device_attrs[] = {
  349. __ATTR_RO(devspec),
  350. __ATTR_RO(name),
  351. __ATTR_RO(modalias),
  352. __ATTR_NULL
  353. };
  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_attrs = ibmebus_bus_device_attrs,
  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);