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