Home | History | Annotate | Line # | Download | only in nvidia
tegra_i2c.c revision 1.17.2.1
      1  1.17.2.1  christos /* $NetBSD: tegra_i2c.c,v 1.17.2.1 2019/06/10 22:05:55 christos 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.17.2.1  christos __KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.17.2.1 2019/06/10 22:05:55 christos 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.9  jmcneill static i2c_tag_t tegra_i2c_get_tag(device_t);
     51       1.9  jmcneill 
     52       1.9  jmcneill struct fdtbus_i2c_controller_func tegra_i2c_funcs = {
     53       1.9  jmcneill 	.get_tag = tegra_i2c_get_tag
     54       1.9  jmcneill };
     55       1.9  jmcneill 
     56       1.1  jmcneill struct tegra_i2c_softc {
     57       1.1  jmcneill 	device_t		sc_dev;
     58       1.1  jmcneill 	bus_space_tag_t		sc_bst;
     59       1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     60       1.1  jmcneill 	void *			sc_ih;
     61      1.11  jmcneill 	struct clk *		sc_clk;
     62      1.11  jmcneill 	struct fdtbus_reset *	sc_rst;
     63      1.11  jmcneill 	u_int			sc_cid;
     64       1.1  jmcneill 
     65       1.1  jmcneill 	struct i2c_controller	sc_ic;
     66       1.1  jmcneill 	kmutex_t		sc_lock;
     67       1.1  jmcneill 	kcondvar_t		sc_cv;
     68       1.1  jmcneill };
     69       1.1  jmcneill 
     70       1.1  jmcneill static void	tegra_i2c_init(struct tegra_i2c_softc *);
     71       1.1  jmcneill static int	tegra_i2c_intr(void *);
     72       1.1  jmcneill 
     73       1.1  jmcneill static int	tegra_i2c_acquire_bus(void *, int);
     74       1.1  jmcneill static void	tegra_i2c_release_bus(void *, int);
     75       1.1  jmcneill static int	tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     76       1.1  jmcneill 			       size_t, void *, size_t, int);
     77       1.1  jmcneill 
     78       1.1  jmcneill static int	tegra_i2c_wait(struct tegra_i2c_softc *, int);
     79       1.1  jmcneill static int	tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
     80       1.6  jmcneill 				const uint8_t *, size_t, int, bool);
     81       1.1  jmcneill static int	tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
     82       1.1  jmcneill 			       size_t, int);
     83       1.1  jmcneill 
     84       1.1  jmcneill CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
     85       1.1  jmcneill 	tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
     86       1.1  jmcneill 
     87       1.1  jmcneill #define I2C_WRITE(sc, reg, val) \
     88       1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     89       1.1  jmcneill #define I2C_READ(sc, reg) \
     90       1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     91       1.1  jmcneill #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
     92       1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
     93       1.1  jmcneill 
     94       1.1  jmcneill static int
     95       1.1  jmcneill tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
     96       1.1  jmcneill {
     97      1.16  jmcneill 	const char * const compatible[] = {
     98      1.16  jmcneill 		"nvidia,tegra210-i2c",
     99      1.16  jmcneill 		"nvidia,tegra124-i2c",
    100      1.16  jmcneill 		"nvidia,tegra114-i2c",
    101      1.16  jmcneill 		NULL
    102      1.16  jmcneill 	};
    103       1.9  jmcneill 	struct fdt_attach_args * const faa = aux;
    104       1.1  jmcneill 
    105       1.9  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    106       1.1  jmcneill }
    107       1.1  jmcneill 
    108       1.1  jmcneill static void
    109       1.1  jmcneill tegra_i2c_attach(device_t parent, device_t self, void *aux)
    110       1.1  jmcneill {
    111       1.1  jmcneill 	struct tegra_i2c_softc * const sc = device_private(self);
    112       1.9  jmcneill 	struct fdt_attach_args * const faa = aux;
    113      1.10  jmcneill 	const int phandle = faa->faa_phandle;
    114       1.9  jmcneill 	char intrstr[128];
    115       1.9  jmcneill 	bus_addr_t addr;
    116       1.9  jmcneill 	bus_size_t size;
    117      1.10  jmcneill 	int error;
    118       1.9  jmcneill 
    119      1.10  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    120       1.9  jmcneill 		aprint_error(": couldn't get registers\n");
    121       1.9  jmcneill 		return;
    122       1.9  jmcneill 	}
    123      1.11  jmcneill 	sc->sc_clk = fdtbus_clock_get(phandle, "div-clk");
    124      1.11  jmcneill 	if (sc->sc_clk == NULL) {
    125      1.11  jmcneill 		aprint_error(": couldn't get clock div-clk\n");
    126      1.11  jmcneill 		return;
    127      1.11  jmcneill 	}
    128      1.11  jmcneill 	sc->sc_rst = fdtbus_reset_get(phandle, "i2c");
    129      1.11  jmcneill 	if (sc->sc_rst == NULL) {
    130      1.11  jmcneill 		aprint_error(": couldn't get reset i2c\n");
    131      1.11  jmcneill 		return;
    132      1.11  jmcneill 	}
    133       1.1  jmcneill 
    134       1.1  jmcneill 	sc->sc_dev = self;
    135       1.9  jmcneill 	sc->sc_bst = faa->faa_bst;
    136      1.11  jmcneill 	sc->sc_cid = device_unit(self);
    137       1.9  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    138       1.9  jmcneill 	if (error) {
    139  1.17.2.1  christos 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
    140  1.17.2.1  christos 		    addr, error);
    141       1.9  jmcneill 		return;
    142       1.9  jmcneill 	}
    143       1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    144       1.1  jmcneill 	cv_init(&sc->sc_cv, device_xname(self));
    145       1.1  jmcneill 
    146       1.1  jmcneill 	aprint_naive("\n");
    147      1.11  jmcneill 	aprint_normal(": I2C\n");
    148       1.1  jmcneill 
    149      1.10  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    150       1.9  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    151       1.9  jmcneill 		return;
    152       1.9  jmcneill 	}
    153       1.9  jmcneill 
    154      1.10  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
    155       1.9  jmcneill 	    FDT_INTR_MPSAFE, tegra_i2c_intr, sc);
    156       1.1  jmcneill 	if (sc->sc_ih == NULL) {
    157       1.9  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    158       1.9  jmcneill 		    intrstr);
    159       1.1  jmcneill 		return;
    160       1.1  jmcneill 	}
    161       1.9  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    162       1.1  jmcneill 
    163       1.8  jmcneill 	/*
    164       1.8  jmcneill 	 * Recommended setting for standard mode is to use an I2C source div
    165       1.8  jmcneill 	 * of 20 (Tegra K1 Technical Reference Manual, Table 137)
    166       1.8  jmcneill 	 */
    167      1.11  jmcneill 	fdtbus_reset_assert(sc->sc_rst);
    168      1.11  jmcneill 	error = clk_set_rate(sc->sc_clk, 20400000);
    169      1.11  jmcneill 	if (error) {
    170      1.11  jmcneill 		aprint_error_dev(self, "couldn't set frequency: %d\n", error);
    171      1.11  jmcneill 		return;
    172      1.11  jmcneill 	}
    173      1.11  jmcneill 	error = clk_enable(sc->sc_clk);
    174      1.11  jmcneill 	if (error) {
    175      1.11  jmcneill 		aprint_error_dev(self, "couldn't enable clock: %d\n", error);
    176      1.11  jmcneill 		return;
    177      1.11  jmcneill 	}
    178      1.11  jmcneill 	fdtbus_reset_deassert(sc->sc_rst);
    179       1.1  jmcneill 
    180       1.1  jmcneill 	tegra_i2c_init(sc);
    181       1.1  jmcneill 
    182       1.1  jmcneill 	sc->sc_ic.ic_cookie = sc;
    183       1.1  jmcneill 	sc->sc_ic.ic_acquire_bus = tegra_i2c_acquire_bus;
    184       1.1  jmcneill 	sc->sc_ic.ic_release_bus = tegra_i2c_release_bus;
    185       1.1  jmcneill 	sc->sc_ic.ic_exec = tegra_i2c_exec;
    186       1.1  jmcneill 
    187      1.10  jmcneill 	fdtbus_register_i2c_controller(self, phandle, &tegra_i2c_funcs);
    188       1.9  jmcneill 
    189  1.17.2.1  christos 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
    190       1.1  jmcneill }
    191       1.1  jmcneill 
    192       1.9  jmcneill static i2c_tag_t
    193       1.9  jmcneill tegra_i2c_get_tag(device_t dev)
    194       1.9  jmcneill {
    195       1.9  jmcneill 	struct tegra_i2c_softc * const sc = device_private(dev);
    196       1.9  jmcneill 
    197       1.9  jmcneill 	return &sc->sc_ic;
    198       1.9  jmcneill }
    199       1.9  jmcneill 
    200       1.1  jmcneill static void
    201       1.1  jmcneill tegra_i2c_init(struct tegra_i2c_softc *sc)
    202       1.1  jmcneill {
    203       1.4  jmcneill 	int retry = 10000;
    204       1.4  jmcneill 
    205       1.1  jmcneill 	I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
    206       1.1  jmcneill 	    __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
    207       1.1  jmcneill 	    __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
    208       1.1  jmcneill 
    209       1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    210       1.2  jmcneill 	I2C_WRITE(sc, I2C_CNFG_REG,
    211       1.2  jmcneill 	    I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN);
    212       1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
    213       1.4  jmcneill 	I2C_WRITE(sc, I2C_FIFO_CONTROL_REG,
    214       1.4  jmcneill 	    __SHIFTIN(7, I2C_FIFO_CONTROL_TX_FIFO_TRIG) |
    215       1.4  jmcneill 	    __SHIFTIN(0, I2C_FIFO_CONTROL_RX_FIFO_TRIG));
    216       1.4  jmcneill 
    217       1.3  jmcneill 	I2C_WRITE(sc, I2C_BUS_CONFIG_LOAD_REG,
    218       1.3  jmcneill 	    I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD);
    219       1.4  jmcneill 	while (--retry > 0) {
    220       1.4  jmcneill 		if (I2C_READ(sc, I2C_BUS_CONFIG_LOAD_REG) == 0)
    221       1.4  jmcneill 			break;
    222       1.4  jmcneill 		delay(10);
    223       1.4  jmcneill 	}
    224       1.4  jmcneill 	if (retry == 0) {
    225       1.4  jmcneill 		device_printf(sc->sc_dev, "config load timeout\n");
    226       1.4  jmcneill 	}
    227       1.1  jmcneill }
    228       1.1  jmcneill 
    229       1.1  jmcneill static int
    230       1.1  jmcneill tegra_i2c_intr(void *priv)
    231       1.1  jmcneill {
    232       1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    233       1.1  jmcneill 
    234       1.1  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    235       1.1  jmcneill 	if (istatus == 0)
    236       1.1  jmcneill 		return 0;
    237       1.1  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    238       1.1  jmcneill 
    239       1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    240       1.1  jmcneill 	cv_broadcast(&sc->sc_cv);
    241       1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    242       1.1  jmcneill 
    243       1.1  jmcneill 	return 1;
    244       1.1  jmcneill }
    245       1.1  jmcneill 
    246       1.1  jmcneill static int
    247       1.1  jmcneill tegra_i2c_acquire_bus(void *priv, int flags)
    248       1.1  jmcneill {
    249       1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    250       1.1  jmcneill 
    251       1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    252       1.1  jmcneill 
    253       1.1  jmcneill 	return 0;
    254       1.1  jmcneill }
    255       1.1  jmcneill 
    256       1.1  jmcneill static void
    257       1.1  jmcneill tegra_i2c_release_bus(void *priv, int flags)
    258       1.1  jmcneill {
    259       1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    260       1.1  jmcneill 
    261       1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    262       1.1  jmcneill }
    263       1.1  jmcneill 
    264       1.1  jmcneill static int
    265       1.1  jmcneill tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    266       1.1  jmcneill     size_t cmdlen, void *buf, size_t buflen, int flags)
    267       1.1  jmcneill {
    268       1.1  jmcneill 	struct tegra_i2c_softc * const sc = priv;
    269       1.1  jmcneill 	int retry, error;
    270       1.1  jmcneill 
    271       1.1  jmcneill #if notyet
    272       1.1  jmcneill 	if (cold)
    273       1.1  jmcneill #endif
    274       1.1  jmcneill 		flags |= I2C_F_POLL;
    275       1.1  jmcneill 
    276       1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    277       1.1  jmcneill 
    278  1.17.2.1  christos 	if (buflen == 0 && cmdlen == 0)
    279  1.17.2.1  christos 		return EINVAL;
    280  1.17.2.1  christos 
    281       1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    282       1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
    283       1.1  jmcneill 		    I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
    284       1.1  jmcneill 		    I2C_INTERRUPT_MASK_TIMEOUT |
    285       1.1  jmcneill 		    I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
    286       1.1  jmcneill 	}
    287       1.1  jmcneill 
    288       1.1  jmcneill 	const uint32_t flush_mask =
    289       1.1  jmcneill 	    I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
    290       1.1  jmcneill 
    291       1.1  jmcneill 	I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
    292       1.1  jmcneill 	for (retry = 10000; retry > 0; retry--) {
    293       1.1  jmcneill 		const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
    294       1.1  jmcneill 		if ((v & flush_mask) == 0)
    295       1.1  jmcneill 			break;
    296       1.1  jmcneill 		delay(1);
    297       1.1  jmcneill 	}
    298       1.1  jmcneill 	if (retry == 0) {
    299       1.1  jmcneill 		device_printf(sc->sc_dev, "timeout flushing FIFO\n");
    300       1.1  jmcneill 		return EIO;
    301       1.1  jmcneill 	}
    302       1.1  jmcneill 
    303       1.1  jmcneill 	if (cmdlen > 0) {
    304       1.7  jmcneill 		error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags,
    305      1.15  jakllsch 		    buflen > 0 ? true : false);
    306       1.1  jmcneill 		if (error) {
    307       1.1  jmcneill 			goto done;
    308       1.1  jmcneill 		}
    309       1.1  jmcneill 	}
    310       1.1  jmcneill 
    311  1.17.2.1  christos 	if (buflen > 0) {
    312  1.17.2.1  christos 		if (I2C_OP_READ_P(op)) {
    313  1.17.2.1  christos 			error = tegra_i2c_read(sc, addr, buf, buflen, flags);
    314  1.17.2.1  christos 		} else {
    315  1.17.2.1  christos 			error = tegra_i2c_write(sc, addr, buf, buflen, flags, false);
    316  1.17.2.1  christos 		}
    317       1.1  jmcneill 	}
    318       1.1  jmcneill 
    319       1.1  jmcneill done:
    320       1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0) {
    321       1.1  jmcneill 		I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
    322       1.1  jmcneill 	}
    323       1.3  jmcneill 
    324       1.3  jmcneill 	if (error) {
    325       1.3  jmcneill 		tegra_i2c_init(sc);
    326       1.3  jmcneill 	}
    327       1.3  jmcneill 
    328       1.1  jmcneill 	return error;
    329       1.1  jmcneill }
    330       1.1  jmcneill 
    331       1.1  jmcneill static int
    332       1.1  jmcneill tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
    333       1.1  jmcneill {
    334       1.2  jmcneill 	int error, retry;
    335       1.2  jmcneill 	uint32_t stat = 0;
    336       1.2  jmcneill 
    337       1.2  jmcneill 	retry = (flags & I2C_F_POLL) ? 100000 : 100;
    338       1.2  jmcneill 
    339       1.2  jmcneill 	while (--retry > 0) {
    340       1.1  jmcneill 		if ((flags & I2C_F_POLL) == 0) {
    341       1.1  jmcneill 			error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
    342  1.17.2.1  christos 			    uimax(mstohz(10), 1));
    343       1.1  jmcneill 			if (error) {
    344       1.1  jmcneill 				return error;
    345       1.1  jmcneill 			}
    346       1.1  jmcneill 		}
    347       1.2  jmcneill 		stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    348       1.2  jmcneill 		if (stat & I2C_INTERRUPT_STATUS_PACKET_XFER_COMPLETE) {
    349       1.1  jmcneill 			break;
    350       1.1  jmcneill 		}
    351       1.1  jmcneill 		if (flags & I2C_F_POLL) {
    352       1.2  jmcneill 			delay(10);
    353       1.1  jmcneill 		}
    354       1.1  jmcneill 	}
    355       1.2  jmcneill 	if (retry == 0) {
    356  1.17.2.1  christos #ifdef TEGRA_I2C_DEBUG
    357       1.2  jmcneill 		device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
    358  1.17.2.1  christos #endif
    359       1.2  jmcneill 		return ETIMEDOUT;
    360       1.2  jmcneill 	}
    361       1.1  jmcneill 
    362       1.2  jmcneill 	const uint32_t err_mask =
    363       1.2  jmcneill 	    I2C_INTERRUPT_STATUS_NOACK |
    364       1.2  jmcneill 	    I2C_INTERRUPT_STATUS_ARB_LOST |
    365       1.2  jmcneill 	    I2C_INTERRUPT_MASK_TIMEOUT;
    366       1.1  jmcneill 
    367       1.2  jmcneill 	if (stat & err_mask) {
    368       1.2  jmcneill 		device_printf(sc->sc_dev, "error, status = %#x\n", stat);
    369       1.1  jmcneill 		return EIO;
    370       1.2  jmcneill 	}
    371       1.1  jmcneill 
    372       1.1  jmcneill 	return 0;
    373       1.1  jmcneill }
    374       1.1  jmcneill 
    375       1.1  jmcneill static int
    376       1.1  jmcneill tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
    377       1.6  jmcneill     size_t buflen, int flags, bool repeat_start)
    378       1.1  jmcneill {
    379       1.2  jmcneill 	const uint8_t *p = buf;
    380       1.2  jmcneill 	size_t n, resid = buflen;
    381       1.2  jmcneill 	uint32_t data;
    382       1.2  jmcneill 	int retry;
    383       1.1  jmcneill 
    384       1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    385       1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    386       1.1  jmcneill 
    387       1.2  jmcneill 	/* Generic Header 0 */
    388       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    389       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    390       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    391      1.11  jmcneill 	    __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
    392       1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    393       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    394       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    395       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    396       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    397       1.2  jmcneill 	/* Generic Header 1 */
    398       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    399       1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    400       1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    401       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    402       1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE |
    403       1.6  jmcneill 	    (repeat_start ? I2C_IOPACKET_XMITHDR_REPEAT_STARTSTOP : 0) |
    404       1.2  jmcneill 	    __SHIFTIN((addr << 1), I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    405       1.2  jmcneill 
    406       1.2  jmcneill 	/* Transmit data */
    407       1.2  jmcneill 	while (resid > 0) {
    408       1.2  jmcneill 		retry = 10000;
    409       1.2  jmcneill 		while (--retry > 0) {
    410       1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    411       1.2  jmcneill 			const u_int cnt =
    412       1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT);
    413       1.2  jmcneill 			if (cnt > 0)
    414       1.2  jmcneill 				break;
    415       1.2  jmcneill 			delay(10);
    416       1.2  jmcneill 		}
    417       1.2  jmcneill 		if (retry == 0) {
    418       1.2  jmcneill 			device_printf(sc->sc_dev, "TX FIFO timeout\n");
    419       1.2  jmcneill 			return ETIMEDOUT;
    420       1.2  jmcneill 		}
    421       1.1  jmcneill 
    422  1.17.2.1  christos 		for (n = 0, data = 0; n < uimin(resid, 4); n++) {
    423       1.2  jmcneill 			data |= (uint32_t)p[n] << (n * 8);
    424       1.2  jmcneill 		}
    425       1.2  jmcneill 		I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG, data);
    426  1.17.2.1  christos 		p += uimin(resid, 4);
    427  1.17.2.1  christos 		resid -= uimin(resid, 4);
    428       1.2  jmcneill 	}
    429       1.1  jmcneill 
    430       1.1  jmcneill 	return tegra_i2c_wait(sc, flags);
    431       1.1  jmcneill }
    432       1.1  jmcneill 
    433       1.1  jmcneill static int
    434       1.1  jmcneill tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
    435       1.1  jmcneill     size_t buflen, int flags)
    436       1.1  jmcneill {
    437       1.2  jmcneill 	uint8_t *p = buf;
    438       1.2  jmcneill 	size_t n, resid = buflen;
    439       1.2  jmcneill 	uint32_t data;
    440       1.3  jmcneill 	int retry;
    441       1.2  jmcneill 
    442       1.2  jmcneill 	const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
    443       1.2  jmcneill 	I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
    444       1.1  jmcneill 
    445       1.2  jmcneill 	/* Generic Header 0 */
    446       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    447       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
    448       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTHDRSZ) |
    449      1.11  jmcneill 	    __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
    450       1.2  jmcneill 	    __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
    451       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
    452       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PROTOCOL) |
    453       1.2  jmcneill 	    __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
    454       1.2  jmcneill 		      I2C_IOPACKET_WORD0_PKTTYPE));
    455       1.2  jmcneill 	/* Generic Header 1 */
    456       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    457       1.2  jmcneill 	    __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
    458       1.2  jmcneill 	/* I2C Master Transmit Packet Header */
    459       1.2  jmcneill 	I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
    460       1.2  jmcneill 	    I2C_IOPACKET_XMITHDR_IE | I2C_IOPACKET_XMITHDR_READ |
    461       1.2  jmcneill 	    __SHIFTIN((addr << 1) | 1, I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
    462       1.1  jmcneill 
    463       1.2  jmcneill 	while (resid > 0) {
    464       1.2  jmcneill 		retry = 10000;
    465       1.2  jmcneill 		while (--retry > 0) {
    466       1.2  jmcneill 			const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
    467       1.2  jmcneill 			const u_int cnt =
    468       1.2  jmcneill 			    __SHIFTOUT(fs, I2C_FIFO_STATUS_RX_FIFO_FULL_CNT);
    469       1.2  jmcneill 			if (cnt > 0)
    470       1.2  jmcneill 				break;
    471       1.2  jmcneill 			delay(10);
    472       1.2  jmcneill 		}
    473       1.2  jmcneill 		if (retry == 0) {
    474       1.2  jmcneill 			device_printf(sc->sc_dev, "RX FIFO timeout\n");
    475       1.2  jmcneill 			return ETIMEDOUT;
    476       1.2  jmcneill 		}
    477       1.1  jmcneill 
    478       1.2  jmcneill 		data = I2C_READ(sc, I2C_RX_FIFO_REG);
    479  1.17.2.1  christos 		for (n = 0; n < uimin(resid, 4); n++) {
    480       1.2  jmcneill 			p[n] = (data >> (n * 8)) & 0xff;
    481       1.2  jmcneill 		}
    482  1.17.2.1  christos 		p += uimin(resid, 4);
    483  1.17.2.1  christos 		resid -= uimin(resid, 4);
    484       1.1  jmcneill 	}
    485       1.1  jmcneill 
    486       1.3  jmcneill 	return tegra_i2c_wait(sc, flags);
    487       1.1  jmcneill }
    488