You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef _IPXE_I2C_H
  2. #define _IPXE_I2C_H
  3. /** @file
  4. *
  5. * I2C interface
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/bitbash.h>
  11. /** An I2C device
  12. *
  13. * An I2C device represents a specific slave device on an I2C bus. It
  14. * is accessed via an I2C interface.
  15. */
  16. struct i2c_device {
  17. /** Address of this device
  18. *
  19. * The actual address sent on the bus will look like
  20. *
  21. * <start> <device address> <word address overflow> <r/w>
  22. *
  23. * The "word address overflow" is any excess bits from the
  24. * word address, i.e. any portion that does not fit within the
  25. * defined word address length.
  26. */
  27. unsigned int dev_addr;
  28. /** Device address length, in bytes
  29. *
  30. * This is the number of bytes that comprise the device
  31. * address, defined to be the portion that terminates with the
  32. * read/write bit.
  33. */
  34. unsigned int dev_addr_len;
  35. /** Word adddress length, in bytes
  36. *
  37. * This is the number of bytes that comprise the word address,
  38. * defined to be the portion that starts after the read/write
  39. * bit and ends before the first data byte.
  40. *
  41. * For some devices, this length will be zero (i.e. the word
  42. * address is contained entirely within the "word address
  43. * overflow").
  44. */
  45. unsigned int word_addr_len;
  46. };
  47. /** An I2C interface
  48. *
  49. * An I2C interface provides access to an I2C bus, via which I2C
  50. * devices may be reached.
  51. */
  52. struct i2c_interface {
  53. /**
  54. * Read data from I2C device
  55. *
  56. * @v i2c I2C interface
  57. * @v i2cdev I2C device
  58. * @v offset Starting offset within the device
  59. * @v data Data buffer
  60. * @v len Length of data buffer
  61. * @ret rc Return status code
  62. */
  63. int ( * read ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
  64. unsigned int offset, uint8_t *data,
  65. unsigned int len );
  66. /**
  67. * Write data to I2C device
  68. *
  69. * @v i2c I2C interface
  70. * @v i2cdev I2C device
  71. * @v offset Starting offset within the device
  72. * @v data Data buffer
  73. * @v len Length of data buffer
  74. * @ret rc Return status code
  75. */
  76. int ( * write ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
  77. unsigned int offset, const uint8_t *data,
  78. unsigned int len );
  79. };
  80. /** A bit-bashing I2C interface
  81. *
  82. * This provides a standardised way to construct I2C buses via a
  83. * bit-bashing interface.
  84. */
  85. struct i2c_bit_basher {
  86. /** I2C interface */
  87. struct i2c_interface i2c;
  88. /** Bit-bashing interface */
  89. struct bit_basher basher;
  90. };
  91. /** Ten-bit address marker
  92. *
  93. * This value is ORed with the I2C device address to indicate a
  94. * ten-bit address format on the bus.
  95. */
  96. #define I2C_TENBIT_ADDRESS 0x7800
  97. /** An I2C write command */
  98. #define I2C_WRITE 0
  99. /** An I2C read command */
  100. #define I2C_READ 1
  101. /** Bit indices used for I2C bit-bashing interface */
  102. enum {
  103. /** Serial clock */
  104. I2C_BIT_SCL = 0,
  105. /** Serial data */
  106. I2C_BIT_SDA,
  107. };
  108. /** Delay required for bit-bashing operation */
  109. #define I2C_UDELAY 5
  110. /** Maximum number of cycles to use when attempting a bus reset */
  111. #define I2C_RESET_MAX_CYCLES 32
  112. /**
  113. * Check presence of I2C device
  114. *
  115. * @v i2c I2C interface
  116. * @v i2cdev I2C device
  117. * @ret rc Return status code
  118. *
  119. * Checks for the presence of the device on the I2C bus by attempting
  120. * a zero-length write.
  121. */
  122. static inline int i2c_check_presence ( struct i2c_interface *i2c,
  123. struct i2c_device *i2cdev ) {
  124. return i2c->write ( i2c, i2cdev, 0, NULL, 0 );
  125. }
  126. extern int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
  127. struct bit_basher_operations *bash_op );
  128. /**
  129. * Initialise generic I2C EEPROM device
  130. *
  131. * @v i2cdev I2C device
  132. */
  133. static inline __always_inline void
  134. init_i2c_eeprom ( struct i2c_device *i2cdev, unsigned int dev_addr ) {
  135. i2cdev->dev_addr = dev_addr;
  136. i2cdev->dev_addr_len = 1;
  137. i2cdev->word_addr_len = 1;
  138. }
  139. /**
  140. * Initialise Atmel AT24C11
  141. *
  142. * @v i2cdev I2C device
  143. */
  144. static inline __always_inline void
  145. init_at24c11 ( struct i2c_device *i2cdev ) {
  146. /* This chip has no device address; it must be the only chip
  147. * on the bus. The word address is contained entirely within
  148. * the device address field.
  149. */
  150. i2cdev->dev_addr = 0;
  151. i2cdev->dev_addr_len = 1;
  152. i2cdev->word_addr_len = 0;
  153. }
  154. #endif /* _IPXE_I2C_H */