08a602f78d859bd8e56c2882f0586d36ad7cab5d
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-qihoo-c301.c
1 /*
2  *  Qihoo 360 C301 board support
3  *
4  *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
5  *  Copyright (C) 2014 Weijie Gao <hackpascal@gmail.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11
12 #include <linux/pci.h>
13 #include <linux/phy.h>
14 #include <linux/gpio.h>
15 #include <linux/platform_device.h>
16 #include <linux/ath9k_platform.h>
17
18 #include <asm/mach-ath79/ar71xx_regs.h>
19
20 #include "common.h"
21 #include "pci.h"
22 #include "dev-eth.h"
23 #include "dev-gpio-buttons.h"
24 #include "dev-leds-gpio.h"
25 #include "dev-m25p80.h"
26 #include "dev-spi.h"
27 #include "dev-usb.h"
28 #include "dev-wmac.h"
29 #include "machtypes.h"
30 #include "nvram.h"
31
32 #define QIHOO_C301_GPIO_LED_STATUS_GREEN        0
33 #define QIHOO_C301_GPIO_LED_STATUS_RED          11
34
35 #define QIHOO_C301_GPIO_LED_WAN                 1
36 #define QIHOO_C301_GPIO_LED_LAN1                2
37 #define QIHOO_C301_GPIO_LED_LAN2                3
38 #define QIHOO_C301_GPIO_ETH_LEN_EN              18
39
40 #define QIHOO_C301_GPIO_BTN_RESET               16
41
42 #define QIHOO_C301_GPIO_USB_POWER               19
43
44 #define QIHOO_C301_GPIO_SPI_CS1                 12
45
46 #define QIHOO_C301_GPIO_EXTERNAL_LNA0           14
47 #define QIHOO_C301_GPIO_EXTERNAL_LNA1           15
48
49 #define QIHOO_C301_KEYS_POLL_INTERVAL           20      /* msecs */
50 #define QIHOO_C301_KEYS_DEBOUNCE_INTERVAL       \
51         (3 * QIHOO_C301_KEYS_POLL_INTERVAL)
52
53 #define QIHOO_C301_WMAC_CALDATA_OFFSET          0x1000
54
55 #define QIHOO_C301_NVRAM_ADDR                   0x1f058010
56 #define QIHOO_C301_NVRAM_SIZE                   0x7ff0
57
58 static struct gpio_led qihoo_c301_leds_gpio[] __initdata = {
59         {
60                 .name           = "qihoo:green:status",
61                 .gpio           = QIHOO_C301_GPIO_LED_STATUS_GREEN,
62                 .active_low     = 1,
63         },
64         {
65                 .name           = "qihoo:red:status",
66                 .gpio           = QIHOO_C301_GPIO_LED_STATUS_RED,
67                 .active_low     = 1,
68         },
69 };
70
71 static struct gpio_keys_button qihoo_c301_gpio_keys[] __initdata = {
72         {
73                 .desc           = "reset",
74                 .type           = EV_KEY,
75                 .code           = KEY_RESTART,
76                 .debounce_interval = QIHOO_C301_KEYS_DEBOUNCE_INTERVAL,
77                 .gpio           = QIHOO_C301_GPIO_BTN_RESET,
78                 .active_low     = 1,
79         },
80 };
81
82 struct flash_platform_data flash __initdata = {NULL, NULL, 0};
83
84 static void qihoo_c301_get_mac(const char *name, char *mac)
85 {
86         u8 *nvram = (u8 *) KSEG1ADDR(QIHOO_C301_NVRAM_ADDR);
87         int err;
88
89         err = ath79_nvram_parse_mac_addr(nvram, QIHOO_C301_NVRAM_SIZE,
90                                          name, mac);
91         if (err)
92                 pr_err("no MAC address found for %s\n", name);
93 }
94
95 static void __init qihoo_c301_setup(void)
96 {
97         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
98         u8 tmpmac[ETH_ALEN];
99
100         ath79_register_m25p80_multi(&flash);
101
102         ath79_gpio_function_enable(AR934X_GPIO_FUNC_JTAG_DISABLE);
103
104         ath79_gpio_output_select(QIHOO_C301_GPIO_LED_WAN,
105                                  AR934X_GPIO_OUT_LED_LINK4);
106         ath79_gpio_output_select(QIHOO_C301_GPIO_LED_LAN1,
107                                  AR934X_GPIO_OUT_LED_LINK1);
108         ath79_gpio_output_select(QIHOO_C301_GPIO_LED_LAN2,
109                                  AR934X_GPIO_OUT_LED_LINK2);
110
111         ath79_gpio_output_select(QIHOO_C301_GPIO_SPI_CS1,
112                                  AR934X_GPIO_OUT_SPI_CS1);
113
114         gpio_request_one(QIHOO_C301_GPIO_ETH_LEN_EN,
115                          GPIOF_OUT_INIT_LOW | GPIOF_EXPORT_DIR_FIXED,
116                          "Ethernet LED enable");
117
118         ath79_register_leds_gpio(-1, ARRAY_SIZE(qihoo_c301_leds_gpio),
119                                  qihoo_c301_leds_gpio);
120
121         ath79_register_gpio_keys_polled(-1, QIHOO_C301_KEYS_POLL_INTERVAL,
122                                         ARRAY_SIZE(qihoo_c301_gpio_keys),
123                                         qihoo_c301_gpio_keys);
124
125         ath79_wmac_set_ext_lna_gpio(0, QIHOO_C301_GPIO_EXTERNAL_LNA0);
126         ath79_wmac_set_ext_lna_gpio(1, QIHOO_C301_GPIO_EXTERNAL_LNA1);
127
128         qihoo_c301_get_mac("wlan24mac=", tmpmac);
129         ath79_register_wmac(art + QIHOO_C301_WMAC_CALDATA_OFFSET, tmpmac);
130
131         ath79_register_pci();
132
133         ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE |
134                                    AR934X_ETH_CFG_SW_PHY_SWAP);
135
136         ath79_register_mdio(1, 0x0);
137
138         /* LAN */
139         qihoo_c301_get_mac("lanmac=", ath79_eth1_data.mac_addr);
140
141         /* GMAC1 is connected to the internal switch */
142         ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
143
144         ath79_register_eth(1);
145
146         /* WAN */
147         qihoo_c301_get_mac("wanmac=", ath79_eth0_data.mac_addr);
148
149         /* GMAC0 is connected to the PHY4 of the internal switch */
150         ath79_switch_data.phy4_mii_en = 1;
151         ath79_switch_data.phy_poll_mask = BIT(0);
152
153         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
154         ath79_eth0_data.phy_mask = BIT(0);
155         ath79_eth0_data.mii_bus_dev = &ath79_mdio1_device.dev;
156
157         ath79_register_eth(0);
158
159         gpio_request_one(QIHOO_C301_GPIO_USB_POWER,
160                          GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
161                          "USB power");
162         ath79_register_usb();
163 }
164
165 MIPS_MACHINE(ATH79_MACH_QIHOO_C301, "QIHOO-C301", "Qihoo 360 C301",
166              qihoo_c301_setup);