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