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