ar71xx: rb2011: simplify setup code
[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
27 #include <asm/mach-ath79/ath79.h>
28 #include <asm/mach-ath79/ar71xx_regs.h>
29
30 #include "common.h"
31 #include "dev-eth.h"
32 #include "dev-m25p80.h"
33 #include "dev-nfc.h"
34 #include "dev-usb.h"
35 #include "dev-wmac.h"
36 #include "machtypes.h"
37 #include "routerboot.h"
38
39 #define RB2011_GPIO_NAND_NCE    14
40 #define RB2011_GPIO_SFP_LOS     21
41
42 #define RB_ROUTERBOOT_OFFSET    0x0000
43 #define RB_ROUTERBOOT_MIN_SIZE  0xb000
44 #define RB_HARD_CFG_SIZE        0x1000
45 #define RB_BIOS_OFFSET          0xd000
46 #define RB_BIOS_SIZE            0x1000
47 #define RB_SOFT_CFG_OFFSET      0xf000
48 #define RB_SOFT_CFG_SIZE        0x1000
49
50 #define RB_ART_SIZE             0x10000
51
52 #define RB2011_FLAG_SFP         BIT(0)
53 #define RB2011_FLAG_USB         BIT(1)
54 #define RB2011_FLAG_WLAN        BIT(2)
55
56 static struct mtd_partition rb2011_spi_partitions[] = {
57         {
58                 .name           = "routerboot",
59                 .offset         = RB_ROUTERBOOT_OFFSET,
60                 .mask_flags     = MTD_WRITEABLE,
61         }, {
62                 .name           = "hard_config",
63                 .size           = RB_HARD_CFG_SIZE,
64                 .mask_flags     = MTD_WRITEABLE,
65         }, {
66                 .name           = "bios",
67                 .offset         = RB_BIOS_OFFSET,
68                 .size           = RB_BIOS_SIZE,
69                 .mask_flags     = MTD_WRITEABLE,
70         }, {
71                 .name           = "soft_config",
72                 .size           = RB_SOFT_CFG_SIZE,
73         }
74 };
75
76 static void __init rb2011_init_partitions(const struct rb_info *info)
77 {
78         rb2011_spi_partitions[0].size = info->hard_cfg_offs;
79         rb2011_spi_partitions[1].offset = info->hard_cfg_offs;
80         rb2011_spi_partitions[3].offset = info->soft_cfg_offs;
81 }
82
83 static struct mtd_partition rb2011_nand_partitions[] = {
84         {
85                 .name   = "booter",
86                 .offset = 0,
87                 .size   = (256 * 1024),
88                 .mask_flags = MTD_WRITEABLE,
89         },
90         {
91                 .name   = "kernel",
92                 .offset = (256 * 1024),
93                 .size   = (4 * 1024 * 1024) - (256 * 1024),
94         },
95         {
96                 .name   = "rootfs",
97                 .offset = MTDPART_OFS_NXTBLK,
98                 .size   = MTDPART_SIZ_FULL,
99         },
100 };
101
102 static struct flash_platform_data rb2011_spi_flash_data = {
103         .parts          = rb2011_spi_partitions,
104         .nr_parts       = ARRAY_SIZE(rb2011_spi_partitions),
105 };
106
107 static struct ar8327_pad_cfg rb2011_ar8327_pad0_cfg = {
108         .mode = AR8327_PAD_MAC_RGMII,
109         .txclk_delay_en = true,
110         .rxclk_delay_en = true,
111         .txclk_delay_sel = AR8327_CLK_DELAY_SEL3,
112         .rxclk_delay_sel = AR8327_CLK_DELAY_SEL0,
113 };
114
115 static struct ar8327_pad_cfg rb2011_ar8327_pad6_cfg;
116 static struct ar8327_sgmii_cfg rb2011_ar8327_sgmii_cfg;
117
118 static struct ar8327_led_cfg rb2011_ar8327_led_cfg = {
119         .led_ctrl0 = 0x0000c731,
120         .led_ctrl1 = 0x00000000,
121         .led_ctrl2 = 0x00000000,
122         .led_ctrl3 = 0x0030c300,
123         .open_drain = false,
124 };
125
126 static struct ar8327_platform_data rb2011_ar8327_data = {
127         .pad0_cfg = &rb2011_ar8327_pad0_cfg,
128         .port0_cfg = {
129                 .force_link = 1,
130                 .speed = AR8327_PORT_SPEED_1000,
131                 .duplex = 1,
132                 .txpause = 1,
133                 .rxpause = 1,
134         },
135         .led_cfg = &rb2011_ar8327_led_cfg,
136 };
137
138 static struct mdio_board_info rb2011_mdio0_info[] = {
139         {
140                 .bus_id = "ag71xx-mdio.0",
141                 .phy_addr = 0,
142                 .platform_data = &rb2011_ar8327_data,
143         },
144 };
145
146 static void __init rb2011_wlan_init(void)
147 {
148         char *art_buf;
149         u8 wlan_mac[ETH_ALEN];
150
151         art_buf = rb_get_wlan_data();
152         if (art_buf == NULL)
153                 return;
154
155         ath79_init_mac(wlan_mac, ath79_mac_base, 11);
156         ath79_register_wmac(art_buf + 0x1000, wlan_mac);
157
158         kfree(art_buf);
159 }
160
161 static void rb2011_nand_select_chip(int chip_no)
162 {
163         switch (chip_no) {
164         case 0:
165                 gpio_set_value(RB2011_GPIO_NAND_NCE, 0);
166                 break;
167         default:
168                 gpio_set_value(RB2011_GPIO_NAND_NCE, 1);
169                 break;
170         }
171         ndelay(500);
172 }
173
174 static struct nand_ecclayout rb2011_nand_ecclayout = {
175         .eccbytes       = 6,
176         .eccpos         = { 8, 9, 10, 13, 14, 15 },
177         .oobavail       = 9,
178         .oobfree        = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
179 };
180
181 static int rb2011_nand_scan_fixup(struct mtd_info *mtd)
182 {
183         struct nand_chip *chip = mtd->priv;
184
185         if (mtd->writesize == 512) {
186                 /*
187                  * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
188                  * will not be able to find the kernel that we load.
189                  */
190                 chip->ecc.layout = &rb2011_nand_ecclayout;
191         }
192
193         return 0;
194 }
195
196 static void __init rb2011_nand_init(void)
197 {
198         gpio_request_one(RB2011_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
199
200         ath79_nfc_set_scan_fixup(rb2011_nand_scan_fixup);
201         ath79_nfc_set_parts(rb2011_nand_partitions,
202                             ARRAY_SIZE(rb2011_nand_partitions));
203         ath79_nfc_set_select_chip(rb2011_nand_select_chip);
204         ath79_nfc_set_swap_dma(true);
205         ath79_register_nfc();
206 }
207
208 static int rb2011_get_port_link(unsigned port)
209 {
210         if (port != 6)
211                 return -EINVAL;
212
213         /* The Loss of signal line is active low */
214         return !gpio_get_value(RB2011_GPIO_SFP_LOS);
215 }
216
217 static void __init rb2011_sfp_init(void)
218 {
219         gpio_request_one(RB2011_GPIO_SFP_LOS, GPIOF_IN, "SFP LOS");
220
221         rb2011_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
222
223         rb2011_ar8327_data.pad6_cfg = &rb2011_ar8327_pad6_cfg;
224
225         rb2011_ar8327_sgmii_cfg.sgmii_ctrl = 0xc70167d0;
226         rb2011_ar8327_sgmii_cfg.serdes_aen = true;
227
228         rb2011_ar8327_data.sgmii_cfg = &rb2011_ar8327_sgmii_cfg;
229
230         rb2011_ar8327_data.port6_cfg.force_link = 1;
231         rb2011_ar8327_data.port6_cfg.speed = AR8327_PORT_SPEED_1000;
232         rb2011_ar8327_data.port6_cfg.duplex = 1;
233
234         rb2011_ar8327_data.get_port_link = rb2011_get_port_link;
235 }
236
237 static int __init rb2011_setup(u32 flags)
238 {
239         const struct rb_info *info;
240
241         info = rb_init_info((void *) KSEG1ADDR(0x1f000000), 0x10000);
242         if (!info)
243                 return -ENODEV;
244
245         rb2011_init_partitions(info);
246
247         ath79_register_m25p80(&rb2011_spi_flash_data);
248         rb2011_nand_init();
249
250         ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
251                                    AR934X_ETH_CFG_SW_ONLY_MODE);
252
253         ath79_register_mdio(1, 0x0);
254         ath79_register_mdio(0, 0x0);
255
256         mdiobus_register_board_info(rb2011_mdio0_info,
257                                     ARRAY_SIZE(rb2011_mdio0_info));
258
259         /* GMAC0 is connected to an ar8327 switch */
260         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
261         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
262         ath79_eth0_data.phy_mask = BIT(0);
263         ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
264         ath79_eth0_pll_data.pll_1000 = 0x06000000;
265
266         ath79_register_eth(0);
267
268         /* GMAC1 is connected to the internal switch */
269         ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 5);
270         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
271         ath79_eth1_data.speed = SPEED_1000;
272         ath79_eth1_data.duplex = DUPLEX_FULL;
273
274         ath79_register_eth(1);
275
276         if (flags & RB2011_FLAG_SFP)
277                 rb2011_sfp_init();
278
279         if (flags & RB2011_FLAG_WLAN)
280                 rb2011_wlan_init();
281
282         if (flags & RB2011_FLAG_USB)
283                 ath79_register_usb();
284
285         return 0;
286 }
287
288 static void __init rb2011l_setup(void)
289 {
290         rb2011_setup(0);
291 }
292
293 MIPS_MACHINE(ATH79_MACH_RB_2011L, "2011L", "MikroTik RouterBOARD 2011L",
294              rb2011l_setup);
295
296 static void __init rb2011us_setup(void)
297 {
298         rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB);
299 }
300
301 MIPS_MACHINE(ATH79_MACH_RB_2011US, "2011US", "MikroTik RouterBOARD 2011UAS",
302              rb2011us_setup);
303
304 static void __init rb2011g_setup(void)
305 {
306         rb2011_setup(RB2011_FLAG_SFP |
307                      RB2011_FLAG_USB |
308                      RB2011_FLAG_WLAN);
309 }
310
311 MIPS_MACHINE(ATH79_MACH_RB_2011G, "2011G", "MikroTik RouterBOARD 2011UAS-2HnD",
312              rb2011g_setup);