ibmebus.c 12 KB

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