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++) {
31 for (m = 0 ; m < 16 ; m++) {
32 power = 2<<n; /* power = 2^(n+1) */
33 freq = tclk/(10*(m+1)*power);
35 margin = speed - freq;
37 margin = freq - speed;
38 if (margin < minMargin) {
47 DP(puts("setup i2c bus\n"));
51 GT_REG_WRITE(I2C_SOFT_RESET, 0);
53 DP(puts("udelay...\n"));
57 DP(puts("set baudrate\n"));
59 GT_REG_WRITE(I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
60 GT_REG_WRITE(I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
62 udelay(I2C_DELAY * 10);
64 DP(puts("read control, baudrate\n"));
66 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
67 GT_REG_READ(I2C_CONTROL, &control);
73 unsigned int control, status;
76 DP(puts("i2c_start\n"));
78 /* Set the start bit */
80 GT_REG_READ(I2C_CONTROL, &control);
81 control |= (0x1 << 5);
82 GT_REG_WRITE(I2C_CONTROL, control);
84 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
87 while ((status & 0xff) != 0x08) {
90 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
93 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
101 i2c_select_device(uchar dev_addr, uchar read, int ten_bit)
103 unsigned int status, data, bits = 7;
106 DP(puts("i2c_select_device\n"));
108 /* Output slave address */
113 data = (dev_addr << 1);
114 /* set the read bit */
116 GT_REG_WRITE(I2C_DATA, data);
117 /* assert the address */
118 RESET_REG_BITS(I2C_CONTROL, BIT3);
122 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
124 while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
127 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
130 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
135 printf("10 bit I2C addressing not yet implemented\n");
143 i2c_get_data(uchar *return_data, int len) {
145 unsigned int data, status = 0;
148 DP(puts("i2c_get_data\n"));
152 /* Get and return the data */
154 RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
156 udelay(I2C_DELAY * 5);
158 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
160 while ((status & 0xff) != 0x50) {
163 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
166 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
169 GT_REG_READ(I2C_DATA, &data);
171 *return_data = (uchar)data;
174 RESET_REG_BITS(I2C_CONTROL, BIT2|BIT3);
175 while ((status & 0xff) != 0x58) {
178 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
181 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
184 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /* stop */
190 i2c_write_data(unsigned int data, int len)
195 DP(puts("i2c_write_data\n"));
201 /* Set and assert the data */
203 GT_REG_WRITE(I2C_DATA, (unsigned int)data);
204 RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
208 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
210 while ((status & 0xff) != 0x28) {
213 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
216 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
221 GT_REG_WRITE(I2C_CONTROL, (0x1 << 3) | (0x1 << 4));
222 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4));
224 udelay(I2C_DELAY * 10);
230 i2c_set_dev_offset(uchar dev_addr, unsigned int offset, int ten_bit)
234 DP(puts("i2c_set_dev_offset\n"));
236 status = i2c_select_device(dev_addr, 0, ten_bit);
239 printf("Failed to select device setting offset: 0x%02x\n",
245 status = i2c_write_data(offset, 1);
248 printf("Failed to write data: 0x%02x\n", status);
257 i2c_read(uchar dev_addr, unsigned int offset, int len, uchar *data,
261 unsigned int i2cFreq = 400000;
263 DP(puts("i2c_read\n"));
265 i2c_init(i2cFreq, 0);
267 status = i2c_start();
271 printf("Transaction start failed: 0x%02x\n", status);
276 status = i2c_set_dev_offset(dev_addr, 0, 0);
279 printf("Failed to set offset: 0x%02x\n", status);
284 i2c_init(i2cFreq, 0);
286 status = i2c_start();
289 printf("Transaction restart failed: 0x%02x\n", status);
294 status = i2c_select_device(dev_addr, 1, ten_bit);
297 printf("Address not acknowledged: 0x%02x\n", status);
302 status = i2c_get_data(data, len);
305 printf("Data not received: 0x%02x\n", status);