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