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