xref: /netbsd-src/sys/arch/arm/nvidia/tegra_cec.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: tegra_cec.c,v 1.4 2018/07/16 23:11:47 christos 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_cec.c,v 1.4 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 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/poll.h>
41 #include <sys/select.h>
42 
43 #include <dev/hdmicec/hdmicecio.h>
44 #include <dev/hdmicec/hdmicec_if.h>
45 
46 #include <arm/nvidia/tegra_var.h>
47 #include <arm/nvidia/tegra_pmcreg.h>
48 #include <arm/nvidia/tegra_cecreg.h>
49 
50 #include <dev/fdt/fdtvar.h>
51 
52 #define CEC_VENDORID_NVIDIA	0x00044b
53 
54 static int	tegra_cec_match(device_t, cfdata_t, void *);
55 static void	tegra_cec_attach(device_t, device_t, void *);
56 
57 static int	tegra_cec_intr(void *);
58 
59 struct tegra_cec_softc {
60 	device_t		sc_dev;
61 	bus_space_tag_t		sc_bst;
62 	bus_space_handle_t	sc_bsh;
63 	void			*sc_ih;
64 	struct clk		*sc_clk;
65 	struct fdtbus_reset	*sc_rst;
66 
67 	kmutex_t		sc_lock;
68 	kcondvar_t		sc_cv;
69 
70 	const char		*sc_hdmidevname;
71 	device_t		sc_cecdev;
72 
73 	struct selinfo		sc_selinfo;
74 
75 	uint8_t			sc_rxbuf[16];
76 	int			sc_rxlen;
77 	bool			sc_rxdone;
78 
79 	uint8_t			sc_txbuf[16];
80 	int			sc_txlen;
81 	int			sc_txcur;
82 	int			sc_txerr;
83 	bool			sc_txdone;
84 };
85 
86 static void	tegra_cec_reset(struct tegra_cec_softc *);
87 
88 static int	tegra_cec_open(void *, int);
89 static void	tegra_cec_close(void *);
90 static int	tegra_cec_ioctl(void *, u_long, void *, int, lwp_t *);
91 static int	tegra_cec_send(void *, const uint8_t *, size_t);
92 static ssize_t	tegra_cec_recv(void *, uint8_t *, size_t);
93 static int	tegra_cec_poll(void *, int, lwp_t *);
94 
95 static const struct hdmicec_hw_if tegra_cec_hw_if = {
96 	.open = tegra_cec_open,
97 	.close = tegra_cec_close,
98 	.ioctl = tegra_cec_ioctl,
99 	.send = tegra_cec_send,
100 	.recv = tegra_cec_recv,
101 	.poll = tegra_cec_poll,
102 };
103 
104 CFATTACH_DECL_NEW(tegra_cec, sizeof(struct tegra_cec_softc),
105 	tegra_cec_match, tegra_cec_attach, NULL, NULL);
106 
107 #define CEC_READ(sc, reg)			\
108     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
109 #define CEC_WRITE(sc, reg, val)			\
110     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
111 #define CEC_SET_CLEAR(sc, reg, set, clr)	\
112     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
113 
114 static int
115 tegra_cec_match(device_t parent, cfdata_t cf, void *aux)
116 {
117 	const char * const compatible[] = { "nvidia,tegra124-cec", NULL };
118 	struct fdt_attach_args * const faa = aux;
119 
120 	return of_match_compatible(faa->faa_phandle, compatible);
121 }
122 
123 static void
124 tegra_cec_attach(device_t parent, device_t self, void *aux)
125 {
126 	struct tegra_cec_softc * const sc = device_private(self);
127 	struct fdt_attach_args * const faa = aux;
128 	prop_dictionary_t prop = device_properties(self);
129 	struct hdmicec_attach_args caa;
130 	char intrstr[128];
131 	bus_addr_t addr;
132 	bus_size_t size;
133 	int error;
134 
135 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
136 		aprint_error(": couldn't get registers\n");
137 		return;
138 	}
139 	sc->sc_clk = fdtbus_clock_get(faa->faa_phandle, "cec");
140 	if (sc->sc_clk == NULL) {
141 		aprint_error(": couldn't get clock cec\n");
142 		return;
143 	}
144 	sc->sc_rst = fdtbus_reset_get(faa->faa_phandle, "cec");
145 	if (sc->sc_rst == NULL) {
146 		aprint_error(": couldn't get reset cec\n");
147 		return;
148 	}
149 
150 	sc->sc_dev = self;
151 	sc->sc_bst = faa->faa_bst;
152 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
153 	if (error) {
154 		aprint_error(": couldn't map %#" PRIx64 ": %d",
155 		    (uint64_t)addr, error);
156 		return;
157 	}
158 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
159 	cv_init(&sc->sc_cv, "tegracec");
160 	selinit(&sc->sc_selinfo);
161 
162 	aprint_naive("\n");
163 	aprint_normal(": HDMI CEC\n");
164 
165 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
166 		aprint_error_dev(self, "failed to decode interrupt\n");
167 		return;
168 	}
169 
170 	sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_VM,
171 	    FDT_INTR_MPSAFE, tegra_cec_intr, sc);
172 	if (sc->sc_ih == NULL) {
173 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
174 		    intrstr);
175 		return;
176 	}
177 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
178 
179 	prop_dictionary_get_cstring_nocopy(prop, "hdmi-device",
180 	    &sc->sc_hdmidevname);
181 
182 	fdtbus_reset_assert(sc->sc_rst);
183 	error = clk_enable(sc->sc_clk);
184 	if (error) {
185 		aprint_error_dev(self, "couldn't enable cec: %d\n", error);
186 		return;
187 	}
188 	fdtbus_reset_deassert(sc->sc_rst);
189 
190 	CEC_WRITE(sc, CEC_SW_CONTROL_REG, 0);
191 	CEC_WRITE(sc, CEC_INPUT_FILTER_REG, 0);
192 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
193 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
194 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
195 
196 	memset(&caa, 0, sizeof(caa));
197 	caa.priv = sc;
198 	caa.hwif = &tegra_cec_hw_if;
199 	sc->sc_cecdev = config_found(self, &caa, NULL);
200 }
201 
202 static int
203 tegra_cec_intr(void *priv)
204 {
205 	struct tegra_cec_softc * const sc = priv;
206 	uint32_t val;
207 	int handled = 0;
208 
209 	mutex_enter(&sc->sc_lock);
210 	const uint32_t int_stat = CEC_READ(sc, CEC_INT_STAT_REG);
211 
212 	if (int_stat & CEC_INT_RX_REGISTER_FULL) {
213 		val = CEC_READ(sc, CEC_RX_REGISTER_REG);
214 		sc->sc_rxbuf[sc->sc_rxlen++] =
215 		    __SHIFTOUT(val, CEC_RX_REGISTER_DATA);
216 		if ((val & CEC_RX_REGISTER_EOM) != 0 ||
217 		    sc->sc_rxlen == 16) {
218 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
219 			    CEC_INT_RX_REGISTER_FULL);
220 			sc->sc_rxdone = true;
221 			cv_broadcast(&sc->sc_cv);
222 			selnotify(&sc->sc_selinfo, POLLIN|POLLRDNORM,
223 			    NOTE_SUBMIT);
224 		}
225 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_RX_REGISTER_FULL);
226 		++handled;
227 	}
228 
229 	if (int_stat & CEC_INT_TX_REGISTER_EMPTY) {
230 		if (sc->sc_txcur < sc->sc_txlen) {
231 			const uint8_t destination = sc->sc_txbuf[0] & 0xf;
232 			val = __SHIFTIN(sc->sc_txbuf[sc->sc_txcur],
233 			    CEC_TX_REGISTER_DATA);
234 			if (sc->sc_txcur == 0)
235 				val |= CEC_TX_REGISTER_GENERATE_START_BIT;
236 			if (sc->sc_txcur == sc->sc_txlen - 1)
237 				val |= CEC_TX_REGISTER_EOM;
238 			if (destination == 0xf)
239 				val |= CEC_TX_REGISTER_ADDRESS_MODE;
240 
241 			CEC_WRITE(sc, CEC_TX_REGISTER_REG, val);
242 			CEC_WRITE(sc, CEC_INT_STAT_REG,
243 			    CEC_INT_TX_REGISTER_EMPTY);
244 			++sc->sc_txcur;
245 		} else {
246 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
247 			    CEC_INT_TX_REGISTER_EMPTY);
248 		}
249 		++handled;
250 	}
251 
252 	if (int_stat & CEC_INT_TX_FRAME_TRANSMITTED) {
253 		CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
254 		    CEC_INT_TX_FRAME_TRANSMITTED |
255 		    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
256 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_TX_FRAME_TRANSMITTED);
257 		if (int_stat & CEC_INT_TX_FRAME_OR_BLOCK_NAKD) {
258 			CEC_WRITE(sc, CEC_INT_STAT_REG,
259 			    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
260 			sc->sc_txerr = ECONNREFUSED;
261 			tegra_cec_reset(sc);
262 		}
263 		sc->sc_txdone = true;
264 		cv_broadcast(&sc->sc_cv);
265 		++handled;
266 	}
267 
268 	if (int_stat & CEC_INT_TX_REGISTER_UNDERRUN) {
269 		tegra_cec_reset(sc);
270 		cv_broadcast(&sc->sc_cv);
271 		++handled;
272 	}
273 
274 	mutex_exit(&sc->sc_lock);
275 
276 	return handled;
277 }
278 
279 static void
280 tegra_cec_reset(struct tegra_cec_softc *sc)
281 {
282 	uint32_t val;
283 
284 	KASSERT(mutex_owned(&sc->sc_lock));
285 
286 	val = CEC_READ(sc, CEC_HW_CONTROL_REG);
287 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
288 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
289 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, val);
290 }
291 
292 static int
293 tegra_cec_open(void *priv, int flag)
294 {
295 	struct tegra_cec_softc * const sc = priv;
296 
297 	mutex_enter(&sc->sc_lock);
298 	sc->sc_rxlen = 0;
299 	sc->sc_rxdone = false;
300 	CEC_WRITE(sc, CEC_INT_MASK_REG, CEC_INT_RX_REGISTER_FULL);
301 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, CEC_HW_CONTROL_TX_RX_MODE);
302 	mutex_exit(&sc->sc_lock);
303 
304 	return 0;
305 }
306 
307 static void
308 tegra_cec_close(void *priv)
309 {
310 	struct tegra_cec_softc * const sc = priv;
311 
312 	mutex_enter(&sc->sc_lock);
313 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
314 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
315 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
316 	mutex_exit(&sc->sc_lock);
317 }
318 
319 static int
320 tegra_cec_get_phys_addr(struct tegra_cec_softc *sc, uint16_t *phys_addr)
321 {
322 	device_t hdmidev;
323 
324 	if (sc->sc_hdmidevname == NULL)
325 		return EIO;
326 	hdmidev = device_find_by_xname(sc->sc_hdmidevname);
327 	if (hdmidev == NULL)
328 		return ENXIO;
329 
330 	const prop_dictionary_t prop = device_properties(hdmidev);
331 	if (!prop_dictionary_get_uint16(prop, "physical-address", phys_addr))
332 		return ENOTCONN;
333 
334 	return 0;
335 }
336 
337 static int
338 tegra_cec_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
339 {
340 	struct tegra_cec_softc * const sc = priv;
341 	uint32_t val;
342 
343 	switch (cmd) {
344 	case CEC_GET_PHYS_ADDR:
345 		return tegra_cec_get_phys_addr(sc, data);
346 	case CEC_GET_LOG_ADDRS:
347 		val = CEC_READ(sc, CEC_HW_CONTROL_REG);
348 		*(uint16_t *)data =
349 		    __SHIFTOUT(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
350 		return 0;
351 	case CEC_SET_LOG_ADDRS:
352 		val = *(uint16_t *)data & 0x7fff;
353 		CEC_SET_CLEAR(sc, CEC_HW_CONTROL_REG,
354 		    __SHIFTIN(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS),
355 		    CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
356 		return 0;
357 	case CEC_GET_VENDOR_ID:
358 		*(uint32_t *)data = CEC_VENDORID_NVIDIA;
359 		return 0;
360 	default:
361 		return EINVAL;
362 	}
363 }
364 
365 static int
366 tegra_cec_send(void *priv, const uint8_t *data, size_t len)
367 {
368 	struct tegra_cec_softc * const sc = priv;
369 	int error = 0;
370 
371 	mutex_enter(&sc->sc_lock);
372 
373 	sc->sc_txdone = false;
374 	sc->sc_txcur = 0;
375 	sc->sc_txerr = 0;
376 	memcpy(sc->sc_txbuf, data, len);
377 	sc->sc_txlen = len;
378 
379 	CEC_SET_CLEAR(sc, CEC_INT_MASK_REG,
380 	    CEC_INT_TX_REGISTER_EMPTY |
381 	    CEC_INT_TX_FRAME_TRANSMITTED |
382 	    CEC_INT_TX_FRAME_OR_BLOCK_NAKD, 0);
383 
384 	while (sc->sc_txdone == false) {
385 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
386 		if (error)
387 			break;
388 	}
389 
390 	if (sc->sc_txdone)
391 		error = sc->sc_txerr;
392 
393 	mutex_exit(&sc->sc_lock);
394 
395 	return error;
396 }
397 
398 static ssize_t
399 tegra_cec_recv(void *priv, uint8_t *data, size_t len)
400 {
401 	struct tegra_cec_softc * const sc = priv;
402 	ssize_t alen = -1;
403 	int error = 0;
404 
405 	mutex_enter(&sc->sc_lock);
406 
407 	while (sc->sc_rxdone == false) {
408 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
409 		if (error)
410 			break;
411 	}
412 
413 	if (sc->sc_rxdone) {
414 		memcpy(data, sc->sc_rxbuf, sc->sc_rxlen);
415 		alen = sc->sc_rxlen;
416 		sc->sc_rxlen = 0;
417 		sc->sc_rxdone = false;
418 	}
419 
420 	mutex_exit(&sc->sc_lock);
421 
422 	return alen;
423 }
424 
425 static int
426 tegra_cec_poll(void *priv, int events, lwp_t *l)
427 {
428 	struct tegra_cec_softc * const sc = priv;
429 	int revents;
430 
431 	revents = events & (POLLOUT | POLLWRNORM);
432 
433 	if ((events & (POLLIN | POLLRDNORM)) == 0)
434 		return revents;
435 
436 	mutex_enter(&sc->sc_lock);
437 	if (sc->sc_rxdone) {
438 		revents = (events & (POLLIN | POLLRDNORM));
439 	} else {
440 		selrecord(l, &sc->sc_selinfo);
441 		revents = 0;
442 	}
443 	mutex_exit(&sc->sc_lock);
444 
445 	return revents;
446 }
447