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