Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

i2c_bit.c 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. #include <unistd.h>
  24. #include <gpxe/bitbash.h>
  25. #include <gpxe/i2c.h>
  26. /** @file
  27. *
  28. * I2C bit-bashing interface
  29. *
  30. * This implements a simple I2C master via a bit-bashing interface
  31. * that provides two lines: SCL (clock) and SDA (data).
  32. */
  33. /**
  34. * Delay between output state changes
  35. *
  36. * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
  37. * i.e. 200k clock transitions per second.
  38. */
  39. static void i2c_delay ( void ) {
  40. udelay ( I2C_UDELAY );
  41. }
  42. /**
  43. * Set state of I2C SCL line
  44. *
  45. * @v basher Bit-bashing interface
  46. * @v state New state of SCL
  47. */
  48. static void setscl ( struct bit_basher *basher, int state ) {
  49. DBG2 ( "%c", ( state ? '/' : '\\' ) );
  50. write_bit ( basher, I2C_BIT_SCL, state );
  51. i2c_delay();
  52. }
  53. /**
  54. * Set state of I2C SDA line
  55. *
  56. * @v basher Bit-bashing interface
  57. * @v state New state of SDA
  58. */
  59. static void setsda ( struct bit_basher *basher, int state ) {
  60. DBG2 ( "%c", ( state ? '1' : '0' ) );
  61. write_bit ( basher, I2C_BIT_SDA, state );
  62. i2c_delay();
  63. }
  64. /**
  65. * Get state of I2C SDA line
  66. *
  67. * @v basher Bit-bashing interface
  68. * @ret state State of SDA
  69. */
  70. static int getsda ( struct bit_basher *basher ) {
  71. int state;
  72. state = read_bit ( basher, I2C_BIT_SDA );
  73. DBG2 ( "%c", ( state ? '+' : '-' ) );
  74. return state;
  75. }
  76. /**
  77. * Send an I2C start condition
  78. *
  79. * @v basher Bit-bashing interface
  80. */
  81. static void i2c_start ( struct bit_basher *basher ) {
  82. setscl ( basher, 1 );
  83. setsda ( basher, 0 );
  84. setscl ( basher, 0 );
  85. setsda ( basher, 1 );
  86. }
  87. /**
  88. * Send an I2C data bit
  89. *
  90. * @v basher Bit-bashing interface
  91. * @v bit Bit to send
  92. */
  93. static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
  94. setsda ( basher, bit );
  95. setscl ( basher, 1 );
  96. setscl ( basher, 0 );
  97. setsda ( basher, 1 );
  98. }
  99. /**
  100. * Receive an I2C data bit
  101. *
  102. * @v basher Bit-bashing interface
  103. * @ret bit Received bit
  104. */
  105. static int i2c_recv_bit ( struct bit_basher *basher ) {
  106. int bit;
  107. setscl ( basher, 1 );
  108. bit = getsda ( basher );
  109. setscl ( basher, 0 );
  110. return bit;
  111. }
  112. /**
  113. * Send an I2C stop condition
  114. *
  115. * @v basher Bit-bashing interface
  116. */
  117. static void i2c_stop ( struct bit_basher *basher ) {
  118. setsda ( basher, 0 );
  119. setscl ( basher, 1 );
  120. setsda ( basher, 1 );
  121. }
  122. /**
  123. * Send byte via I2C bus and check for acknowledgement
  124. *
  125. * @v basher Bit-bashing interface
  126. * @v byte Byte to send
  127. * @ret rc Return status code
  128. *
  129. * Sends a byte via the I2C bus and checks for an acknowledgement from
  130. * the slave device.
  131. */
  132. static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
  133. int i;
  134. int ack;
  135. /* Send byte */
  136. DBG2 ( "[send %02x]", byte );
  137. for ( i = 8 ; i ; i-- ) {
  138. i2c_send_bit ( basher, byte & 0x80 );
  139. byte <<= 1;
  140. }
  141. /* Check for acknowledgement from slave */
  142. ack = ( i2c_recv_bit ( basher ) == 0 );
  143. DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
  144. return ( ack ? 0 : -EIO );
  145. }
  146. /**
  147. * Receive byte via I2C bus
  148. *
  149. * @v basher Bit-bashing interface
  150. * @ret byte Received byte
  151. *
  152. * Receives a byte via the I2C bus and sends NACK to the slave device.
  153. */
  154. static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
  155. uint8_t byte = 0;
  156. int i;
  157. /* Receive byte */
  158. for ( i = 8 ; i ; i-- ) {
  159. byte <<= 1;
  160. byte |= ( i2c_recv_bit ( basher ) & 0x1 );
  161. }
  162. /* Send NACK */
  163. i2c_send_bit ( basher, 1 );
  164. DBG2 ( "[rcvd %02x]", byte );
  165. return byte;
  166. }
  167. /**
  168. * Select I2C device for reading or writing
  169. *
  170. * @v basher Bit-bashing interface
  171. * @v i2cdev I2C device
  172. * @v offset Starting offset within the device
  173. * @v direction I2C_READ or I2C_WRITE
  174. * @ret rc Return status code
  175. */
  176. static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
  177. unsigned int offset, unsigned int direction ) {
  178. unsigned int address;
  179. int shift;
  180. unsigned int byte;
  181. int rc;
  182. i2c_start ( basher );
  183. /* Calculate address to appear on bus */
  184. address = ( ( ( i2cdev->dev_addr |
  185. ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
  186. | direction );
  187. /* Send address a byte at a time */
  188. for ( shift = ( 8 * ( i2cdev->dev_addr_len - 1 ) ) ;
  189. shift >= 0 ; shift -= 8 ) {
  190. byte = ( ( address >> shift ) & 0xff );
  191. if ( ( rc = i2c_send_byte ( basher, byte ) ) != 0 )
  192. return rc;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * Reset I2C bus
  198. *
  199. * @v basher Bit-bashing interface
  200. * @ret rc Return status code
  201. *
  202. * i2c devices often don't have a reset line, so even a reboot or
  203. * system power cycle is sometimes not enough to bring them back to a
  204. * known state.
  205. */
  206. static int i2c_reset ( struct bit_basher *basher ) {
  207. unsigned int i;
  208. int sda;
  209. /* Clock through several cycles, waiting for an opportunity to
  210. * pull SDA low while SCL is high (which creates a start
  211. * condition).
  212. */
  213. setscl ( basher, 0 );
  214. setsda ( basher, 1 );
  215. for ( i = 0 ; i < I2C_RESET_MAX_CYCLES ; i++ ) {
  216. setscl ( basher, 1 );
  217. sda = getsda ( basher );
  218. if ( sda ) {
  219. /* Now that the device will see a start, issue it */
  220. i2c_start ( basher );
  221. /* Stop the bus to leave it in a known good state */
  222. i2c_stop ( basher );
  223. DBGC ( basher, "I2CBIT %p reset after %d attempts\n",
  224. basher, ( i + 1 ) );
  225. return 0;
  226. }
  227. setscl ( basher, 0 );
  228. }
  229. DBGC ( basher, "I2CBIT %p could not reset after %d attempts\n",
  230. basher, i );
  231. return -ETIMEDOUT;
  232. }
  233. /**
  234. * Read data from I2C device via bit-bashing interface
  235. *
  236. * @v i2c I2C interface
  237. * @v i2cdev I2C device
  238. * @v offset Starting offset within the device
  239. * @v data Data buffer
  240. * @v len Length of data buffer
  241. * @ret rc Return status code
  242. *
  243. * Note that attempting to read zero bytes of data is a valid way to
  244. * check for I2C device presence.
  245. */
  246. static int i2c_bit_read ( struct i2c_interface *i2c,
  247. struct i2c_device *i2cdev, unsigned int offset,
  248. uint8_t *data, unsigned int len ) {
  249. struct i2c_bit_basher *i2cbit
  250. = container_of ( i2c, struct i2c_bit_basher, i2c );
  251. struct bit_basher *basher = &i2cbit->basher;
  252. int rc = 0;
  253. DBGC ( basher, "I2CBIT %p reading from device %x: ",
  254. basher, i2cdev->dev_addr );
  255. for ( ; ; data++, offset++ ) {
  256. /* Select device for writing */
  257. if ( ( rc = i2c_select ( basher, i2cdev, offset,
  258. I2C_WRITE ) ) != 0 )
  259. break;
  260. /* Abort at end of data */
  261. if ( ! ( len-- ) )
  262. break;
  263. /* Select offset */
  264. if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
  265. break;
  266. /* Select device for reading */
  267. if ( ( rc = i2c_select ( basher, i2cdev, offset,
  268. I2C_READ ) ) != 0 )
  269. break;
  270. /* Read byte */
  271. *data = i2c_recv_byte ( basher );
  272. DBGC ( basher, "%02x ", *data );
  273. }
  274. DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
  275. i2c_stop ( basher );
  276. return rc;
  277. }
  278. /**
  279. * Write data to I2C device via bit-bashing interface
  280. *
  281. * @v i2c I2C interface
  282. * @v i2cdev I2C device
  283. * @v offset Starting offset within the device
  284. * @v data Data buffer
  285. * @v len Length of data buffer
  286. * @ret rc Return status code
  287. *
  288. * Note that attempting to write zero bytes of data is a valid way to
  289. * check for I2C device presence.
  290. */
  291. static int i2c_bit_write ( struct i2c_interface *i2c,
  292. struct i2c_device *i2cdev, unsigned int offset,
  293. const uint8_t *data, unsigned int len ) {
  294. struct i2c_bit_basher *i2cbit
  295. = container_of ( i2c, struct i2c_bit_basher, i2c );
  296. struct bit_basher *basher = &i2cbit->basher;
  297. int rc = 0;
  298. DBGC ( basher, "I2CBIT %p writing to device %x: ",
  299. basher, i2cdev->dev_addr );
  300. for ( ; ; data++, offset++ ) {
  301. /* Select device for writing */
  302. if ( ( rc = i2c_select ( basher, i2cdev, offset,
  303. I2C_WRITE ) ) != 0 )
  304. break;
  305. /* Abort at end of data */
  306. if ( ! ( len-- ) )
  307. break;
  308. /* Select offset */
  309. if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
  310. break;
  311. /* Write data to device */
  312. DBGC ( basher, "%02x ", *data );
  313. if ( ( rc = i2c_send_byte ( basher, *data ) ) != 0 )
  314. break;
  315. }
  316. DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
  317. i2c_stop ( basher );
  318. return rc;
  319. }
  320. /**
  321. * Initialise I2C bit-bashing interface
  322. *
  323. * @v i2cbit I2C bit-bashing interface
  324. * @v bash_op Bit-basher operations
  325. */
  326. int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
  327. struct bit_basher_operations *bash_op ) {
  328. struct bit_basher *basher = &i2cbit->basher;
  329. int rc;
  330. /* Initialise data structures */
  331. basher->op = bash_op;
  332. assert ( basher->op->read != NULL );
  333. assert ( basher->op->write != NULL );
  334. i2cbit->i2c.read = i2c_bit_read;
  335. i2cbit->i2c.write = i2c_bit_write;
  336. /* Reset I2C bus */
  337. if ( ( rc = i2c_reset ( basher ) ) != 0 ) {
  338. DBGC ( basher, "I2CBIT %p could not reset I2C bus: %s\n",
  339. basher, strerror ( rc ) );
  340. return rc;
  341. }
  342. return 0;
  343. }