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