This source file includes following definitions.
- sm750_hw_i2c_init
- sm750_hw_i2c_close
- hw_i2c_wait_tx_done
- hw_i2c_write_data
- hw_i2c_read_data
- sm750_hw_i2c_read_reg
- sm750_hw_i2c_write_reg
1
2 #define USE_HW_I2C
3 #ifdef USE_HW_I2C
4 #include "ddk750_chip.h"
5 #include "ddk750_reg.h"
6 #include "ddk750_hwi2c.h"
7 #include "ddk750_power.h"
8
9 #define MAX_HWI2C_FIFO 16
10 #define HWI2C_WAIT_TIMEOUT 0xF0000
11
12 int sm750_hw_i2c_init(unsigned char bus_speed_mode)
13 {
14 unsigned int value;
15
16
17 value = peek32(GPIO_MUX);
18
19 value |= (GPIO_MUX_30 | GPIO_MUX_31);
20 poke32(GPIO_MUX, value);
21
22
23
24
25
26 sm750_enable_i2c(1);
27
28
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;
33 poke32(I2C_CTRL, value);
34
35 return 0;
36 }
37
38 void sm750_hw_i2c_close(void)
39 {
40 unsigned int value;
41
42
43 value = peek32(I2C_CTRL) & ~I2C_CTRL_EN;
44 poke32(I2C_CTRL, value);
45
46
47 sm750_enable_i2c(0);
48
49
50 value = peek32(GPIO_MUX);
51 value &= ~GPIO_MUX_30;
52 value &= ~GPIO_MUX_31;
53 poke32(GPIO_MUX, value);
54 }
55
56 static long hw_i2c_wait_tx_done(void)
57 {
58 unsigned int timeout;
59
60
61 timeout = HWI2C_WAIT_TIMEOUT;
62 while (!(peek32(I2C_STATUS) & I2C_STATUS_TX) && (timeout != 0))
63 timeout--;
64
65 if (timeout == 0)
66 return -1;
67
68 return 0;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83 static unsigned int hw_i2c_write_data(unsigned char addr,
84 unsigned int length,
85 unsigned char *buf)
86 {
87 unsigned char count, i;
88 unsigned int total_bytes = 0;
89
90
91 poke32(I2C_SLAVE_ADDRESS, addr & ~0x01);
92
93
94
95
96
97
98 do {
99
100
101
102
103 poke32(I2C_RESET, 0);
104
105
106 if (length < MAX_HWI2C_FIFO)
107 count = length - 1;
108 else
109 count = MAX_HWI2C_FIFO - 1;
110 poke32(I2C_BYTE_COUNT, count);
111
112
113 for (i = 0; i <= count; i++)
114 poke32(I2C_DATA0 + i, *buf++);
115
116
117 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
118
119
120 if (hw_i2c_wait_tx_done() != 0)
121 break;
122
123
124 length -= (count + 1);
125
126
127 total_bytes += (count + 1);
128
129 } while (length > 0);
130
131 return total_bytes;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 static unsigned int hw_i2c_read_data(unsigned char addr,
149 unsigned int length,
150 unsigned char *buf)
151 {
152 unsigned char count, i;
153 unsigned int total_bytes = 0;
154
155
156 poke32(I2C_SLAVE_ADDRESS, addr | 0x01);
157
158
159
160
161
162
163 do {
164
165
166
167
168 poke32(I2C_RESET, 0);
169
170
171 if (length <= MAX_HWI2C_FIFO)
172 count = length - 1;
173 else
174 count = MAX_HWI2C_FIFO - 1;
175 poke32(I2C_BYTE_COUNT, count);
176
177
178 poke32(I2C_CTRL, peek32(I2C_CTRL) | I2C_CTRL_CTRL);
179
180
181 if (hw_i2c_wait_tx_done() != 0)
182 break;
183
184
185 for (i = 0; i <= count; i++)
186 *buf++ = peek32(I2C_DATA0 + i);
187
188
189 length -= (count + 1);
190
191
192 total_bytes += (count + 1);
193
194 } while (length > 0);
195
196 return total_bytes;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 unsigned char sm750_hw_i2c_read_reg(unsigned char addr, unsigned char reg)
211 {
212 unsigned char value = 0xFF;
213
214 if (hw_i2c_write_data(addr, 1, ®) == 1)
215 hw_i2c_read_data(addr, 1, &value);
216
217 return value;
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 int sm750_hw_i2c_write_reg(unsigned char addr,
234 unsigned char reg,
235 unsigned char data)
236 {
237 unsigned char value[2];
238
239 value[0] = reg;
240 value[1] = data;
241 if (hw_i2c_write_data(addr, 2, value) == 2)
242 return 0;
243
244 return -1;
245 }
246
247 #endif