1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * (C) Copyright 2006
3*cfcc706cSMiquel Raynal * KwikByte <kb9200_dev@kwikbyte.com>
4*cfcc706cSMiquel Raynal *
5*cfcc706cSMiquel Raynal * (C) Copyright 2009
6*cfcc706cSMiquel Raynal * Matthias Kaehlcke <matthias@kaehlcke.net>
7*cfcc706cSMiquel Raynal *
8*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
9*cfcc706cSMiquel Raynal */
10*cfcc706cSMiquel Raynal
11*cfcc706cSMiquel Raynal #include <common.h>
12*cfcc706cSMiquel Raynal #include <asm/io.h>
13*cfcc706cSMiquel Raynal #include <asm/arch/AT91RM9200.h>
14*cfcc706cSMiquel Raynal #include <asm/arch/hardware.h>
15*cfcc706cSMiquel Raynal
16*cfcc706cSMiquel Raynal #include <nand.h>
17*cfcc706cSMiquel Raynal
18*cfcc706cSMiquel Raynal /*
19*cfcc706cSMiquel Raynal * hardware specific access to control-lines
20*cfcc706cSMiquel Raynal */
21*cfcc706cSMiquel Raynal
22*cfcc706cSMiquel Raynal #define MASK_ALE (1 << 22) /* our ALE is A22 */
23*cfcc706cSMiquel Raynal #define MASK_CLE (1 << 21) /* our CLE is A21 */
24*cfcc706cSMiquel Raynal
25*cfcc706cSMiquel Raynal #define KB9202_NAND_NCE (1 << 28) /* EN* on D28 */
26*cfcc706cSMiquel Raynal #define KB9202_NAND_BUSY (1 << 29) /* RB* on D29 */
27*cfcc706cSMiquel Raynal
28*cfcc706cSMiquel Raynal #define KB9202_SMC2_NWS (1 << 2)
29*cfcc706cSMiquel Raynal #define KB9202_SMC2_TDF (1 << 8)
30*cfcc706cSMiquel Raynal #define KB9202_SMC2_RWSETUP (1 << 24)
31*cfcc706cSMiquel Raynal #define KB9202_SMC2_RWHOLD (1 << 29)
32*cfcc706cSMiquel Raynal
33*cfcc706cSMiquel Raynal /*
34*cfcc706cSMiquel Raynal * Board-specific function to access device control signals
35*cfcc706cSMiquel Raynal */
kb9202_nand_hwcontrol(struct mtd_info * mtd,int cmd,unsigned int ctrl)36*cfcc706cSMiquel Raynal static void kb9202_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
37*cfcc706cSMiquel Raynal {
38*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
39*cfcc706cSMiquel Raynal
40*cfcc706cSMiquel Raynal if (ctrl & NAND_CTRL_CHANGE) {
41*cfcc706cSMiquel Raynal ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
42*cfcc706cSMiquel Raynal
43*cfcc706cSMiquel Raynal /* clear ALE and CLE bits */
44*cfcc706cSMiquel Raynal IO_ADDR_W &= ~(MASK_ALE | MASK_CLE);
45*cfcc706cSMiquel Raynal
46*cfcc706cSMiquel Raynal if (ctrl & NAND_CLE)
47*cfcc706cSMiquel Raynal IO_ADDR_W |= MASK_CLE;
48*cfcc706cSMiquel Raynal
49*cfcc706cSMiquel Raynal if (ctrl & NAND_ALE)
50*cfcc706cSMiquel Raynal IO_ADDR_W |= MASK_ALE;
51*cfcc706cSMiquel Raynal
52*cfcc706cSMiquel Raynal this->IO_ADDR_W = (void *) IO_ADDR_W;
53*cfcc706cSMiquel Raynal
54*cfcc706cSMiquel Raynal if (ctrl & NAND_NCE)
55*cfcc706cSMiquel Raynal writel(KB9202_NAND_NCE, AT91C_PIOC_CODR);
56*cfcc706cSMiquel Raynal else
57*cfcc706cSMiquel Raynal writel(KB9202_NAND_NCE, AT91C_PIOC_SODR);
58*cfcc706cSMiquel Raynal }
59*cfcc706cSMiquel Raynal
60*cfcc706cSMiquel Raynal if (cmd != NAND_CMD_NONE)
61*cfcc706cSMiquel Raynal writeb(cmd, this->IO_ADDR_W);
62*cfcc706cSMiquel Raynal }
63*cfcc706cSMiquel Raynal
64*cfcc706cSMiquel Raynal
65*cfcc706cSMiquel Raynal /*
66*cfcc706cSMiquel Raynal * Board-specific function to access the device ready signal.
67*cfcc706cSMiquel Raynal */
kb9202_nand_ready(struct mtd_info * mtd)68*cfcc706cSMiquel Raynal static int kb9202_nand_ready(struct mtd_info *mtd)
69*cfcc706cSMiquel Raynal {
70*cfcc706cSMiquel Raynal return readl(AT91C_PIOC_PDSR) & KB9202_NAND_BUSY;
71*cfcc706cSMiquel Raynal }
72*cfcc706cSMiquel Raynal
73*cfcc706cSMiquel Raynal
74*cfcc706cSMiquel Raynal /*
75*cfcc706cSMiquel Raynal * Board-specific NAND init. Copied from include/linux/mtd/nand.h for reference.
76*cfcc706cSMiquel Raynal *
77*cfcc706cSMiquel Raynal * struct nand_chip - NAND Private Flash Chip Data
78*cfcc706cSMiquel Raynal * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the flash device
79*cfcc706cSMiquel Raynal * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the flash device
80*cfcc706cSMiquel Raynal * @hwcontrol: [BOARDSPECIFIC] hardwarespecific function for accesing control-lines
81*cfcc706cSMiquel Raynal * @dev_ready: [BOARDSPECIFIC] hardwarespecific function for accesing device ready/busy line
82*cfcc706cSMiquel Raynal * If set to NULL no access to ready/busy is available and the ready/busy information
83*cfcc706cSMiquel Raynal * is read from the chip status register
84*cfcc706cSMiquel Raynal * @enable_hwecc: [BOARDSPECIFIC] function to enable (reset) hardware ecc generator. Must only
85*cfcc706cSMiquel Raynal * be provided if a hardware ECC is available
86*cfcc706cSMiquel Raynal * @eccmode: [BOARDSPECIFIC] mode of ecc, see defines
87*cfcc706cSMiquel Raynal * @chip_delay: [BOARDSPECIFIC] chip dependent delay for transfering data from array to read regs (tR)
88*cfcc706cSMiquel Raynal * @options: [BOARDSPECIFIC] various chip options. They can partly be set to inform nand_scan about
89*cfcc706cSMiquel Raynal * special functionality. See the defines for further explanation
90*cfcc706cSMiquel Raynal */
91*cfcc706cSMiquel Raynal /*
92*cfcc706cSMiquel Raynal * This routine initializes controller and GPIOs.
93*cfcc706cSMiquel Raynal */
board_nand_init(struct nand_chip * nand)94*cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *nand)
95*cfcc706cSMiquel Raynal {
96*cfcc706cSMiquel Raynal unsigned int value;
97*cfcc706cSMiquel Raynal
98*cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT;
99*cfcc706cSMiquel Raynal nand->cmd_ctrl = kb9202_nand_hwcontrol;
100*cfcc706cSMiquel Raynal nand->dev_ready = kb9202_nand_ready;
101*cfcc706cSMiquel Raynal
102*cfcc706cSMiquel Raynal /* in case running outside of bootloader */
103*cfcc706cSMiquel Raynal writel(1 << AT91C_ID_PIOC, AT91C_PMC_PCER);
104*cfcc706cSMiquel Raynal
105*cfcc706cSMiquel Raynal /* setup nand flash access (allow ample margin) */
106*cfcc706cSMiquel Raynal /* 4 wait states, 1 setup, 1 hold, 1 float for 8-bit device */
107*cfcc706cSMiquel Raynal writel(AT91C_SMC2_WSEN | KB9202_SMC2_NWS | KB9202_SMC2_TDF |
108*cfcc706cSMiquel Raynal AT91C_SMC2_DBW_8 | KB9202_SMC2_RWSETUP | KB9202_SMC2_RWHOLD,
109*cfcc706cSMiquel Raynal AT91C_SMC_CSR3);
110*cfcc706cSMiquel Raynal
111*cfcc706cSMiquel Raynal /* enable internal NAND controller */
112*cfcc706cSMiquel Raynal value = readl(AT91C_EBI_CSA);
113*cfcc706cSMiquel Raynal value |= AT91C_EBI_CS3A_SMC_SmartMedia;
114*cfcc706cSMiquel Raynal writel(value, AT91C_EBI_CSA);
115*cfcc706cSMiquel Raynal
116*cfcc706cSMiquel Raynal /* enable SMOE/SMWE */
117*cfcc706cSMiquel Raynal writel(AT91C_PC1_BFRDY_SMOE | AT91C_PC3_BFBAA_SMWE, AT91C_PIOC_ASR);
118*cfcc706cSMiquel Raynal writel(AT91C_PC1_BFRDY_SMOE | AT91C_PC3_BFBAA_SMWE, AT91C_PIOC_PDR);
119*cfcc706cSMiquel Raynal writel(AT91C_PC1_BFRDY_SMOE | AT91C_PC3_BFBAA_SMWE, AT91C_PIOC_OER);
120*cfcc706cSMiquel Raynal
121*cfcc706cSMiquel Raynal /* set NCE to high */
122*cfcc706cSMiquel Raynal writel(KB9202_NAND_NCE, AT91C_PIOC_SODR);
123*cfcc706cSMiquel Raynal
124*cfcc706cSMiquel Raynal /* disable output on pin connected to the busy line of the NAND */
125*cfcc706cSMiquel Raynal writel(KB9202_NAND_BUSY, AT91C_PIOC_ODR);
126*cfcc706cSMiquel Raynal
127*cfcc706cSMiquel Raynal /* enable the PIO to control NCE and BUSY */
128*cfcc706cSMiquel Raynal writel(KB9202_NAND_NCE | KB9202_NAND_BUSY, AT91C_PIOC_PER);
129*cfcc706cSMiquel Raynal
130*cfcc706cSMiquel Raynal /* enable output for NCE */
131*cfcc706cSMiquel Raynal writel(KB9202_NAND_NCE, AT91C_PIOC_OER);
132*cfcc706cSMiquel Raynal
133*cfcc706cSMiquel Raynal return (0);
134*cfcc706cSMiquel Raynal }
135