2022-07-23 02:43:23 +03:00
|
|
|
/* $NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej Exp $ */
|
2011-01-04 04:24:56 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2011-01-04 05:50:08 +03:00
|
|
|
* by Matt Thomas.
|
2011-01-04 04:24:56 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2022-07-23 02:43:23 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej Exp $");
|
2019-08-05 15:21:00 +03:00
|
|
|
|
|
|
|
#if defined(__arm__) || defined(__aarch64__)
|
|
|
|
#include "opt_fdt.h"
|
|
|
|
#endif
|
2011-01-04 04:24:56 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/intr.h>
|
|
|
|
|
|
|
|
#include <dev/i2c/i2cvar.h>
|
|
|
|
#include <dev/i2c/motoi2creg.h>
|
|
|
|
#include <dev/i2c/motoi2cvar.h>
|
|
|
|
|
2019-08-05 15:21:00 +03:00
|
|
|
#ifdef FDT
|
|
|
|
#include <dev/fdt/fdtvar.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-04 04:24:56 +03:00
|
|
|
#ifdef DEBUG
|
2011-01-12 21:06:26 +03:00
|
|
|
int motoi2c_debug = 0;
|
|
|
|
#define DPRINTF(x) if (motoi2c_debug) printf x
|
2011-01-04 04:24:56 +03:00
|
|
|
#else
|
2011-01-12 21:06:26 +03:00
|
|
|
#define DPRINTF(x)
|
2011-01-04 04:24:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int motoi2c_acquire_bus(void *, int);
|
|
|
|
static void motoi2c_release_bus(void *, int);
|
|
|
|
static int motoi2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
|
|
|
|
void *, size_t, int);
|
2022-07-23 02:43:23 +03:00
|
|
|
static void motoi2c_clear_status(struct motoi2c_softc *, uint8_t);
|
2011-01-04 04:24:56 +03:00
|
|
|
static int motoi2c_busy_wait(struct motoi2c_softc *, uint8_t);
|
|
|
|
|
|
|
|
static const struct motoi2c_settings motoi2c_default_settings = {
|
|
|
|
.i2c_adr = MOTOI2C_ADR_DEFAULT,
|
|
|
|
.i2c_fdr = MOTOI2C_FDR_DEFAULT,
|
|
|
|
.i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define I2C_READ(r) ((*sc->sc_iord)(sc, (r)))
|
|
|
|
#define I2C_WRITE(r,v) ((*sc->sc_iowr)(sc, (r), (v)))
|
|
|
|
#define I2C_SETCLR(r, s, c) \
|
|
|
|
((*sc->sc_iowr)(sc, (r), ((*sc->sc_iord)(sc, (r)) | (s)) & ~(c)))
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
motoi2c_iord1(struct motoi2c_softc *sc, bus_size_t off)
|
|
|
|
{
|
|
|
|
return bus_space_read_1(sc->sc_iot, sc->sc_ioh, off);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
motoi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
|
|
|
|
{
|
|
|
|
bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-23 02:43:23 +03:00
|
|
|
motoi2c_attach(struct motoi2c_softc *sc,
|
|
|
|
const struct motoi2c_settings *settings)
|
2011-01-04 04:24:56 +03:00
|
|
|
{
|
|
|
|
struct i2cbus_attach_args iba;
|
|
|
|
|
2022-07-23 02:43:23 +03:00
|
|
|
if (settings == NULL) {
|
|
|
|
sc->sc_settings = motoi2c_default_settings;
|
|
|
|
} else {
|
|
|
|
sc->sc_settings = *settings;
|
|
|
|
}
|
|
|
|
if (sc->sc_iord == NULL)
|
|
|
|
sc->sc_iord = motoi2c_iord1;
|
|
|
|
if (sc->sc_iowr == NULL)
|
|
|
|
sc->sc_iowr = motoi2c_iowr1;
|
2011-01-04 04:24:56 +03:00
|
|
|
|
2019-12-23 02:23:29 +03:00
|
|
|
iic_tag_init(&sc->sc_i2c);
|
2011-01-04 04:24:56 +03:00
|
|
|
sc->sc_i2c.ic_cookie = sc;
|
2019-12-23 02:23:29 +03:00
|
|
|
sc->sc_i2c.ic_acquire_bus = motoi2c_acquire_bus;
|
|
|
|
sc->sc_i2c.ic_release_bus = motoi2c_release_bus;
|
|
|
|
sc->sc_i2c.ic_exec = motoi2c_exec;
|
2011-01-04 04:24:56 +03:00
|
|
|
memset(&iba, 0, sizeof(iba));
|
|
|
|
iba.iba_tag = &sc->sc_i2c;
|
2021-01-24 21:01:13 +03:00
|
|
|
iba.iba_child_devices = sc->sc_child_devices;
|
2011-01-04 04:24:56 +03:00
|
|
|
|
2021-01-25 15:08:47 +03:00
|
|
|
if ((sc->sc_flags & MOTOI2C_F_ENABLE_INV) != 0) {
|
|
|
|
sc->sc_enable_mask = 0;
|
|
|
|
sc->sc_disable_mask = CR_MEN;
|
|
|
|
} else {
|
|
|
|
sc->sc_enable_mask = CR_MEN;
|
|
|
|
sc->sc_disable_mask = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2C_WRITE(I2CCR, sc->sc_disable_mask); /* reset before config */
|
2022-07-23 02:43:23 +03:00
|
|
|
I2C_WRITE(I2CDFSRR, sc->sc_settings.i2c_dfsrr); /* sampling units */
|
|
|
|
I2C_WRITE(I2CFDR, sc->sc_settings.i2c_fdr); /* divider 3072 */
|
|
|
|
I2C_WRITE(I2CADR, sc->sc_settings.i2c_adr); /* our slave address */
|
|
|
|
motoi2c_clear_status(sc, I2C_READ(I2CSR));
|
2011-01-04 04:24:56 +03:00
|
|
|
|
2019-08-05 15:21:00 +03:00
|
|
|
#ifdef FDT
|
2021-01-24 21:01:13 +03:00
|
|
|
if (sc->sc_phandle != 0) {
|
|
|
|
fdtbus_register_i2c_controller(&sc->sc_i2c, sc->sc_phandle);
|
2022-07-23 02:43:23 +03:00
|
|
|
fdtbus_attach_i2cbus(sc->sc_dev, sc->sc_phandle, &sc->sc_i2c,
|
2021-01-24 21:01:13 +03:00
|
|
|
iicbus_print);
|
|
|
|
} else
|
2019-08-05 15:21:00 +03:00
|
|
|
#endif
|
2022-07-23 02:43:23 +03:00
|
|
|
config_found(sc->sc_dev, &iba, iicbus_print,
|
2021-08-07 19:18:40 +03:00
|
|
|
CFARGS(.iattr = "i2cbus"));
|
2011-01-04 04:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
motoi2c_acquire_bus(void *v, int flags)
|
|
|
|
{
|
|
|
|
struct motoi2c_softc * const sc = v;
|
|
|
|
|
2021-01-25 15:08:47 +03:00
|
|
|
I2C_WRITE(I2CCR, sc->sc_enable_mask); /* enable the I2C module */
|
2011-01-04 04:24:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
motoi2c_release_bus(void *v, int flags)
|
|
|
|
{
|
|
|
|
struct motoi2c_softc * const sc = v;
|
|
|
|
|
2021-01-25 15:08:47 +03:00
|
|
|
I2C_WRITE(I2CCR, sc->sc_disable_mask); /* disable the I2C module */
|
2011-01-04 04:24:56 +03:00
|
|
|
}
|
|
|
|
|
2019-11-29 15:42:53 +03:00
|
|
|
static int
|
|
|
|
motoi2c_stop_wait(struct motoi2c_softc *sc)
|
|
|
|
{
|
|
|
|
u_int timo;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
timo = 1000;
|
|
|
|
while ((I2C_READ(I2CSR) & SR_MBB) != 0 && --timo)
|
|
|
|
DELAY(1);
|
|
|
|
|
|
|
|
if (timo == 0) {
|
|
|
|
DPRINTF(("%s: timeout (sr=%#x)\n", __func__, I2C_READ(I2CSR)));
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:08:47 +03:00
|
|
|
static void
|
|
|
|
motoi2c_clear_status(struct motoi2c_softc *sc, uint8_t sr)
|
|
|
|
{
|
|
|
|
if ((sc->sc_flags & MOTOI2C_F_STATUS_W1C) != 0) {
|
|
|
|
I2C_WRITE(I2CSR, sr);
|
|
|
|
} else {
|
|
|
|
I2C_WRITE(I2CSR, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-04 04:24:56 +03:00
|
|
|
/* busy waiting for byte data transfer completion */
|
|
|
|
static int
|
|
|
|
motoi2c_busy_wait(struct motoi2c_softc *sc, uint8_t cr)
|
|
|
|
{
|
|
|
|
uint8_t sr;
|
|
|
|
u_int timo;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
timo = 1000;
|
|
|
|
while (((sr = I2C_READ(I2CSR)) & SR_MIF) == 0 && --timo)
|
|
|
|
DELAY(10);
|
|
|
|
|
|
|
|
if (timo == 0) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: timeout (sr=%#x, cr=%#x)\n",
|
|
|
|
__func__, sr, I2C_READ(I2CCR)));
|
2011-01-04 04:24:56 +03:00
|
|
|
error = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* RXAK is only valid when transmitting.
|
|
|
|
*/
|
|
|
|
if ((cr & CR_MTX) && (sr & SR_RXAK)) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: missing rx ack (%#x): spin=%u\n",
|
|
|
|
__func__, sr, 1000 - timo));
|
2011-01-04 04:24:56 +03:00
|
|
|
error = EIO;
|
|
|
|
}
|
2021-01-25 15:08:47 +03:00
|
|
|
motoi2c_clear_status(sc, sr);
|
2011-01-04 04:24:56 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
motoi2c_intr(void *v)
|
|
|
|
{
|
|
|
|
struct motoi2c_softc * const sc = v;
|
|
|
|
|
|
|
|
panic("%s(%p)", __func__, sc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
motoi2c_exec(void *v, i2c_op_t op, i2c_addr_t addr,
|
|
|
|
const void *cmdbuf, size_t cmdlen,
|
|
|
|
void *databuf, size_t datalen,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
struct motoi2c_softc * const sc = v;
|
|
|
|
uint8_t sr;
|
|
|
|
uint8_t cr;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
sr = I2C_READ(I2CSR);
|
|
|
|
cr = I2C_READ(I2CCR);
|
|
|
|
|
|
|
|
#if 0
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s(%#x,%#x,%p,%zu,%p,%zu,%#x): sr=%#x cr=%#x\n",
|
2011-01-04 04:24:56 +03:00
|
|
|
__func__, op, addr, cmdbuf, cmdlen, databuf, datalen, flags,
|
2011-01-12 21:06:26 +03:00
|
|
|
sr, cr));
|
2011-01-04 04:24:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((cr & CR_MSTA) == 0 && (sr & SR_MBB) != 0) {
|
|
|
|
/* wait for bus becoming available */
|
2019-11-29 15:42:53 +03:00
|
|
|
error = motoi2c_stop_wait(sc);
|
|
|
|
if (error)
|
2011-01-04 04:24:56 +03:00
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset interrupt and arbitration-lost flags (all others are RO) */
|
2021-01-25 15:08:47 +03:00
|
|
|
motoi2c_clear_status(sc, sr);
|
2011-01-04 04:24:56 +03:00
|
|
|
sr = I2C_READ(I2CSR);
|
|
|
|
|
|
|
|
/*
|
2019-11-29 15:42:53 +03:00
|
|
|
* Generate start condition
|
2011-01-04 04:24:56 +03:00
|
|
|
*/
|
2021-01-25 15:08:47 +03:00
|
|
|
cr = sc->sc_enable_mask | CR_MTX | CR_MSTA;
|
2019-11-29 15:42:53 +03:00
|
|
|
I2C_WRITE(I2CCR, cr);
|
2011-01-04 04:24:56 +03:00
|
|
|
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: started: sr=%#x cr=%#x/%#x\n",
|
|
|
|
__func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
|
2011-01-04 04:24:56 +03:00
|
|
|
|
|
|
|
sr = I2C_READ(I2CSR);
|
|
|
|
if (sr & SR_MAL) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: lost bus: sr=%#x cr=%#x/%#x\n",
|
|
|
|
__func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
|
2021-01-25 15:08:47 +03:00
|
|
|
I2C_WRITE(I2CCR, sc->sc_disable_mask);
|
2011-01-04 04:24:56 +03:00
|
|
|
DELAY(10);
|
2021-01-25 15:08:47 +03:00
|
|
|
I2C_WRITE(I2CCR, sc->sc_enable_mask | CR_MTX | CR_MSTA);
|
2011-01-04 04:24:56 +03:00
|
|
|
DELAY(10);
|
|
|
|
sr = I2C_READ(I2CSR);
|
|
|
|
if (sr & SR_MAL) {
|
|
|
|
error = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: reacquired bus: sr=%#x cr=%#x/%#x\n",
|
|
|
|
__func__, I2C_READ(I2CSR), cr, I2C_READ(I2CCR)));
|
2011-01-04 04:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send target address and transfer direction */
|
|
|
|
uint8_t addr_byte = (addr << 1)
|
|
|
|
| (cmdlen == 0 && I2C_OP_READ_P(op) ? 1 : 0);
|
|
|
|
I2C_WRITE(I2CDR, addr_byte);
|
|
|
|
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: error sending address: %d\n", __func__, error));
|
2011-01-04 04:24:56 +03:00
|
|
|
if (error == EIO)
|
|
|
|
error = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *cmdptr = cmdbuf;
|
|
|
|
for (size_t i = 0; i < cmdlen; i++) {
|
|
|
|
I2C_WRITE(I2CDR, *cmdptr++);
|
|
|
|
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: error sending cmd byte %zu (cr=%#x/%#x):"
|
|
|
|
" %d\n", __func__, i, I2C_READ(I2CCR), cr, error));
|
2011-01-04 04:24:56 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdlen > 0 && I2C_OP_READ_P(op)) {
|
|
|
|
KASSERT(cr & CR_MTX);
|
|
|
|
KASSERT((cr & CR_TXAK) == 0);
|
|
|
|
I2C_WRITE(I2CCR, cr | CR_RSTA);
|
|
|
|
#if 0
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: restarted(read): sr=%#x cr=%#x(%#x)\n",
|
|
|
|
__func__, I2C_READ(I2CSR), cr | CR_RSTA, I2C_READ(I2CCR)));
|
2011-01-04 04:24:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* send target address and read transfer direction */
|
|
|
|
addr_byte |= 1;
|
|
|
|
I2C_WRITE(I2CDR, addr_byte);
|
|
|
|
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
|
|
|
if (error == EIO)
|
|
|
|
error = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I2C_OP_READ_P(op)) {
|
|
|
|
uint8_t *dataptr = databuf;
|
|
|
|
cr &= ~CR_MTX; /* clear transmit flags */
|
2011-04-17 19:14:59 +04:00
|
|
|
if (datalen <= 1)
|
2011-01-04 04:24:56 +03:00
|
|
|
cr |= CR_TXAK;
|
|
|
|
I2C_WRITE(I2CCR, cr);
|
|
|
|
DELAY(10);
|
|
|
|
(void)I2C_READ(I2CDR); /* dummy read */
|
|
|
|
for (size_t i = 0; i < datalen; i++) {
|
|
|
|
/*
|
|
|
|
* If a master receiver wants to terminate a data
|
|
|
|
* transfer, it must inform the slave transmitter by
|
|
|
|
* not acknowledging the last byte of data (by setting
|
|
|
|
* the transmit acknowledge bit (I2CCR[TXAK])) before
|
|
|
|
* reading the next-to-last byte of data.
|
|
|
|
*/
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: error reading byte %zu: %d\n",
|
|
|
|
__func__, i, error));
|
2011-01-04 04:24:56 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2011-04-17 19:14:59 +04:00
|
|
|
if (i == datalen - 2) {
|
|
|
|
cr |= CR_TXAK;
|
|
|
|
I2C_WRITE(I2CCR, cr);
|
|
|
|
} else if (i == datalen - 1 && I2C_OP_STOP_P(op)) {
|
2021-01-25 15:08:47 +03:00
|
|
|
cr = sc->sc_enable_mask | CR_TXAK;
|
2011-04-17 19:14:59 +04:00
|
|
|
I2C_WRITE(I2CCR, cr);
|
2011-01-04 04:24:56 +03:00
|
|
|
}
|
|
|
|
*dataptr++ = I2C_READ(I2CDR);
|
|
|
|
}
|
|
|
|
if (datalen == 0) {
|
2011-04-17 19:14:59 +04:00
|
|
|
if (I2C_OP_STOP_P(op)) {
|
2021-01-25 15:08:47 +03:00
|
|
|
cr = sc->sc_enable_mask | CR_TXAK;
|
2011-04-17 19:14:59 +04:00
|
|
|
I2C_WRITE(I2CCR, cr);
|
|
|
|
}
|
2011-01-04 04:24:56 +03:00
|
|
|
(void)I2C_READ(I2CDR); /* dummy read */
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: error reading dummy last byte:"
|
|
|
|
"%d\n", __func__, error));
|
2011-01-04 04:24:56 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const uint8_t *dataptr = databuf;
|
|
|
|
for (size_t i = 0; i < datalen; i++) {
|
|
|
|
I2C_WRITE(I2CDR, *dataptr++);
|
|
|
|
error = motoi2c_busy_wait(sc, cr);
|
|
|
|
if (error) {
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: error sending data byte %zu:"
|
|
|
|
" %d\n", __func__, i, error));
|
2011-01-04 04:24:56 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* If we encountered an error condition or caller wants a STOP,
|
|
|
|
* send a STOP.
|
|
|
|
*/
|
|
|
|
if (error || (cr & CR_TXAK) || ((cr & CR_MSTA) && I2C_OP_STOP_P(op))) {
|
2021-01-25 15:08:47 +03:00
|
|
|
cr = sc->sc_enable_mask;
|
2011-01-04 04:24:56 +03:00
|
|
|
I2C_WRITE(I2CCR, cr);
|
2019-11-29 15:42:53 +03:00
|
|
|
motoi2c_stop_wait(sc);
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: stopping: cr=%#x/%#x\n", __func__,
|
|
|
|
cr, I2C_READ(I2CCR)));
|
2011-01-04 04:24:56 +03:00
|
|
|
}
|
|
|
|
|
2011-01-12 21:06:26 +03:00
|
|
|
DPRINTF(("%s: exit sr=%#x cr=%#x: %d\n", __func__,
|
|
|
|
I2C_READ(I2CSR), I2C_READ(I2CCR), error));
|
2011-01-04 04:24:56 +03:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|