[lantiq] dgn3500 support with eeprom loading from sysfs
[openwrt/svn-archive/archive.git] / target / linux / lantiq / files-3.3 / arch / mips / lantiq / xway / mach-netgear.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) 2010 John Crispin <blogic@openwrt.org>
7 * Copyright (C) 2012 Pieter Voorthuijsen <p.voorthuijsen@gmail.com>
8 */
9
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
14 #include <linux/mtd/physmap.h>
15 #include <linux/input.h>
16 #include <linux/phy.h>
17 #include <linux/spi/spi.h>
18 #include <linux/spi/flash.h>
19 #include <linux/spi/spi_gpio.h>
20 #include <linux/ath9k_platform.h>
21 #include <linux/if_ether.h>
22 #include <linux/etherdevice.h>
23 #include <linux/kobject.h>
24 #include <linux/sysfs.h>
25
26 #include <lantiq_soc.h>
27 #include <irq.h>
28 #include <dev-gpio-leds.h>
29 #include <dev-gpio-buttons.h>
30 #include "dev-wifi-athxk.h"
31
32 #include "../machtypes.h"
33 #include "devices.h"
34 #include "dev-dwc_otg.h"
35 #include "pci-ath-fixup.h"
36 #include <mtd/mtd-abi.h>
37 #include <asm-generic/sizes.h>
38
39 static struct mtd_partition dgn3500_partitions[] = {
40 {
41 .name = "u-boot",
42 .offset = 0,
43 .size = 0x10000,
44 .mask_flags = MTD_WRITEABLE,
45 },
46 {
47 .name = "environment",
48 .offset = 0x10000,
49 .size = 0x10000,
50 .mask_flags = MTD_WRITEABLE,
51 },
52 {
53 .name = "calibration",
54 .offset = 0x20000,
55 .size = 0x10000,
56 .mask_flags = MTD_WRITEABLE,
57 },
58 {
59 .name = "linux",
60 .offset = 0x50000,
61 .size = 0xfa0000,
62 },
63 };
64
65 static struct ltq_pci_data ltq_pci_data = {
66 .clock = PCI_CLOCK_INT,
67 .gpio = PCI_GNT1 | PCI_REQ1,
68 .irq = {
69 [14] = INT_NUM_IM0_IRL0 + 22,
70 },
71 };
72
73 static struct ltq_eth_data ltq_eth_data = {
74 .mii_mode = PHY_INTERFACE_MODE_MII,
75 };
76
77 static struct gpio_led
78 dgn3500_gpio_leds[] __initdata = {
79 { .name = "soc:green:power", .gpio = 34, .active_low = 1, },
80 { .name = "soc:red:power", .gpio = 39, .active_low = 1, },
81 { .name = "soc:orange:wlan", .gpio = 51, .active_low = 1, },
82 { .name = "soc:green:wps", .gpio = 52, .active_low = 1, },
83 { .name = "soc:green:usb", .gpio = 22, .active_low = 1, },
84 { .name = "soc:green:dsl", .gpio = 4, .active_low = 1, },
85 { .name = "soc:green:internet", .gpio = 2, .active_low = 1, },
86 };
87
88 static struct gpio_keys_button
89 dgn3500_gpio_keys[] __initdata = {
90 {
91 .desc = "wps",
92 .type = EV_KEY,
93 .code = BTN_0,
94 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
95 .gpio = 54,
96 .active_low = 1,
97 },
98 {
99 .desc = "reset",
100 .type = EV_KEY,
101 .code = BTN_1,
102 .debounce_interval = LTQ_KEYS_DEBOUNCE_INTERVAL,
103 .gpio = 36,
104 .active_low = 1,
105 },
106 };
107
108 #define SPI_GPIO_MRST 16
109 #define SPI_GPIO_MTSR 17
110 #define SPI_GPIO_CLK 18
111 #define SPI_GPIO_CS0 10
112
113 static struct spi_gpio_platform_data spi_gpio_data = {
114 .sck = SPI_GPIO_CLK,
115 .mosi = SPI_GPIO_MTSR,
116 .miso = SPI_GPIO_MRST,
117 .num_chipselect = 2,
118 };
119
120 static struct platform_device spi_gpio_device = {
121 .name = "spi_gpio",
122 .dev.platform_data = &spi_gpio_data,
123 };
124
125 static struct flash_platform_data spi_flash_data = {
126 .name = "sflash",
127 .parts = dgn3500_partitions,
128 .nr_parts = ARRAY_SIZE(dgn3500_partitions),
129 };
130
131 static struct spi_board_info spi_flash __initdata = {
132 .modalias = "m25p80",
133 .bus_num = 0,
134 .chip_select = 0,
135 .max_speed_hz = 10 * 1000 * 1000,
136 .mode = SPI_MODE_3,
137 .chip_select = 0,
138 .controller_data = (void *) SPI_GPIO_CS0,
139 .platform_data = &spi_flash_data
140 };
141
142 static u8 ltq_ethaddr[6] = { 0 };
143
144 static int __init setup_ethaddr(char *str)
145 {
146 if (!mac_pton(str, ltq_ethaddr))
147 memset(ltq_ethaddr, 0, 6);
148 return 0;
149 }
150 __setup("ethaddr=", setup_ethaddr);
151
152 static u16 dgn3500_eeprom_data[ATH9K_PLAT_EEP_MAX_WORDS] = {0};
153
154 static ssize_t ath_eeprom_read(struct file *filp, struct kobject *kobj,
155 struct bin_attribute *attr, char *buf,
156 loff_t offset, size_t count)
157 {
158 if (unlikely(offset >= sizeof(dgn3500_eeprom_data)))
159 return 0;
160 if ((offset + count) > sizeof(dgn3500_eeprom_data))
161 count = sizeof(dgn3500_eeprom_data) - offset;
162 if (unlikely(!count))
163 return count;
164
165 memcpy(buf, (char *)(dgn3500_eeprom_data) + offset, count);
166
167 return count;
168 }
169
170 extern struct ath9k_platform_data ath9k_pdata;
171
172 static ssize_t ath_eeprom_write(struct file *filp, struct kobject *kobj,
173 struct bin_attribute *attr, char *buf,
174 loff_t offset, size_t count)
175 {
176 int i;
177 char *eeprom_bytes = (char *)dgn3500_eeprom_data;
178
179 if (unlikely(offset >= sizeof(dgn3500_eeprom_data)))
180 return -EFBIG;
181 if ((offset + count) > sizeof(dgn3500_eeprom_data))
182 count = sizeof(dgn3500_eeprom_data) - offset;
183 if (unlikely(!count))
184 return count;
185 if (count % 2)
186 return 0;
187
188 /* The PCI fixup routine requires an endian swap of the calibartion data
189 * stored in flash */
190 for (i = 0; i < count; i += 2) {
191 eeprom_bytes[offset + i + 1] = buf[i];
192 eeprom_bytes[offset + i] = buf[i+1];
193 }
194
195 /* The original data does not contain a checksum. Set the country and
196 * calculate new checksum when all data is received */
197 if ((count + offset) == sizeof(dgn3500_eeprom_data))
198 memcpy(ath9k_pdata.eeprom_data, dgn3500_eeprom_data,
199 sizeof(ath9k_pdata.eeprom_data));
200
201 return count;
202 }
203
204 static struct bin_attribute dev_attr_ath_eeprom = {
205 .attr = {
206 .name = "ath_eeprom",
207 .mode = S_IRUGO|S_IWUSR,
208 },
209 .read = ath_eeprom_read,
210 .write = ath_eeprom_write,
211 };
212
213 static void __init dgn3500_init(void)
214 {
215 if (sysfs_create_bin_file(firmware_kobj, &dev_attr_ath_eeprom))
216 printk(KERN_INFO "Failed to create ath eeprom sysfs entry\n");
217 ltq_add_device_gpio_leds(-1, ARRAY_SIZE(dgn3500_gpio_leds),
218 dgn3500_gpio_leds);
219 ltq_register_gpio_keys_polled(-1, LTQ_KEYS_POLL_INTERVAL,
220 ARRAY_SIZE(dgn3500_gpio_keys), dgn3500_gpio_keys);
221 platform_device_register(&spi_gpio_device);
222 ltq_register_pci(&ltq_pci_data);
223 spi_register_board_info(&spi_flash, 1);
224 if (!is_valid_ether_addr(ltq_ethaddr)) {
225 printk(KERN_INFO "MAC invalid using random\n");
226 random_ether_addr(ltq_ethaddr);
227 }
228 memcpy(&ltq_eth_data.mac.sa_data, ltq_ethaddr, 6);
229 ltq_register_etop(&ltq_eth_data);
230 ltq_register_ath9k(dgn3500_eeprom_data, ltq_ethaddr);
231 ltq_pci_ath_fixup(14, dgn3500_eeprom_data);
232 /* The usb power is always enabled, protected by a fuse */
233 xway_register_dwc(-1);
234 }
235
236 MIPS_MACHINE(LANTIQ_MACH_DGN3500B,
237 "DGN3500B",
238 "Netgear DGN3500B",
239 dgn3500_init);