sfe4001.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  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. /*****************************************************************************
  10. * Support for the SFE4001 NIC: driver code for the PCA9539 I/O expander that
  11. * controls the PHY power rails, and for the MAX6647 temp. sensor used to check
  12. * the PHY
  13. */
  14. #include <linux/delay.h>
  15. #include "net_driver.h"
  16. #include "efx.h"
  17. #include "phy.h"
  18. #include "boards.h"
  19. #include "falcon.h"
  20. #include "falcon_hwdefs.h"
  21. #include "falcon_io.h"
  22. #include "mac.h"
  23. /**************************************************************************
  24. *
  25. * I2C IO Expander device
  26. *
  27. **************************************************************************/
  28. #define PCA9539 0x74
  29. #define P0_IN 0x00
  30. #define P0_OUT 0x02
  31. #define P0_INVERT 0x04
  32. #define P0_CONFIG 0x06
  33. #define P0_EN_1V0X_LBN 0
  34. #define P0_EN_1V0X_WIDTH 1
  35. #define P0_EN_1V2_LBN 1
  36. #define P0_EN_1V2_WIDTH 1
  37. #define P0_EN_2V5_LBN 2
  38. #define P0_EN_2V5_WIDTH 1
  39. #define P0_EN_3V3X_LBN 3
  40. #define P0_EN_3V3X_WIDTH 1
  41. #define P0_EN_5V_LBN 4
  42. #define P0_EN_5V_WIDTH 1
  43. #define P0_SHORTEN_JTAG_LBN 5
  44. #define P0_SHORTEN_JTAG_WIDTH 1
  45. #define P0_X_TRST_LBN 6
  46. #define P0_X_TRST_WIDTH 1
  47. #define P0_DSP_RESET_LBN 7
  48. #define P0_DSP_RESET_WIDTH 1
  49. #define P1_IN 0x01
  50. #define P1_OUT 0x03
  51. #define P1_INVERT 0x05
  52. #define P1_CONFIG 0x07
  53. #define P1_AFE_PWD_LBN 0
  54. #define P1_AFE_PWD_WIDTH 1
  55. #define P1_DSP_PWD25_LBN 1
  56. #define P1_DSP_PWD25_WIDTH 1
  57. #define P1_RESERVED_LBN 2
  58. #define P1_RESERVED_WIDTH 2
  59. #define P1_SPARE_LBN 4
  60. #define P1_SPARE_WIDTH 4
  61. /**************************************************************************
  62. *
  63. * Temperature Sensor
  64. *
  65. **************************************************************************/
  66. #define MAX6647 0x4e
  67. #define RLTS 0x00
  68. #define RLTE 0x01
  69. #define RSL 0x02
  70. #define RCL 0x03
  71. #define RCRA 0x04
  72. #define RLHN 0x05
  73. #define RLLI 0x06
  74. #define RRHI 0x07
  75. #define RRLS 0x08
  76. #define WCRW 0x0a
  77. #define WLHO 0x0b
  78. #define WRHA 0x0c
  79. #define WRLN 0x0e
  80. #define OSHT 0x0f
  81. #define REET 0x10
  82. #define RIET 0x11
  83. #define RWOE 0x19
  84. #define RWOI 0x20
  85. #define HYS 0x21
  86. #define QUEUE 0x22
  87. #define MFID 0xfe
  88. #define REVID 0xff
  89. /* Status bits */
  90. #define MAX6647_BUSY (1 << 7) /* ADC is converting */
  91. #define MAX6647_LHIGH (1 << 6) /* Local high temp. alarm */
  92. #define MAX6647_LLOW (1 << 5) /* Local low temp. alarm */
  93. #define MAX6647_RHIGH (1 << 4) /* Remote high temp. alarm */
  94. #define MAX6647_RLOW (1 << 3) /* Remote low temp. alarm */
  95. #define MAX6647_FAULT (1 << 2) /* DXN/DXP short/open circuit */
  96. #define MAX6647_EOT (1 << 1) /* Remote junction overtemp. */
  97. #define MAX6647_IOT (1 << 0) /* Local junction overtemp. */
  98. static const u8 xgphy_max_temperature = 90;
  99. static void sfe4001_poweroff(struct efx_nic *efx)
  100. {
  101. struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
  102. struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
  103. /* Turn off all power rails and disable outputs */
  104. i2c_smbus_write_byte_data(ioexp_client, P0_OUT, 0xff);
  105. i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG, 0xff);
  106. i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0xff);
  107. /* Clear any over-temperature alert */
  108. i2c_smbus_read_byte_data(hwmon_client, RSL);
  109. }
  110. static int sfe4001_poweron(struct efx_nic *efx)
  111. {
  112. struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
  113. struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
  114. unsigned int i, j;
  115. int rc;
  116. u8 out;
  117. efx_oword_t reg;
  118. /* Ensure that XGXS and XAUI SerDes are held in reset */
  119. EFX_POPULATE_OWORD_7(reg, XX_PWRDNA_EN, 1,
  120. XX_PWRDNB_EN, 1,
  121. XX_RSTPLLAB_EN, 1,
  122. XX_RESETA_EN, 1,
  123. XX_RESETB_EN, 1,
  124. XX_RSTXGXSRX_EN, 1,
  125. XX_RSTXGXSTX_EN, 1);
  126. falcon_write(efx, &reg, XX_PWR_RST_REG);
  127. udelay(10);
  128. /* Clear any previous over-temperature alert */
  129. rc = i2c_smbus_read_byte_data(hwmon_client, RSL);
  130. if (rc < 0)
  131. return rc;
  132. /* Enable port 0 and port 1 outputs on IO expander */
  133. rc = i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0x00);
  134. if (rc)
  135. return rc;
  136. rc = i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG,
  137. 0xff & ~(1 << P1_SPARE_LBN));
  138. if (rc)
  139. goto fail_on;
  140. /* If PHY power is on, turn it all off and wait 1 second to
  141. * ensure a full reset.
  142. */
  143. rc = i2c_smbus_read_byte_data(ioexp_client, P0_OUT);
  144. if (rc < 0)
  145. goto fail_on;
  146. out = 0xff & ~((0 << P0_EN_1V2_LBN) | (0 << P0_EN_2V5_LBN) |
  147. (0 << P0_EN_3V3X_LBN) | (0 << P0_EN_5V_LBN) |
  148. (0 << P0_EN_1V0X_LBN));
  149. if (rc != out) {
  150. EFX_INFO(efx, "power-cycling PHY\n");
  151. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  152. if (rc)
  153. goto fail_on;
  154. schedule_timeout_uninterruptible(HZ);
  155. }
  156. for (i = 0; i < 20; ++i) {
  157. /* Turn on 1.2V, 2.5V, 3.3V and 5V power rails */
  158. out = 0xff & ~((1 << P0_EN_1V2_LBN) | (1 << P0_EN_2V5_LBN) |
  159. (1 << P0_EN_3V3X_LBN) | (1 << P0_EN_5V_LBN) |
  160. (1 << P0_X_TRST_LBN));
  161. if (efx->phy_mode & PHY_MODE_SPECIAL)
  162. out |= 1 << P0_EN_3V3X_LBN;
  163. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  164. if (rc)
  165. goto fail_on;
  166. msleep(10);
  167. /* Turn on 1V power rail */
  168. out &= ~(1 << P0_EN_1V0X_LBN);
  169. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  170. if (rc)
  171. goto fail_on;
  172. EFX_INFO(efx, "waiting for DSP boot (attempt %d)...\n", i);
  173. /* In flash config mode, DSP does not turn on AFE, so
  174. * just wait 1 second.
  175. */
  176. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  177. schedule_timeout_uninterruptible(HZ);
  178. return 0;
  179. }
  180. for (j = 0; j < 10; ++j) {
  181. msleep(100);
  182. /* Check DSP has asserted AFE power line */
  183. rc = i2c_smbus_read_byte_data(ioexp_client, P1_IN);
  184. if (rc < 0)
  185. goto fail_on;
  186. if (rc & (1 << P1_AFE_PWD_LBN))
  187. return 0;
  188. }
  189. }
  190. EFX_INFO(efx, "timed out waiting for DSP boot\n");
  191. rc = -ETIMEDOUT;
  192. fail_on:
  193. sfe4001_poweroff(efx);
  194. return rc;
  195. }
  196. /* On SFE4001 rev A2 and later, we can control the FLASH_CFG_1 pin
  197. * using the 3V3X output of the IO-expander. Allow the user to set
  198. * this when the device is stopped, and keep it stopped then.
  199. */
  200. static ssize_t show_phy_flash_cfg(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  204. return sprintf(buf, "%d\n", !!(efx->phy_mode & PHY_MODE_SPECIAL));
  205. }
  206. static ssize_t set_phy_flash_cfg(struct device *dev,
  207. struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  211. enum efx_phy_mode old_mode, new_mode;
  212. int err;
  213. rtnl_lock();
  214. old_mode = efx->phy_mode;
  215. if (count == 0 || *buf == '0')
  216. new_mode = old_mode & ~PHY_MODE_SPECIAL;
  217. else
  218. new_mode = PHY_MODE_SPECIAL;
  219. if (old_mode == new_mode) {
  220. err = 0;
  221. } else if (efx->state != STATE_RUNNING || netif_running(efx->net_dev)) {
  222. err = -EBUSY;
  223. } else {
  224. efx->phy_mode = new_mode;
  225. err = sfe4001_poweron(efx);
  226. efx_reconfigure_port(efx);
  227. }
  228. rtnl_unlock();
  229. return err ? err : count;
  230. }
  231. static DEVICE_ATTR(phy_flash_cfg, 0644, show_phy_flash_cfg, set_phy_flash_cfg);
  232. static void sfe4001_fini(struct efx_nic *efx)
  233. {
  234. EFX_INFO(efx, "%s\n", __func__);
  235. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  236. sfe4001_poweroff(efx);
  237. i2c_unregister_device(efx->board_info.ioexp_client);
  238. i2c_unregister_device(efx->board_info.hwmon_client);
  239. }
  240. /* This board uses an I2C expander to provider power to the PHY, which needs to
  241. * be turned on before the PHY can be used.
  242. * Context: Process context, rtnl lock held
  243. */
  244. int sfe4001_init(struct efx_nic *efx)
  245. {
  246. struct i2c_client *hwmon_client;
  247. int rc;
  248. hwmon_client = i2c_new_dummy(&efx->i2c_adap, MAX6647);
  249. if (!hwmon_client)
  250. return -EIO;
  251. efx->board_info.hwmon_client = hwmon_client;
  252. /* Set DSP over-temperature alert threshold */
  253. EFX_INFO(efx, "DSP cut-out at %dC\n", xgphy_max_temperature);
  254. rc = i2c_smbus_write_byte_data(hwmon_client, WLHO,
  255. xgphy_max_temperature);
  256. if (rc)
  257. goto fail_ioexp;
  258. /* Read it back and verify */
  259. rc = i2c_smbus_read_byte_data(hwmon_client, RLHN);
  260. if (rc < 0)
  261. goto fail_ioexp;
  262. if (rc != xgphy_max_temperature) {
  263. rc = -EFAULT;
  264. goto fail_ioexp;
  265. }
  266. efx->board_info.ioexp_client = i2c_new_dummy(&efx->i2c_adap, PCA9539);
  267. if (!efx->board_info.ioexp_client) {
  268. rc = -EIO;
  269. goto fail_hwmon;
  270. }
  271. /* 10Xpress has fixed-function LED pins, so there is no board-specific
  272. * blink code. */
  273. efx->board_info.blink = tenxpress_phy_blink;
  274. efx->board_info.fini = sfe4001_fini;
  275. rc = sfe4001_poweron(efx);
  276. if (rc)
  277. goto fail_ioexp;
  278. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  279. if (rc)
  280. goto fail_on;
  281. EFX_INFO(efx, "PHY is powered on\n");
  282. return 0;
  283. fail_on:
  284. sfe4001_poweroff(efx);
  285. fail_ioexp:
  286. i2c_unregister_device(efx->board_info.ioexp_client);
  287. fail_hwmon:
  288. i2c_unregister_device(hwmon_client);
  289. return rc;
  290. }