ar71xx: wpj531: fix SIG1/RSS1 LED GPIO
[oweals/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/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 #else
260         struct nand_chip *chip = mtd_to_nand(mtd);
261 #endif /* < 4.6.0 */
262
263         if (mtd->writesize == 512) {
264                 /*
265                  * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
266                  * will not be able to find the kernel that we load.
267                  */
268 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
269                 chip->ecc.layout = &rb2011_nand_ecclayout;
270 #else
271                 mtd_set_ooblayout(mtd, &rb2011_nand_ecclayout_ops);
272 #endif
273         }
274
275         chip->options = NAND_NO_SUBPAGE_WRITE;
276
277         return 0;
278 }
279
280 static void __init rb2011_nand_init(void)
281 {
282         gpio_request_one(RB2011_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
283
284         ath79_nfc_set_scan_fixup(rb2011_nand_scan_fixup);
285         ath79_nfc_set_parts(rb2011_nand_partitions,
286                             ARRAY_SIZE(rb2011_nand_partitions));
287         ath79_nfc_set_select_chip(rb2011_nand_select_chip);
288         ath79_nfc_set_swap_dma(true);
289         ath79_register_nfc();
290 }
291
292 static int rb2011_get_port_link(unsigned port)
293 {
294         if (port != 6)
295                 return -EINVAL;
296
297         /* The Loss of signal line is active low */
298         return !gpio_get_value(RB2011_GPIO_SFP_LOS);
299 }
300
301 static void __init rb2011_sfp_init(void)
302 {
303         gpio_request_one(RB2011_GPIO_SFP_LOS, GPIOF_IN, "SFP LOS");
304
305         rb2011_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
306
307         rb2011_ar8327_data.pad6_cfg = &rb2011_ar8327_pad6_cfg;
308
309         rb2011_ar8327_sgmii_cfg.sgmii_ctrl = 0xc70167d0;
310         rb2011_ar8327_sgmii_cfg.serdes_aen = true;
311
312         rb2011_ar8327_data.sgmii_cfg = &rb2011_ar8327_sgmii_cfg;
313
314         rb2011_ar8327_data.port6_cfg.force_link = 1;
315         rb2011_ar8327_data.port6_cfg.speed = AR8327_PORT_SPEED_1000;
316         rb2011_ar8327_data.port6_cfg.duplex = 1;
317
318         rb2011_ar8327_data.get_port_link = rb2011_get_port_link;
319 }
320
321 static int __init rb2011_setup(u32 flags)
322 {
323         const struct rb_info *info;
324         char buf[64];
325
326         info = rb_init_info((void *) KSEG1ADDR(0x1f000000), 0x10000);
327         if (!info)
328                 return -ENODEV;
329
330         scnprintf(buf, sizeof(buf), "Mikrotik RouterBOARD %s",
331                   (info->board_name) ? info->board_name : "");
332         mips_set_machine_name(buf);
333
334         rb2011_init_partitions(info);
335
336         ath79_register_m25p80(&rb2011_spi_flash_data);
337         rb2011_nand_init();
338
339         ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
340                                    AR934X_ETH_CFG_RXD_DELAY |
341                                    AR934X_ETH_CFG_SW_ONLY_MODE);
342
343         ath79_register_mdio(1, 0x0);
344         ath79_register_mdio(0, 0x0);
345
346         mdiobus_register_board_info(rb2011_mdio0_info,
347                                     ARRAY_SIZE(rb2011_mdio0_info));
348
349         /* GMAC0 is connected to an ar8327 switch */
350         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
351         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
352         ath79_eth0_data.phy_mask = BIT(0);
353         ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
354         ath79_eth0_pll_data.pll_1000 = 0x6f000000;
355
356         ath79_register_eth(0);
357
358         /* GMAC1 is connected to the internal switch */
359         ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 5);
360         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
361         ath79_eth1_data.speed = SPEED_1000;
362         ath79_eth1_data.duplex = DUPLEX_FULL;
363
364         ath79_register_eth(1);
365
366         if (flags & RB2011_FLAG_SFP)
367                 rb2011_sfp_init();
368
369         if (flags & RB2011_FLAG_WLAN)
370                 rb2011_wlan_init();
371
372         if (flags & RB2011_FLAG_USB)
373                 ath79_register_usb();
374
375         return 0;
376 }
377
378 static void __init rb2011l_setup(void)
379 {
380         rb2011_setup(0);
381 }
382
383 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011L, "2011L", rb2011l_setup);
384
385 static void __init rb2011us_setup(void)
386 {
387         rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB);
388 }
389
390 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011US, "2011US", rb2011us_setup);
391
392 static void __init rb2011r5_setup(void)
393 {
394         rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB | RB2011_FLAG_WLAN);
395 }
396
397 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011R5, "2011r5", rb2011r5_setup);
398
399 static void __init rb2011g_setup(void)
400 {
401         rb2011_setup(RB2011_FLAG_SFP |
402                      RB2011_FLAG_USB |
403                      RB2011_FLAG_WLAN);
404 }
405
406 MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011G, "2011G", rb2011g_setup);