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