2 * Copyright © 2003 Rick Bronson
4 * Derived from drivers/mtd/nand/autcpu12.c
5 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
7 * Derived from drivers/mtd/spia.c
8 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
12 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
16 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
18 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
21 * Add Nand Flash Controller support for SAMA5 SoC
22 * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
30 #include <linux/clk.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/platform_device.h>
37 #include <linux/of_device.h>
38 #include <linux/of_gpio.h>
39 #include <linux/of_mtd.h>
40 #include <linux/mtd/mtd.h>
41 #include <linux/mtd/nand.h>
42 #include <linux/mtd/partitions.h>
44 #include <linux/delay.h>
45 #include <linux/dmaengine.h>
46 #include <linux/gpio.h>
47 #include <linux/interrupt.h>
49 #include <linux/platform_data/atmel.h>
51 static int use_dma = 1;
52 module_param(use_dma, int, 0);
54 static int on_flash_bbt = 0;
55 module_param(on_flash_bbt, int, 0);
57 /* Register access macros */
58 #define ecc_readl(add, reg) \
59 __raw_readl(add + ATMEL_ECC_##reg)
60 #define ecc_writel(add, reg, value) \
61 __raw_writel((value), add + ATMEL_ECC_##reg)
63 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
64 #include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
66 struct atmel_nand_caps {
67 bool pmecc_correct_erase_page;
70 struct atmel_nand_nfc_caps {
74 /* oob layout for large page size
75 * bad block info is on bytes 0 and 1
76 * the bytes have to be consecutives to avoid
77 * several NAND_CMD_RNDOUT during read
79 static struct nand_ecclayout atmel_oobinfo_large = {
81 .eccpos = {60, 61, 62, 63},
87 /* oob layout for small page size
88 * bad block info is on bytes 4 and 5
89 * the bytes have to be consecutives to avoid
90 * several NAND_CMD_RNDOUT during read
92 static struct nand_ecclayout atmel_oobinfo_small = {
94 .eccpos = {0, 1, 2, 3},
101 void __iomem *base_cmd_regs;
102 void __iomem *hsmc_regs;
104 dma_addr_t sram_bank0_phys;
111 struct completion comp_ready;
112 struct completion comp_cmd_done;
113 struct completion comp_xfer_done;
115 /* Point to the sram bank which include readed data via NFC */
117 bool will_write_sram;
118 const struct atmel_nand_nfc_caps *caps;
120 static struct atmel_nfc nand_nfc;
122 struct atmel_nand_host {
123 struct nand_chip nand_chip;
124 void __iomem *io_base;
126 struct atmel_nand_data board;
130 struct completion comp;
131 struct dma_chan *dma_chan;
133 struct atmel_nfc *nfc;
135 const struct atmel_nand_caps *caps;
138 u16 pmecc_sector_size;
139 bool has_no_lookup_table;
140 u32 pmecc_lookup_table_offset;
141 u32 pmecc_lookup_table_offset_512;
142 u32 pmecc_lookup_table_offset_1024;
144 int pmecc_degree; /* Degree of remainders */
145 int pmecc_cw_len; /* Length of codeword */
147 void __iomem *pmerrloc_base;
148 void __iomem *pmecc_rom_base;
150 /* lookup table for alpha_to and index_of */
151 void __iomem *pmecc_alpha_to;
152 void __iomem *pmecc_index_of;
154 /* data for pmecc computation */
155 int16_t *pmecc_partial_syn;
157 int16_t *pmecc_smu; /* Sigma table */
158 int16_t *pmecc_lmu; /* polynomal order */
164 static struct nand_ecclayout atmel_pmecc_oobinfo;
169 static void atmel_nand_enable(struct atmel_nand_host *host)
171 if (gpio_is_valid(host->board.enable_pin))
172 gpio_set_value(host->board.enable_pin, 0);
178 static void atmel_nand_disable(struct atmel_nand_host *host)
180 if (gpio_is_valid(host->board.enable_pin))
181 gpio_set_value(host->board.enable_pin, 1);
185 * Hardware specific access to control-lines
187 static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
189 struct nand_chip *nand_chip = mtd_to_nand(mtd);
190 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
192 if (ctrl & NAND_CTRL_CHANGE) {
194 atmel_nand_enable(host);
196 atmel_nand_disable(host);
198 if (cmd == NAND_CMD_NONE)
202 writeb(cmd, host->io_base + (1 << host->board.cle));
204 writeb(cmd, host->io_base + (1 << host->board.ale));
208 * Read the Device Ready pin.
210 static int atmel_nand_device_ready(struct mtd_info *mtd)
212 struct nand_chip *nand_chip = mtd_to_nand(mtd);
213 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
215 return gpio_get_value(host->board.rdy_pin) ^
216 !!host->board.rdy_pin_active_low;
219 /* Set up for hardware ready pin and enable pin. */
220 static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
222 struct nand_chip *chip = mtd_to_nand(mtd);
223 struct atmel_nand_host *host = nand_get_controller_data(chip);
226 if (gpio_is_valid(host->board.rdy_pin)) {
227 res = devm_gpio_request(host->dev,
228 host->board.rdy_pin, "nand_rdy");
231 "can't request rdy gpio %d\n",
232 host->board.rdy_pin);
236 res = gpio_direction_input(host->board.rdy_pin);
239 "can't request input direction rdy gpio %d\n",
240 host->board.rdy_pin);
244 chip->dev_ready = atmel_nand_device_ready;
247 if (gpio_is_valid(host->board.enable_pin)) {
248 res = devm_gpio_request(host->dev,
249 host->board.enable_pin, "nand_enable");
252 "can't request enable gpio %d\n",
253 host->board.enable_pin);
257 res = gpio_direction_output(host->board.enable_pin, 1);
260 "can't request output direction enable gpio %d\n",
261 host->board.enable_pin);
270 * Minimal-overhead PIO for data access.
272 static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
274 struct nand_chip *nand_chip = mtd_to_nand(mtd);
275 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
277 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
278 memcpy(buf, host->nfc->data_in_sram, len);
279 host->nfc->data_in_sram += len;
281 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
285 static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
287 struct nand_chip *nand_chip = mtd_to_nand(mtd);
288 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
290 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
291 memcpy(buf, host->nfc->data_in_sram, len);
292 host->nfc->data_in_sram += len;
294 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
298 static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
300 struct nand_chip *nand_chip = mtd_to_nand(mtd);
302 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
305 static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
307 struct nand_chip *nand_chip = mtd_to_nand(mtd);
309 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
312 static void dma_complete_func(void *completion)
314 complete(completion);
317 static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
319 /* NFC only has two banks. Must be 0 or 1 */
324 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
326 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
327 if (mtd->writesize > 2048)
329 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
331 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
337 static uint nfc_get_sram_off(struct atmel_nand_host *host)
339 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
340 return NFC_SRAM_BANK1_OFFSET;
345 static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
347 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
348 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
350 return host->nfc->sram_bank0_phys;
353 static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
356 struct dma_device *dma_dev;
357 enum dma_ctrl_flags flags;
358 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
359 struct dma_async_tx_descriptor *tx = NULL;
361 struct nand_chip *chip = mtd_to_nand(mtd);
362 struct atmel_nand_host *host = nand_get_controller_data(chip);
365 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
366 struct atmel_nfc *nfc = host->nfc;
368 if (buf >= high_memory)
371 dma_dev = host->dma_chan->device;
373 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
375 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
376 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
377 dev_err(host->dev, "Failed to dma_map_single\n");
382 if (nfc && nfc->data_in_sram)
383 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
384 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
386 dma_src_addr = host->io_phys;
388 dma_dst_addr = phys_addr;
390 dma_src_addr = phys_addr;
392 if (nfc && nfc->write_by_sram)
393 dma_dst_addr = nfc_sram_phys(host);
395 dma_dst_addr = host->io_phys;
398 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
399 dma_src_addr, len, flags);
401 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
405 init_completion(&host->comp);
406 tx->callback = dma_complete_func;
407 tx->callback_param = &host->comp;
409 cookie = tx->tx_submit(tx);
410 if (dma_submit_error(cookie)) {
411 dev_err(host->dev, "Failed to do DMA tx_submit\n");
415 dma_async_issue_pending(host->dma_chan);
416 wait_for_completion(&host->comp);
418 if (is_read && nfc && nfc->data_in_sram)
419 /* After read data from SRAM, need to increase the position */
420 nfc->data_in_sram += len;
425 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
428 dev_dbg(host->dev, "Fall back to CPU I/O\n");
432 static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
434 struct nand_chip *chip = mtd_to_nand(mtd);
435 struct atmel_nand_host *host = nand_get_controller_data(chip);
437 if (use_dma && len > mtd->oobsize)
438 /* only use DMA for bigger than oob size: better performances */
439 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
442 if (host->board.bus_width_16)
443 atmel_read_buf16(mtd, buf, len);
445 atmel_read_buf8(mtd, buf, len);
448 static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
450 struct nand_chip *chip = mtd_to_nand(mtd);
451 struct atmel_nand_host *host = nand_get_controller_data(chip);
453 if (use_dma && len > mtd->oobsize)
454 /* only use DMA for bigger than oob size: better performances */
455 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
458 if (host->board.bus_width_16)
459 atmel_write_buf16(mtd, buf, len);
461 atmel_write_buf8(mtd, buf, len);
465 * Return number of ecc bytes per sector according to sector size and
466 * correction capability
468 * Following table shows what at91 PMECC supported:
469 * Correction Capability Sector_512_bytes Sector_1024_bytes
470 * ===================== ================ =================
471 * 2-bits 4-bytes 4-bytes
472 * 4-bits 7-bytes 7-bytes
473 * 8-bits 13-bytes 14-bytes
474 * 12-bits 20-bytes 21-bytes
475 * 24-bits 39-bytes 42-bytes
477 static int pmecc_get_ecc_bytes(int cap, int sector_size)
479 int m = 12 + sector_size / 512;
480 return (m * cap + 7) / 8;
483 static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
484 int oobsize, int ecc_len)
488 layout->eccbytes = ecc_len;
490 /* ECC will occupy the last ecc_len bytes continuously */
491 for (i = 0; i < ecc_len; i++)
492 layout->eccpos[i] = oobsize - ecc_len + i;
494 layout->oobfree[0].offset = PMECC_OOB_RESERVED_BYTES;
495 layout->oobfree[0].length =
496 oobsize - ecc_len - layout->oobfree[0].offset;
499 static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
503 table_size = host->pmecc_sector_size == 512 ?
504 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
506 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
507 table_size * sizeof(int16_t);
510 static int pmecc_data_alloc(struct atmel_nand_host *host)
512 const int cap = host->pmecc_corr_cap;
515 size = (2 * cap + 1) * sizeof(int16_t);
516 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
517 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
518 host->pmecc_lmu = devm_kzalloc(host->dev,
519 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
520 host->pmecc_smu = devm_kzalloc(host->dev,
521 (cap + 2) * size, GFP_KERNEL);
523 size = (cap + 1) * sizeof(int);
524 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
525 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
526 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
528 if (!host->pmecc_partial_syn ||
540 static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
542 struct nand_chip *nand_chip = mtd_to_nand(mtd);
543 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
547 /* Fill odd syndromes */
548 for (i = 0; i < host->pmecc_corr_cap; i++) {
549 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
553 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
557 static void pmecc_substitute(struct mtd_info *mtd)
559 struct nand_chip *nand_chip = mtd_to_nand(mtd);
560 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
561 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
562 int16_t __iomem *index_of = host->pmecc_index_of;
563 int16_t *partial_syn = host->pmecc_partial_syn;
564 const int cap = host->pmecc_corr_cap;
568 /* si[] is a table that holds the current syndrome value,
569 * an element of that table belongs to the field
573 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
575 /* Computation 2t syndromes based on S(x) */
577 for (i = 1; i < 2 * cap; i += 2) {
578 for (j = 0; j < host->pmecc_degree; j++) {
579 if (partial_syn[i] & ((unsigned short)0x1 << j))
580 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
583 /* Even syndrome = (Odd syndrome) ** 2 */
584 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
590 tmp = readw_relaxed(index_of + si[j]);
591 tmp = (tmp * 2) % host->pmecc_cw_len;
592 si[i] = readw_relaxed(alpha_to + tmp);
599 static void pmecc_get_sigma(struct mtd_info *mtd)
601 struct nand_chip *nand_chip = mtd_to_nand(mtd);
602 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
604 int16_t *lmu = host->pmecc_lmu;
605 int16_t *si = host->pmecc_si;
606 int *mu = host->pmecc_mu;
607 int *dmu = host->pmecc_dmu; /* Discrepancy */
608 int *delta = host->pmecc_delta; /* Delta order */
609 int cw_len = host->pmecc_cw_len;
610 const int16_t cap = host->pmecc_corr_cap;
611 const int num = 2 * cap + 1;
612 int16_t __iomem *index_of = host->pmecc_index_of;
613 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
615 uint32_t dmu_0_count, tmp;
616 int16_t *smu = host->pmecc_smu;
618 /* index of largest delta */
630 memset(smu, 0, sizeof(int16_t) * num);
633 /* discrepancy set to 1 */
635 /* polynom order set to 0 */
637 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
643 /* Sigma(x) set to 1 */
644 memset(&smu[num], 0, sizeof(int16_t) * num);
647 /* discrepancy set to S1 */
650 /* polynom order set to 0 */
653 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
655 /* Init the Sigma(x) last row */
656 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
658 for (i = 1; i <= cap; i++) {
660 /* Begin Computing Sigma (Mu+1) and L(mu) */
661 /* check if discrepancy is set to 0 */
665 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
666 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
671 if (dmu_0_count == tmp) {
672 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
673 smu[(cap + 1) * num + j] =
676 lmu[cap + 1] = lmu[i];
681 for (j = 0; j <= lmu[i] >> 1; j++)
682 smu[(i + 1) * num + j] = smu[i * num + j];
684 /* copy previous polynom order to the next */
689 /* find largest delta with dmu != 0 */
690 for (j = 0; j < i; j++) {
691 if ((dmu[j]) && (delta[j] > largest)) {
697 /* compute difference */
698 diff = (mu[i] - mu[ro]);
700 /* Compute degree of the new smu polynomial */
701 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
704 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
706 /* Init smu[i+1] with 0 */
707 for (k = 0; k < num; k++)
708 smu[(i + 1) * num + k] = 0;
710 /* Compute smu[i+1] */
711 for (k = 0; k <= lmu[ro] >> 1; k++) {
714 if (!(smu[ro * num + k] && dmu[i]))
716 a = readw_relaxed(index_of + dmu[i]);
717 b = readw_relaxed(index_of + dmu[ro]);
718 c = readw_relaxed(index_of + smu[ro * num + k]);
719 tmp = a + (cw_len - b) + c;
720 a = readw_relaxed(alpha_to + tmp % cw_len);
721 smu[(i + 1) * num + (k + diff)] = a;
724 for (k = 0; k <= lmu[i] >> 1; k++)
725 smu[(i + 1) * num + k] ^= smu[i * num + k];
728 /* End Computing Sigma (Mu+1) and L(mu) */
729 /* In either case compute delta */
730 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
732 /* Do not compute discrepancy for the last iteration */
736 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
739 dmu[i + 1] = si[tmp + 3];
740 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
742 a = readw_relaxed(index_of +
743 smu[(i + 1) * num + k]);
744 b = si[2 * (i - 1) + 3 - k];
745 c = readw_relaxed(index_of + b);
748 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
757 static int pmecc_err_location(struct mtd_info *mtd)
759 struct nand_chip *nand_chip = mtd_to_nand(mtd);
760 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
761 unsigned long end_time;
762 const int cap = host->pmecc_corr_cap;
763 const int num = 2 * cap + 1;
764 int sector_size = host->pmecc_sector_size;
765 int err_nbr = 0; /* number of error */
766 int roots_nbr; /* number of roots */
769 int16_t *smu = host->pmecc_smu;
771 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
773 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
774 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
775 smu[(cap + 1) * num + i]);
779 val = (err_nbr - 1) << 16;
780 if (sector_size == 1024)
783 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
784 pmerrloc_writel(host->pmerrloc_base, ELEN,
785 sector_size * 8 + host->pmecc_degree * cap);
787 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
788 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
789 & PMERRLOC_CALC_DONE)) {
790 if (unlikely(time_after(jiffies, end_time))) {
791 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
797 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
798 & PMERRLOC_ERR_NUM_MASK) >> 8;
799 /* Number of roots == degree of smu hence <= cap */
800 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
803 /* Number of roots does not match the degree of smu
804 * unable to correct error */
808 static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
809 int sector_num, int extra_bytes, int err_nbr)
811 struct nand_chip *nand_chip = mtd_to_nand(mtd);
812 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
814 int byte_pos, bit_pos, sector_size, pos;
818 sector_size = host->pmecc_sector_size;
821 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
825 if (byte_pos >= (sector_size + extra_bytes))
826 BUG(); /* should never happen */
828 if (byte_pos < sector_size) {
829 err_byte = *(buf + byte_pos);
830 *(buf + byte_pos) ^= (1 << bit_pos);
832 pos = sector_num * host->pmecc_sector_size + byte_pos;
833 dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
834 pos, bit_pos, err_byte, *(buf + byte_pos));
836 /* Bit flip in OOB area */
837 tmp = sector_num * nand_chip->ecc.bytes
838 + (byte_pos - sector_size);
840 ecc[tmp] ^= (1 << bit_pos);
842 pos = tmp + nand_chip->ecc.layout->eccpos[0];
843 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
844 pos, bit_pos, err_byte, ecc[tmp]);
854 static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
857 struct nand_chip *nand_chip = mtd_to_nand(mtd);
858 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
861 int max_bitflips = 0;
863 /* If can correct bitfilps from erased page, do the normal check */
864 if (host->caps->pmecc_correct_erase_page)
867 for (i = 0; i < nand_chip->ecc.total; i++)
870 /* Erased page, return OK */
874 for (i = 0; i < nand_chip->ecc.steps; i++) {
876 if (pmecc_stat & 0x1) {
877 buf_pos = buf + i * host->pmecc_sector_size;
879 pmecc_gen_syndrome(mtd, i);
880 pmecc_substitute(mtd);
881 pmecc_get_sigma(mtd);
883 err_nbr = pmecc_err_location(mtd);
885 dev_err(host->dev, "PMECC: Too many errors\n");
886 mtd->ecc_stats.failed++;
889 pmecc_correct_data(mtd, buf_pos, ecc, i,
890 nand_chip->ecc.bytes, err_nbr);
891 mtd->ecc_stats.corrected += err_nbr;
892 max_bitflips = max_t(int, max_bitflips, err_nbr);
901 static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
905 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
906 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
910 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
911 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
912 val = pmecc_readl_relaxed(host->ecc, CFG);
914 if (ecc_op == NAND_ECC_READ)
915 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
916 | PMECC_CFG_AUTO_ENABLE);
918 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
919 & ~PMECC_CFG_AUTO_ENABLE);
921 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
922 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
925 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
926 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
928 struct atmel_nand_host *host = nand_get_controller_data(chip);
929 int eccsize = chip->ecc.size * chip->ecc.steps;
930 uint8_t *oob = chip->oob_poi;
931 uint32_t *eccpos = chip->ecc.layout->eccpos;
933 unsigned long end_time;
936 if (!host->nfc || !host->nfc->use_nfc_sram)
937 pmecc_enable(host, NAND_ECC_READ);
939 chip->read_buf(mtd, buf, eccsize);
940 chip->read_buf(mtd, oob, mtd->oobsize);
942 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
943 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
944 if (unlikely(time_after(jiffies, end_time))) {
945 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
951 stat = pmecc_readl_relaxed(host->ecc, ISR);
953 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
955 /* uncorrectable errors */
962 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
963 struct nand_chip *chip, const uint8_t *buf, int oob_required,
966 struct atmel_nand_host *host = nand_get_controller_data(chip);
967 uint32_t *eccpos = chip->ecc.layout->eccpos;
969 unsigned long end_time;
971 if (!host->nfc || !host->nfc->write_by_sram) {
972 pmecc_enable(host, NAND_ECC_WRITE);
973 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
976 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
977 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
978 if (unlikely(time_after(jiffies, end_time))) {
979 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
985 for (i = 0; i < chip->ecc.steps; i++) {
986 for (j = 0; j < chip->ecc.bytes; j++) {
989 pos = i * chip->ecc.bytes + j;
990 chip->oob_poi[eccpos[pos]] =
991 pmecc_readb_ecc_relaxed(host->ecc, i, j);
994 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
999 static void atmel_pmecc_core_init(struct mtd_info *mtd)
1001 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1002 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1004 struct nand_ecclayout *ecc_layout;
1006 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1007 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1009 switch (host->pmecc_corr_cap) {
1011 val = PMECC_CFG_BCH_ERR2;
1014 val = PMECC_CFG_BCH_ERR4;
1017 val = PMECC_CFG_BCH_ERR8;
1020 val = PMECC_CFG_BCH_ERR12;
1023 val = PMECC_CFG_BCH_ERR24;
1027 if (host->pmecc_sector_size == 512)
1028 val |= PMECC_CFG_SECTOR512;
1029 else if (host->pmecc_sector_size == 1024)
1030 val |= PMECC_CFG_SECTOR1024;
1032 switch (nand_chip->ecc.steps) {
1034 val |= PMECC_CFG_PAGE_1SECTOR;
1037 val |= PMECC_CFG_PAGE_2SECTORS;
1040 val |= PMECC_CFG_PAGE_4SECTORS;
1043 val |= PMECC_CFG_PAGE_8SECTORS;
1047 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1048 | PMECC_CFG_AUTO_DISABLE);
1049 pmecc_writel(host->ecc, CFG, val);
1051 ecc_layout = nand_chip->ecc.layout;
1052 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1053 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1054 pmecc_writel(host->ecc, EADDR,
1055 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1056 /* See datasheet about PMECC Clock Control Register */
1057 pmecc_writel(host->ecc, CLK, 2);
1058 pmecc_writel(host->ecc, IDR, 0xff);
1059 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1063 * Get minimum ecc requirements from NAND.
1064 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1065 * will set them according to minimum ecc requirement. Otherwise, use the
1066 * value in DTS file.
1067 * return 0 if success. otherwise return error code.
1069 static int pmecc_choose_ecc(struct atmel_nand_host *host,
1070 int *cap, int *sector_size)
1072 /* Get minimum ECC requirements */
1073 if (host->nand_chip.ecc_strength_ds) {
1074 *cap = host->nand_chip.ecc_strength_ds;
1075 *sector_size = host->nand_chip.ecc_step_ds;
1076 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
1077 *cap, *sector_size);
1081 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
1084 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
1085 if (host->pmecc_corr_cap == 0) {
1086 /* use the most fitable ecc bits (the near bigger one ) */
1088 host->pmecc_corr_cap = 2;
1090 host->pmecc_corr_cap = 4;
1092 host->pmecc_corr_cap = 8;
1093 else if (*cap <= 12)
1094 host->pmecc_corr_cap = 12;
1095 else if (*cap <= 24)
1096 host->pmecc_corr_cap = 24;
1100 if (host->pmecc_sector_size == 0) {
1101 /* use the most fitable sector size (the near smaller one ) */
1102 if (*sector_size >= 1024)
1103 host->pmecc_sector_size = 1024;
1104 else if (*sector_size >= 512)
1105 host->pmecc_sector_size = 512;
1112 static inline int deg(unsigned int poly)
1114 /* polynomial degree is the most-significant bit index */
1115 return fls(poly) - 1;
1118 static int build_gf_tables(int mm, unsigned int poly,
1119 int16_t *index_of, int16_t *alpha_to)
1121 unsigned int i, x = 1;
1122 const unsigned int k = 1 << deg(poly);
1123 unsigned int nn = (1 << mm) - 1;
1125 /* primitive polynomial must be of degree m */
1126 if (k != (1u << mm))
1129 for (i = 0; i < nn; i++) {
1133 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1145 static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1147 int degree = (sector_size == 512) ?
1148 PMECC_GF_DIMENSION_13 :
1149 PMECC_GF_DIMENSION_14;
1150 unsigned int poly = (sector_size == 512) ?
1151 PMECC_GF_13_PRIMITIVE_POLY :
1152 PMECC_GF_14_PRIMITIVE_POLY;
1153 int table_size = (sector_size == 512) ?
1154 PMECC_LOOKUP_TABLE_SIZE_512 :
1155 PMECC_LOOKUP_TABLE_SIZE_1024;
1157 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1159 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1165 static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
1166 struct atmel_nand_host *host)
1168 struct nand_chip *nand_chip = &host->nand_chip;
1169 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1170 struct resource *regs, *regs_pmerr, *regs_rom;
1171 uint16_t *galois_table;
1172 int cap, sector_size, err_no;
1174 err_no = pmecc_choose_ecc(host, &cap, §or_size);
1176 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1180 if (cap > host->pmecc_corr_cap ||
1181 sector_size != host->pmecc_sector_size)
1182 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
1184 cap = host->pmecc_corr_cap;
1185 sector_size = host->pmecc_sector_size;
1186 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1187 host->pmecc_lookup_table_offset_512 :
1188 host->pmecc_lookup_table_offset_1024;
1190 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1193 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1196 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1197 nand_chip->ecc.mode = NAND_ECC_SOFT;
1201 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1202 if (IS_ERR(host->ecc)) {
1203 err_no = PTR_ERR(host->ecc);
1207 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1208 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1209 if (IS_ERR(host->pmerrloc_base)) {
1210 err_no = PTR_ERR(host->pmerrloc_base);
1214 if (!host->has_no_lookup_table) {
1215 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1216 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1218 if (IS_ERR(host->pmecc_rom_base)) {
1219 dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
1220 host->has_no_lookup_table = true;
1224 if (host->has_no_lookup_table) {
1225 /* Build the look-up table in runtime */
1226 galois_table = create_lookup_table(host->dev, sector_size);
1227 if (!galois_table) {
1228 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1233 host->pmecc_rom_base = (void __iomem *)galois_table;
1234 host->pmecc_lookup_table_offset = 0;
1237 nand_chip->ecc.size = sector_size;
1239 /* set ECC page size and oob layout */
1240 switch (mtd->writesize) {
1246 if (sector_size > mtd->writesize) {
1247 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1252 host->pmecc_degree = (sector_size == 512) ?
1253 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
1254 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1255 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1256 host->pmecc_index_of = host->pmecc_rom_base +
1257 host->pmecc_lookup_table_offset;
1259 nand_chip->ecc.strength = cap;
1260 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
1261 nand_chip->ecc.steps = mtd->writesize / sector_size;
1262 nand_chip->ecc.total = nand_chip->ecc.bytes *
1263 nand_chip->ecc.steps;
1264 if (nand_chip->ecc.total >
1265 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
1266 dev_err(host->dev, "No room for ECC bytes\n");
1270 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1272 nand_chip->ecc.total);
1274 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1278 "Unsupported page size for PMECC, use Software ECC\n");
1279 /* page size not handled by HW ECC */
1280 /* switching back to soft ECC */
1281 nand_chip->ecc.mode = NAND_ECC_SOFT;
1285 /* Allocate data for PMECC computation */
1286 err_no = pmecc_data_alloc(host);
1289 "Cannot allocate memory for PMECC computation!\n");
1293 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
1294 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1295 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1297 atmel_pmecc_core_init(mtd);
1308 * function called after a write
1310 * mtd: MTD block structure
1311 * dat: raw data (unused)
1312 * ecc_code: buffer for ECC
1314 static int atmel_nand_calculate(struct mtd_info *mtd,
1315 const u_char *dat, unsigned char *ecc_code)
1317 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1318 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1319 unsigned int ecc_value;
1321 /* get the first 2 ECC bytes */
1322 ecc_value = ecc_readl(host->ecc, PR);
1324 ecc_code[0] = ecc_value & 0xFF;
1325 ecc_code[1] = (ecc_value >> 8) & 0xFF;
1327 /* get the last 2 ECC bytes */
1328 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
1330 ecc_code[2] = ecc_value & 0xFF;
1331 ecc_code[3] = (ecc_value >> 8) & 0xFF;
1337 * HW ECC read page function
1339 * mtd: mtd info structure
1340 * chip: nand chip info structure
1341 * buf: buffer to store read data
1342 * oob_required: caller expects OOB data read to chip->oob_poi
1344 static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1345 uint8_t *buf, int oob_required, int page)
1347 int eccsize = chip->ecc.size;
1348 int eccbytes = chip->ecc.bytes;
1349 uint32_t *eccpos = chip->ecc.layout->eccpos;
1351 uint8_t *oob = chip->oob_poi;
1354 unsigned int max_bitflips = 0;
1357 * Errata: ALE is incorrectly wired up to the ECC controller
1358 * on the AP7000, so it will include the address cycles in the
1361 * Workaround: Reset the parity registers before reading the
1364 struct atmel_nand_host *host = nand_get_controller_data(chip);
1365 if (host->board.need_reset_workaround)
1366 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
1369 chip->read_buf(mtd, p, eccsize);
1371 /* move to ECC position if needed */
1372 if (eccpos[0] != 0) {
1373 /* This only works on large pages
1374 * because the ECC controller waits for
1375 * NAND_CMD_RNDOUTSTART after the
1377 * anyway, for small pages, the eccpos[0] == 0
1379 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1380 mtd->writesize + eccpos[0], -1);
1383 /* the ECC controller needs to read the ECC just after the data */
1384 ecc_pos = oob + eccpos[0];
1385 chip->read_buf(mtd, ecc_pos, eccbytes);
1387 /* check if there's an error */
1388 stat = chip->ecc.correct(mtd, p, oob, NULL);
1391 mtd->ecc_stats.failed++;
1393 mtd->ecc_stats.corrected += stat;
1394 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1397 /* get back to oob start (end of page) */
1398 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1401 chip->read_buf(mtd, oob, mtd->oobsize);
1403 return max_bitflips;
1409 * function called after a read
1411 * mtd: MTD block structure
1412 * dat: raw data read from the chip
1413 * read_ecc: ECC from the chip (unused)
1416 * Detect and correct a 1 bit error for a page
1418 static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
1419 u_char *read_ecc, u_char *isnull)
1421 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1422 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1423 unsigned int ecc_status;
1424 unsigned int ecc_word, ecc_bit;
1426 /* get the status from the Status Register */
1427 ecc_status = ecc_readl(host->ecc, SR);
1429 /* if there's no error */
1430 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
1433 /* get error bit offset (4 bits) */
1434 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
1435 /* get word address (12 bits) */
1436 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
1439 /* if there are multiple errors */
1440 if (ecc_status & ATMEL_ECC_MULERR) {
1441 /* check if it is a freshly erased block
1442 * (filled with 0xff) */
1443 if ((ecc_bit == ATMEL_ECC_BITADDR)
1444 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
1445 /* the block has just been erased, return OK */
1448 /* it doesn't seems to be a freshly
1450 * We can't correct so many errors */
1451 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
1452 " Unable to correct.\n");
1456 /* if there's a single bit error : we can correct it */
1457 if (ecc_status & ATMEL_ECC_ECCERR) {
1458 /* there's nothing much to do here.
1459 * the bit error is on the ECC itself.
1461 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
1462 " Nothing to correct\n");
1466 dev_dbg(host->dev, "atmel_nand : one bit error on data."
1467 " (word offset in the page :"
1468 " 0x%x bit offset : 0x%x)\n",
1470 /* correct the error */
1471 if (nand_chip->options & NAND_BUSWIDTH_16) {
1473 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1476 dat[ecc_word] ^= (1 << ecc_bit);
1478 dev_dbg(host->dev, "atmel_nand : error corrected\n");
1483 * Enable HW ECC : unused on most chips
1485 static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1487 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1488 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1490 if (host->board.need_reset_workaround)
1491 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
1494 static int atmel_of_init_port(struct atmel_nand_host *host,
1495 struct device_node *np)
1500 struct atmel_nand_data *board = &host->board;
1501 enum of_gpio_flags flags = 0;
1503 host->caps = (struct atmel_nand_caps *)
1504 of_device_get_match_data(host->dev);
1506 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1508 dev_err(host->dev, "invalid addr-offset %u\n", val);
1514 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1516 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1522 ecc_mode = of_get_nand_ecc_mode(np);
1524 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1526 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1528 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1530 if (of_get_nand_bus_width(np) == 16)
1531 board->bus_width_16 = 1;
1533 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1534 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1536 board->enable_pin = of_get_gpio(np, 1);
1537 board->det_pin = of_get_gpio(np, 2);
1539 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1541 /* load the nfc driver if there is */
1542 of_platform_populate(np, NULL, NULL, host->dev);
1544 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1545 return 0; /* Not using PMECC */
1547 /* use PMECC, get correction capability, sector size and lookup
1549 * If correction bits and sector size are not specified, then find
1550 * them from NAND ONFI parameters.
1552 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1553 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1556 "Required ECC strength not supported: %u\n",
1560 host->pmecc_corr_cap = (u8)val;
1563 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1564 if ((val != 512) && (val != 1024)) {
1566 "Required ECC sector size not supported: %u\n",
1570 host->pmecc_sector_size = (u16)val;
1573 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1575 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1576 host->has_no_lookup_table = true;
1577 /* Will build a lookup table and initialize the offset later */
1580 if (!offset[0] && !offset[1]) {
1581 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1584 host->pmecc_lookup_table_offset_512 = offset[0];
1585 host->pmecc_lookup_table_offset_1024 = offset[1];
1590 static int atmel_hw_nand_init_params(struct platform_device *pdev,
1591 struct atmel_nand_host *host)
1593 struct nand_chip *nand_chip = &host->nand_chip;
1594 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1595 struct resource *regs;
1597 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1600 "Can't get I/O resource regs, use software ECC\n");
1601 nand_chip->ecc.mode = NAND_ECC_SOFT;
1605 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1606 if (IS_ERR(host->ecc))
1607 return PTR_ERR(host->ecc);
1609 /* ECC is calculated for the whole page (1 step) */
1610 nand_chip->ecc.size = mtd->writesize;
1612 /* set ECC page size and oob layout */
1613 switch (mtd->writesize) {
1615 nand_chip->ecc.layout = &atmel_oobinfo_small;
1616 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1619 nand_chip->ecc.layout = &atmel_oobinfo_large;
1620 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1623 nand_chip->ecc.layout = &atmel_oobinfo_large;
1624 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1627 nand_chip->ecc.layout = &atmel_oobinfo_large;
1628 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1631 /* page size not handled by HW ECC */
1632 /* switching back to soft ECC */
1633 nand_chip->ecc.mode = NAND_ECC_SOFT;
1637 /* set up for HW ECC */
1638 nand_chip->ecc.calculate = atmel_nand_calculate;
1639 nand_chip->ecc.correct = atmel_nand_correct;
1640 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1641 nand_chip->ecc.read_page = atmel_nand_read_page;
1642 nand_chip->ecc.bytes = 4;
1643 nand_chip->ecc.strength = 1;
1648 static inline u32 nfc_read_status(struct atmel_nand_host *host)
1650 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1651 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1653 if (unlikely(nfc_status & err_flags)) {
1654 if (nfc_status & NFC_SR_DTOE)
1655 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1656 else if (nfc_status & NFC_SR_UNDEF)
1657 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1658 else if (nfc_status & NFC_SR_AWB)
1659 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1660 else if (nfc_status & NFC_SR_ASE)
1661 dev_err(host->dev, "NFC: Access memory Size Error\n");
1667 /* SMC interrupt service routine */
1668 static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1670 struct atmel_nand_host *host = dev_id;
1671 u32 status, mask, pending;
1672 irqreturn_t ret = IRQ_NONE;
1674 status = nfc_read_status(host);
1675 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1676 pending = status & mask;
1678 if (pending & NFC_SR_XFR_DONE) {
1679 complete(&host->nfc->comp_xfer_done);
1680 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
1683 if (pending & host->nfc->caps->rb_mask) {
1684 complete(&host->nfc->comp_ready);
1685 nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask);
1688 if (pending & NFC_SR_CMD_DONE) {
1689 complete(&host->nfc->comp_cmd_done);
1690 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
1697 /* NFC(Nand Flash Controller) related functions */
1698 static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
1700 if (flag & NFC_SR_XFR_DONE)
1701 init_completion(&host->nfc->comp_xfer_done);
1703 if (flag & host->nfc->caps->rb_mask)
1704 init_completion(&host->nfc->comp_ready);
1706 if (flag & NFC_SR_CMD_DONE)
1707 init_completion(&host->nfc->comp_cmd_done);
1709 /* Enable interrupt that need to wait for */
1710 nfc_writel(host->nfc->hsmc_regs, IER, flag);
1713 static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1716 struct completion *comp[3]; /* Support 3 interrupt completion */
1718 if (flag & NFC_SR_XFR_DONE)
1719 comp[index++] = &host->nfc->comp_xfer_done;
1721 if (flag & host->nfc->caps->rb_mask)
1722 comp[index++] = &host->nfc->comp_ready;
1724 if (flag & NFC_SR_CMD_DONE)
1725 comp[index++] = &host->nfc->comp_cmd_done;
1728 dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
1732 for (i = 0; i < index; i++) {
1733 if (wait_for_completion_timeout(comp[i],
1734 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1735 continue; /* wait for next completion */
1743 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
1744 /* Disable the interrupt as it is not handled by interrupt handler */
1745 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
1749 static int nfc_send_command(struct atmel_nand_host *host,
1750 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1752 unsigned long timeout;
1753 u32 flag = NFC_SR_CMD_DONE;
1754 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1757 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1760 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1761 while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
1762 if (time_after(jiffies, timeout)) {
1764 "Time out to wait for NFC ready!\n");
1769 nfc_prepare_interrupt(host, flag);
1770 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1771 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
1772 return nfc_wait_interrupt(host, flag);
1775 static int nfc_device_ready(struct mtd_info *mtd)
1778 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1779 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1781 status = nfc_read_status(host);
1782 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1784 /* The mask should be 0. If not we may lost interrupts */
1785 if (unlikely(mask & status))
1786 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1789 return status & host->nfc->caps->rb_mask;
1792 static void nfc_select_chip(struct mtd_info *mtd, int chip)
1794 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1795 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
1798 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1800 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1803 static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1804 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
1806 struct nand_chip *chip = mtd_to_nand(mtd);
1809 unsigned char addr_bytes[8];
1810 int index = 0, bit_shift;
1812 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1818 if (chip->options & NAND_BUSWIDTH_16 &&
1819 !nand_opcode_8bits(command))
1821 addr_bytes[acycle++] = column & 0xff;
1822 if (mtd->writesize > 512)
1823 addr_bytes[acycle++] = (column >> 8) & 0xff;
1826 if (page_addr != -1) {
1827 addr_bytes[acycle++] = page_addr & 0xff;
1828 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1829 if (chip->chipsize > (128 << 20))
1830 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1834 *cycle0 = addr_bytes[index++];
1836 for (bit_shift = 0; index < acycle; bit_shift += 8)
1837 *addr1234 += addr_bytes[index++] << bit_shift;
1839 /* return acycle in cmd register */
1840 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1843 static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1844 int column, int page_addr)
1846 struct nand_chip *chip = mtd_to_nand(mtd);
1847 struct atmel_nand_host *host = nand_get_controller_data(chip);
1848 unsigned long timeout;
1849 unsigned int nfc_addr_cmd = 0;
1851 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1853 /* Set default settings: no cmd2, no addr cycle. read from nand */
1854 unsigned int cmd2 = 0;
1855 unsigned int vcmd2 = 0;
1856 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1857 int csid = NFCADDR_CMD_CSID_3;
1858 int dataen = NFCADDR_CMD_DATADIS;
1859 int nfcwr = NFCADDR_CMD_NFCRD;
1860 unsigned int addr1234 = 0;
1861 unsigned int cycle0 = 0;
1862 bool do_addr = true;
1863 host->nfc->data_in_sram = NULL;
1865 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1866 __func__, command, column, page_addr);
1869 case NAND_CMD_RESET:
1870 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1871 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1872 udelay(chip->chip_delay);
1874 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1875 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1876 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1877 if (time_after(jiffies, timeout)) {
1879 "Time out to wait status ready!\n");
1884 case NAND_CMD_STATUS:
1887 case NAND_CMD_PARAM:
1888 case NAND_CMD_READID:
1890 acycle = NFCADDR_CMD_ACYCLE_1;
1894 case NAND_CMD_RNDOUT:
1895 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1896 vcmd2 = NFCADDR_CMD_VCMD2;
1898 case NAND_CMD_READ0:
1899 case NAND_CMD_READOOB:
1900 if (command == NAND_CMD_READOOB) {
1901 column += mtd->writesize;
1902 command = NAND_CMD_READ0; /* only READ0 is valid */
1903 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1905 if (host->nfc->use_nfc_sram) {
1906 /* Enable Data transfer to sram */
1907 dataen = NFCADDR_CMD_DATAEN;
1909 /* Need enable PMECC now, since NFC will transfer
1910 * data in bus after sending nfc read command.
1912 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1913 pmecc_enable(host, NAND_ECC_READ);
1916 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1917 vcmd2 = NFCADDR_CMD_VCMD2;
1919 /* For prgramming command, the cmd need set to write enable */
1920 case NAND_CMD_PAGEPROG:
1921 case NAND_CMD_SEQIN:
1922 case NAND_CMD_RNDIN:
1923 nfcwr = NFCADDR_CMD_NFCWR;
1924 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1925 dataen = NFCADDR_CMD_DATAEN;
1932 acycle = nfc_make_addr(mtd, command, column, page_addr,
1933 &addr1234, &cycle0);
1935 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1936 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1939 * Program and erase have their own busy handlers status, sequential
1940 * in, and deplete1 need no delay.
1943 case NAND_CMD_CACHEDPROG:
1944 case NAND_CMD_PAGEPROG:
1945 case NAND_CMD_ERASE1:
1946 case NAND_CMD_ERASE2:
1947 case NAND_CMD_RNDIN:
1948 case NAND_CMD_STATUS:
1949 case NAND_CMD_RNDOUT:
1950 case NAND_CMD_SEQIN:
1951 case NAND_CMD_READID:
1954 case NAND_CMD_READ0:
1955 if (dataen == NFCADDR_CMD_DATAEN) {
1956 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1957 nfc_get_sram_off(host);
1962 nfc_prepare_interrupt(host, host->nfc->caps->rb_mask);
1963 nfc_wait_interrupt(host, host->nfc->caps->rb_mask);
1967 static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1968 uint32_t offset, int data_len, const uint8_t *buf,
1969 int oob_required, int page, int cached, int raw)
1973 struct atmel_nand_host *host = nand_get_controller_data(chip);
1974 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1976 /* Subpage write is not supported */
1977 if (offset || (data_len < mtd->writesize))
1980 len = mtd->writesize;
1981 /* Copy page data to sram that will write to nand via NFC */
1983 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1984 /* Fall back to use cpu copy */
1985 memcpy(sram, buf, len);
1987 memcpy(sram, buf, len);
1990 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1991 if (unlikely(raw) && oob_required) {
1992 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
1993 len += mtd->oobsize;
1994 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1996 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1999 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2001 * When use NFC sram, need set up PMECC before send
2002 * NAND_CMD_SEQIN command. Since when the nand command
2003 * is sent, nfc will do transfer from sram and nand.
2005 pmecc_enable(host, NAND_ECC_WRITE);
2007 host->nfc->will_write_sram = true;
2008 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2009 host->nfc->will_write_sram = false;
2012 /* Need to write ecc into oob */
2013 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2019 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2020 status = chip->waitfunc(mtd, chip);
2022 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2023 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2025 if (status & NAND_STATUS_FAIL)
2031 static int nfc_sram_init(struct mtd_info *mtd)
2033 struct nand_chip *chip = mtd_to_nand(mtd);
2034 struct atmel_nand_host *host = nand_get_controller_data(chip);
2037 /* Initialize the NFC CFG register */
2038 unsigned int cfg_nfc = 0;
2040 /* set page size and oob layout */
2041 switch (mtd->writesize) {
2043 cfg_nfc = NFC_CFG_PAGESIZE_512;
2046 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2049 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2052 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2055 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2058 dev_err(host->dev, "Unsupported page size for NFC.\n");
2063 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2064 * Max support spare size is 512 bytes. */
2065 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2066 & NFC_CFG_NFC_SPARESIZE);
2067 /* default set a max timeout */
2068 cfg_nfc |= NFC_CFG_RSPARE |
2069 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2071 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2073 host->nfc->will_write_sram = false;
2074 nfc_set_sram_bank(host, 0);
2076 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2077 if (host->nfc->write_by_sram) {
2078 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2079 chip->ecc.mode == NAND_ECC_NONE)
2080 chip->write_page = nfc_sram_write_page;
2082 host->nfc->write_by_sram = false;
2085 dev_info(host->dev, "Using NFC Sram read %s\n",
2086 host->nfc->write_by_sram ? "and write" : "");
2090 static struct platform_driver atmel_nand_nfc_driver;
2092 * Probe for the NAND device.
2094 static int atmel_nand_probe(struct platform_device *pdev)
2096 struct atmel_nand_host *host;
2097 struct mtd_info *mtd;
2098 struct nand_chip *nand_chip;
2099 struct resource *mem;
2102 /* Allocate memory for the device structure (and zero it) */
2103 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
2107 res = platform_driver_register(&atmel_nand_nfc_driver);
2109 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2111 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2112 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2113 if (IS_ERR(host->io_base)) {
2114 res = PTR_ERR(host->io_base);
2115 goto err_nand_ioremap;
2117 host->io_phys = (dma_addr_t)mem->start;
2119 nand_chip = &host->nand_chip;
2120 mtd = nand_to_mtd(nand_chip);
2121 host->dev = &pdev->dev;
2122 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2123 nand_set_flash_node(nand_chip, pdev->dev.of_node);
2124 /* Only when CONFIG_OF is enabled of_node can be parsed */
2125 res = atmel_of_init_port(host, pdev->dev.of_node);
2127 goto err_nand_ioremap;
2129 memcpy(&host->board, dev_get_platdata(&pdev->dev),
2130 sizeof(struct atmel_nand_data));
2133 /* link the private data structures */
2134 nand_set_controller_data(nand_chip, host);
2135 mtd->dev.parent = &pdev->dev;
2137 /* Set address of NAND IO lines */
2138 nand_chip->IO_ADDR_R = host->io_base;
2139 nand_chip->IO_ADDR_W = host->io_base;
2141 if (nand_nfc.is_initialized) {
2142 /* NFC driver is probed and initialized */
2143 host->nfc = &nand_nfc;
2145 nand_chip->select_chip = nfc_select_chip;
2146 nand_chip->dev_ready = nfc_device_ready;
2147 nand_chip->cmdfunc = nfc_nand_command;
2149 /* Initialize the interrupt for NFC */
2150 irq = platform_get_irq(pdev, 0);
2152 dev_err(host->dev, "Cannot get HSMC irq!\n");
2154 goto err_nand_ioremap;
2157 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2160 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2162 goto err_nand_ioremap;
2165 res = atmel_nand_set_enable_ready_pins(mtd);
2167 goto err_nand_ioremap;
2169 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
2172 nand_chip->ecc.mode = host->board.ecc_mode;
2173 nand_chip->chip_delay = 40; /* 40us command delay time */
2175 if (host->board.bus_width_16) /* 16-bit bus width */
2176 nand_chip->options |= NAND_BUSWIDTH_16;
2178 nand_chip->read_buf = atmel_read_buf;
2179 nand_chip->write_buf = atmel_write_buf;
2181 platform_set_drvdata(pdev, host);
2182 atmel_nand_enable(host);
2184 if (gpio_is_valid(host->board.det_pin)) {
2185 res = devm_gpio_request(&pdev->dev,
2186 host->board.det_pin, "nand_det");
2189 "can't request det gpio %d\n",
2190 host->board.det_pin);
2194 res = gpio_direction_input(host->board.det_pin);
2197 "can't request input direction det gpio %d\n",
2198 host->board.det_pin);
2202 if (gpio_get_value(host->board.det_pin)) {
2203 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
2209 if (host->board.on_flash_bbt || on_flash_bbt) {
2210 dev_info(&pdev->dev, "Use On Flash BBT\n");
2211 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
2214 if (!host->board.has_dma)
2218 dma_cap_mask_t mask;
2221 dma_cap_set(DMA_MEMCPY, mask);
2222 host->dma_chan = dma_request_channel(mask, NULL, NULL);
2223 if (!host->dma_chan) {
2224 dev_err(host->dev, "Failed to request DMA channel\n");
2229 dev_info(host->dev, "Using %s for DMA transfers.\n",
2230 dma_chan_name(host->dma_chan));
2232 dev_info(host->dev, "No DMA support for NAND access.\n");
2234 /* first scan to find the device and get the page size */
2235 if (nand_scan_ident(mtd, 1, NULL)) {
2237 goto err_scan_ident;
2240 if (nand_chip->ecc.mode == NAND_ECC_HW) {
2241 if (host->has_pmecc)
2242 res = atmel_pmecc_nand_init_params(pdev, host);
2244 res = atmel_hw_nand_init_params(pdev, host);
2250 /* initialize the nfc configuration register */
2251 if (host->nfc && host->nfc->use_nfc_sram) {
2252 res = nfc_sram_init(mtd);
2254 host->nfc->use_nfc_sram = false;
2255 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2259 /* second phase scan */
2260 if (nand_scan_tail(mtd)) {
2265 mtd->name = "atmel_nand";
2266 res = mtd_device_register(mtd, host->board.parts,
2267 host->board.num_parts);
2272 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
2273 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2277 atmel_nand_disable(host);
2279 dma_release_channel(host->dma_chan);
2285 * Remove a NAND device.
2287 static int atmel_nand_remove(struct platform_device *pdev)
2289 struct atmel_nand_host *host = platform_get_drvdata(pdev);
2290 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
2294 atmel_nand_disable(host);
2296 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2297 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2298 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2303 dma_release_channel(host->dma_chan);
2305 platform_driver_unregister(&atmel_nand_nfc_driver);
2310 static const struct atmel_nand_caps at91rm9200_caps = {
2311 .pmecc_correct_erase_page = false,
2314 static const struct atmel_nand_caps sama5d4_caps = {
2315 .pmecc_correct_erase_page = true,
2318 static const struct of_device_id atmel_nand_dt_ids[] = {
2319 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2320 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
2324 MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
2326 static int atmel_nand_nfc_probe(struct platform_device *pdev)
2328 struct atmel_nfc *nfc = &nand_nfc;
2329 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
2332 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2333 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2334 if (IS_ERR(nfc->base_cmd_regs))
2335 return PTR_ERR(nfc->base_cmd_regs);
2337 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2338 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2339 if (IS_ERR(nfc->hsmc_regs))
2340 return PTR_ERR(nfc->hsmc_regs);
2342 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2344 nfc->sram_bank0 = (void * __force)
2345 devm_ioremap_resource(&pdev->dev, nfc_sram);
2346 if (IS_ERR(nfc->sram_bank0)) {
2347 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2348 PTR_ERR(nfc->sram_bank0));
2350 nfc->use_nfc_sram = true;
2351 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
2353 if (pdev->dev.of_node)
2354 nfc->write_by_sram = of_property_read_bool(
2356 "atmel,write-by-sram");
2360 nfc->caps = (const struct atmel_nand_nfc_caps *)
2361 of_device_get_match_data(&pdev->dev);
2365 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2366 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2368 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2369 if (!IS_ERR(nfc->clk)) {
2370 ret = clk_prepare_enable(nfc->clk);
2374 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2377 nfc->is_initialized = true;
2378 dev_info(&pdev->dev, "NFC is probed.\n");
2383 static int atmel_nand_nfc_remove(struct platform_device *pdev)
2385 struct atmel_nfc *nfc = &nand_nfc;
2387 if (!IS_ERR(nfc->clk))
2388 clk_disable_unprepare(nfc->clk);
2393 static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = {
2394 .rb_mask = NFC_SR_RB_EDGE0,
2397 static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = {
2398 .rb_mask = NFC_SR_RB_EDGE3,
2401 static const struct of_device_id atmel_nand_nfc_match[] = {
2402 { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps },
2403 { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps },
2406 MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
2408 static struct platform_driver atmel_nand_nfc_driver = {
2410 .name = "atmel_nand_nfc",
2411 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2413 .probe = atmel_nand_nfc_probe,
2414 .remove = atmel_nand_nfc_remove,
2417 static struct platform_driver atmel_nand_driver = {
2418 .probe = atmel_nand_probe,
2419 .remove = atmel_nand_remove,
2421 .name = "atmel_nand",
2422 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
2426 module_platform_driver(atmel_nand_driver);
2428 MODULE_LICENSE("GPL");
2429 MODULE_AUTHOR("Rick Bronson");
2430 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
2431 MODULE_ALIAS("platform:atmel_nand");