ar71xx: add v4.14 support
[openwrt/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-rb2011.c
1 /*
2 * MikroTik RouterBOARD 2011 support
3 *
4 * Copyright (C) 2012 Stijn Tintel <stijn@linux-ipv6.be>
5 * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
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 #define pr_fmt(fmt) "rb2011: " fmt
13
14 #include <linux/phy.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/ath9k_platform.h>
18 #include <linux/ar8216_platform.h>
19 #include <linux/mtd/mtd.h>
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
21 #include <linux/mtd/nand.h>
22 #else
23 #include <linux/mtd/rawnand.h>
24 #endif
25 #include <linux/mtd/partitions.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/flash.h>
28 #include <linux/routerboot.h>
29 #include <linux/gpio.h>
30 #include <linux/version.h>
31
32 #include <asm/prom.h>
33 #include <asm/mach-ath79/ath79.h>
34 #include <asm/mach-ath79/ar71xx_regs.h>
35
36 #include "common.h"
37 #include "dev-eth.h"
38 #include "dev-m25p80.h"
39 #include "dev-nfc.h"
40 #include "dev-usb.h"
41 #include "dev-wmac.h"
42 #include "machtypes.h"
43 #include "routerboot.h"
44
45 #define RB2011_GPIO_NAND_NCE 14
46 #define RB2011_GPIO_SFP_LOS 21
47
48 #define RB_ROUTERBOOT_OFFSET 0x0000
49 #define RB_ROUTERBOOT_MIN_SIZE 0xb000
50 #define RB_HARD_CFG_SIZE 0x1000
51 #define RB_BIOS_OFFSET 0xd000
52 #define RB_BIOS_SIZE 0x1000
53 #define RB_SOFT_CFG_OFFSET 0xf000
54 #define RB_SOFT_CFG_SIZE 0x1000
55
56 #define RB_ART_SIZE 0x10000
57
58 #define RB2011_FLAG_SFP BIT(0)
59 #define RB2011_FLAG_USB BIT(1)
60 #define RB2011_FLAG_WLAN BIT(2)
61
62 static struct mtd_partition rb2011_spi_partitions[] = {
63 {
64 .name = "routerboot",
65 .offset = RB_ROUTERBOOT_OFFSET,
66 .mask_flags = MTD_WRITEABLE,
67 }, {
68 .name = "hard_config",
69 .size = RB_HARD_CFG_SIZE,
70 .mask_flags = MTD_WRITEABLE,
71 }, {
72 .name = "bios",
73 .offset = RB_BIOS_OFFSET,
74 .size = RB_BIOS_SIZE,
75 .mask_flags = MTD_WRITEABLE,
76 }, {
77 .name = "soft_config",
78 .size = RB_SOFT_CFG_SIZE,
79 }
80 };
81
82 static void __init rb2011_init_partitions(const struct rb_info *info)
83 {
84 rb2011_spi_partitions[0].size = info->hard_cfg_offs;
85 rb2011_spi_partitions[1].offset = info->hard_cfg_offs;
86 rb2011_spi_partitions[3].offset = info->soft_cfg_offs;
87 }
88
89 static struct mtd_partition rb2011_nand_partitions[] = {
90 {
91 .name = "booter",
92 .offset = 0,
93 .size = (256 * 1024),
94 .mask_flags = MTD_WRITEABLE,
95 },
96 {
97 .name = "kernel",
98 .offset = (256 * 1024),
99 .size = (4 * 1024 * 1024) - (256 * 1024),
100 },
101 {
102 .name = "ubi",
103 .offset = MTDPART_OFS_NXTBLK,
104 .size = MTDPART_SIZ_FULL,
105 },
106 };
107
108 static struct flash_platform_data rb2011_spi_flash_data = {
109 .parts = rb2011_spi_partitions,
110 .nr_parts = ARRAY_SIZE(rb2011_spi_partitions),
111 };
112
113 static struct ar8327_pad_cfg rb2011_ar8327_pad0_cfg = {
114 .mode = AR8327_PAD_MAC_RGMII,
115 .txclk_delay_en = true,
116 .rxclk_delay_en = true,
117 .txclk_delay_sel = AR8327_CLK_DELAY_SEL3,
118 .rxclk_delay_sel = AR8327_CLK_DELAY_SEL0,
119 };
120
121 static struct ar8327_pad_cfg rb2011_ar8327_pad6_cfg;
122 static struct ar8327_sgmii_cfg rb2011_ar8327_sgmii_cfg;
123
124 static struct ar8327_led_cfg rb2011_ar8327_led_cfg = {
125 .led_ctrl0 = 0xc731c731,
126 .led_ctrl1 = 0x00000000,
127 .led_ctrl2 = 0x00000000,
128 .led_ctrl3 = 0x0030c300,
129 .open_drain = false,
130 };
131
132 static const struct ar8327_led_info rb2011_ar8327_leds[] = {
133 AR8327_LED_INFO(PHY0_0, HW, "rb:green:eth1"),
134 AR8327_LED_INFO(PHY1_0, HW, "rb:green:eth2"),
135 AR8327_LED_INFO(PHY2_0, HW, "rb:green:eth3"),
136 AR8327_LED_INFO(PHY3_0, HW, "rb:green:eth4"),
137 AR8327_LED_INFO(PHY4_0, HW, "rb:green:eth5"),
138 AR8327_LED_INFO(PHY0_1, SW, "rb:green:eth6"),
139 AR8327_LED_INFO(PHY1_1, SW, "rb:green:eth7"),
140 AR8327_LED_INFO(PHY2_1, SW, "rb:green:eth8"),
141 AR8327_LED_INFO(PHY3_1, SW, "rb:green:eth9"),
142 AR8327_LED_INFO(PHY4_1, SW, "rb:green:eth10"),
143 AR8327_LED_INFO(PHY4_2, SW, "rb:green:usr"),
144 };
145
146 static struct ar8327_platform_data rb2011_ar8327_data = {
147 .pad0_cfg = &rb2011_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 .led_cfg = &rb2011_ar8327_led_cfg,
156 .num_leds = ARRAY_SIZE(rb2011_ar8327_leds),
157 .leds = rb2011_ar8327_leds,
158 };
159
160 static struct mdio_board_info rb2011_mdio0_info[] = {
161 {
162 .bus_id = "ag71xx-mdio.0",
163 .mdio_addr = 0,
164 .platform_data = &rb2011_ar8327_data,
165 },
166 };
167
168 static void __init rb2011_wlan_init(void)
169 {
170 char *art_buf;
171 u8 wlan_mac[ETH_ALEN];
172
173 art_buf = rb_get_wlan_data();
174 if (art_buf == NULL)
175 return;
176
177 ath79_init_mac(wlan_mac, ath79_mac_base, 11);
178 ath79_register_wmac(art_buf + 0x1000, wlan_mac);
179
180 kfree(art_buf);
181 }
182
183 static void rb2011_nand_select_chip(int chip_no)
184 {
185 switch (chip_no) {
186 case 0:
187 gpio_set_value(RB2011_GPIO_NAND_NCE, 0);
188 break;
189 default:
190 gpio_set_value(RB2011_GPIO_NAND_NCE, 1);
191 break;
192 }
193 ndelay(500);
194 }
195
196 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
197 static struct nand_ecclayout rb2011_nand_ecclayout = {
198 .eccbytes = 6,
199 .eccpos = { 8, 9, 10, 13, 14, 15 },
200 .oobavail = 9,
201 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
202 };
203
204 #else
205
206 static int rb2011_ooblayout_ecc(struct mtd_info *mtd, int section,
207 struct mtd_oob_region *oobregion)
208 {
209 switch (section) {
210 case 0:
211 oobregion->offset = 8;
212 oobregion->length = 3;
213 return 0;
214 case 1:
215 oobregion->offset = 13;
216 oobregion->length = 3;
217 return 0;
218 default:
219 return -ERANGE;
220 }
221 }
222
223 static int rb2011_ooblayout_free(struct mtd_info *mtd, int section,
224 struct mtd_oob_region *oobregion)
225 {
226 switch (section) {
227 case 0:
228 oobregion->offset = 0;
229 oobregion->length = 4;
230 return 0;
231 case 1:
232 oobregion->offset = 4;
233 oobregion->length = 1;
234 return 0;
235 case 2:
236 oobregion->offset = 6;
237 oobregion->length = 2;
238 return 0;
239 case 3:
240 oobregion->offset = 11;
241 oobregion->length = 2;
242 return 0;
243 default:
244 return -ERANGE;
245 }
246 }
247
248 static const struct mtd_ooblayout_ops rb2011_nand_ecclayout_ops = {
249 .ecc = rb2011_ooblayout_ecc,
250 .free = rb2011_ooblayout_free,
251 };
252 #endif /* < 4.6 */
253
254 static int rb2011_nand_scan_fixup(struct mtd_info *mtd)
255 {
256 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
257 struct nand_chip *chip = mtd->priv;
258 #endif
259
260 if (mtd->writesize == 512) {
261 /*
262 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
263 * will not be able to find the kernel that we load.
264 */
265 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
266 chip->ecc.layout = &rb2011_nand_ecclayout;
267 #else
268 mtd_set_ooblayout(mtd, &rb2011_nand_ecclayout_ops);
269 #endif
270 }
271
272 return 0;
273 }
274
275 static void __init rb2011_nand_init(void)
276 {
277 gpio_request_one(RB2011_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
278
279 ath79_nfc_set_scan_fixup(rb2011_nand_scan_fixup);
280 ath79_nfc_set_parts(rb2011_nand_partitions,
281 ARRAY_SIZE(rb2011_nand_partitions));
282 ath79_nfc_set_select_chip(rb2011_nand_select_chip);
283 ath79_nfc_set_swap_dma(true);
284 ath79_register_nfc();
285 }
286
287 static int rb2011_get_port_link(unsigned port)
288 {
289 if (port != 6)
290 return -EINVAL;
291
292 /* The Loss of signal line is active low */
293 return !gpio_get_value(RB2011_GPIO_SFP_LOS);
294 }
295
296 static void __init rb2011_sfp_init(void)
297 {
298 gpio_request_one(RB2011_GPIO_SFP_LOS, GPIOF_IN, "SFP LOS");
299
300 rb2011_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
301
302 rb2011_ar8327_data.pad6_cfg = &rb2011_ar8327_pad6_cfg;
303
304 rb2011_ar8327_sgmii_cfg.sgmii_ctrl = 0xc70167d0;
305 rb2011_ar8327_sgmii_cfg.serdes_aen = true;
306
307 rb2011_ar8327_data.sgmii_cfg = &rb2011_ar8327_sgmii_cfg;
308
309 rb2011_ar8327_data.port6_cfg.force_link = 1;
310 rb2011_ar8327_data.port6_cfg.speed = AR8327_PORT_SPEED_1000;
311 rb2011_ar8327_data.port6_cfg.duplex = 1;
312
313 rb2011_ar8327_data.get_port_link = rb2011_get_port_link;
314 }
315
316 static int __init rb2011_setup(u32 flags)
317 {
318 const struct rb_info *info;
319 char buf[64];
320
321 info = rb_init_info((void *) KSEG1ADDR(0x1f000000), 0x10000);
322 if (!info)
323 return -ENODEV;
324
325 scnprintf(buf, sizeof(buf), "Mikrotik RouterBOARD %s",
326 (info->board_name) ? info->board_name : "");
327 mips_set_machine_name(buf);
328
329 rb2011_init_partitions(info);
330
331 ath79_register_m25p80(&rb2011_spi_flash_data);
332 rb2011_nand_init();
333
334 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
335 AR934X_ETH_CFG_RXD_DELAY |
336 AR934X_ETH_CFG_SW_ONLY_MODE);
337
338 ath79_register_mdio(1, 0x0);
339 ath79_register_mdio(0, 0x0);
340
341 mdiobus_register_board_info(rb2011_mdio0_info,
342 ARRAY_SIZE(rb2011_mdio0_info));
343
344 /* GMAC0 is connected to an ar8327 switch */
345 ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
346 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
347 ath79_eth0_data.phy_mask = BIT(0);
348 ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
349 ath79_eth0_pll_data.pll_1000 = 0x6f000000;
350
351 ath79_register_eth(0);
352
353 /* GMAC1 is connected to the internal switch */
354 ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 5);
355 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
356 ath79_eth1_data.speed = SPEED_1000;
357 ath79_eth1_data.duplex = DUPLEX_FULL;
358
359 ath79_register_eth(1);
360
361 if (flags & RB2011_FLAG_SFP)
362 rb2011_sfp_init();
363
364 if (flags & RB2011_FLAG_WLAN)
365 rb2011_wlan_init();
366
367 if (flags & RB2011_FLAG_USB)
368 ath79_register_usb();
369
370 return 0;
371 }
372
373 static void __init rb2011l_setup(void)
374 {
375 rb2011_setup(0);
376 }
377
378 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011L, "2011L", rb2011l_setup);
379
380 static void __init rb2011us_setup(void)
381 {
382 rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB);
383 }
384
385 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011US, "2011US", rb2011us_setup);
386
387 static void __init rb2011r5_setup(void)
388 {
389 rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB | RB2011_FLAG_WLAN);
390 }
391
392 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011R5, "2011r5", rb2011r5_setup);
393
394 static void __init rb2011g_setup(void)
395 {
396 rb2011_setup(RB2011_FLAG_SFP |
397 RB2011_FLAG_USB |
398 RB2011_FLAG_WLAN);
399 }
400
401 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011G, "2011G", rb2011g_setup);