Merge remote-tracking branch 'asoc/fix/core' into asoc-linus
[deliverable/linux.git] / drivers / staging / goldfish / goldfish_nand.c
1 /*
2 * drivers/mtd/devices/goldfish_nand.c
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (C) 2012 Intel, Inc.
6 * Copyright (C) 2013 Intel, Inc.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19 #include <linux/io.h>
20 #include <linux/device.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/ioport.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/platform_device.h>
27 #include <linux/mutex.h>
28 #include <linux/goldfish.h>
29 #include <asm/div64.h>
30
31 #include "goldfish_nand_reg.h"
32
33 struct goldfish_nand {
34 struct mutex lock;
35 unsigned char __iomem *base;
36 struct cmd_params *cmd_params;
37 size_t mtd_count;
38 struct mtd_info mtd[0];
39 };
40
41 static u32 goldfish_nand_cmd_with_params(struct mtd_info *mtd,
42 enum nand_cmd cmd, u64 addr, u32 len,
43 void *ptr, u32 *rv)
44 {
45 u32 cmdp;
46 struct goldfish_nand *nand = mtd->priv;
47 struct cmd_params *cps = nand->cmd_params;
48 unsigned char __iomem *base = nand->base;
49
50 if (cps == NULL)
51 return -1;
52
53 switch (cmd) {
54 case NAND_CMD_ERASE:
55 cmdp = NAND_CMD_ERASE_WITH_PARAMS;
56 break;
57 case NAND_CMD_READ:
58 cmdp = NAND_CMD_READ_WITH_PARAMS;
59 break;
60 case NAND_CMD_WRITE:
61 cmdp = NAND_CMD_WRITE_WITH_PARAMS;
62 break;
63 default:
64 return -1;
65 }
66 cps->dev = mtd - nand->mtd;
67 cps->addr_high = (u32)(addr >> 32);
68 cps->addr_low = (u32)addr;
69 cps->transfer_size = len;
70 cps->data = (unsigned long)ptr;
71 writel(cmdp, base + NAND_COMMAND);
72 *rv = cps->result;
73 return 0;
74 }
75
76 static u32 goldfish_nand_cmd(struct mtd_info *mtd, enum nand_cmd cmd,
77 u64 addr, u32 len, void *ptr)
78 {
79 struct goldfish_nand *nand = mtd->priv;
80 u32 rv;
81 unsigned char __iomem *base = nand->base;
82
83 mutex_lock(&nand->lock);
84 if (goldfish_nand_cmd_with_params(mtd, cmd, addr, len, ptr, &rv)) {
85 writel(mtd - nand->mtd, base + NAND_DEV);
86 writel((u32)(addr >> 32), base + NAND_ADDR_HIGH);
87 writel((u32)addr, base + NAND_ADDR_LOW);
88 writel(len, base + NAND_TRANSFER_SIZE);
89 gf_write64((u64)ptr, base + NAND_DATA, base + NAND_DATA_HIGH);
90 writel(cmd, base + NAND_COMMAND);
91 rv = readl(base + NAND_RESULT);
92 }
93 mutex_unlock(&nand->lock);
94 return rv;
95 }
96
97 static int goldfish_nand_erase(struct mtd_info *mtd, struct erase_info *instr)
98 {
99 loff_t ofs = instr->addr;
100 u32 len = instr->len;
101 u32 rem;
102
103 if (ofs + len > mtd->size)
104 goto invalid_arg;
105 rem = do_div(ofs, mtd->writesize);
106 if (rem)
107 goto invalid_arg;
108 ofs *= (mtd->writesize + mtd->oobsize);
109
110 if (len % mtd->writesize)
111 goto invalid_arg;
112 len = len / mtd->writesize * (mtd->writesize + mtd->oobsize);
113
114 if (goldfish_nand_cmd(mtd, NAND_CMD_ERASE, ofs, len, NULL) != len) {
115 pr_err("goldfish_nand_erase: erase failed, start %llx, len %x, dev_size %llx, erase_size %x\n",
116 ofs, len, mtd->size, mtd->erasesize);
117 return -EIO;
118 }
119
120 instr->state = MTD_ERASE_DONE;
121 mtd_erase_callback(instr);
122
123 return 0;
124
125 invalid_arg:
126 pr_err("goldfish_nand_erase: invalid erase, start %llx, len %x, dev_size %llx, erase_size %x\n",
127 ofs, len, mtd->size, mtd->erasesize);
128 return -EINVAL;
129 }
130
131 static int goldfish_nand_read_oob(struct mtd_info *mtd, loff_t ofs,
132 struct mtd_oob_ops *ops)
133 {
134 u32 rem;
135
136 if (ofs + ops->len > mtd->size)
137 goto invalid_arg;
138 if (ops->datbuf && ops->len && ops->len != mtd->writesize)
139 goto invalid_arg;
140 if (ops->ooblen + ops->ooboffs > mtd->oobsize)
141 goto invalid_arg;
142
143 rem = do_div(ofs, mtd->writesize);
144 if (rem)
145 goto invalid_arg;
146 ofs *= (mtd->writesize + mtd->oobsize);
147
148 if (ops->datbuf)
149 ops->retlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, ofs,
150 ops->len, ops->datbuf);
151 ofs += mtd->writesize + ops->ooboffs;
152 if (ops->oobbuf)
153 ops->oobretlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, ofs,
154 ops->ooblen, ops->oobbuf);
155 return 0;
156
157 invalid_arg:
158 pr_err("goldfish_nand_read_oob: invalid read, start %llx, len %zx, ooblen %zx, dev_size %llx, write_size %x\n",
159 ofs, ops->len, ops->ooblen, mtd->size, mtd->writesize);
160 return -EINVAL;
161 }
162
163 static int goldfish_nand_write_oob(struct mtd_info *mtd, loff_t ofs,
164 struct mtd_oob_ops *ops)
165 {
166 u32 rem;
167
168 if (ofs + ops->len > mtd->size)
169 goto invalid_arg;
170 if (ops->len && ops->len != mtd->writesize)
171 goto invalid_arg;
172 if (ops->ooblen + ops->ooboffs > mtd->oobsize)
173 goto invalid_arg;
174
175 rem = do_div(ofs, mtd->writesize);
176 if (rem)
177 goto invalid_arg;
178 ofs *= (mtd->writesize + mtd->oobsize);
179
180 if (ops->datbuf)
181 ops->retlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, ofs,
182 ops->len, ops->datbuf);
183 ofs += mtd->writesize + ops->ooboffs;
184 if (ops->oobbuf)
185 ops->oobretlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, ofs,
186 ops->ooblen, ops->oobbuf);
187 return 0;
188
189 invalid_arg:
190 pr_err("goldfish_nand_write_oob: invalid write, start %llx, len %zx, ooblen %zx, dev_size %llx, write_size %x\n",
191 ofs, ops->len, ops->ooblen, mtd->size, mtd->writesize);
192 return -EINVAL;
193 }
194
195 static int goldfish_nand_read(struct mtd_info *mtd, loff_t from, size_t len,
196 size_t *retlen, u_char *buf)
197 {
198 u32 rem;
199
200 if (from + len > mtd->size)
201 goto invalid_arg;
202
203 rem = do_div(from, mtd->writesize);
204 if (rem)
205 goto invalid_arg;
206 from *= (mtd->writesize + mtd->oobsize);
207
208 *retlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, from, len, buf);
209 return 0;
210
211 invalid_arg:
212 pr_err("goldfish_nand_read: invalid read, start %llx, len %zx, dev_size %llx, write_size %x\n",
213 from, len, mtd->size, mtd->writesize);
214 return -EINVAL;
215 }
216
217 static int goldfish_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
218 size_t *retlen, const u_char *buf)
219 {
220 u32 rem;
221
222 if (to + len > mtd->size)
223 goto invalid_arg;
224
225 rem = do_div(to, mtd->writesize);
226 if (rem)
227 goto invalid_arg;
228 to *= (mtd->writesize + mtd->oobsize);
229
230 *retlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, to, len, (void *)buf);
231 return 0;
232
233 invalid_arg:
234 pr_err("goldfish_nand_write: invalid write, start %llx, len %zx, dev_size %llx, write_size %x\n",
235 to, len, mtd->size, mtd->writesize);
236 return -EINVAL;
237 }
238
239 static int goldfish_nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
240 {
241 u32 rem;
242
243 if (ofs >= mtd->size)
244 goto invalid_arg;
245
246 rem = do_div(ofs, mtd->erasesize);
247 if (rem)
248 goto invalid_arg;
249 ofs *= mtd->erasesize / mtd->writesize;
250 ofs *= (mtd->writesize + mtd->oobsize);
251
252 return goldfish_nand_cmd(mtd, NAND_CMD_BLOCK_BAD_GET, ofs, 0, NULL);
253
254 invalid_arg:
255 pr_err("goldfish_nand_block_isbad: invalid arg, ofs %llx, dev_size %llx, write_size %x\n",
256 ofs, mtd->size, mtd->writesize);
257 return -EINVAL;
258 }
259
260 static int goldfish_nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
261 {
262 u32 rem;
263
264 if (ofs >= mtd->size)
265 goto invalid_arg;
266
267 rem = do_div(ofs, mtd->erasesize);
268 if (rem)
269 goto invalid_arg;
270 ofs *= mtd->erasesize / mtd->writesize;
271 ofs *= (mtd->writesize + mtd->oobsize);
272
273 if (goldfish_nand_cmd(mtd, NAND_CMD_BLOCK_BAD_SET, ofs, 0, NULL) != 1)
274 return -EIO;
275 return 0;
276
277 invalid_arg:
278 pr_err("goldfish_nand_block_markbad: invalid arg, ofs %llx, dev_size %llx, write_size %x\n",
279 ofs, mtd->size, mtd->writesize);
280 return -EINVAL;
281 }
282
283 static int nand_setup_cmd_params(struct platform_device *pdev,
284 struct goldfish_nand *nand)
285 {
286 u64 paddr;
287 unsigned char __iomem *base = nand->base;
288
289 nand->cmd_params = devm_kzalloc(&pdev->dev,
290 sizeof(struct cmd_params), GFP_KERNEL);
291 if (!nand->cmd_params)
292 return -1;
293
294 paddr = __pa(nand->cmd_params);
295 writel((u32)(paddr >> 32), base + NAND_CMD_PARAMS_ADDR_HIGH);
296 writel((u32)paddr, base + NAND_CMD_PARAMS_ADDR_LOW);
297 return 0;
298 }
299
300 static int goldfish_nand_init_device(struct platform_device *pdev,
301 struct goldfish_nand *nand, int id)
302 {
303 u32 name_len;
304 u32 result;
305 u32 flags;
306 unsigned char __iomem *base = nand->base;
307 struct mtd_info *mtd = &nand->mtd[id];
308 char *name;
309
310 mutex_lock(&nand->lock);
311 writel(id, base + NAND_DEV);
312 flags = readl(base + NAND_DEV_FLAGS);
313 name_len = readl(base + NAND_DEV_NAME_LEN);
314 mtd->writesize = readl(base + NAND_DEV_PAGE_SIZE);
315 mtd->size = readl(base + NAND_DEV_SIZE_LOW);
316 mtd->size |= (u64)readl(base + NAND_DEV_SIZE_HIGH) << 32;
317 mtd->oobsize = readl(base + NAND_DEV_EXTRA_SIZE);
318 mtd->oobavail = mtd->oobsize;
319 mtd->erasesize = readl(base + NAND_DEV_ERASE_SIZE) /
320 (mtd->writesize + mtd->oobsize) * mtd->writesize;
321 do_div(mtd->size, mtd->writesize + mtd->oobsize);
322 mtd->size *= mtd->writesize;
323 dev_dbg(&pdev->dev,
324 "goldfish nand dev%d: size %llx, page %d, extra %d, erase %d\n",
325 id, mtd->size, mtd->writesize,
326 mtd->oobsize, mtd->erasesize);
327 mutex_unlock(&nand->lock);
328
329 mtd->priv = nand;
330
331 mtd->name = name = devm_kzalloc(&pdev->dev, name_len + 1, GFP_KERNEL);
332 if (name == NULL)
333 return -ENOMEM;
334
335 result = goldfish_nand_cmd(mtd, NAND_CMD_GET_DEV_NAME, 0, name_len,
336 name);
337 if (result != name_len) {
338 dev_err(&pdev->dev,
339 "goldfish_nand_init_device failed to get dev name %d != %d\n",
340 result, name_len);
341 return -ENODEV;
342 }
343 ((char *) mtd->name)[name_len] = '\0';
344
345 /* Setup the MTD structure */
346 mtd->type = MTD_NANDFLASH;
347 mtd->flags = MTD_CAP_NANDFLASH;
348 if (flags & NAND_DEV_FLAG_READ_ONLY)
349 mtd->flags &= ~MTD_WRITEABLE;
350 if (flags & NAND_DEV_FLAG_CMD_PARAMS_CAP)
351 nand_setup_cmd_params(pdev, nand);
352
353 mtd->owner = THIS_MODULE;
354 mtd->_erase = goldfish_nand_erase;
355 mtd->_read = goldfish_nand_read;
356 mtd->_write = goldfish_nand_write;
357 mtd->_read_oob = goldfish_nand_read_oob;
358 mtd->_write_oob = goldfish_nand_write_oob;
359 mtd->_block_isbad = goldfish_nand_block_isbad;
360 mtd->_block_markbad = goldfish_nand_block_markbad;
361
362 if (mtd_device_register(mtd, NULL, 0))
363 return -EIO;
364
365 return 0;
366 }
367
368 static int goldfish_nand_probe(struct platform_device *pdev)
369 {
370 u32 num_dev;
371 int i;
372 int err;
373 u32 num_dev_working;
374 u32 version;
375 struct resource *r;
376 struct goldfish_nand *nand;
377 unsigned char __iomem *base;
378
379 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
380 if (r == NULL)
381 return -ENODEV;
382
383 base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
384 if (base == NULL)
385 return -ENOMEM;
386
387 version = readl(base + NAND_VERSION);
388 if (version != NAND_VERSION_CURRENT) {
389 dev_err(&pdev->dev,
390 "goldfish_nand_init: version mismatch, got %d, expected %d\n",
391 version, NAND_VERSION_CURRENT);
392 return -ENODEV;
393 }
394 num_dev = readl(base + NAND_NUM_DEV);
395 if (num_dev == 0)
396 return -ENODEV;
397
398 nand = devm_kzalloc(&pdev->dev, sizeof(*nand) +
399 sizeof(struct mtd_info) * num_dev, GFP_KERNEL);
400 if (nand == NULL)
401 return -ENOMEM;
402
403 mutex_init(&nand->lock);
404 nand->base = base;
405 nand->mtd_count = num_dev;
406 platform_set_drvdata(pdev, nand);
407
408 num_dev_working = 0;
409 for (i = 0; i < num_dev; i++) {
410 err = goldfish_nand_init_device(pdev, nand, i);
411 if (err == 0)
412 num_dev_working++;
413 }
414 if (num_dev_working == 0)
415 return -ENODEV;
416 return 0;
417 }
418
419 static int goldfish_nand_remove(struct platform_device *pdev)
420 {
421 struct goldfish_nand *nand = platform_get_drvdata(pdev);
422 int i;
423
424 for (i = 0; i < nand->mtd_count; i++) {
425 if (nand->mtd[i].name)
426 mtd_device_unregister(&nand->mtd[i]);
427 }
428 return 0;
429 }
430
431 static struct platform_driver goldfish_nand_driver = {
432 .probe = goldfish_nand_probe,
433 .remove = goldfish_nand_remove,
434 .driver = {
435 .name = "goldfish_nand"
436 }
437 };
438
439 module_platform_driver(goldfish_nand_driver);
440 MODULE_LICENSE("GPL");
This page took 0.039944 seconds and 5 git commands to generate.