d0e6baea49e8b8f855442e1dc019a6e06b674ada
[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/version.h>
16 #include <linux/phy.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/ath9k_platform.h>
20 #include <linux/mtd/mtd.h>
21 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
22 #include <linux/mtd/nand.h>
23 #else
24 #include <linux/mtd/rawnand.h>
25 #endif
26 #include <linux/mtd/partitions.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/flash.h>
29 #include <linux/rle.h>
30 #include <linux/routerboot.h>
31 #include <linux/gpio.h>
32 #include <linux/version.h>
33
34 #include <asm/mach-ath79/ath79.h>
35 #include <asm/mach-ath79/ar71xx_regs.h>
36 #include "common.h"
37 #include "dev-ap9x-pci.h"
38 #include "dev-gpio-buttons.h"
39 #include "dev-leds-gpio.h"
40 #include "dev-eth.h"
41 #include "dev-m25p80.h"
42 #include "dev-nfc.h"
43 #include "dev-wmac.h"
44 #include "dev-usb.h"
45 #include "machtypes.h"
46 #include "routerboot.h"
47 #include <linux/ar8216_platform.h>
48
49 #define SXTLITE_GPIO_NAND_NCE 14
50 #define SXTLITE_GPIO_LED_USER 3
51 #define SXTLITE_GPIO_LED_1 13
52 #define SXTLITE_GPIO_LED_2 12
53 #define SXTLITE_GPIO_LED_3 4
54 #define SXTLITE_GPIO_LED_4 21
55 #define SXTLITE_GPIO_LED_5 18
56 #define SXTLITE_GPIO_LED_POWER 11
57
58 #define SXTLITE_GPIO_BUZZER 19
59
60 #define SXTLITE_GPIO_BTN_RESET 15
61
62 #define SXTLITE_KEYS_POLL_INTERVAL 20
63 #define SXTLITE_KEYS_DEBOUNCE_INTERVAL (3 * SXTLITE_KEYS_POLL_INTERVAL)
64
65 static struct mtd_partition rbsxtlite_nand_partitions[] = {
66 {
67 .name = "booter",
68 .offset = 0,
69 .size = (256 * 1024),
70 .mask_flags = MTD_WRITEABLE,
71 },
72 {
73 .name = "kernel",
74 .offset = (256 * 1024),
75 .size = (4 * 1024 * 1024) - (256 * 1024),
76 },
77 {
78 .name = "ubi",
79 .offset = MTDPART_OFS_NXTBLK,
80 .size = MTDPART_SIZ_FULL,
81 },
82 };
83
84 static struct gpio_led rbsxtlite_leds_gpio[] __initdata = {
85 {
86 .name = "rb:green:user",
87 .gpio = SXTLITE_GPIO_LED_USER,
88 .active_low = 1,
89 },
90 {
91 .name = "rb:green:led1",
92 .gpio = SXTLITE_GPIO_LED_1,
93 .active_low = 1,
94 },
95 {
96 .name = "rb:green:led2",
97 .gpio = SXTLITE_GPIO_LED_2,
98 .active_low = 1,
99 },
100 {
101 .name = "rb:green:led3",
102 .gpio = SXTLITE_GPIO_LED_3,
103 .active_low = 1,
104 },
105 {
106 .name = "rb:green:led4",
107 .gpio = SXTLITE_GPIO_LED_4,
108 .active_low = 1,
109 },
110 {
111 .name = "rb:green:led5",
112 .gpio = SXTLITE_GPIO_LED_5,
113 .active_low = 1,
114 },
115 {
116 .name = "rb:green:power",
117 .gpio = SXTLITE_GPIO_LED_POWER,
118 .default_state = LEDS_GPIO_DEFSTATE_KEEP,
119 },
120 };
121
122 static struct gpio_keys_button rbsxtlite_gpio_keys[] __initdata = {
123 {
124 .desc = "Reset button",
125 .type = EV_KEY,
126 .code = KEY_RESTART,
127 .debounce_interval = SXTLITE_KEYS_DEBOUNCE_INTERVAL,
128 .gpio = SXTLITE_GPIO_BTN_RESET,
129 .active_low = 0,
130 },
131 };
132
133 static int __init rbsxtlite_rbinfo_init(void)
134 {
135 const struct rb_info *info;
136
137 info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x10000);
138 if (!info)
139 return -EINVAL;
140 return 0;
141
142 }
143
144 void __init rbsxtlite_wlan_init(void)
145 {
146 char *art_buf;
147 u8 wlan_mac[ETH_ALEN];
148
149 art_buf = rb_get_wlan_data();
150 if (art_buf == NULL)
151 return;
152
153 ath79_init_mac(wlan_mac, ath79_mac_base, 1);
154 ath79_register_wmac(art_buf + 0x1000, wlan_mac);
155
156 kfree(art_buf);
157 }
158
159 static void rbsxtlite_nand_select_chip(int chip_no)
160 {
161 switch (chip_no) {
162 case 0:
163 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 0);
164 break;
165 default:
166 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 1);
167 break;
168 }
169 ndelay(500);
170 }
171
172 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
173 static struct nand_ecclayout rbsxtlite_nand_ecclayout = {
174 .eccbytes = 6,
175 .eccpos = { 8, 9, 10, 13, 14, 15 },
176 .oobavail = 9,
177 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
178 };
179
180 #else
181
182 static int rbsxtlite_ooblayout_ecc(struct mtd_info *mtd, int section,
183 struct mtd_oob_region *oobregion)
184 {
185 switch (section) {
186 case 0:
187 oobregion->offset = 8;
188 oobregion->length = 3;
189 return 0;
190 case 1:
191 oobregion->offset = 13;
192 oobregion->length = 3;
193 return 0;
194 default:
195 return -ERANGE;
196 }
197 }
198
199 static int rbsxtlite_ooblayout_free(struct mtd_info *mtd, int section,
200 struct mtd_oob_region *oobregion)
201 {
202 switch (section) {
203 case 0:
204 oobregion->offset = 0;
205 oobregion->length = 4;
206 return 0;
207 case 1:
208 oobregion->offset = 4;
209 oobregion->length = 1;
210 return 0;
211 case 2:
212 oobregion->offset = 6;
213 oobregion->length = 2;
214 return 0;
215 case 3:
216 oobregion->offset = 11;
217 oobregion->length = 2;
218 return 0;
219 default:
220 return -ERANGE;
221 }
222 }
223
224 static const struct mtd_ooblayout_ops rbsxtlite_nand_ecclayout_ops = {
225 .ecc = rbsxtlite_ooblayout_ecc,
226 .free = rbsxtlite_ooblayout_free,
227 };
228 #endif /* < 4.6 */
229
230 static int rbsxtlite_nand_scan_fixup(struct mtd_info *mtd)
231 {
232 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
233 struct nand_chip *chip = mtd->priv;
234 #endif
235
236 if (mtd->writesize == 512) {
237 /*
238 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
239 * will not be able to find the kernel that we load.
240 */
241 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
242 chip->ecc.layout = &rbsxtlite_nand_ecclayout;
243 #else
244 mtd_set_ooblayout(mtd, &rbsxtlite_nand_ecclayout_ops);
245 #endif
246 }
247
248 return 0;
249 }
250
251 void __init rbsxtlite_gpio_init(void)
252 {
253 gpio_request_one(SXTLITE_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
254 }
255
256 void __init rbsxtlite_nand_init(void)
257 {
258 ath79_nfc_set_scan_fixup(rbsxtlite_nand_scan_fixup);
259 ath79_nfc_set_parts(rbsxtlite_nand_partitions,
260 ARRAY_SIZE(rbsxtlite_nand_partitions));
261 ath79_nfc_set_select_chip(rbsxtlite_nand_select_chip);
262 ath79_nfc_set_swap_dma(true);
263 ath79_register_nfc();
264 }
265
266
267 static void __init rbsxtlite_setup(void)
268 {
269 if(rbsxtlite_rbinfo_init())
270 return;
271 rbsxtlite_nand_init();
272 rbsxtlite_wlan_init();
273
274 ath79_register_leds_gpio(-1, ARRAY_SIZE(rbsxtlite_leds_gpio),
275 rbsxtlite_leds_gpio);
276 ath79_register_gpio_keys_polled(-1, SXTLITE_KEYS_POLL_INTERVAL,
277 ARRAY_SIZE(rbsxtlite_gpio_keys),
278 rbsxtlite_gpio_keys);
279
280 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
281
282 ath79_register_mdio(1, 0x0);
283
284 /* GMAC0 is left unused */
285
286 /* GMAC1 is connected to MAC0 on the internal switch */
287 /* The ethernet port connects to PHY P0, which connects to MAC1
288 on the internal switch */
289 ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 0);
290 ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
291 ath79_register_eth(1);
292
293
294 }
295
296
297 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE2ND, "sxt2n", "MikroTik RouterBOARD SXT Lite2",
298 rbsxtlite_setup);
299
300 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE5ND, "sxt5n", "MikroTik RouterBOARD SXT Lite5",
301 rbsxtlite_setup);
302