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