1*6e54367aSthorpej /* $NetBSD: sunxi_rsb.c,v 1.14 2021/01/27 03:10:20 thorpej Exp $ */
239a2682bSjmcneill
339a2682bSjmcneill /*-
439a2682bSjmcneill * Copyright (c) 2014-2017 Jared McNeill <jmcneill@invisible.ca>
539a2682bSjmcneill * All rights reserved.
639a2682bSjmcneill *
739a2682bSjmcneill * Redistribution and use in source and binary forms, with or without
839a2682bSjmcneill * modification, are permitted provided that the following conditions
939a2682bSjmcneill * are met:
1039a2682bSjmcneill * 1. Redistributions of source code must retain the above copyright
1139a2682bSjmcneill * notice, this list of conditions and the following disclaimer.
1239a2682bSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1339a2682bSjmcneill * notice, this list of conditions and the following disclaimer in the
1439a2682bSjmcneill * documentation and/or other materials provided with the distribution.
1539a2682bSjmcneill *
1639a2682bSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1739a2682bSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1839a2682bSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1939a2682bSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2039a2682bSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2139a2682bSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2239a2682bSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2339a2682bSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2439a2682bSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2539a2682bSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2639a2682bSjmcneill * SUCH DAMAGE.
2739a2682bSjmcneill */
2839a2682bSjmcneill
2939a2682bSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_rsb.c,v 1.14 2021/01/27 03:10:20 thorpej Exp $");
3139a2682bSjmcneill
3239a2682bSjmcneill #include <sys/param.h>
3339a2682bSjmcneill #include <sys/bus.h>
3439a2682bSjmcneill #include <sys/device.h>
3539a2682bSjmcneill #include <sys/intr.h>
3639a2682bSjmcneill #include <sys/systm.h>
3739a2682bSjmcneill #include <sys/kernel.h>
3839a2682bSjmcneill #include <sys/mutex.h>
3939a2682bSjmcneill #include <sys/condvar.h>
4039a2682bSjmcneill
4139a2682bSjmcneill #include <dev/i2c/i2cvar.h>
4239a2682bSjmcneill
4339a2682bSjmcneill #include <dev/fdt/fdtvar.h>
4439a2682bSjmcneill
4539a2682bSjmcneill #include <arm/sunxi/sunxi_rsb.h>
4639a2682bSjmcneill
4739a2682bSjmcneill enum sunxi_rsb_type {
4839a2682bSjmcneill SUNXI_P2WI,
4939a2682bSjmcneill SUNXI_RSB,
5039a2682bSjmcneill };
5139a2682bSjmcneill
52646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
53646c0f59Sthorpej { .compat = "allwinner,sun6i-a31-p2wi", .value = SUNXI_P2WI },
54646c0f59Sthorpej { .compat = "allwinner,sun8i-a23-rsb", .value = SUNXI_RSB },
55ec189949Sthorpej DEVICE_COMPAT_EOL
5639a2682bSjmcneill };
5739a2682bSjmcneill
5839a2682bSjmcneill #define RSB_ADDR_PMIC_PRIMARY 0x3a3
5939a2682bSjmcneill #define RSB_ADDR_PMIC_SECONDARY 0x745
6039a2682bSjmcneill #define RSB_ADDR_PERIPH_IC 0xe89
6139a2682bSjmcneill
6239a2682bSjmcneill /*
6339a2682bSjmcneill * Device address to Run-time address mappings.
6439a2682bSjmcneill *
6539a2682bSjmcneill * Run-time address (RTA) is an 8-bit value used to address the device during
6639a2682bSjmcneill * a read or write transaction. The following are valid RTAs:
6739a2682bSjmcneill * 0x17 0x2d 0x3a 0x4e 0x59 0x63 0x74 0x8b 0x9c 0xa6 0xb1 0xc5 0xd2 0xe8 0xff
6839a2682bSjmcneill *
6939a2682bSjmcneill * Allwinner uses RTA 0x2d for the primary PMIC, 0x3a for the secondary PMIC,
7039a2682bSjmcneill * and 0x4e for the peripheral IC (where applicable).
7139a2682bSjmcneill */
7239a2682bSjmcneill static const struct {
7339a2682bSjmcneill uint16_t addr;
7439a2682bSjmcneill uint8_t rta;
7539a2682bSjmcneill } rsb_rtamap[] = {
7639a2682bSjmcneill { .addr = RSB_ADDR_PMIC_PRIMARY, .rta = 0x2d },
7739a2682bSjmcneill { .addr = RSB_ADDR_PMIC_SECONDARY, .rta = 0x3a },
7839a2682bSjmcneill { .addr = RSB_ADDR_PERIPH_IC, .rta = 0x4e },
7939a2682bSjmcneill { .addr = 0, .rta = 0 }
8039a2682bSjmcneill };
8139a2682bSjmcneill
8239a2682bSjmcneill struct sunxi_rsb_softc {
8339a2682bSjmcneill device_t sc_dev;
8439a2682bSjmcneill bus_space_tag_t sc_bst;
8539a2682bSjmcneill bus_space_handle_t sc_bsh;
8639a2682bSjmcneill enum sunxi_rsb_type sc_type;
8739a2682bSjmcneill struct i2c_controller sc_ic;
88601e1783Sthorpej kmutex_t sc_intr_lock;
89601e1783Sthorpej kcondvar_t sc_intr_wait;
9039a2682bSjmcneill device_t sc_i2cdev;
9139a2682bSjmcneill void *sc_ih;
9239a2682bSjmcneill uint32_t sc_stat;
93cd4cb201Sjmcneill bool sc_busy;
9439a2682bSjmcneill
9539a2682bSjmcneill uint16_t sc_rsb_last_da;
9639a2682bSjmcneill };
9739a2682bSjmcneill
9839a2682bSjmcneill #define RSB_READ(sc, reg) \
9939a2682bSjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
10039a2682bSjmcneill #define RSB_WRITE(sc, reg, val) \
10139a2682bSjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
10239a2682bSjmcneill
10339a2682bSjmcneill static int sunxi_rsb_exec(void *, i2c_op_t, i2c_addr_t, const void *,
10439a2682bSjmcneill size_t, void *, size_t, int);
10539a2682bSjmcneill
10639a2682bSjmcneill static int sunxi_rsb_intr(void *);
10739a2682bSjmcneill static int sunxi_rsb_wait(struct sunxi_rsb_softc *, int);
10839a2682bSjmcneill static int sunxi_rsb_rsb_config(struct sunxi_rsb_softc *,
10939a2682bSjmcneill uint8_t, i2c_addr_t, int);
11039a2682bSjmcneill
11139a2682bSjmcneill static int sunxi_rsb_match(device_t, cfdata_t, void *);
11239a2682bSjmcneill static void sunxi_rsb_attach(device_t, device_t, void *);
11339a2682bSjmcneill
11439a2682bSjmcneill CFATTACH_DECL_NEW(sunxi_rsb, sizeof(struct sunxi_rsb_softc),
11539a2682bSjmcneill sunxi_rsb_match, sunxi_rsb_attach, NULL, NULL);
11639a2682bSjmcneill
11739a2682bSjmcneill static int
sunxi_rsb_match(device_t parent,cfdata_t cf,void * aux)11839a2682bSjmcneill sunxi_rsb_match(device_t parent, cfdata_t cf, void *aux)
11939a2682bSjmcneill {
12039a2682bSjmcneill struct fdt_attach_args * const faa = aux;
12139a2682bSjmcneill
122*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
12339a2682bSjmcneill }
12439a2682bSjmcneill
12539a2682bSjmcneill static void
sunxi_rsb_attach(device_t parent,device_t self,void * aux)12639a2682bSjmcneill sunxi_rsb_attach(device_t parent, device_t self, void *aux)
12739a2682bSjmcneill {
12839a2682bSjmcneill struct sunxi_rsb_softc * const sc = device_private(self);
12939a2682bSjmcneill struct fdt_attach_args * const faa = aux;
13039a2682bSjmcneill const int phandle = faa->faa_phandle;
13139a2682bSjmcneill struct fdtbus_reset *rst;
13239a2682bSjmcneill struct clk *clk;
13339a2682bSjmcneill char intrstr[128];
13439a2682bSjmcneill bus_addr_t addr;
13539a2682bSjmcneill bus_size_t size;
13639a2682bSjmcneill
13739a2682bSjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
13839a2682bSjmcneill aprint_error(": couldn't get registers\n");
13939a2682bSjmcneill return;
14039a2682bSjmcneill }
14139a2682bSjmcneill
14239a2682bSjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
14339a2682bSjmcneill aprint_error(": couldn't decode interrupt\n");
14439a2682bSjmcneill return;
14539a2682bSjmcneill }
14639a2682bSjmcneill
14739a2682bSjmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
14839a2682bSjmcneill if (clk_enable(clk) != 0) {
14939a2682bSjmcneill aprint_error(": couldn't enable clock\n");
15039a2682bSjmcneill return;
15139a2682bSjmcneill }
15239a2682bSjmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
15339a2682bSjmcneill if (fdtbus_reset_deassert(rst) != 0) {
15439a2682bSjmcneill aprint_error(": couldn't de-assert reset\n");
15539a2682bSjmcneill return;
15639a2682bSjmcneill }
15739a2682bSjmcneill
15839a2682bSjmcneill sc->sc_dev = self;
159*6e54367aSthorpej sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
16039a2682bSjmcneill sc->sc_bst = faa->faa_bst;
16139a2682bSjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
16239a2682bSjmcneill aprint_error(": couldn't map registers\n");
16339a2682bSjmcneill return;
16439a2682bSjmcneill }
16539a2682bSjmcneill
166601e1783Sthorpej mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
167601e1783Sthorpej cv_init(&sc->sc_intr_wait, "sunxirsb");
16839a2682bSjmcneill
16939a2682bSjmcneill aprint_naive("\n");
17039a2682bSjmcneill aprint_normal(": %s\n", sc->sc_type == SUNXI_P2WI ? "P2WI" : "RSB");
17139a2682bSjmcneill
172076a1169Sjmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
173076a1169Sjmcneill sunxi_rsb_intr, sc, device_xname(self));
17439a2682bSjmcneill if (sc->sc_ih == NULL) {
17539a2682bSjmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
17639a2682bSjmcneill intrstr);
17739a2682bSjmcneill return;
17839a2682bSjmcneill }
17939a2682bSjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
18039a2682bSjmcneill
181601e1783Sthorpej iic_tag_init(&sc->sc_ic);
18239a2682bSjmcneill sc->sc_ic.ic_cookie = sc;
18339a2682bSjmcneill sc->sc_ic.ic_exec = sunxi_rsb_exec;
18439a2682bSjmcneill
18521b71bc0Sthorpej fdtbus_register_i2c_controller(&sc->sc_ic, phandle);
18639a2682bSjmcneill
1877d854132Sjmcneill fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
18839a2682bSjmcneill }
18939a2682bSjmcneill
19039a2682bSjmcneill static int
sunxi_rsb_intr(void * priv)19139a2682bSjmcneill sunxi_rsb_intr(void *priv)
19239a2682bSjmcneill {
19339a2682bSjmcneill struct sunxi_rsb_softc *sc = priv;
19439a2682bSjmcneill uint32_t stat;
19539a2682bSjmcneill
19639a2682bSjmcneill stat = RSB_READ(sc, RSB_STAT_REG);
19739a2682bSjmcneill if ((stat & RSB_STAT_MASK) == 0)
19839a2682bSjmcneill return 0;
19939a2682bSjmcneill
20039a2682bSjmcneill RSB_WRITE(sc, RSB_STAT_REG, stat & RSB_STAT_MASK);
20139a2682bSjmcneill
202601e1783Sthorpej mutex_enter(&sc->sc_intr_lock);
20339a2682bSjmcneill sc->sc_stat |= stat;
204601e1783Sthorpej cv_broadcast(&sc->sc_intr_wait);
205601e1783Sthorpej mutex_exit(&sc->sc_intr_lock);
20639a2682bSjmcneill
20739a2682bSjmcneill return 1;
20839a2682bSjmcneill }
20939a2682bSjmcneill
21039a2682bSjmcneill static int
sunxi_rsb_soft_reset(struct sunxi_rsb_softc * sc)211e6c44b7cSjmcneill sunxi_rsb_soft_reset(struct sunxi_rsb_softc *sc)
212e6c44b7cSjmcneill {
213e6c44b7cSjmcneill int retry = 1000;
214e6c44b7cSjmcneill
215e6c44b7cSjmcneill RSB_WRITE(sc, RSB_CTRL_REG, RSB_CTRL_SOFT_RESET);
216e6c44b7cSjmcneill while (--retry > 0) {
217e6c44b7cSjmcneill if ((RSB_READ(sc, RSB_CTRL_REG) & RSB_CTRL_SOFT_RESET) == 0)
218e6c44b7cSjmcneill break;
219e6c44b7cSjmcneill delay(10);
220e6c44b7cSjmcneill }
221e6c44b7cSjmcneill if (retry == 0)
222e6c44b7cSjmcneill return EIO;
223e6c44b7cSjmcneill
224e6c44b7cSjmcneill return 0;
225e6c44b7cSjmcneill }
226e6c44b7cSjmcneill
227e6c44b7cSjmcneill static int
sunxi_rsb_wait(struct sunxi_rsb_softc * sc,int flags)22839a2682bSjmcneill sunxi_rsb_wait(struct sunxi_rsb_softc *sc, int flags)
22939a2682bSjmcneill {
23039a2682bSjmcneill int error = 0, retry;
23139a2682bSjmcneill
23239a2682bSjmcneill /* Wait up to 5 seconds for a transfer to complete */
23339a2682bSjmcneill sc->sc_stat = 0;
23439a2682bSjmcneill for (retry = (flags & I2C_F_POLL) ? 100 : 5; retry > 0; retry--) {
23539a2682bSjmcneill if (flags & I2C_F_POLL) {
23639a2682bSjmcneill sc->sc_stat |= RSB_READ(sc, RSB_STAT_REG);
23739a2682bSjmcneill } else {
238601e1783Sthorpej error = cv_timedwait(&sc->sc_intr_wait,
239601e1783Sthorpej &sc->sc_intr_lock, hz);
24039a2682bSjmcneill if (error && error != EWOULDBLOCK) {
24139a2682bSjmcneill break;
24239a2682bSjmcneill }
24339a2682bSjmcneill }
24439a2682bSjmcneill if (sc->sc_stat & RSB_STAT_MASK) {
24539a2682bSjmcneill break;
24639a2682bSjmcneill }
24739a2682bSjmcneill if (flags & I2C_F_POLL) {
24839a2682bSjmcneill delay(10000);
24939a2682bSjmcneill }
25039a2682bSjmcneill }
25139a2682bSjmcneill if (retry == 0)
25239a2682bSjmcneill error = EAGAIN;
25339a2682bSjmcneill
25439a2682bSjmcneill if (flags & I2C_F_POLL) {
25539a2682bSjmcneill RSB_WRITE(sc, RSB_STAT_REG,
25639a2682bSjmcneill sc->sc_stat & RSB_STAT_MASK);
25739a2682bSjmcneill }
25839a2682bSjmcneill
25939a2682bSjmcneill if (error) {
26039a2682bSjmcneill /* Abort transaction */
26139a2682bSjmcneill device_printf(sc->sc_dev, "transfer timeout, error = %d\n",
26239a2682bSjmcneill error);
26339a2682bSjmcneill RSB_WRITE(sc, RSB_CTRL_REG,
26439a2682bSjmcneill RSB_CTRL_ABORT_TRANS);
26539a2682bSjmcneill return error;
26639a2682bSjmcneill }
26739a2682bSjmcneill
26839a2682bSjmcneill if (sc->sc_stat & RSB_STAT_LOAD_BSY) {
26939a2682bSjmcneill device_printf(sc->sc_dev, "transfer busy\n");
27039a2682bSjmcneill return EBUSY;
27139a2682bSjmcneill }
27239a2682bSjmcneill if (sc->sc_stat & RSB_STAT_TRANS_ERR) {
2732c613b24Schristos device_printf(sc->sc_dev, "transfer error, id 0x%02" PRIx64
2742c613b24Schristos "\n", __SHIFTOUT(sc->sc_stat, RSB_STAT_TRANS_ERR_ID));
27539a2682bSjmcneill return EIO;
27639a2682bSjmcneill }
27739a2682bSjmcneill
27839a2682bSjmcneill return 0;
27939a2682bSjmcneill }
28039a2682bSjmcneill
28139a2682bSjmcneill static int
sunxi_rsb_rsb_config(struct sunxi_rsb_softc * sc,uint8_t rta,i2c_addr_t da,int flags)28239a2682bSjmcneill sunxi_rsb_rsb_config(struct sunxi_rsb_softc *sc, uint8_t rta, i2c_addr_t da,
28339a2682bSjmcneill int flags)
28439a2682bSjmcneill {
28539a2682bSjmcneill uint32_t dar, ctrl;
28639a2682bSjmcneill
287601e1783Sthorpej KASSERT(mutex_owned(&sc->sc_intr_lock));
28839a2682bSjmcneill
28939a2682bSjmcneill RSB_WRITE(sc, RSB_STAT_REG,
29039a2682bSjmcneill RSB_READ(sc, RSB_STAT_REG) & RSB_STAT_MASK);
29139a2682bSjmcneill
29239a2682bSjmcneill dar = __SHIFTIN(rta, RSB_DAR_RTA);
29339a2682bSjmcneill dar |= __SHIFTIN(da, RSB_DAR_DA);
29439a2682bSjmcneill RSB_WRITE(sc, RSB_DAR_REG, dar);
29539a2682bSjmcneill RSB_WRITE(sc, RSB_CMD_REG, RSB_CMD_IDX_SRTA);
29639a2682bSjmcneill
29739a2682bSjmcneill /* Make sure the controller is idle */
29839a2682bSjmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
29939a2682bSjmcneill if (ctrl & RSB_CTRL_START_TRANS) {
30039a2682bSjmcneill device_printf(sc->sc_dev, "device is busy\n");
30139a2682bSjmcneill return EBUSY;
30239a2682bSjmcneill }
30339a2682bSjmcneill
30439a2682bSjmcneill /* Start the transfer */
30539a2682bSjmcneill RSB_WRITE(sc, RSB_CTRL_REG,
30639a2682bSjmcneill ctrl | RSB_CTRL_START_TRANS);
30739a2682bSjmcneill
30839a2682bSjmcneill return sunxi_rsb_wait(sc, flags);
30939a2682bSjmcneill }
31039a2682bSjmcneill
31139a2682bSjmcneill static int
sunxi_rsb_exec(void * priv,i2c_op_t op,i2c_addr_t addr,const void * cmdbuf,size_t cmdlen,void * buf,size_t len,int flags)31239a2682bSjmcneill sunxi_rsb_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
31339a2682bSjmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
31439a2682bSjmcneill {
31539a2682bSjmcneill struct sunxi_rsb_softc *sc = priv;
31639a2682bSjmcneill uint32_t dlen, ctrl;
31739a2682bSjmcneill uint8_t rta;
31839a2682bSjmcneill int error, i;
31939a2682bSjmcneill
32039a2682bSjmcneill if (cmdlen != 1 || (len != 1 && len != 2 && len != 4))
32139a2682bSjmcneill return EINVAL;
32239a2682bSjmcneill
323601e1783Sthorpej mutex_enter(&sc->sc_intr_lock);
324601e1783Sthorpej
325e6c44b7cSjmcneill error = sunxi_rsb_soft_reset(sc);
326e6c44b7cSjmcneill if (error != 0) {
327601e1783Sthorpej mutex_exit(&sc->sc_intr_lock);
328e6c44b7cSjmcneill device_printf(sc->sc_dev, "soft reset timed out\n");
329e6c44b7cSjmcneill return error;
330e6c44b7cSjmcneill }
331e6c44b7cSjmcneill
332e6c44b7cSjmcneill if ((flags & I2C_F_POLL) == 0) {
333e6c44b7cSjmcneill /* Enable interrupts */
334e6c44b7cSjmcneill RSB_WRITE(sc, RSB_INTE_REG,
335e6c44b7cSjmcneill RSB_INTE_LOAD_BSY_ENB |
336e6c44b7cSjmcneill RSB_INTE_TRANS_ERR_ENB |
337e6c44b7cSjmcneill RSB_INTE_TRANS_OVER_ENB);
338e6c44b7cSjmcneill RSB_WRITE(sc, RSB_CTRL_REG,
339e6c44b7cSjmcneill RSB_CTRL_GLOBAL_INT_ENB);
340e6c44b7cSjmcneill }
341e6c44b7cSjmcneill
34239a2682bSjmcneill if (sc->sc_type == SUNXI_RSB && sc->sc_rsb_last_da != addr) {
34339a2682bSjmcneill /* Lookup run-time address for given device address */
34439a2682bSjmcneill for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++)
34539a2682bSjmcneill if (rsb_rtamap[i].addr == addr) {
34639a2682bSjmcneill rta = rsb_rtamap[i].rta;
34739a2682bSjmcneill break;
34839a2682bSjmcneill }
34939a2682bSjmcneill if (rta == 0) {
350601e1783Sthorpej mutex_exit(&sc->sc_intr_lock);
35139a2682bSjmcneill device_printf(sc->sc_dev,
35239a2682bSjmcneill "RTA not known for address %#x\n", addr);
35339a2682bSjmcneill return ENXIO;
35439a2682bSjmcneill }
35539a2682bSjmcneill error = sunxi_rsb_rsb_config(sc, rta, addr, flags);
35639a2682bSjmcneill if (error) {
35739a2682bSjmcneill device_printf(sc->sc_dev,
35839a2682bSjmcneill "SRTA failed, flags = %x, error = %d\n",
35939a2682bSjmcneill flags, error);
36039a2682bSjmcneill sc->sc_rsb_last_da = 0;
361e6c44b7cSjmcneill goto done;
36239a2682bSjmcneill }
36339a2682bSjmcneill
36439a2682bSjmcneill sc->sc_rsb_last_da = addr;
36539a2682bSjmcneill }
36639a2682bSjmcneill
36739a2682bSjmcneill /* Data byte register */
36839a2682bSjmcneill RSB_WRITE(sc, RSB_DADDR0_REG, *(const uint8_t *)cmdbuf);
36939a2682bSjmcneill
37039a2682bSjmcneill if (I2C_OP_WRITE_P(op)) {
37139a2682bSjmcneill uint8_t *pbuf = buf;
37239a2682bSjmcneill uint32_t data;
37339a2682bSjmcneill /* Write data */
37439a2682bSjmcneill switch (len) {
37539a2682bSjmcneill case 1:
37639a2682bSjmcneill data = pbuf[0];
37739a2682bSjmcneill break;
37839a2682bSjmcneill case 2:
37939a2682bSjmcneill data = pbuf[0] | (pbuf[1] << 8);
38039a2682bSjmcneill break;
38139a2682bSjmcneill case 4:
38239a2682bSjmcneill data = pbuf[0] | (pbuf[1] << 8) |
38339a2682bSjmcneill (pbuf[2] << 16) | (pbuf[3] << 24);
38439a2682bSjmcneill break;
38539a2682bSjmcneill default:
386e6c44b7cSjmcneill error = EINVAL;
387e6c44b7cSjmcneill goto done;
38839a2682bSjmcneill }
38939a2682bSjmcneill RSB_WRITE(sc, RSB_DATA0_REG, data);
39039a2682bSjmcneill }
39139a2682bSjmcneill
39239a2682bSjmcneill if (sc->sc_type == SUNXI_RSB) {
39339a2682bSjmcneill uint8_t cmd;
39439a2682bSjmcneill if (I2C_OP_WRITE_P(op)) {
39539a2682bSjmcneill switch (len) {
39639a2682bSjmcneill case 1: cmd = RSB_CMD_IDX_WR8; break;
39739a2682bSjmcneill case 2: cmd = RSB_CMD_IDX_WR16; break;
39839a2682bSjmcneill case 4: cmd = RSB_CMD_IDX_WR32; break;
399e6c44b7cSjmcneill default: error = EINVAL; goto done;
40039a2682bSjmcneill }
40139a2682bSjmcneill } else {
40239a2682bSjmcneill switch (len) {
40339a2682bSjmcneill case 1: cmd = RSB_CMD_IDX_RD8; break;
40439a2682bSjmcneill case 2: cmd = RSB_CMD_IDX_RD16; break;
40539a2682bSjmcneill case 4: cmd = RSB_CMD_IDX_RD32; break;
406e6c44b7cSjmcneill default: error = EINVAL; goto done;
40739a2682bSjmcneill }
40839a2682bSjmcneill }
40939a2682bSjmcneill RSB_WRITE(sc, RSB_CMD_REG, cmd);
41039a2682bSjmcneill }
41139a2682bSjmcneill
41239a2682bSjmcneill /* Program data length register; if reading, set read/write bit */
41339a2682bSjmcneill dlen = __SHIFTIN(len - 1, RSB_DLEN_ACCESS_LENGTH);
41439a2682bSjmcneill if (I2C_OP_READ_P(op)) {
41539a2682bSjmcneill dlen |= RSB_DLEN_READ_WRITE_FLAG;
41639a2682bSjmcneill }
41739a2682bSjmcneill RSB_WRITE(sc, RSB_DLEN_REG, dlen);
41839a2682bSjmcneill
41939a2682bSjmcneill /* Make sure the controller is idle */
42039a2682bSjmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
42139a2682bSjmcneill if (ctrl & RSB_CTRL_START_TRANS) {
42239a2682bSjmcneill device_printf(sc->sc_dev, "device is busy\n");
423e6c44b7cSjmcneill error = EBUSY;
424e6c44b7cSjmcneill goto done;
42539a2682bSjmcneill }
42639a2682bSjmcneill
42739a2682bSjmcneill /* Start the transfer */
42839a2682bSjmcneill RSB_WRITE(sc, RSB_CTRL_REG,
42939a2682bSjmcneill ctrl | RSB_CTRL_START_TRANS);
43039a2682bSjmcneill
43139a2682bSjmcneill error = sunxi_rsb_wait(sc, flags);
432e6c44b7cSjmcneill if (error)
433e6c44b7cSjmcneill goto done;
43439a2682bSjmcneill
43539a2682bSjmcneill if (I2C_OP_READ_P(op)) {
43639a2682bSjmcneill uint32_t data = RSB_READ(sc, RSB_DATA0_REG);
43739a2682bSjmcneill switch (len) {
43839a2682bSjmcneill case 4:
43939a2682bSjmcneill *(uint32_t *)buf = data;
44039a2682bSjmcneill break;
44139a2682bSjmcneill case 2:
44239a2682bSjmcneill *(uint16_t *)buf = data & 0xffff;
44339a2682bSjmcneill break;
44439a2682bSjmcneill case 1:
44539a2682bSjmcneill *(uint8_t *)buf = data & 0xff;
44639a2682bSjmcneill break;
44739a2682bSjmcneill default:
448e6c44b7cSjmcneill error = EINVAL;
449e6c44b7cSjmcneill goto done;
45039a2682bSjmcneill }
45139a2682bSjmcneill }
45239a2682bSjmcneill
453e6c44b7cSjmcneill error = 0;
454e6c44b7cSjmcneill
455e6c44b7cSjmcneill done:
456e6c44b7cSjmcneill RSB_WRITE(sc, RSB_CTRL_REG, 0);
457601e1783Sthorpej mutex_exit(&sc->sc_intr_lock);
458e6c44b7cSjmcneill
459e6c44b7cSjmcneill return error;
46039a2682bSjmcneill }
461