Home | History | Annotate | Line # | Download | only in rockchip
rk_i2c.c revision 1.4.6.2
      1  1.4.6.2    martin /* $NetBSD: rk_i2c.c,v 1.4.6.2 2019/11/16 16:48:25 martin Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2018 Jared 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30      1.1  jmcneill 
     31  1.4.6.2    martin __KERNEL_RCSID(0, "$NetBSD: rk_i2c.c,v 1.4.6.2 2019/11/16 16:48:25 martin Exp $");
     32      1.1  jmcneill 
     33      1.1  jmcneill #include <sys/param.h>
     34      1.1  jmcneill #include <sys/bus.h>
     35      1.1  jmcneill #include <sys/device.h>
     36      1.1  jmcneill #include <sys/intr.h>
     37      1.1  jmcneill #include <sys/systm.h>
     38      1.1  jmcneill #include <sys/time.h>
     39      1.1  jmcneill #include <sys/kmem.h>
     40      1.1  jmcneill 
     41      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     42      1.1  jmcneill 
     43      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44      1.1  jmcneill 
     45      1.1  jmcneill #define	RKI2C_CON		0x000
     46      1.1  jmcneill #define	 RKI2C_CON_ACT2NAK	__BIT(6)
     47      1.1  jmcneill #define	 RKI2C_CON_ACK		__BIT(5)
     48      1.1  jmcneill #define	 RKI2C_CON_STOP		__BIT(4)
     49      1.1  jmcneill #define	 RKI2C_CON_START	__BIT(3)
     50      1.1  jmcneill #define	 RKI2C_CON_I2C_MODE	__BITS(2,1)
     51      1.1  jmcneill #define	  RKI2C_CON_I2C_MODE_TX		0
     52      1.1  jmcneill #define	  RKI2C_CON_I2C_MODE_RTX	1
     53      1.1  jmcneill #define	  RKI2C_CON_I2C_MODE_RX		2
     54      1.1  jmcneill #define	  RKI2C_CON_I2C_MODE_RRX	3
     55      1.1  jmcneill #define	 RKI2C_CON_I2C_EN	__BIT(0)
     56      1.1  jmcneill 
     57      1.1  jmcneill #define	RKI2C_CLKDIV		0x004
     58      1.1  jmcneill #define	 RKI2C_CLKDIV_CLKDIVH	__BITS(31,16)
     59      1.1  jmcneill #define	 RKI2C_CLKDIV_CLKDIVL	__BITS(15,0)
     60      1.1  jmcneill 
     61      1.1  jmcneill #define	RKI2C_MRXADDR		0x008
     62      1.1  jmcneill #define	 RKI2C_MRXADDR_ADDHVLD	__BIT(26)
     63      1.1  jmcneill #define	 RKI2C_MRXADDR_ADDMVLD	__BIT(25)
     64      1.1  jmcneill #define	 RKI2C_MRXADDR_ADDLVLD	__BIT(24)
     65      1.1  jmcneill #define	 RKI2C_MRXADDR_SADDR	__BITS(23,0)
     66      1.1  jmcneill 
     67      1.1  jmcneill #define	RKI2C_MRXRADDR		0x00c
     68      1.1  jmcneill #define	 RKI2C_MRXRADDR_ADDHVLD	__BIT(26)
     69      1.1  jmcneill #define	 RKI2C_MRXRADDR_ADDMVLD	__BIT(25)
     70      1.1  jmcneill #define	 RKI2C_MRXRADDR_ADDLVLD	__BIT(24)
     71      1.1  jmcneill #define	 RKI2C_MRXRADDR_SADDR	__BITS(23,0)
     72      1.1  jmcneill 
     73      1.1  jmcneill #define	RKI2C_MTXCNT		0x010
     74      1.1  jmcneill #define	 RKI2C_MTXCNT_MTXCNT	__BITS(5,0)
     75      1.1  jmcneill 
     76      1.1  jmcneill #define	RKI2C_MRXCNT		0x014
     77      1.1  jmcneill #define	 RKI2C_MRXCNT_MRXCNT	__BITS(5,0)
     78      1.1  jmcneill 
     79      1.1  jmcneill #define	RKI2C_IEN		0x018
     80      1.1  jmcneill #define	 RKI2C_IEN_NAKRCVIEN	__BIT(6)
     81      1.1  jmcneill #define	 RKI2C_IEN_STOPIEN	__BIT(5)
     82      1.1  jmcneill #define	 RKI2C_IEN_STARTIEN	__BIT(4)
     83      1.1  jmcneill #define	 RKI2C_IEN_MBRFIEN	__BIT(3)
     84      1.1  jmcneill #define	 RKI2C_IEN_MBTFIEN	__BIT(2)
     85      1.1  jmcneill #define	 RKI2C_IEN_BRFIEN	__BIT(1)
     86      1.1  jmcneill #define	 RKI2C_IEN_BTFIEN	__BIT(0)
     87      1.1  jmcneill 
     88      1.1  jmcneill #define	RKI2C_IPD		0x01c
     89      1.1  jmcneill #define	 RKI2C_IPD_NAKRCVIPD	__BIT(6)
     90      1.1  jmcneill #define	 RKI2C_IPD_STOPIPD	__BIT(5)
     91      1.1  jmcneill #define	 RKI2C_IPD_STARTIPD	__BIT(4)
     92      1.1  jmcneill #define	 RKI2C_IPD_MBRFIPD	__BIT(3)
     93      1.1  jmcneill #define	 RKI2C_IPD_MBTFIPD	__BIT(2)
     94      1.1  jmcneill #define	 RKI2C_IPD_BRFIPD	__BIT(1)
     95      1.1  jmcneill #define	 RKI2C_IPD_BTFIPD	__BIT(0)
     96      1.1  jmcneill 
     97      1.1  jmcneill #define	RKI2C_FCNT		0x020
     98      1.1  jmcneill #define	 RKI2C_FCNT_FCNT	__BITS(5,0)
     99      1.1  jmcneill 
    100      1.1  jmcneill #define	RKI2C_TXDATA(n)		(0x100 + (n) * 4)
    101      1.1  jmcneill #define	RKI2C_RXDATA(n)		(0x200 + (n) * 4)
    102      1.1  jmcneill 
    103      1.1  jmcneill static const char * const compatible[] = {
    104      1.1  jmcneill 	"rockchip,rk3399-i2c",
    105      1.1  jmcneill 	NULL
    106      1.1  jmcneill };
    107      1.1  jmcneill 
    108      1.1  jmcneill struct rk_i2c_softc {
    109      1.1  jmcneill 	device_t		sc_dev;
    110      1.1  jmcneill 	bus_space_tag_t		sc_bst;
    111      1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    112      1.1  jmcneill 	struct clk		*sc_sclk;
    113      1.1  jmcneill 	struct clk		*sc_pclk;
    114      1.1  jmcneill 
    115      1.1  jmcneill 	u_int			sc_clkfreq;
    116      1.1  jmcneill 
    117      1.1  jmcneill 	struct i2c_controller	sc_ic;
    118      1.1  jmcneill 	kmutex_t		sc_lock;
    119      1.1  jmcneill 	kcondvar_t		sc_cv;
    120      1.1  jmcneill };
    121      1.1  jmcneill 
    122      1.1  jmcneill #define	RD4(sc, reg)				\
    123      1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    124      1.1  jmcneill #define	WR4(sc, reg, val)			\
    125      1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    126      1.1  jmcneill 
    127      1.1  jmcneill static void
    128      1.1  jmcneill rk_i2c_init(struct rk_i2c_softc *sc)
    129      1.1  jmcneill {
    130      1.1  jmcneill 	int div, divl, divh;
    131      1.1  jmcneill 	u_int rate;
    132      1.1  jmcneill 
    133      1.1  jmcneill 	/*
    134      1.1  jmcneill 	 * SCL frequency is calculated by the following formula:
    135      1.1  jmcneill 	 *
    136      1.1  jmcneill 	 * SCL Divisor = 8 * (CLKDIVL + 1 + CLKDIVH + 1)
    137      1.1  jmcneill 	 * SCL = PCLK / SCLK Divisor
    138      1.1  jmcneill 	 */
    139      1.1  jmcneill 
    140      1.3  jmcneill 	rate = clk_get_rate(sc->sc_sclk);
    141      1.1  jmcneill 	div = howmany(rate, sc->sc_clkfreq * 8) - 2;
    142      1.1  jmcneill 	if (div >= 0) {
    143      1.1  jmcneill 		divl = div / 2;
    144      1.1  jmcneill 		if (div % 2 == 0)
    145      1.1  jmcneill 			divh = divl;
    146      1.1  jmcneill 		else
    147      1.1  jmcneill 			divh = howmany(div, 2);
    148      1.1  jmcneill 	} else {
    149      1.1  jmcneill 		divl = divh = 0;
    150      1.1  jmcneill 	}
    151      1.1  jmcneill 
    152      1.1  jmcneill 	WR4(sc, RKI2C_CLKDIV,
    153      1.1  jmcneill 	    __SHIFTIN(divh, RKI2C_CLKDIV_CLKDIVH) |
    154      1.1  jmcneill 	    __SHIFTIN(divl, RKI2C_CLKDIV_CLKDIVL));
    155      1.1  jmcneill 
    156      1.1  jmcneill 	/*
    157      1.1  jmcneill 	 * Disable the module until we are ready to use it.
    158      1.1  jmcneill 	 */
    159      1.1  jmcneill 	WR4(sc, RKI2C_CON, 0);
    160      1.1  jmcneill 	WR4(sc, RKI2C_IEN, 0);
    161      1.1  jmcneill 	WR4(sc, RKI2C_IPD, RD4(sc, RKI2C_IPD));
    162      1.1  jmcneill }
    163      1.1  jmcneill 
    164      1.1  jmcneill static int
    165      1.1  jmcneill rk_i2c_acquire_bus(void *priv, int flags)
    166      1.1  jmcneill {
    167      1.1  jmcneill 	struct rk_i2c_softc * const sc = priv;
    168      1.1  jmcneill 
    169      1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    170      1.1  jmcneill 
    171      1.1  jmcneill 	return 0;
    172      1.1  jmcneill }
    173      1.1  jmcneill 
    174      1.1  jmcneill static void
    175      1.1  jmcneill rk_i2c_release_bus(void *priv, int flags)
    176      1.1  jmcneill {
    177      1.1  jmcneill 	struct rk_i2c_softc * const sc = priv;
    178      1.1  jmcneill 
    179      1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    180      1.1  jmcneill }
    181      1.1  jmcneill 
    182      1.1  jmcneill static int
    183      1.1  jmcneill rk_i2c_wait(struct rk_i2c_softc *sc, uint32_t mask)
    184      1.1  jmcneill {
    185      1.1  jmcneill 	u_int timeo = 100000;
    186      1.1  jmcneill 	uint32_t val;
    187      1.1  jmcneill 
    188      1.1  jmcneill 	const uint32_t ipdmask = mask | RKI2C_IPD_NAKRCVIPD;
    189      1.1  jmcneill 	do {
    190      1.1  jmcneill 		val = RD4(sc, RKI2C_IPD);
    191      1.1  jmcneill 		if (val & ipdmask)
    192      1.1  jmcneill 			break;
    193      1.1  jmcneill 		delay(1);
    194      1.1  jmcneill 	} while (--timeo > 0);
    195      1.1  jmcneill 
    196      1.1  jmcneill 	WR4(sc, RKI2C_IPD, val & ipdmask);
    197      1.1  jmcneill 
    198      1.1  jmcneill 	if ((val & RKI2C_IPD_NAKRCVIPD) != 0)
    199      1.1  jmcneill 		return EIO;
    200      1.1  jmcneill 	if ((val & mask) != 0)
    201      1.1  jmcneill 		return 0;
    202      1.1  jmcneill 
    203      1.1  jmcneill 	return ETIMEDOUT;
    204      1.1  jmcneill }
    205      1.1  jmcneill 
    206      1.1  jmcneill static int
    207      1.1  jmcneill rk_i2c_start(struct rk_i2c_softc *sc)
    208      1.1  jmcneill {
    209      1.1  jmcneill 	uint32_t con;
    210      1.1  jmcneill 	int error;
    211      1.1  jmcneill 
    212      1.1  jmcneill 	/* Send start */
    213      1.1  jmcneill 	con = RD4(sc, RKI2C_CON);
    214      1.1  jmcneill 	con |= RKI2C_CON_START;
    215      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    216      1.1  jmcneill 
    217      1.1  jmcneill 	if ((error = rk_i2c_wait(sc, RKI2C_IPD_STARTIPD)) != 0)
    218      1.1  jmcneill 		return error;
    219      1.1  jmcneill 
    220      1.1  jmcneill 	con &= ~RKI2C_CON_START;
    221      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    222      1.1  jmcneill 
    223      1.1  jmcneill 	return 0;
    224      1.1  jmcneill }
    225      1.1  jmcneill 
    226      1.1  jmcneill static int
    227      1.1  jmcneill rk_i2c_stop(struct rk_i2c_softc *sc)
    228      1.1  jmcneill {
    229      1.1  jmcneill 	uint32_t con;
    230      1.1  jmcneill 	int error;
    231      1.1  jmcneill 
    232      1.1  jmcneill 	/* Send start */
    233      1.1  jmcneill 	con = RD4(sc, RKI2C_CON);
    234      1.1  jmcneill 	con |= RKI2C_CON_STOP;
    235      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    236      1.1  jmcneill 
    237      1.1  jmcneill 	if ((error = rk_i2c_wait(sc, RKI2C_IPD_STOPIPD)) != 0)
    238      1.1  jmcneill 		return error;
    239      1.1  jmcneill 
    240      1.1  jmcneill 	con &= ~RKI2C_CON_STOP;
    241      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    242      1.1  jmcneill 
    243      1.1  jmcneill 	return 0;
    244      1.1  jmcneill }
    245      1.1  jmcneill 
    246      1.1  jmcneill static int
    247  1.4.6.1    martin rk_i2c_write(struct rk_i2c_softc *sc, i2c_addr_t addr, const uint8_t *cmd,
    248  1.4.6.1    martin     size_t cmdlen, const uint8_t *buf, size_t buflen, int flags, bool send_start)
    249      1.1  jmcneill {
    250      1.1  jmcneill 	union {
    251      1.1  jmcneill 		uint8_t data8[32];
    252      1.1  jmcneill 		uint32_t data32[8];
    253      1.1  jmcneill 	} txdata;
    254      1.1  jmcneill 	uint32_t con;
    255      1.1  jmcneill 	u_int mode;
    256      1.1  jmcneill 	int error;
    257  1.4.6.1    martin 	size_t len;
    258      1.1  jmcneill 
    259  1.4.6.1    martin 	len = cmdlen + buflen;
    260  1.4.6.1    martin 	if (len > 31)
    261      1.1  jmcneill 		return EINVAL;
    262      1.1  jmcneill 
    263      1.1  jmcneill 	mode = RKI2C_CON_I2C_MODE_TX;
    264      1.1  jmcneill 	con = RKI2C_CON_I2C_EN | __SHIFTIN(mode, RKI2C_CON_I2C_MODE);
    265      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    266      1.1  jmcneill 
    267      1.1  jmcneill 	if (send_start && (error = rk_i2c_start(sc)) != 0)
    268      1.1  jmcneill 		return error;
    269      1.1  jmcneill 
    270      1.1  jmcneill 	/* Transmit data. Slave address goes in the lower 8 bits of TXDATA0 */
    271      1.1  jmcneill 	txdata.data8[0] = addr << 1;
    272  1.4.6.1    martin 	memcpy(&txdata.data8[1], cmd, cmdlen);
    273  1.4.6.1    martin 	memcpy(&txdata.data8[1 + cmdlen], buf, buflen);
    274      1.1  jmcneill 	bus_space_write_region_4(sc->sc_bst, sc->sc_bsh, RKI2C_TXDATA(0),
    275  1.4.6.1    martin 	    txdata.data32, howmany(len + 1, 4));
    276  1.4.6.1    martin 	WR4(sc, RKI2C_MTXCNT, __SHIFTIN(len + 1, RKI2C_MTXCNT_MTXCNT));
    277      1.1  jmcneill 
    278      1.1  jmcneill 	if ((error = rk_i2c_wait(sc, RKI2C_IPD_MBTFIPD)) != 0)
    279      1.1  jmcneill 		return error;
    280      1.1  jmcneill 
    281      1.1  jmcneill 	return 0;
    282      1.1  jmcneill }
    283      1.1  jmcneill 
    284      1.1  jmcneill static int
    285      1.1  jmcneill rk_i2c_read(struct rk_i2c_softc *sc, i2c_addr_t addr,
    286      1.1  jmcneill     const uint8_t *cmd, size_t cmdlen, uint8_t *buf,
    287  1.4.6.2    martin     size_t buflen, int flags, bool send_start, bool last_ack)
    288      1.1  jmcneill {
    289      1.1  jmcneill 	uint32_t rxdata[8];
    290      1.1  jmcneill 	uint32_t con, mrxaddr, mrxraddr;
    291      1.1  jmcneill 	u_int mode;
    292      1.1  jmcneill 	int error, n;
    293      1.1  jmcneill 
    294      1.1  jmcneill 	if (buflen > 32)
    295      1.1  jmcneill 		return EINVAL;
    296      1.1  jmcneill 	if (cmdlen > 3)
    297      1.1  jmcneill 		return EINVAL;
    298      1.1  jmcneill 
    299  1.4.6.2    martin 	mode = send_start ? RKI2C_CON_I2C_MODE_RTX : RKI2C_CON_I2C_MODE_RX;
    300  1.4.6.2    martin 	con = RKI2C_CON_I2C_EN | __SHIFTIN(mode, RKI2C_CON_I2C_MODE);
    301      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    302      1.1  jmcneill 
    303      1.1  jmcneill 	if (send_start && (error = rk_i2c_start(sc)) != 0)
    304      1.1  jmcneill 		return error;
    305      1.1  jmcneill 
    306  1.4.6.2    martin 	if (send_start) {
    307  1.4.6.2    martin 		mrxaddr = __SHIFTIN((addr << 1) | 1, RKI2C_MRXADDR_SADDR) |
    308  1.4.6.2    martin 		    RKI2C_MRXADDR_ADDLVLD;
    309  1.4.6.2    martin 		WR4(sc, RKI2C_MRXADDR, mrxaddr);
    310  1.4.6.2    martin 		for (n = 0, mrxraddr = 0; n < cmdlen; n++) {
    311  1.4.6.2    martin 			mrxraddr |= cmd[n] << (n * 8);
    312  1.4.6.2    martin 			mrxraddr |= (RKI2C_MRXRADDR_ADDLVLD << n);
    313  1.4.6.2    martin 		}
    314  1.4.6.2    martin 		WR4(sc, RKI2C_MRXRADDR, mrxraddr);
    315      1.1  jmcneill 	}
    316      1.1  jmcneill 
    317  1.4.6.2    martin 	if (last_ack) {
    318  1.4.6.2    martin 		con |= RKI2C_CON_ACK;
    319  1.4.6.2    martin 	}
    320      1.1  jmcneill 	WR4(sc, RKI2C_CON, con);
    321      1.1  jmcneill 
    322      1.1  jmcneill 	/* Receive data. Slave address goes in the lower 8 bits of MRXADDR */
    323      1.1  jmcneill 	WR4(sc, RKI2C_MRXCNT, __SHIFTIN(buflen, RKI2C_MRXCNT_MRXCNT));
    324      1.1  jmcneill 	if ((error = rk_i2c_wait(sc, RKI2C_IPD_MBRFIPD)) != 0)
    325      1.1  jmcneill 		return error;
    326      1.1  jmcneill 
    327  1.4.6.2    martin #if 0
    328      1.1  jmcneill 	bus_space_read_region_4(sc->sc_bst, sc->sc_bsh, RKI2C_RXDATA(0),
    329      1.1  jmcneill 	    rxdata, howmany(buflen, 4));
    330  1.4.6.2    martin #else
    331  1.4.6.2    martin 	for (n = 0; n < roundup(buflen, 4); n += 4)
    332  1.4.6.2    martin 		rxdata[n/4] = RD4(sc, RKI2C_RXDATA(n/4));
    333  1.4.6.2    martin #endif
    334  1.4.6.2    martin 
    335      1.1  jmcneill 	memcpy(buf, rxdata, buflen);
    336      1.1  jmcneill 
    337      1.1  jmcneill 	return 0;
    338      1.1  jmcneill }
    339      1.1  jmcneill 
    340      1.1  jmcneill static int
    341      1.1  jmcneill rk_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
    342      1.1  jmcneill     const void *cmdbuf, size_t cmdlen, void *buf, size_t buflen, int flags)
    343      1.1  jmcneill {
    344      1.1  jmcneill 	struct rk_i2c_softc * const sc = priv;
    345      1.1  jmcneill 	bool send_start = true;
    346      1.1  jmcneill 	int error;
    347      1.1  jmcneill 
    348      1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    349      1.1  jmcneill 
    350      1.1  jmcneill 	if (I2C_OP_READ_P(op)) {
    351  1.4.6.2    martin 		uint8_t *databuf = buf;
    352  1.4.6.2    martin 		while (buflen > 0) {
    353  1.4.6.2    martin 			const size_t datalen = uimin(buflen, 32);
    354  1.4.6.2    martin 			const bool last_ack = datalen == buflen;
    355  1.4.6.2    martin 			error = rk_i2c_read(sc, addr, cmdbuf, cmdlen, databuf, datalen, flags, send_start, last_ack);
    356  1.4.6.2    martin 			if (error != 0)
    357  1.4.6.2    martin 				break;
    358  1.4.6.2    martin 			databuf += datalen;
    359  1.4.6.2    martin 			buflen -= datalen;
    360  1.4.6.2    martin 			send_start = false;
    361  1.4.6.2    martin 			cmdbuf = NULL;
    362  1.4.6.2    martin 			cmdlen = 0;
    363  1.4.6.2    martin 		}
    364      1.1  jmcneill 	} else {
    365  1.4.6.1    martin 		error = rk_i2c_write(sc, addr, cmdbuf, cmdlen, buf, buflen, flags, send_start);
    366      1.1  jmcneill 	}
    367      1.1  jmcneill 
    368      1.1  jmcneill 	if (error != 0 || I2C_OP_STOP_P(op))
    369      1.1  jmcneill 		rk_i2c_stop(sc);
    370      1.1  jmcneill 
    371      1.1  jmcneill 	WR4(sc, RKI2C_CON, 0);
    372      1.1  jmcneill 	WR4(sc, RKI2C_IPD, RD4(sc, RKI2C_IPD));
    373      1.1  jmcneill 
    374      1.1  jmcneill 	return error;
    375      1.1  jmcneill }
    376      1.1  jmcneill 
    377      1.1  jmcneill static i2c_tag_t
    378      1.1  jmcneill rk_i2c_get_tag(device_t dev)
    379      1.1  jmcneill {
    380      1.1  jmcneill 	struct rk_i2c_softc * const sc = device_private(dev);
    381      1.1  jmcneill 
    382      1.1  jmcneill 	return &sc->sc_ic;
    383      1.1  jmcneill }
    384      1.1  jmcneill 
    385      1.1  jmcneill static const struct fdtbus_i2c_controller_func rk_i2c_funcs = {
    386      1.1  jmcneill 	.get_tag = rk_i2c_get_tag,
    387      1.1  jmcneill };
    388      1.1  jmcneill 
    389      1.1  jmcneill static int
    390      1.1  jmcneill rk_i2c_match(device_t parent, cfdata_t cf, void *aux)
    391      1.1  jmcneill {
    392      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    393      1.1  jmcneill 
    394      1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    395      1.1  jmcneill }
    396      1.1  jmcneill 
    397      1.1  jmcneill static void
    398      1.1  jmcneill rk_i2c_attach(device_t parent, device_t self, void *aux)
    399      1.1  jmcneill {
    400      1.1  jmcneill 	struct rk_i2c_softc * const sc = device_private(self);
    401      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    402      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    403      1.1  jmcneill 	bus_addr_t addr;
    404      1.1  jmcneill 	bus_size_t size;
    405      1.1  jmcneill 
    406      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    407      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    408      1.1  jmcneill 		return;
    409      1.1  jmcneill 	}
    410      1.1  jmcneill 
    411      1.1  jmcneill 	sc->sc_sclk = fdtbus_clock_get(phandle, "i2c");
    412      1.1  jmcneill 	if (sc->sc_sclk == NULL || clk_enable(sc->sc_sclk) != 0) {
    413      1.1  jmcneill 		aprint_error(": couldn't enable sclk\n");
    414      1.1  jmcneill 		return;
    415      1.1  jmcneill 	}
    416      1.1  jmcneill 
    417      1.1  jmcneill 	sc->sc_pclk = fdtbus_clock_get(phandle, "pclk");
    418      1.1  jmcneill 	if (sc->sc_pclk == NULL || clk_enable(sc->sc_pclk) != 0) {
    419      1.1  jmcneill 		aprint_error(": couldn't enable pclk\n");
    420      1.1  jmcneill 		return;
    421      1.1  jmcneill 	}
    422      1.1  jmcneill 
    423      1.1  jmcneill 	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_clkfreq))
    424      1.1  jmcneill 		sc->sc_clkfreq = 100000;
    425      1.1  jmcneill 
    426      1.1  jmcneill 	sc->sc_dev = self;
    427      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    428      1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    429      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    430      1.1  jmcneill 		return;
    431      1.1  jmcneill 	}
    432      1.1  jmcneill 
    433      1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
    434      1.1  jmcneill 	cv_init(&sc->sc_cv, "rkiic");
    435      1.1  jmcneill 
    436      1.1  jmcneill 	aprint_naive("\n");
    437      1.1  jmcneill 	aprint_normal(": Rockchip I2C (%u Hz)\n", sc->sc_clkfreq);
    438      1.1  jmcneill 
    439      1.4  jmcneill 	fdtbus_clock_assign(phandle);
    440      1.4  jmcneill 
    441      1.1  jmcneill 	rk_i2c_init(sc);
    442      1.1  jmcneill 
    443      1.1  jmcneill 	sc->sc_ic.ic_cookie = sc;
    444      1.1  jmcneill 	sc->sc_ic.ic_acquire_bus = rk_i2c_acquire_bus;
    445      1.1  jmcneill 	sc->sc_ic.ic_release_bus = rk_i2c_release_bus;
    446      1.1  jmcneill 	sc->sc_ic.ic_exec = rk_i2c_exec;
    447      1.1  jmcneill 
    448      1.1  jmcneill 	fdtbus_register_i2c_controller(self, phandle, &rk_i2c_funcs);
    449      1.1  jmcneill 
    450      1.1  jmcneill 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
    451      1.1  jmcneill }
    452      1.1  jmcneill 
    453      1.1  jmcneill CFATTACH_DECL_NEW(rk_i2c, sizeof(struct rk_i2c_softc),
    454      1.1  jmcneill     rk_i2c_match, rk_i2c_attach, NULL, NULL);
    455