3 * Copyright 2017 Free Electrons
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Derived from the atmel_nand.c driver which contained the following
10 * Copyright 2003 Rick Bronson
12 * Derived from drivers/mtd/nand/autcpu12.c
13 * Copyright 2001 Thomas Gleixner (gleixner@autronix.de)
15 * Derived from drivers/mtd/spia.c
16 * Copyright 2000 Steven J. Hill (sjhill@cotw.com)
19 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
20 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright 2007
22 * Derived from Das U-Boot source code
23 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
24 * Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
26 * Add Programmable Multibit ECC support for various AT91 SoC
27 * Copyright 2012 ATMEL, Hong Xu
29 * Add Nand Flash Controller support for SAMA5 SoC
30 * Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License version 2 as
34 * published by the Free Software Foundation.
36 * A few words about the naming convention in this file. This convention
37 * applies to structure and function names.
41 * - atmel_nand_: all generic structures/functions
42 * - atmel_smc_nand_: all structures/functions specific to the SMC interface
43 * (at91sam9 and avr32 SoCs)
44 * - atmel_hsmc_nand_: all structures/functions specific to the HSMC interface
45 * (sama5 SoCs and later)
46 * - atmel_nfc_: all structures/functions used to manipulate the NFC sub-block
47 * that is available in the HSMC block
48 * - <soc>_nand_: all SoC specific structures/functions
51 #include <linux/clk.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/dmaengine.h>
54 #include <linux/genalloc.h>
55 #include <linux/gpio.h>
56 #include <linux/gpio/consumer.h>
57 #include <linux/interrupt.h>
58 #include <linux/mfd/syscon.h>
59 #include <linux/mfd/syscon/atmel-matrix.h>
60 #include <linux/mfd/syscon/atmel-smc.h>
61 #include <linux/module.h>
62 #include <linux/mtd/nand.h>
63 #include <linux/of_address.h>
64 #include <linux/of_irq.h>
65 #include <linux/of_platform.h>
66 #include <linux/iopoll.h>
67 #include <linux/platform_device.h>
68 #include <linux/platform_data/atmel.h>
69 #include <linux/regmap.h>
73 #define ATMEL_HSMC_NFC_CFG 0x0
74 #define ATMEL_HSMC_NFC_CFG_SPARESIZE(x) (((x) / 4) << 24)
75 #define ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK GENMASK(30, 24)
76 #define ATMEL_HSMC_NFC_CFG_DTO(cyc, mul) (((cyc) << 16) | ((mul) << 20))
77 #define ATMEL_HSMC_NFC_CFG_DTO_MAX GENMASK(22, 16)
78 #define ATMEL_HSMC_NFC_CFG_RBEDGE BIT(13)
79 #define ATMEL_HSMC_NFC_CFG_FALLING_EDGE BIT(12)
80 #define ATMEL_HSMC_NFC_CFG_RSPARE BIT(9)
81 #define ATMEL_HSMC_NFC_CFG_WSPARE BIT(8)
82 #define ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK GENMASK(2, 0)
83 #define ATMEL_HSMC_NFC_CFG_PAGESIZE(x) (fls((x) / 512) - 1)
85 #define ATMEL_HSMC_NFC_CTRL 0x4
86 #define ATMEL_HSMC_NFC_CTRL_EN BIT(0)
87 #define ATMEL_HSMC_NFC_CTRL_DIS BIT(1)
89 #define ATMEL_HSMC_NFC_SR 0x8
90 #define ATMEL_HSMC_NFC_IER 0xc
91 #define ATMEL_HSMC_NFC_IDR 0x10
92 #define ATMEL_HSMC_NFC_IMR 0x14
93 #define ATMEL_HSMC_NFC_SR_ENABLED BIT(1)
94 #define ATMEL_HSMC_NFC_SR_RB_RISE BIT(4)
95 #define ATMEL_HSMC_NFC_SR_RB_FALL BIT(5)
96 #define ATMEL_HSMC_NFC_SR_BUSY BIT(8)
97 #define ATMEL_HSMC_NFC_SR_WR BIT(11)
98 #define ATMEL_HSMC_NFC_SR_CSID GENMASK(14, 12)
99 #define ATMEL_HSMC_NFC_SR_XFRDONE BIT(16)
100 #define ATMEL_HSMC_NFC_SR_CMDDONE BIT(17)
101 #define ATMEL_HSMC_NFC_SR_DTOE BIT(20)
102 #define ATMEL_HSMC_NFC_SR_UNDEF BIT(21)
103 #define ATMEL_HSMC_NFC_SR_AWB BIT(22)
104 #define ATMEL_HSMC_NFC_SR_NFCASE BIT(23)
105 #define ATMEL_HSMC_NFC_SR_ERRORS (ATMEL_HSMC_NFC_SR_DTOE | \
106 ATMEL_HSMC_NFC_SR_UNDEF | \
107 ATMEL_HSMC_NFC_SR_AWB | \
108 ATMEL_HSMC_NFC_SR_NFCASE)
109 #define ATMEL_HSMC_NFC_SR_RBEDGE(x) BIT((x) + 24)
111 #define ATMEL_HSMC_NFC_ADDR 0x18
112 #define ATMEL_HSMC_NFC_BANK 0x1c
114 #define ATMEL_NFC_MAX_RB_ID 7
116 #define ATMEL_NFC_SRAM_SIZE 0x2400
118 #define ATMEL_NFC_CMD(pos, cmd) ((cmd) << (((pos) * 8) + 2))
119 #define ATMEL_NFC_VCMD2 BIT(18)
120 #define ATMEL_NFC_ACYCLE(naddrs) ((naddrs) << 19)
121 #define ATMEL_NFC_CSID(cs) ((cs) << 22)
122 #define ATMEL_NFC_DATAEN BIT(25)
123 #define ATMEL_NFC_NFCWR BIT(26)
125 #define ATMEL_NFC_MAX_ADDR_CYCLES 5
127 #define ATMEL_NAND_ALE_OFFSET BIT(21)
128 #define ATMEL_NAND_CLE_OFFSET BIT(22)
130 #define DEFAULT_TIMEOUT_MS 1000
131 #define MIN_DMA_LEN 128
133 enum atmel_nand_rb_type {
135 ATMEL_NAND_NATIVE_RB,
139 struct atmel_nand_rb {
140 enum atmel_nand_rb_type type;
142 struct gpio_desc *gpio;
147 struct atmel_nand_cs {
149 struct atmel_nand_rb rb;
150 struct gpio_desc *csgpio;
156 struct atmel_smc_cs_conf smcconf;
160 struct list_head node;
162 struct nand_chip base;
163 struct atmel_nand_cs *activecs;
164 struct atmel_pmecc_user *pmecc;
165 struct gpio_desc *cdgpio;
167 struct atmel_nand_cs cs[];
170 static inline struct atmel_nand *to_atmel_nand(struct nand_chip *chip)
172 return container_of(chip, struct atmel_nand, base);
175 enum atmel_nfc_data_xfer {
178 ATMEL_NFC_WRITE_DATA,
181 struct atmel_nfc_op {
187 enum atmel_nfc_data_xfer data;
192 struct atmel_nand_controller;
193 struct atmel_nand_controller_caps;
195 struct atmel_nand_controller_ops {
196 int (*probe)(struct platform_device *pdev,
197 const struct atmel_nand_controller_caps *caps);
198 int (*remove)(struct atmel_nand_controller *nc);
199 void (*nand_init)(struct atmel_nand_controller *nc,
200 struct atmel_nand *nand);
201 int (*ecc_init)(struct atmel_nand *nand);
202 int (*setup_data_interface)(struct atmel_nand *nand, int csline,
203 const struct nand_data_interface *conf);
206 struct atmel_nand_controller_caps {
208 bool legacy_of_bindings;
211 const struct atmel_nand_controller_ops *ops;
214 struct atmel_nand_controller {
215 struct nand_hw_control base;
216 const struct atmel_nand_controller_caps *caps;
219 struct dma_chan *dmac;
220 struct atmel_pmecc *pmecc;
221 struct list_head chips;
225 static inline struct atmel_nand_controller *
226 to_nand_controller(struct nand_hw_control *ctl)
228 return container_of(ctl, struct atmel_nand_controller, base);
231 struct atmel_smc_nand_controller {
232 struct atmel_nand_controller base;
233 struct regmap *matrix;
234 unsigned int ebi_csa_offs;
237 static inline struct atmel_smc_nand_controller *
238 to_smc_nand_controller(struct nand_hw_control *ctl)
240 return container_of(to_nand_controller(ctl),
241 struct atmel_smc_nand_controller, base);
244 struct atmel_hsmc_nand_controller {
245 struct atmel_nand_controller base;
247 struct gen_pool *pool;
252 struct atmel_nfc_op op;
253 struct completion complete;
256 /* Only used when instantiating from legacy DT bindings. */
260 static inline struct atmel_hsmc_nand_controller *
261 to_hsmc_nand_controller(struct nand_hw_control *ctl)
263 return container_of(to_nand_controller(ctl),
264 struct atmel_hsmc_nand_controller, base);
267 static bool atmel_nfc_op_done(struct atmel_nfc_op *op, u32 status)
269 op->errors |= status & ATMEL_HSMC_NFC_SR_ERRORS;
270 op->wait ^= status & op->wait;
272 return !op->wait || op->errors;
275 static irqreturn_t atmel_nfc_interrupt(int irq, void *data)
277 struct atmel_hsmc_nand_controller *nc = data;
281 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &sr);
283 rcvd = sr & (nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
284 done = atmel_nfc_op_done(&nc->op, sr);
287 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, rcvd);
290 complete(&nc->complete);
292 return rcvd ? IRQ_HANDLED : IRQ_NONE;
295 static int atmel_nfc_wait(struct atmel_hsmc_nand_controller *nc, bool poll,
296 unsigned int timeout_ms)
301 timeout_ms = DEFAULT_TIMEOUT_MS;
306 ret = regmap_read_poll_timeout(nc->base.smc,
307 ATMEL_HSMC_NFC_SR, status,
308 atmel_nfc_op_done(&nc->op,
310 0, timeout_ms * 1000);
312 init_completion(&nc->complete);
313 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IER,
314 nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
315 ret = wait_for_completion_timeout(&nc->complete,
316 msecs_to_jiffies(timeout_ms));
322 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
325 if (nc->op.errors & ATMEL_HSMC_NFC_SR_DTOE) {
326 dev_err(nc->base.dev, "Waiting NAND R/B Timeout\n");
330 if (nc->op.errors & ATMEL_HSMC_NFC_SR_UNDEF) {
331 dev_err(nc->base.dev, "Access to an undefined area\n");
335 if (nc->op.errors & ATMEL_HSMC_NFC_SR_AWB) {
336 dev_err(nc->base.dev, "Access while busy\n");
340 if (nc->op.errors & ATMEL_HSMC_NFC_SR_NFCASE) {
341 dev_err(nc->base.dev, "Wrong access size\n");
348 static void atmel_nand_dma_transfer_finished(void *data)
350 struct completion *finished = data;
355 static int atmel_nand_dma_transfer(struct atmel_nand_controller *nc,
356 void *buf, dma_addr_t dev_dma, size_t len,
357 enum dma_data_direction dir)
359 DECLARE_COMPLETION_ONSTACK(finished);
360 dma_addr_t src_dma, dst_dma, buf_dma;
361 struct dma_async_tx_descriptor *tx;
364 buf_dma = dma_map_single(nc->dev, buf, len, dir);
365 if (dma_mapping_error(nc->dev, dev_dma)) {
367 "Failed to prepare a buffer for DMA access\n");
371 if (dir == DMA_FROM_DEVICE) {
379 tx = dmaengine_prep_dma_memcpy(nc->dmac, dst_dma, src_dma, len,
380 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
382 dev_err(nc->dev, "Failed to prepare DMA memcpy\n");
386 tx->callback = atmel_nand_dma_transfer_finished;
387 tx->callback_param = &finished;
389 cookie = dmaengine_submit(tx);
390 if (dma_submit_error(cookie)) {
391 dev_err(nc->dev, "Failed to do DMA tx_submit\n");
395 dma_async_issue_pending(nc->dmac);
396 wait_for_completion(&finished);
401 dma_unmap_single(nc->dev, buf_dma, len, dir);
404 dev_dbg(nc->dev, "Fall back to CPU I/O\n");
409 static u8 atmel_nand_read_byte(struct mtd_info *mtd)
411 struct nand_chip *chip = mtd_to_nand(mtd);
412 struct atmel_nand *nand = to_atmel_nand(chip);
414 return ioread8(nand->activecs->io.virt);
417 static u16 atmel_nand_read_word(struct mtd_info *mtd)
419 struct nand_chip *chip = mtd_to_nand(mtd);
420 struct atmel_nand *nand = to_atmel_nand(chip);
422 return ioread16(nand->activecs->io.virt);
425 static void atmel_nand_write_byte(struct mtd_info *mtd, u8 byte)
427 struct nand_chip *chip = mtd_to_nand(mtd);
428 struct atmel_nand *nand = to_atmel_nand(chip);
430 if (chip->options & NAND_BUSWIDTH_16)
431 iowrite16(byte | (byte << 8), nand->activecs->io.virt);
433 iowrite8(byte, nand->activecs->io.virt);
436 static void atmel_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
438 struct nand_chip *chip = mtd_to_nand(mtd);
439 struct atmel_nand *nand = to_atmel_nand(chip);
440 struct atmel_nand_controller *nc;
442 nc = to_nand_controller(chip->controller);
445 * If the controller supports DMA, the buffer address is DMA-able and
446 * len is long enough to make DMA transfers profitable, let's trigger
447 * a DMA transfer. If it fails, fallback to PIO mode.
449 if (nc->dmac && virt_addr_valid(buf) &&
450 len >= MIN_DMA_LEN &&
451 !atmel_nand_dma_transfer(nc, buf, nand->activecs->io.dma, len,
455 if (chip->options & NAND_BUSWIDTH_16)
456 ioread16_rep(nand->activecs->io.virt, buf, len / 2);
458 ioread8_rep(nand->activecs->io.virt, buf, len);
461 static void atmel_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
463 struct nand_chip *chip = mtd_to_nand(mtd);
464 struct atmel_nand *nand = to_atmel_nand(chip);
465 struct atmel_nand_controller *nc;
467 nc = to_nand_controller(chip->controller);
470 * If the controller supports DMA, the buffer address is DMA-able and
471 * len is long enough to make DMA transfers profitable, let's trigger
472 * a DMA transfer. If it fails, fallback to PIO mode.
474 if (nc->dmac && virt_addr_valid(buf) &&
475 len >= MIN_DMA_LEN &&
476 !atmel_nand_dma_transfer(nc, (void *)buf, nand->activecs->io.dma,
480 if (chip->options & NAND_BUSWIDTH_16)
481 iowrite16_rep(nand->activecs->io.virt, buf, len / 2);
483 iowrite8_rep(nand->activecs->io.virt, buf, len);
486 static int atmel_nand_dev_ready(struct mtd_info *mtd)
488 struct nand_chip *chip = mtd_to_nand(mtd);
489 struct atmel_nand *nand = to_atmel_nand(chip);
491 return gpiod_get_value(nand->activecs->rb.gpio);
494 static void atmel_nand_select_chip(struct mtd_info *mtd, int cs)
496 struct nand_chip *chip = mtd_to_nand(mtd);
497 struct atmel_nand *nand = to_atmel_nand(chip);
499 if (cs < 0 || cs >= nand->numcs) {
500 nand->activecs = NULL;
501 chip->dev_ready = NULL;
505 nand->activecs = &nand->cs[cs];
507 if (nand->activecs->rb.type == ATMEL_NAND_GPIO_RB)
508 chip->dev_ready = atmel_nand_dev_ready;
511 static int atmel_hsmc_nand_dev_ready(struct mtd_info *mtd)
513 struct nand_chip *chip = mtd_to_nand(mtd);
514 struct atmel_nand *nand = to_atmel_nand(chip);
515 struct atmel_hsmc_nand_controller *nc;
518 nc = to_hsmc_nand_controller(chip->controller);
520 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &status);
522 return status & ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id);
525 static void atmel_hsmc_nand_select_chip(struct mtd_info *mtd, int cs)
527 struct nand_chip *chip = mtd_to_nand(mtd);
528 struct atmel_nand *nand = to_atmel_nand(chip);
529 struct atmel_hsmc_nand_controller *nc;
531 nc = to_hsmc_nand_controller(chip->controller);
533 atmel_nand_select_chip(mtd, cs);
535 if (!nand->activecs) {
536 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
537 ATMEL_HSMC_NFC_CTRL_DIS);
541 if (nand->activecs->rb.type == ATMEL_NAND_NATIVE_RB)
542 chip->dev_ready = atmel_hsmc_nand_dev_ready;
544 regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG,
545 ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK |
546 ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK |
547 ATMEL_HSMC_NFC_CFG_RSPARE |
548 ATMEL_HSMC_NFC_CFG_WSPARE,
549 ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) |
550 ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) |
551 ATMEL_HSMC_NFC_CFG_RSPARE);
552 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
553 ATMEL_HSMC_NFC_CTRL_EN);
556 static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll)
558 u8 *addrs = nc->op.addrs;
563 nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE;
565 for (i = 0; i < nc->op.ncmds; i++)
566 op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]);
568 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
569 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++);
571 op |= ATMEL_NFC_CSID(nc->op.cs) |
572 ATMEL_NFC_ACYCLE(nc->op.naddrs);
574 if (nc->op.ncmds > 1)
575 op |= ATMEL_NFC_VCMD2;
577 addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) |
580 if (nc->op.data != ATMEL_NFC_NO_DATA) {
581 op |= ATMEL_NFC_DATAEN;
582 nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE;
584 if (nc->op.data == ATMEL_NFC_WRITE_DATA)
585 op |= ATMEL_NFC_NFCWR;
588 /* Clear all flags. */
589 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val);
591 /* Send the command. */
592 regmap_write(nc->io, op, addr);
594 ret = atmel_nfc_wait(nc, poll, 0);
596 dev_err(nc->base.dev,
597 "Failed to send NAND command (err = %d)!",
600 /* Reset the op state. */
601 memset(&nc->op, 0, sizeof(nc->op));
606 static void atmel_hsmc_nand_cmd_ctrl(struct mtd_info *mtd, int dat,
609 struct nand_chip *chip = mtd_to_nand(mtd);
610 struct atmel_nand *nand = to_atmel_nand(chip);
611 struct atmel_hsmc_nand_controller *nc;
613 nc = to_hsmc_nand_controller(chip->controller);
615 if (ctrl & NAND_ALE) {
616 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
619 nc->op.addrs[nc->op.naddrs++] = dat;
620 } else if (ctrl & NAND_CLE) {
621 if (nc->op.ncmds > 1)
624 nc->op.cmds[nc->op.ncmds++] = dat;
627 if (dat == NAND_CMD_NONE) {
628 nc->op.cs = nand->activecs->id;
629 atmel_nfc_exec_op(nc, true);
633 static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
636 struct nand_chip *chip = mtd_to_nand(mtd);
637 struct atmel_nand *nand = to_atmel_nand(chip);
638 struct atmel_nand_controller *nc;
640 nc = to_nand_controller(chip->controller);
642 if ((ctrl & NAND_CTRL_CHANGE) && nand->activecs->csgpio) {
644 gpiod_set_value(nand->activecs->csgpio, 0);
646 gpiod_set_value(nand->activecs->csgpio, 1);
650 writeb(cmd, nand->activecs->io.virt + nc->caps->ale_offs);
651 else if (ctrl & NAND_CLE)
652 writeb(cmd, nand->activecs->io.virt + nc->caps->cle_offs);
655 static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf,
658 struct mtd_info *mtd = nand_to_mtd(chip);
659 struct atmel_hsmc_nand_controller *nc;
662 nc = to_hsmc_nand_controller(chip->controller);
665 ret = atmel_nand_dma_transfer(&nc->base, (void *)buf,
666 nc->sram.dma, mtd->writesize,
669 /* Falling back to CPU copy. */
671 memcpy_toio(nc->sram.virt, buf, mtd->writesize);
674 memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi,
678 static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf,
681 struct mtd_info *mtd = nand_to_mtd(chip);
682 struct atmel_hsmc_nand_controller *nc;
685 nc = to_hsmc_nand_controller(chip->controller);
688 ret = atmel_nand_dma_transfer(&nc->base, buf, nc->sram.dma,
689 mtd->writesize, DMA_FROM_DEVICE);
691 /* Falling back to CPU copy. */
693 memcpy_fromio(buf, nc->sram.virt, mtd->writesize);
696 memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize,
700 static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column)
702 struct mtd_info *mtd = nand_to_mtd(chip);
703 struct atmel_hsmc_nand_controller *nc;
705 nc = to_hsmc_nand_controller(chip->controller);
708 nc->op.addrs[nc->op.naddrs++] = column;
711 * 2 address cycles for the column offset on large page NANDs.
713 if (mtd->writesize > 512)
714 nc->op.addrs[nc->op.naddrs++] = column >> 8;
718 nc->op.addrs[nc->op.naddrs++] = page;
719 nc->op.addrs[nc->op.naddrs++] = page >> 8;
721 if ((mtd->writesize > 512 && chip->chipsize > SZ_128M) ||
722 (mtd->writesize <= 512 && chip->chipsize > SZ_32M))
723 nc->op.addrs[nc->op.naddrs++] = page >> 16;
727 static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw)
729 struct atmel_nand *nand = to_atmel_nand(chip);
730 struct atmel_nand_controller *nc;
733 nc = to_nand_controller(chip->controller);
738 ret = atmel_pmecc_enable(nand->pmecc, op);
741 "Failed to enable ECC engine (err = %d)\n", ret);
746 static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw)
748 struct atmel_nand *nand = to_atmel_nand(chip);
751 atmel_pmecc_disable(nand->pmecc);
754 static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw)
756 struct atmel_nand *nand = to_atmel_nand(chip);
757 struct mtd_info *mtd = nand_to_mtd(chip);
758 struct atmel_nand_controller *nc;
759 struct mtd_oob_region oobregion;
763 nc = to_nand_controller(chip->controller);
768 ret = atmel_pmecc_wait_rdy(nand->pmecc);
771 "Failed to transfer NAND page data (err = %d)\n",
776 mtd_ooblayout_ecc(mtd, 0, &oobregion);
777 eccbuf = chip->oob_poi + oobregion.offset;
779 for (i = 0; i < chip->ecc.steps; i++) {
780 atmel_pmecc_get_generated_eccbytes(nand->pmecc, i,
782 eccbuf += chip->ecc.bytes;
788 static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf,
791 struct atmel_nand *nand = to_atmel_nand(chip);
792 struct mtd_info *mtd = nand_to_mtd(chip);
793 struct atmel_nand_controller *nc;
794 struct mtd_oob_region oobregion;
795 int ret, i, max_bitflips = 0;
796 void *databuf, *eccbuf;
798 nc = to_nand_controller(chip->controller);
803 ret = atmel_pmecc_wait_rdy(nand->pmecc);
806 "Failed to read NAND page data (err = %d)\n",
811 mtd_ooblayout_ecc(mtd, 0, &oobregion);
812 eccbuf = chip->oob_poi + oobregion.offset;
815 for (i = 0; i < chip->ecc.steps; i++) {
816 ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf,
818 if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc))
819 ret = nand_check_erased_ecc_chunk(databuf,
827 max_bitflips = max(ret, max_bitflips);
829 mtd->ecc_stats.failed++;
831 databuf += chip->ecc.size;
832 eccbuf += chip->ecc.bytes;
838 static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf,
839 bool oob_required, int page, bool raw)
841 struct mtd_info *mtd = nand_to_mtd(chip);
842 struct atmel_nand *nand = to_atmel_nand(chip);
845 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
849 atmel_nand_write_buf(mtd, buf, mtd->writesize);
851 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
853 atmel_pmecc_disable(nand->pmecc);
857 atmel_nand_pmecc_disable(chip, raw);
859 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
864 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
865 struct nand_chip *chip, const u8 *buf,
866 int oob_required, int page)
868 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
871 static int atmel_nand_pmecc_write_page_raw(struct mtd_info *mtd,
872 struct nand_chip *chip,
873 const u8 *buf, int oob_required,
876 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
879 static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
880 bool oob_required, int page, bool raw)
882 struct mtd_info *mtd = nand_to_mtd(chip);
885 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
889 atmel_nand_read_buf(mtd, buf, mtd->writesize);
890 atmel_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
892 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
894 atmel_nand_pmecc_disable(chip, raw);
899 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
900 struct nand_chip *chip, u8 *buf,
901 int oob_required, int page)
903 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
906 static int atmel_nand_pmecc_read_page_raw(struct mtd_info *mtd,
907 struct nand_chip *chip, u8 *buf,
908 int oob_required, int page)
910 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true);
913 static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
914 const u8 *buf, bool oob_required,
917 struct mtd_info *mtd = nand_to_mtd(chip);
918 struct atmel_nand *nand = to_atmel_nand(chip);
919 struct atmel_hsmc_nand_controller *nc;
922 nc = to_hsmc_nand_controller(chip->controller);
924 atmel_nfc_copy_to_sram(chip, buf, false);
926 nc->op.cmds[0] = NAND_CMD_SEQIN;
928 atmel_nfc_set_op_addr(chip, page, 0x0);
929 nc->op.cs = nand->activecs->id;
930 nc->op.data = ATMEL_NFC_WRITE_DATA;
932 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
936 ret = atmel_nfc_exec_op(nc, false);
938 atmel_nand_pmecc_disable(chip, raw);
939 dev_err(nc->base.dev,
940 "Failed to transfer NAND page data (err = %d)\n",
945 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
947 atmel_nand_pmecc_disable(chip, raw);
952 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
954 nc->op.cmds[0] = NAND_CMD_PAGEPROG;
956 nc->op.cs = nand->activecs->id;
957 ret = atmel_nfc_exec_op(nc, false);
959 dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n",
965 static int atmel_hsmc_nand_pmecc_write_page(struct mtd_info *mtd,
966 struct nand_chip *chip,
967 const u8 *buf, int oob_required,
970 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
974 static int atmel_hsmc_nand_pmecc_write_page_raw(struct mtd_info *mtd,
975 struct nand_chip *chip,
977 int oob_required, int page)
979 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
983 static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
984 bool oob_required, int page,
987 struct mtd_info *mtd = nand_to_mtd(chip);
988 struct atmel_nand *nand = to_atmel_nand(chip);
989 struct atmel_hsmc_nand_controller *nc;
992 nc = to_hsmc_nand_controller(chip->controller);
995 * Optimized read page accessors only work when the NAND R/B pin is
996 * connected to a native SoC R/B pin. If that's not the case, fallback
997 * to the non-optimized one.
999 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) {
1000 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1002 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
1006 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
1008 if (mtd->writesize > 512)
1009 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
1011 atmel_nfc_set_op_addr(chip, page, 0x0);
1012 nc->op.cs = nand->activecs->id;
1013 nc->op.data = ATMEL_NFC_READ_DATA;
1015 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
1019 ret = atmel_nfc_exec_op(nc, false);
1021 atmel_nand_pmecc_disable(chip, raw);
1022 dev_err(nc->base.dev,
1023 "Failed to load NAND page data (err = %d)\n",
1028 atmel_nfc_copy_from_sram(chip, buf, true);
1030 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
1032 atmel_nand_pmecc_disable(chip, raw);
1037 static int atmel_hsmc_nand_pmecc_read_page(struct mtd_info *mtd,
1038 struct nand_chip *chip, u8 *buf,
1039 int oob_required, int page)
1041 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1045 static int atmel_hsmc_nand_pmecc_read_page_raw(struct mtd_info *mtd,
1046 struct nand_chip *chip,
1047 u8 *buf, int oob_required,
1050 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1054 static int atmel_nand_pmecc_init(struct nand_chip *chip)
1056 struct mtd_info *mtd = nand_to_mtd(chip);
1057 struct atmel_nand *nand = to_atmel_nand(chip);
1058 struct atmel_nand_controller *nc;
1059 struct atmel_pmecc_user_req req;
1061 nc = to_nand_controller(chip->controller);
1064 dev_err(nc->dev, "HW ECC not supported\n");
1068 if (nc->caps->legacy_of_bindings) {
1071 if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap",
1073 chip->ecc.strength = val;
1075 if (!of_property_read_u32(nc->dev->of_node,
1076 "atmel,pmecc-sector-size",
1078 chip->ecc.size = val;
1081 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
1082 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1083 else if (chip->ecc.strength)
1084 req.ecc.strength = chip->ecc.strength;
1085 else if (chip->ecc_strength_ds)
1086 req.ecc.strength = chip->ecc_strength_ds;
1088 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1091 req.ecc.sectorsize = chip->ecc.size;
1092 else if (chip->ecc_step_ds)
1093 req.ecc.sectorsize = chip->ecc_step_ds;
1095 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1097 req.pagesize = mtd->writesize;
1098 req.oobsize = mtd->oobsize;
1100 if (mtd->writesize <= 512) {
1102 req.ecc.ooboffset = 0;
1104 req.ecc.bytes = mtd->oobsize - 2;
1105 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1108 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1109 if (IS_ERR(nand->pmecc))
1110 return PTR_ERR(nand->pmecc);
1112 chip->ecc.algo = NAND_ECC_BCH;
1113 chip->ecc.size = req.ecc.sectorsize;
1114 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1115 chip->ecc.strength = req.ecc.strength;
1117 chip->options |= NAND_NO_SUBPAGE_WRITE;
1119 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
1124 static int atmel_nand_ecc_init(struct atmel_nand *nand)
1126 struct nand_chip *chip = &nand->base;
1127 struct atmel_nand_controller *nc;
1130 nc = to_nand_controller(chip->controller);
1132 switch (chip->ecc.mode) {
1136 * Nothing to do, the core will initialize everything for us.
1141 ret = atmel_nand_pmecc_init(chip);
1145 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1146 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1147 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1148 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1152 /* Other modes are not supported. */
1153 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1161 static int atmel_hsmc_nand_ecc_init(struct atmel_nand *nand)
1163 struct nand_chip *chip = &nand->base;
1166 ret = atmel_nand_ecc_init(nand);
1170 if (chip->ecc.mode != NAND_ECC_HW)
1173 /* Adjust the ECC operations for the HSMC IP. */
1174 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1175 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1176 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1177 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
1178 chip->ecc.options |= NAND_ECC_CUSTOM_PAGE_ACCESS;
1183 static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1184 const struct nand_data_interface *conf,
1185 struct atmel_smc_cs_conf *smcconf)
1187 u32 ncycles, totalcycles, timeps, mckperiodps;
1188 struct atmel_nand_controller *nc;
1191 nc = to_nand_controller(nand->base.controller);
1193 /* DDR interface not supported. */
1194 if (conf->type != NAND_SDR_IFACE)
1198 * tRC < 30ns implies EDO mode. This controller does not support this
1201 if (conf->timings.sdr.tRC_min < 30)
1204 atmel_smc_cs_conf_init(smcconf);
1206 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1207 mckperiodps *= 1000;
1210 * Set write pulse timing. This one is easy to extract:
1214 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1215 totalcycles = ncycles;
1216 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1222 * The write setup timing depends on the operation done on the NAND.
1223 * All operations goes through the same data bus, but the operation
1224 * type depends on the address we are writing to (ALE/CLE address
1226 * Since we have no way to differentiate the different operations at
1227 * the SMC level, we must consider the worst case (the biggest setup
1228 * time among all operation types):
1230 * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE
1232 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1233 conf->timings.sdr.tALS_min);
1234 timeps = max(timeps, conf->timings.sdr.tDS_min);
1235 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1236 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1237 totalcycles += ncycles;
1238 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1244 * As for the write setup timing, the write hold timing depends on the
1245 * operation done on the NAND:
1247 * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH)
1249 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1250 conf->timings.sdr.tALH_min);
1251 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1252 conf->timings.sdr.tWH_min);
1253 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1254 totalcycles += ncycles;
1257 * The write cycle timing is directly matching tWC, but is also
1258 * dependent on the other timings on the setup and hold timings we
1259 * calculated earlier, which gives:
1261 * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD)
1263 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1264 ncycles = max(totalcycles, ncycles);
1265 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1271 * We don't want the CS line to be toggled between each byte/word
1272 * transfer to the NAND. The only way to guarantee that is to have the
1273 * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1275 * NCS_WR_PULSE = NWE_CYCLE
1277 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1283 * As for the write setup timing, the read hold timing depends on the
1284 * operation done on the NAND:
1286 * NRD_HOLD = max(tREH, tRHOH)
1288 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1289 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1290 totalcycles = ncycles;
1293 * TDF = tRHZ - NRD_HOLD
1295 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1296 ncycles -= totalcycles;
1299 * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and
1300 * we might end up with a config that does not fit in the TDF field.
1301 * Just take the max value in this case and hope that the NAND is more
1302 * tolerant than advertised.
1304 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1305 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1306 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1307 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1309 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1310 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1313 * Read pulse timing directly matches tRP:
1317 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1318 totalcycles += ncycles;
1319 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1325 * The write cycle timing is directly matching tWC, but is also
1326 * dependent on the setup and hold timings we calculated earlier,
1329 * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD)
1331 * NRD_SETUP is always 0.
1333 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1334 ncycles = max(totalcycles, ncycles);
1335 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1341 * We don't want the CS line to be toggled between each byte/word
1342 * transfer from the NAND. The only way to guarantee that is to have
1343 * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1345 * NCS_RD_PULSE = NRD_CYCLE
1347 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1352 /* Txxx timings are directly matching tXXX ones. */
1353 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1354 ret = atmel_smc_cs_conf_set_timing(smcconf,
1355 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1360 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1361 ret = atmel_smc_cs_conf_set_timing(smcconf,
1362 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1367 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1368 ret = atmel_smc_cs_conf_set_timing(smcconf,
1369 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1374 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1375 ret = atmel_smc_cs_conf_set_timing(smcconf,
1376 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1381 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1382 ret = atmel_smc_cs_conf_set_timing(smcconf,
1383 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1388 /* Attach the CS line to the NFC logic. */
1389 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1391 /* Set the appropriate data bus width. */
1392 if (nand->base.options & NAND_BUSWIDTH_16)
1393 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1395 /* Operate in NRD/NWE READ/WRITEMODE. */
1396 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1397 ATMEL_SMC_MODE_WRITEMODE_NWE;
1402 static int atmel_smc_nand_setup_data_interface(struct atmel_nand *nand,
1404 const struct nand_data_interface *conf)
1406 struct atmel_nand_controller *nc;
1407 struct atmel_smc_cs_conf smcconf;
1408 struct atmel_nand_cs *cs;
1411 nc = to_nand_controller(nand->base.controller);
1413 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1417 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1420 cs = &nand->cs[csline];
1421 cs->smcconf = smcconf;
1422 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1427 static int atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand,
1429 const struct nand_data_interface *conf)
1431 struct atmel_nand_controller *nc;
1432 struct atmel_smc_cs_conf smcconf;
1433 struct atmel_nand_cs *cs;
1436 nc = to_nand_controller(nand->base.controller);
1438 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1442 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1445 cs = &nand->cs[csline];
1446 cs->smcconf = smcconf;
1448 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1449 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1451 atmel_hsmc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1456 static int atmel_nand_setup_data_interface(struct mtd_info *mtd, int csline,
1457 const struct nand_data_interface *conf)
1459 struct nand_chip *chip = mtd_to_nand(mtd);
1460 struct atmel_nand *nand = to_atmel_nand(chip);
1461 struct atmel_nand_controller *nc;
1463 nc = to_nand_controller(nand->base.controller);
1465 if (csline >= nand->numcs ||
1466 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1469 return nc->caps->ops->setup_data_interface(nand, csline, conf);
1472 static void atmel_nand_init(struct atmel_nand_controller *nc,
1473 struct atmel_nand *nand)
1475 struct nand_chip *chip = &nand->base;
1476 struct mtd_info *mtd = nand_to_mtd(chip);
1478 mtd->dev.parent = nc->dev;
1479 nand->base.controller = &nc->base;
1481 chip->cmd_ctrl = atmel_nand_cmd_ctrl;
1482 chip->read_byte = atmel_nand_read_byte;
1483 chip->read_word = atmel_nand_read_word;
1484 chip->write_byte = atmel_nand_write_byte;
1485 chip->read_buf = atmel_nand_read_buf;
1486 chip->write_buf = atmel_nand_write_buf;
1487 chip->select_chip = atmel_nand_select_chip;
1489 if (nc->mck && nc->caps->ops->setup_data_interface)
1490 chip->setup_data_interface = atmel_nand_setup_data_interface;
1492 /* Some NANDs require a longer delay than the default one (20us). */
1493 chip->chip_delay = 40;
1496 * Use a bounce buffer when the buffer passed by the MTD user is not
1500 chip->options |= NAND_USE_BOUNCE_BUFFER;
1502 /* Default to HW ECC if pmecc is available. */
1504 chip->ecc.mode = NAND_ECC_HW;
1507 static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1508 struct atmel_nand *nand)
1510 struct nand_chip *chip = &nand->base;
1511 struct atmel_smc_nand_controller *smc_nc;
1514 atmel_nand_init(nc, nand);
1516 smc_nc = to_smc_nand_controller(chip->controller);
1517 if (!smc_nc->matrix)
1520 /* Attach the CS to the NAND Flash logic. */
1521 for (i = 0; i < nand->numcs; i++)
1522 regmap_update_bits(smc_nc->matrix, smc_nc->ebi_csa_offs,
1523 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1526 static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc,
1527 struct atmel_nand *nand)
1529 struct nand_chip *chip = &nand->base;
1531 atmel_nand_init(nc, nand);
1533 /* Overload some methods for the HSMC controller. */
1534 chip->cmd_ctrl = atmel_hsmc_nand_cmd_ctrl;
1535 chip->select_chip = atmel_hsmc_nand_select_chip;
1538 static int atmel_nand_detect(struct atmel_nand *nand)
1540 struct nand_chip *chip = &nand->base;
1541 struct mtd_info *mtd = nand_to_mtd(chip);
1542 struct atmel_nand_controller *nc;
1545 nc = to_nand_controller(chip->controller);
1547 ret = nand_scan_ident(mtd, nand->numcs, NULL);
1549 dev_err(nc->dev, "nand_scan_ident() failed: %d\n", ret);
1554 static int atmel_nand_unregister(struct atmel_nand *nand)
1556 struct nand_chip *chip = &nand->base;
1557 struct mtd_info *mtd = nand_to_mtd(chip);
1560 ret = mtd_device_unregister(mtd);
1565 list_del(&nand->node);
1570 static int atmel_nand_register(struct atmel_nand *nand)
1572 struct nand_chip *chip = &nand->base;
1573 struct mtd_info *mtd = nand_to_mtd(chip);
1574 struct atmel_nand_controller *nc;
1577 nc = to_nand_controller(chip->controller);
1579 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1581 * We keep the MTD name unchanged to avoid breaking platforms
1582 * where the MTD cmdline parser is used and the bootloader
1583 * has not been updated to use the new naming scheme.
1585 mtd->name = "atmel_nand";
1586 } else if (!mtd->name) {
1588 * If the new bindings are used and the bootloader has not been
1589 * updated to pass a new mtdparts parameter on the cmdline, you
1590 * should define the following property in your nand node:
1592 * label = "atmel_nand";
1594 * This way, mtd->name will be set by the core when
1595 * nand_set_flash_node() is called.
1597 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
1598 "%s:nand.%d", dev_name(nc->dev),
1601 dev_err(nc->dev, "Failed to allocate mtd->name\n");
1606 ret = nand_scan_tail(mtd);
1608 dev_err(nc->dev, "nand_scan_tail() failed: %d\n", ret);
1612 ret = mtd_device_register(mtd, NULL, 0);
1614 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1619 list_add_tail(&nand->node, &nc->chips);
1624 static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1625 struct device_node *np,
1628 struct atmel_nand *nand;
1629 struct gpio_desc *gpio;
1632 numcs = of_property_count_elems_of_size(np, "reg",
1633 reg_cells * sizeof(u32));
1635 dev_err(nc->dev, "Missing or invalid reg property\n");
1636 return ERR_PTR(-EINVAL);
1639 nand = devm_kzalloc(nc->dev,
1640 sizeof(*nand) + (numcs * sizeof(*nand->cs)),
1643 dev_err(nc->dev, "Failed to allocate NAND object\n");
1644 return ERR_PTR(-ENOMEM);
1647 nand->numcs = numcs;
1649 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0,
1650 &np->fwnode, GPIOD_IN,
1652 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1654 "Failed to get detect gpio (err = %ld)\n",
1656 return ERR_CAST(gpio);
1660 nand->cdgpio = gpio;
1662 for (i = 0; i < numcs; i++) {
1663 struct resource res;
1666 ret = of_address_to_resource(np, 0, &res);
1668 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1670 return ERR_PTR(ret);
1673 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1676 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1678 return ERR_PTR(ret);
1681 nand->cs[i].id = val;
1683 nand->cs[i].io.dma = res.start;
1684 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1685 if (IS_ERR(nand->cs[i].io.virt))
1686 return ERR_CAST(nand->cs[i].io.virt);
1688 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1689 if (val > ATMEL_NFC_MAX_RB_ID)
1690 return ERR_PTR(-EINVAL);
1692 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1693 nand->cs[i].rb.id = val;
1695 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev,
1696 "rb", i, &np->fwnode,
1697 GPIOD_IN, "nand-rb");
1698 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1700 "Failed to get R/B gpio (err = %ld)\n",
1702 return ERR_CAST(gpio);
1705 if (!IS_ERR(gpio)) {
1706 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1707 nand->cs[i].rb.gpio = gpio;
1711 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs",
1715 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1717 "Failed to get CS gpio (err = %ld)\n",
1719 return ERR_CAST(gpio);
1723 nand->cs[i].csgpio = gpio;
1726 nand_set_flash_node(&nand->base, np);
1732 atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1733 struct atmel_nand *nand)
1737 /* No card inserted, skip this NAND. */
1738 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1739 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1743 nc->caps->ops->nand_init(nc, nand);
1745 ret = atmel_nand_detect(nand);
1749 ret = nc->caps->ops->ecc_init(nand);
1753 return atmel_nand_register(nand);
1757 atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1759 struct atmel_nand *nand, *tmp;
1762 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
1763 ret = atmel_nand_unregister(nand);
1772 atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1774 struct device *dev = nc->dev;
1775 struct platform_device *pdev = to_platform_device(dev);
1776 struct atmel_nand *nand;
1777 struct gpio_desc *gpio;
1778 struct resource *res;
1781 * Legacy bindings only allow connecting a single NAND with a unique CS
1782 * line to the controller.
1784 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1791 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1792 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1793 if (IS_ERR(nand->cs[0].io.virt))
1794 return PTR_ERR(nand->cs[0].io.virt);
1796 nand->cs[0].io.dma = res->start;
1799 * The old driver was hardcoding the CS id to 3 for all sama5
1800 * controllers. Since this id is only meaningful for the sama5
1801 * controller we can safely assign this id to 3 no matter the
1803 * If one wants to connect a NAND to a different CS line, he will
1804 * have to use the new bindings.
1809 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1811 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1813 return PTR_ERR(gpio);
1817 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1818 nand->cs[0].rb.gpio = gpio;
1822 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1824 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1826 return PTR_ERR(gpio);
1829 nand->cs[0].csgpio = gpio;
1831 /* Card detect GPIO. */
1832 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1835 "Failed to get detect gpio (err = %ld)\n",
1837 return PTR_ERR(gpio);
1840 nand->cdgpio = gpio;
1842 nand_set_flash_node(&nand->base, nc->dev->of_node);
1844 return atmel_nand_controller_add_nand(nc, nand);
1847 static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1849 struct device_node *np, *nand_np;
1850 struct device *dev = nc->dev;
1854 /* We do not retrieve the SMC syscon when parsing old DTs. */
1855 if (nc->caps->legacy_of_bindings)
1856 return atmel_nand_controller_legacy_add_nands(nc);
1860 ret = of_property_read_u32(np, "#address-cells", &val);
1862 dev_err(dev, "missing #address-cells property\n");
1868 ret = of_property_read_u32(np, "#size-cells", &val);
1870 dev_err(dev, "missing #address-cells property\n");
1876 for_each_child_of_node(np, nand_np) {
1877 struct atmel_nand *nand;
1879 nand = atmel_nand_create(nc, nand_np, reg_cells);
1881 ret = PTR_ERR(nand);
1885 ret = atmel_nand_controller_add_nand(nc, nand);
1893 atmel_nand_controller_remove_nands(nc);
1898 static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1901 dma_release_channel(nc->dmac);
1906 static const struct of_device_id atmel_matrix_of_ids[] = {
1908 .compatible = "atmel,at91sam9260-matrix",
1909 .data = (void *)AT91SAM9260_MATRIX_EBICSA,
1912 .compatible = "atmel,at91sam9261-matrix",
1913 .data = (void *)AT91SAM9261_MATRIX_EBICSA,
1916 .compatible = "atmel,at91sam9263-matrix",
1917 .data = (void *)AT91SAM9263_MATRIX_EBI0CSA,
1920 .compatible = "atmel,at91sam9rl-matrix",
1921 .data = (void *)AT91SAM9RL_MATRIX_EBICSA,
1924 .compatible = "atmel,at91sam9g45-matrix",
1925 .data = (void *)AT91SAM9G45_MATRIX_EBICSA,
1928 .compatible = "atmel,at91sam9n12-matrix",
1929 .data = (void *)AT91SAM9N12_MATRIX_EBICSA,
1932 .compatible = "atmel,at91sam9x5-matrix",
1933 .data = (void *)AT91SAM9X5_MATRIX_EBICSA,
1938 static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
1939 struct platform_device *pdev,
1940 const struct atmel_nand_controller_caps *caps)
1942 struct device *dev = &pdev->dev;
1943 struct device_node *np = dev->of_node;
1946 nand_hw_control_init(&nc->base);
1947 INIT_LIST_HEAD(&nc->chips);
1951 platform_set_drvdata(pdev, nc);
1953 nc->pmecc = devm_atmel_pmecc_get(dev);
1954 if (IS_ERR(nc->pmecc)) {
1955 ret = PTR_ERR(nc->pmecc);
1956 if (ret != -EPROBE_DEFER)
1957 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1962 if (nc->caps->has_dma) {
1963 dma_cap_mask_t mask;
1966 dma_cap_set(DMA_MEMCPY, mask);
1968 nc->dmac = dma_request_channel(mask, NULL, NULL);
1970 dev_err(nc->dev, "Failed to request DMA channel\n");
1973 /* We do not retrieve the SMC syscon when parsing old DTs. */
1974 if (nc->caps->legacy_of_bindings)
1977 nc->mck = of_clk_get(dev->parent->of_node, 0);
1978 if (IS_ERR(nc->mck)) {
1979 dev_err(dev, "Failed to retrieve MCK clk\n");
1980 return PTR_ERR(nc->mck);
1983 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
1985 dev_err(dev, "Missing or invalid atmel,smc property\n");
1989 nc->smc = syscon_node_to_regmap(np);
1991 if (IS_ERR(nc->smc)) {
1992 ret = PTR_ERR(nc->smc);
1993 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
2001 atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
2003 struct device *dev = nc->base.dev;
2004 const struct of_device_id *match;
2005 struct device_node *np;
2008 /* We do not retrieve the matrix syscon when parsing old DTs. */
2009 if (nc->base.caps->legacy_of_bindings)
2012 np = of_parse_phandle(dev->parent->of_node, "atmel,matrix", 0);
2016 match = of_match_node(atmel_matrix_of_ids, np);
2022 nc->matrix = syscon_node_to_regmap(np);
2024 if (IS_ERR(nc->matrix)) {
2025 ret = PTR_ERR(nc->matrix);
2026 dev_err(dev, "Could not get Matrix regmap (err = %d)\n", ret);
2030 nc->ebi_csa_offs = (unsigned int)match->data;
2033 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
2034 * add 4 to ->ebi_csa_offs.
2036 if (of_device_is_compatible(dev->parent->of_node,
2037 "atmel,at91sam9263-ebi1"))
2038 nc->ebi_csa_offs += 4;
2044 atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2046 struct regmap_config regmap_conf = {
2052 struct device *dev = nc->base.dev;
2053 struct device_node *nand_np, *nfc_np;
2054 void __iomem *iomem;
2055 struct resource res;
2058 nand_np = dev->of_node;
2059 nfc_np = of_find_compatible_node(dev->of_node, NULL,
2060 "atmel,sama5d3-nfc");
2062 nc->clk = of_clk_get(nfc_np, 0);
2063 if (IS_ERR(nc->clk)) {
2064 ret = PTR_ERR(nc->clk);
2065 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2070 ret = clk_prepare_enable(nc->clk);
2072 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2077 nc->irq = of_irq_get(nand_np, 0);
2080 if (ret != -EPROBE_DEFER)
2081 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2086 ret = of_address_to_resource(nfc_np, 0, &res);
2088 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2093 iomem = devm_ioremap_resource(dev, &res);
2094 if (IS_ERR(iomem)) {
2095 ret = PTR_ERR(iomem);
2099 regmap_conf.name = "nfc-io";
2100 regmap_conf.max_register = resource_size(&res) - 4;
2101 nc->io = devm_regmap_init_mmio(dev, iomem, ®map_conf);
2102 if (IS_ERR(nc->io)) {
2103 ret = PTR_ERR(nc->io);
2104 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2109 ret = of_address_to_resource(nfc_np, 1, &res);
2111 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2116 iomem = devm_ioremap_resource(dev, &res);
2117 if (IS_ERR(iomem)) {
2118 ret = PTR_ERR(iomem);
2122 regmap_conf.name = "smc";
2123 regmap_conf.max_register = resource_size(&res) - 4;
2124 nc->base.smc = devm_regmap_init_mmio(dev, iomem, ®map_conf);
2125 if (IS_ERR(nc->base.smc)) {
2126 ret = PTR_ERR(nc->base.smc);
2127 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2132 ret = of_address_to_resource(nfc_np, 2, &res);
2134 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2139 nc->sram.virt = devm_ioremap_resource(dev, &res);
2140 if (IS_ERR(nc->sram.virt)) {
2141 ret = PTR_ERR(nc->sram.virt);
2145 nc->sram.dma = res.start;
2148 of_node_put(nfc_np);
2154 atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2156 struct device *dev = nc->base.dev;
2157 struct device_node *np;
2160 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2162 dev_err(dev, "Missing or invalid atmel,smc property\n");
2166 nc->irq = of_irq_get(np, 0);
2169 if (nc->irq != -EPROBE_DEFER)
2170 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2175 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2177 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2181 nc->io = syscon_node_to_regmap(np);
2183 if (IS_ERR(nc->io)) {
2184 ret = PTR_ERR(nc->io);
2185 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2189 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2190 "atmel,nfc-sram", 0);
2191 if (!nc->sram.pool) {
2192 dev_err(nc->base.dev, "Missing SRAM\n");
2196 nc->sram.virt = gen_pool_dma_alloc(nc->sram.pool,
2197 ATMEL_NFC_SRAM_SIZE,
2199 if (!nc->sram.virt) {
2200 dev_err(nc->base.dev,
2201 "Could not allocate memory from the NFC SRAM pool\n");
2209 atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2211 struct atmel_hsmc_nand_controller *hsmc_nc;
2214 ret = atmel_nand_controller_remove_nands(nc);
2218 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2219 if (hsmc_nc->sram.pool)
2220 gen_pool_free(hsmc_nc->sram.pool,
2221 (unsigned long)hsmc_nc->sram.virt,
2222 ATMEL_NFC_SRAM_SIZE);
2225 clk_disable_unprepare(hsmc_nc->clk);
2226 clk_put(hsmc_nc->clk);
2229 atmel_nand_controller_cleanup(nc);
2234 static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2235 const struct atmel_nand_controller_caps *caps)
2237 struct device *dev = &pdev->dev;
2238 struct atmel_hsmc_nand_controller *nc;
2241 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2245 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2249 if (caps->legacy_of_bindings)
2250 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2252 ret = atmel_hsmc_nand_controller_init(nc);
2257 /* Make sure all irqs are masked before registering our IRQ handler. */
2258 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2259 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2260 IRQF_SHARED, "nfc", nc);
2263 "Could not get register NFC interrupt handler (err = %d)\n",
2268 /* Initial NFC configuration. */
2269 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2270 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2272 ret = atmel_nand_controller_add_nands(&nc->base);
2279 atmel_hsmc_nand_controller_remove(&nc->base);
2284 static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2285 .probe = atmel_hsmc_nand_controller_probe,
2286 .remove = atmel_hsmc_nand_controller_remove,
2287 .ecc_init = atmel_hsmc_nand_ecc_init,
2288 .nand_init = atmel_hsmc_nand_init,
2289 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
2292 static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2294 .ale_offs = BIT(21),
2295 .cle_offs = BIT(22),
2296 .ops = &atmel_hsmc_nc_ops,
2299 /* Only used to parse old bindings. */
2300 static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2302 .ale_offs = BIT(21),
2303 .cle_offs = BIT(22),
2304 .ops = &atmel_hsmc_nc_ops,
2305 .legacy_of_bindings = true,
2308 static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2309 const struct atmel_nand_controller_caps *caps)
2311 struct device *dev = &pdev->dev;
2312 struct atmel_smc_nand_controller *nc;
2315 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2319 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2323 ret = atmel_smc_nand_controller_init(nc);
2327 return atmel_nand_controller_add_nands(&nc->base);
2331 atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2335 ret = atmel_nand_controller_remove_nands(nc);
2339 atmel_nand_controller_cleanup(nc);
2345 * The SMC reg layout of at91rm9200 is completely different which prevents us
2346 * from re-using atmel_smc_nand_setup_data_interface() for the
2347 * ->setup_data_interface() hook.
2348 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2349 * ->setup_data_interface() unassigned.
2351 static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
2352 .probe = atmel_smc_nand_controller_probe,
2353 .remove = atmel_smc_nand_controller_remove,
2354 .ecc_init = atmel_nand_ecc_init,
2355 .nand_init = atmel_smc_nand_init,
2358 static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2359 .ale_offs = BIT(21),
2360 .cle_offs = BIT(22),
2361 .ops = &at91rm9200_nc_ops,
2364 static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2365 .probe = atmel_smc_nand_controller_probe,
2366 .remove = atmel_smc_nand_controller_remove,
2367 .ecc_init = atmel_nand_ecc_init,
2368 .nand_init = atmel_smc_nand_init,
2369 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2372 static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2373 .ale_offs = BIT(21),
2374 .cle_offs = BIT(22),
2375 .ops = &atmel_smc_nc_ops,
2378 static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2379 .ale_offs = BIT(22),
2380 .cle_offs = BIT(21),
2381 .ops = &atmel_smc_nc_ops,
2384 static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2386 .ale_offs = BIT(21),
2387 .cle_offs = BIT(22),
2388 .ops = &atmel_smc_nc_ops,
2391 /* Only used to parse old bindings. */
2392 static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2393 .ale_offs = BIT(21),
2394 .cle_offs = BIT(22),
2395 .ops = &atmel_smc_nc_ops,
2396 .legacy_of_bindings = true,
2399 static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2400 .ale_offs = BIT(22),
2401 .cle_offs = BIT(21),
2402 .ops = &atmel_smc_nc_ops,
2403 .legacy_of_bindings = true,
2406 static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2408 .ale_offs = BIT(21),
2409 .cle_offs = BIT(22),
2410 .ops = &atmel_smc_nc_ops,
2411 .legacy_of_bindings = true,
2414 static const struct of_device_id atmel_nand_controller_of_ids[] = {
2416 .compatible = "atmel,at91rm9200-nand-controller",
2417 .data = &atmel_rm9200_nc_caps,
2420 .compatible = "atmel,at91sam9260-nand-controller",
2421 .data = &atmel_sam9260_nc_caps,
2424 .compatible = "atmel,at91sam9261-nand-controller",
2425 .data = &atmel_sam9261_nc_caps,
2428 .compatible = "atmel,at91sam9g45-nand-controller",
2429 .data = &atmel_sam9g45_nc_caps,
2432 .compatible = "atmel,sama5d3-nand-controller",
2433 .data = &atmel_sama5_nc_caps,
2435 /* Support for old/deprecated bindings: */
2437 .compatible = "atmel,at91rm9200-nand",
2438 .data = &atmel_rm9200_nand_caps,
2441 .compatible = "atmel,sama5d4-nand",
2442 .data = &atmel_rm9200_nand_caps,
2445 .compatible = "atmel,sama5d2-nand",
2446 .data = &atmel_rm9200_nand_caps,
2450 MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2452 static int atmel_nand_controller_probe(struct platform_device *pdev)
2454 const struct atmel_nand_controller_caps *caps;
2457 caps = (void *)pdev->id_entry->driver_data;
2459 caps = of_device_get_match_data(&pdev->dev);
2462 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2466 if (caps->legacy_of_bindings) {
2470 * If we are parsing legacy DT props and the DT contains a
2471 * valid NFC node, forward the request to the sama5 logic.
2473 if (of_find_compatible_node(pdev->dev.of_node, NULL,
2474 "atmel,sama5d3-nfc"))
2475 caps = &atmel_sama5_nand_caps;
2478 * Even if the compatible says we are dealing with an
2479 * at91rm9200 controller, the atmel,nand-has-dma specify that
2480 * this controller supports DMA, which means we are in fact
2481 * dealing with an at91sam9g45+ controller.
2483 if (!caps->has_dma &&
2484 of_property_read_bool(pdev->dev.of_node,
2485 "atmel,nand-has-dma"))
2486 caps = &atmel_sam9g45_nand_caps;
2489 * All SoCs except the at91sam9261 are assigning ALE to A21 and
2490 * CLE to A22. If atmel,nand-addr-offset != 21 this means we're
2491 * actually dealing with an at91sam9261 controller.
2493 of_property_read_u32(pdev->dev.of_node,
2494 "atmel,nand-addr-offset", &ale_offs);
2496 caps = &atmel_sam9261_nand_caps;
2499 return caps->ops->probe(pdev, caps);
2502 static int atmel_nand_controller_remove(struct platform_device *pdev)
2504 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2506 return nc->caps->ops->remove(nc);
2509 static int atmel_nand_controller_resume(struct device *dev)
2511 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2512 struct atmel_nand *nand;
2514 list_for_each_entry(nand, &nc->chips, node) {
2517 for (i = 0; i < nand->numcs; i++)
2518 nand_reset(&nand->base, i);
2524 static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2525 atmel_nand_controller_resume);
2527 static struct platform_driver atmel_nand_controller_driver = {
2529 .name = "atmel-nand-controller",
2530 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
2532 .probe = atmel_nand_controller_probe,
2533 .remove = atmel_nand_controller_remove,
2535 module_platform_driver(atmel_nand_controller_driver);
2537 MODULE_LICENSE("GPL");
2538 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2539 MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2540 MODULE_ALIAS("platform:atmel-nand-controller");