powernv_flash.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OPAL PNOR flash MTD abstraction
  4. *
  5. * Copyright IBM 2015
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <asm/opal.h>
  20. /*
  21. * This driver creates the a Linux MTD abstraction for platform PNOR flash
  22. * backed by OPAL calls
  23. */
  24. struct powernv_flash {
  25. struct mtd_info mtd;
  26. u32 id;
  27. };
  28. enum flash_op {
  29. FLASH_OP_READ,
  30. FLASH_OP_WRITE,
  31. FLASH_OP_ERASE,
  32. };
  33. /*
  34. * Don't return -ERESTARTSYS if we can't get a token, the MTD core
  35. * might have split up the call from userspace and called into the
  36. * driver more than once, we'll already have done some amount of work.
  37. */
  38. static int powernv_flash_async_op(struct mtd_info *mtd, enum flash_op op,
  39. loff_t offset, size_t len, size_t *retlen, u_char *buf)
  40. {
  41. struct powernv_flash *info = (struct powernv_flash *)mtd->priv;
  42. struct device *dev = &mtd->dev;
  43. int token;
  44. struct opal_msg msg;
  45. int rc;
  46. dev_dbg(dev, "%s(op=%d, offset=0x%llx, len=%zu)\n",
  47. __func__, op, offset, len);
  48. token = opal_async_get_token_interruptible();
  49. if (token < 0) {
  50. if (token != -ERESTARTSYS)
  51. dev_err(dev, "Failed to get an async token\n");
  52. else
  53. token = -EINTR;
  54. return token;
  55. }
  56. switch (op) {
  57. case FLASH_OP_READ:
  58. rc = opal_flash_read(info->id, offset, __pa(buf), len, token);
  59. break;
  60. case FLASH_OP_WRITE:
  61. rc = opal_flash_write(info->id, offset, __pa(buf), len, token);
  62. break;
  63. case FLASH_OP_ERASE:
  64. rc = opal_flash_erase(info->id, offset, len, token);
  65. break;
  66. default:
  67. WARN_ON_ONCE(1);
  68. opal_async_release_token(token);
  69. return -EIO;
  70. }
  71. if (rc == OPAL_ASYNC_COMPLETION) {
  72. rc = opal_async_wait_response_interruptible(token, &msg);
  73. if (rc) {
  74. /*
  75. * If we return the mtd core will free the
  76. * buffer we've just passed to OPAL but OPAL
  77. * will continue to read or write from that
  78. * memory.
  79. * It may be tempting to ultimately return 0
  80. * if we're doing a read or a write since we
  81. * are going to end up waiting until OPAL is
  82. * done. However, because the MTD core sends
  83. * us the userspace request in chunks, we need
  84. * it to know we've been interrupted.
  85. */
  86. rc = -EINTR;
  87. if (opal_async_wait_response(token, &msg))
  88. dev_err(dev, "opal_async_wait_response() failed\n");
  89. goto out;
  90. }
  91. rc = opal_get_async_rc(msg);
  92. }
  93. /*
  94. * OPAL does mutual exclusion on the flash, it will return
  95. * OPAL_BUSY.
  96. * During firmware updates by the service processor OPAL may
  97. * be (temporarily) prevented from accessing the flash, in
  98. * this case OPAL will also return OPAL_BUSY.
  99. * Both cases aren't errors exactly but the flash could have
  100. * changed, userspace should be informed.
  101. */
  102. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY)
  103. dev_err(dev, "opal_flash_async_op(op=%d) failed (rc %d)\n",
  104. op, rc);
  105. if (rc == OPAL_SUCCESS && retlen)
  106. *retlen = len;
  107. rc = opal_error_code(rc);
  108. out:
  109. opal_async_release_token(token);
  110. return rc;
  111. }
  112. /**
  113. * powernv_flash_read
  114. * @mtd: the device
  115. * @from: the offset to read from
  116. * @len: the number of bytes to read
  117. * @retlen: the number of bytes actually read
  118. * @buf: the filled in buffer
  119. *
  120. * Returns 0 if read successful, or -ERRNO if an error occurred
  121. */
  122. static int powernv_flash_read(struct mtd_info *mtd, loff_t from, size_t len,
  123. size_t *retlen, u_char *buf)
  124. {
  125. return powernv_flash_async_op(mtd, FLASH_OP_READ, from,
  126. len, retlen, buf);
  127. }
  128. /**
  129. * powernv_flash_write
  130. * @mtd: the device
  131. * @to: the offset to write to
  132. * @len: the number of bytes to write
  133. * @retlen: the number of bytes actually written
  134. * @buf: the buffer to get bytes from
  135. *
  136. * Returns 0 if write successful, -ERRNO if error occurred
  137. */
  138. static int powernv_flash_write(struct mtd_info *mtd, loff_t to, size_t len,
  139. size_t *retlen, const u_char *buf)
  140. {
  141. return powernv_flash_async_op(mtd, FLASH_OP_WRITE, to,
  142. len, retlen, (u_char *)buf);
  143. }
  144. /**
  145. * powernv_flash_erase
  146. * @mtd: the device
  147. * @erase: the erase info
  148. * Returns 0 if erase successful or -ERRNO if an error occurred
  149. */
  150. static int powernv_flash_erase(struct mtd_info *mtd, struct erase_info *erase)
  151. {
  152. int rc;
  153. rc = powernv_flash_async_op(mtd, FLASH_OP_ERASE, erase->addr,
  154. erase->len, NULL, NULL);
  155. if (rc)
  156. erase->fail_addr = erase->addr;
  157. return rc;
  158. }
  159. /**
  160. * powernv_flash_set_driver_info - Fill the mtd_info structure and docg3
  161. * @dev: The device structure
  162. * @mtd: The structure to fill
  163. */
  164. static int powernv_flash_set_driver_info(struct device *dev,
  165. struct mtd_info *mtd)
  166. {
  167. u64 size;
  168. u32 erase_size;
  169. int rc;
  170. rc = of_property_read_u32(dev->of_node, "ibm,flash-block-size",
  171. &erase_size);
  172. if (rc) {
  173. dev_err(dev, "couldn't get resource block size information\n");
  174. return rc;
  175. }
  176. rc = of_property_read_u64(dev->of_node, "reg", &size);
  177. if (rc) {
  178. dev_err(dev, "couldn't get resource size information\n");
  179. return rc;
  180. }
  181. /*
  182. * Going to have to check what details I need to set and how to
  183. * get them
  184. */
  185. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
  186. mtd->type = MTD_NORFLASH;
  187. mtd->flags = MTD_WRITEABLE;
  188. mtd->size = size;
  189. mtd->erasesize = erase_size;
  190. mtd->writebufsize = mtd->writesize = 1;
  191. mtd->owner = THIS_MODULE;
  192. mtd->_erase = powernv_flash_erase;
  193. mtd->_read = powernv_flash_read;
  194. mtd->_write = powernv_flash_write;
  195. mtd->dev.parent = dev;
  196. mtd_set_of_node(mtd, dev->of_node);
  197. return 0;
  198. }
  199. /**
  200. * powernv_flash_probe
  201. * @pdev: platform device
  202. *
  203. * Returns 0 on success, -ENOMEM, -ENXIO on error
  204. */
  205. static int powernv_flash_probe(struct platform_device *pdev)
  206. {
  207. struct device *dev = &pdev->dev;
  208. struct powernv_flash *data;
  209. int ret;
  210. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  211. if (!data)
  212. return -ENOMEM;
  213. data->mtd.priv = data;
  214. ret = of_property_read_u32(dev->of_node, "ibm,opal-id", &(data->id));
  215. if (ret) {
  216. dev_err(dev, "no device property 'ibm,opal-id'\n");
  217. return ret;
  218. }
  219. ret = powernv_flash_set_driver_info(dev, &data->mtd);
  220. if (ret)
  221. return ret;
  222. dev_set_drvdata(dev, data);
  223. /*
  224. * The current flash that skiboot exposes is one contiguous flash chip
  225. * with an ffs partition at the start, it should prove easier for users
  226. * to deal with partitions or not as they see fit
  227. */
  228. return mtd_device_register(&data->mtd, NULL, 0);
  229. }
  230. /**
  231. * op_release - Release the driver
  232. * @pdev: the platform device
  233. *
  234. * Returns 0
  235. */
  236. static int powernv_flash_release(struct platform_device *pdev)
  237. {
  238. struct powernv_flash *data = dev_get_drvdata(&(pdev->dev));
  239. /* All resources should be freed automatically */
  240. WARN_ON(mtd_device_unregister(&data->mtd));
  241. return 0;
  242. }
  243. static const struct of_device_id powernv_flash_match[] = {
  244. { .compatible = "ibm,opal-flash" },
  245. {}
  246. };
  247. static struct platform_driver powernv_flash_driver = {
  248. .driver = {
  249. .name = "powernv_flash",
  250. .of_match_table = powernv_flash_match,
  251. },
  252. .remove = powernv_flash_release,
  253. .probe = powernv_flash_probe,
  254. };
  255. module_platform_driver(powernv_flash_driver);
  256. MODULE_DEVICE_TABLE(of, powernv_flash_match);
  257. MODULE_LICENSE("GPL");
  258. MODULE_AUTHOR("Cyril Bur <[email protected]>");
  259. MODULE_DESCRIPTION("MTD abstraction for OPAL flash");