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 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2 as
23 * published by the Free Software Foundation.
27 #include <linux/dma-mapping.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/platform_device.h>
33 #include <linux/of_device.h>
34 #include <linux/of_gpio.h>
35 #include <linux/of_mtd.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/nand.h>
38 #include <linux/mtd/partitions.h>
40 #include <linux/dmaengine.h>
41 #include <linux/gpio.h>
43 #include <linux/platform_data/atmel.h>
44 #include <linux/pinctrl/consumer.h>
46 static int use_dma
= 1;
47 module_param(use_dma
, int, 0);
49 static int on_flash_bbt
= 0;
50 module_param(on_flash_bbt
, int, 0);
52 /* Register access macros */
53 #define ecc_readl(add, reg) \
54 __raw_readl(add + ATMEL_ECC_##reg)
55 #define ecc_writel(add, reg, value) \
56 __raw_writel((value), add + ATMEL_ECC_##reg)
58 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
60 /* oob layout for large page size
61 * bad block info is on bytes 0 and 1
62 * the bytes have to be consecutives to avoid
63 * several NAND_CMD_RNDOUT during read
65 static struct nand_ecclayout atmel_oobinfo_large
= {
67 .eccpos
= {60, 61, 62, 63},
73 /* oob layout for small page size
74 * bad block info is on bytes 4 and 5
75 * the bytes have to be consecutives to avoid
76 * several NAND_CMD_RNDOUT during read
78 static struct nand_ecclayout atmel_oobinfo_small
= {
80 .eccpos
= {0, 1, 2, 3},
86 struct atmel_nand_host
{
87 struct nand_chip nand_chip
;
89 void __iomem
*io_base
;
91 struct atmel_nand_data board
;
95 struct completion comp
;
96 struct dma_chan
*dma_chan
;
100 u16 pmecc_sector_size
;
101 u32 pmecc_lookup_table_offset
;
102 u32 pmecc_lookup_table_offset_512
;
103 u32 pmecc_lookup_table_offset_1024
;
105 int pmecc_bytes_per_sector
;
106 int pmecc_sector_number
;
107 int pmecc_degree
; /* Degree of remainders */
108 int pmecc_cw_len
; /* Length of codeword */
110 void __iomem
*pmerrloc_base
;
111 void __iomem
*pmecc_rom_base
;
113 /* lookup table for alpha_to and index_of */
114 void __iomem
*pmecc_alpha_to
;
115 void __iomem
*pmecc_index_of
;
117 /* data for pmecc computation */
118 int16_t *pmecc_partial_syn
;
120 int16_t *pmecc_smu
; /* Sigma table */
121 int16_t *pmecc_lmu
; /* polynomal order */
127 static struct nand_ecclayout atmel_pmecc_oobinfo
;
132 static void atmel_nand_enable(struct atmel_nand_host
*host
)
134 if (gpio_is_valid(host
->board
.enable_pin
))
135 gpio_set_value(host
->board
.enable_pin
, 0);
141 static void atmel_nand_disable(struct atmel_nand_host
*host
)
143 if (gpio_is_valid(host
->board
.enable_pin
))
144 gpio_set_value(host
->board
.enable_pin
, 1);
148 * Hardware specific access to control-lines
150 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
152 struct nand_chip
*nand_chip
= mtd
->priv
;
153 struct atmel_nand_host
*host
= nand_chip
->priv
;
155 if (ctrl
& NAND_CTRL_CHANGE
) {
157 atmel_nand_enable(host
);
159 atmel_nand_disable(host
);
161 if (cmd
== NAND_CMD_NONE
)
165 writeb(cmd
, host
->io_base
+ (1 << host
->board
.cle
));
167 writeb(cmd
, host
->io_base
+ (1 << host
->board
.ale
));
171 * Read the Device Ready pin.
173 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
175 struct nand_chip
*nand_chip
= mtd
->priv
;
176 struct atmel_nand_host
*host
= nand_chip
->priv
;
178 return gpio_get_value(host
->board
.rdy_pin
) ^
179 !!host
->board
.rdy_pin_active_low
;
183 * Minimal-overhead PIO for data access.
185 static void atmel_read_buf8(struct mtd_info
*mtd
, u8
*buf
, int len
)
187 struct nand_chip
*nand_chip
= mtd
->priv
;
189 __raw_readsb(nand_chip
->IO_ADDR_R
, buf
, len
);
192 static void atmel_read_buf16(struct mtd_info
*mtd
, u8
*buf
, int len
)
194 struct nand_chip
*nand_chip
= mtd
->priv
;
196 __raw_readsw(nand_chip
->IO_ADDR_R
, buf
, len
/ 2);
199 static void atmel_write_buf8(struct mtd_info
*mtd
, const u8
*buf
, int len
)
201 struct nand_chip
*nand_chip
= mtd
->priv
;
203 __raw_writesb(nand_chip
->IO_ADDR_W
, buf
, len
);
206 static void atmel_write_buf16(struct mtd_info
*mtd
, const u8
*buf
, int len
)
208 struct nand_chip
*nand_chip
= mtd
->priv
;
210 __raw_writesw(nand_chip
->IO_ADDR_W
, buf
, len
/ 2);
213 static void dma_complete_func(void *completion
)
215 complete(completion
);
218 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
221 struct dma_device
*dma_dev
;
222 enum dma_ctrl_flags flags
;
223 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
224 struct dma_async_tx_descriptor
*tx
= NULL
;
226 struct nand_chip
*chip
= mtd
->priv
;
227 struct atmel_nand_host
*host
= chip
->priv
;
230 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
232 if (buf
>= high_memory
)
235 dma_dev
= host
->dma_chan
->device
;
237 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
| DMA_COMPL_SKIP_SRC_UNMAP
|
238 DMA_COMPL_SKIP_DEST_UNMAP
;
240 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
241 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
242 dev_err(host
->dev
, "Failed to dma_map_single\n");
247 dma_src_addr
= host
->io_phys
;
248 dma_dst_addr
= phys_addr
;
250 dma_src_addr
= phys_addr
;
251 dma_dst_addr
= host
->io_phys
;
254 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
255 dma_src_addr
, len
, flags
);
257 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
261 init_completion(&host
->comp
);
262 tx
->callback
= dma_complete_func
;
263 tx
->callback_param
= &host
->comp
;
265 cookie
= tx
->tx_submit(tx
);
266 if (dma_submit_error(cookie
)) {
267 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
271 dma_async_issue_pending(host
->dma_chan
);
272 wait_for_completion(&host
->comp
);
277 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
280 dev_warn(host
->dev
, "Fall back to CPU I/O\n");
284 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
286 struct nand_chip
*chip
= mtd
->priv
;
287 struct atmel_nand_host
*host
= chip
->priv
;
289 if (use_dma
&& len
> mtd
->oobsize
)
290 /* only use DMA for bigger than oob size: better performances */
291 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
294 if (host
->board
.bus_width_16
)
295 atmel_read_buf16(mtd
, buf
, len
);
297 atmel_read_buf8(mtd
, buf
, len
);
300 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
302 struct nand_chip
*chip
= mtd
->priv
;
303 struct atmel_nand_host
*host
= chip
->priv
;
305 if (use_dma
&& len
> mtd
->oobsize
)
306 /* only use DMA for bigger than oob size: better performances */
307 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
310 if (host
->board
.bus_width_16
)
311 atmel_write_buf16(mtd
, buf
, len
);
313 atmel_write_buf8(mtd
, buf
, len
);
317 * Return number of ecc bytes per sector according to sector size and
318 * correction capability
320 * Following table shows what at91 PMECC supported:
321 * Correction Capability Sector_512_bytes Sector_1024_bytes
322 * ===================== ================ =================
323 * 2-bits 4-bytes 4-bytes
324 * 4-bits 7-bytes 7-bytes
325 * 8-bits 13-bytes 14-bytes
326 * 12-bits 20-bytes 21-bytes
327 * 24-bits 39-bytes 42-bytes
329 static int pmecc_get_ecc_bytes(int cap
, int sector_size
)
331 int m
= 12 + sector_size
/ 512;
332 return (m
* cap
+ 7) / 8;
335 static void pmecc_config_ecc_layout(struct nand_ecclayout
*layout
,
336 int oobsize
, int ecc_len
)
340 layout
->eccbytes
= ecc_len
;
342 /* ECC will occupy the last ecc_len bytes continuously */
343 for (i
= 0; i
< ecc_len
; i
++)
344 layout
->eccpos
[i
] = oobsize
- ecc_len
+ i
;
346 layout
->oobfree
[0].offset
= 2;
347 layout
->oobfree
[0].length
=
348 oobsize
- ecc_len
- layout
->oobfree
[0].offset
;
351 static void __iomem
*pmecc_get_alpha_to(struct atmel_nand_host
*host
)
355 table_size
= host
->pmecc_sector_size
== 512 ?
356 PMECC_LOOKUP_TABLE_SIZE_512
: PMECC_LOOKUP_TABLE_SIZE_1024
;
358 return host
->pmecc_rom_base
+ host
->pmecc_lookup_table_offset
+
359 table_size
* sizeof(int16_t);
362 static void pmecc_data_free(struct atmel_nand_host
*host
)
364 kfree(host
->pmecc_partial_syn
);
365 kfree(host
->pmecc_si
);
366 kfree(host
->pmecc_lmu
);
367 kfree(host
->pmecc_smu
);
368 kfree(host
->pmecc_mu
);
369 kfree(host
->pmecc_dmu
);
370 kfree(host
->pmecc_delta
);
373 static int pmecc_data_alloc(struct atmel_nand_host
*host
)
375 const int cap
= host
->pmecc_corr_cap
;
377 host
->pmecc_partial_syn
= kzalloc((2 * cap
+ 1) * sizeof(int16_t),
379 host
->pmecc_si
= kzalloc((2 * cap
+ 1) * sizeof(int16_t), GFP_KERNEL
);
380 host
->pmecc_lmu
= kzalloc((cap
+ 1) * sizeof(int16_t), GFP_KERNEL
);
381 host
->pmecc_smu
= kzalloc((cap
+ 2) * (2 * cap
+ 1) * sizeof(int16_t),
383 host
->pmecc_mu
= kzalloc((cap
+ 1) * sizeof(int), GFP_KERNEL
);
384 host
->pmecc_dmu
= kzalloc((cap
+ 1) * sizeof(int), GFP_KERNEL
);
385 host
->pmecc_delta
= kzalloc((cap
+ 1) * sizeof(int), GFP_KERNEL
);
387 if (host
->pmecc_partial_syn
&&
397 pmecc_data_free(host
);
401 static void pmecc_gen_syndrome(struct mtd_info
*mtd
, int sector
)
403 struct nand_chip
*nand_chip
= mtd
->priv
;
404 struct atmel_nand_host
*host
= nand_chip
->priv
;
408 /* Fill odd syndromes */
409 for (i
= 0; i
< host
->pmecc_corr_cap
; i
++) {
410 value
= pmecc_readl_rem_relaxed(host
->ecc
, sector
, i
/ 2);
414 host
->pmecc_partial_syn
[(2 * i
) + 1] = (int16_t)value
;
418 static void pmecc_substitute(struct mtd_info
*mtd
)
420 struct nand_chip
*nand_chip
= mtd
->priv
;
421 struct atmel_nand_host
*host
= nand_chip
->priv
;
422 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
423 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
424 int16_t *partial_syn
= host
->pmecc_partial_syn
;
425 const int cap
= host
->pmecc_corr_cap
;
429 /* si[] is a table that holds the current syndrome value,
430 * an element of that table belongs to the field
434 memset(&si
[1], 0, sizeof(int16_t) * (2 * cap
- 1));
436 /* Computation 2t syndromes based on S(x) */
438 for (i
= 1; i
< 2 * cap
; i
+= 2) {
439 for (j
= 0; j
< host
->pmecc_degree
; j
++) {
440 if (partial_syn
[i
] & ((unsigned short)0x1 << j
))
441 si
[i
] = readw_relaxed(alpha_to
+ i
* j
) ^ si
[i
];
444 /* Even syndrome = (Odd syndrome) ** 2 */
445 for (i
= 2, j
= 1; j
<= cap
; i
= ++j
<< 1) {
451 tmp
= readw_relaxed(index_of
+ si
[j
]);
452 tmp
= (tmp
* 2) % host
->pmecc_cw_len
;
453 si
[i
] = readw_relaxed(alpha_to
+ tmp
);
460 static void pmecc_get_sigma(struct mtd_info
*mtd
)
462 struct nand_chip
*nand_chip
= mtd
->priv
;
463 struct atmel_nand_host
*host
= nand_chip
->priv
;
465 int16_t *lmu
= host
->pmecc_lmu
;
466 int16_t *si
= host
->pmecc_si
;
467 int *mu
= host
->pmecc_mu
;
468 int *dmu
= host
->pmecc_dmu
; /* Discrepancy */
469 int *delta
= host
->pmecc_delta
; /* Delta order */
470 int cw_len
= host
->pmecc_cw_len
;
471 const int16_t cap
= host
->pmecc_corr_cap
;
472 const int num
= 2 * cap
+ 1;
473 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
474 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
476 uint32_t dmu_0_count
, tmp
;
477 int16_t *smu
= host
->pmecc_smu
;
479 /* index of largest delta */
491 memset(smu
, 0, sizeof(int16_t) * num
);
494 /* discrepancy set to 1 */
496 /* polynom order set to 0 */
498 delta
[0] = (mu
[0] * 2 - lmu
[0]) >> 1;
504 /* Sigma(x) set to 1 */
505 memset(&smu
[num
], 0, sizeof(int16_t) * num
);
508 /* discrepancy set to S1 */
511 /* polynom order set to 0 */
514 delta
[1] = (mu
[1] * 2 - lmu
[1]) >> 1;
516 /* Init the Sigma(x) last row */
517 memset(&smu
[(cap
+ 1) * num
], 0, sizeof(int16_t) * num
);
519 for (i
= 1; i
<= cap
; i
++) {
521 /* Begin Computing Sigma (Mu+1) and L(mu) */
522 /* check if discrepancy is set to 0 */
526 tmp
= ((cap
- (lmu
[i
] >> 1) - 1) / 2);
527 if ((cap
- (lmu
[i
] >> 1) - 1) & 0x1)
532 if (dmu_0_count
== tmp
) {
533 for (j
= 0; j
<= (lmu
[i
] >> 1) + 1; j
++)
534 smu
[(cap
+ 1) * num
+ j
] =
537 lmu
[cap
+ 1] = lmu
[i
];
542 for (j
= 0; j
<= lmu
[i
] >> 1; j
++)
543 smu
[(i
+ 1) * num
+ j
] = smu
[i
* num
+ j
];
545 /* copy previous polynom order to the next */
550 /* find largest delta with dmu != 0 */
551 for (j
= 0; j
< i
; j
++) {
552 if ((dmu
[j
]) && (delta
[j
] > largest
)) {
558 /* compute difference */
559 diff
= (mu
[i
] - mu
[ro
]);
561 /* Compute degree of the new smu polynomial */
562 if ((lmu
[i
] >> 1) > ((lmu
[ro
] >> 1) + diff
))
565 lmu
[i
+ 1] = ((lmu
[ro
] >> 1) + diff
) * 2;
567 /* Init smu[i+1] with 0 */
568 for (k
= 0; k
< num
; k
++)
569 smu
[(i
+ 1) * num
+ k
] = 0;
571 /* Compute smu[i+1] */
572 for (k
= 0; k
<= lmu
[ro
] >> 1; k
++) {
575 if (!(smu
[ro
* num
+ k
] && dmu
[i
]))
577 a
= readw_relaxed(index_of
+ dmu
[i
]);
578 b
= readw_relaxed(index_of
+ dmu
[ro
]);
579 c
= readw_relaxed(index_of
+ smu
[ro
* num
+ k
]);
580 tmp
= a
+ (cw_len
- b
) + c
;
581 a
= readw_relaxed(alpha_to
+ tmp
% cw_len
);
582 smu
[(i
+ 1) * num
+ (k
+ diff
)] = a
;
585 for (k
= 0; k
<= lmu
[i
] >> 1; k
++)
586 smu
[(i
+ 1) * num
+ k
] ^= smu
[i
* num
+ k
];
589 /* End Computing Sigma (Mu+1) and L(mu) */
590 /* In either case compute delta */
591 delta
[i
+ 1] = (mu
[i
+ 1] * 2 - lmu
[i
+ 1]) >> 1;
593 /* Do not compute discrepancy for the last iteration */
597 for (k
= 0; k
<= (lmu
[i
+ 1] >> 1); k
++) {
600 dmu
[i
+ 1] = si
[tmp
+ 3];
601 } else if (smu
[(i
+ 1) * num
+ k
] && si
[tmp
+ 3 - k
]) {
603 a
= readw_relaxed(index_of
+
604 smu
[(i
+ 1) * num
+ k
]);
605 b
= si
[2 * (i
- 1) + 3 - k
];
606 c
= readw_relaxed(index_of
+ b
);
609 dmu
[i
+ 1] = readw_relaxed(alpha_to
+ tmp
) ^
618 static int pmecc_err_location(struct mtd_info
*mtd
)
620 struct nand_chip
*nand_chip
= mtd
->priv
;
621 struct atmel_nand_host
*host
= nand_chip
->priv
;
622 unsigned long end_time
;
623 const int cap
= host
->pmecc_corr_cap
;
624 const int num
= 2 * cap
+ 1;
625 int sector_size
= host
->pmecc_sector_size
;
626 int err_nbr
= 0; /* number of error */
627 int roots_nbr
; /* number of roots */
630 int16_t *smu
= host
->pmecc_smu
;
632 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
, PMERRLOC_DISABLE
);
634 for (i
= 0; i
<= host
->pmecc_lmu
[cap
+ 1] >> 1; i
++) {
635 pmerrloc_writel_sigma_relaxed(host
->pmerrloc_base
, i
,
636 smu
[(cap
+ 1) * num
+ i
]);
640 val
= (err_nbr
- 1) << 16;
641 if (sector_size
== 1024)
644 pmerrloc_writel(host
->pmerrloc_base
, ELCFG
, val
);
645 pmerrloc_writel(host
->pmerrloc_base
, ELEN
,
646 sector_size
* 8 + host
->pmecc_degree
* cap
);
648 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
649 while (!(pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
650 & PMERRLOC_CALC_DONE
)) {
651 if (unlikely(time_after(jiffies
, end_time
))) {
652 dev_err(host
->dev
, "PMECC: Timeout to calculate error location.\n");
658 roots_nbr
= (pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
659 & PMERRLOC_ERR_NUM_MASK
) >> 8;
660 /* Number of roots == degree of smu hence <= cap */
661 if (roots_nbr
== host
->pmecc_lmu
[cap
+ 1] >> 1)
664 /* Number of roots does not match the degree of smu
665 * unable to correct error */
669 static void pmecc_correct_data(struct mtd_info
*mtd
, uint8_t *buf
, uint8_t *ecc
,
670 int sector_num
, int extra_bytes
, int err_nbr
)
672 struct nand_chip
*nand_chip
= mtd
->priv
;
673 struct atmel_nand_host
*host
= nand_chip
->priv
;
675 int byte_pos
, bit_pos
, sector_size
, pos
;
679 sector_size
= host
->pmecc_sector_size
;
682 tmp
= pmerrloc_readl_el_relaxed(host
->pmerrloc_base
, i
) - 1;
686 if (byte_pos
>= (sector_size
+ extra_bytes
))
687 BUG(); /* should never happen */
689 if (byte_pos
< sector_size
) {
690 err_byte
= *(buf
+ byte_pos
);
691 *(buf
+ byte_pos
) ^= (1 << bit_pos
);
693 pos
= sector_num
* host
->pmecc_sector_size
+ byte_pos
;
694 dev_info(host
->dev
, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
695 pos
, bit_pos
, err_byte
, *(buf
+ byte_pos
));
697 /* Bit flip in OOB area */
698 tmp
= sector_num
* host
->pmecc_bytes_per_sector
699 + (byte_pos
- sector_size
);
701 ecc
[tmp
] ^= (1 << bit_pos
);
703 pos
= tmp
+ nand_chip
->ecc
.layout
->eccpos
[0];
704 dev_info(host
->dev
, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
705 pos
, bit_pos
, err_byte
, ecc
[tmp
]);
715 static int pmecc_correction(struct mtd_info
*mtd
, u32 pmecc_stat
, uint8_t *buf
,
718 struct nand_chip
*nand_chip
= mtd
->priv
;
719 struct atmel_nand_host
*host
= nand_chip
->priv
;
720 int i
, err_nbr
, eccbytes
;
724 eccbytes
= nand_chip
->ecc
.bytes
;
725 for (i
= 0; i
< eccbytes
; i
++)
728 /* Erased page, return OK */
732 for (i
= 0; i
< host
->pmecc_sector_number
; i
++) {
734 if (pmecc_stat
& 0x1) {
735 buf_pos
= buf
+ i
* host
->pmecc_sector_size
;
737 pmecc_gen_syndrome(mtd
, i
);
738 pmecc_substitute(mtd
);
739 pmecc_get_sigma(mtd
);
741 err_nbr
= pmecc_err_location(mtd
);
743 dev_err(host
->dev
, "PMECC: Too many errors\n");
744 mtd
->ecc_stats
.failed
++;
747 pmecc_correct_data(mtd
, buf_pos
, ecc
, i
,
748 host
->pmecc_bytes_per_sector
, err_nbr
);
749 mtd
->ecc_stats
.corrected
+= err_nbr
;
750 total_err
+= err_nbr
;
759 static int atmel_nand_pmecc_read_page(struct mtd_info
*mtd
,
760 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
762 struct atmel_nand_host
*host
= chip
->priv
;
763 int eccsize
= chip
->ecc
.size
;
764 uint8_t *oob
= chip
->oob_poi
;
765 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
767 unsigned long end_time
;
770 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
771 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
772 pmecc_writel(host
->ecc
, CFG
, (pmecc_readl_relaxed(host
->ecc
, CFG
)
773 & ~PMECC_CFG_WRITE_OP
) | PMECC_CFG_AUTO_ENABLE
);
775 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
776 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DATA
);
778 chip
->read_buf(mtd
, buf
, eccsize
);
779 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
781 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
782 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
783 if (unlikely(time_after(jiffies
, end_time
))) {
784 dev_err(host
->dev
, "PMECC: Timeout to get error status.\n");
790 stat
= pmecc_readl_relaxed(host
->ecc
, ISR
);
792 bitflips
= pmecc_correction(mtd
, stat
, buf
, &oob
[eccpos
[0]]);
794 /* uncorrectable errors */
801 static int atmel_nand_pmecc_write_page(struct mtd_info
*mtd
,
802 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
)
804 struct atmel_nand_host
*host
= chip
->priv
;
805 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
807 unsigned long end_time
;
809 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
810 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
812 pmecc_writel(host
->ecc
, CFG
, (pmecc_readl_relaxed(host
->ecc
, CFG
) |
813 PMECC_CFG_WRITE_OP
) & ~PMECC_CFG_AUTO_ENABLE
);
815 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
816 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DATA
);
818 chip
->write_buf(mtd
, (u8
*)buf
, mtd
->writesize
);
820 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
821 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
822 if (unlikely(time_after(jiffies
, end_time
))) {
823 dev_err(host
->dev
, "PMECC: Timeout to get ECC value.\n");
829 for (i
= 0; i
< host
->pmecc_sector_number
; i
++) {
830 for (j
= 0; j
< host
->pmecc_bytes_per_sector
; j
++) {
833 pos
= i
* host
->pmecc_bytes_per_sector
+ j
;
834 chip
->oob_poi
[eccpos
[pos
]] =
835 pmecc_readb_ecc_relaxed(host
->ecc
, i
, j
);
838 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
843 static void atmel_pmecc_core_init(struct mtd_info
*mtd
)
845 struct nand_chip
*nand_chip
= mtd
->priv
;
846 struct atmel_nand_host
*host
= nand_chip
->priv
;
848 struct nand_ecclayout
*ecc_layout
;
850 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
851 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
853 switch (host
->pmecc_corr_cap
) {
855 val
= PMECC_CFG_BCH_ERR2
;
858 val
= PMECC_CFG_BCH_ERR4
;
861 val
= PMECC_CFG_BCH_ERR8
;
864 val
= PMECC_CFG_BCH_ERR12
;
867 val
= PMECC_CFG_BCH_ERR24
;
871 if (host
->pmecc_sector_size
== 512)
872 val
|= PMECC_CFG_SECTOR512
;
873 else if (host
->pmecc_sector_size
== 1024)
874 val
|= PMECC_CFG_SECTOR1024
;
876 switch (host
->pmecc_sector_number
) {
878 val
|= PMECC_CFG_PAGE_1SECTOR
;
881 val
|= PMECC_CFG_PAGE_2SECTORS
;
884 val
|= PMECC_CFG_PAGE_4SECTORS
;
887 val
|= PMECC_CFG_PAGE_8SECTORS
;
891 val
|= (PMECC_CFG_READ_OP
| PMECC_CFG_SPARE_DISABLE
892 | PMECC_CFG_AUTO_DISABLE
);
893 pmecc_writel(host
->ecc
, CFG
, val
);
895 ecc_layout
= nand_chip
->ecc
.layout
;
896 pmecc_writel(host
->ecc
, SAREA
, mtd
->oobsize
- 1);
897 pmecc_writel(host
->ecc
, SADDR
, ecc_layout
->eccpos
[0]);
898 pmecc_writel(host
->ecc
, EADDR
,
899 ecc_layout
->eccpos
[ecc_layout
->eccbytes
- 1]);
900 /* See datasheet about PMECC Clock Control Register */
901 pmecc_writel(host
->ecc
, CLK
, 2);
902 pmecc_writel(host
->ecc
, IDR
, 0xff);
903 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
907 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
908 * parameters is not supported.
909 * return 0 if success to get the ECC requirement.
911 static int get_onfi_ecc_param(struct nand_chip
*chip
,
912 int *ecc_bits
, int *sector_size
)
914 *ecc_bits
= *sector_size
= 0;
916 if (chip
->onfi_params
.ecc_bits
== 0xff)
917 /* TODO: the sector_size and ecc_bits need to be find in
918 * extended ecc parameter, currently we don't support it.
922 *ecc_bits
= chip
->onfi_params
.ecc_bits
;
924 /* The default sector size (ecc codeword size) is 512 */
931 * Get ecc requirement from ONFI parameters ecc requirement.
932 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
933 * will set them according to ONFI ecc requirement. Otherwise, use the
935 * return 0 if success. otherwise return error code.
937 static int pmecc_choose_ecc(struct atmel_nand_host
*host
,
938 int *cap
, int *sector_size
)
940 /* Get ECC requirement from ONFI parameters */
941 *cap
= *sector_size
= 0;
942 if (host
->nand_chip
.onfi_version
) {
943 if (!get_onfi_ecc_param(&host
->nand_chip
, cap
, sector_size
))
944 dev_info(host
->dev
, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
947 dev_info(host
->dev
, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
949 dev_info(host
->dev
, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
951 if (*cap
== 0 && *sector_size
== 0) {
956 /* If dts file doesn't specify then use the one in ONFI parameters */
957 if (host
->pmecc_corr_cap
== 0) {
958 /* use the most fitable ecc bits (the near bigger one ) */
960 host
->pmecc_corr_cap
= 2;
962 host
->pmecc_corr_cap
= 4;
964 host
->pmecc_corr_cap
= 8;
966 host
->pmecc_corr_cap
= 12;
968 host
->pmecc_corr_cap
= 24;
972 if (host
->pmecc_sector_size
== 0) {
973 /* use the most fitable sector size (the near smaller one ) */
974 if (*sector_size
>= 1024)
975 host
->pmecc_sector_size
= 1024;
976 else if (*sector_size
>= 512)
977 host
->pmecc_sector_size
= 512;
984 static int __init
atmel_pmecc_nand_init_params(struct platform_device
*pdev
,
985 struct atmel_nand_host
*host
)
987 struct mtd_info
*mtd
= &host
->mtd
;
988 struct nand_chip
*nand_chip
= &host
->nand_chip
;
989 struct resource
*regs
, *regs_pmerr
, *regs_rom
;
990 int cap
, sector_size
, err_no
;
992 err_no
= pmecc_choose_ecc(host
, &cap
, §or_size
);
994 dev_err(host
->dev
, "The NAND flash's ECC requirement are not support!");
998 if (cap
!= host
->pmecc_corr_cap
||
999 sector_size
!= host
->pmecc_sector_size
)
1000 dev_info(host
->dev
, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
1002 cap
= host
->pmecc_corr_cap
;
1003 sector_size
= host
->pmecc_sector_size
;
1004 host
->pmecc_lookup_table_offset
= (sector_size
== 512) ?
1005 host
->pmecc_lookup_table_offset_512
:
1006 host
->pmecc_lookup_table_offset_1024
;
1008 dev_info(host
->dev
, "Initialize PMECC params, cap: %d, sector: %d\n",
1011 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1014 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1015 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1019 host
->ecc
= ioremap(regs
->start
, resource_size(regs
));
1020 if (host
->ecc
== NULL
) {
1021 dev_err(host
->dev
, "ioremap failed\n");
1023 goto err_pmecc_ioremap
;
1026 regs_pmerr
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1027 regs_rom
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
1028 if (regs_pmerr
&& regs_rom
) {
1029 host
->pmerrloc_base
= ioremap(regs_pmerr
->start
,
1030 resource_size(regs_pmerr
));
1031 host
->pmecc_rom_base
= ioremap(regs_rom
->start
,
1032 resource_size(regs_rom
));
1035 if (!host
->pmerrloc_base
|| !host
->pmecc_rom_base
) {
1037 "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
1039 goto err_pmloc_ioremap
;
1042 /* ECC is calculated for the whole page (1 step) */
1043 nand_chip
->ecc
.size
= mtd
->writesize
;
1045 /* set ECC page size and oob layout */
1046 switch (mtd
->writesize
) {
1048 host
->pmecc_degree
= PMECC_GF_DIMENSION_13
;
1049 host
->pmecc_cw_len
= (1 << host
->pmecc_degree
) - 1;
1050 host
->pmecc_sector_number
= mtd
->writesize
/ sector_size
;
1051 host
->pmecc_bytes_per_sector
= pmecc_get_ecc_bytes(
1053 host
->pmecc_alpha_to
= pmecc_get_alpha_to(host
);
1054 host
->pmecc_index_of
= host
->pmecc_rom_base
+
1055 host
->pmecc_lookup_table_offset
;
1057 nand_chip
->ecc
.steps
= 1;
1058 nand_chip
->ecc
.strength
= cap
;
1059 nand_chip
->ecc
.bytes
= host
->pmecc_bytes_per_sector
*
1060 host
->pmecc_sector_number
;
1061 if (nand_chip
->ecc
.bytes
> mtd
->oobsize
- 2) {
1062 dev_err(host
->dev
, "No room for ECC bytes\n");
1064 goto err_no_ecc_room
;
1066 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo
,
1068 nand_chip
->ecc
.bytes
);
1069 nand_chip
->ecc
.layout
= &atmel_pmecc_oobinfo
;
1076 "Unsupported page size for PMECC, use Software ECC\n");
1078 /* page size not handled by HW ECC */
1079 /* switching back to soft ECC */
1080 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1084 /* Allocate data for PMECC computation */
1085 err_no
= pmecc_data_alloc(host
);
1088 "Cannot allocate memory for PMECC computation!\n");
1089 goto err_pmecc_data_alloc
;
1092 nand_chip
->ecc
.read_page
= atmel_nand_pmecc_read_page
;
1093 nand_chip
->ecc
.write_page
= atmel_nand_pmecc_write_page
;
1095 atmel_pmecc_core_init(mtd
);
1099 err_pmecc_data_alloc
:
1103 if (host
->pmerrloc_base
)
1104 iounmap(host
->pmerrloc_base
);
1105 if (host
->pmecc_rom_base
)
1106 iounmap(host
->pmecc_rom_base
);
1114 * function called after a write
1116 * mtd: MTD block structure
1117 * dat: raw data (unused)
1118 * ecc_code: buffer for ECC
1120 static int atmel_nand_calculate(struct mtd_info
*mtd
,
1121 const u_char
*dat
, unsigned char *ecc_code
)
1123 struct nand_chip
*nand_chip
= mtd
->priv
;
1124 struct atmel_nand_host
*host
= nand_chip
->priv
;
1125 unsigned int ecc_value
;
1127 /* get the first 2 ECC bytes */
1128 ecc_value
= ecc_readl(host
->ecc
, PR
);
1130 ecc_code
[0] = ecc_value
& 0xFF;
1131 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
1133 /* get the last 2 ECC bytes */
1134 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
1136 ecc_code
[2] = ecc_value
& 0xFF;
1137 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
1143 * HW ECC read page function
1145 * mtd: mtd info structure
1146 * chip: nand chip info structure
1147 * buf: buffer to store read data
1148 * oob_required: caller expects OOB data read to chip->oob_poi
1150 static int atmel_nand_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1151 uint8_t *buf
, int oob_required
, int page
)
1153 int eccsize
= chip
->ecc
.size
;
1154 int eccbytes
= chip
->ecc
.bytes
;
1155 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
1157 uint8_t *oob
= chip
->oob_poi
;
1160 unsigned int max_bitflips
= 0;
1163 * Errata: ALE is incorrectly wired up to the ECC controller
1164 * on the AP7000, so it will include the address cycles in the
1167 * Workaround: Reset the parity registers before reading the
1170 struct atmel_nand_host
*host
= chip
->priv
;
1171 if (host
->board
.need_reset_workaround
)
1172 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1175 chip
->read_buf(mtd
, p
, eccsize
);
1177 /* move to ECC position if needed */
1178 if (eccpos
[0] != 0) {
1179 /* This only works on large pages
1180 * because the ECC controller waits for
1181 * NAND_CMD_RNDOUTSTART after the
1183 * anyway, for small pages, the eccpos[0] == 0
1185 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
1186 mtd
->writesize
+ eccpos
[0], -1);
1189 /* the ECC controller needs to read the ECC just after the data */
1190 ecc_pos
= oob
+ eccpos
[0];
1191 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
1193 /* check if there's an error */
1194 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
1197 mtd
->ecc_stats
.failed
++;
1199 mtd
->ecc_stats
.corrected
+= stat
;
1200 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1203 /* get back to oob start (end of page) */
1204 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
1207 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
1209 return max_bitflips
;
1215 * function called after a read
1217 * mtd: MTD block structure
1218 * dat: raw data read from the chip
1219 * read_ecc: ECC from the chip (unused)
1222 * Detect and correct a 1 bit error for a page
1224 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
1225 u_char
*read_ecc
, u_char
*isnull
)
1227 struct nand_chip
*nand_chip
= mtd
->priv
;
1228 struct atmel_nand_host
*host
= nand_chip
->priv
;
1229 unsigned int ecc_status
;
1230 unsigned int ecc_word
, ecc_bit
;
1232 /* get the status from the Status Register */
1233 ecc_status
= ecc_readl(host
->ecc
, SR
);
1235 /* if there's no error */
1236 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
1239 /* get error bit offset (4 bits) */
1240 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
1241 /* get word address (12 bits) */
1242 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
1245 /* if there are multiple errors */
1246 if (ecc_status
& ATMEL_ECC_MULERR
) {
1247 /* check if it is a freshly erased block
1248 * (filled with 0xff) */
1249 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
1250 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
1251 /* the block has just been erased, return OK */
1254 /* it doesn't seems to be a freshly
1256 * We can't correct so many errors */
1257 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
1258 " Unable to correct.\n");
1262 /* if there's a single bit error : we can correct it */
1263 if (ecc_status
& ATMEL_ECC_ECCERR
) {
1264 /* there's nothing much to do here.
1265 * the bit error is on the ECC itself.
1267 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
1268 " Nothing to correct\n");
1272 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
1273 " (word offset in the page :"
1274 " 0x%x bit offset : 0x%x)\n",
1276 /* correct the error */
1277 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
1279 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
1282 dat
[ecc_word
] ^= (1 << ecc_bit
);
1284 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
1289 * Enable HW ECC : unused on most chips
1291 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
1293 struct nand_chip
*nand_chip
= mtd
->priv
;
1294 struct atmel_nand_host
*host
= nand_chip
->priv
;
1296 if (host
->board
.need_reset_workaround
)
1297 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1300 #if defined(CONFIG_OF)
1301 static int atmel_of_init_port(struct atmel_nand_host
*host
,
1302 struct device_node
*np
)
1307 struct atmel_nand_data
*board
= &host
->board
;
1308 enum of_gpio_flags flags
;
1310 if (of_property_read_u32(np
, "atmel,nand-addr-offset", &val
) == 0) {
1312 dev_err(host
->dev
, "invalid addr-offset %u\n", val
);
1318 if (of_property_read_u32(np
, "atmel,nand-cmd-offset", &val
) == 0) {
1320 dev_err(host
->dev
, "invalid cmd-offset %u\n", val
);
1326 ecc_mode
= of_get_nand_ecc_mode(np
);
1328 board
->ecc_mode
= ecc_mode
< 0 ? NAND_ECC_SOFT
: ecc_mode
;
1330 board
->on_flash_bbt
= of_get_nand_on_flash_bbt(np
);
1332 board
->has_dma
= of_property_read_bool(np
, "atmel,nand-has-dma");
1334 if (of_get_nand_bus_width(np
) == 16)
1335 board
->bus_width_16
= 1;
1337 board
->rdy_pin
= of_get_gpio_flags(np
, 0, &flags
);
1338 board
->rdy_pin_active_low
= (flags
== OF_GPIO_ACTIVE_LOW
);
1340 board
->enable_pin
= of_get_gpio(np
, 1);
1341 board
->det_pin
= of_get_gpio(np
, 2);
1343 host
->has_pmecc
= of_property_read_bool(np
, "atmel,has-pmecc");
1345 if (!(board
->ecc_mode
== NAND_ECC_HW
) || !host
->has_pmecc
)
1346 return 0; /* Not using PMECC */
1348 /* use PMECC, get correction capability, sector size and lookup
1350 * If correction bits and sector size are not specified, then find
1351 * them from NAND ONFI parameters.
1353 if (of_property_read_u32(np
, "atmel,pmecc-cap", &val
) == 0) {
1354 if ((val
!= 2) && (val
!= 4) && (val
!= 8) && (val
!= 12) &&
1357 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1361 host
->pmecc_corr_cap
= (u8
)val
;
1364 if (of_property_read_u32(np
, "atmel,pmecc-sector-size", &val
) == 0) {
1365 if ((val
!= 512) && (val
!= 1024)) {
1367 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1371 host
->pmecc_sector_size
= (u16
)val
;
1374 if (of_property_read_u32_array(np
, "atmel,pmecc-lookup-table-offset",
1376 dev_err(host
->dev
, "Cannot get PMECC lookup table offset\n");
1379 if (!offset
[0] && !offset
[1]) {
1380 dev_err(host
->dev
, "Invalid PMECC lookup table offset\n");
1383 host
->pmecc_lookup_table_offset_512
= offset
[0];
1384 host
->pmecc_lookup_table_offset_1024
= offset
[1];
1389 static int atmel_of_init_port(struct atmel_nand_host
*host
,
1390 struct device_node
*np
)
1396 static int __init
atmel_hw_nand_init_params(struct platform_device
*pdev
,
1397 struct atmel_nand_host
*host
)
1399 struct mtd_info
*mtd
= &host
->mtd
;
1400 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1401 struct resource
*regs
;
1403 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1406 "Can't get I/O resource regs, use software ECC\n");
1407 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1411 host
->ecc
= ioremap(regs
->start
, resource_size(regs
));
1412 if (host
->ecc
== NULL
) {
1413 dev_err(host
->dev
, "ioremap failed\n");
1417 /* ECC is calculated for the whole page (1 step) */
1418 nand_chip
->ecc
.size
= mtd
->writesize
;
1420 /* set ECC page size and oob layout */
1421 switch (mtd
->writesize
) {
1423 nand_chip
->ecc
.layout
= &atmel_oobinfo_small
;
1424 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
1427 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1428 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
1431 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1432 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
1435 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1436 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
1439 /* page size not handled by HW ECC */
1440 /* switching back to soft ECC */
1441 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1445 /* set up for HW ECC */
1446 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
1447 nand_chip
->ecc
.correct
= atmel_nand_correct
;
1448 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
1449 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
1450 nand_chip
->ecc
.bytes
= 4;
1451 nand_chip
->ecc
.strength
= 1;
1457 * Probe for the NAND device.
1459 static int __init
atmel_nand_probe(struct platform_device
*pdev
)
1461 struct atmel_nand_host
*host
;
1462 struct mtd_info
*mtd
;
1463 struct nand_chip
*nand_chip
;
1464 struct resource
*mem
;
1465 struct mtd_part_parser_data ppdata
= {};
1467 struct pinctrl
*pinctrl
;
1469 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1471 printk(KERN_ERR
"atmel_nand: can't get I/O resource mem\n");
1475 /* Allocate memory for the device structure (and zero it) */
1476 host
= kzalloc(sizeof(struct atmel_nand_host
), GFP_KERNEL
);
1478 printk(KERN_ERR
"atmel_nand: failed to allocate device structure.\n");
1482 host
->io_phys
= (dma_addr_t
)mem
->start
;
1484 host
->io_base
= ioremap(mem
->start
, resource_size(mem
));
1485 if (host
->io_base
== NULL
) {
1486 printk(KERN_ERR
"atmel_nand: ioremap failed\n");
1488 goto err_nand_ioremap
;
1492 nand_chip
= &host
->nand_chip
;
1493 host
->dev
= &pdev
->dev
;
1494 if (pdev
->dev
.of_node
) {
1495 res
= atmel_of_init_port(host
, pdev
->dev
.of_node
);
1497 goto err_ecc_ioremap
;
1499 memcpy(&host
->board
, pdev
->dev
.platform_data
,
1500 sizeof(struct atmel_nand_data
));
1503 nand_chip
->priv
= host
; /* link the private data structures */
1504 mtd
->priv
= nand_chip
;
1505 mtd
->owner
= THIS_MODULE
;
1507 /* Set address of NAND IO lines */
1508 nand_chip
->IO_ADDR_R
= host
->io_base
;
1509 nand_chip
->IO_ADDR_W
= host
->io_base
;
1510 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
1512 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
1513 if (IS_ERR(pinctrl
)) {
1514 dev_err(host
->dev
, "Failed to request pinctrl\n");
1515 res
= PTR_ERR(pinctrl
);
1516 goto err_ecc_ioremap
;
1519 if (gpio_is_valid(host
->board
.rdy_pin
)) {
1520 res
= gpio_request(host
->board
.rdy_pin
, "nand_rdy");
1523 "can't request rdy gpio %d\n",
1524 host
->board
.rdy_pin
);
1525 goto err_ecc_ioremap
;
1528 res
= gpio_direction_input(host
->board
.rdy_pin
);
1531 "can't request input direction rdy gpio %d\n",
1532 host
->board
.rdy_pin
);
1533 goto err_ecc_ioremap
;
1536 nand_chip
->dev_ready
= atmel_nand_device_ready
;
1539 if (gpio_is_valid(host
->board
.enable_pin
)) {
1540 res
= gpio_request(host
->board
.enable_pin
, "nand_enable");
1543 "can't request enable gpio %d\n",
1544 host
->board
.enable_pin
);
1545 goto err_ecc_ioremap
;
1548 res
= gpio_direction_output(host
->board
.enable_pin
, 1);
1551 "can't request output direction enable gpio %d\n",
1552 host
->board
.enable_pin
);
1553 goto err_ecc_ioremap
;
1557 nand_chip
->ecc
.mode
= host
->board
.ecc_mode
;
1558 nand_chip
->chip_delay
= 20; /* 20us command delay time */
1560 if (host
->board
.bus_width_16
) /* 16-bit bus width */
1561 nand_chip
->options
|= NAND_BUSWIDTH_16
;
1563 nand_chip
->read_buf
= atmel_read_buf
;
1564 nand_chip
->write_buf
= atmel_write_buf
;
1566 platform_set_drvdata(pdev
, host
);
1567 atmel_nand_enable(host
);
1569 if (gpio_is_valid(host
->board
.det_pin
)) {
1570 res
= gpio_request(host
->board
.det_pin
, "nand_det");
1573 "can't request det gpio %d\n",
1574 host
->board
.det_pin
);
1578 res
= gpio_direction_input(host
->board
.det_pin
);
1581 "can't request input direction det gpio %d\n",
1582 host
->board
.det_pin
);
1586 if (gpio_get_value(host
->board
.det_pin
)) {
1587 printk(KERN_INFO
"No SmartMedia card inserted.\n");
1593 if (host
->board
.on_flash_bbt
|| on_flash_bbt
) {
1594 printk(KERN_INFO
"atmel_nand: Use On Flash BBT\n");
1595 nand_chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
1598 if (!host
->board
.has_dma
)
1602 dma_cap_mask_t mask
;
1605 dma_cap_set(DMA_MEMCPY
, mask
);
1606 host
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
1607 if (!host
->dma_chan
) {
1608 dev_err(host
->dev
, "Failed to request DMA channel\n");
1613 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
1614 dma_chan_name(host
->dma_chan
));
1616 dev_info(host
->dev
, "No DMA support for NAND access.\n");
1618 /* first scan to find the device and get the page size */
1619 if (nand_scan_ident(mtd
, 1, NULL
)) {
1621 goto err_scan_ident
;
1624 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
1625 if (host
->has_pmecc
)
1626 res
= atmel_pmecc_nand_init_params(pdev
, host
);
1628 res
= atmel_hw_nand_init_params(pdev
, host
);
1634 /* second phase scan */
1635 if (nand_scan_tail(mtd
)) {
1640 mtd
->name
= "atmel_nand";
1641 ppdata
.of_node
= pdev
->dev
.of_node
;
1642 res
= mtd_device_parse_register(mtd
, NULL
, &ppdata
,
1643 host
->board
.parts
, host
->board
.num_parts
);
1648 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) {
1649 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
1650 pmecc_data_free(host
);
1654 if (host
->pmerrloc_base
)
1655 iounmap(host
->pmerrloc_base
);
1656 if (host
->pmecc_rom_base
)
1657 iounmap(host
->pmecc_rom_base
);
1661 atmel_nand_disable(host
);
1663 dma_release_channel(host
->dma_chan
);
1665 iounmap(host
->io_base
);
1672 * Remove a NAND device.
1674 static int __exit
atmel_nand_remove(struct platform_device
*pdev
)
1676 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
1677 struct mtd_info
*mtd
= &host
->mtd
;
1681 atmel_nand_disable(host
);
1683 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) {
1684 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
1685 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
,
1687 pmecc_data_free(host
);
1690 if (gpio_is_valid(host
->board
.det_pin
))
1691 gpio_free(host
->board
.det_pin
);
1693 if (gpio_is_valid(host
->board
.enable_pin
))
1694 gpio_free(host
->board
.enable_pin
);
1696 if (gpio_is_valid(host
->board
.rdy_pin
))
1697 gpio_free(host
->board
.rdy_pin
);
1701 if (host
->pmecc_rom_base
)
1702 iounmap(host
->pmecc_rom_base
);
1703 if (host
->pmerrloc_base
)
1704 iounmap(host
->pmerrloc_base
);
1707 dma_release_channel(host
->dma_chan
);
1709 iounmap(host
->io_base
);
1715 #if defined(CONFIG_OF)
1716 static const struct of_device_id atmel_nand_dt_ids
[] = {
1717 { .compatible
= "atmel,at91rm9200-nand" },
1721 MODULE_DEVICE_TABLE(of
, atmel_nand_dt_ids
);
1724 static struct platform_driver atmel_nand_driver
= {
1725 .remove
= __exit_p(atmel_nand_remove
),
1727 .name
= "atmel_nand",
1728 .owner
= THIS_MODULE
,
1729 .of_match_table
= of_match_ptr(atmel_nand_dt_ids
),
1733 module_platform_driver_probe(atmel_nand_driver
, atmel_nand_probe
);
1735 MODULE_LICENSE("GPL");
1736 MODULE_AUTHOR("Rick Bronson");
1737 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
1738 MODULE_ALIAS("platform:atmel_nand");