sfe4001.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2007-2008 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. /*****************************************************************************
  10. * Support for the SFE4001 and SFN4111T NICs.
  11. *
  12. * The SFE4001 does not power-up fully at reset due to its high power
  13. * consumption. We control its power via a PCA9539 I/O expander.
  14. * Both boards have a MAX6647 temperature monitor which we expose to
  15. * the lm90 driver.
  16. *
  17. * This also provides minimal support for reflashing the PHY, which is
  18. * initiated by resetting it with the FLASH_CFG_1 pin pulled down.
  19. * On SFE4001 rev A2 and later this is connected to the 3V3X output of
  20. * the IO-expander; on the SFN4111T it is connected to Falcon's GPIO3.
  21. * We represent reflash mode as PHY_MODE_SPECIAL and make it mutually
  22. * exclusive with the network device being open.
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/rtnetlink.h>
  26. #include "net_driver.h"
  27. #include "efx.h"
  28. #include "phy.h"
  29. #include "boards.h"
  30. #include "falcon.h"
  31. #include "falcon_hwdefs.h"
  32. #include "falcon_io.h"
  33. #include "mac.h"
  34. #include "workarounds.h"
  35. /**************************************************************************
  36. *
  37. * I2C IO Expander device
  38. *
  39. **************************************************************************/
  40. #define PCA9539 0x74
  41. #define P0_IN 0x00
  42. #define P0_OUT 0x02
  43. #define P0_INVERT 0x04
  44. #define P0_CONFIG 0x06
  45. #define P0_EN_1V0X_LBN 0
  46. #define P0_EN_1V0X_WIDTH 1
  47. #define P0_EN_1V2_LBN 1
  48. #define P0_EN_1V2_WIDTH 1
  49. #define P0_EN_2V5_LBN 2
  50. #define P0_EN_2V5_WIDTH 1
  51. #define P0_EN_3V3X_LBN 3
  52. #define P0_EN_3V3X_WIDTH 1
  53. #define P0_EN_5V_LBN 4
  54. #define P0_EN_5V_WIDTH 1
  55. #define P0_SHORTEN_JTAG_LBN 5
  56. #define P0_SHORTEN_JTAG_WIDTH 1
  57. #define P0_X_TRST_LBN 6
  58. #define P0_X_TRST_WIDTH 1
  59. #define P0_DSP_RESET_LBN 7
  60. #define P0_DSP_RESET_WIDTH 1
  61. #define P1_IN 0x01
  62. #define P1_OUT 0x03
  63. #define P1_INVERT 0x05
  64. #define P1_CONFIG 0x07
  65. #define P1_AFE_PWD_LBN 0
  66. #define P1_AFE_PWD_WIDTH 1
  67. #define P1_DSP_PWD25_LBN 1
  68. #define P1_DSP_PWD25_WIDTH 1
  69. #define P1_RESERVED_LBN 2
  70. #define P1_RESERVED_WIDTH 2
  71. #define P1_SPARE_LBN 4
  72. #define P1_SPARE_WIDTH 4
  73. /* Temperature Sensor */
  74. #define MAX664X_REG_RSL 0x02
  75. #define MAX664X_REG_WLHO 0x0B
  76. static void sfe4001_poweroff(struct efx_nic *efx)
  77. {
  78. struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
  79. struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
  80. /* Turn off all power rails and disable outputs */
  81. i2c_smbus_write_byte_data(ioexp_client, P0_OUT, 0xff);
  82. i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG, 0xff);
  83. i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0xff);
  84. /* Clear any over-temperature alert */
  85. i2c_smbus_read_byte_data(hwmon_client, MAX664X_REG_RSL);
  86. }
  87. static int sfe4001_poweron(struct efx_nic *efx)
  88. {
  89. struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
  90. struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
  91. unsigned int i, j;
  92. int rc;
  93. u8 out;
  94. /* Clear any previous over-temperature alert */
  95. rc = i2c_smbus_read_byte_data(hwmon_client, MAX664X_REG_RSL);
  96. if (rc < 0)
  97. return rc;
  98. /* Enable port 0 and port 1 outputs on IO expander */
  99. rc = i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0x00);
  100. if (rc)
  101. return rc;
  102. rc = i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG,
  103. 0xff & ~(1 << P1_SPARE_LBN));
  104. if (rc)
  105. goto fail_on;
  106. /* If PHY power is on, turn it all off and wait 1 second to
  107. * ensure a full reset.
  108. */
  109. rc = i2c_smbus_read_byte_data(ioexp_client, P0_OUT);
  110. if (rc < 0)
  111. goto fail_on;
  112. out = 0xff & ~((0 << P0_EN_1V2_LBN) | (0 << P0_EN_2V5_LBN) |
  113. (0 << P0_EN_3V3X_LBN) | (0 << P0_EN_5V_LBN) |
  114. (0 << P0_EN_1V0X_LBN));
  115. if (rc != out) {
  116. EFX_INFO(efx, "power-cycling PHY\n");
  117. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  118. if (rc)
  119. goto fail_on;
  120. schedule_timeout_uninterruptible(HZ);
  121. }
  122. for (i = 0; i < 20; ++i) {
  123. /* Turn on 1.2V, 2.5V, 3.3V and 5V power rails */
  124. out = 0xff & ~((1 << P0_EN_1V2_LBN) | (1 << P0_EN_2V5_LBN) |
  125. (1 << P0_EN_3V3X_LBN) | (1 << P0_EN_5V_LBN) |
  126. (1 << P0_X_TRST_LBN));
  127. if (efx->phy_mode & PHY_MODE_SPECIAL)
  128. out |= 1 << P0_EN_3V3X_LBN;
  129. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  130. if (rc)
  131. goto fail_on;
  132. msleep(10);
  133. /* Turn on 1V power rail */
  134. out &= ~(1 << P0_EN_1V0X_LBN);
  135. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  136. if (rc)
  137. goto fail_on;
  138. EFX_INFO(efx, "waiting for DSP boot (attempt %d)...\n", i);
  139. /* In flash config mode, DSP does not turn on AFE, so
  140. * just wait 1 second.
  141. */
  142. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  143. schedule_timeout_uninterruptible(HZ);
  144. return 0;
  145. }
  146. for (j = 0; j < 10; ++j) {
  147. msleep(100);
  148. /* Check DSP has asserted AFE power line */
  149. rc = i2c_smbus_read_byte_data(ioexp_client, P1_IN);
  150. if (rc < 0)
  151. goto fail_on;
  152. if (rc & (1 << P1_AFE_PWD_LBN))
  153. return 0;
  154. }
  155. }
  156. EFX_INFO(efx, "timed out waiting for DSP boot\n");
  157. rc = -ETIMEDOUT;
  158. fail_on:
  159. sfe4001_poweroff(efx);
  160. return rc;
  161. }
  162. static int sfn4111t_reset(struct efx_nic *efx)
  163. {
  164. efx_oword_t reg;
  165. /* GPIO 3 and the GPIO register are shared with I2C, so block that */
  166. mutex_lock(&efx->i2c_adap.bus_lock);
  167. /* Pull RST_N (GPIO 2) low then let it up again, setting the
  168. * FLASH_CFG_1 strap (GPIO 3) appropriately. Only change the
  169. * output enables; the output levels should always be 0 (low)
  170. * and we rely on external pull-ups. */
  171. falcon_read(efx, &reg, GPIO_CTL_REG_KER);
  172. EFX_SET_OWORD_FIELD(reg, GPIO2_OEN, true);
  173. falcon_write(efx, &reg, GPIO_CTL_REG_KER);
  174. msleep(1000);
  175. EFX_SET_OWORD_FIELD(reg, GPIO2_OEN, false);
  176. EFX_SET_OWORD_FIELD(reg, GPIO3_OEN,
  177. !!(efx->phy_mode & PHY_MODE_SPECIAL));
  178. falcon_write(efx, &reg, GPIO_CTL_REG_KER);
  179. msleep(1);
  180. mutex_unlock(&efx->i2c_adap.bus_lock);
  181. ssleep(1);
  182. return 0;
  183. }
  184. static ssize_t show_phy_flash_cfg(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  188. return sprintf(buf, "%d\n", !!(efx->phy_mode & PHY_MODE_SPECIAL));
  189. }
  190. static ssize_t set_phy_flash_cfg(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  195. enum efx_phy_mode old_mode, new_mode;
  196. int err;
  197. rtnl_lock();
  198. old_mode = efx->phy_mode;
  199. if (count == 0 || *buf == '0')
  200. new_mode = old_mode & ~PHY_MODE_SPECIAL;
  201. else
  202. new_mode = PHY_MODE_SPECIAL;
  203. if (old_mode == new_mode) {
  204. err = 0;
  205. } else if (efx->state != STATE_RUNNING || netif_running(efx->net_dev)) {
  206. err = -EBUSY;
  207. } else {
  208. /* Reset the PHY, reconfigure the MAC and enable/disable
  209. * MAC stats accordingly. */
  210. efx->phy_mode = new_mode;
  211. if (new_mode & PHY_MODE_SPECIAL)
  212. efx_stats_disable(efx);
  213. if (efx->board_info.type == EFX_BOARD_SFE4001)
  214. err = sfe4001_poweron(efx);
  215. else
  216. err = sfn4111t_reset(efx);
  217. efx_reconfigure_port(efx);
  218. if (!(new_mode & PHY_MODE_SPECIAL))
  219. efx_stats_enable(efx);
  220. }
  221. rtnl_unlock();
  222. return err ? err : count;
  223. }
  224. static DEVICE_ATTR(phy_flash_cfg, 0644, show_phy_flash_cfg, set_phy_flash_cfg);
  225. static void sfe4001_fini(struct efx_nic *efx)
  226. {
  227. EFX_INFO(efx, "%s\n", __func__);
  228. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  229. sfe4001_poweroff(efx);
  230. i2c_unregister_device(efx->board_info.ioexp_client);
  231. i2c_unregister_device(efx->board_info.hwmon_client);
  232. }
  233. static int sfe4001_check_hw(struct efx_nic *efx)
  234. {
  235. s32 status;
  236. /* If XAUI link is up then do not monitor */
  237. if (EFX_WORKAROUND_7884(efx) && efx->mac_up)
  238. return 0;
  239. /* Check the powered status of the PHY. Lack of power implies that
  240. * the MAX6647 has shut down power to it, probably due to a temp.
  241. * alarm. Reading the power status rather than the MAX6647 status
  242. * directly because the later is read-to-clear and would thus
  243. * start to power up the PHY again when polled, causing us to blip
  244. * the power undesirably.
  245. * We know we can read from the IO expander because we did
  246. * it during power-on. Assume failure now is bad news. */
  247. status = i2c_smbus_read_byte_data(efx->board_info.ioexp_client, P1_IN);
  248. if (status >= 0 &&
  249. (status & ((1 << P1_AFE_PWD_LBN) | (1 << P1_DSP_PWD25_LBN))) != 0)
  250. return 0;
  251. /* Use board power control, not PHY power control */
  252. sfe4001_poweroff(efx);
  253. efx->phy_mode = PHY_MODE_OFF;
  254. return (status < 0) ? -EIO : -ERANGE;
  255. }
  256. static struct i2c_board_info sfe4001_hwmon_info = {
  257. I2C_BOARD_INFO("max6647", 0x4e),
  258. .irq = -1,
  259. };
  260. /* This board uses an I2C expander to provider power to the PHY, which needs to
  261. * be turned on before the PHY can be used.
  262. * Context: Process context, rtnl lock held
  263. */
  264. int sfe4001_init(struct efx_nic *efx)
  265. {
  266. int rc;
  267. #if defined(CONFIG_SENSORS_LM90) || defined(CONFIG_SENSORS_LM90_MODULE)
  268. efx->board_info.hwmon_client =
  269. i2c_new_device(&efx->i2c_adap, &sfe4001_hwmon_info);
  270. #else
  271. efx->board_info.hwmon_client =
  272. i2c_new_dummy(&efx->i2c_adap, sfe4001_hwmon_info.addr);
  273. #endif
  274. if (!efx->board_info.hwmon_client)
  275. return -EIO;
  276. /* Raise board/PHY high limit from 85 to 90 degrees Celsius */
  277. rc = i2c_smbus_write_byte_data(efx->board_info.hwmon_client,
  278. MAX664X_REG_WLHO, 90);
  279. if (rc)
  280. goto fail_hwmon;
  281. efx->board_info.ioexp_client = i2c_new_dummy(&efx->i2c_adap, PCA9539);
  282. if (!efx->board_info.ioexp_client) {
  283. rc = -EIO;
  284. goto fail_hwmon;
  285. }
  286. /* 10Xpress has fixed-function LED pins, so there is no board-specific
  287. * blink code. */
  288. efx->board_info.blink = tenxpress_phy_blink;
  289. efx->board_info.monitor = sfe4001_check_hw;
  290. efx->board_info.fini = sfe4001_fini;
  291. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  292. /* PHY won't generate a 156.25 MHz clock and MAC stats fetch
  293. * will fail. */
  294. efx_stats_disable(efx);
  295. }
  296. rc = sfe4001_poweron(efx);
  297. if (rc)
  298. goto fail_ioexp;
  299. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  300. if (rc)
  301. goto fail_on;
  302. EFX_INFO(efx, "PHY is powered on\n");
  303. return 0;
  304. fail_on:
  305. sfe4001_poweroff(efx);
  306. fail_ioexp:
  307. i2c_unregister_device(efx->board_info.ioexp_client);
  308. fail_hwmon:
  309. i2c_unregister_device(efx->board_info.hwmon_client);
  310. return rc;
  311. }
  312. static int sfn4111t_check_hw(struct efx_nic *efx)
  313. {
  314. s32 status;
  315. /* If XAUI link is up then do not monitor */
  316. if (EFX_WORKAROUND_7884(efx) && efx->mac_up)
  317. return 0;
  318. /* Test LHIGH, RHIGH, FAULT, EOT and IOT alarms */
  319. status = i2c_smbus_read_byte_data(efx->board_info.hwmon_client,
  320. MAX664X_REG_RSL);
  321. if (status < 0)
  322. return -EIO;
  323. if (status & 0x57)
  324. return -ERANGE;
  325. return 0;
  326. }
  327. static void sfn4111t_fini(struct efx_nic *efx)
  328. {
  329. EFX_INFO(efx, "%s\n", __func__);
  330. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  331. i2c_unregister_device(efx->board_info.hwmon_client);
  332. }
  333. static struct i2c_board_info sfn4111t_a0_hwmon_info = {
  334. I2C_BOARD_INFO("max6647", 0x4e),
  335. .irq = -1,
  336. };
  337. static struct i2c_board_info sfn4111t_r5_hwmon_info = {
  338. I2C_BOARD_INFO("max6646", 0x4d),
  339. .irq = -1,
  340. };
  341. int sfn4111t_init(struct efx_nic *efx)
  342. {
  343. int i = 0;
  344. int rc;
  345. efx->board_info.hwmon_client =
  346. i2c_new_device(&efx->i2c_adap,
  347. (efx->board_info.minor < 5) ?
  348. &sfn4111t_a0_hwmon_info :
  349. &sfn4111t_r5_hwmon_info);
  350. if (!efx->board_info.hwmon_client)
  351. return -EIO;
  352. efx->board_info.blink = tenxpress_phy_blink;
  353. efx->board_info.monitor = sfn4111t_check_hw;
  354. efx->board_info.fini = sfn4111t_fini;
  355. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  356. if (rc)
  357. goto fail_hwmon;
  358. do {
  359. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  360. /* PHY may not generate a 156.25 MHz clock and MAC
  361. * stats fetch will fail. */
  362. efx_stats_disable(efx);
  363. sfn4111t_reset(efx);
  364. }
  365. rc = sft9001_wait_boot(efx);
  366. if (rc == 0)
  367. return 0;
  368. efx->phy_mode = PHY_MODE_SPECIAL;
  369. } while (rc == -EINVAL && ++i < 2);
  370. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  371. fail_hwmon:
  372. i2c_unregister_device(efx->board_info.hwmon_client);
  373. return rc;
  374. }