ibmebus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * IBM PowerPC IBM eBus Infrastructure Support.
  3. *
  4. * Copyright (c) 2005 IBM Corporation
  5. * Heiko J Schick <schickhj@de.ibm.com>
  6. *
  7. * All rights reserved.
  8. *
  9. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  10. * BSD.
  11. *
  12. * OpenIB BSD License
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  33. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/console.h>
  39. #include <linux/kobject.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/interrupt.h>
  42. #include <asm/ibmebus.h>
  43. #include <asm/abs_addr.h>
  44. static struct ibmebus_dev ibmebus_bus_device = { /* fake "parent" device */
  45. .name = ibmebus_bus_device.ofdev.dev.bus_id,
  46. .ofdev.dev.bus_id = "ibmebus",
  47. .ofdev.dev.bus = &ibmebus_bus_type,
  48. };
  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 ssize_t ibmebusdev_show_name(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. return sprintf(buf, "%s\n", to_ibmebus_dev(dev)->name);
  142. }
  143. static DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, ibmebusdev_show_name,
  144. NULL);
  145. static struct ibmebus_dev* __devinit ibmebus_register_device_common(
  146. struct ibmebus_dev *dev, const char *name)
  147. {
  148. int err = 0;
  149. dev->name = name;
  150. dev->ofdev.dev.parent = &ibmebus_bus_device.ofdev.dev;
  151. dev->ofdev.dev.bus = &ibmebus_bus_type;
  152. dev->ofdev.dev.release = ibmebus_dev_release;
  153. dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
  154. dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
  155. dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
  156. /* An ibmebusdev is based on a of_device. We have to change the
  157. * bus type to use our own DMA mapping operations.
  158. */
  159. if ((err = of_device_register(&dev->ofdev)) != 0) {
  160. printk(KERN_ERR "%s: failed to register device (%d).\n",
  161. __FUNCTION__, err);
  162. return NULL;
  163. }
  164. device_create_file(&dev->ofdev.dev, &dev_attr_name);
  165. return dev;
  166. }
  167. static struct ibmebus_dev* __devinit ibmebus_register_device_node(
  168. struct device_node *dn)
  169. {
  170. struct ibmebus_dev *dev;
  171. const char *loc_code;
  172. int length;
  173. loc_code = get_property(dn, "ibm,loc-code", NULL);
  174. if (!loc_code) {
  175. printk(KERN_WARNING "%s: node %s missing 'ibm,loc-code'\n",
  176. __FUNCTION__, dn->name ? dn->name : "<unknown>");
  177. return NULL;
  178. }
  179. if (strlen(loc_code) == 0) {
  180. printk(KERN_WARNING "%s: 'ibm,loc-code' is invalid\n",
  181. __FUNCTION__);
  182. return NULL;
  183. }
  184. dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
  185. if (!dev) {
  186. return NULL;
  187. }
  188. dev->ofdev.node = of_node_get(dn);
  189. length = strlen(loc_code);
  190. memcpy(dev->ofdev.dev.bus_id, loc_code
  191. + (length - min(length, BUS_ID_SIZE - 1)),
  192. min(length, BUS_ID_SIZE - 1));
  193. /* Register with generic device framework. */
  194. if (ibmebus_register_device_common(dev, dn->name) == NULL) {
  195. kfree(dev);
  196. return NULL;
  197. }
  198. return dev;
  199. }
  200. static void ibmebus_probe_of_nodes(char* name)
  201. {
  202. struct device_node *dn = NULL;
  203. while ((dn = of_find_node_by_name(dn, name))) {
  204. if (ibmebus_register_device_node(dn) == NULL) {
  205. of_node_put(dn);
  206. return;
  207. }
  208. }
  209. of_node_put(dn);
  210. return;
  211. }
  212. static void ibmebus_add_devices_by_id(struct of_device_id *idt)
  213. {
  214. while (strlen(idt->name) > 0) {
  215. ibmebus_probe_of_nodes(idt->name);
  216. idt++;
  217. }
  218. return;
  219. }
  220. static int ibmebus_match_helper(struct device *dev, void *data)
  221. {
  222. if (strcmp((char*)data, to_ibmebus_dev(dev)->name) == 0)
  223. return 1;
  224. return 0;
  225. }
  226. static int ibmebus_unregister_device(struct device *dev)
  227. {
  228. device_remove_file(dev, &dev_attr_name);
  229. of_device_unregister(to_of_device(dev));
  230. return 0;
  231. }
  232. static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
  233. {
  234. struct device *dev;
  235. while (strlen(idt->name) > 0) {
  236. while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
  237. (void*)idt->name,
  238. ibmebus_match_helper))) {
  239. ibmebus_unregister_device(dev);
  240. }
  241. idt++;
  242. }
  243. return;
  244. }
  245. int ibmebus_register_driver(struct ibmebus_driver *drv)
  246. {
  247. int err = 0;
  248. drv->driver.name = drv->name;
  249. drv->driver.bus = &ibmebus_bus_type;
  250. drv->driver.probe = ibmebus_bus_probe;
  251. drv->driver.remove = ibmebus_bus_remove;
  252. if ((err = driver_register(&drv->driver) != 0))
  253. return err;
  254. ibmebus_add_devices_by_id(drv->id_table);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(ibmebus_register_driver);
  258. void ibmebus_unregister_driver(struct ibmebus_driver *drv)
  259. {
  260. driver_unregister(&drv->driver);
  261. ibmebus_remove_devices_by_id(drv->id_table);
  262. }
  263. EXPORT_SYMBOL(ibmebus_unregister_driver);
  264. int ibmebus_request_irq(struct ibmebus_dev *dev,
  265. u32 ist,
  266. irq_handler_t handler,
  267. unsigned long irq_flags, const char * devname,
  268. void *dev_id)
  269. {
  270. unsigned int irq = irq_create_mapping(NULL, ist);
  271. if (irq == NO_IRQ)
  272. return -EINVAL;
  273. return request_irq(irq, handler,
  274. irq_flags, devname, dev_id);
  275. }
  276. EXPORT_SYMBOL(ibmebus_request_irq);
  277. void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
  278. {
  279. unsigned int irq = irq_find_mapping(NULL, ist);
  280. free_irq(irq, dev_id);
  281. }
  282. EXPORT_SYMBOL(ibmebus_free_irq);
  283. static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
  284. {
  285. const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
  286. struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
  287. const struct of_device_id *ids = ebus_drv->id_table;
  288. const struct of_device_id *found_id;
  289. if (!ids)
  290. return 0;
  291. found_id = of_match_device(ids, &ebus_dev->ofdev);
  292. if (found_id)
  293. return 1;
  294. return 0;
  295. }
  296. struct bus_type ibmebus_bus_type = {
  297. .name = "ibmebus",
  298. .match = ibmebus_bus_match,
  299. };
  300. EXPORT_SYMBOL(ibmebus_bus_type);
  301. static int __init ibmebus_bus_init(void)
  302. {
  303. int err;
  304. printk(KERN_INFO "IBM eBus Device Driver\n");
  305. err = bus_register(&ibmebus_bus_type);
  306. if (err) {
  307. printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
  308. __FUNCTION__);
  309. return err;
  310. }
  311. err = device_register(&ibmebus_bus_device.ofdev.dev);
  312. if (err) {
  313. printk(KERN_WARNING "%s: device_register returned %i\n",
  314. __FUNCTION__, err);
  315. bus_unregister(&ibmebus_bus_type);
  316. return err;
  317. }
  318. return 0;
  319. }
  320. __initcall(ibmebus_bus_init);