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