2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5 * Developed by Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34 * and Allwinner SoCs. Supports master operation only, and works in polling mode.
36 * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37 * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/resource.h>
50 #include <machine/_inttypes.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
57 #include <sys/mutex.h>
59 #include <dev/iicbus/iiconf.h>
60 #include <dev/iicbus/iicbus.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
64 #include <dev/iicbus/twsi/twsi.h>
66 #include "iicbus_if.h"
68 #define TWSI_CONTROL_ACK (1 << 2)
69 #define TWSI_CONTROL_IFLG (1 << 3)
70 #define TWSI_CONTROL_STOP (1 << 4)
71 #define TWSI_CONTROL_START (1 << 5)
72 #define TWSI_CONTROL_TWSIEN (1 << 6)
73 #define TWSI_CONTROL_INTEN (1 << 7)
75 #define TWSI_STATUS_START 0x08
76 #define TWSI_STATUS_RPTD_START 0x10
77 #define TWSI_STATUS_ADDR_W_ACK 0x18
78 #define TWSI_STATUS_DATA_WR_ACK 0x28
79 #define TWSI_STATUS_ADDR_R_ACK 0x40
80 #define TWSI_STATUS_DATA_RD_ACK 0x50
81 #define TWSI_STATUS_DATA_RD_NOACK 0x58
87 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
89 #define debugf(fmt, args...)
92 static struct resource_spec res_spec[] = {
93 { SYS_RES_MEMORY, 0, RF_ACTIVE },
97 static __inline uint32_t
98 TWSI_READ(struct twsi_softc *sc, bus_size_t off)
101 return (bus_read_4(sc->res[0], off));
105 TWSI_WRITE(struct twsi_softc *sc, bus_size_t off, uint32_t val)
108 bus_write_4(sc->res[0], off, val);
112 twsi_control_clear(struct twsi_softc *sc, uint32_t mask)
116 val = TWSI_READ(sc, sc->reg_control);
118 TWSI_WRITE(sc, sc->reg_control, val);
122 twsi_control_set(struct twsi_softc *sc, uint32_t mask)
126 val = TWSI_READ(sc, sc->reg_control);
128 TWSI_WRITE(sc, sc->reg_control, val);
132 twsi_clear_iflg(struct twsi_softc *sc)
136 twsi_control_clear(sc, TWSI_CONTROL_IFLG);
142 * timeout given in us
144 * 0 on successful mask change
145 * non-zero on timeout
148 twsi_poll_ctrl(struct twsi_softc *sc, int timeout, uint32_t mask)
152 while (!(TWSI_READ(sc, sc->reg_control) & mask)) {
162 * 'timeout' is given in us. Note also that timeout handling is not exact --
163 * twsi_locked_start() total wait can be more than 2 x timeout
164 * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
165 * or TWSI_STATUS_RPTD_START
168 twsi_locked_start(device_t dev, struct twsi_softc *sc, int32_t mask,
169 u_char slave, int timeout)
171 int read_access, iflg_set = 0;
174 mtx_assert(&sc->mutex, MA_OWNED);
176 if (mask == TWSI_STATUS_RPTD_START)
177 /* read IFLG to know if it should be cleared later; from NBSD */
178 iflg_set = TWSI_READ(sc, sc->reg_control) & TWSI_CONTROL_IFLG;
180 twsi_control_set(sc, TWSI_CONTROL_START);
182 if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
183 debugf("IFLG set, clearing\n");
188 * Without this delay we timeout checking IFLG if the timeout is 0.
189 * NBSD driver always waits here too.
193 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
194 debugf("timeout sending %sSTART condition\n",
195 mask == TWSI_STATUS_START ? "" : "repeated ");
196 return (IIC_ETIMEOUT);
199 status = TWSI_READ(sc, sc->reg_status);
200 if (status != mask) {
201 debugf("wrong status (%02x) after sending %sSTART condition\n",
202 status, mask == TWSI_STATUS_START ? "" : "repeated ");
203 return (IIC_ESTATUS);
206 TWSI_WRITE(sc, sc->reg_data, slave);
210 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
211 debugf("timeout sending slave address\n");
212 return (IIC_ETIMEOUT);
215 read_access = (slave & 0x1) ? 1 : 0;
216 status = TWSI_READ(sc, sc->reg_status);
217 if (status != (read_access ?
218 TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
219 debugf("no ACK (status: %02x) after sending slave address\n",
228 * Only slave mode supported, disregard [old]addr
231 twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
233 struct twsi_softc *sc;
236 sc = device_get_softc(dev);
241 param = sc->baud_rate[speed].param;
246 param = sc->baud_rate[IIC_FAST].param;
250 mtx_lock(&sc->mutex);
251 TWSI_WRITE(sc, sc->reg_soft_reset, 0x0);
253 TWSI_WRITE(sc, sc->reg_baud_rate, param);
254 TWSI_WRITE(sc, sc->reg_control, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
256 mtx_unlock(&sc->mutex);
262 twsi_stop(device_t dev)
264 struct twsi_softc *sc;
266 sc = device_get_softc(dev);
268 mtx_lock(&sc->mutex);
269 twsi_control_set(sc, TWSI_CONTROL_STOP);
272 mtx_unlock(&sc->mutex);
278 * timeout is given in us
281 twsi_repeated_start(device_t dev, u_char slave, int timeout)
283 struct twsi_softc *sc;
286 sc = device_get_softc(dev);
288 mtx_lock(&sc->mutex);
289 rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
291 mtx_unlock(&sc->mutex);
301 * timeout is given in us
304 twsi_start(device_t dev, u_char slave, int timeout)
306 struct twsi_softc *sc;
309 sc = device_get_softc(dev);
311 mtx_lock(&sc->mutex);
312 rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
313 mtx_unlock(&sc->mutex);
323 twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
325 struct twsi_softc *sc;
329 sc = device_get_softc(dev);
331 mtx_lock(&sc->mutex);
333 while (*read < len) {
335 * Check if we are reading last byte of the last buffer,
336 * do not send ACK then, per I2C specs
338 last_byte = ((*read == len - 1) && last) ? 1 : 0;
340 twsi_control_clear(sc, TWSI_CONTROL_ACK);
342 twsi_control_set(sc, TWSI_CONTROL_ACK);
347 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
348 debugf("timeout reading data\n");
353 status = TWSI_READ(sc, sc->reg_status);
354 if (status != (last_byte ?
355 TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
356 debugf("wrong status (%02x) while reading\n", status);
361 *buf++ = TWSI_READ(sc, sc->reg_data);
366 mtx_unlock(&sc->mutex);
371 twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
373 struct twsi_softc *sc;
377 sc = device_get_softc(dev);
379 mtx_lock(&sc->mutex);
381 while (*sent < len) {
382 TWSI_WRITE(sc, sc->reg_data, *buf++);
385 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
386 debugf("timeout writing data\n");
391 status = TWSI_READ(sc, sc->reg_status);
392 if (status != TWSI_STATUS_DATA_WR_ACK) {
393 debugf("wrong status (%02x) while writing\n", status);
401 mtx_unlock(&sc->mutex);
406 twsi_attach(device_t dev)
408 struct twsi_softc *sc;
410 sc = device_get_softc(dev);
413 mtx_init(&sc->mutex, device_get_nameunit(dev), "twsi", MTX_DEF);
415 if (bus_alloc_resources(dev, res_spec, sc->res)) {
416 device_printf(dev, "could not allocate resources\n");
421 /* Attach the iicbus. */
422 if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL) {
423 device_printf(dev, "could not allocate iicbus instance\n");
427 bus_generic_attach(dev);
433 twsi_detach(device_t dev)
435 struct twsi_softc *sc;
438 sc = device_get_softc(dev);
440 if ((rv = bus_generic_detach(dev)) != 0)
443 if (sc->iicbus != NULL)
444 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
447 bus_release_resources(dev, res_spec, sc->res);
449 mtx_destroy(&sc->mutex);
453 static device_method_t twsi_methods[] = {
454 /* device interface */
455 DEVMETHOD(device_detach, twsi_detach),
458 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
459 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
460 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
461 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
462 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
463 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
464 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
465 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
466 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
468 /* iicbus interface */
469 DEVMETHOD(iicbus_callback, iicbus_null_callback),
470 DEVMETHOD(iicbus_repeated_start, twsi_repeated_start),
471 DEVMETHOD(iicbus_start, twsi_start),
472 DEVMETHOD(iicbus_stop, twsi_stop),
473 DEVMETHOD(iicbus_write, twsi_write),
474 DEVMETHOD(iicbus_read, twsi_read),
475 DEVMETHOD(iicbus_reset, twsi_reset),
476 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
480 DEFINE_CLASS_0(twsi, twsi_driver, twsi_methods,
481 sizeof(struct twsi_softc));