mtd: powernv_flash: Don't return -ERESTARTSYS on interrupted token acquisition
[linux-2.6-block.git] / drivers / mtd / devices / powernv_flash.c
CommitLineData
1cbb4a1c
CB
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
39struct powernv_flash {
40 struct mtd_info mtd;
41 u32 id;
42};
43
44enum flash_op {
45 FLASH_OP_READ,
46 FLASH_OP_WRITE,
47 FLASH_OP_ERASE,
48};
49
efe69414
CB
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 */
1cbb4a1c
CB
55static 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");
efe69414
CB
71 else
72 token = -EINTR;
1cbb4a1c
CB
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:
44e2aa2b
CB
87 WARN_ON_ONCE(1);
88 opal_async_release_token(token);
89 return -EIO;
1cbb4a1c
CB
90 }
91
25ee52e6
CB
92 if (rc == OPAL_SUCCESS)
93 goto out_success;
94
1cbb4a1c
CB
95 if (rc != OPAL_ASYNC_COMPLETION) {
96 dev_err(dev, "opal_flash_async_op(op=%d) failed (rc %d)\n",
97 op, rc);
25ee52e6
CB
98 rc = -EIO;
99 goto out;
1cbb4a1c
CB
100 }
101
102 rc = opal_async_wait_response(token, &msg);
1cbb4a1c
CB
103 if (rc) {
104 dev_err(dev, "opal async wait failed (rc %d)\n", rc);
25ee52e6
CB
105 rc = -EIO;
106 goto out;
1cbb4a1c
CB
107 }
108
d0226d31 109 rc = opal_get_async_rc(msg);
25ee52e6 110out_success:
1cbb4a1c
CB
111 if (rc == OPAL_SUCCESS) {
112 rc = 0;
113 if (retlen)
114 *retlen = len;
115 } else {
116 rc = -EIO;
117 }
118
25ee52e6
CB
119out:
120 opal_async_release_token(token);
1cbb4a1c
CB
121 return rc;
122}
123
124/**
125 * @mtd: the device
126 * @from: the offset to read from
127 * @len: the number of bytes to read
128 * @retlen: the number of bytes actually read
129 * @buf: the filled in buffer
130 *
131 * Returns 0 if read successful, or -ERRNO if an error occurred
132 */
133static int powernv_flash_read(struct mtd_info *mtd, loff_t from, size_t len,
134 size_t *retlen, u_char *buf)
135{
136 return powernv_flash_async_op(mtd, FLASH_OP_READ, from,
137 len, retlen, buf);
138}
139
140/**
141 * @mtd: the device
142 * @to: the offset to write to
143 * @len: the number of bytes to write
144 * @retlen: the number of bytes actually written
145 * @buf: the buffer to get bytes from
146 *
147 * Returns 0 if write successful, -ERRNO if error occurred
148 */
149static int powernv_flash_write(struct mtd_info *mtd, loff_t to, size_t len,
150 size_t *retlen, const u_char *buf)
151{
152 return powernv_flash_async_op(mtd, FLASH_OP_WRITE, to,
153 len, retlen, (u_char *)buf);
154}
155
156/**
157 * @mtd: the device
158 * @erase: the erase info
159 * Returns 0 if erase successful or -ERRNO if an error occurred
160 */
161static int powernv_flash_erase(struct mtd_info *mtd, struct erase_info *erase)
162{
163 int rc;
164
165 erase->state = MTD_ERASING;
166
167 /* todo: register our own notifier to do a true async implementation */
168 rc = powernv_flash_async_op(mtd, FLASH_OP_ERASE, erase->addr,
169 erase->len, NULL, NULL);
170
171 if (rc) {
172 erase->fail_addr = erase->addr;
173 erase->state = MTD_ERASE_FAILED;
174 } else {
175 erase->state = MTD_ERASE_DONE;
176 }
177 mtd_erase_callback(erase);
178 return rc;
179}
180
181/**
182 * powernv_flash_set_driver_info - Fill the mtd_info structure and docg3
183 * structure @pdev: The platform device
184 * @mtd: The structure to fill
185 */
186static int powernv_flash_set_driver_info(struct device *dev,
187 struct mtd_info *mtd)
188{
189 u64 size;
190 u32 erase_size;
191 int rc;
192
193 rc = of_property_read_u32(dev->of_node, "ibm,flash-block-size",
194 &erase_size);
195 if (rc) {
196 dev_err(dev, "couldn't get resource block size information\n");
197 return rc;
198 }
199
200 rc = of_property_read_u64(dev->of_node, "reg", &size);
201 if (rc) {
202 dev_err(dev, "couldn't get resource size information\n");
203 return rc;
204 }
205
206 /*
207 * Going to have to check what details I need to set and how to
208 * get them
209 */
210 mtd->name = of_get_property(dev->of_node, "name", NULL);
211 mtd->type = MTD_NORFLASH;
212 mtd->flags = MTD_WRITEABLE;
213 mtd->size = size;
214 mtd->erasesize = erase_size;
215 mtd->writebufsize = mtd->writesize = 1;
216 mtd->owner = THIS_MODULE;
217 mtd->_erase = powernv_flash_erase;
218 mtd->_read = powernv_flash_read;
219 mtd->_write = powernv_flash_write;
220 mtd->dev.parent = dev;
221 return 0;
222}
223
224/**
225 * powernv_flash_probe
226 * @pdev: platform device
227 *
228 * Returns 0 on success, -ENOMEM, -ENXIO on error
229 */
230static int powernv_flash_probe(struct platform_device *pdev)
231{
232 struct device *dev = &pdev->dev;
233 struct powernv_flash *data;
234 int ret;
235
236 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
e32ec15a
CB
237 if (!data)
238 return -ENOMEM;
239
1cbb4a1c
CB
240 data->mtd.priv = data;
241
242 ret = of_property_read_u32(dev->of_node, "ibm,opal-id", &(data->id));
243 if (ret) {
244 dev_err(dev, "no device property 'ibm,opal-id'\n");
e32ec15a 245 return ret;
1cbb4a1c
CB
246 }
247
248 ret = powernv_flash_set_driver_info(dev, &data->mtd);
249 if (ret)
e32ec15a 250 return ret;
1cbb4a1c
CB
251
252 dev_set_drvdata(dev, data);
253
254 /*
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
258 */
e32ec15a 259 return mtd_device_register(&data->mtd, NULL, 0);
1cbb4a1c
CB
260}
261
262/**
263 * op_release - Release the driver
264 * @pdev: the platform device
265 *
266 * Returns 0
267 */
268static int powernv_flash_release(struct platform_device *pdev)
269{
270 struct powernv_flash *data = dev_get_drvdata(&(pdev->dev));
271
272 /* All resources should be freed automatically */
273 return mtd_device_unregister(&(data->mtd));
274}
275
276static const struct of_device_id powernv_flash_match[] = {
277 { .compatible = "ibm,opal-flash" },
278 {}
279};
280
281static struct platform_driver powernv_flash_driver = {
282 .driver = {
283 .name = "powernv_flash",
284 .of_match_table = powernv_flash_match,
285 },
286 .remove = powernv_flash_release,
287 .probe = powernv_flash_probe,
288};
289
290module_platform_driver(powernv_flash_driver);
291
292MODULE_DEVICE_TABLE(of, powernv_flash_match);
293MODULE_LICENSE("GPL");
294MODULE_AUTHOR("Cyril Bur <cyril.bur@au1.ibm.com>");
295MODULE_DESCRIPTION("MTD abstraction for OPAL flash");