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