fmc-core.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/fmc.h>
  16. #include <linux/fmc-sdb.h>
  17. #include "fmc-private.h"
  18. static int fmc_check_version(unsigned long version, const char *name)
  19. {
  20. if (__FMC_MAJOR(version) != FMC_MAJOR) {
  21. pr_err("%s: \"%s\" has wrong major (has %li, expected %i)\n",
  22. __func__, name, __FMC_MAJOR(version), FMC_MAJOR);
  23. return -EINVAL;
  24. }
  25. if (__FMC_MINOR(version) != FMC_MINOR)
  26. pr_info("%s: \"%s\" has wrong minor (has %li, expected %i)\n",
  27. __func__, name, __FMC_MINOR(version), FMC_MINOR);
  28. return 0;
  29. }
  30. static int fmc_uevent(struct device *dev, struct kobj_uevent_env *env)
  31. {
  32. /* struct fmc_device *fdev = to_fmc_device(dev); */
  33. /* FIXME: The MODALIAS */
  34. add_uevent_var(env, "MODALIAS=%s", "fmc");
  35. return 0;
  36. }
  37. static int fmc_probe(struct device *dev)
  38. {
  39. struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
  40. struct fmc_device *fdev = to_fmc_device(dev);
  41. return fdrv->probe(fdev);
  42. }
  43. static int fmc_remove(struct device *dev)
  44. {
  45. struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
  46. struct fmc_device *fdev = to_fmc_device(dev);
  47. return fdrv->remove(fdev);
  48. }
  49. static void fmc_shutdown(struct device *dev)
  50. {
  51. /* not implemented but mandatory */
  52. }
  53. static struct bus_type fmc_bus_type = {
  54. .name = "fmc",
  55. .match = fmc_match,
  56. .uevent = fmc_uevent,
  57. .probe = fmc_probe,
  58. .remove = fmc_remove,
  59. .shutdown = fmc_shutdown,
  60. };
  61. static void fmc_release(struct device *dev)
  62. {
  63. struct fmc_device *fmc = container_of(dev, struct fmc_device, dev);
  64. kfree(fmc);
  65. }
  66. /*
  67. * The eeprom is exported in sysfs, through a binary attribute
  68. */
  69. static ssize_t fmc_read_eeprom(struct file *file, struct kobject *kobj,
  70. struct bin_attribute *bin_attr,
  71. char *buf, loff_t off, size_t count)
  72. {
  73. struct device *dev;
  74. struct fmc_device *fmc;
  75. int eelen;
  76. dev = container_of(kobj, struct device, kobj);
  77. fmc = container_of(dev, struct fmc_device, dev);
  78. eelen = fmc->eeprom_len;
  79. if (off > eelen)
  80. return -ESPIPE;
  81. if (off == eelen)
  82. return 0; /* EOF */
  83. if (off + count > eelen)
  84. count = eelen - off;
  85. memcpy(buf, fmc->eeprom + off, count);
  86. return count;
  87. }
  88. static ssize_t fmc_write_eeprom(struct file *file, struct kobject *kobj,
  89. struct bin_attribute *bin_attr,
  90. char *buf, loff_t off, size_t count)
  91. {
  92. struct device *dev;
  93. struct fmc_device *fmc;
  94. dev = container_of(kobj, struct device, kobj);
  95. fmc = container_of(dev, struct fmc_device, dev);
  96. return fmc->op->write_ee(fmc, off, buf, count);
  97. }
  98. static struct bin_attribute fmc_eeprom_attr = {
  99. .attr = { .name = "eeprom", .mode = S_IRUGO | S_IWUSR, },
  100. .size = 8192, /* more or less standard */
  101. .read = fmc_read_eeprom,
  102. .write = fmc_write_eeprom,
  103. };
  104. int fmc_irq_request(struct fmc_device *fmc, irq_handler_t h,
  105. char *name, int flags)
  106. {
  107. if (fmc->op->irq_request)
  108. return fmc->op->irq_request(fmc, h, name, flags);
  109. return -EPERM;
  110. }
  111. EXPORT_SYMBOL(fmc_irq_request);
  112. void fmc_irq_free(struct fmc_device *fmc)
  113. {
  114. if (fmc->op->irq_free)
  115. fmc->op->irq_free(fmc);
  116. }
  117. EXPORT_SYMBOL(fmc_irq_free);
  118. void fmc_irq_ack(struct fmc_device *fmc)
  119. {
  120. if (likely(fmc->op->irq_ack))
  121. fmc->op->irq_ack(fmc);
  122. }
  123. EXPORT_SYMBOL(fmc_irq_ack);
  124. int fmc_validate(struct fmc_device *fmc, struct fmc_driver *drv)
  125. {
  126. if (fmc->op->validate)
  127. return fmc->op->validate(fmc, drv);
  128. return -EPERM;
  129. }
  130. EXPORT_SYMBOL(fmc_validate);
  131. int fmc_gpio_config(struct fmc_device *fmc, struct fmc_gpio *gpio, int ngpio)
  132. {
  133. if (fmc->op->gpio_config)
  134. return fmc->op->gpio_config(fmc, gpio, ngpio);
  135. return -EPERM;
  136. }
  137. EXPORT_SYMBOL(fmc_gpio_config);
  138. int fmc_read_ee(struct fmc_device *fmc, int pos, void *d, int l)
  139. {
  140. if (fmc->op->read_ee)
  141. return fmc->op->read_ee(fmc, pos, d, l);
  142. return -EPERM;
  143. }
  144. EXPORT_SYMBOL(fmc_read_ee);
  145. int fmc_write_ee(struct fmc_device *fmc, int pos, const void *d, int l)
  146. {
  147. if (fmc->op->write_ee)
  148. return fmc->op->write_ee(fmc, pos, d, l);
  149. return -EPERM;
  150. }
  151. EXPORT_SYMBOL(fmc_write_ee);
  152. /*
  153. * Functions for client modules follow
  154. */
  155. int fmc_driver_register(struct fmc_driver *drv)
  156. {
  157. if (fmc_check_version(drv->version, drv->driver.name))
  158. return -EINVAL;
  159. drv->driver.bus = &fmc_bus_type;
  160. return driver_register(&drv->driver);
  161. }
  162. EXPORT_SYMBOL(fmc_driver_register);
  163. void fmc_driver_unregister(struct fmc_driver *drv)
  164. {
  165. driver_unregister(&drv->driver);
  166. }
  167. EXPORT_SYMBOL(fmc_driver_unregister);
  168. /*
  169. * When a device set is registered, all eeproms must be read
  170. * and all FRUs must be parsed
  171. */
  172. int fmc_device_register_n_gw(struct fmc_device **devs, int n,
  173. struct fmc_gateware *gw)
  174. {
  175. struct fmc_device *fmc, **devarray;
  176. uint32_t device_id;
  177. int i, ret = 0;
  178. if (n < 1)
  179. return 0;
  180. /* Check the version of the first data structure (function prints) */
  181. if (fmc_check_version(devs[0]->version, devs[0]->carrier_name))
  182. return -EINVAL;
  183. devarray = kmemdup(devs, n * sizeof(*devs), GFP_KERNEL);
  184. if (!devarray)
  185. return -ENOMEM;
  186. /* Make all other checks before continuing, for all devices */
  187. for (i = 0; i < n; i++) {
  188. fmc = devarray[i];
  189. if (!fmc->hwdev) {
  190. pr_err("%s: device nr. %i has no hwdev pointer\n",
  191. __func__, i);
  192. ret = -EINVAL;
  193. break;
  194. }
  195. if (fmc->flags & FMC_DEVICE_NO_MEZZANINE) {
  196. dev_info(fmc->hwdev, "absent mezzanine in slot %d\n",
  197. fmc->slot_id);
  198. continue;
  199. }
  200. if (!fmc->eeprom) {
  201. dev_err(fmc->hwdev, "no eeprom provided for slot %i\n",
  202. fmc->slot_id);
  203. ret = -EINVAL;
  204. }
  205. if (!fmc->eeprom_addr) {
  206. dev_err(fmc->hwdev, "no eeprom_addr for slot %i\n",
  207. fmc->slot_id);
  208. ret = -EINVAL;
  209. }
  210. if (!fmc->carrier_name || !fmc->carrier_data ||
  211. !fmc->device_id) {
  212. dev_err(fmc->hwdev,
  213. "device nr %i: carrier name, "
  214. "data or dev_id not set\n", i);
  215. ret = -EINVAL;
  216. }
  217. if (ret)
  218. break;
  219. }
  220. if (ret) {
  221. kfree(devarray);
  222. return ret;
  223. }
  224. /* Validation is ok. Now init and register the devices */
  225. for (i = 0; i < n; i++) {
  226. fmc = devarray[i];
  227. fmc->nr_slots = n; /* each slot must know how many are there */
  228. fmc->devarray = devarray;
  229. device_initialize(&fmc->dev);
  230. fmc->dev.release = fmc_release;
  231. fmc->dev.parent = fmc->hwdev;
  232. /* Fill the identification stuff (may fail) */
  233. fmc_fill_id_info(fmc);
  234. fmc->dev.bus = &fmc_bus_type;
  235. /* Name from mezzanine info or carrier info. Or 0,1,2.. */
  236. device_id = fmc->device_id;
  237. if (!fmc->mezzanine_name)
  238. dev_set_name(&fmc->dev, "fmc-%04x", device_id);
  239. else
  240. dev_set_name(&fmc->dev, "%s-%04x", fmc->mezzanine_name,
  241. device_id);
  242. if (gw) {
  243. /*
  244. * The carrier already know the bitstream to load
  245. * for this set of FMC mezzanines.
  246. */
  247. ret = fmc->op->reprogram_raw(fmc, NULL,
  248. gw->bitstream, gw->len);
  249. if (ret) {
  250. dev_warn(fmc->hwdev,
  251. "Invalid gateware for FMC mezzanine\n");
  252. goto out;
  253. }
  254. }
  255. ret = device_add(&fmc->dev);
  256. if (ret < 0) {
  257. dev_err(fmc->hwdev, "Slot %i: Failed in registering "
  258. "\"%s\"\n", fmc->slot_id, fmc->dev.kobj.name);
  259. goto out;
  260. }
  261. ret = sysfs_create_bin_file(&fmc->dev.kobj, &fmc_eeprom_attr);
  262. if (ret < 0) {
  263. dev_err(&fmc->dev, "Failed in registering eeprom\n");
  264. goto out1;
  265. }
  266. /* This device went well, give information to the user */
  267. fmc_dump_eeprom(fmc);
  268. fmc_debug_init(fmc);
  269. }
  270. return 0;
  271. out1:
  272. device_del(&fmc->dev);
  273. out:
  274. kfree(devarray);
  275. for (i--; i >= 0; i--) {
  276. fmc_debug_exit(devs[i]);
  277. sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
  278. device_del(&devs[i]->dev);
  279. fmc_free_id_info(devs[i]);
  280. put_device(&devs[i]->dev);
  281. }
  282. return ret;
  283. }
  284. EXPORT_SYMBOL(fmc_device_register_n_gw);
  285. int fmc_device_register_n(struct fmc_device **devs, int n)
  286. {
  287. return fmc_device_register_n_gw(devs, n, NULL);
  288. }
  289. EXPORT_SYMBOL(fmc_device_register_n);
  290. int fmc_device_register_gw(struct fmc_device *fmc, struct fmc_gateware *gw)
  291. {
  292. return fmc_device_register_n_gw(&fmc, 1, gw);
  293. }
  294. EXPORT_SYMBOL(fmc_device_register_gw);
  295. int fmc_device_register(struct fmc_device *fmc)
  296. {
  297. return fmc_device_register_n(&fmc, 1);
  298. }
  299. EXPORT_SYMBOL(fmc_device_register);
  300. void fmc_device_unregister_n(struct fmc_device **devs, int n)
  301. {
  302. int i;
  303. if (n < 1)
  304. return;
  305. /* Free devarray first, not used by the later loop */
  306. kfree(devs[0]->devarray);
  307. for (i = 0; i < n; i++) {
  308. fmc_debug_exit(devs[i]);
  309. sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
  310. device_del(&devs[i]->dev);
  311. fmc_free_id_info(devs[i]);
  312. put_device(&devs[i]->dev);
  313. }
  314. }
  315. EXPORT_SYMBOL(fmc_device_unregister_n);
  316. void fmc_device_unregister(struct fmc_device *fmc)
  317. {
  318. fmc_device_unregister_n(&fmc, 1);
  319. }
  320. EXPORT_SYMBOL(fmc_device_unregister);
  321. /* Init and exit are trivial */
  322. static int fmc_init(void)
  323. {
  324. return bus_register(&fmc_bus_type);
  325. }
  326. static void fmc_exit(void)
  327. {
  328. bus_unregister(&fmc_bus_type);
  329. }
  330. module_init(fmc_init);
  331. module_exit(fmc_exit);
  332. MODULE_LICENSE("GPL");