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