ibmebus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/console.h>
  40. #include <linux/kobject.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/interrupt.h>
  43. #include <asm/ibmebus.h>
  44. #include <asm/abs_addr.h>
  45. static struct device ibmebus_bus_device = { /* fake "parent" device */
  46. .bus_id = "ibmebus",
  47. };
  48. struct bus_type ibmebus_bus_type;
  49. static void *ibmebus_alloc_coherent(struct device *dev,
  50. size_t size,
  51. dma_addr_t *dma_handle,
  52. gfp_t flag)
  53. {
  54. void *mem;
  55. mem = kmalloc(size, flag);
  56. *dma_handle = (dma_addr_t)mem;
  57. return mem;
  58. }
  59. static void ibmebus_free_coherent(struct device *dev,
  60. size_t size, void *vaddr,
  61. dma_addr_t dma_handle)
  62. {
  63. kfree(vaddr);
  64. }
  65. static dma_addr_t ibmebus_map_single(struct device *dev,
  66. void *ptr,
  67. size_t size,
  68. enum dma_data_direction direction)
  69. {
  70. return (dma_addr_t)(ptr);
  71. }
  72. static void ibmebus_unmap_single(struct device *dev,
  73. dma_addr_t dma_addr,
  74. size_t size,
  75. enum dma_data_direction direction)
  76. {
  77. return;
  78. }
  79. static int ibmebus_map_sg(struct device *dev,
  80. struct scatterlist *sg,
  81. int nents, enum dma_data_direction direction)
  82. {
  83. int i;
  84. for (i = 0; i < nents; i++) {
  85. sg[i].dma_address = (dma_addr_t)page_address(sg[i].page)
  86. + sg[i].offset;
  87. sg[i].dma_length = sg[i].length;
  88. }
  89. return nents;
  90. }
  91. static void ibmebus_unmap_sg(struct device *dev,
  92. struct scatterlist *sg,
  93. int nents, enum dma_data_direction direction)
  94. {
  95. return;
  96. }
  97. static int ibmebus_dma_supported(struct device *dev, u64 mask)
  98. {
  99. return 1;
  100. }
  101. static struct dma_mapping_ops ibmebus_dma_ops = {
  102. .alloc_coherent = ibmebus_alloc_coherent,
  103. .free_coherent = ibmebus_free_coherent,
  104. .map_single = ibmebus_map_single,
  105. .unmap_single = ibmebus_unmap_single,
  106. .map_sg = ibmebus_map_sg,
  107. .unmap_sg = ibmebus_unmap_sg,
  108. .dma_supported = ibmebus_dma_supported,
  109. };
  110. static int ibmebus_bus_probe(struct device *dev)
  111. {
  112. struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
  113. struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
  114. const struct of_device_id *id;
  115. int error = -ENODEV;
  116. if (!ibmebusdrv->probe)
  117. return error;
  118. id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev);
  119. if (id) {
  120. error = ibmebusdrv->probe(ibmebusdev, id);
  121. }
  122. return error;
  123. }
  124. static int ibmebus_bus_remove(struct device *dev)
  125. {
  126. struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
  127. struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
  128. if (ibmebusdrv->remove) {
  129. return ibmebusdrv->remove(ibmebusdev);
  130. }
  131. return 0;
  132. }
  133. static void __devinit ibmebus_dev_release(struct device *dev)
  134. {
  135. of_node_put(to_ibmebus_dev(dev)->ofdev.node);
  136. kfree(to_ibmebus_dev(dev));
  137. }
  138. static int __devinit ibmebus_register_device_common(
  139. struct ibmebus_dev *dev, const char *name)
  140. {
  141. int err = 0;
  142. dev->ofdev.dev.parent = &ibmebus_bus_device;
  143. dev->ofdev.dev.bus = &ibmebus_bus_type;
  144. dev->ofdev.dev.release = ibmebus_dev_release;
  145. dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
  146. dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
  147. dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
  148. /* An ibmebusdev is based on a of_device. We have to change the
  149. * bus type to use our own DMA mapping operations.
  150. */
  151. if ((err = of_device_register(&dev->ofdev)) != 0) {
  152. printk(KERN_ERR "%s: failed to register device (%d).\n",
  153. __FUNCTION__, err);
  154. return -ENODEV;
  155. }
  156. return 0;
  157. }
  158. static struct ibmebus_dev* __devinit ibmebus_register_device_node(
  159. struct device_node *dn)
  160. {
  161. struct ibmebus_dev *dev;
  162. int i, len, bus_len;
  163. dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
  164. if (!dev)
  165. return ERR_PTR(-ENOMEM);
  166. dev->ofdev.node = of_node_get(dn);
  167. len = strlen(dn->full_name + 1);
  168. bus_len = min(len, BUS_ID_SIZE - 1);
  169. memcpy(dev->ofdev.dev.bus_id, dn->full_name + 1
  170. + (len - bus_len), bus_len);
  171. for (i = 0; i < bus_len; i++)
  172. if (dev->ofdev.dev.bus_id[i] == '/')
  173. dev->ofdev.dev.bus_id[i] = '_';
  174. /* Register with generic device framework. */
  175. if (ibmebus_register_device_common(dev, dn->name) != 0) {
  176. kfree(dev);
  177. return ERR_PTR(-ENODEV);
  178. }
  179. return dev;
  180. }
  181. static void ibmebus_probe_of_nodes(char* name)
  182. {
  183. struct device_node *dn = NULL;
  184. while ((dn = of_find_node_by_name(dn, name))) {
  185. if (IS_ERR(ibmebus_register_device_node(dn))) {
  186. of_node_put(dn);
  187. return;
  188. }
  189. }
  190. of_node_put(dn);
  191. return;
  192. }
  193. static void ibmebus_add_devices_by_id(struct of_device_id *idt)
  194. {
  195. while (strlen(idt->name) > 0) {
  196. ibmebus_probe_of_nodes(idt->name);
  197. idt++;
  198. }
  199. return;
  200. }
  201. static int ibmebus_match_name(struct device *dev, void *data)
  202. {
  203. const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
  204. const char *name;
  205. name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
  206. if (name && (strcmp(data, name) == 0))
  207. return 1;
  208. return 0;
  209. }
  210. static int ibmebus_unregister_device(struct device *dev)
  211. {
  212. of_device_unregister(to_of_device(dev));
  213. return 0;
  214. }
  215. static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
  216. {
  217. struct device *dev;
  218. while (strlen(idt->name) > 0) {
  219. while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
  220. (void*)idt->name,
  221. ibmebus_match_name))) {
  222. ibmebus_unregister_device(dev);
  223. }
  224. idt++;
  225. }
  226. return;
  227. }
  228. int ibmebus_register_driver(struct ibmebus_driver *drv)
  229. {
  230. int err = 0;
  231. drv->driver.name = drv->name;
  232. drv->driver.bus = &ibmebus_bus_type;
  233. drv->driver.probe = ibmebus_bus_probe;
  234. drv->driver.remove = ibmebus_bus_remove;
  235. if ((err = driver_register(&drv->driver) != 0))
  236. return err;
  237. /* remove all supported devices first, in case someone
  238. * probed them manually before registering the driver */
  239. ibmebus_remove_devices_by_id(drv->id_table);
  240. ibmebus_add_devices_by_id(drv->id_table);
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(ibmebus_register_driver);
  244. void ibmebus_unregister_driver(struct ibmebus_driver *drv)
  245. {
  246. driver_unregister(&drv->driver);
  247. ibmebus_remove_devices_by_id(drv->id_table);
  248. }
  249. EXPORT_SYMBOL(ibmebus_unregister_driver);
  250. int ibmebus_request_irq(struct ibmebus_dev *dev,
  251. u32 ist,
  252. irq_handler_t handler,
  253. unsigned long irq_flags, const char * devname,
  254. void *dev_id)
  255. {
  256. unsigned int irq = irq_create_mapping(NULL, ist);
  257. if (irq == NO_IRQ)
  258. return -EINVAL;
  259. return request_irq(irq, handler,
  260. irq_flags, devname, dev_id);
  261. }
  262. EXPORT_SYMBOL(ibmebus_request_irq);
  263. void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
  264. {
  265. unsigned int irq = irq_find_mapping(NULL, ist);
  266. free_irq(irq, dev_id);
  267. }
  268. EXPORT_SYMBOL(ibmebus_free_irq);
  269. static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
  270. {
  271. const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
  272. struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
  273. const struct of_device_id *ids = ebus_drv->id_table;
  274. const struct of_device_id *found_id;
  275. if (!ids)
  276. return 0;
  277. found_id = of_match_device(ids, &ebus_dev->ofdev);
  278. if (found_id)
  279. return 1;
  280. return 0;
  281. }
  282. static ssize_t name_show(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
  286. const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
  287. return sprintf(buf, "%s\n", name);
  288. }
  289. static struct device_attribute ibmebus_dev_attrs[] = {
  290. __ATTR_RO(name),
  291. __ATTR_NULL
  292. };
  293. static int ibmebus_match_path(struct device *dev, void *data)
  294. {
  295. int rc;
  296. struct device_node *dn =
  297. of_node_get(to_ibmebus_dev(dev)->ofdev.node);
  298. rc = (dn->full_name && (strcasecmp((char*)data, dn->full_name) == 0));
  299. of_node_put(dn);
  300. return rc;
  301. }
  302. static char *ibmebus_chomp(const char *in, size_t count)
  303. {
  304. char *out = kmalloc(count + 1, GFP_KERNEL);
  305. if (!out)
  306. return NULL;
  307. memcpy(out, in, count);
  308. out[count] = '\0';
  309. if (out[count - 1] == '\n')
  310. out[count - 1] = '\0';
  311. return out;
  312. }
  313. static ssize_t ibmebus_store_probe(struct bus_type *bus,
  314. const char *buf, size_t count)
  315. {
  316. struct device_node *dn = NULL;
  317. struct ibmebus_dev *dev;
  318. char *path;
  319. ssize_t rc;
  320. path = ibmebus_chomp(buf, count);
  321. if (!path)
  322. return -ENOMEM;
  323. if (bus_find_device(&ibmebus_bus_type, NULL, path,
  324. ibmebus_match_path)) {
  325. printk(KERN_WARNING "%s: %s has already been probed\n",
  326. __FUNCTION__, path);
  327. rc = -EEXIST;
  328. goto out;
  329. }
  330. if ((dn = of_find_node_by_path(path))) {
  331. dev = ibmebus_register_device_node(dn);
  332. of_node_put(dn);
  333. rc = IS_ERR(dev) ? PTR_ERR(dev) : count;
  334. } else {
  335. printk(KERN_WARNING "%s: no such device node: %s\n",
  336. __FUNCTION__, path);
  337. rc = -ENODEV;
  338. }
  339. out:
  340. kfree(path);
  341. return rc;
  342. }
  343. static ssize_t ibmebus_store_remove(struct bus_type *bus,
  344. const char *buf, size_t count)
  345. {
  346. struct device *dev;
  347. char *path;
  348. path = ibmebus_chomp(buf, count);
  349. if (!path)
  350. return -ENOMEM;
  351. if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
  352. ibmebus_match_path))) {
  353. ibmebus_unregister_device(dev);
  354. kfree(path);
  355. return count;
  356. } else {
  357. printk(KERN_WARNING "%s: %s not on the bus\n",
  358. __FUNCTION__, path);
  359. kfree(path);
  360. return -ENODEV;
  361. }
  362. }
  363. static struct bus_attribute ibmebus_bus_attrs[] = {
  364. __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
  365. __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
  366. __ATTR_NULL
  367. };
  368. struct bus_type ibmebus_bus_type = {
  369. .name = "ibmebus",
  370. .match = ibmebus_bus_match,
  371. .dev_attrs = ibmebus_dev_attrs,
  372. .bus_attrs = ibmebus_bus_attrs
  373. };
  374. EXPORT_SYMBOL(ibmebus_bus_type);
  375. static int __init ibmebus_bus_init(void)
  376. {
  377. int err;
  378. printk(KERN_INFO "IBM eBus Device Driver\n");
  379. err = bus_register(&ibmebus_bus_type);
  380. if (err) {
  381. printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
  382. __FUNCTION__);
  383. return err;
  384. }
  385. err = device_register(&ibmebus_bus_device);
  386. if (err) {
  387. printk(KERN_WARNING "%s: device_register returned %i\n",
  388. __FUNCTION__, err);
  389. bus_unregister(&ibmebus_bus_type);
  390. return err;
  391. }
  392. return 0;
  393. }
  394. __initcall(ibmebus_bus_init);