Home | History | Annotate | Line # | Download | only in nvidia
tegra_i2c.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: tegra_i2c.c,v 1.3 2015/05/16 23:09:08 jmcneill 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 "locators.h"
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.3 2015/05/16 23:09:08 jmcneill Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/bus.h>
     36  1.1  jmcneill #include <sys/device.h>
     37  1.1  jmcneill #include <sys/intr.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/kernel.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     44  1.1  jmcneill #include <arm/nvidia/tegra_i2creg.h>
     45  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     46  1.1  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.2  jmcneill 	u_int			sc_port;
     56  1.1  jmcneill 
     57  1.1  jmcneill 	struct i2c_controller	sc_ic;
     58  1.1  jmcneill 	kmutex_t		sc_lock;
     59  1.1  jmcneill 	kcondvar_t		sc_cv;
     60  1.1  jmcneill 	device_t		sc_i2cdev;
     61  1.1  jmcneill };
     62  1.1  jmcneill 
     63  1.1  jmcneill static void	tegra_i2c_init(struct tegra_i2c_softc *);
     64  1.1  jmcneill static int	tegra_i2c_intr(void *);
     65  1.1  jmcneill 
     66  1.1  jmcneill static int	tegra_i2c_acquire_bus(void *, int);
     67  1.1  jmcneill static void	tegra_i2c_release_bus(void *, int);
     68  1.1  jmcneill static int	tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     69  1.1  jmcneill 			       size_t, void *, size_t, int);
     70  1.1  jmcneill 
     71  1.1  jmcneill static int	tegra_i2c_wait(struct tegra_i2c_softc *, int);
     72  1.1  jmcneill static int	tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
     73  1.1  jmcneill 				const uint8_t *, size_t, int);
     74  1.1  jmcneill static int	tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
     75  1.1  jmcneill 			       size_t, int);
     76  1.1  jmcneill 
     77  1.1  jmcneill CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
     78  1.1  jmcneill 	tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
     79  1.1  jmcneill 
     80  1.1  jmcneill #define I2C_WRITE(sc, reg, val) \
     81  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     82  1.1  jmcneill #define I2C_READ(sc, reg) \
     83  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     84  1.1  jmcneill #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
     85  1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
     86  1.1  jmcneill 
     87  1.1  jmcneill static int
     88  1.1  jmcneill tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
     89  1.1  jmcneill {
     90  1.1  jmcneill 	struct tegraio_attach_args * const tio = aux;
     91  1.1  jmcneill 	const struct tegra_locators * const loc = &tio->tio_loc;
     92  1.1  jmcneill 
     93  1.1  jmcneill 	if (loc->loc_port == TEGRAIOCF_PORT_DEFAULT)
     94  1.1  jmcneill 		return 0;
     95  1.1  jmcneill 
     96  1.1  jmcneill 	return 1;
     97  1.1  jmcneill }
     98  1.1  jmcneill 
     99  1.1  jmcneill static void
    100  1.1  jmcneill tegra_i2c_attach(device_t parent, device_t self, void *aux)
    101  1.1  jmcneill {
    102  1.1  jmcneill 	struct tegra_i2c_softc * const sc = device_private(self);
    103  1.1  jmcneill 	struct tegraio_attach_args * const tio = aux;
    104  1.1  jmcneill 	const struct tegra_locators * const loc = &tio->tio_loc;
    105  1.1  jmcneill 	struct i2cbus_attach_args iba;
    106  1.1  jmcneill 
    107  1.1  jmcneill 	sc->sc_dev = self;
    108  1.1  jmcneill 	sc->sc_bst = tio->tio_bst;
    109  1.1  jmcneill 	bus_space_subregion(tio->tio_bst, tio->tio_bsh,
    110  1.1  jmcneill 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
    111  1.2  jmcneill 	sc->sc_port = loc->loc_port;
    112  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    113  1.1  jmcneill 	cv_init(&sc->sc_cv, device_xname(self));
    114  1.1  jmcneill 
    115  1.1  jmcneill 	aprint_naive("\n");
    116  1.1  jmcneill 	aprint_normal(": I2C%d\n", loc->loc_port + 1);
    117  1.1  jmcneill 
    118  1.1  jmcneill 	sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL|IST_MPSAFE,
    119  1.1  jmcneill 	    tegra_i2c_intr, sc);
    120  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    121  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt %d\n",
    122  1.1  jmcneill 		    loc->loc_intr);
    123  1.1  jmcneill 		return;
    124  1.1  jmcneill 	}
    125  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
    126  1.1  jmcneill 
    127  1.1  jmcneill 	/* Recommended setting for standard mode */
    128  1.3  jmcneill 	tegra_car_periph_i2c_enable(loc->loc_port, 20400000);
    129  1.1  jmcneill 
    130  1.1  jmcneill 	tegra_i2c_init(sc);
    131  1.1  jmcneill 
    132  1.1  jmcneill 	sc->sc_ic.ic_cookie = sc;
    133  1.1  jmcneill 	sc->sc_ic.ic_acquire_bus = tegra_i2c_acquire_bus;
    134  1.1  jmcneill 	sc->sc_ic.ic_release_bus = tegra_i2c_release_bus;
    135  1.1  jmcneill 	sc->sc_ic.ic_exec = tegra_i2c_exec;
    136  1.1  jmcneill 
    137  1.1  jmcneill 	iba.iba_tag = &sc->sc_ic;
    138  1.1  jmcneill 	sc->sc_i2cdev = config_found_ia(self, "i2cbus", &iba, iicbus_print);
    139  1.1  jmcneill }
    140  1.1  jmcneill 
    141  1.1  jmcneill static void
    142  1.1  jmcneill tegra_i2c_init(struct tegra_i2c_softc *sc)
    143  1.1  jmcneill {
    144  1.1  jmcneill 	I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
    145  1.1  jmcneill 	    __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
    146  1.1  jmcneill 	    __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
    147  1.1  jmcneill 
    148  1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    149  1.2  jmcneill 	I2C_WRITE(sc, I2C_CNFG_REG,
    150  1.2  jmcneill 	    I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN);
    151  1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
    152  1.3  jmcneill 	I2C_WRITE(sc, I2C_BUS_CONFIG_LOAD_REG,
    153  1.3  jmcneill 	    I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD);
    154  1.1  jmcneill }
    155  1.1  jmcneill 
    156  1.1  jmcneill static int
    157  1.1  jmcneill tegra_i2c_intr(void *priv)
    158  1.1  jmcneill {
    159  1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    160  1.1  jmcneill 
    161  1.1  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    162  1.1  jmcneill 	if (istatus == 0)
    163  1.1  jmcneill 		return 0;
    164  1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    165  1.1  jmcneill 
    166  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    167  1.1  jmcneill 	cv_broadcast(&sc->sc_cv);
    168  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    169  1.1  jmcneill 
    170  1.1  jmcneill 	return 1;
    171  1.1  jmcneill }
    172  1.1  jmcneill 
    173  1.1  jmcneill static int
    174  1.1  jmcneill tegra_i2c_acquire_bus(void *priv, int flags)
    175  1.1  jmcneill {
    176  1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    177  1.1  jmcneill 
    178  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    179  1.1  jmcneill 
    180  1.1  jmcneill 	return 0;
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.1  jmcneill static void
    184  1.1  jmcneill tegra_i2c_release_bus(void *priv, int flags)
    185  1.1  jmcneill {
    186  1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    187  1.1  jmcneill 
    188  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    189  1.1  jmcneill }
    190  1.1  jmcneill 
    191  1.1  jmcneill static int
    192  1.1  jmcneill tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    193  1.1  jmcneill     size_t cmdlen, void *buf, size_t buflen, int flags)
    194  1.1  jmcneill {
    195  1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    196  1.1  jmcneill 	int retry, error;
    197  1.1  jmcneill 
    198  1.1  jmcneill #if notyet
    199  1.1  jmcneill 	if (cold)
    200  1.1  jmcneill #endif
    201  1.1  jmcneill 		flags |= I2C_F_POLL;
    202  1.1  jmcneill 
    203  1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    204  1.1  jmcneill 
    205  1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    206  1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
    207  1.1  jmcneill 		    I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
    208  1.1  jmcneill 		    I2C_INTERRUPT_MASK_TIMEOUT |
    209  1.1  jmcneill 		    I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
    210  1.1  jmcneill 	}
    211  1.1  jmcneill 
    212  1.1  jmcneill 	const uint32_t flush_mask =
    213  1.1  jmcneill 	    I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
    214  1.1  jmcneill 
    215  1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
    216  1.1  jmcneill 	for (retry = 10000; retry > 0; retry--) {
    217  1.1  jmcneill 		const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
    218  1.1  jmcneill 		if ((v & flush_mask) == 0)
    219  1.1  jmcneill 			break;
    220  1.1  jmcneill 		delay(1);
    221  1.1  jmcneill 	}
    222  1.1  jmcneill 	if (retry == 0) {
    223  1.1  jmcneill 		device_printf(sc->sc_dev, "timeout flushing FIFO\n");
    224  1.1  jmcneill 		return EIO;
    225  1.1  jmcneill 	}
    226  1.1  jmcneill 
    227  1.1  jmcneill 	if (cmdlen > 0) {
    228  1.1  jmcneill 		error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags);
    229  1.1  jmcneill 		if (error) {
    230  1.1  jmcneill 			goto done;
    231  1.1  jmcneill 		}
    232  1.1  jmcneill 	}
    233  1.1  jmcneill 
    234  1.1  jmcneill 	if (I2C_OP_READ_P(op)) {
    235  1.1  jmcneill 		error = tegra_i2c_read(sc, addr, buf, buflen, flags);
    236  1.1  jmcneill 	} else {
    237  1.1  jmcneill 		error = tegra_i2c_write(sc, addr, buf, buflen, flags);
    238  1.1  jmcneill 	}
    239  1.1  jmcneill 
    240  1.1  jmcneill done:
    241  1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    242  1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    243  1.1  jmcneill 	}
    244  1.3  jmcneill 
    245  1.3  jmcneill 	if (error) {
    246  1.3  jmcneill 		tegra_i2c_init(sc);
    247  1.3  jmcneill 	}
    248  1.3  jmcneill 
    249  1.1  jmcneill 	return error;
    250  1.1  jmcneill }
    251  1.1  jmcneill 
    252  1.1  jmcneill static int
    253  1.1  jmcneill tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
    254  1.1  jmcneill {
    255  1.2  jmcneill 	int error, retry;
    256  1.2  jmcneill 	uint32_t stat = 0;
    257  1.2  jmcneill 
    258  1.2  jmcneill 	retry = (flags & I2C_F_POLL) ? 100000 : 100;
    259  1.2  jmcneill 
    260  1.2  jmcneill 	while (--retry > 0) {
    261  1.1  jmcneill 		if ((flags & I2C_F_POLL) == 0) {
    262  1.1  jmcneill 			error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
    263  1.2  jmcneill 			    max(mstohz(10), 1));
    264  1.1  jmcneill 			if (error) {
    265  1.1  jmcneill 				return error;
    266  1.1  jmcneill 			}
    267  1.1  jmcneill 		}
    268  1.2  jmcneill 		stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    269  1.2  jmcneill 		if (stat & I2C_INTERRUPT_STATUS_PACKET_XFER_COMPLETE) {
    270  1.1  jmcneill 			break;
    271  1.1  jmcneill 		}
    272  1.1  jmcneill 		if (flags & I2C_F_POLL) {
    273  1.2  jmcneill 			delay(10);
    274  1.1  jmcneill 		}
    275  1.1  jmcneill 	}
    276  1.2  jmcneill 	if (retry == 0) {
    277  1.2  jmcneill 		stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    278  1.2  jmcneill 		device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
    279  1.2  jmcneill 		return ETIMEDOUT;
    280  1.2  jmcneill 	}
    281  1.1  jmcneill 
    282  1.2  jmcneill 	const uint32_t err_mask =
    283  1.2  jmcneill 	    I2C_INTERRUPT_STATUS_NOACK |
    284  1.2  jmcneill 	    I2C_INTERRUPT_STATUS_ARB_LOST |
    285  1.2  jmcneill 	    I2C_INTERRUPT_MASK_TIMEOUT;
    286  1.1  jmcneill 
    287  1.2  jmcneill 	if (stat & err_mask) {
    288  1.2  jmcneill 		device_printf(sc->sc_dev, "error, status = %#x\n", stat);
    289  1.1  jmcneill 		return EIO;
    290  1.2  jmcneill 	}
    291  1.1  jmcneill 
    292  1.1  jmcneill 	return 0;
    293  1.1  jmcneill }
    294  1.1  jmcneill 
    295  1.1  jmcneill static int
    296  1.1  jmcneill tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
    297  1.1  jmcneill     size_t buflen, int flags)
    298  1.1  jmcneill {
    299  1.2  jmcneill 	const uint8_t *p = buf;
    300  1.2  jmcneill 	size_t n, resid = buflen;
    301  1.2  jmcneill 	uint32_t data;
    302  1.2  jmcneill 	int retry;
    303  1.1  jmcneill 
    304  1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    305  1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    306  1.1  jmcneill 
    307  1.2  jmcneill 	/* Generic Header 0 */
    308  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    309  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    310  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    311  1.2  jmcneill 	    __SHIFTIN(sc->sc_port, I2C_IOPACKET_WORD0_CONTROLLERID) |
    312  1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    313  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    314  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    315  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    316  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    317  1.2  jmcneill 	/* Generic Header 1 */
    318  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    319  1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    320  1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    321  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    322  1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE |
    323  1.2  jmcneill 	    __SHIFTIN((addr << 1), I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    324  1.2  jmcneill 
    325  1.2  jmcneill 	/* Transmit data */
    326  1.2  jmcneill 	while (resid > 0) {
    327  1.2  jmcneill 		retry = 10000;
    328  1.2  jmcneill 		while (--retry > 0) {
    329  1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    330  1.2  jmcneill 			const u_int cnt =
    331  1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT);
    332  1.2  jmcneill 			if (cnt > 0)
    333  1.2  jmcneill 				break;
    334  1.2  jmcneill 			delay(10);
    335  1.2  jmcneill 		}
    336  1.2  jmcneill 		if (retry == 0) {
    337  1.2  jmcneill 			device_printf(sc->sc_dev, "TX FIFO timeout\n");
    338  1.2  jmcneill 			return ETIMEDOUT;
    339  1.2  jmcneill 		}
    340  1.1  jmcneill 
    341  1.2  jmcneill 		for (n = 0, data = 0; n < min(resid, 4); n++) {
    342  1.2  jmcneill 			data |= (uint32_t)p[n] << (n * 8);
    343  1.2  jmcneill 		}
    344  1.2  jmcneill 		I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG, data);
    345  1.2  jmcneill 		resid -= min(resid, 4);
    346  1.2  jmcneill 		p += min(resid, 4);
    347  1.2  jmcneill 	}
    348  1.1  jmcneill 
    349  1.1  jmcneill 	return tegra_i2c_wait(sc, flags);
    350  1.1  jmcneill }
    351  1.1  jmcneill 
    352  1.1  jmcneill static int
    353  1.1  jmcneill tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
    354  1.1  jmcneill     size_t buflen, int flags)
    355  1.1  jmcneill {
    356  1.2  jmcneill 	uint8_t *p = buf;
    357  1.2  jmcneill 	size_t n, resid = buflen;
    358  1.2  jmcneill 	uint32_t data;
    359  1.3  jmcneill 	int retry;
    360  1.2  jmcneill 
    361  1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    362  1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    363  1.1  jmcneill 
    364  1.2  jmcneill 	/* Generic Header 0 */
    365  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    366  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    367  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    368  1.2  jmcneill 	    __SHIFTIN(sc->sc_port, I2C_IOPACKET_WORD0_CONTROLLERID) |
    369  1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    370  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    371  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    372  1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    373  1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    374  1.2  jmcneill 	/* Generic Header 1 */
    375  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    376  1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    377  1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    378  1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    379  1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE | I2C_IOPACKET_XMITHDR_READ |
    380  1.2  jmcneill 	    __SHIFTIN((addr << 1) | 1, I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    381  1.1  jmcneill 
    382  1.2  jmcneill 	while (resid > 0) {
    383  1.2  jmcneill 		retry = 10000;
    384  1.2  jmcneill 		while (--retry > 0) {
    385  1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    386  1.2  jmcneill 			const u_int cnt =
    387  1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_RX_FIFO_FULL_CNT);
    388  1.2  jmcneill 			if (cnt > 0)
    389  1.2  jmcneill 				break;
    390  1.2  jmcneill 			delay(10);
    391  1.2  jmcneill 		}
    392  1.2  jmcneill 		if (retry == 0) {
    393  1.2  jmcneill 			device_printf(sc->sc_dev, "RX FIFO timeout\n");
    394  1.2  jmcneill 			return ETIMEDOUT;
    395  1.2  jmcneill 		}
    396  1.1  jmcneill 
    397  1.2  jmcneill 		data = I2C_READ(sc, I2C_RX_FIFO_REG);
    398  1.2  jmcneill 		for (n = 0; n < min(resid, 4); n++) {
    399  1.2  jmcneill 			p[n] = (data >> (n * 8)) & 0xff;
    400  1.2  jmcneill 		}
    401  1.2  jmcneill 		resid -= min(resid, 4);
    402  1.2  jmcneill 		p += min(resid, 4);
    403  1.1  jmcneill 	}
    404  1.1  jmcneill 
    405  1.3  jmcneill 	return tegra_i2c_wait(sc, flags);
    406  1.1  jmcneill }
    407