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
;
70 struct atmel_nand_nfc_caps
{
75 * oob layout for large page size
76 * bad block info is on bytes 0 and 1
77 * the bytes have to be consecutives to avoid
78 * several NAND_CMD_RNDOUT during read
80 * oob layout for small page size
81 * bad block info is on bytes 4 and 5
82 * the bytes have to be consecutives to avoid
83 * several NAND_CMD_RNDOUT during read
85 static int atmel_ooblayout_ecc_sp(struct mtd_info
*mtd
, int section
,
86 struct mtd_oob_region
*oobregion
)
91 oobregion
->length
= 4;
92 oobregion
->offset
= 0;
97 static int atmel_ooblayout_free_sp(struct mtd_info
*mtd
, int section
,
98 struct mtd_oob_region
*oobregion
)
103 oobregion
->offset
= 6;
104 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
109 static const struct mtd_ooblayout_ops atmel_ooblayout_sp_ops
= {
110 .ecc
= atmel_ooblayout_ecc_sp
,
111 .free
= atmel_ooblayout_free_sp
,
115 void __iomem
*base_cmd_regs
;
116 void __iomem
*hsmc_regs
;
118 dma_addr_t sram_bank0_phys
;
125 struct completion comp_ready
;
126 struct completion comp_cmd_done
;
127 struct completion comp_xfer_done
;
129 /* Point to the sram bank which include readed data via NFC */
131 bool will_write_sram
;
132 const struct atmel_nand_nfc_caps
*caps
;
134 static struct atmel_nfc nand_nfc
;
136 struct atmel_nand_host
{
137 struct nand_chip nand_chip
;
138 void __iomem
*io_base
;
140 struct atmel_nand_data board
;
144 struct completion comp
;
145 struct dma_chan
*dma_chan
;
147 struct atmel_nfc
*nfc
;
149 const struct atmel_nand_caps
*caps
;
152 u16 pmecc_sector_size
;
153 bool has_no_lookup_table
;
154 u32 pmecc_lookup_table_offset
;
155 u32 pmecc_lookup_table_offset_512
;
156 u32 pmecc_lookup_table_offset_1024
;
158 int pmecc_degree
; /* Degree of remainders */
159 int pmecc_cw_len
; /* Length of codeword */
161 void __iomem
*pmerrloc_base
;
162 void __iomem
*pmerrloc_el_base
;
163 void __iomem
*pmecc_rom_base
;
165 /* lookup table for alpha_to and index_of */
166 void __iomem
*pmecc_alpha_to
;
167 void __iomem
*pmecc_index_of
;
169 /* data for pmecc computation */
170 int16_t *pmecc_partial_syn
;
172 int16_t *pmecc_smu
; /* Sigma table */
173 int16_t *pmecc_lmu
; /* polynomal order */
182 static void atmel_nand_enable(struct atmel_nand_host
*host
)
184 if (gpio_is_valid(host
->board
.enable_pin
))
185 gpio_set_value(host
->board
.enable_pin
, 0);
191 static void atmel_nand_disable(struct atmel_nand_host
*host
)
193 if (gpio_is_valid(host
->board
.enable_pin
))
194 gpio_set_value(host
->board
.enable_pin
, 1);
198 * Hardware specific access to control-lines
200 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
202 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
203 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
205 if (ctrl
& NAND_CTRL_CHANGE
) {
207 atmel_nand_enable(host
);
209 atmel_nand_disable(host
);
211 if (cmd
== NAND_CMD_NONE
)
215 writeb(cmd
, host
->io_base
+ (1 << host
->board
.cle
));
217 writeb(cmd
, host
->io_base
+ (1 << host
->board
.ale
));
221 * Read the Device Ready pin.
223 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
225 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
226 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
228 return gpio_get_value(host
->board
.rdy_pin
) ^
229 !!host
->board
.rdy_pin_active_low
;
232 /* Set up for hardware ready pin and enable pin. */
233 static int atmel_nand_set_enable_ready_pins(struct mtd_info
*mtd
)
235 struct nand_chip
*chip
= mtd_to_nand(mtd
);
236 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
239 if (gpio_is_valid(host
->board
.rdy_pin
)) {
240 res
= devm_gpio_request(host
->dev
,
241 host
->board
.rdy_pin
, "nand_rdy");
244 "can't request rdy gpio %d\n",
245 host
->board
.rdy_pin
);
249 res
= gpio_direction_input(host
->board
.rdy_pin
);
252 "can't request input direction rdy gpio %d\n",
253 host
->board
.rdy_pin
);
257 chip
->dev_ready
= atmel_nand_device_ready
;
260 if (gpio_is_valid(host
->board
.enable_pin
)) {
261 res
= devm_gpio_request(host
->dev
,
262 host
->board
.enable_pin
, "nand_enable");
265 "can't request enable gpio %d\n",
266 host
->board
.enable_pin
);
270 res
= gpio_direction_output(host
->board
.enable_pin
, 1);
273 "can't request output direction enable gpio %d\n",
274 host
->board
.enable_pin
);
283 * Minimal-overhead PIO for data access.
285 static void atmel_read_buf8(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_readsb(nand_chip
->IO_ADDR_R
, buf
, len
);
298 static void atmel_read_buf16(struct mtd_info
*mtd
, u8
*buf
, int len
)
300 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
301 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
303 if (host
->nfc
&& host
->nfc
->use_nfc_sram
&& host
->nfc
->data_in_sram
) {
304 memcpy(buf
, host
->nfc
->data_in_sram
, len
);
305 host
->nfc
->data_in_sram
+= len
;
307 __raw_readsw(nand_chip
->IO_ADDR_R
, buf
, len
/ 2);
311 static void atmel_write_buf8(struct mtd_info
*mtd
, const u8
*buf
, int len
)
313 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
315 __raw_writesb(nand_chip
->IO_ADDR_W
, buf
, len
);
318 static void atmel_write_buf16(struct mtd_info
*mtd
, const u8
*buf
, int len
)
320 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
322 __raw_writesw(nand_chip
->IO_ADDR_W
, buf
, len
/ 2);
325 static void dma_complete_func(void *completion
)
327 complete(completion
);
330 static int nfc_set_sram_bank(struct atmel_nand_host
*host
, unsigned int bank
)
332 /* NFC only has two banks. Must be 0 or 1 */
337 struct mtd_info
*mtd
= nand_to_mtd(&host
->nand_chip
);
339 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
340 if (mtd
->writesize
> 2048)
342 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK1
);
344 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK0
);
350 static uint
nfc_get_sram_off(struct atmel_nand_host
*host
)
352 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
353 return NFC_SRAM_BANK1_OFFSET
;
358 static dma_addr_t
nfc_sram_phys(struct atmel_nand_host
*host
)
360 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
361 return host
->nfc
->sram_bank0_phys
+ NFC_SRAM_BANK1_OFFSET
;
363 return host
->nfc
->sram_bank0_phys
;
366 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
369 struct dma_device
*dma_dev
;
370 enum dma_ctrl_flags flags
;
371 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
372 struct dma_async_tx_descriptor
*tx
= NULL
;
374 struct nand_chip
*chip
= mtd_to_nand(mtd
);
375 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
378 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
379 struct atmel_nfc
*nfc
= host
->nfc
;
381 if (buf
>= high_memory
)
384 dma_dev
= host
->dma_chan
->device
;
386 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
388 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
389 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
390 dev_err(host
->dev
, "Failed to dma_map_single\n");
395 if (nfc
&& nfc
->data_in_sram
)
396 dma_src_addr
= nfc_sram_phys(host
) + (nfc
->data_in_sram
397 - (nfc
->sram_bank0
+ nfc_get_sram_off(host
)));
399 dma_src_addr
= host
->io_phys
;
401 dma_dst_addr
= phys_addr
;
403 dma_src_addr
= phys_addr
;
405 if (nfc
&& nfc
->write_by_sram
)
406 dma_dst_addr
= nfc_sram_phys(host
);
408 dma_dst_addr
= host
->io_phys
;
411 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
412 dma_src_addr
, len
, flags
);
414 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
418 init_completion(&host
->comp
);
419 tx
->callback
= dma_complete_func
;
420 tx
->callback_param
= &host
->comp
;
422 cookie
= tx
->tx_submit(tx
);
423 if (dma_submit_error(cookie
)) {
424 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
428 dma_async_issue_pending(host
->dma_chan
);
429 wait_for_completion(&host
->comp
);
431 if (is_read
&& nfc
&& nfc
->data_in_sram
)
432 /* After read data from SRAM, need to increase the position */
433 nfc
->data_in_sram
+= len
;
438 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
441 dev_dbg(host
->dev
, "Fall back to CPU I/O\n");
445 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
447 struct nand_chip
*chip
= mtd_to_nand(mtd
);
449 if (use_dma
&& len
> mtd
->oobsize
)
450 /* only use DMA for bigger than oob size: better performances */
451 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
454 if (chip
->options
& NAND_BUSWIDTH_16
)
455 atmel_read_buf16(mtd
, buf
, len
);
457 atmel_read_buf8(mtd
, buf
, len
);
460 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
462 struct nand_chip
*chip
= mtd_to_nand(mtd
);
464 if (use_dma
&& len
> mtd
->oobsize
)
465 /* only use DMA for bigger than oob size: better performances */
466 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
469 if (chip
->options
& NAND_BUSWIDTH_16
)
470 atmel_write_buf16(mtd
, buf
, len
);
472 atmel_write_buf8(mtd
, buf
, len
);
476 * Return number of ecc bytes per sector according to sector size and
477 * correction capability
479 * Following table shows what at91 PMECC supported:
480 * Correction Capability Sector_512_bytes Sector_1024_bytes
481 * ===================== ================ =================
482 * 2-bits 4-bytes 4-bytes
483 * 4-bits 7-bytes 7-bytes
484 * 8-bits 13-bytes 14-bytes
485 * 12-bits 20-bytes 21-bytes
486 * 24-bits 39-bytes 42-bytes
487 * 32-bits 52-bytes 56-bytes
489 static int pmecc_get_ecc_bytes(int cap
, int sector_size
)
491 int m
= 12 + sector_size
/ 512;
492 return (m
* cap
+ 7) / 8;
495 static void __iomem
*pmecc_get_alpha_to(struct atmel_nand_host
*host
)
499 table_size
= host
->pmecc_sector_size
== 512 ?
500 PMECC_LOOKUP_TABLE_SIZE_512
: PMECC_LOOKUP_TABLE_SIZE_1024
;
502 return host
->pmecc_rom_base
+ host
->pmecc_lookup_table_offset
+
503 table_size
* sizeof(int16_t);
506 static int pmecc_data_alloc(struct atmel_nand_host
*host
)
508 const int cap
= host
->pmecc_corr_cap
;
511 size
= (2 * cap
+ 1) * sizeof(int16_t);
512 host
->pmecc_partial_syn
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
513 host
->pmecc_si
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
514 host
->pmecc_lmu
= devm_kzalloc(host
->dev
,
515 (cap
+ 1) * sizeof(int16_t), GFP_KERNEL
);
516 host
->pmecc_smu
= devm_kzalloc(host
->dev
,
517 (cap
+ 2) * size
, GFP_KERNEL
);
519 size
= (cap
+ 1) * sizeof(int);
520 host
->pmecc_mu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
521 host
->pmecc_dmu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
522 host
->pmecc_delta
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
524 if (!host
->pmecc_partial_syn
||
536 static void pmecc_gen_syndrome(struct mtd_info
*mtd
, int sector
)
538 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
539 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
543 /* Fill odd syndromes */
544 for (i
= 0; i
< host
->pmecc_corr_cap
; i
++) {
545 value
= pmecc_readl_rem_relaxed(host
->ecc
, sector
, i
/ 2);
549 host
->pmecc_partial_syn
[(2 * i
) + 1] = (int16_t)value
;
553 static void pmecc_substitute(struct mtd_info
*mtd
)
555 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
556 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
557 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
558 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
559 int16_t *partial_syn
= host
->pmecc_partial_syn
;
560 const int cap
= host
->pmecc_corr_cap
;
564 /* si[] is a table that holds the current syndrome value,
565 * an element of that table belongs to the field
569 memset(&si
[1], 0, sizeof(int16_t) * (2 * cap
- 1));
571 /* Computation 2t syndromes based on S(x) */
573 for (i
= 1; i
< 2 * cap
; i
+= 2) {
574 for (j
= 0; j
< host
->pmecc_degree
; j
++) {
575 if (partial_syn
[i
] & ((unsigned short)0x1 << j
))
576 si
[i
] = readw_relaxed(alpha_to
+ i
* j
) ^ si
[i
];
579 /* Even syndrome = (Odd syndrome) ** 2 */
580 for (i
= 2, j
= 1; j
<= cap
; i
= ++j
<< 1) {
586 tmp
= readw_relaxed(index_of
+ si
[j
]);
587 tmp
= (tmp
* 2) % host
->pmecc_cw_len
;
588 si
[i
] = readw_relaxed(alpha_to
+ tmp
);
595 static void pmecc_get_sigma(struct mtd_info
*mtd
)
597 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
598 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
600 int16_t *lmu
= host
->pmecc_lmu
;
601 int16_t *si
= host
->pmecc_si
;
602 int *mu
= host
->pmecc_mu
;
603 int *dmu
= host
->pmecc_dmu
; /* Discrepancy */
604 int *delta
= host
->pmecc_delta
; /* Delta order */
605 int cw_len
= host
->pmecc_cw_len
;
606 const int16_t cap
= host
->pmecc_corr_cap
;
607 const int num
= 2 * cap
+ 1;
608 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
609 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
611 uint32_t dmu_0_count
, tmp
;
612 int16_t *smu
= host
->pmecc_smu
;
614 /* index of largest delta */
626 memset(smu
, 0, sizeof(int16_t) * num
);
629 /* discrepancy set to 1 */
631 /* polynom order set to 0 */
633 delta
[0] = (mu
[0] * 2 - lmu
[0]) >> 1;
639 /* Sigma(x) set to 1 */
640 memset(&smu
[num
], 0, sizeof(int16_t) * num
);
643 /* discrepancy set to S1 */
646 /* polynom order set to 0 */
649 delta
[1] = (mu
[1] * 2 - lmu
[1]) >> 1;
651 /* Init the Sigma(x) last row */
652 memset(&smu
[(cap
+ 1) * num
], 0, sizeof(int16_t) * num
);
654 for (i
= 1; i
<= cap
; i
++) {
656 /* Begin Computing Sigma (Mu+1) and L(mu) */
657 /* check if discrepancy is set to 0 */
661 tmp
= ((cap
- (lmu
[i
] >> 1) - 1) / 2);
662 if ((cap
- (lmu
[i
] >> 1) - 1) & 0x1)
667 if (dmu_0_count
== tmp
) {
668 for (j
= 0; j
<= (lmu
[i
] >> 1) + 1; j
++)
669 smu
[(cap
+ 1) * num
+ j
] =
672 lmu
[cap
+ 1] = lmu
[i
];
677 for (j
= 0; j
<= lmu
[i
] >> 1; j
++)
678 smu
[(i
+ 1) * num
+ j
] = smu
[i
* num
+ j
];
680 /* copy previous polynom order to the next */
685 /* find largest delta with dmu != 0 */
686 for (j
= 0; j
< i
; j
++) {
687 if ((dmu
[j
]) && (delta
[j
] > largest
)) {
693 /* compute difference */
694 diff
= (mu
[i
] - mu
[ro
]);
696 /* Compute degree of the new smu polynomial */
697 if ((lmu
[i
] >> 1) > ((lmu
[ro
] >> 1) + diff
))
700 lmu
[i
+ 1] = ((lmu
[ro
] >> 1) + diff
) * 2;
702 /* Init smu[i+1] with 0 */
703 for (k
= 0; k
< num
; k
++)
704 smu
[(i
+ 1) * num
+ k
] = 0;
706 /* Compute smu[i+1] */
707 for (k
= 0; k
<= lmu
[ro
] >> 1; k
++) {
710 if (!(smu
[ro
* num
+ k
] && dmu
[i
]))
712 a
= readw_relaxed(index_of
+ dmu
[i
]);
713 b
= readw_relaxed(index_of
+ dmu
[ro
]);
714 c
= readw_relaxed(index_of
+ smu
[ro
* num
+ k
]);
715 tmp
= a
+ (cw_len
- b
) + c
;
716 a
= readw_relaxed(alpha_to
+ tmp
% cw_len
);
717 smu
[(i
+ 1) * num
+ (k
+ diff
)] = a
;
720 for (k
= 0; k
<= lmu
[i
] >> 1; k
++)
721 smu
[(i
+ 1) * num
+ k
] ^= smu
[i
* num
+ k
];
724 /* End Computing Sigma (Mu+1) and L(mu) */
725 /* In either case compute delta */
726 delta
[i
+ 1] = (mu
[i
+ 1] * 2 - lmu
[i
+ 1]) >> 1;
728 /* Do not compute discrepancy for the last iteration */
732 for (k
= 0; k
<= (lmu
[i
+ 1] >> 1); k
++) {
735 dmu
[i
+ 1] = si
[tmp
+ 3];
736 } else if (smu
[(i
+ 1) * num
+ k
] && si
[tmp
+ 3 - k
]) {
738 a
= readw_relaxed(index_of
+
739 smu
[(i
+ 1) * num
+ k
]);
740 b
= si
[2 * (i
- 1) + 3 - k
];
741 c
= readw_relaxed(index_of
+ b
);
744 dmu
[i
+ 1] = readw_relaxed(alpha_to
+ tmp
) ^
753 static int pmecc_err_location(struct mtd_info
*mtd
)
755 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
756 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
757 unsigned long end_time
;
758 const int cap
= host
->pmecc_corr_cap
;
759 const int num
= 2 * cap
+ 1;
760 int sector_size
= host
->pmecc_sector_size
;
761 int err_nbr
= 0; /* number of error */
762 int roots_nbr
; /* number of roots */
765 int16_t *smu
= host
->pmecc_smu
;
767 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
, PMERRLOC_DISABLE
);
769 for (i
= 0; i
<= host
->pmecc_lmu
[cap
+ 1] >> 1; i
++) {
770 pmerrloc_writel_sigma_relaxed(host
->pmerrloc_base
, i
,
771 smu
[(cap
+ 1) * num
+ i
]);
775 val
= (err_nbr
- 1) << 16;
776 if (sector_size
== 1024)
779 pmerrloc_writel(host
->pmerrloc_base
, ELCFG
, val
);
780 pmerrloc_writel(host
->pmerrloc_base
, ELEN
,
781 sector_size
* 8 + host
->pmecc_degree
* cap
);
783 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
784 while (!(pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
785 & PMERRLOC_CALC_DONE
)) {
786 if (unlikely(time_after(jiffies
, end_time
))) {
787 dev_err(host
->dev
, "PMECC: Timeout to calculate error location.\n");
793 roots_nbr
= (pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
794 & PMERRLOC_ERR_NUM_MASK
) >> 8;
795 /* Number of roots == degree of smu hence <= cap */
796 if (roots_nbr
== host
->pmecc_lmu
[cap
+ 1] >> 1)
799 /* Number of roots does not match the degree of smu
800 * unable to correct error */
804 static void pmecc_correct_data(struct mtd_info
*mtd
, uint8_t *buf
, uint8_t *ecc
,
805 int sector_num
, int extra_bytes
, int err_nbr
)
807 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
808 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
810 int byte_pos
, bit_pos
, sector_size
, pos
;
814 sector_size
= host
->pmecc_sector_size
;
817 tmp
= pmerrloc_readl_el_relaxed(host
->pmerrloc_el_base
, i
) - 1;
821 if (byte_pos
>= (sector_size
+ extra_bytes
))
822 BUG(); /* should never happen */
824 if (byte_pos
< sector_size
) {
825 err_byte
= *(buf
+ byte_pos
);
826 *(buf
+ byte_pos
) ^= (1 << bit_pos
);
828 pos
= sector_num
* host
->pmecc_sector_size
+ byte_pos
;
829 dev_dbg(host
->dev
, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
830 pos
, bit_pos
, err_byte
, *(buf
+ byte_pos
));
832 struct mtd_oob_region oobregion
;
834 /* Bit flip in OOB area */
835 tmp
= sector_num
* nand_chip
->ecc
.bytes
836 + (byte_pos
- sector_size
);
838 ecc
[tmp
] ^= (1 << bit_pos
);
840 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
841 pos
= tmp
+ oobregion
.offset
;
842 dev_dbg(host
->dev
, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
843 pos
, bit_pos
, err_byte
, ecc
[tmp
]);
853 static int pmecc_correction(struct mtd_info
*mtd
, u32 pmecc_stat
, uint8_t *buf
,
856 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
857 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
860 int max_bitflips
= 0;
862 for (i
= 0; i
< nand_chip
->ecc
.steps
; i
++) {
864 if (pmecc_stat
& 0x1) {
865 buf_pos
= buf
+ i
* host
->pmecc_sector_size
;
867 pmecc_gen_syndrome(mtd
, i
);
868 pmecc_substitute(mtd
);
869 pmecc_get_sigma(mtd
);
871 err_nbr
= pmecc_err_location(mtd
);
873 pmecc_correct_data(mtd
, buf_pos
, ecc
, i
,
874 nand_chip
->ecc
.bytes
,
876 } else if (!host
->caps
->pmecc_correct_erase_page
) {
877 u8
*ecc_pos
= ecc
+ (i
* nand_chip
->ecc
.bytes
);
879 /* Try to detect erased pages */
880 err_nbr
= nand_check_erased_ecc_chunk(buf_pos
,
881 host
->pmecc_sector_size
,
883 nand_chip
->ecc
.bytes
,
885 nand_chip
->ecc
.strength
);
889 dev_err(host
->dev
, "PMECC: Too many errors\n");
890 mtd
->ecc_stats
.failed
++;
894 mtd
->ecc_stats
.corrected
+= err_nbr
;
895 max_bitflips
= max_t(int, max_bitflips
, err_nbr
);
903 static void pmecc_enable(struct atmel_nand_host
*host
, int ecc_op
)
907 if (ecc_op
!= NAND_ECC_READ
&& ecc_op
!= NAND_ECC_WRITE
) {
908 dev_err(host
->dev
, "atmel_nand: wrong pmecc operation type!");
912 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
913 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
914 val
= pmecc_readl_relaxed(host
->ecc
, CFG
);
916 if (ecc_op
== NAND_ECC_READ
)
917 pmecc_writel(host
->ecc
, CFG
, (val
& ~PMECC_CFG_WRITE_OP
)
918 | PMECC_CFG_AUTO_ENABLE
);
920 pmecc_writel(host
->ecc
, CFG
, (val
| PMECC_CFG_WRITE_OP
)
921 & ~PMECC_CFG_AUTO_ENABLE
);
923 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
924 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DATA
);
927 static int atmel_nand_pmecc_read_page(struct mtd_info
*mtd
,
928 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
930 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
931 int eccsize
= chip
->ecc
.size
* chip
->ecc
.steps
;
932 uint8_t *oob
= chip
->oob_poi
;
934 unsigned long end_time
;
937 if (!host
->nfc
|| !host
->nfc
->use_nfc_sram
)
938 pmecc_enable(host
, NAND_ECC_READ
);
940 chip
->read_buf(mtd
, buf
, eccsize
);
941 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
943 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
944 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
945 if (unlikely(time_after(jiffies
, end_time
))) {
946 dev_err(host
->dev
, "PMECC: Timeout to get error status.\n");
952 stat
= pmecc_readl_relaxed(host
->ecc
, ISR
);
954 struct mtd_oob_region oobregion
;
956 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
957 bitflips
= pmecc_correction(mtd
, stat
, buf
,
958 &oob
[oobregion
.offset
]);
960 /* uncorrectable errors */
967 static int atmel_nand_pmecc_write_page(struct mtd_info
*mtd
,
968 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
,
971 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
972 struct mtd_oob_region oobregion
= { };
973 int i
, j
, section
= 0;
974 unsigned long end_time
;
976 if (!host
->nfc
|| !host
->nfc
->write_by_sram
) {
977 pmecc_enable(host
, NAND_ECC_WRITE
);
978 chip
->write_buf(mtd
, (u8
*)buf
, mtd
->writesize
);
981 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
982 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
983 if (unlikely(time_after(jiffies
, end_time
))) {
984 dev_err(host
->dev
, "PMECC: Timeout to get ECC value.\n");
990 for (i
= 0; i
< chip
->ecc
.steps
; i
++) {
991 for (j
= 0; j
< chip
->ecc
.bytes
; j
++) {
992 if (!oobregion
.length
)
993 mtd_ooblayout_ecc(mtd
, section
, &oobregion
);
995 chip
->oob_poi
[oobregion
.offset
] =
996 pmecc_readb_ecc_relaxed(host
->ecc
, i
, j
);
1002 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1007 static void atmel_pmecc_core_init(struct mtd_info
*mtd
)
1009 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1010 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1011 int eccbytes
= mtd_ooblayout_count_eccbytes(mtd
);
1013 struct mtd_oob_region oobregion
;
1015 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
1016 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
1018 switch (host
->pmecc_corr_cap
) {
1020 val
= PMECC_CFG_BCH_ERR2
;
1023 val
= PMECC_CFG_BCH_ERR4
;
1026 val
= PMECC_CFG_BCH_ERR8
;
1029 val
= PMECC_CFG_BCH_ERR12
;
1032 val
= PMECC_CFG_BCH_ERR24
;
1035 val
= PMECC_CFG_BCH_ERR32
;
1039 if (host
->pmecc_sector_size
== 512)
1040 val
|= PMECC_CFG_SECTOR512
;
1041 else if (host
->pmecc_sector_size
== 1024)
1042 val
|= PMECC_CFG_SECTOR1024
;
1044 switch (nand_chip
->ecc
.steps
) {
1046 val
|= PMECC_CFG_PAGE_1SECTOR
;
1049 val
|= PMECC_CFG_PAGE_2SECTORS
;
1052 val
|= PMECC_CFG_PAGE_4SECTORS
;
1055 val
|= PMECC_CFG_PAGE_8SECTORS
;
1059 val
|= (PMECC_CFG_READ_OP
| PMECC_CFG_SPARE_DISABLE
1060 | PMECC_CFG_AUTO_DISABLE
);
1061 pmecc_writel(host
->ecc
, CFG
, val
);
1063 pmecc_writel(host
->ecc
, SAREA
, mtd
->oobsize
- 1);
1064 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
1065 pmecc_writel(host
->ecc
, SADDR
, oobregion
.offset
);
1066 pmecc_writel(host
->ecc
, EADDR
,
1067 oobregion
.offset
+ eccbytes
- 1);
1068 /* See datasheet about PMECC Clock Control Register */
1069 pmecc_writel(host
->ecc
, CLK
, 2);
1070 pmecc_writel(host
->ecc
, IDR
, 0xff);
1071 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
1075 * Get minimum ecc requirements from NAND.
1076 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1077 * will set them according to minimum ecc requirement. Otherwise, use the
1078 * value in DTS file.
1079 * return 0 if success. otherwise return error code.
1081 static int pmecc_choose_ecc(struct atmel_nand_host
*host
,
1082 int *cap
, int *sector_size
)
1084 /* Get minimum ECC requirements */
1085 if (host
->nand_chip
.ecc_strength_ds
) {
1086 *cap
= host
->nand_chip
.ecc_strength_ds
;
1087 *sector_size
= host
->nand_chip
.ecc_step_ds
;
1088 dev_info(host
->dev
, "minimum ECC: %d bits in %d bytes\n",
1089 *cap
, *sector_size
);
1093 dev_info(host
->dev
, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
1096 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
1097 if (host
->pmecc_corr_cap
== 0) {
1098 if (*cap
> host
->caps
->pmecc_max_correction
)
1101 /* use the most fitable ecc bits (the near bigger one ) */
1103 host
->pmecc_corr_cap
= 2;
1105 host
->pmecc_corr_cap
= 4;
1107 host
->pmecc_corr_cap
= 8;
1108 else if (*cap
<= 12)
1109 host
->pmecc_corr_cap
= 12;
1110 else if (*cap
<= 24)
1111 host
->pmecc_corr_cap
= 24;
1112 else if (*cap
<= 32)
1113 host
->pmecc_corr_cap
= 32;
1117 if (host
->pmecc_sector_size
== 0) {
1118 /* use the most fitable sector size (the near smaller one ) */
1119 if (*sector_size
>= 1024)
1120 host
->pmecc_sector_size
= 1024;
1121 else if (*sector_size
>= 512)
1122 host
->pmecc_sector_size
= 512;
1129 static inline int deg(unsigned int poly
)
1131 /* polynomial degree is the most-significant bit index */
1132 return fls(poly
) - 1;
1135 static int build_gf_tables(int mm
, unsigned int poly
,
1136 int16_t *index_of
, int16_t *alpha_to
)
1138 unsigned int i
, x
= 1;
1139 const unsigned int k
= 1 << deg(poly
);
1140 unsigned int nn
= (1 << mm
) - 1;
1142 /* primitive polynomial must be of degree m */
1143 if (k
!= (1u << mm
))
1146 for (i
= 0; i
< nn
; i
++) {
1150 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1162 static uint16_t *create_lookup_table(struct device
*dev
, int sector_size
)
1164 int degree
= (sector_size
== 512) ?
1165 PMECC_GF_DIMENSION_13
:
1166 PMECC_GF_DIMENSION_14
;
1167 unsigned int poly
= (sector_size
== 512) ?
1168 PMECC_GF_13_PRIMITIVE_POLY
:
1169 PMECC_GF_14_PRIMITIVE_POLY
;
1170 int table_size
= (sector_size
== 512) ?
1171 PMECC_LOOKUP_TABLE_SIZE_512
:
1172 PMECC_LOOKUP_TABLE_SIZE_1024
;
1174 int16_t *addr
= devm_kzalloc(dev
, 2 * table_size
* sizeof(uint16_t),
1176 if (addr
&& build_gf_tables(degree
, poly
, addr
, addr
+ table_size
))
1182 static int atmel_pmecc_nand_init_params(struct platform_device
*pdev
,
1183 struct atmel_nand_host
*host
)
1185 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1186 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
1187 struct resource
*regs
, *regs_pmerr
, *regs_rom
;
1188 uint16_t *galois_table
;
1189 int cap
, sector_size
, err_no
;
1191 err_no
= pmecc_choose_ecc(host
, &cap
, §or_size
);
1193 dev_err(host
->dev
, "The NAND flash's ECC requirement are not support!");
1197 if (cap
> host
->pmecc_corr_cap
||
1198 sector_size
!= host
->pmecc_sector_size
)
1199 dev_info(host
->dev
, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
1201 cap
= host
->pmecc_corr_cap
;
1202 sector_size
= host
->pmecc_sector_size
;
1203 host
->pmecc_lookup_table_offset
= (sector_size
== 512) ?
1204 host
->pmecc_lookup_table_offset_512
:
1205 host
->pmecc_lookup_table_offset_1024
;
1207 dev_info(host
->dev
, "Initialize PMECC params, cap: %d, sector: %d\n",
1210 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1213 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1214 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1215 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1219 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1220 if (IS_ERR(host
->ecc
)) {
1221 err_no
= PTR_ERR(host
->ecc
);
1225 regs_pmerr
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1226 host
->pmerrloc_base
= devm_ioremap_resource(&pdev
->dev
, regs_pmerr
);
1227 if (IS_ERR(host
->pmerrloc_base
)) {
1228 err_no
= PTR_ERR(host
->pmerrloc_base
);
1231 host
->pmerrloc_el_base
= host
->pmerrloc_base
+ ATMEL_PMERRLOC_SIGMAx
+
1232 (host
->caps
->pmecc_max_correction
+ 1) * 4;
1234 if (!host
->has_no_lookup_table
) {
1235 regs_rom
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
1236 host
->pmecc_rom_base
= devm_ioremap_resource(&pdev
->dev
,
1238 if (IS_ERR(host
->pmecc_rom_base
)) {
1239 dev_err(host
->dev
, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
1240 host
->has_no_lookup_table
= true;
1244 if (host
->has_no_lookup_table
) {
1245 /* Build the look-up table in runtime */
1246 galois_table
= create_lookup_table(host
->dev
, sector_size
);
1247 if (!galois_table
) {
1248 dev_err(host
->dev
, "Failed to build a lookup table in runtime!\n");
1253 host
->pmecc_rom_base
= (void __iomem
*)galois_table
;
1254 host
->pmecc_lookup_table_offset
= 0;
1257 nand_chip
->ecc
.size
= sector_size
;
1259 /* set ECC page size and oob layout */
1260 switch (mtd
->writesize
) {
1266 if (sector_size
> mtd
->writesize
) {
1267 dev_err(host
->dev
, "pmecc sector size is bigger than the page size!\n");
1272 host
->pmecc_degree
= (sector_size
== 512) ?
1273 PMECC_GF_DIMENSION_13
: PMECC_GF_DIMENSION_14
;
1274 host
->pmecc_cw_len
= (1 << host
->pmecc_degree
) - 1;
1275 host
->pmecc_alpha_to
= pmecc_get_alpha_to(host
);
1276 host
->pmecc_index_of
= host
->pmecc_rom_base
+
1277 host
->pmecc_lookup_table_offset
;
1279 nand_chip
->ecc
.strength
= cap
;
1280 nand_chip
->ecc
.bytes
= pmecc_get_ecc_bytes(cap
, sector_size
);
1281 nand_chip
->ecc
.steps
= mtd
->writesize
/ sector_size
;
1282 nand_chip
->ecc
.total
= nand_chip
->ecc
.bytes
*
1283 nand_chip
->ecc
.steps
;
1284 if (nand_chip
->ecc
.total
>
1285 mtd
->oobsize
- PMECC_OOB_RESERVED_BYTES
) {
1286 dev_err(host
->dev
, "No room for ECC bytes\n");
1291 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1295 "Unsupported page size for PMECC, use Software ECC\n");
1296 /* page size not handled by HW ECC */
1297 /* switching back to soft ECC */
1298 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1299 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1303 /* Allocate data for PMECC computation */
1304 err_no
= pmecc_data_alloc(host
);
1307 "Cannot allocate memory for PMECC computation!\n");
1311 nand_chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
1312 nand_chip
->ecc
.read_page
= atmel_nand_pmecc_read_page
;
1313 nand_chip
->ecc
.write_page
= atmel_nand_pmecc_write_page
;
1315 atmel_pmecc_core_init(mtd
);
1326 * function called after a write
1328 * mtd: MTD block structure
1329 * dat: raw data (unused)
1330 * ecc_code: buffer for ECC
1332 static int atmel_nand_calculate(struct mtd_info
*mtd
,
1333 const u_char
*dat
, unsigned char *ecc_code
)
1335 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1336 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1337 unsigned int ecc_value
;
1339 /* get the first 2 ECC bytes */
1340 ecc_value
= ecc_readl(host
->ecc
, PR
);
1342 ecc_code
[0] = ecc_value
& 0xFF;
1343 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
1345 /* get the last 2 ECC bytes */
1346 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
1348 ecc_code
[2] = ecc_value
& 0xFF;
1349 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
1355 * HW ECC read page function
1357 * mtd: mtd info structure
1358 * chip: nand chip info structure
1359 * buf: buffer to store read data
1360 * oob_required: caller expects OOB data read to chip->oob_poi
1362 static int atmel_nand_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1363 uint8_t *buf
, int oob_required
, int page
)
1365 int eccsize
= chip
->ecc
.size
;
1366 int eccbytes
= chip
->ecc
.bytes
;
1368 uint8_t *oob
= chip
->oob_poi
;
1371 unsigned int max_bitflips
= 0;
1372 struct mtd_oob_region oobregion
= {};
1375 * Errata: ALE is incorrectly wired up to the ECC controller
1376 * on the AP7000, so it will include the address cycles in the
1379 * Workaround: Reset the parity registers before reading the
1382 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
1383 if (host
->board
.need_reset_workaround
)
1384 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1387 chip
->read_buf(mtd
, p
, eccsize
);
1389 /* move to ECC position if needed */
1390 mtd_ooblayout_ecc(mtd
, 0, &oobregion
);
1391 if (oobregion
.offset
!= 0) {
1393 * This only works on large pages because the ECC controller
1394 * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT.
1395 * Anyway, for small pages, the first ECC byte is at offset
1396 * 0 in the OOB area.
1398 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
1399 mtd
->writesize
+ oobregion
.offset
, -1);
1402 /* the ECC controller needs to read the ECC just after the data */
1403 ecc_pos
= oob
+ oobregion
.offset
;
1404 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
1406 /* check if there's an error */
1407 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
1410 mtd
->ecc_stats
.failed
++;
1412 mtd
->ecc_stats
.corrected
+= stat
;
1413 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1416 /* get back to oob start (end of page) */
1417 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
1420 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
1422 return max_bitflips
;
1428 * function called after a read
1430 * mtd: MTD block structure
1431 * dat: raw data read from the chip
1432 * read_ecc: ECC from the chip (unused)
1435 * Detect and correct a 1 bit error for a page
1437 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
1438 u_char
*read_ecc
, u_char
*isnull
)
1440 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1441 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1442 unsigned int ecc_status
;
1443 unsigned int ecc_word
, ecc_bit
;
1445 /* get the status from the Status Register */
1446 ecc_status
= ecc_readl(host
->ecc
, SR
);
1448 /* if there's no error */
1449 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
1452 /* get error bit offset (4 bits) */
1453 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
1454 /* get word address (12 bits) */
1455 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
1458 /* if there are multiple errors */
1459 if (ecc_status
& ATMEL_ECC_MULERR
) {
1460 /* check if it is a freshly erased block
1461 * (filled with 0xff) */
1462 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
1463 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
1464 /* the block has just been erased, return OK */
1467 /* it doesn't seems to be a freshly
1469 * We can't correct so many errors */
1470 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
1471 " Unable to correct.\n");
1475 /* if there's a single bit error : we can correct it */
1476 if (ecc_status
& ATMEL_ECC_ECCERR
) {
1477 /* there's nothing much to do here.
1478 * the bit error is on the ECC itself.
1480 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
1481 " Nothing to correct\n");
1485 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
1486 " (word offset in the page :"
1487 " 0x%x bit offset : 0x%x)\n",
1489 /* correct the error */
1490 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
1492 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
1495 dat
[ecc_word
] ^= (1 << ecc_bit
);
1497 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
1502 * Enable HW ECC : unused on most chips
1504 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
1506 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1507 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1509 if (host
->board
.need_reset_workaround
)
1510 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1513 static int atmel_of_init_ecc(struct atmel_nand_host
*host
,
1514 struct device_node
*np
)
1519 host
->has_pmecc
= of_property_read_bool(np
, "atmel,has-pmecc");
1521 /* Not using PMECC */
1522 if (!(host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) || !host
->has_pmecc
)
1525 /* use PMECC, get correction capability, sector size and lookup
1527 * If correction bits and sector size are not specified, then find
1528 * them from NAND ONFI parameters.
1530 if (of_property_read_u32(np
, "atmel,pmecc-cap", &val
) == 0) {
1531 if (val
> host
->caps
->pmecc_max_correction
) {
1533 "Required ECC strength too high: %u max %u\n",
1534 val
, host
->caps
->pmecc_max_correction
);
1537 if ((val
!= 2) && (val
!= 4) && (val
!= 8) &&
1538 (val
!= 12) && (val
!= 24) && (val
!= 32)) {
1540 "Required ECC strength not supported: %u\n",
1544 host
->pmecc_corr_cap
= (u8
)val
;
1547 if (of_property_read_u32(np
, "atmel,pmecc-sector-size", &val
) == 0) {
1548 if ((val
!= 512) && (val
!= 1024)) {
1550 "Required ECC sector size not supported: %u\n",
1554 host
->pmecc_sector_size
= (u16
)val
;
1557 if (of_property_read_u32_array(np
, "atmel,pmecc-lookup-table-offset",
1559 dev_err(host
->dev
, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1560 host
->has_no_lookup_table
= true;
1561 /* Will build a lookup table and initialize the offset later */
1565 if (!offset
[0] && !offset
[1]) {
1566 dev_err(host
->dev
, "Invalid PMECC lookup table offset\n");
1570 host
->pmecc_lookup_table_offset_512
= offset
[0];
1571 host
->pmecc_lookup_table_offset_1024
= offset
[1];
1576 static int atmel_of_init_port(struct atmel_nand_host
*host
,
1577 struct device_node
*np
)
1580 struct atmel_nand_data
*board
= &host
->board
;
1581 enum of_gpio_flags flags
= 0;
1583 host
->caps
= (struct atmel_nand_caps
*)
1584 of_device_get_match_data(host
->dev
);
1586 if (of_property_read_u32(np
, "atmel,nand-addr-offset", &val
) == 0) {
1588 dev_err(host
->dev
, "invalid addr-offset %u\n", val
);
1594 if (of_property_read_u32(np
, "atmel,nand-cmd-offset", &val
) == 0) {
1596 dev_err(host
->dev
, "invalid cmd-offset %u\n", val
);
1602 board
->has_dma
= of_property_read_bool(np
, "atmel,nand-has-dma");
1604 board
->rdy_pin
= of_get_gpio_flags(np
, 0, &flags
);
1605 board
->rdy_pin_active_low
= (flags
== OF_GPIO_ACTIVE_LOW
);
1607 board
->enable_pin
= of_get_gpio(np
, 1);
1608 board
->det_pin
= of_get_gpio(np
, 2);
1610 /* load the nfc driver if there is */
1611 of_platform_populate(np
, NULL
, NULL
, host
->dev
);
1614 * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value
1615 * even if the nand-ecc-mode property is not defined.
1617 host
->nand_chip
.ecc
.mode
= NAND_ECC_SOFT
;
1618 host
->nand_chip
.ecc
.algo
= NAND_ECC_HAMMING
;
1623 static int atmel_hw_nand_init_params(struct platform_device
*pdev
,
1624 struct atmel_nand_host
*host
)
1626 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1627 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
1628 struct resource
*regs
;
1630 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1633 "Can't get I/O resource regs, use software ECC\n");
1634 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1635 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1639 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1640 if (IS_ERR(host
->ecc
))
1641 return PTR_ERR(host
->ecc
);
1643 /* ECC is calculated for the whole page (1 step) */
1644 nand_chip
->ecc
.size
= mtd
->writesize
;
1646 /* set ECC page size and oob layout */
1647 switch (mtd
->writesize
) {
1649 mtd_set_ooblayout(mtd
, &atmel_ooblayout_sp_ops
);
1650 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
1653 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1654 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
1657 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1658 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
1661 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
1662 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
1665 /* page size not handled by HW ECC */
1666 /* switching back to soft ECC */
1667 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1668 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
1672 /* set up for HW ECC */
1673 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
1674 nand_chip
->ecc
.correct
= atmel_nand_correct
;
1675 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
1676 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
1677 nand_chip
->ecc
.bytes
= 4;
1678 nand_chip
->ecc
.strength
= 1;
1683 static inline u32
nfc_read_status(struct atmel_nand_host
*host
)
1685 u32 err_flags
= NFC_SR_DTOE
| NFC_SR_UNDEF
| NFC_SR_AWB
| NFC_SR_ASE
;
1686 u32 nfc_status
= nfc_readl(host
->nfc
->hsmc_regs
, SR
);
1688 if (unlikely(nfc_status
& err_flags
)) {
1689 if (nfc_status
& NFC_SR_DTOE
)
1690 dev_err(host
->dev
, "NFC: Waiting Nand R/B Timeout Error\n");
1691 else if (nfc_status
& NFC_SR_UNDEF
)
1692 dev_err(host
->dev
, "NFC: Access Undefined Area Error\n");
1693 else if (nfc_status
& NFC_SR_AWB
)
1694 dev_err(host
->dev
, "NFC: Access memory While NFC is busy\n");
1695 else if (nfc_status
& NFC_SR_ASE
)
1696 dev_err(host
->dev
, "NFC: Access memory Size Error\n");
1702 /* SMC interrupt service routine */
1703 static irqreturn_t
hsmc_interrupt(int irq
, void *dev_id
)
1705 struct atmel_nand_host
*host
= dev_id
;
1706 u32 status
, mask
, pending
;
1707 irqreturn_t ret
= IRQ_NONE
;
1709 status
= nfc_read_status(host
);
1710 mask
= nfc_readl(host
->nfc
->hsmc_regs
, IMR
);
1711 pending
= status
& mask
;
1713 if (pending
& NFC_SR_XFR_DONE
) {
1714 complete(&host
->nfc
->comp_xfer_done
);
1715 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_XFR_DONE
);
1718 if (pending
& host
->nfc
->caps
->rb_mask
) {
1719 complete(&host
->nfc
->comp_ready
);
1720 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, host
->nfc
->caps
->rb_mask
);
1723 if (pending
& NFC_SR_CMD_DONE
) {
1724 complete(&host
->nfc
->comp_cmd_done
);
1725 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_CMD_DONE
);
1732 /* NFC(Nand Flash Controller) related functions */
1733 static void nfc_prepare_interrupt(struct atmel_nand_host
*host
, u32 flag
)
1735 if (flag
& NFC_SR_XFR_DONE
)
1736 init_completion(&host
->nfc
->comp_xfer_done
);
1738 if (flag
& host
->nfc
->caps
->rb_mask
)
1739 init_completion(&host
->nfc
->comp_ready
);
1741 if (flag
& NFC_SR_CMD_DONE
)
1742 init_completion(&host
->nfc
->comp_cmd_done
);
1744 /* Enable interrupt that need to wait for */
1745 nfc_writel(host
->nfc
->hsmc_regs
, IER
, flag
);
1748 static int nfc_wait_interrupt(struct atmel_nand_host
*host
, u32 flag
)
1751 struct completion
*comp
[3]; /* Support 3 interrupt completion */
1753 if (flag
& NFC_SR_XFR_DONE
)
1754 comp
[index
++] = &host
->nfc
->comp_xfer_done
;
1756 if (flag
& host
->nfc
->caps
->rb_mask
)
1757 comp
[index
++] = &host
->nfc
->comp_ready
;
1759 if (flag
& NFC_SR_CMD_DONE
)
1760 comp
[index
++] = &host
->nfc
->comp_cmd_done
;
1763 dev_err(host
->dev
, "Unknown interrupt flag: 0x%08x\n", flag
);
1767 for (i
= 0; i
< index
; i
++) {
1768 if (wait_for_completion_timeout(comp
[i
],
1769 msecs_to_jiffies(NFC_TIME_OUT_MS
)))
1770 continue; /* wait for next completion */
1778 dev_err(host
->dev
, "Time out to wait for interrupt: 0x%08x\n", flag
);
1779 /* Disable the interrupt as it is not handled by interrupt handler */
1780 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, flag
);
1784 static int nfc_send_command(struct atmel_nand_host
*host
,
1785 unsigned int cmd
, unsigned int addr
, unsigned char cycle0
)
1787 unsigned long timeout
;
1788 u32 flag
= NFC_SR_CMD_DONE
;
1789 flag
|= cmd
& NFCADDR_CMD_DATAEN
? NFC_SR_XFR_DONE
: 0;
1792 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1795 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1796 while (nfc_readl(host
->nfc
->hsmc_regs
, SR
) & NFC_SR_BUSY
) {
1797 if (time_after(jiffies
, timeout
)) {
1799 "Time out to wait for NFC ready!\n");
1804 nfc_prepare_interrupt(host
, flag
);
1805 nfc_writel(host
->nfc
->hsmc_regs
, CYCLE0
, cycle0
);
1806 nfc_cmd_addr1234_writel(cmd
, addr
, host
->nfc
->base_cmd_regs
);
1807 return nfc_wait_interrupt(host
, flag
);
1810 static int nfc_device_ready(struct mtd_info
*mtd
)
1813 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1814 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1816 status
= nfc_read_status(host
);
1817 mask
= nfc_readl(host
->nfc
->hsmc_regs
, IMR
);
1819 /* The mask should be 0. If not we may lost interrupts */
1820 if (unlikely(mask
& status
))
1821 dev_err(host
->dev
, "Lost the interrupt flags: 0x%08x\n",
1824 return status
& host
->nfc
->caps
->rb_mask
;
1827 static void nfc_select_chip(struct mtd_info
*mtd
, int chip
)
1829 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1830 struct atmel_nand_host
*host
= nand_get_controller_data(nand_chip
);
1833 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_DISABLE
);
1835 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_ENABLE
);
1838 static int nfc_make_addr(struct mtd_info
*mtd
, int command
, int column
,
1839 int page_addr
, unsigned int *addr1234
, unsigned int *cycle0
)
1841 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1844 unsigned char addr_bytes
[8];
1845 int index
= 0, bit_shift
;
1847 BUG_ON(addr1234
== NULL
|| cycle0
== NULL
);
1853 if (chip
->options
& NAND_BUSWIDTH_16
&&
1854 !nand_opcode_8bits(command
))
1856 addr_bytes
[acycle
++] = column
& 0xff;
1857 if (mtd
->writesize
> 512)
1858 addr_bytes
[acycle
++] = (column
>> 8) & 0xff;
1861 if (page_addr
!= -1) {
1862 addr_bytes
[acycle
++] = page_addr
& 0xff;
1863 addr_bytes
[acycle
++] = (page_addr
>> 8) & 0xff;
1864 if (chip
->chipsize
> (128 << 20))
1865 addr_bytes
[acycle
++] = (page_addr
>> 16) & 0xff;
1869 *cycle0
= addr_bytes
[index
++];
1871 for (bit_shift
= 0; index
< acycle
; bit_shift
+= 8)
1872 *addr1234
+= addr_bytes
[index
++] << bit_shift
;
1874 /* return acycle in cmd register */
1875 return acycle
<< NFCADDR_CMD_ACYCLE_BIT_POS
;
1878 static void nfc_nand_command(struct mtd_info
*mtd
, unsigned int command
,
1879 int column
, int page_addr
)
1881 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1882 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
1883 unsigned long timeout
;
1884 unsigned int nfc_addr_cmd
= 0;
1886 unsigned int cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1888 /* Set default settings: no cmd2, no addr cycle. read from nand */
1889 unsigned int cmd2
= 0;
1890 unsigned int vcmd2
= 0;
1891 int acycle
= NFCADDR_CMD_ACYCLE_NONE
;
1892 int csid
= NFCADDR_CMD_CSID_3
;
1893 int dataen
= NFCADDR_CMD_DATADIS
;
1894 int nfcwr
= NFCADDR_CMD_NFCRD
;
1895 unsigned int addr1234
= 0;
1896 unsigned int cycle0
= 0;
1897 bool do_addr
= true;
1898 host
->nfc
->data_in_sram
= NULL
;
1900 dev_dbg(host
->dev
, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1901 __func__
, command
, column
, page_addr
);
1904 case NAND_CMD_RESET
:
1905 nfc_addr_cmd
= cmd1
| acycle
| csid
| dataen
| nfcwr
;
1906 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1907 udelay(chip
->chip_delay
);
1909 nfc_nand_command(mtd
, NAND_CMD_STATUS
, -1, -1);
1910 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1911 while (!(chip
->read_byte(mtd
) & NAND_STATUS_READY
)) {
1912 if (time_after(jiffies
, timeout
)) {
1914 "Time out to wait status ready!\n");
1919 case NAND_CMD_STATUS
:
1922 case NAND_CMD_PARAM
:
1923 case NAND_CMD_READID
:
1925 acycle
= NFCADDR_CMD_ACYCLE_1
;
1929 case NAND_CMD_RNDOUT
:
1930 cmd2
= NAND_CMD_RNDOUTSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1931 vcmd2
= NFCADDR_CMD_VCMD2
;
1933 case NAND_CMD_READ0
:
1934 case NAND_CMD_READOOB
:
1935 if (command
== NAND_CMD_READOOB
) {
1936 column
+= mtd
->writesize
;
1937 command
= NAND_CMD_READ0
; /* only READ0 is valid */
1938 cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1940 if (host
->nfc
->use_nfc_sram
) {
1941 /* Enable Data transfer to sram */
1942 dataen
= NFCADDR_CMD_DATAEN
;
1944 /* Need enable PMECC now, since NFC will transfer
1945 * data in bus after sending nfc read command.
1947 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
1948 pmecc_enable(host
, NAND_ECC_READ
);
1951 cmd2
= NAND_CMD_READSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1952 vcmd2
= NFCADDR_CMD_VCMD2
;
1954 /* For prgramming command, the cmd need set to write enable */
1955 case NAND_CMD_PAGEPROG
:
1956 case NAND_CMD_SEQIN
:
1957 case NAND_CMD_RNDIN
:
1958 nfcwr
= NFCADDR_CMD_NFCWR
;
1959 if (host
->nfc
->will_write_sram
&& command
== NAND_CMD_SEQIN
)
1960 dataen
= NFCADDR_CMD_DATAEN
;
1967 acycle
= nfc_make_addr(mtd
, command
, column
, page_addr
,
1968 &addr1234
, &cycle0
);
1970 nfc_addr_cmd
= cmd1
| cmd2
| vcmd2
| acycle
| csid
| dataen
| nfcwr
;
1971 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1974 * Program and erase have their own busy handlers status, sequential
1975 * in, and deplete1 need no delay.
1978 case NAND_CMD_CACHEDPROG
:
1979 case NAND_CMD_PAGEPROG
:
1980 case NAND_CMD_ERASE1
:
1981 case NAND_CMD_ERASE2
:
1982 case NAND_CMD_RNDIN
:
1983 case NAND_CMD_STATUS
:
1984 case NAND_CMD_RNDOUT
:
1985 case NAND_CMD_SEQIN
:
1986 case NAND_CMD_READID
:
1989 case NAND_CMD_READ0
:
1990 if (dataen
== NFCADDR_CMD_DATAEN
) {
1991 host
->nfc
->data_in_sram
= host
->nfc
->sram_bank0
+
1992 nfc_get_sram_off(host
);
1997 nfc_prepare_interrupt(host
, host
->nfc
->caps
->rb_mask
);
1998 nfc_wait_interrupt(host
, host
->nfc
->caps
->rb_mask
);
2002 static int nfc_sram_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2003 uint32_t offset
, int data_len
, const uint8_t *buf
,
2004 int oob_required
, int page
, int cached
, int raw
)
2008 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
2009 void *sram
= host
->nfc
->sram_bank0
+ nfc_get_sram_off(host
);
2011 /* Subpage write is not supported */
2012 if (offset
|| (data_len
< mtd
->writesize
))
2015 len
= mtd
->writesize
;
2016 /* Copy page data to sram that will write to nand via NFC */
2018 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) != 0)
2019 /* Fall back to use cpu copy */
2020 memcpy(sram
, buf
, len
);
2022 memcpy(sram
, buf
, len
);
2025 cfg
= nfc_readl(host
->nfc
->hsmc_regs
, CFG
);
2026 if (unlikely(raw
) && oob_required
) {
2027 memcpy(sram
+ len
, chip
->oob_poi
, mtd
->oobsize
);
2028 len
+= mtd
->oobsize
;
2029 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
| NFC_CFG_WSPARE
);
2031 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
& ~NFC_CFG_WSPARE
);
2034 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
2036 * When use NFC sram, need set up PMECC before send
2037 * NAND_CMD_SEQIN command. Since when the nand command
2038 * is sent, nfc will do transfer from sram and nand.
2040 pmecc_enable(host
, NAND_ECC_WRITE
);
2042 host
->nfc
->will_write_sram
= true;
2043 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, 0x00, page
);
2044 host
->nfc
->will_write_sram
= false;
2047 /* Need to write ecc into oob */
2048 status
= chip
->ecc
.write_page(mtd
, chip
, buf
, oob_required
,
2054 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
2055 status
= chip
->waitfunc(mtd
, chip
);
2057 if ((status
& NAND_STATUS_FAIL
) && (chip
->errstat
))
2058 status
= chip
->errstat(mtd
, chip
, FL_WRITING
, status
, page
);
2060 if (status
& NAND_STATUS_FAIL
)
2066 static int nfc_sram_init(struct mtd_info
*mtd
)
2068 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2069 struct atmel_nand_host
*host
= nand_get_controller_data(chip
);
2072 /* Initialize the NFC CFG register */
2073 unsigned int cfg_nfc
= 0;
2075 /* set page size and oob layout */
2076 switch (mtd
->writesize
) {
2078 cfg_nfc
= NFC_CFG_PAGESIZE_512
;
2081 cfg_nfc
= NFC_CFG_PAGESIZE_1024
;
2084 cfg_nfc
= NFC_CFG_PAGESIZE_2048
;
2087 cfg_nfc
= NFC_CFG_PAGESIZE_4096
;
2090 cfg_nfc
= NFC_CFG_PAGESIZE_8192
;
2093 dev_err(host
->dev
, "Unsupported page size for NFC.\n");
2098 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2099 * Max support spare size is 512 bytes. */
2100 cfg_nfc
|= (((mtd
->oobsize
/ 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2101 & NFC_CFG_NFC_SPARESIZE
);
2102 /* default set a max timeout */
2103 cfg_nfc
|= NFC_CFG_RSPARE
|
2104 NFC_CFG_NFC_DTOCYC
| NFC_CFG_NFC_DTOMUL
;
2106 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg_nfc
);
2108 host
->nfc
->will_write_sram
= false;
2109 nfc_set_sram_bank(host
, 0);
2111 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2112 if (host
->nfc
->write_by_sram
) {
2113 if ((chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
) ||
2114 chip
->ecc
.mode
== NAND_ECC_NONE
)
2115 chip
->write_page
= nfc_sram_write_page
;
2117 host
->nfc
->write_by_sram
= false;
2120 dev_info(host
->dev
, "Using NFC Sram read %s\n",
2121 host
->nfc
->write_by_sram
? "and write" : "");
2125 static struct platform_driver atmel_nand_nfc_driver
;
2127 * Probe for the NAND device.
2129 static int atmel_nand_probe(struct platform_device
*pdev
)
2131 struct atmel_nand_host
*host
;
2132 struct mtd_info
*mtd
;
2133 struct nand_chip
*nand_chip
;
2134 struct resource
*mem
;
2137 /* Allocate memory for the device structure (and zero it) */
2138 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
2142 res
= platform_driver_register(&atmel_nand_nfc_driver
);
2144 dev_err(&pdev
->dev
, "atmel_nand: can't register NFC driver\n");
2146 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2147 host
->io_base
= devm_ioremap_resource(&pdev
->dev
, mem
);
2148 if (IS_ERR(host
->io_base
)) {
2149 res
= PTR_ERR(host
->io_base
);
2150 goto err_nand_ioremap
;
2152 host
->io_phys
= (dma_addr_t
)mem
->start
;
2154 nand_chip
= &host
->nand_chip
;
2155 mtd
= nand_to_mtd(nand_chip
);
2156 host
->dev
= &pdev
->dev
;
2157 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
2158 nand_set_flash_node(nand_chip
, pdev
->dev
.of_node
);
2159 /* Only when CONFIG_OF is enabled of_node can be parsed */
2160 res
= atmel_of_init_port(host
, pdev
->dev
.of_node
);
2162 goto err_nand_ioremap
;
2164 memcpy(&host
->board
, dev_get_platdata(&pdev
->dev
),
2165 sizeof(struct atmel_nand_data
));
2166 nand_chip
->ecc
.mode
= host
->board
.ecc_mode
;
2169 * When using software ECC every supported avr32 board means
2170 * Hamming algorithm. If that ever changes we'll need to add
2171 * ecc_algo field to the struct atmel_nand_data.
2173 if (nand_chip
->ecc
.mode
== NAND_ECC_SOFT
)
2174 nand_chip
->ecc
.algo
= NAND_ECC_HAMMING
;
2176 /* 16-bit bus width */
2177 if (host
->board
.bus_width_16
)
2178 nand_chip
->options
|= NAND_BUSWIDTH_16
;
2181 /* link the private data structures */
2182 nand_set_controller_data(nand_chip
, host
);
2183 mtd
->dev
.parent
= &pdev
->dev
;
2185 /* Set address of NAND IO lines */
2186 nand_chip
->IO_ADDR_R
= host
->io_base
;
2187 nand_chip
->IO_ADDR_W
= host
->io_base
;
2189 if (nand_nfc
.is_initialized
) {
2190 /* NFC driver is probed and initialized */
2191 host
->nfc
= &nand_nfc
;
2193 nand_chip
->select_chip
= nfc_select_chip
;
2194 nand_chip
->dev_ready
= nfc_device_ready
;
2195 nand_chip
->cmdfunc
= nfc_nand_command
;
2197 /* Initialize the interrupt for NFC */
2198 irq
= platform_get_irq(pdev
, 0);
2200 dev_err(host
->dev
, "Cannot get HSMC irq!\n");
2202 goto err_nand_ioremap
;
2205 res
= devm_request_irq(&pdev
->dev
, irq
, hsmc_interrupt
,
2208 dev_err(&pdev
->dev
, "Unable to request HSMC irq %d\n",
2210 goto err_nand_ioremap
;
2213 res
= atmel_nand_set_enable_ready_pins(mtd
);
2215 goto err_nand_ioremap
;
2217 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
2220 nand_chip
->chip_delay
= 40; /* 40us command delay time */
2223 nand_chip
->read_buf
= atmel_read_buf
;
2224 nand_chip
->write_buf
= atmel_write_buf
;
2226 platform_set_drvdata(pdev
, host
);
2227 atmel_nand_enable(host
);
2229 if (gpio_is_valid(host
->board
.det_pin
)) {
2230 res
= devm_gpio_request(&pdev
->dev
,
2231 host
->board
.det_pin
, "nand_det");
2234 "can't request det gpio %d\n",
2235 host
->board
.det_pin
);
2239 res
= gpio_direction_input(host
->board
.det_pin
);
2242 "can't request input direction det gpio %d\n",
2243 host
->board
.det_pin
);
2247 if (gpio_get_value(host
->board
.det_pin
)) {
2248 dev_info(&pdev
->dev
, "No SmartMedia card inserted.\n");
2254 if (!host
->board
.has_dma
)
2258 dma_cap_mask_t mask
;
2261 dma_cap_set(DMA_MEMCPY
, mask
);
2262 host
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
2263 if (!host
->dma_chan
) {
2264 dev_err(host
->dev
, "Failed to request DMA channel\n");
2269 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
2270 dma_chan_name(host
->dma_chan
));
2272 dev_info(host
->dev
, "No DMA support for NAND access.\n");
2274 /* first scan to find the device and get the page size */
2275 if (nand_scan_ident(mtd
, 1, NULL
)) {
2277 goto err_scan_ident
;
2280 if (host
->board
.on_flash_bbt
|| on_flash_bbt
)
2281 nand_chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
2283 if (nand_chip
->bbt_options
& NAND_BBT_USE_FLASH
)
2284 dev_info(&pdev
->dev
, "Use On Flash BBT\n");
2286 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
2287 res
= atmel_of_init_ecc(host
, pdev
->dev
.of_node
);
2292 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
2293 if (host
->has_pmecc
)
2294 res
= atmel_pmecc_nand_init_params(pdev
, host
);
2296 res
= atmel_hw_nand_init_params(pdev
, host
);
2302 /* initialize the nfc configuration register */
2303 if (host
->nfc
&& host
->nfc
->use_nfc_sram
) {
2304 res
= nfc_sram_init(mtd
);
2306 host
->nfc
->use_nfc_sram
= false;
2307 dev_err(host
->dev
, "Disable use nfc sram for data transfer.\n");
2311 /* second phase scan */
2312 if (nand_scan_tail(mtd
)) {
2317 mtd
->name
= "atmel_nand";
2318 res
= mtd_device_register(mtd
, host
->board
.parts
,
2319 host
->board
.num_parts
);
2324 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
)
2325 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2329 atmel_nand_disable(host
);
2331 dma_release_channel(host
->dma_chan
);
2337 * Remove a NAND device.
2339 static int atmel_nand_remove(struct platform_device
*pdev
)
2341 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
2342 struct mtd_info
*mtd
= nand_to_mtd(&host
->nand_chip
);
2346 atmel_nand_disable(host
);
2348 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) {
2349 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2350 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
,
2355 dma_release_channel(host
->dma_chan
);
2357 platform_driver_unregister(&atmel_nand_nfc_driver
);
2363 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2364 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2365 * devices from the SAM9 family that have those.
2367 static const struct atmel_nand_caps at91rm9200_caps
= {
2368 .pmecc_correct_erase_page
= false,
2369 .pmecc_max_correction
= 24,
2372 static const struct atmel_nand_caps sama5d4_caps
= {
2373 .pmecc_correct_erase_page
= true,
2374 .pmecc_max_correction
= 24,
2378 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2379 * as the increased correction strength requires more registers.
2381 static const struct atmel_nand_caps sama5d2_caps
= {
2382 .pmecc_correct_erase_page
= true,
2383 .pmecc_max_correction
= 32,
2386 static const struct of_device_id atmel_nand_dt_ids
[] = {
2387 { .compatible
= "atmel,at91rm9200-nand", .data
= &at91rm9200_caps
},
2388 { .compatible
= "atmel,sama5d4-nand", .data
= &sama5d4_caps
},
2389 { .compatible
= "atmel,sama5d2-nand", .data
= &sama5d2_caps
},
2393 MODULE_DEVICE_TABLE(of
, atmel_nand_dt_ids
);
2395 static int atmel_nand_nfc_probe(struct platform_device
*pdev
)
2397 struct atmel_nfc
*nfc
= &nand_nfc
;
2398 struct resource
*nfc_cmd_regs
, *nfc_hsmc_regs
, *nfc_sram
;
2401 nfc_cmd_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2402 nfc
->base_cmd_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_cmd_regs
);
2403 if (IS_ERR(nfc
->base_cmd_regs
))
2404 return PTR_ERR(nfc
->base_cmd_regs
);
2406 nfc_hsmc_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2407 nfc
->hsmc_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_hsmc_regs
);
2408 if (IS_ERR(nfc
->hsmc_regs
))
2409 return PTR_ERR(nfc
->hsmc_regs
);
2411 nfc_sram
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
2413 nfc
->sram_bank0
= (void * __force
)
2414 devm_ioremap_resource(&pdev
->dev
, nfc_sram
);
2415 if (IS_ERR(nfc
->sram_bank0
)) {
2416 dev_warn(&pdev
->dev
, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2417 PTR_ERR(nfc
->sram_bank0
));
2419 nfc
->use_nfc_sram
= true;
2420 nfc
->sram_bank0_phys
= (dma_addr_t
)nfc_sram
->start
;
2422 if (pdev
->dev
.of_node
)
2423 nfc
->write_by_sram
= of_property_read_bool(
2425 "atmel,write-by-sram");
2429 nfc
->caps
= (const struct atmel_nand_nfc_caps
*)
2430 of_device_get_match_data(&pdev
->dev
);
2434 nfc_writel(nfc
->hsmc_regs
, IDR
, 0xffffffff);
2435 nfc_readl(nfc
->hsmc_regs
, SR
); /* clear the NFC_SR */
2437 nfc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
2438 if (!IS_ERR(nfc
->clk
)) {
2439 ret
= clk_prepare_enable(nfc
->clk
);
2443 dev_warn(&pdev
->dev
, "NFC clock missing, update your Device Tree");
2446 nfc
->is_initialized
= true;
2447 dev_info(&pdev
->dev
, "NFC is probed.\n");
2452 static int atmel_nand_nfc_remove(struct platform_device
*pdev
)
2454 struct atmel_nfc
*nfc
= &nand_nfc
;
2456 if (!IS_ERR(nfc
->clk
))
2457 clk_disable_unprepare(nfc
->clk
);
2462 static const struct atmel_nand_nfc_caps sama5d3_nfc_caps
= {
2463 .rb_mask
= NFC_SR_RB_EDGE0
,
2466 static const struct atmel_nand_nfc_caps sama5d4_nfc_caps
= {
2467 .rb_mask
= NFC_SR_RB_EDGE3
,
2470 static const struct of_device_id atmel_nand_nfc_match
[] = {
2471 { .compatible
= "atmel,sama5d3-nfc", .data
= &sama5d3_nfc_caps
},
2472 { .compatible
= "atmel,sama5d4-nfc", .data
= &sama5d4_nfc_caps
},
2475 MODULE_DEVICE_TABLE(of
, atmel_nand_nfc_match
);
2477 static struct platform_driver atmel_nand_nfc_driver
= {
2479 .name
= "atmel_nand_nfc",
2480 .of_match_table
= of_match_ptr(atmel_nand_nfc_match
),
2482 .probe
= atmel_nand_nfc_probe
,
2483 .remove
= atmel_nand_nfc_remove
,
2486 static struct platform_driver atmel_nand_driver
= {
2487 .probe
= atmel_nand_probe
,
2488 .remove
= atmel_nand_remove
,
2490 .name
= "atmel_nand",
2491 .of_match_table
= of_match_ptr(atmel_nand_dt_ids
),
2495 module_platform_driver(atmel_nand_driver
);
2497 MODULE_LICENSE("GPL");
2498 MODULE_AUTHOR("Rick Bronson");
2499 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
2500 MODULE_ALIAS("platform:atmel_nand");