aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/lantiq/files-3.1/arch/mips/lantiq/xway/mach-gigasx76x.c
blob: 3dbfcb94d400d9b7531951e7a758c0d2b6b01a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 *  Copyright (C) 2011 Andrej Vlašić
 *  Copyright (C) 2011 Luka Perkov
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/gpio.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/physmap.h>
#include <linux/input.h>
#include <linux/ath5k_platform.h>
#include <linux/pci.h>
#include <linux/phy.h>
#include <linux/io.h>
#include <linux/string.h>

#include <irq.h>
#include <lantiq_soc.h>
#include <lantiq_platform.h>
#include <dev-gpio-leds.h>
#include <dev-gpio-buttons.h>

#include "../machtypes.h"
#include "dev-wifi-ath5k.h"
#include "devices.h"
#include "dev-dwc_otg.h"

#include "mach-gigasx76x.h"

#define UBOOT_ENV_OFFSET	0x010000
#define UBOOT_ENV_SIZE		0x010000

static struct mtd_partition gigasx76x_partitions[] =
{
	{
		.name	= "uboot",
		.offset	= 0x000000,
		.size	= 0x010000,
	},
	{
		.name	= "uboot_env",
		.offset	= UBOOT_ENV_OFFSET,
		.size	= UBOOT_ENV_SIZE,
	},
	{
		.name	= "linux",
		.offset	= 0x020000,
		.size	= 0x7e0000,
	},
};

static struct gpio_led
gigasx76x_gpio_leds[] __initdata = {
	{ .name = "soc:green:voip", .gpio = 216, },
	{ .name = "soc:green:adsl", .gpio = 217, },
	{ .name = "soc:green:usb", .gpio = 218, },
	{ .name = "soc:green:wifi", .gpio = 219, },
	{ .name = "soc:green:phone2", .gpio = 220, },
	{ .name = "soc:green:phone1", .gpio = 221, },
	{ .name = "soc:green:line", .gpio = 222, },
	{ .name = "soc:green:online", .gpio = 223, },
};

static struct gpio_keys_button
gigasx76x_gpio_keys[] __initdata = {
	{
		.desc		= "wps",
		.type		= EV_KEY,
		.code		= KEY_WPS_BUTTON,
		.debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
		.gpio		= 22,
		.active_low	= 1,
	},
	{
		.desc		= "reset",
		.type		= EV_KEY,
		.code		= BTN_0,
		.debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
		.gpio		= 14,
		.active_low	= 0,
	},
};

static struct physmap_flash_data gigasx76x_flash_data = {
	.nr_parts	= ARRAY_SIZE(gigasx76x_partitions),
	.parts		= gigasx76x_partitions,
};

static struct ltq_pci_data ltq_pci_data = {
	.clock	= PCI_CLOCK_INT,
	.gpio	= PCI_GNT1 | PCI_REQ1,
	.irq	= { [14] = INT_NUM_IM0_IRL0 + 22, },
};

static struct ltq_eth_data ltq_eth_data = {
	.mii_mode	= PHY_INTERFACE_MODE_MII,
};

static char __init *get_uboot_env_var(char *haystack, int haystack_len, char *needle, int needle_len) {
	int i;
	for (i = 0; i <= haystack_len - needle_len; i++) {
		if (memcmp(haystack + i, needle, needle_len) == 0) {
			return haystack + i + needle_len;
		}
	}
	return NULL;
}

/*
 * gigasx76x_parse_hex_* are not uniq. in arm/orion there are also duplicates:
 * dns323_parse_hex_*
 * TODO: one day write a patch for this :)
 */
static int __init gigasx76x_parse_hex_nibble(char n) {
	if (n >= '0' && n <= '9')
		return n - '0';

	if (n >= 'A' && n <= 'F')
		return n - 'A' + 10;

	if (n >= 'a' && n <= 'f')
		return n - 'a' + 10;

	return -1;
}

static int __init gigasx76x_parse_hex_byte(const char *b) {
	int hi;
	int lo;

	hi = gigasx76x_parse_hex_nibble(b[0]);
	lo = gigasx76x_parse_hex_nibble(b[1]);

	if (hi < 0 || lo < 0)
		return -1;

	return (hi << 4) | lo;
}

static u16 gigasx76x_ath5k_eeprom_data[ATH5K_PLAT_EEP_MAX_WORDS];
static u8 gigasx76x_ath5k_eeprom_mac[6];

static int __init gigasx76x_register_ethernet(void) {
	u_int8_t addr[6];
	int i;
	char *uboot_env_page;
	char *mac;
	char *boardid;

	uboot_env_page = ioremap(LTQ_FLASH_START + UBOOT_ENV_OFFSET, UBOOT_ENV_SIZE);
	if (!uboot_env_page)
		return -ENOMEM;

	mac = get_uboot_env_var(uboot_env_page, UBOOT_ENV_SIZE, "\0ethaddr=", 9);
	boardid = get_uboot_env_var(uboot_env_page, UBOOT_ENV_SIZE, "\0boardid=", 9);

	if (!mac) {
	goto error_fail;
	}

	if (!boardid) {
	goto error_fail;
	}

	/* Sanity check the string we're looking at */
	for (i = 0; i < 5; i++) {
	if (*(mac + (i * 3) + 2) != ':') {
		goto error_fail;
		}
	}

	for (i = 0; i < 6; i++) {
		int byte;
		byte = gigasx76x_parse_hex_byte(mac + (i * 3));
		if (byte < 0) {
			goto error_fail;
		}
		addr[i] = byte;
	}

	iounmap(uboot_env_page);
	printk("GIGASX76X: Found ethernet MAC address: ");
	for (i = 0; i < 6; i++)
		printk("%.2x%s", addr[i], (i < 5) ? ":" : ".\n");

	memcpy(&ltq_eth_data.mac.sa_data, addr, 6);
	ltq_register_etop(&ltq_eth_data);

	memcpy(&gigasx76x_ath5k_eeprom_mac, addr, 6);
	gigasx76x_ath5k_eeprom_mac[5]++;

	if (strncmp(boardid, "sx763", 5) == 0) {
		printk("GIGASX76X: Board id is sx763.");
		memcpy(&gigasx76x_ath5k_eeprom_data, sx763_eeprom_data, ATH5K_PLAT_EEP_MAX_WORDS);
	} else if (strncmp(boardid, "sx762", 5) == 0) {
			printk("GIGASX76X: Board id is sx762.");
			memcpy(&gigasx76x_ath5k_eeprom_data, sx762_eeprom_data, ATH5K_PLAT_EEP_MAX_WORDS);
	} else {
		printk("GIGASX76X: Board id is unknown, fix uboot_env data.");
	}

	return 0;

	error_fail:
		iounmap(uboot_env_page);
		return -EINVAL;
}

static void __init gigasx76x_init(void) {
#define GIGASX76X_USB		29
#define GIGASX76X_MADWIFI_ADDR	0xb07f0000

	ltq_register_gpio_stp();
	ltq_register_nor(&gigasx76x_flash_data);
	ltq_register_pci(&ltq_pci_data);
	ltq_register_tapi();
	ltq_add_device_gpio_leds(-1, ARRAY_SIZE(gigasx76x_gpio_leds), gigasx76x_gpio_leds);
	ltq_register_gpio_keys_polled(-1, LTQ_KEYS_POLL_INTERVAL, ARRAY_SIZE(gigasx76x_gpio_keys), gigasx76x_gpio_keys);
	ltq_register_ath5k(gigasx76x_ath5k_eeprom_data, gigasx76x_ath5k_eeprom_mac);
	xway_register_dwc(GIGASX76X_USB);
	gigasx76x_register_ethernet();
}

MIPS_MACHINE(LANTIQ_MACH_GIGASX76X, "GIGASX76X", "GIGASX76X - Gigaset SX761,SX762,SX763", gigasx76x_init);