Commit | Line | Data |
---|---|---|
81dee67e SM |
1 | #define USE_HW_I2C |
2 | #ifdef USE_HW_I2C | |
3 | #include "ddk750_help.h" | |
4 | #include "ddk750_reg.h" | |
5 | #include "ddk750_hwi2c.h" | |
6 | #include "ddk750_power.h" | |
7 | ||
8 | #define MAX_HWI2C_FIFO 16 | |
9 | #define HWI2C_WAIT_TIMEOUT 0xF0000 | |
10 | ||
19f70eae | 11 | int sm750_hw_i2c_init( |
a503da64 | 12 | unsigned char bus_speed_mode |
81dee67e SM |
13 | ) |
14 | { | |
78376535 | 15 | unsigned int value; |
81dee67e | 16 | |
78376535 | 17 | /* Enable GPIO 30 & 31 as IIC clock & data */ |
81dee67e SM |
18 | value = PEEK32(GPIO_MUX); |
19 | ||
78376535 JL |
20 | value = FIELD_SET(value, GPIO_MUX, 30, I2C) | |
21 | FIELD_SET(0, GPIO_MUX, 31, I2C); | |
81dee67e SM |
22 | POKE32(GPIO_MUX, value); |
23 | ||
78376535 JL |
24 | /* Enable Hardware I2C power. |
25 | TODO: Check if we need to enable GPIO power? | |
26 | */ | |
27 | enableI2C(1); | |
28 | ||
29 | /* Enable the I2C Controller and set the bus speed mode */ | |
30 | value = PEEK32(I2C_CTRL); | |
a503da64 | 31 | if (bus_speed_mode == 0) |
78376535 JL |
32 | value = FIELD_SET(value, I2C_CTRL, MODE, STANDARD); |
33 | else | |
34 | value = FIELD_SET(value, I2C_CTRL, MODE, FAST); | |
35 | value = FIELD_SET(value, I2C_CTRL, EN, ENABLE); | |
36 | POKE32(I2C_CTRL, value); | |
37 | ||
38 | return 0; | |
81dee67e SM |
39 | } |
40 | ||
ed7042ed | 41 | void sm750_hw_i2c_close(void) |
81dee67e | 42 | { |
78376535 | 43 | unsigned int value; |
81dee67e | 44 | |
78376535 JL |
45 | /* Disable I2C controller */ |
46 | value = PEEK32(I2C_CTRL); | |
47 | value = FIELD_SET(value, I2C_CTRL, EN, DISABLE); | |
48 | POKE32(I2C_CTRL, value); | |
81dee67e | 49 | |
78376535 JL |
50 | /* Disable I2C Power */ |
51 | enableI2C(0); | |
81dee67e | 52 | |
78376535 JL |
53 | /* Set GPIO 30 & 31 back as GPIO pins */ |
54 | value = PEEK32(GPIO_MUX); | |
55 | value = FIELD_SET(value, GPIO_MUX, 30, GPIO); | |
56 | value = FIELD_SET(value, GPIO_MUX, 31, GPIO); | |
57 | POKE32(GPIO_MUX, value); | |
81dee67e SM |
58 | } |
59 | ||
ac118951 | 60 | static long hw_i2c_wait_tx_done(void) |
81dee67e | 61 | { |
78376535 | 62 | unsigned int timeout; |
81dee67e | 63 | |
78376535 JL |
64 | /* Wait until the transfer is completed. */ |
65 | timeout = HWI2C_WAIT_TIMEOUT; | |
81dee67e | 66 | while ((FIELD_GET(PEEK32(I2C_STATUS), I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) && |
78376535 | 67 | (timeout != 0)) |
81dee67e SM |
68 | timeout--; |
69 | ||
70 | if (timeout == 0) | |
78376535 | 71 | return (-1); |
81dee67e | 72 | |
78376535 | 73 | return 0; |
81dee67e SM |
74 | } |
75 | ||
81dee67e SM |
76 | /* |
77 | * This function writes data to the i2c slave device registers. | |
78 | * | |
79 | * Parameters: | |
b3696b79 | 80 | * addr - i2c Slave device address |
81dee67e | 81 | * length - Total number of bytes to be written to the device |
b3696b79 | 82 | * buf - The buffer that contains the data to be written to the |
81dee67e SM |
83 | * i2c device. |
84 | * | |
85 | * Return Value: | |
86 | * Total number of bytes those are actually written. | |
87 | */ | |
ac118951 | 88 | static unsigned int hw_i2c_write_data( |
b3696b79 | 89 | unsigned char addr, |
78376535 | 90 | unsigned int length, |
b3696b79 | 91 | unsigned char *buf |
81dee67e SM |
92 | ) |
93 | { | |
78376535 | 94 | unsigned char count, i; |
b3696b79 | 95 | unsigned int total_bytes = 0; |
81dee67e | 96 | |
78376535 | 97 | /* Set the Device Address */ |
b3696b79 | 98 | POKE32(I2C_SLAVE_ADDRESS, addr & ~0x01); |
81dee67e | 99 | |
78376535 JL |
100 | /* Write data. |
101 | * Note: | |
102 | * Only 16 byte can be accessed per i2c start instruction. | |
103 | */ | |
259fef35 | 104 | do { |
78376535 JL |
105 | /* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */ |
106 | POKE32(I2C_RESET, 0); | |
81dee67e | 107 | |
78376535 JL |
108 | /* Set the number of bytes to be written */ |
109 | if (length < MAX_HWI2C_FIFO) | |
110 | count = length - 1; | |
111 | else | |
112 | count = MAX_HWI2C_FIFO - 1; | |
113 | POKE32(I2C_BYTE_COUNT, count); | |
81dee67e | 114 | |
78376535 JL |
115 | /* Move the data to the I2C data register */ |
116 | for (i = 0; i <= count; i++) | |
b3696b79 | 117 | POKE32(I2C_DATA0 + i, *buf++); |
81dee67e | 118 | |
78376535 JL |
119 | /* Start the I2C */ |
120 | POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START)); | |
81dee67e | 121 | |
78376535 | 122 | /* Wait until the transfer is completed. */ |
ac118951 | 123 | if (hw_i2c_wait_tx_done() != 0) |
78376535 | 124 | break; |
81dee67e | 125 | |
78376535 JL |
126 | /* Substract length */ |
127 | length -= (count + 1); | |
81dee67e | 128 | |
78376535 | 129 | /* Total byte written */ |
b3696b79 | 130 | total_bytes += (count + 1); |
81dee67e | 131 | |
78376535 | 132 | } while (length > 0); |
81dee67e | 133 | |
b3696b79 | 134 | return total_bytes; |
81dee67e SM |
135 | } |
136 | ||
81dee67e SM |
137 | /* |
138 | * This function reads data from the slave device and stores them | |
139 | * in the given buffer | |
140 | * | |
141 | * Parameters: | |
b3696b79 | 142 | * addr - i2c Slave device address |
81dee67e | 143 | * length - Total number of bytes to be read |
b3696b79 | 144 | * buf - Pointer to a buffer to be filled with the data read |
81dee67e SM |
145 | * from the slave device. It has to be the same size as the |
146 | * length to make sure that it can keep all the data read. | |
147 | * | |
148 | * Return Value: | |
149 | * Total number of actual bytes read from the slave device | |
150 | */ | |
ac118951 | 151 | static unsigned int hw_i2c_read_data( |
b3696b79 | 152 | unsigned char addr, |
78376535 | 153 | unsigned int length, |
b3696b79 | 154 | unsigned char *buf |
81dee67e SM |
155 | ) |
156 | { | |
78376535 | 157 | unsigned char count, i; |
b3696b79 | 158 | unsigned int total_bytes = 0; |
81dee67e | 159 | |
78376535 | 160 | /* Set the Device Address */ |
b3696b79 | 161 | POKE32(I2C_SLAVE_ADDRESS, addr | 0x01); |
81dee67e | 162 | |
78376535 JL |
163 | /* Read data and save them to the buffer. |
164 | * Note: | |
165 | * Only 16 byte can be accessed per i2c start instruction. | |
166 | */ | |
259fef35 | 167 | do { |
78376535 JL |
168 | /* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */ |
169 | POKE32(I2C_RESET, 0); | |
81dee67e | 170 | |
78376535 JL |
171 | /* Set the number of bytes to be read */ |
172 | if (length <= MAX_HWI2C_FIFO) | |
173 | count = length - 1; | |
174 | else | |
175 | count = MAX_HWI2C_FIFO - 1; | |
176 | POKE32(I2C_BYTE_COUNT, count); | |
81dee67e | 177 | |
78376535 JL |
178 | /* Start the I2C */ |
179 | POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START)); | |
81dee67e | 180 | |
78376535 | 181 | /* Wait until transaction done. */ |
ac118951 | 182 | if (hw_i2c_wait_tx_done() != 0) |
78376535 | 183 | break; |
81dee67e | 184 | |
78376535 JL |
185 | /* Save the data to the given buffer */ |
186 | for (i = 0; i <= count; i++) | |
b3696b79 | 187 | *buf++ = PEEK32(I2C_DATA0 + i); |
81dee67e | 188 | |
78376535 JL |
189 | /* Substract length by 16 */ |
190 | length -= (count + 1); | |
81dee67e | 191 | |
78376535 | 192 | /* Number of bytes read. */ |
b3696b79 | 193 | total_bytes += (count + 1); |
81dee67e | 194 | |
78376535 | 195 | } while (length > 0); |
81dee67e | 196 | |
b3696b79 | 197 | return total_bytes; |
81dee67e SM |
198 | } |
199 | ||
81dee67e SM |
200 | /* |
201 | * This function reads the slave device's register | |
202 | * | |
203 | * Parameters: | |
204 | * deviceAddress - i2c Slave device address which register | |
205 | * to be read from | |
206 | * registerIndex - Slave device's register to be read | |
207 | * | |
208 | * Return Value: | |
209 | * Register value | |
210 | */ | |
5ccf7340 | 211 | unsigned char sm750_hw_i2c_read_reg( |
938ad7ed MR |
212 | unsigned char addr, |
213 | unsigned char reg | |
81dee67e SM |
214 | ) |
215 | { | |
78376535 | 216 | unsigned char value = (0xFF); |
81dee67e | 217 | |
938ad7ed MR |
218 | if (hw_i2c_write_data(addr, 1, ®) == 1) |
219 | hw_i2c_read_data(addr, 1, &value); | |
81dee67e | 220 | |
78376535 | 221 | return value; |
81dee67e SM |
222 | } |
223 | ||
81dee67e SM |
224 | /* |
225 | * This function writes a value to the slave device's register | |
226 | * | |
227 | * Parameters: | |
228 | * deviceAddress - i2c Slave device address which register | |
229 | * to be written | |
230 | * registerIndex - Slave device's register to be written | |
231 | * data - Data to be written to the register | |
232 | * | |
233 | * Result: | |
234 | * 0 - Success | |
235 | * -1 - Fail | |
236 | */ | |
6bdbe62b | 237 | int sm750_hw_i2c_write_reg( |
938ad7ed MR |
238 | unsigned char addr, |
239 | unsigned char reg, | |
78376535 | 240 | unsigned char data |
81dee67e SM |
241 | ) |
242 | { | |
78376535 | 243 | unsigned char value[2]; |
81dee67e | 244 | |
938ad7ed | 245 | value[0] = reg; |
78376535 | 246 | value[1] = data; |
938ad7ed | 247 | if (hw_i2c_write_data(addr, 2, value) == 2) |
78376535 | 248 | return 0; |
81dee67e | 249 | |
78376535 | 250 | return (-1); |
81dee67e SM |
251 | } |
252 | ||
81dee67e | 253 | #endif |