i2c_bitbang.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* $OpenBSD: i2c_bitbang.c,v 1.4 2013/04/20 14:27:09 kettenis Exp $ */
  2. /* $NetBSD: i2c_bitbang.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */
  3. /*
  4. * Copyright (c) 2003 Wasabi Systems, Inc.
  5. * All rights reserved.
  6. *
  7. * Written by Jason R. Thorpe for Wasabi Systems, Inc.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed for the NetBSD Project by
  20. * Wasabi Systems, Inc.
  21. * 4. The name of Wasabi Systems, Inc. may not be used to endorse
  22. * or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  27. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
  29. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Common module for bit-bang'ing an I2C bus.
  39. */
  40. #include <sys/param.h>
  41. #include <dev/i2c/i2cvar.h>
  42. #include <dev/i2c/i2c_bitbang.h>
  43. #define BB_SET(x) ops->ibo_set_bits(v, (x))
  44. #define BB_DIR(x) ops->ibo_set_dir(v, (x))
  45. #define BB_READ ops->ibo_read_bits(v)
  46. #define SDA ops->ibo_bits[I2C_BIT_SDA] /* i2c signal */
  47. #define SCL ops->ibo_bits[I2C_BIT_SCL] /* i2c signal */
  48. #define OUTPUT ops->ibo_bits[I2C_BIT_OUTPUT] /* SDA is output */
  49. #define INPUT ops->ibo_bits[I2C_BIT_INPUT] /* SDA is input */
  50. #define SCL_BAIL_COUNT 1000
  51. int i2c_wait_for_scl(void *, i2c_bitbang_ops_t);
  52. int
  53. i2c_wait_for_scl(void *v, i2c_bitbang_ops_t ops)
  54. {
  55. int bail = 0;
  56. while(((BB_READ & SCL) == 0) && bail < SCL_BAIL_COUNT) {
  57. delay(1);
  58. bail++;
  59. }
  60. if (bail == SCL_BAIL_COUNT) {
  61. i2c_bitbang_send_stop(v, 0, ops);
  62. return (EIO);
  63. }
  64. return (0);
  65. }
  66. /*ARGSUSED*/
  67. int
  68. i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops)
  69. {
  70. BB_DIR(OUTPUT);
  71. BB_SET(SDA | SCL);
  72. delay(5); /* bus free time (4.7 uS) */
  73. BB_SET( SCL);
  74. if (i2c_wait_for_scl(v, ops) != 0)
  75. return (EIO);
  76. delay(4); /* start hold time (4.0 uS) */
  77. BB_SET( 0);
  78. delay(5); /* clock low time (4.7 uS) */
  79. return (0);
  80. }
  81. /*ARGSUSED*/
  82. int
  83. i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops)
  84. {
  85. BB_DIR(OUTPUT);
  86. BB_SET( SCL);
  87. delay(4); /* stop setup time (4.0 uS) */
  88. BB_SET(SDA | SCL);
  89. return (0);
  90. }
  91. int
  92. i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
  93. i2c_bitbang_ops_t ops)
  94. {
  95. int i2caddr;
  96. /* XXX Only support 7-bit addressing for now. */
  97. if ((addr & 0x78) == 0x78)
  98. return (EINVAL);
  99. i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
  100. (void) i2c_bitbang_send_start(v, flags, ops);
  101. return (i2c_bitbang_write_byte(v, i2caddr, flags & ~I2C_F_STOP, ops));
  102. }
  103. int
  104. i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags,
  105. i2c_bitbang_ops_t ops)
  106. {
  107. int i;
  108. uint8_t val = 0;
  109. uint32_t bit;
  110. BB_DIR(INPUT);
  111. BB_SET(SDA );
  112. for (i = 0; i < 8; i++) {
  113. val <<= 1;
  114. BB_SET(SDA | SCL);
  115. if (i2c_wait_for_scl(v, ops) != 0)
  116. return (EIO);
  117. delay(4); /* clock high time (4.0 uS) */
  118. if (BB_READ & SDA)
  119. val |= 1;
  120. BB_SET(SDA );
  121. delay(5); /* clock low time (4.7 uS) */
  122. }
  123. bit = (flags & I2C_F_LAST) ? SDA : 0;
  124. BB_DIR(OUTPUT);
  125. BB_SET(bit );
  126. delay(1); /* data setup time (250 nS) */
  127. BB_SET(bit | SCL);
  128. if (i2c_wait_for_scl(v, ops) != 0)
  129. return (EIO);
  130. delay(4); /* clock high time (4.0 uS) */
  131. BB_SET(bit );
  132. delay(5); /* clock low time (4.7 uS) */
  133. BB_DIR(INPUT);
  134. BB_SET(SDA );
  135. delay(5);
  136. if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST))
  137. (void) i2c_bitbang_send_stop(v, flags, ops);
  138. *valp = val;
  139. return (0);
  140. }
  141. int
  142. i2c_bitbang_write_byte(void *v, uint8_t val, int flags,
  143. i2c_bitbang_ops_t ops)
  144. {
  145. uint32_t bit;
  146. uint8_t mask;
  147. int error;
  148. BB_DIR(OUTPUT);
  149. for (mask = 0x80; mask != 0; mask >>= 1) {
  150. bit = (val & mask) ? SDA : 0;
  151. BB_SET(bit );
  152. delay(1); /* data setup time (250 nS) */
  153. BB_SET(bit | SCL);
  154. if (i2c_wait_for_scl(v, ops) != 0)
  155. return (EIO);
  156. delay(4); /* clock high time (4.0 uS) */
  157. BB_SET(bit );
  158. delay(5); /* clock low time (4.7 uS) */
  159. }
  160. BB_DIR(INPUT);
  161. BB_SET(SDA );
  162. delay(5);
  163. BB_SET(SDA | SCL);
  164. if (i2c_wait_for_scl(v, ops) != 0)
  165. return (EIO);
  166. delay(4);
  167. error = (BB_READ & SDA) ? EIO : 0;
  168. BB_SET(SDA );
  169. delay(5);
  170. if (flags & I2C_F_STOP)
  171. (void) i2c_bitbang_send_stop(v, flags, ops);
  172. return (error);
  173. }