blob: aa576c559ec8b5003f3a9a9a59c434fbc476df8f [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
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/*****************************************************************************
11 * Support for the SFE4001 NIC: driver code for the PCA9539 I/O expander that
12 * controls the PHY power rails, and for the MAX6647 temp. sensor used to check
13 * the PHY
14 */
15#include <linux/delay.h>
Ben Hutchingsf8b87c12008-09-01 12:48:17 +010016#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010017#include "efx.h"
18#include "phy.h"
19#include "boards.h"
20#include "falcon.h"
21#include "falcon_hwdefs.h"
Ben Hutchingsc1e5fcc2008-09-01 12:48:41 +010022#include "falcon_io.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010023#include "mac.h"
Ben Hutchings3e133c42008-11-04 20:34:56 +000024#include "workarounds.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010025
26/**************************************************************************
27 *
28 * I2C IO Expander device
29 *
30 **************************************************************************/
31#define PCA9539 0x74
32
33#define P0_IN 0x00
34#define P0_OUT 0x02
35#define P0_INVERT 0x04
36#define P0_CONFIG 0x06
37
38#define P0_EN_1V0X_LBN 0
39#define P0_EN_1V0X_WIDTH 1
40#define P0_EN_1V2_LBN 1
41#define P0_EN_1V2_WIDTH 1
42#define P0_EN_2V5_LBN 2
43#define P0_EN_2V5_WIDTH 1
44#define P0_EN_3V3X_LBN 3
45#define P0_EN_3V3X_WIDTH 1
46#define P0_EN_5V_LBN 4
47#define P0_EN_5V_WIDTH 1
48#define P0_SHORTEN_JTAG_LBN 5
49#define P0_SHORTEN_JTAG_WIDTH 1
50#define P0_X_TRST_LBN 6
51#define P0_X_TRST_WIDTH 1
52#define P0_DSP_RESET_LBN 7
53#define P0_DSP_RESET_WIDTH 1
54
55#define P1_IN 0x01
56#define P1_OUT 0x03
57#define P1_INVERT 0x05
58#define P1_CONFIG 0x07
59
60#define P1_AFE_PWD_LBN 0
61#define P1_AFE_PWD_WIDTH 1
62#define P1_DSP_PWD25_LBN 1
63#define P1_DSP_PWD25_WIDTH 1
64#define P1_RESERVED_LBN 2
65#define P1_RESERVED_WIDTH 2
66#define P1_SPARE_LBN 4
67#define P1_SPARE_WIDTH 4
68
Ben Hutchings3e133c42008-11-04 20:34:56 +000069/* Temperature Sensor */
70#define MAX664X_REG_RSL 0x02
71#define MAX664X_REG_WLHO 0x0B
Ben Hutchings8ceee662008-04-27 12:55:59 +010072
Ben Hutchings37b5a602008-05-30 22:27:04 +010073static void sfe4001_poweroff(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +010074{
Ben Hutchings37b5a602008-05-30 22:27:04 +010075 struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
76 struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
Ben Hutchings8ceee662008-04-27 12:55:59 +010077
Ben Hutchings37b5a602008-05-30 22:27:04 +010078 /* Turn off all power rails and disable outputs */
79 i2c_smbus_write_byte_data(ioexp_client, P0_OUT, 0xff);
80 i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG, 0xff);
81 i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0xff);
Ben Hutchings8ceee662008-04-27 12:55:59 +010082
83 /* Clear any over-temperature alert */
Ben Hutchings3e133c42008-11-04 20:34:56 +000084 i2c_smbus_read_byte_data(hwmon_client, MAX664X_REG_RSL);
Ben Hutchings37b5a602008-05-30 22:27:04 +010085}
86
Ben Hutchingsf8b87c12008-09-01 12:48:17 +010087static int sfe4001_poweron(struct efx_nic *efx)
Ben Hutchings37b5a602008-05-30 22:27:04 +010088{
Ben Hutchingsf8b87c12008-09-01 12:48:17 +010089 struct i2c_client *hwmon_client = efx->board_info.hwmon_client;
90 struct i2c_client *ioexp_client = efx->board_info.ioexp_client;
Ben Hutchings11f34e62008-09-01 12:45:48 +010091 unsigned int i, j;
Ben Hutchings8ceee662008-04-27 12:55:59 +010092 int rc;
Ben Hutchings37b5a602008-05-30 22:27:04 +010093 u8 out;
Ben Hutchings8ceee662008-04-27 12:55:59 +010094
Ben Hutchings8ceee662008-04-27 12:55:59 +010095 /* Clear any previous over-temperature alert */
Ben Hutchings3e133c42008-11-04 20:34:56 +000096 rc = i2c_smbus_read_byte_data(hwmon_client, MAX664X_REG_RSL);
Ben Hutchings37b5a602008-05-30 22:27:04 +010097 if (rc < 0)
Ben Hutchingsf8b87c12008-09-01 12:48:17 +010098 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +010099
100 /* Enable port 0 and port 1 outputs on IO expander */
Ben Hutchings37b5a602008-05-30 22:27:04 +0100101 rc = i2c_smbus_write_byte_data(ioexp_client, P0_CONFIG, 0x00);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100102 if (rc)
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100103 return rc;
Ben Hutchings37b5a602008-05-30 22:27:04 +0100104 rc = i2c_smbus_write_byte_data(ioexp_client, P1_CONFIG,
105 0xff & ~(1 << P1_SPARE_LBN));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100106 if (rc)
Ben Hutchings37b5a602008-05-30 22:27:04 +0100107 goto fail_on;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108
Ben Hutchings11f34e62008-09-01 12:45:48 +0100109 /* If PHY power is on, turn it all off and wait 1 second to
110 * ensure a full reset.
111 */
112 rc = i2c_smbus_read_byte_data(ioexp_client, P0_OUT);
113 if (rc < 0)
114 goto fail_on;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100115 out = 0xff & ~((0 << P0_EN_1V2_LBN) | (0 << P0_EN_2V5_LBN) |
116 (0 << P0_EN_3V3X_LBN) | (0 << P0_EN_5V_LBN) |
117 (0 << P0_EN_1V0X_LBN));
Ben Hutchings11f34e62008-09-01 12:45:48 +0100118 if (rc != out) {
119 EFX_INFO(efx, "power-cycling PHY\n");
120 rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
121 if (rc)
122 goto fail_on;
123 schedule_timeout_uninterruptible(HZ);
124 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125
Ben Hutchings11f34e62008-09-01 12:45:48 +0100126 for (i = 0; i < 20; ++i) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127 /* Turn on 1.2V, 2.5V, 3.3V and 5V power rails */
128 out = 0xff & ~((1 << P0_EN_1V2_LBN) | (1 << P0_EN_2V5_LBN) |
129 (1 << P0_EN_3V3X_LBN) | (1 << P0_EN_5V_LBN) |
130 (1 << P0_X_TRST_LBN));
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100131 if (efx->phy_mode & PHY_MODE_SPECIAL)
Ben Hutchings75f2d3e2008-05-07 12:55:13 +0100132 out |= 1 << P0_EN_3V3X_LBN;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100133
Ben Hutchings37b5a602008-05-30 22:27:04 +0100134 rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135 if (rc)
Ben Hutchings37b5a602008-05-30 22:27:04 +0100136 goto fail_on;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100137 msleep(10);
138
139 /* Turn on 1V power rail */
140 out &= ~(1 << P0_EN_1V0X_LBN);
Ben Hutchings37b5a602008-05-30 22:27:04 +0100141 rc = i2c_smbus_write_byte_data(ioexp_client, P0_OUT, out);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100142 if (rc)
Ben Hutchings37b5a602008-05-30 22:27:04 +0100143 goto fail_on;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100144
Ben Hutchings11f34e62008-09-01 12:45:48 +0100145 EFX_INFO(efx, "waiting for DSP boot (attempt %d)...\n", i);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100146
Ben Hutchings11f34e62008-09-01 12:45:48 +0100147 /* In flash config mode, DSP does not turn on AFE, so
148 * just wait 1 second.
149 */
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100150 if (efx->phy_mode & PHY_MODE_SPECIAL) {
Ben Hutchings11f34e62008-09-01 12:45:48 +0100151 schedule_timeout_uninterruptible(HZ);
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100152 return 0;
Ben Hutchings11f34e62008-09-01 12:45:48 +0100153 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100154
Ben Hutchings11f34e62008-09-01 12:45:48 +0100155 for (j = 0; j < 10; ++j) {
156 msleep(100);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100157
Ben Hutchings11f34e62008-09-01 12:45:48 +0100158 /* Check DSP has asserted AFE power line */
159 rc = i2c_smbus_read_byte_data(ioexp_client, P1_IN);
160 if (rc < 0)
161 goto fail_on;
162 if (rc & (1 << P1_AFE_PWD_LBN))
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100163 return 0;
Ben Hutchings11f34e62008-09-01 12:45:48 +0100164 }
165 }
166
167 EFX_INFO(efx, "timed out waiting for DSP boot\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100168 rc = -ETIMEDOUT;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100169fail_on:
170 sfe4001_poweroff(efx);
171 return rc;
172}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100173
Ben Hutchings3e133c42008-11-04 20:34:56 +0000174static int sfe4001_check_hw(struct efx_nic *efx)
175{
176 s32 status;
177
178 /* If XAUI link is up then do not monitor */
179 if (EFX_WORKAROUND_7884(efx) && falcon_xaui_link_ok(efx))
180 return 0;
181
182 /* Check the powered status of the PHY. Lack of power implies that
183 * the MAX6647 has shut down power to it, probably due to a temp.
184 * alarm. Reading the power status rather than the MAX6647 status
185 * directly because the later is read-to-clear and would thus
186 * start to power up the PHY again when polled, causing us to blip
187 * the power undesirably.
188 * We know we can read from the IO expander because we did
189 * it during power-on. Assume failure now is bad news. */
190 status = i2c_smbus_read_byte_data(efx->board_info.ioexp_client, P1_IN);
191 if (status >= 0 &&
192 (status & ((1 << P1_AFE_PWD_LBN) | (1 << P1_DSP_PWD25_LBN))) != 0)
193 return 0;
194
195 /* Use board power control, not PHY power control */
196 sfe4001_poweroff(efx);
197 efx->phy_mode = PHY_MODE_OFF;
198
199 return (status < 0) ? -EIO : -ERANGE;
200}
201
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100202/* On SFE4001 rev A2 and later, we can control the FLASH_CFG_1 pin
203 * using the 3V3X output of the IO-expander. Allow the user to set
204 * this when the device is stopped, and keep it stopped then.
205 */
206
207static ssize_t show_phy_flash_cfg(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
211 return sprintf(buf, "%d\n", !!(efx->phy_mode & PHY_MODE_SPECIAL));
212}
213
214static ssize_t set_phy_flash_cfg(struct device *dev,
215 struct device_attribute *attr,
216 const char *buf, size_t count)
217{
218 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
219 enum efx_phy_mode old_mode, new_mode;
220 int err;
221
222 rtnl_lock();
223 old_mode = efx->phy_mode;
224 if (count == 0 || *buf == '0')
225 new_mode = old_mode & ~PHY_MODE_SPECIAL;
226 else
227 new_mode = PHY_MODE_SPECIAL;
228 if (old_mode == new_mode) {
229 err = 0;
230 } else if (efx->state != STATE_RUNNING || netif_running(efx->net_dev)) {
231 err = -EBUSY;
232 } else {
233 efx->phy_mode = new_mode;
234 err = sfe4001_poweron(efx);
235 efx_reconfigure_port(efx);
236 }
237 rtnl_unlock();
238
239 return err ? err : count;
240}
241
242static DEVICE_ATTR(phy_flash_cfg, 0644, show_phy_flash_cfg, set_phy_flash_cfg);
243
244static void sfe4001_fini(struct efx_nic *efx)
245{
246 EFX_INFO(efx, "%s\n", __func__);
247
248 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
249 sfe4001_poweroff(efx);
250 i2c_unregister_device(efx->board_info.ioexp_client);
251 i2c_unregister_device(efx->board_info.hwmon_client);
252}
253
Ben Hutchings3e133c42008-11-04 20:34:56 +0000254static struct i2c_board_info sfe4001_hwmon_info = {
255 I2C_BOARD_INFO("max6647", 0x4e),
256 .irq = -1,
257};
258
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100259/* This board uses an I2C expander to provider power to the PHY, which needs to
260 * be turned on before the PHY can be used.
261 * Context: Process context, rtnl lock held
262 */
263int sfe4001_init(struct efx_nic *efx)
264{
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100265 int rc;
266
Ben Hutchings3e133c42008-11-04 20:34:56 +0000267#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)
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100275 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100276
Ben Hutchings3e133c42008-11-04 20:34:56 +0000277 /* Raise board/PHY high limit from 85 to 90 degrees Celsius */
278 rc = i2c_smbus_write_byte_data(efx->board_info.hwmon_client,
279 MAX664X_REG_WLHO, 90);
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100280 if (rc)
Ben Hutchings3e133c42008-11-04 20:34:56 +0000281 goto fail_hwmon;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100282
283 efx->board_info.ioexp_client = i2c_new_dummy(&efx->i2c_adap, PCA9539);
284 if (!efx->board_info.ioexp_client) {
285 rc = -EIO;
286 goto fail_hwmon;
287 }
288
289 /* 10Xpress has fixed-function LED pins, so there is no board-specific
290 * blink code. */
291 efx->board_info.blink = tenxpress_phy_blink;
292
Ben Hutchings3e133c42008-11-04 20:34:56 +0000293 efx->board_info.monitor = sfe4001_check_hw;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100294 efx->board_info.fini = sfe4001_fini;
295
296 rc = sfe4001_poweron(efx);
297 if (rc)
298 goto fail_ioexp;
299
300 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_flash_cfg);
301 if (rc)
302 goto fail_on;
303
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 EFX_INFO(efx, "PHY is powered on\n");
305 return 0;
306
Ben Hutchings37b5a602008-05-30 22:27:04 +0100307fail_on:
308 sfe4001_poweroff(efx);
309fail_ioexp:
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100310 i2c_unregister_device(efx->board_info.ioexp_client);
Ben Hutchings37b5a602008-05-30 22:27:04 +0100311fail_hwmon:
Ben Hutchings3e133c42008-11-04 20:34:56 +0000312 i2c_unregister_device(efx->board_info.hwmon_client);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100313 return rc;
314}