powernv_flash.c 7.5 KB

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