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 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) {
46 DP(puts("setup i2c bus\n"));
50 GT_REG_WRITE(I2C_SOFT_RESET, 0);
52 DP(puts("udelay...\n"));
56 DP(puts("set baudrate\n"));
58 GT_REG_WRITE(I2C_STATUS_BAUDE_RATE, (actualm << 3) | actualn);
59 GT_REG_WRITE(I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
61 udelay(I2C_DELAY * 10);
63 DP(puts("read control, baudrate\n"));
65 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
66 GT_REG_READ(I2C_CONTROL, &control);
72 unsigned int control, status;
75 DP(puts("i2c_start\n"));
77 /* Set the start bit */
79 GT_REG_READ(I2C_CONTROL, &control);
80 control |= (0x1 << 5);
81 GT_REG_WRITE(I2C_CONTROL, control);
83 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
86 while ((status & 0xff) != 0x08) {
89 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
92 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
100 i2c_select_device(uchar dev_addr, uchar read, int ten_bit)
102 unsigned int status, data, bits = 7;
105 DP(puts("i2c_select_device\n"));
107 /* Output slave address */
112 data = (dev_addr << 1);
113 /* set the read bit */
115 GT_REG_WRITE(I2C_DATA, data);
116 /* assert the address */
117 RESET_REG_BITS(I2C_CONTROL, BIT3);
121 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
123 while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
126 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
129 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
134 printf("10 bit I2C addressing not yet implemented\n");
142 i2c_get_data(uchar *return_data, int len) {
144 unsigned int data, status = 0;
147 DP(puts("i2c_get_data\n"));
151 /* Get and return the data */
153 RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
155 udelay(I2C_DELAY * 5);
157 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
159 while ((status & 0xff) != 0x50) {
162 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
165 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
168 GT_REG_READ(I2C_DATA, &data);
170 *return_data = (uchar)data;
173 RESET_REG_BITS(I2C_CONTROL, BIT2|BIT3);
174 while ((status & 0xff) != 0x58) {
177 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
180 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
183 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /* stop */
189 i2c_write_data(unsigned int data, int len)
194 DP(puts("i2c_write_data\n"));
200 /* Set and assert the data */
202 GT_REG_WRITE(I2C_DATA, (unsigned int)data);
203 RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
207 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
209 while ((status & 0xff) != 0x28) {
212 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
215 GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
220 GT_REG_WRITE(I2C_CONTROL, (0x1 << 3) | (0x1 << 4));
221 GT_REG_WRITE(I2C_CONTROL, (0x1 << 4));
223 udelay(I2C_DELAY * 10);
229 i2c_set_dev_offset(uchar dev_addr, unsigned int offset, int ten_bit)
233 DP(puts("i2c_set_dev_offset\n"));
235 status = i2c_select_device(dev_addr, 0, ten_bit);
238 printf("Failed to select device setting offset: 0x%02x\n",
244 status = i2c_write_data(offset, 1);
247 printf("Failed to write data: 0x%02x\n", status);
256 i2c_read(uchar dev_addr, unsigned int offset, int len, uchar *data,
260 unsigned int i2cfreq = 400000;
262 DP(puts("i2c_read\n"));
264 i2c_init(i2cfreq, 0);
266 status = i2c_start();
270 printf("Transaction start failed: 0x%02x\n", status);
275 status = i2c_set_dev_offset(dev_addr, 0, 0);
278 printf("Failed to set offset: 0x%02x\n", status);
283 i2c_init(i2cfreq, 0);
285 status = i2c_start();
288 printf("Transaction restart failed: 0x%02x\n", status);
293 status = i2c_select_device(dev_addr, 1, ten_bit);
296 printf("Address not acknowledged: 0x%02x\n", status);
301 status = i2c_get_data(data, len);
304 printf("Data not received: 0x%02x\n", status);