blob: b3a04d32074a2242d5f647e592083f47b09d865a [file] [log] [blame]
Mike Frysingerbe853bf2008-10-06 04:16:47 -04001/*
2 * i2c.c - driver for Blackfin on-chip TWI/I2C
3 *
Mike Frysingerb5cebb42010-05-05 03:20:30 -04004 * Copyright (c) 2006-2010 Analog Devices Inc.
Mike Frysingerbe853bf2008-10-06 04:16:47 -04005 *
6 * Licensed under the GPL-2 or later.
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
Mike Frysingerb5cebb42010-05-05 03:20:30 -040015/* Every register is 32bit aligned, but only 16bits in size */
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/* U-Boot I2C framework allows only one active device at a time. */
39#ifdef TWI_CLKDIV
40#define TWI0_CLKDIV TWI_CLKDIV
41#endif
42static volatile struct twi_regs *twi = (void *)TWI0_CLKDIV;
43
Mike Frysingerbe853bf2008-10-06 04:16:47 -040044#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( \
Mike Frysingerb5cebb42010-05-05 03:20:30 -040051 "MSTAT:0x%03x FSTAT:0x%x ISTAT:0x%02x\t%-20s:%-3i: " fmt "\n", \
52 twi->master_stat, twi->fifo_stat, twi->int_stat, \
Mike Frysingerbe853bf2008-10-06 04:16:47 -040053 __func__, __LINE__, ## args)
54
Mike Frysingerbe853bf2008-10-06 04:16:47 -040055#ifdef CONFIG_TWICLK_KHZ
56# error do not define CONFIG_TWICLK_KHZ ... use CONFIG_SYS_I2C_SPEED
57#endif
Mike Frysinger08a1c622009-10-14 19:27:27 -040058
59/*
60 * The way speed is changed into duty often results in integer truncation
61 * with 50% duty, so we'll force rounding up to the next duty by adding 1
62 * to the max. In practice this will get us a speed of something like
63 * 385 KHz. The other limit is easy to handle as it is only 8 bits.
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 /* 8 bit limited */
69#define SYS_I2C_DUTY I2C_SPEED_TO_DUTY(CONFIG_SYS_I2C_SPEED)
70/* Note: duty is inverse of speed, so the comparisons below are correct */
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"
Mike Frysingerbe853bf2008-10-06 04:16:47 -040073#endif
74
75/* All transfers are described by this data structure */
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; /* msg length */
82 u8 *buf; /* pointer to msg data */
83 int alen; /* addr length */
84 u8 *abuf; /* addr buffer */
85};
86
Mike Frysinger3814ea42009-10-14 19:27:26 -040087/* Allow msec timeout per ~byte transfer */
88#define I2C_TIMEOUT 10
89
Mike Frysingerbe853bf2008-10-06 04:16:47 -040090/**
91 * wait_for_completion - manage the actual i2c transfer
92 * @msg: the i2c msg
93 */
94static int wait_for_completion(struct i2c_msg *msg)
95{
96 uint16_t int_stat;
Mike Frysinger3814ea42009-10-14 19:27:26 -040097 ulong timebase = get_timer(0);
Mike Frysingerbe853bf2008-10-06 04:16:47 -040098
Mike Frysinger3814ea42009-10-14 19:27:26 -040099 do {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400100 int_stat = twi->int_stat;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400101
102 if (int_stat & XMTSERV) {
103 debugi("processing XMTSERV");
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400104 twi->int_stat = XMTSERV;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400105 SSYNC();
106 if (msg->alen) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400107 twi->xmt_data8 = *(msg->abuf++);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400108 --msg->alen;
109 } else if (!(msg->flags & I2C_M_COMBO) && msg->len) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400110 twi->xmt_data8 = *(msg->buf++);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400111 --msg->len;
112 } else {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400113 twi->master_ctl |= (msg->flags & I2C_M_COMBO) ? RSTART | MDIR : STOP;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400114 SSYNC();
115 }
116 }
117 if (int_stat & RCVSERV) {
118 debugi("processing RCVSERV");
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400119 twi->int_stat = RCVSERV;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400120 SSYNC();
121 if (msg->len) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400122 *(msg->buf++) = twi->rcv_data8;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400123 --msg->len;
124 } else if (msg->flags & I2C_M_STOP) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400125 twi->master_ctl |= STOP;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400126 SSYNC();
127 }
128 }
129 if (int_stat & MERR) {
130 debugi("processing MERR");
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400131 twi->int_stat = MERR;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400132 SSYNC();
Mike Frysinger3814ea42009-10-14 19:27:26 -0400133 return msg->len;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400134 }
135 if (int_stat & MCOMP) {
136 debugi("processing MCOMP");
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400137 twi->int_stat = MCOMP;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400138 SSYNC();
139 if (msg->flags & I2C_M_COMBO && msg->len) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400140 twi->master_ctl = (twi->master_ctl & ~RSTART) |
141 (min(msg->len, 0xff) << 6) | MEN | MDIR;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400142 SSYNC();
143 } else
144 break;
145 }
Mike Frysinger3814ea42009-10-14 19:27:26 -0400146
147 /* If we were able to do something, reset timeout */
148 if (int_stat)
149 timebase = get_timer(0);
150
151 } while (get_timer(timebase) < I2C_TIMEOUT);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400152
153 return msg->len;
154}
155
156/**
157 * i2c_transfer - setup an i2c transfer
158 * @return: 0 if things worked, non-0 if things failed
159 *
160 * Here we just get the i2c stuff all prepped and ready, and then tail off
161 * into wait_for_completion() for all the bits to go.
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 /* wait for things to settle */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400184 while (twi->master_stat & BUSBUSY)
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400185 if (ctrlc())
186 return 1;
187
188 /* Set Transmit device address */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400189 twi->master_addr = chip;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400190
191 /* Clear the FIFO before starting things */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400192 twi->fifo_ctl = XMTFLUSH | RCVFLUSH;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400193 SSYNC();
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400194 twi->fifo_ctl = 0;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400195 SSYNC();
196
197 /* prime the pump */
198 if (msg.alen) {
Peter Meerwald98ab14e2009-06-29 15:48:33 -0400199 len = (msg.flags & I2C_M_COMBO) ? msg.alen : msg.alen + len;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400200 debugi("first byte=0x%02x", *msg.abuf);
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400201 twi->xmt_data8 = *(msg.abuf++);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400202 --msg.alen;
203 } else if (!(msg.flags & I2C_M_READ) && msg.len) {
204 debugi("first byte=0x%02x", *msg.buf);
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400205 twi->xmt_data8 = *(msg.buf++);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400206 --msg.len;
207 }
208
209 /* clear int stat */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400210 twi->master_stat = -1;
211 twi->int_stat = -1;
212 twi->int_mask = 0;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400213 SSYNC();
214
215 /* Master enable */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400216 twi->master_ctl =
217 (twi->master_ctl & FAST) |
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400218 (min(len, 0xff) << 6) | MEN |
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400219 ((msg.flags & I2C_M_READ) ? MDIR : 0);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400220 SSYNC();
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400221 debugi("CTL=0x%04x", twi->master_ctl);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400222
223 /* process the rest */
224 ret = wait_for_completion(&msg);
225 debugi("ret=%d", ret);
226
227 if (ret) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400228 twi->master_ctl &= ~MEN;
229 twi->control &= ~TWI_ENA;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400230 SSYNC();
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400231 twi->control |= TWI_ENA;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400232 SSYNC();
233 }
234
235 return ret;
236}
237
Mike Frysinger08a1c622009-10-14 19:27:27 -0400238/**
239 * i2c_set_bus_speed - set i2c bus speed
240 * @speed: bus speed (in HZ)
241 */
242int i2c_set_bus_speed(unsigned int speed)
243{
244 u16 clkdiv = I2C_SPEED_TO_DUTY(speed);
245
246 /* Set TWI interface clock */
247 if (clkdiv < I2C_DUTY_MAX || clkdiv > I2C_DUTY_MIN)
248 return -1;
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400249 twi->clkdiv = (clkdiv << 8) | (clkdiv & 0xff);
Mike Frysinger08a1c622009-10-14 19:27:27 -0400250
251 /* Don't turn it on */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400252 twi->master_ctl = (speed > 100000 ? FAST : 0);
Mike Frysinger08a1c622009-10-14 19:27:27 -0400253
254 return 0;
255}
256
257/**
258 * i2c_get_bus_speed - get i2c bus speed
259 * @speed: bus speed (in HZ)
260 */
261unsigned int i2c_get_bus_speed(void)
262{
263 /* 10 MHz / (2 * CLKDIV) -> 5 MHz / CLKDIV */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400264 return 5000000 / (twi->clkdiv & 0xff);
Mike Frysinger08a1c622009-10-14 19:27:27 -0400265}
266
267/**
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400268 * i2c_init - initialize the i2c bus
269 * @speed: bus speed (in HZ)
270 * @slaveaddr: address of device in slave mode (0 - not slave)
271 *
272 * Slave mode isn't actually implemented. It'll stay that way until
273 * we get a real request for it.
274 */
275void i2c_init(int speed, int slaveaddr)
276{
277 uint8_t prescale = ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F;
278
279 /* Set TWI internal clock as 10MHz */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400280 twi->control = prescale;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400281
282 /* Set TWI interface clock as specified */
Mike Frysinger08a1c622009-10-14 19:27:27 -0400283 i2c_set_bus_speed(speed);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400284
Mike Frysinger08a1c622009-10-14 19:27:27 -0400285 /* Enable it */
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400286 twi->control = TWI_ENA | prescale;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400287 SSYNC();
288
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400289 debugi("CONTROL:0x%04x CLKDIV:0x%04x", twi->control, twi->clkdiv);
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400290
291#if CONFIG_SYS_I2C_SLAVE
292# error I2C slave support not tested/supported
293 /* If they want us as a slave, do it */
294 if (slaveaddr) {
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400295 twi->slave_addr = slaveaddr;
296 twi->slave_ctl = SEN;
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400297 }
298#endif
299}
300
301/**
302 * i2c_probe - test if a chip exists at a given i2c address
303 * @chip: i2c chip addr to search for
304 * @return: 0 if found, non-0 if not found
305 */
306int i2c_probe(uchar chip)
307{
308 u8 byte;
309 return i2c_read(chip, 0, 0, &byte, 1);
310}
311
312/**
313 * i2c_read - read data from an i2c device
314 * @chip: i2c chip addr
315 * @addr: memory (register) address in the chip
316 * @alen: byte size of address
317 * @buffer: buffer to store data read from chip
318 * @len: how many bytes to read
319 * @return: 0 on success, non-0 on failure
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 * i2c_write - write data to an i2c device
328 * @chip: i2c chip addr
329 * @addr: memory (register) address in the chip
330 * @alen: byte size of address
Peter Meerwald98ab14e2009-06-29 15:48:33 -0400331 * @buffer: buffer holding data to write to chip
Mike Frysingerbe853bf2008-10-06 04:16:47 -0400332 * @len: how many bytes to write
333 * @return: 0 on success, non-0 on failure
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}
Mike Frysingerb5cebb42010-05-05 03:20:30 -0400339
340/**
341 * i2c_set_bus_num - change active I2C bus
342 * @bus: bus index, zero based
343 * @returns: 0 on success, non-0 on failure
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 * i2c_get_bus_num - returns index of active I2C bus
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}