1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2016-2017 Micron Technology, Inc.
4 *
5 * Authors:
6 * Peter Pan <peterpandong@micron.com>
7 * Boris Brezillon <boris.brezillon@bootlin.com>
8 */
9
10 #define pr_fmt(fmt) "spi-nand: " fmt
11
12 #include <linux/device.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mtd/spinand.h>
17 #include <linux/of.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi-mem.h>
22
23 static struct spi_mem_op
spinand_fill_reset_op(struct spinand_device * spinand)24 spinand_fill_reset_op(struct spinand_device *spinand)
25 {
26 return spinand->op_templates->reset;
27 }
28
29 static struct spi_mem_op
spinand_fill_readid_op(struct spinand_device * spinand,u8 naddr,u8 ndummy,void * buf,unsigned int len)30 spinand_fill_readid_op(struct spinand_device *spinand,
31 u8 naddr, u8 ndummy, void *buf, unsigned int len)
32 {
33 struct spi_mem_op op = spinand->op_templates->readid;
34
35 op.addr.nbytes = naddr;
36 op.dummy.nbytes = ndummy;
37 op.data.buf.in = buf;
38 op.data.nbytes = len;
39
40 return op;
41 }
42
43 struct spi_mem_op
spinand_fill_wr_en_op(struct spinand_device * spinand)44 spinand_fill_wr_en_op(struct spinand_device *spinand)
45 {
46 return spinand->op_templates->wr_en;
47 }
48
49 static __maybe_unused struct spi_mem_op
spinand_fill_wr_dis_op(struct spinand_device * spinand)50 spinand_fill_wr_dis_op(struct spinand_device *spinand)
51 {
52 return spinand->op_templates->wr_dis;
53 }
54
55 struct spi_mem_op
spinand_fill_set_feature_op(struct spinand_device * spinand,u64 reg,const void * valptr)56 spinand_fill_set_feature_op(struct spinand_device *spinand, u64 reg, const void *valptr)
57 {
58 struct spi_mem_op op = spinand->op_templates->set_feature;
59
60 if (op.cmd.dtr && op.cmd.buswidth == 8)
61 reg |= reg << 8;
62
63 op.addr.val = reg;
64 op.data.buf.out = valptr;
65
66 return op;
67 }
68
69 struct spi_mem_op
spinand_fill_get_feature_op(struct spinand_device * spinand,u64 reg,void * valptr)70 spinand_fill_get_feature_op(struct spinand_device *spinand, u64 reg, void *valptr)
71 {
72 struct spi_mem_op op = spinand->op_templates->get_feature;
73
74 if (op.cmd.dtr && op.cmd.buswidth == 8)
75 reg |= reg << 8;
76
77 op.addr.val = reg;
78 op.data.buf.in = valptr;
79
80 return op;
81 }
82
83 static struct spi_mem_op
spinand_fill_blk_erase_op(struct spinand_device * spinand,u64 addr)84 spinand_fill_blk_erase_op(struct spinand_device *spinand, u64 addr)
85 {
86 struct spi_mem_op op = spinand->op_templates->blk_erase;
87
88 op.addr.val = addr;
89
90 return op;
91 }
92
93 static struct spi_mem_op
spinand_fill_page_read_op(struct spinand_device * spinand,u64 addr)94 spinand_fill_page_read_op(struct spinand_device *spinand, u64 addr)
95 {
96 struct spi_mem_op op = spinand->op_templates->page_read;
97
98 op.addr.val = addr;
99
100 return op;
101 }
102
103 static struct spi_mem_op
spinand_fill_page_read_packed_op(struct spinand_device * spinand,u64 addr)104 spinand_fill_page_read_packed_op(struct spinand_device *spinand, u64 addr)
105 {
106 struct spi_mem_op op = spinand->op_templates->page_read;
107
108 op.cmd.opcode |= addr >> 16;
109 op.addr.val = addr & 0xFFFF;
110
111 return op;
112 }
113
114 struct spi_mem_op
spinand_fill_prog_exec_op(struct spinand_device * spinand,u64 addr)115 spinand_fill_prog_exec_op(struct spinand_device *spinand, u64 addr)
116 {
117 struct spi_mem_op op = spinand->op_templates->prog_exec;
118
119 op.addr.val = addr;
120
121 return op;
122 }
123
spinand_read_reg_op(struct spinand_device * spinand,u8 reg,u8 * val)124 int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
125 {
126 struct spi_mem_op op = SPINAND_OP(spinand, get_feature,
127 reg, spinand->scratchbuf);
128 int ret;
129
130 ret = spi_mem_exec_op(spinand->spimem, &op);
131 if (ret)
132 return ret;
133
134 *val = *spinand->scratchbuf;
135 return 0;
136 }
137
spinand_write_reg_op(struct spinand_device * spinand,u8 reg,u8 val)138 int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
139 {
140 struct spi_mem_op op = SPINAND_OP(spinand, set_feature,
141 reg, spinand->scratchbuf);
142
143 *spinand->scratchbuf = val;
144 return spi_mem_exec_op(spinand->spimem, &op);
145 }
146
spinand_read_status(struct spinand_device * spinand,u8 * status)147 static int spinand_read_status(struct spinand_device *spinand, u8 *status)
148 {
149 return spinand_read_reg_op(spinand, REG_STATUS, status);
150 }
151
spinand_get_cfg(struct spinand_device * spinand,u8 * cfg)152 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
153 {
154 struct nand_device *nand = spinand_to_nand(spinand);
155
156 if (WARN_ON(spinand->cur_target < 0 ||
157 spinand->cur_target >= nand->memorg.ntargets))
158 return -EINVAL;
159
160 *cfg = spinand->cfg_cache[spinand->cur_target];
161 return 0;
162 }
163
spinand_set_cfg(struct spinand_device * spinand,u8 cfg)164 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
165 {
166 struct nand_device *nand = spinand_to_nand(spinand);
167 int ret;
168
169 if (WARN_ON(spinand->cur_target < 0 ||
170 spinand->cur_target >= nand->memorg.ntargets))
171 return -EINVAL;
172
173 if (spinand->cfg_cache[spinand->cur_target] == cfg)
174 return 0;
175
176 ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
177 if (ret)
178 return ret;
179
180 spinand->cfg_cache[spinand->cur_target] = cfg;
181 return 0;
182 }
183
184 /**
185 * spinand_upd_cfg() - Update the configuration register
186 * @spinand: the spinand device
187 * @mask: the mask encoding the bits to update in the config reg
188 * @val: the new value to apply
189 *
190 * Update the configuration register.
191 *
192 * Return: 0 on success, a negative error code otherwise.
193 */
spinand_upd_cfg(struct spinand_device * spinand,u8 mask,u8 val)194 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
195 {
196 int ret;
197 u8 cfg;
198
199 ret = spinand_get_cfg(spinand, &cfg);
200 if (ret)
201 return ret;
202
203 cfg &= ~mask;
204 cfg |= val;
205
206 return spinand_set_cfg(spinand, cfg);
207 }
208
209 /**
210 * spinand_select_target() - Select a specific NAND target/die
211 * @spinand: the spinand device
212 * @target: the target/die to select
213 *
214 * Select a new target/die. If chip only has one die, this function is a NOOP.
215 *
216 * Return: 0 on success, a negative error code otherwise.
217 */
spinand_select_target(struct spinand_device * spinand,unsigned int target)218 int spinand_select_target(struct spinand_device *spinand, unsigned int target)
219 {
220 struct nand_device *nand = spinand_to_nand(spinand);
221 int ret;
222
223 if (WARN_ON(target >= nand->memorg.ntargets))
224 return -EINVAL;
225
226 if (spinand->cur_target == target)
227 return 0;
228
229 if (nand->memorg.ntargets == 1) {
230 spinand->cur_target = target;
231 return 0;
232 }
233
234 ret = spinand->select_target(spinand, target);
235 if (ret)
236 return ret;
237
238 spinand->cur_target = target;
239 return 0;
240 }
241
spinand_read_cfg(struct spinand_device * spinand)242 static int spinand_read_cfg(struct spinand_device *spinand)
243 {
244 struct nand_device *nand = spinand_to_nand(spinand);
245 unsigned int target;
246 int ret;
247
248 for (target = 0; target < nand->memorg.ntargets; target++) {
249 ret = spinand_select_target(spinand, target);
250 if (ret)
251 return ret;
252
253 /*
254 * We use spinand_read_reg_op() instead of spinand_get_cfg()
255 * here to bypass the config cache.
256 */
257 ret = spinand_read_reg_op(spinand, REG_CFG,
258 &spinand->cfg_cache[target]);
259 if (ret)
260 return ret;
261 }
262
263 return 0;
264 }
265
spinand_init_cfg_cache(struct spinand_device * spinand)266 static int spinand_init_cfg_cache(struct spinand_device *spinand)
267 {
268 struct nand_device *nand = spinand_to_nand(spinand);
269 struct device *dev = &spinand->spimem->spi->dev;
270
271 spinand->cfg_cache = devm_kcalloc(dev,
272 nand->memorg.ntargets,
273 sizeof(*spinand->cfg_cache),
274 GFP_KERNEL);
275 if (!spinand->cfg_cache)
276 return -ENOMEM;
277
278 return 0;
279 }
280
spinand_init_quad_enable(struct spinand_device * spinand,bool enable)281 static int spinand_init_quad_enable(struct spinand_device *spinand,
282 bool enable)
283 {
284 struct nand_device *nand = spinand_to_nand(spinand);
285 unsigned int target;
286 int ret;
287
288 /*
289 * QE is a per-die setting on some devices. Program each target
290 * individually when enabling or disabling quad I/O mode.
291 */
292 for (target = 0; target < nand->memorg.ntargets; target++) {
293 ret = spinand_select_target(spinand, target);
294 if (ret)
295 return ret;
296
297 ret = spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
298 enable ? CFG_QUAD_ENABLE : 0);
299 if (ret)
300 return ret;
301 }
302
303 return 0;
304 }
305
spinand_ecc_enable(struct spinand_device * spinand,bool enable)306 static int spinand_ecc_enable(struct spinand_device *spinand,
307 bool enable)
308 {
309 return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
310 enable ? CFG_ECC_ENABLE : 0);
311 }
312
spinand_cont_read_enable(struct spinand_device * spinand,bool enable)313 static int spinand_cont_read_enable(struct spinand_device *spinand,
314 bool enable)
315 {
316 return spinand->set_cont_read(spinand, enable);
317 }
318
spinand_check_ecc_status(struct spinand_device * spinand,u8 status)319 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
320 {
321 struct nand_device *nand = spinand_to_nand(spinand);
322
323 if (spinand->eccinfo.get_status)
324 return spinand->eccinfo.get_status(spinand, status);
325
326 switch (status & STATUS_ECC_MASK) {
327 case STATUS_ECC_NO_BITFLIPS:
328 return 0;
329
330 case STATUS_ECC_HAS_BITFLIPS:
331 /*
332 * We have no way to know exactly how many bitflips have been
333 * fixed, so let's return the maximum possible value so that
334 * wear-leveling layers move the data immediately.
335 */
336 return nanddev_get_ecc_conf(nand)->strength;
337
338 case STATUS_ECC_UNCOR_ERROR:
339 return -EBADMSG;
340
341 default:
342 break;
343 }
344
345 return -EINVAL;
346 }
347
spinand_noecc_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)348 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
349 struct mtd_oob_region *region)
350 {
351 return -ERANGE;
352 }
353
spinand_noecc_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)354 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
355 struct mtd_oob_region *region)
356 {
357 if (section)
358 return -ERANGE;
359
360 /* Reserve 2 bytes for the BBM. */
361 region->offset = 2;
362 region->length = 62;
363
364 return 0;
365 }
366
367 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
368 .ecc = spinand_noecc_ooblayout_ecc,
369 .free = spinand_noecc_ooblayout_free,
370 };
371
spinand_ondie_ecc_init_ctx(struct nand_device * nand)372 static int spinand_ondie_ecc_init_ctx(struct nand_device *nand)
373 {
374 struct spinand_device *spinand = nand_to_spinand(nand);
375 struct mtd_info *mtd = nanddev_to_mtd(nand);
376 struct spinand_ondie_ecc_conf *engine_conf;
377
378 nand->ecc.ctx.conf.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
379 nand->ecc.ctx.conf.step_size = nand->ecc.requirements.step_size;
380 nand->ecc.ctx.conf.strength = nand->ecc.requirements.strength;
381
382 engine_conf = kzalloc_obj(*engine_conf);
383 if (!engine_conf)
384 return -ENOMEM;
385
386 nand->ecc.ctx.priv = engine_conf;
387
388 if (spinand->eccinfo.ooblayout)
389 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
390 else
391 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
392
393 return 0;
394 }
395
spinand_ondie_ecc_cleanup_ctx(struct nand_device * nand)396 static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand)
397 {
398 kfree(nand->ecc.ctx.priv);
399 }
400
spinand_ondie_ecc_prepare_io_req(struct nand_device * nand,struct nand_page_io_req * req)401 static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand,
402 struct nand_page_io_req *req)
403 {
404 struct spinand_device *spinand = nand_to_spinand(nand);
405 bool enable = (req->mode != MTD_OPS_RAW);
406
407 if (!enable && spinand->flags & SPINAND_NO_RAW_ACCESS)
408 return -EOPNOTSUPP;
409
410 memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand));
411
412 /* Only enable or disable the engine */
413 return spinand_ecc_enable(spinand, enable);
414 }
415
spinand_ondie_ecc_finish_io_req(struct nand_device * nand,struct nand_page_io_req * req)416 static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand,
417 struct nand_page_io_req *req)
418 {
419 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
420 struct spinand_device *spinand = nand_to_spinand(nand);
421 struct mtd_info *mtd = spinand_to_mtd(spinand);
422 int ret;
423
424 if (req->mode == MTD_OPS_RAW)
425 return 0;
426
427 /* Nothing to do when finishing a page write */
428 if (req->type == NAND_PAGE_WRITE)
429 return 0;
430
431 /* Finish a page read: check the status, report errors/bitflips */
432 ret = spinand_check_ecc_status(spinand, engine_conf->status);
433 if (ret == -EBADMSG) {
434 mtd->ecc_stats.failed++;
435 } else if (ret > 0) {
436 unsigned int pages;
437
438 /*
439 * Continuous reads don't allow us to get the detail,
440 * so we may exagerate the actual number of corrected bitflips.
441 */
442 if (!req->continuous)
443 pages = 1;
444 else
445 pages = req->datalen / nanddev_page_size(nand);
446
447 mtd->ecc_stats.corrected += ret * pages;
448 }
449
450 return ret;
451 }
452
453 static const struct nand_ecc_engine_ops spinand_ondie_ecc_engine_ops = {
454 .init_ctx = spinand_ondie_ecc_init_ctx,
455 .cleanup_ctx = spinand_ondie_ecc_cleanup_ctx,
456 .prepare_io_req = spinand_ondie_ecc_prepare_io_req,
457 .finish_io_req = spinand_ondie_ecc_finish_io_req,
458 };
459
460 static struct nand_ecc_engine spinand_ondie_ecc_engine = {
461 .ops = &spinand_ondie_ecc_engine_ops,
462 };
463
spinand_ondie_ecc_save_status(struct nand_device * nand,u8 status)464 static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status)
465 {
466 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
467
468 if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE &&
469 engine_conf)
470 engine_conf->status = status;
471 }
472
spinand_write_enable_op(struct spinand_device * spinand)473 int spinand_write_enable_op(struct spinand_device *spinand)
474 {
475 struct spi_mem_op op = SPINAND_OP(spinand, wr_en);
476
477 return spi_mem_exec_op(spinand->spimem, &op);
478 }
479
spinand_load_page_op(struct spinand_device * spinand,const struct nand_page_io_req * req)480 static int spinand_load_page_op(struct spinand_device *spinand,
481 const struct nand_page_io_req *req)
482 {
483 struct nand_device *nand = spinand_to_nand(spinand);
484 unsigned int row = nanddev_pos_to_row(nand, &req->pos);
485 bool packed = spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ;
486 struct spi_mem_op op = packed ?
487 SPINAND_OP(spinand, page_read_packed, row) :
488 SPINAND_OP(spinand, page_read, row);
489
490 return spi_mem_exec_op(spinand->spimem, &op);
491 }
492
spinand_read_from_cache_op(struct spinand_device * spinand,const struct nand_page_io_req * req)493 static int spinand_read_from_cache_op(struct spinand_device *spinand,
494 const struct nand_page_io_req *req)
495 {
496 struct nand_device *nand = spinand_to_nand(spinand);
497 struct mtd_info *mtd = spinand_to_mtd(spinand);
498 struct spi_mem_dirmap_desc *rdesc;
499 unsigned int nbytes = 0;
500 void *buf = NULL;
501 u16 column = 0;
502 ssize_t ret;
503
504 if (req->datalen) {
505 buf = spinand->databuf;
506 if (!req->continuous)
507 nbytes = nanddev_page_size(nand);
508 else
509 nbytes = round_up(req->dataoffs + req->datalen,
510 nanddev_page_size(nand));
511 column = 0;
512 }
513
514 if (req->ooblen) {
515 nbytes += nanddev_per_page_oobsize(nand);
516 if (!buf) {
517 buf = spinand->oobbuf;
518 column = nanddev_page_size(nand);
519 }
520 }
521
522 rdesc = spinand->dirmaps[req->pos.plane].rdesc;
523
524 if (spinand->op_templates->cont_read_cache && req->continuous)
525 rdesc->info.op_tmpl = &rdesc->info.secondary_op_tmpl;
526 else
527 rdesc->info.op_tmpl = &rdesc->info.primary_op_tmpl;
528
529 if (nand_ecc_is_pipelined(nand) && req->mode != MTD_OPS_RAW)
530 rdesc->info.op_tmpl->data.ecc = true;
531 else
532 rdesc->info.op_tmpl->data.ecc = false;
533
534 if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT)
535 column |= req->pos.plane << fls(nanddev_page_size(nand));
536
537 while (nbytes) {
538 ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf);
539 if (ret < 0)
540 return ret;
541
542 if (!ret || ret > nbytes)
543 return -EIO;
544
545 nbytes -= ret;
546 column += ret;
547 buf += ret;
548
549 /*
550 * Dirmap accesses are allowed to toggle the CS.
551 * Toggling the CS during a continuous read is forbidden.
552 */
553 if (nbytes && req->continuous) {
554 /*
555 * Spi controller with broken support of continuous
556 * reading was detected. Disable future use of
557 * continuous reading and return -EAGAIN to retry
558 * reading within regular mode.
559 */
560 spinand->cont_read_possible = false;
561 return -EAGAIN;
562 }
563 }
564
565 if (req->datalen)
566 memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
567 req->datalen);
568
569 if (req->ooblen) {
570 if (req->mode == MTD_OPS_AUTO_OOB)
571 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
572 spinand->oobbuf,
573 req->ooboffs,
574 req->ooblen);
575 else
576 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
577 req->ooblen);
578 }
579
580 return 0;
581 }
582
spinand_write_to_cache_op(struct spinand_device * spinand,const struct nand_page_io_req * req)583 static int spinand_write_to_cache_op(struct spinand_device *spinand,
584 const struct nand_page_io_req *req)
585 {
586 struct nand_device *nand = spinand_to_nand(spinand);
587 struct mtd_info *mtd = spinand_to_mtd(spinand);
588 struct spi_mem_dirmap_desc *wdesc;
589 unsigned int nbytes, column = 0;
590 void *buf = spinand->databuf;
591 ssize_t ret;
592
593 /*
594 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
595 * the cache content to 0xFF (depends on vendor implementation), so we
596 * must fill the page cache entirely even if we only want to program
597 * the data portion of the page, otherwise we might corrupt the BBM or
598 * user data previously programmed in OOB area.
599 *
600 * Only reset the data buffer manually, the OOB buffer is prepared by
601 * ECC engines ->prepare_io_req() callback.
602 */
603 nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
604 memset(spinand->databuf, 0xff, nanddev_page_size(nand));
605
606 if (req->datalen)
607 memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
608 req->datalen);
609
610 if (req->ooblen) {
611 if (req->mode == MTD_OPS_AUTO_OOB)
612 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
613 spinand->oobbuf,
614 req->ooboffs,
615 req->ooblen);
616 else
617 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
618 req->ooblen);
619 }
620
621 wdesc = spinand->dirmaps[req->pos.plane].wdesc;
622
623 if (nand_ecc_is_pipelined(nand) && req->mode != MTD_OPS_RAW)
624 wdesc->info.op_tmpl->data.ecc = true;
625 else
626 wdesc->info.op_tmpl->data.ecc = false;
627
628 if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT)
629 column |= req->pos.plane << fls(nanddev_page_size(nand));
630
631 while (nbytes) {
632 ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf);
633 if (ret < 0)
634 return ret;
635
636 if (!ret || ret > nbytes)
637 return -EIO;
638
639 nbytes -= ret;
640 column += ret;
641 buf += ret;
642 }
643
644 return 0;
645 }
646
spinand_program_op(struct spinand_device * spinand,const struct nand_page_io_req * req)647 static int spinand_program_op(struct spinand_device *spinand,
648 const struct nand_page_io_req *req)
649 {
650 struct nand_device *nand = spinand_to_nand(spinand);
651 unsigned int row = nanddev_pos_to_row(nand, &req->pos);
652 struct spi_mem_op op = SPINAND_OP(spinand, prog_exec, row);
653
654 return spi_mem_exec_op(spinand->spimem, &op);
655 }
656
spinand_erase_op(struct spinand_device * spinand,const struct nand_pos * pos)657 static int spinand_erase_op(struct spinand_device *spinand,
658 const struct nand_pos *pos)
659 {
660 struct nand_device *nand = spinand_to_nand(spinand);
661 unsigned int row = nanddev_pos_to_row(nand, pos);
662 struct spi_mem_op op = SPINAND_OP(spinand, blk_erase, row);
663
664 return spi_mem_exec_op(spinand->spimem, &op);
665 }
666
667 /**
668 * spinand_wait() - Poll memory device status
669 * @spinand: the spinand device
670 * @initial_delay_us: delay in us before starting to poll
671 * @poll_delay_us: time to sleep between reads in us
672 * @s: the pointer to variable to store the value of REG_STATUS
673 *
674 * This function polls a status register (REG_STATUS) and returns when
675 * the STATUS_READY bit is 0 or when the timeout has expired.
676 *
677 * Return: 0 on success, a negative error code otherwise.
678 */
spinand_wait(struct spinand_device * spinand,unsigned long initial_delay_us,unsigned long poll_delay_us,u8 * s)679 int spinand_wait(struct spinand_device *spinand, unsigned long initial_delay_us,
680 unsigned long poll_delay_us, u8 *s)
681 {
682 struct spi_mem_op op = SPINAND_OP(spinand, get_feature,
683 REG_STATUS, spinand->scratchbuf);
684 u8 status;
685 int ret;
686
687 ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0,
688 initial_delay_us,
689 poll_delay_us,
690 SPINAND_WAITRDY_TIMEOUT_MS);
691 if (ret)
692 return ret;
693
694 status = *spinand->scratchbuf;
695 if (!(status & STATUS_BUSY))
696 goto out;
697
698 /*
699 * Extra read, just in case the STATUS_READY bit has changed
700 * since our last check
701 */
702 ret = spinand_read_status(spinand, &status);
703 if (ret)
704 return ret;
705
706 out:
707 if (s)
708 *s = status;
709
710 return status & STATUS_BUSY ? -ETIMEDOUT : 0;
711 }
712
spinand_read_id_op(struct spinand_device * spinand,u8 naddr,u8 ndummy,u8 * buf)713 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr,
714 u8 ndummy, u8 *buf)
715 {
716 struct spi_mem_op op = SPINAND_OP(spinand, readid,
717 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
718 int ret;
719
720 ret = spi_mem_exec_op(spinand->spimem, &op);
721 if (!ret)
722 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
723
724 return ret;
725 }
726
spinand_reset_op(struct spinand_device * spinand)727 static int spinand_reset_op(struct spinand_device *spinand)
728 {
729 struct spi_mem_op op = SPINAND_OP(spinand, reset);
730 int ret;
731
732 ret = spi_mem_exec_op(spinand->spimem, &op);
733 if (ret)
734 return ret;
735
736 return spinand_wait(spinand,
737 SPINAND_RESET_INITIAL_DELAY_US,
738 SPINAND_RESET_POLL_DELAY_US,
739 NULL);
740 }
741
spinand_lock_block(struct spinand_device * spinand,u8 lock)742 static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
743 {
744 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
745 }
746
747 /**
748 * spinand_read_page() - Read a page
749 * @spinand: the spinand device
750 * @req: the I/O request
751 *
752 * Return: 0 or a positive number of bitflips corrected on success.
753 * A negative error code otherwise.
754 */
spinand_read_page(struct spinand_device * spinand,const struct nand_page_io_req * req)755 int spinand_read_page(struct spinand_device *spinand,
756 const struct nand_page_io_req *req)
757 {
758 struct nand_device *nand = spinand_to_nand(spinand);
759 u8 status;
760 int ret;
761
762 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
763 if (ret)
764 return ret;
765
766 ret = spinand_load_page_op(spinand, req);
767 if (ret)
768 return ret;
769
770 ret = spinand_wait(spinand,
771 SPINAND_READ_INITIAL_DELAY_US,
772 SPINAND_READ_POLL_DELAY_US,
773 &status);
774 if (ret < 0)
775 return ret;
776
777 spinand_ondie_ecc_save_status(nand, status);
778
779 ret = spinand_read_from_cache_op(spinand, req);
780 if (ret)
781 return ret;
782
783 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
784 }
785
786 /**
787 * spinand_write_page() - Write a page
788 * @spinand: the spinand device
789 * @req: the I/O request
790 *
791 * Return: 0 or a positive number of bitflips corrected on success.
792 * A negative error code otherwise.
793 */
spinand_write_page(struct spinand_device * spinand,const struct nand_page_io_req * req)794 int spinand_write_page(struct spinand_device *spinand,
795 const struct nand_page_io_req *req)
796 {
797 struct nand_device *nand = spinand_to_nand(spinand);
798 u8 status;
799 int ret;
800
801 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
802 if (ret)
803 return ret;
804
805 ret = spinand_write_enable_op(spinand);
806 if (ret)
807 return ret;
808
809 ret = spinand_write_to_cache_op(spinand, req);
810 if (ret)
811 return ret;
812
813 ret = spinand_program_op(spinand, req);
814 if (ret)
815 return ret;
816
817 ret = spinand_wait(spinand,
818 SPINAND_WRITE_INITIAL_DELAY_US,
819 SPINAND_WRITE_POLL_DELAY_US,
820 &status);
821 if (ret)
822 return ret;
823
824 if (status & STATUS_PROG_FAILED)
825 return -EIO;
826
827 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
828 }
829
spinand_mtd_regular_page_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops,unsigned int * max_bitflips)830 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from,
831 struct mtd_oob_ops *ops,
832 unsigned int *max_bitflips)
833 {
834 struct spinand_device *spinand = mtd_to_spinand(mtd);
835 struct nand_device *nand = mtd_to_nanddev(mtd);
836 struct mtd_ecc_stats old_stats;
837 struct nand_io_iter iter;
838 bool disable_ecc = false;
839 bool ecc_failed = false;
840 unsigned int retry_mode = 0;
841 int ret = 0;
842
843 old_stats = mtd->ecc_stats;
844
845 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
846 disable_ecc = true;
847
848 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
849 if (disable_ecc)
850 iter.req.mode = MTD_OPS_RAW;
851
852 ret = spinand_select_target(spinand, iter.req.pos.target);
853 if (ret)
854 break;
855
856 read_retry:
857 ret = spinand_read_page(spinand, &iter.req);
858 if (ret < 0 && ret != -EBADMSG)
859 break;
860
861 if (ret == -EBADMSG && spinand->set_read_retry) {
862 if (spinand->read_retries && (++retry_mode <= spinand->read_retries)) {
863 ret = spinand->set_read_retry(spinand, retry_mode);
864 if (ret < 0) {
865 spinand->set_read_retry(spinand, 0);
866 return ret;
867 }
868
869 /* Reset ecc_stats; retry */
870 mtd->ecc_stats = old_stats;
871 goto read_retry;
872 } else {
873 /* No more retry modes; real failure */
874 ecc_failed = true;
875 }
876 } else if (ret == -EBADMSG) {
877 ecc_failed = true;
878 } else {
879 *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
880 }
881
882 ret = 0;
883 ops->retlen += iter.req.datalen;
884 ops->oobretlen += iter.req.ooblen;
885
886 /* Reset to retry mode 0 */
887 if (retry_mode) {
888 retry_mode = 0;
889 ret = spinand->set_read_retry(spinand, retry_mode);
890 if (ret < 0)
891 return ret;
892 }
893 }
894
895 if (ecc_failed && !ret)
896 ret = -EBADMSG;
897
898 return ret;
899 }
900
spinand_mtd_continuous_page_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops,unsigned int * max_bitflips)901 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from,
902 struct mtd_oob_ops *ops,
903 unsigned int *max_bitflips)
904 {
905 struct spinand_device *spinand = mtd_to_spinand(mtd);
906 struct nand_device *nand = mtd_to_nanddev(mtd);
907 struct nand_io_iter iter;
908 u8 status;
909 int ret;
910
911 ret = spinand_cont_read_enable(spinand, true);
912 if (ret)
913 return ret;
914
915 /*
916 * The cache is divided into two halves. While one half of the cache has
917 * the requested data, the other half is loaded with the next chunk of data.
918 * Therefore, the host can read out the data continuously from page to page.
919 * Each data read must be a multiple of 4-bytes and full pages should be read;
920 * otherwise, the data output might get out of sequence from one read command
921 * to another.
922 *
923 * Continuous reads never cross LUN boundaries. Some devices don't
924 * support crossing planes boundaries. Some devices don't even support
925 * crossing blocks boundaries. The common case being to read through UBI,
926 * we will very rarely read two consequent blocks or more, so let's only enable
927 * continuous reads when reading within the same erase block.
928 */
929 nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) {
930 ret = spinand_select_target(spinand, iter.req.pos.target);
931 if (ret)
932 goto end_cont_read;
933
934 ret = nand_ecc_prepare_io_req(nand, &iter.req);
935 if (ret)
936 goto end_cont_read;
937
938 ret = spinand_load_page_op(spinand, &iter.req);
939 if (ret)
940 goto end_cont_read;
941
942 ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US,
943 SPINAND_READ_POLL_DELAY_US, NULL);
944 if (ret < 0)
945 goto end_cont_read;
946
947 ret = spinand_read_from_cache_op(spinand, &iter.req);
948 if (ret)
949 goto end_cont_read;
950
951 ops->retlen += iter.req.datalen;
952
953 ret = spinand_read_status(spinand, &status);
954 if (ret)
955 goto end_cont_read;
956
957 spinand_ondie_ecc_save_status(nand, status);
958
959 ret = nand_ecc_finish_io_req(nand, &iter.req);
960 if (ret < 0)
961 goto end_cont_read;
962
963 *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
964 ret = 0;
965 }
966
967 end_cont_read:
968 /*
969 * Once all the data has been read out, the host can either pull CS#
970 * high and wait for tRST or manually clear the bit in the configuration
971 * register to terminate the continuous read operation. We have no
972 * guarantee the SPI controller drivers will effectively deassert the CS
973 * when we expect them to, so take the register based approach.
974 */
975 spinand_cont_read_enable(spinand, false);
976
977 return ret;
978 }
979
spinand_cont_read_init(struct spinand_device * spinand)980 static void spinand_cont_read_init(struct spinand_device *spinand)
981 {
982 struct nand_device *nand = spinand_to_nand(spinand);
983 enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type;
984 struct spi_controller *ctlr = spinand->spimem->spi->controller;
985
986 /* OOBs cannot be retrieved so external/on-host ECC engine won't work */
987 if (spinand->set_cont_read) {
988 /*
989 * Ensure continuous read is disabled on probe.
990 * Some devices retain this state across soft reset,
991 * which leaves the OOB area inaccessible and results
992 * in false positive returns from spinand_isbad().
993 */
994 spinand_cont_read_enable(spinand, false);
995
996 if ((engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE ||
997 engine_type == NAND_ECC_ENGINE_TYPE_NONE) &&
998 !spi_mem_controller_is_capable(ctlr, no_cs_assertion))
999 spinand->cont_read_possible = true;
1000 }
1001 }
1002
spinand_use_cont_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)1003 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from,
1004 struct mtd_oob_ops *ops)
1005 {
1006 struct nand_device *nand = mtd_to_nanddev(mtd);
1007 struct spinand_device *spinand = nand_to_spinand(nand);
1008 struct nand_pos start_pos, end_pos;
1009
1010 if (!spinand->cont_read_possible)
1011 return false;
1012
1013 /* OOBs won't be retrieved */
1014 if (ops->ooblen || ops->oobbuf)
1015 return false;
1016
1017 nanddev_offs_to_pos(nand, from, &start_pos);
1018 nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos);
1019
1020 return start_pos.page < end_pos.page;
1021 }
1022
spinand_mtd_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)1023 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
1024 struct mtd_oob_ops *ops)
1025 {
1026 struct spinand_device *spinand = mtd_to_spinand(mtd);
1027 struct mtd_ecc_stats old_stats;
1028 unsigned int max_bitflips = 0;
1029 int ret;
1030
1031 mutex_lock(&spinand->lock);
1032
1033 old_stats = mtd->ecc_stats;
1034
1035 if (spinand_use_cont_read(mtd, from, ops)) {
1036 ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips);
1037 if (ret == -EAGAIN && !spinand->cont_read_possible) {
1038 /*
1039 * Spi controller with broken support of continuous
1040 * reading was detected (see spinand_read_from_cache_op()),
1041 * repeat reading in regular mode.
1042 */
1043 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips);
1044 }
1045 } else {
1046 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips);
1047 }
1048
1049 if (ops->stats) {
1050 ops->stats->uncorrectable_errors +=
1051 mtd->ecc_stats.failed - old_stats.failed;
1052 ops->stats->corrected_bitflips +=
1053 mtd->ecc_stats.corrected - old_stats.corrected;
1054 }
1055
1056 mutex_unlock(&spinand->lock);
1057
1058 return ret ? ret : max_bitflips;
1059 }
1060
spinand_mtd_write(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)1061 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
1062 struct mtd_oob_ops *ops)
1063 {
1064 struct spinand_device *spinand = mtd_to_spinand(mtd);
1065 struct nand_device *nand = mtd_to_nanddev(mtd);
1066 struct nand_io_iter iter;
1067 bool disable_ecc = false;
1068 int ret = 0;
1069
1070 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
1071 disable_ecc = true;
1072
1073 mutex_lock(&spinand->lock);
1074
1075 nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) {
1076 if (disable_ecc)
1077 iter.req.mode = MTD_OPS_RAW;
1078
1079 ret = spinand_select_target(spinand, iter.req.pos.target);
1080 if (ret)
1081 break;
1082
1083 ret = spinand_write_page(spinand, &iter.req);
1084 if (ret)
1085 break;
1086
1087 ops->retlen += iter.req.datalen;
1088 ops->oobretlen += iter.req.ooblen;
1089 }
1090
1091 mutex_unlock(&spinand->lock);
1092
1093 return ret;
1094 }
1095
spinand_isbad(struct nand_device * nand,const struct nand_pos * pos)1096 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
1097 {
1098 struct spinand_device *spinand = nand_to_spinand(nand);
1099 u8 marker[2] = { };
1100 struct nand_page_io_req req = {
1101 .pos = *pos,
1102 .ooblen = sizeof(marker),
1103 .ooboffs = 0,
1104 .oobbuf.in = marker,
1105 .mode = MTD_OPS_RAW,
1106 };
1107 int ret;
1108
1109 spinand_select_target(spinand, pos->target);
1110
1111 ret = spinand_read_page(spinand, &req);
1112 if (ret == -EOPNOTSUPP) {
1113 /* Retry with ECC in case raw access is not supported */
1114 req.mode = MTD_OPS_PLACE_OOB;
1115 spinand_read_page(spinand, &req);
1116 }
1117
1118 if (marker[0] != 0xff || marker[1] != 0xff)
1119 return true;
1120
1121 return false;
1122 }
1123
spinand_mtd_block_isbad(struct mtd_info * mtd,loff_t offs)1124 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
1125 {
1126 struct nand_device *nand = mtd_to_nanddev(mtd);
1127 struct spinand_device *spinand = nand_to_spinand(nand);
1128 struct nand_pos pos;
1129 int ret;
1130
1131 nanddev_offs_to_pos(nand, offs, &pos);
1132 mutex_lock(&spinand->lock);
1133 ret = nanddev_isbad(nand, &pos);
1134 mutex_unlock(&spinand->lock);
1135
1136 return ret;
1137 }
1138
spinand_markbad(struct nand_device * nand,const struct nand_pos * pos)1139 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
1140 {
1141 struct spinand_device *spinand = nand_to_spinand(nand);
1142 u8 marker[2] = { };
1143 struct nand_page_io_req req = {
1144 .pos = *pos,
1145 .ooboffs = 0,
1146 .ooblen = sizeof(marker),
1147 .oobbuf.out = marker,
1148 .mode = MTD_OPS_RAW,
1149 };
1150 int ret;
1151
1152 ret = spinand_select_target(spinand, pos->target);
1153 if (ret)
1154 return ret;
1155
1156 ret = spinand_write_page(spinand, &req);
1157 if (ret == -EOPNOTSUPP) {
1158 /* Retry with ECC in case raw access is not supported */
1159 req.mode = MTD_OPS_PLACE_OOB;
1160 ret = spinand_write_page(spinand, &req);
1161 }
1162
1163 return ret;
1164 }
1165
spinand_mtd_block_markbad(struct mtd_info * mtd,loff_t offs)1166 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
1167 {
1168 struct nand_device *nand = mtd_to_nanddev(mtd);
1169 struct spinand_device *spinand = nand_to_spinand(nand);
1170 struct nand_pos pos;
1171 int ret;
1172
1173 nanddev_offs_to_pos(nand, offs, &pos);
1174 mutex_lock(&spinand->lock);
1175 ret = nanddev_markbad(nand, &pos);
1176 mutex_unlock(&spinand->lock);
1177
1178 return ret;
1179 }
1180
spinand_erase(struct nand_device * nand,const struct nand_pos * pos)1181 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
1182 {
1183 struct spinand_device *spinand = nand_to_spinand(nand);
1184 u8 status;
1185 int ret;
1186
1187 ret = spinand_select_target(spinand, pos->target);
1188 if (ret)
1189 return ret;
1190
1191 ret = spinand_write_enable_op(spinand);
1192 if (ret)
1193 return ret;
1194
1195 ret = spinand_erase_op(spinand, pos);
1196 if (ret)
1197 return ret;
1198
1199 ret = spinand_wait(spinand,
1200 SPINAND_ERASE_INITIAL_DELAY_US,
1201 SPINAND_ERASE_POLL_DELAY_US,
1202 &status);
1203
1204 if (!ret && (status & STATUS_ERASE_FAILED))
1205 ret = -EIO;
1206
1207 return ret;
1208 }
1209
spinand_mtd_erase(struct mtd_info * mtd,struct erase_info * einfo)1210 static int spinand_mtd_erase(struct mtd_info *mtd,
1211 struct erase_info *einfo)
1212 {
1213 struct spinand_device *spinand = mtd_to_spinand(mtd);
1214 int ret;
1215
1216 mutex_lock(&spinand->lock);
1217 ret = nanddev_mtd_erase(mtd, einfo);
1218 mutex_unlock(&spinand->lock);
1219
1220 return ret;
1221 }
1222
spinand_mtd_block_isreserved(struct mtd_info * mtd,loff_t offs)1223 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
1224 {
1225 struct spinand_device *spinand = mtd_to_spinand(mtd);
1226 struct nand_device *nand = mtd_to_nanddev(mtd);
1227 struct nand_pos pos;
1228 int ret;
1229
1230 nanddev_offs_to_pos(nand, offs, &pos);
1231 mutex_lock(&spinand->lock);
1232 ret = nanddev_isreserved(nand, &pos);
1233 mutex_unlock(&spinand->lock);
1234
1235 return ret;
1236 }
1237
spinand_create_rdesc(struct spinand_device * spinand,struct spi_mem_dirmap_info * info)1238 static struct spi_mem_dirmap_desc *spinand_create_rdesc(
1239 struct spinand_device *spinand,
1240 struct spi_mem_dirmap_info *info)
1241 {
1242 struct nand_device *nand = spinand_to_nand(spinand);
1243 struct spi_mem_dirmap_desc *desc = NULL;
1244
1245 if (spinand->cont_read_possible) {
1246 /*
1247 * spi controller may return an error if info->length is
1248 * too large
1249 */
1250 info->length = nanddev_eraseblock_size(nand);
1251 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1252 spinand->spimem, info);
1253 }
1254
1255 if (IS_ERR_OR_NULL(desc)) {
1256 /*
1257 * continuous reading is not supported by flash or
1258 * its spi controller, use regular reading
1259 */
1260 spinand->cont_read_possible = false;
1261 memset(&info->secondary_op_tmpl, 0, sizeof(info->secondary_op_tmpl));
1262
1263 info->length = nanddev_page_size(nand) +
1264 nanddev_per_page_oobsize(nand);
1265 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1266 spinand->spimem, info);
1267 }
1268
1269 return desc;
1270 }
1271
spinand_create_dirmap(struct spinand_device * spinand,unsigned int plane)1272 static int spinand_create_dirmap(struct spinand_device *spinand,
1273 unsigned int plane)
1274 {
1275 struct nand_device *nand = spinand_to_nand(spinand);
1276 struct spi_mem_dirmap_info info = { 0 };
1277 struct spi_mem_dirmap_desc *desc;
1278 bool enable_ecc = false, secondary_op = false;
1279
1280 if (nand_ecc_is_pipelined(nand))
1281 enable_ecc = true;
1282
1283 if (spinand->cont_read_possible && spinand->op_templates->cont_read_cache)
1284 secondary_op = true;
1285
1286 /*
1287 * Continuous read implies that only the main data is retrieved, backed
1288 * by an on-die ECC engine. It is not possible to use a pipelind ECC
1289 * engine with continuous read.
1290 */
1291 if (enable_ecc && secondary_op) {
1292 secondary_op = false;
1293 spinand->cont_read_possible = false;
1294 }
1295
1296 /* The plane number is passed in MSB just above the column address */
1297 info.offset = plane << fls(nand->memorg.pagesize);
1298
1299 /* Write descriptor */
1300 info.length = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
1301 info.primary_op_tmpl = *spinand->op_templates->update_cache;
1302 info.primary_op_tmpl.data.ecc = enable_ecc;
1303 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1304 spinand->spimem, &info);
1305 if (IS_ERR(desc))
1306 return PTR_ERR(desc);
1307
1308 spinand->dirmaps[plane].wdesc = desc;
1309
1310 /* Read descriptor */
1311 info.primary_op_tmpl = *spinand->op_templates->read_cache;
1312 info.primary_op_tmpl.data.ecc = enable_ecc;
1313 if (secondary_op) {
1314 info.secondary_op_tmpl = *spinand->op_templates->cont_read_cache;
1315 info.secondary_op_tmpl.data.ecc = enable_ecc;
1316 }
1317 desc = spinand_create_rdesc(spinand, &info);
1318 if (IS_ERR(desc))
1319 return PTR_ERR(desc);
1320
1321 spinand->dirmaps[plane].rdesc = desc;
1322
1323 return 0;
1324 }
1325
spinand_create_dirmaps(struct spinand_device * spinand)1326 static int spinand_create_dirmaps(struct spinand_device *spinand)
1327 {
1328 struct nand_device *nand = spinand_to_nand(spinand);
1329 int i, ret;
1330
1331 spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev,
1332 sizeof(*spinand->dirmaps) *
1333 nand->memorg.planes_per_lun,
1334 GFP_KERNEL);
1335 if (!spinand->dirmaps)
1336 return -ENOMEM;
1337
1338 for (i = 0; i < nand->memorg.planes_per_lun; i++) {
1339 ret = spinand_create_dirmap(spinand, i);
1340 if (ret)
1341 return ret;
1342 }
1343
1344 return 0;
1345 }
1346
spinand_randomizer_init(struct spinand_device * spinand)1347 static int spinand_randomizer_init(struct spinand_device *spinand)
1348 {
1349 struct device_node *np = spinand->spimem->spi->dev.of_node;
1350 u32 rand_val;
1351 int ret;
1352
1353 if (!spinand->set_randomizer)
1354 return 0;
1355
1356 ret = of_property_read_u32(np, "nand-randomizer", &rand_val);
1357 if (ret)
1358 return 0;
1359
1360 return spinand->set_randomizer(spinand, rand_val);
1361 }
1362
1363 static const struct nand_ops spinand_ops = {
1364 .erase = spinand_erase,
1365 .markbad = spinand_markbad,
1366 .isbad = spinand_isbad,
1367 };
1368
1369 static const struct spinand_manufacturer *spinand_manufacturers[] = {
1370 &alliancememory_spinand_manufacturer,
1371 &ato_spinand_manufacturer,
1372 &dosilicon_spinand_manufacturer,
1373 &esmt_8c_spinand_manufacturer,
1374 &esmt_c8_spinand_manufacturer,
1375 &fmsh_spinand_manufacturer,
1376 &foresee_spinand_manufacturer,
1377 &gigadevice_spinand_manufacturer,
1378 &heyangtek_spinand_manufacturer,
1379 ¯onix_spinand_manufacturer,
1380 µn_spinand_manufacturer,
1381 ¶gon_spinand_manufacturer,
1382 &skyhigh_spinand_manufacturer,
1383 &toshiba_spinand_manufacturer,
1384 &winbond_spinand_manufacturer,
1385 &xtx_spinand_manufacturer,
1386 };
1387
spinand_manufacturer_match(struct spinand_device * spinand,enum spinand_readid_method rdid_method)1388 static int spinand_manufacturer_match(struct spinand_device *spinand,
1389 enum spinand_readid_method rdid_method)
1390 {
1391 u8 *id = spinand->id.data;
1392 unsigned int i;
1393 int ret;
1394
1395 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
1396 const struct spinand_manufacturer *manufacturer =
1397 spinand_manufacturers[i];
1398
1399 if (id[0] != manufacturer->id)
1400 continue;
1401
1402 ret = spinand_match_and_init(spinand,
1403 manufacturer->chips,
1404 manufacturer->nchips,
1405 rdid_method);
1406 if (ret < 0)
1407 continue;
1408
1409 spinand->manufacturer = manufacturer;
1410 return 0;
1411 }
1412 return -EOPNOTSUPP;
1413 }
1414
spinand_id_detect(struct spinand_device * spinand)1415 static int spinand_id_detect(struct spinand_device *spinand)
1416 {
1417 u8 *id = spinand->id.data;
1418 int ret;
1419
1420 ret = spinand_read_id_op(spinand, 0, 0, id);
1421 if (ret)
1422 return ret;
1423 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE);
1424 if (!ret)
1425 return 0;
1426
1427 ret = spinand_read_id_op(spinand, 1, 0, id);
1428 if (ret)
1429 return ret;
1430 ret = spinand_manufacturer_match(spinand,
1431 SPINAND_READID_METHOD_OPCODE_ADDR);
1432 if (!ret)
1433 return 0;
1434
1435 ret = spinand_read_id_op(spinand, 0, 1, id);
1436 if (ret)
1437 return ret;
1438 ret = spinand_manufacturer_match(spinand,
1439 SPINAND_READID_METHOD_OPCODE_DUMMY);
1440
1441 return ret;
1442 }
1443
spinand_manufacturer_init(struct spinand_device * spinand)1444 static int spinand_manufacturer_init(struct spinand_device *spinand)
1445 {
1446 int ret;
1447
1448 if (spinand->manufacturer->ops->init) {
1449 ret = spinand->manufacturer->ops->init(spinand);
1450 if (ret)
1451 return ret;
1452 }
1453
1454 return 0;
1455 }
1456
spinand_manufacturer_cleanup(struct spinand_device * spinand)1457 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
1458 {
1459 /* Release manufacturer private data */
1460 if (spinand->manufacturer->ops->cleanup)
1461 return spinand->manufacturer->ops->cleanup(spinand);
1462 }
1463
spinand_op_is_odtr(const struct spi_mem_op * op)1464 bool spinand_op_is_odtr(const struct spi_mem_op *op)
1465 {
1466 return op->cmd.dtr && op->cmd.buswidth == 8;
1467 }
1468
spinand_init_ssdr_templates(struct spinand_device * spinand)1469 static void spinand_init_ssdr_templates(struct spinand_device *spinand)
1470 {
1471 struct spinand_mem_ops *tmpl = &spinand->ssdr_op_templates;
1472
1473 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_1S_0_0_OP;
1474 tmpl->readid = (struct spi_mem_op)SPINAND_READID_1S_1S_1S_OP(0, 0, NULL, 0);
1475 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_1S_0_0_OP;
1476 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_1S_0_0_OP;
1477 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_1S_1S_1S_OP(0, NULL);
1478 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_1S_1S_1S_OP(0, NULL);
1479 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_1S_1S_0_OP(0);
1480 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_1S_1S_0_OP(0);
1481 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_1S_1S_0_OP(0);
1482 spinand->op_templates = &spinand->ssdr_op_templates;
1483 spinand->bus_iface = SSDR;
1484 }
1485
spinand_support_vendor_ops(struct spinand_device * spinand,const struct spinand_info * info,enum spinand_bus_interface iface)1486 static int spinand_support_vendor_ops(struct spinand_device *spinand,
1487 const struct spinand_info *info,
1488 enum spinand_bus_interface iface)
1489 {
1490 int i;
1491
1492 if (!info->vendor_ops)
1493 return 0;
1494 /*
1495 * The vendor ops array is only used in order to verify this chip and all its memory
1496 * operations are supported. If we see patterns emerging, we could ideally name these
1497 * operations and define them at the SPI NAND core level instead.
1498 * For now, this only serves as a sanity check.
1499 */
1500 for (i = 0; i < info->vendor_ops->nops; i++) {
1501 const struct spi_mem_op *op = &info->vendor_ops->ops[i];
1502
1503 if ((iface == SSDR && spinand_op_is_odtr(op)) ||
1504 (iface == ODTR && !spinand_op_is_odtr(op)))
1505 continue;
1506
1507 if (!spi_mem_supports_op(spinand->spimem, op))
1508 return -EOPNOTSUPP;
1509 }
1510
1511 return 0;
1512 }
1513
spinand_init_odtr_instruction_set(struct spinand_device * spinand)1514 static int spinand_init_odtr_instruction_set(struct spinand_device *spinand)
1515 {
1516 struct spinand_mem_ops *tmpl = &spinand->odtr_op_templates;
1517
1518 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_8D_0_0_OP;
1519 if (!spi_mem_supports_op(spinand->spimem, &tmpl->reset))
1520 return -EOPNOTSUPP;
1521
1522 tmpl->readid = (struct spi_mem_op)SPINAND_READID_8D_8D_8D_OP(0, 0, NULL, 0);
1523 if (!spi_mem_supports_op(spinand->spimem, &tmpl->readid))
1524 return -EOPNOTSUPP;
1525
1526 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_8D_0_0_OP;
1527 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_en))
1528 return -EOPNOTSUPP;
1529
1530 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_8D_0_0_OP;
1531 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_dis))
1532 return -EOPNOTSUPP;
1533
1534 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_8D_8D_8D_OP(0, NULL);
1535 if (!spi_mem_supports_op(spinand->spimem, &tmpl->set_feature))
1536 return -EOPNOTSUPP;
1537
1538 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_8D_8D_8D_OP(0, NULL);
1539 if (!spi_mem_supports_op(spinand->spimem, &tmpl->get_feature))
1540 return -EOPNOTSUPP;
1541
1542 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_8D_8D_0_OP(0);
1543 if (!spi_mem_supports_op(spinand->spimem, &tmpl->blk_erase))
1544 return -EOPNOTSUPP;
1545
1546 if (spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ)
1547 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_PACKED_8D_8D_0_OP(0);
1548 else
1549 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_8D_8D_0_OP(0);
1550 if (!spi_mem_supports_op(spinand->spimem, &tmpl->page_read)) {
1551 return -EOPNOTSUPP;
1552 }
1553
1554 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_8D_8D_0_OP(0);
1555 if (!spi_mem_supports_op(spinand->spimem, &tmpl->prog_exec))
1556 return -EOPNOTSUPP;
1557
1558 return 0;
1559 }
1560
1561 static const struct spi_mem_op *
spinand_select_op_variant(struct spinand_device * spinand,enum spinand_bus_interface iface,const struct spinand_op_variants * variants)1562 spinand_select_op_variant(struct spinand_device *spinand, enum spinand_bus_interface iface,
1563 const struct spinand_op_variants *variants)
1564 {
1565 struct nand_device *nand = spinand_to_nand(spinand);
1566 const struct spi_mem_op *best_variant = NULL;
1567 u64 best_op_duration_ns = ULLONG_MAX;
1568 unsigned int i;
1569
1570 for (i = 0; i < variants->nops; i++) {
1571 struct spi_mem_op op = variants->ops[i];
1572 u64 op_duration_ns = 0;
1573 unsigned int nbytes;
1574 int ret;
1575
1576 if ((iface == SSDR && spinand_op_is_odtr(&op)) ||
1577 (iface == ODTR && !spinand_op_is_odtr(&op)))
1578 continue;
1579
1580 nbytes = nanddev_per_page_oobsize(nand) +
1581 nanddev_page_size(nand);
1582
1583 while (nbytes) {
1584 op.data.nbytes = nbytes;
1585 ret = spi_mem_adjust_op_size(spinand->spimem, &op);
1586 if (ret)
1587 break;
1588
1589 spi_mem_adjust_op_freq(spinand->spimem, &op);
1590
1591 if (!spi_mem_supports_op(spinand->spimem, &op))
1592 break;
1593
1594 nbytes -= op.data.nbytes;
1595
1596 op_duration_ns += spi_mem_calc_op_duration(spinand->spimem, &op);
1597 }
1598
1599 if (!nbytes && op_duration_ns < best_op_duration_ns) {
1600 best_op_duration_ns = op_duration_ns;
1601 best_variant = &variants->ops[i];
1602 }
1603 }
1604
1605 return best_variant;
1606 }
1607
1608 /**
1609 * spinand_match_and_init() - Try to find a match between a device ID and an
1610 * entry in a spinand_info table
1611 * @spinand: SPI NAND object
1612 * @table: SPI NAND device description table
1613 * @table_size: size of the device description table
1614 * @rdid_method: read id method to match
1615 *
1616 * Match between a device ID retrieved through the READ_ID command and an
1617 * entry in the SPI NAND description table. If a match is found, the spinand
1618 * object will be initialized with information provided by the matching
1619 * spinand_info entry.
1620 *
1621 * Return: 0 on success, a negative error code otherwise.
1622 */
spinand_match_and_init(struct spinand_device * spinand,const struct spinand_info * table,unsigned int table_size,enum spinand_readid_method rdid_method)1623 int spinand_match_and_init(struct spinand_device *spinand,
1624 const struct spinand_info *table,
1625 unsigned int table_size,
1626 enum spinand_readid_method rdid_method)
1627 {
1628 u8 *id = spinand->id.data;
1629 struct nand_device *nand = spinand_to_nand(spinand);
1630 unsigned int i;
1631 int ret;
1632
1633 for (i = 0; i < table_size; i++) {
1634 const struct spinand_info *info = &table[i];
1635 const struct spi_mem_op *op;
1636
1637 if (rdid_method != info->devid.method)
1638 continue;
1639
1640 if (memcmp(id + 1, info->devid.id, info->devid.len))
1641 continue;
1642
1643 nand->memorg = table[i].memorg;
1644 nanddev_set_ecc_requirements(nand, &table[i].eccreq);
1645 spinand->eccinfo = table[i].eccinfo;
1646 spinand->flags = table[i].flags;
1647 spinand->id.len = 1 + table[i].devid.len;
1648 spinand->select_target = table[i].select_target;
1649 spinand->configure_chip = table[i].configure_chip;
1650 spinand->set_cont_read = table[i].set_cont_read;
1651 spinand->fact_otp = &table[i].fact_otp;
1652 spinand->user_otp = &table[i].user_otp;
1653 spinand->read_retries = table[i].read_retries;
1654 spinand->set_read_retry = table[i].set_read_retry;
1655 spinand->set_randomizer = table[i].set_randomizer;
1656
1657 /* I/O variants selection with single-spi SDR commands */
1658
1659 op = spinand_select_op_variant(spinand, SSDR,
1660 info->op_variants.read_cache);
1661 if (!op)
1662 return -EOPNOTSUPP;
1663
1664 spinand->ssdr_op_templates.read_cache = op;
1665
1666 op = spinand_select_op_variant(spinand, SSDR,
1667 info->op_variants.write_cache);
1668 if (!op)
1669 return -EOPNOTSUPP;
1670
1671 spinand->ssdr_op_templates.write_cache = op;
1672
1673 op = spinand_select_op_variant(spinand, SSDR,
1674 info->op_variants.update_cache);
1675 if (!op)
1676 return -EOPNOTSUPP;
1677
1678 spinand->ssdr_op_templates.update_cache = op;
1679
1680 ret = spinand_support_vendor_ops(spinand, info, SSDR);
1681 if (ret)
1682 return ret;
1683
1684 if (info->op_variants.cont_read_cache) {
1685 op = spinand_select_op_variant(spinand, SSDR,
1686 info->op_variants.cont_read_cache);
1687 if (op) {
1688 const struct spi_mem_op *read_op;
1689
1690 read_op = spinand->ssdr_op_templates.read_cache;
1691
1692 /*
1693 * Sometimes the fastest continuous read variant may not
1694 * be supported. In this case, prefer to use the fastest
1695 * read from cache variant and disable continuous reads.
1696 */
1697 if (read_op->cmd.buswidth > op->cmd.buswidth ||
1698 (read_op->cmd.dtr && !op->cmd.dtr) ||
1699 read_op->addr.buswidth > op->addr.buswidth ||
1700 (read_op->addr.dtr && !op->addr.dtr) ||
1701 read_op->data.buswidth > op->data.buswidth ||
1702 (read_op->data.dtr && !op->data.dtr))
1703 spinand->cont_read_possible = false;
1704 else
1705 spinand->ssdr_op_templates.cont_read_cache = op;
1706 } else {
1707 spinand->cont_read_possible = false;
1708 }
1709 }
1710
1711 /* I/O variants selection with octo-spi DDR commands (optional) */
1712
1713 ret = spinand_init_odtr_instruction_set(spinand);
1714 if (ret)
1715 return 0;
1716
1717 ret = spinand_support_vendor_ops(spinand, info, ODTR);
1718 if (ret)
1719 return 0;
1720
1721 op = spinand_select_op_variant(spinand, ODTR,
1722 info->op_variants.read_cache);
1723 spinand->odtr_op_templates.read_cache = op;
1724
1725 op = spinand_select_op_variant(spinand, ODTR,
1726 info->op_variants.write_cache);
1727 spinand->odtr_op_templates.write_cache = op;
1728
1729 op = spinand_select_op_variant(spinand, ODTR,
1730 info->op_variants.update_cache);
1731 spinand->odtr_op_templates.update_cache = op;
1732
1733 if (info->op_variants.cont_read_cache) {
1734 op = spinand_select_op_variant(spinand, ODTR,
1735 info->op_variants.cont_read_cache);
1736 if (op)
1737 spinand->odtr_op_templates.cont_read_cache = op;
1738 else
1739 spinand->cont_read_possible = false;
1740 }
1741
1742 return 0;
1743 }
1744
1745 return -EOPNOTSUPP;
1746 }
1747
spinand_detect(struct spinand_device * spinand)1748 static int spinand_detect(struct spinand_device *spinand)
1749 {
1750 struct device *dev = &spinand->spimem->spi->dev;
1751 struct nand_device *nand = spinand_to_nand(spinand);
1752 int ret;
1753
1754 ret = spinand_reset_op(spinand);
1755 if (ret)
1756 return ret;
1757
1758 ret = spinand_id_detect(spinand);
1759 if (ret) {
1760 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
1761 spinand->id.data);
1762 return ret;
1763 }
1764
1765 if (nand->memorg.ntargets > 1 && !spinand->select_target) {
1766 dev_err(dev,
1767 "SPI NANDs with more than one die must implement ->select_target()\n");
1768 return -EINVAL;
1769 }
1770
1771 dev_info(&spinand->spimem->spi->dev,
1772 "%s SPI NAND was found.\n", spinand->manufacturer->name);
1773 dev_info(&spinand->spimem->spi->dev,
1774 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1775 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1776 nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1777
1778 return 0;
1779 }
1780
spinand_configure_chip(struct spinand_device * spinand)1781 static int spinand_configure_chip(struct spinand_device *spinand)
1782 {
1783 bool odtr = false, quad_enable = false;
1784 int ret;
1785
1786 if (spinand->odtr_op_templates.read_cache &&
1787 spinand->odtr_op_templates.write_cache &&
1788 spinand->odtr_op_templates.update_cache)
1789 odtr = true;
1790
1791 if (odtr) {
1792 if (!spinand->configure_chip)
1793 goto try_ssdr;
1794
1795 /* ODTR bus interface configuration happens here */
1796 ret = spinand->configure_chip(spinand, ODTR);
1797 if (ret) {
1798 spinand->odtr_op_templates.read_cache = NULL;
1799 spinand->odtr_op_templates.write_cache = NULL;
1800 spinand->odtr_op_templates.update_cache = NULL;
1801 goto try_ssdr;
1802 }
1803
1804 spinand->op_templates = &spinand->odtr_op_templates;
1805 spinand->bus_iface = ODTR;
1806
1807 return 0;
1808 }
1809
1810 try_ssdr:
1811 if (spinand->flags & SPINAND_HAS_QE_BIT) {
1812 if (spinand->ssdr_op_templates.read_cache->data.buswidth == 4 ||
1813 spinand->ssdr_op_templates.write_cache->data.buswidth == 4 ||
1814 spinand->ssdr_op_templates.update_cache->data.buswidth == 4)
1815 quad_enable = true;
1816
1817 ret = spinand_init_quad_enable(spinand, quad_enable);
1818 if (ret)
1819 return ret;
1820 }
1821
1822 if (spinand->configure_chip) {
1823 ret = spinand->configure_chip(spinand, SSDR);
1824 if (ret)
1825 return ret;
1826 }
1827
1828 return 0;
1829 }
1830
spinand_init_flash(struct spinand_device * spinand)1831 static int spinand_init_flash(struct spinand_device *spinand)
1832 {
1833 struct device *dev = &spinand->spimem->spi->dev;
1834 struct nand_device *nand = spinand_to_nand(spinand);
1835 int ret, i;
1836
1837 ret = spinand_read_cfg(spinand);
1838 if (ret)
1839 return ret;
1840
1841 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1842 if (ret)
1843 return ret;
1844
1845 ret = spinand_manufacturer_init(spinand);
1846 if (ret) {
1847 dev_err(dev,
1848 "Failed to initialize the SPI NAND chip (err = %d)\n",
1849 ret);
1850 return ret;
1851 }
1852
1853 ret = spinand_configure_chip(spinand);
1854 if (ret)
1855 goto manuf_cleanup;
1856
1857 /* After power up, all blocks are locked, so unlock them here. */
1858 for (i = 0; i < nand->memorg.ntargets; i++) {
1859 ret = spinand_select_target(spinand, i);
1860 if (ret)
1861 goto manuf_cleanup;
1862
1863 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1864 if (ret)
1865 goto manuf_cleanup;
1866 }
1867
1868 return 0;
1869
1870 manuf_cleanup:
1871 spinand_manufacturer_cleanup(spinand);
1872
1873 return ret;
1874 }
1875
spinand_mtd_resume(struct mtd_info * mtd)1876 static void spinand_mtd_resume(struct mtd_info *mtd)
1877 {
1878 struct spinand_device *spinand = mtd_to_spinand(mtd);
1879 int ret;
1880
1881 ret = spinand_reset_op(spinand);
1882 if (ret)
1883 return;
1884
1885 ret = spinand_init_flash(spinand);
1886 if (ret)
1887 return;
1888
1889 spinand_ecc_enable(spinand, false);
1890 }
1891
spinand_mtd_suspend(struct mtd_info * mtd)1892 static int spinand_mtd_suspend(struct mtd_info *mtd)
1893 {
1894 struct spinand_device *spinand = mtd_to_spinand(mtd);
1895 int ret;
1896
1897 /*
1898 * Return to SSDR interface in the suspend path to make sure the
1899 * reset operation is correctly processed upon resume.
1900 *
1901 * Note: Once back in SSDR mode, every operation but the page helpers
1902 * (dirmap based I/O accessors) will work. Page accesses would require
1903 * destroying and recreating the dirmaps twice to work, which would be
1904 * impacting for no reason, as this is just a transitional state.
1905 */
1906 if (spinand->bus_iface == ODTR) {
1907 ret = spinand->configure_chip(spinand, SSDR);
1908 if (ret)
1909 return ret;
1910
1911 spinand->op_templates = &spinand->ssdr_op_templates;
1912 spinand->bus_iface = SSDR;
1913 }
1914
1915 return 0;
1916 }
1917
spinand_init(struct spinand_device * spinand)1918 static int spinand_init(struct spinand_device *spinand)
1919 {
1920 struct device *dev = &spinand->spimem->spi->dev;
1921 struct mtd_info *mtd = spinand_to_mtd(spinand);
1922 struct nand_device *nand = mtd_to_nanddev(mtd);
1923 int ret;
1924
1925 /*
1926 * We need a scratch buffer because the spi_mem interface requires that
1927 * buf passed in spi_mem_op->data.buf be DMA-able.
1928 */
1929 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1930 if (!spinand->scratchbuf)
1931 return -ENOMEM;
1932
1933 spinand_init_ssdr_templates(spinand);
1934
1935 ret = spinand_detect(spinand);
1936 if (ret)
1937 goto err_free_bufs;
1938
1939 /*
1940 * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1941 * may use this buffer for DMA access.
1942 * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1943 */
1944 spinand->databuf = kzalloc(nanddev_eraseblock_size(nand),
1945 GFP_KERNEL);
1946 if (!spinand->databuf) {
1947 ret = -ENOMEM;
1948 goto err_free_bufs;
1949 }
1950
1951 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1952
1953 ret = spinand_init_cfg_cache(spinand);
1954 if (ret)
1955 goto err_free_bufs;
1956
1957 ret = spinand_init_flash(spinand);
1958 if (ret)
1959 goto err_free_bufs;
1960
1961 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1962 if (ret)
1963 goto err_manuf_cleanup;
1964
1965 /* SPI-NAND default ECC engine is on-die */
1966 nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
1967 nand->ecc.ondie_engine = &spinand_ondie_ecc_engine;
1968
1969 spinand_ecc_enable(spinand, false);
1970 ret = nanddev_ecc_engine_init(nand);
1971 if (ret)
1972 goto err_cleanup_nanddev;
1973
1974 /*
1975 * Continuous read can only be enabled with an on-die ECC engine, so the
1976 * ECC initialization must have happened previously.
1977 */
1978 spinand_cont_read_init(spinand);
1979 ret = spinand_randomizer_init(spinand);
1980 if (ret)
1981 goto err_cleanup_nanddev;
1982
1983 mtd->_read_oob = spinand_mtd_read;
1984 mtd->_write_oob = spinand_mtd_write;
1985 mtd->_block_isbad = spinand_mtd_block_isbad;
1986 mtd->_block_markbad = spinand_mtd_block_markbad;
1987 mtd->_block_isreserved = spinand_mtd_block_isreserved;
1988 mtd->_erase = spinand_mtd_erase;
1989 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
1990 mtd->_suspend = spinand_mtd_suspend;
1991 mtd->_resume = spinand_mtd_resume;
1992
1993 if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) {
1994 ret = spinand_set_mtd_otp_ops(spinand);
1995 if (ret)
1996 goto err_cleanup_ecc_engine;
1997 }
1998
1999 if (!nand->ecc.engine) {
2000 if (spinand->eccinfo.ooblayout)
2001 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
2002 else
2003 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
2004 }
2005
2006 ret = mtd_ooblayout_count_freebytes(mtd);
2007 if (ret < 0)
2008 goto err_cleanup_ecc_engine;
2009
2010 mtd->oobavail = ret;
2011
2012 /* Propagate ECC information to mtd_info */
2013 mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength;
2014 mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size;
2015 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
2016
2017 ret = spinand_create_dirmaps(spinand);
2018 if (ret) {
2019 dev_err(dev,
2020 "Failed to create direct mappings for read/write operations (err = %d)\n",
2021 ret);
2022 goto err_cleanup_ecc_engine;
2023 }
2024
2025 return 0;
2026
2027 err_cleanup_ecc_engine:
2028 nanddev_ecc_engine_cleanup(nand);
2029
2030 err_cleanup_nanddev:
2031 nanddev_cleanup(nand);
2032
2033 err_manuf_cleanup:
2034 spinand_manufacturer_cleanup(spinand);
2035
2036 err_free_bufs:
2037 kfree(spinand->databuf);
2038 kfree(spinand->scratchbuf);
2039 return ret;
2040 }
2041
spinand_cleanup(struct spinand_device * spinand)2042 static void spinand_cleanup(struct spinand_device *spinand)
2043 {
2044 struct nand_device *nand = spinand_to_nand(spinand);
2045
2046 nanddev_ecc_engine_cleanup(nand);
2047 nanddev_cleanup(nand);
2048 spinand_manufacturer_cleanup(spinand);
2049 kfree(spinand->databuf);
2050 kfree(spinand->scratchbuf);
2051 }
2052
spinand_probe(struct spi_mem * mem)2053 static int spinand_probe(struct spi_mem *mem)
2054 {
2055 struct spinand_device *spinand;
2056 struct mtd_info *mtd;
2057 int ret;
2058
2059 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
2060 GFP_KERNEL);
2061 if (!spinand)
2062 return -ENOMEM;
2063
2064 spinand->spimem = mem;
2065 spi_mem_set_drvdata(mem, spinand);
2066 spinand_set_of_node(spinand, mem->spi->dev.of_node);
2067 mutex_init(&spinand->lock);
2068 mtd = spinand_to_mtd(spinand);
2069 mtd->dev.parent = &mem->spi->dev;
2070
2071 ret = spinand_init(spinand);
2072 if (ret)
2073 return ret;
2074
2075 ret = mtd_device_register(mtd, NULL, 0);
2076 if (ret)
2077 goto err_spinand_cleanup;
2078
2079 return 0;
2080
2081 err_spinand_cleanup:
2082 spinand_cleanup(spinand);
2083
2084 return ret;
2085 }
2086
spinand_remove(struct spi_mem * mem)2087 static int spinand_remove(struct spi_mem *mem)
2088 {
2089 struct spinand_device *spinand;
2090 struct mtd_info *mtd;
2091 int ret;
2092
2093 spinand = spi_mem_get_drvdata(mem);
2094 mtd = spinand_to_mtd(spinand);
2095
2096 ret = mtd_device_unregister(mtd);
2097 if (ret)
2098 return ret;
2099
2100 spinand_cleanup(spinand);
2101
2102 return 0;
2103 }
2104
2105 static const struct spi_device_id spinand_ids[] = {
2106 { .name = "spi-nand" },
2107 { /* sentinel */ },
2108 };
2109 MODULE_DEVICE_TABLE(spi, spinand_ids);
2110
2111 #ifdef CONFIG_OF
2112 static const struct of_device_id spinand_of_ids[] = {
2113 { .compatible = "spi-nand" },
2114 { /* sentinel */ },
2115 };
2116 MODULE_DEVICE_TABLE(of, spinand_of_ids);
2117 #endif
2118
2119 static struct spi_mem_driver spinand_drv = {
2120 .spidrv = {
2121 .id_table = spinand_ids,
2122 .driver = {
2123 .name = "spi-nand",
2124 .of_match_table = of_match_ptr(spinand_of_ids),
2125 },
2126 },
2127 .probe = spinand_probe,
2128 .remove = spinand_remove,
2129 };
2130 module_spi_mem_driver(spinand_drv);
2131
2132 MODULE_DESCRIPTION("SPI NAND framework");
2133 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
2134 MODULE_LICENSE("GPL v2");
2135