ar71xx: add initial support for RB750GL
[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/platform_device.h>
13 #include <linux/phy.h>
14 #include <linux/ar8216_platform.h>
15
16 #include <asm/mach-ath79/ar71xx_regs.h>
17 #include <asm/mach-ath79/ath79.h>
18 #include <asm/mach-ath79/mach-rb750.h>
19
20 #include "common.h"
21 #include "dev-eth.h"
22 #include "machtypes.h"
23
24 static struct rb750_led_data rb750_leds[] = {
25         {
26                 .name           = "rb750:green:act",
27                 .mask           = RB750_LED_ACT,
28                 .active_low     = 1,
29         }, {
30                 .name           = "rb750:green:port1",
31                 .mask           = RB750_LED_PORT5,
32                 .active_low     = 1,
33         }, {
34                 .name           = "rb750:green:port2",
35                 .mask           = RB750_LED_PORT4,
36                 .active_low     = 1,
37         }, {
38                 .name           = "rb750:green:port3",
39                 .mask           = RB750_LED_PORT3,
40                 .active_low     = 1,
41         }, {
42                 .name           = "rb750:green:port4",
43                 .mask           = RB750_LED_PORT2,
44                 .active_low     = 1,
45         }, {
46                 .name           = "rb750:green:port5",
47                 .mask           = RB750_LED_PORT1,
48                 .active_low     = 1,
49         }
50 };
51
52 static struct rb750_led_data rb750gr3_leds[] = {
53         {
54                 .name           = "rb750:green:act",
55                 .mask           = RB7XX_LED_ACT,
56                 .active_low     = 1,
57         },
58 };
59
60 static struct rb750_led_platform_data rb750_leds_data;
61 static struct platform_device rb750_leds_device = {
62         .name   = "leds-rb750",
63         .dev    = {
64                 .platform_data = &rb750_leds_data,
65         }
66 };
67
68 static struct rb7xx_nand_platform_data rb750_nand_data;
69 static struct platform_device rb750_nand_device = {
70         .name   = "rb750-nand",
71         .id     = -1,
72         .dev    = {
73                 .platform_data = &rb750_nand_data,
74         }
75 };
76
77 static void rb750_latch_change(u32 mask_clr, u32 mask_set)
78 {
79         static DEFINE_SPINLOCK(lock);
80         static u32 latch_set = RB750_LED_BITS | RB750_LVC573_LE;
81         static u32 latch_oe;
82         static u32 latch_clr;
83         unsigned long flags;
84         u32 t;
85
86         spin_lock_irqsave(&lock, flags);
87
88         if ((mask_clr & BIT(31)) != 0 &&
89             (latch_set & RB750_LVC573_LE) == 0) {
90                 goto unlock;
91         }
92
93         latch_set = (latch_set | mask_set) & ~mask_clr;
94         latch_clr = (latch_clr | mask_clr) & ~mask_set;
95
96         if (latch_oe == 0)
97                 latch_oe = __raw_readl(ath79_gpio_base + AR71XX_GPIO_REG_OE);
98
99         if (likely(latch_set & RB750_LVC573_LE)) {
100                 void __iomem *base = ath79_gpio_base;
101
102                 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
103                 t |= mask_clr | latch_oe | mask_set;
104
105                 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
106                 __raw_writel(latch_clr, base + AR71XX_GPIO_REG_CLEAR);
107                 __raw_writel(latch_set, base + AR71XX_GPIO_REG_SET);
108         } else if (mask_clr & RB750_LVC573_LE) {
109                 void __iomem *base = ath79_gpio_base;
110
111                 latch_oe = __raw_readl(base + AR71XX_GPIO_REG_OE);
112                 __raw_writel(RB750_LVC573_LE, base + AR71XX_GPIO_REG_CLEAR);
113                 /* flush write */
114                 __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
115         }
116
117 unlock:
118         spin_unlock_irqrestore(&lock, flags);
119 }
120
121 static void rb750_nand_enable_pins(void)
122 {
123         rb750_latch_change(RB750_LVC573_LE, 0);
124         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
125                                   AR724X_GPIO_FUNC_SPI_EN);
126 }
127
128 static void rb750_nand_disable_pins(void)
129 {
130         ath79_gpio_function_setup(AR724X_GPIO_FUNC_SPI_EN,
131                                   AR724X_GPIO_FUNC_JTAG_DISABLE);
132         rb750_latch_change(0, RB750_LVC573_LE);
133 }
134
135 static void __init rb750_setup(void)
136 {
137         ath79_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
138                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
139                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
140                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
141                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);
142
143         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
144         ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 1);
145
146         ath79_register_mdio(0, 0x0);
147
148         /* LAN ports */
149         ath79_register_eth(1);
150
151         /* WAN port */
152         ath79_register_eth(0);
153
154         rb750_leds_data.num_leds = ARRAY_SIZE(rb750_leds);
155         rb750_leds_data.leds = rb750_leds;
156         rb750_leds_data.latch_change = rb750_latch_change;
157         platform_device_register(&rb750_leds_device);
158
159         rb750_nand_data.nce_line = RB750_NAND_NCE;
160         rb750_nand_data.enable_pins = rb750_nand_enable_pins;
161         rb750_nand_data.disable_pins = rb750_nand_disable_pins;
162         rb750_nand_data.latch_change = rb750_latch_change;
163         platform_device_register(&rb750_nand_device);
164 }
165
166 MIPS_MACHINE(ATH79_MACH_RB_750, "750i", "MikroTik RouterBOARD 750",
167              rb750_setup);
168
169 static struct ar8327_pad_cfg rb750gr3_ar8327_pad0_cfg = {
170         .mode = AR8327_PAD_MAC_RGMII,
171         .txclk_delay_en = true,
172         .rxclk_delay_en = true,
173         .txclk_delay_sel = AR8327_CLK_DELAY_SEL1,
174         .rxclk_delay_sel = AR8327_CLK_DELAY_SEL2,
175 };
176
177 static struct ar8327_platform_data rb750gr3_ar8327_data = {
178         .pad0_cfg = &rb750gr3_ar8327_pad0_cfg,
179         .cpuport_cfg = {
180                 .force_link = 1,
181                 .speed = AR8327_PORT_SPEED_100,
182                 .duplex = 1,
183                 .txpause = 1,
184                 .rxpause = 1,
185         }
186 };
187
188 static struct mdio_board_info rb750g3_mdio_info[] = {
189         {
190                 .bus_id = "ag71xx-mdio.0",
191                 .phy_addr = 0,
192                 .platform_data = &rb750gr3_ar8327_data,
193         },
194 };
195
196 static void rb750gr3_nand_enable_pins(void)
197 {
198         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
199                                   AR724X_GPIO_FUNC_SPI_EN |
200                                   AR724X_GPIO_FUNC_SPI_CS_EN2);
201 }
202
203 static void rb750gr3_nand_disable_pins(void)
204 {
205         ath79_gpio_function_setup(AR724X_GPIO_FUNC_SPI_EN |
206                                   AR724X_GPIO_FUNC_SPI_CS_EN2,
207                                   AR724X_GPIO_FUNC_JTAG_DISABLE);
208 }
209
210 static void rb750gr3_latch_change(u32 mask_clr, u32 mask_set)
211 {
212         static DEFINE_SPINLOCK(lock);
213         static u32 latch_set = RB7XX_LED_ACT;
214         static u32 latch_clr;
215         void __iomem *base = ath79_gpio_base;
216         unsigned long flags;
217         u32 t;
218
219         spin_lock_irqsave(&lock, flags);
220
221         latch_set = (latch_set | mask_set) & ~mask_clr;
222         latch_clr = (latch_clr | mask_clr) & ~mask_set;
223
224         mask_set = latch_set & (RB7XX_USB_POWERON | RB7XX_MONITOR);
225         mask_clr = latch_clr & (RB7XX_USB_POWERON | RB7XX_MONITOR);
226
227         if ((latch_set ^ RB7XX_LED_ACT) & RB7XX_LED_ACT) {
228                 /* enable output mode */
229                 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
230                 t |= RB7XX_LED_ACT;
231                 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
232
233                 mask_clr |= RB7XX_LED_ACT;
234         } else {
235                 /* disable 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
241         __raw_writel(mask_set, base + AR71XX_GPIO_REG_SET);
242         __raw_writel(mask_clr, base + AR71XX_GPIO_REG_CLEAR);
243
244         spin_unlock_irqrestore(&lock, flags);
245 }
246
247 static void __init rb750gr3_setup(void)
248 {
249         ath79_register_mdio(0, 0x0);
250         mdiobus_register_board_info(rb750g3_mdio_info,
251                                     ARRAY_SIZE(rb750g3_mdio_info));
252
253         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
254         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
255         ath79_eth0_data.phy_mask = BIT(0);
256
257         ath79_register_eth(0);
258
259         rb750_leds_data.num_leds = ARRAY_SIZE(rb750gr3_leds);
260         rb750_leds_data.leds = rb750gr3_leds;
261         rb750_leds_data.latch_change = rb750gr3_latch_change;
262         platform_device_register(&rb750_leds_device);
263
264         rb750_nand_data.nce_line = RB7XX_NAND_NCE;
265         rb750_nand_data.enable_pins = rb750gr3_nand_enable_pins;
266         rb750_nand_data.disable_pins = rb750gr3_nand_disable_pins;
267         rb750_nand_data.latch_change = rb750gr3_latch_change;
268         platform_device_register(&rb750_nand_device);
269 }
270
271 MIPS_MACHINE(ATH79_MACH_RB_750G_R3, "750Gr3", "MikroTik RouterBOARD 750GL",
272              rb750gr3_setup);