ar71xx: improve support for TP-Link CPE510 v2
[oweals/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-cpe510.c
1 /*
2  *  TP-LINK CPE210/210 v2/220/510/520 board support
3  *
4  *  Copyright (C) 2014 Matthias Schiffer <mschiffer@universe-factory.net>
5  *  Copyright (C) 2017 Robert Marko <robimarko@gmail.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11
12 #include <linux/gpio.h>
13 #include <linux/platform_device.h>
14
15 #include <asm/mach-ath79/ath79.h>
16 #include <asm/mach-ath79/ar71xx_regs.h>
17
18 #include "common.h"
19 #include "dev-eth.h"
20 #include "dev-gpio-buttons.h"
21 #include "dev-leds-gpio.h"
22 #include "dev-m25p80.h"
23 #include "dev-wmac.h"
24 #include "machtypes.h"
25
26
27 #define CPE510_GPIO_LED_LAN0    11
28 #define CPE510_GPIO_LED_LAN1    12
29 #define CPE510_GPIO_LED_L1      13
30 #define CPE510_GPIO_LED_L2      14
31 #define CPE510_GPIO_LED_L3      15
32 #define CPE510_GPIO_LED_L4      16
33
34 /* All LEDs/button except for link4 are the same for CPE and WBS series */
35 #define WBS510_GPIO_LED_L4      2
36
37 #define CPE510_GPIO_EXTERNAL_LNA0       18
38 #define CPE510_GPIO_EXTERNAL_LNA1       19
39
40 #define CPE510_GPIO_BTN_RESET   4
41
42 #define CPE510_KEYS_POLL_INTERVAL       20 /* msecs */
43 #define CPE510_KEYS_DEBOUNCE_INTERVAL   (3 * CPE510_KEYS_POLL_INTERVAL)
44
45 /* CPE210 v2 reset GPIO */
46 #define CPE210_V2_GPIO_BTN_RESET        17
47
48 static struct gpio_led cpe510_leds_gpio[] __initdata = {
49         {
50                 .name           = "tp-link:green:lan0",
51                 .gpio           = CPE510_GPIO_LED_LAN0,
52                 .active_low     = 1,
53         }, {
54                 .name           = "tp-link:green:lan1",
55                 .gpio           = CPE510_GPIO_LED_LAN1,
56                 .active_low     = 1,
57         }, {
58                 .name           = "tp-link:green:link1",
59                 .gpio           = CPE510_GPIO_LED_L1,
60                 .active_low     = 1,
61         }, {
62                 .name           = "tp-link:green:link2",
63                 .gpio           = CPE510_GPIO_LED_L2,
64                 .active_low     = 1,
65         }, {
66                 .name           = "tp-link:green:link3",
67                 .gpio           = CPE510_GPIO_LED_L3,
68                 .active_low     = 1,
69         }, {
70                 .name           = "tp-link:green:link4",
71                 .gpio           = CPE510_GPIO_LED_L4,
72                 .active_low     = 1,
73         },
74 };
75
76 static struct gpio_led wbs510_leds_gpio[] __initdata = {
77         {
78                 .name           = "tp-link:green:lan0",
79                 .gpio           = CPE510_GPIO_LED_LAN0,
80                 .active_low     = 1,
81         }, {
82                 .name           = "tp-link:green:lan1",
83                 .gpio           = CPE510_GPIO_LED_LAN1,
84                 .active_low     = 1,
85         }, {
86                 .name           = "tp-link:green:link1",
87                 .gpio           = CPE510_GPIO_LED_L1,
88                 .active_low     = 1,
89         }, {
90                 .name           = "tp-link:green:link2",
91                 .gpio           = CPE510_GPIO_LED_L2,
92                 .active_low     = 1,
93         }, {
94                 .name           = "tp-link:green:link3",
95                 .gpio           = CPE510_GPIO_LED_L3,
96                 .active_low     = 1,
97         }, {
98                 .name           = "tp-link:green:link4",
99                 .gpio           = WBS510_GPIO_LED_L4,
100                 .active_low     = 1,
101         },
102 };
103
104 static struct gpio_led cpe210_v2_leds_gpio[] __initdata = {
105         {
106                 .name           = "tp-link:green:lan0",
107                 .gpio           = CPE510_GPIO_LED_LAN0,
108                 .active_low     = 1,
109         }, {
110                 .name           = "tp-link:green:link1",
111                 .gpio           = CPE510_GPIO_LED_L1,
112                 .active_low     = 1,
113         }, {
114                 .name           = "tp-link:green:link2",
115                 .gpio           = CPE510_GPIO_LED_L2,
116                 .active_low     = 1,
117         }, {
118                 .name           = "tp-link:green:link3",
119                 .gpio           = CPE510_GPIO_LED_L3,
120                 .active_low     = 1,
121         }, {
122                 .name           = "tp-link:green:link4",
123                 .gpio           = CPE510_GPIO_LED_L4,
124                 .active_low     = 1,
125         },
126 };
127
128 static struct gpio_keys_button cpe510_gpio_keys[] __initdata = {
129         {
130                 .desc           = "Reset button",
131                 .type           = EV_KEY,
132                 .code           = KEY_RESTART,
133                 .debounce_interval = CPE510_KEYS_DEBOUNCE_INTERVAL,
134                 .gpio           = CPE510_GPIO_BTN_RESET,
135                 .active_low     = 1,
136         }
137 };
138
139 static struct gpio_keys_button cpe210_v2_gpio_keys[] __initdata = {
140         {
141                 .desc           = "Reset button",
142                 .type           = EV_KEY,
143                 .code           = KEY_RESTART,
144                 .debounce_interval = CPE510_KEYS_DEBOUNCE_INTERVAL,
145                 .gpio           = CPE210_V2_GPIO_BTN_RESET,
146                 .active_low     = 1,
147         }
148 };
149
150 static void __init cpe_setup(u8 *mac)
151 {
152         /* Disable JTAG, enabling GPIOs 0-3 */
153         /* Configure OBS4 line, for GPIO 4*/
154         ath79_gpio_function_setup(AR934X_GPIO_FUNC_JTAG_DISABLE,
155                                   AR934X_GPIO_FUNC_CLK_OBS4_EN);
156
157         ath79_register_gpio_keys_polled(1, CPE510_KEYS_POLL_INTERVAL,
158                                         ARRAY_SIZE(cpe510_gpio_keys),
159                                         cpe510_gpio_keys);
160
161         ath79_wmac_set_ext_lna_gpio(0, CPE510_GPIO_EXTERNAL_LNA0);
162         ath79_wmac_set_ext_lna_gpio(1, CPE510_GPIO_EXTERNAL_LNA1);
163
164         ath79_register_m25p80(NULL);
165
166         ath79_register_mdio(1, 0);
167         ath79_init_mac(ath79_eth1_data.mac_addr, mac, 0);
168         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
169         ath79_register_eth(1);
170 }
171
172
173 static void __init cpe210_setup(void)
174 {
175         u8 *mac = (u8 *) KSEG1ADDR(0x1f830008);
176         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
177
178         ath79_register_leds_gpio(-1, ARRAY_SIZE(cpe510_leds_gpio),
179                                  cpe510_leds_gpio);
180
181         cpe_setup(mac);
182
183         ath79_register_wmac(ee, mac);
184 }
185
186 static void __init cpe510_setup(void)
187 {
188         u8 *mac = (u8 *) KSEG1ADDR(0x1f830008);
189         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
190
191         ath79_register_leds_gpio(-1, ARRAY_SIZE(cpe510_leds_gpio),
192                                  cpe510_leds_gpio);
193
194         cpe_setup(mac);
195
196         ath79_register_wmac(ee, mac);
197 }
198
199 static void __init wbs_setup(void)
200 {
201         u8 *mac = (u8 *) KSEG1ADDR(0x1f830008);
202         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
203
204         ath79_register_leds_gpio(-1, ARRAY_SIZE(wbs510_leds_gpio),
205                                  wbs510_leds_gpio);
206
207         cpe_setup(mac);
208
209         ath79_register_wmac(ee, mac);
210 }
211
212 static void __init cpe210_v2_setup(void)
213 {
214         u8 *mac = (u8 *) KSEG1ADDR(0x1f830008);
215         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
216
217         ath79_register_leds_gpio(-1, ARRAY_SIZE(cpe210_v2_leds_gpio),
218                                  cpe210_v2_leds_gpio);
219         ath79_register_gpio_keys_polled(-1, CPE510_KEYS_POLL_INTERVAL,
220                                         ARRAY_SIZE(cpe210_v2_gpio_keys),
221                                         cpe210_v2_gpio_keys);
222         ath79_register_m25p80(NULL);
223         ath79_register_mdio(0, 0x0);
224         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 0);
225         ath79_eth0_data.duplex = DUPLEX_FULL;
226         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
227         ath79_eth0_data.speed = SPEED_100;
228         ath79_eth0_data.phy_mask = BIT(4);
229         ath79_register_eth(0);
230         ath79_register_wmac(ee, mac);
231 }
232
233 static void __init cpe510_v2_setup(void)
234 {
235         u8 *mac = (u8 *) KSEG1ADDR(0x1f830008);
236         u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
237
238         /* disable PHY_SWAP and PHY_ADDR_SWAP bits */
239         ath79_setup_ar933x_phy4_switch(false, false);
240
241         ath79_register_leds_gpio(-1, ARRAY_SIZE(cpe210_v2_leds_gpio),
242                                         cpe210_v2_leds_gpio);
243         ath79_register_gpio_keys_polled(-1, CPE510_KEYS_POLL_INTERVAL,
244                                         ARRAY_SIZE(cpe510_gpio_keys),
245                                         cpe510_gpio_keys);
246
247         ath79_register_m25p80(NULL);
248         ath79_register_mdio(1, 0x0);
249
250         /* LAN port */
251         ath79_switch_data.phy4_mii_en = 1;
252         ath79_switch_data.phy_poll_mask = BIT(4);
253         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
254         ath79_eth0_data.phy_mask = BIT(4);
255         ath79_eth0_data.mii_bus_dev = &ath79_mdio1_device.dev;
256         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 0);
257         ath79_register_eth(0);
258
259         ath79_register_wmac(ee, mac);
260 }
261
262 MIPS_MACHINE(ATH79_MACH_CPE210, "CPE210", "TP-LINK CPE210/220",
263              cpe210_setup);
264
265 MIPS_MACHINE(ATH79_MACH_CPE210_V2, "CPE210V2", "TP-LINK CPE210 v2",
266              cpe210_v2_setup);
267
268 MIPS_MACHINE(ATH79_MACH_CPE210_V3, "CPE210V3", "TP-LINK CPE210 v3",
269              cpe210_v2_setup);
270
271 MIPS_MACHINE(ATH79_MACH_CPE510, "CPE510", "TP-LINK CPE510/520",
272              cpe510_setup);
273
274 MIPS_MACHINE(ATH79_MACH_CPE510_V2, "CPE510V2", "TP-LINK CPE510 v2",
275              cpe510_v2_setup);
276
277 MIPS_MACHINE(ATH79_MACH_WBS210, "WBS210", "TP-LINK WBS210",
278              wbs_setup);
279
280 MIPS_MACHINE(ATH79_MACH_WBS510, "WBS510", "TP-LINK WBS510",
281              wbs_setup);