tegra_i2c.c revision 1.19 1 /* $NetBSD: tegra_i2c.c,v 1.19 2018/07/16 23:11:47 christos 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.19 2018/07/16 23:11:47 christos 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 static i2c_tag_t tegra_i2c_get_tag(device_t);
51
52 struct fdtbus_i2c_controller_func tegra_i2c_funcs = {
53 .get_tag = tegra_i2c_get_tag
54 };
55
56 struct tegra_i2c_softc {
57 device_t sc_dev;
58 bus_space_tag_t sc_bst;
59 bus_space_handle_t sc_bsh;
60 void * sc_ih;
61 struct clk * sc_clk;
62 struct fdtbus_reset * sc_rst;
63 u_int sc_cid;
64
65 struct i2c_controller sc_ic;
66 kmutex_t sc_lock;
67 kcondvar_t sc_cv;
68 };
69
70 static void tegra_i2c_init(struct tegra_i2c_softc *);
71 static int tegra_i2c_intr(void *);
72
73 static int tegra_i2c_acquire_bus(void *, int);
74 static void tegra_i2c_release_bus(void *, int);
75 static int tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
76 size_t, void *, size_t, int);
77
78 static int tegra_i2c_wait(struct tegra_i2c_softc *, int);
79 static int tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
80 const uint8_t *, size_t, int, bool);
81 static int tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
82 size_t, int);
83
84 CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
85 tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
86
87 #define I2C_WRITE(sc, reg, val) \
88 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
89 #define I2C_READ(sc, reg) \
90 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
91 #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
92 tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
93
94 static int
95 tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
96 {
97 const char * const compatible[] = {
98 "nvidia,tegra210-i2c",
99 "nvidia,tegra124-i2c",
100 "nvidia,tegra114-i2c",
101 NULL
102 };
103 struct fdt_attach_args * const faa = aux;
104
105 return of_match_compatible(faa->faa_phandle, compatible);
106 }
107
108 static void
109 tegra_i2c_attach(device_t parent, device_t self, void *aux)
110 {
111 struct tegra_i2c_softc * const sc = device_private(self);
112 struct fdt_attach_args * const faa = aux;
113 const int phandle = faa->faa_phandle;
114 char intrstr[128];
115 bus_addr_t addr;
116 bus_size_t size;
117 int error;
118
119 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
120 aprint_error(": couldn't get registers\n");
121 return;
122 }
123 sc->sc_clk = fdtbus_clock_get(phandle, "div-clk");
124 if (sc->sc_clk == NULL) {
125 aprint_error(": couldn't get clock div-clk\n");
126 return;
127 }
128 sc->sc_rst = fdtbus_reset_get(phandle, "i2c");
129 if (sc->sc_rst == NULL) {
130 aprint_error(": couldn't get reset i2c\n");
131 return;
132 }
133
134 sc->sc_dev = self;
135 sc->sc_bst = faa->faa_bst;
136 sc->sc_cid = device_unit(self);
137 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
138 if (error) {
139 aprint_error(": couldn't map %#" PRIx64 ": %d",
140 (uint64_t)addr, error);
141 return;
142 }
143 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
144 cv_init(&sc->sc_cv, device_xname(self));
145
146 aprint_naive("\n");
147 aprint_normal(": I2C\n");
148
149 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
150 aprint_error_dev(self, "failed to decode interrupt\n");
151 return;
152 }
153
154 sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
155 FDT_INTR_MPSAFE, tegra_i2c_intr, sc);
156 if (sc->sc_ih == NULL) {
157 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
158 intrstr);
159 return;
160 }
161 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
162
163 /*
164 * Recommended setting for standard mode is to use an I2C source div
165 * of 20 (Tegra K1 Technical Reference Manual, Table 137)
166 */
167 fdtbus_reset_assert(sc->sc_rst);
168 error = clk_set_rate(sc->sc_clk, 20400000);
169 if (error) {
170 aprint_error_dev(self, "couldn't set frequency: %d\n", error);
171 return;
172 }
173 error = clk_enable(sc->sc_clk);
174 if (error) {
175 aprint_error_dev(self, "couldn't enable clock: %d\n", error);
176 return;
177 }
178 fdtbus_reset_deassert(sc->sc_rst);
179
180 tegra_i2c_init(sc);
181
182 sc->sc_ic.ic_cookie = sc;
183 sc->sc_ic.ic_acquire_bus = tegra_i2c_acquire_bus;
184 sc->sc_ic.ic_release_bus = tegra_i2c_release_bus;
185 sc->sc_ic.ic_exec = tegra_i2c_exec;
186
187 fdtbus_register_i2c_controller(self, phandle, &tegra_i2c_funcs);
188
189 fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
190 }
191
192 static i2c_tag_t
193 tegra_i2c_get_tag(device_t dev)
194 {
195 struct tegra_i2c_softc * const sc = device_private(dev);
196
197 return &sc->sc_ic;
198 }
199
200 static void
201 tegra_i2c_init(struct tegra_i2c_softc *sc)
202 {
203 int retry = 10000;
204
205 I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
206 __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
207 __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
208
209 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
210 I2C_WRITE(sc, I2C_CNFG_REG,
211 I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN);
212 I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
213 I2C_WRITE(sc, I2C_FIFO_CONTROL_REG,
214 __SHIFTIN(7, I2C_FIFO_CONTROL_TX_FIFO_TRIG) |
215 __SHIFTIN(0, I2C_FIFO_CONTROL_RX_FIFO_TRIG));
216
217 I2C_WRITE(sc, I2C_BUS_CONFIG_LOAD_REG,
218 I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD);
219 while (--retry > 0) {
220 if (I2C_READ(sc, I2C_BUS_CONFIG_LOAD_REG) == 0)
221 break;
222 delay(10);
223 }
224 if (retry == 0) {
225 device_printf(sc->sc_dev, "config load timeout\n");
226 }
227 }
228
229 static int
230 tegra_i2c_intr(void *priv)
231 {
232 struct tegra_i2c_softc * const sc = priv;
233
234 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
235 if (istatus == 0)
236 return 0;
237 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
238
239 mutex_enter(&sc->sc_lock);
240 cv_broadcast(&sc->sc_cv);
241 mutex_exit(&sc->sc_lock);
242
243 return 1;
244 }
245
246 static int
247 tegra_i2c_acquire_bus(void *priv, int flags)
248 {
249 struct tegra_i2c_softc * const sc = priv;
250
251 mutex_enter(&sc->sc_lock);
252
253 return 0;
254 }
255
256 static void
257 tegra_i2c_release_bus(void *priv, int flags)
258 {
259 struct tegra_i2c_softc * const sc = priv;
260
261 mutex_exit(&sc->sc_lock);
262 }
263
264 static int
265 tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
266 size_t cmdlen, void *buf, size_t buflen, int flags)
267 {
268 struct tegra_i2c_softc * const sc = priv;
269 int retry, error;
270
271 #if notyet
272 if (cold)
273 #endif
274 flags |= I2C_F_POLL;
275
276 KASSERT(mutex_owned(&sc->sc_lock));
277
278 if ((flags & I2C_F_POLL) == 0) {
279 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
280 I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
281 I2C_INTERRUPT_MASK_TIMEOUT |
282 I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
283 }
284
285 const uint32_t flush_mask =
286 I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
287
288 I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
289 for (retry = 10000; retry > 0; retry--) {
290 const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
291 if ((v & flush_mask) == 0)
292 break;
293 delay(1);
294 }
295 if (retry == 0) {
296 device_printf(sc->sc_dev, "timeout flushing FIFO\n");
297 return EIO;
298 }
299
300 if (cmdlen > 0) {
301 error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags,
302 buflen > 0 ? true : false);
303 if (error) {
304 goto done;
305 }
306 }
307
308 if (I2C_OP_READ_P(op)) {
309 error = tegra_i2c_read(sc, addr, buf, buflen, flags);
310 } else {
311 error = tegra_i2c_write(sc, addr, buf, buflen, flags, false);
312 }
313
314 done:
315 if ((flags & I2C_F_POLL) == 0) {
316 I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
317 }
318
319 if (error) {
320 tegra_i2c_init(sc);
321 }
322
323 return error;
324 }
325
326 static int
327 tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
328 {
329 int error, retry;
330 uint32_t stat = 0;
331
332 retry = (flags & I2C_F_POLL) ? 100000 : 100;
333
334 while (--retry > 0) {
335 if ((flags & I2C_F_POLL) == 0) {
336 error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
337 max(mstohz(10), 1));
338 if (error) {
339 return error;
340 }
341 }
342 stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
343 if (stat & I2C_INTERRUPT_STATUS_PACKET_XFER_COMPLETE) {
344 break;
345 }
346 if (flags & I2C_F_POLL) {
347 delay(10);
348 }
349 }
350 if (retry == 0) {
351 stat = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
352 device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
353 return ETIMEDOUT;
354 }
355
356 const uint32_t err_mask =
357 I2C_INTERRUPT_STATUS_NOACK |
358 I2C_INTERRUPT_STATUS_ARB_LOST |
359 I2C_INTERRUPT_MASK_TIMEOUT;
360
361 if (stat & err_mask) {
362 device_printf(sc->sc_dev, "error, status = %#x\n", stat);
363 return EIO;
364 }
365
366 return 0;
367 }
368
369 static int
370 tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
371 size_t buflen, int flags, bool repeat_start)
372 {
373 const uint8_t *p = buf;
374 size_t n, resid = buflen;
375 uint32_t data;
376 int retry;
377
378 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
379 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
380
381 /* Generic Header 0 */
382 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
383 __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
384 I2C_IOPACKET_WORD0_PROTHDRSZ) |
385 __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
386 __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
387 __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
388 I2C_IOPACKET_WORD0_PROTOCOL) |
389 __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
390 I2C_IOPACKET_WORD0_PKTTYPE));
391 /* Generic Header 1 */
392 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
393 __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
394 /* I2C Master Transmit Packet Header */
395 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
396 I2C_IOPACKET_XMITHDR_IE |
397 (repeat_start ? I2C_IOPACKET_XMITHDR_REPEAT_STARTSTOP : 0) |
398 __SHIFTIN((addr << 1), I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
399
400 /* Transmit data */
401 while (resid > 0) {
402 retry = 10000;
403 while (--retry > 0) {
404 const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
405 const u_int cnt =
406 __SHIFTOUT(fs, I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT);
407 if (cnt > 0)
408 break;
409 delay(10);
410 }
411 if (retry == 0) {
412 device_printf(sc->sc_dev, "TX FIFO timeout\n");
413 return ETIMEDOUT;
414 }
415
416 for (n = 0, data = 0; n < min(resid, 4); n++) {
417 data |= (uint32_t)p[n] << (n * 8);
418 }
419 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG, data);
420 p += min(resid, 4);
421 resid -= min(resid, 4);
422 }
423
424 return tegra_i2c_wait(sc, flags);
425 }
426
427 static int
428 tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
429 size_t buflen, int flags)
430 {
431 uint8_t *p = buf;
432 size_t n, resid = buflen;
433 uint32_t data;
434 int retry;
435
436 const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
437 I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
438
439 /* Generic Header 0 */
440 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
441 __SHIFTIN(I2C_IOPACKET_WORD0_PROTHDRSZ_REQ,
442 I2C_IOPACKET_WORD0_PROTHDRSZ) |
443 __SHIFTIN(sc->sc_cid, I2C_IOPACKET_WORD0_CONTROLLERID) |
444 __SHIFTIN(1, I2C_IOPACKET_WORD0_PKTID) |
445 __SHIFTIN(I2C_IOPACKET_WORD0_PROTOCOL_I2C,
446 I2C_IOPACKET_WORD0_PROTOCOL) |
447 __SHIFTIN(I2C_IOPACKET_WORD0_PKTTYPE_REQ,
448 I2C_IOPACKET_WORD0_PKTTYPE));
449 /* Generic Header 1 */
450 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
451 __SHIFTIN(buflen - 1, I2C_IOPACKET_WORD1_PAYLOADSIZE));
452 /* I2C Master Transmit Packet Header */
453 I2C_WRITE(sc, I2C_TX_PACKET_FIFO_REG,
454 I2C_IOPACKET_XMITHDR_IE | I2C_IOPACKET_XMITHDR_READ |
455 __SHIFTIN((addr << 1) | 1, I2C_IOPACKET_XMITHDR_SLAVE_ADDR));
456
457 while (resid > 0) {
458 retry = 10000;
459 while (--retry > 0) {
460 const uint32_t fs = I2C_READ(sc, I2C_FIFO_STATUS_REG);
461 const u_int cnt =
462 __SHIFTOUT(fs, I2C_FIFO_STATUS_RX_FIFO_FULL_CNT);
463 if (cnt > 0)
464 break;
465 delay(10);
466 }
467 if (retry == 0) {
468 device_printf(sc->sc_dev, "RX FIFO timeout\n");
469 return ETIMEDOUT;
470 }
471
472 data = I2C_READ(sc, I2C_RX_FIFO_REG);
473 for (n = 0; n < min(resid, 4); n++) {
474 p[n] = (data >> (n * 8)) & 0xff;
475 }
476 p += min(resid, 4);
477 resid -= min(resid, 4);
478 }
479
480 return tegra_i2c_wait(sc, flags);
481 }
482