]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/uart/uart_dev_snps.c
Merge compiler-rt release_40 branch r292009.
[FreeBSD/FreeBSD.git] / sys / dev / uart / uart_dev_snps.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * 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  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <machine/bus.h>
38
39 #include <dev/uart/uart.h>
40 #include <dev/uart/uart_bus.h>
41 #include <dev/uart/uart_cpu_fdt.h>
42 #include <dev/uart/uart_dev_ns8250.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #ifdef EXT_RESOURCES
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #endif
51
52 #include "uart_if.h"
53
54 struct snps_softc {
55         struct ns8250_softc     ns8250;
56
57 #ifdef EXT_RESOURCES
58         clk_t                   baudclk;
59         clk_t                   apb_pclk;
60         hwreset_t               reset;
61 #endif
62 };
63
64 static int
65 snps_uart_attach(struct uart_softc *uart_sc)
66 {
67         struct snps_softc *sc;
68
69         sc = (struct snps_softc *)uart_sc;
70
71         /* UART requires to read USR reg when IIR_BUSY */
72         sc->ns8250.busy_detect = 1;
73
74         return (ns8250_bus_attach(uart_sc));
75 }
76
77 static kobj_method_t snps_methods[] = {
78         KOBJMETHOD(uart_probe,          ns8250_bus_probe),
79         KOBJMETHOD(uart_attach,         snps_uart_attach),
80         KOBJMETHOD(uart_detach,         ns8250_bus_detach),
81         KOBJMETHOD(uart_flush,          ns8250_bus_flush),
82         KOBJMETHOD(uart_getsig,         ns8250_bus_getsig),
83         KOBJMETHOD(uart_ioctl,          ns8250_bus_ioctl),
84         KOBJMETHOD(uart_ipend,          ns8250_bus_ipend),
85         KOBJMETHOD(uart_param,          ns8250_bus_param),
86         KOBJMETHOD(uart_receive,        ns8250_bus_receive),
87         KOBJMETHOD(uart_setsig,         ns8250_bus_setsig),
88         KOBJMETHOD(uart_transmit,       ns8250_bus_transmit),
89         KOBJMETHOD(uart_grab,           ns8250_bus_grab),
90         KOBJMETHOD(uart_ungrab,         ns8250_bus_ungrab),
91         KOBJMETHOD_END
92 };
93
94 struct uart_class uart_snps_class = {
95         "snps",
96         snps_methods,
97         sizeof(struct snps_softc),
98         .uc_ops = &uart_ns8250_ops,
99         .uc_range = 8,
100         .uc_rclk = 0,
101 };
102
103 static struct ofw_compat_data compat_data[] = {
104         { "snps,dw-apb-uart",           (uintptr_t)&uart_snps_class },
105         { NULL,                         (uintptr_t)NULL }
106 };
107 UART_FDT_CLASS(compat_data);
108
109 #ifdef EXT_RESOURCES
110 static int
111 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
112 {
113         struct snps_softc *sc;
114
115         sc = device_get_softc(dev);
116         *baudclk = NULL;
117         *apb_pclk = NULL;
118
119         /* Baud clock is either named "baudclk", or there is a single
120          * unnamed clock.
121          */
122         if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
123             clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
124                 return (ENOENT);
125
126         /* APB peripheral clock is optional */
127         (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
128
129         return (0);
130 }
131 #endif
132
133 static int
134 snps_probe(device_t dev)
135 {
136         struct snps_softc *sc;
137         struct uart_class *uart_class;
138         phandle_t node;
139         uint32_t shift, clock;
140         uint64_t freq;
141         int error;
142 #ifdef EXT_RESOURCES
143         clk_t baudclk, apb_pclk;
144         hwreset_t reset;
145 #endif
146
147         if (!ofw_bus_status_okay(dev))
148                 return (ENXIO);
149
150         uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
151             compat_data)->ocd_data;
152         if (uart_class == NULL)
153                 return (ENXIO);
154
155         freq = 0;
156         sc = device_get_softc(dev);
157         sc->ns8250.base.sc_class = uart_class;
158
159         node = ofw_bus_get_node(dev);
160         if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
161                 shift = 0;
162         if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
163                 clock = 0;
164
165 #ifdef EXT_RESOURCES
166         if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
167                 error = hwreset_deassert(reset);
168                 if (error != 0) {
169                         device_printf(dev, "cannot de-assert reset\n");
170                         return (error);
171                 }
172         }
173
174         if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
175                 error = clk_enable(baudclk);
176                 if (error != 0) {
177                         device_printf(dev, "cannot enable baud clock\n");
178                         return (error);
179                 }
180                 if (apb_pclk != NULL) {
181                         error = clk_enable(apb_pclk);
182                         if (error != 0) {
183                                 device_printf(dev,
184                                     "cannot enable peripheral clock\n");
185                                 return (error);
186                         }
187                 }
188
189                 if (clock == 0) {
190                         error = clk_get_freq(baudclk, &freq);
191                         if (error != 0) {
192                                 device_printf(dev, "cannot get frequency\n");
193                                 return (error);
194                         }
195                         clock = (uint32_t)freq;
196                 }
197         }
198 #endif
199
200         if (bootverbose && clock == 0)
201                 device_printf(dev, "could not determine frequency\n");
202
203         error = uart_bus_probe(dev, (int)shift, (int)clock, 0, 0);
204         if (error != 0)
205                 return (error);
206
207 #ifdef EXT_RESOURCES
208         /* XXX uart_bus_probe has changed the softc, so refresh it */
209         sc = device_get_softc(dev);
210
211         /* Store clock and reset handles for detach */
212         sc->baudclk = baudclk;
213         sc->apb_pclk = apb_pclk;
214         sc->reset = reset;
215 #endif
216
217         return (0);
218 }
219
220 static int
221 snps_detach(device_t dev)
222 {
223 #ifdef EXT_RESOURCES
224         struct snps_softc *sc;
225         clk_t baudclk, apb_pclk;
226         hwreset_t reset;
227 #endif
228         int error;
229
230 #ifdef EXT_RESOURCES
231         sc = device_get_softc(dev);
232         baudclk = sc->baudclk;
233         apb_pclk = sc->apb_pclk;
234         reset = sc->reset;
235 #endif
236
237         error = uart_bus_detach(dev);
238         if (error != 0)
239                 return (error);
240
241 #ifdef EXT_RESOURCES
242         if (reset != NULL) {
243                 error = hwreset_assert(reset);
244                 if (error != 0) {
245                         device_printf(dev, "cannot assert reset\n");
246                         return (error);
247                 }
248                 hwreset_release(reset);
249         }
250         if (apb_pclk != NULL) {
251                 error = clk_release(apb_pclk);
252                 if (error != 0) {
253                         device_printf(dev, "cannot release peripheral clock\n");
254                         return (error);
255                 }
256         }
257         if (baudclk != NULL) {
258                 error = clk_release(baudclk);
259                 if (error != 0) {
260                         device_printf(dev, "cannot release baud clock\n");
261                         return (error);
262                 }
263         }
264 #endif
265
266         return (0);
267 }
268
269 static device_method_t snps_bus_methods[] = {
270         /* Device interface */
271         DEVMETHOD(device_probe,         snps_probe),
272         DEVMETHOD(device_attach,        uart_bus_attach),
273         DEVMETHOD(device_detach,        snps_detach),
274         DEVMETHOD_END
275 };
276
277 static driver_t snps_uart_driver = {
278         uart_driver_name,
279         snps_bus_methods,
280         sizeof(struct snps_softc)
281 };
282
283 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);