8846545e57d3a765466e57507c8d28b1d3df17ed
[openwrt.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 OpenWrt.org
7  *  Copyright (C) 2007 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
17  *  modify it under the terms of the GNU General Public License
18  *  as published by the Free Software Foundation; either version 2
19  *  of the License, or (at your option) any later version.
20  *
21  *  This program is distributed in the hope that it will be useful,
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *  GNU General Public License for more details.
25  *
26  *  You should have received a copy of the GNU General Public License
27  *  along with this program; if not, write to the
28  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29  *  Boston, MA  02110-1301, USA.
30  *
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36
37 #include <asm/bootinfo.h>
38 #include <asm/gpio.h>
39
40 #include <adm5120_defs.h>
41 #include <adm5120_irq.h>
42 #include <adm5120_nand.h>
43 #include <adm5120_board.h>
44 #include <adm5120_platform.h>
45 #include <adm5120_info.h>
46
47 #include <prom/routerboot.h>
48
49 #define RB1XX_NAND_CHIP_DELAY   25
50
51 #define RB150_NAND_BASE         0x1FC80000
52 #define RB150_NAND_SIZE         1
53
54 #define RB150_GPIO_NAND_READY   ADM5120_GPIO_PIN0
55 #define RB150_GPIO_NAND_NCE     ADM5120_GPIO_PIN1
56 #define RB150_GPIO_NAND_CLE     ADM5120_GPIO_P2L2
57 #define RB150_GPIO_NAND_ALE     ADM5120_GPIO_P3L2
58
59 #define RB150_NAND_DELAY        100
60
61 #define RB150_NAND_WRITE(v) \
62         writeb((v), (void __iomem *)KSEG1ADDR(RB150_NAND_BASE))
63
64 /*--------------------------------------------------------------------------*/
65
66 static struct adm5120_pci_irq rb1xx_pci_irqs[] __initdata = {
67         PCIIRQ(1, 0, 1, ADM5120_IRQ_PCI0),
68         PCIIRQ(2, 0, 1, ADM5120_IRQ_PCI1),
69         PCIIRQ(3, 0, 1, ADM5120_IRQ_PCI2)
70 };
71
72 static struct mtd_partition rb1xx_nor_parts[] = {
73         {
74                 .name   = "booter",
75                 .offset = 0,
76                 .size   = 64*1024,
77                 .mask_flags = MTD_WRITEABLE,
78         } , {
79                 .name   = "firmware",
80                 .offset = MTDPART_OFS_APPEND,
81                 .size   = MTDPART_SIZ_FULL,
82         }
83 };
84
85 static struct mtd_partition rb1xx_nand_parts[] = {
86         {
87                 .name   = "kernel",
88                 .offset = 0,
89                 .size   = 4 * 1024 * 1024,
90         } , {
91                 .name   = "rootfs",
92                 .offset = MTDPART_OFS_NXTBLK,
93                 .size   = MTDPART_SIZ_FULL
94         }
95 };
96
97 static struct platform_device *rb1xx_devices[] __initdata = {
98         &adm5120_flash0_device,
99         &adm5120_nand_device,
100 };
101
102 /*
103  * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
104  * will not be able to find the kernel that we load.  So set the oobinfo
105  * when creating the partitions
106  */
107 static struct nand_ecclayout rb1xx_nand_ecclayout = {
108         .eccbytes       = 6,
109         .eccpos         = { 8, 9, 10, 13, 14, 15 },
110         .oobavail       = 9,
111         .oobfree        = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
112 };
113
114 static struct resource rb150_nand_resource[] = {
115         [0] = {
116                 .start  = RB150_NAND_BASE,
117                 .end    = RB150_NAND_BASE + RB150_NAND_SIZE-1,
118                 .flags  = IORESOURCE_MEM,
119         },
120 };
121
122 static struct resource rb153_cf_resources[] = {
123         {
124                 .name   = "cf_membase",
125                 .start  = ADM5120_EXTIO1_BASE,
126                 .end    = ADM5120_EXTIO1_BASE + ADM5120_EXTIO1_SIZE-1 ,
127                 .flags  = IORESOURCE_MEM
128         }, {
129                 .name   = "cf_irq",
130                 .start  = ADM5120_IRQ_GPIO4,
131                 .end    = ADM5120_IRQ_GPIO4,
132                 .flags  = IORESOURCE_IRQ
133         }
134 };
135
136 static struct platform_device rb153_cf_device = {
137         .name           = "pata-rb153-cf",
138         .id             = -1,
139         .resource       = rb153_cf_resources,
140         .num_resources  = ARRAY_SIZE(rb153_cf_resources),
141 };
142
143 static struct platform_device *rb153_devices[] __initdata = {
144         &adm5120_flash0_device,
145         &adm5120_nand_device,
146         &rb153_cf_device,
147 };
148
149 #if 0
150 /*
151  * RB1xx boards have bad network performance with the default VLAN matrixes.
152  * Disable it while the ethernet driver gets fixed.
153  */
154 static unsigned char rb11x_vlans[6] __initdata = {
155         /* FIXME: untested */
156         0x41, 0x00, 0x00, 0x00, 0x00, 0x00
157 };
158
159 static unsigned char rb133_vlans[6] __initdata = {
160         /* FIXME: untested */
161         0x44, 0x42, 0x41, 0x00, 0x00, 0x00
162 };
163
164 static unsigned char rb133c_vlans[6] __initdata = {
165         /* FIXME: untested */
166         0x44, 0x00, 0x00, 0x00, 0x00, 0x00
167 };
168
169 static unsigned char rb15x_vlans[6] __initdata = {
170         /* FIXME: untested */
171         0x41, 0x42, 0x44, 0x48, 0x50, 0x00
172 };
173
174 static unsigned char rb192_vlans[6] __initdata = {
175         /* FIXME: untested */
176         0x41, 0x50, 0x48, 0x44, 0x42, 0x00
177 };
178 #else
179 static unsigned char rb_vlans[6] __initdata = {
180         0x7F, 0x00, 0x00, 0x00, 0x00, 0x00
181 };
182 #define rb11x_vlans     rb_vlans
183 #define rb133_vlans     rb_vlans
184 #define rb133c_vlans    rb_vlans
185 #define rb15x_vlans     rb_vlans
186 #define rb192_vlans     rb_vlans
187 #endif
188
189 /*--------------------------------------------------------------------------*/
190
191 static int rb150_nand_ready(struct mtd_info *mtd)
192 {
193         return gpio_get_value(RB150_GPIO_NAND_READY);
194 }
195
196 static void rb150_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
197                 unsigned int ctrl)
198 {
199         if (ctrl & NAND_CTRL_CHANGE) {
200                 gpio_set_value(RB150_GPIO_NAND_CLE, (ctrl & NAND_CLE) ? 1 : 0);
201                 gpio_set_value(RB150_GPIO_NAND_ALE, (ctrl & NAND_ALE) ? 1 : 0);
202                 gpio_set_value(RB150_GPIO_NAND_NCE, (ctrl & NAND_NCE) ? 0 : 1);
203         }
204
205         udelay(RB150_NAND_DELAY);
206
207         if (cmd != NAND_CMD_NONE)
208                 RB150_NAND_WRITE(cmd);
209 }
210
211 /*--------------------------------------------------------------------------*/
212
213 static void __init rb1xx_mac_setup(void)
214 {
215         int i, j;
216
217         if (!rb_hs.mac_base)
218                 return;
219
220         for (i = 0; i < 6; i++) {
221                 for (j = 0; j < 5; j++)
222                         adm5120_eth_macs[i][j] = rb_hs.mac_base[j];
223                 adm5120_eth_macs[i][5] = rb_hs.mac_base[5]+i;
224         }
225 }
226
227 static void __init rb1xx_flash_setup(void)
228 {
229         /* setup data for flash0 device */
230         adm5120_flash0_data.nr_parts = ARRAY_SIZE(rb1xx_nor_parts);
231         adm5120_flash0_data.parts = rb1xx_nor_parts;
232
233         /* setup data for NAND device */
234         adm5120_nand_data.chip.nr_chips = 1;
235         adm5120_nand_data.chip.nr_partitions = ARRAY_SIZE(rb1xx_nand_parts);
236         adm5120_nand_data.chip.partitions = rb1xx_nand_parts;
237         adm5120_nand_data.chip.ecclayout = &rb1xx_nand_ecclayout;
238         adm5120_nand_data.chip.chip_delay = RB1XX_NAND_CHIP_DELAY;
239         adm5120_nand_data.chip.options = NAND_NO_AUTOINCR;
240 }
241
242 static void __init rb1xx_setup(void)
243 {
244         /* enable NAND flash interface */
245         adm5120_nand_enable();
246
247         /* initialize NAND chip */
248         adm5120_nand_set_spn(1);
249         adm5120_nand_set_wpn(0);
250
251         rb1xx_flash_setup();
252         rb1xx_mac_setup();
253 }
254
255 static void __init rb150_setup(void)
256 {
257         /* setup GPIO pins for NAND flash chip */
258         gpio_request(RB150_GPIO_NAND_READY, "nand-ready");
259         gpio_direction_input(RB150_GPIO_NAND_READY);
260         gpio_request(RB150_GPIO_NAND_NCE, "nand-nce");
261         gpio_direction_output(RB150_GPIO_NAND_NCE, 1);
262         gpio_request(RB150_GPIO_NAND_CLE, "nand-cle");
263         gpio_direction_output(RB150_GPIO_NAND_CLE, 0);
264         gpio_request(RB150_GPIO_NAND_ALE, "nand-ale");
265         gpio_direction_output(RB150_GPIO_NAND_ALE, 0);
266
267         adm5120_nand_device.num_resources = ARRAY_SIZE(rb150_nand_resource);
268         adm5120_nand_device.resource = rb150_nand_resource;
269         adm5120_nand_data.ctrl.cmd_ctrl = rb150_nand_cmd_ctrl;
270         adm5120_nand_data.ctrl.dev_ready = rb150_nand_ready;
271
272         adm5120_flash0_data.window_size = 512*1024;
273
274         rb1xx_flash_setup();
275         rb1xx_mac_setup();
276 }
277
278 static void __init rb153_setup(void)
279 {
280         /* enable CSX1:INTX1 on GPIO[3:4] for the CF slot */
281         adm5120_gpio_csx1_enable();
282         /* enable the wait state pin GPIO[0] for external I/O control */
283         adm5120_gpio_ew_enable();
284
285         rb1xx_setup();
286 }
287
288 /*--------------------------------------------------------------------------*/
289
290 ADM5120_BOARD_START(RB_111, "Mikrotik RouterBOARD 111")
291         .board_setup    = rb1xx_setup,
292         .eth_num_ports  = 1,
293         .eth_vlans      = rb11x_vlans,
294         .num_devices    = ARRAY_SIZE(rb1xx_devices),
295         .devices        = rb1xx_devices,
296         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
297         .pci_irq_map    = rb1xx_pci_irqs,
298 ADM5120_BOARD_END
299
300 ADM5120_BOARD_START(RB_112, "Mikrotik RouterBOARD 112")
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_133, "Mikrotik RouterBOARD 133")
311         .board_setup    = rb1xx_setup,
312         .eth_num_ports  = 3,
313         .eth_vlans      = rb133_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_133C, "Mikrotik RouterBOARD 133C")
321         .board_setup    = rb1xx_setup,
322         .eth_num_ports  = 1,
323         .eth_vlans      = rb133c_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_150, "Mikrotik RouterBOARD 150")
331         .board_setup    = rb150_setup,
332         .eth_num_ports  = 5,
333         .eth_vlans      = rb15x_vlans,
334         .num_devices    = ARRAY_SIZE(rb1xx_devices),
335         .devices        = rb1xx_devices,
336 ADM5120_BOARD_END
337
338 ADM5120_BOARD_START(RB_153, "Mikrotik RouterBOARD 153")
339         .board_setup    = rb153_setup,
340         .eth_num_ports  = 5,
341         .eth_vlans      = rb15x_vlans,
342         .num_devices    = ARRAY_SIZE(rb153_devices),
343         .devices        = rb153_devices,
344         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
345         .pci_irq_map    = rb1xx_pci_irqs,
346 ADM5120_BOARD_END
347
348 ADM5120_BOARD_START(RB_192, "Mikrotik RouterBOARD 192")
349         .board_setup    = rb1xx_setup,
350         .eth_num_ports  = 5,
351         .eth_vlans      = rb192_vlans,
352         .num_devices    = ARRAY_SIZE(rb1xx_devices),
353         .devices        = rb1xx_devices,
354         .pci_nr_irqs    = ARRAY_SIZE(rb1xx_pci_irqs),
355         .pci_irq_map    = rb1xx_pci_irqs,
356 ADM5120_BOARD_END