aq100x.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2005-2008 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. #include "regs.h"
  34. enum {
  35. /* MDIO_DEV_PMA_PMD registers */
  36. AQ_LINK_STAT = 0xe800,
  37. AQ_IMASK_PMA = 0xf000,
  38. /* MDIO_DEV_XGXS registers */
  39. AQ_XAUI_RX_CFG = 0xc400,
  40. AQ_XAUI_TX_CFG = 0xe400,
  41. /* MDIO_DEV_ANEG registers */
  42. AQ_100M_CTRL = 0x0010,
  43. AQ_10G_CTRL = 0x0020,
  44. AQ_1G_CTRL = 0xc400,
  45. AQ_ANEG_STAT = 0xc800,
  46. /* MDIO_DEV_VEND1 registers */
  47. AQ_FW_VERSION = 0x0020,
  48. AQ_IFLAG_GLOBAL = 0xfc00,
  49. AQ_IMASK_GLOBAL = 0xff00,
  50. };
  51. #define AQBIT(x) (1 << (x))
  52. #define IMASK_PMA AQBIT(0x2)
  53. #define IMASK_GLOBAL AQBIT(0xf)
  54. #define ADV_1G_FULL AQBIT(0xf)
  55. #define ADV_1G_HALF AQBIT(0xe)
  56. #define ADV_10G_FULL AQBIT(0xc)
  57. #define AQ_RESET (AQBIT(0xe) | AQBIT(0xf))
  58. #define AQ_LOWPOWER AQBIT(0xb)
  59. static int aq100x_reset(struct cphy *phy, int wait)
  60. {
  61. /*
  62. * Ignore the caller specified wait time; always wait for the reset to
  63. * complete. Can take up to 3s.
  64. */
  65. int err = t3_phy_reset(phy, MDIO_MMD_VEND1, 3000);
  66. if (err)
  67. CH_WARN(phy->adapter, "PHY%d: reset failed (0x%x).\n",
  68. phy->mdio.prtad, err);
  69. return err;
  70. }
  71. static int aq100x_intr_enable(struct cphy *phy)
  72. {
  73. int err = t3_mdio_write(phy, MDIO_MMD_PMAPMD, AQ_IMASK_PMA, IMASK_PMA);
  74. if (err)
  75. return err;
  76. err = t3_mdio_write(phy, MDIO_MMD_VEND1, AQ_IMASK_GLOBAL, IMASK_GLOBAL);
  77. return err;
  78. }
  79. static int aq100x_intr_disable(struct cphy *phy)
  80. {
  81. return t3_mdio_write(phy, MDIO_MMD_VEND1, AQ_IMASK_GLOBAL, 0);
  82. }
  83. static int aq100x_intr_clear(struct cphy *phy)
  84. {
  85. unsigned int v;
  86. t3_mdio_read(phy, MDIO_MMD_VEND1, AQ_IFLAG_GLOBAL, &v);
  87. t3_mdio_read(phy, MDIO_MMD_PMAPMD, MDIO_STAT1, &v);
  88. return 0;
  89. }
  90. static int aq100x_intr_handler(struct cphy *phy)
  91. {
  92. int err;
  93. unsigned int cause, v;
  94. err = t3_mdio_read(phy, MDIO_MMD_VEND1, AQ_IFLAG_GLOBAL, &cause);
  95. if (err)
  96. return err;
  97. /* Read (and reset) the latching version of the status */
  98. t3_mdio_read(phy, MDIO_MMD_PMAPMD, MDIO_STAT1, &v);
  99. return cphy_cause_link_change;
  100. }
  101. static int aq100x_power_down(struct cphy *phy, int off)
  102. {
  103. return mdio_set_flag(&phy->mdio, phy->mdio.prtad,
  104. MDIO_MMD_PMAPMD, MDIO_CTRL1,
  105. MDIO_CTRL1_LPOWER, off);
  106. }
  107. static int aq100x_autoneg_enable(struct cphy *phy)
  108. {
  109. int err;
  110. err = aq100x_power_down(phy, 0);
  111. if (!err)
  112. err = mdio_set_flag(&phy->mdio, phy->mdio.prtad,
  113. MDIO_MMD_AN, MDIO_CTRL1,
  114. BMCR_ANENABLE | BMCR_ANRESTART, 1);
  115. return err;
  116. }
  117. static int aq100x_autoneg_restart(struct cphy *phy)
  118. {
  119. int err;
  120. err = aq100x_power_down(phy, 0);
  121. if (!err)
  122. err = mdio_set_flag(&phy->mdio, phy->mdio.prtad,
  123. MDIO_MMD_AN, MDIO_CTRL1,
  124. BMCR_ANENABLE | BMCR_ANRESTART, 1);
  125. return err;
  126. }
  127. static int aq100x_advertise(struct cphy *phy, unsigned int advertise_map)
  128. {
  129. unsigned int adv;
  130. int err;
  131. /* 10G advertisement */
  132. adv = 0;
  133. if (advertise_map & ADVERTISED_10000baseT_Full)
  134. adv |= ADV_10G_FULL;
  135. err = t3_mdio_change_bits(phy, MDIO_MMD_AN, AQ_10G_CTRL,
  136. ADV_10G_FULL, adv);
  137. if (err)
  138. return err;
  139. /* 1G advertisement */
  140. adv = 0;
  141. if (advertise_map & ADVERTISED_1000baseT_Full)
  142. adv |= ADV_1G_FULL;
  143. if (advertise_map & ADVERTISED_1000baseT_Half)
  144. adv |= ADV_1G_HALF;
  145. err = t3_mdio_change_bits(phy, MDIO_MMD_AN, AQ_1G_CTRL,
  146. ADV_1G_FULL | ADV_1G_HALF, adv);
  147. if (err)
  148. return err;
  149. /* 100M, pause advertisement */
  150. adv = 0;
  151. if (advertise_map & ADVERTISED_100baseT_Half)
  152. adv |= ADVERTISE_100HALF;
  153. if (advertise_map & ADVERTISED_100baseT_Full)
  154. adv |= ADVERTISE_100FULL;
  155. if (advertise_map & ADVERTISED_Pause)
  156. adv |= ADVERTISE_PAUSE_CAP;
  157. if (advertise_map & ADVERTISED_Asym_Pause)
  158. adv |= ADVERTISE_PAUSE_ASYM;
  159. err = t3_mdio_change_bits(phy, MDIO_MMD_AN, AQ_100M_CTRL, 0xfe0, adv);
  160. return err;
  161. }
  162. static int aq100x_set_loopback(struct cphy *phy, int mmd, int dir, int enable)
  163. {
  164. return mdio_set_flag(&phy->mdio, phy->mdio.prtad,
  165. MDIO_MMD_PMAPMD, MDIO_CTRL1,
  166. BMCR_LOOPBACK, enable);
  167. }
  168. static int aq100x_set_speed_duplex(struct cphy *phy, int speed, int duplex)
  169. {
  170. /* no can do */
  171. return -1;
  172. }
  173. static int aq100x_get_link_status(struct cphy *phy, int *link_ok,
  174. int *speed, int *duplex, int *fc)
  175. {
  176. int err;
  177. unsigned int v;
  178. if (link_ok) {
  179. err = t3_mdio_read(phy, MDIO_MMD_PMAPMD, AQ_LINK_STAT, &v);
  180. if (err)
  181. return err;
  182. *link_ok = v & 1;
  183. if (!*link_ok)
  184. return 0;
  185. }
  186. err = t3_mdio_read(phy, MDIO_MMD_AN, AQ_ANEG_STAT, &v);
  187. if (err)
  188. return err;
  189. if (speed) {
  190. switch (v & 0x6) {
  191. case 0x6:
  192. *speed = SPEED_10000;
  193. break;
  194. case 0x4:
  195. *speed = SPEED_1000;
  196. break;
  197. case 0x2:
  198. *speed = SPEED_100;
  199. break;
  200. case 0x0:
  201. *speed = SPEED_10;
  202. break;
  203. }
  204. }
  205. if (duplex)
  206. *duplex = v & 1 ? DUPLEX_FULL : DUPLEX_HALF;
  207. return 0;
  208. }
  209. static struct cphy_ops aq100x_ops = {
  210. .reset = aq100x_reset,
  211. .intr_enable = aq100x_intr_enable,
  212. .intr_disable = aq100x_intr_disable,
  213. .intr_clear = aq100x_intr_clear,
  214. .intr_handler = aq100x_intr_handler,
  215. .autoneg_enable = aq100x_autoneg_enable,
  216. .autoneg_restart = aq100x_autoneg_restart,
  217. .advertise = aq100x_advertise,
  218. .set_loopback = aq100x_set_loopback,
  219. .set_speed_duplex = aq100x_set_speed_duplex,
  220. .get_link_status = aq100x_get_link_status,
  221. .power_down = aq100x_power_down,
  222. .mmds = MDIO_DEVS_PMAPMD | MDIO_DEVS_PCS | MDIO_DEVS_PHYXS,
  223. };
  224. int t3_aq100x_phy_prep(struct cphy *phy, struct adapter *adapter, int phy_addr,
  225. const struct mdio_ops *mdio_ops)
  226. {
  227. unsigned int v, v2, gpio, wait;
  228. int err;
  229. cphy_init(phy, adapter, phy_addr, &aq100x_ops, mdio_ops,
  230. SUPPORTED_1000baseT_Full | SUPPORTED_10000baseT_Full |
  231. SUPPORTED_Autoneg | SUPPORTED_AUI, "1000/10GBASE-T");
  232. /*
  233. * The PHY has been out of reset ever since the system powered up. So
  234. * we do a hard reset over here.
  235. */
  236. gpio = phy_addr ? F_GPIO10_OUT_VAL : F_GPIO6_OUT_VAL;
  237. t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, gpio, 0);
  238. msleep(1);
  239. t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, gpio, gpio);
  240. /*
  241. * Give it enough time to load the firmware and get ready for mdio.
  242. */
  243. msleep(1000);
  244. wait = 500; /* in 10ms increments */
  245. do {
  246. err = t3_mdio_read(phy, MDIO_MMD_VEND1, MDIO_CTRL1, &v);
  247. if (err || v == 0xffff) {
  248. /* Allow prep_adapter to succeed when ffff is read */
  249. CH_WARN(adapter, "PHY%d: reset failed (0x%x, 0x%x).\n",
  250. phy_addr, err, v);
  251. goto done;
  252. }
  253. v &= AQ_RESET;
  254. if (v)
  255. msleep(10);
  256. } while (v && --wait);
  257. if (v) {
  258. CH_WARN(adapter, "PHY%d: reset timed out (0x%x).\n",
  259. phy_addr, v);
  260. goto done; /* let prep_adapter succeed */
  261. }
  262. /* Datasheet says 3s max but this has been observed */
  263. wait = (500 - wait) * 10 + 1000;
  264. if (wait > 3000)
  265. CH_WARN(adapter, "PHY%d: reset took %ums\n", phy_addr, wait);
  266. /* Firmware version check. */
  267. t3_mdio_read(phy, MDIO_MMD_VEND1, AQ_FW_VERSION, &v);
  268. if (v != 30) {
  269. CH_WARN(adapter, "PHY%d: unsupported firmware %d\n",
  270. phy_addr, v);
  271. return 0; /* allow t3_prep_adapter to succeed */
  272. }
  273. /*
  274. * The PHY should start in really-low-power mode. Prepare it for normal
  275. * operations.
  276. */
  277. err = t3_mdio_read(phy, MDIO_MMD_VEND1, MDIO_CTRL1, &v);
  278. if (err)
  279. return err;
  280. if (v & AQ_LOWPOWER) {
  281. err = t3_mdio_change_bits(phy, MDIO_MMD_VEND1, MDIO_CTRL1,
  282. AQ_LOWPOWER, 0);
  283. if (err)
  284. return err;
  285. msleep(10);
  286. } else
  287. CH_WARN(adapter, "PHY%d does not start in low power mode.\n",
  288. phy_addr);
  289. /*
  290. * Verify XAUI settings, but let prep succeed no matter what.
  291. */
  292. v = v2 = 0;
  293. t3_mdio_read(phy, MDIO_MMD_PHYXS, AQ_XAUI_RX_CFG, &v);
  294. t3_mdio_read(phy, MDIO_MMD_PHYXS, AQ_XAUI_TX_CFG, &v2);
  295. if (v != 0x1b || v2 != 0x1b)
  296. CH_WARN(adapter,
  297. "PHY%d: incorrect XAUI settings (0x%x, 0x%x).\n",
  298. phy_addr, v, v2);
  299. done:
  300. return err;
  301. }