vsc8211.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2005-2007 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "common.h"
  33. /* VSC8211 PHY specific registers. */
  34. enum {
  35. VSC8211_INTR_ENABLE = 25,
  36. VSC8211_INTR_STATUS = 26,
  37. VSC8211_AUX_CTRL_STAT = 28,
  38. };
  39. enum {
  40. VSC_INTR_RX_ERR = 1 << 0,
  41. VSC_INTR_MS_ERR = 1 << 1, /* master/slave resolution error */
  42. VSC_INTR_CABLE = 1 << 2, /* cable impairment */
  43. VSC_INTR_FALSE_CARR = 1 << 3, /* false carrier */
  44. VSC_INTR_MEDIA_CHG = 1 << 4, /* AMS media change */
  45. VSC_INTR_RX_FIFO = 1 << 5, /* Rx FIFO over/underflow */
  46. VSC_INTR_TX_FIFO = 1 << 6, /* Tx FIFO over/underflow */
  47. VSC_INTR_DESCRAMBL = 1 << 7, /* descrambler lock-lost */
  48. VSC_INTR_SYMBOL_ERR = 1 << 8, /* symbol error */
  49. VSC_INTR_NEG_DONE = 1 << 10, /* autoneg done */
  50. VSC_INTR_NEG_ERR = 1 << 11, /* autoneg error */
  51. VSC_INTR_LINK_CHG = 1 << 13, /* link change */
  52. VSC_INTR_ENABLE = 1 << 15, /* interrupt enable */
  53. };
  54. #define CFG_CHG_INTR_MASK (VSC_INTR_LINK_CHG | VSC_INTR_NEG_ERR | \
  55. VSC_INTR_NEG_DONE)
  56. #define INTR_MASK (CFG_CHG_INTR_MASK | VSC_INTR_TX_FIFO | VSC_INTR_RX_FIFO | \
  57. VSC_INTR_ENABLE)
  58. /* PHY specific auxiliary control & status register fields */
  59. #define S_ACSR_ACTIPHY_TMR 0
  60. #define M_ACSR_ACTIPHY_TMR 0x3
  61. #define V_ACSR_ACTIPHY_TMR(x) ((x) << S_ACSR_ACTIPHY_TMR)
  62. #define S_ACSR_SPEED 3
  63. #define M_ACSR_SPEED 0x3
  64. #define G_ACSR_SPEED(x) (((x) >> S_ACSR_SPEED) & M_ACSR_SPEED)
  65. #define S_ACSR_DUPLEX 5
  66. #define F_ACSR_DUPLEX (1 << S_ACSR_DUPLEX)
  67. #define S_ACSR_ACTIPHY 6
  68. #define F_ACSR_ACTIPHY (1 << S_ACSR_ACTIPHY)
  69. /*
  70. * Reset the PHY. This PHY completes reset immediately so we never wait.
  71. */
  72. static int vsc8211_reset(struct cphy *cphy, int wait)
  73. {
  74. return t3_phy_reset(cphy, 0, 0);
  75. }
  76. static int vsc8211_intr_enable(struct cphy *cphy)
  77. {
  78. return mdio_write(cphy, 0, VSC8211_INTR_ENABLE, INTR_MASK);
  79. }
  80. static int vsc8211_intr_disable(struct cphy *cphy)
  81. {
  82. return mdio_write(cphy, 0, VSC8211_INTR_ENABLE, 0);
  83. }
  84. static int vsc8211_intr_clear(struct cphy *cphy)
  85. {
  86. u32 val;
  87. /* Clear PHY interrupts by reading the register. */
  88. return mdio_read(cphy, 0, VSC8211_INTR_STATUS, &val);
  89. }
  90. static int vsc8211_autoneg_enable(struct cphy *cphy)
  91. {
  92. return t3_mdio_change_bits(cphy, 0, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE,
  93. BMCR_ANENABLE | BMCR_ANRESTART);
  94. }
  95. static int vsc8211_autoneg_restart(struct cphy *cphy)
  96. {
  97. return t3_mdio_change_bits(cphy, 0, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE,
  98. BMCR_ANRESTART);
  99. }
  100. static int vsc8211_get_link_status(struct cphy *cphy, int *link_ok,
  101. int *speed, int *duplex, int *fc)
  102. {
  103. unsigned int bmcr, status, lpa, adv;
  104. int err, sp = -1, dplx = -1, pause = 0;
  105. err = mdio_read(cphy, 0, MII_BMCR, &bmcr);
  106. if (!err)
  107. err = mdio_read(cphy, 0, MII_BMSR, &status);
  108. if (err)
  109. return err;
  110. if (link_ok) {
  111. /*
  112. * BMSR_LSTATUS is latch-low, so if it is 0 we need to read it
  113. * once more to get the current link state.
  114. */
  115. if (!(status & BMSR_LSTATUS))
  116. err = mdio_read(cphy, 0, MII_BMSR, &status);
  117. if (err)
  118. return err;
  119. *link_ok = (status & BMSR_LSTATUS) != 0;
  120. }
  121. if (!(bmcr & BMCR_ANENABLE)) {
  122. dplx = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
  123. if (bmcr & BMCR_SPEED1000)
  124. sp = SPEED_1000;
  125. else if (bmcr & BMCR_SPEED100)
  126. sp = SPEED_100;
  127. else
  128. sp = SPEED_10;
  129. } else if (status & BMSR_ANEGCOMPLETE) {
  130. err = mdio_read(cphy, 0, VSC8211_AUX_CTRL_STAT, &status);
  131. if (err)
  132. return err;
  133. dplx = (status & F_ACSR_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
  134. sp = G_ACSR_SPEED(status);
  135. if (sp == 0)
  136. sp = SPEED_10;
  137. else if (sp == 1)
  138. sp = SPEED_100;
  139. else
  140. sp = SPEED_1000;
  141. if (fc && dplx == DUPLEX_FULL) {
  142. err = mdio_read(cphy, 0, MII_LPA, &lpa);
  143. if (!err)
  144. err = mdio_read(cphy, 0, MII_ADVERTISE, &adv);
  145. if (err)
  146. return err;
  147. if (lpa & adv & ADVERTISE_PAUSE_CAP)
  148. pause = PAUSE_RX | PAUSE_TX;
  149. else if ((lpa & ADVERTISE_PAUSE_CAP) &&
  150. (lpa & ADVERTISE_PAUSE_ASYM) &&
  151. (adv & ADVERTISE_PAUSE_ASYM))
  152. pause = PAUSE_TX;
  153. else if ((lpa & ADVERTISE_PAUSE_ASYM) &&
  154. (adv & ADVERTISE_PAUSE_CAP))
  155. pause = PAUSE_RX;
  156. }
  157. }
  158. if (speed)
  159. *speed = sp;
  160. if (duplex)
  161. *duplex = dplx;
  162. if (fc)
  163. *fc = pause;
  164. return 0;
  165. }
  166. static int vsc8211_power_down(struct cphy *cphy, int enable)
  167. {
  168. return t3_mdio_change_bits(cphy, 0, MII_BMCR, BMCR_PDOWN,
  169. enable ? BMCR_PDOWN : 0);
  170. }
  171. static int vsc8211_intr_handler(struct cphy *cphy)
  172. {
  173. unsigned int cause;
  174. int err, cphy_cause = 0;
  175. err = mdio_read(cphy, 0, VSC8211_INTR_STATUS, &cause);
  176. if (err)
  177. return err;
  178. cause &= INTR_MASK;
  179. if (cause & CFG_CHG_INTR_MASK)
  180. cphy_cause |= cphy_cause_link_change;
  181. if (cause & (VSC_INTR_RX_FIFO | VSC_INTR_TX_FIFO))
  182. cphy_cause |= cphy_cause_fifo_error;
  183. return cphy_cause;
  184. }
  185. static struct cphy_ops vsc8211_ops = {
  186. .reset = vsc8211_reset,
  187. .intr_enable = vsc8211_intr_enable,
  188. .intr_disable = vsc8211_intr_disable,
  189. .intr_clear = vsc8211_intr_clear,
  190. .intr_handler = vsc8211_intr_handler,
  191. .autoneg_enable = vsc8211_autoneg_enable,
  192. .autoneg_restart = vsc8211_autoneg_restart,
  193. .advertise = t3_phy_advertise,
  194. .set_speed_duplex = t3_set_phy_speed_duplex,
  195. .get_link_status = vsc8211_get_link_status,
  196. .power_down = vsc8211_power_down,
  197. };
  198. void t3_vsc8211_phy_prep(struct cphy *phy, struct adapter *adapter,
  199. int phy_addr, const struct mdio_ops *mdio_ops)
  200. {
  201. cphy_init(phy, adapter, phy_addr, &vsc8211_ops, mdio_ops);
  202. }