ar71xx: add common m25p80 device
[librecmc/librecmc.git] / target / linux / ar71xx / files / arch / mips / ar71xx / mach-wndr3700.c
1 /*
2  *  Netgear WNDR3700 board support
3  *
4  *  Copyright (C) 2009 Marco Porsch
5  *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11
12 #include <linux/platform_device.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
15 #include <linux/input.h>
16 #include <linux/pci.h>
17 #include <linux/ath9k_platform.h>
18 #include <linux/delay.h>
19 #include <linux/rtl8366_smi.h>
20
21 #include <asm/mips_machine.h>
22 #include <asm/mach-ar71xx/ar71xx.h>
23 #include <asm/mach-ar71xx/pci.h>
24
25 #include "devices.h"
26 #include "dev-m25p80.h"
27
28 #define WNDR3700_GPIO_LED_WPS_ORANGE    0
29 #define WNDR3700_GPIO_LED_POWER_ORANGE  1
30 #define WNDR3700_GPIO_LED_POWER_GREEN   2
31 #define WNDR3700_GPIO_LED_WPS_GREEN     4
32
33 #define WNDR3700_GPIO_BTN_WPS           3
34 #define WNDR3700_GPIO_BTN_RESET         8
35 #define WNDR3700_GPIO_BTN_WIFI          11
36
37 #define WNDR3700_GPIO_RTL8366_SDA       5
38 #define WNDR3700_GPIO_RTL8366_SCK       7
39
40 #define WNDR3700_BUTTONS_POLL_INTERVAL    20
41
42 #ifdef CONFIG_MTD_PARTITIONS
43 static struct mtd_partition wndr3700_partitions[] = {
44         {
45                 .name           = "uboot",
46                 .offset         = 0,
47                 .size           = 0x050000,
48                 .mask_flags     = MTD_WRITEABLE,
49         } , {
50                 .name           = "env",
51                 .offset         = 0x050000,
52                 .size           = 0x020000,
53                 .mask_flags     = MTD_WRITEABLE,
54         } , {
55                 .name           = "rootfs",
56                 .offset         = 0x070000,
57                 .size           = 0x720000,
58         } , {
59                 .name           = "config",
60                 .offset         = 0x790000,
61                 .size           = 0x010000,
62                 .mask_flags     = MTD_WRITEABLE,
63         } , {
64                 .name           = "config_bak",
65                 .offset         = 0x7a0000,
66                 .size           = 0x010000,
67                 .mask_flags     = MTD_WRITEABLE,
68         } , {
69                 .name           = "pot",
70                 .offset         = 0x7b0000,
71                 .size           = 0x010000,
72                 .mask_flags     = MTD_WRITEABLE,
73         } , {
74                 .name           = "traffic_meter",
75                 .offset         = 0x7c0000,
76                 .size           = 0x010000,
77                 .mask_flags     = MTD_WRITEABLE,
78         } , {
79                 .name           = "language",
80                 .offset         = 0x7d0000,
81                 .size           = 0x020000,
82                 .mask_flags     = MTD_WRITEABLE,
83         } , {
84                 .name           = "caldata",
85                 .offset         = 0x7f0000,
86                 .size           = 0x010000,
87                 .mask_flags     = MTD_WRITEABLE,
88         }
89 };
90 #endif /* CONFIG_MTD_PARTITIONS */
91
92 static struct flash_platform_data wndr3700_flash_data = {
93 #ifdef CONFIG_MTD_PARTITIONS
94         .parts          = wndr3700_partitions,
95         .nr_parts       = ARRAY_SIZE(wndr3700_partitions),
96 #endif
97 };
98
99 #ifdef CONFIG_PCI
100 static struct ar71xx_pci_irq wndr3700_pci_irqs[] __initdata = {
101         {
102                 .slot   = 0,
103                 .pin    = 1,
104                 .irq    = AR71XX_PCI_IRQ_DEV0,
105         }, {
106                 .slot   = 1,
107                 .pin    = 1,
108                 .irq    = AR71XX_PCI_IRQ_DEV1,
109         }
110 };
111
112 static struct ath9k_platform_data wndr3700_wmac0_data;
113 static u8 wndr3700_wmac0_macaddr[6];
114 static struct ath9k_platform_data wndr3700_wmac1_data;
115 static u8 wndr3700_wmac1_macaddr[6];
116
117 static void wndr3700_pci_fixup(struct pci_dev *dev)
118 {
119         void __iomem *mem;
120         u16 *cal_data;
121         u16 cmd;
122         u32 bar0;
123         u32 val;
124
125         if (ar71xx_mach != AR71XX_MACH_WNDR3700)
126                 return;
127
128         switch (PCI_SLOT(dev->devfn)) {
129         case 17:
130                 cal_data = wndr3700_wmac0_data.eeprom_data;
131                 break;
132         case 18:
133                 cal_data = wndr3700_wmac1_data.eeprom_data;
134                 break;
135         default:
136                 return;
137         }
138
139         if (*cal_data != 0xa55a) {
140                 printk(KERN_ERR "PCI: no calibration data found for %s\n",
141                        pci_name(dev));
142                 return;
143         }
144
145         mem = ioremap(AR71XX_PCI_MEM_BASE, 0x10000);
146         if (!mem) {
147                 printk(KERN_ERR "PCI: ioremap error for device %s\n",
148                        pci_name(dev));
149                 return;
150         }
151
152         printk(KERN_INFO "PCI: fixup device %s\n", pci_name(dev));
153
154         pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &bar0);
155
156         /* Setup the PCI device to allow access to the internal registers */
157         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, AR71XX_PCI_MEM_BASE);
158         pci_read_config_word(dev, PCI_COMMAND, &cmd);
159         cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
160         pci_write_config_word(dev, PCI_COMMAND, cmd);
161
162         /* set pointer to first reg address */
163         cal_data += 3;
164         while (*cal_data != 0xffff) {
165                 u32 reg;
166                 reg = *cal_data++;
167                 val = *cal_data++;
168                 val |= (*cal_data++) << 16;
169
170                 __raw_writel(val, mem + reg);
171                 udelay(100);
172         }
173
174         pci_read_config_dword(dev, PCI_VENDOR_ID, &val);
175         dev->vendor = val & 0xffff;
176         dev->device = (val >> 16) & 0xffff;
177
178         pci_read_config_dword(dev, PCI_CLASS_REVISION, &val);
179         dev->revision = val & 0xff;
180         dev->class = val >> 8; /* upper 3 bytes */
181
182         pci_read_config_word(dev, PCI_COMMAND, &cmd);
183         cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
184         pci_write_config_word(dev, PCI_COMMAND, cmd);
185
186         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
187
188         iounmap(mem);
189 }
190 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_ATHEROS, PCI_ANY_ID,
191                         wndr3700_pci_fixup);
192
193 static int wndr3700_pci_plat_dev_init(struct pci_dev *dev)
194 {
195         switch (PCI_SLOT(dev->devfn)) {
196         case 17:
197                 dev->dev.platform_data = &wndr3700_wmac0_data;
198                 break;
199         case 18:
200                 dev->dev.platform_data = &wndr3700_wmac1_data;
201                 break;
202         }
203
204         return 0;
205 }
206
207 static void __init wndr3700_pci_init(void)
208 {
209         u8 *ee = (u8 *) KSEG1ADDR(0x1fff0000);
210
211         memcpy(wndr3700_wmac0_data.eeprom_data, ee + 0x1000,
212                sizeof(wndr3700_wmac0_data.eeprom_data));
213         memcpy(wndr3700_wmac0_macaddr, ee, sizeof(wndr3700_wmac0_macaddr));
214         wndr3700_wmac0_data.macaddr = wndr3700_wmac0_macaddr;
215
216         memcpy(wndr3700_wmac1_data.eeprom_data, ee + 0x5000,
217                sizeof(wndr3700_wmac1_data.eeprom_data));
218         memcpy(wndr3700_wmac1_macaddr, ee + 12, sizeof(wndr3700_wmac1_macaddr));
219         wndr3700_wmac1_data.macaddr = wndr3700_wmac1_macaddr;
220
221         ar71xx_pci_plat_dev_init = wndr3700_pci_plat_dev_init;
222         ar71xx_pci_init(ARRAY_SIZE(wndr3700_pci_irqs), wndr3700_pci_irqs);
223 }
224 #else
225 static inline void wndr3700_pci_init(void) { };
226 #endif /* CONFIG_PCI */
227
228 static struct gpio_led wndr3700_leds_gpio[] __initdata = {
229         {
230                 .name           = "wndr3700:green:power",
231                 .gpio           = WNDR3700_GPIO_LED_POWER_GREEN,
232                 .active_low     = 1,
233         }, {
234                 .name           = "wndr3700:orange:power",
235                 .gpio           = WNDR3700_GPIO_LED_POWER_ORANGE,
236                 .active_low     = 1,
237         }, {
238                 .name           = "wndr3700:green:wps",
239                 .gpio           = WNDR3700_GPIO_LED_WPS_GREEN,
240                 .active_low     = 1,
241         }, {
242                 .name           = "wndr3700:orange:wps",
243                 .gpio           = WNDR3700_GPIO_LED_WPS_ORANGE,
244                 .active_low     = 1,
245         }
246 };
247
248 static struct gpio_button wndr3700_gpio_buttons[] __initdata = {
249         {
250                 .desc           = "reset",
251                 .type           = EV_KEY,
252                 .code           = BTN_0,
253                 .threshold      = 5,
254                 .gpio           = WNDR3700_GPIO_BTN_RESET,
255         }, {
256                 .desc           = "wps",
257                 .type           = EV_KEY,
258                 .code           = BTN_1,
259                 .threshold      = 5,
260                 .gpio           = WNDR3700_GPIO_BTN_WPS,
261         } , {
262                 .desc           = "wifi",
263                 .type           = EV_KEY,
264                 .code           = BTN_2,
265                 .threshold      = 5,
266                 .gpio           = WNDR3700_GPIO_BTN_WIFI,
267         }
268 };
269
270 static struct rtl8366_smi_platform_data wndr3700_rtl8366_smi_data = {
271         .gpio_sda        = WNDR3700_GPIO_RTL8366_SDA,
272         .gpio_sck        = WNDR3700_GPIO_RTL8366_SCK,
273 };
274
275 static struct platform_device wndr3700_rtl8366_smi_device = {
276         .name           = "rtl8366-smi",
277         .id             = -1,
278         .dev = {
279                 .platform_data  = &wndr3700_rtl8366_smi_data,
280         }
281 };
282
283 static void __init wndr3700_setup(void)
284 {
285         u8 *mac = (u8 *) KSEG1ADDR(0x1fff0000);
286
287         ar71xx_set_mac_base(mac);
288
289         ar71xx_eth0_pll_data.pll_1000 = 0x11110000;
290         ar71xx_eth0_data.mii_bus_dev = &wndr3700_rtl8366_smi_device.dev;
291         ar71xx_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
292         ar71xx_eth0_data.phy_mask = 0xf;
293         ar71xx_eth0_data.speed = SPEED_1000;
294         ar71xx_eth0_data.duplex = DUPLEX_FULL;
295
296         ar71xx_eth1_pll_data.pll_1000 = 0x11110000;
297         ar71xx_eth1_data.mii_bus_dev = &wndr3700_rtl8366_smi_device.dev;
298         ar71xx_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
299         ar71xx_eth1_data.phy_mask = 0x10;
300
301         ar71xx_add_device_eth(0);
302         ar71xx_add_device_eth(1);
303
304         ar71xx_add_device_usb();
305
306         ar71xx_add_device_m25p80(&wndr3700_flash_data);
307
308         ar71xx_add_device_leds_gpio(-1, ARRAY_SIZE(wndr3700_leds_gpio),
309                                     wndr3700_leds_gpio);
310
311         ar71xx_add_device_gpio_buttons(-1, WNDR3700_BUTTONS_POLL_INTERVAL,
312                                       ARRAY_SIZE(wndr3700_gpio_buttons),
313                                       wndr3700_gpio_buttons);
314
315         platform_device_register(&wndr3700_rtl8366_smi_device);
316         platform_device_register_simple("wndr3700-led-usb", -1, NULL, 0);
317         wndr3700_pci_init();
318 }
319
320 MIPS_MACHINE(AR71XX_MACH_WNDR3700, "NETGEAR WNDR3700", wndr3700_setup);