powernv_flash.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. static int powernv_flash_async_op(struct mtd_info *mtd, enum flash_op op,
  43. loff_t offset, size_t len, size_t *retlen, u_char *buf)
  44. {
  45. struct powernv_flash *info = (struct powernv_flash *)mtd->priv;
  46. struct device *dev = &mtd->dev;
  47. int token;
  48. struct opal_msg msg;
  49. int rc;
  50. dev_dbg(dev, "%s(op=%d, offset=0x%llx, len=%zu)\n",
  51. __func__, op, offset, len);
  52. token = opal_async_get_token_interruptible();
  53. if (token < 0) {
  54. if (token != -ERESTARTSYS)
  55. dev_err(dev, "Failed to get an async token\n");
  56. return token;
  57. }
  58. switch (op) {
  59. case FLASH_OP_READ:
  60. rc = opal_flash_read(info->id, offset, __pa(buf), len, token);
  61. break;
  62. case FLASH_OP_WRITE:
  63. rc = opal_flash_write(info->id, offset, __pa(buf), len, token);
  64. break;
  65. case FLASH_OP_ERASE:
  66. rc = opal_flash_erase(info->id, offset, len, token);
  67. break;
  68. default:
  69. BUG_ON(1);
  70. }
  71. if (rc != OPAL_ASYNC_COMPLETION) {
  72. dev_err(dev, "opal_flash_async_op(op=%d) failed (rc %d)\n",
  73. op, rc);
  74. opal_async_release_token(token);
  75. return -EIO;
  76. }
  77. rc = opal_async_wait_response(token, &msg);
  78. opal_async_release_token(token);
  79. if (rc) {
  80. dev_err(dev, "opal async wait failed (rc %d)\n", rc);
  81. return -EIO;
  82. }
  83. rc = be64_to_cpu(msg.params[1]);
  84. if (rc == OPAL_SUCCESS) {
  85. rc = 0;
  86. if (retlen)
  87. *retlen = len;
  88. } else {
  89. rc = -EIO;
  90. }
  91. return rc;
  92. }
  93. /**
  94. * @mtd: the device
  95. * @from: the offset to read from
  96. * @len: the number of bytes to read
  97. * @retlen: the number of bytes actually read
  98. * @buf: the filled in buffer
  99. *
  100. * Returns 0 if read successful, or -ERRNO if an error occurred
  101. */
  102. static int powernv_flash_read(struct mtd_info *mtd, loff_t from, size_t len,
  103. size_t *retlen, u_char *buf)
  104. {
  105. return powernv_flash_async_op(mtd, FLASH_OP_READ, from,
  106. len, retlen, buf);
  107. }
  108. /**
  109. * @mtd: the device
  110. * @to: the offset to write to
  111. * @len: the number of bytes to write
  112. * @retlen: the number of bytes actually written
  113. * @buf: the buffer to get bytes from
  114. *
  115. * Returns 0 if write successful, -ERRNO if error occurred
  116. */
  117. static int powernv_flash_write(struct mtd_info *mtd, loff_t to, size_t len,
  118. size_t *retlen, const u_char *buf)
  119. {
  120. return powernv_flash_async_op(mtd, FLASH_OP_WRITE, to,
  121. len, retlen, (u_char *)buf);
  122. }
  123. /**
  124. * @mtd: the device
  125. * @erase: the erase info
  126. * Returns 0 if erase successful or -ERRNO if an error occurred
  127. */
  128. static int powernv_flash_erase(struct mtd_info *mtd, struct erase_info *erase)
  129. {
  130. int rc;
  131. erase->state = MTD_ERASING;
  132. /* todo: register our own notifier to do a true async implementation */
  133. rc = powernv_flash_async_op(mtd, FLASH_OP_ERASE, erase->addr,
  134. erase->len, NULL, NULL);
  135. if (rc) {
  136. erase->fail_addr = erase->addr;
  137. erase->state = MTD_ERASE_FAILED;
  138. } else {
  139. erase->state = MTD_ERASE_DONE;
  140. }
  141. mtd_erase_callback(erase);
  142. return rc;
  143. }
  144. /**
  145. * powernv_flash_set_driver_info - Fill the mtd_info structure and docg3
  146. * structure @pdev: The platform device
  147. * @mtd: The structure to fill
  148. */
  149. static int powernv_flash_set_driver_info(struct device *dev,
  150. struct mtd_info *mtd)
  151. {
  152. u64 size;
  153. u32 erase_size;
  154. int rc;
  155. rc = of_property_read_u32(dev->of_node, "ibm,flash-block-size",
  156. &erase_size);
  157. if (rc) {
  158. dev_err(dev, "couldn't get resource block size information\n");
  159. return rc;
  160. }
  161. rc = of_property_read_u64(dev->of_node, "reg", &size);
  162. if (rc) {
  163. dev_err(dev, "couldn't get resource size information\n");
  164. return rc;
  165. }
  166. /*
  167. * Going to have to check what details I need to set and how to
  168. * get them
  169. */
  170. mtd->name = of_get_property(dev->of_node, "name", NULL);
  171. mtd->type = MTD_NORFLASH;
  172. mtd->flags = MTD_WRITEABLE;
  173. mtd->size = size;
  174. mtd->erasesize = erase_size;
  175. mtd->writebufsize = mtd->writesize = 1;
  176. mtd->owner = THIS_MODULE;
  177. mtd->_erase = powernv_flash_erase;
  178. mtd->_read = powernv_flash_read;
  179. mtd->_write = powernv_flash_write;
  180. mtd->dev.parent = dev;
  181. return 0;
  182. }
  183. /**
  184. * powernv_flash_probe
  185. * @pdev: platform device
  186. *
  187. * Returns 0 on success, -ENOMEM, -ENXIO on error
  188. */
  189. static int powernv_flash_probe(struct platform_device *pdev)
  190. {
  191. struct device *dev = &pdev->dev;
  192. struct powernv_flash *data;
  193. int ret;
  194. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  195. if (!data) {
  196. ret = -ENOMEM;
  197. goto out;
  198. }
  199. data->mtd.priv = data;
  200. ret = of_property_read_u32(dev->of_node, "ibm,opal-id", &(data->id));
  201. if (ret) {
  202. dev_err(dev, "no device property 'ibm,opal-id'\n");
  203. goto out;
  204. }
  205. ret = powernv_flash_set_driver_info(dev, &data->mtd);
  206. if (ret)
  207. goto out;
  208. dev_set_drvdata(dev, data);
  209. /*
  210. * The current flash that skiboot exposes is one contiguous flash chip
  211. * with an ffs partition at the start, it should prove easier for users
  212. * to deal with partitions or not as they see fit
  213. */
  214. ret = mtd_device_register(&data->mtd, NULL, 0);
  215. out:
  216. return ret;
  217. }
  218. /**
  219. * op_release - Release the driver
  220. * @pdev: the platform device
  221. *
  222. * Returns 0
  223. */
  224. static int powernv_flash_release(struct platform_device *pdev)
  225. {
  226. struct powernv_flash *data = dev_get_drvdata(&(pdev->dev));
  227. /* All resources should be freed automatically */
  228. return mtd_device_unregister(&(data->mtd));
  229. }
  230. static const struct of_device_id powernv_flash_match[] = {
  231. { .compatible = "ibm,opal-flash" },
  232. {}
  233. };
  234. static struct platform_driver powernv_flash_driver = {
  235. .driver = {
  236. .name = "powernv_flash",
  237. .of_match_table = powernv_flash_match,
  238. },
  239. .remove = powernv_flash_release,
  240. .probe = powernv_flash_probe,
  241. };
  242. module_platform_driver(powernv_flash_driver);
  243. MODULE_DEVICE_TABLE(of, powernv_flash_match);
  244. MODULE_LICENSE("GPL");
  245. MODULE_AUTHOR("Cyril Bur <cyril.bur@au1.ibm.com>");
  246. MODULE_DESCRIPTION("MTD abstraction for OPAL flash");