i2c-direct.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005 Fen Systems Ltd.
  4. * Copyright 2006-2008 Solarflare Communications Inc.
  5. *
  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.
  9. */
  10. #include <linux/delay.h>
  11. #include "net_driver.h"
  12. #include "i2c-direct.h"
  13. /*
  14. * I2C data (SDA) and clock (SCL) line read/writes with appropriate
  15. * delays.
  16. */
  17. static inline void setsda(struct efx_i2c_interface *i2c, int state)
  18. {
  19. udelay(i2c->op->udelay);
  20. i2c->sda = state;
  21. i2c->op->setsda(i2c);
  22. udelay(i2c->op->udelay);
  23. }
  24. static inline void setscl(struct efx_i2c_interface *i2c, int state)
  25. {
  26. udelay(i2c->op->udelay);
  27. i2c->scl = state;
  28. i2c->op->setscl(i2c);
  29. udelay(i2c->op->udelay);
  30. }
  31. static inline int getsda(struct efx_i2c_interface *i2c)
  32. {
  33. int sda;
  34. udelay(i2c->op->udelay);
  35. sda = i2c->op->getsda(i2c);
  36. udelay(i2c->op->udelay);
  37. return sda;
  38. }
  39. static inline int getscl(struct efx_i2c_interface *i2c)
  40. {
  41. int scl;
  42. udelay(i2c->op->udelay);
  43. scl = i2c->op->getscl(i2c);
  44. udelay(i2c->op->udelay);
  45. return scl;
  46. }
  47. /*
  48. * I2C low-level protocol operations
  49. *
  50. */
  51. static inline void i2c_release(struct efx_i2c_interface *i2c)
  52. {
  53. EFX_WARN_ON_PARANOID(!i2c->scl);
  54. EFX_WARN_ON_PARANOID(!i2c->sda);
  55. /* Devices may time out if operations do not end */
  56. setscl(i2c, 1);
  57. setsda(i2c, 1);
  58. EFX_BUG_ON_PARANOID(getsda(i2c) != 1);
  59. EFX_BUG_ON_PARANOID(getscl(i2c) != 1);
  60. }
  61. static inline void i2c_start(struct efx_i2c_interface *i2c)
  62. {
  63. /* We may be restarting immediately after a {send,recv}_bit,
  64. * so SCL will not necessarily already be high.
  65. */
  66. EFX_WARN_ON_PARANOID(!i2c->sda);
  67. setscl(i2c, 1);
  68. setsda(i2c, 0);
  69. setscl(i2c, 0);
  70. setsda(i2c, 1);
  71. }
  72. static inline void i2c_send_bit(struct efx_i2c_interface *i2c, int bit)
  73. {
  74. EFX_WARN_ON_PARANOID(i2c->scl != 0);
  75. setsda(i2c, bit);
  76. setscl(i2c, 1);
  77. setscl(i2c, 0);
  78. setsda(i2c, 1);
  79. }
  80. static inline int i2c_recv_bit(struct efx_i2c_interface *i2c)
  81. {
  82. int bit;
  83. EFX_WARN_ON_PARANOID(i2c->scl != 0);
  84. EFX_WARN_ON_PARANOID(!i2c->sda);
  85. setscl(i2c, 1);
  86. bit = getsda(i2c);
  87. setscl(i2c, 0);
  88. return bit;
  89. }
  90. static inline void i2c_stop(struct efx_i2c_interface *i2c)
  91. {
  92. EFX_WARN_ON_PARANOID(i2c->scl != 0);
  93. setsda(i2c, 0);
  94. setscl(i2c, 1);
  95. setsda(i2c, 1);
  96. }
  97. /*
  98. * I2C mid-level protocol operations
  99. *
  100. */
  101. /* Sends a byte via the I2C bus and checks for an acknowledgement from
  102. * the slave device.
  103. */
  104. static int i2c_send_byte(struct efx_i2c_interface *i2c, u8 byte)
  105. {
  106. int i;
  107. /* Send byte */
  108. for (i = 0; i < 8; i++) {
  109. i2c_send_bit(i2c, !!(byte & 0x80));
  110. byte <<= 1;
  111. }
  112. /* Check for acknowledgement from slave */
  113. return (i2c_recv_bit(i2c) == 0 ? 0 : -EIO);
  114. }
  115. /* Receives a byte via the I2C bus and sends ACK/NACK to the slave device. */
  116. static u8 i2c_recv_byte(struct efx_i2c_interface *i2c, int ack)
  117. {
  118. u8 value = 0;
  119. int i;
  120. /* Receive byte */
  121. for (i = 0; i < 8; i++)
  122. value = (value << 1) | i2c_recv_bit(i2c);
  123. /* Send ACK/NACK */
  124. i2c_send_bit(i2c, (ack ? 0 : 1));
  125. return value;
  126. }
  127. /* Calculate command byte for a read operation */
  128. static inline u8 i2c_read_cmd(u8 device_id)
  129. {
  130. return ((device_id << 1) | 1);
  131. }
  132. /* Calculate command byte for a write operation */
  133. static inline u8 i2c_write_cmd(u8 device_id)
  134. {
  135. return ((device_id << 1) | 0);
  136. }
  137. int efx_i2c_check_presence(struct efx_i2c_interface *i2c, u8 device_id)
  138. {
  139. int rc;
  140. /* If someone is driving the bus low we just give up. */
  141. if (getsda(i2c) == 0 || getscl(i2c) == 0) {
  142. EFX_ERR(i2c->efx, "%s someone is holding the I2C bus low."
  143. " Giving up.\n", __func__);
  144. return -EFAULT;
  145. }
  146. /* Pretend to initiate a device write */
  147. i2c_start(i2c);
  148. rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
  149. if (rc)
  150. goto out;
  151. out:
  152. i2c_stop(i2c);
  153. i2c_release(i2c);
  154. return rc;
  155. }
  156. /* This performs a fast read of one or more consecutive bytes from an
  157. * I2C device. Not all devices support consecutive reads of more than
  158. * one byte; for these devices use efx_i2c_read() instead.
  159. */
  160. int efx_i2c_fast_read(struct efx_i2c_interface *i2c,
  161. u8 device_id, u8 offset, u8 *data, unsigned int len)
  162. {
  163. int i;
  164. int rc;
  165. EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
  166. EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
  167. EFX_WARN_ON_PARANOID(data == NULL);
  168. EFX_WARN_ON_PARANOID(len < 1);
  169. /* Select device and starting offset */
  170. i2c_start(i2c);
  171. rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
  172. if (rc)
  173. goto out;
  174. rc = i2c_send_byte(i2c, offset);
  175. if (rc)
  176. goto out;
  177. /* Read data from device */
  178. i2c_start(i2c);
  179. rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
  180. if (rc)
  181. goto out;
  182. for (i = 0; i < (len - 1); i++)
  183. /* Read and acknowledge all but the last byte */
  184. data[i] = i2c_recv_byte(i2c, 1);
  185. /* Read last byte with no acknowledgement */
  186. data[i] = i2c_recv_byte(i2c, 0);
  187. out:
  188. i2c_stop(i2c);
  189. i2c_release(i2c);
  190. return rc;
  191. }
  192. /* This performs a fast write of one or more consecutive bytes to an
  193. * I2C device. Not all devices support consecutive writes of more
  194. * than one byte; for these devices use efx_i2c_write() instead.
  195. */
  196. int efx_i2c_fast_write(struct efx_i2c_interface *i2c,
  197. u8 device_id, u8 offset,
  198. const u8 *data, unsigned int len)
  199. {
  200. int i;
  201. int rc;
  202. EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
  203. EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
  204. EFX_WARN_ON_PARANOID(len < 1);
  205. /* Select device and starting offset */
  206. i2c_start(i2c);
  207. rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
  208. if (rc)
  209. goto out;
  210. rc = i2c_send_byte(i2c, offset);
  211. if (rc)
  212. goto out;
  213. /* Write data to device */
  214. for (i = 0; i < len; i++) {
  215. rc = i2c_send_byte(i2c, data[i]);
  216. if (rc)
  217. goto out;
  218. }
  219. out:
  220. i2c_stop(i2c);
  221. i2c_release(i2c);
  222. return rc;
  223. }
  224. /* I2C byte-by-byte read */
  225. int efx_i2c_read(struct efx_i2c_interface *i2c,
  226. u8 device_id, u8 offset, u8 *data, unsigned int len)
  227. {
  228. int rc;
  229. /* i2c_fast_read with length 1 is a single byte read */
  230. for (; len > 0; offset++, data++, len--) {
  231. rc = efx_i2c_fast_read(i2c, device_id, offset, data, 1);
  232. if (rc)
  233. return rc;
  234. }
  235. return 0;
  236. }
  237. /* I2C byte-by-byte write */
  238. int efx_i2c_write(struct efx_i2c_interface *i2c,
  239. u8 device_id, u8 offset, const u8 *data, unsigned int len)
  240. {
  241. int rc;
  242. /* i2c_fast_write with length 1 is a single byte write */
  243. for (; len > 0; offset++, data++, len--) {
  244. rc = efx_i2c_fast_write(i2c, device_id, offset, data, 1);
  245. if (rc)
  246. return rc;
  247. mdelay(i2c->op->mdelay);
  248. }
  249. return 0;
  250. }
  251. /* This is just a slightly neater wrapper round efx_i2c_fast_write
  252. * in the case where the target doesn't take an offset
  253. */
  254. int efx_i2c_send_bytes(struct efx_i2c_interface *i2c,
  255. u8 device_id, const u8 *data, unsigned int len)
  256. {
  257. return efx_i2c_fast_write(i2c, device_id, data[0], data + 1, len - 1);
  258. }
  259. /* I2C receiving of bytes - does not send an offset byte */
  260. int efx_i2c_recv_bytes(struct efx_i2c_interface *i2c, u8 device_id,
  261. u8 *bytes, unsigned int len)
  262. {
  263. int i;
  264. int rc;
  265. EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
  266. EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
  267. EFX_WARN_ON_PARANOID(len < 1);
  268. /* Select device */
  269. i2c_start(i2c);
  270. /* Read data from device */
  271. rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
  272. if (rc)
  273. goto out;
  274. for (i = 0; i < (len - 1); i++)
  275. /* Read and acknowledge all but the last byte */
  276. bytes[i] = i2c_recv_byte(i2c, 1);
  277. /* Read last byte with no acknowledgement */
  278. bytes[i] = i2c_recv_byte(i2c, 0);
  279. out:
  280. i2c_stop(i2c);
  281. i2c_release(i2c);
  282. return rc;
  283. }
  284. /* SMBus and some I2C devices will time out if the I2C clock is
  285. * held low for too long. This is most likely to happen in virtualised
  286. * systems (when the entire domain is descheduled) but could in
  287. * principle happen due to preemption on any busy system (and given the
  288. * potential length of an I2C operation turning preemption off is not
  289. * a sensible option). The following functions deal with the failure by
  290. * retrying up to a fixed number of times.
  291. */
  292. #define I2C_MAX_RETRIES (10)
  293. /* The timeout problem will result in -EIO. If the wrapped function
  294. * returns any other error, pass this up and do not retry. */
  295. #define RETRY_WRAPPER(_f) \
  296. int retries = I2C_MAX_RETRIES; \
  297. int rc; \
  298. while (retries) { \
  299. rc = _f; \
  300. if (rc != -EIO) \
  301. return rc; \
  302. retries--; \
  303. } \
  304. return rc; \
  305. int efx_i2c_check_presence_retry(struct efx_i2c_interface *i2c, u8 device_id)
  306. {
  307. RETRY_WRAPPER(efx_i2c_check_presence(i2c, device_id))
  308. }
  309. int efx_i2c_read_retry(struct efx_i2c_interface *i2c,
  310. u8 device_id, u8 offset, u8 *data, unsigned int len)
  311. {
  312. RETRY_WRAPPER(efx_i2c_read(i2c, device_id, offset, data, len))
  313. }
  314. int efx_i2c_write_retry(struct efx_i2c_interface *i2c,
  315. u8 device_id, u8 offset, const u8 *data, unsigned int len)
  316. {
  317. RETRY_WRAPPER(efx_i2c_write(i2c, device_id, offset, data, len))
  318. }