5256fddc7f64c12dffd85b5d7fbe33ba55614c6d
[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 "dev-wifi-ath5k.h"
34 #include "devices.h"
35 #include "dev-dwc_otg.h"
36
37 #include "mach-gigasx76x.h"
38
39 #define UBOOT_ENV_OFFSET 0x010000
40 #define UBOOT_ENV_SIZE 0x010000
41
42 static struct mtd_partition gigasx76x_partitions[] =
43 {
44 {
45 .name = "uboot",
46 .offset = 0x000000,
47 .size = 0x010000,
48 },
49 {
50 .name = "uboot_env",
51 .offset = UBOOT_ENV_OFFSET,
52 .size = UBOOT_ENV_SIZE,
53 },
54 {
55 .name = "linux",
56 .offset = 0x020000,
57 .size = 0x7d0000,
58 },
59 {
60 .name = "board_config",
61 .offset = 0x7f0000,
62 .size = 0x010000,
63 },
64 };
65
66 static struct gpio_led
67 gigasx76x_gpio_leds[] __initdata = {
68
69 { .name = "soc:green:usb", .gpio = 202, },
70 { .name = "soc:green:wifi", .gpio = 203, },
71 { .name = "soc:green:phone2", .gpio = 204, },
72 { .name = "soc:green:phone1", .gpio = 205, },
73 { .name = "soc:green:line", .gpio = 206, },
74 { .name = "soc:green:online", .gpio = 207, },
75 { .name = "soc:green:voip", .gpio = 208, },
76 };
77
78 static struct gpio_keys_button
79 gigasx76x_gpio_keys[] __initdata = {
80 {
81 .desc = "wps",
82 .type = EV_KEY,
83 .code = BTN_0,
84 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
85 .gpio = 22,
86 .active_low = 1,
87 },
88 /*{
89 .desc = "factory",
90 .type = EV_KEY,
91 .code = BTN_1,
92 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
93 .gpio = 14,
94 .active_low = 1,
95 },*/
96 };
97
98 static struct physmap_flash_data gigasx76x_flash_data = {
99 .nr_parts = ARRAY_SIZE(gigasx76x_partitions),
100 .parts = gigasx76x_partitions,
101 };
102
103 static struct ltq_pci_data ltq_pci_data = {
104 .clock = PCI_CLOCK_INT,
105 .gpio = PCI_GNT1 | PCI_REQ1,
106 .irq = { [14] = INT_NUM_IM0_IRL0 + 22, },
107 };
108
109 static struct ltq_eth_data ltq_eth_data = {
110 .mii_mode = PHY_INTERFACE_MODE_MII,
111 };
112
113 static char __init *get_uboot_env_var(char *haystack, int haystack_len, char *needle, int needle_len) {
114 int i;
115 for (i = 0; i <= haystack_len - needle_len; i++) {
116 if (memcmp(haystack + i, needle, needle_len) == 0) {
117 return haystack + i + needle_len;
118 }
119 }
120 return NULL;
121 }
122
123 /*
124 * gigasx76x_parse_hex_* are not uniq. in arm/orion there are also duplicates:
125 * dns323_parse_hex_*
126 * TODO: one day write a patch for this :)
127 */
128 static int __init gigasx76x_parse_hex_nibble(char n) {
129 if (n >= '0' && n <= '9')
130 return n - '0';
131
132 if (n >= 'A' && n <= 'F')
133 return n - 'A' + 10;
134
135 if (n >= 'a' && n <= 'f')
136 return n - 'a' + 10;
137
138 return -1;
139 }
140
141 static int __init gigasx76x_parse_hex_byte(const char *b) {
142 int hi;
143 int lo;
144
145 hi = gigasx76x_parse_hex_nibble(b[0]);
146 lo = gigasx76x_parse_hex_nibble(b[1]);
147
148 if (hi < 0 || lo < 0)
149 return -1;
150
151 return (hi << 4) | lo;
152 }
153
154 static u16 gigasx76x_ath5k_eeprom_data[ATH5K_PLAT_EEP_MAX_WORDS];
155 static u8 gigasx76x_ath5k_eeprom_mac[6];
156
157 static int __init gigasx76x_register_ethernet(void) {
158 u_int8_t addr[6];
159 int i;
160 char *uboot_env_page;
161 char *mac;
162 char *boardid;
163
164 uboot_env_page = ioremap(LTQ_FLASH_START + UBOOT_ENV_OFFSET, UBOOT_ENV_SIZE);
165 if (!uboot_env_page)
166 return -ENOMEM;
167
168 mac = get_uboot_env_var(uboot_env_page, UBOOT_ENV_SIZE, "\0ethaddr=", 9);
169 boardid = get_uboot_env_var(uboot_env_page, UBOOT_ENV_SIZE, "\0boardid=", 9);
170
171 if (!mac) {
172 goto error_fail;
173 }
174
175 if (!boardid) {
176 goto error_fail;
177 }
178
179 /* Sanity check the string we're looking at */
180 for (i = 0; i < 5; i++) {
181 if (*(mac + (i * 3) + 2) != ':') {
182 goto error_fail;
183 }
184 }
185
186 for (i = 0; i < 6; i++) {
187 int byte;
188 byte = gigasx76x_parse_hex_byte(mac + (i * 3));
189 if (byte < 0) {
190 goto error_fail;
191 }
192 addr[i] = byte;
193 }
194
195 iounmap(uboot_env_page);
196 printk("GIGASX76X: Found ethernet MAC address: ");
197 for (i = 0; i < 6; i++)
198 printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");
199
200 memcpy(&ltq_eth_data.mac.sa_data, addr, 6);
201 ltq_register_etop(&ltq_eth_data);
202
203 memcpy(&gigasx76x_ath5k_eeprom_mac, addr, 6);
204 gigasx76x_ath5k_eeprom_mac[5]++;
205
206 if (strncmp(boardid, "sx763", 5) == 0) {
207 printk("GIGASX76X: Board id is sx763.");
208 memcpy(&gigasx76x_ath5k_eeprom_data, sx763_eeprom_data, ATH5K_PLAT_EEP_MAX_WORDS);
209 } else if (strncmp(boardid, "sx762", 5) == 0) {
210 printk("GIGASX76X: Board id is sx762.");
211 memcpy(&gigasx76x_ath5k_eeprom_data, sx762_eeprom_data, ATH5K_PLAT_EEP_MAX_WORDS);
212 } else {
213 printk("GIGASX76X: Board id is unknown, fix uboot_env data.");
214 }
215
216 return 0;
217
218 error_fail:
219 iounmap(uboot_env_page);
220 return -EINVAL;
221 }
222
223 static void __init gigasx76x_init(void) {
224 #define GIGASX76X_USB 29
225 #define GIGASX76X_MADWIFI_ADDR 0xb07f0000
226
227 ltq_register_gpio_stp();
228 ltq_register_nor(&gigasx76x_flash_data);
229 ltq_register_pci(&ltq_pci_data);
230 ltq_register_tapi();
231 ltq_register_madwifi_eep(GIGASX76X_MADWIFI_ADDR);
232 ltq_add_device_gpio_leds(-1, ARRAY_SIZE(gigasx76x_gpio_leds), gigasx76x_gpio_leds);
233 ltq_register_gpio_keys_polled(-1, LTQ_KEYS_POLL_INTERVAL, ARRAY_SIZE(gigasx76x_gpio_keys), gigasx76x_gpio_keys);
234 ltq_register_ath5k(gigasx76x_ath5k_eeprom_data, gigasx76x_ath5k_eeprom_mac);
235 xway_register_dwc(GIGASX76X_USB);
236 gigasx76x_register_ethernet();
237 }
238
239 MIPS_MACHINE(LANTIQ_MACH_GIGASX76X, "GIGASX76X", "GIGASX76X - Gigaset SX761,SX762,SX763", gigasx76x_init);