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