fpga-mgr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. * @info: fpga image specific information
  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() or fpga_mgr_get() and checked that it is
  41. * not an error code.
  42. *
  43. * Return: 0 on success, negative error code otherwise.
  44. */
  45. int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
  46. const char *buf, size_t count)
  47. {
  48. struct device *dev = &mgr->dev;
  49. int ret;
  50. /*
  51. * Call the low level driver's write_init function. This will do the
  52. * device-specific things to get the FPGA into the state where it is
  53. * ready to receive an FPGA image. The low level driver only gets to
  54. * see the first initial_header_size bytes in the buffer.
  55. */
  56. mgr->state = FPGA_MGR_STATE_WRITE_INIT;
  57. ret = mgr->mops->write_init(mgr, info, buf,
  58. min(mgr->mops->initial_header_size, count));
  59. if (ret) {
  60. dev_err(dev, "Error preparing FPGA for writing\n");
  61. mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
  62. return ret;
  63. }
  64. /*
  65. * Write the FPGA image to the FPGA.
  66. */
  67. mgr->state = FPGA_MGR_STATE_WRITE;
  68. ret = mgr->mops->write(mgr, buf, count);
  69. if (ret) {
  70. dev_err(dev, "Error while writing image data to FPGA\n");
  71. mgr->state = FPGA_MGR_STATE_WRITE_ERR;
  72. return ret;
  73. }
  74. /*
  75. * After all the FPGA image has been written, do the device specific
  76. * steps to finish and set the FPGA into operating mode.
  77. */
  78. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
  79. ret = mgr->mops->write_complete(mgr, info);
  80. if (ret) {
  81. dev_err(dev, "Error after writing image data to FPGA\n");
  82. mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
  83. return ret;
  84. }
  85. mgr->state = FPGA_MGR_STATE_OPERATING;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
  89. /**
  90. * fpga_mgr_firmware_load - request firmware and load to fpga
  91. * @mgr: fpga manager
  92. * @info: fpga image specific information
  93. * @image_name: name of image file on the firmware search path
  94. *
  95. * Request an FPGA image using the firmware class, then write out to the FPGA.
  96. * Update the state before each step to provide info on what step failed if
  97. * there is a failure. This code assumes the caller got the mgr pointer
  98. * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
  99. * code.
  100. *
  101. * Return: 0 on success, negative error code otherwise.
  102. */
  103. int fpga_mgr_firmware_load(struct fpga_manager *mgr,
  104. struct fpga_image_info *info,
  105. const char *image_name)
  106. {
  107. struct device *dev = &mgr->dev;
  108. const struct firmware *fw;
  109. int ret;
  110. dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
  111. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
  112. ret = request_firmware(&fw, image_name, dev);
  113. if (ret) {
  114. mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
  115. dev_err(dev, "Error requesting firmware %s\n", image_name);
  116. return ret;
  117. }
  118. ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
  119. release_firmware(fw);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
  123. static const char * const state_str[] = {
  124. [FPGA_MGR_STATE_UNKNOWN] = "unknown",
  125. [FPGA_MGR_STATE_POWER_OFF] = "power off",
  126. [FPGA_MGR_STATE_POWER_UP] = "power up",
  127. [FPGA_MGR_STATE_RESET] = "reset",
  128. /* requesting FPGA image from firmware */
  129. [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
  130. [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
  131. /* Preparing FPGA to receive image */
  132. [FPGA_MGR_STATE_WRITE_INIT] = "write init",
  133. [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
  134. /* Writing image to FPGA */
  135. [FPGA_MGR_STATE_WRITE] = "write",
  136. [FPGA_MGR_STATE_WRITE_ERR] = "write error",
  137. /* Finishing configuration after image has been written */
  138. [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
  139. [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
  140. /* FPGA reports to be in normal operating mode */
  141. [FPGA_MGR_STATE_OPERATING] = "operating",
  142. };
  143. static ssize_t name_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct fpga_manager *mgr = to_fpga_manager(dev);
  147. return sprintf(buf, "%s\n", mgr->name);
  148. }
  149. static ssize_t state_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. struct fpga_manager *mgr = to_fpga_manager(dev);
  153. return sprintf(buf, "%s\n", state_str[mgr->state]);
  154. }
  155. static DEVICE_ATTR_RO(name);
  156. static DEVICE_ATTR_RO(state);
  157. static struct attribute *fpga_mgr_attrs[] = {
  158. &dev_attr_name.attr,
  159. &dev_attr_state.attr,
  160. NULL,
  161. };
  162. ATTRIBUTE_GROUPS(fpga_mgr);
  163. struct fpga_manager *__fpga_mgr_get(struct device *dev)
  164. {
  165. struct fpga_manager *mgr;
  166. int ret = -ENODEV;
  167. mgr = to_fpga_manager(dev);
  168. if (!mgr)
  169. goto err_dev;
  170. /* Get exclusive use of fpga manager */
  171. if (!mutex_trylock(&mgr->ref_mutex)) {
  172. ret = -EBUSY;
  173. goto err_dev;
  174. }
  175. if (!try_module_get(dev->parent->driver->owner))
  176. goto err_ll_mod;
  177. return mgr;
  178. err_ll_mod:
  179. mutex_unlock(&mgr->ref_mutex);
  180. err_dev:
  181. put_device(dev);
  182. return ERR_PTR(ret);
  183. }
  184. static int fpga_mgr_dev_match(struct device *dev, const void *data)
  185. {
  186. return dev->parent == data;
  187. }
  188. /**
  189. * fpga_mgr_get - get an exclusive reference to a fpga mgr
  190. * @dev: parent device that fpga mgr was registered with
  191. *
  192. * Given a device, get an exclusive reference to a fpga mgr.
  193. *
  194. * Return: fpga manager struct or IS_ERR() condition containing error code.
  195. */
  196. struct fpga_manager *fpga_mgr_get(struct device *dev)
  197. {
  198. struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
  199. fpga_mgr_dev_match);
  200. if (!mgr_dev)
  201. return ERR_PTR(-ENODEV);
  202. return __fpga_mgr_get(mgr_dev);
  203. }
  204. EXPORT_SYMBOL_GPL(fpga_mgr_get);
  205. static int fpga_mgr_of_node_match(struct device *dev, const void *data)
  206. {
  207. return dev->of_node == data;
  208. }
  209. /**
  210. * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
  211. * @node: device node
  212. *
  213. * Given a device node, get an exclusive reference to a fpga mgr.
  214. *
  215. * Return: fpga manager struct or IS_ERR() condition containing error code.
  216. */
  217. struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
  218. {
  219. struct device *dev;
  220. dev = class_find_device(fpga_mgr_class, NULL, node,
  221. fpga_mgr_of_node_match);
  222. if (!dev)
  223. return ERR_PTR(-ENODEV);
  224. return __fpga_mgr_get(dev);
  225. }
  226. EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
  227. /**
  228. * fpga_mgr_put - release a reference to a fpga manager
  229. * @mgr: fpga manager structure
  230. */
  231. void fpga_mgr_put(struct fpga_manager *mgr)
  232. {
  233. module_put(mgr->dev.parent->driver->owner);
  234. mutex_unlock(&mgr->ref_mutex);
  235. put_device(&mgr->dev);
  236. }
  237. EXPORT_SYMBOL_GPL(fpga_mgr_put);
  238. /**
  239. * fpga_mgr_register - register a low level fpga manager driver
  240. * @dev: fpga manager device from pdev
  241. * @name: fpga manager name
  242. * @mops: pointer to structure of fpga manager ops
  243. * @priv: fpga manager private data
  244. *
  245. * Return: 0 on success, negative error code otherwise.
  246. */
  247. int fpga_mgr_register(struct device *dev, const char *name,
  248. const struct fpga_manager_ops *mops,
  249. void *priv)
  250. {
  251. struct fpga_manager *mgr;
  252. int id, ret;
  253. if (!mops || !mops->write_init || !mops->write ||
  254. !mops->write_complete || !mops->state) {
  255. dev_err(dev, "Attempt to register without fpga_manager_ops\n");
  256. return -EINVAL;
  257. }
  258. if (!name || !strlen(name)) {
  259. dev_err(dev, "Attempt to register with no name!\n");
  260. return -EINVAL;
  261. }
  262. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  263. if (!mgr)
  264. return -ENOMEM;
  265. id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
  266. if (id < 0) {
  267. ret = id;
  268. goto error_kfree;
  269. }
  270. mutex_init(&mgr->ref_mutex);
  271. mgr->name = name;
  272. mgr->mops = mops;
  273. mgr->priv = priv;
  274. /*
  275. * Initialize framework state by requesting low level driver read state
  276. * from device. FPGA may be in reset mode or may have been programmed
  277. * by bootloader or EEPROM.
  278. */
  279. mgr->state = mgr->mops->state(mgr);
  280. device_initialize(&mgr->dev);
  281. mgr->dev.class = fpga_mgr_class;
  282. mgr->dev.parent = dev;
  283. mgr->dev.of_node = dev->of_node;
  284. mgr->dev.id = id;
  285. dev_set_drvdata(dev, mgr);
  286. ret = dev_set_name(&mgr->dev, "fpga%d", id);
  287. if (ret)
  288. goto error_device;
  289. ret = device_add(&mgr->dev);
  290. if (ret)
  291. goto error_device;
  292. dev_info(&mgr->dev, "%s registered\n", mgr->name);
  293. return 0;
  294. error_device:
  295. ida_simple_remove(&fpga_mgr_ida, id);
  296. error_kfree:
  297. kfree(mgr);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL_GPL(fpga_mgr_register);
  301. /**
  302. * fpga_mgr_unregister - unregister a low level fpga manager driver
  303. * @dev: fpga manager device from pdev
  304. */
  305. void fpga_mgr_unregister(struct device *dev)
  306. {
  307. struct fpga_manager *mgr = dev_get_drvdata(dev);
  308. dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
  309. /*
  310. * If the low level driver provides a method for putting fpga into
  311. * a desired state upon unregister, do it.
  312. */
  313. if (mgr->mops->fpga_remove)
  314. mgr->mops->fpga_remove(mgr);
  315. device_unregister(&mgr->dev);
  316. }
  317. EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
  318. static void fpga_mgr_dev_release(struct device *dev)
  319. {
  320. struct fpga_manager *mgr = to_fpga_manager(dev);
  321. ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
  322. kfree(mgr);
  323. }
  324. static int __init fpga_mgr_class_init(void)
  325. {
  326. pr_info("FPGA manager framework\n");
  327. fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
  328. if (IS_ERR(fpga_mgr_class))
  329. return PTR_ERR(fpga_mgr_class);
  330. fpga_mgr_class->dev_groups = fpga_mgr_groups;
  331. fpga_mgr_class->dev_release = fpga_mgr_dev_release;
  332. return 0;
  333. }
  334. static void __exit fpga_mgr_class_exit(void)
  335. {
  336. class_destroy(fpga_mgr_class);
  337. ida_destroy(&fpga_mgr_ida);
  338. }
  339. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  340. MODULE_DESCRIPTION("FPGA manager framework");
  341. MODULE_LICENSE("GPL v2");
  342. subsys_initcall(fpga_mgr_class_init);
  343. module_exit(fpga_mgr_class_exit);