2 * MikroTik RouterBOARD SXT Lite support
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>
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.
13 #define pr_fmt(fmt) "sxtlite: " fmt
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>
24 #include <linux/mtd/rawnand.h>
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>
34 #include <asm/mach-ath79/ath79.h>
35 #include <asm/mach-ath79/ar71xx_regs.h>
37 #include "dev-ap9x-pci.h"
38 #include "dev-gpio-buttons.h"
39 #include "dev-leds-gpio.h"
41 #include "dev-m25p80.h"
45 #include "machtypes.h"
46 #include "routerboot.h"
47 #include <linux/ar8216_platform.h>
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
58 #define SXTLITE_GPIO_BUZZER 19
60 #define SXTLITE_GPIO_BTN_RESET 15
62 #define SXTLITE_KEYS_POLL_INTERVAL 20
63 #define SXTLITE_KEYS_DEBOUNCE_INTERVAL (3 * SXTLITE_KEYS_POLL_INTERVAL)
65 static struct mtd_partition rbsxtlite_nand_partitions[] = {
70 .mask_flags = MTD_WRITEABLE,
74 .offset = (256 * 1024),
75 .size = (4 * 1024 * 1024) - (256 * 1024),
79 .offset = MTDPART_OFS_NXTBLK,
80 .size = MTDPART_SIZ_FULL,
84 static struct gpio_led rbsxtlite_leds_gpio[] __initdata = {
86 .name = "rb:green:user",
87 .gpio = SXTLITE_GPIO_LED_USER,
91 .name = "rb:green:led1",
92 .gpio = SXTLITE_GPIO_LED_1,
96 .name = "rb:green:led2",
97 .gpio = SXTLITE_GPIO_LED_2,
101 .name = "rb:green:led3",
102 .gpio = SXTLITE_GPIO_LED_3,
106 .name = "rb:green:led4",
107 .gpio = SXTLITE_GPIO_LED_4,
111 .name = "rb:green:led5",
112 .gpio = SXTLITE_GPIO_LED_5,
116 .name = "rb:green:power",
117 .gpio = SXTLITE_GPIO_LED_POWER,
118 .default_state = LEDS_GPIO_DEFSTATE_ON,
122 static struct gpio_keys_button rbsxtlite_gpio_keys[] __initdata = {
124 .desc = "Reset button",
127 .debounce_interval = SXTLITE_KEYS_DEBOUNCE_INTERVAL,
128 .gpio = SXTLITE_GPIO_BTN_RESET,
133 static int __init rbsxtlite_rbinfo_init(void)
135 const struct rb_info *info;
137 info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x10000);
144 void __init rbsxtlite_wlan_init(void)
147 u8 wlan_mac[ETH_ALEN];
149 art_buf = rb_get_wlan_data();
153 ath79_init_mac(wlan_mac, ath79_mac_base, 1);
154 ath79_register_wmac(art_buf + 0x1000, wlan_mac);
159 static void rbsxtlite_nand_select_chip(int chip_no)
163 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 0);
166 gpio_set_value(SXTLITE_GPIO_NAND_NCE, 1);
172 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
173 static struct nand_ecclayout rbsxtlite_nand_ecclayout = {
175 .eccpos = { 8, 9, 10, 13, 14, 15 },
177 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
182 static int rbsxtlite_ooblayout_ecc(struct mtd_info *mtd, int section,
183 struct mtd_oob_region *oobregion)
187 oobregion->offset = 8;
188 oobregion->length = 3;
191 oobregion->offset = 13;
192 oobregion->length = 3;
199 static int rbsxtlite_ooblayout_free(struct mtd_info *mtd, int section,
200 struct mtd_oob_region *oobregion)
204 oobregion->offset = 0;
205 oobregion->length = 4;
208 oobregion->offset = 4;
209 oobregion->length = 1;
212 oobregion->offset = 6;
213 oobregion->length = 2;
216 oobregion->offset = 11;
217 oobregion->length = 2;
224 static const struct mtd_ooblayout_ops rbsxtlite_nand_ecclayout_ops = {
225 .ecc = rbsxtlite_ooblayout_ecc,
226 .free = rbsxtlite_ooblayout_free,
230 static int rbsxtlite_nand_scan_fixup(struct mtd_info *mtd)
232 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
233 struct nand_chip *chip = mtd->priv;
236 if (mtd->writesize == 512) {
238 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
239 * will not be able to find the kernel that we load.
241 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
242 chip->ecc.layout = &rbsxtlite_nand_ecclayout;
244 mtd_set_ooblayout(mtd, &rbsxtlite_nand_ecclayout_ops);
251 void __init rbsxtlite_gpio_init(void)
253 gpio_request_one(SXTLITE_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
256 void __init rbsxtlite_nand_init(void)
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();
267 static void __init rbsxtlite_setup(void)
269 if(rbsxtlite_rbinfo_init())
271 rbsxtlite_nand_init();
272 rbsxtlite_wlan_init();
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);
280 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
282 ath79_register_mdio(1, 0x0);
284 /* GMAC0 is left unused */
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);
297 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE2ND, "sxt2n", "MikroTik RouterBOARD SXT Lite2",
300 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE5ND, "sxt5n", "MikroTik RouterBOARD SXT Lite5",