tenxpress.c 14 KB

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