xref: /freebsd/sys/arm/nvidia/tegra_uart.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 
31 /*
32  * UART driver for Tegra SoCs.
33  */
34 #include "opt_platform.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <machine/bus.h>
44 
45 #include <dev/extres/clk/clk.h>
46 #include <dev/extres/hwreset/hwreset.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/uart/uart.h>
50 #include <dev/uart/uart_cpu.h>
51 #include <dev/uart/uart_cpu_fdt.h>
52 #include <dev/uart/uart_bus.h>
53 #include <dev/uart/uart_dev_ns8250.h>
54 #include <dev/ic/ns16550.h>
55 
56 #include "uart_if.h"
57 
58 /*
59  * High-level UART interface.
60  */
61 struct tegra_softc {
62 	struct ns8250_softc 	ns8250_base;
63 	clk_t			clk;
64 	hwreset_t		reset;
65 };
66 
67 /*
68  * UART class interface.
69  */
70 static int
71 tegra_uart_attach(struct uart_softc *sc)
72 {
73 	int rv;
74 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
75 	struct uart_bas *bas = &sc->sc_bas;
76 
77 	rv = ns8250_bus_attach(sc);
78 	if (rv != 0)
79 		return (rv);
80 
81 	ns8250->ier_rxbits = 0x1d;
82 	ns8250->ier_mask = 0xc0;
83 	ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
84 	ns8250->ier |= ns8250->ier_rxbits;
85 	uart_setreg(bas, REG_IER, ns8250->ier);
86 	uart_barrier(bas);
87 	return (0);
88 }
89 
90 static void
91 tegra_uart_grab(struct uart_softc *sc)
92 {
93 	struct uart_bas *bas = &sc->sc_bas;
94 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
95 	u_char ier;
96 
97 	/*
98 	 * turn off all interrupts to enter polling mode. Leave the
99 	 * saved mask alone. We'll restore whatever it was in ungrab.
100 	 * All pending interrupt signals are reset when IER is set to 0.
101 	 */
102 	uart_lock(sc->sc_hwmtx);
103 	ier = uart_getreg(bas, REG_IER);
104 	uart_setreg(bas, REG_IER, ier & ns8250->ier_mask);
105 	uart_setreg(bas, REG_FCR, 0);
106 	uart_barrier(bas);
107 	uart_unlock(sc->sc_hwmtx);
108 }
109 
110 static void
111 tegra_uart_ungrab(struct uart_softc *sc)
112 {
113 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
114 	struct uart_bas *bas = &sc->sc_bas;
115 
116 	/*
117 	 * Restore previous interrupt mask
118 	 */
119 	uart_lock(sc->sc_hwmtx);
120 	uart_setreg(bas, REG_FCR, ns8250->fcr);
121 	uart_setreg(bas, REG_IER, ns8250->ier);
122 	uart_barrier(bas);
123 	uart_unlock(sc->sc_hwmtx);
124 }
125 
126 static kobj_method_t tegra_methods[] = {
127 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
128 	KOBJMETHOD(uart_attach,		tegra_uart_attach),
129 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
130 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
131 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
132 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
133 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
134 	KOBJMETHOD(uart_param,		ns8250_bus_param),
135 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
136 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
137 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
138 	KOBJMETHOD(uart_grab,		tegra_uart_grab),
139 	KOBJMETHOD(uart_ungrab,		tegra_uart_ungrab),
140 	KOBJMETHOD_END
141 };
142 
143 static struct uart_class tegra_uart_class = {
144 	"tegra class",
145 	tegra_methods,
146 	sizeof(struct tegra_softc),
147 	.uc_ops = &uart_ns8250_ops,
148 	.uc_range = 8,
149 	.uc_rclk = 0,
150 };
151 
152 /* Compatible devices. */
153 static struct ofw_compat_data compat_data[] = {
154 	{"nvidia,tegra124-uart", (uintptr_t)&tegra_uart_class},
155 	{NULL,			(uintptr_t)NULL},
156 };
157 
158 UART_FDT_CLASS(compat_data);
159 
160 /*
161  * UART Driver interface.
162  */
163 static int
164 uart_fdt_get_shift1(phandle_t node)
165 {
166 	pcell_t shift;
167 
168 	if ((OF_getencprop(node, "reg-shift", &shift, sizeof(shift))) <= 0)
169 		shift = 2;
170 	return ((int)shift);
171 }
172 
173 static int
174 tegra_uart_probe(device_t dev)
175 {
176 	struct tegra_softc *sc;
177 	phandle_t node;
178 	uint64_t freq;
179 	int shift;
180 	int rv;
181 	const struct ofw_compat_data *cd;
182 
183 	sc = device_get_softc(dev);
184 	if (!ofw_bus_status_okay(dev))
185 		return (ENXIO);
186 	cd = ofw_bus_search_compatible(dev, compat_data);
187 	if (cd->ocd_data == 0)
188 		return (ENXIO);
189 	sc->ns8250_base.base.sc_class = (struct uart_class *)cd->ocd_data;
190 
191 	rv = hwreset_get_by_ofw_name(dev, 0, "serial", &sc->reset);
192 	if (rv != 0) {
193 		device_printf(dev, "Cannot get 'serial' reset\n");
194 		return (ENXIO);
195 	}
196 	rv = hwreset_deassert(sc->reset);
197 	if (rv != 0) {
198 		device_printf(dev, "Cannot unreset 'serial' reset\n");
199 		return (ENXIO);
200 	}
201 
202 	node = ofw_bus_get_node(dev);
203 	shift = uart_fdt_get_shift1(node);
204 	rv = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
205 	if (rv != 0) {
206 		device_printf(dev, "Cannot get UART clock: %d\n", rv);
207 		return (ENXIO);
208 	}
209 	rv = clk_enable(sc->clk);
210 	if (rv != 0) {
211 		device_printf(dev, "Cannot enable UART clock: %d\n", rv);
212 		return (ENXIO);
213 	}
214 	rv = clk_get_freq(sc->clk, &freq);
215 	if (rv != 0) {
216 		device_printf(dev, "Cannot enable UART clock: %d\n", rv);
217 		return (ENXIO);
218 	}
219 	return (uart_bus_probe(dev, shift, 0, (int)freq, 0, 0));
220 }
221 
222 static int
223 tegra_uart_detach(device_t dev)
224 {
225 	struct tegra_softc *sc;
226 
227 	sc = device_get_softc(dev);
228 	if (sc->clk != NULL) {
229 		clk_release(sc->clk);
230 	}
231 
232 	return (uart_bus_detach(dev));
233 }
234 
235 static device_method_t tegra_uart_bus_methods[] = {
236 	/* Device interface */
237 	DEVMETHOD(device_probe,		tegra_uart_probe),
238 	DEVMETHOD(device_attach,	uart_bus_attach),
239 	DEVMETHOD(device_detach,	tegra_uart_detach),
240 	{ 0, 0 }
241 };
242 
243 static driver_t tegra_uart_driver = {
244 	uart_driver_name,
245 	tegra_uart_bus_methods,
246 	sizeof(struct tegra_softc),
247 };
248 
249 DRIVER_MODULE(tegra_uart, simplebus,  tegra_uart_driver, uart_devclass,
250     0, 0);
251