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