mach-rbspi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * MikroTik SPI-NOR RouterBOARDs support
  3. *
  4. * - MikroTik RouterBOARD mAP L-2nD
  5. *
  6. * Copyright (C) 2017 Thibaut VARENE <[email protected]>
  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. #include <linux/platform_device.h>
  13. #include <linux/phy.h>
  14. #include <linux/routerboot.h>
  15. #include <linux/gpio.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <asm/prom.h>
  19. #include <asm/mach-ath79/ar71xx_regs.h>
  20. #include <asm/mach-ath79/ath79.h>
  21. #include "common.h"
  22. #include "dev-eth.h"
  23. #include "dev-spi.h"
  24. #include "dev-gpio-buttons.h"
  25. #include "dev-leds-gpio.h"
  26. #include "dev-m25p80.h"
  27. #include "dev-usb.h"
  28. #include "dev-wmac.h"
  29. #include "machtypes.h"
  30. #include "routerboot.h"
  31. #define RBSPI_KEYS_POLL_INTERVAL 20 /* msecs */
  32. #define RBSPI_KEYS_DEBOUNCE_INTERVAL (3 * RBSPI_KEYS_POLL_INTERVAL)
  33. #define RBSPI_HAS_USB BIT(0)
  34. #define RBSPI_HAS_WLAN BIT(1)
  35. #define RBSPI_HAS_WAN4 BIT(2) /* has WAN port on PHY4 */
  36. #define RB_ROUTERBOOT_OFFSET 0x0000
  37. #define RB_BIOS_SIZE 0x1000
  38. #define RB_SOFT_CFG_SIZE 0x1000
  39. #define RB_KERNEL_SIZE (2 * 1024 * 1024) /* 2MB kernel */
  40. /* Flash partitions indexes */
  41. enum {
  42. RBSPI_PART_RBOOT,
  43. RBSPI_PART_HCONF,
  44. RBSPI_PART_BIOS,
  45. RBSPI_PART_RBOOT2,
  46. RBSPI_PART_SCONF,
  47. RBSPI_PART_KERN,
  48. RBSPI_PART_ROOT,
  49. RBSPI_PARTS
  50. };
  51. static struct mtd_partition rbspi_spi_partitions[RBSPI_PARTS];
  52. /*
  53. * Setup the SPI flash partition table based on initial parsing.
  54. * The kernel can be at any aligned position and have any size.
  55. * The size of the kernel partition is the desired RB_KERNEL_SIZE
  56. * minus the size of the preceding partitions (128KB).
  57. */
  58. static void __init rbspi_init_partitions(const struct rb_info *info)
  59. {
  60. struct mtd_partition *parts = rbspi_spi_partitions;
  61. memset(parts, 0x0, sizeof(*parts));
  62. parts[RBSPI_PART_RBOOT].name = "routerboot";
  63. parts[RBSPI_PART_RBOOT].offset = RB_ROUTERBOOT_OFFSET;
  64. parts[RBSPI_PART_RBOOT].size = info->hard_cfg_offs;
  65. parts[RBSPI_PART_RBOOT].mask_flags = MTD_WRITEABLE;
  66. parts[RBSPI_PART_HCONF].name = "hard_config";
  67. parts[RBSPI_PART_HCONF].offset = info->hard_cfg_offs;
  68. parts[RBSPI_PART_HCONF].size = info->hard_cfg_size;
  69. parts[RBSPI_PART_HCONF].mask_flags = MTD_WRITEABLE;
  70. parts[RBSPI_PART_BIOS].name = "bios";
  71. parts[RBSPI_PART_BIOS].offset = info->hard_cfg_offs
  72. + info->hard_cfg_size;
  73. parts[RBSPI_PART_BIOS].size = RB_BIOS_SIZE;
  74. parts[RBSPI_PART_BIOS].mask_flags = MTD_WRITEABLE;
  75. parts[RBSPI_PART_RBOOT2].name = "routerboot2";
  76. parts[RBSPI_PART_RBOOT2].offset = parts[RBSPI_PART_BIOS].offset
  77. + RB_BIOS_SIZE;
  78. parts[RBSPI_PART_RBOOT2].size = info->soft_cfg_offs
  79. - parts[RBSPI_PART_RBOOT2].offset;
  80. parts[RBSPI_PART_RBOOT2].mask_flags = MTD_WRITEABLE;
  81. parts[RBSPI_PART_SCONF].name = "soft_config";
  82. parts[RBSPI_PART_SCONF].offset = info->soft_cfg_offs;
  83. parts[RBSPI_PART_SCONF].size = RB_SOFT_CFG_SIZE;
  84. parts[RBSPI_PART_KERN].name = "kernel";
  85. parts[RBSPI_PART_KERN].offset = parts[RBSPI_PART_SCONF].offset
  86. + parts[RBSPI_PART_SCONF].size;
  87. parts[RBSPI_PART_KERN].size = RB_KERNEL_SIZE
  88. - parts[RBSPI_PART_KERN].offset;
  89. parts[RBSPI_PART_ROOT].name = "rootfs";
  90. parts[RBSPI_PART_ROOT].offset = parts[RBSPI_PART_KERN].offset
  91. + parts[RBSPI_PART_KERN].size;
  92. parts[RBSPI_PART_ROOT].size = MTDPART_SIZ_FULL;
  93. }
  94. static struct flash_platform_data rbspi_spi_flash_data = {
  95. .parts = rbspi_spi_partitions,
  96. .nr_parts = ARRAY_SIZE(rbspi_spi_partitions),
  97. };
  98. /* Several boards only have a single reset button wired to GPIO 16 */
  99. #define RBSPI_GPIO_BTN_RESET16 16
  100. static struct gpio_keys_button rbspi_gpio_keys_reset16[] __initdata = {
  101. {
  102. .desc = "Reset button",
  103. .type = EV_KEY,
  104. .code = KEY_RESTART,
  105. .debounce_interval = RBSPI_KEYS_DEBOUNCE_INTERVAL,
  106. .gpio = RBSPI_GPIO_BTN_RESET16,
  107. .active_low = 1,
  108. },
  109. };
  110. /* RB mAP L-2nD gpios */
  111. #define RBMAPL_GPIO_LED_POWER 17
  112. #define RBMAPL_GPIO_LED_USER 14
  113. #define RBMAPL_GPIO_LED_ETH 4
  114. #define RBMAPL_GPIO_LED_WLAN 11
  115. static struct gpio_led rbmapl_leds[] __initdata = {
  116. {
  117. .name = "rb:green:power",
  118. .gpio = RBMAPL_GPIO_LED_POWER,
  119. .active_low = 0,
  120. .default_state = LEDS_GPIO_DEFSTATE_ON,
  121. }, {
  122. .name = "rb:green:user",
  123. .gpio = RBMAPL_GPIO_LED_USER,
  124. .active_low = 0,
  125. }, {
  126. .name = "rb:green:eth",
  127. .gpio = RBMAPL_GPIO_LED_ETH,
  128. .active_low = 0,
  129. }, {
  130. .name = "rb:green:wlan",
  131. .gpio = RBMAPL_GPIO_LED_WLAN,
  132. .active_low = 0,
  133. },
  134. };
  135. void __init rbspi_wlan_init(int wmac_offset)
  136. {
  137. char *art_buf;
  138. u8 wlan_mac[ETH_ALEN];
  139. art_buf = rb_get_wlan_data();
  140. if (!art_buf)
  141. return;
  142. ath79_init_mac(wlan_mac, ath79_mac_base, wmac_offset);
  143. ath79_register_wmac(art_buf + 0x1000, wlan_mac);
  144. kfree(art_buf);
  145. }
  146. /*
  147. * Common platform init routine for all SPI NOR devices.
  148. */
  149. static int __init rbspi_platform_setup(void)
  150. {
  151. const struct rb_info *info;
  152. char buf[64];
  153. info = rb_init_info((void *)(KSEG1ADDR(AR71XX_SPI_BASE)), 0x20000);
  154. if (!info)
  155. return -ENODEV;
  156. scnprintf(buf, sizeof(buf), "MikroTik %s",
  157. (info->board_name) ? info->board_name : "");
  158. mips_set_machine_name(buf);
  159. /* fix partitions based on flash parsing */
  160. rbspi_init_partitions(info);
  161. return 0;
  162. }
  163. /*
  164. * Common peripherals init routine for all SPI NOR devices.
  165. * Sets SPI and USB.
  166. */
  167. static void __init rbspi_peripherals_setup(u32 flags)
  168. {
  169. ath79_register_m25p80(&rbspi_spi_flash_data);
  170. if (flags & RBSPI_HAS_USB)
  171. ath79_register_usb();
  172. }
  173. /*
  174. * Common network init routine for all SPI NOR devices.
  175. * Sets LAN/WAN/WLAN.
  176. */
  177. static void __init rbspi_network_setup(u32 flags, int gmac1_offset,
  178. int wmac_offset)
  179. {
  180. /* for QCA953x that will init mdio1_device/data */
  181. ath79_register_mdio(0, 0x0);
  182. if (flags & RBSPI_HAS_WAN4) {
  183. ath79_setup_ar934x_eth_cfg(0);
  184. /* set switch to oper mode 1, PHY4 connected to CPU */
  185. ath79_switch_data.phy4_mii_en = 1;
  186. ath79_switch_data.phy_poll_mask |= BIT(4);
  187. /* init GMAC0 connected to PHY4 at 100M */
  188. ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
  189. ath79_eth0_data.phy_mask = BIT(4);
  190. ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
  191. ath79_register_eth(0);
  192. } else {
  193. /* set the SoC to SW_ONLY_MODE, which connects all PHYs
  194. * to the internal switch.
  195. * We hijack ath79_setup_ar934x_eth_cfg() to set the switch in
  196. * the QCA953x, this works because this configuration bit is
  197. * the same as the AR934x. There's no equivalent function for
  198. * QCA953x for now. */
  199. ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_ONLY_MODE);
  200. }
  201. /* init GMAC1 */
  202. ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, gmac1_offset);
  203. ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
  204. ath79_register_eth(1);
  205. if (flags & RBSPI_HAS_WLAN)
  206. rbspi_wlan_init(wmac_offset);
  207. }
  208. /*
  209. * Init the mAP lite hardware.
  210. * The mAP L-2nD (mAP lite) has a single ethernet port, connected to PHY0.
  211. * Trying to use GMAC0 in direct mode was unsucessful, so we're
  212. * using SW_ONLY_MODE, which connects PHY0 to MAC1 on the internal
  213. * switch, which is connected to GMAC1 on the SoC. GMAC0 is unused.
  214. */
  215. static void __init rbmapl_setup(void)
  216. {
  217. u32 flags = RBSPI_HAS_WLAN;
  218. if (rbspi_platform_setup())
  219. return;
  220. rbspi_peripherals_setup(flags);
  221. /* GMAC1 is HW MAC, WLAN MAC is HW MAC + 1 */
  222. rbspi_network_setup(flags, 0, 1);
  223. ath79_register_leds_gpio(-1, ARRAY_SIZE(rbmapl_leds), rbmapl_leds);
  224. /* mAP lite has a single reset button as gpio 16 */
  225. ath79_register_gpio_keys_polled(-1, RBSPI_KEYS_POLL_INTERVAL,
  226. ARRAY_SIZE(rbspi_gpio_keys_reset16),
  227. rbspi_gpio_keys_reset16);
  228. /* clear internal multiplexing */
  229. ath79_gpio_output_select(RBMAPL_GPIO_LED_ETH, AR934X_GPIO_OUT_GPIO);
  230. ath79_gpio_output_select(RBMAPL_GPIO_LED_POWER, AR934X_GPIO_OUT_GPIO);
  231. }
  232. MIPS_MACHINE_NONAME(ATH79_MACH_RB_MAPL, "map-hb", rbmapl_setup);