mv88e1xxx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* $Date: 2005/10/24 23:18:13 $ $RCSfile: mv88e1xxx.c,v $ $Revision: 1.49 $ */
  2. #include "common.h"
  3. #include "mv88e1xxx.h"
  4. #include "cphy.h"
  5. #include "elmer0.h"
  6. /* MV88E1XXX MDI crossover register values */
  7. #define CROSSOVER_MDI 0
  8. #define CROSSOVER_MDIX 1
  9. #define CROSSOVER_AUTO 3
  10. #define INTR_ENABLE_MASK 0x6CA0
  11. /*
  12. * Set the bits given by 'bitval' in PHY register 'reg'.
  13. */
  14. static void mdio_set_bit(struct cphy *cphy, int reg, u32 bitval)
  15. {
  16. u32 val;
  17. (void) simple_mdio_read(cphy, reg, &val);
  18. (void) simple_mdio_write(cphy, reg, val | bitval);
  19. }
  20. /*
  21. * Clear the bits given by 'bitval' in PHY register 'reg'.
  22. */
  23. static void mdio_clear_bit(struct cphy *cphy, int reg, u32 bitval)
  24. {
  25. u32 val;
  26. (void) simple_mdio_read(cphy, reg, &val);
  27. (void) simple_mdio_write(cphy, reg, val & ~bitval);
  28. }
  29. /*
  30. * NAME: phy_reset
  31. *
  32. * DESC: Reset the given PHY's port. NOTE: This is not a global
  33. * chip reset.
  34. *
  35. * PARAMS: cphy - Pointer to PHY instance data.
  36. *
  37. * RETURN: 0 - Successful reset.
  38. * -1 - Timeout.
  39. */
  40. static int mv88e1xxx_reset(struct cphy *cphy, int wait)
  41. {
  42. u32 ctl;
  43. int time_out = 1000;
  44. mdio_set_bit(cphy, MII_BMCR, BMCR_RESET);
  45. do {
  46. (void) simple_mdio_read(cphy, MII_BMCR, &ctl);
  47. ctl &= BMCR_RESET;
  48. if (ctl)
  49. udelay(1);
  50. } while (ctl && --time_out);
  51. return ctl ? -1 : 0;
  52. }
  53. static int mv88e1xxx_interrupt_enable(struct cphy *cphy)
  54. {
  55. /* Enable PHY interrupts. */
  56. (void) simple_mdio_write(cphy, MV88E1XXX_INTERRUPT_ENABLE_REGISTER,
  57. INTR_ENABLE_MASK);
  58. /* Enable Marvell interrupts through Elmer0. */
  59. if (t1_is_asic(cphy->adapter)) {
  60. u32 elmer;
  61. t1_tpi_read(cphy->adapter, A_ELMER0_INT_ENABLE, &elmer);
  62. elmer |= ELMER0_GP_BIT1;
  63. if (is_T2(cphy->adapter))
  64. elmer |= ELMER0_GP_BIT2 | ELMER0_GP_BIT3 | ELMER0_GP_BIT4;
  65. t1_tpi_write(cphy->adapter, A_ELMER0_INT_ENABLE, elmer);
  66. }
  67. return 0;
  68. }
  69. static int mv88e1xxx_interrupt_disable(struct cphy *cphy)
  70. {
  71. /* Disable all phy interrupts. */
  72. (void) simple_mdio_write(cphy, MV88E1XXX_INTERRUPT_ENABLE_REGISTER, 0);
  73. /* Disable Marvell interrupts through Elmer0. */
  74. if (t1_is_asic(cphy->adapter)) {
  75. u32 elmer;
  76. t1_tpi_read(cphy->adapter, A_ELMER0_INT_ENABLE, &elmer);
  77. elmer &= ~ELMER0_GP_BIT1;
  78. if (is_T2(cphy->adapter))
  79. elmer &= ~(ELMER0_GP_BIT2|ELMER0_GP_BIT3|ELMER0_GP_BIT4);
  80. t1_tpi_write(cphy->adapter, A_ELMER0_INT_ENABLE, elmer);
  81. }
  82. return 0;
  83. }
  84. static int mv88e1xxx_interrupt_clear(struct cphy *cphy)
  85. {
  86. u32 elmer;
  87. /* Clear PHY interrupts by reading the register. */
  88. (void) simple_mdio_read(cphy,
  89. MV88E1XXX_INTERRUPT_STATUS_REGISTER, &elmer);
  90. /* Clear Marvell interrupts through Elmer0. */
  91. if (t1_is_asic(cphy->adapter)) {
  92. t1_tpi_read(cphy->adapter, A_ELMER0_INT_CAUSE, &elmer);
  93. elmer |= ELMER0_GP_BIT1;
  94. if (is_T2(cphy->adapter))
  95. elmer |= ELMER0_GP_BIT2|ELMER0_GP_BIT3|ELMER0_GP_BIT4;
  96. t1_tpi_write(cphy->adapter, A_ELMER0_INT_CAUSE, elmer);
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Set the PHY speed and duplex. This also disables auto-negotiation, except
  102. * for 1Gb/s, where auto-negotiation is mandatory.
  103. */
  104. static int mv88e1xxx_set_speed_duplex(struct cphy *phy, int speed, int duplex)
  105. {
  106. u32 ctl;
  107. (void) simple_mdio_read(phy, MII_BMCR, &ctl);
  108. if (speed >= 0) {
  109. ctl &= ~(BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
  110. if (speed == SPEED_100)
  111. ctl |= BMCR_SPEED100;
  112. else if (speed == SPEED_1000)
  113. ctl |= BMCR_SPEED1000;
  114. }
  115. if (duplex >= 0) {
  116. ctl &= ~(BMCR_FULLDPLX | BMCR_ANENABLE);
  117. if (duplex == DUPLEX_FULL)
  118. ctl |= BMCR_FULLDPLX;
  119. }
  120. if (ctl & BMCR_SPEED1000) /* auto-negotiation required for 1Gb/s */
  121. ctl |= BMCR_ANENABLE;
  122. (void) simple_mdio_write(phy, MII_BMCR, ctl);
  123. return 0;
  124. }
  125. static int mv88e1xxx_crossover_set(struct cphy *cphy, int crossover)
  126. {
  127. u32 data32;
  128. (void) simple_mdio_read(cphy,
  129. MV88E1XXX_SPECIFIC_CNTRL_REGISTER, &data32);
  130. data32 &= ~V_PSCR_MDI_XOVER_MODE(M_PSCR_MDI_XOVER_MODE);
  131. data32 |= V_PSCR_MDI_XOVER_MODE(crossover);
  132. (void) simple_mdio_write(cphy,
  133. MV88E1XXX_SPECIFIC_CNTRL_REGISTER, data32);
  134. return 0;
  135. }
  136. static int mv88e1xxx_autoneg_enable(struct cphy *cphy)
  137. {
  138. u32 ctl;
  139. (void) mv88e1xxx_crossover_set(cphy, CROSSOVER_AUTO);
  140. (void) simple_mdio_read(cphy, MII_BMCR, &ctl);
  141. /* restart autoneg for change to take effect */
  142. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  143. (void) simple_mdio_write(cphy, MII_BMCR, ctl);
  144. return 0;
  145. }
  146. static int mv88e1xxx_autoneg_disable(struct cphy *cphy)
  147. {
  148. u32 ctl;
  149. /*
  150. * Crossover *must* be set to manual in order to disable auto-neg.
  151. * The Alaska FAQs document highlights this point.
  152. */
  153. (void) mv88e1xxx_crossover_set(cphy, CROSSOVER_MDI);
  154. /*
  155. * Must include autoneg reset when disabling auto-neg. This
  156. * is described in the Alaska FAQ document.
  157. */
  158. (void) simple_mdio_read(cphy, MII_BMCR, &ctl);
  159. ctl &= ~BMCR_ANENABLE;
  160. (void) simple_mdio_write(cphy, MII_BMCR, ctl | BMCR_ANRESTART);
  161. return 0;
  162. }
  163. static int mv88e1xxx_autoneg_restart(struct cphy *cphy)
  164. {
  165. mdio_set_bit(cphy, MII_BMCR, BMCR_ANRESTART);
  166. return 0;
  167. }
  168. static int mv88e1xxx_advertise(struct cphy *phy, unsigned int advertise_map)
  169. {
  170. u32 val = 0;
  171. if (advertise_map &
  172. (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
  173. (void) simple_mdio_read(phy, MII_GBCR, &val);
  174. val &= ~(GBCR_ADV_1000HALF | GBCR_ADV_1000FULL);
  175. if (advertise_map & ADVERTISED_1000baseT_Half)
  176. val |= GBCR_ADV_1000HALF;
  177. if (advertise_map & ADVERTISED_1000baseT_Full)
  178. val |= GBCR_ADV_1000FULL;
  179. }
  180. (void) simple_mdio_write(phy, MII_GBCR, val);
  181. val = 1;
  182. if (advertise_map & ADVERTISED_10baseT_Half)
  183. val |= ADVERTISE_10HALF;
  184. if (advertise_map & ADVERTISED_10baseT_Full)
  185. val |= ADVERTISE_10FULL;
  186. if (advertise_map & ADVERTISED_100baseT_Half)
  187. val |= ADVERTISE_100HALF;
  188. if (advertise_map & ADVERTISED_100baseT_Full)
  189. val |= ADVERTISE_100FULL;
  190. if (advertise_map & ADVERTISED_PAUSE)
  191. val |= ADVERTISE_PAUSE;
  192. if (advertise_map & ADVERTISED_ASYM_PAUSE)
  193. val |= ADVERTISE_PAUSE_ASYM;
  194. (void) simple_mdio_write(phy, MII_ADVERTISE, val);
  195. return 0;
  196. }
  197. static int mv88e1xxx_set_loopback(struct cphy *cphy, int on)
  198. {
  199. if (on)
  200. mdio_set_bit(cphy, MII_BMCR, BMCR_LOOPBACK);
  201. else
  202. mdio_clear_bit(cphy, MII_BMCR, BMCR_LOOPBACK);
  203. return 0;
  204. }
  205. static int mv88e1xxx_get_link_status(struct cphy *cphy, int *link_ok,
  206. int *speed, int *duplex, int *fc)
  207. {
  208. u32 status;
  209. int sp = -1, dplx = -1, pause = 0;
  210. (void) simple_mdio_read(cphy,
  211. MV88E1XXX_SPECIFIC_STATUS_REGISTER, &status);
  212. if ((status & V_PSSR_STATUS_RESOLVED) != 0) {
  213. if (status & V_PSSR_RX_PAUSE)
  214. pause |= PAUSE_RX;
  215. if (status & V_PSSR_TX_PAUSE)
  216. pause |= PAUSE_TX;
  217. dplx = (status & V_PSSR_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
  218. sp = G_PSSR_SPEED(status);
  219. if (sp == 0)
  220. sp = SPEED_10;
  221. else if (sp == 1)
  222. sp = SPEED_100;
  223. else
  224. sp = SPEED_1000;
  225. }
  226. if (link_ok)
  227. *link_ok = (status & V_PSSR_LINK) != 0;
  228. if (speed)
  229. *speed = sp;
  230. if (duplex)
  231. *duplex = dplx;
  232. if (fc)
  233. *fc = pause;
  234. return 0;
  235. }
  236. static int mv88e1xxx_downshift_set(struct cphy *cphy, int downshift_enable)
  237. {
  238. u32 val;
  239. (void) simple_mdio_read(cphy,
  240. MV88E1XXX_EXT_PHY_SPECIFIC_CNTRL_REGISTER, &val);
  241. /*
  242. * Set the downshift counter to 2 so we try to establish Gb link
  243. * twice before downshifting.
  244. */
  245. val &= ~(V_DOWNSHIFT_ENABLE | V_DOWNSHIFT_CNT(M_DOWNSHIFT_CNT));
  246. if (downshift_enable)
  247. val |= V_DOWNSHIFT_ENABLE | V_DOWNSHIFT_CNT(2);
  248. (void) simple_mdio_write(cphy,
  249. MV88E1XXX_EXT_PHY_SPECIFIC_CNTRL_REGISTER, val);
  250. return 0;
  251. }
  252. static int mv88e1xxx_interrupt_handler(struct cphy *cphy)
  253. {
  254. int cphy_cause = 0;
  255. u32 status;
  256. /*
  257. * Loop until cause reads zero. Need to handle bouncing interrupts.
  258. */
  259. while (1) {
  260. u32 cause;
  261. (void) simple_mdio_read(cphy,
  262. MV88E1XXX_INTERRUPT_STATUS_REGISTER,
  263. &cause);
  264. cause &= INTR_ENABLE_MASK;
  265. if (!cause)
  266. break;
  267. if (cause & MV88E1XXX_INTR_LINK_CHNG) {
  268. (void) simple_mdio_read(cphy,
  269. MV88E1XXX_SPECIFIC_STATUS_REGISTER, &status);
  270. if (status & MV88E1XXX_INTR_LINK_CHNG)
  271. cphy->state |= PHY_LINK_UP;
  272. else {
  273. cphy->state &= ~PHY_LINK_UP;
  274. if (cphy->state & PHY_AUTONEG_EN)
  275. cphy->state &= ~PHY_AUTONEG_RDY;
  276. cphy_cause |= cphy_cause_link_change;
  277. }
  278. }
  279. if (cause & MV88E1XXX_INTR_AUTONEG_DONE)
  280. cphy->state |= PHY_AUTONEG_RDY;
  281. if ((cphy->state & (PHY_LINK_UP | PHY_AUTONEG_RDY)) ==
  282. (PHY_LINK_UP | PHY_AUTONEG_RDY))
  283. cphy_cause |= cphy_cause_link_change;
  284. }
  285. return cphy_cause;
  286. }
  287. static void mv88e1xxx_destroy(struct cphy *cphy)
  288. {
  289. kfree(cphy);
  290. }
  291. static struct cphy_ops mv88e1xxx_ops = {
  292. .destroy = mv88e1xxx_destroy,
  293. .reset = mv88e1xxx_reset,
  294. .interrupt_enable = mv88e1xxx_interrupt_enable,
  295. .interrupt_disable = mv88e1xxx_interrupt_disable,
  296. .interrupt_clear = mv88e1xxx_interrupt_clear,
  297. .interrupt_handler = mv88e1xxx_interrupt_handler,
  298. .autoneg_enable = mv88e1xxx_autoneg_enable,
  299. .autoneg_disable = mv88e1xxx_autoneg_disable,
  300. .autoneg_restart = mv88e1xxx_autoneg_restart,
  301. .advertise = mv88e1xxx_advertise,
  302. .set_loopback = mv88e1xxx_set_loopback,
  303. .set_speed_duplex = mv88e1xxx_set_speed_duplex,
  304. .get_link_status = mv88e1xxx_get_link_status,
  305. };
  306. static struct cphy *mv88e1xxx_phy_create(struct net_device *dev, int phy_addr,
  307. const struct mdio_ops *mdio_ops)
  308. {
  309. struct adapter *adapter = netdev_priv(dev);
  310. struct cphy *cphy = kzalloc(sizeof(*cphy), GFP_KERNEL);
  311. if (!cphy)
  312. return NULL;
  313. cphy_init(cphy, dev, phy_addr, &mv88e1xxx_ops, mdio_ops);
  314. /* Configure particular PHY's to run in a different mode. */
  315. if ((board_info(adapter)->caps & SUPPORTED_TP) &&
  316. board_info(adapter)->chip_phy == CHBT_PHY_88E1111) {
  317. /*
  318. * Configure the PHY transmitter as class A to reduce EMI.
  319. */
  320. (void) simple_mdio_write(cphy,
  321. MV88E1XXX_EXTENDED_ADDR_REGISTER, 0xB);
  322. (void) simple_mdio_write(cphy,
  323. MV88E1XXX_EXTENDED_REGISTER, 0x8004);
  324. }
  325. (void) mv88e1xxx_downshift_set(cphy, 1); /* Enable downshift */
  326. /* LED */
  327. if (is_T2(adapter)) {
  328. (void) simple_mdio_write(cphy,
  329. MV88E1XXX_LED_CONTROL_REGISTER, 0x1);
  330. }
  331. return cphy;
  332. }
  333. static int mv88e1xxx_phy_reset(adapter_t* adapter)
  334. {
  335. return 0;
  336. }
  337. const struct gphy t1_mv88e1xxx_ops = {
  338. .create = mv88e1xxx_phy_create,
  339. .reset = mv88e1xxx_phy_reset
  340. };