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 * 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/bus.h>
51 #include <machine/resource.h>
56 #include <sys/mutex.h>
58 #include <dev/iicbus/iiconf.h>
59 #include <dev/iicbus/iicbus.h>
60 #include "iicbus_if.h"
62 #define MV_TWSI_NAME "twsi"
64 #define TWSI_SLAVE_ADDR 0x00
65 #define TWSI_EXT_SLAVE_ADDR 0x10
66 #define TWSI_DATA 0x04
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)
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
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 */
91 #define TWSI_SOFT_RESET 0x1c
97 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
99 #define debugf(fmt, args...)
102 struct mv_twsi_softc {
104 struct resource *res[1]; /* SYS_RES_MEMORY */
109 static int mv_twsi_probe(device_t);
110 static int mv_twsi_attach(device_t);
111 static int mv_twsi_detach(device_t);
113 static int mv_twsi_reset(device_t dev, u_char speed, u_char addr,
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,
120 static int mv_twsi_write(device_t dev, const char *buf, int len, int *sent,
123 static struct resource_spec res_spec[] = {
124 { SYS_RES_MEMORY, 0, RF_ACTIVE },
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),
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),
146 static devclass_t mv_twsi_devclass;
148 static driver_t mv_twsi_driver = {
151 sizeof(struct mv_twsi_softc),
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);
158 static __inline uint32_t
159 TWSI_READ(struct mv_twsi_softc *sc, bus_size_t off)
162 return (bus_read_4(sc->res[0], off));
166 TWSI_WRITE(struct mv_twsi_softc *sc, bus_size_t off, uint32_t val)
169 bus_write_4(sc->res[0], off, val);
173 twsi_control_clear(struct mv_twsi_softc *sc, uint32_t mask)
177 val = TWSI_READ(sc, TWSI_CONTROL);
179 TWSI_WRITE(sc, TWSI_CONTROL, val);
183 twsi_control_set(struct mv_twsi_softc *sc, uint32_t mask)
187 val = TWSI_READ(sc, TWSI_CONTROL);
189 TWSI_WRITE(sc, TWSI_CONTROL, val);
193 twsi_clear_iflg(struct mv_twsi_softc *sc)
197 twsi_control_clear(sc, TWSI_CONTROL_IFLG);
203 * timeout given in us
205 * 0 on sucessfull mask change
206 * non-zero on timeout
209 twsi_poll_ctrl(struct mv_twsi_softc *sc, int timeout, uint32_t mask)
213 while (!(TWSI_READ(sc, TWSI_CONTROL) & mask)) {
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
229 twsi_locked_start(device_t dev, struct mv_twsi_softc *sc, int32_t mask,
230 u_char slave, int timeout)
232 int read_access, iflg_set = 0;
235 mtx_assert(&sc->mutex, MA_OWNED);
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;
241 twsi_control_set(sc, TWSI_CONTROL_START);
243 if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
244 debugf("IFLG set, clearing\n");
249 * Without this delay we timeout checking IFLG if the timeout is 0.
250 * NBSD driver always waits here too.
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);
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);
267 TWSI_WRITE(sc, TWSI_DATA, slave);
271 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
272 debugf("timeout sending slave address\n");
273 return (IIC_ETIMEOUT);
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",
289 mv_twsi_probe(device_t dev)
292 device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
293 return (BUS_PROBE_DEFAULT);
297 mv_twsi_attach(device_t dev)
299 struct mv_twsi_softc *sc;
301 sc = device_get_softc(dev);
304 mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
306 /* Allocate IO resources */
307 if (bus_alloc_resources(dev, res_spec, sc->res)) {
308 device_printf(dev, "could not allocate resources\n");
313 sc->iicbus = device_add_child(dev, "iicbus", -1);
314 if (sc->iicbus == NULL) {
315 device_printf(dev, "could not add iicbus child\n");
320 bus_generic_attach(dev);
325 mv_twsi_detach(device_t dev)
327 struct mv_twsi_softc *sc;
330 sc = device_get_softc(dev);
332 if ((rv = bus_generic_detach(dev)) != 0)
335 if (sc->iicbus != NULL)
336 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
339 bus_release_resources(dev, res_spec, sc->res);
341 mtx_destroy(&sc->mutex);
346 * Only slave mode supported, disregard [old]addr
349 mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
351 struct mv_twsi_softc *sc;
354 sc = device_get_softc(dev);
358 baud_rate = TWSI_BAUD_RATE_51DOT8;
361 baud_rate = TWSI_BAUD_RATE_51DOT8;
366 baud_rate = TWSI_BAUD_RATE_99DOT7;
370 mtx_lock(&sc->mutex);
371 TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
373 TWSI_WRITE(sc, TWSI_BAUD_RATE, baud_rate);
374 TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
376 mtx_unlock(&sc->mutex);
382 * timeout is given in us
385 mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
387 struct mv_twsi_softc *sc;
390 sc = device_get_softc(dev);
392 mtx_lock(&sc->mutex);
393 rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
395 mtx_unlock(&sc->mutex);
405 * timeout is given in us
408 mv_twsi_start(device_t dev, u_char slave, int timeout)
410 struct mv_twsi_softc *sc;
413 sc = device_get_softc(dev);
415 mtx_lock(&sc->mutex);
416 rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
417 mtx_unlock(&sc->mutex);
427 mv_twsi_stop(device_t dev)
429 struct mv_twsi_softc *sc;
431 sc = device_get_softc(dev);
433 mtx_lock(&sc->mutex);
434 twsi_control_set(sc, TWSI_CONTROL_STOP);
437 mtx_unlock(&sc->mutex);
443 mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
445 struct mv_twsi_softc *sc;
449 sc = device_get_softc(dev);
451 mtx_lock(&sc->mutex);
453 while (*read < len) {
455 * Check if we are reading last byte of the last buffer,
456 * do not send ACK then, per I2C specs
458 last_byte = ((*read == len - 1) && last) ? 1 : 0;
460 twsi_control_clear(sc, TWSI_CONTROL_ACK);
462 twsi_control_set(sc, TWSI_CONTROL_ACK);
467 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
468 debugf("timeout reading data\n");
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);
481 *buf++ = TWSI_READ(sc, TWSI_DATA);
486 mtx_unlock(&sc->mutex);
491 mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
493 struct mv_twsi_softc *sc;
497 sc = device_get_softc(dev);
499 mtx_lock(&sc->mutex);
501 while (*sent < len) {
502 TWSI_WRITE(sc, TWSI_DATA, *buf++);
505 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
506 debugf("timeout writing data\n");
511 status = TWSI_READ(sc, TWSI_STATUS);
512 if (status != TWSI_STATUS_DATA_WR_ACK) {
513 debugf("wrong status (%02x) while writing\n", status);
521 mtx_unlock(&sc->mutex);