fpga-mgr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. if (ret)
  115. return ret;
  116. release_firmware(fw);
  117. return 0;
  118. }
  119. EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
  120. static const char * const state_str[] = {
  121. [FPGA_MGR_STATE_UNKNOWN] = "unknown",
  122. [FPGA_MGR_STATE_POWER_OFF] = "power off",
  123. [FPGA_MGR_STATE_POWER_UP] = "power up",
  124. [FPGA_MGR_STATE_RESET] = "reset",
  125. /* requesting FPGA image from firmware */
  126. [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
  127. [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
  128. /* Preparing FPGA to receive image */
  129. [FPGA_MGR_STATE_WRITE_INIT] = "write init",
  130. [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
  131. /* Writing image to FPGA */
  132. [FPGA_MGR_STATE_WRITE] = "write",
  133. [FPGA_MGR_STATE_WRITE_ERR] = "write error",
  134. /* Finishing configuration after image has been written */
  135. [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
  136. [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
  137. /* FPGA reports to be in normal operating mode */
  138. [FPGA_MGR_STATE_OPERATING] = "operating",
  139. };
  140. static ssize_t name_show(struct device *dev,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. struct fpga_manager *mgr = to_fpga_manager(dev);
  144. return sprintf(buf, "%s\n", mgr->name);
  145. }
  146. static ssize_t state_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct fpga_manager *mgr = to_fpga_manager(dev);
  150. return sprintf(buf, "%s\n", state_str[mgr->state]);
  151. }
  152. static DEVICE_ATTR_RO(name);
  153. static DEVICE_ATTR_RO(state);
  154. static struct attribute *fpga_mgr_attrs[] = {
  155. &dev_attr_name.attr,
  156. &dev_attr_state.attr,
  157. NULL,
  158. };
  159. ATTRIBUTE_GROUPS(fpga_mgr);
  160. static int fpga_mgr_of_node_match(struct device *dev, const void *data)
  161. {
  162. return dev->of_node == data;
  163. }
  164. /**
  165. * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
  166. * @node: device node
  167. *
  168. * Given a device node, get an exclusive reference to a fpga mgr.
  169. *
  170. * Return: fpga manager struct or IS_ERR() condition containing error code.
  171. */
  172. struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
  173. {
  174. struct fpga_manager *mgr;
  175. struct device *dev;
  176. int ret = -ENODEV;
  177. dev = class_find_device(fpga_mgr_class, NULL, node,
  178. fpga_mgr_of_node_match);
  179. if (!dev)
  180. return ERR_PTR(-ENODEV);
  181. mgr = to_fpga_manager(dev);
  182. if (!mgr)
  183. goto err_dev;
  184. /* Get exclusive use of fpga manager */
  185. if (!mutex_trylock(&mgr->ref_mutex)) {
  186. ret = -EBUSY;
  187. goto err_dev;
  188. }
  189. if (!try_module_get(dev->parent->driver->owner))
  190. goto err_ll_mod;
  191. return mgr;
  192. err_ll_mod:
  193. mutex_unlock(&mgr->ref_mutex);
  194. err_dev:
  195. put_device(dev);
  196. return ERR_PTR(ret);
  197. }
  198. EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
  199. /**
  200. * fpga_mgr_put - release a reference to a fpga manager
  201. * @mgr: fpga manager structure
  202. */
  203. void fpga_mgr_put(struct fpga_manager *mgr)
  204. {
  205. module_put(mgr->dev.parent->driver->owner);
  206. mutex_unlock(&mgr->ref_mutex);
  207. put_device(&mgr->dev);
  208. }
  209. EXPORT_SYMBOL_GPL(fpga_mgr_put);
  210. /**
  211. * fpga_mgr_register - register a low level fpga manager driver
  212. * @dev: fpga manager device from pdev
  213. * @name: fpga manager name
  214. * @mops: pointer to structure of fpga manager ops
  215. * @priv: fpga manager private data
  216. *
  217. * Return: 0 on success, negative error code otherwise.
  218. */
  219. int fpga_mgr_register(struct device *dev, const char *name,
  220. const struct fpga_manager_ops *mops,
  221. void *priv)
  222. {
  223. struct fpga_manager *mgr;
  224. const char *dt_label;
  225. int id, ret;
  226. if (!mops || !mops->write_init || !mops->write ||
  227. !mops->write_complete || !mops->state) {
  228. dev_err(dev, "Attempt to register without fpga_manager_ops\n");
  229. return -EINVAL;
  230. }
  231. if (!name || !strlen(name)) {
  232. dev_err(dev, "Attempt to register with no name!\n");
  233. return -EINVAL;
  234. }
  235. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  236. if (!mgr)
  237. return -ENOMEM;
  238. id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
  239. if (id < 0) {
  240. ret = id;
  241. goto error_kfree;
  242. }
  243. mutex_init(&mgr->ref_mutex);
  244. mgr->name = name;
  245. mgr->mops = mops;
  246. mgr->priv = priv;
  247. /*
  248. * Initialize framework state by requesting low level driver read state
  249. * from device. FPGA may be in reset mode or may have been programmed
  250. * by bootloader or EEPROM.
  251. */
  252. mgr->state = mgr->mops->state(mgr);
  253. device_initialize(&mgr->dev);
  254. mgr->dev.class = fpga_mgr_class;
  255. mgr->dev.parent = dev;
  256. mgr->dev.of_node = dev->of_node;
  257. mgr->dev.id = id;
  258. dev_set_drvdata(dev, mgr);
  259. dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
  260. if (dt_label)
  261. ret = dev_set_name(&mgr->dev, "%s", dt_label);
  262. else
  263. ret = dev_set_name(&mgr->dev, "fpga%d", id);
  264. ret = device_add(&mgr->dev);
  265. if (ret)
  266. goto error_device;
  267. dev_info(&mgr->dev, "%s registered\n", mgr->name);
  268. return 0;
  269. error_device:
  270. ida_simple_remove(&fpga_mgr_ida, id);
  271. error_kfree:
  272. kfree(mgr);
  273. return ret;
  274. }
  275. EXPORT_SYMBOL_GPL(fpga_mgr_register);
  276. /**
  277. * fpga_mgr_unregister - unregister a low level fpga manager driver
  278. * @dev: fpga manager device from pdev
  279. */
  280. void fpga_mgr_unregister(struct device *dev)
  281. {
  282. struct fpga_manager *mgr = dev_get_drvdata(dev);
  283. dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
  284. /*
  285. * If the low level driver provides a method for putting fpga into
  286. * a desired state upon unregister, do it.
  287. */
  288. if (mgr->mops->fpga_remove)
  289. mgr->mops->fpga_remove(mgr);
  290. device_unregister(&mgr->dev);
  291. }
  292. EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
  293. static void fpga_mgr_dev_release(struct device *dev)
  294. {
  295. struct fpga_manager *mgr = to_fpga_manager(dev);
  296. ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
  297. kfree(mgr);
  298. }
  299. static int __init fpga_mgr_class_init(void)
  300. {
  301. pr_info("FPGA manager framework\n");
  302. fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
  303. if (IS_ERR(fpga_mgr_class))
  304. return PTR_ERR(fpga_mgr_class);
  305. fpga_mgr_class->dev_groups = fpga_mgr_groups;
  306. fpga_mgr_class->dev_release = fpga_mgr_dev_release;
  307. return 0;
  308. }
  309. static void __exit fpga_mgr_class_exit(void)
  310. {
  311. class_destroy(fpga_mgr_class);
  312. ida_destroy(&fpga_mgr_ida);
  313. }
  314. MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
  315. MODULE_DESCRIPTION("FPGA manager framework");
  316. MODULE_LICENSE("GPL v2");
  317. subsys_initcall(fpga_mgr_class_init);
  318. module_exit(fpga_mgr_class_exit);