tenxpress.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /****************************************************************************
  2. * Driver for Solarflare 802.3an compliant PHY
  3. * Copyright 2007 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. #include <linux/delay.h>
  10. #include <linux/seq_file.h>
  11. #include "efx.h"
  12. #include "gmii.h"
  13. #include "mdio_10g.h"
  14. #include "falcon.h"
  15. #include "phy.h"
  16. #include "falcon_hwdefs.h"
  17. #include "boards.h"
  18. #include "mac.h"
  19. /* We expect these MMDs to be in the package */
  20. /* AN not here as mdio_check_mmds() requires STAT2 support */
  21. #define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS0_PMAPMD | \
  22. MDIO_MMDREG_DEVS0_PCS | \
  23. MDIO_MMDREG_DEVS0_PHYXS)
  24. #define TENXPRESS_LOOPBACKS ((1 << LOOPBACK_PHYXS) | \
  25. (1 << LOOPBACK_PCS) | \
  26. (1 << LOOPBACK_PMAPMD) | \
  27. (1 << LOOPBACK_NETWORK))
  28. /* We complain if we fail to see the link partner as 10G capable this many
  29. * times in a row (must be > 1 as sampling the autoneg. registers is racy)
  30. */
  31. #define MAX_BAD_LP_TRIES (5)
  32. /* Extended control register */
  33. #define PMA_PMD_XCONTROL_REG 0xc000
  34. #define PMA_PMD_LNPGA_POWERDOWN_LBN 8
  35. #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
  36. /* extended status register */
  37. #define PMA_PMD_XSTATUS_REG 0xc001
  38. #define PMA_PMD_XSTAT_FLP_LBN (12)
  39. /* LED control register */
  40. #define PMA_PMD_LED_CTRL_REG (0xc007)
  41. #define PMA_PMA_LED_ACTIVITY_LBN (3)
  42. /* LED function override register */
  43. #define PMA_PMD_LED_OVERR_REG (0xc009)
  44. /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
  45. #define PMA_PMD_LED_LINK_LBN (0)
  46. #define PMA_PMD_LED_SPEED_LBN (2)
  47. #define PMA_PMD_LED_TX_LBN (4)
  48. #define PMA_PMD_LED_RX_LBN (6)
  49. /* Override settings */
  50. #define PMA_PMD_LED_AUTO (0) /* H/W control */
  51. #define PMA_PMD_LED_ON (1)
  52. #define PMA_PMD_LED_OFF (2)
  53. #define PMA_PMD_LED_FLASH (3)
  54. /* All LEDs under hardware control */
  55. #define PMA_PMD_LED_FULL_AUTO (0)
  56. /* Green and Amber under hardware control, Red off */
  57. #define PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
  58. /* Special Software reset register */
  59. #define PMA_PMD_EXT_CTRL_REG 49152
  60. #define PMA_PMD_EXT_SSR_LBN 15
  61. /* Misc register defines */
  62. #define PCS_CLOCK_CTRL_REG 0xd801
  63. #define PLL312_RST_N_LBN 2
  64. #define PCS_SOFT_RST2_REG 0xd806
  65. #define SERDES_RST_N_LBN 13
  66. #define XGXS_RST_N_LBN 12
  67. #define PCS_TEST_SELECT_REG 0xd807 /* PRM 10.5.8 */
  68. #define CLK312_EN_LBN 3
  69. /* PHYXS registers */
  70. #define PHYXS_TEST1 (49162)
  71. #define LOOPBACK_NEAR_LBN (8)
  72. #define LOOPBACK_NEAR_WIDTH (1)
  73. /* Boot status register */
  74. #define PCS_BOOT_STATUS_REG (0xd000)
  75. #define PCS_BOOT_FATAL_ERR_LBN (0)
  76. #define PCS_BOOT_PROGRESS_LBN (1)
  77. #define PCS_BOOT_PROGRESS_WIDTH (2)
  78. #define PCS_BOOT_COMPLETE_LBN (3)
  79. #define PCS_BOOT_MAX_DELAY (100)
  80. #define PCS_BOOT_POLL_DELAY (10)
  81. /* Time to wait between powering down the LNPGA and turning off the power
  82. * rails */
  83. #define LNPGA_PDOWN_WAIT (HZ / 5)
  84. static int crc_error_reset_threshold = 100;
  85. module_param(crc_error_reset_threshold, int, 0644);
  86. MODULE_PARM_DESC(crc_error_reset_threshold,
  87. "Max number of CRC errors before XAUI reset");
  88. struct tenxpress_phy_data {
  89. enum efx_loopback_mode loopback_mode;
  90. atomic_t bad_crc_count;
  91. enum efx_phy_mode phy_mode;
  92. int bad_lp_tries;
  93. };
  94. void tenxpress_crc_err(struct efx_nic *efx)
  95. {
  96. struct tenxpress_phy_data *phy_data = efx->phy_data;
  97. if (phy_data != NULL)
  98. atomic_inc(&phy_data->bad_crc_count);
  99. }
  100. /* Check that the C166 has booted successfully */
  101. static int tenxpress_phy_check(struct efx_nic *efx)
  102. {
  103. int phy_id = efx->mii.phy_id;
  104. int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
  105. int boot_stat;
  106. /* Wait for the boot to complete (or not) */
  107. while (count) {
  108. boot_stat = mdio_clause45_read(efx, phy_id,
  109. MDIO_MMD_PCS,
  110. PCS_BOOT_STATUS_REG);
  111. if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
  112. break;
  113. count--;
  114. udelay(PCS_BOOT_POLL_DELAY);
  115. }
  116. if (!count) {
  117. EFX_ERR(efx, "%s: PHY boot timed out. Last status "
  118. "%x\n", __func__,
  119. (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
  120. ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
  121. return -ETIMEDOUT;
  122. }
  123. return 0;
  124. }
  125. static void tenxpress_reset_xaui(struct efx_nic *efx);
  126. static int tenxpress_init(struct efx_nic *efx)
  127. {
  128. int rc, reg;
  129. /* Turn on the clock */
  130. reg = (1 << CLK312_EN_LBN);
  131. mdio_clause45_write(efx, efx->mii.phy_id,
  132. MDIO_MMD_PCS, PCS_TEST_SELECT_REG, reg);
  133. rc = tenxpress_phy_check(efx);
  134. if (rc < 0)
  135. return rc;
  136. /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
  137. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  138. MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG);
  139. reg |= (1 << PMA_PMA_LED_ACTIVITY_LBN);
  140. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  141. PMA_PMD_LED_CTRL_REG, reg);
  142. reg = PMA_PMD_LED_DEFAULT;
  143. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  144. PMA_PMD_LED_OVERR_REG, reg);
  145. return rc;
  146. }
  147. static int tenxpress_phy_init(struct efx_nic *efx)
  148. {
  149. struct tenxpress_phy_data *phy_data;
  150. int rc = 0;
  151. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  152. if (!phy_data)
  153. return -ENOMEM;
  154. efx->phy_data = phy_data;
  155. phy_data->phy_mode = efx->phy_mode;
  156. rc = mdio_clause45_wait_reset_mmds(efx,
  157. TENXPRESS_REQUIRED_DEVS);
  158. if (rc < 0)
  159. goto fail;
  160. rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
  161. if (rc < 0)
  162. goto fail;
  163. rc = tenxpress_init(efx);
  164. if (rc < 0)
  165. goto fail;
  166. schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
  167. /* Let XGXS and SerDes out of reset and resets 10XPress */
  168. falcon_reset_xaui(efx);
  169. return 0;
  170. fail:
  171. kfree(efx->phy_data);
  172. efx->phy_data = NULL;
  173. return rc;
  174. }
  175. static int tenxpress_special_reset(struct efx_nic *efx)
  176. {
  177. int rc, reg;
  178. EFX_TRACE(efx, "%s\n", __func__);
  179. /* Initiate reset */
  180. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  181. MDIO_MMD_PMAPMD, PMA_PMD_EXT_CTRL_REG);
  182. reg |= (1 << PMA_PMD_EXT_SSR_LBN);
  183. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  184. PMA_PMD_EXT_CTRL_REG, reg);
  185. msleep(200);
  186. /* Wait for the blocks to come out of reset */
  187. rc = mdio_clause45_wait_reset_mmds(efx,
  188. TENXPRESS_REQUIRED_DEVS);
  189. if (rc < 0)
  190. return rc;
  191. /* Try and reconfigure the device */
  192. rc = tenxpress_init(efx);
  193. if (rc < 0)
  194. return rc;
  195. return 0;
  196. }
  197. static void tenxpress_set_bad_lp(struct efx_nic *efx, bool bad_lp)
  198. {
  199. struct tenxpress_phy_data *pd = efx->phy_data;
  200. int reg;
  201. /* Nothing to do if all is well and was previously so. */
  202. if (!(bad_lp || pd->bad_lp_tries))
  203. return;
  204. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  205. MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG);
  206. if (bad_lp)
  207. pd->bad_lp_tries++;
  208. else
  209. pd->bad_lp_tries = 0;
  210. if (pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
  211. pd->bad_lp_tries = 0; /* Restart count */
  212. reg &= ~(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
  213. reg |= (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
  214. EFX_ERR(efx, "This NIC appears to be plugged into"
  215. " a port that is not 10GBASE-T capable.\n"
  216. " This PHY is 10GBASE-T ONLY, so no link can"
  217. " be established.\n");
  218. } else {
  219. reg |= (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN);
  220. }
  221. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  222. PMA_PMD_LED_OVERR_REG, reg);
  223. }
  224. /* Check link status and return a boolean OK value. If the link is NOT
  225. * OK we have a quick rummage round to see if we appear to be plugged
  226. * into a non-10GBT port and if so warn the user that they won't get
  227. * link any time soon as we are 10GBT only, unless caller specified
  228. * not to do this check (it isn't useful in loopback) */
  229. static bool tenxpress_link_ok(struct efx_nic *efx, bool check_lp)
  230. {
  231. bool ok = mdio_clause45_links_ok(efx, TENXPRESS_REQUIRED_DEVS);
  232. if (ok) {
  233. tenxpress_set_bad_lp(efx, false);
  234. } else if (check_lp) {
  235. /* Are we plugged into the wrong sort of link? */
  236. bool bad_lp = false;
  237. int phy_id = efx->mii.phy_id;
  238. int an_stat = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
  239. MDIO_AN_STATUS);
  240. int xphy_stat = mdio_clause45_read(efx, phy_id,
  241. MDIO_MMD_PMAPMD,
  242. PMA_PMD_XSTATUS_REG);
  243. /* Are we plugged into anything that sends FLPs? If
  244. * not we can't distinguish between not being plugged
  245. * in and being plugged into a non-AN antique. The FLP
  246. * bit has the advantage of not clearing when autoneg
  247. * restarts. */
  248. if (!(xphy_stat & (1 << PMA_PMD_XSTAT_FLP_LBN))) {
  249. tenxpress_set_bad_lp(efx, false);
  250. return ok;
  251. }
  252. /* If it can do 10GBT it must be XNP capable */
  253. bad_lp = !(an_stat & (1 << MDIO_AN_STATUS_XNP_LBN));
  254. if (!bad_lp && (an_stat & (1 << MDIO_AN_STATUS_PAGE_LBN))) {
  255. bad_lp = !(mdio_clause45_read(efx, phy_id,
  256. MDIO_MMD_AN, MDIO_AN_10GBT_STATUS) &
  257. (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN));
  258. }
  259. tenxpress_set_bad_lp(efx, bad_lp);
  260. }
  261. return ok;
  262. }
  263. static void tenxpress_phyxs_loopback(struct efx_nic *efx)
  264. {
  265. int phy_id = efx->mii.phy_id;
  266. int ctrl1, ctrl2;
  267. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
  268. PHYXS_TEST1);
  269. if (efx->loopback_mode == LOOPBACK_PHYXS)
  270. ctrl2 |= (1 << LOOPBACK_NEAR_LBN);
  271. else
  272. ctrl2 &= ~(1 << LOOPBACK_NEAR_LBN);
  273. if (ctrl1 != ctrl2)
  274. mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
  275. PHYXS_TEST1, ctrl2);
  276. }
  277. static void tenxpress_phy_reconfigure(struct efx_nic *efx)
  278. {
  279. struct tenxpress_phy_data *phy_data = efx->phy_data;
  280. bool loop_change = LOOPBACK_OUT_OF(phy_data, efx,
  281. TENXPRESS_LOOPBACKS);
  282. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  283. phy_data->phy_mode = efx->phy_mode;
  284. return;
  285. }
  286. /* When coming out of transmit disable, coming out of low power
  287. * mode, or moving out of any PHY internal loopback mode,
  288. * perform a special software reset */
  289. if ((efx->phy_mode == PHY_MODE_NORMAL &&
  290. phy_data->phy_mode != PHY_MODE_NORMAL) ||
  291. loop_change) {
  292. tenxpress_special_reset(efx);
  293. falcon_reset_xaui(efx);
  294. }
  295. mdio_clause45_transmit_disable(efx);
  296. mdio_clause45_phy_reconfigure(efx);
  297. tenxpress_phyxs_loopback(efx);
  298. phy_data->loopback_mode = efx->loopback_mode;
  299. phy_data->phy_mode = efx->phy_mode;
  300. efx->link_up = tenxpress_link_ok(efx, false);
  301. efx->link_options = GM_LPA_10000FULL;
  302. }
  303. static void tenxpress_phy_clear_interrupt(struct efx_nic *efx)
  304. {
  305. /* Nothing done here - LASI interrupts aren't reliable so poll */
  306. }
  307. /* Poll PHY for interrupt */
  308. static int tenxpress_phy_check_hw(struct efx_nic *efx)
  309. {
  310. struct tenxpress_phy_data *phy_data = efx->phy_data;
  311. bool link_ok;
  312. link_ok = (phy_data->phy_mode == PHY_MODE_NORMAL &&
  313. tenxpress_link_ok(efx, true));
  314. if (link_ok != efx->link_up)
  315. falcon_xmac_sim_phy_event(efx);
  316. if (phy_data->phy_mode != PHY_MODE_NORMAL)
  317. return 0;
  318. if (atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
  319. EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
  320. falcon_reset_xaui(efx);
  321. atomic_set(&phy_data->bad_crc_count, 0);
  322. }
  323. return 0;
  324. }
  325. static void tenxpress_phy_fini(struct efx_nic *efx)
  326. {
  327. int reg;
  328. /* Power down the LNPGA */
  329. reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
  330. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  331. PMA_PMD_XCONTROL_REG, reg);
  332. /* Waiting here ensures that the board fini, which can turn off the
  333. * power to the PHY, won't get run until the LNPGA powerdown has been
  334. * given long enough to complete. */
  335. schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
  336. kfree(efx->phy_data);
  337. efx->phy_data = NULL;
  338. }
  339. /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
  340. * (which probably aren't wired anyway) are left in AUTO mode */
  341. void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
  342. {
  343. int reg;
  344. if (blink)
  345. reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
  346. (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
  347. (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
  348. else
  349. reg = PMA_PMD_LED_DEFAULT;
  350. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  351. PMA_PMD_LED_OVERR_REG, reg);
  352. }
  353. static void tenxpress_reset_xaui(struct efx_nic *efx)
  354. {
  355. int phy = efx->mii.phy_id;
  356. int clk_ctrl, test_select, soft_rst2;
  357. /* Real work is done on clock_ctrl other resets are thought to be
  358. * optional but make the reset more reliable
  359. */
  360. /* Read */
  361. clk_ctrl = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
  362. PCS_CLOCK_CTRL_REG);
  363. test_select = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
  364. PCS_TEST_SELECT_REG);
  365. soft_rst2 = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
  366. PCS_SOFT_RST2_REG);
  367. /* Put in reset */
  368. test_select &= ~(1 << CLK312_EN_LBN);
  369. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  370. PCS_TEST_SELECT_REG, test_select);
  371. soft_rst2 &= ~((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
  372. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  373. PCS_SOFT_RST2_REG, soft_rst2);
  374. clk_ctrl &= ~(1 << PLL312_RST_N_LBN);
  375. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  376. PCS_CLOCK_CTRL_REG, clk_ctrl);
  377. udelay(10);
  378. /* Remove reset */
  379. clk_ctrl |= (1 << PLL312_RST_N_LBN);
  380. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  381. PCS_CLOCK_CTRL_REG, clk_ctrl);
  382. udelay(10);
  383. soft_rst2 |= ((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
  384. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  385. PCS_SOFT_RST2_REG, soft_rst2);
  386. udelay(10);
  387. test_select |= (1 << CLK312_EN_LBN);
  388. mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
  389. PCS_TEST_SELECT_REG, test_select);
  390. udelay(10);
  391. }
  392. static int tenxpress_phy_test(struct efx_nic *efx)
  393. {
  394. /* BIST is automatically run after a special software reset */
  395. return tenxpress_special_reset(efx);
  396. }
  397. struct efx_phy_operations falcon_tenxpress_phy_ops = {
  398. .init = tenxpress_phy_init,
  399. .reconfigure = tenxpress_phy_reconfigure,
  400. .check_hw = tenxpress_phy_check_hw,
  401. .fini = tenxpress_phy_fini,
  402. .clear_interrupt = tenxpress_phy_clear_interrupt,
  403. .reset_xaui = tenxpress_reset_xaui,
  404. .test = tenxpress_phy_test,
  405. .mmds = TENXPRESS_REQUIRED_DEVS,
  406. .loopbacks = TENXPRESS_LOOPBACKS,
  407. };