]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/evb64260/i2c.c
4 #include <galileo/gt64260R.h>
5 #include <galileo/core.h>
7 #define MAX_I2C_RETRYS 10
8 #define I2C_DELAY 1000 /* Should be at least the # of MHz of Tclk */
17 /* Assuming that there is only one master on the bus (us) */
20 i2c_init(int speed
, int slaveaddr
)
22 unsigned int n
, m
, freq
, margin
, power
;
23 unsigned int actualFreq
, actualN
=0, actualM
=0;
24 unsigned int control
, status
;
25 unsigned int minMargin
= 0xffffffff;
26 unsigned int tclk
= 125000000;
28 DP(puts("i2c_init\n"));
30 for(n
= 0 ; n
< 8 ; n
++)
32 for(m
= 0 ; m
< 16 ; m
++)
34 power
= 2<<n
; /* power = 2^(n+1) */
35 freq
= tclk
/(10*(m
+1)*power
);
37 margin
= speed
- freq
;
39 margin
= freq
- speed
;
40 if(margin
< minMargin
)
50 DP(puts("setup i2c bus\n"));
54 GT_REG_WRITE(I2C_SOFT_RESET
, 0);
56 DP(puts("udelay...\n"));
60 DP(puts("set baudrate\n"));
62 GT_REG_WRITE(I2C_STATUS_BAUDE_RATE
, (actualM
<< 3) | actualN
);
63 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 2) | (0x1 << 6));
65 udelay(I2C_DELAY
* 10);
67 DP(puts("read control, baudrate\n"));
69 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
70 GT_REG_READ(I2C_CONTROL
, &control
);
76 unsigned int control
, status
;
79 DP(puts("i2c_start\n"));
81 /* Set the start bit */
83 GT_REG_READ(I2C_CONTROL
, &control
);
84 control
|= (0x1 << 5);
85 GT_REG_WRITE(I2C_CONTROL
, control
);
87 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
90 while ((status
& 0xff) != 0x08) {
93 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /*stop*/
96 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
104 i2c_select_device(uchar dev_addr
, uchar read
, int ten_bit
)
106 unsigned int status
, data
, bits
= 7;
109 DP(puts("i2c_select_device\n"));
111 /* Output slave address */
117 data
= (dev_addr
<< 1);
118 /* set the read bit */
120 GT_REG_WRITE(I2C_DATA
, data
);
121 /* assert the address */
122 RESET_REG_BITS(I2C_CONTROL
, BIT3
);
126 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
128 while (((status
& 0xff) != 0x40) && ((status
& 0xff) != 0x18)) {
131 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /*stop*/
134 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
139 printf("10 bit I2C addressing not yet implemented\n");
147 i2c_get_data(uchar
* return_data
, int len
) {
149 unsigned int data
, status
;
152 DP(puts("i2c_get_data\n"));
156 /* Get and return the data */
158 RESET_REG_BITS(I2C_CONTROL
, (0x1 << 3));
160 udelay(I2C_DELAY
* 5);
162 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
164 while ((status
& 0xff) != 0x50) {
167 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /*stop*/
170 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
173 GT_REG_READ(I2C_DATA
, &data
);
175 *return_data
= (uchar
)data
;
178 RESET_REG_BITS(I2C_CONTROL
, BIT2
|BIT3
);
179 while ((status
& 0xff) != 0x58) {
182 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /*stop*/
185 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
188 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /* stop */
194 i2c_write_data(unsigned int data
, int len
)
199 DP(puts("i2c_write_data\n"));
205 /* Set and assert the data */
207 GT_REG_WRITE(I2C_DATA
, (unsigned int)data
);
208 RESET_REG_BITS(I2C_CONTROL
, (0x1 << 3));
212 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
214 while ((status
& 0xff) != 0x28) {
217 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4)); /*stop*/
220 GT_REG_READ(I2C_STATUS_BAUDE_RATE
, &status
);
225 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 3) | (0x1 << 4));
226 GT_REG_WRITE(I2C_CONTROL
, (0x1 << 4));
228 udelay(I2C_DELAY
* 10);
234 i2c_set_dev_offset(uchar dev_addr
, unsigned int offset
, int ten_bit
)
238 DP(puts("i2c_set_dev_offset\n"));
240 status
= i2c_select_device(dev_addr
, 0, ten_bit
);
243 printf("Failed to select device setting offset: 0x%02x\n",
249 status
= i2c_write_data(offset
, 1);
252 printf("Failed to write data: 0x%02x\n", status
);
261 i2c_read(uchar dev_addr
, unsigned int offset
, int len
, uchar
* data
,
265 unsigned int i2cFreq
= 400000;
267 DP(puts("i2c_read\n"));
271 status
= i2c_start();
275 printf("Transaction start failed: 0x%02x\n", status
);
280 status
= i2c_set_dev_offset(dev_addr
, 0, 0);
283 printf("Failed to set offset: 0x%02x\n", status
);
290 status
= i2c_start();
293 printf("Transaction restart failed: 0x%02x\n", status
);
298 status
= i2c_select_device(dev_addr
, 1, ten_bit
);
301 printf("Address not acknowledged: 0x%02x\n", status
);
306 status
= i2c_get_data(data
, len
);
309 printf("Data not recieved: 0x%02x\n", status
);