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