add missing buttons device for RB-1xx boards, thanks to Christophe Lucas
[librecmc/librecmc.git] / target / linux / adm5120 / files / arch / mips / adm5120 / boards / mikrotik.c
1 /*
2  *  $Id$
3  *
4  *  Mikrotik RouterBOARD 1xx series
5  *
6  *  Copyright (C) 2007-2008 OpenWrt.org
7  *  Copyright (C) 2007-2008 Gabor Juhos <juhosg at openwrt.org>
8  *
9  *  NAND initialization code was based on a driver for Linux 2.6.19+ which
10  *  was derived from the driver for Linux 2.4.xx published by Mikrotik for
11  *  their RouterBoard 1xx and 5xx series boards.
12  *    Copyright (C) 2007 David Goodenough <david.goodenough@linkchoose.co.uk>
13  *    Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
14  *    The original Mikrotik code seems not to have a license.
15  *
16  *  This program is free software; you can redistribute it and/or modify it
17  *  under the terms of the GNU General Public License version 2 as published
18  *  by the Free Software Foundation.
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25
26 #include <asm/bootinfo.h>
27 #include <asm/gpio.h>
28
29 #include <adm5120_defs.h>
30 #include <adm5120_irq.h>
31 #include <adm5120_nand.h>
32 #include <adm5120_board.h>
33 #include <adm5120_platform.h>
34 #include <adm5120_info.h>
35
36 #include <prom/routerboot.h>
37
38 #define RB1XX_NAND_CHIP_DELAY   25
39
40 #define RB150_NAND_BASE         0x1FC80000
41 #define RB150_NAND_SIZE         1
42
43 #define RB150_GPIO_NAND_READY   ADM5120_GPIO_PIN0
44 #define RB150_GPIO_NAND_NCE     ADM5120_GPIO_PIN1
45 #define RB150_GPIO_NAND_CLE     ADM5120_GPIO_P2L2
46 #define RB150_GPIO_NAND_ALE     ADM5120_GPIO_P3L2
47
48 #define RB150_NAND_DELAY        100
49
50 #define RB150_NAND_WRITE(v) \
51         writeb((v), (void __iomem *)KSEG1ADDR(RB150_NAND_BASE))
52
53 /*--------------------------------------------------------------------------*/
54
55 static struct adm5120_pci_irq rb1xx_pci_irqs[] __initdata = {
56         PCIIRQ(1, 0, 1, ADM5120_IRQ_PCI0),
57         PCIIRQ(2, 0, 1, ADM5120_IRQ_PCI1),
58         PCIIRQ(3, 0, 1, ADM5120_IRQ_PCI2)
59 };
60
61 static struct mtd_partition rb1xx_nor_parts[] = {
62         {
63                 .name   = "booter",
64                 .offset = 0,
65                 .size   = 64*1024,
66                 .mask_flags = MTD_WRITEABLE,
67         } , {
68                 .name   = "firmware",
69                 .offset = MTDPART_OFS_APPEND,
70                 .size   = MTDPART_SIZ_FULL,
71         }
72 };
73
74 static struct mtd_partition rb1xx_nand_parts[] = {
75         {
76                 .name   = "kernel",
77                 .offset = 0,
78                 .size   = 4 * 1024 * 1024,
79         } , {
80                 .name   = "rootfs",
81                 .offset = MTDPART_OFS_NXTBLK,
82                 .size   = MTDPART_SIZ_FULL
83         }
84 };
85
86 static struct platform_device *rb1xx_devices[] __initdata = {
87         &adm5120_flash0_device,
88         &adm5120_nand_device,
89         &adm5120_buttons_device,
90 };
91
92 /*
93  * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
94  * will not be able to find the kernel that we load.  So set the oobinfo
95  * when creating the partitions
96  */
97 static struct nand_ecclayout rb1xx_nand_ecclayout = {
98         .eccbytes       = 6,
99         .eccpos         = { 8, 9, 10, 13, 14, 15 },
100         .oobavail       = 9,
101         .oobfree        = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
102 };
103
104 static struct resource rb150_nand_resource[] = {
105         [0] = {
106                 .start  = RB150_NAND_BASE,
107                 .end    = RB150_NAND_BASE + RB150_NAND_SIZE-1,
108                 .flags  = IORESOURCE_MEM,
109         },
110 };
111
112 static struct resource rb153_cf_resources[] = {
113         {
114                 .name   = "cf_membase",
115                 .start  = ADM5120_EXTIO1_BASE,
116                 .end    = ADM5120_EXTIO1_BASE + ADM5120_EXTIO1_SIZE-1 ,
117                 .flags  = IORESOURCE_MEM
118         }, {
119                 .name   = "cf_irq",
120                 .start  = ADM5120_IRQ_GPIO4,
121                 .end    = ADM5120_IRQ_GPIO4,
122                 .flags  = IORESOURCE_IRQ
123         }
124 };
125
126 static struct platform_device rb153_cf_device = {
127         .name           = "pata-rb153-cf",
128         .id             = -1,
129         .resource       = rb153_cf_resources,
130         .num_resources  = ARRAY_SIZE(rb153_cf_resources),
131 };
132
133 static struct platform_device *rb153_devices[] __initdata = {
134         &adm5120_flash0_device,
135         &adm5120_nand_device,
136         &adm5120_buttons_device,
137         &rb153_cf_device,
138 };
139
140 #if 0
141 /*
142  * RB1xx boards have bad network performance with the default VLAN matrixes.
143  * Disable it while the ethernet driver gets fixed.
144  */
145 static unsigned char rb11x_vlans[6] __initdata = {
146         /* FIXME: untested */
147         0x41, 0x00, 0x00, 0x00, 0x00, 0x00
148 };
149
150 static unsigned char rb133_vlans[6] __initdata = {
151         /* FIXME: untested */
152         0x44, 0x42, 0x41, 0x00, 0x00, 0x00
153 };
154
155 static unsigned char rb133c_vlans[6] __initdata = {
156         /* FIXME: untested */
157         0x44, 0x00, 0x00, 0x00, 0x00, 0x00
158 };
159
160 static unsigned char rb15x_vlans[6] __initdata = {
161         /* FIXME: untested */
162         0x41, 0x42, 0x44, 0x48, 0x50, 0x00
163 };
164
165 static unsigned char rb192_vlans[6] __initdata = {
166         /* FIXME: untested */
167         0x41, 0x50, 0x48, 0x44, 0x42, 0x00
168 };
169 #else
170 static unsigned char rb_vlans[6] __initdata = {
171         0x7F, 0x00, 0x00, 0x00, 0x00, 0x00
172 };
173 #define rb11x_vlans     rb_vlans
174 #define rb133_vlans     rb_vlans
175 #define rb133c_vlans    rb_vlans
176 #define rb15x_vlans     rb_vlans
177 #define rb192_vlans     rb_vlans
178 #endif
179
180 /*--------------------------------------------------------------------------*/
181
182 static int rb150_nand_ready(struct mtd_info *mtd)
183 {
184         return gpio_get_value(RB150_GPIO_NAND_READY);
185 }
186
187 static void rb150_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
188                 unsigned int ctrl)
189 {
190         if (ctrl & NAND_CTRL_CHANGE) {
191                 gpio_set_value(RB150_GPIO_NAND_CLE, (ctrl & NAND_CLE) ? 1 : 0);
192                 gpio_set_value(RB150_GPIO_NAND_ALE, (ctrl & NAND_ALE) ? 1 : 0);
193                 gpio_set_value(RB150_GPIO_NAND_NCE, (ctrl & NAND_NCE) ? 0 : 1);
194         }
195
196         udelay(RB150_NAND_DELAY);
197
198         if (cmd != NAND_CMD_NONE)
199                 RB150_NAND_WRITE(cmd);
200 }
201
202 /*--------------------------------------------------------------------------*/
203
204 static void __init rb1xx_mac_setup(void)
205 {
206         int i, j;
207
208         if (!rb_hs.mac_base)
209                 return;
210
211         for (i = 0; i < 6; i++) {
212                 for (j = 0; j < 5; j++)
213                         adm5120_eth_macs[i][j] = rb_hs.mac_base[j];
214                 adm5120_eth_macs[i][5] = rb_hs.mac_base[5]+i;
215         }
216 }
217
218 static int rb1xx_nand_fixup(struct mtd_info *mtd)
219 {
220         struct nand_chip *chip = mtd->priv;
221
222         if (mtd->writesize == 512)
223                 chip->ecc.layout = &rb1xx_nand_ecclayout;
224
225         return 0;
226 }
227
228 static void __init rb1xx_flash_setup(void)
229 {
230         /* setup data for flash0 device */
231         adm5120_flash0_data.nr_parts = ARRAY_SIZE(rb1xx_nor_parts);
232         adm5120_flash0_data.parts = rb1xx_nor_parts;
233
234         /* setup data for NAND device */
235         adm5120_nand_data.chip.nr_chips = 1;
236         adm5120_nand_data.chip.nr_partitions = ARRAY_SIZE(rb1xx_nand_parts);
237         adm5120_nand_data.chip.partitions = rb1xx_nand_parts;
238         adm5120_nand_data.chip.chip_delay = RB1XX_NAND_CHIP_DELAY;
239         adm5120_nand_data.chip.options = NAND_NO_AUTOINCR;
240
241         adm5120_nand_data.chip.chip_fixup = &rb1xx_nand_fixup;
242 }
243
244 static void __init rb1xx_setup(void)
245 {
246         /* enable NAND flash interface */
247         adm5120_nand_enable();
248
249         /* initialize NAND chip */
250         adm5120_nand_set_spn(1);
251         adm5120_nand_set_wpn(0);
252
253         adm5120_buttons_data.nbuttons = 1;
254         adm5120_buttons[0].desc = "reset button";
255         adm5120_buttons[0].gpio = ADM5120_GPIO_PIN7;
256
257         rb1xx_flash_setup();
258         rb1xx_mac_setup();
259 }
260
261 static void __init rb150_setup(void)
262 {
263         /* setup GPIO pins for NAND flash chip */
264         gpio_request(RB150_GPIO_NAND_READY, "nand-ready");
265         gpio_direction_input(RB150_GPIO_NAND_READY);
266         gpio_request(RB150_GPIO_NAND_NCE, "nand-nce");
267         gpio_direction_output(RB150_GPIO_NAND_NCE, 1);
268         gpio_request(RB150_GPIO_NAND_CLE, "nand-cle");
269         gpio_direction_output(RB150_GPIO_NAND_CLE, 0);
270         gpio_request(RB150_GPIO_NAND_ALE, "nand-ale");
271         gpio_direction_output(RB150_GPIO_NAND_ALE, 0);
272
273         adm5120_nand_device.num_resources = ARRAY_SIZE(rb150_nand_resource);
274         adm5120_nand_device.resource = rb150_nand_resource;
275         adm5120_nand_data.ctrl.cmd_ctrl = rb150_nand_cmd_ctrl;
276         adm5120_nand_data.ctrl.dev_ready = rb150_nand_ready;
277
278         adm5120_buttons_data.nbuttons = 1;
279         adm5120_buttons[0].desc = "reset button";
280         adm5120_buttons[0].gpio = ADM5120_GPIO_PIN1; /* FIXME: valid? */
281
282         adm5120_flash0_data.window_size = 512*1024;
283
284         rb1xx_flash_setup();
285         rb1xx_mac_setup();
286 }
287
288 static void __init rb153_setup(void)
289 {
290         /* enable CSX1:INTX1 on GPIO[3:4] for the CF slot */
291         adm5120_gpio_csx1_enable();
292         /* enable the wait state pin GPIO[0] for external I/O control */
293         adm5120_gpio_ew_enable();
294
295         rb1xx_setup();
296 }
297
298 /*--------------------------------------------------------------------------*/
299
300 ADM5120_BOARD_START(RB_111, "Mikrotik RouterBOARD 111")
301         .board_setup    = rb1xx_setup,
302         .eth_num_ports  = 1,
303         .eth_vlans      = rb11x_vlans,
304         .num_devices    = ARRAY_SIZE(rb1xx_devices),
305         .devices        = rb1xx_devices,
306         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
307         .pci_irq_map    = rb1xx_pci_irqs,
308 ADM5120_BOARD_END
309
310 ADM5120_BOARD_START(RB_112, "Mikrotik RouterBOARD 112")
311         .board_setup    = rb1xx_setup,
312         .eth_num_ports  = 1,
313         .eth_vlans      = rb11x_vlans,
314         .num_devices    = ARRAY_SIZE(rb1xx_devices),
315         .devices        = rb1xx_devices,
316         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
317         .pci_irq_map    = rb1xx_pci_irqs,
318 ADM5120_BOARD_END
319
320 ADM5120_BOARD_START(RB_133, "Mikrotik RouterBOARD 133")
321         .board_setup    = rb1xx_setup,
322         .eth_num_ports  = 3,
323         .eth_vlans      = rb133_vlans,
324         .num_devices    = ARRAY_SIZE(rb1xx_devices),
325         .devices        = rb1xx_devices,
326         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
327         .pci_irq_map    = rb1xx_pci_irqs,
328 ADM5120_BOARD_END
329
330 ADM5120_BOARD_START(RB_133C, "Mikrotik RouterBOARD 133C")
331         .board_setup    = rb1xx_setup,
332         .eth_num_ports  = 1,
333         .eth_vlans      = rb133c_vlans,
334         .num_devices    = ARRAY_SIZE(rb1xx_devices),
335         .devices        = rb1xx_devices,
336         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
337         .pci_irq_map    = rb1xx_pci_irqs,
338 ADM5120_BOARD_END
339
340 ADM5120_BOARD_START(RB_150, "Mikrotik RouterBOARD 150")
341         .board_setup    = rb150_setup,
342         .eth_num_ports  = 5,
343         .eth_vlans      = rb15x_vlans,
344         .num_devices    = ARRAY_SIZE(rb1xx_devices),
345         .devices        = rb1xx_devices,
346 ADM5120_BOARD_END
347
348 ADM5120_BOARD_START(RB_153, "Mikrotik RouterBOARD 153")
349         .board_setup    = rb153_setup,
350         .eth_num_ports  = 5,
351         .eth_vlans      = rb15x_vlans,
352         .num_devices    = ARRAY_SIZE(rb153_devices),
353         .devices        = rb153_devices,
354         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
355         .pci_irq_map    = rb1xx_pci_irqs,
356 ADM5120_BOARD_END
357
358 ADM5120_BOARD_START(RB_192, "Mikrotik RouterBOARD 192")
359         .board_setup    = rb1xx_setup,
360         .eth_num_ports  = 5,
361         .eth_vlans      = rb192_vlans,
362         .num_devices    = ARRAY_SIZE(rb1xx_devices),
363         .devices        = rb1xx_devices,
364         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
365         .pci_irq_map    = rb1xx_pci_irqs,
366 ADM5120_BOARD_END