mach-rb2011.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * MikroTik RouterBOARD 2011 support
  3. *
  4. * Copyright (C) 2012 Stijn Tintel <stijn@linux-ipv6.be>
  5. * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) "rb2011: " fmt
  12. #include <linux/version.h>
  13. #include <linux/phy.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ath9k_platform.h>
  17. #include <linux/ar8216_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/routerboot.h>
  28. #include <linux/gpio.h>
  29. #include <linux/version.h>
  30. #include <asm/prom.h>
  31. #include <asm/mach-ath79/ath79.h>
  32. #include <asm/mach-ath79/ar71xx_regs.h>
  33. #include "common.h"
  34. #include "dev-eth.h"
  35. #include "dev-m25p80.h"
  36. #include "dev-nfc.h"
  37. #include "dev-usb.h"
  38. #include "dev-wmac.h"
  39. #include "machtypes.h"
  40. #include "routerboot.h"
  41. #define RB2011_GPIO_NAND_NCE 14
  42. #define RB2011_GPIO_SFP_LOS 21
  43. #define RB_ROUTERBOOT_OFFSET 0x0000
  44. #define RB_ROUTERBOOT_MIN_SIZE 0xb000
  45. #define RB_HARD_CFG_SIZE 0x1000
  46. #define RB_BIOS_OFFSET 0xd000
  47. #define RB_BIOS_SIZE 0x1000
  48. #define RB_SOFT_CFG_OFFSET 0xf000
  49. #define RB_SOFT_CFG_SIZE 0x1000
  50. #define RB_ART_SIZE 0x10000
  51. #define RB2011_FLAG_SFP BIT(0)
  52. #define RB2011_FLAG_USB BIT(1)
  53. #define RB2011_FLAG_WLAN BIT(2)
  54. static struct mtd_partition rb2011_spi_partitions[] = {
  55. {
  56. .name = "routerboot",
  57. .offset = RB_ROUTERBOOT_OFFSET,
  58. .mask_flags = MTD_WRITEABLE,
  59. }, {
  60. .name = "hard_config",
  61. .size = RB_HARD_CFG_SIZE,
  62. .mask_flags = MTD_WRITEABLE,
  63. }, {
  64. .name = "bios",
  65. .offset = RB_BIOS_OFFSET,
  66. .size = RB_BIOS_SIZE,
  67. .mask_flags = MTD_WRITEABLE,
  68. }, {
  69. .name = "soft_config",
  70. .size = RB_SOFT_CFG_SIZE,
  71. }
  72. };
  73. static void __init rb2011_init_partitions(const struct rb_info *info)
  74. {
  75. rb2011_spi_partitions[0].size = info->hard_cfg_offs;
  76. rb2011_spi_partitions[1].offset = info->hard_cfg_offs;
  77. rb2011_spi_partitions[3].offset = info->soft_cfg_offs;
  78. }
  79. static struct mtd_partition rb2011_nand_partitions[] = {
  80. {
  81. .name = "booter",
  82. .offset = 0,
  83. .size = (256 * 1024),
  84. .mask_flags = MTD_WRITEABLE,
  85. },
  86. {
  87. .name = "kernel",
  88. .offset = (256 * 1024),
  89. .size = (4 * 1024 * 1024) - (256 * 1024),
  90. },
  91. {
  92. .name = "ubi",
  93. .offset = MTDPART_OFS_NXTBLK,
  94. .size = MTDPART_SIZ_FULL,
  95. },
  96. };
  97. static struct flash_platform_data rb2011_spi_flash_data = {
  98. .parts = rb2011_spi_partitions,
  99. .nr_parts = ARRAY_SIZE(rb2011_spi_partitions),
  100. };
  101. static struct ar8327_pad_cfg rb2011_ar8327_pad0_cfg = {
  102. .mode = AR8327_PAD_MAC_RGMII,
  103. .txclk_delay_en = true,
  104. .rxclk_delay_en = true,
  105. .txclk_delay_sel = AR8327_CLK_DELAY_SEL3,
  106. .rxclk_delay_sel = AR8327_CLK_DELAY_SEL0,
  107. };
  108. static struct ar8327_pad_cfg rb2011_ar8327_pad6_cfg;
  109. static struct ar8327_sgmii_cfg rb2011_ar8327_sgmii_cfg;
  110. static struct ar8327_led_cfg rb2011_ar8327_led_cfg = {
  111. .led_ctrl0 = 0xc731c731,
  112. .led_ctrl1 = 0x00000000,
  113. .led_ctrl2 = 0x00000000,
  114. .led_ctrl3 = 0x0030c300,
  115. .open_drain = false,
  116. };
  117. static const struct ar8327_led_info rb2011_ar8327_leds[] = {
  118. AR8327_LED_INFO(PHY0_0, HW, "rb:green:eth1"),
  119. AR8327_LED_INFO(PHY1_0, HW, "rb:green:eth2"),
  120. AR8327_LED_INFO(PHY2_0, HW, "rb:green:eth3"),
  121. AR8327_LED_INFO(PHY3_0, HW, "rb:green:eth4"),
  122. AR8327_LED_INFO(PHY4_0, HW, "rb:green:eth5"),
  123. AR8327_LED_INFO(PHY0_1, SW, "rb:green:eth6"),
  124. AR8327_LED_INFO(PHY1_1, SW, "rb:green:eth7"),
  125. AR8327_LED_INFO(PHY2_1, SW, "rb:green:eth8"),
  126. AR8327_LED_INFO(PHY3_1, SW, "rb:green:eth9"),
  127. AR8327_LED_INFO(PHY4_1, SW, "rb:green:eth10"),
  128. AR8327_LED_INFO(PHY4_2, SW, "rb:green:usr"),
  129. };
  130. static struct ar8327_platform_data rb2011_ar8327_data = {
  131. .pad0_cfg = &rb2011_ar8327_pad0_cfg,
  132. .port0_cfg = {
  133. .force_link = 1,
  134. .speed = AR8327_PORT_SPEED_1000,
  135. .duplex = 1,
  136. .txpause = 1,
  137. .rxpause = 1,
  138. },
  139. .led_cfg = &rb2011_ar8327_led_cfg,
  140. .num_leds = ARRAY_SIZE(rb2011_ar8327_leds),
  141. .leds = rb2011_ar8327_leds,
  142. };
  143. static struct mdio_board_info rb2011_mdio0_info[] = {
  144. {
  145. .bus_id = "ag71xx-mdio.0",
  146. .mdio_addr = 0,
  147. .platform_data = &rb2011_ar8327_data,
  148. },
  149. };
  150. static void __init rb2011_wlan_init(void)
  151. {
  152. char *art_buf;
  153. u8 wlan_mac[ETH_ALEN];
  154. art_buf = rb_get_wlan_data();
  155. if (art_buf == NULL)
  156. return;
  157. ath79_init_mac(wlan_mac, ath79_mac_base, 11);
  158. ath79_register_wmac(art_buf + 0x1000, wlan_mac);
  159. kfree(art_buf);
  160. }
  161. static void rb2011_nand_select_chip(int chip_no)
  162. {
  163. switch (chip_no) {
  164. case 0:
  165. gpio_set_value(RB2011_GPIO_NAND_NCE, 0);
  166. break;
  167. default:
  168. gpio_set_value(RB2011_GPIO_NAND_NCE, 1);
  169. break;
  170. }
  171. ndelay(500);
  172. }
  173. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  174. static struct nand_ecclayout rb2011_nand_ecclayout = {
  175. .eccbytes = 6,
  176. .eccpos = { 8, 9, 10, 13, 14, 15 },
  177. .oobavail = 9,
  178. .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  179. };
  180. #else
  181. static int rb2011_ooblayout_ecc(struct mtd_info *mtd, int section,
  182. struct mtd_oob_region *oobregion)
  183. {
  184. switch (section) {
  185. case 0:
  186. oobregion->offset = 8;
  187. oobregion->length = 3;
  188. return 0;
  189. case 1:
  190. oobregion->offset = 13;
  191. oobregion->length = 3;
  192. return 0;
  193. default:
  194. return -ERANGE;
  195. }
  196. }
  197. static int rb2011_ooblayout_free(struct mtd_info *mtd, int section,
  198. struct mtd_oob_region *oobregion)
  199. {
  200. switch (section) {
  201. case 0:
  202. oobregion->offset = 0;
  203. oobregion->length = 4;
  204. return 0;
  205. case 1:
  206. oobregion->offset = 4;
  207. oobregion->length = 1;
  208. return 0;
  209. case 2:
  210. oobregion->offset = 6;
  211. oobregion->length = 2;
  212. return 0;
  213. case 3:
  214. oobregion->offset = 11;
  215. oobregion->length = 2;
  216. return 0;
  217. default:
  218. return -ERANGE;
  219. }
  220. }
  221. static const struct mtd_ooblayout_ops rb2011_nand_ecclayout_ops = {
  222. .ecc = rb2011_ooblayout_ecc,
  223. .free = rb2011_ooblayout_free,
  224. };
  225. #endif /* < 4.6 */
  226. static int rb2011_nand_scan_fixup(struct mtd_info *mtd)
  227. {
  228. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  229. struct nand_chip *chip = mtd->priv;
  230. #endif
  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 = &rb2011_nand_ecclayout;
  238. #else
  239. mtd_set_ooblayout(mtd, &rb2011_nand_ecclayout_ops);
  240. #endif
  241. }
  242. return 0;
  243. }
  244. static void __init rb2011_nand_init(void)
  245. {
  246. gpio_request_one(RB2011_GPIO_NAND_NCE, GPIOF_OUT_INIT_HIGH, "NAND nCE");
  247. ath79_nfc_set_scan_fixup(rb2011_nand_scan_fixup);
  248. ath79_nfc_set_parts(rb2011_nand_partitions,
  249. ARRAY_SIZE(rb2011_nand_partitions));
  250. ath79_nfc_set_select_chip(rb2011_nand_select_chip);
  251. ath79_nfc_set_swap_dma(true);
  252. ath79_register_nfc();
  253. }
  254. static int rb2011_get_port_link(unsigned port)
  255. {
  256. if (port != 6)
  257. return -EINVAL;
  258. /* The Loss of signal line is active low */
  259. return !gpio_get_value(RB2011_GPIO_SFP_LOS);
  260. }
  261. static void __init rb2011_sfp_init(void)
  262. {
  263. gpio_request_one(RB2011_GPIO_SFP_LOS, GPIOF_IN, "SFP LOS");
  264. rb2011_ar8327_pad6_cfg.mode = AR8327_PAD_MAC_SGMII;
  265. rb2011_ar8327_data.pad6_cfg = &rb2011_ar8327_pad6_cfg;
  266. rb2011_ar8327_sgmii_cfg.sgmii_ctrl = 0xc70167d0;
  267. rb2011_ar8327_sgmii_cfg.serdes_aen = true;
  268. rb2011_ar8327_data.sgmii_cfg = &rb2011_ar8327_sgmii_cfg;
  269. rb2011_ar8327_data.port6_cfg.force_link = 1;
  270. rb2011_ar8327_data.port6_cfg.speed = AR8327_PORT_SPEED_1000;
  271. rb2011_ar8327_data.port6_cfg.duplex = 1;
  272. rb2011_ar8327_data.get_port_link = rb2011_get_port_link;
  273. }
  274. static int __init rb2011_setup(u32 flags)
  275. {
  276. const struct rb_info *info;
  277. char buf[64];
  278. info = rb_init_info((void *) KSEG1ADDR(0x1f000000), 0x10000);
  279. if (!info)
  280. return -ENODEV;
  281. scnprintf(buf, sizeof(buf), "Mikrotik RouterBOARD %s",
  282. (info->board_name) ? info->board_name : "");
  283. mips_set_machine_name(buf);
  284. rb2011_init_partitions(info);
  285. ath79_register_m25p80(&rb2011_spi_flash_data);
  286. rb2011_nand_init();
  287. ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
  288. AR934X_ETH_CFG_RXD_DELAY |
  289. AR934X_ETH_CFG_SW_ONLY_MODE);
  290. ath79_register_mdio(1, 0x0);
  291. ath79_register_mdio(0, 0x0);
  292. mdiobus_register_board_info(rb2011_mdio0_info,
  293. ARRAY_SIZE(rb2011_mdio0_info));
  294. /* GMAC0 is connected to an ar8327 switch */
  295. ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
  296. ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
  297. ath79_eth0_data.phy_mask = BIT(0);
  298. ath79_eth0_data.mii_bus_dev = &ath79_mdio0_device.dev;
  299. ath79_eth0_pll_data.pll_1000 = 0x6f000000;
  300. ath79_register_eth(0);
  301. /* GMAC1 is connected to the internal switch */
  302. ath79_init_mac(ath79_eth1_data.mac_addr, ath79_mac_base, 5);
  303. ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
  304. ath79_eth1_data.speed = SPEED_1000;
  305. ath79_eth1_data.duplex = DUPLEX_FULL;
  306. ath79_register_eth(1);
  307. if (flags & RB2011_FLAG_SFP)
  308. rb2011_sfp_init();
  309. if (flags & RB2011_FLAG_WLAN)
  310. rb2011_wlan_init();
  311. if (flags & RB2011_FLAG_USB)
  312. ath79_register_usb();
  313. return 0;
  314. }
  315. static void __init rb2011l_setup(void)
  316. {
  317. rb2011_setup(0);
  318. }
  319. MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011L, "2011L", rb2011l_setup);
  320. static void __init rb2011us_setup(void)
  321. {
  322. rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB);
  323. }
  324. MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011US, "2011US", rb2011us_setup);
  325. static void __init rb2011r5_setup(void)
  326. {
  327. rb2011_setup(RB2011_FLAG_SFP | RB2011_FLAG_USB | RB2011_FLAG_WLAN);
  328. }
  329. MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011R5, "2011r5", rb2011r5_setup);
  330. static void __init rb2011g_setup(void)
  331. {
  332. rb2011_setup(RB2011_FLAG_SFP |
  333. RB2011_FLAG_USB |
  334. RB2011_FLAG_WLAN);
  335. }
  336. MIPS_MACHINE_NONAME(ATH79_MACH_RB_2011G, "2011G", rb2011g_setup);