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 #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 #include <linux/version.h>
29 #include <asm/mach-ath79/ath79.h>
30 #include <asm/mach-ath79/ar71xx_regs.h>
32 #include "dev-ap9x-pci.h"
33 #include "dev-gpio-buttons.h"
34 #include "dev-leds-gpio.h"
36 #include "dev-m25p80.h"
40 #include "machtypes.h"
41 #include "routerboot.h"
42 #include <linux/ar8216_platform.h>
44 #define SXTLITE_GPIO_NAND_NCE 14
45 #define SXTLITE_GPIO_LED_USER 3
46 #define SXTLITE_GPIO_LED_1 13
47 #define SXTLITE_GPIO_LED_2 12
48 #define SXTLITE_GPIO_LED_3 4
49 #define SXTLITE_GPIO_LED_4 21
50 #define SXTLITE_GPIO_LED_5 18
51 #define SXTLITE_GPIO_LED_POWER 11
53 #define SXTLITE_GPIO_BUZZER 19
55 #define SXTLITE_GPIO_BTN_RESET 15
57 #define SXTLITE_KEYS_POLL_INTERVAL 20
58 #define SXTLITE_KEYS_DEBOUNCE_INTERVAL (3 * SXTLITE_KEYS_POLL_INTERVAL)
60 static struct mtd_partition rbsxtlite_nand_partitions
[] = {
65 .mask_flags
= MTD_WRITEABLE
,
69 .offset
= (256 * 1024),
70 .size
= (4 * 1024 * 1024) - (256 * 1024),
74 .offset
= MTDPART_OFS_NXTBLK
,
75 .size
= MTDPART_SIZ_FULL
,
79 static struct gpio_led rbsxtlite_leds_gpio
[] __initdata
= {
81 .name
= "rb:green:user",
82 .gpio
= SXTLITE_GPIO_LED_USER
,
86 .name
= "rb:green:led1",
87 .gpio
= SXTLITE_GPIO_LED_1
,
91 .name
= "rb:green:led2",
92 .gpio
= SXTLITE_GPIO_LED_2
,
96 .name
= "rb:green:led3",
97 .gpio
= SXTLITE_GPIO_LED_3
,
101 .name
= "rb:green:led4",
102 .gpio
= SXTLITE_GPIO_LED_4
,
106 .name
= "rb:green:led5",
107 .gpio
= SXTLITE_GPIO_LED_5
,
111 .name
= "rb:green:power",
112 .gpio
= SXTLITE_GPIO_LED_POWER
,
113 .default_state
= LEDS_GPIO_DEFSTATE_KEEP
,
117 static struct gpio_keys_button rbsxtlite_gpio_keys
[] __initdata
= {
119 .desc
= "Reset button",
122 .debounce_interval
= SXTLITE_KEYS_DEBOUNCE_INTERVAL
,
123 .gpio
= SXTLITE_GPIO_BTN_RESET
,
128 static int __init
rbsxtlite_rbinfo_init(void)
130 const struct rb_info
*info
;
132 info
= rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE
)), 0x10000);
139 void __init
rbsxtlite_wlan_init(void)
142 u8 wlan_mac
[ETH_ALEN
];
144 art_buf
= rb_get_wlan_data();
148 ath79_init_mac(wlan_mac
, ath79_mac_base
, 1);
149 ath79_register_wmac(art_buf
+ 0x1000, wlan_mac
);
154 static void rbsxtlite_nand_select_chip(int chip_no
)
158 gpio_set_value(SXTLITE_GPIO_NAND_NCE
, 0);
161 gpio_set_value(SXTLITE_GPIO_NAND_NCE
, 1);
167 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
168 static struct nand_ecclayout rbsxtlite_nand_ecclayout
= {
170 .eccpos
= { 8, 9, 10, 13, 14, 15 },
172 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
177 static int rbsxtlite_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
178 struct mtd_oob_region
*oobregion
)
182 oobregion
->offset
= 8;
183 oobregion
->length
= 3;
186 oobregion
->offset
= 13;
187 oobregion
->length
= 3;
194 static int rbsxtlite_ooblayout_free(struct mtd_info
*mtd
, int section
,
195 struct mtd_oob_region
*oobregion
)
199 oobregion
->offset
= 0;
200 oobregion
->length
= 4;
203 oobregion
->offset
= 4;
204 oobregion
->length
= 1;
207 oobregion
->offset
= 6;
208 oobregion
->length
= 2;
211 oobregion
->offset
= 11;
212 oobregion
->length
= 2;
219 static const struct mtd_ooblayout_ops rbsxtlite_nand_ecclayout_ops
= {
220 .ecc
= rbsxtlite_ooblayout_ecc
,
221 .free
= rbsxtlite_ooblayout_free
,
225 static int rbsxtlite_nand_scan_fixup(struct mtd_info
*mtd
)
227 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
228 struct nand_chip
*chip
= mtd
->priv
;
231 if (mtd
->writesize
== 512) {
233 * Use the OLD Yaffs-1 OOB layout, otherwise RouterBoot
234 * will not be able to find the kernel that we load.
236 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
237 chip
->ecc
.layout
= &rbsxtlite_nand_ecclayout
;
239 mtd_set_ooblayout(mtd
, &rbsxtlite_nand_ecclayout_ops
);
246 void __init
rbsxtlite_gpio_init(void)
248 gpio_request_one(SXTLITE_GPIO_NAND_NCE
, GPIOF_OUT_INIT_HIGH
, "NAND nCE");
251 void __init
rbsxtlite_nand_init(void)
253 ath79_nfc_set_scan_fixup(rbsxtlite_nand_scan_fixup
);
254 ath79_nfc_set_parts(rbsxtlite_nand_partitions
,
255 ARRAY_SIZE(rbsxtlite_nand_partitions
));
256 ath79_nfc_set_select_chip(rbsxtlite_nand_select_chip
);
257 ath79_nfc_set_swap_dma(true);
258 ath79_register_nfc();
262 static void __init
rbsxtlite_setup(void)
264 if(rbsxtlite_rbinfo_init())
266 rbsxtlite_nand_init();
267 rbsxtlite_wlan_init();
269 ath79_register_leds_gpio(-1, ARRAY_SIZE(rbsxtlite_leds_gpio
),
270 rbsxtlite_leds_gpio
);
271 ath79_register_gpio_keys_polled(-1, SXTLITE_KEYS_POLL_INTERVAL
,
272 ARRAY_SIZE(rbsxtlite_gpio_keys
),
273 rbsxtlite_gpio_keys
);
275 ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE
);
277 ath79_register_mdio(1, 0x0);
279 /* GMAC0 is left unused */
281 /* GMAC1 is connected to MAC0 on the internal switch */
282 /* The ethernet port connects to PHY P0, which connects to MAC1
283 on the internal switch */
284 ath79_init_mac(ath79_eth1_data
.mac_addr
, ath79_mac_base
, 0);
285 ath79_eth1_data
.phy_if_mode
= PHY_INTERFACE_MODE_GMII
;
286 ath79_register_eth(1);
292 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE2ND
, "sxt2n", "MikroTik RouterBOARD SXT Lite2",
295 MIPS_MACHINE(ATH79_MACH_RB_SXTLITE5ND
, "sxt5n", "MikroTik RouterBOARD SXT Lite5",