ar71xx: add support for Fritz!Box 4020
[openwrt/staging/lynxis.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-fritz4020.c
1 /*
2 * AVM FRITZ!Box 4020 board support
3 *
4 * Copyright (C) 2018 David Bauer <mail@david-bauer.net>
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/init.h>
12 #include <linux/ath9k_platform.h>
13 #include <linux/etherdevice.h>
14 #include <linux/gpio.h>
15 #include <linux/platform_device.h>
16
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/platform_data/mdio-gpio.h>
20 #include <linux/spi/spi_gpio.h>
21 #include <linux/spi/74x164.h>
22
23 #include <asm/mach-ath79/ath79.h>
24 #include <asm/mach-ath79/ar71xx_regs.h>
25
26 #include "common.h"
27 #include "dev-eth.h"
28 #include "dev-gpio-buttons.h"
29 #include "dev-leds-gpio.h"
30 #include "dev-m25p80.h"
31 #include "dev-spi.h"
32 #include "dev-usb.h"
33 #include "dev-wmac.h"
34 #include "machtypes.h"
35
36
37 #define FRITZ4020_GPIO_SHIFT_SER 19 /* DS, Data Serial Input */
38 #define FRITZ4020_GPIO_SHIFT_SRCLK 20 /* SHCP, Shift Reg Clock Input */
39
40 #define FRITZ4020_SSR_BIT_0 0
41 #define FRITZ4020_SSR_BIT_1 1
42 #define FRITZ4020_SSR_BIT_2 2
43 #define FRITZ4020_SSR_BIT_3 3
44 #define FRITZ4020_SSR_BIT_4 4
45 #define FRITZ4020_SSR_BIT_5 5
46 #define FRITZ4020_SSR_BIT_6 6
47 #define FRITZ4020_SSR_BIT_7 7
48
49 #define FRITZ4020_74HC_GPIO_BASE 32
50 #define FRITZ4020_74HC_GPIO_LED_LAN (FRITZ4020_74HC_GPIO_BASE + 0)
51 #define FRITZ4020_74HC_GPIO_LED_INFO_RED (FRITZ4020_74HC_GPIO_BASE + 1)
52 #define FRITZ4020_74HC_GPIO_LED_POWER (FRITZ4020_74HC_GPIO_BASE + 2)
53 #define FRITZ4020_74HC_GPIO_LED_WLAN (FRITZ4020_74HC_GPIO_BASE + 3)
54 #define FRITZ4020_74HC_GPIO_LED_WAN (FRITZ4020_74HC_GPIO_BASE + 4)
55 #define FRITZ4020_74HC_GPIO_USB_RST (FRITZ4020_74HC_GPIO_BASE + 5)
56 #define FRITZ4020_74HC_GPIO_LED_INFO (FRITZ4020_74HC_GPIO_BASE + 6)
57
58
59 #define FRITZ4020_GPIO_BTN_WPS 2
60 #define FRITZ4020_GPIO_BTN_WLAN 21
61 #define FRITZ4020_KEYS_POLL_INTERVAL 20 /* msecs */
62 #define FRITZ4020_KEYS_DEBOUNCE_INTERVAL (3 * FRITZ4020_KEYS_POLL_INTERVAL)
63
64 #define FRTIZ4020_OFFSET_URLADER_WIFI_MAC_REVERSE 0x1979
65
66
67 static struct spi_gpio_platform_data fritz4020_spi_data = {
68 .sck = FRITZ4020_GPIO_SHIFT_SRCLK,
69 .miso = SPI_GPIO_NO_MISO,
70 .mosi = FRITZ4020_GPIO_SHIFT_SER,
71 .num_chipselect = 1,
72 };
73
74 static u8 fritz4020_ssr_initdata[] = {
75 BIT(FRITZ4020_SSR_BIT_7) |
76 BIT(FRITZ4020_SSR_BIT_6) |
77 BIT(FRITZ4020_SSR_BIT_5) |
78 BIT(FRITZ4020_SSR_BIT_4) |
79 BIT(FRITZ4020_SSR_BIT_3) |
80 BIT(FRITZ4020_SSR_BIT_2) |
81 BIT(FRITZ4020_SSR_BIT_1)
82 };
83
84 static struct gen_74x164_chip_platform_data fritz4020_ssr_data = {
85 .base = FRITZ4020_74HC_GPIO_BASE,
86 .num_registers = ARRAY_SIZE(fritz4020_ssr_initdata),
87 .init_data = fritz4020_ssr_initdata,
88 };
89
90 static struct platform_device fritz4020_spi_device = {
91 .name = "spi_gpio",
92 .id = 1,
93 .dev = {
94 .platform_data = &fritz4020_spi_data,
95 },
96 };
97
98 static struct spi_board_info fritz4020_spi_info[] = {
99 {
100 .bus_num = 1,
101 .chip_select = 0,
102 .max_speed_hz = 10000000,
103 .modalias = "74x164",
104 .platform_data = &fritz4020_ssr_data,
105 .controller_data = (void *) 0x0,
106 },
107 };
108
109 static struct mtd_partition fritz4020_flash_partitions[] = {
110 {
111 .name = "urlader",
112 .offset = 0,
113 .size = 0x0020000,
114 .mask_flags = MTD_WRITEABLE,
115 }, {
116 .name = "firmware",
117 .offset = 0x0020000,
118 .size = 0x0EE0000,
119 }, {
120 .name = "tffs (1)",
121 .offset = 0x0f00000,
122 .size = 0x0080000,
123 .mask_flags = MTD_WRITEABLE,
124 }, {
125 .name = "tffs (2)",
126 .offset = 0x0f80000,
127 .size = 0x0080000,
128 .mask_flags = MTD_WRITEABLE,
129 }
130 };
131
132 static struct flash_platform_data fritz4020_flash_data = {
133 .parts = fritz4020_flash_partitions,
134 .nr_parts = ARRAY_SIZE(fritz4020_flash_partitions),
135 };
136
137 static struct gpio_led fritz4020_leds_gpio[] __initdata = {
138 {
139 .name = "fritz4020:green:lan",
140 .gpio = FRITZ4020_74HC_GPIO_LED_LAN,
141 .active_low = 1,
142 }, {
143 .name = "fritz4020:green:info",
144 .gpio = FRITZ4020_74HC_GPIO_LED_INFO,
145 .active_low = 1,
146 }, {
147 .name = "fritz4020:red:info",
148 .gpio = FRITZ4020_74HC_GPIO_LED_INFO_RED,
149 .active_low = 1,
150 }, {
151 .name = "fritz4020:green:power",
152 .gpio = FRITZ4020_74HC_GPIO_LED_POWER,
153 .active_low = 1,
154 }, {
155 .name = "fritz4020:green:wlan",
156 .gpio = FRITZ4020_74HC_GPIO_LED_WLAN,
157 .active_low = 1,
158 }, {
159 .name = "fritz4020:green:wan",
160 .gpio = FRITZ4020_74HC_GPIO_LED_WAN,
161 .active_low = 1,
162 },
163 };
164
165 static struct gpio_keys_button fritz4020_gpio_keys[] __initdata = {
166 {
167 .desc = "RFKILL button",
168 .type = EV_KEY,
169 .code = KEY_RFKILL,
170 .debounce_interval = FRITZ4020_KEYS_DEBOUNCE_INTERVAL,
171 .gpio = FRITZ4020_GPIO_BTN_WLAN,
172 .active_low = 0,
173 },
174 {
175 .desc = "WPS button",
176 .type = EV_KEY,
177 .code = KEY_WPS_BUTTON,
178 .debounce_interval = FRITZ4020_KEYS_DEBOUNCE_INTERVAL,
179 .gpio = FRITZ4020_GPIO_BTN_WPS,
180 .active_low = 0,
181 },
182 };
183
184 static void __init fritz4020_setup(void) {
185 u8 *urlader = (u8 *) KSEG1ADDR(0x1f000000);
186 u8 wifi_mac[ETH_ALEN];
187
188 ath79_register_m25p80(&fritz4020_flash_data);
189
190 /* Initialize ethernet */
191 ath79_extract_mac_reverse(urlader + FRTIZ4020_OFFSET_URLADER_WIFI_MAC_REVERSE, wifi_mac);
192 ath79_setup_qca956x_eth_cfg(QCA956X_ETH_CFG_SW_PHY_SWAP |
193 QCA956X_ETH_CFG_SW_PHY_ADDR_SWAP);
194
195 ath79_register_mdio(0, 0x0);
196 ath79_register_mdio(1, 0x0);
197 ath79_init_mac(ath79_eth0_data.mac_addr, wifi_mac, -1);
198 ath79_init_mac(ath79_eth1_data.mac_addr, wifi_mac, -2);
199
200 /* WAN port */
201 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
202 ath79_eth0_data.speed = SPEED_100;
203 ath79_eth0_data.duplex = DUPLEX_FULL;
204 ath79_eth0_data.phy_mask = BIT(0);
205 ath79_register_eth(0);
206
207 /* LAN ports */
208 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
209 ath79_eth1_data.speed = SPEED_1000;
210 ath79_eth1_data.duplex = DUPLEX_FULL;
211 ath79_switch_data.phy_poll_mask |= BIT(0);
212 ath79_switch_data.phy4_mii_en = 1;
213 ath79_register_eth(1);
214
215 /* Initialize 2.4GHz WiFi */
216 ath79_register_wmac_simple();
217
218 /* Activate USB Power */
219 gpio_request_one(FRITZ4020_74HC_GPIO_USB_RST,
220 GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
221 "USB power");
222
223 /* Initialize USB port */
224 ath79_register_usb();
225
226 /* Register LED shift-register */
227 spi_register_board_info(fritz4020_spi_info,
228 ARRAY_SIZE(fritz4020_spi_info));
229 platform_device_register(&fritz4020_spi_device);
230
231 /* Register GPIO buttons */
232 ath79_register_gpio_keys_polled(-1, FRITZ4020_KEYS_POLL_INTERVAL,
233 ARRAY_SIZE(fritz4020_gpio_keys),
234 fritz4020_gpio_keys);
235
236 /* Register LEDs */
237 ath79_register_leds_gpio(-1, ARRAY_SIZE(fritz4020_leds_gpio),
238 fritz4020_leds_gpio);
239 }
240
241 MIPS_MACHINE(ATH79_MACH_FRITZ4020, "FRITZ4020",
242 "AVM FRITZ!Box 4020", fritz4020_setup);