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