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