mach-rbsxtlite.c 6.6 KB

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