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