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