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