1 /****************************************************************
2 * $ID: i2c.c 24 Oct 2006 12:00:00 +0800 $ *
6 * Maintainer: sonicz <sonic.zhang@analog.com> *
8 * CopyRight (c) 2006 Analog Device *
9 * All rights reserved. *
11 * This file is free software; *
12 * you are free to modify and/or redistribute it *
13 * under the terms of the GNU General Public Licence (GPL).*
15 ****************************************************************/
19 #ifdef CONFIG_HARD_I2C
21 #include <asm/blackfin.h>
24 #include <asm/mach-common/bits/twi.h>
26 DECLARE_GLOBAL_DATA_PTR;
29 #define PRINTD(fmt,args...) do { \
30 if (gd->have_console) \
31 printf(fmt ,##args); \
34 #define PRINTD(fmt,args...)
37 #ifndef CONFIG_TWICLK_KHZ
38 #define CONFIG_TWICLK_KHZ 50
41 /* All transfers are described by this data structure */
43 u16 addr; /* slave address */
45 #define I2C_M_STOP 0x2
47 u16 len; /* msg length */
48 u8 *buf; /* pointer to msg data */
52 * i2c_reset: - reset the host controller
56 static void i2c_reset(void)
59 bfin_write_TWI_CONTROL(0);
62 /* Set TWI internal clock as 10MHz */
63 bfin_write_TWI_CONTROL(((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
65 /* Set Twi interface clock as specified */
66 if (CONFIG_TWICLK_KHZ > 400)
67 bfin_write_TWI_CLKDIV(((5 * 1024 / 400) << 8) | ((5 * 1024 /
70 bfin_write_TWI_CLKDIV(((5 * 1024 /
71 CONFIG_TWICLK_KHZ) << 8) | ((5 * 1024 /
76 bfin_write_TWI_CONTROL(bfin_read_TWI_CONTROL() | TWI_ENA);
80 int wait_for_completion(struct i2c_msg *msg, int timeout_count)
82 unsigned short twi_int_stat;
83 unsigned short mast_stat;
86 for (i = 0; i < timeout_count; i++) {
87 twi_int_stat = bfin_read_TWI_INT_STAT();
88 mast_stat = bfin_read_TWI_MASTER_STAT();
90 if (XMTSERV & twi_int_stat) {
91 /* Transmit next data */
93 bfin_write_TWI_XMT_DATA8(*(msg->buf++));
95 } else if (msg->flags & I2C_M_STOP)
96 bfin_write_TWI_MASTER_CTL
97 (bfin_read_TWI_MASTER_CTL() | STOP);
100 bfin_write_TWI_INT_STAT(XMTSERV);
104 if (RCVSERV & twi_int_stat) {
106 /* Receive next data */
107 *(msg->buf++) = bfin_read_TWI_RCV_DATA8();
109 } else if (msg->flags & I2C_M_STOP) {
110 bfin_write_TWI_MASTER_CTL
111 (bfin_read_TWI_MASTER_CTL() | STOP);
114 /* Clear interrupt source */
115 bfin_write_TWI_INT_STAT(RCVSERV);
119 if (MERR & twi_int_stat) {
120 bfin_write_TWI_INT_STAT(MERR);
121 bfin_write_TWI_INT_MASK(0);
122 bfin_write_TWI_MASTER_STAT(0x3e);
123 bfin_write_TWI_MASTER_CTL(0);
126 * if both err and complete int stats are set,
127 * return proper results.
129 if (MCOMP & twi_int_stat) {
130 bfin_write_TWI_INT_STAT(MCOMP);
131 bfin_write_TWI_INT_MASK(0);
132 bfin_write_TWI_MASTER_CTL(0);
135 * If it is a quick transfer,
136 * only address bug no data, not an err.
138 if (msg->len == 0 && mast_stat & BUFRDERR)
141 * If address not acknowledged return -3,
144 else if (!(mast_stat & ANAK))
151 if (MCOMP & twi_int_stat) {
152 bfin_write_TWI_INT_STAT(MCOMP);
154 bfin_write_TWI_INT_MASK(0);
155 bfin_write_TWI_MASTER_CTL(0);
160 if (msg->flags & I2C_M_RD)
167 * i2c_transfer: - Transfer one byte over the i2c bus
169 * This function can tranfer a byte over the i2c bus in both directions.
170 * It is used by the public API functions.
172 * @return: 0: transfer successful
174 * -2: transmit timeout
176 * -4: receive timeout
177 * -5: controller not ready
179 int i2c_transfer(struct i2c_msg *msg)
182 int timeout_count = 10000;
185 if (!(bfin_read_TWI_CONTROL() & TWI_ENA)) {
190 while (bfin_read_TWI_MASTER_STAT() & BUSBUSY) ;
192 /* Set Transmit device address */
193 bfin_write_TWI_MASTER_ADDR(msg->addr);
197 * Data in FIFO should be discarded before start a new operation.
199 bfin_write_TWI_FIFO_CTL(0x3);
201 bfin_write_TWI_FIFO_CTL(0);
204 if (!(msg->flags & I2C_M_RD)) {
205 /* Transmit first data */
207 PRINTD("1 in i2c_transfer: buf=%d, len=%d\n", *msg->buf,
209 bfin_write_TWI_XMT_DATA8(*(msg->buf++));
216 bfin_write_TWI_INT_STAT(MERR | MCOMP | XMTSERV | RCVSERV);
218 /* Interrupt mask . Enable XMT, RCV interrupt */
219 bfin_write_TWI_INT_MASK(MCOMP | MERR |
220 ((msg->flags & I2C_M_RD) ? RCVSERV : XMTSERV));
223 if (len > 0 && len <= 255)
224 bfin_write_TWI_MASTER_CTL((len << 6));
225 else if (msg->len > 255) {
226 bfin_write_TWI_MASTER_CTL((0xff << 6));
227 msg->flags &= I2C_M_STOP;
229 bfin_write_TWI_MASTER_CTL(0);
232 bfin_write_TWI_MASTER_CTL(bfin_read_TWI_MASTER_CTL() | MEN |
233 ((msg->flags & I2C_M_RD)
234 ? MDIR : 0) | ((CONFIG_TWICLK_KHZ >
238 ret = wait_for_completion(msg, timeout_count);
239 PRINTD("3 in i2c_transfer: ret=%d\n", ret);
244 PRINTD(("i2c_transfer: error: transfer fail\n"));
247 PRINTD(("i2c_transfer: error: transmit timeout\n"));
250 PRINTD(("i2c_transfer: error: ACK missing\n"));
253 PRINTD(("i2c_transfer: error: receive timeout\n"));
256 PRINTD(("i2c_transfer: error: controller not ready\n"));
266 /* ---------------------------------------------------------------------*/
268 /* ---------------------------------------------------------------------*/
270 void i2c_init(int speed, int slaveaddr)
276 * i2c_probe: - Test if a chip answers for a given i2c address
278 * @chip: address of the chip which is searched for
279 * @return: 0 if a chip was found, -1 otherwhise
282 int i2c_probe(uchar chip)
294 if (i2c_transfer(&msg))
298 msg.flags = I2C_M_RD;
301 if (i2c_transfer(&msg))
308 * i2c_read: - Read multiple bytes from an i2c device
310 * chip: I2C chip address, range 0..127
311 * addr: Memory (register) address within the chip
312 * alen: Number of bytes to use for addr (typically 1, 2 for larger
313 * memories, 0 for register type devices with only one
315 * buffer: Where to read/write the data
316 * len: How many bytes to read/write
318 * Returns: 0 on success, not 0 on failure
321 int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
324 u8 addr_bytes[3]; /* lowest...highest byte of data address */
326 PRINTD("i2c_read: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x\n", chip,
330 addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
331 addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
332 addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
336 msg.buf = addr_bytes;
337 if (i2c_transfer(&msg))
341 /* start read sequence */
342 PRINTD(("i2c_read: start read sequence\n"));
344 msg.flags = I2C_M_RD;
347 if (i2c_transfer(&msg))
354 * i2c_write: - Write multiple bytes to an i2c device
356 * chip: I2C chip address, range 0..127
357 * addr: Memory (register) address within the chip
358 * alen: Number of bytes to use for addr (typically 1, 2 for larger
359 * memories, 0 for register type devices with only one
361 * buffer: Where to read/write the data
362 * len: How many bytes to read/write
364 * Returns: 0 on success, not 0 on failure
367 int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
370 u8 addr_bytes[3]; /* lowest...highest byte of data address */
373 ("i2c_write: chip=0x%x, addr=0x%x, alen=0x%x, len=0x%x, buf0=0x%x\n",
374 chip, addr, alen, len, buffer[0]);
376 /* chip address write */
378 addr_bytes[0] = (u8) ((addr >> 0) & 0x000000FF);
379 addr_bytes[1] = (u8) ((addr >> 8) & 0x000000FF);
380 addr_bytes[2] = (u8) ((addr >> 16) & 0x000000FF);
384 msg.buf = addr_bytes;
385 if (i2c_transfer(&msg))
389 /* start read sequence */
390 PRINTD(("i2c_write: start write sequence\n"));
395 if (i2c_transfer(&msg))
402 uchar i2c_reg_read(uchar chip, uchar reg)
406 PRINTD("i2c_reg_read: chip=0x%02x, reg=0x%02x\n", chip, reg);
407 i2c_read(chip, reg, 0, &buf, 1);
411 void i2c_reg_write(uchar chip, uchar reg, uchar val)
413 PRINTD("i2c_reg_write: chip=0x%02x, reg=0x%02x, val=0x%02x\n", chip,
415 i2c_write(chip, reg, 0, &val, 1);
418 #endif /* CONFIG_HARD_I2C */