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
25 * for cpci750 by reinhard.arlt@esd-electronics.com
31 #include "../../Marvell/include/mv_gen_reg.h"
32 #include "../../Marvell/include/core.h"
43 /* Assuming that there is only one master on the bus (us) */
45 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 minMargin = 0xffffffff;
50 unsigned int tclk = CONFIG_SYS_TCLK;
51 unsigned int i2cFreq = speed; /* 100000 max. Fast mode not supported */
53 DP (puts ("i2c_init\n"));
55 for (n = 0; n < 8; n++) {
56 for (m = 0; m < 16; m++) {
57 power = 2 << n; /* power = 2^(n+1) */
58 freq = tclk / (10 * (m + 1) * power);
60 margin = i2cFreq - freq;
62 margin = freq - i2cFreq;
63 if (margin < minMargin) {
71 DP (puts ("setup i2c bus\n"));
75 GT_REG_WRITE (I2C_SOFT_RESET, 0);
77 GT_REG_WRITE (I2C_CONTROL, 0);
80 DP (puts ("set baudrate\n"));
82 GT_REG_WRITE (I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
85 DP (puts ("udelay...\n"));
89 GT_REG_WRITE (I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
94 static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
96 unsigned int status, data, bits = 7;
100 DP (puts ("i2c_select_device\n"));
102 /* Output slave address */
108 GT_REG_READ (I2C_CONTROL, &control);
109 control |= (0x1 << 2);
110 GT_REG_WRITE (I2C_CONTROL, control);
113 GT_REG_READ (I2C_CONTROL, &control);
114 control |= (0x1 << 5); /* generate the I2C_START_BIT */
115 GT_REG_WRITE (I2C_CONTROL, control);
117 RESET_REG_BITS (I2C_CONTROL, (0x01 << 3));
120 GT_REG_READ (I2C_CONTROL, &status);
121 while ((status & 0x08) != 0x08) {
122 GT_REG_READ (I2C_CONTROL, &status);
128 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
129 while (((status & 0xff) != 0x08) && ((status & 0xff) != 0x10)){
132 printf ("Failed to set startbit: 0x%02x\n", status);
134 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
138 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
142 DP (puts ("i2c_select_device:write addr byte\n"));
144 /* assert the address */
146 data = (dev_addr << 1);
147 /* set the read bit */
149 GT_REG_WRITE (I2C_DATA, data);
151 RESET_REG_BITS (I2C_CONTROL, BIT3);
154 GT_REG_READ (I2C_CONTROL, &status);
155 while ((status & 0x08) != 0x08) {
156 GT_REG_READ (I2C_CONTROL, &status);
159 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
161 while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
164 printf ("Failed to write address: 0x%02x\n", status);
166 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
169 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
175 printf ("10 bit I2C addressing not yet implemented\n");
182 static uchar i2c_get_data (uchar * return_data, int len)
185 unsigned int data, status;
188 DP (puts ("i2c_get_data\n"));
192 RESET_REG_BITS (I2C_CONTROL, BIT3);
195 /* Get and return the data */
197 GT_REG_READ (I2C_CONTROL, &status);
198 while ((status & 0x08) != 0x08) {
199 GT_REG_READ (I2C_CONTROL, &status);
202 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
204 while ((status & 0xff) != 0x50) {
207 printf ("Failed to get data len status: 0x%02x\n", status);
209 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
213 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
216 GT_REG_READ (I2C_DATA, &data);
218 *return_data = (uchar) data;
222 RESET_REG_BITS (I2C_CONTROL, BIT2 | BIT3);
226 GT_REG_READ (I2C_CONTROL, &status);
227 while ((status & 0x08) != 0x08) {
228 GT_REG_READ (I2C_CONTROL, &status);
231 while ((status & 0xff) != 0x58) {
233 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
236 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
239 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /* stop */
241 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
248 static uchar i2c_write_data (unsigned int *data, int len)
253 unsigned int *temp_ptr = data;
255 DP (puts ("i2c_write_data\n"));
259 temp = (unsigned int) (*temp_ptr);
260 GT_REG_WRITE (I2C_DATA, temp);
262 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
265 GT_REG_READ (I2C_CONTROL, &status);
266 while ((status & 0x08) != 0x08) {
267 GT_REG_READ (I2C_CONTROL, &status);
270 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
272 while ((status & 0xff) != 0x28) {
274 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
278 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
288 static uchar i2c_write_byte (unsigned char *data, int len)
293 unsigned char *temp_ptr = data;
295 DP (puts ("i2c_write_byte\n"));
299 /* Set and assert the data */
301 GT_REG_WRITE (I2C_DATA, temp);
303 RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
307 GT_REG_READ (I2C_CONTROL, &status);
308 while ((status & 0x08) != 0x08) {
309 GT_REG_READ (I2C_CONTROL, &status);
312 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
314 while ((status & 0xff) != 0x28) {
316 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
320 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
330 i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit,
334 unsigned int table[2];
336 table[1] = (offset ) & 0x0ff; /* low byte */
337 table[0] = (offset >> 8) & 0x0ff; /* high byte */
339 DP (puts ("i2c_set_dev_offset\n"));
341 status = i2c_select_device (dev_addr, 0, ten_bit);
344 22 printf ("Failed to select device setting offset: 0x%02x\n",
349 /* check the address offset length */
351 /* no address offset */
353 else if (alen == 1) {
354 /* 1 byte address offset */
355 status = i2c_write_data (&offset, 1);
358 printf ("Failed to write data: 0x%02x\n", status);
362 } else if (alen == 2) {
363 /* 2 bytes address offset */
364 status = i2c_write_data (table, 2);
367 printf ("Failed to write data: 0x%02x\n", status);
372 /* address offset unknown or not supported */
373 printf ("Address length offset %d is not supported\n", alen);
376 return 0; /* sucessful completion */
380 i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data,
384 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
386 DP (puts ("i2c_read\n"));
388 /* set the i2c frequency */
389 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
391 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
394 printf ("Failed to set slave address & offset: 0x%02x\n",
400 status = i2c_select_device (dev_addr, 1, 0);
403 printf ("Failed to select device for data read: 0x%02x\n",
409 status = i2c_get_data (data, len);
412 printf ("Data not read: 0x%02x\n", status);
423 GT_REG_WRITE (I2C_CONTROL, (0x1 << 4));
429 i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
433 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
435 DP (puts ("i2c_write\n"));
437 /* set the i2c frequency */
438 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
440 status = i2c_set_dev_offset (dev_addr, offset, 0, alen); /* send the slave address + offset */
443 printf ("Failed to set slave address & offset: 0x%02x\n",
450 status = i2c_write_byte (data, len); /* write the data */
453 printf ("Data not written: 0x%02x\n", status);
457 /* issue a stop bit */
463 int i2c_probe (uchar chip)
467 unsigned int i2c_status;
470 unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
472 DP (puts ("i2c_probe\n"));
474 /* set the i2c frequency */
475 i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
477 status = i2c_set_dev_offset (chip, 0, 0, 0); /* send the slave address + no offset */
480 printf ("Failed to set slave address: 0x%02x\n", status);
485 GT_REG_READ (I2C_STATUS_BAUDE_RATE, &i2c_status);
486 printf ("address %#x returned %#x\n", chip, i2c_status);
488 /* issue a stop bit */
490 return 0; /* successful completion */