tegra_i2c.c revision 1.26.14.1 1 /* $NetBSD: tegra_i2c.c,v 1.26.14.1 2021/08/09 00:30:07 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.26.14.1 2021/08/09 00:30:07 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38
39 #include <dev/i2c/i2cvar.h>
40
41 #include <arm/nvidia/tegra_reg.h>
42 #include <arm/nvidia/tegra_i2creg.h>
43 #include <arm/nvidia/tegra_var.h>
44
45 #include <dev/fdt/fdtvar.h>
46
47 static int tegra_i2c_match(device_t, cfdata_t, void *);
48 static void tegra_i2c_attach(device_t, device_t, void *);
49
50 struct tegra_i2c_softc {
51 device_t sc_dev;
52 bus_space_tag_t sc_bst;
53 bus_space_handle_t sc_bsh;
54 void * sc_ih;
55 struct clk * sc_clk;
56 struct fdtbus_reset * sc_rst;
57 u_int sc_cid;
58
59 struct i2c_controller sc_ic;
60 kmutex_t sc_intr_lock;
61 kcondvar_t sc_intr_wait;
62 };
63
64 static void tegra_i2c_init(struct tegra_i2c_softc *);
65 static int tegra_i2c_intr(void *);
66
67 static int tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
68 size_t, void *, size_t, int);
69
70 static int tegra_i2c_wait(struct tegra_i2c_softc *, int);
71 static int tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
72 const uint8_t *, size_t, int, bool);
73 static int tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
74 size_t, int);
75
76 CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
77 tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
78
79 #define I2C_WRITE(sc, reg, val) \
80 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
81 #define I2C_READ(sc, reg) \
82 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
83 #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
84 tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
85
86 static const struct device_compatible_entry compat_data[] = {
87 { .compat = "nvidia,tegra210-i2c" },
88 { .compat = "nvidia,tegra124-i2c" },
89 { .compat = "nvidia,tegra114-i2c" },
90 DEVICE_COMPAT_EOL
91 };
92
93 static int
94 tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
95 {
96 struct fdt_attach_args * const faa = aux;
97
98 return of_compatible_match(faa->faa_phandle, compat_data);
99 }
100
101 static void
102 tegra_i2c_attach(device_t parent, device_t self, void *aux)
103 {
104 struct tegra_i2c_softc * const sc = device_private(self);
105 struct fdt_attach_args * const faa = aux;
106 const int phandle = faa->faa_phandle;
107 char intrstr[128];
108 bus_addr_t addr;
109 bus_size_t size;
110 int error;
111
112 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
113 aprint_error(": couldn't get registers\n");
114 return;
115 }
116 sc->sc_clk = fdtbus_clock_get(phandle, "div-clk");
117 if (sc->sc_clk == NULL) {
118 aprint_error(": couldn't get clock div-clk\n");
119 return;
120 }
121 sc->sc_rst = fdtbus_reset_get(phandle, "i2c");
122 if (sc->sc_rst == NULL) {
123 aprint_error(": couldn't get reset i2c\n");
124 return;
125 }
126
127 sc->sc_dev = self;
128 sc->sc_bst = faa->faa_bst;
129 sc->sc_cid = device_unit(self);
130 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
131 if (error) {
132 aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
133 addr, error);
134 return;
135 }
136 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
137 cv_init(&sc->sc_intr_wait, device_xname(self));
138
139 aprint_naive("\n");
140 aprint_normal(": I2C\n");
141
142 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
143 aprint_error_dev(self, "failed to decode interrupt\n");
144 return;
145 }
146
147 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
148 FDT_INTR_MPSAFE, tegra_i2c_intr, sc, device_xname(self));
149 if (sc->sc_ih == NULL) {
150 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
151 intrstr);
152 return;
153 }
154 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
155
156 /*
157 * Recommended setting for standard mode is to use an I2C source div
158 * of 20 (Tegra K1 Technical Reference Manual, Table 137)
159 */
160 fdtbus_reset_assert(sc->sc_rst);
161 error = clk_set_rate(sc->sc_clk, 20400000);
162 if (error) {
163 aprint_error_dev(self, "couldn't set frequency: %d\n", error);
164 return;
165 }
166 error = clk_enable(sc->sc_clk);
167 if (error) {
168 aprint_error_dev(self, "couldn't enable clock: %d\n", error);
169 return;
170 }
171 fdtbus_reset_deassert(sc->sc_rst);
172
173 mutex_enter(&sc->sc_intr_lock);
174 tegra_i2c_init(sc);
175 mutex_exit(&sc->sc_intr_lock);
176
177 iic_tag_init(&sc->sc_ic);
178 sc->sc_ic.ic_cookie = sc;
179 sc->sc_ic.ic_exec = tegra_i2c_exec;
180
181 fdtbus_register_i2c_controller(&sc->sc_ic, phandle);
182
183 struct i2cbus_attach_args iba = {
184 .iba_tag = &sc->sc_ic,
185 };
186 config_found(self, &iba, iicbus_print,
187 CFARGS(.devhandle = device_handle(self)));
188 }
189
190 static void
191 tegra_i2c_init(struct tegra_i2c_softc *sc)
192 {
193 int retry = 10000;
194
195 I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
196 __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
197 __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
198
199 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
200 I2C_WRITE(sc, I2C_CNFG_REG,
201 I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN);
202 I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
203 I2C_WRITE(sc, I2C_FIFO_CONTROL_REG,
204 __SHIFTIN(7, I2C_FIFO_CONTROL_TX_FIFO_TRIG) |
205 __SHIFTIN(0, I2C_FIFO_CONTROL_RX_FIFO_TRIG));
206
207 I2C_WRITE(sc, I2C_BUS_CONFIG_LOAD_REG,
208 I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD);
209 while (--retry > 0) {
210 if (I2C_READ(sc, I2C_BUS_CONFIG_LOAD_REG) == 0)
211 break;
212 delay(10);
213 }
214 if (retry == 0) {
215 device_printf(sc->sc_dev, "config load timeout\n");
216 }
217 }
218
219 static int
220 tegra_i2c_intr(void *priv)
221 {
222 struct tegra_i2c_softc * const sc = priv;
223
224 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
225 if (istatus == 0)
226 return 0;
227 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
228
229 mutex_enter(&sc->sc_intr_lock);
230 cv_broadcast(&sc->sc_intr_wait);
231 mutex_exit(&sc->sc_intr_lock);
232
233 return 1;
234 }
235
236 static int
237 tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
238 size_t cmdlen, void *buf, size_t buflen, int flags)
239 {
240 struct tegra_i2c_softc * const sc = priv;
241 int retry, error;
242
243 /*
244 * XXXJRT This is probably no longer necessary? Before these
245 * changes, the bus lock was also used for the interrupt handler,
246 * and there would be a deadlock when the interrupt handler tried to
247 * acquire it again. The bus lock is now owned by the mid-layer and
248 * we have our own interrupt lock.
249 */
250 flags |= I2C_F_POLL;
251
252 if (buflen == 0 && cmdlen == 0)
253 return EINVAL;
254
255 mutex_enter(&sc->sc_intr_lock);
256
257 if ((flags & I2C_F_POLL) == 0) {
258 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
259 I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
260 I2C_INTERRUPT_MASK_TIMEOUT |
261 I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
262 }
263
264 const uint32_t flush_mask =
265 I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
266
267 I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
268 for (retry = 10000; retry > 0; retry--) {
269 const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
270 if ((v & flush_mask) == 0)
271 break;
272 delay(1);
273 }
274 if (retry == 0) {
275 mutex_exit(&sc->sc_intr_lock);
276 device_printf(sc->sc_dev, "timeout flushing FIFO\n");
277 return EIO;
278 }
279
280 if (cmdlen > 0) {
281 error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags,
282 buflen > 0 ? true : false);
283 if (error) {
284 goto done;
285 }
286 }
287
288 if (buflen > 0) {
289 if (I2C_OP_READ_P(op)) {
290 error = tegra_i2c_read(sc, addr, buf, buflen, flags);
291 } else {
292 error = tegra_i2c_write(sc, addr, buf, buflen, flags, false);
293 }
294 }
295
296 done:
297 if ((flags & I2C_F_POLL) == 0) {
298 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
299 }
300
301 if (error) {
302 tegra_i2c_init(sc);
303 }
304
305 mutex_exit(&sc->sc_intr_lock);
306
307 return error;
308 }
309
310 static int
311 tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
312 {
313 int error, retry;
314 uint32_t stat = 0;
315
316 retry = (flags & I2C_F_POLL) ? 100000 : 100;
317
318 while (--retry > 0) {
319 if ((flags & I2C_F_POLL) == 0) {
320 error = cv_timedwait_sig(&sc->sc_intr_wait,
321 &sc->sc_intr_lock,
322 uimax(mstohz(10), 1));
323 if (error) {
324 return error;
325 }
326 }
327 stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
328 if (stat & I2C_INTERRUPT_STATUS_PACKET_XFER_COMPLETE) {
329 break;
330 }
331 if (flags & I2C_F_POLL) {
332 delay(10);
333 }
334 }
335 if (retry == 0) {
336 #ifdef TEGRA_I2C_DEBUG
337 device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
338 #endif
339 return ETIMEDOUT;
340 }
341
342 const uint32_t err_mask =
343 I2C_INTERRUPT_STATUS_NOACK |
344 I2C_INTERRUPT_STATUS_ARB_LOST |
345 I2C_INTERRUPT_MASK_TIMEOUT;
346
347 if (stat & err_mask) {
348 device_printf(sc->sc_dev, "error, status = %#x\n", stat);
349 return EIO;
350 }
351
352 return 0;
353 }
354
355 static int
356 tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
357 size_t buflen, int flags, bool repeat_start)
358 {
359 const uint8_t *p = buf;
360 size_t n, resid = buflen;
361 uint32_t data;
362 int retry;
363
364 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
365 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
366
367 /* Generic Header 0 */
368 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
369 __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
370 I2C_IOPACKET_WORD0_PROTHDRSZ) |
371 __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
372 __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
373 __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
374 I2C_IOPACKET_WORD0_PROTOCOL) |
375 __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
376 I2C_IOPACKET_WORD0_PKTTYPE));
377 /* Generic Header 1 */
378 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
379 __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
380 /* I2C Master Transmit Packet Header */
381 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
382 I2C_IOPACKET_XMITHDR_IE |
383 (repeat_start ? I2C_IOPACKET_XMITHDR_REPEAT_STARTSTOP : 0) |
384 __SHIFTIN((addr << 1), I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
385
386 /* Transmit data */
387 while (resid > 0) {
388 retry = 10000;
389 while (--retry > 0) {
390 const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
391 const u_int cnt =
392 __SHIFTOUT(fs, I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT);
393 if (cnt > 0)
394 break;
395 delay(10);
396 }
397 if (retry == 0) {
398 device_printf(sc->sc_dev, "TX FIFO timeout\n");
399 return ETIMEDOUT;
400 }
401
402 for (n = 0, data = 0; n < uimin(resid, 4); n++) {
403 data |= (uint32_t)p[n] << (n * 8);
404 }
405 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG, data);
406 p += uimin(resid, 4);
407 resid -= uimin(resid, 4);
408 }
409
410 return tegra_i2c_wait(sc, flags);
411 }
412
413 static int
414 tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
415 size_t buflen, int flags)
416 {
417 uint8_t *p = buf;
418 size_t n, resid = buflen;
419 uint32_t data;
420 int retry;
421
422 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
423 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
424
425 /* Generic Header 0 */
426 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
427 __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
428 I2C_IOPACKET_WORD0_PROTHDRSZ) |
429 __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
430 __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
431 __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
432 I2C_IOPACKET_WORD0_PROTOCOL) |
433 __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
434 I2C_IOPACKET_WORD0_PKTTYPE));
435 /* Generic Header 1 */
436 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
437 __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
438 /* I2C Master Transmit Packet Header */
439 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
440 I2C_IOPACKET_XMITHDR_IE | I2C_IOPACKET_XMITHDR_READ |
441 __SHIFTIN((addr << 1) | 1, I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
442
443 while (resid > 0) {
444 retry = 10000;
445 while (--retry > 0) {
446 const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
447 const u_int cnt =
448 __SHIFTOUT(fs, I2C_FIFO_STATUS_RX_FIFO_FULL_CNT);
449 if (cnt > 0)
450 break;
451 delay(10);
452 }
453 if (retry == 0) {
454 device_printf(sc->sc_dev, "RX FIFO timeout\n");
455 return ETIMEDOUT;
456 }
457
458 data = I2C_READ(sc, I2C_RX_FIFO_REG);
459 for (n = 0; n < uimin(resid, 4); n++) {
460 p[n] = (data >> (n * 8)) & 0xff;
461 }
462 p += uimin(resid, 4);
463 resid -= uimin(resid, 4);
464 }
465
466 return tegra_i2c_wait(sc, flags);
467 }
468