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