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/mtd/mtd.h>
40 #include <linux/mtd/nand.h>
41 #include <linux/mtd/partitions.h>
43 #include <linux/delay.h>
44 #include <linux/dmaengine.h>
45 #include <linux/gpio.h>
46 #include <linux/interrupt.h>
48 #include <linux/platform_data/atmel.h>
50 static int use_dma
= 1;
51 module_param(use_dma
, int, 0);
53 static int on_flash_bbt
= 0;
54 module_param(on_flash_bbt
, int, 0);
56 /* Register access macros */
57 #define ecc_readl(add, reg) \
58 __raw_readl(add + ATMEL_ECC_##reg)
59 #define ecc_writel(add, reg, value) \
60 __raw_writel((value), add + ATMEL_ECC_##reg)
62 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
63 #include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
65 struct atmel_nand_caps
{
66 bool pmecc_correct_erase_page
;
67 uint8_t pmecc_max_correction
;
71 * oob layout for large page size
72 * bad block info is on bytes 0 and 1
73 * the bytes have to be consecutives to avoid
74 * several NAND_CMD_RNDOUT during read
76 * oob layout for small page size
77 * bad block info is on bytes 4 and 5
78 * the bytes have to be consecutives to avoid
79 * several NAND_CMD_RNDOUT during read
81 static int atmel_ooblayout_ecc_sp(struct mtd_info
*mtd
, int section
,
82 struct mtd_oob_region
*oobregion
)
87 oobregion
->length
= 4;
88 oobregion
->offset
= 0;
93 static int atmel_ooblayout_free_sp(struct mtd_info
*mtd
, int section
,
94 struct mtd_oob_region
*oobregion
)
99 oobregion
->offset
= 6;
100 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
105 static const struct mtd_ooblayout_ops atmel_ooblayout_sp_ops
= {
106 .ecc
= atmel_ooblayout_ecc_sp
,
107 .free
= atmel_ooblayout_free_sp
,
111 void __iomem
*base_cmd_regs
;
112 void __iomem
*hsmc_regs
;
114 dma_addr_t sram_bank0_phys
;
121 struct completion comp_ready
;
122 struct completion comp_cmd_done
;
123 struct completion comp_xfer_done
;
125 /* Point to the sram bank which include readed data via NFC */
127 bool will_write_sram
;
129 static struct atmel_nfc nand_nfc
;
131 struct atmel_nand_host
{
132 struct nand_chip nand_chip
;
133 void __iomem
*io_base
;
135 struct atmel_nand_data board
;
139 struct completion comp
;
140 struct dma_chan
*dma_chan
;
142 struct atmel_nfc
*nfc
;
144 const struct atmel_nand_caps
*caps
;
147 u16 pmecc_sector_size
;
148 bool has_no_lookup_table
;
149 u32 pmecc_lookup_table_offset
;
150 u32 pmecc_lookup_table_offset_512
;
151 u32 pmecc_lookup_table_offset_1024
;
153 int pmecc_degree
; /* Degree of remainders */
154 int pmecc_cw_len
; /* Length of codeword */
156 void __iomem
*pmerrloc_base
;
157 void __iomem
*pmerrloc_el_base
;
158 void __iomem
*pmecc_rom_base
;
160 /* lookup table for alpha_to and index_of */
161 void __iomem
*pmecc_alpha_to
;
162 void __iomem
*pmecc_index_of
;
164 /* data for pmecc computation */
165 int16_t *pmecc_partial_syn
;
167 int16_t *pmecc_smu
; /* Sigma table */
168 int16_t *pmecc_lmu
; /* polynomal order */
177 static void atmel_nand_enable(struct atmel_nand_host
*host
)
179 if (gpio_is_valid(host
->board
.enable_pin
))
180 gpio_set_value(host
->board
.enable_pin
, 0);
186 static void atmel_nand_disable(struct atmel_nand_host
*host
)
188 if (gpio_is_valid(host
->board
.enable_pin
))
189 gpio_set_value(host
->board
.enable_pin
, 1);
193 * Hardware specific access to control-lines
195 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
197 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
198 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
200 if (ctrl
& NAND_CTRL_CHANGE
) {
202 atmel_nand_enable(host
);
204 atmel_nand_disable(host
);
206 if (cmd
== NAND_CMD_NONE
)
210 writeb(cmd
, host
->io_base
+ (1 << host
->board
.cle
));
212 writeb(cmd
, host
->io_base
+ (1 << host
->board
.ale
));
216 * Read the Device Ready pin.
218 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
220 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
221 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
223 return gpio_get_value(host
->board
.rdy_pin
) ^
224 !!host
->board
.rdy_pin_active_low
;
227 /* Set up for hardware ready pin and enable pin. */
228 static int atmel_nand_set_enable_ready_pins(struct mtd_info
*mtd
)
230 struct nand_chip
*chip
= mtd_to_nand(mtd
);
231 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
234 if (gpio_is_valid(host
->board
.rdy_pin
)) {
235 res
= devm_gpio_request(host
->dev
,
236 host
->board
.rdy_pin
, "nand_rdy");
239 "can't request rdy gpio %d\n",
240 host
->board
.rdy_pin
);
244 res
= gpio_direction_input(host
->board
.rdy_pin
);
247 "can't request input direction rdy gpio %d\n",
248 host
->board
.rdy_pin
);
252 chip
->dev_ready
= atmel_nand_device_ready
;
255 if (gpio_is_valid(host
->board
.enable_pin
)) {
256 res
= devm_gpio_request(host
->dev
,
257 host
->board
.enable_pin
, "nand_enable");
260 "can't request enable gpio %d\n",
261 host
->board
.enable_pin
);
265 res
= gpio_direction_output(host
->board
.enable_pin
, 1);
268 "can't request output direction enable gpio %d\n",
269 host
->board
.enable_pin
);
278 * Minimal-overhead PIO for data access.
280 static void atmel_read_buf8(struct mtd_info
*mtd
, u8
*buf
, int len
)
282 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
283 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
285 if (host
->nfc
&& host
->nfc
->use_nfc_sram
&& host
->nfc
->data_in_sram
) {
286 memcpy(buf
, host
->nfc
->data_in_sram
, len
);
287 host
->nfc
->data_in_sram
+= len
;
289 __raw_readsb(nand_chip
->IO_ADDR_R
, buf
, len
);
293 static void atmel_read_buf16(struct mtd_info
*mtd
, u8
*buf
, int len
)
295 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
296 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
298 if (host
->nfc
&& host
->nfc
->use_nfc_sram
&& host
->nfc
->data_in_sram
) {
299 memcpy(buf
, host
->nfc
->data_in_sram
, len
);
300 host
->nfc
->data_in_sram
+= len
;
302 __raw_readsw(nand_chip
->IO_ADDR_R
, buf
, len
/ 2);
306 static void atmel_write_buf8(struct mtd_info
*mtd
, const u8
*buf
, int len
)
308 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
310 __raw_writesb(nand_chip
->IO_ADDR_W
, buf
, len
);
313 static void atmel_write_buf16(struct mtd_info
*mtd
, const u8
*buf
, int len
)
315 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
317 __raw_writesw(nand_chip
->IO_ADDR_W
, buf
, len
/ 2);
320 static void dma_complete_func(void *completion
)
322 complete(completion
);
325 static int nfc_set_sram_bank(struct atmel_nand_host
*host
, unsigned int bank
)
327 /* NFC only has two banks. Must be 0 or 1 */
332 struct mtd_info
*mtd
= nand_to_mtd(&host
->nand_chip
);
334 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
335 if (mtd
->writesize
> 2048)
337 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK1
);
339 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK0
);
345 static uint
nfc_get_sram_off(struct atmel_nand_host
*host
)
347 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
348 return NFC_SRAM_BANK1_OFFSET
;
353 static dma_addr_t
nfc_sram_phys(struct atmel_nand_host
*host
)
355 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
356 return host
->nfc
->sram_bank0_phys
+ NFC_SRAM_BANK1_OFFSET
;
358 return host
->nfc
->sram_bank0_phys
;
361 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
364 struct dma_device
*dma_dev
;
365 enum dma_ctrl_flags flags
;
366 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
367 struct dma_async_tx_descriptor
*tx
= NULL
;
369 struct nand_chip
*chip
= mtd_to_nand(mtd
);
370 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
373 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
374 struct atmel_nfc
*nfc
= host
->nfc
;
376 if (buf
>= high_memory
)
379 dma_dev
= host
->dma_chan
->device
;
381 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
383 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
384 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
385 dev_err(host
->dev
, "Failed to dma_map_single\n");
390 if (nfc
&& nfc
->data_in_sram
)
391 dma_src_addr
= nfc_sram_phys(host
) + (nfc
->data_in_sram
392 - (nfc
->sram_bank0
+ nfc_get_sram_off(host
)));
394 dma_src_addr
= host
->io_phys
;
396 dma_dst_addr
= phys_addr
;
398 dma_src_addr
= phys_addr
;
400 if (nfc
&& nfc
->write_by_sram
)
401 dma_dst_addr
= nfc_sram_phys(host
);
403 dma_dst_addr
= host
->io_phys
;
406 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
407 dma_src_addr
, len
, flags
);
409 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
413 init_completion(&host
->comp
);
414 tx
->callback
= dma_complete_func
;
415 tx
->callback_param
= &host
->comp
;
417 cookie
= tx
->tx_submit(tx
);
418 if (dma_submit_error(cookie
)) {
419 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
423 dma_async_issue_pending(host
->dma_chan
);
424 wait_for_completion(&host
->comp
);
426 if (is_read
&& nfc
&& nfc
->data_in_sram
)
427 /* After read data from SRAM, need to increase the position */
428 nfc
->data_in_sram
+= len
;
433 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
436 dev_dbg(host
->dev
, "Fall back to CPU I/O\n");
440 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
442 struct nand_chip
*chip
= mtd_to_nand(mtd
);
444 if (use_dma
&& len
> mtd
->oobsize
)
445 /* only use DMA for bigger than oob size: better performances */
446 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
449 if (chip
->options
& NAND_BUSWIDTH_16
)
450 atmel_read_buf16(mtd
, buf
, len
);
452 atmel_read_buf8(mtd
, buf
, len
);
455 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
457 struct nand_chip
*chip
= mtd_to_nand(mtd
);
459 if (use_dma
&& len
> mtd
->oobsize
)
460 /* only use DMA for bigger than oob size: better performances */
461 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
464 if (chip
->options
& NAND_BUSWIDTH_16
)
465 atmel_write_buf16(mtd
, buf
, len
);
467 atmel_write_buf8(mtd
, buf
, len
);
471 * Return number of ecc bytes per sector according to sector size and
472 * correction capability
474 * Following table shows what at91 PMECC supported:
475 * Correction Capability Sector_512_bytes Sector_1024_bytes
476 * ===================== ================ =================
477 * 2-bits 4-bytes 4-bytes
478 * 4-bits 7-bytes 7-bytes
479 * 8-bits 13-bytes 14-bytes
480 * 12-bits 20-bytes 21-bytes
481 * 24-bits 39-bytes 42-bytes
482 * 32-bits 52-bytes 56-bytes
484 static int pmecc_get_ecc_bytes(int cap
, int sector_size
)
486 int m
= 12 + sector_size
/ 512;
487 return (m
* cap
+ 7) / 8;
490 static void __iomem
*pmecc_get_alpha_to(struct atmel_nand_host
*host
)
494 table_size
= host
->pmecc_sector_size
== 512 ?
495 PMECC_LOOKUP_TABLE_SIZE_512
: PMECC_LOOKUP_TABLE_SIZE_1024
;
497 return host
->pmecc_rom_base
+ host
->pmecc_lookup_table_offset
+
498 table_size
* sizeof(int16_t);
501 static int pmecc_data_alloc(struct atmel_nand_host
*host
)
503 const int cap
= host
->pmecc_corr_cap
;
506 size
= (2 * cap
+ 1) * sizeof(int16_t);
507 host
->pmecc_partial_syn
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
508 host
->pmecc_si
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
509 host
->pmecc_lmu
= devm_kzalloc(host
->dev
,
510 (cap
+ 1) * sizeof(int16_t), GFP_KERNEL
);
511 host
->pmecc_smu
= devm_kzalloc(host
->dev
,
512 (cap
+ 2) * size
, GFP_KERNEL
);
514 size
= (cap
+ 1) * sizeof(int);
515 host
->pmecc_mu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
516 host
->pmecc_dmu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
517 host
->pmecc_delta
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
519 if (!host
->pmecc_partial_syn
||
531 static void pmecc_gen_syndrome(struct mtd_info
*mtd
, int sector
)
533 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
534 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
538 /* Fill odd syndromes */
539 for (i
= 0; i
< host
->pmecc_corr_cap
; i
++) {
540 value
= pmecc_readl_rem_relaxed(host
->ecc
, sector
, i
/ 2);
544 host
->pmecc_partial_syn
[(2 * i
) + 1] = (int16_t)value
;
548 static void pmecc_substitute(struct mtd_info
*mtd
)
550 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
551 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
552 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
553 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
554 int16_t *partial_syn
= host
->pmecc_partial_syn
;
555 const int cap
= host
->pmecc_corr_cap
;
559 /* si[] is a table that holds the current syndrome value,
560 * an element of that table belongs to the field
564 memset(&si
[1], 0, sizeof(int16_t) * (2 * cap
- 1));
566 /* Computation 2t syndromes based on S(x) */
568 for (i
= 1; i
< 2 * cap
; i
+= 2) {
569 for (j
= 0; j
< host
->pmecc_degree
; j
++) {
570 if (partial_syn
[i
] & ((unsigned short)0x1 << j
))
571 si
[i
] = readw_relaxed(alpha_to
+ i
* j
) ^ si
[i
];
574 /* Even syndrome = (Odd syndrome) ** 2 */
575 for (i
= 2, j
= 1; j
<= cap
; i
= ++j
<< 1) {
581 tmp
= readw_relaxed(index_of
+ si
[j
]);
582 tmp
= (tmp
* 2) % host
->pmecc_cw_len
;
583 si
[i
] = readw_relaxed(alpha_to
+ tmp
);
590 static void pmecc_get_sigma(struct mtd_info
*mtd
)
592 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
593 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
595 int16_t *lmu
= host
->pmecc_lmu
;
596 int16_t *si
= host
->pmecc_si
;
597 int *mu
= host
->pmecc_mu
;
598 int *dmu
= host
->pmecc_dmu
; /* Discrepancy */
599 int *delta
= host
->pmecc_delta
; /* Delta order */
600 int cw_len
= host
->pmecc_cw_len
;
601 const int16_t cap
= host
->pmecc_corr_cap
;
602 const int num
= 2 * cap
+ 1;
603 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
604 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
606 uint32_t dmu_0_count
, tmp
;
607 int16_t *smu
= host
->pmecc_smu
;
609 /* index of largest delta */
621 memset(smu
, 0, sizeof(int16_t) * num
);
624 /* discrepancy set to 1 */
626 /* polynom order set to 0 */
628 delta
[0] = (mu
[0] * 2 - lmu
[0]) >> 1;
634 /* Sigma(x) set to 1 */
635 memset(&smu
[num
], 0, sizeof(int16_t) * num
);
638 /* discrepancy set to S1 */
641 /* polynom order set to 0 */
644 delta
[1] = (mu
[1] * 2 - lmu
[1]) >> 1;
646 /* Init the Sigma(x) last row */
647 memset(&smu
[(cap
+ 1) * num
], 0, sizeof(int16_t) * num
);
649 for (i
= 1; i
<= cap
; i
++) {
651 /* Begin Computing Sigma (Mu+1) and L(mu) */
652 /* check if discrepancy is set to 0 */
656 tmp
= ((cap
- (lmu
[i
] >> 1) - 1) / 2);
657 if ((cap
- (lmu
[i
] >> 1) - 1) & 0x1)
662 if (dmu_0_count
== tmp
) {
663 for (j
= 0; j
<= (lmu
[i
] >> 1) + 1; j
++)
664 smu
[(cap
+ 1) * num
+ j
] =
667 lmu
[cap
+ 1] = lmu
[i
];
672 for (j
= 0; j
<= lmu
[i
] >> 1; j
++)
673 smu
[(i
+ 1) * num
+ j
] = smu
[i
* num
+ j
];
675 /* copy previous polynom order to the next */
680 /* find largest delta with dmu != 0 */
681 for (j
= 0; j
< i
; j
++) {
682 if ((dmu
[j
]) && (delta
[j
] > largest
)) {
688 /* compute difference */
689 diff
= (mu
[i
] - mu
[ro
]);
691 /* Compute degree of the new smu polynomial */
692 if ((lmu
[i
] >> 1) > ((lmu
[ro
] >> 1) + diff
))
695 lmu
[i
+ 1] = ((lmu
[ro
] >> 1) + diff
) * 2;
697 /* Init smu[i+1] with 0 */
698 for (k
= 0; k
< num
; k
++)
699 smu
[(i
+ 1) * num
+ k
] = 0;
701 /* Compute smu[i+1] */
702 for (k
= 0; k
<= lmu
[ro
] >> 1; k
++) {
705 if (!(smu
[ro
* num
+ k
] && dmu
[i
]))
707 a
= readw_relaxed(index_of
+ dmu
[i
]);
708 b
= readw_relaxed(index_of
+ dmu
[ro
]);
709 c
= readw_relaxed(index_of
+ smu
[ro
* num
+ k
]);
710 tmp
= a
+ (cw_len
- b
) + c
;
711 a
= readw_relaxed(alpha_to
+ tmp
% cw_len
);
712 smu
[(i
+ 1) * num
+ (k
+ diff
)] = a
;
715 for (k
= 0; k
<= lmu
[i
] >> 1; k
++)
716 smu
[(i
+ 1) * num
+ k
] ^= smu
[i
* num
+ k
];
719 /* End Computing Sigma (Mu+1) and L(mu) */
720 /* In either case compute delta */
721 delta
[i
+ 1] = (mu
[i
+ 1] * 2 - lmu
[i
+ 1]) >> 1;
723 /* Do not compute discrepancy for the last iteration */
727 for (k
= 0; k
<= (lmu
[i
+ 1] >> 1); k
++) {
730 dmu
[i
+ 1] = si
[tmp
+ 3];
731 } else if (smu
[(i
+ 1) * num
+ k
] && si
[tmp
+ 3 - k
]) {
733 a
= readw_relaxed(index_of
+
734 smu
[(i
+ 1) * num
+ k
]);
735 b
= si
[2 * (i
- 1) + 3 - k
];
736 c
= readw_relaxed(index_of
+ b
);
739 dmu
[i
+ 1] = readw_relaxed(alpha_to
+ tmp
) ^
748 static int pmecc_err_location(struct mtd_info
*mtd
)
750 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
751 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
752 unsigned long end_time
;
753 const int cap
= host
->pmecc_corr_cap
;
754 const int num
= 2 * cap
+ 1;
755 int sector_size
= host
->pmecc_sector_size
;
756 int err_nbr
= 0; /* number of error */
757 int roots_nbr
; /* number of roots */
760 int16_t *smu
= host
->pmecc_smu
;
762 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
, PMERRLOC_DISABLE
);
764 for (i
= 0; i
<= host
->pmecc_lmu
[cap
+ 1] >> 1; i
++) {
765 pmerrloc_writel_sigma_relaxed(host
->pmerrloc_base
, i
,
766 smu
[(cap
+ 1) * num
+ i
]);
770 val
= (err_nbr
- 1) << 16;
771 if (sector_size
== 1024)
774 pmerrloc_writel(host
->pmerrloc_base
, ELCFG
, val
);
775 pmerrloc_writel(host
->pmerrloc_base
, ELEN
,
776 sector_size
* 8 + host
->pmecc_degree
* cap
);
778 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
779 while (!(pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
780 & PMERRLOC_CALC_DONE
)) {
781 if (unlikely(time_after(jiffies
, end_time
))) {
782 dev_err(host
->dev
, "PMECC: Timeout to calculate error location.\n");
788 roots_nbr
= (pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
789 & PMERRLOC_ERR_NUM_MASK
) >> 8;
790 /* Number of roots == degree of smu hence <= cap */
791 if (roots_nbr
== host
->pmecc_lmu
[cap
+ 1] >> 1)
794 /* Number of roots does not match the degree of smu
795 * unable to correct error */
799 static void pmecc_correct_data(struct mtd_info
*mtd
, uint8_t *buf
, uint8_t *ecc
,
800 int sector_num
, int extra_bytes
, int err_nbr
)
802 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
803 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
805 int byte_pos
, bit_pos
, sector_size
, pos
;
809 sector_size
= host
->pmecc_sector_size
;
812 tmp
= pmerrloc_readl_el_relaxed(host
->pmerrloc_el_base
, i
) - 1;
816 if (byte_pos
>= (sector_size
+ extra_bytes
))
817 BUG(); /* should never happen */
819 if (byte_pos
< sector_size
) {
820 err_byte
= *(buf
+ byte_pos
);
821 *(buf
+ byte_pos
) ^= (1 << bit_pos
);
823 pos
= sector_num
* host
->pmecc_sector_size
+ byte_pos
;
824 dev_dbg(host
->dev
, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
825 pos
, bit_pos
, err_byte
, *(buf
+ byte_pos
));
827 struct mtd_oob_region oobregion
;
829 /* Bit flip in OOB area */
830 tmp
= sector_num
* nand_chip
->ecc
.bytes
831 + (byte_pos
- sector_size
);
833 ecc
[tmp
] ^= (1 << bit_pos
);
835 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
836 pos
= tmp
+ oobregion
.offset
;
837 dev_dbg(host
->dev
, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
838 pos
, bit_pos
, err_byte
, ecc
[tmp
]);
848 static int pmecc_correction(struct mtd_info
*mtd
, u32 pmecc_stat
, uint8_t *buf
,
851 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
852 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
855 int max_bitflips
= 0;
857 for (i
= 0; i
< nand_chip
->ecc
.steps
; i
++) {
859 if (pmecc_stat
& 0x1) {
860 buf_pos
= buf
+ i
* host
->pmecc_sector_size
;
862 pmecc_gen_syndrome(mtd
, i
);
863 pmecc_substitute(mtd
);
864 pmecc_get_sigma(mtd
);
866 err_nbr
= pmecc_err_location(mtd
);
868 pmecc_correct_data(mtd
, buf_pos
, ecc
, i
,
869 nand_chip
->ecc
.bytes
,
871 } else if (!host
->caps
->pmecc_correct_erase_page
) {
872 u8
*ecc_pos
= ecc
+ (i
* nand_chip
->ecc
.bytes
);
874 /* Try to detect erased pages */
875 err_nbr
= nand_check_erased_ecc_chunk(buf_pos
,
876 host
->pmecc_sector_size
,
878 nand_chip
->ecc
.bytes
,
880 nand_chip
->ecc
.strength
);
884 dev_err(host
->dev
, "PMECC: Too many errors\n");
885 mtd
->ecc_stats
.failed
++;
889 mtd
->ecc_stats
.corrected
+= err_nbr
;
890 max_bitflips
= max_t(int, max_bitflips
, err_nbr
);
898 static void pmecc_enable(struct atmel_nand_host
*host
, int ecc_op
)
902 if (ecc_op
!= NAND_ECC_READ
&& ecc_op
!= NAND_ECC_WRITE
) {
903 dev_err(host
->dev
, "atmel_nand: wrong pmecc operation type!");
907 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
908 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
909 val
= pmecc_readl_relaxed(host
->ecc
, CFG
);
911 if (ecc_op
== NAND_ECC_READ
)
912 pmecc_writel(host
->ecc
, CFG
, (val
& ~PMECC_CFG_WRITE_OP
)
913 | PMECC_CFG_AUTO_ENABLE
);
915 pmecc_writel(host
->ecc
, CFG
, (val
| PMECC_CFG_WRITE_OP
)
916 & ~PMECC_CFG_AUTO_ENABLE
);
918 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
919 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DATA
);
922 static int atmel_nand_pmecc_read_page(struct mtd_info
*mtd
,
923 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
925 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
926 int eccsize
= chip
->ecc
.size
* chip
->ecc
.steps
;
927 uint8_t *oob
= chip
->oob_poi
;
929 unsigned long end_time
;
932 if (!host
->nfc
|| !host
->nfc
->use_nfc_sram
)
933 pmecc_enable(host
, NAND_ECC_READ
);
935 chip
->read_buf(mtd
, buf
, eccsize
);
936 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
938 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
939 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
940 if (unlikely(time_after(jiffies
, end_time
))) {
941 dev_err(host
->dev
, "PMECC: Timeout to get error status.\n");
947 stat
= pmecc_readl_relaxed(host
->ecc
, ISR
);
949 struct mtd_oob_region oobregion
;
951 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
952 bitflips
= pmecc_correction(mtd
, stat
, buf
,
953 &oob
[oobregion
.offset
]);
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 struct mtd_oob_region oobregion
= { };
968 int i
, j
, section
= 0;
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
++) {
987 if (!oobregion
.length
)
988 mtd_ooblayout_ecc(mtd
, section
, &oobregion
);
990 chip
->oob_poi
[oobregion
.offset
] =
991 pmecc_readb_ecc_relaxed(host
->ecc
, i
, j
);
997 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1002 static void atmel_pmecc_core_init(struct mtd_info
*mtd
)
1004 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1005 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1006 int eccbytes
= mtd_ooblayout_count_eccbytes(mtd
);
1008 struct mtd_oob_region oobregion
;
1010 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
1011 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
1013 switch (host
->pmecc_corr_cap
) {
1015 val
= PMECC_CFG_BCH_ERR2
;
1018 val
= PMECC_CFG_BCH_ERR4
;
1021 val
= PMECC_CFG_BCH_ERR8
;
1024 val
= PMECC_CFG_BCH_ERR12
;
1027 val
= PMECC_CFG_BCH_ERR24
;
1030 val
= PMECC_CFG_BCH_ERR32
;
1034 if (host
->pmecc_sector_size
== 512)
1035 val
|= PMECC_CFG_SECTOR512
;
1036 else if (host
->pmecc_sector_size
== 1024)
1037 val
|= PMECC_CFG_SECTOR1024
;
1039 switch (nand_chip
->ecc
.steps
) {
1041 val
|= PMECC_CFG_PAGE_1SECTOR
;
1044 val
|= PMECC_CFG_PAGE_2SECTORS
;
1047 val
|= PMECC_CFG_PAGE_4SECTORS
;
1050 val
|= PMECC_CFG_PAGE_8SECTORS
;
1054 val
|= (PMECC_CFG_READ_OP
| PMECC_CFG_SPARE_DISABLE
1055 | PMECC_CFG_AUTO_DISABLE
);
1056 pmecc_writel(host
->ecc
, CFG
, val
);
1058 pmecc_writel(host
->ecc
, SAREA
, mtd
->oobsize
- 1);
1059 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
1060 pmecc_writel(host
->ecc
, SADDR
, oobregion
.offset
);
1061 pmecc_writel(host
->ecc
, EADDR
,
1062 oobregion
.offset
+ eccbytes
- 1);
1063 /* See datasheet about PMECC Clock Control Register */
1064 pmecc_writel(host
->ecc
, CLK
, 2);
1065 pmecc_writel(host
->ecc
, IDR
, 0xff);
1066 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
1070 * Get minimum ecc requirements from NAND.
1071 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1072 * will set them according to minimum ecc requirement. Otherwise, use the
1073 * value in DTS file.
1074 * return 0 if success. otherwise return error code.
1076 static int pmecc_choose_ecc(struct atmel_nand_host
*host
,
1077 int *cap
, int *sector_size
)
1079 /* Get minimum ECC requirements */
1080 if (host
->nand_chip
.ecc_strength_ds
) {
1081 *cap
= host
->nand_chip
.ecc_strength_ds
;
1082 *sector_size
= host
->nand_chip
.ecc_step_ds
;
1083 dev_info(host
->dev
, "minimum ECC: %d bits in %d bytes\n",
1084 *cap
, *sector_size
);
1088 dev_info(host
->dev
, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
1091 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
1092 if (host
->pmecc_corr_cap
== 0) {
1093 if (*cap
> host
->caps
->pmecc_max_correction
)
1096 /* use the most fitable ecc bits (the near bigger one ) */
1098 host
->pmecc_corr_cap
= 2;
1100 host
->pmecc_corr_cap
= 4;
1102 host
->pmecc_corr_cap
= 8;
1103 else if (*cap
<= 12)
1104 host
->pmecc_corr_cap
= 12;
1105 else if (*cap
<= 24)
1106 host
->pmecc_corr_cap
= 24;
1107 else if (*cap
<= 32)
1108 host
->pmecc_corr_cap
= 32;
1112 if (host
->pmecc_sector_size
== 0) {
1113 /* use the most fitable sector size (the near smaller one ) */
1114 if (*sector_size
>= 1024)
1115 host
->pmecc_sector_size
= 1024;
1116 else if (*sector_size
>= 512)
1117 host
->pmecc_sector_size
= 512;
1124 static inline int deg(unsigned int poly
)
1126 /* polynomial degree is the most-significant bit index */
1127 return fls(poly
) - 1;
1130 static int build_gf_tables(int mm
, unsigned int poly
,
1131 int16_t *index_of
, int16_t *alpha_to
)
1133 unsigned int i
, x
= 1;
1134 const unsigned int k
= 1 << deg(poly
);
1135 unsigned int nn
= (1 << mm
) - 1;
1137 /* primitive polynomial must be of degree m */
1138 if (k
!= (1u << mm
))
1141 for (i
= 0; i
< nn
; i
++) {
1145 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1157 static uint16_t *create_lookup_table(struct device
*dev
, int sector_size
)
1159 int degree
= (sector_size
== 512) ?
1160 PMECC_GF_DIMENSION_13
:
1161 PMECC_GF_DIMENSION_14
;
1162 unsigned int poly
= (sector_size
== 512) ?
1163 PMECC_GF_13_PRIMITIVE_POLY
:
1164 PMECC_GF_14_PRIMITIVE_POLY
;
1165 int table_size
= (sector_size
== 512) ?
1166 PMECC_LOOKUP_TABLE_SIZE_512
:
1167 PMECC_LOOKUP_TABLE_SIZE_1024
;
1169 int16_t *addr
= devm_kzalloc(dev
, 2 * table_size
* sizeof(uint16_t),
1171 if (addr
&& build_gf_tables(degree
, poly
, addr
, addr
+ table_size
))
1177 static int atmel_pmecc_nand_init_params(struct platform_device
*pdev
,
1178 struct atmel_nand_host
*host
)
1180 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1181 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
1182 struct resource
*regs
, *regs_pmerr
, *regs_rom
;
1183 uint16_t *galois_table
;
1184 int cap
, sector_size
, err_no
;
1186 err_no
= pmecc_choose_ecc(host
, &cap
, §or_size
);
1188 dev_err(host
->dev
, "The NAND flash's ECC requirement are not support!");
1192 if (cap
> host
->pmecc_corr_cap
||
1193 sector_size
!= host
->pmecc_sector_size
)
1194 dev_info(host
->dev
, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
1196 cap
= host
->pmecc_corr_cap
;
1197 sector_size
= host
->pmecc_sector_size
;
1198 host
->pmecc_lookup_table_offset
= (sector_size
== 512) ?
1199 host
->pmecc_lookup_table_offset_512
:
1200 host
->pmecc_lookup_table_offset_1024
;
1202 dev_info(host
->dev
, "Initialize PMECC params, cap: %d, sector: %d\n",
1205 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1208 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1209 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1210 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1214 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1215 if (IS_ERR(host
->ecc
)) {
1216 err_no
= PTR_ERR(host
->ecc
);
1220 regs_pmerr
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1221 host
->pmerrloc_base
= devm_ioremap_resource(&pdev
->dev
, regs_pmerr
);
1222 if (IS_ERR(host
->pmerrloc_base
)) {
1223 err_no
= PTR_ERR(host
->pmerrloc_base
);
1226 host
->pmerrloc_el_base
= host
->pmerrloc_base
+ ATMEL_PMERRLOC_SIGMAx
+
1227 (host
->caps
->pmecc_max_correction
+ 1) * 4;
1229 if (!host
->has_no_lookup_table
) {
1230 regs_rom
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
1231 host
->pmecc_rom_base
= devm_ioremap_resource(&pdev
->dev
,
1233 if (IS_ERR(host
->pmecc_rom_base
)) {
1234 dev_err(host
->dev
, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
1235 host
->has_no_lookup_table
= true;
1239 if (host
->has_no_lookup_table
) {
1240 /* Build the look-up table in runtime */
1241 galois_table
= create_lookup_table(host
->dev
, sector_size
);
1242 if (!galois_table
) {
1243 dev_err(host
->dev
, "Failed to build a lookup table in runtime!\n");
1248 host
->pmecc_rom_base
= (void __iomem
*)galois_table
;
1249 host
->pmecc_lookup_table_offset
= 0;
1252 nand_chip
->ecc
.size
= sector_size
;
1254 /* set ECC page size and oob layout */
1255 switch (mtd
->writesize
) {
1261 if (sector_size
> mtd
->writesize
) {
1262 dev_err(host
->dev
, "pmecc sector size is bigger than the page size!\n");
1267 host
->pmecc_degree
= (sector_size
== 512) ?
1268 PMECC_GF_DIMENSION_13
: PMECC_GF_DIMENSION_14
;
1269 host
->pmecc_cw_len
= (1 << host
->pmecc_degree
) - 1;
1270 host
->pmecc_alpha_to
= pmecc_get_alpha_to(host
);
1271 host
->pmecc_index_of
= host
->pmecc_rom_base
+
1272 host
->pmecc_lookup_table_offset
;
1274 nand_chip
->ecc
.strength
= cap
;
1275 nand_chip
->ecc
.bytes
= pmecc_get_ecc_bytes(cap
, sector_size
);
1276 nand_chip
->ecc
.steps
= mtd
->writesize
/ sector_size
;
1277 nand_chip
->ecc
.total
= nand_chip
->ecc
.bytes
*
1278 nand_chip
->ecc
.steps
;
1279 if (nand_chip
->ecc
.total
>
1280 mtd
->oobsize
- PMECC_OOB_RESERVED_BYTES
) {
1281 dev_err(host
->dev
, "No room for ECC bytes\n");
1286 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1290 "Unsupported page size for PMECC, use Software ECC\n");
1291 /* page size not handled by HW ECC */
1292 /* switching back to soft ECC */
1293 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1294 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1298 /* Allocate data for PMECC computation */
1299 err_no
= pmecc_data_alloc(host
);
1302 "Cannot allocate memory for PMECC computation!\n");
1306 nand_chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
1307 nand_chip
->ecc
.read_page
= atmel_nand_pmecc_read_page
;
1308 nand_chip
->ecc
.write_page
= atmel_nand_pmecc_write_page
;
1310 atmel_pmecc_core_init(mtd
);
1321 * function called after a write
1323 * mtd: MTD block structure
1324 * dat: raw data (unused)
1325 * ecc_code: buffer for ECC
1327 static int atmel_nand_calculate(struct mtd_info
*mtd
,
1328 const u_char
*dat
, unsigned char *ecc_code
)
1330 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1331 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1332 unsigned int ecc_value
;
1334 /* get the first 2 ECC bytes */
1335 ecc_value
= ecc_readl(host
->ecc
, PR
);
1337 ecc_code
[0] = ecc_value
& 0xFF;
1338 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
1340 /* get the last 2 ECC bytes */
1341 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
1343 ecc_code
[2] = ecc_value
& 0xFF;
1344 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
1350 * HW ECC read page function
1352 * mtd: mtd info structure
1353 * chip: nand chip info structure
1354 * buf: buffer to store read data
1355 * oob_required: caller expects OOB data read to chip->oob_poi
1357 static int atmel_nand_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1358 uint8_t *buf
, int oob_required
, int page
)
1360 int eccsize
= chip
->ecc
.size
;
1361 int eccbytes
= chip
->ecc
.bytes
;
1363 uint8_t *oob
= chip
->oob_poi
;
1366 unsigned int max_bitflips
= 0;
1367 struct mtd_oob_region oobregion
= {};
1370 * Errata: ALE is incorrectly wired up to the ECC controller
1371 * on the AP7000, so it will include the address cycles in the
1374 * Workaround: Reset the parity registers before reading the
1377 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
1378 if (host
->board
.need_reset_workaround
)
1379 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1382 chip
->read_buf(mtd
, p
, eccsize
);
1384 /* move to ECC position if needed */
1385 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
1386 if (oobregion
.offset
!= 0) {
1388 * This only works on large pages because the ECC controller
1389 * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT.
1390 * Anyway, for small pages, the first ECC byte is at offset
1391 * 0 in the OOB area.
1393 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
1394 mtd
->writesize
+ oobregion
.offset
, -1);
1397 /* the ECC controller needs to read the ECC just after the data */
1398 ecc_pos
= oob
+ oobregion
.offset
;
1399 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
1401 /* check if there's an error */
1402 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
1405 mtd
->ecc_stats
.failed
++;
1407 mtd
->ecc_stats
.corrected
+= stat
;
1408 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1411 /* get back to oob start (end of page) */
1412 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
1415 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
1417 return max_bitflips
;
1423 * function called after a read
1425 * mtd: MTD block structure
1426 * dat: raw data read from the chip
1427 * read_ecc: ECC from the chip (unused)
1430 * Detect and correct a 1 bit error for a page
1432 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
1433 u_char
*read_ecc
, u_char
*isnull
)
1435 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1436 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1437 unsigned int ecc_status
;
1438 unsigned int ecc_word
, ecc_bit
;
1440 /* get the status from the Status Register */
1441 ecc_status
= ecc_readl(host
->ecc
, SR
);
1443 /* if there's no error */
1444 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
1447 /* get error bit offset (4 bits) */
1448 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
1449 /* get word address (12 bits) */
1450 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
1453 /* if there are multiple errors */
1454 if (ecc_status
& ATMEL_ECC_MULERR
) {
1455 /* check if it is a freshly erased block
1456 * (filled with 0xff) */
1457 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
1458 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
1459 /* the block has just been erased, return OK */
1462 /* it doesn't seems to be a freshly
1464 * We can't correct so many errors */
1465 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
1466 " Unable to correct.\n");
1470 /* if there's a single bit error : we can correct it */
1471 if (ecc_status
& ATMEL_ECC_ECCERR
) {
1472 /* there's nothing much to do here.
1473 * the bit error is on the ECC itself.
1475 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
1476 " Nothing to correct\n");
1480 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
1481 " (word offset in the page :"
1482 " 0x%x bit offset : 0x%x)\n",
1484 /* correct the error */
1485 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
1487 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
1490 dat
[ecc_word
] ^= (1 << ecc_bit
);
1492 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
1497 * Enable HW ECC : unused on most chips
1499 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
1501 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1502 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1504 if (host
->board
.need_reset_workaround
)
1505 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1508 static int atmel_of_init_ecc(struct atmel_nand_host
*host
,
1509 struct device_node
*np
)
1514 host
->has_pmecc
= of_property_read_bool(np
, "atmel,has-pmecc");
1516 /* Not using PMECC */
1517 if (!(host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) || !host
->has_pmecc
)
1520 /* use PMECC, get correction capability, sector size and lookup
1522 * If correction bits and sector size are not specified, then find
1523 * them from NAND ONFI parameters.
1525 if (of_property_read_u32(np
, "atmel,pmecc-cap", &val
) == 0) {
1526 if (val
> host
->caps
->pmecc_max_correction
) {
1528 "Required ECC strength too high: %u max %u\n",
1529 val
, host
->caps
->pmecc_max_correction
);
1532 if ((val
!= 2) && (val
!= 4) && (val
!= 8) &&
1533 (val
!= 12) && (val
!= 24) && (val
!= 32)) {
1535 "Required ECC strength not supported: %u\n",
1539 host
->pmecc_corr_cap
= (u8
)val
;
1542 if (of_property_read_u32(np
, "atmel,pmecc-sector-size", &val
) == 0) {
1543 if ((val
!= 512) && (val
!= 1024)) {
1545 "Required ECC sector size not supported: %u\n",
1549 host
->pmecc_sector_size
= (u16
)val
;
1552 if (of_property_read_u32_array(np
, "atmel,pmecc-lookup-table-offset",
1554 dev_err(host
->dev
, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1555 host
->has_no_lookup_table
= true;
1556 /* Will build a lookup table and initialize the offset later */
1560 if (!offset
[0] && !offset
[1]) {
1561 dev_err(host
->dev
, "Invalid PMECC lookup table offset\n");
1565 host
->pmecc_lookup_table_offset_512
= offset
[0];
1566 host
->pmecc_lookup_table_offset_1024
= offset
[1];
1571 static int atmel_of_init_port(struct atmel_nand_host
*host
,
1572 struct device_node
*np
)
1575 struct atmel_nand_data
*board
= &host
->board
;
1576 enum of_gpio_flags flags
= 0;
1578 host
->caps
= (struct atmel_nand_caps
*)
1579 of_device_get_match_data(host
->dev
);
1581 if (of_property_read_u32(np
, "atmel,nand-addr-offset", &val
) == 0) {
1583 dev_err(host
->dev
, "invalid addr-offset %u\n", val
);
1589 if (of_property_read_u32(np
, "atmel,nand-cmd-offset", &val
) == 0) {
1591 dev_err(host
->dev
, "invalid cmd-offset %u\n", val
);
1597 board
->has_dma
= of_property_read_bool(np
, "atmel,nand-has-dma");
1599 board
->rdy_pin
= of_get_gpio_flags(np
, 0, &flags
);
1600 board
->rdy_pin_active_low
= (flags
== OF_GPIO_ACTIVE_LOW
);
1602 board
->enable_pin
= of_get_gpio(np
, 1);
1603 board
->det_pin
= of_get_gpio(np
, 2);
1605 /* load the nfc driver if there is */
1606 of_platform_populate(np
, NULL
, NULL
, host
->dev
);
1609 * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value
1610 * even if the nand-ecc-mode property is not defined.
1612 host
->nand_chip
.ecc
.mode
= NAND_ECC_SOFT
;
1613 host
->nand_chip
.ecc
.algo
= NAND_ECC_HAMMING
;
1618 static int atmel_hw_nand_init_params(struct platform_device
*pdev
,
1619 struct atmel_nand_host
*host
)
1621 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1622 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
1623 struct resource
*regs
;
1625 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1628 "Can't get I/O resource regs, use software ECC\n");
1629 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1630 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1634 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1635 if (IS_ERR(host
->ecc
))
1636 return PTR_ERR(host
->ecc
);
1638 /* ECC is calculated for the whole page (1 step) */
1639 nand_chip
->ecc
.size
= mtd
->writesize
;
1641 /* set ECC page size and oob layout */
1642 switch (mtd
->writesize
) {
1644 mtd_set_ooblayout(mtd
, &atmel_ooblayout_sp_ops
);
1645 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
1648 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1649 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
1652 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1653 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
1656 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1657 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
1660 /* page size not handled by HW ECC */
1661 /* switching back to soft ECC */
1662 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1663 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1667 /* set up for HW ECC */
1668 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
1669 nand_chip
->ecc
.correct
= atmel_nand_correct
;
1670 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
1671 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
1672 nand_chip
->ecc
.bytes
= 4;
1673 nand_chip
->ecc
.strength
= 1;
1678 static inline u32
nfc_read_status(struct atmel_nand_host
*host
)
1680 u32 err_flags
= NFC_SR_DTOE
| NFC_SR_UNDEF
| NFC_SR_AWB
| NFC_SR_ASE
;
1681 u32 nfc_status
= nfc_readl(host
->nfc
->hsmc_regs
, SR
);
1683 if (unlikely(nfc_status
& err_flags
)) {
1684 if (nfc_status
& NFC_SR_DTOE
)
1685 dev_err(host
->dev
, "NFC: Waiting Nand R/B Timeout Error\n");
1686 else if (nfc_status
& NFC_SR_UNDEF
)
1687 dev_err(host
->dev
, "NFC: Access Undefined Area Error\n");
1688 else if (nfc_status
& NFC_SR_AWB
)
1689 dev_err(host
->dev
, "NFC: Access memory While NFC is busy\n");
1690 else if (nfc_status
& NFC_SR_ASE
)
1691 dev_err(host
->dev
, "NFC: Access memory Size Error\n");
1697 /* SMC interrupt service routine */
1698 static irqreturn_t
hsmc_interrupt(int irq
, void *dev_id
)
1700 struct atmel_nand_host
*host
= dev_id
;
1701 u32 status
, mask
, pending
;
1702 irqreturn_t ret
= IRQ_NONE
;
1704 status
= nfc_read_status(host
);
1705 mask
= nfc_readl(host
->nfc
->hsmc_regs
, IMR
);
1706 pending
= status
& mask
;
1708 if (pending
& NFC_SR_XFR_DONE
) {
1709 complete(&host
->nfc
->comp_xfer_done
);
1710 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_XFR_DONE
);
1713 if (pending
& NFC_SR_RB_EDGE
) {
1714 complete(&host
->nfc
->comp_ready
);
1715 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_RB_EDGE
);
1718 if (pending
& NFC_SR_CMD_DONE
) {
1719 complete(&host
->nfc
->comp_cmd_done
);
1720 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_CMD_DONE
);
1727 /* NFC(Nand Flash Controller) related functions */
1728 static void nfc_prepare_interrupt(struct atmel_nand_host
*host
, u32 flag
)
1730 if (flag
& NFC_SR_XFR_DONE
)
1731 init_completion(&host
->nfc
->comp_xfer_done
);
1733 if (flag
& NFC_SR_RB_EDGE
)
1734 init_completion(&host
->nfc
->comp_ready
);
1736 if (flag
& NFC_SR_CMD_DONE
)
1737 init_completion(&host
->nfc
->comp_cmd_done
);
1739 /* Enable interrupt that need to wait for */
1740 nfc_writel(host
->nfc
->hsmc_regs
, IER
, flag
);
1743 static int nfc_wait_interrupt(struct atmel_nand_host
*host
, u32 flag
)
1746 struct completion
*comp
[3]; /* Support 3 interrupt completion */
1748 if (flag
& NFC_SR_XFR_DONE
)
1749 comp
[index
++] = &host
->nfc
->comp_xfer_done
;
1751 if (flag
& NFC_SR_RB_EDGE
)
1752 comp
[index
++] = &host
->nfc
->comp_ready
;
1754 if (flag
& NFC_SR_CMD_DONE
)
1755 comp
[index
++] = &host
->nfc
->comp_cmd_done
;
1758 dev_err(host
->dev
, "Unknown interrupt flag: 0x%08x\n", flag
);
1762 for (i
= 0; i
< index
; i
++) {
1763 if (wait_for_completion_timeout(comp
[i
],
1764 msecs_to_jiffies(NFC_TIME_OUT_MS
)))
1765 continue; /* wait for next completion */
1773 dev_err(host
->dev
, "Time out to wait for interrupt: 0x%08x\n", flag
);
1774 /* Disable the interrupt as it is not handled by interrupt handler */
1775 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, flag
);
1779 static int nfc_send_command(struct atmel_nand_host
*host
,
1780 unsigned int cmd
, unsigned int addr
, unsigned char cycle0
)
1782 unsigned long timeout
;
1783 u32 flag
= NFC_SR_CMD_DONE
;
1784 flag
|= cmd
& NFCADDR_CMD_DATAEN
? NFC_SR_XFR_DONE
: 0;
1787 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1790 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1791 while (nfc_readl(host
->nfc
->hsmc_regs
, SR
) & NFC_SR_BUSY
) {
1792 if (time_after(jiffies
, timeout
)) {
1794 "Time out to wait for NFC ready!\n");
1799 nfc_prepare_interrupt(host
, flag
);
1800 nfc_writel(host
->nfc
->hsmc_regs
, CYCLE0
, cycle0
);
1801 nfc_cmd_addr1234_writel(cmd
, addr
, host
->nfc
->base_cmd_regs
);
1802 return nfc_wait_interrupt(host
, flag
);
1805 static int nfc_device_ready(struct mtd_info
*mtd
)
1808 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1809 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1811 status
= nfc_read_status(host
);
1812 mask
= nfc_readl(host
->nfc
->hsmc_regs
, IMR
);
1814 /* The mask should be 0. If not we may lost interrupts */
1815 if (unlikely(mask
& status
))
1816 dev_err(host
->dev
, "Lost the interrupt flags: 0x%08x\n",
1819 return status
& NFC_SR_RB_EDGE
;
1822 static void nfc_select_chip(struct mtd_info
*mtd
, int chip
)
1824 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1825 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1828 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_DISABLE
);
1830 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_ENABLE
);
1833 static int nfc_make_addr(struct mtd_info
*mtd
, int command
, int column
,
1834 int page_addr
, unsigned int *addr1234
, unsigned int *cycle0
)
1836 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1839 unsigned char addr_bytes
[8];
1840 int index
= 0, bit_shift
;
1842 BUG_ON(addr1234
== NULL
|| cycle0
== NULL
);
1848 if (chip
->options
& NAND_BUSWIDTH_16
&&
1849 !nand_opcode_8bits(command
))
1851 addr_bytes
[acycle
++] = column
& 0xff;
1852 if (mtd
->writesize
> 512)
1853 addr_bytes
[acycle
++] = (column
>> 8) & 0xff;
1856 if (page_addr
!= -1) {
1857 addr_bytes
[acycle
++] = page_addr
& 0xff;
1858 addr_bytes
[acycle
++] = (page_addr
>> 8) & 0xff;
1859 if (chip
->chipsize
> (128 << 20))
1860 addr_bytes
[acycle
++] = (page_addr
>> 16) & 0xff;
1864 *cycle0
= addr_bytes
[index
++];
1866 for (bit_shift
= 0; index
< acycle
; bit_shift
+= 8)
1867 *addr1234
+= addr_bytes
[index
++] << bit_shift
;
1869 /* return acycle in cmd register */
1870 return acycle
<< NFCADDR_CMD_ACYCLE_BIT_POS
;
1873 static void nfc_nand_command(struct mtd_info
*mtd
, unsigned int command
,
1874 int column
, int page_addr
)
1876 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1877 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
1878 unsigned long timeout
;
1879 unsigned int nfc_addr_cmd
= 0;
1881 unsigned int cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1883 /* Set default settings: no cmd2, no addr cycle. read from nand */
1884 unsigned int cmd2
= 0;
1885 unsigned int vcmd2
= 0;
1886 int acycle
= NFCADDR_CMD_ACYCLE_NONE
;
1887 int csid
= NFCADDR_CMD_CSID_3
;
1888 int dataen
= NFCADDR_CMD_DATADIS
;
1889 int nfcwr
= NFCADDR_CMD_NFCRD
;
1890 unsigned int addr1234
= 0;
1891 unsigned int cycle0
= 0;
1892 bool do_addr
= true;
1893 host
->nfc
->data_in_sram
= NULL
;
1895 dev_dbg(host
->dev
, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1896 __func__
, command
, column
, page_addr
);
1899 case NAND_CMD_RESET
:
1900 nfc_addr_cmd
= cmd1
| acycle
| csid
| dataen
| nfcwr
;
1901 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1902 udelay(chip
->chip_delay
);
1904 nfc_nand_command(mtd
, NAND_CMD_STATUS
, -1, -1);
1905 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1906 while (!(chip
->read_byte(mtd
) & NAND_STATUS_READY
)) {
1907 if (time_after(jiffies
, timeout
)) {
1909 "Time out to wait status ready!\n");
1914 case NAND_CMD_STATUS
:
1917 case NAND_CMD_PARAM
:
1918 case NAND_CMD_READID
:
1920 acycle
= NFCADDR_CMD_ACYCLE_1
;
1924 case NAND_CMD_RNDOUT
:
1925 cmd2
= NAND_CMD_RNDOUTSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1926 vcmd2
= NFCADDR_CMD_VCMD2
;
1928 case NAND_CMD_READ0
:
1929 case NAND_CMD_READOOB
:
1930 if (command
== NAND_CMD_READOOB
) {
1931 column
+= mtd
->writesize
;
1932 command
= NAND_CMD_READ0
; /* only READ0 is valid */
1933 cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1935 if (host
->nfc
->use_nfc_sram
) {
1936 /* Enable Data transfer to sram */
1937 dataen
= NFCADDR_CMD_DATAEN
;
1939 /* Need enable PMECC now, since NFC will transfer
1940 * data in bus after sending nfc read command.
1942 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
1943 pmecc_enable(host
, NAND_ECC_READ
);
1946 cmd2
= NAND_CMD_READSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1947 vcmd2
= NFCADDR_CMD_VCMD2
;
1949 /* For prgramming command, the cmd need set to write enable */
1950 case NAND_CMD_PAGEPROG
:
1951 case NAND_CMD_SEQIN
:
1952 case NAND_CMD_RNDIN
:
1953 nfcwr
= NFCADDR_CMD_NFCWR
;
1954 if (host
->nfc
->will_write_sram
&& command
== NAND_CMD_SEQIN
)
1955 dataen
= NFCADDR_CMD_DATAEN
;
1962 acycle
= nfc_make_addr(mtd
, command
, column
, page_addr
,
1963 &addr1234
, &cycle0
);
1965 nfc_addr_cmd
= cmd1
| cmd2
| vcmd2
| acycle
| csid
| dataen
| nfcwr
;
1966 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1969 * Program and erase have their own busy handlers status, sequential
1970 * in, and deplete1 need no delay.
1973 case NAND_CMD_CACHEDPROG
:
1974 case NAND_CMD_PAGEPROG
:
1975 case NAND_CMD_ERASE1
:
1976 case NAND_CMD_ERASE2
:
1977 case NAND_CMD_RNDIN
:
1978 case NAND_CMD_STATUS
:
1979 case NAND_CMD_RNDOUT
:
1980 case NAND_CMD_SEQIN
:
1981 case NAND_CMD_READID
:
1984 case NAND_CMD_READ0
:
1985 if (dataen
== NFCADDR_CMD_DATAEN
) {
1986 host
->nfc
->data_in_sram
= host
->nfc
->sram_bank0
+
1987 nfc_get_sram_off(host
);
1992 nfc_prepare_interrupt(host
, NFC_SR_RB_EDGE
);
1993 nfc_wait_interrupt(host
, NFC_SR_RB_EDGE
);
1997 static int nfc_sram_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1998 uint32_t offset
, int data_len
, const uint8_t *buf
,
1999 int oob_required
, int page
, int cached
, int raw
)
2003 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
2004 void *sram
= host
->nfc
->sram_bank0
+ nfc_get_sram_off(host
);
2006 /* Subpage write is not supported */
2007 if (offset
|| (data_len
< mtd
->writesize
))
2010 len
= mtd
->writesize
;
2011 /* Copy page data to sram that will write to nand via NFC */
2013 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) != 0)
2014 /* Fall back to use cpu copy */
2015 memcpy(sram
, buf
, len
);
2017 memcpy(sram
, buf
, len
);
2020 cfg
= nfc_readl(host
->nfc
->hsmc_regs
, CFG
);
2021 if (unlikely(raw
) && oob_required
) {
2022 memcpy(sram
+ len
, chip
->oob_poi
, mtd
->oobsize
);
2023 len
+= mtd
->oobsize
;
2024 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
| NFC_CFG_WSPARE
);
2026 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
& ~NFC_CFG_WSPARE
);
2029 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
2031 * When use NFC sram, need set up PMECC before send
2032 * NAND_CMD_SEQIN command. Since when the nand command
2033 * is sent, nfc will do transfer from sram and nand.
2035 pmecc_enable(host
, NAND_ECC_WRITE
);
2037 host
->nfc
->will_write_sram
= true;
2038 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, 0x00, page
);
2039 host
->nfc
->will_write_sram
= false;
2042 /* Need to write ecc into oob */
2043 status
= chip
->ecc
.write_page(mtd
, chip
, buf
, oob_required
,
2049 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
2050 status
= chip
->waitfunc(mtd
, chip
);
2052 if ((status
& NAND_STATUS_FAIL
) && (chip
->errstat
))
2053 status
= chip
->errstat(mtd
, chip
, FL_WRITING
, status
, page
);
2055 if (status
& NAND_STATUS_FAIL
)
2061 static int nfc_sram_init(struct mtd_info
*mtd
)
2063 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2064 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
2067 /* Initialize the NFC CFG register */
2068 unsigned int cfg_nfc
= 0;
2070 /* set page size and oob layout */
2071 switch (mtd
->writesize
) {
2073 cfg_nfc
= NFC_CFG_PAGESIZE_512
;
2076 cfg_nfc
= NFC_CFG_PAGESIZE_1024
;
2079 cfg_nfc
= NFC_CFG_PAGESIZE_2048
;
2082 cfg_nfc
= NFC_CFG_PAGESIZE_4096
;
2085 cfg_nfc
= NFC_CFG_PAGESIZE_8192
;
2088 dev_err(host
->dev
, "Unsupported page size for NFC.\n");
2093 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2094 * Max support spare size is 512 bytes. */
2095 cfg_nfc
|= (((mtd
->oobsize
/ 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2096 & NFC_CFG_NFC_SPARESIZE
);
2097 /* default set a max timeout */
2098 cfg_nfc
|= NFC_CFG_RSPARE
|
2099 NFC_CFG_NFC_DTOCYC
| NFC_CFG_NFC_DTOMUL
;
2101 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg_nfc
);
2103 host
->nfc
->will_write_sram
= false;
2104 nfc_set_sram_bank(host
, 0);
2106 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2107 if (host
->nfc
->write_by_sram
) {
2108 if ((chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
) ||
2109 chip
->ecc
.mode
== NAND_ECC_NONE
)
2110 chip
->write_page
= nfc_sram_write_page
;
2112 host
->nfc
->write_by_sram
= false;
2115 dev_info(host
->dev
, "Using NFC Sram read %s\n",
2116 host
->nfc
->write_by_sram
? "and write" : "");
2120 static struct platform_driver atmel_nand_nfc_driver
;
2122 * Probe for the NAND device.
2124 static int atmel_nand_probe(struct platform_device
*pdev
)
2126 struct atmel_nand_host
*host
;
2127 struct mtd_info
*mtd
;
2128 struct nand_chip
*nand_chip
;
2129 struct resource
*mem
;
2132 /* Allocate memory for the device structure (and zero it) */
2133 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
2137 res
= platform_driver_register(&atmel_nand_nfc_driver
);
2139 dev_err(&pdev
->dev
, "atmel_nand: can't register NFC driver\n");
2141 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2142 host
->io_base
= devm_ioremap_resource(&pdev
->dev
, mem
);
2143 if (IS_ERR(host
->io_base
)) {
2144 res
= PTR_ERR(host
->io_base
);
2145 goto err_nand_ioremap
;
2147 host
->io_phys
= (dma_addr_t
)mem
->start
;
2149 nand_chip
= &host
->nand_chip
;
2150 mtd
= nand_to_mtd(nand_chip
);
2151 host
->dev
= &pdev
->dev
;
2152 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
2153 nand_set_flash_node(nand_chip
, pdev
->dev
.of_node
);
2154 /* Only when CONFIG_OF is enabled of_node can be parsed */
2155 res
= atmel_of_init_port(host
, pdev
->dev
.of_node
);
2157 goto err_nand_ioremap
;
2159 memcpy(&host
->board
, dev_get_platdata(&pdev
->dev
),
2160 sizeof(struct atmel_nand_data
));
2161 nand_chip
->ecc
.mode
= host
->board
.ecc_mode
;
2164 * When using software ECC every supported avr32 board means
2165 * Hamming algorithm. If that ever changes we'll need to add
2166 * ecc_algo field to the struct atmel_nand_data.
2168 if (nand_chip
->ecc
.mode
== NAND_ECC_SOFT
)
2169 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
2171 /* 16-bit bus width */
2172 if (host
->board
.bus_width_16
)
2173 nand_chip
->options
|= NAND_BUSWIDTH_16
;
2176 /* link the private data structures */
2177 nand_set_controller_data(nand_chip
, host
);
2178 mtd
->dev
.parent
= &pdev
->dev
;
2180 /* Set address of NAND IO lines */
2181 nand_chip
->IO_ADDR_R
= host
->io_base
;
2182 nand_chip
->IO_ADDR_W
= host
->io_base
;
2184 if (nand_nfc
.is_initialized
) {
2185 /* NFC driver is probed and initialized */
2186 host
->nfc
= &nand_nfc
;
2188 nand_chip
->select_chip
= nfc_select_chip
;
2189 nand_chip
->dev_ready
= nfc_device_ready
;
2190 nand_chip
->cmdfunc
= nfc_nand_command
;
2192 /* Initialize the interrupt for NFC */
2193 irq
= platform_get_irq(pdev
, 0);
2195 dev_err(host
->dev
, "Cannot get HSMC irq!\n");
2197 goto err_nand_ioremap
;
2200 res
= devm_request_irq(&pdev
->dev
, irq
, hsmc_interrupt
,
2203 dev_err(&pdev
->dev
, "Unable to request HSMC irq %d\n",
2205 goto err_nand_ioremap
;
2208 res
= atmel_nand_set_enable_ready_pins(mtd
);
2210 goto err_nand_ioremap
;
2212 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
2215 nand_chip
->chip_delay
= 40; /* 40us command delay time */
2218 nand_chip
->read_buf
= atmel_read_buf
;
2219 nand_chip
->write_buf
= atmel_write_buf
;
2221 platform_set_drvdata(pdev
, host
);
2222 atmel_nand_enable(host
);
2224 if (gpio_is_valid(host
->board
.det_pin
)) {
2225 res
= devm_gpio_request(&pdev
->dev
,
2226 host
->board
.det_pin
, "nand_det");
2229 "can't request det gpio %d\n",
2230 host
->board
.det_pin
);
2234 res
= gpio_direction_input(host
->board
.det_pin
);
2237 "can't request input direction det gpio %d\n",
2238 host
->board
.det_pin
);
2242 if (gpio_get_value(host
->board
.det_pin
)) {
2243 dev_info(&pdev
->dev
, "No SmartMedia card inserted.\n");
2249 if (!host
->board
.has_dma
)
2253 dma_cap_mask_t mask
;
2256 dma_cap_set(DMA_MEMCPY
, mask
);
2257 host
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
2258 if (!host
->dma_chan
) {
2259 dev_err(host
->dev
, "Failed to request DMA channel\n");
2264 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
2265 dma_chan_name(host
->dma_chan
));
2267 dev_info(host
->dev
, "No DMA support for NAND access.\n");
2269 /* first scan to find the device and get the page size */
2270 res
= nand_scan_ident(mtd
, 1, NULL
);
2272 goto err_scan_ident
;
2274 if (host
->board
.on_flash_bbt
|| on_flash_bbt
)
2275 nand_chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
2277 if (nand_chip
->bbt_options
& NAND_BBT_USE_FLASH
)
2278 dev_info(&pdev
->dev
, "Use On Flash BBT\n");
2280 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
2281 res
= atmel_of_init_ecc(host
, pdev
->dev
.of_node
);
2286 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
2287 if (host
->has_pmecc
)
2288 res
= atmel_pmecc_nand_init_params(pdev
, host
);
2290 res
= atmel_hw_nand_init_params(pdev
, host
);
2296 /* initialize the nfc configuration register */
2297 if (host
->nfc
&& host
->nfc
->use_nfc_sram
) {
2298 res
= nfc_sram_init(mtd
);
2300 host
->nfc
->use_nfc_sram
= false;
2301 dev_err(host
->dev
, "Disable use nfc sram for data transfer.\n");
2305 /* second phase scan */
2306 res
= nand_scan_tail(mtd
);
2310 mtd
->name
= "atmel_nand";
2311 res
= mtd_device_register(mtd
, host
->board
.parts
,
2312 host
->board
.num_parts
);
2317 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
)
2318 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2322 atmel_nand_disable(host
);
2324 dma_release_channel(host
->dma_chan
);
2330 * Remove a NAND device.
2332 static int atmel_nand_remove(struct platform_device
*pdev
)
2334 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
2335 struct mtd_info
*mtd
= nand_to_mtd(&host
->nand_chip
);
2339 atmel_nand_disable(host
);
2341 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) {
2342 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2343 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
,
2348 dma_release_channel(host
->dma_chan
);
2350 platform_driver_unregister(&atmel_nand_nfc_driver
);
2356 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2357 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2358 * devices from the SAM9 family that have those.
2360 static const struct atmel_nand_caps at91rm9200_caps
= {
2361 .pmecc_correct_erase_page
= false,
2362 .pmecc_max_correction
= 24,
2365 static const struct atmel_nand_caps sama5d4_caps
= {
2366 .pmecc_correct_erase_page
= true,
2367 .pmecc_max_correction
= 24,
2371 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2372 * as the increased correction strength requires more registers.
2374 static const struct atmel_nand_caps sama5d2_caps
= {
2375 .pmecc_correct_erase_page
= true,
2376 .pmecc_max_correction
= 32,
2379 static const struct of_device_id atmel_nand_dt_ids
[] = {
2380 { .compatible
= "atmel,at91rm9200-nand", .data
= &at91rm9200_caps
},
2381 { .compatible
= "atmel,sama5d4-nand", .data
= &sama5d4_caps
},
2382 { .compatible
= "atmel,sama5d2-nand", .data
= &sama5d2_caps
},
2386 MODULE_DEVICE_TABLE(of
, atmel_nand_dt_ids
);
2388 static int atmel_nand_nfc_probe(struct platform_device
*pdev
)
2390 struct atmel_nfc
*nfc
= &nand_nfc
;
2391 struct resource
*nfc_cmd_regs
, *nfc_hsmc_regs
, *nfc_sram
;
2394 nfc_cmd_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2395 nfc
->base_cmd_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_cmd_regs
);
2396 if (IS_ERR(nfc
->base_cmd_regs
))
2397 return PTR_ERR(nfc
->base_cmd_regs
);
2399 nfc_hsmc_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2400 nfc
->hsmc_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_hsmc_regs
);
2401 if (IS_ERR(nfc
->hsmc_regs
))
2402 return PTR_ERR(nfc
->hsmc_regs
);
2404 nfc_sram
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
2406 nfc
->sram_bank0
= (void * __force
)
2407 devm_ioremap_resource(&pdev
->dev
, nfc_sram
);
2408 if (IS_ERR(nfc
->sram_bank0
)) {
2409 dev_warn(&pdev
->dev
, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2410 PTR_ERR(nfc
->sram_bank0
));
2412 nfc
->use_nfc_sram
= true;
2413 nfc
->sram_bank0_phys
= (dma_addr_t
)nfc_sram
->start
;
2415 if (pdev
->dev
.of_node
)
2416 nfc
->write_by_sram
= of_property_read_bool(
2418 "atmel,write-by-sram");
2422 nfc_writel(nfc
->hsmc_regs
, IDR
, 0xffffffff);
2423 nfc_readl(nfc
->hsmc_regs
, SR
); /* clear the NFC_SR */
2425 nfc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
2426 if (!IS_ERR(nfc
->clk
)) {
2427 ret
= clk_prepare_enable(nfc
->clk
);
2431 dev_warn(&pdev
->dev
, "NFC clock missing, update your Device Tree");
2434 nfc
->is_initialized
= true;
2435 dev_info(&pdev
->dev
, "NFC is probed.\n");
2440 static int atmel_nand_nfc_remove(struct platform_device
*pdev
)
2442 struct atmel_nfc
*nfc
= &nand_nfc
;
2444 if (!IS_ERR(nfc
->clk
))
2445 clk_disable_unprepare(nfc
->clk
);
2450 static const struct of_device_id atmel_nand_nfc_match
[] = {
2451 { .compatible
= "atmel,sama5d3-nfc" },
2454 MODULE_DEVICE_TABLE(of
, atmel_nand_nfc_match
);
2456 static struct platform_driver atmel_nand_nfc_driver
= {
2458 .name
= "atmel_nand_nfc",
2459 .of_match_table
= of_match_ptr(atmel_nand_nfc_match
),
2461 .probe
= atmel_nand_nfc_probe
,
2462 .remove
= atmel_nand_nfc_remove
,
2465 static struct platform_driver atmel_nand_driver
= {
2466 .probe
= atmel_nand_probe
,
2467 .remove
= atmel_nand_remove
,
2469 .name
= "atmel_nand",
2470 .of_match_table
= of_match_ptr(atmel_nand_dt_ids
),
2474 module_platform_driver(atmel_nand_driver
);
2476 MODULE_LICENSE("GPL");
2477 MODULE_AUTHOR("Rick Bronson");
2478 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
2479 MODULE_ALIAS("platform:atmel_nand");