Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-nbg6716.c
1 /*
2  * ZyXEL NBG6716/NBG6616 board support
3  *
4  * Based on the Qualcomm Atheros AP135/AP136 reference board support code
5  * Copyright (c) 2012 Qualcomm Atheros
6  * Copyright (c) 2012-2013 Gabor Juhos <juhosg@openwrt.org>
7  * Copyright (c) 2013 Andre Valentin <avalentin@marcant.net>
8  *
9  * Permission to use, copy, modify, and/or distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  */
22
23 #include <linux/version.h>
24 #include <linux/platform_device.h>
25 #include <linux/ar8216_platform.h>
26 #include <linux/gpio.h>
27 #include <linux/mtd/mtd.h>
28 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
29 #include <linux/mtd/nand.h>
30 #else
31 #include <linux/mtd/rawnand.h>
32 #endif
33 #include <linux/platform/ar934x_nfc.h>
34
35 #include <asm/mach-ath79/ar71xx_regs.h>
36
37 #include "common.h"
38 #include "pci.h"
39 #include "dev-ap9x-pci.h"
40 #include "dev-gpio-buttons.h"
41 #include "dev-eth.h"
42 #include "dev-leds-gpio.h"
43 #include "dev-nfc.h"
44 #include "dev-m25p80.h"
45 #include "dev-usb.h"
46 #include "dev-wmac.h"
47 #include "machtypes.h"
48 #include "nvram.h"
49
50 #define NBG6716_GPIO_LED_INTERNET       18
51 #define NBG6716_GPIO_LED_POWER          15
52 #define NBG6716_GPIO_LED_USB1           4
53 #define NBG6716_GPIO_LED_USB2           13
54 #define NBG6716_GPIO_LED_WIFI2G         19
55 #define NBG6716_GPIO_LED_WIFI5G         17
56 #define NBG6716_GPIO_LED_WPS            21
57
58 #define NBG6716_GPIO_BTN_RESET          23
59 #define NBG6716_GPIO_BTN_RFKILL         1
60 #define NBG6716_GPIO_BTN_USB1           0
61 #define NBG6716_GPIO_BTN_USB2           14
62 #define NBG6716_GPIO_BTN_WPS            22
63
64 #define NBG6716_GPIO_USB_POWER          16
65
66 #define NBG6716_KEYS_POLL_INTERVAL      20      /* msecs */
67 #define NBG6716_KEYS_DEBOUNCE_INTERVAL  (3 * NBG6716_KEYS_POLL_INTERVAL)
68
69 #define NBG6716_MAC0_OFFSET             0
70 #define NBG6716_MAC1_OFFSET             6
71 #define NBG6716_WMAC_CALDATA_OFFSET     0x1000
72 #define NBG6716_PCIE_CALDATA_OFFSET     0x5000
73
74 /* NBG6616 has a different GPIO usage as it does not have USB Buttons */
75 #define NBG6616_GPIO_LED_USB0           14
76 #define NBG6616_GPIO_LED_USB1           21
77 #define NBG6616_GPIO_LED_WPS            0
78
79 static struct gpio_led nbg6716_leds_gpio[] __initdata = {
80         {
81                 .name           = "nbg6716:white:internet",
82                 .gpio           = NBG6716_GPIO_LED_INTERNET,
83                 .active_low     = 1,
84         },
85         {
86                 .name           = "nbg6716:white:power",
87                 .gpio           = NBG6716_GPIO_LED_POWER,
88                 .active_low     = 1,
89         },
90         {
91                 .name           = "nbg6716:white:usb1",
92                 .gpio           = NBG6716_GPIO_LED_USB1,
93                 .active_low     = 1,
94         },
95         {
96                 .name           = "nbg6716:white:usb2",
97                 .gpio           = NBG6716_GPIO_LED_USB2,
98                 .active_low     = 1,
99         },
100         {
101                 .name           = "nbg6716:white:wifi2g",
102                 .gpio           = NBG6716_GPIO_LED_WIFI2G,
103                 .active_low     = 1,
104         },
105         {
106                 .name           = "nbg6716:white:wifi5g",
107                 .gpio           = NBG6716_GPIO_LED_WIFI5G,
108                 .active_low     = 1,
109         },
110         {
111                 .name           = "nbg6716:white:wps",
112                 .gpio           = NBG6716_GPIO_LED_WPS,
113                 .active_low     = 1,
114         }
115 };
116
117 static struct gpio_keys_button nbg6716_gpio_keys[] __initdata = {
118         {
119                 .desc           = "RESET button",
120                 .type           = EV_KEY,
121                 .code           = KEY_RESTART,
122                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
123                 .gpio           = NBG6716_GPIO_BTN_RESET,
124                 .active_low     = 1,
125         },
126         {
127                 .desc           = "RFKILL button",
128                 .type           = EV_SW,
129                 .code           = KEY_RFKILL,
130                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
131                 .gpio           = NBG6716_GPIO_BTN_RFKILL,
132                 .active_low     = 0,
133         },
134         {
135                 .desc           = "USB1 eject button",
136                 .type           = EV_KEY,
137                 .code           = BTN_1,
138                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
139                 .gpio           = NBG6716_GPIO_BTN_USB1,
140                 .active_low     = 1,
141         },
142         {
143                 .desc           = "USB2 eject button",
144                 .type           = EV_KEY,
145                 .code           = BTN_2,
146                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
147                 .gpio           = NBG6716_GPIO_BTN_USB2,
148                 .active_low     = 1,
149         },
150         {
151                 .desc           = "WPS button",
152                 .type           = EV_KEY,
153                 .code           = KEY_WPS_BUTTON,
154                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
155                 .gpio           = NBG6716_GPIO_BTN_WPS,
156                 .active_low     = 1,
157         },
158 };
159
160
161
162 static struct gpio_led nbg6616_leds_gpio[] __initdata = {
163         {
164                 .name           = "nbg6616:green:power",
165                 .gpio           = NBG6716_GPIO_LED_POWER,
166                 .active_low     = 1,
167         },
168         {
169                 .name           = "nbg6616:green:usb2",
170                 .gpio           = NBG6616_GPIO_LED_USB0,
171                 .active_low     = 1,
172         },
173         {
174                 .name           = "nbg6616:green:usb1",
175                 .gpio           = NBG6616_GPIO_LED_USB1,
176                 .active_low     = 1,
177         },
178         {
179                 .name           = "nbg6616:green:wifi2g",
180                 .gpio           = NBG6716_GPIO_LED_WIFI2G,
181                 .active_low     = 1,
182         },
183         {
184                 .name           = "nbg6616:green:wifi5g",
185                 .gpio           = NBG6716_GPIO_LED_WIFI5G,
186                 .active_low     = 1,
187         },
188         {
189                 .name           = "nbg6616:green:wps",
190                 .gpio           = NBG6616_GPIO_LED_WPS,
191                 .active_low     = 1,
192         }
193 };
194
195 static struct gpio_keys_button nbg6616_gpio_keys[] __initdata = {
196         {
197                 .desc           = "RESET button",
198                 .type           = EV_KEY,
199                 .code           = KEY_RESTART,
200                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
201                 .gpio           = NBG6716_GPIO_BTN_RESET,
202                 .active_low     = 1,
203         },
204         {
205                 .desc           = "RFKILL button",
206                 .type           = EV_KEY,
207                 .code           = KEY_RFKILL,
208                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
209                 .gpio           = NBG6716_GPIO_BTN_RFKILL,
210                 .active_low     = 1,
211         },
212         {
213                 .desc           = "WPS button",
214                 .type           = EV_KEY,
215                 .code           = KEY_WPS_BUTTON,
216                 .debounce_interval = NBG6716_KEYS_DEBOUNCE_INTERVAL,
217                 .gpio           = NBG6716_GPIO_BTN_WPS,
218                 .active_low     = 1,
219         },
220 };
221
222
223 static struct ar8327_pad_cfg nbg6716_ar8327_pad0_cfg;
224 static struct ar8327_pad_cfg nbg6716_ar8327_pad6_cfg;
225 static struct ar8327_led_cfg nbg6716_ar8327_led_cfg;
226
227 static struct ar8327_platform_data nbg6716_ar8327_data = {
228         .pad0_cfg = &nbg6716_ar8327_pad0_cfg,
229         .pad6_cfg = &nbg6716_ar8327_pad6_cfg,
230         .port0_cfg = {
231                 .force_link = 1,
232                 .speed = AR8327_PORT_SPEED_1000,
233                 .duplex = 1,
234                 .txpause = 1,
235                 .rxpause = 1,
236         },
237         .port6_cfg = {
238                 .force_link = 1,
239                 .speed = AR8327_PORT_SPEED_1000,
240                 .duplex = 1,
241                 .txpause = 1,
242                 .rxpause = 1,
243         },
244         .led_cfg = &nbg6716_ar8327_led_cfg
245 };
246
247 static struct mdio_board_info nbg6716_mdio0_info[] = {
248         {
249                 .bus_id = "ag71xx-mdio.0",
250                 .mdio_addr = 0,
251                 .platform_data = &nbg6716_ar8327_data,
252         },
253 };
254
255 static void nbg6716_get_mac(void* nvram_addr, const char *name, char *mac)
256 {
257         u8 *nvram = (u8 *) KSEG1ADDR(nvram_addr);
258         int err;
259
260         err = ath79_nvram_parse_mac_addr(nvram, 0x10000,
261                                          name, mac);
262         if (err)
263                 pr_err("no MAC address found for %s\n", name);
264 }
265
266 static void __init nbg6716_common_setup(u32 leds_num, struct gpio_led* leds,
267                                         u32 keys_num,
268                                         struct gpio_keys_button* keys,
269                                         void* art_addr, void* nvram)
270 {
271         u8 *art = (u8 *) KSEG1ADDR(art_addr);
272         u8 tmpmac[ETH_ALEN];
273
274         ath79_register_m25p80(NULL);
275
276         ath79_register_leds_gpio(-1, leds_num, leds);
277         ath79_register_gpio_keys_polled(-1, NBG6716_KEYS_POLL_INTERVAL,
278                                         keys_num, keys);
279
280         ath79_nfc_set_ecc_mode(AR934X_NFC_ECC_HW);
281         ath79_register_nfc();
282
283         gpio_request_one(NBG6716_GPIO_USB_POWER,
284                 GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
285                 "USB power");
286
287         ath79_register_usb();
288
289         nbg6716_get_mac(nvram, "ethaddr=", tmpmac);
290
291         ath79_register_pci();
292
293         ath79_register_wmac(art + NBG6716_WMAC_CALDATA_OFFSET, tmpmac);
294
295         ath79_setup_qca955x_eth_cfg(QCA955X_ETH_CFG_RGMII_EN);
296
297         ath79_register_mdio(0, 0x0);
298
299         ath79_init_mac(ath79_eth0_data.mac_addr, tmpmac, 2);
300         ath79_init_mac(ath79_eth1_data.mac_addr, tmpmac, 3);
301
302         mdiobus_register_board_info(nbg6716_mdio0_info,
303                                     ARRAY_SIZE(nbg6716_mdio0_info));
304
305         /* GMAC0 is connected to the RMGII interface */
306         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
307         ath79_eth0_data.phy_mask = BIT(0);
308         ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
309
310         ath79_register_eth(0);
311
312         /* GMAC1 is connected to the SGMII interface */
313         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_SGMII;
314         ath79_eth1_data.speed = SPEED_1000;
315         ath79_eth1_data.duplex = DUPLEX_FULL;
316
317         ath79_register_eth(1);
318 }
319
320 static void __init nbg6716_010_setup(void)
321 {
322         /* GMAC0 of the AR8337 switch is connected to GMAC0 via RGMII */
323         nbg6716_ar8327_pad0_cfg.mode = AR8327_PAD_MAC_RGMII;
324         nbg6716_ar8327_pad0_cfg.txclk_delay_en = true;
325         nbg6716_ar8327_pad0_cfg.rxclk_delay_en = true;
326         nbg6716_ar8327_pad0_cfg.txclk_delay_sel = AR8327_CLK_DELAY_SEL1;
327         nbg6716_ar8327_pad0_cfg.rxclk_delay_sel = AR8327_CLK_DELAY_SEL2;
328
329         /* GMAC6 of the AR8337 switch is connected to GMAC1 via SGMII */
330         nbg6716_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
331         nbg6716_ar8327_pad6_cfg.rxclk_delay_en = true;
332         nbg6716_ar8327_pad6_cfg.rxclk_delay_sel = AR8327_CLK_DELAY_SEL0;
333
334         ath79_eth0_pll_data.pll_1000 = 0xa6000000;
335         ath79_eth1_pll_data.pll_1000 = 0x03000101;
336
337         nbg6716_ar8327_led_cfg.open_drain = 0;
338         nbg6716_ar8327_led_cfg.led_ctrl0 = 0xffb7ffb7;
339         nbg6716_ar8327_led_cfg.led_ctrl1 = 0xffb7ffb7;
340         nbg6716_ar8327_led_cfg.led_ctrl2 = 0xffb7ffb7;
341         nbg6716_ar8327_led_cfg.led_ctrl3 = 0x03ffff00;
342
343         nbg6716_common_setup(ARRAY_SIZE(nbg6716_leds_gpio), nbg6716_leds_gpio,
344                              ARRAY_SIZE(nbg6716_gpio_keys), nbg6716_gpio_keys,
345                              (void*) 0x1f050000, (void*) 0x1f040000);
346 }
347
348 static void __init nbg6616_010_setup(void)
349 {
350         /* GMAC0 of the AR8337 switch is connected to GMAC0 via RGMII */
351         nbg6716_ar8327_pad0_cfg.mode = AR8327_PAD_MAC_RGMII;
352         nbg6716_ar8327_pad0_cfg.txclk_delay_en = true;
353         nbg6716_ar8327_pad0_cfg.rxclk_delay_en = true;
354         nbg6716_ar8327_pad0_cfg.txclk_delay_sel = AR8327_CLK_DELAY_SEL1;
355         nbg6716_ar8327_pad0_cfg.rxclk_delay_sel = AR8327_CLK_DELAY_SEL2;
356
357         /* GMAC6 of the AR8337 switch is connected to GMAC1 via SGMII */
358         nbg6716_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
359         nbg6716_ar8327_pad6_cfg.rxclk_delay_en = true;
360         nbg6716_ar8327_pad6_cfg.rxclk_delay_sel = AR8327_CLK_DELAY_SEL0;
361
362         ath79_eth0_pll_data.pll_1000 = 0xa6000000;
363         ath79_eth1_pll_data.pll_1000 = 0x03000101;
364
365         nbg6716_ar8327_led_cfg.open_drain = 0;
366         nbg6716_ar8327_led_cfg.led_ctrl0 = 0xffb7ffb7;
367         nbg6716_ar8327_led_cfg.led_ctrl1 = 0xffb7ffb7;
368         nbg6716_ar8327_led_cfg.led_ctrl2 = 0xffb7ffb7;
369         nbg6716_ar8327_led_cfg.led_ctrl3 = 0x03ffff00;
370
371
372         nbg6716_common_setup(ARRAY_SIZE(nbg6616_leds_gpio), nbg6616_leds_gpio,
373                              ARRAY_SIZE(nbg6616_gpio_keys), nbg6616_gpio_keys,
374                              (void*) 0x1f040000, (void*) 0x1f030000);
375 }
376
377
378 MIPS_MACHINE(ATH79_MACH_NBG6716, "NBG6716",
379              "Zyxel NBG6716",
380              nbg6716_010_setup);
381
382 MIPS_MACHINE(ATH79_MACH_NBG6616, "NBG6616",
383              "Zyxel NBG6616",
384              nbg6616_010_setup);
385