qt202x_phy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2006-2009 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. /*
  10. * Driver for AMCC QT202x SFP+ and XFP adapters; see www.amcc.com for details
  11. */
  12. #include <linux/timer.h>
  13. #include <linux/delay.h>
  14. #include "efx.h"
  15. #include "mdio_10g.h"
  16. #include "phy.h"
  17. #include "nic.h"
  18. #define QT202X_REQUIRED_DEVS (MDIO_DEVS_PCS | \
  19. MDIO_DEVS_PMAPMD | \
  20. MDIO_DEVS_PHYXS)
  21. #define QT202X_LOOPBACKS ((1 << LOOPBACK_PCS) | \
  22. (1 << LOOPBACK_PMAPMD) | \
  23. (1 << LOOPBACK_PHYXS_WS))
  24. /****************************************************************************/
  25. /* Quake-specific MDIO registers */
  26. #define MDIO_QUAKE_LED0_REG (0xD006)
  27. /* QT2025C only */
  28. #define PCS_FW_HEARTBEAT_REG 0xd7ee
  29. #define PCS_FW_HEARTB_LBN 0
  30. #define PCS_FW_HEARTB_WIDTH 8
  31. #define PCS_FW_PRODUCT_CODE_1 0xd7f0
  32. #define PCS_FW_VERSION_1 0xd7f3
  33. #define PCS_FW_BUILD_1 0xd7f6
  34. #define PCS_UC8051_STATUS_REG 0xd7fd
  35. #define PCS_UC_STATUS_LBN 0
  36. #define PCS_UC_STATUS_WIDTH 8
  37. #define PCS_UC_STATUS_FW_SAVE 0x20
  38. #define PMA_PMD_FTX_CTRL2_REG 0xc309
  39. #define PMA_PMD_FTX_STATIC_LBN 13
  40. #define PMA_PMD_VEND1_REG 0xc001
  41. #define PMA_PMD_VEND1_LBTXD_LBN 15
  42. #define PCS_VEND1_REG 0xc000
  43. #define PCS_VEND1_LBTXD_LBN 5
  44. void falcon_qt202x_set_led(struct efx_nic *p, int led, int mode)
  45. {
  46. int addr = MDIO_QUAKE_LED0_REG + led;
  47. efx_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
  48. }
  49. struct qt202x_phy_data {
  50. enum efx_phy_mode phy_mode;
  51. bool bug17190_in_bad_state;
  52. unsigned long bug17190_timer;
  53. u32 firmware_ver;
  54. };
  55. #define QT2022C2_MAX_RESET_TIME 500
  56. #define QT2022C2_RESET_WAIT 10
  57. #define BUG17190_INTERVAL (2 * HZ)
  58. static int qt2025c_wait_reset(struct efx_nic *efx)
  59. {
  60. unsigned long timeout = jiffies + 10 * HZ;
  61. int reg, old_counter = 0;
  62. /* Wait for firmware heartbeat to start */
  63. for (;;) {
  64. int counter;
  65. reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
  66. if (reg < 0)
  67. return reg;
  68. counter = ((reg >> PCS_FW_HEARTB_LBN) &
  69. ((1 << PCS_FW_HEARTB_WIDTH) - 1));
  70. if (old_counter == 0)
  71. old_counter = counter;
  72. else if (counter != old_counter)
  73. break;
  74. if (time_after(jiffies, timeout))
  75. return -ETIMEDOUT;
  76. msleep(10);
  77. }
  78. /* Wait for firmware status to look good */
  79. for (;;) {
  80. reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
  81. if (reg < 0)
  82. return reg;
  83. if ((reg &
  84. ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
  85. PCS_UC_STATUS_FW_SAVE)
  86. break;
  87. if (time_after(jiffies, timeout))
  88. return -ETIMEDOUT;
  89. msleep(100);
  90. }
  91. return 0;
  92. }
  93. static void qt2025c_firmware_id(struct efx_nic *efx)
  94. {
  95. struct qt202x_phy_data *phy_data = efx->phy_data;
  96. u8 firmware_id[9];
  97. size_t i;
  98. for (i = 0; i < sizeof(firmware_id); i++)
  99. firmware_id[i] = efx_mdio_read(efx, MDIO_MMD_PCS,
  100. PCS_FW_PRODUCT_CODE_1 + i);
  101. EFX_INFO(efx, "QT2025C firmware %xr%d v%d.%d.%d.%d [20%02d-%02d-%02d]\n",
  102. (firmware_id[0] << 8) | firmware_id[1], firmware_id[2],
  103. firmware_id[3] >> 4, firmware_id[3] & 0xf,
  104. firmware_id[4], firmware_id[5],
  105. firmware_id[6], firmware_id[7], firmware_id[8]);
  106. phy_data->firmware_ver = ((firmware_id[3] & 0xf0) << 20) |
  107. ((firmware_id[3] & 0x0f) << 16) |
  108. (firmware_id[4] << 8) | firmware_id[5];
  109. }
  110. static void qt2025c_bug17190_workaround(struct efx_nic *efx)
  111. {
  112. struct qt202x_phy_data *phy_data = efx->phy_data;
  113. /* The PHY can get stuck in a state where it reports PHY_XS and PMA/PMD
  114. * layers up, but PCS down (no block_lock). If we notice this state
  115. * persisting for a couple of seconds, we switch PMA/PMD loopback
  116. * briefly on and then off again, which is normally sufficient to
  117. * recover it.
  118. */
  119. if (efx->link_state.up ||
  120. !efx_mdio_links_ok(efx, MDIO_DEVS_PMAPMD | MDIO_DEVS_PHYXS)) {
  121. phy_data->bug17190_in_bad_state = false;
  122. return;
  123. }
  124. if (!phy_data->bug17190_in_bad_state) {
  125. phy_data->bug17190_in_bad_state = true;
  126. phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
  127. return;
  128. }
  129. if (time_after_eq(jiffies, phy_data->bug17190_timer)) {
  130. EFX_LOG(efx, "bashing QT2025C PMA/PMD\n");
  131. efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
  132. MDIO_PMA_CTRL1_LOOPBACK, true);
  133. msleep(100);
  134. efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
  135. MDIO_PMA_CTRL1_LOOPBACK, false);
  136. phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
  137. }
  138. }
  139. static int qt2025c_select_phy_mode(struct efx_nic *efx)
  140. {
  141. struct qt202x_phy_data *phy_data = efx->phy_data;
  142. struct falcon_board *board = falcon_board(efx);
  143. int reg, rc, i;
  144. uint16_t phy_op_mode;
  145. /* Only 2.0.1.0+ PHY firmware supports the more optimal SFP+
  146. * Self-Configure mode. Don't attempt any switching if we encounter
  147. * older firmware. */
  148. if (phy_data->firmware_ver < 0x02000100)
  149. return 0;
  150. /* In general we will get optimal behaviour in "SFP+ Self-Configure"
  151. * mode; however, that powers down most of the PHY when no module is
  152. * present, so we must use a different mode (any fixed mode will do)
  153. * to be sure that loopbacks will work. */
  154. phy_op_mode = (efx->loopback_mode == LOOPBACK_NONE) ? 0x0038 : 0x0020;
  155. /* Only change mode if really necessary */
  156. reg = efx_mdio_read(efx, 1, 0xc319);
  157. if ((reg & 0x0038) == phy_op_mode)
  158. return 0;
  159. EFX_LOG(efx, "Switching PHY to mode 0x%04x\n", phy_op_mode);
  160. /* This sequence replicates the register writes configured in the boot
  161. * EEPROM (including the differences between board revisions), except
  162. * that the operating mode is changed, and the PHY is prevented from
  163. * unnecessarily reloading the main firmware image again. */
  164. efx_mdio_write(efx, 1, 0xc300, 0x0000);
  165. /* (Note: this portion of the boot EEPROM sequence, which bit-bashes 9
  166. * STOPs onto the firmware/module I2C bus to reset it, varies across
  167. * board revisions, as the bus is connected to different GPIO/LED
  168. * outputs on the PHY.) */
  169. if (board->major == 0 && board->minor < 2) {
  170. efx_mdio_write(efx, 1, 0xc303, 0x4498);
  171. for (i = 0; i < 9; i++) {
  172. efx_mdio_write(efx, 1, 0xc303, 0x4488);
  173. efx_mdio_write(efx, 1, 0xc303, 0x4480);
  174. efx_mdio_write(efx, 1, 0xc303, 0x4490);
  175. efx_mdio_write(efx, 1, 0xc303, 0x4498);
  176. }
  177. } else {
  178. efx_mdio_write(efx, 1, 0xc303, 0x0920);
  179. efx_mdio_write(efx, 1, 0xd008, 0x0004);
  180. for (i = 0; i < 9; i++) {
  181. efx_mdio_write(efx, 1, 0xc303, 0x0900);
  182. efx_mdio_write(efx, 1, 0xd008, 0x0005);
  183. efx_mdio_write(efx, 1, 0xc303, 0x0920);
  184. efx_mdio_write(efx, 1, 0xd008, 0x0004);
  185. }
  186. efx_mdio_write(efx, 1, 0xc303, 0x4900);
  187. }
  188. efx_mdio_write(efx, 1, 0xc303, 0x4900);
  189. efx_mdio_write(efx, 1, 0xc302, 0x0004);
  190. efx_mdio_write(efx, 1, 0xc316, 0x0013);
  191. efx_mdio_write(efx, 1, 0xc318, 0x0054);
  192. efx_mdio_write(efx, 1, 0xc319, phy_op_mode);
  193. efx_mdio_write(efx, 1, 0xc31a, 0x0098);
  194. efx_mdio_write(efx, 3, 0x0026, 0x0e00);
  195. efx_mdio_write(efx, 3, 0x0027, 0x0013);
  196. efx_mdio_write(efx, 3, 0x0028, 0xa528);
  197. efx_mdio_write(efx, 1, 0xd006, 0x000a);
  198. efx_mdio_write(efx, 1, 0xd007, 0x0009);
  199. efx_mdio_write(efx, 1, 0xd008, 0x0004);
  200. /* This additional write is not present in the boot EEPROM. It
  201. * prevents the PHY's internal boot ROM doing another pointless (and
  202. * slow) reload of the firmware image (the microcontroller's code
  203. * memory is not affected by the microcontroller reset). */
  204. efx_mdio_write(efx, 1, 0xc317, 0x00ff);
  205. efx_mdio_write(efx, 1, 0xc300, 0x0002);
  206. msleep(20);
  207. /* Restart microcontroller execution from RAM */
  208. efx_mdio_write(efx, 3, 0xe854, 0x00c0);
  209. efx_mdio_write(efx, 3, 0xe854, 0x0040);
  210. msleep(50);
  211. /* Wait for the microcontroller to be ready again */
  212. rc = qt2025c_wait_reset(efx);
  213. if (rc < 0) {
  214. EFX_ERR(efx, "PHY microcontroller reset during mode switch "
  215. "timed out\n");
  216. return rc;
  217. }
  218. return 0;
  219. }
  220. static int qt202x_reset_phy(struct efx_nic *efx)
  221. {
  222. int rc;
  223. if (efx->phy_type == PHY_TYPE_QT2025C) {
  224. /* Wait for the reset triggered by falcon_reset_hw()
  225. * to complete */
  226. rc = qt2025c_wait_reset(efx);
  227. if (rc < 0)
  228. goto fail;
  229. } else {
  230. /* Reset the PHYXS MMD. This is documented as doing
  231. * a complete soft reset. */
  232. rc = efx_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
  233. QT2022C2_MAX_RESET_TIME /
  234. QT2022C2_RESET_WAIT,
  235. QT2022C2_RESET_WAIT);
  236. if (rc < 0)
  237. goto fail;
  238. }
  239. /* Wait 250ms for the PHY to complete bootup */
  240. msleep(250);
  241. /* Check that all the MMDs we expect are present and responding. We
  242. * expect faults on some if the link is down, but not on the PHY XS */
  243. rc = efx_mdio_check_mmds(efx, QT202X_REQUIRED_DEVS, MDIO_DEVS_PHYXS);
  244. if (rc < 0)
  245. goto fail;
  246. falcon_board(efx)->type->init_phy(efx);
  247. return rc;
  248. fail:
  249. EFX_ERR(efx, "PHY reset timed out\n");
  250. return rc;
  251. }
  252. static int qt202x_phy_probe(struct efx_nic *efx)
  253. {
  254. struct qt202x_phy_data *phy_data;
  255. phy_data = kzalloc(sizeof(struct qt202x_phy_data), GFP_KERNEL);
  256. if (!phy_data)
  257. return -ENOMEM;
  258. efx->phy_data = phy_data;
  259. phy_data->phy_mode = efx->phy_mode;
  260. phy_data->bug17190_in_bad_state = false;
  261. phy_data->bug17190_timer = 0;
  262. efx->mdio.mmds = QT202X_REQUIRED_DEVS;
  263. efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  264. efx->loopback_modes = QT202X_LOOPBACKS | FALCON_XMAC_LOOPBACKS;
  265. return 0;
  266. }
  267. static int qt202x_phy_init(struct efx_nic *efx)
  268. {
  269. u32 devid;
  270. int rc;
  271. rc = qt202x_reset_phy(efx);
  272. if (rc) {
  273. EFX_ERR(efx, "PHY init failed\n");
  274. return rc;
  275. }
  276. devid = efx_mdio_read_id(efx, MDIO_MMD_PHYXS);
  277. EFX_INFO(efx, "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
  278. devid, efx_mdio_id_oui(devid), efx_mdio_id_model(devid),
  279. efx_mdio_id_rev(devid));
  280. if (efx->phy_type == PHY_TYPE_QT2025C)
  281. qt2025c_firmware_id(efx);
  282. return 0;
  283. }
  284. static int qt202x_link_ok(struct efx_nic *efx)
  285. {
  286. return efx_mdio_links_ok(efx, QT202X_REQUIRED_DEVS);
  287. }
  288. static bool qt202x_phy_poll(struct efx_nic *efx)
  289. {
  290. bool was_up = efx->link_state.up;
  291. efx->link_state.up = qt202x_link_ok(efx);
  292. efx->link_state.speed = 10000;
  293. efx->link_state.fd = true;
  294. efx->link_state.fc = efx->wanted_fc;
  295. if (efx->phy_type == PHY_TYPE_QT2025C)
  296. qt2025c_bug17190_workaround(efx);
  297. return efx->link_state.up != was_up;
  298. }
  299. static int qt202x_phy_reconfigure(struct efx_nic *efx)
  300. {
  301. struct qt202x_phy_data *phy_data = efx->phy_data;
  302. if (efx->phy_type == PHY_TYPE_QT2025C) {
  303. int rc = qt2025c_select_phy_mode(efx);
  304. if (rc)
  305. return rc;
  306. /* There are several different register bits which can
  307. * disable TX (and save power) on direct-attach cables
  308. * or optical transceivers, varying somewhat between
  309. * firmware versions. Only 'static mode' appears to
  310. * cover everything. */
  311. mdio_set_flag(
  312. &efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
  313. PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
  314. efx->phy_mode & PHY_MODE_TX_DISABLED ||
  315. efx->phy_mode & PHY_MODE_LOW_POWER ||
  316. efx->loopback_mode == LOOPBACK_PCS ||
  317. efx->loopback_mode == LOOPBACK_PMAPMD);
  318. } else {
  319. /* Reset the PHY when moving from tx off to tx on */
  320. if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
  321. (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
  322. qt202x_reset_phy(efx);
  323. efx_mdio_transmit_disable(efx);
  324. }
  325. efx_mdio_phy_reconfigure(efx);
  326. phy_data->phy_mode = efx->phy_mode;
  327. return 0;
  328. }
  329. static void qt202x_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  330. {
  331. mdio45_ethtool_gset(&efx->mdio, ecmd);
  332. }
  333. static void qt202x_phy_remove(struct efx_nic *efx)
  334. {
  335. /* Free the context block */
  336. kfree(efx->phy_data);
  337. efx->phy_data = NULL;
  338. }
  339. struct efx_phy_operations falcon_qt202x_phy_ops = {
  340. .probe = qt202x_phy_probe,
  341. .init = qt202x_phy_init,
  342. .reconfigure = qt202x_phy_reconfigure,
  343. .poll = qt202x_phy_poll,
  344. .fini = efx_port_dummy_op_void,
  345. .remove = qt202x_phy_remove,
  346. .get_settings = qt202x_phy_get_settings,
  347. .set_settings = efx_mdio_set_settings,
  348. };