Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-tl-wr1043nd-v4.c
1 /*
2  *  TP-LINK WR1043 V4 support
3  *
4  *  Copyright (C) 2015-2016 P. Wassi <p.wassi at gmx.at>
5  *  Copyright (C) 2016 Matthias Schiffer <mschiffer@universe-factory.net>
6  *  Copyright (C) 2016 Andreas Ziegler <github@andreas-ziegler.de>
7  *  Copyright (C) 2016 Ludwig Thomeczek <ledesrc@wxorx.net>
8  *  Copyright (C) 2017 Tim Thorpe <tim@tfthorpe.net>
9  *
10  *  Derived from: mach-dir-869-a1.c
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License version 2 as published
14  *  by the Free Software Foundation.
15  */
16
17
18 #include <linux/gpio.h>
19 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
22 #include <linux/ath9k_platform.h>
23
24 #include <asm/mach-ath79/ath79.h>
25 #include <asm/mach-ath79/irq.h>
26 #include <asm/mach-ath79/ar71xx_regs.h>
27
28 #include <linux/platform_data/phy-at803x.h>
29 #include <linux/ar8216_platform.h>
30
31 #include "common.h"
32 #include "dev-ap9x-pci.h"
33 #include "dev-eth.h"
34 #include "dev-gpio-buttons.h"
35 #include "dev-leds-gpio.h"
36 #include "dev-m25p80.h"
37 #include "dev-wmac.h"
38 #include "dev-usb.h"
39 #include "machtypes.h"
40 #include "nvram.h"
41
42 #define TL_WR1043_V4_GPIO_BTN_RESET             2
43 #define TL_WR1043_V4_GPIO_BTN_RFKILL            5
44
45 #define TL_WR1043_V4_GPIO_LED_WLAN              19
46 #define TL_WR1043_V4_GPIO_LED_USB               7
47 #define TL_WR1043_V4_GPIO_LED_WPS               1
48 #define TL_WR1043_V4_GPIO_LED_SYSTEM            6
49
50 #define TL_WR1043_V4_GPIO_USB_POWER             8
51
52 #define TL_WR1043_V4_GPIO_LED_INET              15
53 #define TL_WR1043_V4_GPIO_LED_WAN               16
54 #define TL_WR1043_V4_GPIO_LED_LAN1              9
55 #define TL_WR1043_V4_GPIO_LED_LAN2              14
56 #define TL_WR1043_V4_GPIO_LED_LAN3              21
57 #define TL_WR1043_V4_GPIO_LED_LAN4              20
58
59 #define TL_WR1043_V4_KEYS_POLL_INTERVAL         20 /* msecs */
60 #define TL_WR1043_V4_KEYS_DEBOUNCE_INTERVAL     (3 * TL_WR1043_V4_KEYS_POLL_INTERVAL)
61
62 #define TL_WR1043_V4_MAC_LOCATION               0x1ff50008
63
64 #define TL_WR1043_V4_EEPROM_ADDR                0x1fff0000
65 #define TL_WR1043_V4_WMAC_CALDATA_OFFSET        0x1000
66
67 #define TL_WR1043_V5_MAC_LOCATION               0x1ff00008
68
69 static struct gpio_led tl_wr1043nd_v4_leds_gpio[] __initdata = {
70         {
71                 .name           = "tp-link:green:wps",
72                 .gpio           = TL_WR1043_V4_GPIO_LED_WPS,
73                 .active_low     = 1,
74         },
75         {
76                 .name           = "tp-link:green:system",
77                 .gpio           = TL_WR1043_V4_GPIO_LED_SYSTEM,
78                 .active_low     = 1,
79         },
80         {
81                 .name           = "tp-link:green:wlan",
82                 .gpio           = TL_WR1043_V4_GPIO_LED_WLAN,
83                 .active_low     = 1,
84         },
85         {
86                 .name           = "tp-link:green:usb",
87                 .gpio           = TL_WR1043_V4_GPIO_LED_USB,
88                 .active_low     = 1,
89         },
90         {
91                 .name           = "tp-link:green:wan",
92                 .gpio           = TL_WR1043_V4_GPIO_LED_INET,
93                 .active_low     = 1,
94         },
95         {
96                 .name           = "tp-link:orange:wan",
97                 .gpio           = TL_WR1043_V4_GPIO_LED_WAN,
98                 .active_low     = 1,
99         },
100         {
101                 .name           = "tp-link:green:lan1",
102                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN1,
103                 .active_low     = 1,
104         },
105         {
106                 .name           = "tp-link:green:lan2",
107                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN2,
108                 .active_low     = 1,
109         },
110         {
111                 .name           = "tp-link:green:lan3",
112                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN3,
113                 .active_low     = 1,
114         },
115         {
116                 .name           = "tp-link:green:lan4",
117                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN4,
118                 .active_low     = 1,
119         },
120 };
121
122 static struct gpio_keys_button tl_wr1043nd_v4_gpio_keys[] __initdata = {
123         {
124                 .desc           = "Reset button",
125                 .type           = EV_KEY,
126                 .code           = KEY_RESTART,
127                 .debounce_interval = TL_WR1043_V4_KEYS_DEBOUNCE_INTERVAL,
128                 .gpio           = TL_WR1043_V4_GPIO_BTN_RESET,
129                 .active_low     = 1,
130         },
131         {
132                 .desc           = "RFKILL button",
133                 .type           = EV_KEY,
134                 .code           = KEY_RFKILL,
135                 .debounce_interval = TL_WR1043_V4_KEYS_DEBOUNCE_INTERVAL,
136                 .gpio           = TL_WR1043_V4_GPIO_BTN_RFKILL,
137                 .active_low     = 1,
138         },
139 };
140
141 static struct ar8327_pad_cfg tl_wr1043nd_v4_ar8327_pad0_cfg = {
142         .mode = AR8327_PAD_MAC_SGMII,
143         .sgmii_delay_en = true,
144 };
145
146 static struct ar8327_platform_data tl_wr1043nd_v4_ar8327_data = {
147         .pad0_cfg = &tl_wr1043nd_v4_ar8327_pad0_cfg,
148         .port0_cfg = {
149                 .force_link = 1,
150                 .speed = AR8327_PORT_SPEED_1000,
151                 .duplex = 1,
152                 .txpause = 1,
153                 .rxpause = 1,
154         },
155 };
156
157 static struct mdio_board_info tl_wr1043nd_v4_mdio0_info[] = {
158         {
159                 .bus_id = "ag71xx-mdio.0",
160                 .mdio_addr = 0,
161                 .platform_data = &tl_wr1043nd_v4_ar8327_data,
162         },
163 };
164
165 static void __init tl_wr1043nd_v4_setup(void)
166 {
167         u8 *mac = (u8 *) KSEG1ADDR(TL_WR1043_V4_MAC_LOCATION);
168         u8 *eeprom = (u8 *) KSEG1ADDR(TL_WR1043_V4_EEPROM_ADDR);
169
170         ath79_register_m25p80(NULL);
171
172         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 0);
173         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_SGMII;
174         ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
175         ath79_eth0_data.phy_mask = BIT(0);
176
177         mdiobus_register_board_info(tl_wr1043nd_v4_mdio0_info,
178                                     ARRAY_SIZE(tl_wr1043nd_v4_mdio0_info));
179
180         ath79_register_usb();
181         ath79_register_mdio(0, 0);
182         ath79_register_eth(0);
183
184         ath79_register_wmac(eeprom + TL_WR1043_V4_WMAC_CALDATA_OFFSET, mac);
185
186         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr1043nd_v4_leds_gpio),
187                                  tl_wr1043nd_v4_leds_gpio);
188
189         ath79_register_gpio_keys_polled(-1, TL_WR1043_V4_KEYS_POLL_INTERVAL,
190                                         ARRAY_SIZE(tl_wr1043nd_v4_gpio_keys),
191                                         tl_wr1043nd_v4_gpio_keys);
192
193         gpio_request_one(TL_WR1043_V4_GPIO_USB_POWER,
194                          GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
195                          "USB power");
196 }
197
198 MIPS_MACHINE(ATH79_MACH_TL_WR1043ND_V4, "TL-WR1043ND-v4",
199              "TP-LINK TL-WR1043ND v4", tl_wr1043nd_v4_setup);
200
201 static struct gpio_led tl_wr1043n_v5_leds_gpio[] __initdata = {
202         {
203                 .name           = "tp-link:green:wps",
204                 .gpio           = TL_WR1043_V4_GPIO_LED_WPS,
205                 .active_low     = 1,
206         },
207         {
208                 .name           = "tp-link:green:system",
209                 .gpio           = TL_WR1043_V4_GPIO_LED_SYSTEM,
210                 .active_low     = 1,
211         },
212         {
213                 .name           = "tp-link:green:wlan",
214                 .gpio           = TL_WR1043_V4_GPIO_LED_WLAN,
215                 .active_low     = 1,
216         },
217         {
218                 .name           = "tp-link:green:wan",
219                 .gpio           = TL_WR1043_V4_GPIO_LED_INET,
220                 .active_low     = 1,
221         },
222         {
223                 .name           = "tp-link:orange:wan",
224                 .gpio           = TL_WR1043_V4_GPIO_LED_WAN,
225                 .active_low     = 1,
226         },
227         {
228                 .name           = "tp-link:green:lan1",
229                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN1,
230                 .active_low     = 1,
231         },
232         {
233                 .name           = "tp-link:green:lan2",
234                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN2,
235                 .active_low     = 1,
236         },
237         {
238                 .name           = "tp-link:green:lan3",
239                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN3,
240                 .active_low     = 1,
241         },
242         {
243                 .name           = "tp-link:green:lan4",
244                 .gpio           = TL_WR1043_V4_GPIO_LED_LAN4,
245                 .active_low     = 1,
246         },
247 };
248
249 /* The 1043Nv5 is identical to the 1043NDv4,
250  *  only missing the usb and small firmware layout changes  */
251 static void __init tl_wr1043nv5_setup(void)
252 {
253         u8 *art = (u8 *) KSEG1ADDR(TL_WR1043_V4_EEPROM_ADDR);
254         u8 *mac = (u8 *) KSEG1ADDR(TL_WR1043_V5_MAC_LOCATION);
255
256         ath79_register_m25p80(NULL);
257
258         ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr1043n_v5_leds_gpio),
259                                  tl_wr1043n_v5_leds_gpio);
260         ath79_register_gpio_keys_polled(-1, TL_WR1043_V4_KEYS_POLL_INTERVAL,
261                                         ARRAY_SIZE(tl_wr1043nd_v4_gpio_keys),
262                                         tl_wr1043nd_v4_gpio_keys);
263
264         platform_device_register(&ath79_mdio0_device);
265
266         mdiobus_register_board_info(tl_wr1043nd_v4_mdio0_info,
267                                     ARRAY_SIZE(tl_wr1043nd_v4_mdio0_info));
268
269         ath79_register_wmac(art + TL_WR1043_V4_WMAC_CALDATA_OFFSET, mac);
270
271         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 0);
272
273         /* GMAC0 is connected to an AR8337 switch */
274         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_SGMII;
275         ath79_eth0_data.speed = SPEED_1000;
276         ath79_eth0_data.duplex = DUPLEX_FULL;
277         ath79_eth0_data.phy_mask = BIT(0);
278         ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
279         ath79_register_eth(0);
280 }
281
282 MIPS_MACHINE(ATH79_MACH_TL_WR1043N_V5, "TL-WR1043N-v5", "TP-LINK TL-WR1043N v5",
283              tl_wr1043nv5_setup);