fix flash driver, it should work on RB150 as well
[openwrt/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_cf.h>
46
47 #define RB1XX_NAND_CHIP_DELAY 25
48
49 #define RB150_NAND_BASE 0x1FC80000
50 #define RB150_NAND_SIZE 1
51
52 #define RB150_GPIO_NAND_READY ADM5120_GPIO_PIN0
53 #define RB150_GPIO_NAND_NCE ADM5120_GPIO_PIN1
54 #define RB150_GPIO_NAND_CLE ADM5120_GPIO_P2L2
55 #define RB150_GPIO_NAND_ALE ADM5120_GPIO_P3L2
56
57 #define RB150_NAND_DELAY 100
58
59 #define RB150_NAND_WRITE(v) \
60 writeb((v), (void __iomem *)KSEG1ADDR(RB150_NAND_BASE))
61
62 /*--------------------------------------------------------------------------*/
63
64 static struct adm5120_pci_irq rb1xx_pci_irqs[] __initdata = {
65 PCIIRQ(1, 0, 1, ADM5120_IRQ_PCI0),
66 PCIIRQ(2, 0, 1, ADM5120_IRQ_PCI1),
67 PCIIRQ(3, 0, 1, ADM5120_IRQ_PCI2)
68 };
69
70 static struct mtd_partition rb1xx_nor_parts[] = {
71 {
72 .name = "booter",
73 .offset = 0,
74 .size = 64*1024,
75 .mask_flags = MTD_WRITEABLE,
76 } , {
77 .name = "firmware",
78 .offset = MTDPART_OFS_APPEND,
79 .size = MTDPART_SIZ_FULL,
80 }
81 };
82
83 static struct mtd_partition rb1xx_nand_parts[] = {
84 {
85 .name = "kernel",
86 .offset = 0,
87 .size = 4 * 1024 * 1024,
88 } , {
89 .name = "rootfs",
90 .offset = MTDPART_OFS_NXTBLK,
91 .size = MTDPART_SIZ_FULL
92 }
93 };
94
95 static struct platform_device *rb1xx_devices[] __initdata = {
96 &adm5120_flash0_device,
97 &adm5120_nand_device,
98 };
99
100 /*
101 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
102 * will not be able to find the kernel that we load. So set the oobinfo
103 * when creating the partitions
104 */
105 static struct nand_ecclayout rb1xx_nand_ecclayout = {
106 .eccbytes = 6,
107 .eccpos = { 8, 9, 10, 13, 14, 15 },
108 .oobavail = 9,
109 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
110 };
111
112 static struct resource rb150_nand_resource[] = {
113 [0] = {
114 .start = RB150_NAND_BASE,
115 .end = RB150_NAND_BASE + RB150_NAND_SIZE-1,
116 .flags = IORESOURCE_MEM,
117 },
118 };
119
120 #if 0
121 /*
122 * RB1xx boards have bad network performance with the default VLAN matrixes.
123 * Disable it while the ethernet driver gets fixed.
124 */
125 static unsigned char rb11x_vlans[6] __initdata = {
126 /* FIXME: untested */
127 0x41, 0x00, 0x00, 0x00, 0x00, 0x00
128 };
129
130 static unsigned char rb133_vlans[6] __initdata = {
131 /* FIXME: untested */
132 0x44, 0x42, 0x41, 0x00, 0x00, 0x00
133 };
134
135 static unsigned char rb133c_vlans[6] __initdata = {
136 /* FIXME: untested */
137 0x44, 0x00, 0x00, 0x00, 0x00, 0x00
138 };
139
140 static unsigned char rb15x_vlans[6] __initdata = {
141 /* FIXME: untested */
142 0x41, 0x42, 0x44, 0x48, 0x50, 0x00
143 };
144
145 static unsigned char rb192_vlans[6] __initdata = {
146 /* FIXME: untested */
147 0x41, 0x50, 0x48, 0x44, 0x42, 0x00
148 };
149 #else
150 static unsigned char rb_vlans[6] __initdata = {
151 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00
152 };
153 #define rb11x_vlans rb_vlans
154 #define rb133_vlans rb_vlans
155 #define rb133c_vlans rb_vlans
156 #define rb15x_vlans rb_vlans
157 #define rb192_vlans rb_vlans
158 #endif
159
160 /*--------------------------------------------------------------------------*/
161
162 static int rb150_nand_ready(struct mtd_info *mtd)
163 {
164 return gpio_get_value(RB150_GPIO_NAND_READY);
165 }
166
167 static void rb150_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
168 unsigned int ctrl)
169 {
170 if (ctrl & NAND_CTRL_CHANGE) {
171 gpio_set_value(RB150_GPIO_NAND_CLE, (ctrl & NAND_CLE) ? 1 : 0);
172 gpio_set_value(RB150_GPIO_NAND_ALE, (ctrl & NAND_ALE) ? 1 : 0);
173 gpio_set_value(RB150_GPIO_NAND_NCE, (ctrl & NAND_NCE) ? 0 : 1);
174 }
175
176 udelay(RB150_NAND_DELAY);
177
178 if (cmd != NAND_CMD_NONE)
179 RB150_NAND_WRITE(cmd);
180 }
181
182 /*--------------------------------------------------------------------------*/
183 static struct resource cf_slot0_res[] = {
184 {
185 .name = "cf_membase",
186 .start = ADM5120_EXTIO0_BASE,
187 .end = ADM5120_EXTIO0_BASE + ADM5120_MPMC_SIZE-1 ,
188 .flags = IORESOURCE_MEM
189 }, {
190 .name = "cf_irq",
191 .start = ADM5120_IRQ_GPIO4, /* 5 */
192 .end = ADM5120_IRQ_GPIO4,
193 .flags = IORESOURCE_IRQ
194 }
195 };
196
197 static struct cf_device cf_slot0_data = {
198 .gpio_pin = 4
199 };
200
201 static struct platform_device cf_slot0 = {
202 .id = 0,
203 .name = "rb153-cf",
204 .dev.platform_data = &cf_slot0_data,
205 .resource = cf_slot0_res,
206 .num_resources = ARRAY_SIZE(cf_slot0_res),
207 };
208
209 static struct platform_device *rb153_devices[] __initdata = {
210 &adm5120_flash0_device,
211 &adm5120_nand_device,
212 &cf_slot0,
213 };
214
215 static void __init rb1xx_mac_setup(void)
216 {
217 /* TODO */
218 }
219
220 static void __init rb1xx_flash_setup(void)
221 {
222 /* setup data for flash0 device */
223 adm5120_flash0_data.nr_parts = ARRAY_SIZE(rb1xx_nor_parts);
224 adm5120_flash0_data.parts = rb1xx_nor_parts;
225
226 /* setup data for NAND device */
227 adm5120_nand_data.chip.nr_chips = 1;
228 adm5120_nand_data.chip.nr_partitions = ARRAY_SIZE(rb1xx_nand_parts);
229 adm5120_nand_data.chip.partitions = rb1xx_nand_parts;
230 adm5120_nand_data.chip.ecclayout = &rb1xx_nand_ecclayout;
231 adm5120_nand_data.chip.chip_delay = RB1XX_NAND_CHIP_DELAY;
232 adm5120_nand_data.chip.options = NAND_NO_AUTOINCR;
233 }
234
235 static void __init rb1xx_setup(void)
236 {
237 /* enable NAND flash interface */
238 adm5120_nand_enable();
239
240 /* initialize NAND chip */
241 adm5120_nand_set_spn(1);
242 adm5120_nand_set_wpn(0);
243
244 rb1xx_flash_setup();
245 rb1xx_mac_setup();
246 }
247
248 static void __init rb150_setup(void)
249 {
250 /* setup GPIO pins for NAND flash chip */
251 gpio_request(RB150_GPIO_NAND_READY, "nand-ready");
252 gpio_direction_input(RB150_GPIO_NAND_READY);
253 gpio_request(RB150_GPIO_NAND_NCE, "nand-nce");
254 gpio_direction_output(RB150_GPIO_NAND_NCE, 1);
255 gpio_request(RB150_GPIO_NAND_CLE, "nand-cle");
256 gpio_direction_output(RB150_GPIO_NAND_CLE, 0);
257 gpio_request(RB150_GPIO_NAND_ALE, "nand-ale");
258 gpio_direction_output(RB150_GPIO_NAND_ALE, 0);
259
260 adm5120_nand_device.num_resources = ARRAY_SIZE(rb150_nand_resource);
261 adm5120_nand_device.resource = rb150_nand_resource;
262 adm5120_nand_data.ctrl.cmd_ctrl = rb150_nand_cmd_ctrl;
263 adm5120_nand_data.ctrl.dev_ready = rb150_nand_ready;
264
265 adm5120_flash0_data.window_size = 512*1024;
266
267 rb1xx_flash_setup();
268 rb1xx_mac_setup();
269 }
270
271 /*--------------------------------------------------------------------------*/
272
273 static struct adm5120_board rb111_board __initdata = {
274 .mach_type = MACH_ADM5120_RB_111,
275 .name = "Mikrotik RouterBOARD 111",
276 .board_setup = rb1xx_setup,
277 .eth_num_ports = 1,
278 .eth_vlans = rb11x_vlans,
279 .num_devices = ARRAY_SIZE(rb1xx_devices),
280 .devices = rb1xx_devices,
281 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
282 .pci_irq_map = rb1xx_pci_irqs,
283 };
284
285 static struct adm5120_board rb112_board __initdata = {
286 .mach_type = MACH_ADM5120_RB_112,
287 .name = "Mikrotik RouterBOARD 112",
288 .board_setup = rb1xx_setup,
289 .eth_num_ports = 1,
290 .eth_vlans = rb11x_vlans,
291 .num_devices = ARRAY_SIZE(rb1xx_devices),
292 .devices = rb1xx_devices,
293 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
294 .pci_irq_map = rb1xx_pci_irqs,
295 };
296
297 static struct adm5120_board rb133_board __initdata = {
298 .mach_type = MACH_ADM5120_RB_133,
299 .name = "Mikrotik RouterBOARD 133",
300 .board_setup = rb1xx_setup,
301 .eth_num_ports = 3,
302 .eth_vlans = rb133_vlans,
303 .num_devices = ARRAY_SIZE(rb1xx_devices),
304 .devices = rb1xx_devices,
305 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
306 .pci_irq_map = rb1xx_pci_irqs,
307 };
308
309 static struct adm5120_board rb133c_board __initdata = {
310 .mach_type = MACH_ADM5120_RB_133C,
311 .name = "Mikrotik RouterBOARD 133C",
312 .board_setup = rb1xx_setup,
313 .eth_num_ports = 1,
314 .eth_vlans = rb133c_vlans,
315 .num_devices = ARRAY_SIZE(rb1xx_devices),
316 .devices = rb1xx_devices,
317 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
318 .pci_irq_map = rb1xx_pci_irqs,
319 };
320
321 static struct adm5120_board rb150_board __initdata = {
322 .mach_type = MACH_ADM5120_RB_150,
323 .name = "Mikrotik RouterBOARD 150",
324 .board_setup = rb150_setup,
325 .eth_num_ports = 5,
326 .eth_vlans = rb15x_vlans,
327 .num_devices = ARRAY_SIZE(rb1xx_devices),
328 .devices = rb1xx_devices,
329 };
330
331 static struct adm5120_board rb153_board __initdata = {
332 .mach_type = MACH_ADM5120_RB_153,
333 .name = "Mikrotik RouterBOARD 153",
334 .board_setup = rb1xx_setup,
335 .eth_num_ports = 5,
336 .eth_vlans = rb15x_vlans,
337 .num_devices = ARRAY_SIZE(rb153_devices),
338 .devices = rb153_devices,
339 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
340 .pci_irq_map = rb1xx_pci_irqs,
341 };
342
343 static struct adm5120_board rb192_board __initdata = {
344 .mach_type = MACH_ADM5120_RB_192,
345 .name = "Mikrotik RouterBOARD 192",
346 .board_setup = rb1xx_setup,
347 .eth_num_ports = 5,
348 .eth_vlans = rb192_vlans,
349 .num_devices = ARRAY_SIZE(rb1xx_devices),
350 .devices = rb1xx_devices,
351 .pci_nr_irqs = ARRAY_SIZE(rb1xx_pci_irqs),
352 .pci_irq_map = rb1xx_pci_irqs,
353 };
354
355 static int __init register_boards(void)
356 {
357 adm5120_board_register(&rb111_board);
358 adm5120_board_register(&rb112_board);
359 adm5120_board_register(&rb133_board);
360 adm5120_board_register(&rb133c_board);
361 adm5120_board_register(&rb150_board);
362 adm5120_board_register(&rb153_board);
363 adm5120_board_register(&rb192_board);
364 return 0;
365 }
366
367 pure_initcall(register_boards);