5b98833c06b44db0f1412ad1e32feb3dc989eb4f
[librecmc/librecmc.git] / target / linux / adm5120-2.6 / files / drivers / mtd / nand / rbmipsnand.c
1 /*==============================================================================*/
2 /* rbmipsnand.c                                                                 */
3 /* This module is derived from the 2.4 driver shipped by Microtik for their     */
4 /* Routerboard 1xx and 5xx series boards.  It provides support for the built in */
5 /* NAND flash on the Routerboard 1xx series boards for Linux 2.6.19+.           */
6 /* Licence: Original Microtik code seems not to have a licence.                 */
7 /*          Rewritten code all GPL V2.                                          */
8 /* Copyright(C) 2007 david.goodenough@linkchoose.co.uk (for rewriten code)      */
9 /*==============================================================================*/
10
11 #include <linux/init.h>
12 #include <linux/mtd/nand.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
15 #include <linux/delay.h>
16
17 #include <asm/io.h>
18 #include <asm/irq.h>
19 #include <asm/bootinfo.h>
20 #include <asm/mach-adm5120/adm5120_info.h>
21 #include <asm/mach-adm5120/adm5120_defs.h>
22
23 #define SMEM1(x) (*((volatile unsigned char *) (KSEG1ADDR(ADM5120_SRAM1_BASE) + x)))
24
25 #define NAND_RW_REG     0x0     //data register
26 #define NAND_SET_CEn    0x1     //CE# low
27 #define NAND_CLR_CEn    0x2     //CE# high
28 #define NAND_CLR_CLE    0x3     //CLE low
29 #define NAND_SET_CLE    0x4     //CLE high
30 #define NAND_CLR_ALE    0x5     //ALE low
31 #define NAND_SET_ALE    0x6     //ALE high
32 #define NAND_SET_SPn    0x7     //SP# low (use spare area)
33 #define NAND_CLR_SPn    0x8     //SP# high (do not use spare area)
34 #define NAND_SET_WPn    0x9     //WP# low
35 #define NAND_CLR_WPn    0xA     //WP# high
36 #define NAND_STS_REG    0xB     //Status register
37
38 #define MEM32(x) *((volatile unsigned *) (x))
39 static void __iomem *p_nand;
40
41 static int rb100_dev_ready(struct mtd_info *mtd) {
42     return SMEM1(NAND_STS_REG) & 0x80;
43 }
44
45 static void rbmips_hwcontrol100(struct mtd_info *mtd, int cmd, unsigned int ctrl) {
46     struct nand_chip *chip = mtd->priv;
47     if (ctrl & NAND_CTRL_CHANGE) {
48         SMEM1((( ctrl & NAND_CLE) ? NAND_SET_CLE : NAND_CLR_CLE)) = 0x01;
49         SMEM1((( ctrl & NAND_ALE) ? NAND_SET_ALE : NAND_CLR_ALE)) = 0x01;
50         SMEM1((( ctrl & NAND_NCE) ? NAND_SET_CEn : NAND_CLR_CEn)) = 0x01;
51         }
52     if( cmd != NAND_CMD_NONE)
53         writeb( cmd, chip->IO_ADDR_W);
54 }
55
56 static struct mtd_partition partition_info[] = {
57     {
58         name: "RouterBoard NAND Boot",
59         offset: 0,
60         size: 4 * 1024 * 1024
61     },
62     {
63         name: "rootfs",
64         offset: MTDPART_OFS_NXTBLK,
65         size: MTDPART_SIZ_FULL
66     }
67 };
68
69 static struct mtd_info rmtd;
70 static struct nand_chip rnand;
71 /*========================================================================*/
72 /* We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader */
73 /* will not be able to find the kernel that we load.  So set the oobinfo  */
74 /* when creating the partitions.                                          */ 
75 /*========================================================================*/
76 static struct nand_ecclayout rb_ecclayout = {
77         .eccbytes = 6,
78         .eccpos = { 8, 9, 10, 13, 14, 15 },
79         .oobavail = 9,
80         .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1} }
81 };
82 static unsigned init_ok = 0;
83
84 unsigned get_rbnand_block_size(void) {
85         return init_ok ? rmtd.writesize : 0;
86 }
87
88 EXPORT_SYMBOL(get_rbnand_block_size);
89
90 int __init rbmips_init(void) {
91
92         if (!adm5120_nand_boot)
93                 return -ENODEV;
94
95         memset(&rmtd, 0, sizeof(rmtd));
96         memset(&rnand, 0, sizeof(rnand));
97         printk(KERN_INFO "RB1xx nand\n");
98         MEM32(0xB2000064) = 0x100;
99         MEM32(0xB2000008) = 0x1;
100         SMEM1(NAND_SET_SPn) = 0x01;
101         SMEM1(NAND_CLR_WPn) = 0x01;
102         rnand.IO_ADDR_R = (unsigned char *)KSEG1ADDR(ADM5120_SRAM1_BASE);
103         rnand.IO_ADDR_W = rnand.IO_ADDR_R;
104         rnand.cmd_ctrl = rbmips_hwcontrol100;
105         rnand.dev_ready = rb100_dev_ready;
106         p_nand = (void __iomem *)ioremap(( unsigned long)ADM5120_SRAM1_BASE, 0x1000);
107         if (!p_nand) {
108                 printk(KERN_WARNING "RB1xx nand Unable ioremap buffer\n");
109                 return -ENXIO;
110         }
111         rnand.ecc.mode = NAND_ECC_SOFT;
112         rnand.ecc.layout = &rb_ecclayout;
113         rnand.chip_delay = 25;
114         rnand.options |= NAND_NO_AUTOINCR;
115         rmtd.priv = &rnand;
116         if (nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1)
117             && nand_scan(&rmtd, 1)  && nand_scan(&rmtd, 1)) {
118                 printk(KERN_INFO "RB1xxx nand device not found\n");
119                 iounmap ((void *)p_nand);
120                 return -ENXIO;
121         }
122         add_mtd_partitions(&rmtd, partition_info, 2);
123         init_ok = 1;
124         return 0;
125 }
126
127 module_init(rbmips_init);
128