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,
24 * extra improvments by Brain Waite
29 #include "../include/mv_gen_reg.h"
30 #include "../include/core.h"
32 #define MAX_I2C_RETRYS 10
33 #define I2C_DELAY 1000 /* Should be at least the # of MHz of Tclk */
43 /* Assuming that there is only one master on the bus (us) */
45 static void i2c_init (int speed, int slaveaddr)
47 unsigned int n, m, freq, margin, power;
48 unsigned int actualN = 0, actualM = 0;
49 unsigned int control, status;
50 unsigned int minMargin = 0xffffffff;
51 unsigned int tclk = CFG_TCLK;
52 unsigned int i2cFreq = speed; /* 100000 max. Fast mode not supported */
54 DP (puts ("i2c_init\n"));
56 for (n = 0; n < 8; n++) {
57 for (m = 0; m < 16; m++) {
58 power = 2 << n; /* power = 2^(n+1) */
59 freq = tclk / (10 * (m + 1) * power);
61 margin = i2cFreq - freq;
63 margin = freq - i2cFreq;
64 if (margin < minMargin) {
72 DP (puts ("setup i2c bus\n"));
76 GT_REG_WRITE (I2C_SOFT_RESET, 0);
78 DP (puts ("udelay...\n"));
82 DP (puts ("set baudrate\n"));
84 GT_REG_WRITE (I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
85 GT_REG_WRITE (I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
87 udelay (I2C_DELAY * 10);
89 DP (puts ("read control, baudrate\n"));
91 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
92 GT_REG_READ (I2C_CONTROL, &control);
95 static uchar i2c_start (void)
96 { /* DB64360 checked -> ok */
97 unsigned int control, status;
100 DP (puts ("i2c_start\n"));
102 /* Set the start bit */
104 /* gtI2cGenerateStartBit() */
106 GT_REG_READ (I2C_CONTROL, &control);
107 control |= (0x1 << 5); /* generate the I2C_START_BIT */
108 GT_REG_WRITE (I2C_CONTROL, control);
110 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
113 while ((status & 0xff) != 0x08) {
116 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
119 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
126 static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
128 unsigned int status, data, bits = 7;
131 DP (puts ("i2c_select_device\n"));
133 /* Output slave address */
139 data = (dev_addr << 1);
140 /* set the read bit */
142 GT_REG_WRITE (I2C_DATA, data);
143 /* assert the address */
144 RESET_REG_BITS (I2C_CONTROL, BIT3);
148 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
150 while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
153 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
156 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
161 printf ("10 bit I2C addressing not yet implemented\n");
168 static uchar i2c_get_data (uchar * return_data, int len)
171 unsigned int data, status;
174 DP (puts ("i2c_get_data\n"));
178 /* Get and return the data */
180 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
182 udelay (I2C_DELAY * 5);
184 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
186 while ((status & 0xff) != 0x50) {
189 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
192 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
195 GT_REG_READ (I2C_DATA, &data);
197 *return_data = (uchar) data;
200 RESET_REG_BITS (I2C_CONTROL, BIT2 | BIT3);
201 while ((status & 0xff) != 0x58) {
204 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
207 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
210 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /* stop */
215 static uchar i2c_write_data (unsigned int *data, int len)
220 unsigned int *temp_ptr = data;
222 DP (puts ("i2c_write_data\n"));
225 temp = (unsigned int) (*temp_ptr);
226 GT_REG_WRITE (I2C_DATA, temp);
227 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
231 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
233 while ((status & 0xff) != 0x28) {
236 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
239 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
245 /* 11-14-2002 Paul Marchese */
246 /* Can't have the write issuing a stop command */
247 /* it's wrong to have a stop bit in read stream or write stream */
248 /* since we don't know if it's really the end of the command */
249 /* or whether we have just send the device address + offset */
250 /* we will push issuing the stop command off to the original */
251 /* calling function */
252 /* set the interrupt bit in the control register */
253 GT_REG_WRITE (I2C_CONTROL, (0x1 << 3));
254 udelay (I2C_DELAY * 10);
258 /* 11-14-2002 Paul Marchese */
259 /* created this function to get the i2c_write() */
260 /* function working properly. */
261 /* function to write bytes out on the i2c bus */
262 /* this is identical to the function i2c_write_data() */
263 /* except that it requires a buffer that is an */
264 /* unsigned character array. You can't use */
265 /* i2c_write_data() to send an array of unsigned characters */
266 /* since the byte of interest ends up on the wrong end of the bus */
267 /* aah, the joys of big endian versus little endian! */
269 /* returns 0 = success */
270 /* anything other than zero is failure */
271 static uchar i2c_write_byte (unsigned char *data, int len)
276 unsigned char *temp_ptr = data;
278 DP (puts ("i2c_write_byte\n"));
281 /* Set and assert the data */
283 GT_REG_WRITE (I2C_DATA, temp);
284 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
288 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
290 while ((status & 0xff) != 0x28) {
293 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
296 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
302 /* Can't have the write issuing a stop command */
303 /* it's wrong to have a stop bit in read stream or write stream */
304 /* since we don't know if it's really the end of the command */
305 /* or whether we have just send the device address + offset */
306 /* we will push issuing the stop command off to the original */
307 /* calling function */
308 /* GT_REG_WRITE(I2C_CONTROL, (0x1 << 3) | (0x1 << 4));
309 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); */
310 /* set the interrupt bit in the control register */
311 GT_REG_WRITE (I2C_CONTROL, (0x1 << 3));
312 udelay (I2C_DELAY * 10);
318 i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit,
322 unsigned int table[2];
324 /* initialize the table of address offset bytes */
325 /* utilized for 2 byte address offsets */
326 /* NOTE: the order is high byte first! */
327 table[1] = offset & 0xff; /* low byte */
328 table[0] = offset / 0x100; /* high byte */
330 DP (puts ("i2c_set_dev_offset\n"));
332 status = i2c_select_device (dev_addr, 0, ten_bit);
335 printf ("Failed to select device setting offset: 0x%02x\n",
340 /* check the address offset length */
342 /* no address offset */
344 else if (alen == 1) {
345 /* 1 byte address offset */
346 status = i2c_write_data (&offset, 1);
349 printf ("Failed to write data: 0x%02x\n", status);
353 } else if (alen == 2) {
354 /* 2 bytes address offset */
355 status = i2c_write_data (table, 2);
358 printf ("Failed to write data: 0x%02x\n", status);
363 /* address offset unknown or not supported */
364 printf ("Address length offset %d is not supported\n", alen);
367 return 0; /* sucessful completion */
371 i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data,
375 unsigned int i2cFreq = CFG_I2C_SPEED;
377 DP (puts ("i2c_read\n"));
379 i2c_init (i2cFreq, 0); /* set the i2c frequency */
381 status = i2c_start ();
385 printf ("Transaction start failed: 0x%02x\n", status);
390 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
393 printf ("Failed to set slave address & offset: 0x%02x\n",
399 i2c_init (i2cFreq, 0); /* set the i2c frequency again */
401 status = i2c_start ();
404 printf ("Transaction restart failed: 0x%02x\n", status);
409 status = i2c_select_device (dev_addr, 1, 0); /* send the slave address */
412 printf ("Address not acknowledged: 0x%02x\n", status);
417 status = i2c_get_data (data, len);
420 printf ("Data not recieved: 0x%02x\n", status);
428 /* 11-14-2002 Paul Marchese */
429 /* Function to set the I2C stop bit */
432 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4));
435 /* 11-14-2002 Paul Marchese */
436 /* I2C write function */
437 /* dev_addr = device address */
438 /* offset = address offset */
439 /* alen = length in bytes of the address offset */
440 /* data = pointer to buffer to read data into */
441 /* len = # of bytes to read */
443 /* returns 0 = succesful */
444 /* anything but zero is failure */
446 i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
450 unsigned int i2cFreq = CFG_I2C_SPEED;
452 DP (puts ("i2c_write\n"));
454 i2c_init (i2cFreq, 0); /* set the i2c frequency */
456 status = i2c_start (); /* send a start bit */
460 printf ("Transaction start failed: 0x%02x\n", status);
465 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
468 printf ("Failed to set slave address & offset: 0x%02x\n",
475 status = i2c_write_byte (data, len); /* write the data */
478 printf ("Data not written: 0x%02x\n", status);
482 /* issue a stop bit */
487 /* 11-14-2002 Paul Marchese */
488 /* function to determine if an I2C device is present */
489 /* chip = device address of chip to check for */
491 /* returns 0 = sucessful, the device exists */
492 /* anything other than zero is failure, no device */
493 int i2c_probe (uchar chip)
496 /* We are just looking for an <ACK> back. */
497 /* To see if the device/chip is there */
500 unsigned int i2c_status;
503 unsigned int i2cFreq = CFG_I2C_SPEED;
505 DP (puts ("i2c_probe\n"));
507 i2c_init (i2cFreq, 0); /* set the i2c frequency */
509 status = i2c_start (); /* send a start bit */
513 printf ("Transaction start failed: 0x%02x\n", status);
518 status = i2c_set_dev_offset (chip, 0, 0, 0); /* send the slave address + no offset */
521 printf ("Failed to set slave address: 0x%02x\n", status);
526 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &i2c_status);
527 printf ("address %#x returned %#x\n", chip, i2c_status);
529 /* issue a stop bit */
531 return 0; /* successful completion */