129195dde67c9a996cac385a69ac5199164e4941
[openwrt/openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-rbsxtlite.c
1 /*
2 * MikroTik RouterBOARD SXT Lite support
3 *
4 * Copyright (C) 2012 Stijn Tintel <stijn@linux-ipv6.be>
5 * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2013 Vyacheslav Adamanov <adamanov@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 */
12
13 #define pr_fmt(fmt) "sxtlite: " fmt
14
15 #include <linux/phy.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/ath9k_platform.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/spi/spi.h>
23 #include <linux/spi/flash.h>
24 #include <linux/rle.h>
25 #include <linux/routerboot.h>
26 #include <linux/gpio.h>
27
28 #include <asm/mach-ath79/ath79.h>
29 #include <asm/mach-ath79/ar71xx_regs.h>
30 #include "common.h"
31 #include "dev-ap9x-pci.h"
32 #include "dev-gpio-buttons.h"
33 #include "dev-leds-gpio.h"
34 #include "dev-eth.h"
35 #include "dev-m25p80.h"
36 #include "dev-nfc.h"
37 #include "dev-wmac.h"
38 #include "dev-usb.h"
39 #include "machtypes.h"
40 #include "routerboot.h"
41 #include <linux/ar8216_platform.h>
42
43 #define SXTLITE_GPIO_NAND_NCE 14
44 #define SXTLITE_GPIO_LED_USER 3
45 #define SXTLITE_GPIO_LED_1 13
46 #define SXTLITE_GPIO_LED_2 12
47 #define SXTLITE_GPIO_LED_3 4
48 #define SXTLITE_GPIO_LED_4 21
49 #define SXTLITE_GPIO_LED_5 18
50 #define SXTLITE_GPIO_LED_POWER 11
51
52 #define SXTLITE_GPIO_BUZZER 19
53
54 #define SXTLITE_GPIO_BTN_RESET 15
55
56 #define SXTLITE_KEYS_POLL_INTERVAL 20
57 #define SXTLITE_KEYS_DEBOUNCE_INTERVAL (3 * SXTLITE_KEYS_POLL_INTERVAL)
58
59 static struct mtd_partition rbsxtlite_nand_partitions[] = {
60 {
61 .name = "booter",
62 .offset = 0,
63 .size = (256 * 1024),
64 .mask_flags = MTD_WRITEABLE,
65 },
66 {
67 .name = "kernel",
68 .offset = (256 * 1024),
69 .size = (4 * 1024 * 1024) - (256 * 1024),
70 },
71 {
72 .name = "ubi",
73 .offset = MTDPART_OFS_NXTBLK,
74 .size = MTDPART_SIZ_FULL,
75 },
76 };
77
78 static struct gpio_led rbsxtlite_leds_gpio[] __initdata = {
79 {
80 .name = "rb:green:user",
81 .gpio = SXTLITE_GPIO_LED_USER,
82 .active_low = 1,
83 },
84 {
85 .name = "rb:green:led1",
86 .gpio = SXTLITE_GPIO_LED_1,
87 .active_low = 1,
88 },
89 {
90 .name = "rb:green:led2",
91 .gpio = SXTLITE_GPIO_LED_2,
92 .active_low = 1,
93 },
94 {
95 .name = "rb:green:led3",
96 .gpio = SXTLITE_GPIO_LED_3,
97 .active_low = 1,
98 },
99 {
100 .name = "rb:green:led4",
101 .gpio = SXTLITE_GPIO_LED_4,
102 .active_low = 1,
103 },
104 {
105 .name = "rb:green:led5",
106 .gpio = SXTLITE_GPIO_LED_5,
107 .active_low = 1,
108 },
109 {
110 .name = "rb:green:power",
111 .gpio = SXTLITE_GPIO_LED_POWER,
112 },
113 };
114
115 static struct gpio_keys_button rbsxtlite_gpio_keys[] __initdata = {
116 {
117 .desc = "Reset button",
118 .type = EV_KEY,
119 .code = KEY_RESTART,
120 .debounce_interval = SXTLITE_KEYS_DEBOUNCE_INTERVAL,
121 .gpio = SXTLITE_GPIO_BTN_RESET,
122 .active_low = 0,
123 },
124 };
125
126 static int __init rbsxtlite_rbinfo_init(void)
127 {
128 const struct rb_info *info;
129
130 info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x10000);
131 if (!info)
132 return -EINVAL;
133 return 0;
134
135 }
136
137 void __init rbsxtlite_wlan_init(void)
138 {
139 char *art_buf;
140 u8 wlan_mac[ETH_ALEN];
141
142 art_buf = rb_get_wlan_data();
143 if (art_buf == NULL)
144 return;
145
146 ath79_init_mac(wlan_mac, ath79_mac_base, 1);
147 ath79_register_wmac(art_buf + 0x1000, wlan_mac);
148
149 kfree(art_buf);
150 }
151
152 static void rbsxtlite_nand_select_chip(int chip_no)
153 {
154 switch (chip_no) {
155 case 0:
156 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 0);
157 break;
158 default:
159 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 1);
160 break;
161 }
162 ndelay(500);
163 }
164
165 static struct nand_ecclayout rbsxtlite_nand_ecclayout = {
166 .eccbytes = 6,
167 .eccpos = { 8, 9, 10, 13, 14, 15 },
168 .oobavail = 9,
169 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
170 };
171
172 static int rbsxtlite_nand_scan_fixup(struct mtd_info *mtd)
173 {
174 struct nand_chip *chip = mtd->priv;
175
176 if (mtd->writesize == 512) {
177 /*
178 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
179 * will not be able to find the kernel that we load.
180 */
181 chip->ecc.layout = &rbsxtlite_nand_ecclayout;
182 }
183
184 return 0;
185 }
186
187 void __init rbsxtlite_gpio_init(void)
188 {
189 gpio_request_one(SXTLITE_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
190 }
191
192 void __init rbsxtlite_nand_init(void)
193 {
194 ath79_nfc_set_scan_fixup(rbsxtlite_nand_scan_fixup);
195 ath79_nfc_set_parts(rbsxtlite_nand_partitions,
196 ARRAY_SIZE(rbsxtlite_nand_partitions));
197 ath79_nfc_set_select_chip(rbsxtlite_nand_select_chip);
198 ath79_nfc_set_swap_dma(true);
199 ath79_register_nfc();
200 }
201
202
203 static void __init rbsxtlite_setup(void)
204 {
205 if(rbsxtlite_rbinfo_init())
206 return;
207 rbsxtlite_nand_init();
208 rbsxtlite_wlan_init();
209
210 ath79_register_leds_gpio(-1, ARRAY_SIZE(rbsxtlite_leds_gpio),
211 rbsxtlite_leds_gpio);
212 ath79_register_gpio_keys_polled(-1, SXTLITE_KEYS_POLL_INTERVAL,
213 ARRAY_SIZE(rbsxtlite_gpio_keys),
214 rbsxtlite_gpio_keys);
215
216 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
217
218 ath79_register_mdio(1, 0x0);
219
220 /* GMAC0 is left unused */
221
222 /* GMAC1 is connected to MAC0 on the internal switch */
223 /* The ethernet port connects to PHY P0, which connects to MAC1
224 on the internal switch */
225 ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 0);
226 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
227 ath79_register_eth(1);
228
229
230 }
231
232
233 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE2ND, "sxt2n", "Mikrotik RouterBOARD SXT Lite2",
234 rbsxtlite_setup);
235
236 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE5ND, "sxt5n", "Mikrotik RouterBOARD SXT Lite5",
237 rbsxtlite_setup);
238