FreeBSD/Linux Kernel Cross Reference
sys/arm/mv/twsi.c
1 /*-
2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3 * All rights reserved.
4 *
5 * Developed by Semihalf.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of MARVELL nor the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34 * SoCs. Supports master operation only, and works in polling mode.
35 *
36 * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37 * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38 */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
49
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52
53 #include <sys/rman.h>
54
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57
58 #include <dev/iicbus/iiconf.h>
59 #include <dev/iicbus/iicbus.h>
60 #include "iicbus_if.h"
61
62 #define MV_TWSI_NAME "twsi"
63
64 #define TWSI_SLAVE_ADDR 0x00
65 #define TWSI_EXT_SLAVE_ADDR 0x10
66 #define TWSI_DATA 0x04
67
68 #define TWSI_CONTROL 0x08
69 #define TWSI_CONTROL_ACK (1 << 2)
70 #define TWSI_CONTROL_IFLG (1 << 3)
71 #define TWSI_CONTROL_STOP (1 << 4)
72 #define TWSI_CONTROL_START (1 << 5)
73 #define TWSI_CONTROL_TWSIEN (1 << 6)
74 #define TWSI_CONTROL_INTEN (1 << 7)
75
76 #define TWSI_STATUS 0x0c
77 #define TWSI_STATUS_START 0x08
78 #define TWSI_STATUS_RPTD_START 0x10
79 #define TWSI_STATUS_ADDR_W_ACK 0x18
80 #define TWSI_STATUS_DATA_WR_ACK 0x28
81 #define TWSI_STATUS_ADDR_R_ACK 0x40
82 #define TWSI_STATUS_DATA_RD_ACK 0x50
83 #define TWSI_STATUS_DATA_RD_NOACK 0x58
84
85 #define TWSI_BAUD_RATE 0x0c
86 #define TWSI_BAUD_RATE_94DOT3 0x53 /* N=3, M=10 */
87 #define TWSI_BAUD_RATE_74DOT1 0x6b /* N=3, M=13 */
88 #define TWSI_BAUD_RATE_51DOT8 0x4c /* N=4, M=9 */
89 #define TWSI_BAUD_RATE_99DOT7 0x66 /* N=6, M=12 */
90
91 #define TWSI_SOFT_RESET 0x1c
92
93 #define TWSI_DEBUG
94 #undef TWSI_DEBUG
95
96 #ifdef TWSI_DEBUG
97 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
98 #else
99 #define debugf(fmt, args...)
100 #endif
101
102 struct mv_twsi_softc {
103 device_t dev;
104 struct resource *res[1]; /* SYS_RES_MEMORY */
105 struct mtx mutex;
106 device_t iicbus;
107 };
108
109 static int mv_twsi_probe(device_t);
110 static int mv_twsi_attach(device_t);
111 static int mv_twsi_detach(device_t);
112
113 static int mv_twsi_reset(device_t dev, u_char speed, u_char addr,
114 u_char *oldaddr);
115 static int mv_twsi_repeated_start(device_t dev, u_char slave, int timeout);
116 static int mv_twsi_start(device_t dev, u_char slave, int timeout);
117 static int mv_twsi_stop(device_t dev);
118 static int mv_twsi_read(device_t dev, char *buf, int len, int *read, int last,
119 int delay);
120 static int mv_twsi_write(device_t dev, const char *buf, int len, int *sent,
121 int timeout);
122
123 static struct resource_spec res_spec[] = {
124 { SYS_RES_MEMORY, 0, RF_ACTIVE },
125 { -1, 0 }
126 };
127
128 static device_method_t mv_twsi_methods[] = {
129 /* device interface */
130 DEVMETHOD(device_probe, mv_twsi_probe),
131 DEVMETHOD(device_attach, mv_twsi_attach),
132 DEVMETHOD(device_detach, mv_twsi_detach),
133
134 /* iicbus interface */
135 DEVMETHOD(iicbus_callback, iicbus_null_callback),
136 DEVMETHOD(iicbus_repeated_start, mv_twsi_repeated_start),
137 DEVMETHOD(iicbus_start, mv_twsi_start),
138 DEVMETHOD(iicbus_stop, mv_twsi_stop),
139 DEVMETHOD(iicbus_write, mv_twsi_write),
140 DEVMETHOD(iicbus_read, mv_twsi_read),
141 DEVMETHOD(iicbus_reset, mv_twsi_reset),
142 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
143 { 0, 0 }
144 };
145
146 static devclass_t mv_twsi_devclass;
147
148 static driver_t mv_twsi_driver = {
149 MV_TWSI_NAME,
150 mv_twsi_methods,
151 sizeof(struct mv_twsi_softc),
152 };
153
154 DRIVER_MODULE(twsi, mbus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
155 DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
156 MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
157
158 static __inline uint32_t
159 TWSI_READ(struct mv_twsi_softc *sc, bus_size_t off)
160 {
161
162 return (bus_read_4(sc->res[0], off));
163 }
164
165 static __inline void
166 TWSI_WRITE(struct mv_twsi_softc *sc, bus_size_t off, uint32_t val)
167 {
168
169 bus_write_4(sc->res[0], off, val);
170 }
171
172 static __inline void
173 twsi_control_clear(struct mv_twsi_softc *sc, uint32_t mask)
174 {
175 uint32_t val;
176
177 val = TWSI_READ(sc, TWSI_CONTROL);
178 val &= ~mask;
179 TWSI_WRITE(sc, TWSI_CONTROL, val);
180 }
181
182 static __inline void
183 twsi_control_set(struct mv_twsi_softc *sc, uint32_t mask)
184 {
185 uint32_t val;
186
187 val = TWSI_READ(sc, TWSI_CONTROL);
188 val |= mask;
189 TWSI_WRITE(sc, TWSI_CONTROL, val);
190 }
191
192 static __inline void
193 twsi_clear_iflg(struct mv_twsi_softc *sc)
194 {
195
196 DELAY(1000);
197 twsi_control_clear(sc, TWSI_CONTROL_IFLG);
198 DELAY(1000);
199 }
200
201
202 /*
203 * timeout given in us
204 * returns
205 * 0 on sucessfull mask change
206 * non-zero on timeout
207 */
208 static int
209 twsi_poll_ctrl(struct mv_twsi_softc *sc, int timeout, uint32_t mask)
210 {
211
212 timeout /= 10;
213 while (!(TWSI_READ(sc, TWSI_CONTROL) & mask)) {
214 DELAY(10);
215 if (--timeout < 0)
216 return (timeout);
217 }
218 return (0);
219 }
220
221
222 /*
223 * 'timeout' is given in us. Note also that timeout handling is not exact --
224 * twsi_locked_start() total wait can be more than 2 x timeout
225 * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
226 * or TWSI_STATUS_RPTD_START
227 */
228 static int
229 twsi_locked_start(device_t dev, struct mv_twsi_softc *sc, int32_t mask,
230 u_char slave, int timeout)
231 {
232 int read_access, iflg_set = 0;
233 uint32_t status;
234
235 mtx_assert(&sc->mutex, MA_OWNED);
236
237 if (mask == TWSI_STATUS_RPTD_START)
238 /* read IFLG to know if it should be cleared later; from NBSD */
239 iflg_set = TWSI_READ(sc, TWSI_CONTROL) & TWSI_CONTROL_IFLG;
240
241 twsi_control_set(sc, TWSI_CONTROL_START);
242
243 if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
244 debugf("IFLG set, clearing\n");
245 twsi_clear_iflg(sc);
246 }
247
248 /*
249 * Without this delay we timeout checking IFLG if the timeout is 0.
250 * NBSD driver always waits here too.
251 */
252 DELAY(1000);
253
254 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
255 debugf("timeout sending %sSTART condition\n",
256 mask == TWSI_STATUS_START ? "" : "repeated ");
257 return (IIC_ETIMEOUT);
258 }
259
260 status = TWSI_READ(sc, TWSI_STATUS);
261 if (status != mask) {
262 debugf("wrong status (%02x) after sending %sSTART condition\n",
263 status, mask == TWSI_STATUS_START ? "" : "repeated ");
264 return (IIC_ESTATUS);
265 }
266
267 TWSI_WRITE(sc, TWSI_DATA, slave);
268 DELAY(1000);
269 twsi_clear_iflg(sc);
270
271 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
272 debugf("timeout sending slave address\n");
273 return (IIC_ETIMEOUT);
274 }
275
276 read_access = (slave & 0x1) ? 1 : 0;
277 status = TWSI_READ(sc, TWSI_STATUS);
278 if (status != (read_access ?
279 TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
280 debugf("no ACK (status: %02x) after sending slave address\n",
281 status);
282 return (IIC_ENOACK);
283 }
284
285 return (IIC_NOERR);
286 }
287
288 static int
289 mv_twsi_probe(device_t dev)
290 {
291
292 device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
293 return (BUS_PROBE_DEFAULT);
294 }
295
296 static int
297 mv_twsi_attach(device_t dev)
298 {
299 struct mv_twsi_softc *sc;
300
301 sc = device_get_softc(dev);
302 sc->dev = dev;
303
304 mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
305
306 /* Allocate IO resources */
307 if (bus_alloc_resources(dev, res_spec, sc->res)) {
308 device_printf(dev, "could not allocate resources\n");
309 mv_twsi_detach(dev);
310 return (ENXIO);
311 }
312
313 sc->iicbus = device_add_child(dev, "iicbus", -1);
314 if (sc->iicbus == NULL) {
315 device_printf(dev, "could not add iicbus child\n");
316 mv_twsi_detach(dev);
317 return (ENXIO);
318 }
319
320 bus_generic_attach(dev);
321 return (0);
322 }
323
324 static int
325 mv_twsi_detach(device_t dev)
326 {
327 struct mv_twsi_softc *sc;
328 int rv;
329
330 sc = device_get_softc(dev);
331
332 if ((rv = bus_generic_detach(dev)) != 0)
333 return (rv);
334
335 if (sc->iicbus != NULL)
336 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
337 return (rv);
338
339 bus_release_resources(dev, res_spec, sc->res);
340
341 mtx_destroy(&sc->mutex);
342 return (0);
343 }
344
345 /*
346 * Only slave mode supported, disregard [old]addr
347 */
348 static int
349 mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
350 {
351 struct mv_twsi_softc *sc;
352 uint32_t baud_rate;
353
354 sc = device_get_softc(dev);
355
356 switch (speed) {
357 case IIC_SLOW:
358 baud_rate = TWSI_BAUD_RATE_51DOT8;
359 break;
360 case IIC_FAST:
361 baud_rate = TWSI_BAUD_RATE_51DOT8;
362 break;
363 case IIC_UNKNOWN:
364 case IIC_FASTEST:
365 default:
366 baud_rate = TWSI_BAUD_RATE_99DOT7;
367 break;
368 }
369
370 mtx_lock(&sc->mutex);
371 TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
372 DELAY(2000);
373 TWSI_WRITE(sc, TWSI_BAUD_RATE, baud_rate);
374 TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
375 DELAY(1000);
376 mtx_unlock(&sc->mutex);
377
378 return (0);
379 }
380
381 /*
382 * timeout is given in us
383 */
384 static int
385 mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
386 {
387 struct mv_twsi_softc *sc;
388 int rv;
389
390 sc = device_get_softc(dev);
391
392 mtx_lock(&sc->mutex);
393 rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
394 timeout);
395 mtx_unlock(&sc->mutex);
396
397 if (rv) {
398 mv_twsi_stop(dev);
399 return (rv);
400 } else
401 return (IIC_NOERR);
402 }
403
404 /*
405 * timeout is given in us
406 */
407 static int
408 mv_twsi_start(device_t dev, u_char slave, int timeout)
409 {
410 struct mv_twsi_softc *sc;
411 int rv;
412
413 sc = device_get_softc(dev);
414
415 mtx_lock(&sc->mutex);
416 rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
417 mtx_unlock(&sc->mutex);
418
419 if (rv) {
420 mv_twsi_stop(dev);
421 return (rv);
422 } else
423 return (IIC_NOERR);
424 }
425
426 static int
427 mv_twsi_stop(device_t dev)
428 {
429 struct mv_twsi_softc *sc;
430
431 sc = device_get_softc(dev);
432
433 mtx_lock(&sc->mutex);
434 twsi_control_set(sc, TWSI_CONTROL_STOP);
435 DELAY(1000);
436 twsi_clear_iflg(sc);
437 mtx_unlock(&sc->mutex);
438
439 return (IIC_NOERR);
440 }
441
442 static int
443 mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
444 {
445 struct mv_twsi_softc *sc;
446 uint32_t status;
447 int last_byte, rv;
448
449 sc = device_get_softc(dev);
450
451 mtx_lock(&sc->mutex);
452 *read = 0;
453 while (*read < len) {
454 /*
455 * Check if we are reading last byte of the last buffer,
456 * do not send ACK then, per I2C specs
457 */
458 last_byte = ((*read == len - 1) && last) ? 1 : 0;
459 if (last_byte)
460 twsi_control_clear(sc, TWSI_CONTROL_ACK);
461 else
462 twsi_control_set(sc, TWSI_CONTROL_ACK);
463
464 DELAY (1000);
465 twsi_clear_iflg(sc);
466
467 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
468 debugf("timeout reading data\n");
469 rv = IIC_ETIMEOUT;
470 goto out;
471 }
472
473 status = TWSI_READ(sc, TWSI_STATUS);
474 if (status != (last_byte ?
475 TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
476 debugf("wrong status (%02x) while reading\n", status);
477 rv = IIC_ESTATUS;
478 goto out;
479 }
480
481 *buf++ = TWSI_READ(sc, TWSI_DATA);
482 (*read)++;
483 }
484 rv = IIC_NOERR;
485 out:
486 mtx_unlock(&sc->mutex);
487 return (rv);
488 }
489
490 static int
491 mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
492 {
493 struct mv_twsi_softc *sc;
494 uint32_t status;
495 int rv;
496
497 sc = device_get_softc(dev);
498
499 mtx_lock(&sc->mutex);
500 *sent = 0;
501 while (*sent < len) {
502 TWSI_WRITE(sc, TWSI_DATA, *buf++);
503
504 twsi_clear_iflg(sc);
505 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
506 debugf("timeout writing data\n");
507 rv = IIC_ETIMEOUT;
508 goto out;
509 }
510
511 status = TWSI_READ(sc, TWSI_STATUS);
512 if (status != TWSI_STATUS_DATA_WR_ACK) {
513 debugf("wrong status (%02x) while writing\n", status);
514 rv = IIC_ESTATUS;
515 goto out;
516 }
517 (*sent)++;
518 }
519 rv = IIC_NOERR;
520 out:
521 mtx_unlock(&sc->mutex);
522 return (rv);
523 }
Cache object: 289ca685ea974b8cbc26e19ad555e026
|