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