Convert the NAND driver to platform_driver, allow tgz image to be built correctly
[openwrt/svn-archive/archive.git] / target / linux / rb532-2.6 / files / drivers / mtd / nand / rbmipsnand.c
1 #include <linux/io.h>
2 #include <linux/module.h>
3 #include <linux/platform_device.h>
4 #include <linux/mtd/nand.h>
5 #include <linux/mtd/mtd.h>
6 #include <linux/mtd/partitions.h>
7 #include <linux/delay.h>
8
9 #include <asm/io.h>
10 #include <asm/irq.h>
11 #include <asm/bootinfo.h>
12
13 #define IDT434_REG_BASE ((volatile void *) KSEG1ADDR(0x18000000))
14
15 #define GPIOF 0x050000
16 #define GPIOC 0x050004
17 #define GPIOD 0x050008
18
19 #define GPIO_RDY (1 << 0x08)
20 #define GPIO_WPX (1 << 0x09)
21 #define GPIO_ALE (1 << 0x0a)
22 #define GPIO_CLE (1 << 0x0b)
23
24 #define DEV2BASE 0x010020
25
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)
33
34 #define MEM32(x) *((volatile unsigned *) (x))
35
36 struct rb500_nand_info {
37 struct nand_chip chip;
38 struct mtd_info mtd;
39 void __iomem *io_base;
40 #ifdef CONFIG_MTD_PARTITIONS
41 int nr_parts;
42 struct mtd_partition *parts;
43 #endif
44 int init_ok;
45 int flags1;
46 int flags2;
47 };
48
49 static struct mtd_partition partition_info[] = {
50 {
51 name:"RouterBoard NAND Boot",
52 offset:0,
53 size:4 * 1024 * 1024},
54 {
55 name:"rootfs",
56 offset:MTDPART_OFS_NXTBLK,
57 size:MTDPART_SIZ_FULL}
58 };
59
60 extern void changeLatchU5(unsigned char orMask, unsigned char nandMask);
61
62 static int rb500_dev_ready(struct mtd_info *mtd)
63 {
64 return MEM32(IDT434_REG_BASE + GPIOD) & GPIO_RDY;
65 }
66
67 /*
68 * hardware specific access to control-lines
69 *
70 * ctrl:
71 * NAND_CLE: bit 2 -> bit 3
72 * NAND_ALE: bit 3 -> bit 2
73 */
74 static void rbmips_hwcontrol500(struct mtd_info *mtd, int cmd,
75 unsigned int ctrl)
76 {
77 struct nand_chip *chip = mtd->priv;
78 unsigned char orbits, nandbits;
79
80 if (ctrl & NAND_CTRL_CHANGE) {
81
82 orbits = (ctrl & NAND_CLE) << 1;
83 orbits |= (ctrl & NAND_ALE) >> 1;
84
85 nandbits = (~ctrl & NAND_CLE) << 1;
86 nandbits |= (~ctrl & NAND_ALE) >> 1;
87
88 changeLatchU5(orbits, nandbits);
89 }
90 if (cmd != NAND_CMD_NONE)
91 writeb(cmd, chip->IO_ADDR_W);
92
93 }
94
95 unsigned get_rbnand_block_size(struct rb500_nand_info *data)
96 {
97 if (data->init_ok)
98 return data->mtd.writesize;
99 else
100 return 0;
101 }
102
103 EXPORT_SYMBOL(get_rbnand_block_size);
104
105 static int rbmips_probe(struct platform_device *pdev)
106 {
107 struct rb500_nand_info *data;
108 int res = 0;
109 int *b;
110
111 /* Allocate memory for the structure */
112 data = kzalloc(sizeof(*data), GFP_KERNEL);
113 if (!data) {
114 dev_err(&pdev->dev, "Failed to allocate device structure\n");
115 return -ENOMEM;
116 }
117
118 data->io_base = ioremap(pdev->resource[0].start, pdev->resource[0].end - pdev->resource[0].start + 1);
119
120 if (data->io_base == NULL) {
121 dev_err(&pdev->dev, "ioremap failed\n");
122 kfree(data);
123 return -EIO;
124 }
125
126 /* FIXME : this seems to work only for newer RB500, check the version to set the right flags accordingly */
127 data->flags1 = LO_FOFF | LO_CEX;
128 data->flags2 = LO_ULED | LO_ALE | LO_CLE | LO_WPX;
129
130 changeLatchU5(data->flags1, data->flags2);
131
132 data->chip.cmd_ctrl = rbmips_hwcontrol500;
133
134 data->chip.dev_ready = rb500_dev_ready;
135 data->chip.IO_ADDR_W = (unsigned char *)KSEG1ADDR(MEM32(IDT434_REG_BASE + DEV2BASE));
136 data->chip.IO_ADDR_R = data->chip.IO_ADDR_W;
137
138 data->chip.ecc.mode = NAND_ECC_SOFT;
139 data->chip.chip_delay = 25;
140 data->chip.options |= NAND_NO_AUTOINCR;
141
142 data->chip.priv = &data;
143 data->mtd.priv = &data->chip;
144 data->mtd.owner = THIS_MODULE;
145
146 b = (int *) KSEG1ADDR(0x18010020);
147 printk("dev2base 0x%08x mask 0x%08x c 0x%08x tc 0x%08x\n", b[0],
148 b[1], b[2], b[3]);
149
150 platform_set_drvdata(pdev, data);
151
152 /* Why do we need to scan 4 times ? */
153 if (nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1) && nand_scan(&data->mtd, 1)) {
154 printk(KERN_INFO "RB500 nand device not found\n");
155 res = -ENXIO;
156 goto out;
157 }
158
159 printk(KERN_INFO "RB500 NAND\n");
160 add_mtd_partitions(&data->mtd, partition_info, 2);
161 data->init_ok = 1;
162
163 res = add_mtd_device(&data->mtd);
164 if (!res)
165 return res;
166
167 nand_release(&data->mtd);
168 out:
169 platform_set_drvdata(pdev, NULL);
170 iounmap(data->io_base);
171 kfree(data);
172 return res;
173 }
174
175 static int __devexit rbmips_remove(struct platform_device *pdev)
176 {
177 struct rb500_nand_info *data = platform_get_drvdata(pdev);
178
179 nand_release(&data->mtd);
180 iounmap(data->io_base);
181 kfree(data);
182
183 return 0;
184 }
185
186 static struct platform_driver rb500_nand_driver = {
187 .probe = rbmips_probe,
188 .remove = rbmips_remove,
189 .driver = {
190 .name = "rb500-nand",
191 .owner = THIS_MODULE,
192 },
193 };
194
195 static int __init rb500_nand_init(void)
196 {
197 int err;
198 err = platform_driver_register(&rb500_nand_driver);
199 return err;
200 }
201
202 static void __exit rb500_nand_exit(void)
203 {
204 platform_driver_unregister(&rb500_nand_driver);
205 }
206
207 module_init(rb500_nand_init);
208 module_exit(rb500_nand_exit);
209
210 MODULE_LICENSE("GPL");
211 MODULE_AUTHOR("David Goodenough, Felix Fietkau, Florian Fainelli");
212 MODULE_DESCRIPTION("RouterBOARD 500 NAND driver");