1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/delay.h>
12 #include "net_driver.h"
13 #include "i2c-direct.h"
16 * I2C data (SDA) and clock (SCL) line read/writes with appropriate
20 static inline void setsda(struct efx_i2c_interface *i2c, int state)
22 udelay(i2c->op->udelay);
25 udelay(i2c->op->udelay);
28 static inline void setscl(struct efx_i2c_interface *i2c, int state)
30 udelay(i2c->op->udelay);
33 udelay(i2c->op->udelay);
36 static inline int getsda(struct efx_i2c_interface *i2c)
40 udelay(i2c->op->udelay);
41 sda = i2c->op->getsda(i2c);
42 udelay(i2c->op->udelay);
46 static inline int getscl(struct efx_i2c_interface *i2c)
50 udelay(i2c->op->udelay);
51 scl = i2c->op->getscl(i2c);
52 udelay(i2c->op->udelay);
57 * I2C low-level protocol operations
61 static inline void i2c_release(struct efx_i2c_interface *i2c)
63 EFX_WARN_ON_PARANOID(!i2c->scl);
64 EFX_WARN_ON_PARANOID(!i2c->sda);
65 /* Devices may time out if operations do not end */
68 EFX_BUG_ON_PARANOID(getsda(i2c) != 1);
69 EFX_BUG_ON_PARANOID(getscl(i2c) != 1);
72 static inline void i2c_start(struct efx_i2c_interface *i2c)
74 /* We may be restarting immediately after a {send,recv}_bit,
75 * so SCL will not necessarily already be high.
77 EFX_WARN_ON_PARANOID(!i2c->sda);
84 static inline void i2c_send_bit(struct efx_i2c_interface *i2c, int bit)
86 EFX_WARN_ON_PARANOID(i2c->scl != 0);
93 static inline int i2c_recv_bit(struct efx_i2c_interface *i2c)
97 EFX_WARN_ON_PARANOID(i2c->scl != 0);
98 EFX_WARN_ON_PARANOID(!i2c->sda);
105 static inline void i2c_stop(struct efx_i2c_interface *i2c)
107 EFX_WARN_ON_PARANOID(i2c->scl != 0);
114 * I2C mid-level protocol operations
118 /* Sends a byte via the I2C bus and checks for an acknowledgement from
121 static int i2c_send_byte(struct efx_i2c_interface *i2c, u8 byte)
126 for (i = 0; i < 8; i++) {
127 i2c_send_bit(i2c, !!(byte & 0x80));
131 /* Check for acknowledgement from slave */
132 return (i2c_recv_bit(i2c) == 0 ? 0 : -EIO);
135 /* Receives a byte via the I2C bus and sends ACK/NACK to the slave device. */
136 static u8 i2c_recv_byte(struct efx_i2c_interface *i2c, int ack)
142 for (i = 0; i < 8; i++)
143 value = (value << 1) | i2c_recv_bit(i2c);
146 i2c_send_bit(i2c, (ack ? 0 : 1));
151 /* Calculate command byte for a read operation */
152 static inline u8 i2c_read_cmd(u8 device_id)
154 return ((device_id << 1) | 1);
157 /* Calculate command byte for a write operation */
158 static inline u8 i2c_write_cmd(u8 device_id)
160 return ((device_id << 1) | 0);
163 int efx_i2c_check_presence(struct efx_i2c_interface *i2c, u8 device_id)
167 /* If someone is driving the bus low we just give up. */
168 if (getsda(i2c) == 0 || getscl(i2c) == 0) {
169 EFX_ERR(i2c->efx, "%s someone is holding the I2C bus low."
170 " Giving up.\n", __func__);
174 /* Pretend to initiate a device write */
176 rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
187 /* This performs a fast read of one or more consecutive bytes from an
188 * I2C device. Not all devices support consecutive reads of more than
189 * one byte; for these devices use efx_i2c_read() instead.
191 int efx_i2c_fast_read(struct efx_i2c_interface *i2c,
192 u8 device_id, u8 offset, u8 *data, unsigned int len)
197 EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
198 EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
199 EFX_WARN_ON_PARANOID(data == NULL);
200 EFX_WARN_ON_PARANOID(len < 1);
202 /* Select device and starting offset */
204 rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
207 rc = i2c_send_byte(i2c, offset);
211 /* Read data from device */
213 rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
216 for (i = 0; i < (len - 1); i++)
217 /* Read and acknowledge all but the last byte */
218 data[i] = i2c_recv_byte(i2c, 1);
219 /* Read last byte with no acknowledgement */
220 data[i] = i2c_recv_byte(i2c, 0);
229 /* This performs a fast write of one or more consecutive bytes to an
230 * I2C device. Not all devices support consecutive writes of more
231 * than one byte; for these devices use efx_i2c_write() instead.
233 int efx_i2c_fast_write(struct efx_i2c_interface *i2c,
234 u8 device_id, u8 offset,
235 const u8 *data, unsigned int len)
240 EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
241 EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
242 EFX_WARN_ON_PARANOID(len < 1);
244 /* Select device and starting offset */
246 rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
249 rc = i2c_send_byte(i2c, offset);
253 /* Write data to device */
254 for (i = 0; i < len; i++) {
255 rc = i2c_send_byte(i2c, data[i]);
267 /* I2C byte-by-byte read */
268 int efx_i2c_read(struct efx_i2c_interface *i2c,
269 u8 device_id, u8 offset, u8 *data, unsigned int len)
273 /* i2c_fast_read with length 1 is a single byte read */
274 for (; len > 0; offset++, data++, len--) {
275 rc = efx_i2c_fast_read(i2c, device_id, offset, data, 1);
283 /* I2C byte-by-byte write */
284 int efx_i2c_write(struct efx_i2c_interface *i2c,
285 u8 device_id, u8 offset, const u8 *data, unsigned int len)
289 /* i2c_fast_write with length 1 is a single byte write */
290 for (; len > 0; offset++, data++, len--) {
291 rc = efx_i2c_fast_write(i2c, device_id, offset, data, 1);
294 mdelay(i2c->op->mdelay);
301 /* This is just a slightly neater wrapper round efx_i2c_fast_write
302 * in the case where the target doesn't take an offset
304 int efx_i2c_send_bytes(struct efx_i2c_interface *i2c,
305 u8 device_id, const u8 *data, unsigned int len)
307 return efx_i2c_fast_write(i2c, device_id, data[0], data + 1, len - 1);
310 /* I2C receiving of bytes - does not send an offset byte */
311 int efx_i2c_recv_bytes(struct efx_i2c_interface *i2c, u8 device_id,
312 u8 *bytes, unsigned int len)
317 EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
318 EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
319 EFX_WARN_ON_PARANOID(len < 1);
324 /* Read data from device */
325 rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
329 for (i = 0; i < (len - 1); i++)
330 /* Read and acknowledge all but the last byte */
331 bytes[i] = i2c_recv_byte(i2c, 1);
332 /* Read last byte with no acknowledgement */
333 bytes[i] = i2c_recv_byte(i2c, 0);
342 /* SMBus and some I2C devices will time out if the I2C clock is
343 * held low for too long. This is most likely to happen in virtualised
344 * systems (when the entire domain is descheduled) but could in
345 * principle happen due to preemption on any busy system (and given the
346 * potential length of an I2C operation turning preemption off is not
347 * a sensible option). The following functions deal with the failure by
348 * retrying up to a fixed number of times.
351 #define I2C_MAX_RETRIES (10)
353 /* The timeout problem will result in -EIO. If the wrapped function
354 * returns any other error, pass this up and do not retry. */
355 #define RETRY_WRAPPER(_f) \
356 int retries = I2C_MAX_RETRIES; \
366 int efx_i2c_check_presence_retry(struct efx_i2c_interface *i2c, u8 device_id)
368 RETRY_WRAPPER(efx_i2c_check_presence(i2c, device_id))
371 int efx_i2c_read_retry(struct efx_i2c_interface *i2c,
372 u8 device_id, u8 offset, u8 *data, unsigned int len)
374 RETRY_WRAPPER(efx_i2c_read(i2c, device_id, offset, data, len))
377 int efx_i2c_write_retry(struct efx_i2c_interface *i2c,
378 u8 device_id, u8 offset, const u8 *data, unsigned int len)
380 RETRY_WRAPPER(efx_i2c_write(i2c, device_id, offset, data, len))