qt202x_phy.c 13 KB

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