sfe4001.c 8.9 KB

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