sunxi_rsb.c revision 1.6 1 1.6 jmcneill /* $NetBSD: sunxi_rsb.c,v 1.6 2018/11/17 19:30:51 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.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_rsb.c,v 1.6 2018/11/17 19:30:51 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.6 jmcneill bool sc_busy;
94 1.1 jmcneill
95 1.1 jmcneill uint16_t sc_rsb_last_da;
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill #define RSB_READ(sc, reg) \
99 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
100 1.1 jmcneill #define RSB_WRITE(sc, reg, val) \
101 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
102 1.1 jmcneill
103 1.1 jmcneill static int sunxi_rsb_acquire_bus(void *, int);
104 1.1 jmcneill static void sunxi_rsb_release_bus(void *, int);
105 1.1 jmcneill static int sunxi_rsb_exec(void *, i2c_op_t, i2c_addr_t, const void *,
106 1.1 jmcneill size_t, void *, size_t, int);
107 1.1 jmcneill
108 1.1 jmcneill static int sunxi_rsb_intr(void *);
109 1.1 jmcneill static int sunxi_rsb_wait(struct sunxi_rsb_softc *, int);
110 1.1 jmcneill static int sunxi_rsb_rsb_config(struct sunxi_rsb_softc *,
111 1.1 jmcneill uint8_t, i2c_addr_t, int);
112 1.1 jmcneill
113 1.1 jmcneill static int sunxi_rsb_match(device_t, cfdata_t, void *);
114 1.1 jmcneill static void sunxi_rsb_attach(device_t, device_t, void *);
115 1.1 jmcneill
116 1.1 jmcneill static i2c_tag_t
117 1.1 jmcneill sunxi_rsb_get_tag(device_t dev)
118 1.1 jmcneill {
119 1.1 jmcneill struct sunxi_rsb_softc * const sc = device_private(dev);
120 1.1 jmcneill
121 1.1 jmcneill return &sc->sc_ic;
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill static const struct fdtbus_i2c_controller_func sunxi_rsb_funcs = {
125 1.1 jmcneill .get_tag = sunxi_rsb_get_tag,
126 1.1 jmcneill };
127 1.1 jmcneill
128 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_rsb, sizeof(struct sunxi_rsb_softc),
129 1.1 jmcneill sunxi_rsb_match, sunxi_rsb_attach, NULL, NULL);
130 1.1 jmcneill
131 1.1 jmcneill static int
132 1.1 jmcneill sunxi_rsb_match(device_t parent, cfdata_t cf, void *aux)
133 1.1 jmcneill {
134 1.1 jmcneill struct fdt_attach_args * const faa = aux;
135 1.1 jmcneill
136 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
137 1.1 jmcneill }
138 1.1 jmcneill
139 1.1 jmcneill static void
140 1.1 jmcneill sunxi_rsb_attach(device_t parent, device_t self, void *aux)
141 1.1 jmcneill {
142 1.1 jmcneill struct sunxi_rsb_softc * const sc = device_private(self);
143 1.1 jmcneill struct fdt_attach_args * const faa = aux;
144 1.1 jmcneill const int phandle = faa->faa_phandle;
145 1.1 jmcneill struct fdtbus_reset *rst;
146 1.1 jmcneill struct clk *clk;
147 1.1 jmcneill char intrstr[128];
148 1.1 jmcneill bus_addr_t addr;
149 1.1 jmcneill bus_size_t size;
150 1.1 jmcneill
151 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
152 1.1 jmcneill aprint_error(": couldn't get registers\n");
153 1.1 jmcneill return;
154 1.1 jmcneill }
155 1.1 jmcneill
156 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
157 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
158 1.1 jmcneill return;
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
162 1.1 jmcneill if (clk_enable(clk) != 0) {
163 1.1 jmcneill aprint_error(": couldn't enable clock\n");
164 1.1 jmcneill return;
165 1.1 jmcneill }
166 1.1 jmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
167 1.1 jmcneill if (fdtbus_reset_deassert(rst) != 0) {
168 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
169 1.1 jmcneill return;
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill sc->sc_dev = self;
173 1.1 jmcneill sc->sc_type = of_search_compatible(phandle, compat_data)->data;
174 1.1 jmcneill sc->sc_bst = faa->faa_bst;
175 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
176 1.1 jmcneill aprint_error(": couldn't map registers\n");
177 1.1 jmcneill return;
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
181 1.1 jmcneill cv_init(&sc->sc_cv, "awinp2wi");
182 1.1 jmcneill
183 1.1 jmcneill aprint_naive("\n");
184 1.1 jmcneill aprint_normal(": %s\n", sc->sc_type == SUNXI_P2WI ? "P2WI" : "RSB");
185 1.1 jmcneill
186 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
187 1.1 jmcneill sunxi_rsb_intr, sc);
188 1.1 jmcneill if (sc->sc_ih == NULL) {
189 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
190 1.1 jmcneill intrstr);
191 1.1 jmcneill return;
192 1.1 jmcneill }
193 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
194 1.1 jmcneill
195 1.1 jmcneill sc->sc_ic.ic_cookie = sc;
196 1.1 jmcneill sc->sc_ic.ic_acquire_bus = sunxi_rsb_acquire_bus;
197 1.1 jmcneill sc->sc_ic.ic_release_bus = sunxi_rsb_release_bus;
198 1.1 jmcneill sc->sc_ic.ic_exec = sunxi_rsb_exec;
199 1.1 jmcneill
200 1.1 jmcneill fdtbus_register_i2c_controller(self, phandle, &sunxi_rsb_funcs);
201 1.1 jmcneill
202 1.3 jmcneill fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
203 1.1 jmcneill }
204 1.1 jmcneill
205 1.1 jmcneill static int
206 1.1 jmcneill sunxi_rsb_intr(void *priv)
207 1.1 jmcneill {
208 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
209 1.1 jmcneill uint32_t stat;
210 1.1 jmcneill
211 1.1 jmcneill stat = RSB_READ(sc, RSB_STAT_REG);
212 1.1 jmcneill if ((stat & RSB_STAT_MASK) == 0)
213 1.1 jmcneill return 0;
214 1.1 jmcneill
215 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG, stat & RSB_STAT_MASK);
216 1.1 jmcneill
217 1.1 jmcneill mutex_enter(&sc->sc_lock);
218 1.1 jmcneill sc->sc_stat |= stat;
219 1.1 jmcneill cv_broadcast(&sc->sc_cv);
220 1.1 jmcneill mutex_exit(&sc->sc_lock);
221 1.1 jmcneill
222 1.1 jmcneill return 1;
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill static int
226 1.4 jmcneill sunxi_rsb_soft_reset(struct sunxi_rsb_softc *sc)
227 1.4 jmcneill {
228 1.4 jmcneill int retry = 1000;
229 1.4 jmcneill
230 1.4 jmcneill RSB_WRITE(sc, RSB_CTRL_REG, RSB_CTRL_SOFT_RESET);
231 1.4 jmcneill while (--retry > 0) {
232 1.4 jmcneill if ((RSB_READ(sc, RSB_CTRL_REG) & RSB_CTRL_SOFT_RESET) == 0)
233 1.4 jmcneill break;
234 1.4 jmcneill delay(10);
235 1.4 jmcneill }
236 1.4 jmcneill if (retry == 0)
237 1.4 jmcneill return EIO;
238 1.4 jmcneill
239 1.4 jmcneill return 0;
240 1.4 jmcneill }
241 1.4 jmcneill
242 1.4 jmcneill static int
243 1.1 jmcneill sunxi_rsb_wait(struct sunxi_rsb_softc *sc, int flags)
244 1.1 jmcneill {
245 1.1 jmcneill int error = 0, retry;
246 1.1 jmcneill
247 1.1 jmcneill /* Wait up to 5 seconds for a transfer to complete */
248 1.1 jmcneill sc->sc_stat = 0;
249 1.1 jmcneill for (retry = (flags & I2C_F_POLL) ? 100 : 5; retry > 0; retry--) {
250 1.1 jmcneill if (flags & I2C_F_POLL) {
251 1.1 jmcneill sc->sc_stat |= RSB_READ(sc, RSB_STAT_REG);
252 1.1 jmcneill } else {
253 1.1 jmcneill error = cv_timedwait(&sc->sc_cv, &sc->sc_lock, hz);
254 1.1 jmcneill if (error && error != EWOULDBLOCK) {
255 1.1 jmcneill break;
256 1.1 jmcneill }
257 1.1 jmcneill }
258 1.1 jmcneill if (sc->sc_stat & RSB_STAT_MASK) {
259 1.1 jmcneill break;
260 1.1 jmcneill }
261 1.1 jmcneill if (flags & I2C_F_POLL) {
262 1.1 jmcneill delay(10000);
263 1.1 jmcneill }
264 1.1 jmcneill }
265 1.1 jmcneill if (retry == 0)
266 1.1 jmcneill error = EAGAIN;
267 1.1 jmcneill
268 1.1 jmcneill if (flags & I2C_F_POLL) {
269 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG,
270 1.1 jmcneill sc->sc_stat & RSB_STAT_MASK);
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill if (error) {
274 1.1 jmcneill /* Abort transaction */
275 1.1 jmcneill device_printf(sc->sc_dev, "transfer timeout, error = %d\n",
276 1.1 jmcneill error);
277 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
278 1.1 jmcneill RSB_CTRL_ABORT_TRANS);
279 1.1 jmcneill return error;
280 1.1 jmcneill }
281 1.1 jmcneill
282 1.1 jmcneill if (sc->sc_stat & RSB_STAT_LOAD_BSY) {
283 1.1 jmcneill device_printf(sc->sc_dev, "transfer busy\n");
284 1.1 jmcneill return EBUSY;
285 1.1 jmcneill }
286 1.1 jmcneill if (sc->sc_stat & RSB_STAT_TRANS_ERR) {
287 1.5 christos device_printf(sc->sc_dev, "transfer error, id 0x%02" PRIx64
288 1.5 christos "\n", __SHIFTOUT(sc->sc_stat, RSB_STAT_TRANS_ERR_ID));
289 1.1 jmcneill return EIO;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill return 0;
293 1.1 jmcneill }
294 1.1 jmcneill
295 1.1 jmcneill static int
296 1.1 jmcneill sunxi_rsb_rsb_config(struct sunxi_rsb_softc *sc, uint8_t rta, i2c_addr_t da,
297 1.1 jmcneill int flags)
298 1.1 jmcneill {
299 1.1 jmcneill uint32_t dar, ctrl;
300 1.1 jmcneill
301 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
302 1.1 jmcneill
303 1.1 jmcneill RSB_WRITE(sc, RSB_STAT_REG,
304 1.1 jmcneill RSB_READ(sc, RSB_STAT_REG) & RSB_STAT_MASK);
305 1.1 jmcneill
306 1.1 jmcneill dar = __SHIFTIN(rta, RSB_DAR_RTA);
307 1.1 jmcneill dar |= __SHIFTIN(da, RSB_DAR_DA);
308 1.1 jmcneill RSB_WRITE(sc, RSB_DAR_REG, dar);
309 1.1 jmcneill RSB_WRITE(sc, RSB_CMD_REG, RSB_CMD_IDX_SRTA);
310 1.1 jmcneill
311 1.1 jmcneill /* Make sure the controller is idle */
312 1.1 jmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
313 1.1 jmcneill if (ctrl & RSB_CTRL_START_TRANS) {
314 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
315 1.1 jmcneill return EBUSY;
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill /* Start the transfer */
319 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
320 1.1 jmcneill ctrl | RSB_CTRL_START_TRANS);
321 1.1 jmcneill
322 1.1 jmcneill return sunxi_rsb_wait(sc, flags);
323 1.1 jmcneill }
324 1.1 jmcneill
325 1.1 jmcneill static int
326 1.1 jmcneill sunxi_rsb_acquire_bus(void *priv, int flags)
327 1.1 jmcneill {
328 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
329 1.1 jmcneill
330 1.6 jmcneill for (;;) {
331 1.6 jmcneill mutex_enter(&sc->sc_lock);
332 1.6 jmcneill if (sc->sc_busy == false)
333 1.6 jmcneill break;
334 1.6 jmcneill mutex_exit(&sc->sc_lock);
335 1.6 jmcneill }
336 1.6 jmcneill sc->sc_busy = true;
337 1.1 jmcneill
338 1.1 jmcneill return 0;
339 1.1 jmcneill }
340 1.1 jmcneill
341 1.1 jmcneill static void
342 1.1 jmcneill sunxi_rsb_release_bus(void *priv, int flags)
343 1.1 jmcneill {
344 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
345 1.1 jmcneill
346 1.6 jmcneill sc->sc_busy = false;
347 1.1 jmcneill mutex_exit(&sc->sc_lock);
348 1.1 jmcneill }
349 1.1 jmcneill
350 1.1 jmcneill static int
351 1.1 jmcneill sunxi_rsb_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
352 1.1 jmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
353 1.1 jmcneill {
354 1.1 jmcneill struct sunxi_rsb_softc *sc = priv;
355 1.1 jmcneill uint32_t dlen, ctrl;
356 1.1 jmcneill uint8_t rta;
357 1.1 jmcneill int error, i;
358 1.1 jmcneill
359 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
360 1.6 jmcneill KASSERT(sc->sc_busy == true);
361 1.1 jmcneill
362 1.1 jmcneill if (cmdlen != 1 || (len != 1 && len != 2 && len != 4))
363 1.1 jmcneill return EINVAL;
364 1.1 jmcneill
365 1.4 jmcneill error = sunxi_rsb_soft_reset(sc);
366 1.4 jmcneill if (error != 0) {
367 1.4 jmcneill device_printf(sc->sc_dev, "soft reset timed out\n");
368 1.4 jmcneill return error;
369 1.4 jmcneill }
370 1.4 jmcneill
371 1.4 jmcneill if ((flags & I2C_F_POLL) == 0) {
372 1.4 jmcneill /* Enable interrupts */
373 1.4 jmcneill RSB_WRITE(sc, RSB_INTE_REG,
374 1.4 jmcneill RSB_INTE_LOAD_BSY_ENB |
375 1.4 jmcneill RSB_INTE_TRANS_ERR_ENB |
376 1.4 jmcneill RSB_INTE_TRANS_OVER_ENB);
377 1.4 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
378 1.4 jmcneill RSB_CTRL_GLOBAL_INT_ENB);
379 1.4 jmcneill }
380 1.4 jmcneill
381 1.1 jmcneill if (sc->sc_type == SUNXI_RSB && sc->sc_rsb_last_da != addr) {
382 1.1 jmcneill /* Lookup run-time address for given device address */
383 1.1 jmcneill for (rta = 0, i = 0; rsb_rtamap[i].rta != 0; i++)
384 1.1 jmcneill if (rsb_rtamap[i].addr == addr) {
385 1.1 jmcneill rta = rsb_rtamap[i].rta;
386 1.1 jmcneill break;
387 1.1 jmcneill }
388 1.1 jmcneill if (rta == 0) {
389 1.1 jmcneill device_printf(sc->sc_dev,
390 1.1 jmcneill "RTA not known for address %#x\n", addr);
391 1.1 jmcneill return ENXIO;
392 1.1 jmcneill }
393 1.1 jmcneill error = sunxi_rsb_rsb_config(sc, rta, addr, flags);
394 1.1 jmcneill if (error) {
395 1.1 jmcneill device_printf(sc->sc_dev,
396 1.1 jmcneill "SRTA failed, flags = %x, error = %d\n",
397 1.1 jmcneill flags, error);
398 1.1 jmcneill sc->sc_rsb_last_da = 0;
399 1.4 jmcneill goto done;
400 1.1 jmcneill }
401 1.1 jmcneill
402 1.1 jmcneill sc->sc_rsb_last_da = addr;
403 1.1 jmcneill }
404 1.1 jmcneill
405 1.1 jmcneill /* Data byte register */
406 1.1 jmcneill RSB_WRITE(sc, RSB_DADDR0_REG, *(const uint8_t *)cmdbuf);
407 1.1 jmcneill
408 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
409 1.1 jmcneill uint8_t *pbuf = buf;
410 1.1 jmcneill uint32_t data;
411 1.1 jmcneill /* Write data */
412 1.1 jmcneill switch (len) {
413 1.1 jmcneill case 1:
414 1.1 jmcneill data = pbuf[0];
415 1.1 jmcneill break;
416 1.1 jmcneill case 2:
417 1.1 jmcneill data = pbuf[0] | (pbuf[1] << 8);
418 1.1 jmcneill break;
419 1.1 jmcneill case 4:
420 1.1 jmcneill data = pbuf[0] | (pbuf[1] << 8) |
421 1.1 jmcneill (pbuf[2] << 16) | (pbuf[3] << 24);
422 1.1 jmcneill break;
423 1.1 jmcneill default:
424 1.4 jmcneill error = EINVAL;
425 1.4 jmcneill goto done;
426 1.1 jmcneill }
427 1.1 jmcneill RSB_WRITE(sc, RSB_DATA0_REG, data);
428 1.1 jmcneill }
429 1.1 jmcneill
430 1.1 jmcneill if (sc->sc_type == SUNXI_RSB) {
431 1.1 jmcneill uint8_t cmd;
432 1.1 jmcneill if (I2C_OP_WRITE_P(op)) {
433 1.1 jmcneill switch (len) {
434 1.1 jmcneill case 1: cmd = RSB_CMD_IDX_WR8; break;
435 1.1 jmcneill case 2: cmd = RSB_CMD_IDX_WR16; break;
436 1.1 jmcneill case 4: cmd = RSB_CMD_IDX_WR32; break;
437 1.4 jmcneill default: error = EINVAL; goto done;
438 1.1 jmcneill }
439 1.1 jmcneill } else {
440 1.1 jmcneill switch (len) {
441 1.1 jmcneill case 1: cmd = RSB_CMD_IDX_RD8; break;
442 1.1 jmcneill case 2: cmd = RSB_CMD_IDX_RD16; break;
443 1.1 jmcneill case 4: cmd = RSB_CMD_IDX_RD32; break;
444 1.4 jmcneill default: error = EINVAL; goto done;
445 1.1 jmcneill }
446 1.1 jmcneill }
447 1.1 jmcneill RSB_WRITE(sc, RSB_CMD_REG, cmd);
448 1.1 jmcneill }
449 1.1 jmcneill
450 1.1 jmcneill /* Program data length register; if reading, set read/write bit */
451 1.1 jmcneill dlen = __SHIFTIN(len - 1, RSB_DLEN_ACCESS_LENGTH);
452 1.1 jmcneill if (I2C_OP_READ_P(op)) {
453 1.1 jmcneill dlen |= RSB_DLEN_READ_WRITE_FLAG;
454 1.1 jmcneill }
455 1.1 jmcneill RSB_WRITE(sc, RSB_DLEN_REG, dlen);
456 1.1 jmcneill
457 1.1 jmcneill /* Make sure the controller is idle */
458 1.1 jmcneill ctrl = RSB_READ(sc, RSB_CTRL_REG);
459 1.1 jmcneill if (ctrl & RSB_CTRL_START_TRANS) {
460 1.1 jmcneill device_printf(sc->sc_dev, "device is busy\n");
461 1.4 jmcneill error = EBUSY;
462 1.4 jmcneill goto done;
463 1.1 jmcneill }
464 1.1 jmcneill
465 1.1 jmcneill /* Start the transfer */
466 1.1 jmcneill RSB_WRITE(sc, RSB_CTRL_REG,
467 1.1 jmcneill ctrl | RSB_CTRL_START_TRANS);
468 1.1 jmcneill
469 1.1 jmcneill error = sunxi_rsb_wait(sc, flags);
470 1.4 jmcneill if (error)
471 1.4 jmcneill goto done;
472 1.1 jmcneill
473 1.1 jmcneill if (I2C_OP_READ_P(op)) {
474 1.1 jmcneill uint32_t data = RSB_READ(sc, RSB_DATA0_REG);
475 1.1 jmcneill switch (len) {
476 1.1 jmcneill case 4:
477 1.1 jmcneill *(uint32_t *)buf = data;
478 1.1 jmcneill break;
479 1.1 jmcneill case 2:
480 1.1 jmcneill *(uint16_t *)buf = data & 0xffff;
481 1.1 jmcneill break;
482 1.1 jmcneill case 1:
483 1.1 jmcneill *(uint8_t *)buf = data & 0xff;
484 1.1 jmcneill break;
485 1.1 jmcneill default:
486 1.4 jmcneill error = EINVAL;
487 1.4 jmcneill goto done;
488 1.1 jmcneill }
489 1.1 jmcneill }
490 1.1 jmcneill
491 1.4 jmcneill error = 0;
492 1.4 jmcneill
493 1.4 jmcneill done:
494 1.4 jmcneill RSB_WRITE(sc, RSB_CTRL_REG, 0);
495 1.4 jmcneill
496 1.4 jmcneill return error;
497 1.1 jmcneill }
498