ar71xx: use routerboot_find_tag to find wlan data offset on RB751
[openwrt/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-rb750.c
1 /*
2 * MikroTik RouterBOARD 750/750GL support
3 *
4 * Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 */
10
11 #include <linux/export.h>
12 #include <linux/pci.h>
13 #include <linux/ath9k_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/phy.h>
16 #include <linux/ar8216_platform.h>
17 #include <linux/rle.h>
18 #include <linux/routerboot.h>
19
20 #include <asm/mach-ath79/ar71xx_regs.h>
21 #include <asm/mach-ath79/ath79.h>
22 #include <asm/mach-ath79/irq.h>
23 #include <asm/mach-ath79/mach-rb750.h>
24
25 #include "common.h"
26 #include "dev-ap9x-pci.h"
27 #include "dev-usb.h"
28 #include "dev-eth.h"
29 #include "machtypes.h"
30 #include "routerboot.h"
31
32 static struct rb750_led_data rb750_leds[] = {
33 {
34 .name = "rb750:green:act",
35 .mask = RB750_LED_ACT,
36 .active_low = 1,
37 }, {
38 .name = "rb750:green:port1",
39 .mask = RB750_LED_PORT5,
40 .active_low = 1,
41 }, {
42 .name = "rb750:green:port2",
43 .mask = RB750_LED_PORT4,
44 .active_low = 1,
45 }, {
46 .name = "rb750:green:port3",
47 .mask = RB750_LED_PORT3,
48 .active_low = 1,
49 }, {
50 .name = "rb750:green:port4",
51 .mask = RB750_LED_PORT2,
52 .active_low = 1,
53 }, {
54 .name = "rb750:green:port5",
55 .mask = RB750_LED_PORT1,
56 .active_low = 1,
57 }
58 };
59
60 static struct rb750_led_data rb750gr3_leds[] = {
61 {
62 .name = "rb750:green:act",
63 .mask = RB7XX_LED_ACT,
64 .active_low = 1,
65 },
66 };
67
68 static struct rb750_led_platform_data rb750_leds_data;
69 static struct platform_device rb750_leds_device = {
70 .name = "leds-rb750",
71 .dev = {
72 .platform_data = &rb750_leds_data,
73 }
74 };
75
76 static struct rb7xx_nand_platform_data rb750_nand_data;
77 static struct platform_device rb750_nand_device = {
78 .name = "rb750-nand",
79 .id = -1,
80 .dev = {
81 .platform_data = &rb750_nand_data,
82 }
83 };
84
85 static void rb750_latch_change(u32 mask_clr, u32 mask_set)
86 {
87 static DEFINE_SPINLOCK(lock);
88 static u32 latch_set = RB750_LED_BITS | RB750_LVC573_LE;
89 static u32 latch_oe;
90 static u32 latch_clr;
91 unsigned long flags;
92 u32 t;
93
94 spin_lock_irqsave(&lock, flags);
95
96 if ((mask_clr & BIT(31)) != 0 &&
97 (latch_set & RB750_LVC573_LE) == 0) {
98 goto unlock;
99 }
100
101 latch_set = (latch_set | mask_set) & ~mask_clr;
102 latch_clr = (latch_clr | mask_clr) & ~mask_set;
103
104 if (latch_oe == 0)
105 latch_oe = __raw_readl(ath79_gpio_base + AR71XX_GPIO_REG_OE);
106
107 if (likely(latch_set & RB750_LVC573_LE)) {
108 void __iomem *base = ath79_gpio_base;
109
110 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
111 t |= mask_clr | latch_oe | mask_set;
112
113 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
114 __raw_writel(latch_clr, base + AR71XX_GPIO_REG_CLEAR);
115 __raw_writel(latch_set, base + AR71XX_GPIO_REG_SET);
116 } else if (mask_clr & RB750_LVC573_LE) {
117 void __iomem *base = ath79_gpio_base;
118
119 latch_oe = __raw_readl(base + AR71XX_GPIO_REG_OE);
120 __raw_writel(RB750_LVC573_LE, base + AR71XX_GPIO_REG_CLEAR);
121 /* flush write */
122 __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
123 }
124
125 unlock:
126 spin_unlock_irqrestore(&lock, flags);
127 }
128
129 static void rb750_nand_enable_pins(void)
130 {
131 rb750_latch_change(RB750_LVC573_LE, 0);
132 ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
133 AR724X_GPIO_FUNC_SPI_EN);
134 }
135
136 static void rb750_nand_disable_pins(void)
137 {
138 ath79_gpio_function_setup(AR724X_GPIO_FUNC_SPI_EN,
139 AR724X_GPIO_FUNC_JTAG_DISABLE);
140 rb750_latch_change(0, RB750_LVC573_LE);
141 }
142
143 static void __init rb750_setup(void)
144 {
145 ath79_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
146 AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
147 AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
148 AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
149 AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);
150
151 ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
152 ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 1);
153
154 ath79_register_mdio(0, 0x0);
155
156 /* LAN ports */
157 ath79_register_eth(1);
158
159 /* WAN port */
160 ath79_register_eth(0);
161
162 rb750_leds_data.num_leds = ARRAY_SIZE(rb750_leds);
163 rb750_leds_data.leds = rb750_leds;
164 rb750_leds_data.latch_change = rb750_latch_change;
165 platform_device_register(&rb750_leds_device);
166
167 rb750_nand_data.nce_line = RB750_NAND_NCE;
168 rb750_nand_data.enable_pins = rb750_nand_enable_pins;
169 rb750_nand_data.disable_pins = rb750_nand_disable_pins;
170 rb750_nand_data.latch_change = rb750_latch_change;
171 platform_device_register(&rb750_nand_device);
172 }
173
174 MIPS_MACHINE(ATH79_MACH_RB_750, "750i", "MikroTik RouterBOARD 750",
175 rb750_setup);
176
177 static struct ar8327_pad_cfg rb750gr3_ar8327_pad0_cfg = {
178 .mode = AR8327_PAD_MAC_RGMII,
179 .txclk_delay_en = true,
180 .rxclk_delay_en = true,
181 .txclk_delay_sel = AR8327_CLK_DELAY_SEL1,
182 .rxclk_delay_sel = AR8327_CLK_DELAY_SEL2,
183 };
184
185 static struct ar8327_platform_data rb750gr3_ar8327_data = {
186 .pad0_cfg = &rb750gr3_ar8327_pad0_cfg,
187 .cpuport_cfg = {
188 .force_link = 1,
189 .speed = AR8327_PORT_SPEED_100,
190 .duplex = 1,
191 .txpause = 1,
192 .rxpause = 1,
193 }
194 };
195
196 static struct mdio_board_info rb750g3_mdio_info[] = {
197 {
198 .bus_id = "ag71xx-mdio.0",
199 .phy_addr = 0,
200 .platform_data = &rb750gr3_ar8327_data,
201 },
202 };
203
204 static void rb750gr3_nand_enable_pins(void)
205 {
206 ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
207 AR724X_GPIO_FUNC_SPI_EN |
208 AR724X_GPIO_FUNC_SPI_CS_EN2);
209 }
210
211 static void rb750gr3_nand_disable_pins(void)
212 {
213 ath79_gpio_function_setup(AR724X_GPIO_FUNC_SPI_EN |
214 AR724X_GPIO_FUNC_SPI_CS_EN2,
215 AR724X_GPIO_FUNC_JTAG_DISABLE);
216 }
217
218 static void rb750gr3_latch_change(u32 mask_clr, u32 mask_set)
219 {
220 static DEFINE_SPINLOCK(lock);
221 static u32 latch_set = RB7XX_LED_ACT;
222 static u32 latch_clr;
223 void __iomem *base = ath79_gpio_base;
224 unsigned long flags;
225 u32 t;
226
227 spin_lock_irqsave(&lock, flags);
228
229 latch_set = (latch_set | mask_set) & ~mask_clr;
230 latch_clr = (latch_clr | mask_clr) & ~mask_set;
231
232 mask_set = latch_set & (RB7XX_USB_POWERON | RB7XX_MONITOR);
233 mask_clr = latch_clr & (RB7XX_USB_POWERON | RB7XX_MONITOR);
234
235 if ((latch_set ^ RB7XX_LED_ACT) & RB7XX_LED_ACT) {
236 /* enable output mode */
237 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
238 t |= RB7XX_LED_ACT;
239 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
240
241 mask_clr |= RB7XX_LED_ACT;
242 } else {
243 /* disable output mode */
244 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
245 t &= ~RB7XX_LED_ACT;
246 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
247 }
248
249 __raw_writel(mask_set, base + AR71XX_GPIO_REG_SET);
250 __raw_writel(mask_clr, base + AR71XX_GPIO_REG_CLEAR);
251
252 spin_unlock_irqrestore(&lock, flags);
253 }
254
255 static void __init rb750gr3_setup(void)
256 {
257 ath79_register_mdio(0, 0x0);
258 mdiobus_register_board_info(rb750g3_mdio_info,
259 ARRAY_SIZE(rb750g3_mdio_info));
260
261 ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
262 ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
263 ath79_eth0_data.phy_mask = BIT(0);
264
265 ath79_register_eth(0);
266
267 rb750_leds_data.num_leds = ARRAY_SIZE(rb750gr3_leds);
268 rb750_leds_data.leds = rb750gr3_leds;
269 rb750_leds_data.latch_change = rb750gr3_latch_change;
270 platform_device_register(&rb750_leds_device);
271
272 rb750_nand_data.nce_line = RB7XX_NAND_NCE;
273 rb750_nand_data.enable_pins = rb750gr3_nand_enable_pins;
274 rb750_nand_data.disable_pins = rb750gr3_nand_disable_pins;
275 rb750_nand_data.latch_change = rb750gr3_latch_change;
276 platform_device_register(&rb750_nand_device);
277 }
278
279 MIPS_MACHINE(ATH79_MACH_RB_750G_R3, "750Gr3", "MikroTik RouterBOARD 750GL",
280 rb750gr3_setup);
281
282 #define RB751_HARDCONFIG 0x1f00b000
283 #define RB751_HARDCONFIG_SIZE 0x1000
284 #define RB751_MAC_ADDRESS_OFFSET 0xE80
285
286 static void __init rb751_wlan_setup(void)
287 {
288 u8 *hardconfig = (u8 *) KSEG1ADDR(RB751_HARDCONFIG);
289 struct ath9k_platform_data *wmac_data;
290 u16 tag_len;
291 u8 *tag;
292 int err;
293
294 wmac_data = ap9x_pci_get_wmac_data(0);
295 if (!wmac_data) {
296 pr_err("rb75x: unable to get address of wlan data\n");
297 return;
298 }
299
300 ap9x_pci_setup_wmac_led_pin(0, 9);
301
302 err = routerboot_find_tag(hardconfig, RB751_HARDCONFIG_SIZE,
303 RB_ID_WLAN_DATA, &tag, &tag_len);
304 if (err) {
305 pr_err("rb75x: no calibration data found\n");
306 return;
307 }
308
309 err = rle_decode(tag, tag_len, (unsigned char *) wmac_data->eeprom_data,
310 sizeof(wmac_data->eeprom_data), NULL, NULL);
311 if (err) {
312 pr_err("rb75x: unable to decode wlan eeprom data\n");
313 return;
314 }
315
316 ap91_pci_init(NULL, hardconfig + RB751_MAC_ADDRESS_OFFSET);
317 }
318
319 static void __init rb751_setup(void)
320 {
321 rb750_setup();
322 ath79_register_usb();
323 rb751_wlan_setup();
324 }
325
326 MIPS_MACHINE(ATH79_MACH_RB_751, "751", "MikroTik RouterBOARD 751",
327 rb751_setup);
328
329 static void __init rb751g_setup(void)
330 {
331 rb750gr3_setup();
332 ath79_register_usb();
333 rb751_wlan_setup();
334 }
335
336 MIPS_MACHINE(ATH79_MACH_RB_751G, "751g", "MikroTik RouterBOARD 751G",
337 rb751g_setup);