sunxi_rsb.c revision 1.1 1 1.1 jmcneill /* $NetBSD: sunxi_rsb.c,v 1.1 2017/07/02 18:06:45 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014-2017 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 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_rsb.c,v 1.1 2017/07/02 18:06:45 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 #include <sys/mutex.h>
39 1.1 jmcneill #include <sys/condvar.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 #include <arm/sunxi/sunxi_rsb.h>
46 1.1 jmcneill
47 1.1 jmcneill enum sunxi_rsb_type {
48 1.1 jmcneill SUNXI_P2WI,
49 1.1 jmcneill SUNXI_RSB,
50 1.1 jmcneill };
51 1.1 jmcneill
52 1.1 jmcneill static const struct of_compat_data compat_data[] = {
53 1.1 jmcneill { "allwinner,sun6i-a31-p2wi", SUNXI_P2WI },
54 1.1 jmcneill { "allwinner,sun8i-a23-rsb", SUNXI_RSB },
55 1.1 jmcneill { NULL }
56 1.1 jmcneill };
57 1.1 jmcneill
58 1.1 jmcneill #define RSB_ADDR_PMIC_PRIMARY 0x3a3
59 1.1 jmcneill #define RSB_ADDR_PMIC_SECONDARY 0x745
60 1.1 jmcneill #define RSB_ADDR_PERIPH_IC 0xe89
61 1.1 jmcneill
62 1.1 jmcneill /*
63 1.1 jmcneill * Device address to Run-time address mappings.
64 1.1 jmcneill *
65 1.1 jmcneill * Run-time address (RTA) is an 8-bit value used to address the device during
66 1.1 jmcneill * a read or write transaction. The following are valid RTAs:
67 1.1 jmcneill * 0x17 0x2d 0x3a 0x4e 0x59 0x63 0x74 0x8b 0x9c 0xa6 0xb1 0xc5 0xd2 0xe8 0xff
68 1.1 jmcneill *
69 1.1 jmcneill * Allwinner uses RTA 0x2d for the primary PMIC, 0x3a for the secondary PMIC,
70 1.1 jmcneill * and 0x4e for the peripheral IC (where applicable).
71 1.1 jmcneill */
72 1.1 jmcneill static const struct {
73 1.1 jmcneill uint16_t addr;
74 1.1 jmcneill uint8_t rta;
75 1.1 jmcneill } rsb_rtamap[] = {
76 1.1 jmcneill { .addr = RSB_ADDR_PMIC_PRIMARY, .rta = 0x2d },
77 1.1 jmcneill { .addr = RSB_ADDR_PMIC_SECONDARY, .rta = 0x3a },
78 1.1 jmcneill { .addr = RSB_ADDR_PERIPH_IC, .rta = 0x4e },
79 1.1 jmcneill { .addr = 0, .rta = 0 }
80 1.1 jmcneill };
81 1.1 jmcneill
82 1.1 jmcneill struct sunxi_rsb_softc {
83 1.1 jmcneill device_t sc_dev;
84 1.1 jmcneill bus_space_tag_t sc_bst;
85 1.1 jmcneill bus_space_handle_t sc_bsh;
86 1.1 jmcneill enum sunxi_rsb_type sc_type;
87 1.1 jmcneill struct i2c_controller sc_ic;
88 1.1 jmcneill kmutex_t sc_lock;
89 1.1 jmcneill kcondvar_t sc_cv;
90 1.1 jmcneill device_t sc_i2cdev;
91 1.1 jmcneill void *sc_ih;
92 1.1 jmcneill uint32_t sc_stat;
93 1.1 jmcneill
94 1.1 jmcneill uint16_t sc_rsb_last_da;
95 1.1 jmcneill };
96 1.1 jmcneill
97 1.1 jmcneill #define RSB_READ(sc, reg) \
98 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
99 1.1 jmcneill #define RSB_WRITE(sc, reg, val) \
100 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
101 1.1 jmcneill
102 1.1 jmcneill static int sunxi_rsb_acquire_bus(void *, int);
103 1.1 jmcneill static void sunxi_rsb_release_bus(void *, int);
104 1.1 jmcneill static int sunxi_rsb_exec(void *, i2c_op_t, i2c_addr_t, const void *,
105 1.1 jmcneill size_t, void *, size_t, int);
106 1.1 jmcneill
107 1.1 jmcneill static int sunxi_rsb_intr(void *);
108 1.1 jmcneill static int sunxi_rsb_wait(struct sunxi_rsb_softc *, int);
109 1.1 jmcneill static int sunxi_rsb_rsb_config(struct sunxi_rsb_softc *,
110 1.1 jmcneill uint8_t, i2c_addr_t, int);
111 1.1 jmcneill
112 1.1 jmcneill static int sunxi_rsb_match(device_t, cfdata_t, void *);
113 1.1 jmcneill static void sunxi_rsb_attach(device_t, device_t, void *);
114 1.1 jmcneill
115 1.1 jmcneill static i2c_tag_t
116 1.1 jmcneill sunxi_rsb_get_tag(device_t dev)
117 1.1 jmcneill {
118 1.1 jmcneill struct sunxi_rsb_softc * const sc = device_private(dev);
119 1.1 jmcneill
120 1.1 jmcneill return &sc->sc_ic;
121 1.1 jmcneill }
122 1.1 jmcneill
123 1.1 jmcneill static const struct fdtbus_i2c_controller_func sunxi_rsb_funcs = {
124 1.1 jmcneill .get_tag = sunxi_rsb_get_tag,
125 1.1 jmcneill };
126 1.1 jmcneill
127 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_rsb, sizeof(struct sunxi_rsb_softc),
128 1.1 jmcneill sunxi_rsb_match, sunxi_rsb_attach, NULL, NULL);
129 1.1 jmcneill
130 1.1 jmcneill static int
131 1.1 jmcneill sunxi_rsb_match(device_t parent, cfdata_t cf, void *aux)
132 1.1 jmcneill {
133 1.1 jmcneill struct fdt_attach_args * const faa = aux;
134 1.1 jmcneill
135 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
136 1.1 jmcneill }
137 1.1 jmcneill
138 1.1 jmcneill static void
139 1.1 jmcneill sunxi_rsb_attach(device_t parent, device_t self, void *aux)
140 1.1 jmcneill {
141 1.1 jmcneill struct sunxi_rsb_softc * const sc = device_private(self);
142 1.1 jmcneill struct fdt_attach_args * const faa = aux;
143 1.1 jmcneill const int phandle = faa->faa_phandle;
144 1.1 jmcneill struct i2cbus_attach_args iba;
145 1.1 jmcneill prop_dictionary_t devs;
146 1.1 jmcneill uint32_t address_cells;
147 1.1 jmcneill struct fdtbus_reset *rst;
148 1.1 jmcneill struct clk *clk;
149 1.1 jmcneill char intrstr[128];
150 1.1 jmcneill bus_addr_t addr;
151 1.1 jmcneill bus_size_t size;
152 1.1 jmcneill
153 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
154 1.1 jmcneill aprint_error(": couldn't get registers\n");
155 1.1 jmcneill return;
156 1.1 jmcneill }
157 1.1 jmcneill
158 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
159 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
160 1.1 jmcneill return;
161 1.1 jmcneill }
162 1.1 jmcneill
163 1.1 jmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
164 1.1 jmcneill if (clk_enable(clk) != 0) {
165 1.1 jmcneill aprint_error(": couldn't enable clock\n");
166 1.1 jmcneill return;
167 1.1 jmcneill }
168 1.1 jmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
169 1.1 jmcneill if (fdtbus_reset_deassert(rst) != 0) {
170 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
171 1.1 jmcneill return;
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill sc->sc_dev = self;
175 1.1 jmcneill sc->sc_type = of_search_compatible(phandle, compat_data)->data;
176 1.1 jmcneill sc->sc_bst = faa->faa_bst;
177 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
178 1.1 jmcneill aprint_error(": couldn't map registers\n");
179 1.1 jmcneill return;
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
183 1.1 jmcneill cv_init(&sc->sc_cv, "awinp2wi");
184 1.1 jmcneill
185 1.1 jmcneill aprint_naive("\n");
186 1.1 jmcneill aprint_normal(": %s\n", sc->sc_type == SUNXI_P2WI ? "P2WI" : "RSB");
187 1.1 jmcneill
188 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
189 1.1 jmcneill sunxi_rsb_intr, sc);
190 1.1 jmcneill if (sc->sc_ih == NULL) {
191 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
192 1.1 jmcneill intrstr);
193 1.1 jmcneill return;
194 1.1 jmcneill }
195 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
196 1.1 jmcneill
197 1.1 jmcneill /* Enable interrupts */
198 1.1 jmcneill RSB_WRITE(sc, RSB_INTE_REG,
199 1.1 jmcneill RSB_INTE_LOAD_BSY_ENB |
200 1.1 jmcneill RSB_INTE_TRANS_ERR_ENB |
201 1.1 jmcneill RSB_INTE_TRANS_OVER_ENB);
202 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
203 1.1 jmcneill RSB_CTRL_GLOBAL_INT_ENB);
204 1.1 jmcneill
205 1.1 jmcneill sc->sc_ic.ic_cookie = sc;
206 1.1 jmcneill sc->sc_ic.ic_acquire_bus = sunxi_rsb_acquire_bus;
207 1.1 jmcneill sc->sc_ic.ic_release_bus = sunxi_rsb_release_bus;
208 1.1 jmcneill sc->sc_ic.ic_exec = sunxi_rsb_exec;
209 1.1 jmcneill
210 1.1 jmcneill fdtbus_register_i2c_controller(self, phandle, &sunxi_rsb_funcs);
211 1.1 jmcneill
212 1.1 jmcneill devs = prop_dictionary_create();
213 1.1 jmcneill if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
214 1.1 jmcneill address_cells = 1;
215 1.1 jmcneill
216 1.1 jmcneill of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
217 1.1 jmcneill
218 1.1 jmcneill memset(&iba, 0, sizeof(iba));
219 1.1 jmcneill iba.iba_tag = &sc->sc_ic;
220 1.1 jmcneill iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
221 1.1 jmcneill if (iba.iba_child_devices)
222 1.1 jmcneill prop_object_retain(iba.iba_child_devices);
223 1.1 jmcneill else
224 1.1 jmcneill iba.iba_child_devices = prop_array_create();
225 1.1 jmcneill prop_object_release(devs);
226 1.1 jmcneill
227 1.1 jmcneill sc->sc_i2cdev = config_found_ia(self, "i2cbus", &iba, iicbus_print);
228 1.1 jmcneill }
229 1.1 jmcneill
230 1.1 jmcneill static int
231 1.1 jmcneill sunxi_rsb_intr(void *priv)
232 1.1 jmcneill {
233 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
234 1.1 jmcneill uint32_t stat;
235 1.1 jmcneill
236 1.1 jmcneill stat = RSB_READ(sc, RSB_STAT_REG);
237 1.1 jmcneill if ((stat & RSB_STAT_MASK) == 0)
238 1.1 jmcneill return 0;
239 1.1 jmcneill
240 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG, stat & RSB_STAT_MASK);
241 1.1 jmcneill
242 1.1 jmcneill mutex_enter(&sc->sc_lock);
243 1.1 jmcneill sc->sc_stat |= stat;
244 1.1 jmcneill cv_broadcast(&sc->sc_cv);
245 1.1 jmcneill mutex_exit(&sc->sc_lock);
246 1.1 jmcneill
247 1.1 jmcneill return 1;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill static int
251 1.1 jmcneill sunxi_rsb_wait(struct sunxi_rsb_softc *sc, int flags)
252 1.1 jmcneill {
253 1.1 jmcneill int error = 0, retry;
254 1.1 jmcneill
255 1.1 jmcneill /* Wait up to 5 seconds for a transfer to complete */
256 1.1 jmcneill sc->sc_stat = 0;
257 1.1 jmcneill for (retry = (flags & I2C_F_POLL) ? 100 : 5; retry > 0; retry--) {
258 1.1 jmcneill if (flags & I2C_F_POLL) {
259 1.1 jmcneill sc->sc_stat |= RSB_READ(sc, RSB_STAT_REG);
260 1.1 jmcneill } else {
261 1.1 jmcneill error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, hz);
262 1.1 jmcneill if (error && error != EWOULDBLOCK) {
263 1.1 jmcneill break;
264 1.1 jmcneill }
265 1.1 jmcneill }
266 1.1 jmcneill if (sc->sc_stat & RSB_STAT_MASK) {
267 1.1 jmcneill break;
268 1.1 jmcneill }
269 1.1 jmcneill if (flags & I2C_F_POLL) {
270 1.1 jmcneill delay(10000);
271 1.1 jmcneill }
272 1.1 jmcneill }
273 1.1 jmcneill if (retry == 0)
274 1.1 jmcneill error = EAGAIN;
275 1.1 jmcneill
276 1.1 jmcneill if (flags & I2C_F_POLL) {
277 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG,
278 1.1 jmcneill sc->sc_stat & RSB_STAT_MASK);
279 1.1 jmcneill }
280 1.1 jmcneill
281 1.1 jmcneill if (error) {
282 1.1 jmcneill /* Abort transaction */
283 1.1 jmcneill device_printf(sc->sc_dev, "transfer timeout, error = %d\n",
284 1.1 jmcneill error);
285 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
286 1.1 jmcneill RSB_CTRL_ABORT_TRANS);
287 1.1 jmcneill return error;
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill if (sc->sc_stat & RSB_STAT_LOAD_BSY) {
291 1.1 jmcneill device_printf(sc->sc_dev, "transfer busy\n");
292 1.1 jmcneill return EBUSY;
293 1.1 jmcneill }
294 1.1 jmcneill if (sc->sc_stat & RSB_STAT_TRANS_ERR) {
295 1.1 jmcneill device_printf(sc->sc_dev, "transfer error, id 0x%02llx\n",
296 1.1 jmcneill __SHIFTOUT(sc->sc_stat, RSB_STAT_TRANS_ERR_ID));
297 1.1 jmcneill return EIO;
298 1.1 jmcneill }
299 1.1 jmcneill
300 1.1 jmcneill return 0;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill static int
304 1.1 jmcneill sunxi_rsb_rsb_config(struct sunxi_rsb_softc *sc, uint8_t rta, i2c_addr_t da,
305 1.1 jmcneill int flags)
306 1.1 jmcneill {
307 1.1 jmcneill uint32_t dar, ctrl;
308 1.1 jmcneill
309 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
310 1.1 jmcneill
311 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG,
312 1.1 jmcneill RSB_READ(sc, RSB_STAT_REG) & RSB_STAT_MASK);
313 1.1 jmcneill
314 1.1 jmcneill dar = __SHIFTIN(rta, RSB_DAR_RTA);
315 1.1 jmcneill dar |= __SHIFTIN(da, RSB_DAR_DA);
316 1.1 jmcneill RSB_WRITE(sc, RSB_DAR_REG, dar);
317 1.1 jmcneill RSB_WRITE(sc, RSB_CMD_REG, RSB_CMD_IDX_SRTA);
318 1.1 jmcneill
319 1.1 jmcneill /* Make sure the controller is idle */
320 1.1 jmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
321 1.1 jmcneill if (ctrl & RSB_CTRL_START_TRANS) {
322 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
323 1.1 jmcneill return EBUSY;
324 1.1 jmcneill }
325 1.1 jmcneill
326 1.1 jmcneill /* Start the transfer */
327 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
328 1.1 jmcneill ctrl | RSB_CTRL_START_TRANS);
329 1.1 jmcneill
330 1.1 jmcneill return sunxi_rsb_wait(sc, flags);
331 1.1 jmcneill }
332 1.1 jmcneill
333 1.1 jmcneill static int
334 1.1 jmcneill sunxi_rsb_acquire_bus(void *priv, int flags)
335 1.1 jmcneill {
336 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
337 1.1 jmcneill
338 1.1 jmcneill if (flags & I2C_F_POLL) {
339 1.1 jmcneill if (!mutex_tryenter(&sc->sc_lock))
340 1.1 jmcneill return EBUSY;
341 1.1 jmcneill } else {
342 1.1 jmcneill mutex_enter(&sc->sc_lock);
343 1.1 jmcneill }
344 1.1 jmcneill
345 1.1 jmcneill return 0;
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.1 jmcneill static void
349 1.1 jmcneill sunxi_rsb_release_bus(void *priv, int flags)
350 1.1 jmcneill {
351 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
352 1.1 jmcneill
353 1.1 jmcneill mutex_exit(&sc->sc_lock);
354 1.1 jmcneill }
355 1.1 jmcneill
356 1.1 jmcneill static int
357 1.1 jmcneill sunxi_rsb_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
358 1.1 jmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
359 1.1 jmcneill {
360 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
361 1.1 jmcneill uint32_t dlen, ctrl;
362 1.1 jmcneill uint8_t rta;
363 1.1 jmcneill int error, i;
364 1.1 jmcneill
365 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
366 1.1 jmcneill
367 1.1 jmcneill if (cmdlen != 1 || (len != 1 && len != 2 && len != 4))
368 1.1 jmcneill return EINVAL;
369 1.1 jmcneill
370 1.1 jmcneill if (sc->sc_type == SUNXI_RSB && sc->sc_rsb_last_da != addr) {
371 1.1 jmcneill /* Lookup run-time address for given device address */
372 1.1 jmcneill for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++)
373 1.1 jmcneill if (rsb_rtamap[i].addr == addr) {
374 1.1 jmcneill rta = rsb_rtamap[i].rta;
375 1.1 jmcneill break;
376 1.1 jmcneill }
377 1.1 jmcneill if (rta == 0) {
378 1.1 jmcneill device_printf(sc->sc_dev,
379 1.1 jmcneill "RTA not known for address %#x\n", addr);
380 1.1 jmcneill return ENXIO;
381 1.1 jmcneill }
382 1.1 jmcneill error = sunxi_rsb_rsb_config(sc, rta, addr, flags);
383 1.1 jmcneill if (error) {
384 1.1 jmcneill device_printf(sc->sc_dev,
385 1.1 jmcneill "SRTA failed, flags = %x, error = %d\n",
386 1.1 jmcneill flags, error);
387 1.1 jmcneill sc->sc_rsb_last_da = 0;
388 1.1 jmcneill return error;
389 1.1 jmcneill }
390 1.1 jmcneill
391 1.1 jmcneill sc->sc_rsb_last_da = addr;
392 1.1 jmcneill }
393 1.1 jmcneill
394 1.1 jmcneill /* Data byte register */
395 1.1 jmcneill RSB_WRITE(sc, RSB_DADDR0_REG, *(const uint8_t *)cmdbuf);
396 1.1 jmcneill
397 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
398 1.1 jmcneill uint8_t *pbuf = buf;
399 1.1 jmcneill uint32_t data;
400 1.1 jmcneill /* Write data */
401 1.1 jmcneill switch (len) {
402 1.1 jmcneill case 1:
403 1.1 jmcneill data = pbuf[0];
404 1.1 jmcneill break;
405 1.1 jmcneill case 2:
406 1.1 jmcneill data = pbuf[0] | (pbuf[1] << 8);
407 1.1 jmcneill break;
408 1.1 jmcneill case 4:
409 1.1 jmcneill data = pbuf[0] | (pbuf[1] << 8) |
410 1.1 jmcneill (pbuf[2] << 16) | (pbuf[3] << 24);
411 1.1 jmcneill break;
412 1.1 jmcneill default:
413 1.1 jmcneill return EINVAL;
414 1.1 jmcneill }
415 1.1 jmcneill RSB_WRITE(sc, RSB_DATA0_REG, data);
416 1.1 jmcneill }
417 1.1 jmcneill
418 1.1 jmcneill if (sc->sc_type == SUNXI_RSB) {
419 1.1 jmcneill uint8_t cmd;
420 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
421 1.1 jmcneill switch (len) {
422 1.1 jmcneill case 1: cmd = RSB_CMD_IDX_WR8; break;
423 1.1 jmcneill case 2: cmd = RSB_CMD_IDX_WR16; break;
424 1.1 jmcneill case 4: cmd = RSB_CMD_IDX_WR32; break;
425 1.1 jmcneill default: return EINVAL;
426 1.1 jmcneill }
427 1.1 jmcneill } else {
428 1.1 jmcneill switch (len) {
429 1.1 jmcneill case 1: cmd = RSB_CMD_IDX_RD8; break;
430 1.1 jmcneill case 2: cmd = RSB_CMD_IDX_RD16; break;
431 1.1 jmcneill case 4: cmd = RSB_CMD_IDX_RD32; break;
432 1.1 jmcneill default: return EINVAL;
433 1.1 jmcneill }
434 1.1 jmcneill }
435 1.1 jmcneill RSB_WRITE(sc, RSB_CMD_REG, cmd);
436 1.1 jmcneill }
437 1.1 jmcneill
438 1.1 jmcneill /* Program data length register; if reading, set read/write bit */
439 1.1 jmcneill dlen = __SHIFTIN(len - 1, RSB_DLEN_ACCESS_LENGTH);
440 1.1 jmcneill if (I2C_OP_READ_P(op)) {
441 1.1 jmcneill dlen |= RSB_DLEN_READ_WRITE_FLAG;
442 1.1 jmcneill }
443 1.1 jmcneill RSB_WRITE(sc, RSB_DLEN_REG, dlen);
444 1.1 jmcneill
445 1.1 jmcneill /* Make sure the controller is idle */
446 1.1 jmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
447 1.1 jmcneill if (ctrl & RSB_CTRL_START_TRANS) {
448 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
449 1.1 jmcneill return EBUSY;
450 1.1 jmcneill }
451 1.1 jmcneill
452 1.1 jmcneill /* Start the transfer */
453 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
454 1.1 jmcneill ctrl | RSB_CTRL_START_TRANS);
455 1.1 jmcneill
456 1.1 jmcneill error = sunxi_rsb_wait(sc, flags);
457 1.1 jmcneill if (error) {
458 1.1 jmcneill return error;
459 1.1 jmcneill }
460 1.1 jmcneill
461 1.1 jmcneill if (I2C_OP_READ_P(op)) {
462 1.1 jmcneill uint32_t data = RSB_READ(sc, RSB_DATA0_REG);
463 1.1 jmcneill switch (len) {
464 1.1 jmcneill case 4:
465 1.1 jmcneill *(uint32_t *)buf = data;
466 1.1 jmcneill break;
467 1.1 jmcneill case 2:
468 1.1 jmcneill *(uint16_t *)buf = data & 0xffff;
469 1.1 jmcneill break;
470 1.1 jmcneill case 1:
471 1.1 jmcneill *(uint8_t *)buf = data & 0xff;
472 1.1 jmcneill break;
473 1.1 jmcneill default:
474 1.1 jmcneill return EINVAL;
475 1.1 jmcneill }
476 1.1 jmcneill }
477 1.1 jmcneill
478 1.1 jmcneill return 0;
479 1.1 jmcneill }
480