Convert the nand driver to a platform_device, should only allow routerboards to use it
[openwrt/svn-archive/archive.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/io.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/mtd/nand.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/delay.h>
18
19 #include <asm/io.h>
20 #include <asm/irq.h>
21 #include <asm/bootinfo.h>
22 #include <asm/mach-adm5120/adm5120_info.h>
23 #include <asm/mach-adm5120/adm5120_defs.h>
24
25 #define SMEM1(x) (*((volatile unsigned char *) (KSEG1ADDR(ADM5120_SRAM1_BASE) + x)))
26
27 #define NAND_RW_REG 0x0 //data register
28 #define NAND_SET_CEn 0x1 //CE# low
29 #define NAND_CLR_CEn 0x2 //CE# high
30 #define NAND_CLR_CLE 0x3 //CLE low
31 #define NAND_SET_CLE 0x4 //CLE high
32 #define NAND_CLR_ALE 0x5 //ALE low
33 #define NAND_SET_ALE 0x6 //ALE high
34 #define NAND_SET_SPn 0x7 //SP# low (use spare area)
35 #define NAND_CLR_SPn 0x8 //SP# high (do not use spare area)
36 #define NAND_SET_WPn 0x9 //WP# low
37 #define NAND_CLR_WPn 0xA //WP# high
38 #define NAND_STS_REG 0xB //Status register
39
40 #define MEM32(x) *((volatile unsigned *) (x))
41
42 static struct mtd_partition partition_info[] = {
43 {
44 name: "RouterBoard NAND Boot",
45 offset: 0,
46 size: 4 * 1024 * 1024
47 },
48 {
49 name: "rootfs",
50 offset: MTDPART_OFS_NXTBLK,
51 size: MTDPART_SIZ_FULL
52 }
53 };
54
55 static struct nand_ecclayout rb_ecclayout = {
56 .eccbytes = 6,
57 .eccpos = { 8, 9, 10, 13, 14, 15 },
58 .oobavail = 9,
59 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1} }
60 };
61
62 struct adm5120_nand_info {
63 struct nand_chip chip;
64 struct mtd_info mtd;
65 void __iomem *io_base;
66 #ifdef CONFIG_MTD_PARTITIONS
67 int nr_parts;
68 struct mtd_partition *parts;
69 #endif
70 };
71
72 static int rb100_dev_ready(struct mtd_info *mtd)
73 {
74 return SMEM1(NAND_STS_REG) & 0x80;
75 }
76
77 static void rbmips_hwcontrol100(struct mtd_info *mtd, int cmd, unsigned int ctrl)
78 {
79 struct nand_chip *chip = mtd->priv;
80 if (ctrl & NAND_CTRL_CHANGE)
81 {
82 SMEM1((( ctrl & NAND_CLE) ? NAND_SET_CLE : NAND_CLR_CLE)) = 0x01;
83 SMEM1((( ctrl & NAND_ALE) ? NAND_SET_ALE : NAND_CLR_ALE)) = 0x01;
84 SMEM1((( ctrl & NAND_NCE) ? NAND_SET_CEn : NAND_CLR_CEn)) = 0x01;
85 }
86 if (cmd != NAND_CMD_NONE)
87 writeb( cmd, chip->IO_ADDR_W);
88 }
89
90 /*========================================================================*/
91 /* We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader */
92 /* will not be able to find the kernel that we load. So set the oobinfo */
93 /* when creating the partitions. */
94 /*========================================================================*/
95
96 static unsigned init_ok = 0;
97
98 unsigned get_rbnand_block_size(struct adm5120_nand_info *data)
99 {
100 return init_ok ? data->mtd.writesize : 0;
101 }
102
103 EXPORT_SYMBOL(get_rbnand_block_size);
104
105 static int rbmips_probe(struct platform_device *pdev)
106 {
107 struct adm5120_nand_info *data;
108 int res = 0;
109
110 /* Allocate memory for the nand_chip structure */
111 data = kzalloc(sizeof(*data), GFP_KERNEL);
112 if (!data) {
113 dev_err(&pdev->dev, "Failed to allocate device structure\n");
114 return -ENOMEM;
115
116 }
117
118 data->io_base = ioremap(pdev->resource[0].start, 0x1000);
119
120 if (data->io_base == NULL) {
121 dev_err(&pdev->dev, "ioremap failed\n");
122 kfree(data);
123 return -EIO;
124 }
125
126 MEM32(0xB2000064) = 0x100;
127 MEM32(0xB2000008) = 0x1;
128 SMEM1(NAND_SET_SPn) = 0x01;
129 SMEM1(NAND_CLR_WPn) = 0x01;
130
131 data->chip.priv = &data;
132 data->mtd.priv = &data->chip;
133 data->mtd.owner = THIS_MODULE;
134
135 data->chip.IO_ADDR_R = (unsigned char *)KSEG1ADDR(ADM5120_SRAM1_BASE);
136 data->chip.IO_ADDR_W = data->chip.IO_ADDR_R;
137 data->chip.cmd_ctrl = rbmips_hwcontrol100;
138 data->chip.dev_ready = rb100_dev_ready;
139 data->chip.ecc.mode = NAND_ECC_SOFT;
140 data->chip.ecc.layout = &rb_ecclayout;
141 data->chip.chip_delay = 25;
142 data->chip.options |= NAND_NO_AUTOINCR;
143
144 platform_set_drvdata(pdev, data);
145
146 /* Why do we need to scan 4 times ? */
147 if (nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1)) {
148 printk(KERN_INFO "RB1xxx nand device not found\n");
149 res = -ENXIO;
150 goto out;
151 }
152
153 add_mtd_partitions(&data->mtd, partition_info, 2);
154 init_ok = 1;
155
156 res = add_mtd_device(&data->mtd);
157 if (!res)
158 return res;
159
160 nand_release(&data->mtd);
161 out:
162 platform_set_drvdata(pdev, NULL);
163 iounmap(data->io_base);
164 kfree(data);
165 return res;
166 }
167
168 static int __devexit rbmips_remove(struct platform_device *pdev)
169 {
170 struct adm5120_nand_info *data = platform_get_drvdata(pdev);
171
172 nand_release(&data->mtd);
173 iounmap(data->io_base);
174 kfree(data);
175
176 return 0;
177 }
178
179 static struct platform_driver adm5120_nand_driver = {
180 .probe = rbmips_probe,
181 .remove = rbmips_remove,
182 .driver = {
183 .name = "adm5120-nand",
184 .owner = THIS_MODULE,
185 },
186 };
187
188 static int __init adm5120_nand_init(void)
189 {
190 int err;
191 err = platform_driver_register(&adm5120_nand_driver);
192 return err;
193 }
194
195 static void __exit adm5120_nand_exit(void)
196 {
197 platform_driver_unregister(&adm5120_nand_driver);
198 }
199
200 module_init(adm5120_nand_init);
201 module_exit(adm5120_nand_exit);
202
203 MODULE_LICENSE("GPL");
204 MODULE_AUTHOR("David Goodenough, Florian Fainelli");
205 MODULE_DESCRIPTION("RouterBOARD 100 NAND driver");
206