xref: /freebsd/sys/dev/uart/uart_dev_snps.c (revision 29fc4075e69fd27de0cded313ac6000165d99f8b)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
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  * $FreeBSD$
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <machine/bus.h>
37 
38 #include <dev/uart/uart.h>
39 #include <dev/uart/uart_bus.h>
40 #include <dev/uart/uart_cpu_fdt.h>
41 #include <dev/uart/uart_dev_ns8250.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <dev/extres/clk/clk.h>
47 #include <dev/extres/hwreset/hwreset.h>
48 
49 #include "uart_if.h"
50 
51 struct snps_softc {
52 	struct ns8250_softc	ns8250;
53 
54 	clk_t			baudclk;
55 	clk_t			apb_pclk;
56 	hwreset_t		reset;
57 };
58 
59 /*
60  * To use early printf on 64 bits Allwinner SoC, add to kernel config
61  * options SOCDEV_PA=0x0
62  * options SOCDEV_VA=0x40000000
63  * options EARLY_PRINTF
64  *
65  * To use early printf on 32 bits Allwinner SoC, add to kernel config
66  * options SOCDEV_PA=0x01C00000
67  * options SOCDEV_VA=0x10000000
68  * options EARLY_PRINTF
69  *
70  * remove the if 0
71 */
72 #if 0
73 #ifdef EARLY_PRINTF
74 static void
75 uart_snps_early_putc(int c)
76 {
77 	volatile uint32_t *stat;
78 	volatile uint32_t *tx;
79 
80 #ifdef ALLWINNER_64
81 	stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
82 	tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
83 #endif
84 #ifdef ALLWINNER_32
85 	stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
86 	tx = (uint32_t *) (SOCDEV_VA + 0x28000);
87 #endif
88 
89 	while ((*stat & (1 << 2)) == 0)
90 		continue;
91 	*tx = c;
92 }
93 early_putc_t *early_putc = uart_snps_early_putc;
94 #endif /* EARLY_PRINTF */
95 #endif
96 
97 static kobj_method_t snps_methods[] = {
98 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
99 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
100 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
101 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
102 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
103 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
104 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
105 	KOBJMETHOD(uart_param,		ns8250_bus_param),
106 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
107 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
108 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
109 	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
110 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
111 	KOBJMETHOD_END
112 };
113 
114 struct uart_class uart_snps_class = {
115 	"snps",
116 	snps_methods,
117 	sizeof(struct snps_softc),
118 	.uc_ops = &uart_ns8250_ops,
119 	.uc_range = 8,
120 	.uc_rclk = 0,
121 };
122 
123 static struct ofw_compat_data compat_data[] = {
124 	{ "snps,dw-apb-uart",		(uintptr_t)&uart_snps_class },
125 	{ "marvell,armada-38x-uart",	(uintptr_t)&uart_snps_class },
126 	{ NULL,				(uintptr_t)NULL }
127 };
128 UART_FDT_CLASS(compat_data);
129 
130 static int
131 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
132 {
133 
134 	*baudclk = NULL;
135 	*apb_pclk = NULL;
136 
137 	/* Baud clock is either named "baudclk", or there is a single
138 	 * unnamed clock.
139 	 */
140 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
141 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
142 		return (ENOENT);
143 
144 	/* APB peripheral clock is optional */
145 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
146 
147 	return (0);
148 }
149 
150 static int
151 snps_probe(device_t dev)
152 {
153 	struct snps_softc *sc;
154 	struct uart_class *uart_class;
155 	phandle_t node;
156 	uint32_t shift, iowidth, clock;
157 	uint64_t freq;
158 	int error;
159 	clk_t baudclk, apb_pclk;
160 	hwreset_t reset;
161 
162 	if (!ofw_bus_status_okay(dev))
163 		return (ENXIO);
164 
165 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
166 	    compat_data)->ocd_data;
167 	if (uart_class == NULL)
168 		return (ENXIO);
169 
170 	freq = 0;
171 	sc = device_get_softc(dev);
172 	sc->ns8250.base.sc_class = uart_class;
173 
174 	node = ofw_bus_get_node(dev);
175 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
176 		shift = 0;
177 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
178 		iowidth = 1;
179 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
180 		clock = 0;
181 
182 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
183 		error = hwreset_deassert(reset);
184 		if (error != 0) {
185 			device_printf(dev, "cannot de-assert reset\n");
186 			return (error);
187 		}
188 	}
189 
190 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
191 		error = clk_enable(baudclk);
192 		if (error != 0) {
193 			device_printf(dev, "cannot enable baud clock\n");
194 			return (error);
195 		}
196 		if (apb_pclk != NULL) {
197 			error = clk_enable(apb_pclk);
198 			if (error != 0) {
199 				device_printf(dev,
200 				    "cannot enable peripheral clock\n");
201 				return (error);
202 			}
203 		}
204 
205 		if (clock == 0) {
206 			error = clk_get_freq(baudclk, &freq);
207 			if (error != 0) {
208 				device_printf(dev, "cannot get frequency\n");
209 				return (error);
210 			}
211 			clock = (uint32_t)freq;
212 		}
213 	}
214 
215 	if (bootverbose && clock == 0)
216 		device_printf(dev, "could not determine frequency\n");
217 
218 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
219 	if (error != 0)
220 		return (error);
221 
222 	/* XXX uart_bus_probe has changed the softc, so refresh it */
223 	sc = device_get_softc(dev);
224 
225 	/* Store clock and reset handles for detach */
226 	sc->baudclk = baudclk;
227 	sc->apb_pclk = apb_pclk;
228 	sc->reset = reset;
229 
230 	return (0);
231 }
232 
233 static int
234 snps_detach(device_t dev)
235 {
236 	struct snps_softc *sc;
237 	clk_t baudclk, apb_pclk;
238 	hwreset_t reset;
239 	int error;
240 
241 	sc = device_get_softc(dev);
242 	baudclk = sc->baudclk;
243 	apb_pclk = sc->apb_pclk;
244 	reset = sc->reset;
245 
246 	error = uart_bus_detach(dev);
247 	if (error != 0)
248 		return (error);
249 
250 	if (reset != NULL) {
251 		error = hwreset_assert(reset);
252 		if (error != 0) {
253 			device_printf(dev, "cannot assert reset\n");
254 			return (error);
255 		}
256 		hwreset_release(reset);
257 	}
258 	if (apb_pclk != NULL) {
259 		error = clk_release(apb_pclk);
260 		if (error != 0) {
261 			device_printf(dev, "cannot release peripheral clock\n");
262 			return (error);
263 		}
264 	}
265 	if (baudclk != NULL) {
266 		error = clk_release(baudclk);
267 		if (error != 0) {
268 			device_printf(dev, "cannot release baud clock\n");
269 			return (error);
270 		}
271 	}
272 
273 	return (0);
274 }
275 
276 static device_method_t snps_bus_methods[] = {
277 	/* Device interface */
278 	DEVMETHOD(device_probe,		snps_probe),
279 	DEVMETHOD(device_attach,	uart_bus_attach),
280 	DEVMETHOD(device_detach, 	snps_detach),
281 	DEVMETHOD_END
282 };
283 
284 static driver_t snps_uart_driver = {
285 	uart_driver_name,
286 	snps_bus_methods,
287 	sizeof(struct snps_softc)
288 };
289 
290 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, 0, 0);
291