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 
11 int sm750_hw_i2c_init(
12 unsigned char bus_speed_mode
13 )
14 {
15 	unsigned int value;
16 
17 	/* Enable GPIO 30 & 31 as IIC clock & data */
18 	value = PEEK32(GPIO_MUX);
19 
20 	value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
21 			  FIELD_SET(0, GPIO_MUX, 31, I2C);
22 	POKE32(GPIO_MUX, value);
23 
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);
31 	if (bus_speed_mode == 0)
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;
39 }
40 
41 void sm750_hw_i2c_close(void)
42 {
43 	unsigned int value;
44 
45 	/* Disable I2C controller */
46 	value = PEEK32(I2C_CTRL);
47 	value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
48 	POKE32(I2C_CTRL, value);
49 
50 	/* Disable I2C Power */
51 	enableI2C(0);
52 
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);
58 }
59 
60 static long hw_i2c_wait_tx_done(void)
61 {
62 	unsigned int timeout;
63 
64 	/* Wait until the transfer is completed. */
65 	timeout = HWI2C_WAIT_TIMEOUT;
66 	while ((FIELD_GET(PEEK32(I2C_STATUS), I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
67 	       (timeout != 0))
68 		timeout--;
69 
70 	if (timeout == 0)
71 		return (-1);
72 
73 	return 0;
74 }
75 
76 /*
77  *  This function writes data to the i2c slave device registers.
78  *
79  *  Parameters:
80  *      addr            - i2c Slave device address
81  *      length          - Total number of bytes to be written to the device
82  *      buf             - The buffer that contains the data to be written to the
83  *                     i2c device.
84  *
85  *  Return Value:
86  *      Total number of bytes those are actually written.
87  */
88 static unsigned int hw_i2c_write_data(
89 	unsigned char addr,
90 	unsigned int length,
91 	unsigned char *buf
92 )
93 {
94 	unsigned char count, i;
95 	unsigned int total_bytes = 0;
96 
97 	/* Set the Device Address */
98 	POKE32(I2C_SLAVE_ADDRESS, addr & ~0x01);
99 
100 	/* Write data.
101 	 * Note:
102 	 *      Only 16 byte can be accessed per i2c start instruction.
103 	 */
104 	do {
105 		/* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */
106 		POKE32(I2C_RESET, 0);
107 
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);
114 
115 		/* Move the data to the I2C data register */
116 		for (i = 0; i <= count; i++)
117 			POKE32(I2C_DATA0 + i, *buf++);
118 
119 		/* Start the I2C */
120 		POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
121 
122 		/* Wait until the transfer is completed. */
123 		if (hw_i2c_wait_tx_done() != 0)
124 			break;
125 
126 		/* Substract length */
127 		length -= (count + 1);
128 
129 		/* Total byte written */
130 		total_bytes += (count + 1);
131 
132 	} while (length > 0);
133 
134 	return total_bytes;
135 }
136 
137 /*
138  *  This function reads data from the slave device and stores them
139  *  in the given buffer
140  *
141  *  Parameters:
142  *      addr            - i2c Slave device address
143  *      length          - Total number of bytes to be read
144  *      buf             - Pointer to a buffer to be filled with the data read
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  */
151 static unsigned int hw_i2c_read_data(
152 	unsigned char addr,
153 	unsigned int length,
154 	unsigned char *buf
155 )
156 {
157 	unsigned char count, i;
158 	unsigned int total_bytes = 0;
159 
160 	/* Set the Device Address */
161 	POKE32(I2C_SLAVE_ADDRESS, addr | 0x01);
162 
163 	/* Read data and save them to the buffer.
164 	 * Note:
165 	 *      Only 16 byte can be accessed per i2c start instruction.
166 	 */
167 	do {
168 		/* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */
169 		POKE32(I2C_RESET, 0);
170 
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);
177 
178 		/* Start the I2C */
179 		POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
180 
181 		/* Wait until transaction done. */
182 		if (hw_i2c_wait_tx_done() != 0)
183 			break;
184 
185 		/* Save the data to the given buffer */
186 		for (i = 0; i <= count; i++)
187 			*buf++ = PEEK32(I2C_DATA0 + i);
188 
189 		/* Substract length by 16 */
190 		length -= (count + 1);
191 
192 		/* Number of bytes read. */
193 		total_bytes += (count + 1);
194 
195 	} while (length > 0);
196 
197 	return total_bytes;
198 }
199 
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  */
211 unsigned char sm750_hw_i2c_read_reg(
212 	unsigned char addr,
213 	unsigned char reg
214 )
215 {
216 	unsigned char value = (0xFF);
217 
218 	if (hw_i2c_write_data(addr, 1, &reg) == 1)
219 		hw_i2c_read_data(addr, 1, &value);
220 
221 	return value;
222 }
223 
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  */
237 int sm750_hw_i2c_write_reg(
238 	unsigned char addr,
239 	unsigned char reg,
240 	unsigned char data
241 )
242 {
243 	unsigned char value[2];
244 
245 	value[0] = reg;
246 	value[1] = data;
247 	if (hw_i2c_write_data(addr, 2, value) == 2)
248 		return 0;
249 
250 	return (-1);
251 }
252 
253 #endif
254