xref: /freebsd/sys/dev/uart/uart_dev_snps.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 
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_grab,		ns8250_bus_grab),
106 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
107 	KOBJMETHOD_END
108 };
109 
110 struct uart_class uart_snps_class = {
111 	"snps",
112 	snps_methods,
113 	sizeof(struct snps_softc),
114 	.uc_ops = &uart_ns8250_ops,
115 	.uc_range = 8,
116 	.uc_rclk = 0,
117 };
118 
119 static struct ofw_compat_data compat_data[] = {
120 	{ "snps,dw-apb-uart",		(uintptr_t)&uart_snps_class },
121 	{ "marvell,armada-38x-uart",	(uintptr_t)&uart_snps_class },
122 	{ NULL,				(uintptr_t)NULL }
123 };
124 UART_FDT_CLASS(compat_data);
125 
126 static int
127 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
128 {
129 
130 	*baudclk = NULL;
131 	*apb_pclk = NULL;
132 
133 	/* Baud clock is either named "baudclk", or there is a single
134 	 * unnamed clock.
135 	 */
136 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
137 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
138 		return (ENOENT);
139 
140 	/* APB peripheral clock is optional */
141 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
142 
143 	return (0);
144 }
145 
146 static int
147 snps_probe(device_t dev)
148 {
149 	struct snps_softc *sc;
150 	struct uart_class *uart_class;
151 	phandle_t node;
152 	uint32_t shift, iowidth, clock;
153 	uint64_t freq;
154 	int error;
155 	clk_t baudclk, apb_pclk;
156 	hwreset_t reset;
157 
158 	if (!ofw_bus_status_okay(dev))
159 		return (ENXIO);
160 
161 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
162 	    compat_data)->ocd_data;
163 	if (uart_class == NULL)
164 		return (ENXIO);
165 
166 	freq = 0;
167 	sc = device_get_softc(dev);
168 	sc->ns8250.base.sc_class = uart_class;
169 
170 	node = ofw_bus_get_node(dev);
171 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
172 		shift = 0;
173 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
174 		iowidth = 1;
175 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
176 		clock = 0;
177 
178 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
179 		error = hwreset_deassert(reset);
180 		if (error != 0) {
181 			device_printf(dev, "cannot de-assert reset\n");
182 			return (error);
183 		}
184 	}
185 
186 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
187 		error = clk_enable(baudclk);
188 		if (error != 0) {
189 			device_printf(dev, "cannot enable baud clock\n");
190 			return (error);
191 		}
192 		if (apb_pclk != NULL) {
193 			error = clk_enable(apb_pclk);
194 			if (error != 0) {
195 				device_printf(dev,
196 				    "cannot enable peripheral clock\n");
197 				return (error);
198 			}
199 		}
200 
201 		if (clock == 0) {
202 			error = clk_get_freq(baudclk, &freq);
203 			if (error != 0) {
204 				device_printf(dev, "cannot get frequency\n");
205 				return (error);
206 			}
207 			clock = (uint32_t)freq;
208 		}
209 	}
210 
211 	if (bootverbose && clock == 0)
212 		device_printf(dev, "could not determine frequency\n");
213 
214 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
215 	if (error > 0)
216 		return (error);
217 
218 	/* XXX uart_bus_probe has changed the softc, so refresh it */
219 	sc = device_get_softc(dev);
220 
221 	/* Store clock and reset handles for detach */
222 	sc->baudclk = baudclk;
223 	sc->apb_pclk = apb_pclk;
224 	sc->reset = reset;
225 
226 	return (BUS_PROBE_VENDOR);
227 }
228 
229 static int
230 snps_detach(device_t dev)
231 {
232 	struct snps_softc *sc;
233 	clk_t baudclk, apb_pclk;
234 	hwreset_t reset;
235 	int error;
236 
237 	sc = device_get_softc(dev);
238 	baudclk = sc->baudclk;
239 	apb_pclk = sc->apb_pclk;
240 	reset = sc->reset;
241 
242 	error = uart_bus_detach(dev);
243 	if (error != 0)
244 		return (error);
245 
246 	if (reset != NULL) {
247 		error = hwreset_assert(reset);
248 		if (error != 0) {
249 			device_printf(dev, "cannot assert reset\n");
250 			return (error);
251 		}
252 		hwreset_release(reset);
253 	}
254 	if (apb_pclk != NULL) {
255 		error = clk_release(apb_pclk);
256 		if (error != 0) {
257 			device_printf(dev, "cannot release peripheral clock\n");
258 			return (error);
259 		}
260 	}
261 	if (baudclk != NULL) {
262 		error = clk_release(baudclk);
263 		if (error != 0) {
264 			device_printf(dev, "cannot release baud clock\n");
265 			return (error);
266 		}
267 	}
268 
269 	return (0);
270 }
271 
272 static device_method_t snps_bus_methods[] = {
273 	/* Device interface */
274 	DEVMETHOD(device_probe,		snps_probe),
275 	DEVMETHOD(device_attach,	uart_bus_attach),
276 	DEVMETHOD(device_detach, 	snps_detach),
277 	DEVMETHOD_END
278 };
279 
280 static driver_t snps_uart_driver = {
281 	uart_driver_name,
282 	snps_bus_methods,
283 	sizeof(struct snps_softc)
284 };
285 
286 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, 0, 0);
287