qt202x_phy.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2006-2008 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 "falcon.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_NETWORK))
  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_UC8051_STATUS_REG 0xd7fd
  32. #define PCS_UC_STATUS_LBN 0
  33. #define PCS_UC_STATUS_WIDTH 8
  34. #define PCS_UC_STATUS_FW_SAVE 0x20
  35. #define PMA_PMD_FTX_CTRL2_REG 0xc309
  36. #define PMA_PMD_FTX_STATIC_LBN 13
  37. #define PMA_PMD_VEND1_REG 0xc001
  38. #define PMA_PMD_VEND1_LBTXD_LBN 15
  39. #define PCS_VEND1_REG 0xc000
  40. #define PCS_VEND1_LBTXD_LBN 5
  41. void falcon_qt202x_set_led(struct efx_nic *p, int led, int mode)
  42. {
  43. int addr = MDIO_QUAKE_LED0_REG + led;
  44. efx_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
  45. }
  46. struct qt202x_phy_data {
  47. enum efx_phy_mode phy_mode;
  48. };
  49. #define QT2022C2_MAX_RESET_TIME 500
  50. #define QT2022C2_RESET_WAIT 10
  51. static int qt2025c_wait_reset(struct efx_nic *efx)
  52. {
  53. unsigned long timeout = jiffies + 10 * HZ;
  54. int reg, old_counter = 0;
  55. /* Wait for firmware heartbeat to start */
  56. for (;;) {
  57. int counter;
  58. reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
  59. if (reg < 0)
  60. return reg;
  61. counter = ((reg >> PCS_FW_HEARTB_LBN) &
  62. ((1 << PCS_FW_HEARTB_WIDTH) - 1));
  63. if (old_counter == 0)
  64. old_counter = counter;
  65. else if (counter != old_counter)
  66. break;
  67. if (time_after(jiffies, timeout))
  68. return -ETIMEDOUT;
  69. msleep(10);
  70. }
  71. /* Wait for firmware status to look good */
  72. for (;;) {
  73. reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
  74. if (reg < 0)
  75. return reg;
  76. if ((reg &
  77. ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
  78. PCS_UC_STATUS_FW_SAVE)
  79. break;
  80. if (time_after(jiffies, timeout))
  81. return -ETIMEDOUT;
  82. msleep(100);
  83. }
  84. return 0;
  85. }
  86. static int qt202x_reset_phy(struct efx_nic *efx)
  87. {
  88. int rc;
  89. if (efx->phy_type == PHY_TYPE_QT2025C) {
  90. /* Wait for the reset triggered by falcon_reset_hw()
  91. * to complete */
  92. rc = qt2025c_wait_reset(efx);
  93. if (rc < 0)
  94. goto fail;
  95. } else {
  96. /* Reset the PHYXS MMD. This is documented as doing
  97. * a complete soft reset. */
  98. rc = efx_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
  99. QT2022C2_MAX_RESET_TIME /
  100. QT2022C2_RESET_WAIT,
  101. QT2022C2_RESET_WAIT);
  102. if (rc < 0)
  103. goto fail;
  104. }
  105. /* Wait 250ms for the PHY to complete bootup */
  106. msleep(250);
  107. /* Check that all the MMDs we expect are present and responding. We
  108. * expect faults on some if the link is down, but not on the PHY XS */
  109. rc = efx_mdio_check_mmds(efx, QT202X_REQUIRED_DEVS, MDIO_DEVS_PHYXS);
  110. if (rc < 0)
  111. goto fail;
  112. falcon_board(efx)->type->init_phy(efx);
  113. return rc;
  114. fail:
  115. EFX_ERR(efx, "PHY reset timed out\n");
  116. return rc;
  117. }
  118. static int qt202x_phy_init(struct efx_nic *efx)
  119. {
  120. struct qt202x_phy_data *phy_data;
  121. u32 devid;
  122. int rc;
  123. rc = qt202x_reset_phy(efx);
  124. if (rc) {
  125. EFX_ERR(efx, "PHY init failed\n");
  126. return rc;
  127. }
  128. phy_data = kzalloc(sizeof(struct qt202x_phy_data), GFP_KERNEL);
  129. if (!phy_data)
  130. return -ENOMEM;
  131. efx->phy_data = phy_data;
  132. devid = efx_mdio_read_id(efx, MDIO_MMD_PHYXS);
  133. EFX_INFO(efx, "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
  134. devid, efx_mdio_id_oui(devid), efx_mdio_id_model(devid),
  135. efx_mdio_id_rev(devid));
  136. phy_data->phy_mode = efx->phy_mode;
  137. return 0;
  138. }
  139. static int qt202x_link_ok(struct efx_nic *efx)
  140. {
  141. return efx_mdio_links_ok(efx, QT202X_REQUIRED_DEVS);
  142. }
  143. static bool qt202x_phy_poll(struct efx_nic *efx)
  144. {
  145. bool was_up = efx->link_state.up;
  146. efx->link_state.up = qt202x_link_ok(efx);
  147. efx->link_state.speed = 10000;
  148. efx->link_state.fd = true;
  149. efx->link_state.fc = efx->wanted_fc;
  150. return efx->link_state.up != was_up;
  151. }
  152. static void qt202x_phy_reconfigure(struct efx_nic *efx)
  153. {
  154. struct qt202x_phy_data *phy_data = efx->phy_data;
  155. if (efx->phy_type == PHY_TYPE_QT2025C) {
  156. /* There are several different register bits which can
  157. * disable TX (and save power) on direct-attach cables
  158. * or optical transceivers, varying somewhat between
  159. * firmware versions. Only 'static mode' appears to
  160. * cover everything. */
  161. mdio_set_flag(
  162. &efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
  163. PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
  164. efx->phy_mode & PHY_MODE_TX_DISABLED ||
  165. efx->phy_mode & PHY_MODE_LOW_POWER ||
  166. efx->loopback_mode == LOOPBACK_PCS ||
  167. efx->loopback_mode == LOOPBACK_PMAPMD);
  168. } else {
  169. /* Reset the PHY when moving from tx off to tx on */
  170. if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
  171. (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
  172. qt202x_reset_phy(efx);
  173. efx_mdio_transmit_disable(efx);
  174. }
  175. efx_mdio_phy_reconfigure(efx);
  176. phy_data->phy_mode = efx->phy_mode;
  177. }
  178. static void qt202x_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  179. {
  180. mdio45_ethtool_gset(&efx->mdio, ecmd);
  181. }
  182. static void qt202x_phy_fini(struct efx_nic *efx)
  183. {
  184. /* Free the context block */
  185. kfree(efx->phy_data);
  186. efx->phy_data = NULL;
  187. }
  188. struct efx_phy_operations falcon_qt202x_phy_ops = {
  189. .macs = EFX_XMAC,
  190. .init = qt202x_phy_init,
  191. .reconfigure = qt202x_phy_reconfigure,
  192. .poll = qt202x_phy_poll,
  193. .fini = qt202x_phy_fini,
  194. .get_settings = qt202x_phy_get_settings,
  195. .set_settings = efx_mdio_set_settings,
  196. .mmds = QT202X_REQUIRED_DEVS,
  197. .loopbacks = QT202X_LOOPBACKS,
  198. };