6f511e5be06d4439fbfdbe26a93f34b971f62621
[openwrt/svn-archive/archive.git] / target / linux / lantiq / files-3.1 / arch / mips / lantiq / xway / mach-gigasx76x.c
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
6 * Copyright (C) 2011 Andrej Vlašić
7 * Copyright (C) 2011 Luka Perkov
8 *
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/leds.h>
15 #include <linux/gpio.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/mtd/physmap.h>
19 #include <linux/input.h>
20 #include <linux/ath5k_platform.h>
21 #include <linux/pci.h>
22 #include <linux/phy.h>
23 #include <linux/io.h>
24 #include <linux/string.h>
25
26 #include <irq.h>
27 #include <lantiq_soc.h>
28 #include <lantiq_platform.h>
29 #include <dev-gpio-leds.h>
30 #include <dev-gpio-buttons.h>
31
32 #include "../machtypes.h"
33 #include "devices.h"
34 #include "dev-dwc_otg.h"
35
36 #define UBOOT_ENV_OFFSET 0x010000
37 #define UBOOT_ENV_SIZE 0x010000
38
39 static struct mtd_partition gigasx76x_partitions[] =
40 {
41 {
42 .name = "uboot",
43 .offset = 0x000000,
44 .size = 0x010000,
45 },
46 {
47 .name = "uboot_env",
48 .offset = UBOOT_ENV_OFFSET,
49 .size = UBOOT_ENV_SIZE,
50 },
51 {
52 .name = "linux",
53 .offset = 0x020000,
54 .size = 0x7d0000,
55 },
56 {
57 .name = "board_config",
58 .offset = 0x7f0000,
59 .size = 0x010000,
60 },
61 };
62
63 static struct gpio_led
64 gigasx76x_gpio_leds[] __initdata = {
65 { .name = "soc:green:usb", .gpio = 202, },
66 { .name = "soc:green:wlan", .gpio = 203, },
67 { .name = "soc:green:phone2", .gpio = 204, },
68 { .name = "soc:green:phone1", .gpio = 205, },
69 { .name = "soc:green:line", .gpio = 206, },
70 { .name = "soc:green:online", .gpio = 207, },
71 { .name = "soc:green:voip", .gpio = 208, },
72 };
73
74 static struct gpio_keys_button
75 gigasx76x_gpio_keys[] __initdata = {
76 {
77 .desc = "restart",
78 .type = EV_KEY,
79 .code = KEY_RESTART,
80 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
81 .gpio = 14,
82 .active_low = 1,
83 },
84 {
85 .desc = "wps",
86 .type = EV_KEY,
87 .code = KEY_WPS_BUTTON,
88 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
89 .gpio = 22,
90 .active_low = 1,
91 },
92 };
93
94 static struct physmap_flash_data gigasx76x_flash_data = {
95 .nr_parts = ARRAY_SIZE(gigasx76x_partitions),
96 .parts = gigasx76x_partitions,
97 };
98
99 static struct ltq_pci_data ltq_pci_data = {
100 .clock = PCI_CLOCK_INT,
101 .gpio = PCI_GNT1 | PCI_REQ1,
102 .irq = { [14] = INT_NUM_IM0_IRL0 + 22, },
103 };
104
105 static struct ltq_eth_data ltq_eth_data = {
106 .mii_mode = PHY_INTERFACE_MODE_MII,
107 };
108
109 static char __init *get_uboot_env_var(char *haystack, int haystack_len, char *needle, int needle_len) {
110 int i;
111 for (i = 0; i <= haystack_len - needle_len; i++) {
112 if (memcmp(haystack + i, needle, needle_len) == 0) {
113 return haystack + i + needle_len;
114 }
115 }
116 return NULL;
117 }
118
119 /*
120 * gigasx76x_parse_hex_* are not uniq. in arm/orion there are also duplicates:
121 * dns323_parse_hex_*
122 * TODO: one day write a patch for this :)
123 */
124 static int __init gigasx76x_parse_hex_nibble(char n) {
125 if (n >= '0' && n <= '9')
126 return n - '0';
127
128 if (n >= 'A' && n <= 'F')
129 return n - 'A' + 10;
130
131 if (n >= 'a' && n <= 'f')
132 return n - 'a' + 10;
133
134 return -1;
135 }
136
137 static int __init gigasx76x_parse_hex_byte(const char *b) {
138 int hi;
139 int lo;
140
141 hi = gigasx76x_parse_hex_nibble(b[0]);
142 lo = gigasx76x_parse_hex_nibble(b[1]);
143
144 if (hi < 0 || lo < 0)
145 return -1;
146
147 return (hi << 4) | lo;
148 }
149
150 static int __init gigasx76x_register_ethernet(void) {
151 u_int8_t addr[6];
152 int i;
153 char *uboot_env_page;
154 char *mac;
155
156 uboot_env_page = ioremap(LTQ_FLASH_START + UBOOT_ENV_OFFSET, UBOOT_ENV_SIZE);
157 if (!uboot_env_page)
158 return -ENOMEM;
159
160 mac = get_uboot_env_var(uboot_env_page, UBOOT_ENV_SIZE, "\0ethaddr=", 9);
161
162 if (!mac) {
163 goto error_fail;
164 }
165
166 /* Sanity check the string we're looking at */
167 for (i = 0; i < 5; i++) {
168 if (*(mac + (i * 3) + 2) != ':') {
169 goto error_fail;
170 }
171 }
172
173 for (i = 0; i < 6; i++) {
174 int byte;
175 byte = gigasx76x_parse_hex_byte(mac + (i * 3));
176 if (byte < 0) {
177 goto error_fail;
178 }
179 addr[i] = byte;
180 }
181
182 iounmap(uboot_env_page);
183 printk("GIGASX76X: Found ethernet MAC address: ");
184 for (i = 0; i < 6; i++)
185 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
186
187 memcpy(&ltq_eth_data.mac.sa_data, addr, 6);
188 ltq_register_etop(&ltq_eth_data);
189
190 return 0;
191
192 error_fail:
193 iounmap(uboot_env_page);
194 return -EINVAL;
195 }
196
197 static void __init gigasx76x_init(void) {
198 #define GIGASX76X_USB 29
199 #define GIGASX76X_MADWIFI_ADDR 0xb07f0000
200
201 ltq_register_gpio_stp();
202 ltq_register_nor(&gigasx76x_flash_data);
203 ltq_register_pci(&ltq_pci_data);
204 gigasx76x_register_ethernet();
205 xway_register_dwc(GIGASX76X_USB);
206 ltq_register_tapi();
207 ltq_register_madwifi_eep(GIGASX76X_MADWIFI_ADDR);
208 ltq_add_device_gpio_leds(-1, ARRAY_SIZE(gigasx76x_gpio_leds), gigasx76x_gpio_leds);
209 ltq_register_gpio_keys_polled(-1, LTQ_KEYS_POLL_INTERVAL, ARRAY_SIZE(gigasx76x_gpio_keys), gigasx76x_gpio_keys);
210 }
211
212 MIPS_MACHINE(LANTIQ_MACH_GIGASX76X, "GIGASX76X", "GIGASX76X - Gigaset SX761,SX762,SX763", gigasx76x_init);