Home | History | Annotate | Line # | Download | only in nvidia
tegra_i2c.c revision 1.26.14.1
      1  1.26.14.1   thorpej /* $NetBSD: tegra_i2c.c,v 1.26.14.1 2021/08/09 00:30:07 thorpej Exp $ */
      2        1.1  jmcneill 
      3        1.1  jmcneill /*-
      4        1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5        1.1  jmcneill  * All rights reserved.
      6        1.1  jmcneill  *
      7        1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8        1.1  jmcneill  * modification, are permitted provided that the following conditions
      9        1.1  jmcneill  * are met:
     10        1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12        1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15        1.1  jmcneill  *
     16        1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21        1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22        1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23        1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24        1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1  jmcneill  * SUCH DAMAGE.
     27        1.1  jmcneill  */
     28        1.1  jmcneill 
     29        1.1  jmcneill #include <sys/cdefs.h>
     30  1.26.14.1   thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.26.14.1 2021/08/09 00:30:07 thorpej Exp $");
     31        1.1  jmcneill 
     32        1.1  jmcneill #include <sys/param.h>
     33        1.1  jmcneill #include <sys/bus.h>
     34        1.1  jmcneill #include <sys/device.h>
     35        1.1  jmcneill #include <sys/intr.h>
     36        1.1  jmcneill #include <sys/systm.h>
     37        1.1  jmcneill #include <sys/kernel.h>
     38        1.1  jmcneill 
     39        1.1  jmcneill #include <dev/i2c/i2cvar.h>
     40        1.1  jmcneill 
     41        1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     42        1.1  jmcneill #include <arm/nvidia/tegra_i2creg.h>
     43        1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     44        1.1  jmcneill 
     45        1.9  jmcneill #include <dev/fdt/fdtvar.h>
     46        1.9  jmcneill 
     47        1.1  jmcneill static int	tegra_i2c_match(device_t, cfdata_t, void *);
     48        1.1  jmcneill static void	tegra_i2c_attach(device_t, device_t, void *);
     49        1.1  jmcneill 
     50        1.1  jmcneill struct tegra_i2c_softc {
     51        1.1  jmcneill 	device_t		sc_dev;
     52        1.1  jmcneill 	bus_space_tag_t		sc_bst;
     53        1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     54        1.1  jmcneill 	void *			sc_ih;
     55       1.11  jmcneill 	struct clk *		sc_clk;
     56       1.11  jmcneill 	struct fdtbus_reset *	sc_rst;
     57       1.11  jmcneill 	u_int			sc_cid;
     58        1.1  jmcneill 
     59        1.1  jmcneill 	struct i2c_controller	sc_ic;
     60       1.23   thorpej 	kmutex_t		sc_intr_lock;
     61       1.23   thorpej 	kcondvar_t		sc_intr_wait;
     62        1.1  jmcneill };
     63        1.1  jmcneill 
     64        1.1  jmcneill static void	tegra_i2c_init(struct tegra_i2c_softc *);
     65        1.1  jmcneill static int	tegra_i2c_intr(void *);
     66        1.1  jmcneill 
     67        1.1  jmcneill static int	tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     68        1.1  jmcneill 			       size_t, void *, size_t, int);
     69        1.1  jmcneill 
     70        1.1  jmcneill static int	tegra_i2c_wait(struct tegra_i2c_softc *, int);
     71        1.1  jmcneill static int	tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
     72        1.6  jmcneill 				const uint8_t *, size_t, int, bool);
     73        1.1  jmcneill static int	tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
     74        1.1  jmcneill 			       size_t, int);
     75        1.1  jmcneill 
     76        1.1  jmcneill CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
     77        1.1  jmcneill 	tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
     78        1.1  jmcneill 
     79        1.1  jmcneill #define I2C_WRITE(sc, reg, val) \
     80        1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     81        1.1  jmcneill #define I2C_READ(sc, reg) \
     82        1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     83        1.1  jmcneill #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
     84        1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
     85        1.1  jmcneill 
     86       1.26   thorpej static const struct device_compatible_entry compat_data[] = {
     87       1.26   thorpej 	{ .compat = "nvidia,tegra210-i2c" },
     88       1.26   thorpej 	{ .compat = "nvidia,tegra124-i2c" },
     89       1.26   thorpej 	{ .compat = "nvidia,tegra114-i2c" },
     90       1.26   thorpej 	DEVICE_COMPAT_EOL
     91       1.26   thorpej };
     92       1.26   thorpej 
     93        1.1  jmcneill static int
     94        1.1  jmcneill tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
     95        1.1  jmcneill {
     96        1.9  jmcneill 	struct fdt_attach_args * const faa = aux;
     97        1.1  jmcneill 
     98       1.26   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     99        1.1  jmcneill }
    100        1.1  jmcneill 
    101        1.1  jmcneill static void
    102        1.1  jmcneill tegra_i2c_attach(device_t parent, device_t self, void *aux)
    103        1.1  jmcneill {
    104        1.1  jmcneill 	struct tegra_i2c_softc * const sc = device_private(self);
    105        1.9  jmcneill 	struct fdt_attach_args * const faa = aux;
    106       1.10  jmcneill 	const int phandle = faa->faa_phandle;
    107        1.9  jmcneill 	char intrstr[128];
    108        1.9  jmcneill 	bus_addr_t addr;
    109        1.9  jmcneill 	bus_size_t size;
    110       1.10  jmcneill 	int error;
    111        1.9  jmcneill 
    112       1.10  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    113        1.9  jmcneill 		aprint_error(": couldn't get registers\n");
    114        1.9  jmcneill 		return;
    115        1.9  jmcneill 	}
    116       1.11  jmcneill 	sc->sc_clk = fdtbus_clock_get(phandle, "div-clk");
    117       1.11  jmcneill 	if (sc->sc_clk == NULL) {
    118       1.11  jmcneill 		aprint_error(": couldn't get clock div-clk\n");
    119       1.11  jmcneill 		return;
    120       1.11  jmcneill 	}
    121       1.11  jmcneill 	sc->sc_rst = fdtbus_reset_get(phandle, "i2c");
    122       1.11  jmcneill 	if (sc->sc_rst == NULL) {
    123       1.11  jmcneill 		aprint_error(": couldn't get reset i2c\n");
    124       1.11  jmcneill 		return;
    125       1.11  jmcneill 	}
    126        1.1  jmcneill 
    127        1.1  jmcneill 	sc->sc_dev = self;
    128        1.9  jmcneill 	sc->sc_bst = faa->faa_bst;
    129       1.11  jmcneill 	sc->sc_cid = device_unit(self);
    130        1.9  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    131        1.9  jmcneill 	if (error) {
    132       1.20     skrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
    133       1.20     skrll 		    addr, error);
    134        1.9  jmcneill 		return;
    135        1.9  jmcneill 	}
    136       1.23   thorpej 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    137       1.23   thorpej 	cv_init(&sc->sc_intr_wait, device_xname(self));
    138        1.1  jmcneill 
    139        1.1  jmcneill 	aprint_naive("\n");
    140       1.11  jmcneill 	aprint_normal(": I2C\n");
    141        1.1  jmcneill 
    142       1.10  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    143        1.9  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    144        1.9  jmcneill 		return;
    145        1.9  jmcneill 	}
    146        1.9  jmcneill 
    147       1.25  jmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
    148       1.25  jmcneill 	    FDT_INTR_MPSAFE, tegra_i2c_intr, sc, device_xname(self));
    149        1.1  jmcneill 	if (sc->sc_ih == NULL) {
    150        1.9  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    151        1.9  jmcneill 		    intrstr);
    152        1.1  jmcneill 		return;
    153        1.1  jmcneill 	}
    154        1.9  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    155        1.1  jmcneill 
    156        1.8  jmcneill 	/*
    157        1.8  jmcneill 	 * Recommended setting for standard mode is to use an I2C source div
    158        1.8  jmcneill 	 * of 20 (Tegra K1 Technical Reference Manual, Table 137)
    159        1.8  jmcneill 	 */
    160       1.11  jmcneill 	fdtbus_reset_assert(sc->sc_rst);
    161       1.11  jmcneill 	error = clk_set_rate(sc->sc_clk, 20400000);
    162       1.11  jmcneill 	if (error) {
    163       1.11  jmcneill 		aprint_error_dev(self, "couldn't set frequency: %d\n", error);
    164       1.11  jmcneill 		return;
    165       1.11  jmcneill 	}
    166       1.11  jmcneill 	error = clk_enable(sc->sc_clk);
    167       1.11  jmcneill 	if (error) {
    168       1.11  jmcneill 		aprint_error_dev(self, "couldn't enable clock: %d\n", error);
    169       1.11  jmcneill 		return;
    170       1.11  jmcneill 	}
    171       1.11  jmcneill 	fdtbus_reset_deassert(sc->sc_rst);
    172        1.1  jmcneill 
    173       1.23   thorpej 	mutex_enter(&sc->sc_intr_lock);
    174        1.1  jmcneill 	tegra_i2c_init(sc);
    175       1.23   thorpej 	mutex_exit(&sc->sc_intr_lock);
    176        1.1  jmcneill 
    177       1.23   thorpej 	iic_tag_init(&sc->sc_ic);
    178        1.1  jmcneill 	sc->sc_ic.ic_cookie = sc;
    179        1.1  jmcneill 	sc->sc_ic.ic_exec = tegra_i2c_exec;
    180        1.1  jmcneill 
    181       1.24   thorpej 	fdtbus_register_i2c_controller(&sc->sc_ic, phandle);
    182        1.9  jmcneill 
    183  1.26.14.1   thorpej 	struct i2cbus_attach_args iba = {
    184  1.26.14.1   thorpej 		.iba_tag = &sc->sc_ic,
    185  1.26.14.1   thorpej 	};
    186  1.26.14.1   thorpej 	config_found(self, &iba, iicbus_print,
    187  1.26.14.1   thorpej 	    CFARGS(.devhandle = device_handle(self)));
    188        1.1  jmcneill }
    189        1.1  jmcneill 
    190        1.1  jmcneill static void
    191        1.1  jmcneill tegra_i2c_init(struct tegra_i2c_softc *sc)
    192        1.1  jmcneill {
    193        1.4  jmcneill 	int retry = 10000;
    194        1.4  jmcneill 
    195        1.1  jmcneill 	I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
    196        1.1  jmcneill 	    __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
    197        1.1  jmcneill 	    __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
    198        1.1  jmcneill 
    199        1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    200        1.2  jmcneill 	I2C_WRITE(sc, I2C_CNFG_REG,
    201        1.2  jmcneill 	    I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN);
    202        1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
    203        1.4  jmcneill 	I2C_WRITE(sc, I2C_FIFO_CONTROL_REG,
    204        1.4  jmcneill 	    __SHIFTIN(7, I2C_FIFO_CONTROL_TX_FIFO_TRIG) |
    205        1.4  jmcneill 	    __SHIFTIN(0, I2C_FIFO_CONTROL_RX_FIFO_TRIG));
    206        1.4  jmcneill 
    207        1.3  jmcneill 	I2C_WRITE(sc, I2C_BUS_CONFIG_LOAD_REG,
    208        1.3  jmcneill 	    I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD);
    209        1.4  jmcneill 	while (--retry > 0) {
    210        1.4  jmcneill 		if (I2C_READ(sc, I2C_BUS_CONFIG_LOAD_REG) == 0)
    211        1.4  jmcneill 			break;
    212        1.4  jmcneill 		delay(10);
    213        1.4  jmcneill 	}
    214        1.4  jmcneill 	if (retry == 0) {
    215        1.4  jmcneill 		device_printf(sc->sc_dev, "config load timeout\n");
    216        1.4  jmcneill 	}
    217        1.1  jmcneill }
    218        1.1  jmcneill 
    219        1.1  jmcneill static int
    220        1.1  jmcneill tegra_i2c_intr(void *priv)
    221        1.1  jmcneill {
    222        1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    223        1.1  jmcneill 
    224        1.1  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    225        1.1  jmcneill 	if (istatus == 0)
    226        1.1  jmcneill 		return 0;
    227        1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    228        1.1  jmcneill 
    229       1.23   thorpej 	mutex_enter(&sc->sc_intr_lock);
    230       1.23   thorpej 	cv_broadcast(&sc->sc_intr_wait);
    231       1.23   thorpej 	mutex_exit(&sc->sc_intr_lock);
    232        1.1  jmcneill 
    233        1.1  jmcneill 	return 1;
    234        1.1  jmcneill }
    235        1.1  jmcneill 
    236        1.1  jmcneill static int
    237        1.1  jmcneill tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    238        1.1  jmcneill     size_t cmdlen, void *buf, size_t buflen, int flags)
    239        1.1  jmcneill {
    240        1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    241        1.1  jmcneill 	int retry, error;
    242        1.1  jmcneill 
    243       1.23   thorpej 	/*
    244       1.23   thorpej 	 * XXXJRT This is probably no longer necessary?  Before these
    245       1.23   thorpej 	 * changes, the bus lock was also used for the interrupt handler,
    246       1.23   thorpej 	 * and there would be a deadlock when the interrupt handler tried to
    247       1.23   thorpej 	 * acquire it again.  The bus lock is now owned by the mid-layer and
    248       1.23   thorpej 	 * we have our own interrupt lock.
    249       1.23   thorpej 	 */
    250       1.23   thorpej 	flags |= I2C_F_POLL;
    251        1.1  jmcneill 
    252       1.22  jmcneill 	if (buflen == 0 && cmdlen == 0)
    253       1.22  jmcneill 		return EINVAL;
    254       1.22  jmcneill 
    255       1.23   thorpej 	mutex_enter(&sc->sc_intr_lock);
    256       1.23   thorpej 
    257        1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    258        1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
    259        1.1  jmcneill 		    I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
    260        1.1  jmcneill 		    I2C_INTERRUPT_MASK_TIMEOUT |
    261        1.1  jmcneill 		    I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
    262        1.1  jmcneill 	}
    263        1.1  jmcneill 
    264        1.1  jmcneill 	const uint32_t flush_mask =
    265        1.1  jmcneill 	    I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
    266        1.1  jmcneill 
    267        1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
    268        1.1  jmcneill 	for (retry = 10000; retry > 0; retry--) {
    269        1.1  jmcneill 		const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
    270        1.1  jmcneill 		if ((v & flush_mask) == 0)
    271        1.1  jmcneill 			break;
    272        1.1  jmcneill 		delay(1);
    273        1.1  jmcneill 	}
    274        1.1  jmcneill 	if (retry == 0) {
    275       1.23   thorpej 		mutex_exit(&sc->sc_intr_lock);
    276        1.1  jmcneill 		device_printf(sc->sc_dev, "timeout flushing FIFO\n");
    277        1.1  jmcneill 		return EIO;
    278        1.1  jmcneill 	}
    279        1.1  jmcneill 
    280        1.1  jmcneill 	if (cmdlen > 0) {
    281        1.7  jmcneill 		error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags,
    282       1.15  jakllsch 		    buflen > 0 ? true : false);
    283        1.1  jmcneill 		if (error) {
    284        1.1  jmcneill 			goto done;
    285        1.1  jmcneill 		}
    286        1.1  jmcneill 	}
    287        1.1  jmcneill 
    288       1.22  jmcneill 	if (buflen > 0) {
    289       1.22  jmcneill 		if (I2C_OP_READ_P(op)) {
    290       1.22  jmcneill 			error = tegra_i2c_read(sc, addr, buf, buflen, flags);
    291       1.22  jmcneill 		} else {
    292       1.22  jmcneill 			error = tegra_i2c_write(sc, addr, buf, buflen, flags, false);
    293       1.22  jmcneill 		}
    294        1.1  jmcneill 	}
    295        1.1  jmcneill 
    296        1.1  jmcneill done:
    297        1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    298        1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    299        1.1  jmcneill 	}
    300        1.3  jmcneill 
    301        1.3  jmcneill 	if (error) {
    302        1.3  jmcneill 		tegra_i2c_init(sc);
    303        1.3  jmcneill 	}
    304        1.3  jmcneill 
    305       1.23   thorpej 	mutex_exit(&sc->sc_intr_lock);
    306       1.23   thorpej 
    307        1.1  jmcneill 	return error;
    308        1.1  jmcneill }
    309        1.1  jmcneill 
    310        1.1  jmcneill static int
    311        1.1  jmcneill tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
    312        1.1  jmcneill {
    313        1.2  jmcneill 	int error, retry;
    314        1.2  jmcneill 	uint32_t stat = 0;
    315        1.2  jmcneill 
    316        1.2  jmcneill 	retry = (flags & I2C_F_POLL) ? 100000 : 100;
    317        1.2  jmcneill 
    318        1.2  jmcneill 	while (--retry > 0) {
    319        1.1  jmcneill 		if ((flags & I2C_F_POLL) == 0) {
    320       1.23   thorpej 			error = cv_timedwait_sig(&sc->sc_intr_wait,
    321       1.23   thorpej 						 &sc->sc_intr_lock,
    322       1.23   thorpej 						 uimax(mstohz(10), 1));
    323        1.1  jmcneill 			if (error) {
    324        1.1  jmcneill 				return error;
    325        1.1  jmcneill 			}
    326        1.1  jmcneill 		}
    327        1.2  jmcneill 		stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    328        1.2  jmcneill 		if (stat & I2C_INTERRUPT_STATUS_PACKET_XFER_COMPLETE) {
    329        1.1  jmcneill 			break;
    330        1.1  jmcneill 		}
    331        1.1  jmcneill 		if (flags & I2C_F_POLL) {
    332        1.2  jmcneill 			delay(10);
    333        1.1  jmcneill 		}
    334        1.1  jmcneill 	}
    335        1.2  jmcneill 	if (retry == 0) {
    336       1.22  jmcneill #ifdef TEGRA_I2C_DEBUG
    337        1.2  jmcneill 		device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
    338       1.22  jmcneill #endif
    339        1.2  jmcneill 		return ETIMEDOUT;
    340        1.2  jmcneill 	}
    341        1.1  jmcneill 
    342        1.2  jmcneill 	const uint32_t err_mask =
    343        1.2  jmcneill 	    I2C_INTERRUPT_STATUS_NOACK |
    344        1.2  jmcneill 	    I2C_INTERRUPT_STATUS_ARB_LOST |
    345        1.2  jmcneill 	    I2C_INTERRUPT_MASK_TIMEOUT;
    346        1.1  jmcneill 
    347        1.2  jmcneill 	if (stat & err_mask) {
    348        1.2  jmcneill 		device_printf(sc->sc_dev, "error, status = %#x\n", stat);
    349        1.1  jmcneill 		return EIO;
    350        1.2  jmcneill 	}
    351        1.1  jmcneill 
    352        1.1  jmcneill 	return 0;
    353        1.1  jmcneill }
    354        1.1  jmcneill 
    355        1.1  jmcneill static int
    356        1.1  jmcneill tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
    357        1.6  jmcneill     size_t buflen, int flags, bool repeat_start)
    358        1.1  jmcneill {
    359        1.2  jmcneill 	const uint8_t *p = buf;
    360        1.2  jmcneill 	size_t n, resid = buflen;
    361        1.2  jmcneill 	uint32_t data;
    362        1.2  jmcneill 	int retry;
    363        1.1  jmcneill 
    364        1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    365        1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    366        1.1  jmcneill 
    367        1.2  jmcneill 	/* Generic Header 0 */
    368        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    369        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    370        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    371       1.11  jmcneill 	    __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
    372        1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    373        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    374        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    375        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    376        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    377        1.2  jmcneill 	/* Generic Header 1 */
    378        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    379        1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    380        1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    381        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    382        1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE |
    383        1.6  jmcneill 	    (repeat_start ? I2C_IOPACKET_XMITHDR_REPEAT_STARTSTOP : 0) |
    384        1.2  jmcneill 	    __SHIFTIN((addr << 1), I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    385        1.2  jmcneill 
    386        1.2  jmcneill 	/* Transmit data */
    387        1.2  jmcneill 	while (resid > 0) {
    388        1.2  jmcneill 		retry = 10000;
    389        1.2  jmcneill 		while (--retry > 0) {
    390        1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    391        1.2  jmcneill 			const u_int cnt =
    392        1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT);
    393        1.2  jmcneill 			if (cnt > 0)
    394        1.2  jmcneill 				break;
    395        1.2  jmcneill 			delay(10);
    396        1.2  jmcneill 		}
    397        1.2  jmcneill 		if (retry == 0) {
    398        1.2  jmcneill 			device_printf(sc->sc_dev, "TX FIFO timeout\n");
    399        1.2  jmcneill 			return ETIMEDOUT;
    400        1.2  jmcneill 		}
    401        1.1  jmcneill 
    402       1.21  riastrad 		for (n = 0, data = 0; n < uimin(resid, 4); n++) {
    403        1.2  jmcneill 			data |= (uint32_t)p[n] << (n * 8);
    404        1.2  jmcneill 		}
    405        1.2  jmcneill 		I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG, data);
    406       1.21  riastrad 		p += uimin(resid, 4);
    407       1.21  riastrad 		resid -= uimin(resid, 4);
    408        1.2  jmcneill 	}
    409        1.1  jmcneill 
    410        1.1  jmcneill 	return tegra_i2c_wait(sc, flags);
    411        1.1  jmcneill }
    412        1.1  jmcneill 
    413        1.1  jmcneill static int
    414        1.1  jmcneill tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
    415        1.1  jmcneill     size_t buflen, int flags)
    416        1.1  jmcneill {
    417        1.2  jmcneill 	uint8_t *p = buf;
    418        1.2  jmcneill 	size_t n, resid = buflen;
    419        1.2  jmcneill 	uint32_t data;
    420        1.3  jmcneill 	int retry;
    421        1.2  jmcneill 
    422        1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    423        1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    424        1.1  jmcneill 
    425        1.2  jmcneill 	/* Generic Header 0 */
    426        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    427        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    428        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    429       1.11  jmcneill 	    __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
    430        1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    431        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    432        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    433        1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    434        1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    435        1.2  jmcneill 	/* Generic Header 1 */
    436        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    437        1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    438        1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    439        1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    440        1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE | I2C_IOPACKET_XMITHDR_READ |
    441        1.2  jmcneill 	    __SHIFTIN((addr << 1) | 1, I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    442        1.1  jmcneill 
    443        1.2  jmcneill 	while (resid > 0) {
    444        1.2  jmcneill 		retry = 10000;
    445        1.2  jmcneill 		while (--retry > 0) {
    446        1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    447        1.2  jmcneill 			const u_int cnt =
    448        1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_RX_FIFO_FULL_CNT);
    449        1.2  jmcneill 			if (cnt > 0)
    450        1.2  jmcneill 				break;
    451        1.2  jmcneill 			delay(10);
    452        1.2  jmcneill 		}
    453        1.2  jmcneill 		if (retry == 0) {
    454        1.2  jmcneill 			device_printf(sc->sc_dev, "RX FIFO timeout\n");
    455        1.2  jmcneill 			return ETIMEDOUT;
    456        1.2  jmcneill 		}
    457        1.1  jmcneill 
    458        1.2  jmcneill 		data = I2C_READ(sc, I2C_RX_FIFO_REG);
    459       1.21  riastrad 		for (n = 0; n < uimin(resid, 4); n++) {
    460        1.2  jmcneill 			p[n] = (data >> (n * 8)) & 0xff;
    461        1.2  jmcneill 		}
    462       1.21  riastrad 		p += uimin(resid, 4);
    463       1.21  riastrad 		resid -= uimin(resid, 4);
    464        1.1  jmcneill 	}
    465        1.1  jmcneill 
    466        1.3  jmcneill 	return tegra_i2c_wait(sc, flags);
    467        1.1  jmcneill }
    468