xref: /freebsd-14.2/sys/dev/uart/uart_dev_snps.c (revision 8595e76a)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <[email protected]>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <machine/bus.h>
33 
34 #include <dev/uart/uart.h>
35 #include <dev/uart/uart_bus.h>
36 #include <dev/uart/uart_cpu_fdt.h>
37 #include <dev/uart/uart_dev_ns8250.h>
38 
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include <dev/extres/clk/clk.h>
43 #include <dev/extres/hwreset/hwreset.h>
44 
45 #include "uart_if.h"
46 
47 struct snps_softc {
48 	struct ns8250_softc	ns8250;
49 
50 	clk_t			baudclk;
51 	clk_t			apb_pclk;
52 	hwreset_t		reset;
53 };
54 
55 /*
56  * To use early printf on 64 bits Allwinner SoC, add to kernel config
57  * options SOCDEV_PA=0x0
58  * options SOCDEV_VA=0x40000000
59  * options EARLY_PRINTF
60  *
61  * To use early printf on 32 bits Allwinner SoC, add to kernel config
62  * options SOCDEV_PA=0x01C00000
63  * options SOCDEV_VA=0x10000000
64  * options EARLY_PRINTF
65  *
66  * remove the if 0
67 */
68 #if 0
69 #ifdef EARLY_PRINTF
70 static void
71 uart_snps_early_putc(int c)
72 {
73 	volatile uint32_t *stat;
74 	volatile uint32_t *tx;
75 
76 #ifdef ALLWINNER_64
77 	stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
78 	tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
79 #endif
80 #ifdef ALLWINNER_32
81 	stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
82 	tx = (uint32_t *) (SOCDEV_VA + 0x28000);
83 #endif
84 
85 	while ((*stat & (1 << 2)) == 0)
86 		continue;
87 	*tx = c;
88 }
89 early_putc_t *early_putc = uart_snps_early_putc;
90 #endif /* EARLY_PRINTF */
91 #endif
92 
93 static kobj_method_t snps_methods[] = {
94 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
95 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
96 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
97 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
98 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
99 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
100 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
101 	KOBJMETHOD(uart_param,		ns8250_bus_param),
102 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
103 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
104 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
105 	KOBJMETHOD(uart_txbusy,		ns8250_bus_txbusy),
106 	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
107 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
108 	KOBJMETHOD_END
109 };
110 
111 struct uart_class uart_snps_class = {
112 	"snps",
113 	snps_methods,
114 	sizeof(struct snps_softc),
115 	.uc_ops = &uart_ns8250_ops,
116 	.uc_range = 8,
117 	.uc_rclk = 0,
118 };
119 
120 static struct ofw_compat_data compat_data[] = {
121 	{ "snps,dw-apb-uart",		(uintptr_t)&uart_snps_class },
122 	{ "marvell,armada-38x-uart",	(uintptr_t)&uart_snps_class },
123 	{ NULL,				(uintptr_t)NULL }
124 };
125 UART_FDT_CLASS(compat_data);
126 
127 static int
snps_get_clocks(device_t dev,clk_t * baudclk,clk_t * apb_pclk)128 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
129 {
130 
131 	*baudclk = NULL;
132 	*apb_pclk = NULL;
133 
134 	/* Baud clock is either named "baudclk", or there is a single
135 	 * unnamed clock.
136 	 */
137 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
138 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
139 		return (ENOENT);
140 
141 	/* APB peripheral clock is optional */
142 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
143 
144 	return (0);
145 }
146 
147 static int
snps_probe(device_t dev)148 snps_probe(device_t dev)
149 {
150 	struct snps_softc *sc;
151 	struct uart_class *uart_class;
152 	phandle_t node;
153 	uint32_t shift, iowidth, clock;
154 	uint64_t freq;
155 	int error;
156 	clk_t baudclk, apb_pclk;
157 	hwreset_t reset;
158 
159 	if (!ofw_bus_status_okay(dev))
160 		return (ENXIO);
161 
162 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
163 	    compat_data)->ocd_data;
164 	if (uart_class == NULL)
165 		return (ENXIO);
166 
167 	freq = 0;
168 	sc = device_get_softc(dev);
169 	sc->ns8250.base.sc_class = uart_class;
170 
171 	node = ofw_bus_get_node(dev);
172 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
173 		shift = 0;
174 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
175 		iowidth = 1;
176 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
177 		clock = 0;
178 
179 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
180 		error = hwreset_deassert(reset);
181 		if (error != 0) {
182 			device_printf(dev, "cannot de-assert reset\n");
183 			return (error);
184 		}
185 	}
186 
187 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
188 		error = clk_enable(baudclk);
189 		if (error != 0) {
190 			device_printf(dev, "cannot enable baud clock\n");
191 			return (error);
192 		}
193 		if (apb_pclk != NULL) {
194 			error = clk_enable(apb_pclk);
195 			if (error != 0) {
196 				device_printf(dev,
197 				    "cannot enable peripheral clock\n");
198 				return (error);
199 			}
200 		}
201 
202 		if (clock == 0) {
203 			error = clk_get_freq(baudclk, &freq);
204 			if (error != 0) {
205 				device_printf(dev, "cannot get frequency\n");
206 				return (error);
207 			}
208 			clock = (uint32_t)freq;
209 		}
210 	}
211 
212 	if (bootverbose && clock == 0)
213 		device_printf(dev, "could not determine frequency\n");
214 
215 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
216 	if (error > 0)
217 		return (error);
218 
219 	/* XXX uart_bus_probe has changed the softc, so refresh it */
220 	sc = device_get_softc(dev);
221 
222 	/* Store clock and reset handles for detach */
223 	sc->baudclk = baudclk;
224 	sc->apb_pclk = apb_pclk;
225 	sc->reset = reset;
226 
227 	return (BUS_PROBE_VENDOR);
228 }
229 
230 static int
snps_detach(device_t dev)231 snps_detach(device_t dev)
232 {
233 	struct snps_softc *sc;
234 	clk_t baudclk, apb_pclk;
235 	hwreset_t reset;
236 	int error;
237 
238 	sc = device_get_softc(dev);
239 	baudclk = sc->baudclk;
240 	apb_pclk = sc->apb_pclk;
241 	reset = sc->reset;
242 
243 	error = uart_bus_detach(dev);
244 	if (error != 0)
245 		return (error);
246 
247 	if (reset != NULL) {
248 		error = hwreset_assert(reset);
249 		if (error != 0) {
250 			device_printf(dev, "cannot assert reset\n");
251 			return (error);
252 		}
253 		hwreset_release(reset);
254 	}
255 	if (apb_pclk != NULL) {
256 		error = clk_release(apb_pclk);
257 		if (error != 0) {
258 			device_printf(dev, "cannot release peripheral clock\n");
259 			return (error);
260 		}
261 	}
262 	if (baudclk != NULL) {
263 		error = clk_release(baudclk);
264 		if (error != 0) {
265 			device_printf(dev, "cannot release baud clock\n");
266 			return (error);
267 		}
268 	}
269 
270 	return (0);
271 }
272 
273 static device_method_t snps_bus_methods[] = {
274 	/* Device interface */
275 	DEVMETHOD(device_probe,		snps_probe),
276 	DEVMETHOD(device_attach,	uart_bus_attach),
277 	DEVMETHOD(device_detach, 	snps_detach),
278 	DEVMETHOD_END
279 };
280 
281 static driver_t snps_uart_driver = {
282 	uart_driver_name,
283 	snps_bus_methods,
284 	sizeof(struct snps_softc)
285 };
286 
287 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, 0, 0);
288