dwiic.c revision 1.5 1 1.5 jakllsch /* $NetBSD: dwiic.c,v 1.5 2018/09/26 19:06:33 jakllsch Exp $ */
2 1.1 bouyer
3 1.4 jakllsch /* $OpenBSD: dwiic.c,v 1.4 2018/05/23 22:08:00 kettenis Exp $ */
4 1.1 bouyer
5 1.1 bouyer /*-
6 1.1 bouyer * Copyright (c) 2017 The NetBSD Foundation, Inc.
7 1.1 bouyer * All rights reserved.
8 1.1 bouyer *
9 1.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
10 1.1 bouyer * by Manuel Bouyer.
11 1.1 bouyer *
12 1.1 bouyer * Redistribution and use in source and binary forms, with or without
13 1.1 bouyer * modification, are permitted provided that the following conditions
14 1.1 bouyer * are met:
15 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
16 1.1 bouyer * notice, this list of conditions and the following disclaimer.
17 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
19 1.1 bouyer * documentation and/or other materials provided with the distribution.
20 1.1 bouyer *
21 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 bouyer * POSSIBILITY OF SUCH DAMAGE.
32 1.1 bouyer */
33 1.1 bouyer /*
34 1.1 bouyer * Synopsys DesignWare I2C controller
35 1.1 bouyer *
36 1.1 bouyer * Copyright (c) 2015, 2016 joshua stein <jcs (at) openbsd.org>
37 1.1 bouyer *
38 1.1 bouyer * Permission to use, copy, modify, and/or distribute this software for any
39 1.1 bouyer * purpose with or without fee is hereby granted, provided that the above
40 1.1 bouyer * copyright notice and this permission notice appear in all copies.
41 1.1 bouyer *
42 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43 1.1 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 1.1 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45 1.1 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 1.1 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 1.1 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48 1.1 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 1.1 bouyer */
50 1.1 bouyer
51 1.1 bouyer #include <sys/cdefs.h>
52 1.5 jakllsch __KERNEL_RCSID(0, "$NetBSD: dwiic.c,v 1.5 2018/09/26 19:06:33 jakllsch Exp $");
53 1.1 bouyer
54 1.1 bouyer #include <sys/param.h>
55 1.1 bouyer #include <sys/bus.h>
56 1.1 bouyer #include <sys/device.h>
57 1.1 bouyer #include <sys/kernel.h>
58 1.1 bouyer #include <sys/systm.h>
59 1.1 bouyer
60 1.1 bouyer #include <dev/ic/dwiic_var.h>
61 1.1 bouyer
62 1.1 bouyer //#define DWIIC_DEBUG
63 1.1 bouyer
64 1.1 bouyer #ifdef DWIIC_DEBUG
65 1.1 bouyer #define DPRINTF(x) printf x
66 1.1 bouyer #else
67 1.1 bouyer #define DPRINTF(x)
68 1.1 bouyer #endif
69 1.1 bouyer
70 1.1 bouyer /* register offsets */
71 1.1 bouyer #define DW_IC_CON 0x0
72 1.1 bouyer #define DW_IC_TAR 0x4
73 1.1 bouyer #define DW_IC_DATA_CMD 0x10
74 1.1 bouyer #define DW_IC_SS_SCL_HCNT 0x14
75 1.1 bouyer #define DW_IC_SS_SCL_LCNT 0x18
76 1.1 bouyer #define DW_IC_FS_SCL_HCNT 0x1c
77 1.1 bouyer #define DW_IC_FS_SCL_LCNT 0x20
78 1.1 bouyer #define DW_IC_INTR_STAT 0x2c
79 1.1 bouyer #define DW_IC_INTR_MASK 0x30
80 1.1 bouyer #define DW_IC_RAW_INTR_STAT 0x34
81 1.1 bouyer #define DW_IC_RX_TL 0x38
82 1.1 bouyer #define DW_IC_TX_TL 0x3c
83 1.1 bouyer #define DW_IC_CLR_INTR 0x40
84 1.1 bouyer #define DW_IC_CLR_RX_UNDER 0x44
85 1.1 bouyer #define DW_IC_CLR_RX_OVER 0x48
86 1.1 bouyer #define DW_IC_CLR_TX_OVER 0x4c
87 1.1 bouyer #define DW_IC_CLR_RD_REQ 0x50
88 1.1 bouyer #define DW_IC_CLR_TX_ABRT 0x54
89 1.1 bouyer #define DW_IC_CLR_RX_DONE 0x58
90 1.1 bouyer #define DW_IC_CLR_ACTIVITY 0x5c
91 1.1 bouyer #define DW_IC_CLR_STOP_DET 0x60
92 1.1 bouyer #define DW_IC_CLR_START_DET 0x64
93 1.1 bouyer #define DW_IC_CLR_GEN_CALL 0x68
94 1.1 bouyer #define DW_IC_ENABLE 0x6c
95 1.1 bouyer #define DW_IC_STATUS 0x70
96 1.1 bouyer #define DW_IC_TXFLR 0x74
97 1.1 bouyer #define DW_IC_RXFLR 0x78
98 1.1 bouyer #define DW_IC_SDA_HOLD 0x7c
99 1.1 bouyer #define DW_IC_TX_ABRT_SOURCE 0x80
100 1.1 bouyer #define DW_IC_ENABLE_STATUS 0x9c
101 1.1 bouyer #define DW_IC_COMP_PARAM_1 0xf4
102 1.1 bouyer #define DW_IC_COMP_VERSION 0xf8
103 1.1 bouyer #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
104 1.1 bouyer #define DW_IC_COMP_TYPE 0xfc
105 1.1 bouyer #define DW_IC_COMP_TYPE_VALUE 0x44570140
106 1.1 bouyer
107 1.1 bouyer #define DW_IC_CON_MASTER 0x1
108 1.1 bouyer #define DW_IC_CON_SPEED_STD 0x2
109 1.1 bouyer #define DW_IC_CON_SPEED_FAST 0x4
110 1.1 bouyer #define DW_IC_CON_10BITADDR_MASTER 0x10
111 1.1 bouyer #define DW_IC_CON_RESTART_EN 0x20
112 1.1 bouyer #define DW_IC_CON_SLAVE_DISABLE 0x40
113 1.1 bouyer
114 1.1 bouyer #define DW_IC_DATA_CMD_READ 0x100
115 1.1 bouyer #define DW_IC_DATA_CMD_STOP 0x200
116 1.1 bouyer #define DW_IC_DATA_CMD_RESTART 0x400
117 1.1 bouyer
118 1.1 bouyer #define DW_IC_INTR_RX_UNDER 0x001
119 1.1 bouyer #define DW_IC_INTR_RX_OVER 0x002
120 1.1 bouyer #define DW_IC_INTR_RX_FULL 0x004
121 1.1 bouyer #define DW_IC_INTR_TX_OVER 0x008
122 1.1 bouyer #define DW_IC_INTR_TX_EMPTY 0x010
123 1.1 bouyer #define DW_IC_INTR_RD_REQ 0x020
124 1.1 bouyer #define DW_IC_INTR_TX_ABRT 0x040
125 1.1 bouyer #define DW_IC_INTR_RX_DONE 0x080
126 1.1 bouyer #define DW_IC_INTR_ACTIVITY 0x100
127 1.1 bouyer #define DW_IC_INTR_STOP_DET 0x200
128 1.1 bouyer #define DW_IC_INTR_START_DET 0x400
129 1.1 bouyer #define DW_IC_INTR_GEN_CALL 0x800
130 1.1 bouyer
131 1.1 bouyer #define DW_IC_STATUS_ACTIVITY 0x1
132 1.1 bouyer
133 1.1 bouyer /* hardware abort codes from the DW_IC_TX_ABRT_SOURCE register */
134 1.1 bouyer #define ABRT_7B_ADDR_NOACK 0
135 1.1 bouyer #define ABRT_10ADDR1_NOACK 1
136 1.1 bouyer #define ABRT_10ADDR2_NOACK 2
137 1.1 bouyer #define ABRT_TXDATA_NOACK 3
138 1.1 bouyer #define ABRT_GCALL_NOACK 4
139 1.1 bouyer #define ABRT_GCALL_READ 5
140 1.1 bouyer #define ABRT_SBYTE_ACKDET 7
141 1.1 bouyer #define ABRT_SBYTE_NORSTRT 9
142 1.1 bouyer #define ABRT_10B_RD_NORSTRT 10
143 1.1 bouyer #define ABRT_MASTER_DIS 11
144 1.1 bouyer #define ARB_LOST 12
145 1.1 bouyer
146 1.1 bouyer static int dwiic_init(struct dwiic_softc *);
147 1.1 bouyer static void dwiic_enable(struct dwiic_softc *, bool);
148 1.1 bouyer static int dwiic_i2c_acquire_bus(void *, int);
149 1.1 bouyer static void dwiic_i2c_release_bus(void *, int);
150 1.1 bouyer static uint32_t dwiic_read(struct dwiic_softc *, int);
151 1.1 bouyer static void dwiic_write(struct dwiic_softc *, int, uint32_t);
152 1.1 bouyer static int dwiic_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
153 1.1 bouyer size_t, void *, size_t, int);
154 1.1 bouyer
155 1.1 bouyer bool
156 1.1 bouyer dwiic_attach(struct dwiic_softc *sc)
157 1.1 bouyer {
158 1.1 bouyer if (sc->sc_power != NULL) {
159 1.1 bouyer if (!sc->sc_power(sc, 1)) {
160 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed to power up\n");
161 1.1 bouyer return 0;
162 1.1 bouyer }
163 1.1 bouyer }
164 1.1 bouyer
165 1.1 bouyer /* fetch timing parameters */
166 1.1 bouyer if (sc->ss_hcnt == 0)
167 1.1 bouyer sc->ss_hcnt = dwiic_read(sc, DW_IC_SS_SCL_HCNT);
168 1.1 bouyer if (sc->ss_lcnt == 0)
169 1.1 bouyer sc->ss_lcnt = dwiic_read(sc, DW_IC_SS_SCL_LCNT);
170 1.1 bouyer if (sc->fs_hcnt == 0)
171 1.1 bouyer sc->fs_hcnt = dwiic_read(sc, DW_IC_FS_SCL_HCNT);
172 1.1 bouyer if (sc->fs_lcnt == 0)
173 1.1 bouyer sc->fs_lcnt = dwiic_read(sc, DW_IC_FS_SCL_LCNT);
174 1.1 bouyer if (sc->sda_hold_time == 0)
175 1.1 bouyer sc->sda_hold_time = dwiic_read(sc, DW_IC_SDA_HOLD);
176 1.1 bouyer
177 1.1 bouyer if (dwiic_init(sc)) {
178 1.1 bouyer aprint_error_dev(sc->sc_dev, "failed initializing\n");
179 1.1 bouyer return 0;
180 1.1 bouyer }
181 1.1 bouyer
182 1.1 bouyer /* leave the controller disabled */
183 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
184 1.1 bouyer dwiic_enable(sc, 0);
185 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
186 1.1 bouyer
187 1.1 bouyer mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE);
188 1.1 bouyer mutex_init(&sc->sc_int_lock, MUTEX_DEFAULT, IPL_VM);
189 1.1 bouyer cv_init(&sc->sc_int_readwait, "dwiicr");
190 1.1 bouyer cv_init(&sc->sc_int_writewait, "dwiicw");
191 1.4 jakllsch cv_init(&sc->sc_int_stopwait, "dwiics");
192 1.1 bouyer
193 1.1 bouyer /* setup and attach iic bus */
194 1.1 bouyer sc->sc_i2c_tag.ic_cookie = sc;
195 1.1 bouyer sc->sc_i2c_tag.ic_acquire_bus = dwiic_i2c_acquire_bus;
196 1.1 bouyer sc->sc_i2c_tag.ic_release_bus = dwiic_i2c_release_bus;
197 1.1 bouyer sc->sc_i2c_tag.ic_exec = dwiic_i2c_exec;
198 1.1 bouyer
199 1.1 bouyer sc->sc_iba.iba_tag = &sc->sc_i2c_tag;
200 1.1 bouyer
201 1.5 jakllsch /* config_found_ia for "i2cbus" is done in the bus-attachment glue */
202 1.5 jakllsch
203 1.1 bouyer return 1;
204 1.1 bouyer }
205 1.1 bouyer
206 1.1 bouyer int
207 1.1 bouyer dwiic_detach(device_t self, int flags)
208 1.1 bouyer {
209 1.1 bouyer struct dwiic_softc *sc = device_private(self);
210 1.1 bouyer
211 1.1 bouyer dwiic_enable(sc, 0);
212 1.1 bouyer if (sc->sc_ih != NULL) {
213 1.1 bouyer intr_disestablish(sc->sc_ih);
214 1.1 bouyer sc->sc_ih = NULL;
215 1.1 bouyer }
216 1.1 bouyer
217 1.1 bouyer return 0;
218 1.1 bouyer }
219 1.1 bouyer
220 1.1 bouyer bool
221 1.1 bouyer dwiic_suspend(device_t self, const pmf_qual_t *qual)
222 1.1 bouyer {
223 1.1 bouyer struct dwiic_softc *sc = device_private(self);
224 1.1 bouyer
225 1.1 bouyer /* disable controller */
226 1.1 bouyer dwiic_enable(sc, 0);
227 1.1 bouyer
228 1.1 bouyer /* disable interrupts */
229 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
230 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
231 1.1 bouyer if (sc->sc_power != NULL) {
232 1.1 bouyer if (!sc->sc_power(sc, 0)) {
233 1.3 jakllsch device_printf(sc->sc_dev, "failed to power off\n");
234 1.1 bouyer }
235 1.1 bouyer }
236 1.1 bouyer return true;
237 1.1 bouyer }
238 1.1 bouyer
239 1.1 bouyer bool
240 1.1 bouyer dwiic_resume(device_t self, const pmf_qual_t *qual)
241 1.1 bouyer {
242 1.1 bouyer struct dwiic_softc *sc = device_private(self);
243 1.1 bouyer if (sc->sc_power != NULL) {
244 1.1 bouyer if (!sc->sc_power(sc, 1)) {
245 1.3 jakllsch device_printf(sc->sc_dev, "failed to power up\n");
246 1.1 bouyer return false;
247 1.1 bouyer }
248 1.1 bouyer }
249 1.1 bouyer dwiic_init(sc);
250 1.1 bouyer return true;
251 1.1 bouyer }
252 1.1 bouyer
253 1.1 bouyer static uint32_t
254 1.1 bouyer dwiic_read(struct dwiic_softc *sc, int offset)
255 1.1 bouyer {
256 1.1 bouyer u_int32_t b = bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
257 1.1 bouyer
258 1.1 bouyer DPRINTF(("%s: read at 0x%x = 0x%x\n", device_xname(sc->sc_dev), offset, b));
259 1.1 bouyer
260 1.1 bouyer return b;
261 1.1 bouyer }
262 1.1 bouyer
263 1.1 bouyer static void
264 1.1 bouyer dwiic_write(struct dwiic_softc *sc, int offset, uint32_t val)
265 1.1 bouyer {
266 1.1 bouyer bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
267 1.1 bouyer
268 1.1 bouyer DPRINTF(("%s: write at 0x%x: 0x%x\n", device_xname(sc->sc_dev), offset,
269 1.1 bouyer val));
270 1.1 bouyer }
271 1.1 bouyer
272 1.1 bouyer static int
273 1.1 bouyer dwiic_i2c_acquire_bus(void *cookie, int flags)
274 1.1 bouyer {
275 1.1 bouyer struct dwiic_softc *sc = cookie;
276 1.1 bouyer
277 1.1 bouyer if (cold || sc->sc_poll || (flags & I2C_F_POLL))
278 1.1 bouyer return (0);
279 1.1 bouyer
280 1.1 bouyer mutex_enter(&sc->sc_i2c_lock);
281 1.2 jakllsch return 0;
282 1.1 bouyer }
283 1.1 bouyer
284 1.1 bouyer void
285 1.1 bouyer dwiic_i2c_release_bus(void *cookie, int flags)
286 1.1 bouyer {
287 1.1 bouyer struct dwiic_softc *sc = cookie;
288 1.1 bouyer
289 1.1 bouyer if (cold || sc->sc_poll || (flags & I2C_F_POLL))
290 1.1 bouyer return;
291 1.1 bouyer
292 1.1 bouyer mutex_exit(&sc->sc_i2c_lock);
293 1.1 bouyer }
294 1.1 bouyer
295 1.1 bouyer static int
296 1.1 bouyer dwiic_init(struct dwiic_softc *sc)
297 1.1 bouyer {
298 1.1 bouyer uint32_t reg;
299 1.1 bouyer
300 1.1 bouyer /* make sure we're talking to a device we know */
301 1.1 bouyer reg = dwiic_read(sc, DW_IC_COMP_TYPE);
302 1.1 bouyer if (reg != DW_IC_COMP_TYPE_VALUE) {
303 1.1 bouyer DPRINTF(("%s: invalid component type 0x%x\n",
304 1.1 bouyer device_xname(sc->sc_dev), reg));
305 1.1 bouyer return 1;
306 1.1 bouyer }
307 1.1 bouyer
308 1.1 bouyer /* disable the adapter */
309 1.1 bouyer dwiic_enable(sc, 0);
310 1.1 bouyer
311 1.1 bouyer /* write standard-mode SCL timing parameters */
312 1.1 bouyer dwiic_write(sc, DW_IC_SS_SCL_HCNT, sc->ss_hcnt);
313 1.1 bouyer dwiic_write(sc, DW_IC_SS_SCL_LCNT, sc->ss_lcnt);
314 1.1 bouyer
315 1.1 bouyer /* and fast-mode SCL timing parameters */
316 1.1 bouyer dwiic_write(sc, DW_IC_FS_SCL_HCNT, sc->fs_hcnt);
317 1.1 bouyer dwiic_write(sc, DW_IC_FS_SCL_LCNT, sc->fs_lcnt);
318 1.1 bouyer
319 1.1 bouyer /* SDA hold time */
320 1.1 bouyer reg = dwiic_read(sc, DW_IC_COMP_VERSION);
321 1.1 bouyer if (reg >= DW_IC_SDA_HOLD_MIN_VERS)
322 1.1 bouyer dwiic_write(sc, DW_IC_SDA_HOLD, sc->sda_hold_time);
323 1.1 bouyer
324 1.1 bouyer /* FIFO threshold levels */
325 1.1 bouyer sc->tx_fifo_depth = 32;
326 1.1 bouyer sc->rx_fifo_depth = 32;
327 1.1 bouyer dwiic_write(sc, DW_IC_TX_TL, sc->tx_fifo_depth / 2);
328 1.1 bouyer dwiic_write(sc, DW_IC_RX_TL, 0);
329 1.1 bouyer
330 1.1 bouyer /* configure as i2c master with fast speed */
331 1.1 bouyer sc->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
332 1.1 bouyer DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
333 1.1 bouyer dwiic_write(sc, DW_IC_CON, sc->master_cfg);
334 1.1 bouyer
335 1.1 bouyer return 0;
336 1.1 bouyer }
337 1.1 bouyer
338 1.1 bouyer static void
339 1.1 bouyer dwiic_enable(struct dwiic_softc *sc, bool enable)
340 1.1 bouyer {
341 1.1 bouyer int retries;
342 1.1 bouyer
343 1.1 bouyer for (retries = 100; retries > 0; retries--) {
344 1.1 bouyer dwiic_write(sc, DW_IC_ENABLE, enable);
345 1.1 bouyer if ((dwiic_read(sc, DW_IC_ENABLE_STATUS) & 1) == enable)
346 1.1 bouyer return;
347 1.1 bouyer
348 1.1 bouyer DELAY(25);
349 1.1 bouyer }
350 1.1 bouyer
351 1.3 jakllsch device_printf(sc->sc_dev, "failed to %sable\n",
352 1.1 bouyer (enable ? "en" : "dis"));
353 1.1 bouyer }
354 1.1 bouyer
355 1.1 bouyer static int
356 1.1 bouyer dwiic_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
357 1.1 bouyer size_t cmdlen, void *buf, size_t len, int flags)
358 1.1 bouyer {
359 1.1 bouyer struct dwiic_softc *sc = cookie;
360 1.1 bouyer u_int32_t ic_con, st, cmd, resp;
361 1.1 bouyer int retries, tx_limit, rx_avail, x, readpos;
362 1.1 bouyer const uint8_t *bcmd;
363 1.1 bouyer uint8_t *bdata;
364 1.1 bouyer
365 1.1 bouyer if (cold || sc->sc_poll)
366 1.1 bouyer flags |= I2C_F_POLL;
367 1.1 bouyer
368 1.1 bouyer DPRINTF(("%s: %s: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
369 1.1 bouyer "flags 0x%02x\n", device_xname(sc->sc_dev), __func__, op, addr, cmdlen,
370 1.1 bouyer len, flags));
371 1.1 bouyer
372 1.1 bouyer /* setup transfer */
373 1.1 bouyer sc->sc_i2c_xfer.op = op;
374 1.1 bouyer sc->sc_i2c_xfer.buf = buf;
375 1.1 bouyer sc->sc_i2c_xfer.len = len;
376 1.1 bouyer sc->sc_i2c_xfer.flags = flags;
377 1.1 bouyer sc->sc_i2c_xfer.error = 0;
378 1.1 bouyer
379 1.1 bouyer /* wait for bus to be idle */
380 1.1 bouyer for (retries = 100; retries > 0; retries--) {
381 1.1 bouyer st = dwiic_read(sc, DW_IC_STATUS);
382 1.1 bouyer if (!(st & DW_IC_STATUS_ACTIVITY))
383 1.1 bouyer break;
384 1.1 bouyer DELAY(1000);
385 1.1 bouyer }
386 1.1 bouyer DPRINTF(("%s: %s: status 0x%x\n", device_xname(sc->sc_dev), __func__, st));
387 1.1 bouyer if (st & DW_IC_STATUS_ACTIVITY) {
388 1.1 bouyer return (1);
389 1.1 bouyer }
390 1.1 bouyer
391 1.1 bouyer /* disable controller */
392 1.1 bouyer dwiic_enable(sc, 0);
393 1.1 bouyer
394 1.1 bouyer /* set slave address */
395 1.1 bouyer ic_con = dwiic_read(sc, DW_IC_CON);
396 1.1 bouyer ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
397 1.1 bouyer dwiic_write(sc, DW_IC_CON, ic_con);
398 1.1 bouyer dwiic_write(sc, DW_IC_TAR, addr);
399 1.1 bouyer
400 1.1 bouyer /* disable interrupts */
401 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
402 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
403 1.1 bouyer
404 1.1 bouyer /* enable controller */
405 1.1 bouyer dwiic_enable(sc, 1);
406 1.1 bouyer
407 1.1 bouyer /* wait until the controller is ready for commands */
408 1.1 bouyer if (flags & I2C_F_POLL)
409 1.1 bouyer DELAY(200);
410 1.1 bouyer else {
411 1.1 bouyer mutex_enter(&sc->sc_int_lock);
412 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
413 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, DW_IC_INTR_TX_EMPTY);
414 1.1 bouyer
415 1.1 bouyer if (cv_timedwait(&sc->sc_int_writewait,
416 1.1 bouyer &sc->sc_int_lock, hz / 2) != 0)
417 1.3 jakllsch device_printf(sc->sc_dev,
418 1.1 bouyer "timed out waiting for tx_empty intr\n");
419 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
420 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
421 1.1 bouyer mutex_exit(&sc->sc_int_lock);
422 1.1 bouyer }
423 1.1 bouyer
424 1.1 bouyer /* send our command, one byte at a time */
425 1.1 bouyer if (cmdlen > 0) {
426 1.1 bouyer bcmd = (const void *)cmdbuf;
427 1.1 bouyer
428 1.1 bouyer DPRINTF(("%s: %s: sending cmd (len %zu):", device_xname(sc->sc_dev),
429 1.1 bouyer __func__, cmdlen));
430 1.1 bouyer for (x = 0; x < cmdlen; x++)
431 1.1 bouyer DPRINTF((" %02x", bcmd[x]));
432 1.1 bouyer DPRINTF(("\n"));
433 1.1 bouyer
434 1.1 bouyer tx_limit = sc->tx_fifo_depth - dwiic_read(sc, DW_IC_TXFLR);
435 1.1 bouyer if (cmdlen > tx_limit) {
436 1.1 bouyer /* TODO */
437 1.3 jakllsch device_printf(sc->sc_dev, "can't write %zu (> %d)\n",
438 1.1 bouyer cmdlen, tx_limit);
439 1.1 bouyer sc->sc_i2c_xfer.error = 1;
440 1.1 bouyer return (1);
441 1.1 bouyer }
442 1.1 bouyer
443 1.1 bouyer for (x = 0; x < cmdlen; x++) {
444 1.1 bouyer cmd = bcmd[x];
445 1.1 bouyer /*
446 1.1 bouyer * Generate STOP condition if this is the last
447 1.1 bouyer * byte of the transfer.
448 1.1 bouyer */
449 1.1 bouyer if (x == (cmdlen - 1) && len == 0 && I2C_OP_STOP_P(op))
450 1.1 bouyer cmd |= DW_IC_DATA_CMD_STOP;
451 1.1 bouyer dwiic_write(sc, DW_IC_DATA_CMD, cmd);
452 1.1 bouyer }
453 1.1 bouyer }
454 1.1 bouyer
455 1.1 bouyer bdata = (void *)buf;
456 1.1 bouyer x = readpos = 0;
457 1.1 bouyer tx_limit = sc->tx_fifo_depth - dwiic_read(sc, DW_IC_TXFLR);
458 1.1 bouyer
459 1.1 bouyer DPRINTF(("%s: %s: need to read %zu bytes, can send %d read reqs\n",
460 1.1 bouyer device_xname(sc->sc_dev), __func__, len, tx_limit));
461 1.1 bouyer
462 1.1 bouyer while (x < len) {
463 1.1 bouyer if (I2C_OP_WRITE_P(op))
464 1.1 bouyer cmd = bdata[x];
465 1.1 bouyer else
466 1.1 bouyer cmd = DW_IC_DATA_CMD_READ;
467 1.1 bouyer
468 1.1 bouyer /*
469 1.1 bouyer * Generate RESTART condition if we're reversing
470 1.1 bouyer * direction.
471 1.1 bouyer */
472 1.1 bouyer if (x == 0 && cmdlen > 0 && I2C_OP_READ_P(op))
473 1.1 bouyer cmd |= DW_IC_DATA_CMD_RESTART;
474 1.1 bouyer /*
475 1.1 bouyer * Generate STOP conditon on the last byte of the
476 1.1 bouyer * transfer.
477 1.1 bouyer */
478 1.1 bouyer if (x == (len - 1) && I2C_OP_STOP_P(op))
479 1.1 bouyer cmd |= DW_IC_DATA_CMD_STOP;
480 1.1 bouyer
481 1.1 bouyer dwiic_write(sc, DW_IC_DATA_CMD, cmd);
482 1.1 bouyer
483 1.1 bouyer tx_limit--;
484 1.1 bouyer x++;
485 1.1 bouyer
486 1.1 bouyer /*
487 1.1 bouyer * As TXFLR fills up, we need to clear it out by reading all
488 1.1 bouyer * available data.
489 1.1 bouyer */
490 1.4 jakllsch while (I2C_OP_READ_P(op) && (tx_limit == 0 || x == len)) {
491 1.1 bouyer DPRINTF(("%s: %s: tx_limit %d, sent %d read reqs\n",
492 1.1 bouyer device_xname(sc->sc_dev), __func__, tx_limit, x));
493 1.1 bouyer
494 1.1 bouyer if (flags & I2C_F_POLL) {
495 1.1 bouyer for (retries = 100; retries > 0; retries--) {
496 1.1 bouyer rx_avail = dwiic_read(sc, DW_IC_RXFLR);
497 1.1 bouyer if (rx_avail > 0)
498 1.1 bouyer break;
499 1.1 bouyer DELAY(50);
500 1.1 bouyer }
501 1.1 bouyer } else {
502 1.1 bouyer mutex_enter(&sc->sc_int_lock);
503 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
504 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK,
505 1.1 bouyer DW_IC_INTR_RX_FULL);
506 1.1 bouyer
507 1.1 bouyer if (cv_timedwait(&sc->sc_int_readwait,
508 1.1 bouyer &sc->sc_int_lock, hz / 2) != 0)
509 1.3 jakllsch device_printf(sc->sc_dev,
510 1.1 bouyer "timed out waiting for "
511 1.1 bouyer "rx_full intr\n");
512 1.1 bouyer
513 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
514 1.1 bouyer dwiic_read(sc, DW_IC_CLR_INTR);
515 1.1 bouyer mutex_exit(&sc->sc_int_lock);
516 1.1 bouyer rx_avail = dwiic_read(sc, DW_IC_RXFLR);
517 1.1 bouyer }
518 1.1 bouyer
519 1.1 bouyer if (rx_avail == 0) {
520 1.3 jakllsch device_printf(sc->sc_dev,
521 1.1 bouyer "timed out reading remaining %d\n",
522 1.1 bouyer (int)(len - 1 - readpos));
523 1.1 bouyer sc->sc_i2c_xfer.error = 1;
524 1.1 bouyer return (1);
525 1.1 bouyer }
526 1.1 bouyer
527 1.1 bouyer DPRINTF(("%s: %s: %d avail to read (%zu remaining)\n",
528 1.1 bouyer device_xname(sc->sc_dev), __func__, rx_avail,
529 1.1 bouyer len - readpos));
530 1.1 bouyer
531 1.1 bouyer while (rx_avail > 0) {
532 1.1 bouyer resp = dwiic_read(sc, DW_IC_DATA_CMD);
533 1.1 bouyer if (readpos < len) {
534 1.1 bouyer bdata[readpos] = resp;
535 1.1 bouyer readpos++;
536 1.1 bouyer }
537 1.1 bouyer rx_avail--;
538 1.1 bouyer }
539 1.1 bouyer
540 1.1 bouyer if (readpos >= len)
541 1.1 bouyer break;
542 1.1 bouyer
543 1.1 bouyer DPRINTF(("%s: still need to read %d bytes\n",
544 1.1 bouyer device_xname(sc->sc_dev), (int)(len - readpos)));
545 1.1 bouyer tx_limit = sc->tx_fifo_depth -
546 1.1 bouyer dwiic_read(sc, DW_IC_TXFLR);
547 1.1 bouyer }
548 1.1 bouyer }
549 1.1 bouyer
550 1.4 jakllsch if (I2C_OP_STOP_P(op) && I2C_OP_WRITE_P(op)) {
551 1.4 jakllsch if (flags & I2C_F_POLL) {
552 1.4 jakllsch /* wait for bus to be idle */
553 1.4 jakllsch for (retries = 100; retries > 0; retries--) {
554 1.4 jakllsch st = dwiic_read(sc, DW_IC_STATUS);
555 1.4 jakllsch if (!(st & DW_IC_STATUS_ACTIVITY))
556 1.4 jakllsch break;
557 1.4 jakllsch DELAY(1000);
558 1.4 jakllsch }
559 1.4 jakllsch if (st & DW_IC_STATUS_ACTIVITY)
560 1.4 jakllsch device_printf(sc->sc_dev, "timed out waiting "
561 1.4 jakllsch "for bus idle\n");
562 1.4 jakllsch } else {
563 1.4 jakllsch mutex_enter(&sc->sc_int_lock);
564 1.4 jakllsch dwiic_read(sc, DW_IC_CLR_INTR);
565 1.4 jakllsch dwiic_write(sc, DW_IC_INTR_MASK,
566 1.4 jakllsch DW_IC_INTR_STOP_DET);
567 1.4 jakllsch if (cv_timedwait(&sc->sc_int_stopwait,
568 1.4 jakllsch &sc->sc_int_lock, hz / 2) != 0)
569 1.4 jakllsch device_printf(sc->sc_dev, "timed out waiting "
570 1.4 jakllsch "for stop intr\n");
571 1.4 jakllsch dwiic_write(sc, DW_IC_INTR_MASK, 0);
572 1.4 jakllsch dwiic_read(sc, DW_IC_CLR_INTR);
573 1.4 jakllsch mutex_exit(&sc->sc_int_lock);
574 1.4 jakllsch }
575 1.4 jakllsch }
576 1.4 jakllsch
577 1.1 bouyer return 0;
578 1.1 bouyer }
579 1.1 bouyer
580 1.1 bouyer static uint32_t
581 1.1 bouyer dwiic_read_clear_intrbits(struct dwiic_softc *sc)
582 1.1 bouyer {
583 1.1 bouyer uint32_t stat;
584 1.1 bouyer
585 1.1 bouyer stat = dwiic_read(sc, DW_IC_INTR_STAT);
586 1.1 bouyer
587 1.1 bouyer if (stat & DW_IC_INTR_RX_UNDER)
588 1.1 bouyer dwiic_read(sc, DW_IC_CLR_RX_UNDER);
589 1.1 bouyer if (stat & DW_IC_INTR_RX_OVER)
590 1.1 bouyer dwiic_read(sc, DW_IC_CLR_RX_OVER);
591 1.1 bouyer if (stat & DW_IC_INTR_TX_OVER)
592 1.1 bouyer dwiic_read(sc, DW_IC_CLR_TX_OVER);
593 1.1 bouyer if (stat & DW_IC_INTR_RD_REQ)
594 1.1 bouyer dwiic_read(sc, DW_IC_CLR_RD_REQ);
595 1.1 bouyer if (stat & DW_IC_INTR_TX_ABRT)
596 1.1 bouyer dwiic_read(sc, DW_IC_CLR_TX_ABRT);
597 1.1 bouyer if (stat & DW_IC_INTR_RX_DONE)
598 1.1 bouyer dwiic_read(sc, DW_IC_CLR_RX_DONE);
599 1.1 bouyer if (stat & DW_IC_INTR_ACTIVITY)
600 1.1 bouyer dwiic_read(sc, DW_IC_CLR_ACTIVITY);
601 1.1 bouyer if (stat & DW_IC_INTR_STOP_DET)
602 1.1 bouyer dwiic_read(sc, DW_IC_CLR_STOP_DET);
603 1.1 bouyer if (stat & DW_IC_INTR_START_DET)
604 1.1 bouyer dwiic_read(sc, DW_IC_CLR_START_DET);
605 1.1 bouyer if (stat & DW_IC_INTR_GEN_CALL)
606 1.1 bouyer dwiic_read(sc, DW_IC_CLR_GEN_CALL);
607 1.1 bouyer
608 1.1 bouyer return stat;
609 1.1 bouyer }
610 1.1 bouyer
611 1.1 bouyer int
612 1.1 bouyer dwiic_intr(void *arg)
613 1.1 bouyer {
614 1.1 bouyer struct dwiic_softc *sc = arg;
615 1.1 bouyer uint32_t en, stat;
616 1.1 bouyer
617 1.1 bouyer en = dwiic_read(sc, DW_IC_ENABLE);
618 1.1 bouyer /* probably for the other controller */
619 1.1 bouyer if (!en)
620 1.1 bouyer return 0;
621 1.1 bouyer
622 1.1 bouyer stat = dwiic_read_clear_intrbits(sc);
623 1.1 bouyer DPRINTF(("%s: %s: enabled=0x%x stat=0x%x\n", device_xname(sc->sc_dev),
624 1.1 bouyer __func__, en, stat));
625 1.1 bouyer if (!(stat & ~DW_IC_INTR_ACTIVITY))
626 1.1 bouyer return 1;
627 1.1 bouyer
628 1.1 bouyer if (stat & DW_IC_INTR_TX_ABRT)
629 1.1 bouyer sc->sc_i2c_xfer.error = 1;
630 1.1 bouyer
631 1.1 bouyer if (sc->sc_i2c_xfer.flags & I2C_F_POLL)
632 1.1 bouyer DPRINTF(("%s: %s: intr in poll mode?\n", device_xname(sc->sc_dev),
633 1.1 bouyer __func__));
634 1.1 bouyer else {
635 1.1 bouyer mutex_enter(&sc->sc_int_lock);
636 1.1 bouyer if (stat & DW_IC_INTR_RX_FULL) {
637 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
638 1.1 bouyer DPRINTF(("%s: %s: waking up reader\n",
639 1.1 bouyer device_xname(sc->sc_dev), __func__));
640 1.1 bouyer cv_signal(&sc->sc_int_readwait);
641 1.1 bouyer }
642 1.1 bouyer if (stat & DW_IC_INTR_TX_EMPTY) {
643 1.1 bouyer dwiic_write(sc, DW_IC_INTR_MASK, 0);
644 1.1 bouyer DPRINTF(("%s: %s: waking up writer\n",
645 1.1 bouyer device_xname(sc->sc_dev), __func__));
646 1.1 bouyer cv_signal(&sc->sc_int_writewait);
647 1.1 bouyer }
648 1.4 jakllsch if (stat & DW_IC_INTR_STOP_DET) {
649 1.4 jakllsch dwiic_write(sc, DW_IC_INTR_MASK, 0);
650 1.4 jakllsch DPRINTF(("%s: %s: waking up stopper\n",
651 1.4 jakllsch device_xname(sc->sc_dev), __func__));
652 1.4 jakllsch cv_signal(&sc->sc_int_stopwait);
653 1.4 jakllsch }
654 1.1 bouyer mutex_exit(&sc->sc_int_lock);
655 1.1 bouyer }
656 1.1 bouyer
657 1.1 bouyer return 1;
658 1.1 bouyer }
659