1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2017 Free Electrons
8 * Derived from the atmel_nand.c driver which contained the following
11 * Copyright 2003 Rick Bronson
13 * Derived from drivers/mtd/nand/autcpu12.c (removed in v3.8)
16 * Derived from drivers/mtd/spia.c (removed in v3.8)
20 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
23 * Derived from Das U-Boot source code
24 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
25 * Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
27 * Add Programmable Multibit ECC support for various AT91 SoC
28 * Copyright 2012 ATMEL, Hong Xu
30 * Add Nand Flash Controller support for SAMA5 SoC
35 * Copyright (C) 2022 Microchip Technology Inc.
37 * A few words about the naming convention in this file. This convention
38 * applies to structure and function names.
42 * - atmel_nand_: all generic structures/functions
43 * - atmel_smc_nand_: all structures/functions specific to the SMC interface
44 * (at91sam9 and avr32 SoCs)
45 * - atmel_hsmc_nand_: all structures/functions specific to the HSMC interface
46 * (sama5 SoCs and later)
47 * - atmel_nfc_: all structures/functions used to manipulate the NFC sub-block
48 * that is available in the HSMC block
49 * - <soc>_nand_: all SoC specific structures/functions
52 #include <asm-generic/gpio.h>
54 #include <dm/device_compat.h>
55 #include <dm/devres.h>
56 #include <dm/of_addr.h>
57 #include <dm/of_access.h>
58 #include <dm/uclass.h>
59 #include <linux/completion.h>
61 #include <linux/iopoll.h>
62 #include <linux/ioport.h>
63 #include <linux/mfd/syscon/atmel-matrix.h>
64 #include <linux/mfd/syscon/atmel-smc.h>
65 #include <linux/mtd/rawnand.h>
66 #include <linux/mtd/mtd.h>
67 #include <linux/time.h>
68 #include <mach/at91_sfr.h>
75 #define ATMEL_HSMC_NFC_CFG 0x0
76 #define ATMEL_HSMC_NFC_CFG_SPARESIZE(x) (((x) / 4) << 24)
77 #define ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK GENMASK(30, 24)
78 #define ATMEL_HSMC_NFC_CFG_DTO(cyc, mul) (((cyc) << 16) | ((mul) << 20))
79 #define ATMEL_HSMC_NFC_CFG_DTO_MAX GENMASK(22, 16)
80 #define ATMEL_HSMC_NFC_CFG_RBEDGE BIT(13)
81 #define ATMEL_HSMC_NFC_CFG_FALLING_EDGE BIT(12)
82 #define ATMEL_HSMC_NFC_CFG_RSPARE BIT(9)
83 #define ATMEL_HSMC_NFC_CFG_WSPARE BIT(8)
84 #define ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK GENMASK(2, 0)
85 #define ATMEL_HSMC_NFC_CFG_PAGESIZE(x) (fls((x) / 512) - 1)
87 #define ATMEL_HSMC_NFC_CTRL 0x4
88 #define ATMEL_HSMC_NFC_CTRL_EN BIT(0)
89 #define ATMEL_HSMC_NFC_CTRL_DIS BIT(1)
91 #define ATMEL_HSMC_NFC_SR 0x8
92 #define ATMEL_HSMC_NFC_IER 0xc
93 #define ATMEL_HSMC_NFC_IDR 0x10
94 #define ATMEL_HSMC_NFC_IMR 0x14
95 #define ATMEL_HSMC_NFC_SR_ENABLED BIT(1)
96 #define ATMEL_HSMC_NFC_SR_RB_RISE BIT(4)
97 #define ATMEL_HSMC_NFC_SR_RB_FALL BIT(5)
98 #define ATMEL_HSMC_NFC_SR_BUSY BIT(8)
99 #define ATMEL_HSMC_NFC_SR_WR BIT(11)
100 #define ATMEL_HSMC_NFC_SR_CSID GENMASK(14, 12)
101 #define ATMEL_HSMC_NFC_SR_XFRDONE BIT(16)
102 #define ATMEL_HSMC_NFC_SR_CMDDONE BIT(17)
103 #define ATMEL_HSMC_NFC_SR_DTOE BIT(20)
104 #define ATMEL_HSMC_NFC_SR_UNDEF BIT(21)
105 #define ATMEL_HSMC_NFC_SR_AWB BIT(22)
106 #define ATMEL_HSMC_NFC_SR_NFCASE BIT(23)
107 #define ATMEL_HSMC_NFC_SR_ERRORS (ATMEL_HSMC_NFC_SR_DTOE | \
108 ATMEL_HSMC_NFC_SR_UNDEF | \
109 ATMEL_HSMC_NFC_SR_AWB | \
110 ATMEL_HSMC_NFC_SR_NFCASE)
111 #define ATMEL_HSMC_NFC_SR_RBEDGE(x) BIT((x) + 24)
113 #define ATMEL_HSMC_NFC_ADDR 0x18
114 #define ATMEL_HSMC_NFC_BANK 0x1c
116 #define ATMEL_NFC_MAX_RB_ID 7
118 #define ATMEL_NFC_SRAM_SIZE 0x2400
120 #define ATMEL_NFC_CMD(pos, cmd) ((cmd) << (((pos) * 8) + 2))
121 #define ATMEL_NFC_VCMD2 BIT(18)
122 #define ATMEL_NFC_ACYCLE(naddrs) ((naddrs) << 19)
123 #define ATMEL_NFC_CSID(cs) ((cs) << 22)
124 #define ATMEL_NFC_DATAEN BIT(25)
125 #define ATMEL_NFC_NFCWR BIT(26)
127 #define ATMEL_NFC_MAX_ADDR_CYCLES 5
129 #define ATMEL_NAND_ALE_OFFSET BIT(21)
130 #define ATMEL_NAND_CLE_OFFSET BIT(22)
132 #define DEFAULT_TIMEOUT_MS 1000
133 #define MIN_DMA_LEN 128
135 static struct nand_ecclayout atmel_pmecc_oobinfo;
137 struct nand_controller_ops {
138 int (*attach_chip)(struct nand_chip *chip);
139 int (*setup_data_interface)(struct mtd_info *mtd, int chipnr,
140 const struct nand_data_interface *conf);
143 struct nand_controller {
144 const struct nand_controller_ops *ops;
147 enum atmel_nand_rb_type {
149 ATMEL_NAND_NATIVE_RB,
153 struct atmel_nand_rb {
154 enum atmel_nand_rb_type type;
156 struct gpio_desc gpio;
161 struct atmel_nand_cs {
163 struct atmel_nand_rb rb;
164 struct gpio_desc csgpio;
170 struct atmel_smc_cs_conf smcconf;
174 struct list_head node;
176 struct nand_chip base;
177 struct atmel_nand_cs *activecs;
178 struct atmel_pmecc_user *pmecc;
179 struct gpio_desc cdgpio;
181 struct nand_controller *controller;
182 struct atmel_nand_cs cs[];
185 static inline struct atmel_nand *to_atmel_nand(struct nand_chip *chip)
187 return container_of(chip, struct atmel_nand, base);
190 enum atmel_nfc_data_xfer {
193 ATMEL_NFC_WRITE_DATA,
196 struct atmel_nfc_op {
202 enum atmel_nfc_data_xfer data;
207 struct atmel_nand_controller;
208 struct atmel_nand_controller_caps;
210 struct atmel_nand_controller_ops {
211 int (*probe)(struct udevice *udev,
212 const struct atmel_nand_controller_caps *caps);
213 int (*remove)(struct atmel_nand_controller *nc);
214 void (*nand_init)(struct atmel_nand_controller *nc,
215 struct atmel_nand *nand);
216 int (*ecc_init)(struct nand_chip *chip);
217 int (*setup_data_interface)(struct atmel_nand *nand, int csline,
218 const struct nand_data_interface *conf);
221 struct atmel_nand_controller_caps {
223 bool legacy_of_bindings;
226 const char *ebi_csa_regmap_name;
227 const struct atmel_nand_controller_ops *ops;
230 struct atmel_nand_controller {
231 struct nand_controller base;
232 const struct atmel_nand_controller_caps *caps;
235 struct dma_chan *dmac;
236 struct atmel_pmecc *pmecc;
237 struct list_head chips;
241 static inline struct atmel_nand_controller *
242 to_nand_controller(struct nand_controller *ctl)
244 return container_of(ctl, struct atmel_nand_controller, base);
247 struct atmel_smc_nand_ebi_csa_cfg {
252 struct atmel_smc_nand_controller {
253 struct atmel_nand_controller base;
254 struct regmap *ebi_csa_regmap;
255 struct atmel_smc_nand_ebi_csa_cfg *ebi_csa;
258 static inline struct atmel_smc_nand_controller *
259 to_smc_nand_controller(struct nand_controller *ctl)
261 return container_of(to_nand_controller(ctl),
262 struct atmel_smc_nand_controller, base);
265 struct atmel_hsmc_nand_controller {
266 struct atmel_nand_controller base;
268 struct gen_pool *pool;
272 const struct atmel_hsmc_reg_layout *hsmc_layout;
274 struct atmel_nfc_op op;
275 struct completion complete;
278 /* Only used when instantiating from legacy DT bindings. */
282 static inline struct atmel_hsmc_nand_controller *
283 to_hsmc_nand_controller(struct nand_controller *ctl)
285 return container_of(to_nand_controller(ctl),
286 struct atmel_hsmc_nand_controller, base);
289 static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
290 int oobsize, int ecc_len)
294 layout->eccbytes = ecc_len;
296 /* ECC will occupy the last ecc_len bytes continuously */
297 for (i = 0; i < ecc_len; i++)
298 layout->eccpos[i] = oobsize - ecc_len + i;
300 layout->oobfree[0].offset = 2;
301 layout->oobfree[0].length =
302 oobsize - ecc_len - layout->oobfree[0].offset;
305 static bool atmel_nfc_op_done(struct atmel_nfc_op *op, u32 status)
307 op->errors |= status & ATMEL_HSMC_NFC_SR_ERRORS;
308 op->wait ^= status & op->wait;
310 return !op->wait || op->errors;
313 static int atmel_nfc_wait(struct atmel_hsmc_nand_controller *nc, bool poll,
314 unsigned int timeout_ms)
320 timeout_ms = DEFAULT_TIMEOUT_MS;
323 ret = regmap_read_poll_timeout(nc->base.smc,
324 ATMEL_HSMC_NFC_SR, status,
325 atmel_nfc_op_done(&nc->op,
331 if (nc->op.errors & ATMEL_HSMC_NFC_SR_DTOE) {
332 dev_err(nc->base.dev, "Waiting NAND R/B Timeout\n");
336 if (nc->op.errors & ATMEL_HSMC_NFC_SR_UNDEF) {
337 dev_err(nc->base.dev, "Access to an undefined area\n");
341 if (nc->op.errors & ATMEL_HSMC_NFC_SR_AWB) {
342 dev_err(nc->base.dev, "Access while busy\n");
346 if (nc->op.errors & ATMEL_HSMC_NFC_SR_NFCASE) {
347 dev_err(nc->base.dev, "Wrong access size\n");
354 static u8 atmel_nand_read_byte(struct mtd_info *mtd)
356 struct nand_chip *chip = mtd_to_nand(mtd);
357 struct atmel_nand *nand = to_atmel_nand(chip);
359 return ioread8(nand->activecs->io.virt);
362 static void atmel_nand_write_byte(struct mtd_info *mtd, u8 byte)
364 struct nand_chip *chip = mtd_to_nand(mtd);
365 struct atmel_nand *nand = to_atmel_nand(chip);
367 if (chip->options & NAND_BUSWIDTH_16)
368 iowrite16(byte | (byte << 8), nand->activecs->io.virt);
370 iowrite8(byte, nand->activecs->io.virt);
373 static void atmel_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
375 struct nand_chip *chip = mtd_to_nand(mtd);
376 struct atmel_nand *nand = to_atmel_nand(chip);
378 if (chip->options & NAND_BUSWIDTH_16)
379 ioread16_rep(nand->activecs->io.virt, buf, len / 2);
381 ioread8_rep(nand->activecs->io.virt, buf, len);
384 static void atmel_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
386 struct nand_chip *chip = mtd_to_nand(mtd);
387 struct atmel_nand *nand = to_atmel_nand(chip);
389 if (chip->options & NAND_BUSWIDTH_16)
390 iowrite16_rep(nand->activecs->io.virt, buf, len / 2);
392 iowrite8_rep(nand->activecs->io.virt, buf, len);
395 static int atmel_nand_dev_ready(struct mtd_info *mtd)
397 struct nand_chip *chip = mtd_to_nand(mtd);
398 struct atmel_nand *nand = to_atmel_nand(chip);
400 return dm_gpio_get_value(&nand->activecs->rb.gpio);
403 static void atmel_nand_select_chip(struct mtd_info *mtd, int cs)
405 struct nand_chip *chip = mtd_to_nand(mtd);
406 struct atmel_nand *nand = to_atmel_nand(chip);
408 if (cs < 0 || cs >= nand->numcs) {
409 nand->activecs = NULL;
410 chip->dev_ready = NULL;
414 nand->activecs = &nand->cs[cs];
416 if (nand->activecs->rb.type == ATMEL_NAND_GPIO_RB)
417 chip->dev_ready = atmel_nand_dev_ready;
420 static int atmel_hsmc_nand_dev_ready(struct mtd_info *mtd)
422 struct nand_chip *chip = mtd_to_nand(mtd);
423 struct atmel_nand *nand = to_atmel_nand(chip);
424 struct atmel_hsmc_nand_controller *nc;
427 nc = to_hsmc_nand_controller(nand->controller);
429 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &status);
431 return status & ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id);
434 static void atmel_hsmc_nand_select_chip(struct mtd_info *mtd, int cs)
436 struct nand_chip *chip = mtd_to_nand(mtd);
437 struct atmel_nand *nand = to_atmel_nand(chip);
438 struct atmel_hsmc_nand_controller *nc;
440 nc = to_hsmc_nand_controller(nand->controller);
442 atmel_nand_select_chip(mtd, cs);
444 if (!nand->activecs) {
445 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
446 ATMEL_HSMC_NFC_CTRL_DIS);
450 if (nand->activecs->rb.type == ATMEL_NAND_NATIVE_RB)
451 chip->dev_ready = atmel_hsmc_nand_dev_ready;
453 regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG,
454 ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK |
455 ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK |
456 ATMEL_HSMC_NFC_CFG_RSPARE |
457 ATMEL_HSMC_NFC_CFG_WSPARE,
458 ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) |
459 ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) |
460 ATMEL_HSMC_NFC_CFG_RSPARE);
461 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
462 ATMEL_HSMC_NFC_CTRL_EN);
465 static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll)
467 u8 *addrs = nc->op.addrs;
472 nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE;
474 for (i = 0; i < nc->op.ncmds; i++)
475 op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]);
477 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
478 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++);
480 op |= ATMEL_NFC_CSID(nc->op.cs) |
481 ATMEL_NFC_ACYCLE(nc->op.naddrs);
483 if (nc->op.ncmds > 1)
484 op |= ATMEL_NFC_VCMD2;
486 addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) |
489 if (nc->op.data != ATMEL_NFC_NO_DATA) {
490 op |= ATMEL_NFC_DATAEN;
491 nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE;
493 if (nc->op.data == ATMEL_NFC_WRITE_DATA)
494 op |= ATMEL_NFC_NFCWR;
497 /* Clear all flags. */
498 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val);
500 /* Send the command. */
501 regmap_write(nc->io, op, addr);
503 ret = atmel_nfc_wait(nc, poll, 0);
505 dev_err(nc->base.dev,
506 "Failed to send NAND command (err = %d)!",
509 /* Reset the op state. */
510 memset(&nc->op, 0, sizeof(nc->op));
515 static void atmel_hsmc_nand_cmd_ctrl(struct mtd_info *mtd, int dat,
518 struct nand_chip *chip = mtd_to_nand(mtd);
519 struct atmel_nand *nand = to_atmel_nand(chip);
520 struct atmel_hsmc_nand_controller *nc;
522 nc = to_hsmc_nand_controller(nand->controller);
524 if (ctrl & NAND_ALE) {
525 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
528 nc->op.addrs[nc->op.naddrs++] = dat;
529 } else if (ctrl & NAND_CLE) {
530 if (nc->op.ncmds > 1)
533 nc->op.cmds[nc->op.ncmds++] = dat;
536 if (dat == NAND_CMD_NONE) {
537 nc->op.cs = nand->activecs->id;
538 atmel_nfc_exec_op(nc, true);
542 static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
545 struct nand_chip *chip = mtd_to_nand(mtd);
546 struct atmel_nand *nand = to_atmel_nand(chip);
547 struct atmel_nand_controller *nc;
549 nc = to_nand_controller(nand->controller);
551 if ((ctrl & NAND_CTRL_CHANGE) &&
552 dm_gpio_is_valid(&nand->activecs->csgpio)) {
554 dm_gpio_set_value(&nand->activecs->csgpio, 0);
556 dm_gpio_set_value(&nand->activecs->csgpio, 1);
560 writeb(cmd, nand->activecs->io.virt + nc->caps->ale_offs);
561 else if (ctrl & NAND_CLE)
562 writeb(cmd, nand->activecs->io.virt + nc->caps->cle_offs);
565 static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf,
568 struct mtd_info *mtd = nand_to_mtd(chip);
569 struct atmel_nand *nand = to_atmel_nand(chip);
570 struct atmel_hsmc_nand_controller *nc;
573 nc = to_hsmc_nand_controller(nand->controller);
576 memcpy_toio(nc->sram.virt, buf, mtd->writesize);
579 memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi,
583 static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf,
586 struct mtd_info *mtd = nand_to_mtd(chip);
587 struct atmel_nand *nand = to_atmel_nand(chip);
588 struct atmel_hsmc_nand_controller *nc;
591 nc = to_hsmc_nand_controller(nand->controller);
594 memcpy_fromio(buf, nc->sram.virt, mtd->writesize);
597 memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize,
601 static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column)
603 struct mtd_info *mtd = nand_to_mtd(chip);
604 struct atmel_nand *nand = to_atmel_nand(chip);
605 struct atmel_hsmc_nand_controller *nc;
607 nc = to_hsmc_nand_controller(nand->controller);
610 nc->op.addrs[nc->op.naddrs++] = column;
613 * 2 address cycles for the column offset on large page NANDs.
615 if (mtd->writesize > 512)
616 nc->op.addrs[nc->op.naddrs++] = column >> 8;
620 nc->op.addrs[nc->op.naddrs++] = page;
621 nc->op.addrs[nc->op.naddrs++] = page >> 8;
623 if (chip->options & NAND_ROW_ADDR_3)
624 nc->op.addrs[nc->op.naddrs++] = page >> 16;
628 static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw)
630 struct atmel_nand *nand = to_atmel_nand(chip);
631 struct atmel_nand_controller *nc;
634 nc = to_nand_controller(nand->controller);
639 ret = atmel_pmecc_enable(nand->pmecc, op);
642 "Failed to enable ECC engine (err = %d)\n", ret);
647 static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw)
649 struct atmel_nand *nand = to_atmel_nand(chip);
652 atmel_pmecc_disable(nand->pmecc);
655 static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw)
657 struct atmel_nand *nand = to_atmel_nand(chip);
658 struct mtd_info *mtd = nand_to_mtd(chip);
659 struct atmel_nand_controller *nc;
660 struct mtd_oob_region oobregion;
664 nc = to_nand_controller(nand->controller);
669 ret = atmel_pmecc_wait_rdy(nand->pmecc);
672 "Failed to transfer NAND page data (err = %d)\n",
677 mtd_ooblayout_ecc(mtd, 0, &oobregion);
678 eccbuf = chip->oob_poi + oobregion.offset;
680 for (i = 0; i < chip->ecc.steps; i++) {
681 atmel_pmecc_get_generated_eccbytes(nand->pmecc, i,
683 eccbuf += chip->ecc.bytes;
689 static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf,
692 struct atmel_nand *nand = to_atmel_nand(chip);
693 struct mtd_info *mtd = nand_to_mtd(chip);
694 struct atmel_nand_controller *nc;
695 struct mtd_oob_region oobregion;
696 int ret, i, max_bitflips = 0;
697 void *databuf, *eccbuf;
699 nc = to_nand_controller(nand->controller);
704 ret = atmel_pmecc_wait_rdy(nand->pmecc);
707 "Failed to read NAND page data (err = %d)\n", ret);
711 mtd_ooblayout_ecc(mtd, 0, &oobregion);
712 eccbuf = chip->oob_poi + oobregion.offset;
715 for (i = 0; i < chip->ecc.steps; i++) {
716 ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf,
718 if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc))
719 ret = nand_check_erased_ecc_chunk(databuf,
727 max_bitflips = max(ret, max_bitflips);
729 mtd->ecc_stats.failed++;
731 databuf += chip->ecc.size;
732 eccbuf += chip->ecc.bytes;
738 static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf,
739 bool oob_required, int page, bool raw)
741 struct mtd_info *mtd = nand_to_mtd(chip);
742 struct atmel_nand *nand = to_atmel_nand(chip);
745 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
747 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
751 atmel_nand_write_buf(mtd, buf, mtd->writesize);
753 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
755 atmel_pmecc_disable(nand->pmecc);
759 atmel_nand_pmecc_disable(chip, raw);
761 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
763 return nand_prog_page_end_op(chip);
766 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
767 struct nand_chip *chip, const u8 *buf,
768 int oob_required, int page)
770 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
773 static int atmel_nand_pmecc_write_page_raw(struct mtd_info *mtd,
774 struct nand_chip *chip,
775 const u8 *buf, int oob_required,
778 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
781 static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
782 bool oob_required, int page, bool raw)
784 struct mtd_info *mtd = nand_to_mtd(chip);
787 nand_read_page_op(chip, page, 0, NULL, 0);
789 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
793 atmel_nand_read_buf(mtd, buf, mtd->writesize);
794 atmel_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
796 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
798 atmel_nand_pmecc_disable(chip, raw);
803 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
804 struct nand_chip *chip, u8 *buf,
805 int oob_required, int page)
807 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
810 static int atmel_nand_pmecc_read_page_raw(struct mtd_info *mtd,
811 struct nand_chip *chip, u8 *buf,
812 int oob_required, int page)
814 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true);
817 static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
818 const u8 *buf, bool oob_required,
821 struct mtd_info *mtd = nand_to_mtd(chip);
822 struct atmel_nand *nand = to_atmel_nand(chip);
823 struct atmel_hsmc_nand_controller *nc;
826 nc = to_hsmc_nand_controller(nand->controller);
828 atmel_nfc_copy_to_sram(chip, buf, false);
830 nc->op.cmds[0] = NAND_CMD_SEQIN;
832 atmel_nfc_set_op_addr(chip, page, 0x0);
833 nc->op.cs = nand->activecs->id;
834 nc->op.data = ATMEL_NFC_WRITE_DATA;
836 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
840 ret = atmel_nfc_exec_op(nc, true);
842 atmel_nand_pmecc_disable(chip, raw);
843 dev_err(nc->base.dev,
844 "Failed to transfer NAND page data (err = %d)\n",
849 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
851 atmel_nand_pmecc_disable(chip, raw);
856 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
858 nc->op.cmds[0] = NAND_CMD_PAGEPROG;
860 nc->op.cs = nand->activecs->id;
861 ret = atmel_nfc_exec_op(nc, true);
863 dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n",
866 status = chip->waitfunc(mtd, chip);
867 if (status & NAND_STATUS_FAIL)
874 atmel_hsmc_nand_pmecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
875 const u8 *buf, int oob_required,
878 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
883 atmel_hsmc_nand_pmecc_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
885 int oob_required, int page)
887 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
891 static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
892 bool oob_required, int page,
895 struct mtd_info *mtd = nand_to_mtd(chip);
896 struct atmel_nand *nand = to_atmel_nand(chip);
897 struct atmel_hsmc_nand_controller *nc;
900 nc = to_hsmc_nand_controller(nand->controller);
903 * Optimized read page accessors only work when the NAND R/B pin is
904 * connected to a native SoC R/B pin. If that's not the case, fallback
905 * to the non-optimized one.
907 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) {
908 nand_read_page_op(chip, page, 0, NULL, 0);
910 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
914 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
916 if (mtd->writesize > 512)
917 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
919 atmel_nfc_set_op_addr(chip, page, 0x0);
920 nc->op.cs = nand->activecs->id;
921 nc->op.data = ATMEL_NFC_READ_DATA;
923 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
927 ret = atmel_nfc_exec_op(nc, true);
929 atmel_nand_pmecc_disable(chip, raw);
930 dev_err(nc->base.dev,
931 "Failed to load NAND page data (err = %d)\n",
936 atmel_nfc_copy_from_sram(chip, buf, true);
938 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
940 atmel_nand_pmecc_disable(chip, raw);
945 static int atmel_hsmc_nand_pmecc_read_page(struct mtd_info *mtd,
946 struct nand_chip *chip, u8 *buf,
947 int oob_required, int page)
949 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
953 static int atmel_hsmc_nand_pmecc_read_page_raw(struct mtd_info *mtd,
954 struct nand_chip *chip,
955 u8 *buf, int oob_required,
958 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
962 static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
963 struct mtd_oob_region *oobregion)
965 struct nand_chip *chip = mtd_to_nand(mtd);
966 struct nand_ecc_ctrl *ecc = &chip->ecc;
968 if (section || !ecc->total)
971 oobregion->length = ecc->total;
972 oobregion->offset = mtd->oobsize - oobregion->length;
977 static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
978 struct mtd_oob_region *oobregion)
980 struct nand_chip *chip = mtd_to_nand(mtd);
981 struct nand_ecc_ctrl *ecc = &chip->ecc;
986 oobregion->length = mtd->oobsize - ecc->total - 2;
987 oobregion->offset = 2;
992 static const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
993 .ecc = nand_ooblayout_ecc_lp,
994 .rfree = nand_ooblayout_free_lp,
997 const struct mtd_ooblayout_ops *nand_get_large_page_ooblayout(void)
999 return &nand_ooblayout_lp_ops;
1002 static int atmel_nand_pmecc_init(struct nand_chip *chip)
1004 struct mtd_info *mtd = nand_to_mtd(chip);
1005 struct atmel_nand *nand = to_atmel_nand(chip);
1006 struct atmel_nand_controller *nc;
1007 struct atmel_pmecc_user_req req;
1009 nc = to_nand_controller(nand->controller);
1012 dev_err(nc->dev, "HW ECC not supported\n");
1016 if (nc->caps->legacy_of_bindings) {
1019 if (!ofnode_read_u32(nc->dev->node_, "atmel,pmecc-cap", &val))
1020 chip->ecc.strength = val;
1022 if (!ofnode_read_u32(nc->dev->node_,
1023 "atmel,pmecc-sector-size",
1025 chip->ecc.size = val;
1028 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
1029 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1030 else if (chip->ecc.strength)
1031 req.ecc.strength = chip->ecc.strength;
1033 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1036 req.ecc.sectorsize = chip->ecc.size;
1038 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1040 req.pagesize = mtd->writesize;
1041 req.oobsize = mtd->oobsize;
1043 if (mtd->writesize <= 512) {
1045 req.ecc.ooboffset = 0;
1047 req.ecc.bytes = mtd->oobsize - 2;
1048 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1051 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1052 if (IS_ERR(nand->pmecc))
1053 return PTR_ERR(nand->pmecc);
1055 chip->ecc.algo = NAND_ECC_BCH;
1056 chip->ecc.size = req.ecc.sectorsize;
1057 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1058 chip->ecc.strength = req.ecc.strength;
1060 chip->options |= NAND_NO_SUBPAGE_WRITE;
1062 mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1063 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1066 chip->ecc.layout = &atmel_pmecc_oobinfo;
1071 static int atmel_nand_ecc_init(struct nand_chip *chip)
1073 struct atmel_nand_controller *nc;
1074 struct atmel_nand *nand = to_atmel_nand(chip);
1077 nc = to_nand_controller(nand->controller);
1079 switch (chip->ecc.mode) {
1083 * Nothing to do, the core will initialize everything for us.
1088 ret = atmel_nand_pmecc_init(chip);
1092 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1093 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1094 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1095 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1099 /* Other modes are not supported. */
1100 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1108 static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip)
1112 ret = atmel_nand_ecc_init(chip);
1116 if (chip->ecc.mode != NAND_ECC_HW)
1119 /* Adjust the ECC operations for the HSMC IP. */
1120 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1121 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1122 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1123 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
1128 static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1129 const struct nand_data_interface *conf,
1130 struct atmel_smc_cs_conf *smcconf)
1132 u32 ncycles, totalcycles, timeps, mckperiodps;
1133 struct atmel_nand_controller *nc;
1136 nc = to_nand_controller(nand->controller);
1138 /* DDR interface not supported. */
1139 if (conf->type != NAND_SDR_IFACE)
1143 * tRC < 30ns implies EDO mode. This controller does not support this
1146 if (conf->timings.sdr.tRC_min < 30000)
1149 atmel_smc_cs_conf_init(smcconf);
1151 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1152 mckperiodps *= 1000;
1155 * Set write pulse timing. This one is easy to extract:
1159 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1160 totalcycles = ncycles;
1161 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1167 * The write setup timing depends on the operation done on the NAND.
1168 * All operations goes through the same data bus, but the operation
1169 * type depends on the address we are writing to (ALE/CLE address
1171 * Since we have no way to differentiate the different operations at
1172 * the SMC level, we must consider the worst case (the biggest setup
1173 * time among all operation types):
1175 * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE
1177 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1178 conf->timings.sdr.tALS_min);
1179 timeps = max(timeps, conf->timings.sdr.tDS_min);
1180 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1181 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1182 totalcycles += ncycles;
1183 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1189 * As for the write setup timing, the write hold timing depends on the
1190 * operation done on the NAND:
1192 * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH)
1194 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1195 conf->timings.sdr.tALH_min);
1196 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1197 conf->timings.sdr.tWH_min);
1198 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1199 totalcycles += ncycles;
1202 * The write cycle timing is directly matching tWC, but is also
1203 * dependent on the other timings on the setup and hold timings we
1204 * calculated earlier, which gives:
1206 * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD)
1208 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1209 ncycles = max(totalcycles, ncycles);
1210 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1216 * We don't want the CS line to be toggled between each byte/word
1217 * transfer to the NAND. The only way to guarantee that is to have the
1218 * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1220 * NCS_WR_PULSE = NWE_CYCLE
1222 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1228 * As for the write setup timing, the read hold timing depends on the
1229 * operation done on the NAND:
1231 * NRD_HOLD = max(tREH, tRHOH)
1233 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1234 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1235 totalcycles = ncycles;
1238 * TDF = tRHZ - NRD_HOLD
1240 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1241 ncycles -= totalcycles;
1244 * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and
1245 * we might end up with a config that does not fit in the TDF field.
1246 * Just take the max value in this case and hope that the NAND is more
1247 * tolerant than advertised.
1249 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1250 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1251 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1252 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1254 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1255 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1258 * Read pulse timing directly matches tRP:
1262 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1263 totalcycles += ncycles;
1264 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1270 * The write cycle timing is directly matching tWC, but is also
1271 * dependent on the setup and hold timings we calculated earlier,
1274 * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD)
1276 * NRD_SETUP is always 0.
1278 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1279 ncycles = max(totalcycles, ncycles);
1280 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1286 * We don't want the CS line to be toggled between each byte/word
1287 * transfer from the NAND. The only way to guarantee that is to have
1288 * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1290 * NCS_RD_PULSE = NRD_CYCLE
1292 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1297 /* Txxx timings are directly matching tXXX ones. */
1298 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1299 ret = atmel_smc_cs_conf_set_timing(smcconf,
1300 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1305 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1306 ret = atmel_smc_cs_conf_set_timing(smcconf,
1307 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1310 * Version 4 of the ONFI spec mandates that tADL be at least 400
1311 * nanoseconds, but, depending on the master clock rate, 400 ns may not
1312 * fit in the tADL field of the SMC reg. We need to relax the check and
1313 * accept the -ERANGE return code.
1315 * Note that previous versions of the ONFI spec had a lower tADL_min
1316 * (100 or 200 ns). It's not clear why this timing constraint got
1317 * increased but it seems most NANDs are fine with values lower than
1318 * 400ns, so we should be safe.
1320 if (ret && ret != -ERANGE)
1323 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1324 ret = atmel_smc_cs_conf_set_timing(smcconf,
1325 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1330 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1331 ret = atmel_smc_cs_conf_set_timing(smcconf,
1332 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1337 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1338 ret = atmel_smc_cs_conf_set_timing(smcconf,
1339 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1344 /* Attach the CS line to the NFC logic. */
1345 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1347 /* Set the appropriate data bus width. */
1348 if (nand->base.options & NAND_BUSWIDTH_16)
1349 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1351 /* Operate in NRD/NWE READ/WRITEMODE. */
1352 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1353 ATMEL_SMC_MODE_WRITEMODE_NWE;
1359 atmel_smc_nand_setup_data_interface(struct atmel_nand *nand,
1361 const struct nand_data_interface *conf)
1363 struct atmel_nand_controller *nc;
1364 struct atmel_smc_cs_conf smcconf;
1365 struct atmel_nand_cs *cs;
1368 nc = to_nand_controller(nand->controller);
1370 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1374 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1377 cs = &nand->cs[csline];
1378 cs->smcconf = smcconf;
1380 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1386 atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand,
1388 const struct nand_data_interface *conf)
1390 struct atmel_hsmc_nand_controller *nc;
1391 struct atmel_smc_cs_conf smcconf;
1392 struct atmel_nand_cs *cs;
1395 nc = to_hsmc_nand_controller(nand->controller);
1397 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1401 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1404 cs = &nand->cs[csline];
1405 cs->smcconf = smcconf;
1407 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1408 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1410 atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id,
1416 static int atmel_nand_setup_data_interface(struct mtd_info *mtd, int csline,
1417 const struct nand_data_interface *conf)
1419 struct nand_chip *chip = mtd_to_nand(mtd);
1420 struct atmel_nand *nand = to_atmel_nand(chip);
1421 struct atmel_nand_controller *nc;
1423 nc = to_nand_controller(nand->controller);
1425 if (csline >= nand->numcs ||
1426 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1429 return nc->caps->ops->setup_data_interface(nand, csline, conf);
1432 #define NAND_KEEP_TIMINGS 0x00800000
1434 static void atmel_nand_init(struct atmel_nand_controller *nc,
1435 struct atmel_nand *nand)
1437 struct nand_chip *chip = &nand->base;
1438 struct mtd_info *mtd = nand_to_mtd(chip);
1440 mtd->dev->parent = nc->dev;
1441 nand->controller = &nc->base;
1443 chip->cmd_ctrl = atmel_nand_cmd_ctrl;
1444 chip->read_byte = atmel_nand_read_byte;
1445 chip->write_byte = atmel_nand_write_byte;
1446 chip->read_buf = atmel_nand_read_buf;
1447 chip->write_buf = atmel_nand_write_buf;
1448 chip->select_chip = atmel_nand_select_chip;
1449 chip->setup_data_interface = atmel_nand_setup_data_interface;
1451 if (!nc->mck || !nc->caps->ops->setup_data_interface)
1452 chip->options |= NAND_KEEP_TIMINGS;
1454 /* Some NANDs require a longer delay than the default one (20us). */
1455 chip->chip_delay = 40;
1457 /* Default to HW ECC if pmecc is available. */
1459 chip->ecc.mode = NAND_ECC_HW;
1462 static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1463 struct atmel_nand *nand)
1465 struct atmel_smc_nand_controller *smc_nc;
1468 atmel_nand_init(nc, nand);
1470 smc_nc = to_smc_nand_controller(nand->controller);
1471 if (!smc_nc->ebi_csa_regmap)
1474 /* Attach the CS to the NAND Flash logic. */
1475 for (i = 0; i < nand->numcs; i++)
1476 regmap_update_bits(smc_nc->ebi_csa_regmap,
1477 smc_nc->ebi_csa->offs,
1478 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1480 if (smc_nc->ebi_csa->nfd0_on_d16)
1481 regmap_update_bits(smc_nc->ebi_csa_regmap,
1482 smc_nc->ebi_csa->offs,
1483 smc_nc->ebi_csa->nfd0_on_d16,
1484 smc_nc->ebi_csa->nfd0_on_d16);
1487 static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc,
1488 struct atmel_nand *nand)
1490 struct nand_chip *chip = &nand->base;
1492 atmel_nand_init(nc, nand);
1494 /* Overload some methods for the HSMC controller. */
1495 chip->cmd_ctrl = atmel_hsmc_nand_cmd_ctrl;
1496 chip->select_chip = atmel_hsmc_nand_select_chip;
1499 static int atmel_nand_controller_remove_nand(struct atmel_nand *nand)
1501 list_del(&nand->node);
1506 static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1510 struct atmel_nand *nand;
1518 /* Count num of nand nodes */
1519 ofnode_for_each_subnode(n, ofnode_get_parent(np))
1522 dev_err(nc->dev, "Missing or invalid reg property\n");
1523 return ERR_PTR(-EINVAL);
1526 nand = devm_kzalloc(nc->dev,
1527 sizeof(struct atmel_nand) +
1528 (numcs * sizeof(struct atmel_nand_cs)),
1531 dev_err(nc->dev, "Failed to allocate NAND object\n");
1532 return ERR_PTR(-ENOMEM);
1535 nand->numcs = numcs;
1537 gpio_request_by_name_nodev(np, "det-gpios", 0, &nand->cdgpio,
1540 for (i = 0; i < numcs; i++) {
1541 ret = ofnode_read_u32(np, "reg", &val);
1543 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1545 return ERR_PTR(ret);
1547 nand->cs[i].id = val;
1549 /* Read base address */
1550 struct resource res;
1552 if (ofnode_read_resource(np, 0, &res)) {
1553 dev_err(nc->dev, "Unable to read resource\n");
1554 return ERR_PTR(-ENOMEM);
1557 faddr = cpu_to_fdt32(val);
1558 base = ofnode_translate_address(np, &faddr);
1559 nand->cs[i].io.virt = (void *)base;
1561 if (!ofnode_read_u32(np, "atmel,rb", &val)) {
1562 if (val > ATMEL_NFC_MAX_RB_ID)
1563 return ERR_PTR(-EINVAL);
1565 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1566 nand->cs[i].rb.id = val;
1568 ret = gpio_request_by_name_nodev(np, "rb-gpios", 0,
1569 &nand->cs[i].rb.gpio,
1571 if (ret && ret != -ENOENT)
1572 dev_err(nc->dev, "Failed to get R/B gpio (err = %d)\n", ret);
1574 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1577 gpio_request_by_name_nodev(np, "cs-gpios", 0,
1578 &nand->cs[i].csgpio,
1582 nand_set_flash_node(&nand->base, np);
1587 static int nand_attach(struct nand_chip *chip)
1589 struct atmel_nand *nand = to_atmel_nand(chip);
1591 if (nand->controller->ops && nand->controller->ops->attach_chip)
1592 return nand->controller->ops->attach_chip(chip);
1597 int atmel_nand_scan(struct mtd_info *mtd, int maxchips)
1601 ret = nand_scan_ident(mtd, maxchips, NULL);
1605 ret = nand_attach(mtd_to_nand(mtd));
1609 ret = nand_scan_tail(mtd);
1614 atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1615 struct atmel_nand *nand)
1617 struct nand_chip *chip = &nand->base;
1618 struct mtd_info *mtd = nand_to_mtd(chip);
1621 /* No card inserted, skip this NAND. */
1622 if (dm_gpio_is_valid(&nand->cdgpio) &&
1623 dm_gpio_get_value(&nand->cdgpio)) {
1624 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1628 nc->caps->ops->nand_init(nc, nand);
1630 ret = atmel_nand_scan(mtd, nand->numcs);
1632 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
1636 ret = nand_register(0, mtd);
1638 dev_err(nc->dev, "nand register failed: %d\n", ret);
1642 list_add_tail(&nand->node, &nc->chips);
1648 atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1650 struct atmel_nand *nand, *tmp;
1653 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
1654 ret = atmel_nand_controller_remove_nand(nand);
1662 static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1670 * Add support for legacy nands
1673 np = nc->dev->node_;
1675 ret = ofnode_read_u32(np, "#address-cells", &val);
1677 dev_err(nc->dev, "missing #address-cells property\n");
1683 ret = ofnode_read_u32(np, "#size-cells", &val);
1685 dev_err(nc->dev, "missing #size-cells property\n");
1691 ofnode_for_each_subnode(nand_np, np) {
1692 struct atmel_nand *nand;
1694 nand = atmel_nand_create(nc, nand_np, reg_cells);
1696 ret = PTR_ERR(nand);
1700 ret = atmel_nand_controller_add_nand(nc, nand);
1708 atmel_nand_controller_remove_nands(nc);
1713 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9260_ebi_csa = {
1714 .offs = AT91SAM9260_MATRIX_EBICSA,
1717 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9261_ebi_csa = {
1718 .offs = AT91SAM9261_MATRIX_EBICSA,
1721 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9263_ebi_csa = {
1722 .offs = AT91SAM9263_MATRIX_EBI0CSA,
1725 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9rl_ebi_csa = {
1726 .offs = AT91SAM9RL_MATRIX_EBICSA,
1729 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9g45_ebi_csa = {
1730 .offs = AT91SAM9G45_MATRIX_EBICSA,
1733 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9n12_ebi_csa = {
1734 .offs = AT91SAM9N12_MATRIX_EBICSA,
1737 static const struct atmel_smc_nand_ebi_csa_cfg at91sam9x5_ebi_csa = {
1738 .offs = AT91SAM9X5_MATRIX_EBICSA,
1741 static const struct atmel_smc_nand_ebi_csa_cfg sam9x60_ebi_csa = {
1742 .offs = AT91_SFR_CCFG_EBICSA,
1743 .nfd0_on_d16 = AT91_SFR_CCFG_NFD0_ON_D16,
1746 static const struct udevice_id atmel_ebi_csa_regmap_of_ids[] = {
1748 .compatible = "atmel,at91sam9260-matrix",
1749 .data = (ulong)&at91sam9260_ebi_csa,
1752 .compatible = "atmel,at91sam9261-matrix",
1753 .data = (ulong)&at91sam9261_ebi_csa,
1756 .compatible = "atmel,at91sam9263-matrix",
1757 .data = (ulong)&at91sam9263_ebi_csa,
1760 .compatible = "atmel,at91sam9rl-matrix",
1761 .data = (ulong)&at91sam9rl_ebi_csa,
1764 .compatible = "atmel,at91sam9g45-matrix",
1765 .data = (ulong)&at91sam9g45_ebi_csa,
1768 .compatible = "atmel,at91sam9n12-matrix",
1769 .data = (ulong)&at91sam9n12_ebi_csa,
1772 .compatible = "atmel,at91sam9x5-matrix",
1773 .data = (ulong)&at91sam9x5_ebi_csa,
1776 .compatible = "microchip,sam9x60-sfr",
1777 .data = (ulong)&sam9x60_ebi_csa,
1782 static int atmel_nand_attach_chip(struct nand_chip *chip)
1784 struct atmel_nand *nand = to_atmel_nand(chip);
1785 struct atmel_nand_controller *nc = to_nand_controller(nand->controller);
1786 struct mtd_info *mtd = nand_to_mtd(chip);
1789 ret = nc->caps->ops->ecc_init(chip);
1793 if (nc->caps->legacy_of_bindings || !ofnode_valid(nc->dev->node_)) {
1795 * We keep the MTD name unchanged to avoid breaking platforms
1796 * where the MTD cmdline parser is used and the bootloader
1797 * has not been updated to use the new naming scheme.
1799 mtd->name = "atmel_nand";
1800 } else if (!mtd->name) {
1802 * If the new bindings are used and the bootloader has not been
1803 * updated to pass a new mtdparts parameter on the cmdline, you
1804 * should define the following property in your nand node:
1806 * label = "atmel_nand";
1808 * This way, mtd->name will be set by the core when
1809 * nand_set_flash_node() is called.
1811 sprintf(mtd->name, "%s:nand.%d", nc->dev->name, nand->cs[0].id);
1817 static const struct nand_controller_ops atmel_nand_controller_ops = {
1818 .attach_chip = atmel_nand_attach_chip,
1822 atmel_nand_controller_init(struct atmel_nand_controller *nc,
1823 struct udevice *dev,
1824 const struct atmel_nand_controller_caps *caps)
1826 struct ofnode_phandle_args args;
1829 nc->base.ops = &atmel_nand_controller_ops;
1830 INIT_LIST_HEAD(&nc->chips);
1834 nc->pmecc = devm_atmel_pmecc_get(dev);
1835 if (IS_ERR(nc->pmecc)) {
1836 ret = PTR_ERR(nc->pmecc);
1837 if (ret != -EPROBE_DEFER)
1838 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1843 /* We do not retrieve the SMC syscon when parsing old DTs. */
1844 if (nc->caps->legacy_of_bindings)
1847 nc->mck = devm_kzalloc(dev, sizeof(nc->mck), GFP_KERNEL);
1851 clk_get_by_index(dev->parent, 0, nc->mck);
1852 if (IS_ERR(nc->mck)) {
1853 dev_err(dev, "Failed to retrieve MCK clk\n");
1854 return PTR_ERR(nc->mck);
1857 ret = ofnode_parse_phandle_with_args(dev->parent->node_,
1858 "atmel,smc", NULL, 0, 0, &args);
1860 dev_err(dev, "Missing or invalid atmel,smc property\n");
1864 nc->smc = syscon_node_to_regmap(args.node);
1865 if (IS_ERR(nc->smc)) {
1866 ret = PTR_ERR(nc->smc);
1867 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
1875 atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
1877 struct udevice *dev = nc->base.dev;
1878 struct ofnode_phandle_args args;
1879 const struct udevice_id *match = NULL;
1885 /* We do not retrieve the EBICSA regmap when parsing old DTs. */
1886 if (nc->base.caps->legacy_of_bindings)
1889 ret = ofnode_parse_phandle_with_args(dev->parent->node_,
1890 nc->base.caps->ebi_csa_regmap_name,
1893 dev_err(dev, "Unable to read ebi csa regmap\n");
1897 name = ofnode_get_property(args.node, "compatible", &len);
1899 for (i = 0; i < ARRAY_SIZE(atmel_ebi_csa_regmap_of_ids); i++) {
1900 if (!strcmp(name, atmel_ebi_csa_regmap_of_ids[i].compatible)) {
1901 match = &atmel_ebi_csa_regmap_of_ids[i];
1907 dev_err(dev, "Unable to find ebi csa conf");
1910 nc->ebi_csa = (struct atmel_smc_nand_ebi_csa_cfg *)match->data;
1912 nc->ebi_csa_regmap = syscon_node_to_regmap(args.node);
1913 if (IS_ERR(nc->ebi_csa_regmap)) {
1914 ret = PTR_ERR(nc->ebi_csa_regmap);
1915 dev_err(dev, "Could not get EBICSA regmap (err = %d)\n", ret);
1920 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
1921 * add 4 to ->ebi_csa->offs.
1927 static int atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
1929 struct udevice *dev = nc->base.dev;
1930 struct ofnode_phandle_args args;
1935 ret = ofnode_parse_phandle_with_args(dev->parent->node_,
1936 "atmel,smc", NULL, 0, 0, &args);
1938 dev_err(dev, "Missing or invalid atmel,smc property\n");
1942 nc->hsmc_layout = atmel_hsmc_get_reg_layout(args.node);
1943 if (IS_ERR(nc->hsmc_layout)) {
1944 dev_err(dev, "Could not get hsmc layout\n");
1948 /* Enable smc clock */
1949 ret = clk_get_by_index_nodev(args.node, 0, &smc_clk);
1951 dev_err(dev, "Unable to get smc clock (err = %d)", ret);
1955 ret = clk_prepare_enable(&smc_clk);
1959 ret = ofnode_parse_phandle_with_args(dev->node_,
1960 "atmel,nfc-io", NULL, 0, 0, &args);
1962 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
1966 nc->io = syscon_node_to_regmap(args.node);
1967 if (IS_ERR(nc->io)) {
1968 ret = PTR_ERR(nc->io);
1969 dev_err(dev, "Could not get NFC IO regmap\n");
1973 ret = ofnode_parse_phandle_with_args(dev->node_,
1974 "atmel,nfc-sram", NULL, 0, 0, &args);
1976 dev_err(dev, "Missing or invalid atmel,nfc-sram property\n");
1980 ret = ofnode_read_u32(args.node, "reg", &addr);
1982 dev_err(dev, "Could not read reg addr of nfc sram");
1985 nc->sram.virt = (void *)addr;
1991 atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
1993 struct atmel_hsmc_nand_controller *hsmc_nc;
1996 ret = atmel_nand_controller_remove_nands(nc);
2000 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2003 clk_disable_unprepare(hsmc_nc->clk);
2004 devm_clk_put(nc->dev, hsmc_nc->clk);
2011 atmel_hsmc_nand_controller_probe(struct udevice *dev,
2012 const struct atmel_nand_controller_caps *caps)
2014 struct atmel_hsmc_nand_controller *nc;
2017 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2021 ret = atmel_nand_controller_init(&nc->base, dev, caps);
2025 ret = atmel_hsmc_nand_controller_init(nc);
2029 /* Make sure all irqs are masked before registering our IRQ handler. */
2030 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2032 /* Initial NFC configuration. */
2033 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2034 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2036 ret = atmel_nand_controller_add_nands(&nc->base);
2043 atmel_hsmc_nand_controller_remove(&nc->base);
2048 static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2049 .probe = atmel_hsmc_nand_controller_probe,
2050 .remove = atmel_hsmc_nand_controller_remove,
2051 .ecc_init = atmel_hsmc_nand_ecc_init,
2052 .nand_init = atmel_hsmc_nand_init,
2053 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
2056 static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2058 .ale_offs = BIT(21),
2059 .cle_offs = BIT(22),
2060 .ops = &atmel_hsmc_nc_ops,
2064 atmel_smc_nand_controller_probe(struct udevice *dev,
2065 const struct atmel_nand_controller_caps *caps)
2067 struct atmel_smc_nand_controller *nc;
2070 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2074 ret = atmel_nand_controller_init(&nc->base, dev, caps);
2078 ret = atmel_smc_nand_controller_init(nc);
2082 return atmel_nand_controller_add_nands(&nc->base);
2086 atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2090 ret = atmel_nand_controller_remove_nands(nc);
2098 * The SMC reg layout of at91rm9200 is completely different which prevents us
2099 * from re-using atmel_smc_nand_setup_data_interface() for the
2100 * ->setup_data_interface() hook.
2101 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2102 * ->setup_data_interface() unassigned.
2104 static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
2105 .probe = atmel_smc_nand_controller_probe,
2106 .remove = atmel_smc_nand_controller_remove,
2107 .ecc_init = atmel_nand_ecc_init,
2108 .nand_init = atmel_smc_nand_init,
2111 static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2112 .ale_offs = BIT(21),
2113 .cle_offs = BIT(22),
2114 .ebi_csa_regmap_name = "atmel,matrix",
2115 .ops = &at91rm9200_nc_ops,
2118 static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2119 .probe = atmel_smc_nand_controller_probe,
2120 .remove = atmel_smc_nand_controller_remove,
2121 .ecc_init = atmel_nand_ecc_init,
2122 .nand_init = atmel_smc_nand_init,
2123 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2126 static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2127 .ale_offs = BIT(21),
2128 .cle_offs = BIT(22),
2129 .ebi_csa_regmap_name = "atmel,matrix",
2130 .ops = &atmel_smc_nc_ops,
2133 static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2134 .ale_offs = BIT(22),
2135 .cle_offs = BIT(21),
2136 .ebi_csa_regmap_name = "atmel,matrix",
2137 .ops = &atmel_smc_nc_ops,
2140 static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2142 .ale_offs = BIT(21),
2143 .cle_offs = BIT(22),
2144 .ebi_csa_regmap_name = "atmel,matrix",
2145 .ops = &atmel_smc_nc_ops,
2148 static const struct atmel_nand_controller_caps microchip_sam9x60_nc_caps = {
2150 .ale_offs = BIT(21),
2151 .cle_offs = BIT(22),
2152 .ebi_csa_regmap_name = "microchip,sfr",
2153 .ops = &atmel_smc_nc_ops,
2156 /* Only used to parse old bindings. */
2157 static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2158 .ale_offs = BIT(21),
2159 .cle_offs = BIT(22),
2160 .ops = &atmel_smc_nc_ops,
2161 .legacy_of_bindings = true,
2164 static const struct udevice_id atmel_nand_controller_of_ids[] = {
2166 .compatible = "atmel,at91rm9200-nand-controller",
2167 .data = (ulong)&atmel_rm9200_nc_caps,
2170 .compatible = "atmel,at91sam9260-nand-controller",
2171 .data = (ulong)&atmel_sam9260_nc_caps,
2174 .compatible = "atmel,at91sam9261-nand-controller",
2175 .data = (ulong)&atmel_sam9261_nc_caps,
2178 .compatible = "atmel,at91sam9g45-nand-controller",
2179 .data = (ulong)&atmel_sam9g45_nc_caps,
2182 .compatible = "atmel,sama5d3-nand-controller",
2183 .data = (ulong)&atmel_sama5_nc_caps,
2186 .compatible = "microchip,sam9x60-nand-controller",
2187 .data = (ulong)µchip_sam9x60_nc_caps,
2189 /* Support for old/deprecated bindings: */
2191 .compatible = "atmel,at91rm9200-nand",
2192 .data = (ulong)&atmel_rm9200_nand_caps,
2195 .compatible = "atmel,sama5d4-nand",
2196 .data = (ulong)&atmel_rm9200_nand_caps,
2199 .compatible = "atmel,sama5d2-nand",
2200 .data = (ulong)&atmel_rm9200_nand_caps,
2205 static int atmel_nand_controller_probe(struct udevice *dev)
2207 const struct atmel_nand_controller_caps *caps;
2208 struct udevice *pmecc_dev;
2210 caps = (struct atmel_nand_controller_caps *)dev_get_driver_data(dev);
2212 printf("Could not retrieve NFC caps\n");
2216 /* Probe pmecc driver */
2217 if (uclass_get_device(UCLASS_MTD, 1, &pmecc_dev)) {
2218 printf("%s: get device fail\n", __func__);
2222 return caps->ops->probe(dev, caps);
2225 static int atmel_nand_controller_remove(struct udevice *dev)
2227 struct atmel_nand_controller *nc;
2229 nc = (struct atmel_nand_controller *)dev_get_driver_data(dev);
2231 return nc->caps->ops->remove(nc);
2234 U_BOOT_DRIVER(atmel_nand_controller) = {
2235 .name = "atmel-nand-controller",
2237 .of_match = atmel_nand_controller_of_ids,
2238 .probe = atmel_nand_controller_probe,
2239 .remove = atmel_nand_controller_remove,
2242 void board_nand_init(void)
2244 struct udevice *dev;
2247 ret = uclass_get_device_by_driver(UCLASS_MTD,
2248 DM_DRIVER_GET(atmel_nand_controller),
2250 if (ret && ret != -ENODEV)
2251 printf("Failed to initialize NAND controller. (error %d)\n",