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