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/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>
23 #include <linux/mtd/rawnand.h>
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>
33 #include <asm/mach-ath79/ath79.h>
34 #include <asm/mach-ath79/ar71xx_regs.h>
36 #include "dev-ap9x-pci.h"
37 #include "dev-gpio-buttons.h"
38 #include "dev-leds-gpio.h"
40 #include "dev-m25p80.h"
44 #include "machtypes.h"
45 #include "routerboot.h"
46 #include <linux/ar8216_platform.h>
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
57 #define SXTLITE_GPIO_BUZZER 19
59 #define SXTLITE_GPIO_BTN_RESET 15
61 #define SXTLITE_KEYS_POLL_INTERVAL 20
62 #define SXTLITE_KEYS_DEBOUNCE_INTERVAL (3 * SXTLITE_KEYS_POLL_INTERVAL)
64 static struct mtd_partition rbsxtlite_nand_partitions
[] = {
69 .mask_flags
= MTD_WRITEABLE
,
73 .offset
= (256 * 1024),
74 .size
= (4 * 1024 * 1024) - (256 * 1024),
78 .offset
= MTDPART_OFS_NXTBLK
,
79 .size
= MTDPART_SIZ_FULL
,
83 static struct gpio_led rbsxtlite_leds_gpio
[] __initdata
= {
85 .name
= "rb:green:user",
86 .gpio
= SXTLITE_GPIO_LED_USER
,
90 .name
= "rb:green:led1",
91 .gpio
= SXTLITE_GPIO_LED_1
,
95 .name
= "rb:green:led2",
96 .gpio
= SXTLITE_GPIO_LED_2
,
100 .name
= "rb:green:led3",
101 .gpio
= SXTLITE_GPIO_LED_3
,
105 .name
= "rb:green:led4",
106 .gpio
= SXTLITE_GPIO_LED_4
,
110 .name
= "rb:green:led5",
111 .gpio
= SXTLITE_GPIO_LED_5
,
115 .name
= "rb:green:power",
116 .gpio
= SXTLITE_GPIO_LED_POWER
,
117 .default_state
= LEDS_GPIO_DEFSTATE_KEEP
,
121 static struct gpio_keys_button rbsxtlite_gpio_keys
[] __initdata
= {
123 .desc
= "Reset button",
126 .debounce_interval
= SXTLITE_KEYS_DEBOUNCE_INTERVAL
,
127 .gpio
= SXTLITE_GPIO_BTN_RESET
,
132 static int __init
rbsxtlite_rbinfo_init(void)
134 const struct rb_info
*info
;
136 info
= rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE
)), 0x10000);
143 void __init
rbsxtlite_wlan_init(void)
146 u8 wlan_mac
[ETH_ALEN
];
148 art_buf
= rb_get_wlan_data();
152 ath79_init_mac(wlan_mac
, ath79_mac_base
, 1);
153 ath79_register_wmac(art_buf
+ 0x1000, wlan_mac
);
158 static void rbsxtlite_nand_select_chip(int chip_no
)
162 gpio_set_value(SXTLITE_GPIO_NAND_NCE
, 0);
165 gpio_set_value(SXTLITE_GPIO_NAND_NCE
, 1);
171 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
172 static struct nand_ecclayout rbsxtlite_nand_ecclayout
= {
174 .eccpos
= { 8, 9, 10, 13, 14, 15 },
176 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
181 static int rbsxtlite_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
182 struct mtd_oob_region
*oobregion
)
186 oobregion
->offset
= 8;
187 oobregion
->length
= 3;
190 oobregion
->offset
= 13;
191 oobregion
->length
= 3;
198 static int rbsxtlite_ooblayout_free(struct mtd_info
*mtd
, int section
,
199 struct mtd_oob_region
*oobregion
)
203 oobregion
->offset
= 0;
204 oobregion
->length
= 4;
207 oobregion
->offset
= 4;
208 oobregion
->length
= 1;
211 oobregion
->offset
= 6;
212 oobregion
->length
= 2;
215 oobregion
->offset
= 11;
216 oobregion
->length
= 2;
223 static const struct mtd_ooblayout_ops rbsxtlite_nand_ecclayout_ops
= {
224 .ecc
= rbsxtlite_ooblayout_ecc
,
225 .free
= rbsxtlite_ooblayout_free
,
229 static int rbsxtlite_nand_scan_fixup(struct mtd_info
*mtd
)
231 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
232 struct nand_chip
*chip
= mtd
->priv
;
235 if (mtd
->writesize
== 512) {
237 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
238 * will not be able to find the kernel that we load.
240 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
241 chip
->ecc
.layout
= &rbsxtlite_nand_ecclayout
;
243 mtd_set_ooblayout(mtd
, &rbsxtlite_nand_ecclayout_ops
);
250 void __init
rbsxtlite_gpio_init(void)
252 gpio_request_one(SXTLITE_GPIO_NAND_NCE
, GPIOF_OUT_INIT_HIGH
, "NAND nCE");
255 void __init
rbsxtlite_nand_init(void)
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();
266 static void __init
rbsxtlite_setup(void)
268 if(rbsxtlite_rbinfo_init())
270 rbsxtlite_nand_init();
271 rbsxtlite_wlan_init();
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
);
279 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE
);
281 ath79_register_mdio(1, 0x0);
283 /* GMAC0 is left unused */
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);
296 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE2ND
, "sxt2n", "MikroTik RouterBOARD SXT Lite2",
299 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE5ND
, "sxt5n", "MikroTik RouterBOARD SXT Lite5",