1 #include <linux/init.h>
2 #include <linux/mtd/nand.h>
3 #include <linux/mtd/mtd.h>
4 #include <linux/mtd/partitions.h>
5 #include <linux/delay.h>
8 #include <asm/bootinfo.h>
9 #ifdef CONFIG_MIPS_ADM5120
10 #include <asm/mach-adm5120/adm5120_defs.h>
13 #define IDT434_REG_BASE ((volatile void *) KSEG1ADDR(0x18000000))
15 #define GPIOF 0x050000
16 #define GPIOC 0x050004
17 #define GPIOD 0x050008
19 #define GPIO_RDY (1 << 0x08)
20 #define GPIO_WPX (1 << 0x09)
21 #define GPIO_ALE (1 << 0x0a)
22 #define GPIO_CLE (1 << 0x0b)
24 #define DEV2BASE 0x010020
26 #define LO_WPX (1 << 0)
27 #define LO_ALE (1 << 1)
28 #define LO_CLE (1 << 2)
29 #define LO_CEX (1 << 3)
30 #define LO_FOFF (1 << 5)
31 #define LO_SPICS (1 << 6)
32 #define LO_ULED (1 << 7)
34 #define MEM32(x) *((volatile unsigned *) (x))
37 /* RouterBoard RB1xx specific definitions */
38 #define SMEM1(x) (*((volatile unsigned char *) (KSEG1ADDR(ADM5120_SRAM1_BASE) + x)))
40 #define NAND_RW_REG 0x0 //data register
41 #define NAND_SET_CEn 0x1 //CE# low
42 #define NAND_CLR_CEn 0x2 //CE# high
43 #define NAND_CLR_CLE 0x3 //CLE low
44 #define NAND_SET_CLE 0x4 //CLE high
45 #define NAND_CLR_ALE 0x5 //ALE low
46 #define NAND_SET_ALE 0x6 //ALE high
47 #define NAND_SET_SPn 0x7 //SP# low (use spare area)
48 #define NAND_CLR_SPn 0x8 //SP# high (do not use spare area)
49 #define NAND_SET_WPn 0x9 //WP# low
50 #define NAND_CLR_WPn 0xA //WP# high
51 #define NAND_STS_REG 0xB //Status register
53 static void __iomem
*p_nand
;
55 #ifdef CONFIG_MIKROTIK_RB500
56 extern void changeLatchU5(unsigned char orMask
, unsigned char nandMask
);
58 static int rb500_dev_ready(struct mtd_info
*mtd
)
60 return MEM32(IDT434_REG_BASE
+ GPIOD
) & GPIO_RDY
;
64 * hardware specific access to control-lines
67 * NAND_CLE: bit 2 -> bit 3
68 * NAND_ALE: bit 3 -> bit 2
70 static void rbmips_hwcontrol500(struct mtd_info
*mtd
, int cmd
,
73 struct nand_chip
*chip
= mtd
->priv
;
74 unsigned char orbits
, nandbits
;
76 if (ctrl
& NAND_CTRL_CHANGE
) {
78 orbits
= (ctrl
& NAND_CLE
) << 1;
79 orbits
|= (ctrl
& NAND_ALE
) >> 1;
81 nandbits
= (~ctrl
& NAND_CLE
) << 1;
82 nandbits
|= (~ctrl
& NAND_ALE
) >> 1;
84 changeLatchU5(orbits
, nandbits
);
86 if (cmd
!= NAND_CMD_NONE
)
87 writeb(cmd
, chip
->IO_ADDR_W
);
92 #ifdef CONFIG_MIPS_ADM5120
93 static int rb100_dev_ready(struct mtd_info
*mtd
)
95 return SMEM1(NAND_STS_REG
) & 0x80; /* found out by experiment */
98 static void rbmips_hwcontrol100(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
100 struct nand_chip
*chip
= mtd
->priv
;
102 if (ctrl
& NAND_CTRL_CHANGE
)
104 if (cmd
!= NAND_CMD_NONE
)
105 writeb(cmd
, chip
->IO_ADDR_W
);
110 static struct mtd_partition partition_info
[] = {
112 name
:"RouterBoard NAND Boot",
114 size
:4 * 1024 * 1024},
116 name
:"RouterBoard NAND Main",
117 offset
:MTDPART_OFS_NXTBLK
,
118 size
:MTDPART_SIZ_FULL
}
121 static struct mtd_info rmtd
;
122 static struct nand_chip rnand
;
124 static unsigned init_ok
= 0;
126 unsigned get_rbnand_block_size(void)
129 return rmtd
.writesize
;
134 EXPORT_SYMBOL(get_rbnand_block_size
);
136 int __init
rbmips_init(void)
138 memset(&rmtd
, 0, sizeof(rmtd
));
139 memset(&rnand
, 0, sizeof(rnand
));
141 #ifdef CONFIG_MIKROTIK_RB500
142 printk("RB500 nand\n");
143 changeLatchU5(LO_WPX
| LO_FOFF
| LO_CEX
,
144 LO_ULED
| LO_ALE
| LO_CLE
);
145 rnand
.cmd_ctrl
= rbmips_hwcontrol500
;
147 rnand
.dev_ready
= rb500_dev_ready
;
148 rnand
.IO_ADDR_W
= (unsigned char *)
149 KSEG1ADDR(MEM32(IDT434_REG_BASE
+ DEV2BASE
));
150 rnand
.IO_ADDR_R
= rnand
.IO_ADDR_W
;
153 #ifdef CONFIG_MIPS_ADM5120
154 printk("RB100 nand\n");
155 /* enable NAND flash */
156 MEM32(0xB2000064) = 0x100;
158 MEM32(0xB2000008) = 0x1;
159 SMEM1(NAND_SET_SPn
) = 0x01;
160 SMEM1(NAND_CLR_WPn
) = 0x01;
161 rnand
.IO_ADDR_R
= (unsigned char *)KSEG1ADDR(ADM5120_SRAM1_BASE
);
162 rnand
.IO_ADDR_W
= rnand
.IO_ADDR_R
;
163 rnand
.cmd_ctrl
= rbmips_hwcontrol100
;
164 rnand
.dev_ready
= rb100_dev_ready
;
166 p_nand
= (void __iomem
*) ioremap((void *) 0x18a20000, 0x1000);
168 printk("RBnand Unable ioremap buffer");
171 rnand
.ecc
.mode
= NAND_ECC_SOFT
;
172 rnand
.chip_delay
= 25;
173 rnand
.options
|= NAND_NO_AUTOINCR
;
176 #ifdef CONFIG_MIKROTIK_RB500
179 b
= (int *) KSEG1ADDR(0x18010020);
180 printk("dev2base 0x%08x mask 0x%08x c 0x%08x tc 0x%08x\n", b
[0],
184 if (nand_scan(&rmtd
, 1) && nand_scan(&rmtd
, 1)
185 && nand_scan(&rmtd
, 1) && nand_scan(&rmtd
, 1)) {
186 printk("RBxxx nand device not found");
187 iounmap((void *) p_nand
);
191 add_mtd_partitions(&rmtd
, partition_info
, 2);
196 module_init(rbmips_init
);