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