3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * Hacked for the DB64360 board by Ingo.Assmus@keymile.com
24 * extra improvments by Brain Waite
30 #include "../include/mv_gen_reg.h"
31 #include "../include/core.h"
33 #define MAX_I2C_RETRYS 10
34 #define I2C_DELAY 1000 /* Should be at least the # of MHz of Tclk */
44 /* Assuming that there is only one master on the bus (us) */
46 void i2c_init (int speed, int slaveaddr)
48 unsigned int n, m, freq, margin, power;
49 unsigned int actualN = 0, actualM = 0;
50 unsigned int control, status;
51 unsigned int minMargin = 0xffffffff;
52 unsigned int tclk = CONFIG_SYS_TCLK;
53 unsigned int i2cFreq = speed; /* 100000 max. Fast mode not supported */
55 DP (puts ("i2c_init\n"));
57 for (n = 0; n < 8; n++) {
58 for (m = 0; m < 16; m++) {
59 power = 2 << n; /* power = 2^(n+1) */
60 freq = tclk / (10 * (m + 1) * power);
62 margin = i2cFreq - freq;
64 margin = freq - i2cFreq;
65 if (margin < minMargin) {
73 DP (puts ("setup i2c bus\n"));
77 GT_REG_WRITE (I2C_SOFT_RESET, 0);
79 DP (puts ("udelay...\n"));
83 DP (puts ("set baudrate\n"));
85 GT_REG_WRITE (I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
86 GT_REG_WRITE (I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
88 udelay (I2C_DELAY * 10);
90 DP (puts ("read control, baudrate\n"));
92 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
93 GT_REG_READ (I2C_CONTROL, &control);
96 static uchar i2c_start (void)
97 { /* DB64360 checked -> ok */
98 unsigned int control, status;
101 DP (puts ("i2c_start\n"));
103 /* Set the start bit */
105 /* gtI2cGenerateStartBit() */
107 GT_REG_READ (I2C_CONTROL, &control);
108 control |= (0x1 << 5); /* generate the I2C_START_BIT */
109 GT_REG_WRITE (I2C_CONTROL, control);
111 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
114 while ((status & 0xff) != 0x08) {
117 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
120 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
127 static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
129 unsigned int status, data, bits = 7;
132 DP (puts ("i2c_select_device\n"));
134 /* Output slave address */
140 data = (dev_addr << 1);
141 /* set the read bit */
143 GT_REG_WRITE (I2C_DATA, data);
144 /* assert the address */
145 RESET_REG_BITS (I2C_CONTROL, BIT3);
149 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
151 while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
154 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
157 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
162 printf ("10 bit I2C addressing not yet implemented\n");
169 static uchar i2c_get_data (uchar * return_data, int len)
172 unsigned int data, status = 0;
175 DP (puts ("i2c_get_data\n"));
179 /* Get and return the data */
181 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
183 udelay (I2C_DELAY * 5);
185 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
187 while ((status & 0xff) != 0x50) {
190 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
193 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
196 GT_REG_READ (I2C_DATA, &data);
198 *return_data = (uchar) data;
201 RESET_REG_BITS (I2C_CONTROL, BIT2 | BIT3);
202 while ((status & 0xff) != 0x58) {
205 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
208 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
211 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /* stop */
216 static uchar i2c_write_data (unsigned int *data, int len)
221 unsigned int *temp_ptr = data;
223 DP (puts ("i2c_write_data\n"));
226 temp = (unsigned int) (*temp_ptr);
227 GT_REG_WRITE (I2C_DATA, temp);
228 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
232 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
234 while ((status & 0xff) != 0x28) {
237 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
240 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
246 /* 11-14-2002 Paul Marchese */
247 /* Can't have the write issuing a stop command */
248 /* it's wrong to have a stop bit in read stream or write stream */
249 /* since we don't know if it's really the end of the command */
250 /* or whether we have just send the device address + offset */
251 /* we will push issuing the stop command off to the original */
252 /* calling function */
253 /* set the interrupt bit in the control register */
254 GT_REG_WRITE (I2C_CONTROL, (0x1 << 3));
255 udelay (I2C_DELAY * 10);
259 /* 11-14-2002 Paul Marchese */
260 /* created this function to get the i2c_write() */
261 /* function working properly. */
262 /* function to write bytes out on the i2c bus */
263 /* this is identical to the function i2c_write_data() */
264 /* except that it requires a buffer that is an */
265 /* unsigned character array. You can't use */
266 /* i2c_write_data() to send an array of unsigned characters */
267 /* since the byte of interest ends up on the wrong end of the bus */
268 /* aah, the joys of big endian versus little endian! */
270 /* returns 0 = success */
271 /* anything other than zero is failure */
272 static uchar i2c_write_byte (unsigned char *data, int len)
277 unsigned char *temp_ptr = data;
279 DP (puts ("i2c_write_byte\n"));
282 /* Set and assert the data */
284 GT_REG_WRITE (I2C_DATA, temp);
285 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
289 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
291 while ((status & 0xff) != 0x28) {
294 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
297 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
303 /* Can't have the write issuing a stop command */
304 /* it's wrong to have a stop bit in read stream or write stream */
305 /* since we don't know if it's really the end of the command */
306 /* or whether we have just send the device address + offset */
307 /* we will push issuing the stop command off to the original */
308 /* calling function */
309 /* GT_REG_WRITE(I2C_CONTROL, (0x1 << 3) | (0x1 << 4));
310 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); */
311 /* set the interrupt bit in the control register */
312 GT_REG_WRITE (I2C_CONTROL, (0x1 << 3));
313 udelay (I2C_DELAY * 10);
319 i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit,
323 unsigned int table[2];
325 /* initialize the table of address offset bytes */
326 /* utilized for 2 byte address offsets */
327 /* NOTE: the order is high byte first! */
328 table[1] = offset & 0xff; /* low byte */
329 table[0] = offset / 0x100; /* high byte */
331 DP (puts ("i2c_set_dev_offset\n"));
333 status = i2c_select_device (dev_addr, 0, ten_bit);
336 printf ("Failed to select device setting offset: 0x%02x\n",
341 /* check the address offset length */
343 /* no address offset */
345 else if (alen == 1) {
346 /* 1 byte address offset */
347 status = i2c_write_data (&offset, 1);
350 printf ("Failed to write data: 0x%02x\n", status);
354 } else if (alen == 2) {
355 /* 2 bytes address offset */
356 status = i2c_write_data (table, 2);
359 printf ("Failed to write data: 0x%02x\n", status);
364 /* address offset unknown or not supported */
365 printf ("Address length offset %d is not supported\n", alen);
368 return 0; /* sucessful completion */
372 i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data,
376 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
378 DP (puts ("i2c_read\n"));
380 /* set the i2c frequency */
381 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
383 status = i2c_start ();
387 printf ("Transaction start failed: 0x%02x\n", status);
392 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
395 printf ("Failed to set slave address & offset: 0x%02x\n",
401 /* set the i2c frequency again */
402 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
404 status = i2c_start ();
407 printf ("Transaction restart failed: 0x%02x\n", status);
412 status = i2c_select_device (dev_addr, 1, 0); /* send the slave address */
415 printf ("Address not acknowledged: 0x%02x\n", status);
420 status = i2c_get_data (data, len);
423 printf ("Data not received: 0x%02x\n", status);
431 /* 11-14-2002 Paul Marchese */
432 /* Function to set the I2C stop bit */
435 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4));
438 /* 11-14-2002 Paul Marchese */
439 /* I2C write function */
440 /* dev_addr = device address */
441 /* offset = address offset */
442 /* alen = length in bytes of the address offset */
443 /* data = pointer to buffer to read data into */
444 /* len = # of bytes to read */
446 /* returns 0 = succesful */
447 /* anything but zero is failure */
449 i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
453 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
455 DP (puts ("i2c_write\n"));
457 /* set the i2c frequency */
458 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
460 status = i2c_start (); /* send a start bit */
464 printf ("Transaction start failed: 0x%02x\n", status);
469 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
472 printf ("Failed to set slave address & offset: 0x%02x\n",
479 status = i2c_write_byte (data, len); /* write the data */
482 printf ("Data not written: 0x%02x\n", status);
486 /* issue a stop bit */
491 /* 11-14-2002 Paul Marchese */
492 /* function to determine if an I2C device is present */
493 /* chip = device address of chip to check for */
495 /* returns 0 = sucessful, the device exists */
496 /* anything other than zero is failure, no device */
497 int i2c_probe (uchar chip)
500 /* We are just looking for an <ACK> back. */
501 /* To see if the device/chip is there */
504 unsigned int i2c_status;
507 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
509 DP (puts ("i2c_probe\n"));
511 /* set the i2c frequency */
512 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
514 status = i2c_start (); /* send a start bit */
518 printf ("Transaction start failed: 0x%02x\n", status);
523 status = i2c_set_dev_offset (chip, 0, 0, 0); /* send the slave address + no offset */
526 printf ("Failed to set slave address: 0x%02x\n", status);
531 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &i2c_status);
532 printf ("address %#x returned %#x\n", chip, i2c_status);
534 /* issue a stop bit */
536 return 0; /* successful completion */