c1cd5f97b90c5e5e929368448c05e9fd57494ef8
[openwrt/openwrt.git] / target / linux / ar7 / files / arch / mips / ar7 / platform.c
1 /*
2 * Copyright (C) 2006, 2007 OpenWrt.org
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <linux/autoconf.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/platform_device.h>
26 #include <linux/mtd/physmap.h>
27 #include <linux/serial.h>
28 #include <linux/serial_8250.h>
29 #include <linux/ioport.h>
30 #include <linux/io.h>
31 #include <linux/version.h>
32 #include <linux/vlynq.h>
33 #include <linux/leds.h>
34 #include <linux/string.h>
35
36 #include <asm/addrspace.h>
37 #include <asm/ar7/ar7.h>
38 #include <asm/ar7/gpio.h>
39 #include <asm/ar7/prom.h>
40
41 struct plat_vlynq_data {
42 struct plat_vlynq_ops ops;
43 int gpio_bit;
44 int reset_bit;
45 };
46
47
48 static int vlynq_on(struct vlynq_device *dev)
49 {
50 int result;
51 struct plat_vlynq_data *pdata = dev->dev.platform_data;
52
53 if ((result = gpio_request(pdata->gpio_bit, "vlynq")))
54 goto out;
55
56 ar7_device_reset(pdata->reset_bit);
57
58 if ((result = ar7_gpio_disable(pdata->gpio_bit)))
59 goto out_enabled;
60
61 if ((result = ar7_gpio_enable(pdata->gpio_bit)))
62 goto out_enabled;
63
64 if ((result = gpio_direction_output(pdata->gpio_bit, 0)))
65 goto out_gpio_enabled;
66
67 mdelay(50);
68
69 gpio_set_value(pdata->gpio_bit, 1);
70 mdelay(50);
71
72 return 0;
73
74 out_gpio_enabled:
75 ar7_gpio_disable(pdata->gpio_bit);
76 out_enabled:
77 ar7_device_disable(pdata->reset_bit);
78 gpio_free(pdata->gpio_bit);
79 out:
80 return result;
81 }
82
83 static void vlynq_off(struct vlynq_device *dev)
84 {
85 struct plat_vlynq_data *pdata = dev->dev.platform_data;
86 ar7_gpio_disable(pdata->gpio_bit);
87 gpio_free(pdata->gpio_bit);
88 ar7_device_disable(pdata->reset_bit);
89 }
90
91 static struct resource physmap_flash_resource = {
92 .name = "mem",
93 .flags = IORESOURCE_MEM,
94 .start = 0x10000000,
95 .end = 0x107fffff,
96 };
97
98 static struct resource cpmac_low_res[] = {
99 {
100 .name = "regs",
101 .flags = IORESOURCE_MEM,
102 .start = AR7_REGS_MAC0,
103 .end = AR7_REGS_MAC0 + 0x7ff,
104 },
105 {
106 .name = "irq",
107 .flags = IORESOURCE_IRQ,
108 .start = 27,
109 .end = 27,
110 },
111 };
112
113 static struct resource cpmac_high_res[] = {
114 {
115 .name = "regs",
116 .flags = IORESOURCE_MEM,
117 .start = AR7_REGS_MAC1,
118 .end = AR7_REGS_MAC1 + 0x7ff,
119 },
120 {
121 .name = "irq",
122 .flags = IORESOURCE_IRQ,
123 .start = 41,
124 .end = 41,
125 },
126 };
127
128 static struct resource vlynq_low_res[] = {
129 {
130 .name = "regs",
131 .flags = IORESOURCE_MEM,
132 .start = AR7_REGS_VLYNQ0,
133 .end = AR7_REGS_VLYNQ0 + 0xff,
134 },
135 {
136 .name = "irq",
137 .flags = IORESOURCE_IRQ,
138 .start = 29,
139 .end = 29,
140 },
141 {
142 .name = "mem",
143 .flags = IORESOURCE_MEM,
144 .start = 0x04000000,
145 .end = 0x04ffffff,
146 },
147 {
148 .name = "devirq",
149 .flags = IORESOURCE_IRQ,
150 .start = 80,
151 .end = 111,
152 },
153 };
154
155 static struct resource vlynq_high_res[] = {
156 {
157 .name = "regs",
158 .flags = IORESOURCE_MEM,
159 .start = AR7_REGS_VLYNQ1,
160 .end = AR7_REGS_VLYNQ1 + 0xff,
161 },
162 {
163 .name = "irq",
164 .flags = IORESOURCE_IRQ,
165 .start = 33,
166 .end = 33,
167 },
168 {
169 .name = "mem",
170 .flags = IORESOURCE_MEM,
171 .start = 0x0c000000,
172 .end = 0x0cffffff,
173 },
174 {
175 .name = "devirq",
176 .flags = IORESOURCE_IRQ,
177 .start = 112,
178 .end = 143,
179 },
180 };
181
182 static struct resource usb_res[] = {
183 {
184 .name = "regs",
185 .flags = IORESOURCE_MEM,
186 .start = AR7_REGS_USB,
187 .end = AR7_REGS_USB + 0xff,
188 },
189 {
190 .name = "irq",
191 .flags = IORESOURCE_IRQ,
192 .start = 32,
193 .end = 32,
194 },
195 {
196 .name = "mem",
197 .flags = IORESOURCE_MEM,
198 .start = 0x03400000,
199 .end = 0x034001fff,
200 },
201 };
202
203 static struct physmap_flash_data physmap_flash_data = {
204 .width = 2,
205 };
206
207 static struct plat_cpmac_data cpmac_low_data = {
208 .reset_bit = 17,
209 .power_bit = 20,
210 .phy_mask = 0x80000000,
211 };
212
213 static struct plat_cpmac_data cpmac_high_data = {
214 .reset_bit = 21,
215 .power_bit = 22,
216 .phy_mask = 0x7fffffff,
217 };
218
219 static struct plat_vlynq_data vlynq_low_data = {
220 .ops.on = vlynq_on,
221 .ops.off = vlynq_off,
222 .reset_bit = 20,
223 .gpio_bit = 18,
224 };
225
226 static struct plat_vlynq_data vlynq_high_data = {
227 .ops.on = vlynq_on,
228 .ops.off = vlynq_off,
229 .reset_bit = 16,
230 .gpio_bit = 19,
231 };
232
233 static struct platform_device physmap_flash = {
234 .id = 0,
235 .name = "physmap-flash",
236 .dev.platform_data = &physmap_flash_data,
237 .resource = &physmap_flash_resource,
238 .num_resources = 1,
239 };
240
241 static u64 cpmac_dma_mask = DMA_32BIT_MASK;
242 static struct platform_device cpmac_low = {
243 .id = 0,
244 .name = "cpmac",
245 .dev = {
246 .dma_mask = &cpmac_dma_mask,
247 .coherent_dma_mask = DMA_32BIT_MASK,
248 .platform_data = &cpmac_low_data,
249 },
250 .resource = cpmac_low_res,
251 .num_resources = ARRAY_SIZE(cpmac_low_res),
252 };
253
254 static struct platform_device cpmac_high = {
255 .id = 1,
256 .name = "cpmac",
257 .dev = {
258 .dma_mask = &cpmac_dma_mask,
259 .coherent_dma_mask = DMA_32BIT_MASK,
260 .platform_data = &cpmac_high_data,
261 },
262 .resource = cpmac_high_res,
263 .num_resources = ARRAY_SIZE(cpmac_high_res),
264 };
265
266 static struct platform_device vlynq_low = {
267 .id = 0,
268 .name = "vlynq",
269 .dev.platform_data = &vlynq_low_data,
270 .resource = vlynq_low_res,
271 .num_resources = ARRAY_SIZE(vlynq_low_res),
272 };
273
274 static struct platform_device vlynq_high = {
275 .id = 1,
276 .name = "vlynq",
277 .dev.platform_data = &vlynq_high_data,
278 .resource = vlynq_high_res,
279 .num_resources = ARRAY_SIZE(vlynq_high_res),
280 };
281
282
283 /* This is proper way to define uart ports, but they are then detected
284 * as xscale and, obviously, don't work...
285 */
286 #if !defined(CONFIG_SERIAL_8250)
287
288 static struct plat_serial8250_port uart0_data = {
289 .mapbase = AR7_REGS_UART0,
290 .irq = AR7_IRQ_UART0,
291 .regshift = 2,
292 .iotype = UPIO_MEM,
293 .flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP,
294 };
295
296 static struct plat_serial8250_port uart1_data = {
297 .mapbase = UR8_REGS_UART1,
298 .irq = AR7_IRQ_UART1,
299 .regshift = 2,
300 .iotype = UPIO_MEM,
301 .flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP,
302 };
303
304 static struct plat_serial8250_port uart_data[] = {
305 uart0_data,
306 uart1_data,
307 { .flags = 0 }
308 };
309
310 static struct plat_serial8250_port uart_data_single[] = {
311 uart0_data,
312 { .flags = 0 }
313 };
314
315 static struct platform_device uart = {
316 .id = 0,
317 .name = "serial8250",
318 .dev.platform_data = uart_data_single
319 };
320 #endif
321
322 static struct gpio_led default_leds[] = {
323 { .name = "status", .gpio = 8, .active_low = 1, },
324 };
325
326 static struct gpio_led fb_sl_leds[] = {
327 { .name = "1", .gpio = 7, },
328 { .name = "2", .gpio = 13, .active_low = 1, },
329 { .name = "3", .gpio = 10, .active_low = 1, },
330 { .name = "4", .gpio = 12, .active_low = 1, },
331 { .name = "5", .gpio = 9, .active_low = 1, },
332 };
333
334 static struct gpio_led fb_fon_leds[] = {
335 { .name = "1", .gpio = 8, },
336 { .name = "2", .gpio = 3, .active_low = 1, },
337 { .name = "3", .gpio = 5, },
338 { .name = "4", .gpio = 4, .active_low = 1, },
339 { .name = "5", .gpio = 11, .active_low = 1, },
340 };
341
342 static struct gpio_led_platform_data ar7_led_data;
343
344 static struct platform_device ar7_gpio_leds = {
345 .name = "leds-gpio",
346 .id = -1,
347 .dev = {
348 .platform_data = &ar7_led_data,
349 }
350 };
351
352 static struct platform_device ar7_udc = {
353 .id = -1,
354 .name = "ar7_udc",
355 .resource = usb_res,
356 .num_resources = ARRAY_SIZE(usb_res),
357 };
358
359 static inline unsigned char char2hex(char h)
360 {
361 switch (h) {
362 case '0': case '1': case '2': case '3': case '4':
363 case '5': case '6': case '7': case '8': case '9':
364 return h - '0';
365 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
366 return h - 'A' + 10;
367 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
368 return h - 'a' + 10;
369 default:
370 return 0;
371 }
372 }
373
374 static void cpmac_get_mac(int instance, unsigned char *dev_addr)
375 {
376 int i;
377 char name[5], default_mac[] = "00:00:00:12:34:56", *mac;
378
379 mac = NULL;
380 sprintf(name, "mac%c", 'a' + instance);
381 mac = prom_getenv(name);
382 if (!mac) {
383 sprintf(name, "mac%c", 'a');
384 mac = prom_getenv(name);
385 }
386 if (!mac)
387 mac = default_mac;
388 for (i = 0; i < 6; i++)
389 dev_addr[i] = (char2hex(mac[i * 3]) << 4) +
390 char2hex(mac[i * 3 + 1]);
391 }
392
393 static void __init detect_leds(void)
394 {
395 char *prId;
396
397 /* Default LEDs */
398 ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
399 ar7_led_data.leds = default_leds;
400
401 /* FIXME: the whole thing is unreliable */
402 prId = prom_getenv("ProductID");
403
404 /* If we can't get the product id from PROM, use the default LEDs */
405 if (!prId)
406 return;
407
408 if (strstr(prId, "Fritz_Box_FON")) {
409 ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
410 ar7_led_data.leds = fb_fon_leds;
411 } else if (strstr(prId, "Fritz_Box_")) {
412 ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
413 ar7_led_data.leds = fb_sl_leds;
414 }
415 }
416
417 static int __init ar7_register_devices(void)
418 {
419 int res;
420
421 #ifdef CONFIG_SERIAL_8250
422
423 static struct uart_port uart_port[2];
424
425 memset(uart_port, 0, sizeof(struct uart_port) * 2);
426
427 uart_port[0].type = PORT_AR7;
428 uart_port[0].line = 0;
429 uart_port[0].irq = AR7_IRQ_UART0;
430 uart_port[0].uartclk = ar7_bus_freq() / 2;
431 uart_port[0].iotype = UPIO_MEM;
432 uart_port[0].mapbase = AR7_REGS_UART0;
433 uart_port[0].membase = ioremap(uart_port[0].mapbase, 256);
434 uart_port[0].regshift = 2;
435 res = early_serial_setup(&uart_port[0]);
436 if (res)
437 return res;
438
439
440 /* Only TNETD73xx have a second serial port */
441 if (ar7_has_second_uart()) {
442 uart_port[1].type = PORT_AR7;
443 uart_port[1].line = 1;
444 uart_port[1].irq = AR7_IRQ_UART1;
445 uart_port[1].uartclk = ar7_bus_freq() / 2;
446 uart_port[1].iotype = UPIO_MEM;
447 uart_port[1].mapbase = UR8_REGS_UART1;
448 uart_port[1].membase = ioremap(uart_port[1].mapbase, 256);
449 uart_port[1].regshift = 2;
450 res = early_serial_setup(&uart_port[1]);
451 if (res)
452 return res;
453 }
454
455 #else /* !CONFIG_SERIAL_8250 */
456
457 uart_data[0].uartclk = ar7_bus_freq() / 2;
458 uart_data[1].uartclk = uart_data[0].uartclk;
459
460 /* Only TNETD73xx have a second serial port */
461 if (ar7_has_second_uart())
462 uart.dev.platform_data = uart_data;
463
464 res = platform_device_register(&uart);
465 if (res)
466 return res;
467
468 #endif /* CONFIG_SERIAL_8250 */
469
470 res = platform_device_register(&physmap_flash);
471 if (res)
472 return res;
473
474 res = platform_device_register(&vlynq_low);
475 if (res)
476 return res;
477
478 ar7_device_disable(vlynq_low_data.reset_bit);
479 if (ar7_has_high_vlynq()) {
480 ar7_device_disable(vlynq_high_data.reset_bit);
481 res = platform_device_register(&vlynq_high);
482 if (res)
483 return res;
484 }
485
486 if (ar7_has_high_cpmac()) {
487 cpmac_get_mac(1, cpmac_high_data.dev_addr);
488 res = platform_device_register(&cpmac_high);
489 if (res)
490 return res;
491 } else {
492 cpmac_low_data.phy_mask = 0xffffffff;
493 }
494
495 cpmac_get_mac(0, cpmac_low_data.dev_addr);
496 res = platform_device_register(&cpmac_low);
497 if (res)
498 return res;
499
500 detect_leds();
501 res = platform_device_register(&ar7_gpio_leds);
502 if (res)
503 return res;
504
505 res = platform_device_register(&ar7_udc);
506
507 return res;
508 }
509
510
511 arch_initcall(ar7_register_devices);