ar71xx: WNR2200: fix for random WLAN MAC
[openwrt/staging/wigyori.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-wnr2200.c
1 /*
2 * NETGEAR WNR2200 board support
3 *
4 * Copyright (C) 2013 Aidan Kissane <aidankissane at googlemail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 */
10
11 #include <linux/gpio.h>
12
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
15 #include <linux/kernel.h> /* for max() macro */
16
17 #include <asm/mach-ath79/ath79.h>
18
19 #include "dev-ap9x-pci.h"
20 #include "dev-eth.h"
21 #include "dev-gpio-buttons.h"
22 #include "dev-leds-gpio.h"
23 #include "dev-m25p80.h"
24 #include "dev-usb.h"
25 #include "machtypes.h"
26
27 #define WNR2200_GPIO_LED_LAN2_AMBER 0
28 #define WNR2200_GPIO_LED_LAN4_AMBER 1
29 #define WNR2200_GPIO_LED_WPS 5
30 #define WNR2200_GPIO_LED_WAN_GREEN 7
31 #define WNR2200_GPIO_LED_USB 8
32 #define WNR2200_GPIO_LED_LAN3_AMBER 11
33 #define WNR2200_GPIO_LED_WAN_AMBER 12
34 #define WNR2200_GPIO_LED_LAN1_GREEN 13
35 #define WNR2200_GPIO_LED_LAN2_GREEN 14
36 #define WNR2200_GPIO_LED_LAN3_GREEN 15
37 #define WNR2200_GPIO_LED_LAN4_GREEN 16
38 #define WNR2200_GPIO_LED_PWR_AMBER 21
39 #define WNR2200_GPIO_LED_PWR_GREEN 22
40 #define WNR2200_GPIO_USB_5V 4
41 #define WNR2200_GPIO_USB_POWER 24
42
43 #define WNR2200_KEYS_POLL_INTERVAL 20 /* msecs */
44 #define WNR2200_KEYS_DEBOUNCE_INTERVAL (3 * WNR2200_KEYS_POLL_INTERVAL)
45
46 #define WNR2200_MAC0_OFFSET 0
47 #define WNR2200_MAC1_OFFSET 6
48 #define WNR2200_PCIE_CALDATA_OFFSET 0x1000
49 #define WNR2200_WMAC_OFFSET 0x108c /* wireless MAC is inside ART */
50
51 static struct gpio_led wnr2200_leds_gpio[] __initdata = {
52 {
53 .name = "netgear:amber:lan2",
54 .gpio = WNR2200_GPIO_LED_LAN2_AMBER,
55 .active_low = 1,
56 }, {
57 .name = "netgear:amber:lan4",
58 .gpio = WNR2200_GPIO_LED_LAN4_AMBER,
59 .active_low = 1,
60 }, {
61 .name = "netgear:green:wps",
62 .gpio = WNR2200_GPIO_LED_WPS,
63 .active_low = 1,
64 }, {
65 .name = "netgear:green:wan",
66 .gpio = WNR2200_GPIO_LED_WAN_GREEN,
67 .active_low = 1,
68 }, {
69 .name = "netgear:green:usb",
70 .gpio = WNR2200_GPIO_LED_USB,
71 .active_low = 1,
72 }, {
73 .name = "netgear:amber:lan3",
74 .gpio = WNR2200_GPIO_LED_LAN3_AMBER,
75 .active_low = 1,
76 }, {
77 .name = "netgear:amber:wan",
78 .gpio = WNR2200_GPIO_LED_WAN_AMBER,
79 .active_low = 1,
80 }, {
81 .name = "netgear:green:lan1",
82 .gpio = WNR2200_GPIO_LED_LAN1_GREEN,
83 .active_low = 1,
84 }, {
85 .name = "netgear:green:lan2",
86 .gpio = WNR2200_GPIO_LED_LAN2_GREEN,
87 .active_low = 1,
88 }, {
89 .name = "netgear:green:lan3",
90 .gpio = WNR2200_GPIO_LED_LAN3_GREEN,
91 .active_low = 1,
92 }, {
93 .name = "netgear:green:lan4",
94 .gpio = WNR2200_GPIO_LED_LAN4_GREEN,
95 .active_low = 1,
96 }, {
97 .name = "netgear:amber:power",
98 .gpio = WNR2200_GPIO_LED_PWR_AMBER,
99 .active_low = 1,
100 }, {
101 .name = "netgear:green:power",
102 .gpio = WNR2200_GPIO_LED_PWR_GREEN,
103 .active_low = 1,
104 }
105 };
106
107 /*
108 * For WNR2200 ART flash area used for WLAN MAC is usually empty (0xff)
109 * so ath9k driver uses random MAC instead each time module is loaded.
110 * OpenWrt's original fix was to copy eth1 address to WLAN interface.
111 * New solution does not duplicate hardware addresses and is taken from
112 * WNR2000v3 code. It assigns permanent WLAN MAC equal to ethN's MAC
113 * plus 1, so network interfaces get sequential addresses.
114 * If ART wireless MAC address field has been filled by user, use it.
115 */
116 static void __init wnr2200_get_wmac(u8 *wmac_gen_addr, int mac0_art_offset,
117 int mac1_art_offset, int wmac_art_offset)
118 {
119 u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
120 u8 *eth0_mac_addr = (u8 *) (art + mac0_art_offset);
121 u8 *eth1_mac_addr = (u8 *) (art + mac1_art_offset);
122 u8 *wlan_mac_addr = (u8 *) (art + wmac_art_offset);
123
124 /* only 0xff if all bits are set - address is invalid, empty area */
125 if ((wlan_mac_addr[0] & wlan_mac_addr[1] & wlan_mac_addr[2] &
126 wlan_mac_addr[3] & wlan_mac_addr[4] & wlan_mac_addr[5]) == 0xff) {
127 memcpy(wmac_gen_addr, eth0_mac_addr, 5);
128 wmac_gen_addr[5] = max(eth0_mac_addr[5], eth1_mac_addr[5]) + 1;
129
130 /* Avoid potential conflict in case max(0xff,0x00)+1==0x00 */
131 if (!wmac_gen_addr[5])
132 wmac_gen_addr[5] = 1;
133 } else
134 memcpy(wmac_gen_addr, wlan_mac_addr, 6);
135 }
136
137 static void __init wnr2200_setup(void)
138 {
139 u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
140 u8 wlan_mac_addr[6];
141
142 ath79_register_mdio(0, 0x0);
143
144 ath79_init_mac(ath79_eth0_data.mac_addr, art+WNR2200_MAC0_OFFSET, 0);
145 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
146 ath79_eth0_data.speed = SPEED_100;
147 ath79_eth0_data.duplex = DUPLEX_FULL;
148
149 ath79_init_mac(ath79_eth1_data.mac_addr, art+WNR2200_MAC1_OFFSET, 0);
150 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_RMII;
151 ath79_eth1_data.phy_mask = 0x10;
152
153 ath79_register_eth(0);
154 ath79_register_eth(1);
155
156 ath79_register_m25p80(NULL);
157
158 wnr2200_get_wmac(wlan_mac_addr, WNR2200_MAC0_OFFSET,
159 WNR2200_MAC1_OFFSET, WNR2200_WMAC_OFFSET);
160 ap91_pci_init(art + WNR2200_PCIE_CALDATA_OFFSET, wlan_mac_addr);
161
162 ath79_register_leds_gpio(-1, ARRAY_SIZE(wnr2200_leds_gpio),
163 wnr2200_leds_gpio);
164
165 /* enable power for the USB port */
166 ap9x_pci_setup_wmac_gpio(0,
167 BIT(WNR2200_GPIO_USB_5V),
168 BIT(WNR2200_GPIO_USB_5V));
169
170 ath79_register_usb();
171 }
172
173 MIPS_MACHINE(ATH79_MACH_WNR2200, "WNR2200", "NETGEAR WNR2200", wnr2200_setup);