1
2
3
4
5
6
7
8
9#include <common.h>
10#include <i2c.h>
11
12#include <asm/blackfin.h>
13#include <asm/mach-common/bits/twi.h>
14
15
16#define ureg(name) u16 name; u16 __pad_##name;
17struct twi_regs {
18 ureg(clkdiv);
19 ureg(control);
20 ureg(slave_ctl);
21 ureg(slave_stat);
22 ureg(slave_addr);
23 ureg(master_ctl);
24 ureg(master_stat);
25 ureg(master_addr);
26 ureg(int_stat);
27 ureg(int_mask);
28 ureg(fifo_ctl);
29 ureg(fifo_stat);
30 char __pad[0x50];
31 ureg(xmt_data8);
32 ureg(xmt_data16);
33 ureg(rcv_data8);
34 ureg(rcv_data16);
35};
36#undef ureg
37
38
39#ifdef TWI_CLKDIV
40#define TWI0_CLKDIV TWI_CLKDIV
41#endif
42static volatile struct twi_regs *twi = (void *)TWI0_CLKDIV;
43
44#ifdef DEBUG
45# define dmemset(s, c, n) memset(s, c, n)
46#else
47# define dmemset(s, c, n)
48#endif
49#define debugi(fmt, args...) \
50 debug( \
51 "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t%-20s:%-3i: " fmt "\n", \
52 twi->master_stat, twi->fifo_stat, twi->int_stat, \
53 __func__, __LINE__, ## args)
54
55#ifdef CONFIG_TWICLK_KHZ
56# error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
57#endif
58
59
60
61
62
63
64
65#define I2C_SPEED_MAX 400000
66#define I2C_SPEED_TO_DUTY(speed) (5000000 / (speed))
67#define I2C_DUTY_MAX (I2C_SPEED_TO_DUTY(I2C_SPEED_MAX) + 1)
68#define I2C_DUTY_MIN 0xff
69#define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
70
71#if SYS_I2C_DUTY < I2C_DUTY_MAX || SYS_I2C_DUTY > I2C_DUTY_MIN
72# error "The Blackfin I2C hardware can only operate 20KHz - 400KHz"
73#endif
74
75
76struct i2c_msg {
77 u8 flags;
78#define I2C_M_COMBO 0x4
79#define I2C_M_STOP 0x2
80#define I2C_M_READ 0x1
81 int len;
82 u8 *buf;
83 int alen;
84 u8 *abuf;
85};
86
87
88#define I2C_TIMEOUT 10
89
90
91
92
93
94static int wait_for_completion(struct i2c_msg *msg)
95{
96 uint16_t int_stat;
97 ulong timebase = get_timer(0);
98
99 do {
100 int_stat = twi->int_stat;
101
102 if (int_stat & XMTSERV) {
103 debugi("processing XMTSERV");
104 twi->int_stat = XMTSERV;
105 SSYNC();
106 if (msg->alen) {
107 twi->xmt_data8 = *(msg->abuf++);
108 --msg->alen;
109 } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
110 twi->xmt_data8 = *(msg->buf++);
111 --msg->len;
112 } else {
113 twi->master_ctl |= (msg->flags & I2C_M_COMBO) ? RSTART | MDIR : STOP;
114 SSYNC();
115 }
116 }
117 if (int_stat & RCVSERV) {
118 debugi("processing RCVSERV");
119 twi->int_stat = RCVSERV;
120 SSYNC();
121 if (msg->len) {
122 *(msg->buf++) = twi->rcv_data8;
123 --msg->len;
124 } else if (msg->flags & I2C_M_STOP) {
125 twi->master_ctl |= STOP;
126 SSYNC();
127 }
128 }
129 if (int_stat & MERR) {
130 debugi("processing MERR");
131 twi->int_stat = MERR;
132 SSYNC();
133 return msg->len;
134 }
135 if (int_stat & MCOMP) {
136 debugi("processing MCOMP");
137 twi->int_stat = MCOMP;
138 SSYNC();
139 if (msg->flags & I2C_M_COMBO && msg->len) {
140 twi->master_ctl = (twi->master_ctl & ~RSTART) |
141 (min(msg->len, 0xff) << 6) | MEN | MDIR;
142 SSYNC();
143 } else
144 break;
145 }
146
147
148 if (int_stat)
149 timebase = get_timer(0);
150
151 } while (get_timer(timebase) < I2C_TIMEOUT);
152
153 return msg->len;
154}
155
156
157
158
159
160
161
162
163static int i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer, int len, u8 flags)
164{
165 uchar addr_buffer[] = {
166 (addr >> 0),
167 (addr >> 8),
168 (addr >> 16),
169 };
170 struct i2c_msg msg = {
171 .flags = flags | (len >= 0xff ? I2C_M_STOP : 0),
172 .buf = buffer,
173 .len = len,
174 .abuf = addr_buffer,
175 .alen = alen,
176 };
177 int ret;
178
179 dmemset(buffer, 0xff, len);
180 debugi("chip=0x%x addr=0x%02x alen=%i buf[0]=0x%02x len=%i flags=0x%02x[%s] ",
181 chip, addr, alen, buffer[0], len, flags, (flags & I2C_M_READ ? "rd" : "wr"));
182
183
184 while (twi->master_stat & BUSBUSY)
185 if (ctrlc())
186 return 1;
187
188
189 twi->master_addr = chip;
190
191
192 twi->fifo_ctl = XMTFLUSH | RCVFLUSH;
193 SSYNC();
194 twi->fifo_ctl = 0;
195 SSYNC();
196
197
198 if (msg.alen) {
199 len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
200 debugi("first byte=0x%02x", *msg.abuf);
201 twi->xmt_data8 = *(msg.abuf++);
202 --msg.alen;
203 } else if (!(msg.flags & I2C_M_READ) && msg.len) {
204 debugi("first byte=0x%02x", *msg.buf);
205 twi->xmt_data8 = *(msg.buf++);
206 --msg.len;
207 }
208
209
210 twi->master_stat = -1;
211 twi->int_stat = -1;
212 twi->int_mask = 0;
213 SSYNC();
214
215
216 twi->master_ctl =
217 (twi->master_ctl & FAST) |
218 (min(len, 0xff) << 6) | MEN |
219 ((msg.flags & I2C_M_READ) ? MDIR : 0);
220 SSYNC();
221 debugi("CTL=0x%04x", twi->master_ctl);
222
223
224 ret = wait_for_completion(&msg);
225 debugi("ret=%d", ret);
226
227 if (ret) {
228 twi->master_ctl &= ~MEN;
229 twi->control &= ~TWI_ENA;
230 SSYNC();
231 twi->control |= TWI_ENA;
232 SSYNC();
233 }
234
235 return ret;
236}
237
238
239
240
241
242int i2c_set_bus_speed(unsigned int speed)
243{
244 u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
245
246
247 if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
248 return -1;
249 twi->clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
250
251
252 twi->master_ctl = (speed > 100000 ? FAST : 0);
253
254 return 0;
255}
256
257
258
259
260
261unsigned int i2c_get_bus_speed(void)
262{
263
264 return 5000000 / (twi->clkdiv & 0xff);
265}
266
267
268
269
270
271
272
273
274
275void i2c_init(int speed, int slaveaddr)
276{
277 uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
278
279
280 twi->control = prescale;
281
282
283 i2c_set_bus_speed(speed);
284
285
286 twi->control = TWI_ENA | prescale;
287 SSYNC();
288
289 debugi("CONTROL:0x%04x CLKDIV:0x%04x", twi->control, twi->clkdiv);
290
291#if CONFIG_SYS_I2C_SLAVE
292# error I2C slave support not tested/supported
293
294 if (slaveaddr) {
295 twi->slave_addr = slaveaddr;
296 twi->slave_ctl = SEN;
297 }
298#endif
299}
300
301
302
303
304
305
306int i2c_probe(uchar chip)
307{
308 u8 byte;
309 return i2c_read(chip, 0, 0, &byte, 1);
310}
311
312
313
314
315
316
317
318
319
320
321int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
322{
323 return i2c_transfer(chip, addr, alen, buffer, len, (alen ? I2C_M_COMBO : I2C_M_READ));
324}
325
326
327
328
329
330
331
332
333
334
335int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
336{
337 return i2c_transfer(chip, addr, alen, buffer, len, 0);
338}
339
340
341
342
343
344
345int i2c_set_bus_num(unsigned int bus)
346{
347 switch (bus) {
348#if CONFIG_SYS_MAX_I2C_BUS > 0
349 case 0: twi = (void *)TWI0_CLKDIV; return 0;
350#endif
351#if CONFIG_SYS_MAX_I2C_BUS > 1
352 case 1: twi = (void *)TWI1_CLKDIV; return 0;
353#endif
354#if CONFIG_SYS_MAX_I2C_BUS > 2
355 case 2: twi = (void *)TWI2_CLKDIV; return 0;
356#endif
357 default: return -1;
358 }
359}
360
361
362
363
364unsigned int i2c_get_bus_num(void)
365{
366 switch ((unsigned long)twi) {
367#if CONFIG_SYS_MAX_I2C_BUS > 0
368 case TWI0_CLKDIV: return 0;
369#endif
370#if CONFIG_SYS_MAX_I2C_BUS > 1
371 case TWI1_CLKDIV: return 1;
372#endif
373#if CONFIG_SYS_MAX_I2C_BUS > 2
374 case TWI2_CLKDIV: return 2;
375#endif
376 default: return -1;
377 }
378}
379