mach-rbspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * MikroTik SPI-NOR RouterBOARDs support
  3. *
  4. * - MikroTik RouterBOARD mAP L-2nD
  5. * - MikroTik RouterBOARD 941L-2nD
  6. * - MikroTik RouterBOARD 951Ui-2nD
  7. *
  8. * Copyright (C) 2017 Thibaut VARENE <[email protected]>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/phy.h>
  16. #include <linux/routerboot.h>
  17. #include <linux/gpio.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/74x164.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <asm/prom.h>
  23. #include <asm/mach-ath79/ar71xx_regs.h>
  24. #include <asm/mach-ath79/ath79.h>
  25. #include "common.h"
  26. #include "dev-eth.h"
  27. #include "dev-spi.h"
  28. #include "dev-gpio-buttons.h"
  29. #include "dev-leds-gpio.h"
  30. #include "dev-m25p80.h"
  31. #include "dev-usb.h"
  32. #include "dev-wmac.h"
  33. #include "machtypes.h"
  34. #include "routerboot.h"
  35. #define RBSPI_KEYS_POLL_INTERVAL 20 /* msecs */
  36. #define RBSPI_KEYS_DEBOUNCE_INTERVAL (3 * RBSPI_KEYS_POLL_INTERVAL)
  37. #define RBSPI_HAS_USB BIT(0)
  38. #define RBSPI_HAS_WLAN BIT(1)
  39. #define RBSPI_HAS_WAN4 BIT(2) /* has WAN port on PHY4 */
  40. #define RBSPI_HAS_SSR BIT(3) /* has an SSR on SPI bus 0 */
  41. #define RB_ROUTERBOOT_OFFSET 0x0000
  42. #define RB_BIOS_SIZE 0x1000
  43. #define RB_SOFT_CFG_SIZE 0x1000
  44. #define RB_KERNEL_SIZE (2 * 1024 * 1024) /* 2MB kernel */
  45. /* Flash partitions indexes */
  46. enum {
  47. RBSPI_PART_RBOOT,
  48. RBSPI_PART_HCONF,
  49. RBSPI_PART_BIOS,
  50. RBSPI_PART_RBOOT2,
  51. RBSPI_PART_SCONF,
  52. RBSPI_PART_KERN,
  53. RBSPI_PART_ROOT,
  54. RBSPI_PARTS
  55. };
  56. static struct mtd_partition rbspi_spi_partitions[RBSPI_PARTS];
  57. /*
  58. * Setup the SPI flash partition table based on initial parsing.
  59. * The kernel can be at any aligned position and have any size.
  60. * The size of the kernel partition is the desired RB_KERNEL_SIZE
  61. * minus the size of the preceding partitions (128KB).
  62. */
  63. static void __init rbspi_init_partitions(const struct rb_info *info)
  64. {
  65. struct mtd_partition *parts = rbspi_spi_partitions;
  66. memset(parts, 0x0, sizeof(*parts));
  67. parts[RBSPI_PART_RBOOT].name = "routerboot";
  68. parts[RBSPI_PART_RBOOT].offset = RB_ROUTERBOOT_OFFSET;
  69. parts[RBSPI_PART_RBOOT].size = info->hard_cfg_offs;
  70. parts[RBSPI_PART_RBOOT].mask_flags = MTD_WRITEABLE;
  71. parts[RBSPI_PART_HCONF].name = "hard_config";
  72. parts[RBSPI_PART_HCONF].offset = info->hard_cfg_offs;
  73. parts[RBSPI_PART_HCONF].size = info->hard_cfg_size;
  74. parts[RBSPI_PART_HCONF].mask_flags = MTD_WRITEABLE;
  75. parts[RBSPI_PART_BIOS].name = "bios";
  76. parts[RBSPI_PART_BIOS].offset = info->hard_cfg_offs
  77. + info->hard_cfg_size;
  78. parts[RBSPI_PART_BIOS].size = RB_BIOS_SIZE;
  79. parts[RBSPI_PART_BIOS].mask_flags = MTD_WRITEABLE;
  80. parts[RBSPI_PART_RBOOT2].name = "routerboot2";
  81. parts[RBSPI_PART_RBOOT2].offset = parts[RBSPI_PART_BIOS].offset
  82. + RB_BIOS_SIZE;
  83. parts[RBSPI_PART_RBOOT2].size = info->soft_cfg_offs
  84. - parts[RBSPI_PART_RBOOT2].offset;
  85. parts[RBSPI_PART_RBOOT2].mask_flags = MTD_WRITEABLE;
  86. parts[RBSPI_PART_SCONF].name = "soft_config";
  87. parts[RBSPI_PART_SCONF].offset = info->soft_cfg_offs;
  88. parts[RBSPI_PART_SCONF].size = RB_SOFT_CFG_SIZE;
  89. parts[RBSPI_PART_KERN].name = "kernel";
  90. parts[RBSPI_PART_KERN].offset = parts[RBSPI_PART_SCONF].offset
  91. + parts[RBSPI_PART_SCONF].size;
  92. parts[RBSPI_PART_KERN].size = RB_KERNEL_SIZE
  93. - parts[RBSPI_PART_KERN].offset;
  94. parts[RBSPI_PART_ROOT].name = "rootfs";
  95. parts[RBSPI_PART_ROOT].offset = parts[RBSPI_PART_KERN].offset
  96. + parts[RBSPI_PART_KERN].size;
  97. parts[RBSPI_PART_ROOT].size = MTDPART_SIZ_FULL;
  98. }
  99. static struct flash_platform_data rbspi_spi_flash_data = {
  100. .parts = rbspi_spi_partitions,
  101. .nr_parts = ARRAY_SIZE(rbspi_spi_partitions),
  102. };
  103. /* Several boards only have a single reset button wired to GPIO 16 */
  104. #define RBSPI_GPIO_BTN_RESET16 16
  105. static struct gpio_keys_button rbspi_gpio_keys_reset16[] __initdata = {
  106. {
  107. .desc = "Reset button",
  108. .type = EV_KEY,
  109. .code = KEY_RESTART,
  110. .debounce_interval = RBSPI_KEYS_DEBOUNCE_INTERVAL,
  111. .gpio = RBSPI_GPIO_BTN_RESET16,
  112. .active_low = 1,
  113. },
  114. };
  115. /* RB mAP L-2nD gpios */
  116. #define RBMAPL_GPIO_LED_POWER 17
  117. #define RBMAPL_GPIO_LED_USER 14
  118. #define RBMAPL_GPIO_LED_ETH 4
  119. #define RBMAPL_GPIO_LED_WLAN 11
  120. static struct gpio_led rbmapl_leds[] __initdata = {
  121. {
  122. .name = "rb:green:power",
  123. .gpio = RBMAPL_GPIO_LED_POWER,
  124. .active_low = 0,
  125. .default_state = LEDS_GPIO_DEFSTATE_ON,
  126. }, {
  127. .name = "rb:green:user",
  128. .gpio = RBMAPL_GPIO_LED_USER,
  129. .active_low = 0,
  130. }, {
  131. .name = "rb:green:eth",
  132. .gpio = RBMAPL_GPIO_LED_ETH,
  133. .active_low = 0,
  134. }, {
  135. .name = "rb:green:wlan",
  136. .gpio = RBMAPL_GPIO_LED_WLAN,
  137. .active_low = 0,
  138. },
  139. };
  140. /* RB 941L-2nD gpios */
  141. #define RBHAPL_GPIO_LED_USER 14
  142. static struct gpio_led rbhapl_leds[] __initdata = {
  143. {
  144. .name = "rb:green:user",
  145. .gpio = RBHAPL_GPIO_LED_USER,
  146. .active_low = 1,
  147. },
  148. };
  149. /* common RB SSRs */
  150. #define RBSPI_SSR_GPIO_BASE 40
  151. #define RBSPI_SSR_GPIO(bit) (RBSPI_SSR_GPIO_BASE + (bit))
  152. /* RB 951Ui-2nD gpios */
  153. #define RB952_SSR_BIT_LED_LAN1 0
  154. #define RB952_SSR_BIT_LED_LAN2 1
  155. #define RB952_SSR_BIT_LED_LAN3 2
  156. #define RB952_SSR_BIT_LED_LAN4 3
  157. #define RB952_SSR_BIT_LED_LAN5 4
  158. #define RB952_SSR_BIT_USB_POWER 5
  159. #define RB952_SSR_BIT_LED_WLAN 6
  160. #define RB952_GPIO_SSR_CS 11
  161. #define RB952_GPIO_LED_USER 4
  162. #define RB952_GPIO_POE_POWER 14
  163. #define RB952_GPIO_USB_POWER RBSPI_SSR_GPIO(RB952_SSR_BIT_USB_POWER)
  164. #define RB952_GPIO_LED_LAN1 RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_LAN1)
  165. #define RB952_GPIO_LED_LAN2 RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_LAN2)
  166. #define RB952_GPIO_LED_LAN3 RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_LAN3)
  167. #define RB952_GPIO_LED_LAN4 RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_LAN4)
  168. #define RB952_GPIO_LED_LAN5 RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_LAN5)
  169. #define RB952_GPIO_LED_WLAN RBSPI_SSR_GPIO(RB952_SSR_BIT_LED_WLAN)
  170. static struct gpio_led rb952_leds[] __initdata = {
  171. {
  172. .name = "rb:green:user",
  173. .gpio = RB952_GPIO_LED_USER,
  174. .active_low = 0,
  175. }, {
  176. .name = "rb:blue:wlan",
  177. .gpio = RB952_GPIO_LED_WLAN,
  178. .active_low = 1,
  179. }, {
  180. .name = "rb:green:port1",
  181. .gpio = RB952_GPIO_LED_LAN1,
  182. .active_low = 1,
  183. }, {
  184. .name = "rb:green:port2",
  185. .gpio = RB952_GPIO_LED_LAN2,
  186. .active_low = 1,
  187. }, {
  188. .name = "rb:green:port3",
  189. .gpio = RB952_GPIO_LED_LAN3,
  190. .active_low = 1,
  191. }, {
  192. .name = "rb:green:port4",
  193. .gpio = RB952_GPIO_LED_LAN4,
  194. .active_low = 1,
  195. }, {
  196. .name = "rb:green:port5",
  197. .gpio = RB952_GPIO_LED_LAN5,
  198. .active_low = 1,
  199. },
  200. };
  201. static struct gen_74x164_chip_platform_data rbspi_ssr_data = {
  202. .base = RBSPI_SSR_GPIO_BASE,
  203. };
  204. /* the spi-ath79 driver can only natively handle CS0. Other CS are bit-banged */
  205. static int rbspi_spi_cs_gpios[] = {
  206. -ENOENT, /* CS0 is always -ENOENT: natively handled */
  207. -ENOENT, /* CS1 can be updated by the code as necessary */
  208. };
  209. static struct ath79_spi_platform_data rbspi_ath79_spi_data = {
  210. .bus_num = 0,
  211. .cs_gpios = rbspi_spi_cs_gpios,
  212. };
  213. /*
  214. * Global spi_board_info: devices that don't have an SSR only have the SPI NOR
  215. * flash on bus0 CS0, while devices that have an SSR add it on the same bus CS1
  216. */
  217. static struct spi_board_info rbspi_spi_info[] = {
  218. {
  219. .bus_num = 0,
  220. .chip_select = 0,
  221. .max_speed_hz = 25000000,
  222. .modalias = "m25p80",
  223. .platform_data = &rbspi_spi_flash_data,
  224. }, {
  225. .bus_num = 0,
  226. .chip_select = 1,
  227. .max_speed_hz = 25000000,
  228. .modalias = "74x164",
  229. .platform_data = &rbspi_ssr_data,
  230. }
  231. };
  232. void __init rbspi_wlan_init(int wmac_offset)
  233. {
  234. char *art_buf;
  235. u8 wlan_mac[ETH_ALEN];
  236. art_buf = rb_get_wlan_data();
  237. if (!art_buf)
  238. return;
  239. ath79_init_mac(wlan_mac, ath79_mac_base, wmac_offset);
  240. ath79_register_wmac(art_buf + 0x1000, wlan_mac);
  241. kfree(art_buf);
  242. }
  243. /*
  244. * Common platform init routine for all SPI NOR devices.
  245. */
  246. static int __init rbspi_platform_setup(void)
  247. {
  248. const struct rb_info *info;
  249. char buf[64];
  250. info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x20000);
  251. if (!info)
  252. return -ENODEV;
  253. scnprintf(buf, sizeof(buf), "MikroTik %s",
  254. (info->board_name) ? info->board_name : "");
  255. mips_set_machine_name(buf);
  256. /* fix partitions based on flash parsing */
  257. rbspi_init_partitions(info);
  258. return 0;
  259. }
  260. /*
  261. * Common peripherals init routine for all SPI NOR devices.
  262. * Sets SPI and USB.
  263. */
  264. static void __init rbspi_peripherals_setup(u32 flags)
  265. {
  266. unsigned spi_n;
  267. if (flags & RBSPI_HAS_SSR)
  268. spi_n = ARRAY_SIZE(rbspi_spi_info);
  269. else
  270. spi_n = 1; /* only one device on bus0 */
  271. rbspi_ath79_spi_data.num_chipselect = spi_n;
  272. rbspi_ath79_spi_data.cs_gpios = rbspi_spi_cs_gpios;
  273. ath79_register_spi(&rbspi_ath79_spi_data, rbspi_spi_info, spi_n);
  274. if (flags & RBSPI_HAS_USB)
  275. ath79_register_usb();
  276. }
  277. /*
  278. * Common network init routine for all SPI NOR devices.
  279. * Sets LAN/WAN/WLAN.
  280. */
  281. static void __init rbspi_network_setup(u32 flags, int gmac1_offset,
  282. int wmac_offset)
  283. {
  284. /* for QCA953x that will init mdio1_device/data */
  285. ath79_register_mdio(0, 0x0);
  286. if (flags & RBSPI_HAS_WAN4) {
  287. ath79_setup_ar934x_eth_cfg(0);
  288. /* set switch to oper mode 1, PHY4 connected to CPU */
  289. ath79_switch_data.phy4_mii_en = 1;
  290. ath79_switch_data.phy_poll_mask |= BIT(4);
  291. /* init GMAC0 connected to PHY4 at 100M */
  292. ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
  293. ath79_eth0_data.phy_mask = BIT(4);
  294. ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
  295. ath79_register_eth(0);
  296. } else {
  297. /* set the SoC to SW_ONLY_MODE, which connects all PHYs
  298. * to the internal switch.
  299. * We hijack ath79_setup_ar934x_eth_cfg() to set the switch in
  300. * the QCA953x, this works because this configuration bit is
  301. * the same as the AR934x. There's no equivalent function for
  302. * QCA953x for now. */
  303. ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
  304. }
  305. /* init GMAC1 */
  306. ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, gmac1_offset);
  307. ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
  308. ath79_register_eth(1);
  309. if (flags & RBSPI_HAS_WLAN)
  310. rbspi_wlan_init(wmac_offset);
  311. }
  312. /*
  313. * Init the mAP lite hardware.
  314. * The mAP L-2nD (mAP lite) has a single ethernet port, connected to PHY0.
  315. * Trying to use GMAC0 in direct mode was unsucessful, so we're
  316. * using SW_ONLY_MODE, which connects PHY0 to MAC1 on the internal
  317. * switch, which is connected to GMAC1 on the SoC. GMAC0 is unused.
  318. */
  319. static void __init rbmapl_setup(void)
  320. {
  321. u32 flags = RBSPI_HAS_WLAN;
  322. if (rbspi_platform_setup())
  323. return;
  324. rbspi_peripherals_setup(flags);
  325. /* GMAC1 is HW MAC, WLAN MAC is HW MAC + 1 */
  326. rbspi_network_setup(flags, 0, 1);
  327. ath79_register_leds_gpio(-1, ARRAY_SIZE(rbmapl_leds), rbmapl_leds);
  328. /* mAP lite has a single reset button as gpio 16 */
  329. ath79_register_gpio_keys_polled(-1, RBSPI_KEYS_POLL_INTERVAL,
  330. ARRAY_SIZE(rbspi_gpio_keys_reset16),
  331. rbspi_gpio_keys_reset16);
  332. /* clear internal multiplexing */
  333. ath79_gpio_output_select(RBMAPL_GPIO_LED_ETH, AR934X_GPIO_OUT_GPIO);
  334. ath79_gpio_output_select(RBMAPL_GPIO_LED_POWER, AR934X_GPIO_OUT_GPIO);
  335. }
  336. /*
  337. * Init the hAP lite hardware.
  338. * The 941-2nD (hAP lite) has 4 ethernet ports, with port 2-4
  339. * being assigned to LAN on the casing, and port 1 being assigned
  340. * to "internet" (WAN) on the casing. Port 1 is connected to PHY3.
  341. * Since WAN is neither PHY0 nor PHY4, we cannot use GMAC0 with this device.
  342. */
  343. static void __init rbhapl_setup(void)
  344. {
  345. u32 flags = RBSPI_HAS_WLAN;
  346. if (rbspi_platform_setup())
  347. return;
  348. rbspi_peripherals_setup(flags);
  349. /* GMAC1 is HW MAC, WLAN MAC is HW MAC + 4 */
  350. rbspi_network_setup(flags, 0, 4);
  351. ath79_register_leds_gpio(-1, ARRAY_SIZE(rbhapl_leds), rbhapl_leds);
  352. /* hAP lite has a single reset button as gpio 16 */
  353. ath79_register_gpio_keys_polled(-1, RBSPI_KEYS_POLL_INTERVAL,
  354. ARRAY_SIZE(rbspi_gpio_keys_reset16),
  355. rbspi_gpio_keys_reset16);
  356. }
  357. /*
  358. * Init the hAP hardware.
  359. * The 951Ui-2nD (hAP) has 5 ethernet ports, with ports 2-5 being assigned
  360. * to LAN on the casing, and port 1 being assigned to "internet" (WAN).
  361. * Port 1 is connected to PHY4 (the ports are labelled in reverse physical
  362. * number), so the SoC can be set to connect GMAC0 to PHY4 and GMAC1 to the
  363. * internal switch for the LAN ports.
  364. * The device also has USB, PoE output and an SSR used for LED multiplexing.
  365. */
  366. static void __init rb952_setup(void)
  367. {
  368. u32 flags = RBSPI_HAS_WLAN | RBSPI_HAS_WAN4 | RBSPI_HAS_USB |
  369. RBSPI_HAS_SSR;
  370. if (rbspi_platform_setup())
  371. return;
  372. rbspi_spi_cs_gpios[1] = RB952_GPIO_SSR_CS;
  373. rbspi_peripherals_setup(flags);
  374. /* GMAC1 is HW MAC + 1, WLAN MAC IS HW MAC + 5 */
  375. rbspi_network_setup(flags, 1, 5);
  376. gpio_request_one(RB952_GPIO_USB_POWER,
  377. GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
  378. "USB power");
  379. gpio_request_one(RB952_GPIO_POE_POWER,
  380. GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
  381. "POE power");
  382. ath79_register_leds_gpio(-1, ARRAY_SIZE(rb952_leds), rb952_leds);
  383. /* hAP has a single reset button as gpio 16 */
  384. ath79_register_gpio_keys_polled(-1, RBSPI_KEYS_POLL_INTERVAL,
  385. ARRAY_SIZE(rbspi_gpio_keys_reset16),
  386. rbspi_gpio_keys_reset16);
  387. }
  388. MIPS_MACHINE_NONAME(ATH79_MACH_RB_MAPL, "map-hb", rbmapl_setup);
  389. MIPS_MACHINE_NONAME(ATH79_MACH_RB_941, "H951L", rbhapl_setup);
  390. MIPS_MACHINE_NONAME(ATH79_MACH_RB_952, "952-hb", rb952_setup);