Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc.c revision 1.12.2.2
      1  1.12.2.2    martin /*	$NetBSD: bcm2835_bsc.c,v 1.12.2.2 2020/04/08 14:07:28 martin Exp $	*/
      2       1.1  jakllsch 
      3       1.1  jakllsch /*
      4  1.12.2.2    martin  * Copyright (c) 2019 Jason R. Thorpe
      5       1.1  jakllsch  * Copyright (c) 2012 Jonathan A. Kollasch
      6       1.1  jakllsch  * All rights reserved.
      7       1.1  jakllsch  *
      8       1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      9       1.1  jakllsch  * modification, are permitted provided that the following conditions
     10       1.1  jakllsch  * are met:
     11       1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     12       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     13       1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     16       1.1  jakllsch  *
     17       1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18       1.1  jakllsch  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19       1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20       1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     21       1.1  jakllsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22       1.1  jakllsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23       1.1  jakllsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24       1.1  jakllsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25       1.1  jakllsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26       1.1  jakllsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27       1.1  jakllsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1  jakllsch  */
     29       1.1  jakllsch 
     30       1.1  jakllsch #include <sys/cdefs.h>
     31  1.12.2.2    martin __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc.c,v 1.12.2.2 2020/04/08 14:07:28 martin Exp $");
     32       1.1  jakllsch 
     33       1.1  jakllsch #include <sys/param.h>
     34       1.3     skrll #include <sys/bus.h>
     35       1.1  jakllsch #include <sys/device.h>
     36       1.8     skrll #include <sys/kernhist.h>
     37       1.3     skrll #include <sys/intr.h>
     38       1.3     skrll #include <sys/mutex.h>
     39       1.1  jakllsch #include <sys/systm.h>
     40       1.1  jakllsch 
     41       1.1  jakllsch #include <dev/i2c/i2cvar.h>
     42       1.1  jakllsch 
     43       1.1  jakllsch #include <arm/broadcom/bcm2835reg.h>
     44       1.1  jakllsch #include <arm/broadcom/bcm2835_bscreg.h>
     45  1.12.2.2    martin #include <arm/broadcom/bcm2835_bscvar.h>
     46       1.1  jakllsch 
     47  1.12.2.2    martin static void	bsciic_exec_func_idle(struct bsciic_softc * const);
     48  1.12.2.2    martin static void	bsciic_exec_func_send_addr(struct bsciic_softc * const);
     49  1.12.2.2    martin static void	bsciic_exec_func_send_cmd(struct bsciic_softc * const);
     50  1.12.2.2    martin static void	bsciic_exec_func_send_data(struct bsciic_softc * const);
     51  1.12.2.2    martin static void	bsciic_exec_func_recv_data(struct bsciic_softc * const);
     52  1.12.2.2    martin static void	bsciic_exec_func_done(struct bsciic_softc * const);
     53  1.12.2.2    martin static void	bsciic_exec_func_error(struct bsciic_softc * const);
     54  1.12.2.2    martin 
     55  1.12.2.2    martin const struct {
     56  1.12.2.2    martin 	void			(*func)(struct bsciic_softc * const);
     57  1.12.2.2    martin 	uint32_t		c_bits;
     58  1.12.2.2    martin 	uint32_t		s_bits;
     59  1.12.2.2    martin } bsciic_exec_state_data[] = {
     60  1.12.2.2    martin 	[BSC_EXEC_STATE_IDLE] = {
     61  1.12.2.2    martin 		.func = bsciic_exec_func_idle,
     62  1.12.2.2    martin 	},
     63  1.12.2.2    martin 	[BSC_EXEC_STATE_SEND_ADDR] = {
     64  1.12.2.2    martin 		.func = bsciic_exec_func_send_addr,
     65  1.12.2.2    martin 	},
     66  1.12.2.2    martin 	[BSC_EXEC_STATE_SEND_CMD] = {
     67  1.12.2.2    martin 		.func = bsciic_exec_func_send_cmd,
     68  1.12.2.2    martin 		.c_bits = BSC_C_INTT,
     69  1.12.2.2    martin 		.s_bits = BSC_S_TXW,
     70  1.12.2.2    martin 	},
     71  1.12.2.2    martin 	[BSC_EXEC_STATE_SEND_DATA] = {
     72  1.12.2.2    martin 		.func = bsciic_exec_func_send_data,
     73  1.12.2.2    martin 		.c_bits = BSC_C_INTT,
     74  1.12.2.2    martin 		.s_bits = BSC_S_TXW,
     75  1.12.2.2    martin 	},
     76  1.12.2.2    martin 	[BSC_EXEC_STATE_RECV_DATA] = {
     77  1.12.2.2    martin 		.func = bsciic_exec_func_recv_data,
     78  1.12.2.2    martin 		.c_bits = BSC_C_READ | BSC_C_INTR,
     79  1.12.2.2    martin 		.s_bits = BSC_S_RXR,
     80  1.12.2.2    martin 	},
     81  1.12.2.2    martin 	[BSC_EXEC_STATE_DONE] = {
     82  1.12.2.2    martin 		.func = bsciic_exec_func_done,
     83  1.12.2.2    martin 	},
     84  1.12.2.2    martin 	[BSC_EXEC_STATE_ERROR] = {
     85  1.12.2.2    martin 		.func = bsciic_exec_func_error,
     86  1.12.2.2    martin 	},
     87       1.1  jakllsch };
     88       1.1  jakllsch 
     89  1.12.2.2    martin int	bsciic_debug = 0;
     90       1.1  jakllsch 
     91  1.12.2.2    martin void
     92  1.12.2.2    martin bsciic_attach(struct bsciic_softc *sc)
     93  1.12.2.2    martin {
     94  1.12.2.2    martin 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
     95  1.12.2.2    martin 	cv_init(&sc->sc_intr_wait, device_xname(sc->sc_dev));
     96       1.1  jakllsch 
     97  1.12.2.2    martin 	/* clear FIFO, disable controller */
     98  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
     99  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
    100  1.12.2.2    martin 	    BSC_S_ERR | BSC_S_DONE);
    101  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
    102       1.1  jakllsch 
    103  1.12.2.2    martin 	u_int divider = howmany(sc->sc_frequency, sc->sc_clkrate);
    104  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DIV,
    105  1.12.2.2    martin 	   __SHIFTIN(divider, BSC_DIV_CDIV));
    106  1.12.2.2    martin }
    107       1.1  jakllsch 
    108  1.12.2.2    martin int
    109  1.12.2.2    martin bsciic_acquire_bus(void *v, int flags)
    110       1.3     skrll {
    111  1.12.2.2    martin 	struct bsciic_softc * const sc = v;
    112  1.12.2.2    martin 	uint32_t s __diagused;
    113       1.3     skrll 
    114  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S, BSC_S_CLKT |
    115  1.12.2.2    martin 	    BSC_S_ERR | BSC_S_DONE);
    116  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN |
    117  1.12.2.2    martin 	    BSC_C_CLEAR_CLEAR);
    118  1.12.2.2    martin 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    119  1.12.2.2    martin 	KASSERT((s & BSC_S_TA) == 0);
    120       1.3     skrll 
    121       1.3     skrll 	return 0;
    122       1.3     skrll }
    123       1.3     skrll 
    124  1.12.2.2    martin void
    125  1.12.2.2    martin bsciic_release_bus(void *v, int flags)
    126       1.1  jakllsch {
    127  1.12.2.2    martin 	struct bsciic_softc * const sc = v;
    128       1.1  jakllsch 
    129  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_CLEAR_CLEAR);
    130       1.1  jakllsch }
    131       1.1  jakllsch 
    132       1.1  jakllsch static void
    133  1.12.2.2    martin bsciic_exec_lock(struct bsciic_softc * const sc)
    134       1.1  jakllsch {
    135  1.12.2.2    martin 	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
    136  1.12.2.2    martin 		mutex_enter(&sc->sc_intr_lock);
    137       1.1  jakllsch 	}
    138  1.12.2.2    martin }
    139       1.1  jakllsch 
    140  1.12.2.2    martin static void
    141  1.12.2.2    martin bsciic_exec_unlock(struct bsciic_softc * const sc)
    142  1.12.2.2    martin {
    143  1.12.2.2    martin 	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
    144  1.12.2.2    martin 		mutex_exit(&sc->sc_intr_lock);
    145       1.6  jmcneill 	}
    146  1.12.2.2    martin }
    147       1.6  jmcneill 
    148  1.12.2.2    martin static void
    149  1.12.2.2    martin bsciic_txfill(struct bsciic_softc * const sc)
    150  1.12.2.2    martin {
    151  1.12.2.2    martin 	uint32_t s;
    152       1.1  jakllsch 
    153  1.12.2.2    martin 	while (sc->sc_bufpos != sc->sc_buflen) {
    154  1.12.2.2    martin 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    155  1.12.2.2    martin 		if ((s & BSC_S_TXD) == 0)
    156  1.12.2.2    martin 			break;
    157  1.12.2.2    martin 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO,
    158  1.12.2.2    martin 		    sc->sc_buf[sc->sc_bufpos++]);
    159       1.8     skrll 	}
    160  1.12.2.2    martin }
    161       1.1  jakllsch 
    162  1.12.2.2    martin static void
    163  1.12.2.2    martin bsciic_rxdrain(struct bsciic_softc * const sc)
    164  1.12.2.2    martin {
    165  1.12.2.2    martin 	uint32_t s;
    166       1.1  jakllsch 
    167  1.12.2.2    martin 	while (sc->sc_bufpos != sc->sc_buflen) {
    168  1.12.2.2    martin 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    169  1.12.2.2    martin 		if ((s & BSC_S_RXD) == 0)
    170  1.12.2.2    martin 			break;
    171  1.12.2.2    martin 		sc->sc_buf[sc->sc_bufpos++] =
    172  1.12.2.2    martin 		    (uint8_t)bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_FIFO);
    173       1.8     skrll 	}
    174  1.12.2.2    martin }
    175       1.1  jakllsch 
    176  1.12.2.2    martin static bsc_exec_state_t
    177  1.12.2.2    martin bsciic_next_state(struct bsciic_softc * const sc)
    178  1.12.2.2    martin {
    179  1.12.2.2    martin 	switch (sc->sc_exec_state) {
    180  1.12.2.2    martin 	case BSC_EXEC_STATE_IDLE:
    181  1.12.2.2    martin 		if (sc->sc_exec.addr > 0x7f) {
    182  1.12.2.2    martin 			return BSC_EXEC_STATE_SEND_ADDR;
    183  1.12.2.2    martin 		}
    184  1.12.2.2    martin 		/* FALLTHROUGH */
    185       1.1  jakllsch 
    186  1.12.2.2    martin 	case BSC_EXEC_STATE_SEND_ADDR:
    187  1.12.2.2    martin 		if (sc->sc_exec.cmdlen) {
    188  1.12.2.2    martin 			return BSC_EXEC_STATE_SEND_CMD;
    189  1.12.2.2    martin 		}
    190  1.12.2.2    martin 		/* FALLTHROUGH */
    191       1.8     skrll 
    192  1.12.2.2    martin 	case BSC_EXEC_STATE_SEND_CMD:
    193  1.12.2.2    martin 		if (sc->sc_exec.datalen == 0) {
    194  1.12.2.2    martin 			return BSC_EXEC_STATE_DONE;
    195  1.12.2.2    martin 		}
    196       1.1  jakllsch 
    197  1.12.2.2    martin 		if (I2C_OP_READ_P(sc->sc_exec.op)) {
    198  1.12.2.2    martin 			return BSC_EXEC_STATE_RECV_DATA;
    199  1.12.2.2    martin 		}
    200       1.8     skrll 
    201  1.12.2.2    martin 		return BSC_EXEC_STATE_SEND_DATA;
    202       1.1  jakllsch 
    203  1.12.2.2    martin 	case BSC_EXEC_STATE_SEND_DATA:
    204  1.12.2.2    martin 	case BSC_EXEC_STATE_RECV_DATA:
    205  1.12.2.2    martin 		return BSC_EXEC_STATE_DONE;
    206  1.12.2.2    martin 
    207  1.12.2.2    martin 	case BSC_EXEC_STATE_DONE:
    208  1.12.2.2    martin 	case BSC_EXEC_STATE_ERROR:
    209  1.12.2.2    martin 		return sc->sc_exec_state;
    210  1.12.2.2    martin 	}
    211       1.1  jakllsch 
    212  1.12.2.2    martin 	panic("bsciic_next_state: invalid state: %d", sc->sc_exec_state);
    213       1.1  jakllsch }
    214       1.1  jakllsch 
    215  1.12.2.2    martin #define	BSC_EXEC_PHASE_COMPLETE(sc)				\
    216  1.12.2.2    martin 	((sc)->sc_exec_state == BSC_EXEC_STATE_ERROR ||		\
    217  1.12.2.2    martin 	 (sc)->sc_bufpos == (sc)->sc_buflen)
    218       1.1  jakllsch 
    219  1.12.2.2    martin static void
    220  1.12.2.2    martin bsciic_signal(struct bsciic_softc * const sc)
    221  1.12.2.2    martin {
    222  1.12.2.2    martin 	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
    223  1.12.2.2    martin 		cv_signal(&sc->sc_intr_wait);
    224  1.12.2.2    martin 	}
    225       1.1  jakllsch }
    226       1.1  jakllsch 
    227  1.12.2.2    martin static void
    228  1.12.2.2    martin bsciic_phase_done(struct bsciic_softc * const sc)
    229       1.1  jakllsch {
    230  1.12.2.2    martin 	sc->sc_exec_state = bsciic_next_state(sc);
    231  1.12.2.2    martin 	(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
    232       1.1  jakllsch }
    233       1.1  jakllsch 
    234       1.1  jakllsch static void
    235  1.12.2.2    martin bsciic_abort(struct bsciic_softc * const sc)
    236       1.1  jakllsch {
    237  1.12.2.2    martin 	sc->sc_exec_state = BSC_EXEC_STATE_ERROR;
    238  1.12.2.2    martin 	bsciic_phase_done(sc);
    239       1.1  jakllsch }
    240       1.1  jakllsch 
    241  1.12.2.2    martin int
    242  1.12.2.2    martin bsciic_intr(void *v)
    243       1.1  jakllsch {
    244       1.1  jakllsch 	struct bsciic_softc * const sc = v;
    245  1.12.2.2    martin 	uint32_t s;
    246      1.12   thorpej 
    247  1.12.2.2    martin 	bsciic_exec_lock(sc);
    248       1.1  jakllsch 
    249  1.12.2.2    martin 	if ((sc->sc_exec.flags & I2C_F_POLL) == 0 &&
    250  1.12.2.2    martin 	    sc->sc_expecting_interrupt == false) {
    251  1.12.2.2    martin 		bsciic_exec_unlock(sc);
    252  1.12.2.2    martin 		return 0;
    253  1.12.2.2    martin 	}
    254       1.1  jakllsch 
    255       1.1  jakllsch 	s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    256  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
    257  1.12.2.2    martin 	    BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
    258       1.1  jakllsch 
    259  1.12.2.2    martin 	if (s & (BSC_S_CLKT | BSC_S_ERR)) {
    260  1.12.2.2    martin 		/*
    261  1.12.2.2    martin 		 * ERR might be a normal "probing for device" sort
    262  1.12.2.2    martin 		 * of thing, so don't complain about that one.
    263  1.12.2.2    martin 		 * Do complain about CLKT, though.
    264  1.12.2.2    martin 		 */
    265  1.12.2.2    martin 		if ((s & BSC_S_CLKT) ||
    266  1.12.2.2    martin 		    (bsciic_debug && (s & BSC_S_ERR))) {
    267  1.12.2.2    martin 			device_printf(sc->sc_dev,
    268  1.12.2.2    martin 			    "error s=0x%08x, aborting transfer\n", s);
    269  1.12.2.2    martin 		}
    270  1.12.2.2    martin 		bsciic_abort(sc);
    271  1.12.2.2    martin 		goto out;
    272  1.12.2.2    martin 	}
    273       1.1  jakllsch 
    274  1.12.2.2    martin 	if (BSC_EXEC_STATE_SENDING(sc)) {
    275  1.12.2.2    martin 		/*
    276  1.12.2.2    martin 		 * When transmitting, we need to wait for one final
    277  1.12.2.2    martin 		 * interrupt after pushing out the last of our data.
    278  1.12.2.2    martin 		 * Catch that case here and go to the next state.
    279  1.12.2.2    martin 		 */
    280  1.12.2.2    martin 		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
    281  1.12.2.2    martin 			bsciic_phase_done(sc);
    282  1.12.2.2    martin 		} else {
    283  1.12.2.2    martin 			bsciic_txfill(sc);
    284       1.1  jakllsch 		}
    285  1.12.2.2    martin 	} else if (BSC_EXEC_STATE_RECEIVING(sc)) {
    286  1.12.2.2    martin 		bsciic_rxdrain(sc);
    287  1.12.2.2    martin 		/*
    288  1.12.2.2    martin 		 * If we've received all of the data, go to the next
    289  1.12.2.2    martin 		 * state now; we might not get another interrupt.
    290  1.12.2.2    martin 		 */
    291  1.12.2.2    martin 		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
    292  1.12.2.2    martin 			bsciic_phase_done(sc);
    293       1.1  jakllsch 		}
    294  1.12.2.2    martin 	} else {
    295  1.12.2.2    martin 		device_printf(sc->sc_dev,
    296  1.12.2.2    martin 		    "unexpected interrupt: state=%d s=0x%08x\n",
    297  1.12.2.2    martin 		    sc->sc_exec_state, s);
    298  1.12.2.2    martin 		bsciic_abort(sc);
    299       1.1  jakllsch 	}
    300       1.1  jakllsch 
    301  1.12.2.2    martin  out:
    302  1.12.2.2    martin 	bsciic_exec_unlock(sc);
    303  1.12.2.2    martin 	return (1);
    304  1.12.2.2    martin }
    305  1.12.2.2    martin 
    306  1.12.2.2    martin static void
    307  1.12.2.2    martin bsciic_wait(struct bsciic_softc * const sc, const uint32_t events)
    308  1.12.2.2    martin {
    309  1.12.2.2    martin 	if ((sc->sc_exec.flags & I2C_F_POLL) == 0) {
    310  1.12.2.2    martin 		return;
    311  1.12.2.2    martin 	}
    312  1.12.2.2    martin 
    313  1.12.2.2    martin 	const uint32_t s_bits =
    314  1.12.2.2    martin 	    events | BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
    315  1.12.2.2    martin 	uint32_t s;
    316  1.12.2.2    martin 
    317  1.12.2.2    martin 	/* sc_intr_lock is not held in this case. */
    318  1.12.2.2    martin 
    319  1.12.2.2    martin 	for (;;) {
    320       1.1  jakllsch 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    321  1.12.2.2    martin 		if (s & s_bits) {
    322  1.12.2.2    martin 			(void) bsciic_intr(sc);
    323  1.12.2.2    martin 		}
    324  1.12.2.2    martin 		if (BSC_EXEC_PHASE_COMPLETE(sc)) {
    325  1.12.2.2    martin 			bsciic_phase_done(sc);
    326  1.12.2.2    martin 		}
    327  1.12.2.2    martin 		if (sc->sc_exec_state >= BSC_EXEC_STATE_DONE) {
    328  1.12.2.2    martin 			return;
    329  1.12.2.2    martin 		}
    330  1.12.2.2    martin 		delay(1);
    331  1.12.2.2    martin 	}
    332  1.12.2.2    martin }
    333       1.1  jakllsch 
    334  1.12.2.2    martin static void
    335  1.12.2.2    martin bsciic_start(struct bsciic_softc * const sc)
    336  1.12.2.2    martin {
    337       1.1  jakllsch 
    338  1.12.2.2    martin 	sc->sc_c_bits = BSC_C_I2CEN | BSC_C_INTD |
    339  1.12.2.2    martin 	    bsciic_exec_state_data[sc->sc_exec_state].c_bits;
    340       1.4  jakllsch 
    341  1.12.2.2    martin 	/* Clear the interrupt-enable bits if we're polling. */
    342  1.12.2.2    martin 	if (sc->sc_exec.flags & I2C_F_POLL) {
    343  1.12.2.2    martin 		sc->sc_c_bits &= ~(BSC_C_INTD | BSC_C_INTT | BSC_C_INTR);
    344  1.12.2.2    martin 	}
    345       1.1  jakllsch 
    346  1.12.2.2    martin 	sc->sc_expecting_interrupt =
    347  1.12.2.2    martin 	    (sc->sc_c_bits & (BSC_C_INTD | BSC_C_INTT | BSC_C_INTR)) ? true
    348  1.12.2.2    martin 								     : false;
    349  1.12.2.2    martin 
    350  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
    351  1.12.2.2    martin 	    sc->sc_c_bits | BSC_C_ST);
    352  1.12.2.2    martin 	bsciic_wait(sc, bsciic_exec_state_data[sc->sc_exec_state].s_bits);
    353  1.12.2.2    martin }
    354       1.1  jakllsch 
    355  1.12.2.2    martin static void
    356  1.12.2.2    martin bsciic_exec_func_idle(struct bsciic_softc * const sc)
    357  1.12.2.2    martin {
    358  1.12.2.2    martin 	/* We kick off a transfer by setting the slave address register. */
    359  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_A, sc->sc_exec.addr);
    360       1.1  jakllsch 
    361  1.12.2.2    martin 	/* Immediately transition to the next state. */
    362  1.12.2.2    martin 	bsciic_phase_done(sc);
    363  1.12.2.2    martin }
    364       1.1  jakllsch 
    365  1.12.2.2    martin static void
    366  1.12.2.2    martin bsciic_exec_func_send_addr(struct bsciic_softc * const sc)
    367  1.12.2.2    martin {
    368  1.12.2.2    martin 	/* XXX For 10-bit addressing; not implemented yet. */
    369  1.12.2.2    martin 	panic("bsciic_exec_func_send_addr is not supposed to be called");
    370  1.12.2.2    martin }
    371  1.12.2.2    martin 
    372  1.12.2.2    martin static void
    373  1.12.2.2    martin bsciic_exec_func_send_cmd(struct bsciic_softc * const sc)
    374  1.12.2.2    martin {
    375  1.12.2.2    martin 	sc->sc_buf = __UNCONST(sc->sc_exec.cmdbuf);
    376  1.12.2.2    martin 	sc->sc_bufpos = 0;
    377  1.12.2.2    martin 	sc->sc_buflen = sc->sc_exec.cmdlen;
    378  1.12.2.2    martin 
    379  1.12.2.2    martin 	uint32_t dlen = sc->sc_exec.cmdlen;
    380  1.12.2.2    martin 	if (! I2C_OP_READ_P(sc->sc_exec.op)) {
    381  1.12.2.2    martin 		dlen += sc->sc_exec.datalen;
    382       1.1  jakllsch 	}
    383  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    384       1.1  jakllsch 
    385  1.12.2.2    martin 	bsciic_start(sc);
    386  1.12.2.2    martin }
    387       1.1  jakllsch 
    388  1.12.2.2    martin static void
    389  1.12.2.2    martin bsciic_exec_func_send_data(struct bsciic_softc * const sc)
    390  1.12.2.2    martin {
    391  1.12.2.2    martin 	sc->sc_buf = sc->sc_exec.databuf;
    392  1.12.2.2    martin 	sc->sc_bufpos = 0;
    393  1.12.2.2    martin 	sc->sc_buflen = sc->sc_exec.datalen;
    394  1.12.2.2    martin 
    395  1.12.2.2    martin 	if (sc->sc_exec.cmdlen) {
    396  1.12.2.2    martin 		/*
    397  1.12.2.2    martin 		 * Output has already been started in this case; we just
    398  1.12.2.2    martin 		 * needed to switch buffers.
    399  1.12.2.2    martin 		 */
    400  1.12.2.2    martin 		bsciic_wait(sc, BSC_S_TXW);
    401  1.12.2.2    martin 	} else {
    402  1.12.2.2    martin 		uint32_t dlen = sc->sc_exec.datalen;
    403  1.12.2.2    martin 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    404  1.12.2.2    martin 		bsciic_start(sc);
    405  1.12.2.2    martin 	}
    406  1.12.2.2    martin }
    407       1.1  jakllsch 
    408  1.12.2.2    martin static void
    409  1.12.2.2    martin bsciic_exec_func_recv_data(struct bsciic_softc * const sc)
    410  1.12.2.2    martin {
    411  1.12.2.2    martin 	sc->sc_buf = sc->sc_exec.databuf;
    412  1.12.2.2    martin 	sc->sc_bufpos = 0;
    413  1.12.2.2    martin 	sc->sc_buflen = sc->sc_exec.datalen;
    414       1.1  jakllsch 
    415  1.12.2.2    martin 	uint32_t dlen = sc->sc_exec.datalen;
    416  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, dlen);
    417       1.1  jakllsch 
    418  1.12.2.2    martin 	bsciic_start(sc);
    419  1.12.2.2    martin }
    420       1.1  jakllsch 
    421  1.12.2.2    martin static void
    422  1.12.2.2    martin bsciic_exec_func_done(struct bsciic_softc * const sc)
    423  1.12.2.2    martin {
    424  1.12.2.2    martin 	/* We're done!  Disable interrupts. */
    425  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C, BSC_C_I2CEN);
    426  1.12.2.2    martin 	sc->sc_expecting_interrupt = false;
    427  1.12.2.2    martin 	bsciic_signal(sc);
    428  1.12.2.2    martin }
    429       1.1  jakllsch 
    430  1.12.2.2    martin static void
    431  1.12.2.2    martin bsciic_exec_func_error(struct bsciic_softc * const sc)
    432  1.12.2.2    martin {
    433  1.12.2.2    martin 	/* Clear the FIFO and disable interrupts. */
    434  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
    435  1.12.2.2    martin 	    BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
    436  1.12.2.2    martin 	sc->sc_expecting_interrupt = false;
    437  1.12.2.2    martin 	bsciic_signal(sc);
    438  1.12.2.2    martin }
    439       1.1  jakllsch 
    440  1.12.2.2    martin int
    441  1.12.2.2    martin bsciic_exec(void *v, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    442  1.12.2.2    martin     size_t cmdlen, void *databuf, size_t datalen, int flags)
    443  1.12.2.2    martin {
    444  1.12.2.2    martin 	struct bsciic_softc * const sc = v;
    445       1.1  jakllsch 
    446  1.12.2.2    martin 	/* XXX We don't do 10-bit addressing correctly yet. */
    447  1.12.2.2    martin 	if (addr > 0x7f)
    448  1.12.2.2    martin 		return (ENOTSUP);
    449  1.12.2.2    martin 
    450  1.12.2.2    martin 	/*
    451  1.12.2.2    martin 	 * The I2C middle layer has ensured that the client device has
    452  1.12.2.2    martin 	 * exclusive access to the controller.  Copy the parameters
    453  1.12.2.2    martin 	 * and start the state machine that runs through the necessary
    454  1.12.2.2    martin 	 * phases of the request.
    455  1.12.2.2    martin 	 */
    456  1.12.2.2    martin 	KASSERT(sc->sc_exec_state == BSC_EXEC_STATE_IDLE);
    457  1.12.2.2    martin 	sc->sc_exec.op = op;
    458  1.12.2.2    martin 	sc->sc_exec.addr = addr;
    459  1.12.2.2    martin 	sc->sc_exec.cmdbuf = cmdbuf;
    460  1.12.2.2    martin 	sc->sc_exec.cmdlen = cmdlen;
    461  1.12.2.2    martin 	sc->sc_exec.databuf = databuf;
    462  1.12.2.2    martin 	sc->sc_exec.datalen = datalen;
    463  1.12.2.2    martin 	sc->sc_exec.flags = flags;
    464  1.12.2.2    martin 
    465  1.12.2.2    martin 	bsciic_exec_lock(sc);
    466  1.12.2.2    martin 	(*bsciic_exec_state_data[sc->sc_exec_state].func)(sc);
    467  1.12.2.2    martin 	while (sc->sc_exec_state < BSC_EXEC_STATE_DONE) {
    468  1.12.2.2    martin 		KASSERT((flags & I2C_F_POLL) == 0);
    469  1.12.2.2    martin 		cv_wait(&sc->sc_intr_wait, &sc->sc_intr_lock);
    470  1.12.2.2    martin 	}
    471  1.12.2.2    martin 	int error = sc->sc_exec_state == BSC_EXEC_STATE_ERROR ? EIO : 0;
    472  1.12.2.2    martin 	uint32_t s;
    473  1.12.2.2    martin 	do {
    474  1.12.2.2    martin 		s = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BSC_S);
    475  1.12.2.2    martin 	} while ((s & BSC_S_TA) != 0);
    476  1.12.2.2    martin 	if (s & (BSC_S_CLKT | BSC_S_ERR)) {
    477       1.4  jakllsch 		error = EIO;
    478  1.12.2.2    martin 	}
    479  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_C,
    480  1.12.2.2    martin 	    BSC_C_I2CEN | BSC_C_CLEAR_CLEAR);
    481  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_S,
    482  1.12.2.2    martin 	    BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
    483  1.12.2.2    martin 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BSC_DLEN, 0);
    484  1.12.2.2    martin 	bsciic_exec_unlock(sc);
    485  1.12.2.2    martin 
    486  1.12.2.2    martin 	sc->sc_exec.flags = 0;
    487  1.12.2.2    martin 	sc->sc_exec_state = BSC_EXEC_STATE_IDLE;
    488  1.12.2.2    martin 	memset(&sc->sc_exec, 0, sizeof(sc->sc_exec));
    489       1.4  jakllsch 
    490       1.1  jakllsch 	return error;
    491       1.1  jakllsch }
    492