ar71xx: WNR612v2: fix for random WLAN MAC
[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
19 #include <asm/mach-ath79/ath79.h>
20 #include <asm/mach-ath79/ar71xx_regs.h> /* needed to disable switch LEDs */
21 #include "common.h" /* needed to disable switch LEDs */
22
23 #include "dev-ap9x-pci.h"
24 #include "dev-eth.h"
25 #include "dev-gpio-buttons.h"
26 #include "dev-leds-gpio.h"
27 #include "dev-m25p80.h"
28 #include "machtypes.h"
29
30 #define WNR2000V3_GPIO_LED_WAN_GREEN    0
31 #define WNR2000V3_GPIO_LED_LAN1_AMBER   1
32 #define WNR2000V3_GPIO_LED_LAN4_AMBER   12
33 #define WNR2000V3_GPIO_LED_PWR_GREEN    14
34 #define WNR2000V3_GPIO_BTN_WPS          11
35
36 #define WNR612V2_GPIO_LED_PWR_GREEN     11
37
38 #define WNR1000V2_GPIO_LED_PWR_AMBER    1
39 #define WNR1000V2_GPIO_LED_PWR_GREEN    11
40
41 /* Connected through AR7240 */
42 #define WPN824N_GPIO_LED_WAN_AMBER      0
43 #define WPN824N_GPIO_LED_STATUS_AMBER   1
44 #define WPN824N_GPIO_LED_LAN1_AMBER     6 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
45 #define WPN824N_GPIO_LED_LAN2_AMBER     7 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
46 #define WPN824N_GPIO_LED_LAN3_AMBER     8 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
47 #define WPN824N_GPIO_LED_LAN4_AMBER     12
48 #define WPN824N_GPIO_LED_LAN1_GREEN     13
49 #define WPN824N_GPIO_LED_LAN2_GREEN     14
50 #define WPN824N_GPIO_LED_LAN3_GREEN     15 /* AR724X_GPIO_FUNC_CLK_OBS3_EN */
51 #define WPN824N_GPIO_LED_LAN4_GREEN     16
52 #define WPN824N_GPIO_LED_WAN_GREEN      17
53
54 /* Connected through AR9285 */
55 #define WPN824N_WGPIO_LED_PWR_GREEN     0
56 #define WPN824N_WGPIO_LED_WLAN_BLUE     1
57 #define WPN824N_WGPIO_LED_WPS1_BLUE     5
58 #define WPN824N_WGPIO_LED_WPS2_BLUE     9
59 #define WPN824N_WGPIO_LED_TEST_AMBER    10
60 #define WPN824N_WGPIO_BTN_PUSH          6       /* currently unused */
61 #define WPN824N_WGPIO_BTN_RESET         7       /* currently unused */
62 #define WPN824N_WGPIO_BTN_WLAN          8       /* currently unused */
63
64 #define WNR2000V3_KEYS_POLL_INTERVAL    20      /* msecs */
65 #define WNR2000V3_KEYS_DEBOUNCE_INTERVAL        (3 * WNR2000V3_KEYS_POLL_INTERVAL)
66
67 /* ART offsets for: WNR2000v3, WNR612v2 */
68 #define WNR2000V3_MAC0_OFFSET           0
69 #define WNR2000V3_MAC1_OFFSET           6
70 #define WNR2000V3_PCIE_CALDATA_OFFSET   0x1000
71 #define WNR2000V3_WMAC_OFFSET           0x108c  /* wireless MAC is inside ART */
72
73 static struct gpio_led wnr2000v3_leds_gpio[] __initdata = {
74         {
75                 .name           = "wnr2000v3:green:power",
76                 .gpio           = WNR2000V3_GPIO_LED_PWR_GREEN,
77                 .active_low     = 1,
78         }, {
79                 .name           = "wnr2000v3:green:wan",
80                 .gpio           = WNR2000V3_GPIO_LED_WAN_GREEN,
81                 .active_low     = 1,
82         }
83 };
84
85 static struct gpio_led wnr612v2_leds_gpio[] __initdata = {
86         {
87                 .name           = "netgear:green:power",
88                 .gpio           = WNR612V2_GPIO_LED_PWR_GREEN,
89                 .active_low     = 1,
90         }
91 };
92
93 static struct gpio_led wnr1000v2_leds_gpio[] __initdata = {
94         {
95                 .name           = "netgear:green:power",
96                 .gpio           = WNR1000V2_GPIO_LED_PWR_GREEN,
97                 .active_low     = 1,
98         }, {
99                 .name           = "netgear:amber:power",
100                 .gpio           = WNR1000V2_GPIO_LED_PWR_AMBER,
101                 .active_low     = 1,
102         }
103 };
104
105 static struct gpio_led wpn824n_leds_gpio[] __initdata = {
106         {
107                 .name           = "netgear:amber:wan",
108                 .gpio           = WPN824N_GPIO_LED_WAN_AMBER,
109                 .active_low     = 1,
110         }, {
111                 .name           = "netgear:amber:status",
112                 .gpio           = WPN824N_GPIO_LED_STATUS_AMBER,
113                 .active_low     = 1,
114         }, {
115                 .name           = "netgear:amber:lan1",
116                 .gpio           = WPN824N_GPIO_LED_LAN1_AMBER,
117                 .active_low     = 1,
118         }, {
119                 .name           = "netgear:amber:lan2",
120                 .gpio           = WPN824N_GPIO_LED_LAN2_AMBER,
121                 .active_low     = 1,
122         }, {
123                 .name           = "netgear:amber:lan3",
124                 .gpio           = WPN824N_GPIO_LED_LAN3_AMBER,
125                 .active_low     = 1,
126         }, {
127                 .name           = "netgear:amber:lan4",
128                 .gpio           = WPN824N_GPIO_LED_LAN4_AMBER,
129                 .active_low     = 1,
130         }, {
131                 .name           = "netgear:green:lan1",
132                 .gpio           = WPN824N_GPIO_LED_LAN1_GREEN,
133                 .active_low     = 1,
134         }, {
135                 .name           = "netgear:green:lan2",
136                 .gpio           = WPN824N_GPIO_LED_LAN2_GREEN,
137                 .active_low     = 1,
138         }, {
139                 .name           = "netgear:green:lan3",
140                 .gpio           = WPN824N_GPIO_LED_LAN3_GREEN,
141                 .active_low     = 1,
142         }, {
143                 .name           = "netgear:green:lan4",
144                 .gpio           = WPN824N_GPIO_LED_LAN4_GREEN,
145                 .active_low     = 1,
146         }, {
147                 .name           = "netgear:green:wan",
148                 .gpio           = WPN824N_GPIO_LED_WAN_GREEN,
149                 .active_low     = 1,
150         }
151 };
152
153 static struct gpio_led wpn824n_wmac_leds_gpio[] = {
154         {
155                 .name           = "netgear:green:power",
156                 .gpio           = WPN824N_WGPIO_LED_PWR_GREEN,
157                 .active_low     = 1,
158         }, {
159                 .name           = "netgear:blue:wlan",
160                 .gpio           = WPN824N_WGPIO_LED_WLAN_BLUE,
161                 .active_low     = 1,
162         }, {
163                 .name           = "netgear:blue:wps1",
164                 .gpio           = WPN824N_WGPIO_LED_WPS1_BLUE,
165                 .active_low     = 1,
166         }, {
167                 .name           = "netgear:blue:wps2",
168                 .gpio           = WPN824N_WGPIO_LED_WPS2_BLUE,
169                 .active_low     = 1,
170         }, {
171                 .name           = "netgear:amber:test",
172                 .gpio           = WPN824N_WGPIO_LED_TEST_AMBER,
173                 .active_low     = 1,
174         }
175 };
176
177 static struct gpio_keys_button wnr2000v3_gpio_keys[] __initdata = {
178         {
179                 .desc           = "wps",
180                 .type           = EV_KEY,
181                 .code           = KEY_WPS_BUTTON,
182                 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
183                 .gpio           = WNR2000V3_GPIO_BTN_WPS,
184         }
185 };
186
187 /*
188  * For WNR2000v3 ART flash area used for WLAN MAC is usually empty (0xff)
189  * so ath9k driver uses random MAC instead each time module is loaded.
190  * To fix that, assign permanent WLAN MAC equal to ethN's MAC plus 1,
191  * so network interfaces get sequential addresses.
192  * If ART wireless MAC address field has been filled by user, use it.
193  */
194 static void __init wnr_get_wmac(u8 *wmac_gen_addr, int mac0_art_offset,
195                                 int mac1_art_offset, int wmac_art_offset)
196 {
197         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
198         u8 *eth0_mac_addr = (u8 *) (art + mac0_art_offset);
199         u8 *eth1_mac_addr = (u8 *) (art + mac1_art_offset);
200         u8 *wlan_mac_addr = (u8 *) (art + wmac_art_offset);
201
202         /* only 0xff if all bits are set - address is invalid, empty area */
203         if ((wlan_mac_addr[0] & wlan_mac_addr[1] & wlan_mac_addr[2] &
204              wlan_mac_addr[3] & wlan_mac_addr[4] & wlan_mac_addr[5]) == 0xff) {
205                 memcpy(wmac_gen_addr, eth0_mac_addr, 5);
206                 wmac_gen_addr[5] = max(eth0_mac_addr[5], eth1_mac_addr[5]) + 1;
207
208                 /* Avoid potential conflict in case max(0xff,0x00)+1==0x00 */
209                 if (!wmac_gen_addr[5])
210                         wmac_gen_addr[5] = 1;
211         } else
212                 memcpy(wmac_gen_addr, wlan_mac_addr, 6);
213 }
214
215 static void __init wnr_common_setup(u8 *wmac_addr)
216 {
217         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
218
219         ath79_register_mdio(0, 0x0);
220
221         ath79_init_mac(ath79_eth0_data.mac_addr, art+WNR2000V3_MAC0_OFFSET, 0);
222         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
223         ath79_eth0_data.speed = SPEED_100;
224         ath79_eth0_data.duplex = DUPLEX_FULL;
225
226         ath79_init_mac(ath79_eth1_data.mac_addr, art+WNR2000V3_MAC1_OFFSET, 0);
227         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
228         ath79_eth1_data.phy_mask = 0x10;
229
230         ath79_register_eth(0);
231         ath79_register_eth(1);
232
233         ath79_register_m25p80(NULL);
234         ap91_pci_init(art + WNR2000V3_PCIE_CALDATA_OFFSET, wmac_addr);
235 }
236
237 static void __init wnr2000v3_setup(void)
238 {
239         u8 wlan_mac_addr[6];
240
241         wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
242                      WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
243
244         wnr_common_setup(wlan_mac_addr);
245
246         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr2000v3_leds_gpio),
247                                  wnr2000v3_leds_gpio);
248
249         ath79_register_gpio_keys_polled(-1, WNR2000V3_KEYS_POLL_INTERVAL,
250                                         ARRAY_SIZE(wnr2000v3_gpio_keys),
251                                         wnr2000v3_gpio_keys);
252 }
253
254 MIPS_MACHINE(ATH79_MACH_WNR2000_V3, "WNR2000V3", "NETGEAR WNR2000 V3", wnr2000v3_setup);
255
256 static void __init wnr612v2_setup(void)
257 {
258         u8 wlan_mac_addr[6];
259
260         wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
261                      WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
262
263         wnr_common_setup(wlan_mac_addr);
264
265         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr612v2_leds_gpio),
266                                  wnr612v2_leds_gpio);
267 }
268
269 MIPS_MACHINE(ATH79_MACH_WNR612_V2, "WNR612V2", "NETGEAR WNR612 V2", wnr612v2_setup);
270
271 static void __init wnr1000v2_setup(void)
272 {
273         wnr_common_setup(NULL);
274
275         ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr1000v2_leds_gpio),
276                                  wnr1000v2_leds_gpio);
277 }
278
279 MIPS_MACHINE(ATH79_MACH_WNR1000_V2, "WNR1000V2", "NETGEAR WNR1000 V2", wnr1000v2_setup);
280
281 static void __init wpn824n_setup(void)
282 {
283         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
284                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
285                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
286                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
287                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
288                                   AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN |
289                                   AR724X_GPIO_FUNC_CLK_OBS3_EN);
290
291         wnr_common_setup(NULL);
292
293         ath79_register_leds_gpio(-1, ARRAY_SIZE(wpn824n_leds_gpio),
294                                  wpn824n_leds_gpio);
295
296         ap9x_pci_setup_wmac_led_pin(0, WPN824N_WGPIO_LED_WLAN_BLUE);
297         ap9x_pci_setup_wmac_leds(0, wpn824n_wmac_leds_gpio,
298                                  ARRAY_SIZE(wpn824n_wmac_leds_gpio));
299 }
300
301 MIPS_MACHINE(ATH79_MACH_WPN824N, "WPN824N", "NETGEAR WPN824N", wpn824n_setup);