1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OPAL PNOR flash MTD abstraction
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/errno.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
26 * This driver creates the a Linux MTD abstraction for platform PNOR flash
27 * backed by OPAL calls
30 struct powernv_flash {
42 * Don't return -ERESTARTSYS if we can't get a token, the MTD core
43 * might have split up the call from userspace and called into the
44 * driver more than once, we'll already have done some amount of work.
46 static int powernv_flash_async_op(struct mtd_info *mtd, enum flash_op op,
47 loff_t offset, size_t len, size_t *retlen, u_char *buf)
49 struct powernv_flash *info = (struct powernv_flash *)mtd->priv;
50 struct device *dev = &mtd->dev;
55 dev_dbg(dev, "%s(op=%d, offset=0x%llx, len=%zu)\n",
56 __func__, op, offset, len);
58 token = opal_async_get_token_interruptible();
60 if (token != -ERESTARTSYS)
61 dev_err(dev, "Failed to get an async token\n");
69 rc = opal_flash_read(info->id, offset, __pa(buf), len, token);
72 rc = opal_flash_write(info->id, offset, __pa(buf), len, token);
75 rc = opal_flash_erase(info->id, offset, len, token);
79 opal_async_release_token(token);
83 if (rc == OPAL_ASYNC_COMPLETION) {
84 rc = opal_async_wait_response_interruptible(token, &msg);
87 * If we return the mtd core will free the
88 * buffer we've just passed to OPAL but OPAL
89 * will continue to read or write from that
91 * It may be tempting to ultimately return 0
92 * if we're doing a read or a write since we
93 * are going to end up waiting until OPAL is
94 * done. However, because the MTD core sends
95 * us the userspace request in chunks, we need
96 * it to know we've been interrupted.
99 if (opal_async_wait_response(token, &msg))
100 dev_err(dev, "opal_async_wait_response() failed\n");
103 rc = opal_get_async_rc(msg);
107 * OPAL does mutual exclusion on the flash, it will return
109 * During firmware updates by the service processor OPAL may
110 * be (temporarily) prevented from accessing the flash, in
111 * this case OPAL will also return OPAL_BUSY.
112 * Both cases aren't errors exactly but the flash could have
113 * changed, userspace should be informed.
115 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY)
116 dev_err(dev, "opal_flash_async_op(op=%d) failed (rc %d)\n",
119 if (rc == OPAL_SUCCESS && retlen)
122 rc = opal_error_code(rc);
124 opal_async_release_token(token);
131 * @from: the offset to read from
132 * @len: the number of bytes to read
133 * @retlen: the number of bytes actually read
134 * @buf: the filled in buffer
136 * Returns 0 if read successful, or -ERRNO if an error occurred
138 static int powernv_flash_read(struct mtd_info *mtd, loff_t from, size_t len,
139 size_t *retlen, u_char *buf)
141 return powernv_flash_async_op(mtd, FLASH_OP_READ, from,
146 * powernv_flash_write
148 * @to: the offset to write to
149 * @len: the number of bytes to write
150 * @retlen: the number of bytes actually written
151 * @buf: the buffer to get bytes from
153 * Returns 0 if write successful, -ERRNO if error occurred
155 static int powernv_flash_write(struct mtd_info *mtd, loff_t to, size_t len,
156 size_t *retlen, const u_char *buf)
158 return powernv_flash_async_op(mtd, FLASH_OP_WRITE, to,
159 len, retlen, (u_char *)buf);
163 * powernv_flash_erase
165 * @erase: the erase info
166 * Returns 0 if erase successful or -ERRNO if an error occurred
168 static int powernv_flash_erase(struct mtd_info *mtd, struct erase_info *erase)
172 rc = powernv_flash_async_op(mtd, FLASH_OP_ERASE, erase->addr,
173 erase->len, NULL, NULL);
175 erase->fail_addr = erase->addr;
181 * powernv_flash_set_driver_info - Fill the mtd_info structure and docg3
182 * @dev: The device structure
183 * @mtd: The structure to fill
185 static int powernv_flash_set_driver_info(struct device *dev,
186 struct mtd_info *mtd)
192 rc = of_property_read_u32(dev->of_node, "ibm,flash-block-size",
195 dev_err(dev, "couldn't get resource block size information\n");
199 rc = of_property_read_u64(dev->of_node, "reg", &size);
201 dev_err(dev, "couldn't get resource size information\n");
206 * Going to have to check what details I need to set and how to
209 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
210 mtd->type = MTD_NORFLASH;
211 mtd->flags = MTD_WRITEABLE;
213 mtd->erasesize = erase_size;
214 mtd->writebufsize = mtd->writesize = 1;
215 mtd->owner = THIS_MODULE;
216 mtd->_erase = powernv_flash_erase;
217 mtd->_read = powernv_flash_read;
218 mtd->_write = powernv_flash_write;
219 mtd->dev.parent = dev;
220 mtd_set_of_node(mtd, dev->of_node);
225 * powernv_flash_probe
226 * @pdev: platform device
228 * Returns 0 on success, -ENOMEM, -ENXIO on error
230 static int powernv_flash_probe(struct platform_device *pdev)
232 struct device *dev = &pdev->dev;
233 struct powernv_flash *data;
236 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
240 data->mtd.priv = data;
242 ret = of_property_read_u32(dev->of_node, "ibm,opal-id", &(data->id));
244 dev_err(dev, "no device property 'ibm,opal-id'\n");
248 ret = powernv_flash_set_driver_info(dev, &data->mtd);
252 dev_set_drvdata(dev, data);
255 * The current flash that skiboot exposes is one contiguous flash chip
256 * with an ffs partition at the start, it should prove easier for users
257 * to deal with partitions or not as they see fit
259 return mtd_device_register(&data->mtd, NULL, 0);
263 * op_release - Release the driver
264 * @pdev: the platform device
268 static int powernv_flash_release(struct platform_device *pdev)
270 struct powernv_flash *data = dev_get_drvdata(&(pdev->dev));
272 /* All resources should be freed automatically */
273 WARN_ON(mtd_device_unregister(&data->mtd));
278 static const struct of_device_id powernv_flash_match[] = {
279 { .compatible = "ibm,opal-flash" },
283 static struct platform_driver powernv_flash_driver = {
285 .name = "powernv_flash",
286 .of_match_table = powernv_flash_match,
288 .remove = powernv_flash_release,
289 .probe = powernv_flash_probe,
292 module_platform_driver(powernv_flash_driver);
294 MODULE_DEVICE_TABLE(of, powernv_flash_match);
295 MODULE_LICENSE("GPL");
297 MODULE_DESCRIPTION("MTD abstraction for OPAL flash");