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_status_okay(dev))
311 if (!ofw_bus_is_compatible(dev, "mrvl,twsi"))
314 device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
315 return (BUS_PROBE_DEFAULT);
318 #define ABSSUB(a,b) (((a) > (b)) ? (a) - (b) : (b) - (a))
320 mv_twsi_cal_baud_rate(const uint32_t target, struct mv_twsi_baud_rate *rate)
322 uint32_t clk, cur, diff, diff0;
325 /* Calculate baud rate. */
326 m0 = n0 = 4; /* Default values on reset */
330 for (n = 0; n < 8; n++) {
331 for (m = 0; m < 16; m++) {
332 cur = TWSI_BAUD_RATE_RAW(clk,m,n);
333 diff = ABSSUB(target, cur);
341 rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
342 rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
348 mv_twsi_attach(device_t dev)
350 struct mv_twsi_softc *sc;
351 phandle_t child, iicbusnode;
353 struct iicbus_ivar *devi;
354 char dname[32]; /* 32 is taken from struct u_device */
358 sc = device_get_softc(dev);
360 bzero(baud_rate, sizeof(baud_rate));
362 mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
364 /* Allocate IO resources */
365 if (bus_alloc_resources(dev, res_spec, sc->res)) {
366 device_printf(dev, "could not allocate resources\n");
371 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &baud_rate[IIC_SLOW]);
372 mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &baud_rate[IIC_FAST]);
374 device_printf(dev, "calculated baud rates are:\n"
375 " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
376 " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
377 baud_rate[IIC_SLOW].raw / 1000,
378 baud_rate[IIC_SLOW].m,
379 baud_rate[IIC_SLOW].n,
380 baud_rate[IIC_FAST].raw / 1000,
381 baud_rate[IIC_FAST].m,
382 baud_rate[IIC_FAST].n);
384 sc->iicbus = device_add_child(dev, IICBUS_DEVNAME, -1);
385 if (sc->iicbus == NULL) {
386 device_printf(dev, "could not add iicbus child\n");
391 bus_generic_attach(dev);
394 /* Find iicbus as the child devices in the device tree. */
395 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
396 child = OF_peer(child)) {
397 len = OF_getproplen(child, "model");
398 if (len <= 0 || len > sizeof(dname) - 1)
400 error = OF_getprop(child, "model", &dname, len);
401 dname[len + 1] = '\0';
405 if (len == strlen(IICBUS_DEVNAME) &&
406 strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
414 /* Attach child devices onto iicbus. */
415 for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
416 /* Get slave address. */
417 error = OF_getprop(child, "i2c-address", &paddr, sizeof(paddr));
419 error = OF_getprop(child, "reg", &paddr, sizeof(paddr));
423 /* Get device driver name. */
424 len = OF_getproplen(child, "model");
425 if (len <= 0 || len > sizeof(dname) - 1)
427 OF_getprop(child, "model", &dname, len);
428 dname[len + 1] = '\0';
431 device_printf(dev, "adding a device %s at %d.\n",
432 dname, fdt32_to_cpu(paddr));
433 childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
434 devi = IICBUS_IVAR(childdev);
435 devi->addr = fdt32_to_cpu(paddr);
439 bus_generic_attach(sc->iicbus);
445 mv_twsi_detach(device_t dev)
447 struct mv_twsi_softc *sc;
450 sc = device_get_softc(dev);
452 if ((rv = bus_generic_detach(dev)) != 0)
455 if (sc->iicbus != NULL)
456 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
459 bus_release_resources(dev, res_spec, sc->res);
461 mtx_destroy(&sc->mutex);
466 * Only slave mode supported, disregard [old]addr
469 mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
471 struct mv_twsi_softc *sc;
474 sc = device_get_softc(dev);
479 param = baud_rate[speed].param;
484 param = baud_rate[IIC_FAST].param;
488 mtx_lock(&sc->mutex);
489 TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
491 TWSI_WRITE(sc, TWSI_BAUD_RATE, param);
492 TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
494 mtx_unlock(&sc->mutex);
500 * timeout is given in us
503 mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
505 struct mv_twsi_softc *sc;
508 sc = device_get_softc(dev);
510 mtx_lock(&sc->mutex);
511 rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
513 mtx_unlock(&sc->mutex);
523 * timeout is given in us
526 mv_twsi_start(device_t dev, u_char slave, int timeout)
528 struct mv_twsi_softc *sc;
531 sc = device_get_softc(dev);
533 mtx_lock(&sc->mutex);
534 rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
535 mtx_unlock(&sc->mutex);
545 mv_twsi_stop(device_t dev)
547 struct mv_twsi_softc *sc;
549 sc = device_get_softc(dev);
551 mtx_lock(&sc->mutex);
552 twsi_control_set(sc, TWSI_CONTROL_STOP);
555 mtx_unlock(&sc->mutex);
561 mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
563 struct mv_twsi_softc *sc;
567 sc = device_get_softc(dev);
569 mtx_lock(&sc->mutex);
571 while (*read < len) {
573 * Check if we are reading last byte of the last buffer,
574 * do not send ACK then, per I2C specs
576 last_byte = ((*read == len - 1) && last) ? 1 : 0;
578 twsi_control_clear(sc, TWSI_CONTROL_ACK);
580 twsi_control_set(sc, TWSI_CONTROL_ACK);
585 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
586 debugf("timeout reading data\n");
591 status = TWSI_READ(sc, TWSI_STATUS);
592 if (status != (last_byte ?
593 TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
594 debugf("wrong status (%02x) while reading\n", status);
599 *buf++ = TWSI_READ(sc, TWSI_DATA);
604 mtx_unlock(&sc->mutex);
609 mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
611 struct mv_twsi_softc *sc;
615 sc = device_get_softc(dev);
617 mtx_lock(&sc->mutex);
619 while (*sent < len) {
620 TWSI_WRITE(sc, TWSI_DATA, *buf++);
623 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
624 debugf("timeout writing data\n");
629 status = TWSI_READ(sc, TWSI_STATUS);
630 if (status != TWSI_STATUS_DATA_WR_ACK) {
631 debugf("wrong status (%02x) while writing\n", status);
639 mtx_unlock(&sc->mutex);