7d77e9ce6b7c8db8806fde59ac5d3876d6667c6b
[openwrt/svn-archive/archive.git] / target / linux / ifxmips / files / arch / mips / ifxmips / board.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
17 */
18
19 #include <linux/autoconf.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/mtd/physmap.h>
25 #include <linux/kernel.h>
26 #include <linux/reboot.h>
27 #include <linux/platform_device.h>
28 #include <linux/leds.h>
29 #include <linux/etherdevice.h>
30 #include <linux/reboot.h>
31 #include <linux/time.h>
32 #include <linux/io.h>
33 #include <linux/gpio.h>
34
35 #include <asm/bootinfo.h>
36 #include <asm/irq.h>
37
38 #include <ifxmips.h>
39 #include <ifxmips_irq.h>
40
41 #define MAX_BOARD_NAME_LEN 32
42 #define MAX_IFXMIPS_DEVS 9
43
44 #define SYSTEM_DANUBE "Danube"
45 #define SYSTEM_DANUBE_CHIPID1 0x00129083
46 #define SYSTEM_DANUBE_CHIPID2 0x0012B083
47
48 #define SYSTEM_TWINPASS "Twinpass"
49 #define SYSTEM_TWINPASS_CHIPID 0x0012D083
50
51 enum {
52 EASY50712,
53 EASY4010,
54 ARV4519,
55 };
56
57 extern int ifxmips_pci_external_clock;
58
59 static unsigned int chiprev;
60 static int cmdline_mac;
61 char board_name[MAX_BOARD_NAME_LEN + 1] = { 0 };
62
63 struct ifxmips_board {
64 int type;
65 char name[32];
66 unsigned int system_type;
67 struct platform_device **devs;
68 struct resource reset_resource;
69 struct resource gpiodev_resource;
70 struct gpio_led *ifxmips_leds;
71 struct gpio_led *gpio_leds;
72 int pci_external_clock;
73 int num_devs;
74 };
75
76 DEFINE_SPINLOCK(ebu_lock);
77 EXPORT_SYMBOL_GPL(ebu_lock);
78
79 static unsigned char ifxmips_ethaddr[6];
80 static int ifxmips_brn;
81
82 static struct gpio_led_platform_data ifxmips_led_data;
83
84 static struct platform_device ifxmips_led = {
85 .id = 0,
86 .name = "ifxmips_led",
87 .dev = {
88 .platform_data = (void *) &ifxmips_led_data,
89 }
90 };
91
92 static struct platform_device ifxmips_gpio = {
93 .id = 0,
94 .name = "ifxmips_gpio",
95 .num_resources = 1,
96 };
97
98 static struct platform_device ifxmips_mii = {
99 .id = 0,
100 .name = "ifxmips_mii0",
101 .dev = {
102 .platform_data = ifxmips_ethaddr,
103 }
104 };
105
106 static struct platform_device ifxmips_wdt = {
107 .id = 0,
108 .name = "ifxmips_wdt",
109 };
110
111 static struct resource ifxmips_mtd_resource = {
112 .start = IFXMIPS_FLASH_START,
113 .end = IFXMIPS_FLASH_START + IFXMIPS_FLASH_MAX - 1,
114 .flags = IORESOURCE_MEM,
115 };
116
117 static struct platform_device ifxmips_mtd = {
118 .id = 0,
119 .name = "ifxmips_mtd",
120 .num_resources = 1,
121 .resource = &ifxmips_mtd_resource,
122 };
123
124 static struct platform_device ifxmips_gpio_dev = {
125 .name = "GPIODEV",
126 .id = -1,
127 .num_resources = 1,
128 };
129
130 #ifdef CONFIG_LEDS_GPIO
131 static struct gpio_led arv4519_gpio_leds[] = {
132 { .name = "ifx:green:power", .gpio = 3, .active_low = 1, },
133 { .name = "ifx:red:power", .gpio = 7, .active_low = 1, },
134 { .name = "ifx:green:adsl", .gpio = 4, .active_low = 1, },
135 { .name = "ifx:green:internet", .gpio = 5, .active_low = 1, },
136 { .name = "ifx:red:internet", .gpio = 8, .active_low = 1, },
137 { .name = "ifx:green:wlan", .gpio = 6, .active_low = 1, },
138 { .name = "ifx:green:usbpwr", .gpio = 14, .active_low = 1, },
139 { .name = "ifx:green:usb", .gpio = 19, .active_low = 1, },
140 };
141
142 static struct gpio_led_platform_data ifxmips_gpio_led_data;
143
144 static struct platform_device ifxmips_gpio_leds = {
145 .name = "leds-gpio",
146 .id = -1,
147 .dev = {
148 .platform_data = (void *) &ifxmips_gpio_led_data,
149 }
150 };
151 #endif
152
153 static struct resource dwc_usb_res[] = {
154 {
155 .name = "dwc_usb_membase",
156 .flags = IORESOURCE_MEM,
157 .start = 0x1E101000,
158 .end = 0x1E101FFF
159 },
160 {
161 .name = "dwc_usb_irq",
162 .flags = IORESOURCE_IRQ,
163 .start = IFXMIPS_USB_INT,
164 }
165 };
166
167 static struct platform_device dwc_usb =
168 {
169 .id = 0,
170 .name = "dwc_usb",
171 .resource = dwc_usb_res,
172 .num_resources = ARRAY_SIZE(dwc_usb_res),
173 };
174
175 struct platform_device *easy50712_devs[] = {
176 &ifxmips_led, &ifxmips_gpio, &ifxmips_mii,
177 &ifxmips_mtd, &ifxmips_wdt, &ifxmips_gpio_dev, &dwc_usb
178 };
179
180 struct platform_device *easy4010_devs[] = {
181 &ifxmips_led, &ifxmips_gpio, &ifxmips_mii,
182 &ifxmips_mtd, &ifxmips_wdt, &ifxmips_gpio_dev, &dwc_usb
183 };
184
185 struct platform_device *arv5419_devs[] = {
186 &ifxmips_gpio, &ifxmips_mii, &ifxmips_mtd,
187 &ifxmips_gpio_dev, &ifxmips_wdt, &dwc_usb,
188 #ifdef CONFIG_LEDS_GPIO
189 &ifxmips_gpio_leds,
190 #endif
191 };
192
193 static struct gpio_led easy50712_leds[] = {
194 { .name = "ifx:green:test0", .gpio = 0,},
195 { .name = "ifx:green:test1", .gpio = 1,},
196 { .name = "ifx:green:test2", .gpio = 2,},
197 { .name = "ifx:green:test3", .gpio = 3,},
198 };
199
200 static struct gpio_led easy4010_leds[] = {
201 { .name = "ifx:green:test0", .gpio = 0,},
202 { .name = "ifx:green:test1", .gpio = 1,},
203 { .name = "ifx:green:test2", .gpio = 2,},
204 { .name = "ifx:green:test3", .gpio = 3,},
205 };
206
207 static struct ifxmips_board boards[] = {
208 {
209 /* infineon eval kit */
210 .type = EASY50712,
211 .name = "EASY50712",
212 .system_type = SYSTEM_DANUBE_CHIPID1,
213 .devs = easy50712_devs,
214 .reset_resource = {.name = "reset", .start = 1, .end = 15,},
215 .gpiodev_resource = { .name = "gpio",
216 .start = (1 << 0) | (1 << 1),
217 .end = (1 << 0) | (1 << 1)},
218 .ifxmips_leds = easy50712_leds,
219 }, {
220 /* infineon eval kit */
221 .type = EASY4010,
222 .name = "EASY4010",
223 .system_type = SYSTEM_TWINPASS_CHIPID,
224 .devs = easy4010_devs,
225 .reset_resource = {.name = "reset", .start = 1, .end = 15},
226 .gpiodev_resource = { .name = "gpio",
227 .start = (1 << 0) | (1 << 1),
228 .end = (1 << 0) | (1 << 1)},
229 .ifxmips_leds = easy4010_leds,
230 }, {
231 /* arcaydian annex-a board used by thompson, airties, ... */
232 .type = ARV4519,
233 .name = "ARV4519",
234 .system_type = SYSTEM_DANUBE_CHIPID2,
235 .devs = arv5419_devs,
236 .reset_resource = {.name = "reset", .start = 1, .end = 14},
237 .pci_external_clock = 1,
238 .gpio_leds = arv4519_gpio_leds,
239 },
240 };
241
242 const char *get_system_type(void)
243 {
244 chiprev = (ifxmips_r32(IFXMIPS_MPS_CHIPID) & 0x0FFFFFFF);
245
246 switch (chiprev) {
247 case SYSTEM_DANUBE_CHIPID1:
248 case SYSTEM_DANUBE_CHIPID2:
249 return SYSTEM_DANUBE;
250
251 case SYSTEM_TWINPASS_CHIPID:
252 return SYSTEM_TWINPASS;
253 }
254
255 return BOARD_SYSTEM_TYPE;
256 }
257
258 static int __init ifxmips_set_board_type(char *str)
259 {
260 str = strchr(str, '=');
261 if (!str)
262 goto out;
263 str++;
264 if (strlen(str) > MAX_BOARD_NAME_LEN)
265 goto out;
266 strncpy(board_name, str, MAX_BOARD_NAME_LEN);
267 printk(KERN_INFO "bootloader told us, that this is a %s board\n",
268 board_name);
269 out:
270 return 1;
271 }
272 __setup("ifxmips_board", ifxmips_set_board_type);
273
274 static int __init ifxmips_set_ethaddr(char *str)
275 {
276 #define IS_HEX(x) \
277 (((x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') \
278 || (x >= 'A' && x <= 'F')) ? (1) : (0))
279 int i;
280 str = strchr(str, '=');
281 if (!str)
282 goto out;
283 str++;
284 if (strlen(str) != 17)
285 goto out;
286 for (i = 0; i < 6; i++) {
287 if (!IS_HEX(str[3 * i]) || !IS_HEX(str[(3 * i) + 1]))
288 goto out;
289 if ((i != 5) && (str[(3 * i) + 2] != ':'))
290 goto out;
291 ifxmips_ethaddr[i] = simple_strtoul(&str[3 * i], NULL, 16);
292 }
293 if (is_valid_ether_addr(ifxmips_ethaddr))
294 cmdline_mac = 1;
295 out:
296 return 1;
297 }
298 __setup("ethaddr", ifxmips_set_ethaddr);
299
300 int ifxmips_find_brn_block(void)
301 {
302 unsigned char temp[8];
303 memcpy_fromio(temp,
304 (void *)KSEG1ADDR(IFXMIPS_FLASH_START + 0x800000 - 0x10000), 8);
305 if (memcmp(temp, "BRN-BOOT", 8) == 0) {
306 if (!cmdline_mac)
307 memcpy_fromio(ifxmips_ethaddr,
308 (void *)KSEG1ADDR(IFXMIPS_FLASH_START +
309 0x800000 - 0x10000 + 0x16), 6);
310 if (is_valid_ether_addr(ifxmips_ethaddr))
311 cmdline_mac = 1;
312 return 1;
313 } else {
314 return 0;
315 }
316 }
317
318 int ifxmips_has_brn_block(void)
319 {
320 return ifxmips_brn;
321 }
322 EXPORT_SYMBOL(ifxmips_has_brn_block);
323
324 struct ifxmips_board *ifxmips_find_board(void)
325 {
326 int i;
327 if (!*board_name)
328 return 0;
329 for (i = 0; i < ARRAY_SIZE(boards); i++)
330 if ((boards[i].system_type == chiprev) &&
331 (!strcmp(boards[i].name, board_name)))
332 return &boards[i];
333 return 0;
334 }
335
336 int __init ifxmips_init_devices(void)
337 {
338 struct ifxmips_board *board = ifxmips_find_board();
339
340 chiprev = (ifxmips_r32(IFXMIPS_MPS_CHIPID) & 0x0FFFFFFF);
341 ifxmips_brn = ifxmips_find_brn_block();
342
343 if (!cmdline_mac)
344 random_ether_addr(ifxmips_ethaddr);
345
346 if (!board) {
347 switch (chiprev) {
348 case SYSTEM_DANUBE_CHIPID1:
349 case SYSTEM_DANUBE_CHIPID2:
350 default:
351 board = &boards[0];
352 break;
353 case SYSTEM_TWINPASS_CHIPID:
354 board = &boards[1];
355 break;
356 }
357 }
358
359 switch (board->type) {
360 case EASY50712:
361 board->num_devs = ARRAY_SIZE(easy50712_devs);
362 ifxmips_led_data.num_leds = ARRAY_SIZE(easy50712_leds);
363 break;
364 case EASY4010:
365 board->num_devs = ARRAY_SIZE(easy4010_devs);
366 ifxmips_led_data.num_leds = ARRAY_SIZE(easy4010_leds);
367 break;
368 case ARV4519:
369 /* set some sane defaults for the gpios */
370 gpio_set_value(3, 0);
371 gpio_set_value(4, 0);
372 gpio_set_value(5, 0);
373 gpio_set_value(6, 0);
374 gpio_set_value(7, 1);
375 gpio_set_value(8, 1);
376 gpio_set_value(19, 0);
377 board->num_devs = ARRAY_SIZE(arv5419_devs);
378 #ifdef CONFIG_LEDS_GPIO
379 ifxmips_gpio_led_data.num_leds = ARRAY_SIZE(arv4519_gpio_leds);
380 #endif
381 break;
382 }
383 #ifdef CONFIG_LEDS_GPIO
384 ifxmips_gpio_led_data.leds = board->gpio_leds;
385 #endif
386 ifxmips_led_data.leds = board->ifxmips_leds;
387
388 printk(KERN_INFO "%s: adding %d devs\n",
389 __func__, board->num_devs);
390
391 ifxmips_gpio.resource = &board->reset_resource;
392 ifxmips_gpio_dev.resource = &board->gpiodev_resource;
393 if (board->pci_external_clock)
394 ifxmips_pci_external_clock = 1;
395 printk(KERN_INFO "using board definition %s\n", board->name);
396 return platform_add_devices(board->devs, board->num_devs);
397 }
398
399 arch_initcall(ifxmips_init_devices);