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/_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/fdt/fdt_common.h>
62 #include <dev/ofw/ofw_bus.h>
63 #include <dev/ofw/ofw_bus_subr.h>
65 #include <arm/mv/mvreg.h>
66 #include <arm/mv/mvvar.h>
68 #include "iicbus_if.h"
70 #define MV_TWSI_NAME "twsi"
71 #define IICBUS_DEVNAME "iicbus"
73 #define TWSI_SLAVE_ADDR 0x00
74 #define TWSI_EXT_SLAVE_ADDR 0x10
75 #define TWSI_DATA 0x04
77 #define TWSI_CONTROL 0x08
78 #define TWSI_CONTROL_ACK (1 << 2)
79 #define TWSI_CONTROL_IFLG (1 << 3)
80 #define TWSI_CONTROL_STOP (1 << 4)
81 #define TWSI_CONTROL_START (1 << 5)
82 #define TWSI_CONTROL_TWSIEN (1 << 6)
83 #define TWSI_CONTROL_INTEN (1 << 7)
85 #define TWSI_STATUS 0x0c
86 #define TWSI_STATUS_START 0x08
87 #define TWSI_STATUS_RPTD_START 0x10
88 #define TWSI_STATUS_ADDR_W_ACK 0x18
89 #define TWSI_STATUS_DATA_WR_ACK 0x28
90 #define TWSI_STATUS_ADDR_R_ACK 0x40
91 #define TWSI_STATUS_DATA_RD_ACK 0x50
92 #define TWSI_STATUS_DATA_RD_NOACK 0x58
94 #define TWSI_BAUD_RATE 0x0c
95 #define TWSI_BAUD_RATE_PARAM(M,N) ((((M) << 3) | ((N) & 0x7)) & 0x7f)
96 #define TWSI_BAUD_RATE_RAW(C,M,N) ((C)/((10*(M+1))<<(N+1)))
97 #define TWSI_BAUD_RATE_SLOW 50000 /* 50kHz */
98 #define TWSI_BAUD_RATE_FAST 100000 /* 100kHz */
100 #define TWSI_SOFT_RESET 0x1c
106 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
108 #define debugf(fmt, args...)
111 struct mv_twsi_softc {
113 struct resource *res[1]; /* SYS_RES_MEMORY */
118 static struct mv_twsi_baud_rate {
123 } baud_rate[IIC_FASTEST + 1];
125 static int mv_twsi_probe(device_t);
126 static int mv_twsi_attach(device_t);
127 static int mv_twsi_detach(device_t);
129 static int mv_twsi_reset(device_t dev, u_char speed, u_char addr,
131 static int mv_twsi_repeated_start(device_t dev, u_char slave, int timeout);
132 static int mv_twsi_start(device_t dev, u_char slave, int timeout);
133 static int mv_twsi_stop(device_t dev);
134 static int mv_twsi_read(device_t dev, char *buf, int len, int *read, int last,
136 static int mv_twsi_write(device_t dev, const char *buf, int len, int *sent,
139 static struct resource_spec res_spec[] = {
140 { SYS_RES_MEMORY, 0, RF_ACTIVE },
144 static device_method_t mv_twsi_methods[] = {
145 /* device interface */
146 DEVMETHOD(device_probe, mv_twsi_probe),
147 DEVMETHOD(device_attach, mv_twsi_attach),
148 DEVMETHOD(device_detach, mv_twsi_detach),
150 /* iicbus interface */
151 DEVMETHOD(iicbus_callback, iicbus_null_callback),
152 DEVMETHOD(iicbus_repeated_start, mv_twsi_repeated_start),
153 DEVMETHOD(iicbus_start, mv_twsi_start),
154 DEVMETHOD(iicbus_stop, mv_twsi_stop),
155 DEVMETHOD(iicbus_write, mv_twsi_write),
156 DEVMETHOD(iicbus_read, mv_twsi_read),
157 DEVMETHOD(iicbus_reset, mv_twsi_reset),
158 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
162 static devclass_t mv_twsi_devclass;
164 static driver_t mv_twsi_driver = {
167 sizeof(struct mv_twsi_softc),
170 DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
171 DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
172 MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
174 static __inline uint32_t
175 TWSI_READ(struct mv_twsi_softc *sc, bus_size_t off)
178 return (bus_read_4(sc->res[0], off));
182 TWSI_WRITE(struct mv_twsi_softc *sc, bus_size_t off, uint32_t val)
185 bus_write_4(sc->res[0], off, val);
189 twsi_control_clear(struct mv_twsi_softc *sc, uint32_t mask)
193 val = TWSI_READ(sc, TWSI_CONTROL);
195 TWSI_WRITE(sc, TWSI_CONTROL, val);
199 twsi_control_set(struct mv_twsi_softc *sc, uint32_t mask)
203 val = TWSI_READ(sc, TWSI_CONTROL);
205 TWSI_WRITE(sc, TWSI_CONTROL, val);
209 twsi_clear_iflg(struct mv_twsi_softc *sc)
213 twsi_control_clear(sc, TWSI_CONTROL_IFLG);
219 * timeout given in us
221 * 0 on sucessfull mask change
222 * non-zero on timeout
225 twsi_poll_ctrl(struct mv_twsi_softc *sc, int timeout, uint32_t mask)
229 while (!(TWSI_READ(sc, TWSI_CONTROL) & mask)) {
239 * 'timeout' is given in us. Note also that timeout handling is not exact --
240 * twsi_locked_start() total wait can be more than 2 x timeout
241 * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
242 * or TWSI_STATUS_RPTD_START
245 twsi_locked_start(device_t dev, struct mv_twsi_softc *sc, int32_t mask,
246 u_char slave, int timeout)
248 int read_access, iflg_set = 0;
251 mtx_assert(&sc->mutex, MA_OWNED);
253 if (mask == TWSI_STATUS_RPTD_START)
254 /* read IFLG to know if it should be cleared later; from NBSD */
255 iflg_set = TWSI_READ(sc, TWSI_CONTROL) & TWSI_CONTROL_IFLG;
257 twsi_control_set(sc, TWSI_CONTROL_START);
259 if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
260 debugf("IFLG set, clearing\n");
265 * Without this delay we timeout checking IFLG if the timeout is 0.
266 * NBSD driver always waits here too.
270 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
271 debugf("timeout sending %sSTART condition\n",
272 mask == TWSI_STATUS_START ? "" : "repeated ");
273 return (IIC_ETIMEOUT);
276 status = TWSI_READ(sc, TWSI_STATUS);
277 if (status != mask) {
278 debugf("wrong status (%02x) after sending %sSTART condition\n",
279 status, mask == TWSI_STATUS_START ? "" : "repeated ");
280 return (IIC_ESTATUS);
283 TWSI_WRITE(sc, TWSI_DATA, slave);
287 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
288 debugf("timeout sending slave address\n");
289 return (IIC_ETIMEOUT);
292 read_access = (slave & 0x1) ? 1 : 0;
293 status = TWSI_READ(sc, TWSI_STATUS);
294 if (status != (read_access ?
295 TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
296 debugf("no ACK (status: %02x) after sending slave address\n",
305 mv_twsi_probe(device_t dev)
308 if (!ofw_bus_is_compatible(dev, "mrvl,twsi"))
311 device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
312 return (BUS_PROBE_DEFAULT);
315 #define ABSSUB(a,b) (((a) > (b)) ? (a) - (b) : (b) - (a))
317 mv_twsi_cal_baud_rate(const uint32_t target, struct mv_twsi_baud_rate *rate)
319 uint32_t clk, cur, diff, diff0;
322 /* Calculate baud rate. */
323 m0 = n0 = 4; /* Default values on reset */
327 for (n = 0; n < 8; n++) {
328 for (m = 0; m < 16; m++) {
329 cur = TWSI_BAUD_RATE_RAW(clk,m,n);
330 diff = ABSSUB(target, cur);
338 rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
339 rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
345 mv_twsi_attach(device_t dev)
347 struct mv_twsi_softc *sc;
348 phandle_t child, iicbusnode;
350 struct iicbus_ivar *devi;
351 char dname[32]; /* 32 is taken from struct u_device */
355 sc = device_get_softc(dev);
357 bzero(baud_rate, sizeof(baud_rate));
359 mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
361 /* Allocate IO resources */
362 if (bus_alloc_resources(dev, res_spec, sc->res)) {
363 device_printf(dev, "could not allocate resources\n");
368 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &baud_rate[IIC_SLOW]);
369 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &baud_rate[IIC_FAST]);
371 device_printf(dev, "calculated baud rates are:\n"
372 " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
373 " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
374 baud_rate[IIC_SLOW].raw / 1000,
375 baud_rate[IIC_SLOW].m,
376 baud_rate[IIC_SLOW].n,
377 baud_rate[IIC_FAST].raw / 1000,
378 baud_rate[IIC_FAST].m,
379 baud_rate[IIC_FAST].n);
381 sc->iicbus = device_add_child(dev, IICBUS_DEVNAME, -1);
382 if (sc->iicbus == NULL) {
383 device_printf(dev, "could not add iicbus child\n");
388 bus_generic_attach(dev);
391 /* Find iicbus as the child devices in the device tree. */
392 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
393 child = OF_peer(child)) {
394 len = OF_getproplen(child, "model");
395 if (len <= 0 || len > sizeof(dname) - 1)
397 error = OF_getprop(child, "model", &dname, len);
398 dname[len + 1] = '\0';
402 if (len == strlen(IICBUS_DEVNAME) &&
403 strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
411 /* Attach child devices onto iicbus. */
412 for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
413 /* Get slave address. */
414 error = OF_getprop(child, "i2c-address", &paddr, sizeof(paddr));
416 error = OF_getprop(child, "reg", &paddr, sizeof(paddr));
420 /* Get device driver name. */
421 len = OF_getproplen(child, "model");
422 if (len <= 0 || len > sizeof(dname) - 1)
424 OF_getprop(child, "model", &dname, len);
425 dname[len + 1] = '\0';
428 device_printf(dev, "adding a device %s at %d.\n",
429 dname, fdt32_to_cpu(paddr));
430 childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
431 devi = IICBUS_IVAR(childdev);
432 devi->addr = fdt32_to_cpu(paddr);
436 bus_generic_attach(sc->iicbus);
442 mv_twsi_detach(device_t dev)
444 struct mv_twsi_softc *sc;
447 sc = device_get_softc(dev);
449 if ((rv = bus_generic_detach(dev)) != 0)
452 if (sc->iicbus != NULL)
453 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
456 bus_release_resources(dev, res_spec, sc->res);
458 mtx_destroy(&sc->mutex);
463 * Only slave mode supported, disregard [old]addr
466 mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
468 struct mv_twsi_softc *sc;
471 sc = device_get_softc(dev);
476 param = baud_rate[speed].param;
481 param = baud_rate[IIC_FAST].param;
485 mtx_lock(&sc->mutex);
486 TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
488 TWSI_WRITE(sc, TWSI_BAUD_RATE, param);
489 TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
491 mtx_unlock(&sc->mutex);
497 * timeout is given in us
500 mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
502 struct mv_twsi_softc *sc;
505 sc = device_get_softc(dev);
507 mtx_lock(&sc->mutex);
508 rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
510 mtx_unlock(&sc->mutex);
520 * timeout is given in us
523 mv_twsi_start(device_t dev, u_char slave, int timeout)
525 struct mv_twsi_softc *sc;
528 sc = device_get_softc(dev);
530 mtx_lock(&sc->mutex);
531 rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
532 mtx_unlock(&sc->mutex);
542 mv_twsi_stop(device_t dev)
544 struct mv_twsi_softc *sc;
546 sc = device_get_softc(dev);
548 mtx_lock(&sc->mutex);
549 twsi_control_set(sc, TWSI_CONTROL_STOP);
552 mtx_unlock(&sc->mutex);
558 mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
560 struct mv_twsi_softc *sc;
564 sc = device_get_softc(dev);
566 mtx_lock(&sc->mutex);
568 while (*read < len) {
570 * Check if we are reading last byte of the last buffer,
571 * do not send ACK then, per I2C specs
573 last_byte = ((*read == len - 1) && last) ? 1 : 0;
575 twsi_control_clear(sc, TWSI_CONTROL_ACK);
577 twsi_control_set(sc, TWSI_CONTROL_ACK);
582 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
583 debugf("timeout reading data\n");
588 status = TWSI_READ(sc, TWSI_STATUS);
589 if (status != (last_byte ?
590 TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
591 debugf("wrong status (%02x) while reading\n", status);
596 *buf++ = TWSI_READ(sc, TWSI_DATA);
601 mtx_unlock(&sc->mutex);
606 mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
608 struct mv_twsi_softc *sc;
612 sc = device_get_softc(dev);
614 mtx_lock(&sc->mutex);
616 while (*sent < len) {
617 TWSI_WRITE(sc, TWSI_DATA, *buf++);
620 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
621 debugf("timeout writing data\n");
626 status = TWSI_READ(sc, TWSI_STATUS);
627 if (status != TWSI_STATUS_DATA_WR_ACK) {
628 debugf("wrong status (%02x) while writing\n", status);
636 mtx_unlock(&sc->mutex);