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