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