sfe4001.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /* Clear any previous over-temperature alert */
  118. rc = i2c_smbus_read_byte_data(hwmon_client, RSL);
  119. if (rc < 0)
  120. return rc;
  121. /* Enable port 0 and port 1 outputs on IO expander */
  122. rc = i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0x00);
  123. if (rc)
  124. return rc;
  125. rc = i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG,
  126. 0xff & ~(1 << P1_SPARE_LBN));
  127. if (rc)
  128. goto fail_on;
  129. /* If PHY power is on, turn it all off and wait 1 second to
  130. * ensure a full reset.
  131. */
  132. rc = i2c_smbus_read_byte_data(ioexp_client, P0_OUT);
  133. if (rc < 0)
  134. goto fail_on;
  135. out = 0xff & ~((0 << P0_EN_1V2_LBN) | (0 << P0_EN_2V5_LBN) |
  136. (0 << P0_EN_3V3X_LBN) | (0 << P0_EN_5V_LBN) |
  137. (0 << P0_EN_1V0X_LBN));
  138. if (rc != out) {
  139. EFX_INFO(efx, "power-cycling PHY\n");
  140. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  141. if (rc)
  142. goto fail_on;
  143. schedule_timeout_uninterruptible(HZ);
  144. }
  145. for (i = 0; i < 20; ++i) {
  146. /* Turn on 1.2V, 2.5V, 3.3V and 5V power rails */
  147. out = 0xff & ~((1 << P0_EN_1V2_LBN) | (1 << P0_EN_2V5_LBN) |
  148. (1 << P0_EN_3V3X_LBN) | (1 << P0_EN_5V_LBN) |
  149. (1 << P0_X_TRST_LBN));
  150. if (efx->phy_mode & PHY_MODE_SPECIAL)
  151. out |= 1 << P0_EN_3V3X_LBN;
  152. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  153. if (rc)
  154. goto fail_on;
  155. msleep(10);
  156. /* Turn on 1V power rail */
  157. out &= ~(1 << P0_EN_1V0X_LBN);
  158. rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
  159. if (rc)
  160. goto fail_on;
  161. EFX_INFO(efx, "waiting for DSP boot (attempt %d)...\n", i);
  162. /* In flash config mode, DSP does not turn on AFE, so
  163. * just wait 1 second.
  164. */
  165. if (efx->phy_mode & PHY_MODE_SPECIAL) {
  166. schedule_timeout_uninterruptible(HZ);
  167. return 0;
  168. }
  169. for (j = 0; j < 10; ++j) {
  170. msleep(100);
  171. /* Check DSP has asserted AFE power line */
  172. rc = i2c_smbus_read_byte_data(ioexp_client, P1_IN);
  173. if (rc < 0)
  174. goto fail_on;
  175. if (rc & (1 << P1_AFE_PWD_LBN))
  176. return 0;
  177. }
  178. }
  179. EFX_INFO(efx, "timed out waiting for DSP boot\n");
  180. rc = -ETIMEDOUT;
  181. fail_on:
  182. sfe4001_poweroff(efx);
  183. return rc;
  184. }
  185. /* On SFE4001 rev A2 and later, we can control the FLASH_CFG_1 pin
  186. * using the 3V3X output of the IO-expander. Allow the user to set
  187. * this when the device is stopped, and keep it stopped then.
  188. */
  189. static ssize_t show_phy_flash_cfg(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  193. return sprintf(buf, "%d\n", !!(efx->phy_mode & PHY_MODE_SPECIAL));
  194. }
  195. static ssize_t set_phy_flash_cfg(struct device *dev,
  196. struct device_attribute *attr,
  197. const char *buf, size_t count)
  198. {
  199. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  200. enum efx_phy_mode old_mode, new_mode;
  201. int err;
  202. rtnl_lock();
  203. old_mode = efx->phy_mode;
  204. if (count == 0 || *buf == '0')
  205. new_mode = old_mode & ~PHY_MODE_SPECIAL;
  206. else
  207. new_mode = PHY_MODE_SPECIAL;
  208. if (old_mode == new_mode) {
  209. err = 0;
  210. } else if (efx->state != STATE_RUNNING || netif_running(efx->net_dev)) {
  211. err = -EBUSY;
  212. } else {
  213. efx->phy_mode = new_mode;
  214. err = sfe4001_poweron(efx);
  215. efx_reconfigure_port(efx);
  216. }
  217. rtnl_unlock();
  218. return err ? err : count;
  219. }
  220. static DEVICE_ATTR(phy_flash_cfg, 0644, show_phy_flash_cfg, set_phy_flash_cfg);
  221. static void sfe4001_fini(struct efx_nic *efx)
  222. {
  223. EFX_INFO(efx, "%s\n", __func__);
  224. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  225. sfe4001_poweroff(efx);
  226. i2c_unregister_device(efx->board_info.ioexp_client);
  227. i2c_unregister_device(efx->board_info.hwmon_client);
  228. }
  229. /* This board uses an I2C expander to provider power to the PHY, which needs to
  230. * be turned on before the PHY can be used.
  231. * Context: Process context, rtnl lock held
  232. */
  233. int sfe4001_init(struct efx_nic *efx)
  234. {
  235. struct i2c_client *hwmon_client;
  236. int rc;
  237. hwmon_client = i2c_new_dummy(&efx->i2c_adap, MAX6647);
  238. if (!hwmon_client)
  239. return -EIO;
  240. efx->board_info.hwmon_client = hwmon_client;
  241. /* Set DSP over-temperature alert threshold */
  242. EFX_INFO(efx, "DSP cut-out at %dC\n", xgphy_max_temperature);
  243. rc = i2c_smbus_write_byte_data(hwmon_client, WLHO,
  244. xgphy_max_temperature);
  245. if (rc)
  246. goto fail_ioexp;
  247. /* Read it back and verify */
  248. rc = i2c_smbus_read_byte_data(hwmon_client, RLHN);
  249. if (rc < 0)
  250. goto fail_ioexp;
  251. if (rc != xgphy_max_temperature) {
  252. rc = -EFAULT;
  253. goto fail_ioexp;
  254. }
  255. efx->board_info.ioexp_client = i2c_new_dummy(&efx->i2c_adap, PCA9539);
  256. if (!efx->board_info.ioexp_client) {
  257. rc = -EIO;
  258. goto fail_hwmon;
  259. }
  260. /* 10Xpress has fixed-function LED pins, so there is no board-specific
  261. * blink code. */
  262. efx->board_info.blink = tenxpress_phy_blink;
  263. efx->board_info.fini = sfe4001_fini;
  264. rc = sfe4001_poweron(efx);
  265. if (rc)
  266. goto fail_ioexp;
  267. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
  268. if (rc)
  269. goto fail_on;
  270. EFX_INFO(efx, "PHY is powered on\n");
  271. return 0;
  272. fail_on:
  273. sfe4001_poweroff(efx);
  274. fail_ioexp:
  275. i2c_unregister_device(efx->board_info.ioexp_client);
  276. fail_hwmon:
  277. i2c_unregister_device(hwmon_client);
  278. return rc;
  279. }