i2c_bitbang.c revision 1.8 1 /* $NetBSD: i2c_bitbang.c,v 1.8 2007/12/01 06:32:54 kiyohara Exp $ */
2
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 /*
39 * Common module for bit-bang'ing an I2C bus.
40 */
41
42 #include <sys/param.h>
43
44 #include <dev/i2c/i2cvar.h>
45 #include <dev/i2c/i2c_bitbang.h>
46
47 #define SETBITS(x) ops->ibo_set_bits(v, (x))
48 #define DIR(x) ops->ibo_set_dir(v, (x))
49 #define READ ops->ibo_read_bits(v)
50
51 #define SDA ops->ibo_bits[I2C_BIT_SDA] /* i2c signal */
52 #define SCL ops->ibo_bits[I2C_BIT_SCL] /* i2c signal */
53 #define OUTPUT ops->ibo_bits[I2C_BIT_OUTPUT] /* SDA is output */
54 #define INPUT ops->ibo_bits[I2C_BIT_INPUT] /* SDA is input */
55
56 #ifndef SCL_BAIL_COUNT
57 #define SCL_BAIL_COUNT 1000
58 #endif
59
60 static inline int i2c_wait_for_scl(void *, i2c_bitbang_ops_t);
61
62 static inline int
63 i2c_wait_for_scl(void *v, i2c_bitbang_ops_t ops)
64 {
65 int bail = 0;
66
67 DIR(INPUT);
68
69 while (((READ & SCL) == 0) && (bail < SCL_BAIL_COUNT)) {
70
71 delay(1);
72 bail++;
73 }
74 if (bail == SCL_BAIL_COUNT) {
75
76 i2c_bitbang_send_stop(v, 0, ops);
77 return EIO;
78 }
79 return 0;
80 }
81
82 /*ARGSUSED*/
83 int
84 i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops)
85 {
86
87 DIR(OUTPUT);
88 SETBITS(SDA | SCL);
89 delay(5); /* bus free time (4.7 uS) */
90 SETBITS( SCL);
91
92 if (i2c_wait_for_scl(v, ops) != 0)
93 return EIO;
94 delay(4); /* start hold time (4.0 uS) */
95
96 DIR(OUTPUT);
97 SETBITS( 0);
98 delay(5); /* clock low time (4.7 uS) */
99
100 return (0);
101 }
102
103 /*ARGSUSED*/
104 int
105 i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops)
106 {
107
108 DIR(OUTPUT);
109 SETBITS( SCL);
110 delay(4); /* stop setup time (4.0 uS) */
111 SETBITS(SDA | SCL);
112
113 return (0);
114 }
115
116 int
117 i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
118 i2c_bitbang_ops_t ops)
119 {
120
121 if (addr < 0x80) {
122 uint8_t i2caddr;
123
124 /* disallow the 10-bit address prefix */
125 if ((addr & 0x78) == 0x78)
126 return EINVAL;
127 i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
128 (void) i2c_bitbang_send_start(v, flags, ops);
129
130 return (i2c_bitbang_write_byte(v, i2caddr,
131 flags & ~I2C_F_STOP, ops));
132
133 } else if (addr < 0x400) {
134 uint16_t i2caddr;
135 int rv;
136
137 i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0) |
138 0xf000;
139
140 (void) i2c_bitbang_send_start(v, flags, ops);
141 rv = i2c_bitbang_write_byte(v, i2caddr >> 8,
142 flags & ~I2C_F_STOP, ops);
143 /* did a slave ack the 10-bit prefix? */
144 if (rv != 0)
145 return rv;
146
147 /* send the lower 7-bits (+ read/write mode) */
148 return (i2c_bitbang_write_byte(v, i2caddr & 0xff,
149 flags & ~I2C_F_STOP, ops));
150
151 } else
152 return EINVAL;
153 }
154
155 int
156 i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags,
157 i2c_bitbang_ops_t ops)
158 {
159 int i;
160 uint8_t val = 0;
161 uint32_t bit;
162
163 DIR(OUTPUT);
164 SETBITS(SDA );
165
166 for (i = 0; i < 8; i++) {
167 val <<= 1;
168
169 DIR(OUTPUT);
170 SETBITS(SDA | SCL);
171
172 if (i2c_wait_for_scl(v, ops) != 0)
173 return EIO;
174 delay(4); /* clock high time (4.0 uS) */
175
176 DIR(INPUT);
177 if (READ & SDA)
178 val |= 1;
179
180 DIR(OUTPUT);
181 SETBITS(SDA );
182 delay(5); /* clock low time (4.7 uS) */
183 }
184
185 bit = (flags & I2C_F_LAST) ? SDA : 0;
186
187 DIR(OUTPUT);
188 SETBITS(bit );
189 delay(1); /* data setup time (250 nS) */
190 SETBITS(bit | SCL);
191
192 if (i2c_wait_for_scl(v, ops) != 0)
193 return EIO;
194 delay(4); /* clock high time (4.0 uS) */
195
196 DIR(OUTPUT);
197 SETBITS(bit );
198 delay(5); /* clock low time (4.7 uS) */
199
200 DIR(INPUT);
201 SETBITS(SDA );
202 delay(5);
203
204 if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST))
205 (void) i2c_bitbang_send_stop(v, flags, ops);
206
207 *valp = val;
208 return (0);
209 }
210
211 int
212 i2c_bitbang_write_byte(void *v, uint8_t val, int flags,
213 i2c_bitbang_ops_t ops)
214 {
215 uint32_t bit;
216 uint8_t mask;
217 int error;
218
219 for (mask = 0x80; mask != 0; mask >>= 1) {
220 bit = (val & mask) ? SDA : 0;
221
222 DIR(OUTPUT);
223 SETBITS(bit );
224 delay(1); /* data setup time (250 nS) */
225 SETBITS(bit | SCL);
226
227 if (i2c_wait_for_scl(v, ops))
228 return EIO;
229 delay(4); /* clock high time (4.0 uS) */
230
231 DIR(OUTPUT);
232 SETBITS(bit );
233 delay(5); /* clock low time (4.7 uS) */
234 }
235
236 DIR(OUTPUT);
237 SETBITS(SDA );
238 delay(5);
239 SETBITS(SDA | SCL);
240
241 if (i2c_wait_for_scl(v, ops) != 0)
242 return EIO;
243 delay(4);
244
245 DIR(INPUT);
246 error = (READ & SDA) ? EIO : 0;
247
248 DIR(OUTPUT);
249 SETBITS(SDA );
250 delay(5);
251
252 if (flags & I2C_F_STOP)
253 (void) i2c_bitbang_send_stop(v, flags, ops);
254
255 return (error);
256 }
257