]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/esd/cpci750/i2c.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
7 * Hacked for the DB64360 board by Ingo.Assmus@keymile.com
8 * extra improvments by Brain Waite
9 * for cpci750 by reinhard.arlt@esd-electronics.com
15 #include "../../Marvell/include/mv_gen_reg.h"
16 #include "../../Marvell/include/core.h"
27 /* Assuming that there is only one master on the bus (us) */
29 void i2c_init (int speed
, int slaveaddr
)
31 unsigned int n
, m
, freq
, margin
, power
;
32 unsigned int actualN
= 0, actualM
= 0;
33 unsigned int minMargin
= 0xffffffff;
34 unsigned int tclk
= CONFIG_SYS_TCLK
;
35 unsigned int i2cFreq
= speed
; /* 100000 max. Fast mode not supported */
37 DP (puts ("i2c_init\n"));
39 for (n
= 0; n
< 8; n
++) {
40 for (m
= 0; m
< 16; m
++) {
41 power
= 2 << n
; /* power = 2^(n+1) */
42 freq
= tclk
/ (10 * (m
+ 1) * power
);
44 margin
= i2cFreq
- freq
;
46 margin
= freq
- i2cFreq
;
47 if (margin
< minMargin
) {
55 DP (puts ("setup i2c bus\n"));
59 GT_REG_WRITE (I2C_SOFT_RESET
, 0);
61 GT_REG_WRITE (I2C_CONTROL
, 0);
64 DP (puts ("set baudrate\n"));
66 GT_REG_WRITE (I2C_STATUS_BAUDE_RATE
, (actualM
<< 3) | actualN
);
69 DP (puts ("udelay...\n"));
73 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 2) | (0x1 << 6));
78 static uchar
i2c_select_device (uchar dev_addr
, uchar read
, int ten_bit
)
80 unsigned int status
, data
, bits
= 7;
84 DP (puts ("i2c_select_device\n"));
86 /* Output slave address */
92 GT_REG_READ (I2C_CONTROL
, &control
);
93 control
|= (0x1 << 2);
94 GT_REG_WRITE (I2C_CONTROL
, control
);
97 GT_REG_READ (I2C_CONTROL
, &control
);
98 control
|= (0x1 << 5); /* generate the I2C_START_BIT */
99 GT_REG_WRITE (I2C_CONTROL
, control
);
101 RESET_REG_BITS (I2C_CONTROL
, (0x01 << 3));
104 GT_REG_READ (I2C_CONTROL
, &status
);
105 while ((status
& 0x08) != 0x08) {
106 GT_REG_READ (I2C_CONTROL
, &status
);
112 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
113 while (((status
& 0xff) != 0x08) && ((status
& 0xff) != 0x10)){
116 printf ("Failed to set startbit: 0x%02x\n", status
);
118 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
122 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
126 DP (puts ("i2c_select_device:write addr byte\n"));
128 /* assert the address */
130 data
= (dev_addr
<< 1);
131 /* set the read bit */
133 GT_REG_WRITE (I2C_DATA
, data
);
135 RESET_REG_BITS (I2C_CONTROL
, BIT3
);
138 GT_REG_READ (I2C_CONTROL
, &status
);
139 while ((status
& 0x08) != 0x08) {
140 GT_REG_READ (I2C_CONTROL
, &status
);
143 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
145 while (((status
& 0xff) != 0x40) && ((status
& 0xff) != 0x18)) {
148 printf ("Failed to write address: 0x%02x\n", status
);
150 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
153 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
159 printf ("10 bit I2C addressing not yet implemented\n");
166 static uchar
i2c_get_data (uchar
* return_data
, int len
)
169 unsigned int data
, status
;
172 DP (puts ("i2c_get_data\n"));
176 RESET_REG_BITS (I2C_CONTROL
, BIT3
);
179 /* Get and return the data */
181 GT_REG_READ (I2C_CONTROL
, &status
);
182 while ((status
& 0x08) != 0x08) {
183 GT_REG_READ (I2C_CONTROL
, &status
);
186 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
188 while ((status
& 0xff) != 0x50) {
191 printf ("Failed to get data len status: 0x%02x\n", status
);
193 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
197 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
200 GT_REG_READ (I2C_DATA
, &data
);
202 *return_data
= (uchar
) data
;
206 RESET_REG_BITS (I2C_CONTROL
, BIT2
| BIT3
);
210 GT_REG_READ (I2C_CONTROL
, &status
);
211 while ((status
& 0x08) != 0x08) {
212 GT_REG_READ (I2C_CONTROL
, &status
);
215 while ((status
& 0xff) != 0x58) {
217 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
220 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
223 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /* stop */
225 RESET_REG_BITS (I2C_CONTROL
, (0x1 << 3));
232 static uchar
i2c_write_data (unsigned int *data
, int len
)
237 unsigned int *temp_ptr
= data
;
239 DP (puts ("i2c_write_data\n"));
243 temp
= (unsigned int) (*temp_ptr
);
244 GT_REG_WRITE (I2C_DATA
, temp
);
246 RESET_REG_BITS (I2C_CONTROL
, (0x1 << 3));
249 GT_REG_READ (I2C_CONTROL
, &status
);
250 while ((status
& 0x08) != 0x08) {
251 GT_REG_READ (I2C_CONTROL
, &status
);
254 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
256 while ((status
& 0xff) != 0x28) {
258 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
262 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
272 static uchar
i2c_write_byte (unsigned char *data
, int len
)
277 unsigned char *temp_ptr
= data
;
279 DP (puts ("i2c_write_byte\n"));
283 /* Set and assert the data */
285 GT_REG_WRITE (I2C_DATA
, temp
);
287 RESET_REG_BITS (I2C_CONTROL
, (0x1 << 3));
291 GT_REG_READ (I2C_CONTROL
, &status
);
292 while ((status
& 0x08) != 0x08) {
293 GT_REG_READ (I2C_CONTROL
, &status
);
296 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
298 while ((status
& 0xff) != 0x28) {
300 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4)); /*stop */
304 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &status
);
314 i2c_set_dev_offset (uchar dev_addr
, unsigned int offset
, int ten_bit
,
318 unsigned int table
[2];
320 table
[1] = (offset
) & 0x0ff; /* low byte */
321 table
[0] = (offset
>> 8) & 0x0ff; /* high byte */
323 DP (puts ("i2c_set_dev_offset\n"));
325 status
= i2c_select_device (dev_addr
, 0, ten_bit
);
328 22 printf ("Failed to select device setting offset: 0x%02x\n",
333 /* check the address offset length */
335 /* no address offset */
337 else if (alen
== 1) {
338 /* 1 byte address offset */
339 status
= i2c_write_data (&offset
, 1);
342 printf ("Failed to write data: 0x%02x\n", status
);
346 } else if (alen
== 2) {
347 /* 2 bytes address offset */
348 status
= i2c_write_data (table
, 2);
351 printf ("Failed to write data: 0x%02x\n", status
);
356 /* address offset unknown or not supported */
357 printf ("Address length offset %d is not supported\n", alen
);
360 return 0; /* sucessful completion */
364 i2c_read (uchar dev_addr
, unsigned int offset
, int alen
, uchar
* data
,
368 unsigned int i2cFreq
= CONFIG_SYS_I2C_SPEED
;
370 DP (puts ("i2c_read\n"));
372 /* set the i2c frequency */
373 i2c_init (i2cFreq
, CONFIG_SYS_I2C_SLAVE
);
375 status
= i2c_set_dev_offset (dev_addr
, offset
, 0, alen
); /* send the slave address + offset */
378 printf ("Failed to set slave address & offset: 0x%02x\n",
384 status
= i2c_select_device (dev_addr
, 1, 0);
387 printf ("Failed to select device for data read: 0x%02x\n",
393 status
= i2c_get_data (data
, len
);
396 printf ("Data not read: 0x%02x\n", status
);
407 GT_REG_WRITE (I2C_CONTROL
, (0x1 << 4));
413 i2c_write (uchar dev_addr
, unsigned int offset
, int alen
, uchar
* data
,
417 unsigned int i2cFreq
= CONFIG_SYS_I2C_SPEED
;
419 DP (puts ("i2c_write\n"));
421 /* set the i2c frequency */
422 i2c_init (i2cFreq
, CONFIG_SYS_I2C_SLAVE
);
424 status
= i2c_set_dev_offset (dev_addr
, offset
, 0, alen
); /* send the slave address + offset */
427 printf ("Failed to set slave address & offset: 0x%02x\n",
434 status
= i2c_write_byte (data
, len
); /* write the data */
437 printf ("Data not written: 0x%02x\n", status
);
441 /* issue a stop bit */
447 int i2c_probe (uchar chip
)
451 unsigned int i2c_status
;
454 unsigned int i2cFreq
= CONFIG_SYS_I2C_SPEED
;
456 DP (puts ("i2c_probe\n"));
458 /* set the i2c frequency */
459 i2c_init (i2cFreq
, CONFIG_SYS_I2C_SLAVE
);
461 status
= i2c_set_dev_offset (chip
, 0, 0, 0); /* send the slave address + no offset */
464 printf ("Failed to set slave address: 0x%02x\n", status
);
469 GT_REG_READ (I2C_STATUS_BAUDE_RATE
, &i2c_status
);
470 printf ("address %#x returned %#x\n", chip
, i2c_status
);
472 /* issue a stop bit */
474 return 0; /* successful completion */