tenxpress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 int tenxpress_init(struct efx_nic *efx)
  126. {
  127. int rc, reg;
  128. /* Turn on the clock */
  129. reg = (1 << CLK312_EN_LBN);
  130. mdio_clause45_write(efx, efx->mii.phy_id,
  131. MDIO_MMD_PCS, PCS_TEST_SELECT_REG, reg);
  132. rc = tenxpress_phy_check(efx);
  133. if (rc < 0)
  134. return rc;
  135. /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
  136. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  137. MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG);
  138. reg |= (1 << PMA_PMA_LED_ACTIVITY_LBN);
  139. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  140. PMA_PMD_LED_CTRL_REG, reg);
  141. reg = PMA_PMD_LED_DEFAULT;
  142. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  143. PMA_PMD_LED_OVERR_REG, reg);
  144. return rc;
  145. }
  146. static int tenxpress_phy_init(struct efx_nic *efx)
  147. {
  148. struct tenxpress_phy_data *phy_data;
  149. int rc = 0;
  150. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  151. if (!phy_data)
  152. return -ENOMEM;
  153. efx->phy_data = phy_data;
  154. phy_data->phy_mode = efx->phy_mode;
  155. rc = mdio_clause45_wait_reset_mmds(efx,
  156. TENXPRESS_REQUIRED_DEVS);
  157. if (rc < 0)
  158. goto fail;
  159. rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
  160. if (rc < 0)
  161. goto fail;
  162. rc = tenxpress_init(efx);
  163. if (rc < 0)
  164. goto fail;
  165. schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
  166. /* Let XGXS and SerDes out of reset and resets 10XPress */
  167. falcon_reset_xaui(efx);
  168. return 0;
  169. fail:
  170. kfree(efx->phy_data);
  171. efx->phy_data = NULL;
  172. return rc;
  173. }
  174. static int tenxpress_special_reset(struct efx_nic *efx)
  175. {
  176. int rc, reg;
  177. /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
  178. * a special software reset can glitch the XGMAC sufficiently for stats
  179. * requests to fail. Since we don't ofen special_reset, just lock. */
  180. spin_lock(&efx->stats_lock);
  181. /* Initiate reset */
  182. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  183. MDIO_MMD_PMAPMD, PMA_PMD_EXT_CTRL_REG);
  184. reg |= (1 << PMA_PMD_EXT_SSR_LBN);
  185. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  186. PMA_PMD_EXT_CTRL_REG, reg);
  187. mdelay(200);
  188. /* Wait for the blocks to come out of reset */
  189. rc = mdio_clause45_wait_reset_mmds(efx,
  190. TENXPRESS_REQUIRED_DEVS);
  191. if (rc < 0)
  192. goto unlock;
  193. /* Try and reconfigure the device */
  194. rc = tenxpress_init(efx);
  195. if (rc < 0)
  196. goto unlock;
  197. unlock:
  198. spin_unlock(&efx->stats_lock);
  199. return rc;
  200. }
  201. static void tenxpress_set_bad_lp(struct efx_nic *efx, bool bad_lp)
  202. {
  203. struct tenxpress_phy_data *pd = efx->phy_data;
  204. int reg;
  205. /* Nothing to do if all is well and was previously so. */
  206. if (!(bad_lp || pd->bad_lp_tries))
  207. return;
  208. reg = mdio_clause45_read(efx, efx->mii.phy_id,
  209. MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG);
  210. if (bad_lp)
  211. pd->bad_lp_tries++;
  212. else
  213. pd->bad_lp_tries = 0;
  214. if (pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
  215. pd->bad_lp_tries = 0; /* Restart count */
  216. reg &= ~(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
  217. reg |= (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
  218. EFX_ERR(efx, "This NIC appears to be plugged into"
  219. " a port that is not 10GBASE-T capable.\n"
  220. " This PHY is 10GBASE-T ONLY, so no link can"
  221. " be established.\n");
  222. } else {
  223. reg |= (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN);
  224. }
  225. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  226. PMA_PMD_LED_OVERR_REG, reg);
  227. }
  228. /* Check link status and return a boolean OK value. If the link is NOT
  229. * OK we have a quick rummage round to see if we appear to be plugged
  230. * into a non-10GBT port and if so warn the user that they won't get
  231. * link any time soon as we are 10GBT only, unless caller specified
  232. * not to do this check (it isn't useful in loopback) */
  233. static bool tenxpress_link_ok(struct efx_nic *efx, bool check_lp)
  234. {
  235. bool ok = mdio_clause45_links_ok(efx, TENXPRESS_REQUIRED_DEVS);
  236. if (ok) {
  237. tenxpress_set_bad_lp(efx, false);
  238. } else if (check_lp) {
  239. /* Are we plugged into the wrong sort of link? */
  240. bool bad_lp = false;
  241. int phy_id = efx->mii.phy_id;
  242. int an_stat = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
  243. MDIO_AN_STATUS);
  244. int xphy_stat = mdio_clause45_read(efx, phy_id,
  245. MDIO_MMD_PMAPMD,
  246. PMA_PMD_XSTATUS_REG);
  247. /* Are we plugged into anything that sends FLPs? If
  248. * not we can't distinguish between not being plugged
  249. * in and being plugged into a non-AN antique. The FLP
  250. * bit has the advantage of not clearing when autoneg
  251. * restarts. */
  252. if (!(xphy_stat & (1 << PMA_PMD_XSTAT_FLP_LBN))) {
  253. tenxpress_set_bad_lp(efx, false);
  254. return ok;
  255. }
  256. /* If it can do 10GBT it must be XNP capable */
  257. bad_lp = !(an_stat & (1 << MDIO_AN_STATUS_XNP_LBN));
  258. if (!bad_lp && (an_stat & (1 << MDIO_AN_STATUS_PAGE_LBN))) {
  259. bad_lp = !(mdio_clause45_read(efx, phy_id,
  260. MDIO_MMD_AN, MDIO_AN_10GBT_STATUS) &
  261. (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN));
  262. }
  263. tenxpress_set_bad_lp(efx, bad_lp);
  264. }
  265. return ok;
  266. }
  267. static void tenxpress_phyxs_loopback(struct efx_nic *efx)
  268. {
  269. int phy_id = efx->mii.phy_id;
  270. int ctrl1, ctrl2;
  271. ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
  272. PHYXS_TEST1);
  273. if (efx->loopback_mode == LOOPBACK_PHYXS)
  274. ctrl2 |= (1 << LOOPBACK_NEAR_LBN);
  275. else
  276. ctrl2 &= ~(1 << LOOPBACK_NEAR_LBN);
  277. if (ctrl1 != ctrl2)
  278. mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
  279. PHYXS_TEST1, ctrl2);
  280. }
  281. static void tenxpress_phy_reconfigure(struct efx_nic *efx)
  282. {
  283. struct tenxpress_phy_data *phy_data = efx->phy_data;
  284. bool loop_change = LOOPBACK_OUT_OF(phy_data, efx,
  285. TENXPRESS_LOOPBACKS);
  286. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  287. phy_data->phy_mode = efx->phy_mode;
  288. return;
  289. }
  290. /* When coming out of transmit disable, coming out of low power
  291. * mode, or moving out of any PHY internal loopback mode,
  292. * perform a special software reset */
  293. if ((efx->phy_mode == PHY_MODE_NORMAL &&
  294. phy_data->phy_mode != PHY_MODE_NORMAL) ||
  295. loop_change) {
  296. tenxpress_special_reset(efx);
  297. falcon_reset_xaui(efx);
  298. }
  299. mdio_clause45_transmit_disable(efx);
  300. mdio_clause45_phy_reconfigure(efx);
  301. tenxpress_phyxs_loopback(efx);
  302. phy_data->loopback_mode = efx->loopback_mode;
  303. phy_data->phy_mode = efx->phy_mode;
  304. efx->link_up = tenxpress_link_ok(efx, false);
  305. efx->link_options = GM_LPA_10000FULL;
  306. }
  307. static void tenxpress_phy_clear_interrupt(struct efx_nic *efx)
  308. {
  309. /* Nothing done here - LASI interrupts aren't reliable so poll */
  310. }
  311. /* Poll PHY for interrupt */
  312. static int tenxpress_phy_check_hw(struct efx_nic *efx)
  313. {
  314. struct tenxpress_phy_data *phy_data = efx->phy_data;
  315. bool link_ok;
  316. link_ok = tenxpress_link_ok(efx, true);
  317. if (link_ok != efx->link_up)
  318. falcon_xmac_sim_phy_event(efx);
  319. if (phy_data->phy_mode != PHY_MODE_NORMAL)
  320. return 0;
  321. if (atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
  322. EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
  323. falcon_reset_xaui(efx);
  324. atomic_set(&phy_data->bad_crc_count, 0);
  325. }
  326. return 0;
  327. }
  328. static void tenxpress_phy_fini(struct efx_nic *efx)
  329. {
  330. int reg;
  331. /* Power down the LNPGA */
  332. reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
  333. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  334. PMA_PMD_XCONTROL_REG, reg);
  335. /* Waiting here ensures that the board fini, which can turn off the
  336. * power to the PHY, won't get run until the LNPGA powerdown has been
  337. * given long enough to complete. */
  338. schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
  339. kfree(efx->phy_data);
  340. efx->phy_data = NULL;
  341. }
  342. /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
  343. * (which probably aren't wired anyway) are left in AUTO mode */
  344. void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
  345. {
  346. int reg;
  347. if (blink)
  348. reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
  349. (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
  350. (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
  351. else
  352. reg = PMA_PMD_LED_DEFAULT;
  353. mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
  354. PMA_PMD_LED_OVERR_REG, reg);
  355. }
  356. static int tenxpress_phy_test(struct efx_nic *efx)
  357. {
  358. /* BIST is automatically run after a special software reset */
  359. return tenxpress_special_reset(efx);
  360. }
  361. struct efx_phy_operations falcon_tenxpress_phy_ops = {
  362. .init = tenxpress_phy_init,
  363. .reconfigure = tenxpress_phy_reconfigure,
  364. .check_hw = tenxpress_phy_check_hw,
  365. .fini = tenxpress_phy_fini,
  366. .clear_interrupt = tenxpress_phy_clear_interrupt,
  367. .test = tenxpress_phy_test,
  368. .mmds = TENXPRESS_REQUIRED_DEVS,
  369. .loopbacks = TENXPRESS_LOOPBACKS,
  370. };