qt202x_phy.c 14 KB

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