1
2
3
4
5
6
7
8
9
10
11#include <common.h>
12#include <i2c.h>
13#include <asm/errno.h>
14#include <asm/io.h>
15
16
17
18
19
20
21#if defined(CONFIG_ORION5X)
22#include <asm/arch/orion5x.h>
23#elif defined(CONFIG_KIRKWOOD)
24#include <asm/arch/kirkwood.h>
25#else
26#error Driver mvtwsi not supported by SoC or board
27#endif
28
29
30
31
32
33struct mvtwsi_registers {
34 u32 slave_address;
35 u32 data;
36 u32 control;
37 union {
38 u32 status;
39 u32 baudrate;
40 };
41 u32 xtnd_slave_addr;
42 u32 reserved[2];
43 u32 soft_reset;
44};
45
46
47
48
49
50#define MVTWSI_CONTROL_ACK 0x00000004
51#define MVTWSI_CONTROL_IFLG 0x00000008
52#define MVTWSI_CONTROL_STOP 0x00000010
53#define MVTWSI_CONTROL_START 0x00000020
54#define MVTWSI_CONTROL_TWSIEN 0x00000040
55#define MVTWSI_CONTROL_INTEN 0x00000080
56
57
58
59
60
61
62
63
64
65#define MVTWSI_STATUS_START 0x08
66#define MVTWSI_STATUS_REPEATED_START 0x10
67#define MVTWSI_STATUS_ADDR_W_ACK 0x18
68#define MVTWSI_STATUS_DATA_W_ACK 0x28
69#define MVTWSI_STATUS_ADDR_R_ACK 0x40
70#define MVTWSI_STATUS_ADDR_R_NAK 0x48
71#define MVTWSI_STATUS_DATA_R_ACK 0x50
72#define MVTWSI_STATUS_DATA_R_NAK 0x58
73#define MVTWSI_STATUS_IDLE 0xF8
74
75
76
77
78
79static struct mvtwsi_registers *twsi =
80 (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE;
81
82
83
84
85
86
87
88
89
90
91
92#define MVTWSI_ERROR_WRONG_STATUS 0x01
93#define MVTWSI_ERROR_TIMEOUT 0x02
94
95#define MVTWSI_ERROR(ec, lc, ls, es) (((ec << 24) & 0xFF000000) | \
96 ((lc << 16) & 0x00FF0000) | ((ls<<8) & 0x0000FF00) | (es & 0xFF))
97
98
99
100
101
102static int twsi_wait(int expected_status)
103{
104 int control, status;
105 int timeout = 1000;
106
107 do {
108 control = readl(&twsi->control);
109 if (control & MVTWSI_CONTROL_IFLG) {
110 status = readl(&twsi->status);
111 if (status == expected_status)
112 return 0;
113 else
114 return MVTWSI_ERROR(
115 MVTWSI_ERROR_WRONG_STATUS,
116 control, status, expected_status);
117 }
118 udelay(10);
119 } while (timeout--);
120 status = readl(&twsi->status);
121 return MVTWSI_ERROR(
122 MVTWSI_ERROR_TIMEOUT, control, status, expected_status);
123}
124
125
126
127
128
129
130
131
132static u8 twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
133
134
135
136
137
138static int twsi_start(int expected_status)
139{
140
141 twsi_control_flags |= MVTWSI_CONTROL_TWSIEN;
142
143 writel(twsi_control_flags | MVTWSI_CONTROL_START, &twsi->control);
144
145 return twsi_wait(expected_status);
146}
147
148
149
150
151static int twsi_send(u8 byte, int expected_status)
152{
153
154 writel(byte, &twsi->data);
155
156 writel(twsi_control_flags, &twsi->control);
157
158 return twsi_wait(expected_status);
159}
160
161
162
163
164
165static int twsi_recv(u8 *byte)
166{
167 int expected_status, status;
168
169
170 if (twsi_control_flags & MVTWSI_CONTROL_ACK)
171 expected_status = MVTWSI_STATUS_DATA_R_ACK;
172 else
173 expected_status = MVTWSI_STATUS_DATA_R_NAK;
174
175 writel(twsi_control_flags, &twsi->control);
176
177 status = twsi_wait(expected_status);
178
179 if (status == 0)
180 *byte = readl(&twsi->data);
181
182 return status;
183}
184
185
186
187
188
189static int twsi_stop(int status)
190{
191 int control, stop_status;
192 int timeout = 1000;
193
194
195 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
196 writel(control, &twsi->control);
197
198 do {
199 stop_status = readl(&twsi->status);
200 if (stop_status == MVTWSI_STATUS_IDLE)
201 break;
202 udelay(10);
203 } while (timeout--);
204 control = readl(&twsi->control);
205 if (stop_status != MVTWSI_STATUS_IDLE)
206 if (status == 0)
207 status = MVTWSI_ERROR(
208 MVTWSI_ERROR_TIMEOUT,
209 control, status, MVTWSI_STATUS_IDLE);
210 return status;
211}
212
213
214
215
216
217
218#define TWSI_FREQUENCY(m, n) \
219 (CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n)))
220
221
222
223
224
225
226
227static void twsi_reset(u8 baud_rate, u8 slave_address)
228{
229
230 twsi_control_flags = MVTWSI_CONTROL_TWSIEN;
231
232 writel(0, &twsi->soft_reset);
233
234 udelay(20000);
235
236 writel(baud_rate, &twsi->baudrate);
237
238 writel(slave_address, &twsi->slave_address);
239 writel(0, &twsi->xtnd_slave_addr);
240
241 (void) twsi_stop(0);
242}
243
244
245
246
247
248void i2c_init(int requested_speed, int slaveadd)
249{
250 int tmp_speed, highest_speed, n, m;
251 int baud = 0x44;
252
253
254 highest_speed = 0;
255
256 for (n = 0; n < 8; n++) {
257 for (m = 0; m < 16; m++) {
258 tmp_speed = TWSI_FREQUENCY(m, n);
259 if ((tmp_speed <= requested_speed)
260 && (tmp_speed > highest_speed)) {
261 highest_speed = tmp_speed;
262 baud = (m << 3) | n;
263 }
264 }
265 }
266
267 twsi_reset(baud, slaveadd);
268}
269
270
271
272
273
274
275static int i2c_begin(int expected_start_status, u8 addr)
276{
277 int status, expected_addr_status;
278
279
280 if (addr & 1)
281 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
282 else
283 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
284
285 status = twsi_start(expected_start_status);
286
287 if (status == 0)
288 status = twsi_send(addr, expected_addr_status);
289
290 return status;
291}
292
293
294
295
296
297int i2c_probe(uchar chip)
298{
299 u8 dummy_byte;
300 int status;
301
302
303 status = i2c_begin(MVTWSI_STATUS_START, (chip << 1) | 1);
304
305 if (status == 0)
306 status = twsi_recv(&dummy_byte);
307
308 twsi_stop(0);
309
310 return status;
311}
312
313
314
315
316
317
318
319
320
321
322
323int i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
324{
325 int status;
326
327
328 status = i2c_begin(MVTWSI_STATUS_START, (dev << 1));
329
330 while ((status == 0) && alen--)
331 status = twsi_send(addr >> (8*alen),
332 MVTWSI_STATUS_DATA_W_ACK);
333
334 if (status == 0)
335 status = i2c_begin(
336 MVTWSI_STATUS_REPEATED_START, (dev << 1) | 1);
337
338 if (length > 0)
339 twsi_control_flags |= MVTWSI_CONTROL_ACK;
340
341 while ((status == 0) && length--) {
342
343 if (length == 0)
344 twsi_control_flags &= ~MVTWSI_CONTROL_ACK;
345
346 status = twsi_recv(data++);
347 }
348
349 status = twsi_stop(status);
350
351 return status;
352}
353
354
355
356
357
358int i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
359{
360 int status;
361
362
363 status = i2c_begin(MVTWSI_STATUS_START, (dev << 1));
364
365 while ((status == 0) && alen--)
366 status = twsi_send(addr >> (8*alen),
367 MVTWSI_STATUS_DATA_W_ACK);
368
369 while ((status == 0) && (length-- > 0))
370 status = twsi_send(*(data++), MVTWSI_STATUS_DATA_W_ACK);
371
372 status = twsi_stop(status);
373
374 return status;
375}
376
377
378
379
380int i2c_set_bus_num(unsigned int bus)
381{
382 if (bus > 0) {
383 return -1;
384 }
385 return 0;
386}
387
388
389
390
391unsigned int i2c_get_bus_num(void)
392{
393 return 0;
394}
395