ar71xx: mach-rbspi: simplify reset button registration
[oweals/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         /* USB */
174         ath79_register_usb();
175 }
176
177 MIPS_MACHINE(ATH79_MACH_RB_750, "750i", "MikroTik RouterBOARD 750",
178              rb750_setup);
179
180 static struct ar8327_pad_cfg rb750gr3_ar8327_pad0_cfg = {
181         .mode = AR8327_PAD_MAC_RGMII,
182         .txclk_delay_en = true,
183         .rxclk_delay_en = true,
184         .txclk_delay_sel = AR8327_CLK_DELAY_SEL1,
185         .rxclk_delay_sel = AR8327_CLK_DELAY_SEL2,
186 };
187
188 static struct ar8327_platform_data rb750gr3_ar8327_data = {
189         .pad0_cfg = &rb750gr3_ar8327_pad0_cfg,
190         .port0_cfg = {
191                 .force_link = 1,
192                 .speed = AR8327_PORT_SPEED_1000,
193                 .duplex = 1,
194                 .txpause = 1,
195                 .rxpause = 1,
196         }
197 };
198
199 static struct mdio_board_info rb750g3_mdio_info[] = {
200         {
201                 .bus_id = "ag71xx-mdio.0",
202                 .phy_addr = 0,
203                 .platform_data = &rb750gr3_ar8327_data,
204         },
205 };
206
207 static void rb750gr3_nand_enable_pins(void)
208 {
209         ath79_gpio_function_setup(AR724X_GPIO_FUNC_JTAG_DISABLE,
210                                   AR724X_GPIO_FUNC_SPI_EN |
211                                   AR724X_GPIO_FUNC_SPI_CS_EN2);
212 }
213
214 static void rb750gr3_nand_disable_pins(void)
215 {
216         ath79_gpio_function_setup(AR724X_GPIO_FUNC_SPI_EN |
217                                   AR724X_GPIO_FUNC_SPI_CS_EN2,
218                                   AR724X_GPIO_FUNC_JTAG_DISABLE);
219 }
220
221 static void rb750gr3_latch_change(u32 mask_clr, u32 mask_set)
222 {
223         static DEFINE_SPINLOCK(lock);
224         static u32 latch_set = RB7XX_LED_ACT;
225         static u32 latch_clr;
226         void __iomem *base = ath79_gpio_base;
227         unsigned long flags;
228         u32 t;
229
230         spin_lock_irqsave(&lock, flags);
231
232         latch_set = (latch_set | mask_set) & ~mask_clr;
233         latch_clr = (latch_clr | mask_clr) & ~mask_set;
234
235         mask_set = latch_set & (RB7XX_USB_POWERON | RB7XX_MONITOR);
236         mask_clr = latch_clr & (RB7XX_USB_POWERON | RB7XX_MONITOR);
237
238         if ((latch_set ^ RB7XX_LED_ACT) & RB7XX_LED_ACT) {
239                 /* enable output mode */
240                 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
241                 t |= RB7XX_LED_ACT;
242                 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
243
244                 mask_clr |= RB7XX_LED_ACT;
245         } else {
246                 /* disable output mode */
247                 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
248                 t &= ~RB7XX_LED_ACT;
249                 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
250         }
251
252         __raw_writel(mask_set, base + AR71XX_GPIO_REG_SET);
253         __raw_writel(mask_clr, base + AR71XX_GPIO_REG_CLEAR);
254
255         spin_unlock_irqrestore(&lock, flags);
256 }
257
258 static void __init rb750gr3_setup(void)
259 {
260         ath79_register_mdio(0, 0x0);
261         mdiobus_register_board_info(rb750g3_mdio_info,
262                                     ARRAY_SIZE(rb750g3_mdio_info));
263
264         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
265         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
266         ath79_eth0_data.phy_mask = BIT(0);
267         ath79_eth0_pll_data.pll_1000 = 0x62000000;
268
269         ath79_register_eth(0);
270
271         rb750_leds_data.num_leds = ARRAY_SIZE(rb750gr3_leds);
272         rb750_leds_data.leds = rb750gr3_leds;
273         rb750_leds_data.latch_change = rb750gr3_latch_change;
274         platform_device_register(&rb750_leds_device);
275
276         rb750_nand_data.nce_line = RB7XX_NAND_NCE;
277         rb750_nand_data.enable_pins = rb750gr3_nand_enable_pins;
278         rb750_nand_data.disable_pins = rb750gr3_nand_disable_pins;
279         rb750_nand_data.latch_change = rb750gr3_latch_change;
280         platform_device_register(&rb750_nand_device);
281 }
282
283 MIPS_MACHINE(ATH79_MACH_RB_750G_R3, "750Gr3", "MikroTik RouterBOARD 750GL",
284              rb750gr3_setup);
285
286 #define RB751_HARDCONFIG        0x1f00b000
287 #define RB751_HARDCONFIG_SIZE   0x1000
288
289 static void __init rb751_wlan_setup(void)
290 {
291         u8 *hardconfig = (u8 *) KSEG1ADDR(RB751_HARDCONFIG);
292         struct ath9k_platform_data *wmac_data;
293         u16 tag_len;
294         u8 *tag;
295         u16 mac_len;
296         u8 *mac;
297         int err;
298
299         wmac_data = ap9x_pci_get_wmac_data(0);
300         if (!wmac_data) {
301                 pr_err("rb75x: unable to get address of wlan data\n");
302                 return;
303         }
304
305         ap9x_pci_setup_wmac_led_pin(0, 9);
306
307         err = routerboot_find_tag(hardconfig, RB751_HARDCONFIG_SIZE,
308                                   RB_ID_WLAN_DATA, &tag, &tag_len);
309         if (err) {
310                 pr_err("rb75x: no calibration data found\n");
311                 return;
312         }
313
314         err = rle_decode(tag, tag_len, (unsigned char *) wmac_data->eeprom_data,
315                          sizeof(wmac_data->eeprom_data), NULL, NULL);
316         if (err) {
317                 pr_err("rb75x: unable to decode wlan eeprom data\n");
318                 return;
319         }
320
321         err = routerboot_find_tag(hardconfig, RB751_HARDCONFIG_SIZE,
322                                  RB_ID_MAC_ADDRESS_PACK, &mac, &mac_len);
323         if (err) {
324                 pr_err("rb75x: no mac address found\n");
325                 return;
326         }
327
328         ap91_pci_init(NULL, mac);
329 }
330
331 static void __init rb751_setup(void)
332 {
333         rb750_setup();
334         ath79_register_usb();
335         rb751_wlan_setup();
336 }
337
338 MIPS_MACHINE(ATH79_MACH_RB_751, "751", "MikroTik RouterBOARD 751",
339              rb751_setup);
340
341 static void __init rb751g_setup(void)
342 {
343         rb750gr3_setup();
344         ath79_register_usb();
345         rb751_wlan_setup();
346 }
347
348 MIPS_MACHINE(ATH79_MACH_RB_751G, "751g", "MikroTik RouterBOARD 751G",
349              rb751g_setup);