ar71xx: WNR1000v2: fix for random WLAN MAC
[openwrt/svn-archive/archive.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 /* WNR612v2 - connected through AR7241 */
53 #define WNR612V2_GPIO_LED_POWER_GREEN 11
54 #define WNR612V2_GPIO_LED_LAN1_GREEN 13
55 #define WNR612V2_GPIO_LED_LAN2_GREEN 14
56 #define WNR612V2_GPIO_LED_WAN_GREEN 17
57
58 /* WNR612v2 - connected through AR9285 */
59 #define WNR612V2_GPIO_WMAC_LED_WLAN_GREEN 1
60 #define WNR612V2_GPIO_WMAC_BTN_RESET 7
61
62 #define WNR1000V2_GPIO_LED_PWR_AMBER 1
63 #define WNR1000V2_GPIO_LED_PWR_GREEN 11
64
65 /* Connected through AR7240 */
66 #define WPN824N_GPIO_LED_WAN_AMBER 0
67 #define WPN824N_GPIO_LED_STATUS_AMBER 1
68 #define WPN824N_GPIO_LED_LAN1_AMBER 6 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
69 #define WPN824N_GPIO_LED_LAN2_AMBER 7 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
70 #define WPN824N_GPIO_LED_LAN3_AMBER 8 /* AR724X_GPIO_FUNC_JTAG_DISABLE */
71 #define WPN824N_GPIO_LED_LAN4_AMBER 12
72 #define WPN824N_GPIO_LED_LAN1_GREEN 13
73 #define WPN824N_GPIO_LED_LAN2_GREEN 14
74 #define WPN824N_GPIO_LED_LAN3_GREEN 15 /* AR724X_GPIO_FUNC_CLK_OBS3_EN */
75 #define WPN824N_GPIO_LED_LAN4_GREEN 16
76 #define WPN824N_GPIO_LED_WAN_GREEN 17
77
78 /* Connected through AR9285 */
79 #define WPN824N_WGPIO_LED_PWR_GREEN 0
80 #define WPN824N_WGPIO_LED_WLAN_BLUE 1
81 #define WPN824N_WGPIO_LED_WPS1_BLUE 5
82 #define WPN824N_WGPIO_LED_WPS2_BLUE 9
83 #define WPN824N_WGPIO_LED_TEST_AMBER 10
84 #define WPN824N_WGPIO_BTN_PUSH 6 /* currently unused */
85 #define WPN824N_WGPIO_BTN_RESET 7 /* currently unused */
86 #define WPN824N_WGPIO_BTN_WLAN 8 /* currently unused */
87
88 #define WNR2000V3_KEYS_POLL_INTERVAL 20 /* msecs */
89 #define WNR2000V3_KEYS_DEBOUNCE_INTERVAL (3 * WNR2000V3_KEYS_POLL_INTERVAL)
90
91 /* ART offsets for: WNR2000v3, WNR612v2, WNR1000v2 */
92 #define WNR2000V3_MAC0_OFFSET 0
93 #define WNR2000V3_MAC1_OFFSET 6
94 #define WNR2000V3_PCIE_CALDATA_OFFSET 0x1000
95 #define WNR2000V3_WMAC_OFFSET 0x108c /* wireless MAC is inside ART */
96
97 static struct gpio_led wnr2000v3_leds_gpio[] __initdata = {
98 {
99 .name = "netgear:green:wan",
100 .gpio = WNR2000V3_GPIO_LED_WAN_GREEN,
101 .active_low = 1,
102 }, {
103 .name = "netgear:amber:lan1",
104 .gpio = WNR2000V3_GPIO_LED_LAN1_AMBER,
105 .active_low = 1,
106 }, {
107 .name = "netgear:amber:lan2",
108 .gpio = WNR2000V3_GPIO_LED_LAN2_AMBER,
109 .active_low = 1,
110 }, {
111 .name = "netgear:amber:lan3",
112 .gpio = WNR2000V3_GPIO_LED_LAN3_AMBER,
113 .active_low = 1,
114 }, {
115 .name = "netgear:amber:lan4",
116 .gpio = WNR2000V3_GPIO_LED_LAN4_AMBER,
117 .active_low = 1,
118 }, {
119 .name = "netgear:green:wps",
120 .gpio = WNR2000V3_GPIO_LED_WPS_GREEN,
121 .active_low = 1,
122 }, {
123 .name = "netgear:green:lan1",
124 .gpio = WNR2000V3_GPIO_LED_LAN1_GREEN,
125 .active_low = 1,
126 }, {
127 .name = "netgear:green:lan2",
128 .gpio = WNR2000V3_GPIO_LED_LAN2_GREEN,
129 .active_low = 1,
130 }, {
131 .name = "netgear:green:lan3",
132 .gpio = WNR2000V3_GPIO_LED_LAN3_GREEN,
133 .active_low = 1,
134 }, {
135 .name = "netgear:green:lan4",
136 .gpio = WNR2000V3_GPIO_LED_LAN4_GREEN,
137 .active_low = 1,
138 }, {
139 .name = "netgear:amber:wan",
140 .gpio = WNR2000V3_GPIO_LED_WAN_AMBER,
141 .active_low = 1,
142 }
143 };
144
145 static struct gpio_led wnr2000v3_wmac_leds_gpio[] = {
146 {
147 .name = "netgear:green:power",
148 .gpio = WNR2000V3_GPIO_WMAC_LED_POWER_GREEN,
149 .active_low = 1,
150 .default_state = LEDS_GPIO_DEFSTATE_ON,
151 }, {
152 .name = "netgear:amber:test",
153 .gpio = WNR2000V3_GPIO_WMAC_LED_TEST_AMBER,
154 .active_low = 1,
155 }
156 };
157
158 static const char *wnr2000v3_wmac_led_name = "netgear:blue:wlan";
159
160 static struct gpio_led wnr612v2_leds_gpio[] __initdata = {
161 {
162 .name = "netgear:green:power",
163 .gpio = WNR612V2_GPIO_LED_POWER_GREEN,
164 .active_low = 1,
165 }, {
166 .name = "netgear:green:lan1",
167 .gpio = WNR612V2_GPIO_LED_LAN1_GREEN,
168 .active_low = 1,
169 }, {
170 .name = "netgear:green:lan2",
171 .gpio = WNR612V2_GPIO_LED_LAN2_GREEN,
172 .active_low = 1,
173 }, {
174 .name = "netgear:green:wan",
175 .gpio = WNR612V2_GPIO_LED_WAN_GREEN,
176 .active_low = 1,
177 }
178 };
179
180 static const char *wnr612v2_wmac_led_name = "netgear:green:wlan";
181
182 static struct gpio_led wnr1000v2_leds_gpio[] __initdata = {
183 {
184 .name = "netgear:green:power",
185 .gpio = WNR1000V2_GPIO_LED_PWR_GREEN,
186 .active_low = 1,
187 }, {
188 .name = "netgear:amber:power",
189 .gpio = WNR1000V2_GPIO_LED_PWR_AMBER,
190 .active_low = 1,
191 }
192 };
193
194 static struct gpio_led wpn824n_leds_gpio[] __initdata = {
195 {
196 .name = "netgear:amber:wan",
197 .gpio = WPN824N_GPIO_LED_WAN_AMBER,
198 .active_low = 1,
199 }, {
200 .name = "netgear:amber:status",
201 .gpio = WPN824N_GPIO_LED_STATUS_AMBER,
202 .active_low = 1,
203 }, {
204 .name = "netgear:amber:lan1",
205 .gpio = WPN824N_GPIO_LED_LAN1_AMBER,
206 .active_low = 1,
207 }, {
208 .name = "netgear:amber:lan2",
209 .gpio = WPN824N_GPIO_LED_LAN2_AMBER,
210 .active_low = 1,
211 }, {
212 .name = "netgear:amber:lan3",
213 .gpio = WPN824N_GPIO_LED_LAN3_AMBER,
214 .active_low = 1,
215 }, {
216 .name = "netgear:amber:lan4",
217 .gpio = WPN824N_GPIO_LED_LAN4_AMBER,
218 .active_low = 1,
219 }, {
220 .name = "netgear:green:lan1",
221 .gpio = WPN824N_GPIO_LED_LAN1_GREEN,
222 .active_low = 1,
223 }, {
224 .name = "netgear:green:lan2",
225 .gpio = WPN824N_GPIO_LED_LAN2_GREEN,
226 .active_low = 1,
227 }, {
228 .name = "netgear:green:lan3",
229 .gpio = WPN824N_GPIO_LED_LAN3_GREEN,
230 .active_low = 1,
231 }, {
232 .name = "netgear:green:lan4",
233 .gpio = WPN824N_GPIO_LED_LAN4_GREEN,
234 .active_low = 1,
235 }, {
236 .name = "netgear:green:wan",
237 .gpio = WPN824N_GPIO_LED_WAN_GREEN,
238 .active_low = 1,
239 }
240 };
241
242 static struct gpio_led wpn824n_wmac_leds_gpio[] = {
243 {
244 .name = "netgear:green:power",
245 .gpio = WPN824N_WGPIO_LED_PWR_GREEN,
246 .active_low = 1,
247 }, {
248 .name = "netgear:blue:wlan",
249 .gpio = WPN824N_WGPIO_LED_WLAN_BLUE,
250 .active_low = 1,
251 }, {
252 .name = "netgear:blue:wps1",
253 .gpio = WPN824N_WGPIO_LED_WPS1_BLUE,
254 .active_low = 1,
255 }, {
256 .name = "netgear:blue:wps2",
257 .gpio = WPN824N_WGPIO_LED_WPS2_BLUE,
258 .active_low = 1,
259 }, {
260 .name = "netgear:amber:test",
261 .gpio = WPN824N_WGPIO_LED_TEST_AMBER,
262 .active_low = 1,
263 }
264 };
265
266 static struct gpio_keys_button wnr2000v3_keys_gpio[] __initdata = {
267 {
268 .desc = "wps",
269 .type = EV_KEY,
270 .code = KEY_WPS_BUTTON,
271 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
272 .gpio = WNR2000V3_GPIO_BTN_WPS,
273 .active_low = 1,
274 }
275 };
276
277 static struct gpio_keys_button wnr2000v3_wmac_keys_gpio[] = {
278 {
279 .desc = "reset",
280 .type = EV_KEY,
281 .code = KEY_RESTART,
282 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
283 .gpio = WNR2000V3_GPIO_WMAC_BTN_RESET,
284 .active_low = 1,
285 }, {
286 .desc = "rfkill",
287 .type = EV_KEY,
288 .code = KEY_RFKILL,
289 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
290 .gpio = WNR2000V3_GPIO_WMAC_BTN_RFKILL,
291 .active_low = 1,
292 }
293 };
294
295 static struct gpio_keys_button wnr612v2_wmac_keys_gpio[] = {
296 {
297 .desc = "reset",
298 .type = EV_KEY,
299 .code = KEY_RESTART,
300 .debounce_interval = WNR2000V3_KEYS_DEBOUNCE_INTERVAL,
301 .gpio = WNR612V2_GPIO_WMAC_BTN_RESET,
302 .active_low = 1,
303 }
304 };
305
306 /*
307 * For WNR2000v3 ART flash area used for WLAN MAC is usually empty (0xff)
308 * so ath9k driver uses random MAC instead each time module is loaded.
309 * To fix that, assign permanent WLAN MAC equal to ethN's MAC plus 1,
310 * so network interfaces get sequential addresses.
311 * If ART wireless MAC address field has been filled by user, use it.
312 */
313 static void __init wnr_get_wmac(u8 *wmac_gen_addr, int mac0_art_offset,
314 int mac1_art_offset, int wmac_art_offset)
315 {
316 u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
317 u8 *eth0_mac_addr = (u8 *) (art + mac0_art_offset);
318 u8 *eth1_mac_addr = (u8 *) (art + mac1_art_offset);
319 u8 *wlan_mac_addr = (u8 *) (art + wmac_art_offset);
320
321 /* only 0xff if all bits are set - address is invalid, empty area */
322 if ((wlan_mac_addr[0] & wlan_mac_addr[1] & wlan_mac_addr[2] &
323 wlan_mac_addr[3] & wlan_mac_addr[4] & wlan_mac_addr[5]) == 0xff) {
324 memcpy(wmac_gen_addr, eth0_mac_addr, 5);
325 wmac_gen_addr[5] = max(eth0_mac_addr[5], eth1_mac_addr[5]) + 1;
326
327 /* Avoid potential conflict in case max(0xff,0x00)+1==0x00 */
328 if (!wmac_gen_addr[5])
329 wmac_gen_addr[5] = 1;
330 } else
331 memcpy(wmac_gen_addr, wlan_mac_addr, 6);
332 }
333
334 static void __init wnr_common_setup(u8 *wmac_addr)
335 {
336 u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
337
338 ath79_register_mdio(0, 0x0);
339
340 ath79_init_mac(ath79_eth0_data.mac_addr, art+WNR2000V3_MAC0_OFFSET, 0);
341 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
342 ath79_eth0_data.speed = SPEED_100;
343 ath79_eth0_data.duplex = DUPLEX_FULL;
344
345 ath79_init_mac(ath79_eth1_data.mac_addr, art+WNR2000V3_MAC1_OFFSET, 0);
346 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
347 ath79_eth1_data.phy_mask = 0x10;
348
349 ath79_register_eth(0);
350 ath79_register_eth(1);
351
352 ath79_register_m25p80(NULL);
353 ap91_pci_init(art + WNR2000V3_PCIE_CALDATA_OFFSET, wmac_addr);
354 }
355
356 static void __init wnr2000v3_setup(void)
357 {
358 u8 wlan_mac_addr[6];
359
360 /*
361 * Disable JTAG to use all AR724X GPIO LEDs.
362 * Also disable CLKs and bit 20 as u-boot does.
363 * Finally, allow OS to control all link LEDs.
364 */
365 ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE |
366 AR724X_GPIO_FUNC_UART_EN,
367 AR724X_GPIO_FUNC_CLK_OBS1_EN |
368 AR724X_GPIO_FUNC_CLK_OBS2_EN |
369 AR724X_GPIO_FUNC_CLK_OBS3_EN |
370 AR724X_GPIO_FUNC_CLK_OBS4_EN |
371 AR724X_GPIO_FUNC_CLK_OBS5_EN |
372 AR724X_GPIO_FUNC_GE0_MII_CLK_EN |
373 AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
374 AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
375 AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
376 AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
377 AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN |
378 BIT(20));
379
380 wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
381 WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
382
383 wnr_common_setup(wlan_mac_addr);
384
385 ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr2000v3_leds_gpio),
386 wnr2000v3_leds_gpio);
387
388 /* Do not use id=-1, we can have more GPIO key-polled devices */
389 ath79_register_gpio_keys_polled(PLATFORM_DEVID_AUTO,
390 WNR2000V3_KEYS_POLL_INTERVAL,
391 ARRAY_SIZE(wnr2000v3_keys_gpio),
392 wnr2000v3_keys_gpio);
393
394 ap9x_pci_setup_wmac_led_pin(0, WNR2000V3_GPIO_WMAC_LED_WLAN_BLUE);
395 ap9x_pci_setup_wmac_led_name(0, wnr2000v3_wmac_led_name);
396
397 ap9x_pci_setup_wmac_leds(0, wnr2000v3_wmac_leds_gpio,
398 ARRAY_SIZE(wnr2000v3_wmac_leds_gpio));
399
400 ap9x_pci_setup_wmac_btns(0, wnr2000v3_wmac_keys_gpio,
401 ARRAY_SIZE(wnr2000v3_wmac_keys_gpio),
402 WNR2000V3_KEYS_POLL_INTERVAL);
403 }
404
405 MIPS_MACHINE(ATH79_MACH_WNR2000_V3, "WNR2000V3", "NETGEAR WNR2000 V3", wnr2000v3_setup);
406
407 static void __init wnr612v2_setup(void)
408 {
409 u8 wlan_mac_addr[6];
410
411 /*
412 * Disable JTAG and CLKs. Allow OS to control all link LEDs.
413 * Note: U-Boot for WNR612v2 sets undocumented bit 15 but
414 * we leave it for now.
415 */
416 ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE |
417 AR724X_GPIO_FUNC_UART_EN,
418 AR724X_GPIO_FUNC_CLK_OBS1_EN |
419 AR724X_GPIO_FUNC_CLK_OBS2_EN |
420 AR724X_GPIO_FUNC_CLK_OBS3_EN |
421 AR724X_GPIO_FUNC_CLK_OBS4_EN |
422 AR724X_GPIO_FUNC_CLK_OBS5_EN |
423 AR724X_GPIO_FUNC_GE0_MII_CLK_EN |
424 AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
425 AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
426 AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
427 AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
428 AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);
429
430 wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
431 WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
432
433 wnr_common_setup(wlan_mac_addr);
434
435 ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr612v2_leds_gpio),
436 wnr612v2_leds_gpio);
437
438 /*
439 * This device has no buttons on AR7241 GPIO and no extra LEDs
440 * connected to AR9285 so setup is simpler than for WNR2000v3.
441 */
442 ap9x_pci_setup_wmac_led_pin(0, WNR612V2_GPIO_WMAC_LED_WLAN_GREEN);
443 ap9x_pci_setup_wmac_led_name(0, wnr612v2_wmac_led_name);
444
445 ap9x_pci_setup_wmac_leds(0, NULL, 0);
446
447 ap9x_pci_setup_wmac_btns(0, wnr612v2_wmac_keys_gpio,
448 ARRAY_SIZE(wnr612v2_wmac_keys_gpio),
449 WNR2000V3_KEYS_POLL_INTERVAL);
450 }
451
452 MIPS_MACHINE(ATH79_MACH_WNR612_V2, "WNR612V2", "NETGEAR WNR612 V2", wnr612v2_setup);
453
454 static void __init wnr1000v2_setup(void)
455 {
456 u8 wlan_mac_addr[6];
457
458 wnr_get_wmac(wlan_mac_addr, WNR2000V3_MAC0_OFFSET,
459 WNR2000V3_MAC1_OFFSET, WNR2000V3_WMAC_OFFSET);
460
461 wnr_common_setup(wlan_mac_addr);
462
463 ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr1000v2_leds_gpio),
464 wnr1000v2_leds_gpio);
465 }
466
467 MIPS_MACHINE(ATH79_MACH_WNR1000_V2, "WNR1000V2", "NETGEAR WNR1000 V2", wnr1000v2_setup);
468
469 static void __init wpn824n_setup(void)
470 {
471 ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
472 AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
473 AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
474 AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
475 AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
476 AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN |
477 AR724X_GPIO_FUNC_CLK_OBS3_EN);
478
479 wnr_common_setup(NULL);
480
481 ath79_register_leds_gpio(-1, ARRAY_SIZE(wpn824n_leds_gpio),
482 wpn824n_leds_gpio);
483
484 ap9x_pci_setup_wmac_led_pin(0, WPN824N_WGPIO_LED_WLAN_BLUE);
485 ap9x_pci_setup_wmac_leds(0, wpn824n_wmac_leds_gpio,
486 ARRAY_SIZE(wpn824n_wmac_leds_gpio));
487 }
488
489 MIPS_MACHINE(ATH79_MACH_WPN824N, "WPN824N", "NETGEAR WPN824N", wpn824n_setup);