fpga-mgr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * FPGA Manager Core
  3. *
  4. * Copyright (C) 2013-2015 Altera Corporation
  5. *
  6. * With code from the mailing list:
  7. * Copyright (C) 2013 Xilinx, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/firmware.h>
  22. #include <linux/fpga/fpga-mgr.h>
  23. #include <linux/idr.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. static DEFINE_IDA(fpga_mgr_ida);
  29. static struct class *fpga_mgr_class;
  30. /**
  31. * fpga_mgr_buf_load - load fpga from image in buffer
  32. * @mgr: fpga manager
  33. * @flags: flags setting fpga confuration modes
  34. * @buf: buffer contain fpga image
  35. * @count: byte count of buf
  36. *
  37. * Step the low level fpga manager through the device-specific steps of getting
  38. * an FPGA ready to be configured, writing the image to it, then doing whatever
  39. * post-configuration steps necessary. This code assumes the caller got the
  40. * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
  41. *
  42. * Return: 0 on success, negative error code otherwise.
  43. */
  44. int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags, const char *buf,
  45. size_t count)
  46. {
  47. struct device *dev = &mgr->dev;
  48. int ret;
  49. /*
  50. * Call the low level driver's write_init function. This will do the
  51. * device-specific things to get the FPGA into the state where it is
  52. * ready to receive an FPGA image.
  53. */
  54. mgr->state = FPGA_MGR_STATE_WRITE_INIT;
  55. ret = mgr->mops->write_init(mgr, flags, buf, count);
  56. if (ret) {
  57. dev_err(dev, "Error preparing FPGA for writing\n");
  58. mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
  59. return ret;
  60. }
  61. /*
  62. * Write the FPGA image to the FPGA.
  63. */
  64. mgr->state = FPGA_MGR_STATE_WRITE;
  65. ret = mgr->mops->write(mgr, buf, count);
  66. if (ret) {
  67. dev_err(dev, "Error while writing image data to FPGA\n");
  68. mgr->state = FPGA_MGR_STATE_WRITE_ERR;
  69. return ret;
  70. }
  71. /*
  72. * After all the FPGA image has been written, do the device specific
  73. * steps to finish and set the FPGA into operating mode.
  74. */
  75. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
  76. ret = mgr->mops->write_complete(mgr, flags);
  77. if (ret) {
  78. dev_err(dev, "Error after writing image data to FPGA\n");
  79. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
  80. return ret;
  81. }
  82. mgr->state = FPGA_MGR_STATE_OPERATING;
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
  86. /**
  87. * fpga_mgr_firmware_load - request firmware and load to fpga
  88. * @mgr: fpga manager
  89. * @flags: flags setting fpga confuration modes
  90. * @image_name: name of image file on the firmware search path
  91. *
  92. * Request an FPGA image using the firmware class, then write out to the FPGA.
  93. * Update the state before each step to provide info on what step failed if
  94. * there is a failure. This code assumes the caller got the mgr pointer
  95. * from of_fpga_mgr_get() and checked that it is not an error code.
  96. *
  97. * Return: 0 on success, negative error code otherwise.
  98. */
  99. int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
  100. const char *image_name)
  101. {
  102. struct device *dev = &mgr->dev;
  103. const struct firmware *fw;
  104. int ret;
  105. dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
  106. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
  107. ret = request_firmware(&fw, image_name, dev);
  108. if (ret) {
  109. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
  110. dev_err(dev, "Error requesting firmware %s\n", image_name);
  111. return ret;
  112. }
  113. ret = fpga_mgr_buf_load(mgr, flags, fw->data, fw->size);
  114. release_firmware(fw);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
  118. static const char * const state_str[] = {
  119. [FPGA_MGR_STATE_UNKNOWN] = "unknown",
  120. [FPGA_MGR_STATE_POWER_OFF] = "power off",
  121. [FPGA_MGR_STATE_POWER_UP] = "power up",
  122. [FPGA_MGR_STATE_RESET] = "reset",
  123. /* requesting FPGA image from firmware */
  124. [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
  125. [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
  126. /* Preparing FPGA to receive image */
  127. [FPGA_MGR_STATE_WRITE_INIT] = "write init",
  128. [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
  129. /* Writing image to FPGA */
  130. [FPGA_MGR_STATE_WRITE] = "write",
  131. [FPGA_MGR_STATE_WRITE_ERR] = "write error",
  132. /* Finishing configuration after image has been written */
  133. [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
  134. [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
  135. /* FPGA reports to be in normal operating mode */
  136. [FPGA_MGR_STATE_OPERATING] = "operating",
  137. };
  138. static ssize_t name_show(struct device *dev,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct fpga_manager *mgr = to_fpga_manager(dev);
  142. return sprintf(buf, "%s\n", mgr->name);
  143. }
  144. static ssize_t state_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct fpga_manager *mgr = to_fpga_manager(dev);
  148. return sprintf(buf, "%s\n", state_str[mgr->state]);
  149. }
  150. static DEVICE_ATTR_RO(name);
  151. static DEVICE_ATTR_RO(state);
  152. static struct attribute *fpga_mgr_attrs[] = {
  153. &dev_attr_name.attr,
  154. &dev_attr_state.attr,
  155. NULL,
  156. };
  157. ATTRIBUTE_GROUPS(fpga_mgr);
  158. static int fpga_mgr_of_node_match(struct device *dev, const void *data)
  159. {
  160. return dev->of_node == data;
  161. }
  162. /**
  163. * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
  164. * @node: device node
  165. *
  166. * Given a device node, get an exclusive reference to a fpga mgr.
  167. *
  168. * Return: fpga manager struct or IS_ERR() condition containing error code.
  169. */
  170. struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
  171. {
  172. struct fpga_manager *mgr;
  173. struct device *dev;
  174. int ret = -ENODEV;
  175. dev = class_find_device(fpga_mgr_class, NULL, node,
  176. fpga_mgr_of_node_match);
  177. if (!dev)
  178. return ERR_PTR(-ENODEV);
  179. mgr = to_fpga_manager(dev);
  180. if (!mgr)
  181. goto err_dev;
  182. /* Get exclusive use of fpga manager */
  183. if (!mutex_trylock(&mgr->ref_mutex)) {
  184. ret = -EBUSY;
  185. goto err_dev;
  186. }
  187. if (!try_module_get(dev->parent->driver->owner))
  188. goto err_ll_mod;
  189. return mgr;
  190. err_ll_mod:
  191. mutex_unlock(&mgr->ref_mutex);
  192. err_dev:
  193. put_device(dev);
  194. return ERR_PTR(ret);
  195. }
  196. EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
  197. /**
  198. * fpga_mgr_put - release a reference to a fpga manager
  199. * @mgr: fpga manager structure
  200. */
  201. void fpga_mgr_put(struct fpga_manager *mgr)
  202. {
  203. module_put(mgr->dev.parent->driver->owner);
  204. mutex_unlock(&mgr->ref_mutex);
  205. put_device(&mgr->dev);
  206. }
  207. EXPORT_SYMBOL_GPL(fpga_mgr_put);
  208. /**
  209. * fpga_mgr_register - register a low level fpga manager driver
  210. * @dev: fpga manager device from pdev
  211. * @name: fpga manager name
  212. * @mops: pointer to structure of fpga manager ops
  213. * @priv: fpga manager private data
  214. *
  215. * Return: 0 on success, negative error code otherwise.
  216. */
  217. int fpga_mgr_register(struct device *dev, const char *name,
  218. const struct fpga_manager_ops *mops,
  219. void *priv)
  220. {
  221. struct fpga_manager *mgr;
  222. int id, ret;
  223. if (!mops || !mops->write_init || !mops->write ||
  224. !mops->write_complete || !mops->state) {
  225. dev_err(dev, "Attempt to register without fpga_manager_ops\n");
  226. return -EINVAL;
  227. }
  228. if (!name || !strlen(name)) {
  229. dev_err(dev, "Attempt to register with no name!\n");
  230. return -EINVAL;
  231. }
  232. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  233. if (!mgr)
  234. return -ENOMEM;
  235. id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
  236. if (id < 0) {
  237. ret = id;
  238. goto error_kfree;
  239. }
  240. mutex_init(&mgr->ref_mutex);
  241. mgr->name = name;
  242. mgr->mops = mops;
  243. mgr->priv = priv;
  244. /*
  245. * Initialize framework state by requesting low level driver read state
  246. * from device. FPGA may be in reset mode or may have been programmed
  247. * by bootloader or EEPROM.
  248. */
  249. mgr->state = mgr->mops->state(mgr);
  250. device_initialize(&mgr->dev);
  251. mgr->dev.class = fpga_mgr_class;
  252. mgr->dev.parent = dev;
  253. mgr->dev.of_node = dev->of_node;
  254. mgr->dev.id = id;
  255. dev_set_drvdata(dev, mgr);
  256. ret = dev_set_name(&mgr->dev, "fpga%d", id);
  257. if (ret)
  258. goto error_device;
  259. ret = device_add(&mgr->dev);
  260. if (ret)
  261. goto error_device;
  262. dev_info(&mgr->dev, "%s registered\n", mgr->name);
  263. return 0;
  264. error_device:
  265. ida_simple_remove(&fpga_mgr_ida, id);
  266. error_kfree:
  267. kfree(mgr);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL_GPL(fpga_mgr_register);
  271. /**
  272. * fpga_mgr_unregister - unregister a low level fpga manager driver
  273. * @dev: fpga manager device from pdev
  274. */
  275. void fpga_mgr_unregister(struct device *dev)
  276. {
  277. struct fpga_manager *mgr = dev_get_drvdata(dev);
  278. dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
  279. /*
  280. * If the low level driver provides a method for putting fpga into
  281. * a desired state upon unregister, do it.
  282. */
  283. if (mgr->mops->fpga_remove)
  284. mgr->mops->fpga_remove(mgr);
  285. device_unregister(&mgr->dev);
  286. }
  287. EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
  288. static void fpga_mgr_dev_release(struct device *dev)
  289. {
  290. struct fpga_manager *mgr = to_fpga_manager(dev);
  291. ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
  292. kfree(mgr);
  293. }
  294. static int __init fpga_mgr_class_init(void)
  295. {
  296. pr_info("FPGA manager framework\n");
  297. fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
  298. if (IS_ERR(fpga_mgr_class))
  299. return PTR_ERR(fpga_mgr_class);
  300. fpga_mgr_class->dev_groups = fpga_mgr_groups;
  301. fpga_mgr_class->dev_release = fpga_mgr_dev_release;
  302. return 0;
  303. }
  304. static void __exit fpga_mgr_class_exit(void)
  305. {
  306. class_destroy(fpga_mgr_class);
  307. ida_destroy(&fpga_mgr_ida);
  308. }
  309. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  310. MODULE_DESCRIPTION("FPGA manager framework");
  311. MODULE_LICENSE("GPL v2");
  312. subsys_initcall(fpga_mgr_class_init);
  313. module_exit(fpga_mgr_class_exit);