c2261ab9f121d3ea4052256dc9b184bac9630dd4
[openwrt/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-rb95x.c
1 /*
2 * MikroTik RouterBOARD 95X support
3 *
4 * Copyright (C) 2012 Stijn Tintel <stijn@linux-ipv6.be>
5 * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2013 Kamil Trzcinski <ayufan@ayufan.eu>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 */
12
13 #define pr_fmt(fmt) "rb95x: " fmt
14
15 #include <linux/phy.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/ath9k_platform.h>
19 #include <linux/ar8216_platform.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/nand.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/spi/spi.h>
24 #include <linux/spi/flash.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 #include "dev-leds-gpio.h"
40
41 #define RB95X_GPIO_NAND_NCE 14
42
43 static struct mtd_partition rb95x_nand_partitions[] = {
44 {
45 .name = "booter",
46 .offset = 0,
47 .size = (256 * 1024),
48 .mask_flags = MTD_WRITEABLE,
49 },
50 {
51 .name = "kernel",
52 .offset = (256 * 1024),
53 .size = (4 * 1024 * 1024) - (256 * 1024),
54 },
55 {
56 .name = "rootfs",
57 .offset = MTDPART_OFS_NXTBLK,
58 .size = MTDPART_SIZ_FULL,
59 },
60 };
61
62 static struct gpio_led rb951ui_leds_gpio[] __initdata = {
63 {
64 .name = "rb:green:wlan",
65 .gpio = 11,
66 .active_low = 1,
67 }, {
68 .name = "rb:green:act",
69 .gpio = 3,
70 .active_low = 1,
71 }, {
72 .name = "rb:green:port1",
73 .gpio = 13,
74 .active_low = 1,
75 }, {
76 .name = "rb:green:port2",
77 .gpio = 12,
78 .active_low = 1,
79 }, {
80 .name = "rb:green:port3",
81 .gpio = 4,
82 .active_low = 1,
83 }, {
84 .name = "rb:green:port4",
85 .gpio = 21,
86 .active_low = 1,
87 }, {
88 .name = "rb:green:port5",
89 .gpio = 16,
90 .active_low = 1,
91 }
92 };
93
94 static struct ar8327_pad_cfg rb95x_ar8327_pad0_cfg = {
95 .mode = AR8327_PAD_MAC_RGMII,
96 .txclk_delay_en = true,
97 .rxclk_delay_en = true,
98 .txclk_delay_sel = AR8327_CLK_DELAY_SEL1,
99 .rxclk_delay_sel = AR8327_CLK_DELAY_SEL2,
100 };
101
102 static struct ar8327_platform_data rb95x_ar8327_data = {
103 .pad0_cfg = &rb95x_ar8327_pad0_cfg,
104 .port0_cfg = {
105 .force_link = 1,
106 .speed = AR8327_PORT_SPEED_1000,
107 .duplex = 1,
108 .txpause = 1,
109 .rxpause = 1,
110 }
111 };
112
113 static struct mdio_board_info rb95x_mdio0_info[] = {
114 {
115 .bus_id = "ag71xx-mdio.0",
116 .phy_addr = 0,
117 .platform_data = &rb95x_ar8327_data,
118 },
119 };
120
121 void __init rb95x_wlan_init(void)
122 {
123 char *art_buf;
124 u8 wlan_mac[ETH_ALEN];
125
126 art_buf = rb_get_wlan_data();
127 if (art_buf == NULL)
128 return;
129
130 ath79_init_mac(wlan_mac, ath79_mac_base, 11);
131 ath79_register_wmac(art_buf + 0x1000, wlan_mac);
132
133 kfree(art_buf);
134 }
135
136 static void rb95x_nand_select_chip(int chip_no)
137 {
138 switch (chip_no) {
139 case 0:
140 gpio_set_value(RB95X_GPIO_NAND_NCE, 0);
141 break;
142 default:
143 gpio_set_value(RB95X_GPIO_NAND_NCE, 1);
144 break;
145 }
146 ndelay(500);
147 }
148
149 static struct nand_ecclayout rb95x_nand_ecclayout = {
150 .eccbytes = 6,
151 .eccpos = { 8, 9, 10, 13, 14, 15 },
152 .oobavail = 9,
153 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
154 };
155
156 static int rb95x_nand_scan_fixup(struct mtd_info *mtd)
157 {
158 struct nand_chip *chip = mtd->priv;
159
160 if (mtd->writesize == 512) {
161 /*
162 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
163 * will not be able to find the kernel that we load.
164 */
165 chip->ecc.layout = &rb95x_nand_ecclayout;
166 }
167
168 return 0;
169 }
170
171 void __init rb95x_nand_init(void)
172 {
173 gpio_request_one(RB95X_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
174
175 ath79_nfc_set_scan_fixup(rb95x_nand_scan_fixup);
176 ath79_nfc_set_parts(rb95x_nand_partitions,
177 ARRAY_SIZE(rb95x_nand_partitions));
178 ath79_nfc_set_select_chip(rb95x_nand_select_chip);
179 ath79_nfc_set_swap_dma(true);
180 ath79_register_nfc();
181 }
182
183 static int __init rb95x_setup(void)
184 {
185 const struct rb_info *info;
186
187 info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x10000);
188 if (!info)
189 return -EINVAL;
190
191 rb95x_nand_init();
192
193 return 0;
194 }
195
196 static void __init rb951g_setup(void)
197 {
198 if (rb95x_setup())
199 return;
200
201 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
202 AR934X_ETH_CFG_SW_ONLY_MODE);
203
204 ath79_register_mdio(0, 0x0);
205
206 mdiobus_register_board_info(rb95x_mdio0_info,
207 ARRAY_SIZE(rb95x_mdio0_info));
208
209 ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
210 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
211 ath79_eth0_data.phy_mask = BIT(0);
212
213 ath79_register_eth(0);
214
215 rb95x_wlan_init();
216 ath79_register_usb();
217 }
218
219 MIPS_MACHINE(ATH79_MACH_RB_951G, "951G", "MikroTik RouterBOARD 951G-2HnD",
220 rb951g_setup);
221
222 static void __init rb951ui_setup(void)
223 {
224 if (rb95x_setup())
225 return;
226
227 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
228
229 ath79_register_mdio(1, 0x0);
230
231 ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
232 ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 1);
233
234 ath79_switch_data.phy4_mii_en = 1;
235 ath79_switch_data.phy_poll_mask = BIT(4);
236 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
237 ath79_eth0_data.phy_mask = BIT(4);
238 ath79_eth0_data.mii_bus_dev = &ath79_mdio1_device.dev;
239 ath79_register_eth(0);
240
241 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
242 ath79_register_eth(1);
243
244 gpio_request_one(20, GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
245 "USB power");
246
247 gpio_request_one(2, GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
248 "POE power");
249
250 rb95x_wlan_init();
251 ath79_register_usb();
252
253 ath79_register_leds_gpio(-1, ARRAY_SIZE(rb951ui_leds_gpio),
254 rb951ui_leds_gpio);
255 }
256
257 MIPS_MACHINE(ATH79_MACH_RB_951U, "951HnD", "MikroTik RouterBOARD 951Ui-2HnD",
258 rb951ui_setup);