1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <common.h>
19#if defined(CONFIG_AT91FAMILY)
20#include <asm/io.h>
21#include <asm/arch/hardware.h>
22#include <asm/arch/at91_pio.h>
23#ifdef CONFIG_ATMEL_LEGACY
24#include <asm/arch/gpio.h>
25#endif
26#endif
27#include <i2c.h>
28#include <asm/global_data.h>
29#include <linux/delay.h>
30
31#if defined(CONFIG_SOFT_I2C_GPIO_SCL)
32# include <asm/gpio.h>
33
34# ifndef I2C_GPIO_SYNC
35# define I2C_GPIO_SYNC
36# endif
37
38# ifndef I2C_INIT
39# define I2C_INIT \
40 do { \
41 gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
42 gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
43 } while (0)
44# endif
45
46# ifndef I2C_ACTIVE
47# define I2C_ACTIVE do { } while (0)
48# endif
49
50# ifndef I2C_TRISTATE
51# define I2C_TRISTATE do { } while (0)
52# endif
53
54# ifndef I2C_READ
55# define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
56# endif
57
58# ifndef I2C_SDA
59# define I2C_SDA(bit) \
60 do { \
61 if (bit) \
62 gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
63 else \
64 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
65 I2C_GPIO_SYNC; \
66 } while (0)
67# endif
68
69# ifndef I2C_SCL
70# define I2C_SCL(bit) \
71 do { \
72 gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
73 I2C_GPIO_SYNC; \
74 } while (0)
75# endif
76
77# ifndef I2C_DELAY
78# define I2C_DELAY udelay(5)
79# endif
80
81#endif
82
83
84
85DECLARE_GLOBAL_DATA_PTR;
86
87#ifndef I2C_SOFT_DECLARATIONS
88# define I2C_SOFT_DECLARATIONS
89#endif
90
91#if !defined(CONFIG_SYS_I2C_SOFT_SPEED)
92#define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED
93#endif
94#if !defined(CONFIG_SYS_I2C_SOFT_SLAVE)
95#define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE
96#endif
97
98
99
100
101#define RETRIES 0
102
103#define I2C_ACK 0
104#define I2C_NOACK 1
105
106
107#ifdef DEBUG_I2C
108#define PRINTD(fmt,args...) do { \
109 printf (fmt ,##args); \
110 } while (0)
111#else
112#define PRINTD(fmt,args...)
113#endif
114
115
116
117
118#if !defined(CONFIG_SYS_I2C_INIT_BOARD)
119static void send_reset (void);
120#endif
121static void send_start (void);
122static void send_stop (void);
123static void send_ack (int);
124static int write_byte (uchar byte);
125static uchar read_byte (int);
126
127#if !defined(CONFIG_SYS_I2C_INIT_BOARD)
128
129
130
131
132
133static void send_reset(void)
134{
135 I2C_SOFT_DECLARATIONS
136 int j;
137
138 I2C_SCL(1);
139 I2C_SDA(1);
140#ifdef I2C_INIT
141 I2C_INIT;
142#endif
143 I2C_TRISTATE;
144 for(j = 0; j < 9; j++) {
145 I2C_SCL(0);
146 I2C_DELAY;
147 I2C_DELAY;
148 I2C_SCL(1);
149 I2C_DELAY;
150 I2C_DELAY;
151 }
152 send_stop();
153 I2C_TRISTATE;
154}
155#endif
156
157
158
159
160static void send_start(void)
161{
162 I2C_SOFT_DECLARATIONS
163
164 I2C_DELAY;
165 I2C_SDA(1);
166 I2C_ACTIVE;
167 I2C_DELAY;
168 I2C_SCL(1);
169 I2C_DELAY;
170 I2C_SDA(0);
171 I2C_DELAY;
172}
173
174
175
176
177static void send_stop(void)
178{
179 I2C_SOFT_DECLARATIONS
180
181 I2C_SCL(0);
182 I2C_DELAY;
183 I2C_SDA(0);
184 I2C_ACTIVE;
185 I2C_DELAY;
186 I2C_SCL(1);
187 I2C_DELAY;
188 I2C_SDA(1);
189 I2C_DELAY;
190 I2C_TRISTATE;
191}
192
193
194
195
196static void send_ack(int ack)
197{
198 I2C_SOFT_DECLARATIONS
199
200 I2C_SCL(0);
201 I2C_DELAY;
202 I2C_ACTIVE;
203 I2C_SDA(ack);
204 I2C_DELAY;
205 I2C_SCL(1);
206 I2C_DELAY;
207 I2C_DELAY;
208 I2C_SCL(0);
209 I2C_DELAY;
210}
211
212
213
214
215static int write_byte(uchar data)
216{
217 I2C_SOFT_DECLARATIONS
218 int j;
219 int nack;
220
221 I2C_ACTIVE;
222 for(j = 0; j < 8; j++) {
223 I2C_SCL(0);
224 I2C_DELAY;
225 I2C_SDA(data & 0x80);
226 I2C_DELAY;
227 I2C_SCL(1);
228 I2C_DELAY;
229 I2C_DELAY;
230
231 data <<= 1;
232 }
233
234
235
236
237 I2C_SCL(0);
238 I2C_DELAY;
239 I2C_SDA(1);
240 I2C_TRISTATE;
241 I2C_DELAY;
242 I2C_SCL(1);
243 I2C_DELAY;
244 I2C_DELAY;
245 nack = I2C_READ;
246 I2C_SCL(0);
247 I2C_DELAY;
248 I2C_ACTIVE;
249
250 return(nack);
251}
252
253
254
255
256
257static uchar read_byte(int ack)
258{
259 I2C_SOFT_DECLARATIONS
260 int data;
261 int j;
262
263
264
265
266 I2C_TRISTATE;
267 I2C_SDA(1);
268 data = 0;
269 for(j = 0; j < 8; j++) {
270 I2C_SCL(0);
271 I2C_DELAY;
272 I2C_SCL(1);
273 I2C_DELAY;
274 data <<= 1;
275 data |= I2C_READ;
276 I2C_DELAY;
277 }
278 send_ack(ack);
279
280 return(data);
281}
282
283
284
285
286static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
287{
288#if defined(CONFIG_SYS_I2C_INIT_BOARD)
289
290
291
292 i2c_init_board();
293#else
294
295
296
297
298
299
300 send_reset ();
301#endif
302}
303
304
305
306
307
308
309static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr)
310{
311 int rc;
312
313
314
315
316
317 send_start();
318 rc = write_byte ((addr << 1) | 0);
319 send_stop();
320
321 return (rc ? 1 : 0);
322}
323
324
325
326
327static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
328 int alen, uchar *buffer, int len)
329{
330 int shift;
331 PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
332 chip, addr, alen, buffer, len);
333
334#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
335
336
337
338
339
340
341
342
343
344
345
346 chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
347
348 PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
349 chip, addr);
350#endif
351
352
353
354
355
356
357
358 send_start();
359 if(alen > 0) {
360 if(write_byte(chip << 1)) {
361 send_stop();
362 PRINTD("i2c_read, no chip responded %02X\n", chip);
363 return(1);
364 }
365 shift = (alen-1) * 8;
366 while(alen-- > 0) {
367 if(write_byte(addr >> shift)) {
368 PRINTD("i2c_read, address not <ACK>ed\n");
369 return(1);
370 }
371 shift -= 8;
372 }
373
374
375
376
377
378
379#ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
380 send_start();
381#else
382 send_stop();
383 send_start();
384#endif
385 }
386
387
388
389
390
391 write_byte((chip << 1) | 1);
392 while(len-- > 0) {
393 *buffer++ = read_byte(len == 0);
394 }
395 send_stop();
396 return(0);
397}
398
399
400
401
402static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
403 int alen, uchar *buffer, int len)
404{
405 int shift, failures = 0;
406
407 PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
408 chip, addr, alen, buffer, len);
409
410 send_start();
411 if(write_byte(chip << 1)) {
412 send_stop();
413 PRINTD("i2c_write, no chip responded %02X\n", chip);
414 return(1);
415 }
416 shift = (alen-1) * 8;
417 while(alen-- > 0) {
418 if(write_byte(addr >> shift)) {
419 PRINTD("i2c_write, address not <ACK>ed\n");
420 return(1);
421 }
422 shift -= 8;
423 }
424
425 while(len-- > 0) {
426 if(write_byte(*buffer++)) {
427 failures++;
428 }
429 }
430 send_stop();
431 return(failures);
432}
433
434
435
436
437U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe,
438 soft_i2c_read, soft_i2c_write, NULL,
439 CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE,
440 0)
441