tenxpress.c 12 KB

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