ar71xx: WNR2000v3: enable control of all LEDs and buttons
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-wnr2000-v3.c
1 /*
2  *  NETGEAR WNR2000v3/WNR612v2/WNR1000v2/WPN824N board support
3  *
4  *  Copyright (C) 2015 Hartmut Knaack <knaack.h@gmx.de>
5  *  Copyright (C) 2013 Mathieu Olivari <mathieu.olivari@gmail.com>
6  *  Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
7  *  Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
8  *  Copyright (C) 2008-2009 Andy Boyett <agb@openwrt.org>
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License version 2 as published
12  *  by the Free Software Foundation.
13  */
14
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/kernel.h> /* for max() macro */
18 #include <linux/platform_device.h> /* PLATFORM_DEVID_AUTO is defined here */
19
20 #include <asm/mach-ath79/ath79.h>
21 #include <asm/mach-ath79/ar71xx_regs.h> /* needed to disable switch LEDs */
22 #include "common.h" /* needed to disable switch LEDs */
23
24 #include "dev-ap9x-pci.h"
25 #include "dev-eth.h"
26 #include "dev-gpio-buttons.h"
27 #include "dev-leds-gpio.h"
28 #include "dev-m25p80.h"
29 #include "machtypes.h"
30
31 /* WNR2000v3 - connected through AR7241 */
32 #define WNR2000V3_GPIO_LED_WAN_GREEN    0
33 #define WNR2000V3_GPIO_LED_LAN1_AMBER   1
34 #define WNR2000V3_GPIO_LED_LAN2_AMBER   6
35 #define WNR2000V3_GPIO_LED_WPS_GREEN    7
36 #define WNR2000V3_GPIO_LED_LAN3_AMBER   8
37 #define WNR2000V3_GPIO_BTN_WPS          11
38 #define WNR2000V3_GPIO_LED_LAN4_AMBER   12
39 #define WNR2000V3_GPIO_LED_LAN1_GREEN   13
40 #define WNR2000V3_GPIO_LED_LAN2_GREEN   14
41 #define WNR2000V3_GPIO_LED_LAN3_GREEN   15
42 #define WNR2000V3_GPIO_LED_LAN4_GREEN   16
43 #define WNR2000V3_GPIO_LED_WAN_AMBER    17
44
45 /* WNR2000v3 - connected through AR9287 */
46 #define WNR2000V3_GPIO_WMAC_LED_WLAN_BLUE       1
47 #define WNR2000V3_GPIO_WMAC_LED_TEST_AMBER      2
48 #define WNR2000V3_GPIO_WMAC_LED_POWER_GREEN     3
49 #define WNR2000V3_GPIO_WMAC_BTN_RESET           8
50 #define WNR2000V3_GPIO_WMAC_BTN_RFKILL          9
51
52 #define WNR612V2_GPIO_LED_PWR_GREEN     11
53
54 #define WNR1000V2_GPIO_LED_PWR_AMBER    1
55 #define WNR1000V2_GPIO_LED_PWR_GREEN    11
56
57 /* Connected through AR7240 */
58 #define WPN824N_GPIO_LED_WAN_AMBER      0
59 #define WPN824N_GPIO_LED_STATUS_AMBER   1
60 #define WPN824N_GPIO_LED_LAN1_AMBER     6 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
61 #define WPN824N_GPIO_LED_LAN2_AMBER     7 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
62 #define WPN824N_GPIO_LED_LAN3_AMBER     8 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
63 #define WPN824N_GPIO_LED_LAN4_AMBER     12
64 #define WPN824N_GPIO_LED_LAN1_GREEN     13
65 #define WPN824N_GPIO_LED_LAN2_GREEN     14
66 #define WPN824N_GPIO_LED_LAN3_GREEN     15 /* AR724X_GPIO_FUNC_CLK_OBS3_EN */
67 #define WPN824N_GPIO_LED_LAN4_GREEN     16
68 #define WPN824N_GPIO_LED_WAN_GREEN      17
69
70 /* Connected through AR9285 */
71 #define WPN824N_WGPIO_LED_PWR_GREEN     0
72 #define WPN824N_WGPIO_LED_WLAN_BLUE     1
73 #define WPN824N_WGPIO_LED_WPS1_BLUE     5
74 #define WPN824N_WGPIO_LED_WPS2_BLUE     9
75 #define WPN824N_WGPIO_LED_TEST_AMBER    10
76 #define WPN824N_WGPIO_BTN_PUSH          6       /* currently unused */
77 #define WPN824N_WGPIO_BTN_RESET         7       /* currently unused */
78 #define WPN824N_WGPIO_BTN_WLAN          8       /* currently unused */
79
80 #define WNR2000V3_KEYS_POLL_INTERVAL    20      /* msecs */
81 #define WNR2000V3_KEYS_DEBOUNCE_INTERVAL        (3 * WNR2000V3_KEYS_POLL_INTERVAL)
82
83 /* ART offsets for: WNR2000v3, WNR612v2 */
84 #define WNR2000V3_MAC0_OFFSET           0
85 #define WNR2000V3_MAC1_OFFSET           6
86 #define WNR2000V3_PCIE_CALDATA_OFFSET   0x1000
87 #define WNR2000V3_WMAC_OFFSET           0x108c  /* wireless MAC is inside ART */
88
89 static struct gpio_led wnr2000v3_leds_gpio[] __initdata = {
90         {
91                 .name           = "netgear:green:wan",
92                 .gpio           = WNR2000V3_GPIO_LED_WAN_GREEN,
93                 .active_low     = 1,
94         }, {
95                 .name           = "netgear:amber:lan1",
96                 .gpio           = WNR2000V3_GPIO_LED_LAN1_AMBER,
97                 .active_low     = 1,
98         }, {
99                 .name           = "netgear:amber:lan2",
100                 .gpio           = WNR2000V3_GPIO_LED_LAN2_AMBER,
101                 .active_low     = 1,
102         }, {
103                 .name           = "netgear:amber:lan3",
104                 .gpio           = WNR2000V3_GPIO_LED_LAN3_AMBER,
105                 .active_low     = 1,
106         }, {
107                 .name           = "netgear:amber:lan4",
108                 .gpio           = WNR2000V3_GPIO_LED_LAN4_AMBER,
109                 .active_low     = 1,
110         }, {
111                 .name           = "netgear:green:wps",
112                 .gpio           = WNR2000V3_GPIO_LED_WPS_GREEN,
113                 .active_low     = 1,
114         }, {
115                 .name           = "netgear:green:lan1",
116                 .gpio           = WNR2000V3_GPIO_LED_LAN1_GREEN,
117                 .active_low     = 1,
118         }, {
119                 .name           = "netgear:green:lan2",
120                 .gpio           = WNR2000V3_GPIO_LED_LAN2_GREEN,
121                 .active_low     = 1,
122         }, {
123                 .name           = "netgear:green:lan3",
124                 .gpio           = WNR2000V3_GPIO_LED_LAN3_GREEN,
125                 .active_low     = 1,
126         }, {
127                 .name           = "netgear:green:lan4",
128                 .gpio           = WNR2000V3_GPIO_LED_LAN4_GREEN,
129                 .active_low     = 1,
130         }, {
131                 .name           = "netgear:amber:wan",
132                 .gpio           = WNR2000V3_GPIO_LED_WAN_AMBER,
133                 .active_low     = 1,
134         }
135 };
136
137 static struct gpio_led wnr2000v3_wmac_leds_gpio[] = {
138         {
139                 .name           = "netgear:green:power",
140                 .gpio           = WNR2000V3_GPIO_WMAC_LED_POWER_GREEN,
141                 .active_low     = 1,
142                 .default_state  = LEDS_GPIO_DEFSTATE_ON,
143         }, {
144                 .name           = "netgear:amber:test",
145                 .gpio           = WNR2000V3_GPIO_WMAC_LED_TEST_AMBER,
146                 .active_low     = 1,
147         }
148 };
149
150 static const char *wnr2000v3_wmac_led_name = "netgear:blue:wlan";
151
152 static struct gpio_led wnr612v2_leds_gpio[] __initdata = {
153         {
154                 .name           = "netgear:green:power",
155                 .gpio           = WNR612V2_GPIO_LED_PWR_GREEN,
156                 .active_low     = 1,
157         }
158 };
159
160 static struct gpio_led wnr1000v2_leds_gpio[] __initdata = {
161         {
162                 .name           = "netgear:green:power",
163                 .gpio           = WNR1000V2_GPIO_LED_PWR_GREEN,
164                 .active_low     = 1,
165         }, {
166                 .name           = "netgear:amber:power",
167                 .gpio           = WNR1000V2_GPIO_LED_PWR_AMBER,
168                 .active_low     = 1,
169         }
170 };
171
172 static struct gpio_led wpn824n_leds_gpio[] __initdata = {
173         {
174                 .name           = "netgear:amber:wan",
175                 .gpio           = WPN824N_GPIO_LED_WAN_AMBER,
176                 .active_low     = 1,
177         }, {
178                 .name           = "netgear:amber:status",
179                 .gpio           = WPN824N_GPIO_LED_STATUS_AMBER,
180                 .active_low     = 1,
181         }, {
182                 .name           = "netgear:amber:lan1",
183                 .gpio           = WPN824N_GPIO_LED_LAN1_AMBER,
184                 .active_low     = 1,
185         }, {
186                 .name           = "netgear:amber:lan2",
187                 .gpio           = WPN824N_GPIO_LED_LAN2_AMBER,
188                 .active_low     = 1,
189         }, {
190                 .name           = "netgear:amber:lan3",
191                 .gpio           = WPN824N_GPIO_LED_LAN3_AMBER,
192                 .active_low     = 1,
193         }, {
194                 .name           = "netgear:amber:lan4",
195                 .gpio           = WPN824N_GPIO_LED_LAN4_AMBER,
196                 .active_low     = 1,
197         }, {
198                 .name           = "netgear:green:lan1",
199                 .gpio           = WPN824N_GPIO_LED_LAN1_GREEN,
200                 .active_low     = 1,
201         }, {
202                 .name           = "netgear:green:lan2",
203                 .gpio           = WPN824N_GPIO_LED_LAN2_GREEN,
204                 .active_low     = 1,
205         }, {
206                 .name           = "netgear:green:lan3",
207                 .gpio           = WPN824N_GPIO_LED_LAN3_GREEN,
208                 .active_low     = 1,
209         }, {
210                 .name           = "netgear:green:lan4",
211                 .gpio           = WPN824N_GPIO_LED_LAN4_GREEN,
212                 .active_low     = 1,
213         }, {
214                 .name           = "netgear:green:wan",
215                 .gpio           = WPN824N_GPIO_LED_WAN_GREEN,
216                 .active_low     = 1,
217         }
218 };
219
220 static struct gpio_led wpn824n_wmac_leds_gpio[] = {
221         {
222                 .name           = "netgear:green:power",
223                 .gpio           = WPN824N_WGPIO_LED_PWR_GREEN,
224                 .active_low     = 1,
225         }, {
226                 .name           = "netgear:blue:wlan",
227                 .gpio           = WPN824N_WGPIO_LED_WLAN_BLUE,
228                 .active_low     = 1,
229         }, {
230                 .name           = "netgear:blue:wps1",
231                 .gpio           = WPN824N_WGPIO_LED_WPS1_BLUE,
232                 .active_low     = 1,
233         }, {
234                 .name           = "netgear:blue:wps2",
235                 .gpio           = WPN824N_WGPIO_LED_WPS2_BLUE,
236                 .active_low     = 1,
237         }, {
238                 .name           = "netgear:amber:test",
239                 .gpio           = WPN824N_WGPIO_LED_TEST_AMBER,
240                 .active_low     = 1,
241         }
242 };
243
244 static struct gpio_keys_button wnr2000v3_keys_gpio[] __initdata = {
245         {
246                 .desc           = "wps",
247                 .type           = EV_KEY,
248                 .code           = KEY_WPS_BUTTON,
249                 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
250                 .gpio           = WNR2000V3_GPIO_BTN_WPS,
251                 .active_low     = 1,
252         }
253 };
254
255 static struct gpio_keys_button wnr2000v3_wmac_keys_gpio[] = {
256         {
257                 .desc           = "reset",
258                 .type           = EV_KEY,
259                 .code           = KEY_RESTART,
260                 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
261                 .gpio           = WNR2000V3_GPIO_WMAC_BTN_RESET,
262                 .active_low     = 1,
263         }, {
264                 .desc           = "rfkill",
265                 .type           = EV_KEY,
266                 .code           = KEY_RFKILL,
267                 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
268                 .gpio           = WNR2000V3_GPIO_WMAC_BTN_RFKILL,
269                 .active_low     = 1,
270         }
271 };
272
273 /*
274  * For WNR2000v3 ART flash area used for WLAN MAC is usually empty (0xff)
275  * so ath9k driver uses random MAC instead each time module is loaded.
276  * To fix that, assign permanent WLAN MAC equal to ethN's MAC plus 1,
277  * so network interfaces get sequential addresses.
278  * If ART wireless MAC address field has been filled by user, use it.
279  */
280 static void __init wnr_get_wmac(u8 *wmac_gen_addr, int mac0_art_offset,
281                                 int mac1_art_offset, int wmac_art_offset)
282 {
283         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
284         u8 *eth0_mac_addr = (u8 *) (art + mac0_art_offset);
285         u8 *eth1_mac_addr = (u8 *) (art + mac1_art_offset);
286         u8 *wlan_mac_addr = (u8 *) (art + wmac_art_offset);
287
288         /* only 0xff if all bits are set - address is invalid, empty area */
289         if ((wlan_mac_addr[0] & wlan_mac_addr[1] & wlan_mac_addr[2] &
290              wlan_mac_addr[3] & wlan_mac_addr[4] & wlan_mac_addr[5]) == 0xff) {
291                 memcpy(wmac_gen_addr, eth0_mac_addr, 5);
292                 wmac_gen_addr[5] = max(eth0_mac_addr[5], eth1_mac_addr[5]) + 1;
293
294                 /* Avoid potential conflict in case max(0xff,0x00)+1==0x00 */
295                 if (!wmac_gen_addr[5])
296                         wmac_gen_addr[5] = 1;
297         } else
298                 memcpy(wmac_gen_addr, wlan_mac_addr, 6);
299 }
300
301 static void __init wnr_common_setup(u8 *wmac_addr)
302 {
303         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
304
305         ath79_register_mdio(0, 0x0);
306
307         ath79_init_mac(ath79_eth0_data.mac_addr, art+WNR2000V3_MAC0_OFFSET, 0);
308         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
309         ath79_eth0_data.speed = SPEED_100;
310         ath79_eth0_data.duplex = DUPLEX_FULL;
311
312         ath79_init_mac(ath79_eth1_data.mac_addr, art+WNR2000V3_MAC1_OFFSET, 0);
313         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
314         ath79_eth1_data.phy_mask = 0x10;
315
316         ath79_register_eth(0);
317         ath79_register_eth(1);
318
319         ath79_register_m25p80(NULL);
320         ap91_pci_init(art + WNR2000V3_PCIE_CALDATA_OFFSET, wmac_addr);
321 }
322
323 static void __init wnr2000v3_setup(void)
324 {
325         u8 wlan_mac_addr[6];
326
327         /*
328          * Disable JTAG to use all AR724X GPIO LEDs.
329          * Also disable CLKs and bit 20 as u-boot does.
330          * Finally, allow OS to control all link LEDs.
331          */
332         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE |
333                                   AR724X_GPIO_FUNC_UART_EN,
334                                   AR724X_GPIO_FUNC_CLK_OBS1_EN |
335                                   AR724X_GPIO_FUNC_CLK_OBS2_EN |
336                                   AR724X_GPIO_FUNC_CLK_OBS3_EN |
337                                   AR724X_GPIO_FUNC_CLK_OBS4_EN |
338                                   AR724X_GPIO_FUNC_CLK_OBS5_EN |
339                                   AR724X_GPIO_FUNC_GE0_MII_CLK_EN |
340                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
341                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
342                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
343                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
344                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN |
345                                   BIT(20));
346
347         wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
348                      WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
349
350         wnr_common_setup(wlan_mac_addr);
351
352         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr2000v3_leds_gpio),
353                                  wnr2000v3_leds_gpio);
354
355         /* Do not use id=-1, we can have more GPIO key-polled devices */
356         ath79_register_gpio_keys_polled(PLATFORM_DEVID_AUTO,
357                                         WNR2000V3_KEYS_POLL_INTERVAL,
358                                         ARRAY_SIZE(wnr2000v3_keys_gpio),
359                                         wnr2000v3_keys_gpio);
360
361         ap9x_pci_setup_wmac_led_pin(0, WNR2000V3_GPIO_WMAC_LED_WLAN_BLUE);
362         ap9x_pci_setup_wmac_led_name(0, wnr2000v3_wmac_led_name);
363
364         ap9x_pci_setup_wmac_leds(0, wnr2000v3_wmac_leds_gpio,
365                                  ARRAY_SIZE(wnr2000v3_wmac_leds_gpio));
366
367         ap9x_pci_setup_wmac_btns(0, wnr2000v3_wmac_keys_gpio,
368                                  ARRAY_SIZE(wnr2000v3_wmac_keys_gpio),
369                                  WNR2000V3_KEYS_POLL_INTERVAL);
370 }
371
372 MIPS_MACHINE(ATH79_MACH_WNR2000_V3, "WNR2000V3", "NETGEAR WNR2000 V3", wnr2000v3_setup);
373
374 static void __init wnr612v2_setup(void)
375 {
376         u8 wlan_mac_addr[6];
377
378         wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
379                      WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
380
381         wnr_common_setup(wlan_mac_addr);
382
383         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr612v2_leds_gpio),
384                                  wnr612v2_leds_gpio);
385 }
386
387 MIPS_MACHINE(ATH79_MACH_WNR612_V2, "WNR612V2", "NETGEAR WNR612 V2", wnr612v2_setup);
388
389 static void __init wnr1000v2_setup(void)
390 {
391         wnr_common_setup(NULL);
392
393         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr1000v2_leds_gpio),
394                                  wnr1000v2_leds_gpio);
395 }
396
397 MIPS_MACHINE(ATH79_MACH_WNR1000_V2, "WNR1000V2", "NETGEAR WNR1000 V2", wnr1000v2_setup);
398
399 static void __init wpn824n_setup(void)
400 {
401         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
402                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
403                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
404                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
405                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
406                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN |
407                                   AR724X_GPIO_FUNC_CLK_OBS3_EN);
408
409         wnr_common_setup(NULL);
410
411         ath79_register_leds_gpio(-1, ARRAY_SIZE(wpn824n_leds_gpio),
412                                  wpn824n_leds_gpio);
413
414         ap9x_pci_setup_wmac_led_pin(0, WPN824N_WGPIO_LED_WLAN_BLUE);
415         ap9x_pci_setup_wmac_leds(0, wpn824n_wmac_leds_gpio,
416                                  ARRAY_SIZE(wpn824n_wmac_leds_gpio));
417 }
418
419 MIPS_MACHINE(ATH79_MACH_WPN824N, "WPN824N", "NETGEAR WPN824N", wpn824n_setup);