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/dma-mapping.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/platform_device.h>
36 #include <linux/of_device.h>
37 #include <linux/of_gpio.h>
38 #include <linux/of_mtd.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 /* oob layout for large page size
66 * bad block info is on bytes 0 and 1
67 * the bytes have to be consecutives to avoid
68 * several NAND_CMD_RNDOUT during read
70 static struct nand_ecclayout atmel_oobinfo_large
= {
72 .eccpos
= {60, 61, 62, 63},
78 /* oob layout for small page size
79 * bad block info is on bytes 4 and 5
80 * the bytes have to be consecutives to avoid
81 * several NAND_CMD_RNDOUT during read
83 static struct nand_ecclayout atmel_oobinfo_small
= {
85 .eccpos
= {0, 1, 2, 3},
92 void __iomem
*base_cmd_regs
;
93 void __iomem
*hsmc_regs
;
94 void __iomem
*sram_bank0
;
95 dma_addr_t sram_bank0_phys
;
100 struct completion comp_nfc
;
102 /* Point to the sram bank which include readed data via NFC */
103 void __iomem
*data_in_sram
;
104 bool will_write_sram
;
106 static struct atmel_nfc nand_nfc
;
108 struct atmel_nand_host
{
109 struct nand_chip nand_chip
;
111 void __iomem
*io_base
;
113 struct atmel_nand_data board
;
117 struct completion comp
;
118 struct dma_chan
*dma_chan
;
120 struct atmel_nfc
*nfc
;
124 u16 pmecc_sector_size
;
125 u32 pmecc_lookup_table_offset
;
126 u32 pmecc_lookup_table_offset_512
;
127 u32 pmecc_lookup_table_offset_1024
;
129 int pmecc_bytes_per_sector
;
130 int pmecc_sector_number
;
131 int pmecc_degree
; /* Degree of remainders */
132 int pmecc_cw_len
; /* Length of codeword */
134 void __iomem
*pmerrloc_base
;
135 void __iomem
*pmecc_rom_base
;
137 /* lookup table for alpha_to and index_of */
138 void __iomem
*pmecc_alpha_to
;
139 void __iomem
*pmecc_index_of
;
141 /* data for pmecc computation */
142 int16_t *pmecc_partial_syn
;
144 int16_t *pmecc_smu
; /* Sigma table */
145 int16_t *pmecc_lmu
; /* polynomal order */
151 static struct nand_ecclayout atmel_pmecc_oobinfo
;
156 static void atmel_nand_enable(struct atmel_nand_host
*host
)
158 if (gpio_is_valid(host
->board
.enable_pin
))
159 gpio_set_value(host
->board
.enable_pin
, 0);
165 static void atmel_nand_disable(struct atmel_nand_host
*host
)
167 if (gpio_is_valid(host
->board
.enable_pin
))
168 gpio_set_value(host
->board
.enable_pin
, 1);
172 * Hardware specific access to control-lines
174 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
176 struct nand_chip
*nand_chip
= mtd
->priv
;
177 struct atmel_nand_host
*host
= nand_chip
->priv
;
179 if (ctrl
& NAND_CTRL_CHANGE
) {
181 atmel_nand_enable(host
);
183 atmel_nand_disable(host
);
185 if (cmd
== NAND_CMD_NONE
)
189 writeb(cmd
, host
->io_base
+ (1 << host
->board
.cle
));
191 writeb(cmd
, host
->io_base
+ (1 << host
->board
.ale
));
195 * Read the Device Ready pin.
197 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
199 struct nand_chip
*nand_chip
= mtd
->priv
;
200 struct atmel_nand_host
*host
= nand_chip
->priv
;
202 return gpio_get_value(host
->board
.rdy_pin
) ^
203 !!host
->board
.rdy_pin_active_low
;
206 /* Set up for hardware ready pin and enable pin. */
207 static int atmel_nand_set_enable_ready_pins(struct mtd_info
*mtd
)
209 struct nand_chip
*chip
= mtd
->priv
;
210 struct atmel_nand_host
*host
= chip
->priv
;
213 if (gpio_is_valid(host
->board
.rdy_pin
)) {
214 res
= devm_gpio_request(host
->dev
,
215 host
->board
.rdy_pin
, "nand_rdy");
218 "can't request rdy gpio %d\n",
219 host
->board
.rdy_pin
);
223 res
= gpio_direction_input(host
->board
.rdy_pin
);
226 "can't request input direction rdy gpio %d\n",
227 host
->board
.rdy_pin
);
231 chip
->dev_ready
= atmel_nand_device_ready
;
234 if (gpio_is_valid(host
->board
.enable_pin
)) {
235 res
= devm_gpio_request(host
->dev
,
236 host
->board
.enable_pin
, "nand_enable");
239 "can't request enable gpio %d\n",
240 host
->board
.enable_pin
);
244 res
= gpio_direction_output(host
->board
.enable_pin
, 1);
247 "can't request output direction enable gpio %d\n",
248 host
->board
.enable_pin
);
256 static void memcpy32_fromio(void *trg
, const void __iomem
*src
, size_t size
)
260 const __iomem u32
*s
= src
;
262 for (i
= 0; i
< (size
>> 2); i
++)
263 *t
++ = readl_relaxed(s
++);
266 static void memcpy32_toio(void __iomem
*trg
, const void *src
, int size
)
269 u32 __iomem
*t
= trg
;
272 for (i
= 0; i
< (size
>> 2); i
++)
273 writel_relaxed(*s
++, t
++);
277 * Minimal-overhead PIO for data access.
279 static void atmel_read_buf8(struct mtd_info
*mtd
, u8
*buf
, int len
)
281 struct nand_chip
*nand_chip
= mtd
->priv
;
282 struct atmel_nand_host
*host
= nand_chip
->priv
;
284 if (host
->nfc
&& host
->nfc
->use_nfc_sram
&& host
->nfc
->data_in_sram
) {
285 memcpy32_fromio(buf
, host
->nfc
->data_in_sram
, len
);
286 host
->nfc
->data_in_sram
+= len
;
288 __raw_readsb(nand_chip
->IO_ADDR_R
, buf
, len
);
292 static void atmel_read_buf16(struct mtd_info
*mtd
, u8
*buf
, int len
)
294 struct nand_chip
*nand_chip
= mtd
->priv
;
295 struct atmel_nand_host
*host
= nand_chip
->priv
;
297 if (host
->nfc
&& host
->nfc
->use_nfc_sram
&& host
->nfc
->data_in_sram
) {
298 memcpy32_fromio(buf
, host
->nfc
->data_in_sram
, len
);
299 host
->nfc
->data_in_sram
+= len
;
301 __raw_readsw(nand_chip
->IO_ADDR_R
, buf
, len
/ 2);
305 static void atmel_write_buf8(struct mtd_info
*mtd
, const u8
*buf
, int len
)
307 struct nand_chip
*nand_chip
= mtd
->priv
;
309 __raw_writesb(nand_chip
->IO_ADDR_W
, buf
, len
);
312 static void atmel_write_buf16(struct mtd_info
*mtd
, const u8
*buf
, int len
)
314 struct nand_chip
*nand_chip
= mtd
->priv
;
316 __raw_writesw(nand_chip
->IO_ADDR_W
, buf
, len
/ 2);
319 static void dma_complete_func(void *completion
)
321 complete(completion
);
324 static int nfc_set_sram_bank(struct atmel_nand_host
*host
, unsigned int bank
)
326 /* NFC only has two banks. Must be 0 or 1 */
331 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
332 if (host
->mtd
.writesize
> 2048)
334 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK1
);
336 nfc_writel(host
->nfc
->hsmc_regs
, BANK
, ATMEL_HSMC_NFC_BANK0
);
342 static uint
nfc_get_sram_off(struct atmel_nand_host
*host
)
344 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
345 return NFC_SRAM_BANK1_OFFSET
;
350 static dma_addr_t
nfc_sram_phys(struct atmel_nand_host
*host
)
352 if (nfc_readl(host
->nfc
->hsmc_regs
, BANK
) & ATMEL_HSMC_NFC_BANK1
)
353 return host
->nfc
->sram_bank0_phys
+ NFC_SRAM_BANK1_OFFSET
;
355 return host
->nfc
->sram_bank0_phys
;
358 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
361 struct dma_device
*dma_dev
;
362 enum dma_ctrl_flags flags
;
363 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
364 struct dma_async_tx_descriptor
*tx
= NULL
;
366 struct nand_chip
*chip
= mtd
->priv
;
367 struct atmel_nand_host
*host
= chip
->priv
;
370 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
371 struct atmel_nfc
*nfc
= host
->nfc
;
373 if (buf
>= high_memory
)
376 dma_dev
= host
->dma_chan
->device
;
378 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
380 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
381 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
382 dev_err(host
->dev
, "Failed to dma_map_single\n");
387 if (nfc
&& nfc
->data_in_sram
)
388 dma_src_addr
= nfc_sram_phys(host
) + (nfc
->data_in_sram
389 - (nfc
->sram_bank0
+ nfc_get_sram_off(host
)));
391 dma_src_addr
= host
->io_phys
;
393 dma_dst_addr
= phys_addr
;
395 dma_src_addr
= phys_addr
;
397 if (nfc
&& nfc
->write_by_sram
)
398 dma_dst_addr
= nfc_sram_phys(host
);
400 dma_dst_addr
= host
->io_phys
;
403 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
404 dma_src_addr
, len
, flags
);
406 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
410 init_completion(&host
->comp
);
411 tx
->callback
= dma_complete_func
;
412 tx
->callback_param
= &host
->comp
;
414 cookie
= tx
->tx_submit(tx
);
415 if (dma_submit_error(cookie
)) {
416 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
420 dma_async_issue_pending(host
->dma_chan
);
421 wait_for_completion(&host
->comp
);
423 if (is_read
&& nfc
&& nfc
->data_in_sram
)
424 /* After read data from SRAM, need to increase the position */
425 nfc
->data_in_sram
+= len
;
430 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
433 dev_dbg(host
->dev
, "Fall back to CPU I/O\n");
437 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
439 struct nand_chip
*chip
= mtd
->priv
;
440 struct atmel_nand_host
*host
= chip
->priv
;
442 if (use_dma
&& len
> mtd
->oobsize
)
443 /* only use DMA for bigger than oob size: better performances */
444 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
447 if (host
->board
.bus_width_16
)
448 atmel_read_buf16(mtd
, buf
, len
);
450 atmel_read_buf8(mtd
, buf
, len
);
453 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
455 struct nand_chip
*chip
= mtd
->priv
;
456 struct atmel_nand_host
*host
= chip
->priv
;
458 if (use_dma
&& len
> mtd
->oobsize
)
459 /* only use DMA for bigger than oob size: better performances */
460 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
463 if (host
->board
.bus_width_16
)
464 atmel_write_buf16(mtd
, buf
, len
);
466 atmel_write_buf8(mtd
, buf
, len
);
470 * Return number of ecc bytes per sector according to sector size and
471 * correction capability
473 * Following table shows what at91 PMECC supported:
474 * Correction Capability Sector_512_bytes Sector_1024_bytes
475 * ===================== ================ =================
476 * 2-bits 4-bytes 4-bytes
477 * 4-bits 7-bytes 7-bytes
478 * 8-bits 13-bytes 14-bytes
479 * 12-bits 20-bytes 21-bytes
480 * 24-bits 39-bytes 42-bytes
482 static int pmecc_get_ecc_bytes(int cap
, int sector_size
)
484 int m
= 12 + sector_size
/ 512;
485 return (m
* cap
+ 7) / 8;
488 static void pmecc_config_ecc_layout(struct nand_ecclayout
*layout
,
489 int oobsize
, int ecc_len
)
493 layout
->eccbytes
= ecc_len
;
495 /* ECC will occupy the last ecc_len bytes continuously */
496 for (i
= 0; i
< ecc_len
; i
++)
497 layout
->eccpos
[i
] = oobsize
- ecc_len
+ i
;
499 layout
->oobfree
[0].offset
= 2;
500 layout
->oobfree
[0].length
=
501 oobsize
- ecc_len
- layout
->oobfree
[0].offset
;
504 static void __iomem
*pmecc_get_alpha_to(struct atmel_nand_host
*host
)
508 table_size
= host
->pmecc_sector_size
== 512 ?
509 PMECC_LOOKUP_TABLE_SIZE_512
: PMECC_LOOKUP_TABLE_SIZE_1024
;
511 return host
->pmecc_rom_base
+ host
->pmecc_lookup_table_offset
+
512 table_size
* sizeof(int16_t);
515 static int pmecc_data_alloc(struct atmel_nand_host
*host
)
517 const int cap
= host
->pmecc_corr_cap
;
520 size
= (2 * cap
+ 1) * sizeof(int16_t);
521 host
->pmecc_partial_syn
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
522 host
->pmecc_si
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
523 host
->pmecc_lmu
= devm_kzalloc(host
->dev
,
524 (cap
+ 1) * sizeof(int16_t), GFP_KERNEL
);
525 host
->pmecc_smu
= devm_kzalloc(host
->dev
,
526 (cap
+ 2) * size
, GFP_KERNEL
);
528 size
= (cap
+ 1) * sizeof(int);
529 host
->pmecc_mu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
530 host
->pmecc_dmu
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
531 host
->pmecc_delta
= devm_kzalloc(host
->dev
, size
, GFP_KERNEL
);
533 if (!host
->pmecc_partial_syn
||
545 static void pmecc_gen_syndrome(struct mtd_info
*mtd
, int sector
)
547 struct nand_chip
*nand_chip
= mtd
->priv
;
548 struct atmel_nand_host
*host
= nand_chip
->priv
;
552 /* Fill odd syndromes */
553 for (i
= 0; i
< host
->pmecc_corr_cap
; i
++) {
554 value
= pmecc_readl_rem_relaxed(host
->ecc
, sector
, i
/ 2);
558 host
->pmecc_partial_syn
[(2 * i
) + 1] = (int16_t)value
;
562 static void pmecc_substitute(struct mtd_info
*mtd
)
564 struct nand_chip
*nand_chip
= mtd
->priv
;
565 struct atmel_nand_host
*host
= nand_chip
->priv
;
566 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
567 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
568 int16_t *partial_syn
= host
->pmecc_partial_syn
;
569 const int cap
= host
->pmecc_corr_cap
;
573 /* si[] is a table that holds the current syndrome value,
574 * an element of that table belongs to the field
578 memset(&si
[1], 0, sizeof(int16_t) * (2 * cap
- 1));
580 /* Computation 2t syndromes based on S(x) */
582 for (i
= 1; i
< 2 * cap
; i
+= 2) {
583 for (j
= 0; j
< host
->pmecc_degree
; j
++) {
584 if (partial_syn
[i
] & ((unsigned short)0x1 << j
))
585 si
[i
] = readw_relaxed(alpha_to
+ i
* j
) ^ si
[i
];
588 /* Even syndrome = (Odd syndrome) ** 2 */
589 for (i
= 2, j
= 1; j
<= cap
; i
= ++j
<< 1) {
595 tmp
= readw_relaxed(index_of
+ si
[j
]);
596 tmp
= (tmp
* 2) % host
->pmecc_cw_len
;
597 si
[i
] = readw_relaxed(alpha_to
+ tmp
);
604 static void pmecc_get_sigma(struct mtd_info
*mtd
)
606 struct nand_chip
*nand_chip
= mtd
->priv
;
607 struct atmel_nand_host
*host
= nand_chip
->priv
;
609 int16_t *lmu
= host
->pmecc_lmu
;
610 int16_t *si
= host
->pmecc_si
;
611 int *mu
= host
->pmecc_mu
;
612 int *dmu
= host
->pmecc_dmu
; /* Discrepancy */
613 int *delta
= host
->pmecc_delta
; /* Delta order */
614 int cw_len
= host
->pmecc_cw_len
;
615 const int16_t cap
= host
->pmecc_corr_cap
;
616 const int num
= 2 * cap
+ 1;
617 int16_t __iomem
*index_of
= host
->pmecc_index_of
;
618 int16_t __iomem
*alpha_to
= host
->pmecc_alpha_to
;
620 uint32_t dmu_0_count
, tmp
;
621 int16_t *smu
= host
->pmecc_smu
;
623 /* index of largest delta */
635 memset(smu
, 0, sizeof(int16_t) * num
);
638 /* discrepancy set to 1 */
640 /* polynom order set to 0 */
642 delta
[0] = (mu
[0] * 2 - lmu
[0]) >> 1;
648 /* Sigma(x) set to 1 */
649 memset(&smu
[num
], 0, sizeof(int16_t) * num
);
652 /* discrepancy set to S1 */
655 /* polynom order set to 0 */
658 delta
[1] = (mu
[1] * 2 - lmu
[1]) >> 1;
660 /* Init the Sigma(x) last row */
661 memset(&smu
[(cap
+ 1) * num
], 0, sizeof(int16_t) * num
);
663 for (i
= 1; i
<= cap
; i
++) {
665 /* Begin Computing Sigma (Mu+1) and L(mu) */
666 /* check if discrepancy is set to 0 */
670 tmp
= ((cap
- (lmu
[i
] >> 1) - 1) / 2);
671 if ((cap
- (lmu
[i
] >> 1) - 1) & 0x1)
676 if (dmu_0_count
== tmp
) {
677 for (j
= 0; j
<= (lmu
[i
] >> 1) + 1; j
++)
678 smu
[(cap
+ 1) * num
+ j
] =
681 lmu
[cap
+ 1] = lmu
[i
];
686 for (j
= 0; j
<= lmu
[i
] >> 1; j
++)
687 smu
[(i
+ 1) * num
+ j
] = smu
[i
* num
+ j
];
689 /* copy previous polynom order to the next */
694 /* find largest delta with dmu != 0 */
695 for (j
= 0; j
< i
; j
++) {
696 if ((dmu
[j
]) && (delta
[j
] > largest
)) {
702 /* compute difference */
703 diff
= (mu
[i
] - mu
[ro
]);
705 /* Compute degree of the new smu polynomial */
706 if ((lmu
[i
] >> 1) > ((lmu
[ro
] >> 1) + diff
))
709 lmu
[i
+ 1] = ((lmu
[ro
] >> 1) + diff
) * 2;
711 /* Init smu[i+1] with 0 */
712 for (k
= 0; k
< num
; k
++)
713 smu
[(i
+ 1) * num
+ k
] = 0;
715 /* Compute smu[i+1] */
716 for (k
= 0; k
<= lmu
[ro
] >> 1; k
++) {
719 if (!(smu
[ro
* num
+ k
] && dmu
[i
]))
721 a
= readw_relaxed(index_of
+ dmu
[i
]);
722 b
= readw_relaxed(index_of
+ dmu
[ro
]);
723 c
= readw_relaxed(index_of
+ smu
[ro
* num
+ k
]);
724 tmp
= a
+ (cw_len
- b
) + c
;
725 a
= readw_relaxed(alpha_to
+ tmp
% cw_len
);
726 smu
[(i
+ 1) * num
+ (k
+ diff
)] = a
;
729 for (k
= 0; k
<= lmu
[i
] >> 1; k
++)
730 smu
[(i
+ 1) * num
+ k
] ^= smu
[i
* num
+ k
];
733 /* End Computing Sigma (Mu+1) and L(mu) */
734 /* In either case compute delta */
735 delta
[i
+ 1] = (mu
[i
+ 1] * 2 - lmu
[i
+ 1]) >> 1;
737 /* Do not compute discrepancy for the last iteration */
741 for (k
= 0; k
<= (lmu
[i
+ 1] >> 1); k
++) {
744 dmu
[i
+ 1] = si
[tmp
+ 3];
745 } else if (smu
[(i
+ 1) * num
+ k
] && si
[tmp
+ 3 - k
]) {
747 a
= readw_relaxed(index_of
+
748 smu
[(i
+ 1) * num
+ k
]);
749 b
= si
[2 * (i
- 1) + 3 - k
];
750 c
= readw_relaxed(index_of
+ b
);
753 dmu
[i
+ 1] = readw_relaxed(alpha_to
+ tmp
) ^
762 static int pmecc_err_location(struct mtd_info
*mtd
)
764 struct nand_chip
*nand_chip
= mtd
->priv
;
765 struct atmel_nand_host
*host
= nand_chip
->priv
;
766 unsigned long end_time
;
767 const int cap
= host
->pmecc_corr_cap
;
768 const int num
= 2 * cap
+ 1;
769 int sector_size
= host
->pmecc_sector_size
;
770 int err_nbr
= 0; /* number of error */
771 int roots_nbr
; /* number of roots */
774 int16_t *smu
= host
->pmecc_smu
;
776 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
, PMERRLOC_DISABLE
);
778 for (i
= 0; i
<= host
->pmecc_lmu
[cap
+ 1] >> 1; i
++) {
779 pmerrloc_writel_sigma_relaxed(host
->pmerrloc_base
, i
,
780 smu
[(cap
+ 1) * num
+ i
]);
784 val
= (err_nbr
- 1) << 16;
785 if (sector_size
== 1024)
788 pmerrloc_writel(host
->pmerrloc_base
, ELCFG
, val
);
789 pmerrloc_writel(host
->pmerrloc_base
, ELEN
,
790 sector_size
* 8 + host
->pmecc_degree
* cap
);
792 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
793 while (!(pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
794 & PMERRLOC_CALC_DONE
)) {
795 if (unlikely(time_after(jiffies
, end_time
))) {
796 dev_err(host
->dev
, "PMECC: Timeout to calculate error location.\n");
802 roots_nbr
= (pmerrloc_readl_relaxed(host
->pmerrloc_base
, ELISR
)
803 & PMERRLOC_ERR_NUM_MASK
) >> 8;
804 /* Number of roots == degree of smu hence <= cap */
805 if (roots_nbr
== host
->pmecc_lmu
[cap
+ 1] >> 1)
808 /* Number of roots does not match the degree of smu
809 * unable to correct error */
813 static void pmecc_correct_data(struct mtd_info
*mtd
, uint8_t *buf
, uint8_t *ecc
,
814 int sector_num
, int extra_bytes
, int err_nbr
)
816 struct nand_chip
*nand_chip
= mtd
->priv
;
817 struct atmel_nand_host
*host
= nand_chip
->priv
;
819 int byte_pos
, bit_pos
, sector_size
, pos
;
823 sector_size
= host
->pmecc_sector_size
;
826 tmp
= pmerrloc_readl_el_relaxed(host
->pmerrloc_base
, i
) - 1;
830 if (byte_pos
>= (sector_size
+ extra_bytes
))
831 BUG(); /* should never happen */
833 if (byte_pos
< sector_size
) {
834 err_byte
= *(buf
+ byte_pos
);
835 *(buf
+ byte_pos
) ^= (1 << bit_pos
);
837 pos
= sector_num
* host
->pmecc_sector_size
+ byte_pos
;
838 dev_info(host
->dev
, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
839 pos
, bit_pos
, err_byte
, *(buf
+ byte_pos
));
841 /* Bit flip in OOB area */
842 tmp
= sector_num
* host
->pmecc_bytes_per_sector
843 + (byte_pos
- sector_size
);
845 ecc
[tmp
] ^= (1 << bit_pos
);
847 pos
= tmp
+ nand_chip
->ecc
.layout
->eccpos
[0];
848 dev_info(host
->dev
, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
849 pos
, bit_pos
, err_byte
, ecc
[tmp
]);
859 static int pmecc_correction(struct mtd_info
*mtd
, u32 pmecc_stat
, uint8_t *buf
,
862 struct nand_chip
*nand_chip
= mtd
->priv
;
863 struct atmel_nand_host
*host
= nand_chip
->priv
;
868 for (i
= 0; i
< nand_chip
->ecc
.total
; i
++)
871 /* Erased page, return OK */
875 for (i
= 0; i
< host
->pmecc_sector_number
; i
++) {
877 if (pmecc_stat
& 0x1) {
878 buf_pos
= buf
+ i
* host
->pmecc_sector_size
;
880 pmecc_gen_syndrome(mtd
, i
);
881 pmecc_substitute(mtd
);
882 pmecc_get_sigma(mtd
);
884 err_nbr
= pmecc_err_location(mtd
);
886 dev_err(host
->dev
, "PMECC: Too many errors\n");
887 mtd
->ecc_stats
.failed
++;
890 pmecc_correct_data(mtd
, buf_pos
, ecc
, i
,
891 host
->pmecc_bytes_per_sector
, err_nbr
);
892 mtd
->ecc_stats
.corrected
+= err_nbr
;
893 total_err
+= err_nbr
;
902 static void pmecc_enable(struct atmel_nand_host
*host
, int ecc_op
)
906 if (ecc_op
!= NAND_ECC_READ
&& ecc_op
!= NAND_ECC_WRITE
) {
907 dev_err(host
->dev
, "atmel_nand: wrong pmecc operation type!");
911 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
912 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
913 val
= pmecc_readl_relaxed(host
->ecc
, CFG
);
915 if (ecc_op
== NAND_ECC_READ
)
916 pmecc_writel(host
->ecc
, CFG
, (val
& ~PMECC_CFG_WRITE_OP
)
917 | PMECC_CFG_AUTO_ENABLE
);
919 pmecc_writel(host
->ecc
, CFG
, (val
| PMECC_CFG_WRITE_OP
)
920 & ~PMECC_CFG_AUTO_ENABLE
);
922 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
923 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DATA
);
926 static int atmel_nand_pmecc_read_page(struct mtd_info
*mtd
,
927 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
929 struct atmel_nand_host
*host
= chip
->priv
;
930 int eccsize
= chip
->ecc
.size
* chip
->ecc
.steps
;
931 uint8_t *oob
= chip
->oob_poi
;
932 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
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 bitflips
= pmecc_correction(mtd
, stat
, buf
, &oob
[eccpos
[0]]);
956 /* uncorrectable errors */
963 static int atmel_nand_pmecc_write_page(struct mtd_info
*mtd
,
964 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
)
966 struct atmel_nand_host
*host
= chip
->priv
;
967 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
969 unsigned long end_time
;
971 if (!host
->nfc
|| !host
->nfc
->write_by_sram
) {
972 pmecc_enable(host
, NAND_ECC_WRITE
);
973 chip
->write_buf(mtd
, (u8
*)buf
, mtd
->writesize
);
976 end_time
= jiffies
+ msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS
);
977 while ((pmecc_readl_relaxed(host
->ecc
, SR
) & PMECC_SR_BUSY
)) {
978 if (unlikely(time_after(jiffies
, end_time
))) {
979 dev_err(host
->dev
, "PMECC: Timeout to get ECC value.\n");
985 for (i
= 0; i
< host
->pmecc_sector_number
; i
++) {
986 for (j
= 0; j
< host
->pmecc_bytes_per_sector
; j
++) {
989 pos
= i
* host
->pmecc_bytes_per_sector
+ j
;
990 chip
->oob_poi
[eccpos
[pos
]] =
991 pmecc_readb_ecc_relaxed(host
->ecc
, i
, j
);
994 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
999 static void atmel_pmecc_core_init(struct mtd_info
*mtd
)
1001 struct nand_chip
*nand_chip
= mtd
->priv
;
1002 struct atmel_nand_host
*host
= nand_chip
->priv
;
1004 struct nand_ecclayout
*ecc_layout
;
1006 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_RST
);
1007 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
1009 switch (host
->pmecc_corr_cap
) {
1011 val
= PMECC_CFG_BCH_ERR2
;
1014 val
= PMECC_CFG_BCH_ERR4
;
1017 val
= PMECC_CFG_BCH_ERR8
;
1020 val
= PMECC_CFG_BCH_ERR12
;
1023 val
= PMECC_CFG_BCH_ERR24
;
1027 if (host
->pmecc_sector_size
== 512)
1028 val
|= PMECC_CFG_SECTOR512
;
1029 else if (host
->pmecc_sector_size
== 1024)
1030 val
|= PMECC_CFG_SECTOR1024
;
1032 switch (host
->pmecc_sector_number
) {
1034 val
|= PMECC_CFG_PAGE_1SECTOR
;
1037 val
|= PMECC_CFG_PAGE_2SECTORS
;
1040 val
|= PMECC_CFG_PAGE_4SECTORS
;
1043 val
|= PMECC_CFG_PAGE_8SECTORS
;
1047 val
|= (PMECC_CFG_READ_OP
| PMECC_CFG_SPARE_DISABLE
1048 | PMECC_CFG_AUTO_DISABLE
);
1049 pmecc_writel(host
->ecc
, CFG
, val
);
1051 ecc_layout
= nand_chip
->ecc
.layout
;
1052 pmecc_writel(host
->ecc
, SAREA
, mtd
->oobsize
- 1);
1053 pmecc_writel(host
->ecc
, SADDR
, ecc_layout
->eccpos
[0]);
1054 pmecc_writel(host
->ecc
, EADDR
,
1055 ecc_layout
->eccpos
[ecc_layout
->eccbytes
- 1]);
1056 /* See datasheet about PMECC Clock Control Register */
1057 pmecc_writel(host
->ecc
, CLK
, 2);
1058 pmecc_writel(host
->ecc
, IDR
, 0xff);
1059 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_ENABLE
);
1063 * Get minimum ecc requirements from NAND.
1064 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1065 * will set them according to minimum ecc requirement. Otherwise, use the
1066 * value in DTS file.
1067 * return 0 if success. otherwise return error code.
1069 static int pmecc_choose_ecc(struct atmel_nand_host
*host
,
1070 int *cap
, int *sector_size
)
1072 /* Get minimum ECC requirements */
1073 if (host
->nand_chip
.ecc_strength_ds
) {
1074 *cap
= host
->nand_chip
.ecc_strength_ds
;
1075 *sector_size
= host
->nand_chip
.ecc_step_ds
;
1076 dev_info(host
->dev
, "minimum ECC: %d bits in %d bytes\n",
1077 *cap
, *sector_size
);
1081 dev_info(host
->dev
, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
1084 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
1085 if (host
->pmecc_corr_cap
== 0) {
1086 /* use the most fitable ecc bits (the near bigger one ) */
1088 host
->pmecc_corr_cap
= 2;
1090 host
->pmecc_corr_cap
= 4;
1092 host
->pmecc_corr_cap
= 8;
1093 else if (*cap
<= 12)
1094 host
->pmecc_corr_cap
= 12;
1095 else if (*cap
<= 24)
1096 host
->pmecc_corr_cap
= 24;
1100 if (host
->pmecc_sector_size
== 0) {
1101 /* use the most fitable sector size (the near smaller one ) */
1102 if (*sector_size
>= 1024)
1103 host
->pmecc_sector_size
= 1024;
1104 else if (*sector_size
>= 512)
1105 host
->pmecc_sector_size
= 512;
1112 static int atmel_pmecc_nand_init_params(struct platform_device
*pdev
,
1113 struct atmel_nand_host
*host
)
1115 struct mtd_info
*mtd
= &host
->mtd
;
1116 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1117 struct resource
*regs
, *regs_pmerr
, *regs_rom
;
1118 int cap
, sector_size
, err_no
;
1120 err_no
= pmecc_choose_ecc(host
, &cap
, §or_size
);
1122 dev_err(host
->dev
, "The NAND flash's ECC requirement are not support!");
1126 if (cap
> host
->pmecc_corr_cap
||
1127 sector_size
!= host
->pmecc_sector_size
)
1128 dev_info(host
->dev
, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
1130 cap
= host
->pmecc_corr_cap
;
1131 sector_size
= host
->pmecc_sector_size
;
1132 host
->pmecc_lookup_table_offset
= (sector_size
== 512) ?
1133 host
->pmecc_lookup_table_offset_512
:
1134 host
->pmecc_lookup_table_offset_1024
;
1136 dev_info(host
->dev
, "Initialize PMECC params, cap: %d, sector: %d\n",
1139 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1142 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1143 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1147 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1148 if (IS_ERR(host
->ecc
)) {
1149 dev_err(host
->dev
, "ioremap failed\n");
1150 err_no
= PTR_ERR(host
->ecc
);
1154 regs_pmerr
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1155 host
->pmerrloc_base
= devm_ioremap_resource(&pdev
->dev
, regs_pmerr
);
1156 if (IS_ERR(host
->pmerrloc_base
)) {
1158 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1159 err_no
= PTR_ERR(host
->pmerrloc_base
);
1163 regs_rom
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
1164 host
->pmecc_rom_base
= devm_ioremap_resource(&pdev
->dev
, regs_rom
);
1165 if (IS_ERR(host
->pmecc_rom_base
)) {
1166 dev_err(host
->dev
, "Can not get I/O resource for ROM!\n");
1167 err_no
= PTR_ERR(host
->pmecc_rom_base
);
1171 nand_chip
->ecc
.size
= sector_size
;
1173 /* set ECC page size and oob layout */
1174 switch (mtd
->writesize
) {
1176 host
->pmecc_degree
= (sector_size
== 512) ?
1177 PMECC_GF_DIMENSION_13
: PMECC_GF_DIMENSION_14
;
1178 host
->pmecc_cw_len
= (1 << host
->pmecc_degree
) - 1;
1179 host
->pmecc_sector_number
= mtd
->writesize
/ sector_size
;
1180 host
->pmecc_bytes_per_sector
= pmecc_get_ecc_bytes(
1182 host
->pmecc_alpha_to
= pmecc_get_alpha_to(host
);
1183 host
->pmecc_index_of
= host
->pmecc_rom_base
+
1184 host
->pmecc_lookup_table_offset
;
1186 nand_chip
->ecc
.steps
= host
->pmecc_sector_number
;
1187 nand_chip
->ecc
.strength
= cap
;
1188 nand_chip
->ecc
.bytes
= host
->pmecc_bytes_per_sector
;
1189 nand_chip
->ecc
.total
= host
->pmecc_bytes_per_sector
*
1190 host
->pmecc_sector_number
;
1191 if (nand_chip
->ecc
.total
> mtd
->oobsize
- 2) {
1192 dev_err(host
->dev
, "No room for ECC bytes\n");
1196 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo
,
1198 nand_chip
->ecc
.total
);
1200 nand_chip
->ecc
.layout
= &atmel_pmecc_oobinfo
;
1207 "Unsupported page size for PMECC, use Software ECC\n");
1209 /* page size not handled by HW ECC */
1210 /* switching back to soft ECC */
1211 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1215 /* Allocate data for PMECC computation */
1216 err_no
= pmecc_data_alloc(host
);
1219 "Cannot allocate memory for PMECC computation!\n");
1223 nand_chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
1224 nand_chip
->ecc
.read_page
= atmel_nand_pmecc_read_page
;
1225 nand_chip
->ecc
.write_page
= atmel_nand_pmecc_write_page
;
1227 atmel_pmecc_core_init(mtd
);
1238 * function called after a write
1240 * mtd: MTD block structure
1241 * dat: raw data (unused)
1242 * ecc_code: buffer for ECC
1244 static int atmel_nand_calculate(struct mtd_info
*mtd
,
1245 const u_char
*dat
, unsigned char *ecc_code
)
1247 struct nand_chip
*nand_chip
= mtd
->priv
;
1248 struct atmel_nand_host
*host
= nand_chip
->priv
;
1249 unsigned int ecc_value
;
1251 /* get the first 2 ECC bytes */
1252 ecc_value
= ecc_readl(host
->ecc
, PR
);
1254 ecc_code
[0] = ecc_value
& 0xFF;
1255 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
1257 /* get the last 2 ECC bytes */
1258 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
1260 ecc_code
[2] = ecc_value
& 0xFF;
1261 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
1267 * HW ECC read page function
1269 * mtd: mtd info structure
1270 * chip: nand chip info structure
1271 * buf: buffer to store read data
1272 * oob_required: caller expects OOB data read to chip->oob_poi
1274 static int atmel_nand_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1275 uint8_t *buf
, int oob_required
, int page
)
1277 int eccsize
= chip
->ecc
.size
;
1278 int eccbytes
= chip
->ecc
.bytes
;
1279 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
1281 uint8_t *oob
= chip
->oob_poi
;
1284 unsigned int max_bitflips
= 0;
1287 * Errata: ALE is incorrectly wired up to the ECC controller
1288 * on the AP7000, so it will include the address cycles in the
1291 * Workaround: Reset the parity registers before reading the
1294 struct atmel_nand_host
*host
= chip
->priv
;
1295 if (host
->board
.need_reset_workaround
)
1296 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1299 chip
->read_buf(mtd
, p
, eccsize
);
1301 /* move to ECC position if needed */
1302 if (eccpos
[0] != 0) {
1303 /* This only works on large pages
1304 * because the ECC controller waits for
1305 * NAND_CMD_RNDOUTSTART after the
1307 * anyway, for small pages, the eccpos[0] == 0
1309 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
1310 mtd
->writesize
+ eccpos
[0], -1);
1313 /* the ECC controller needs to read the ECC just after the data */
1314 ecc_pos
= oob
+ eccpos
[0];
1315 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
1317 /* check if there's an error */
1318 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
1321 mtd
->ecc_stats
.failed
++;
1323 mtd
->ecc_stats
.corrected
+= stat
;
1324 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1327 /* get back to oob start (end of page) */
1328 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
1331 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
1333 return max_bitflips
;
1339 * function called after a read
1341 * mtd: MTD block structure
1342 * dat: raw data read from the chip
1343 * read_ecc: ECC from the chip (unused)
1346 * Detect and correct a 1 bit error for a page
1348 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
1349 u_char
*read_ecc
, u_char
*isnull
)
1351 struct nand_chip
*nand_chip
= mtd
->priv
;
1352 struct atmel_nand_host
*host
= nand_chip
->priv
;
1353 unsigned int ecc_status
;
1354 unsigned int ecc_word
, ecc_bit
;
1356 /* get the status from the Status Register */
1357 ecc_status
= ecc_readl(host
->ecc
, SR
);
1359 /* if there's no error */
1360 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
1363 /* get error bit offset (4 bits) */
1364 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
1365 /* get word address (12 bits) */
1366 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
1369 /* if there are multiple errors */
1370 if (ecc_status
& ATMEL_ECC_MULERR
) {
1371 /* check if it is a freshly erased block
1372 * (filled with 0xff) */
1373 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
1374 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
1375 /* the block has just been erased, return OK */
1378 /* it doesn't seems to be a freshly
1380 * We can't correct so many errors */
1381 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
1382 " Unable to correct.\n");
1386 /* if there's a single bit error : we can correct it */
1387 if (ecc_status
& ATMEL_ECC_ECCERR
) {
1388 /* there's nothing much to do here.
1389 * the bit error is on the ECC itself.
1391 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
1392 " Nothing to correct\n");
1396 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
1397 " (word offset in the page :"
1398 " 0x%x bit offset : 0x%x)\n",
1400 /* correct the error */
1401 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
1403 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
1406 dat
[ecc_word
] ^= (1 << ecc_bit
);
1408 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
1413 * Enable HW ECC : unused on most chips
1415 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
1417 struct nand_chip
*nand_chip
= mtd
->priv
;
1418 struct atmel_nand_host
*host
= nand_chip
->priv
;
1420 if (host
->board
.need_reset_workaround
)
1421 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
1424 static int atmel_of_init_port(struct atmel_nand_host
*host
,
1425 struct device_node
*np
)
1430 struct atmel_nand_data
*board
= &host
->board
;
1431 enum of_gpio_flags flags
= 0;
1433 if (of_property_read_u32(np
, "atmel,nand-addr-offset", &val
) == 0) {
1435 dev_err(host
->dev
, "invalid addr-offset %u\n", val
);
1441 if (of_property_read_u32(np
, "atmel,nand-cmd-offset", &val
) == 0) {
1443 dev_err(host
->dev
, "invalid cmd-offset %u\n", val
);
1449 ecc_mode
= of_get_nand_ecc_mode(np
);
1451 board
->ecc_mode
= ecc_mode
< 0 ? NAND_ECC_SOFT
: ecc_mode
;
1453 board
->on_flash_bbt
= of_get_nand_on_flash_bbt(np
);
1455 board
->has_dma
= of_property_read_bool(np
, "atmel,nand-has-dma");
1457 if (of_get_nand_bus_width(np
) == 16)
1458 board
->bus_width_16
= 1;
1460 board
->rdy_pin
= of_get_gpio_flags(np
, 0, &flags
);
1461 board
->rdy_pin_active_low
= (flags
== OF_GPIO_ACTIVE_LOW
);
1463 board
->enable_pin
= of_get_gpio(np
, 1);
1464 board
->det_pin
= of_get_gpio(np
, 2);
1466 host
->has_pmecc
= of_property_read_bool(np
, "atmel,has-pmecc");
1468 /* load the nfc driver if there is */
1469 of_platform_populate(np
, NULL
, NULL
, host
->dev
);
1471 if (!(board
->ecc_mode
== NAND_ECC_HW
) || !host
->has_pmecc
)
1472 return 0; /* Not using PMECC */
1474 /* use PMECC, get correction capability, sector size and lookup
1476 * If correction bits and sector size are not specified, then find
1477 * them from NAND ONFI parameters.
1479 if (of_property_read_u32(np
, "atmel,pmecc-cap", &val
) == 0) {
1480 if ((val
!= 2) && (val
!= 4) && (val
!= 8) && (val
!= 12) &&
1483 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1487 host
->pmecc_corr_cap
= (u8
)val
;
1490 if (of_property_read_u32(np
, "atmel,pmecc-sector-size", &val
) == 0) {
1491 if ((val
!= 512) && (val
!= 1024)) {
1493 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1497 host
->pmecc_sector_size
= (u16
)val
;
1500 if (of_property_read_u32_array(np
, "atmel,pmecc-lookup-table-offset",
1502 dev_err(host
->dev
, "Cannot get PMECC lookup table offset\n");
1505 if (!offset
[0] && !offset
[1]) {
1506 dev_err(host
->dev
, "Invalid PMECC lookup table offset\n");
1509 host
->pmecc_lookup_table_offset_512
= offset
[0];
1510 host
->pmecc_lookup_table_offset_1024
= offset
[1];
1515 static int atmel_hw_nand_init_params(struct platform_device
*pdev
,
1516 struct atmel_nand_host
*host
)
1518 struct mtd_info
*mtd
= &host
->mtd
;
1519 struct nand_chip
*nand_chip
= &host
->nand_chip
;
1520 struct resource
*regs
;
1522 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1525 "Can't get I/O resource regs, use software ECC\n");
1526 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1530 host
->ecc
= devm_ioremap_resource(&pdev
->dev
, regs
);
1531 if (IS_ERR(host
->ecc
)) {
1532 dev_err(host
->dev
, "ioremap failed\n");
1533 return PTR_ERR(host
->ecc
);
1536 /* ECC is calculated for the whole page (1 step) */
1537 nand_chip
->ecc
.size
= mtd
->writesize
;
1539 /* set ECC page size and oob layout */
1540 switch (mtd
->writesize
) {
1542 nand_chip
->ecc
.layout
= &atmel_oobinfo_small
;
1543 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
1546 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1547 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
1550 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1551 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
1554 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
1555 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
1558 /* page size not handled by HW ECC */
1559 /* switching back to soft ECC */
1560 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
1564 /* set up for HW ECC */
1565 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
1566 nand_chip
->ecc
.correct
= atmel_nand_correct
;
1567 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
1568 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
1569 nand_chip
->ecc
.bytes
= 4;
1570 nand_chip
->ecc
.strength
= 1;
1575 static inline u32
nfc_read_status(struct atmel_nand_host
*host
)
1577 u32 err_flags
= NFC_SR_DTOE
| NFC_SR_UNDEF
| NFC_SR_AWB
| NFC_SR_ASE
;
1578 u32 nfc_status
= nfc_readl(host
->nfc
->hsmc_regs
, SR
);
1580 if (unlikely(nfc_status
& err_flags
)) {
1581 if (nfc_status
& NFC_SR_DTOE
)
1582 dev_err(host
->dev
, "NFC: Waiting Nand R/B Timeout Error\n");
1583 else if (nfc_status
& NFC_SR_UNDEF
)
1584 dev_err(host
->dev
, "NFC: Access Undefined Area Error\n");
1585 else if (nfc_status
& NFC_SR_AWB
)
1586 dev_err(host
->dev
, "NFC: Access memory While NFC is busy\n");
1587 else if (nfc_status
& NFC_SR_ASE
)
1588 dev_err(host
->dev
, "NFC: Access memory Size Error\n");
1594 /* SMC interrupt service routine */
1595 static irqreturn_t
hsmc_interrupt(int irq
, void *dev_id
)
1597 struct atmel_nand_host
*host
= dev_id
;
1598 u32 status
, mask
, pending
;
1599 irqreturn_t ret
= IRQ_HANDLED
;
1601 status
= nfc_read_status(host
);
1602 mask
= nfc_readl(host
->nfc
->hsmc_regs
, IMR
);
1603 pending
= status
& mask
;
1605 if (pending
& NFC_SR_XFR_DONE
) {
1606 complete(&host
->nfc
->comp_nfc
);
1607 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_XFR_DONE
);
1608 } else if (pending
& NFC_SR_RB_EDGE
) {
1609 complete(&host
->nfc
->comp_nfc
);
1610 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_RB_EDGE
);
1611 } else if (pending
& NFC_SR_CMD_DONE
) {
1612 complete(&host
->nfc
->comp_nfc
);
1613 nfc_writel(host
->nfc
->hsmc_regs
, IDR
, NFC_SR_CMD_DONE
);
1621 /* NFC(Nand Flash Controller) related functions */
1622 static int nfc_wait_interrupt(struct atmel_nand_host
*host
, u32 flag
)
1624 unsigned long timeout
;
1625 init_completion(&host
->nfc
->comp_nfc
);
1627 /* Enable interrupt that need to wait for */
1628 nfc_writel(host
->nfc
->hsmc_regs
, IER
, flag
);
1630 timeout
= wait_for_completion_timeout(&host
->nfc
->comp_nfc
,
1631 msecs_to_jiffies(NFC_TIME_OUT_MS
));
1635 /* Time out to wait for the interrupt */
1636 dev_err(host
->dev
, "Time out to wait for interrupt: 0x%08x\n", flag
);
1640 static int nfc_send_command(struct atmel_nand_host
*host
,
1641 unsigned int cmd
, unsigned int addr
, unsigned char cycle0
)
1643 unsigned long timeout
;
1645 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1648 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1649 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY
, host
->nfc
->base_cmd_regs
)
1650 & NFCADDR_CMD_NFCBUSY
) {
1651 if (time_after(jiffies
, timeout
)) {
1653 "Time out to wait CMD_NFCBUSY ready!\n");
1657 nfc_writel(host
->nfc
->hsmc_regs
, CYCLE0
, cycle0
);
1658 nfc_cmd_addr1234_writel(cmd
, addr
, host
->nfc
->base_cmd_regs
);
1659 return nfc_wait_interrupt(host
, NFC_SR_CMD_DONE
);
1662 static int nfc_device_ready(struct mtd_info
*mtd
)
1664 struct nand_chip
*nand_chip
= mtd
->priv
;
1665 struct atmel_nand_host
*host
= nand_chip
->priv
;
1666 if (!nfc_wait_interrupt(host
, NFC_SR_RB_EDGE
))
1671 static void nfc_select_chip(struct mtd_info
*mtd
, int chip
)
1673 struct nand_chip
*nand_chip
= mtd
->priv
;
1674 struct atmel_nand_host
*host
= nand_chip
->priv
;
1677 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_DISABLE
);
1679 nfc_writel(host
->nfc
->hsmc_regs
, CTRL
, NFC_CTRL_ENABLE
);
1682 static int nfc_make_addr(struct mtd_info
*mtd
, int command
, int column
,
1683 int page_addr
, unsigned int *addr1234
, unsigned int *cycle0
)
1685 struct nand_chip
*chip
= mtd
->priv
;
1688 unsigned char addr_bytes
[8];
1689 int index
= 0, bit_shift
;
1691 BUG_ON(addr1234
== NULL
|| cycle0
== NULL
);
1697 if (chip
->options
& NAND_BUSWIDTH_16
&&
1698 !nand_opcode_8bits(command
))
1700 addr_bytes
[acycle
++] = column
& 0xff;
1701 if (mtd
->writesize
> 512)
1702 addr_bytes
[acycle
++] = (column
>> 8) & 0xff;
1705 if (page_addr
!= -1) {
1706 addr_bytes
[acycle
++] = page_addr
& 0xff;
1707 addr_bytes
[acycle
++] = (page_addr
>> 8) & 0xff;
1708 if (chip
->chipsize
> (128 << 20))
1709 addr_bytes
[acycle
++] = (page_addr
>> 16) & 0xff;
1713 *cycle0
= addr_bytes
[index
++];
1715 for (bit_shift
= 0; index
< acycle
; bit_shift
+= 8)
1716 *addr1234
+= addr_bytes
[index
++] << bit_shift
;
1718 /* return acycle in cmd register */
1719 return acycle
<< NFCADDR_CMD_ACYCLE_BIT_POS
;
1722 static void nfc_nand_command(struct mtd_info
*mtd
, unsigned int command
,
1723 int column
, int page_addr
)
1725 struct nand_chip
*chip
= mtd
->priv
;
1726 struct atmel_nand_host
*host
= chip
->priv
;
1727 unsigned long timeout
;
1728 unsigned int nfc_addr_cmd
= 0;
1730 unsigned int cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1732 /* Set default settings: no cmd2, no addr cycle. read from nand */
1733 unsigned int cmd2
= 0;
1734 unsigned int vcmd2
= 0;
1735 int acycle
= NFCADDR_CMD_ACYCLE_NONE
;
1736 int csid
= NFCADDR_CMD_CSID_3
;
1737 int dataen
= NFCADDR_CMD_DATADIS
;
1738 int nfcwr
= NFCADDR_CMD_NFCRD
;
1739 unsigned int addr1234
= 0;
1740 unsigned int cycle0
= 0;
1741 bool do_addr
= true;
1742 host
->nfc
->data_in_sram
= NULL
;
1744 dev_dbg(host
->dev
, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1745 __func__
, command
, column
, page_addr
);
1748 case NAND_CMD_RESET
:
1749 nfc_addr_cmd
= cmd1
| acycle
| csid
| dataen
| nfcwr
;
1750 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1751 udelay(chip
->chip_delay
);
1753 nfc_nand_command(mtd
, NAND_CMD_STATUS
, -1, -1);
1754 timeout
= jiffies
+ msecs_to_jiffies(NFC_TIME_OUT_MS
);
1755 while (!(chip
->read_byte(mtd
) & NAND_STATUS_READY
)) {
1756 if (time_after(jiffies
, timeout
)) {
1758 "Time out to wait status ready!\n");
1763 case NAND_CMD_STATUS
:
1766 case NAND_CMD_PARAM
:
1767 case NAND_CMD_READID
:
1769 acycle
= NFCADDR_CMD_ACYCLE_1
;
1773 case NAND_CMD_RNDOUT
:
1774 cmd2
= NAND_CMD_RNDOUTSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1775 vcmd2
= NFCADDR_CMD_VCMD2
;
1777 case NAND_CMD_READ0
:
1778 case NAND_CMD_READOOB
:
1779 if (command
== NAND_CMD_READOOB
) {
1780 column
+= mtd
->writesize
;
1781 command
= NAND_CMD_READ0
; /* only READ0 is valid */
1782 cmd1
= command
<< NFCADDR_CMD_CMD1_BIT_POS
;
1784 if (host
->nfc
->use_nfc_sram
) {
1785 /* Enable Data transfer to sram */
1786 dataen
= NFCADDR_CMD_DATAEN
;
1788 /* Need enable PMECC now, since NFC will transfer
1789 * data in bus after sending nfc read command.
1791 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
1792 pmecc_enable(host
, NAND_ECC_READ
);
1795 cmd2
= NAND_CMD_READSTART
<< NFCADDR_CMD_CMD2_BIT_POS
;
1796 vcmd2
= NFCADDR_CMD_VCMD2
;
1798 /* For prgramming command, the cmd need set to write enable */
1799 case NAND_CMD_PAGEPROG
:
1800 case NAND_CMD_SEQIN
:
1801 case NAND_CMD_RNDIN
:
1802 nfcwr
= NFCADDR_CMD_NFCWR
;
1803 if (host
->nfc
->will_write_sram
&& command
== NAND_CMD_SEQIN
)
1804 dataen
= NFCADDR_CMD_DATAEN
;
1811 acycle
= nfc_make_addr(mtd
, command
, column
, page_addr
,
1812 &addr1234
, &cycle0
);
1814 nfc_addr_cmd
= cmd1
| cmd2
| vcmd2
| acycle
| csid
| dataen
| nfcwr
;
1815 nfc_send_command(host
, nfc_addr_cmd
, addr1234
, cycle0
);
1817 if (dataen
== NFCADDR_CMD_DATAEN
)
1818 if (nfc_wait_interrupt(host
, NFC_SR_XFR_DONE
))
1819 dev_err(host
->dev
, "something wrong, No XFR_DONE interrupt comes.\n");
1822 * Program and erase have their own busy handlers status, sequential
1823 * in, and deplete1 need no delay.
1826 case NAND_CMD_CACHEDPROG
:
1827 case NAND_CMD_PAGEPROG
:
1828 case NAND_CMD_ERASE1
:
1829 case NAND_CMD_ERASE2
:
1830 case NAND_CMD_RNDIN
:
1831 case NAND_CMD_STATUS
:
1832 case NAND_CMD_RNDOUT
:
1833 case NAND_CMD_SEQIN
:
1834 case NAND_CMD_READID
:
1837 case NAND_CMD_READ0
:
1838 if (dataen
== NFCADDR_CMD_DATAEN
) {
1839 host
->nfc
->data_in_sram
= host
->nfc
->sram_bank0
+
1840 nfc_get_sram_off(host
);
1845 nfc_wait_interrupt(host
, NFC_SR_RB_EDGE
);
1849 static int nfc_sram_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1850 uint32_t offset
, int data_len
, const uint8_t *buf
,
1851 int oob_required
, int page
, int cached
, int raw
)
1855 struct atmel_nand_host
*host
= chip
->priv
;
1856 void __iomem
*sram
= host
->nfc
->sram_bank0
+ nfc_get_sram_off(host
);
1858 /* Subpage write is not supported */
1859 if (offset
|| (data_len
< mtd
->writesize
))
1862 cfg
= nfc_readl(host
->nfc
->hsmc_regs
, CFG
);
1863 len
= mtd
->writesize
;
1865 if (unlikely(raw
)) {
1866 len
+= mtd
->oobsize
;
1867 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
| NFC_CFG_WSPARE
);
1869 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg
& ~NFC_CFG_WSPARE
);
1871 /* Copy page data to sram that will write to nand via NFC */
1873 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) != 0)
1874 /* Fall back to use cpu copy */
1875 memcpy32_toio(sram
, buf
, len
);
1877 memcpy32_toio(sram
, buf
, len
);
1880 if (chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
)
1882 * When use NFC sram, need set up PMECC before send
1883 * NAND_CMD_SEQIN command. Since when the nand command
1884 * is sent, nfc will do transfer from sram and nand.
1886 pmecc_enable(host
, NAND_ECC_WRITE
);
1888 host
->nfc
->will_write_sram
= true;
1889 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, 0x00, page
);
1890 host
->nfc
->will_write_sram
= false;
1893 /* Need to write ecc into oob */
1894 status
= chip
->ecc
.write_page(mtd
, chip
, buf
, oob_required
);
1899 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
1900 status
= chip
->waitfunc(mtd
, chip
);
1902 if ((status
& NAND_STATUS_FAIL
) && (chip
->errstat
))
1903 status
= chip
->errstat(mtd
, chip
, FL_WRITING
, status
, page
);
1905 if (status
& NAND_STATUS_FAIL
)
1911 static int nfc_sram_init(struct mtd_info
*mtd
)
1913 struct nand_chip
*chip
= mtd
->priv
;
1914 struct atmel_nand_host
*host
= chip
->priv
;
1917 /* Initialize the NFC CFG register */
1918 unsigned int cfg_nfc
= 0;
1920 /* set page size and oob layout */
1921 switch (mtd
->writesize
) {
1923 cfg_nfc
= NFC_CFG_PAGESIZE_512
;
1926 cfg_nfc
= NFC_CFG_PAGESIZE_1024
;
1929 cfg_nfc
= NFC_CFG_PAGESIZE_2048
;
1932 cfg_nfc
= NFC_CFG_PAGESIZE_4096
;
1935 cfg_nfc
= NFC_CFG_PAGESIZE_8192
;
1938 dev_err(host
->dev
, "Unsupported page size for NFC.\n");
1943 /* oob bytes size = (NFCSPARESIZE + 1) * 4
1944 * Max support spare size is 512 bytes. */
1945 cfg_nfc
|= (((mtd
->oobsize
/ 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
1946 & NFC_CFG_NFC_SPARESIZE
);
1947 /* default set a max timeout */
1948 cfg_nfc
|= NFC_CFG_RSPARE
|
1949 NFC_CFG_NFC_DTOCYC
| NFC_CFG_NFC_DTOMUL
;
1951 nfc_writel(host
->nfc
->hsmc_regs
, CFG
, cfg_nfc
);
1953 host
->nfc
->will_write_sram
= false;
1954 nfc_set_sram_bank(host
, 0);
1956 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
1957 if (host
->nfc
->write_by_sram
) {
1958 if ((chip
->ecc
.mode
== NAND_ECC_HW
&& host
->has_pmecc
) ||
1959 chip
->ecc
.mode
== NAND_ECC_NONE
)
1960 chip
->write_page
= nfc_sram_write_page
;
1962 host
->nfc
->write_by_sram
= false;
1965 dev_info(host
->dev
, "Using NFC Sram read %s\n",
1966 host
->nfc
->write_by_sram
? "and write" : "");
1970 static struct platform_driver atmel_nand_nfc_driver
;
1972 * Probe for the NAND device.
1974 static int atmel_nand_probe(struct platform_device
*pdev
)
1976 struct atmel_nand_host
*host
;
1977 struct mtd_info
*mtd
;
1978 struct nand_chip
*nand_chip
;
1979 struct resource
*mem
;
1980 struct mtd_part_parser_data ppdata
= {};
1983 /* Allocate memory for the device structure (and zero it) */
1984 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
1988 res
= platform_driver_register(&atmel_nand_nfc_driver
);
1990 dev_err(&pdev
->dev
, "atmel_nand: can't register NFC driver\n");
1992 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1993 host
->io_base
= devm_ioremap_resource(&pdev
->dev
, mem
);
1994 if (IS_ERR(host
->io_base
)) {
1995 dev_err(&pdev
->dev
, "atmel_nand: ioremap resource failed\n");
1996 res
= PTR_ERR(host
->io_base
);
1997 goto err_nand_ioremap
;
1999 host
->io_phys
= (dma_addr_t
)mem
->start
;
2002 nand_chip
= &host
->nand_chip
;
2003 host
->dev
= &pdev
->dev
;
2004 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
2005 /* Only when CONFIG_OF is enabled of_node can be parsed */
2006 res
= atmel_of_init_port(host
, pdev
->dev
.of_node
);
2008 goto err_nand_ioremap
;
2010 memcpy(&host
->board
, dev_get_platdata(&pdev
->dev
),
2011 sizeof(struct atmel_nand_data
));
2014 nand_chip
->priv
= host
; /* link the private data structures */
2015 mtd
->priv
= nand_chip
;
2016 mtd
->owner
= THIS_MODULE
;
2018 /* Set address of NAND IO lines */
2019 nand_chip
->IO_ADDR_R
= host
->io_base
;
2020 nand_chip
->IO_ADDR_W
= host
->io_base
;
2022 if (nand_nfc
.is_initialized
) {
2023 /* NFC driver is probed and initialized */
2024 host
->nfc
= &nand_nfc
;
2026 nand_chip
->select_chip
= nfc_select_chip
;
2027 nand_chip
->dev_ready
= nfc_device_ready
;
2028 nand_chip
->cmdfunc
= nfc_nand_command
;
2030 /* Initialize the interrupt for NFC */
2031 irq
= platform_get_irq(pdev
, 0);
2033 dev_err(host
->dev
, "Cannot get HSMC irq!\n");
2035 goto err_nand_ioremap
;
2038 res
= devm_request_irq(&pdev
->dev
, irq
, hsmc_interrupt
,
2041 dev_err(&pdev
->dev
, "Unable to request HSMC irq %d\n",
2043 goto err_nand_ioremap
;
2046 res
= atmel_nand_set_enable_ready_pins(mtd
);
2048 goto err_nand_ioremap
;
2050 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
2053 nand_chip
->ecc
.mode
= host
->board
.ecc_mode
;
2054 nand_chip
->chip_delay
= 20; /* 20us command delay time */
2056 if (host
->board
.bus_width_16
) /* 16-bit bus width */
2057 nand_chip
->options
|= NAND_BUSWIDTH_16
;
2059 nand_chip
->read_buf
= atmel_read_buf
;
2060 nand_chip
->write_buf
= atmel_write_buf
;
2062 platform_set_drvdata(pdev
, host
);
2063 atmel_nand_enable(host
);
2065 if (gpio_is_valid(host
->board
.det_pin
)) {
2066 res
= devm_gpio_request(&pdev
->dev
,
2067 host
->board
.det_pin
, "nand_det");
2070 "can't request det gpio %d\n",
2071 host
->board
.det_pin
);
2075 res
= gpio_direction_input(host
->board
.det_pin
);
2078 "can't request input direction det gpio %d\n",
2079 host
->board
.det_pin
);
2083 if (gpio_get_value(host
->board
.det_pin
)) {
2084 dev_info(&pdev
->dev
, "No SmartMedia card inserted.\n");
2090 if (host
->board
.on_flash_bbt
|| on_flash_bbt
) {
2091 dev_info(&pdev
->dev
, "Use On Flash BBT\n");
2092 nand_chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
2095 if (!host
->board
.has_dma
)
2099 dma_cap_mask_t mask
;
2102 dma_cap_set(DMA_MEMCPY
, mask
);
2103 host
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
2104 if (!host
->dma_chan
) {
2105 dev_err(host
->dev
, "Failed to request DMA channel\n");
2110 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
2111 dma_chan_name(host
->dma_chan
));
2113 dev_info(host
->dev
, "No DMA support for NAND access.\n");
2115 /* first scan to find the device and get the page size */
2116 if (nand_scan_ident(mtd
, 1, NULL
)) {
2118 goto err_scan_ident
;
2121 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
2122 if (host
->has_pmecc
)
2123 res
= atmel_pmecc_nand_init_params(pdev
, host
);
2125 res
= atmel_hw_nand_init_params(pdev
, host
);
2131 /* initialize the nfc configuration register */
2132 if (host
->nfc
&& host
->nfc
->use_nfc_sram
) {
2133 res
= nfc_sram_init(mtd
);
2135 host
->nfc
->use_nfc_sram
= false;
2136 dev_err(host
->dev
, "Disable use nfc sram for data transfer.\n");
2140 /* second phase scan */
2141 if (nand_scan_tail(mtd
)) {
2146 mtd
->name
= "atmel_nand";
2147 ppdata
.of_node
= pdev
->dev
.of_node
;
2148 res
= mtd_device_parse_register(mtd
, NULL
, &ppdata
,
2149 host
->board
.parts
, host
->board
.num_parts
);
2154 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
)
2155 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2159 atmel_nand_disable(host
);
2161 dma_release_channel(host
->dma_chan
);
2167 * Remove a NAND device.
2169 static int atmel_nand_remove(struct platform_device
*pdev
)
2171 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
2172 struct mtd_info
*mtd
= &host
->mtd
;
2176 atmel_nand_disable(host
);
2178 if (host
->has_pmecc
&& host
->nand_chip
.ecc
.mode
== NAND_ECC_HW
) {
2179 pmecc_writel(host
->ecc
, CTRL
, PMECC_CTRL_DISABLE
);
2180 pmerrloc_writel(host
->pmerrloc_base
, ELDIS
,
2185 dma_release_channel(host
->dma_chan
);
2187 platform_driver_unregister(&atmel_nand_nfc_driver
);
2192 static const struct of_device_id atmel_nand_dt_ids
[] = {
2193 { .compatible
= "atmel,at91rm9200-nand" },
2197 MODULE_DEVICE_TABLE(of
, atmel_nand_dt_ids
);
2199 static int atmel_nand_nfc_probe(struct platform_device
*pdev
)
2201 struct atmel_nfc
*nfc
= &nand_nfc
;
2202 struct resource
*nfc_cmd_regs
, *nfc_hsmc_regs
, *nfc_sram
;
2204 nfc_cmd_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2205 nfc
->base_cmd_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_cmd_regs
);
2206 if (IS_ERR(nfc
->base_cmd_regs
))
2207 return PTR_ERR(nfc
->base_cmd_regs
);
2209 nfc_hsmc_regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2210 nfc
->hsmc_regs
= devm_ioremap_resource(&pdev
->dev
, nfc_hsmc_regs
);
2211 if (IS_ERR(nfc
->hsmc_regs
))
2212 return PTR_ERR(nfc
->hsmc_regs
);
2214 nfc_sram
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
2216 nfc
->sram_bank0
= devm_ioremap_resource(&pdev
->dev
, nfc_sram
);
2217 if (IS_ERR(nfc
->sram_bank0
)) {
2218 dev_warn(&pdev
->dev
, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2219 PTR_ERR(nfc
->sram_bank0
));
2221 nfc
->use_nfc_sram
= true;
2222 nfc
->sram_bank0_phys
= (dma_addr_t
)nfc_sram
->start
;
2224 if (pdev
->dev
.of_node
)
2225 nfc
->write_by_sram
= of_property_read_bool(
2227 "atmel,write-by-sram");
2231 nfc_writel(nfc
->hsmc_regs
, IDR
, 0xffffffff);
2232 nfc_readl(nfc
->hsmc_regs
, SR
); /* clear the NFC_SR */
2234 nfc
->is_initialized
= true;
2235 dev_info(&pdev
->dev
, "NFC is probed.\n");
2239 static const struct of_device_id atmel_nand_nfc_match
[] = {
2240 { .compatible
= "atmel,sama5d3-nfc" },
2243 MODULE_DEVICE_TABLE(of
, atmel_nand_nfc_match
);
2245 static struct platform_driver atmel_nand_nfc_driver
= {
2247 .name
= "atmel_nand_nfc",
2248 .owner
= THIS_MODULE
,
2249 .of_match_table
= of_match_ptr(atmel_nand_nfc_match
),
2251 .probe
= atmel_nand_nfc_probe
,
2254 static struct platform_driver atmel_nand_driver
= {
2255 .probe
= atmel_nand_probe
,
2256 .remove
= atmel_nand_remove
,
2258 .name
= "atmel_nand",
2259 .owner
= THIS_MODULE
,
2260 .of_match_table
= of_match_ptr(atmel_nand_dt_ids
),
2264 module_platform_driver(atmel_nand_driver
);
2266 MODULE_LICENSE("GPL");
2267 MODULE_AUTHOR("Rick Bronson");
2268 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
2269 MODULE_ALIAS("platform:atmel_nand");