vsc9953.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright 2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Driver for the Vitesse VSC9953 L2 Switch
  7. */
  8. #include <asm/io.h>
  9. #include <asm/fsl_serdes.h>
  10. #include <fm_eth.h>
  11. #include <asm/fsl_memac.h>
  12. #include <vsc9953.h>
  13. static struct vsc9953_info vsc9953_l2sw = {
  14. .port[0] = VSC9953_PORT_INFO_INITIALIZER(0),
  15. .port[1] = VSC9953_PORT_INFO_INITIALIZER(1),
  16. .port[2] = VSC9953_PORT_INFO_INITIALIZER(2),
  17. .port[3] = VSC9953_PORT_INFO_INITIALIZER(3),
  18. .port[4] = VSC9953_PORT_INFO_INITIALIZER(4),
  19. .port[5] = VSC9953_PORT_INFO_INITIALIZER(5),
  20. .port[6] = VSC9953_PORT_INFO_INITIALIZER(6),
  21. .port[7] = VSC9953_PORT_INFO_INITIALIZER(7),
  22. .port[8] = VSC9953_PORT_INFO_INITIALIZER(8),
  23. .port[9] = VSC9953_PORT_INFO_INITIALIZER(9),
  24. };
  25. void vsc9953_port_info_set_mdio(int port, struct mii_dev *bus)
  26. {
  27. if (!VSC9953_PORT_CHECK(port))
  28. return;
  29. vsc9953_l2sw.port[port].bus = bus;
  30. }
  31. void vsc9953_port_info_set_phy_address(int port, int address)
  32. {
  33. if (!VSC9953_PORT_CHECK(port))
  34. return;
  35. vsc9953_l2sw.port[port].phyaddr = address;
  36. }
  37. void vsc9953_port_info_set_phy_int(int port, phy_interface_t phy_int)
  38. {
  39. if (!VSC9953_PORT_CHECK(port))
  40. return;
  41. vsc9953_l2sw.port[port].enet_if = phy_int;
  42. }
  43. void vsc9953_port_enable(int port)
  44. {
  45. if (!VSC9953_PORT_CHECK(port))
  46. return;
  47. vsc9953_l2sw.port[port].enabled = 1;
  48. }
  49. void vsc9953_port_disable(int port)
  50. {
  51. if (!VSC9953_PORT_CHECK(port))
  52. return;
  53. vsc9953_l2sw.port[port].enabled = 0;
  54. }
  55. static void vsc9953_mdio_write(struct vsc9953_mii_mng *phyregs, int port_addr,
  56. int regnum, int value)
  57. {
  58. int timeout = 50000;
  59. out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
  60. ((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
  61. (0x1 << 1));
  62. asm("sync");
  63. while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
  64. udelay(1);
  65. if (timeout == 0)
  66. debug("Timeout waiting for MDIO write\n");
  67. }
  68. static int vsc9953_mdio_read(struct vsc9953_mii_mng *phyregs, int port_addr,
  69. int regnum)
  70. {
  71. int value = 0xFFFF;
  72. int timeout = 50000;
  73. while ((in_le32(&phyregs->miimstatus) & MIIMIND_OPR_PEND) && --timeout)
  74. udelay(1);
  75. if (timeout == 0) {
  76. debug("Timeout waiting for MDIO operation to finish\n");
  77. return value;
  78. }
  79. /* Put the address of the phy, and the register
  80. * number into MIICMD
  81. */
  82. out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
  83. ((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
  84. (0x2 << 1));
  85. timeout = 50000;
  86. /* Wait for the the indication that the read is done */
  87. while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
  88. udelay(1);
  89. if (timeout == 0)
  90. debug("Timeout waiting for MDIO read\n");
  91. /* Grab the value read from the PHY */
  92. value = in_le32(&phyregs->miimdata);
  93. if ((value & 0x00030000) == 0)
  94. return value & 0x0000ffff;
  95. return value;
  96. }
  97. static int init_phy(struct eth_device *dev)
  98. {
  99. struct vsc9953_port_info *l2sw_port = dev->priv;
  100. struct phy_device *phydev = NULL;
  101. #ifdef CONFIG_PHYLIB
  102. if (!l2sw_port->bus)
  103. return 0;
  104. phydev = phy_connect(l2sw_port->bus, l2sw_port->phyaddr, dev,
  105. l2sw_port->enet_if);
  106. if (!phydev) {
  107. printf("Failed to connect\n");
  108. return -1;
  109. }
  110. phydev->supported &= SUPPORTED_10baseT_Half |
  111. SUPPORTED_10baseT_Full |
  112. SUPPORTED_100baseT_Half |
  113. SUPPORTED_100baseT_Full |
  114. SUPPORTED_1000baseT_Full;
  115. phydev->advertising = phydev->supported;
  116. l2sw_port->phydev = phydev;
  117. phy_config(phydev);
  118. #endif
  119. return 0;
  120. }
  121. static int vsc9953_port_init(int port)
  122. {
  123. struct eth_device *dev;
  124. /* Internal ports never have a PHY */
  125. if (VSC9953_INTERNAL_PORT_CHECK(port))
  126. return 0;
  127. /* alloc eth device */
  128. dev = (struct eth_device *)calloc(1, sizeof(struct eth_device));
  129. if (!dev)
  130. return 1;
  131. sprintf(dev->name, "SW@PORT%d", port);
  132. dev->priv = &vsc9953_l2sw.port[port];
  133. dev->init = NULL;
  134. dev->halt = NULL;
  135. dev->send = NULL;
  136. dev->recv = NULL;
  137. if (init_phy(dev)) {
  138. free(dev);
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. void vsc9953_init(bd_t *bis)
  144. {
  145. u32 i, hdx_cfg = 0, phy_addr = 0;
  146. int timeout;
  147. struct vsc9953_system_reg *l2sys_reg;
  148. struct vsc9953_qsys_reg *l2qsys_reg;
  149. struct vsc9953_dev_gmii *l2dev_gmii_reg;
  150. struct vsc9953_analyzer *l2ana_reg;
  151. struct vsc9953_devcpu_gcb *l2dev_gcb;
  152. l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(VSC9953_OFFSET +
  153. VSC9953_DEV_GMII_OFFSET);
  154. l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
  155. VSC9953_ANA_OFFSET);
  156. l2sys_reg = (struct vsc9953_system_reg *)(VSC9953_OFFSET +
  157. VSC9953_SYS_OFFSET);
  158. l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
  159. VSC9953_QSYS_OFFSET);
  160. l2dev_gcb = (struct vsc9953_devcpu_gcb *)(VSC9953_OFFSET +
  161. VSC9953_DEVCPU_GCB);
  162. out_le32(&l2dev_gcb->chip_regs.soft_rst,
  163. CONFIG_VSC9953_SOFT_SWC_RST_ENA);
  164. timeout = 50000;
  165. while ((in_le32(&l2dev_gcb->chip_regs.soft_rst) &
  166. CONFIG_VSC9953_SOFT_SWC_RST_ENA) && --timeout)
  167. udelay(1); /* busy wait for vsc9953 soft reset */
  168. if (timeout == 0)
  169. debug("Timeout waiting for VSC9953 to reset\n");
  170. out_le32(&l2sys_reg->sys.reset_cfg, CONFIG_VSC9953_MEM_ENABLE |
  171. CONFIG_VSC9953_MEM_INIT);
  172. timeout = 50000;
  173. while ((in_le32(&l2sys_reg->sys.reset_cfg) &
  174. CONFIG_VSC9953_MEM_INIT) && --timeout)
  175. udelay(1); /* busy wait for vsc9953 memory init */
  176. if (timeout == 0)
  177. debug("Timeout waiting for VSC9953 memory to initialize\n");
  178. out_le32(&l2sys_reg->sys.reset_cfg, (in_le32(&l2sys_reg->sys.reset_cfg)
  179. | CONFIG_VSC9953_CORE_ENABLE));
  180. /* VSC9953 Setting to be done once only */
  181. out_le32(&l2qsys_reg->sys.ext_cpu_cfg, 0x00000b00);
  182. for (i = 0; i < VSC9953_MAX_PORTS; i++) {
  183. if (vsc9953_port_init(i))
  184. printf("Failed to initialize l2switch port %d\n", i);
  185. /* Enable VSC9953 GMII Ports Port ID 0 - 7 */
  186. if (VSC9953_INTERNAL_PORT_CHECK(i)) {
  187. out_le32(&l2ana_reg->pfc[i].pfc_cfg,
  188. CONFIG_VSC9953_PFC_FC_QSGMII);
  189. out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
  190. CONFIG_VSC9953_MAC_FC_CFG_QSGMII);
  191. } else {
  192. out_le32(&l2ana_reg->pfc[i].pfc_cfg,
  193. CONFIG_VSC9953_PFC_FC);
  194. out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
  195. CONFIG_VSC9953_MAC_FC_CFG);
  196. }
  197. out_le32(&l2dev_gmii_reg->port_mode.clock_cfg,
  198. CONFIG_VSC9953_CLOCK_CFG);
  199. out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ena_cfg,
  200. CONFIG_VSC9953_MAC_ENA_CFG);
  201. out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_mode_cfg,
  202. CONFIG_VSC9953_MAC_MODE_CFG);
  203. out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ifg_cfg,
  204. CONFIG_VSC9953_MAC_IFG_CFG);
  205. /* mac_hdx_cfg varies with port id*/
  206. hdx_cfg = (CONFIG_VSC9953_MAC_HDX_CFG | (i << 16));
  207. out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_hdx_cfg, hdx_cfg);
  208. out_le32(&l2sys_reg->sys.front_port_mode[i],
  209. CONFIG_VSC9953_FRONT_PORT_MODE);
  210. out_le32(&l2qsys_reg->sys.switch_port_mode[i],
  211. CONFIG_VSC9953_PORT_ENA);
  212. out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_maxlen_cfg,
  213. CONFIG_VSC9953_MAC_MAX_LEN);
  214. out_le32(&l2sys_reg->pause_cfg.pause_cfg[i],
  215. CONFIG_VSC9953_PAUSE_CFG);
  216. /* WAIT FOR 2 us*/
  217. udelay(2);
  218. l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(
  219. (char *)l2dev_gmii_reg
  220. + T1040_SWITCH_GMII_DEV_OFFSET);
  221. /* Initialize Lynx PHY Wrappers */
  222. phy_addr = 0;
  223. if (vsc9953_l2sw.port[i].enet_if ==
  224. PHY_INTERFACE_MODE_QSGMII)
  225. phy_addr = (i + 0x4) & 0x1F;
  226. else if (vsc9953_l2sw.port[i].enet_if ==
  227. PHY_INTERFACE_MODE_SGMII)
  228. phy_addr = (i + 1) & 0x1F;
  229. if (phy_addr) {
  230. /* SGMII IF mode + AN enable */
  231. vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
  232. 0x14, PHY_SGMII_IF_MODE_AN |
  233. PHY_SGMII_IF_MODE_SGMII);
  234. /* Dev ability according to SGMII specification */
  235. vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
  236. 0x4, PHY_SGMII_DEV_ABILITY_SGMII);
  237. /* Adjust link timer for SGMII
  238. * 1.6 ms in units of 8 ns = 2 * 10^5 = 0x30d40
  239. */
  240. vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
  241. 0x13, 0x0003);
  242. vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
  243. 0x12, 0x0d40);
  244. /* Restart AN */
  245. vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
  246. 0x0, PHY_SGMII_CR_DEF_VAL |
  247. PHY_SGMII_CR_RESET_AN);
  248. timeout = 50000;
  249. while ((vsc9953_mdio_read(&l2dev_gcb->mii_mng[0],
  250. phy_addr, 0x01) & 0x0020) && --timeout)
  251. udelay(1); /* wait for AN to complete */
  252. if (timeout == 0)
  253. debug("Timeout waiting for AN to complete\n");
  254. }
  255. }
  256. printf("VSC9953 L2 switch initialized\n");
  257. return;
  258. }
  259. #ifdef CONFIG_VSC9953_CMD
  260. /* Enable/disable status of a VSC9953 port */
  261. static void vsc9953_port_status_set(int port_nr, u8 enabled)
  262. {
  263. u32 val;
  264. struct vsc9953_qsys_reg *l2qsys_reg;
  265. /* Administrative down */
  266. if (vsc9953_l2sw.port[port_nr].enabled == 0)
  267. return;
  268. l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
  269. VSC9953_QSYS_OFFSET);
  270. val = in_le32(&l2qsys_reg->sys.switch_port_mode[port_nr]);
  271. if (enabled == 1)
  272. val |= (1 << 13);
  273. else
  274. val &= ~(1 << 13);
  275. out_le32(&l2qsys_reg->sys.switch_port_mode[port_nr], val);
  276. }
  277. /* Set all VSC9953 ports' status */
  278. static void vsc9953_port_all_status_set(u8 enabled)
  279. {
  280. int i;
  281. for (i = 0; i < VSC9953_MAX_PORTS; i++)
  282. vsc9953_port_status_set(i, enabled);
  283. }
  284. /* Start autonegotiation for a VSC9953 PHY */
  285. static void vsc9953_phy_autoneg(int port_nr)
  286. {
  287. if (!vsc9953_l2sw.port[port_nr].phydev)
  288. return;
  289. if (vsc9953_l2sw.port[port_nr].phydev->drv->startup(
  290. vsc9953_l2sw.port[port_nr].phydev))
  291. printf("Failed to start PHY for port %d\n", port_nr);
  292. }
  293. /* Start autonegotiation for all VSC9953 PHYs */
  294. static void vsc9953_phy_all_autoneg(void)
  295. {
  296. int i;
  297. for (i = 0; i < VSC9953_MAX_PORTS; i++)
  298. vsc9953_phy_autoneg(i);
  299. }
  300. /* Print a VSC9953 port's configuration */
  301. static void vsc9953_port_config_show(int port)
  302. {
  303. int speed;
  304. int duplex;
  305. int link;
  306. u8 enabled;
  307. u32 val;
  308. struct vsc9953_qsys_reg *l2qsys_reg;
  309. l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
  310. VSC9953_QSYS_OFFSET);
  311. val = in_le32(&l2qsys_reg->sys.switch_port_mode[port]);
  312. enabled = vsc9953_l2sw.port[port].enabled &
  313. ((val & 0x00002000) >> 13);
  314. /* internal ports (8 and 9) are fixed */
  315. if (VSC9953_INTERNAL_PORT_CHECK(port)) {
  316. link = 1;
  317. speed = SPEED_2500;
  318. duplex = DUPLEX_FULL;
  319. } else {
  320. if (vsc9953_l2sw.port[port].phydev) {
  321. link = vsc9953_l2sw.port[port].phydev->link;
  322. speed = vsc9953_l2sw.port[port].phydev->speed;
  323. duplex = vsc9953_l2sw.port[port].phydev->duplex;
  324. } else {
  325. link = -1;
  326. speed = -1;
  327. duplex = -1;
  328. }
  329. }
  330. printf("%8d ", port);
  331. printf("%8s ", enabled == 1 ? "enabled" : "disabled");
  332. printf("%8s ", link == 1 ? "up" : "down");
  333. switch (speed) {
  334. case SPEED_10:
  335. printf("%8d ", 10);
  336. break;
  337. case SPEED_100:
  338. printf("%8d ", 100);
  339. break;
  340. case SPEED_1000:
  341. printf("%8d ", 1000);
  342. break;
  343. case SPEED_2500:
  344. printf("%8d ", 2500);
  345. break;
  346. case SPEED_10000:
  347. printf("%8d ", 10000);
  348. break;
  349. default:
  350. printf("%8s ", "-");
  351. }
  352. printf("%8s\n", duplex == DUPLEX_FULL ? "full" : "half");
  353. }
  354. /* Print VSC9953 ports' configuration */
  355. static void vsc9953_port_all_config_show(void)
  356. {
  357. int i;
  358. for (i = 0; i < VSC9953_MAX_PORTS; i++)
  359. vsc9953_port_config_show(i);
  360. }
  361. /* function to interpret commands starting with "ethsw " */
  362. static int do_ethsw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  363. {
  364. u8 enable;
  365. u32 port;
  366. if (argc < 4)
  367. return -1;
  368. if (strcmp(argv[1], "port"))
  369. return -1;
  370. if (!strcmp(argv[3], "show")) {
  371. if (!strcmp(argv[2], "all")) {
  372. vsc9953_phy_all_autoneg();
  373. printf("%8s %8s %8s %8s %8s\n",
  374. "Port", "Status", "Link", "Speed",
  375. "Duplex");
  376. vsc9953_port_all_config_show();
  377. return 0;
  378. } else {
  379. port = simple_strtoul(argv[2], NULL, 10);
  380. if (!VSC9953_PORT_CHECK(port))
  381. return -1;
  382. vsc9953_phy_autoneg(port);
  383. printf("%8s %8s %8s %8s %8s\n",
  384. "Port", "Status", "Link", "Speed",
  385. "Duplex");
  386. vsc9953_port_config_show(port);
  387. return 0;
  388. }
  389. } else if (!strcmp(argv[3], "enable")) {
  390. enable = 1;
  391. } else if (!strcmp(argv[3], "disable")) {
  392. enable = 0;
  393. } else {
  394. return -1;
  395. }
  396. if (!strcmp(argv[2], "all")) {
  397. vsc9953_port_all_status_set(enable);
  398. return 0;
  399. } else {
  400. port = simple_strtoul(argv[2], NULL, 10);
  401. if (!VSC9953_PORT_CHECK(port))
  402. return -1;
  403. vsc9953_port_status_set(port, enable);
  404. return 0;
  405. }
  406. return -1;
  407. }
  408. U_BOOT_CMD(ethsw, 5, 0, do_ethsw,
  409. "vsc9953 l2 switch commands",
  410. "port <port_nr> enable|disable\n"
  411. " - enable/disable an l2 switch port\n"
  412. " port_nr=0..9; use \"all\" for all ports\n"
  413. "ethsw port <port_nr> show\n"
  414. " - show an l2 switch port's configuration\n"
  415. " port_nr=0..9; use \"all\" for all ports\n"
  416. );
  417. #endif /* CONFIG_VSC9953_CMD */