46#include <sys/kernel.h>
49#include <sys/module.h>
50#include <sys/resource.h>
52#include <sys/sysctl.h>
54#include <machine/_inttypes.h>
55#include <machine/bus.h>
56#include <machine/resource.h>
65#define TWSI_CONTROL_ACK (1 << 2)
66#define TWSI_CONTROL_IFLG (1 << 3)
67#define TWSI_CONTROL_STOP (1 << 4)
68#define TWSI_CONTROL_START (1 << 5)
69#define TWSI_CONTROL_TWSIEN (1 << 6)
70#define TWSI_CONTROL_INTEN (1 << 7)
72#define TWSI_STATUS_BUS_ERROR 0x00
73#define TWSI_STATUS_START 0x08
74#define TWSI_STATUS_RPTD_START 0x10
75#define TWSI_STATUS_ADDR_W_ACK 0x18
76#define TWSI_STATUS_ADDR_W_NACK 0x20
77#define TWSI_STATUS_DATA_WR_ACK 0x28
78#define TWSI_STATUS_DATA_WR_NACK 0x30
79#define TWSI_STATUS_ARBITRATION_LOST 0x38
80#define TWSI_STATUS_ADDR_R_ACK 0x40
81#define TWSI_STATUS_ADDR_R_NACK 0x48
82#define TWSI_STATUS_DATA_RD_ACK 0x50
83#define TWSI_STATUS_DATA_RD_NOACK 0x58
84#define TWSI_STATUS_IDLE 0xf8
89#define debugf(sc, fmt, args...) if ((sc)->debug) \
90 device_printf((sc)->dev, "%s: " fmt, __func__, ##args)
93 { SYS_RES_MEMORY, 0, RF_ACTIVE },
94 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
98static __inline uint32_t
103 val = bus_read_4(sc->
res[0], off);
105 debugf(sc,
"read %x from %lx\n",
val, off);
114 debugf(sc,
"Writing %x to %lx\n",
val, off);
115 bus_write_4(sc->
res[0], off,
val);
169 debugf(sc,
"Waiting for ctrl reg to match mask %x\n", mask);
190 int read_access, iflg_set = 0;
193 mtx_assert(&sc->
mutex, MA_OWNED);
199 debugf(sc,
"send start\n");
203 debugf(sc,
"IFLG set, clearing (mask=%x)\n", mask);
214 debugf(sc,
"timeout sending %sSTART condition\n",
220 debugf(sc,
"status=%x\n", status);
222 if (status != mask) {
223 debugf(sc,
"wrong status (%02x) after sending %sSTART condition\n",
233 debugf(sc,
"timeout sending slave address (timeout=%d)\n",
timeout);
237 read_access = (
slave & 0x1) ? 1 : 0;
239 if (status != (read_access ?
241 debugf(sc,
"no ACK (status: %02x) after sending slave address\n",
249#define TWSI_BAUD_RATE_RAW(C,M,N) ((C)/((10*(M+1))<<(N)))
250#define ABSSUB(a,b) (((a) > (b)) ? (a) - (b) : (b) - (a))
257 uint32_t cur, diff, diff0;
263 if (clk_get_freq(sc->
clk_core, &clk) < 0)
266 debugf(sc,
"Bus clock is at %ju\n", clk);
268 for (n = 0; n < 8; n++) {
269 for (m = 0; m < 16; m++) {
271 diff =
ABSSUB(target, cur);
294 sc = device_get_softc(
dev);
303 debugf(sc,
"Using IIC_FAST mode with speed param=%x\n", param);
309 debugf(sc,
"Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param);
314 debugf(sc,
"Using clock param=%x\n", param);
316 mtx_lock(&sc->
mutex);
321 mtx_unlock(&sc->
mutex);
331 sc = device_get_softc(
dev);
333 debugf(sc,
"%s\n", __func__);
334 mtx_lock(&sc->
mutex);
339 mtx_unlock(&sc->
mutex);
353 sc = device_get_softc(
dev);
356 mtx_lock(&sc->
mutex);
359 mtx_unlock(&sc->
mutex);
377 sc = device_get_softc(
dev);
380 mtx_lock(&sc->
mutex);
382 mtx_unlock(&sc->
mutex);
398 sc = device_get_softc(
dev);
400 mtx_lock(&sc->
mutex);
417 debugf(sc,
"timeout reading data (delay=%d)\n",
delay);
423 if (status != (last_byte ?
425 debugf(sc,
"wrong status (%02x) while reading\n", status);
435 mtx_unlock(&sc->
mutex);
446 sc = device_get_softc(
dev);
448 mtx_lock(&sc->
mutex);
450 while (*sent <
len) {
463 debugf(sc,
"wrong status (%02x) while writing\n", status);
471 mtx_unlock(&sc->
mutex);
481 debugf(sc,
"Sending STOP condition for error %d\n", err);
495 sc = device_get_softc(
dev);
500 mtx_lock(&sc->
mutex);
502 (
"starting a transfer while another is active"));
506 debugf(sc,
"status=0x%x\n", status);
508 debugf(sc,
"Bad status at start of transfer\n");
520 for (
int i = 0; i <
nmsgs; i++)
527 msleep_sbt(sc, &sc->
mutex, 0,
"twsi", 3000 * SBT_1MS, SBT_1MS, 0);
528 debugf(sc,
"pause finish\n");
530 device_printf(sc->
dev,
"transfer timeout\n");
544 mtx_unlock(&sc->
mutex);
560 mtx_lock(&sc->
mutex);
564 debugf(sc,
"reg control = 0x%x, status = 0x%x\n",
568 device_printf(sc->
dev,
"interrupt without active transfer, "
569 "status = 0x%x\n", status);
576 message_done =
false;
582 debugf(sc,
"Send address 0x%x\n",
594 debugf(sc,
"Address ACK-ed (write)\n");
599 debugf(sc,
"Sending byte 0 (of %d) = %x\n",
605 debugf(sc,
"Zero-length write, sending STOP\n");
612 debugf(sc,
"Address ACK-ed (read)\n");
616 debugf(sc,
"Zero-length read, sending STOP\n");
628 debugf(sc,
"Address NACK-ed\n");
632 debugf(sc,
"Data byte NACK-ed\n");
637 (
"sent_bytes beyond message length"));
638 debugf(sc,
"ACK received after transmitting data\n");
640 debugf(sc,
"Done TX data\n");
647 debugf(sc,
"NOSTOP flag\n");
653 debugf(sc,
"Sending byte %d (of %d) = 0x%x\n",
663 debugf(sc,
"Received and ACK-ed data\n");
665 (
"receiving beyond the end of buffer"));
669 debugf(sc,
"Received byte %d (of %d) = 0x%x\n",
683 debugf(sc,
"RX all but asked for more?\n");
689 debugf(sc,
"Received and NACK-ed data\n");
691 (
"sent NACK before receiving all requested data"));
694 debugf(sc,
"Received byte %d (of %d) = 0x%x\n",
701 debugf(sc,
"Done RX data\n");
703 debugf(sc,
"Send STOP\n");
713 debugf(sc,
"NACK-ed before receving all bytes?\n");
719 debugf(sc,
"Bus error\n");
723 debugf(sc,
"Arbitration lost\n");
727 debugf(sc,
"unexpected status 0x%x\n", status);
735 debugf(sc,
"All messages transmitted\n");
739 debugf(sc,
"Send (repeated) start\n");
744 (
"NOSTART message after STOP"));
747 (
"change of transfer direction without a START"));
748 debugf(sc,
"NOSTART message after NOSTOP\n");
755 debugf(sc,
"Read+NOSTART unsupported\n");
764 debugf(sc,
"Refresh reg_control\n");
772 mtx_unlock(&sc->
mutex);
780 sc = device_get_softc(pdev);
782 if ((bus_setup_intr(pdev, sc->
res[1], INTR_TYPE_MISC | INTR_MPSAFE,
784 device_printf(pdev,
"unable to register interrupt handler\n");
793 struct sysctl_ctx_list *ctx;
794 struct sysctl_oid *tree_node;
795 struct sysctl_oid_list *tree;
797 sc = device_get_softc(
dev);
800 mtx_init(&sc->
mutex, device_get_nameunit(
dev),
"twsi", MTX_DEF);
803 device_printf(
dev,
"could not allocate resources\n");
811 ctx = device_get_sysctl_ctx(
dev);
812 tree_node = device_get_sysctl_tree(
dev);
813 tree = SYSCTL_CHILDREN(tree_node);
814 SYSCTL_ADD_INT(ctx, tree, OID_AUTO,
"debug", CTLFLAG_RWTUN,
815 &sc->
debug, 0,
"Set debug level (zero to disable)");
818 if ((sc->
iicbus = device_add_child(
dev,
"iicbus", -1)) == NULL) {
819 device_printf(
dev,
"could not allocate iicbus instance\n");
823 bus_generic_attach(
dev);
836 sc = device_get_softc(
dev);
838 if ((rv = bus_generic_detach(
dev)) != 0)
842 if ((rv = device_delete_child(
dev, sc->
iicbus)) != 0)
850 mtx_destroy(&sc->
mutex);
859 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
860 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
861 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
862 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
863 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
864 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
865 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
866 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
867 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
int iicbus_null_callback(device_t dev, int index, caddr_t data)
int iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
int iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
int iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
int iicbus_stop(device_t bus)
int iicbus_repeated_start(device_t bus, u_char slave, int timeout)
int iicbus_start(device_t bus, u_char slave, int timeout)
int iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
#define iicbus_reset(bus, speed, addr, oldaddr)
bus_size_t reg_soft_reset
struct twsi_baud_rate baud_rate[IIC_FASTEST+1]
#define TWSI_STATUS_DATA_WR_ACK
#define TWSI_STATUS_DATA_RD_NOACK
#define TWSI_STATUS_RPTD_START
static int twsi_repeated_start(device_t dev, u_char slave, int timeout)
static int twsi_start(device_t dev, u_char slave, int timeout)
static int twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
static __inline void twsi_control_set(struct twsi_softc *sc, uint32_t mask)
static __inline void twsi_control_clear(struct twsi_softc *sc, uint32_t mask)
static __inline void twsi_clear_iflg(struct twsi_softc *sc)
static struct resource_spec res_spec[]
static int twsi_stop(device_t dev)
#define TWSI_CONTROL_IFLG
static void twsi_error(struct twsi_softc *sc, int err)
static void twsi_intr_start(void *pdev)
#define TWSI_CONTROL_STOP
#define TWSI_STATUS_ARBITRATION_LOST
static int twsi_calc_baud_rate(struct twsi_softc *sc, const u_int target, int *param)
static __inline void TWSI_WRITE(struct twsi_softc *sc, bus_size_t off, uint32_t val)
static void twsi_intr(void *arg)
int twsi_detach(device_t dev)
static device_method_t twsi_methods[]
static int twsi_locked_start(device_t dev, struct twsi_softc *sc, int32_t mask, u_char slave, int timeout)
#define TWSI_CONTROL_START
static __inline uint32_t TWSI_READ(struct twsi_softc *sc, bus_size_t off)
#define TWSI_STATUS_START
static int twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
DEFINE_CLASS_0(twsi, twsi_driver, twsi_methods, sizeof(struct twsi_softc))
static int twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
static int twsi_poll_ctrl(struct twsi_softc *sc, int timeout, uint32_t mask)
#define TWSI_STATUS_ADDR_R_NACK
#define TWSI_STATUS_DATA_WR_NACK
#define TWSI_CONTROL_TWSIEN
#define TWSI_STATUS_ADDR_R_ACK
#define TWSI_STATUS_ADDR_W_ACK
#define TWSI_CONTROL_INTEN
int twsi_attach(device_t dev)
#define debugf(sc, fmt, args...)
#define TWSI_BAUD_RATE_RAW(C, M, N)
#define TWSI_STATUS_BUS_ERROR
static int twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
#define TWSI_STATUS_ADDR_W_NACK
#define TWSI_STATUS_DATA_RD_ACK
#define TWSI_BAUD_RATE_PARAM(M, N)