dw_apb_uart.c revision 1.13 1 /* $NetBSD: dw_apb_uart.c,v 1.13 2025/09/06 22:53:48 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30
31 __KERNEL_RCSID(1, "$NetBSD: dw_apb_uart.c,v 1.13 2025/09/06 22:53:48 thorpej Exp $");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/intr.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39 #include <sys/termios.h>
40
41 #include <dev/ic/comvar.h>
42
43 #include <dev/fdt/fdtvar.h>
44 #include <dev/fdt/fdt_console.h>
45
46 static int dw_apb_uart_match(device_t, cfdata_t, void *);
47 static void dw_apb_uart_attach(device_t, device_t, void *);
48
49 static const struct device_compatible_entry compat_data[] = {
50 { .compat = "snps,dw-apb-uart" },
51 DEVICE_COMPAT_EOL
52 };
53
54 struct dw_apb_uart_softc {
55 struct com_softc ssc_sc;
56 void *ssc_ih;
57
58 struct clk *ssc_clk;
59 struct clk *ssc_pclk;
60 struct fdtbus_reset *ssc_rst;
61 };
62
63 CFATTACH_DECL_NEW(dw_apb_uart, sizeof(struct dw_apb_uart_softc),
64 dw_apb_uart_match, dw_apb_uart_attach, NULL, NULL);
65
66 static int
67 dw_apb_uart_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 struct fdt_attach_args * const faa = aux;
70
71 return of_compatible_match(faa->faa_phandle, compat_data);
72 }
73
74 static void
75 dw_apb_uart_attach(device_t parent, device_t self, void *aux)
76 {
77 struct dw_apb_uart_softc * const ssc = device_private(self);
78 struct com_softc * const sc = &ssc->ssc_sc;
79 struct fdt_attach_args * const faa = aux;
80 const int phandle = faa->faa_phandle;
81 bus_space_tag_t bst = faa->faa_bst;
82 bus_space_handle_t bsh;
83 char intrstr[128];
84 bus_addr_t addr;
85 bus_size_t size;
86 u_int reg_shift, reg_iowidth;
87 int error;
88
89 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
90 aprint_error(": couldn't get registers\n");
91 return;
92 }
93
94 if (of_getprop_uint32(phandle, "reg-shift", ®_shift)) {
95 /* missing or bad reg-shift property, assume 2 */
96 reg_shift = 2;
97 }
98 if (of_getprop_uint32(phandle, "reg-io-width", ®_iowidth)) {
99 /* missing or bad reg-io-width property, assume 1 */
100 reg_iowidth = 1;
101 }
102
103 sc->sc_dev = self;
104
105 ssc->ssc_clk = fdtbus_clock_get_index(phandle, 0);
106 if (ssc->ssc_clk == NULL) {
107 aprint_error(": couldn't get clock\n");
108 return;
109 }
110 if (clk_enable(ssc->ssc_clk) != 0) {
111 aprint_error(": couldn't enable clock\n");
112 return;
113 }
114
115 ssc->ssc_pclk = fdtbus_clock_get(phandle, "apb_pclk");
116 if (ssc->ssc_pclk != NULL && clk_enable(ssc->ssc_pclk) != 0) {
117 aprint_error(": couldn't enable peripheral clock\n");
118 return;
119 }
120
121 ssc->ssc_rst = fdtbus_reset_get_index(phandle, 0);
122 if (ssc->ssc_rst && fdtbus_reset_deassert(ssc->ssc_rst) != 0) {
123 aprint_error(": couldn't de-assert reset\n");
124 return;
125 }
126
127 sc->sc_frequency = clk_get_rate(ssc->ssc_clk);
128 sc->sc_type = COM_TYPE_DW_APB;
129
130 error = bus_space_map(bst, addr, size, 0, &bsh);
131 if (error) {
132 aprint_error(": couldn't map %#" PRIx64 ": %d", (uint64_t)addr, error);
133 return;
134 }
135
136 com_init_regs_stride_width(&sc->sc_regs, bst, bsh, addr, reg_shift, reg_iowidth);
137
138 com_attach_subr(sc);
139
140 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
141 aprint_error_dev(self, "failed to decode interrupt\n");
142 return;
143 }
144
145 ssc->ssc_ih = fdtbus_intr_establish_xname(phandle, 0,
146 IPL_SERIAL, FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
147 if (ssc->ssc_ih == NULL) {
148 aprint_error_dev(self, "failed to establish interrupt on %s\n",
149 intrstr);
150 }
151 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
152 }
153
154 /*
155 * Console support
156 */
157
158 static int
159 dw_apb_uart_console_match(int phandle)
160 {
161 return of_compatible_match(phandle, compat_data);
162 }
163
164 static void
165 dw_apb_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
166 {
167 const int phandle = faa->faa_phandle;
168 bus_space_tag_t bst = faa->faa_bst;
169 bus_space_handle_t dummy_bsh;
170 struct com_regs regs;
171 bus_addr_t addr;
172 tcflag_t flags;
173 u_int reg_shift, reg_iowidth;
174 int speed;
175
176 fdtbus_get_reg(phandle, 0, &addr, NULL);
177 speed = fdtbus_get_stdout_speed();
178 if (speed < 0)
179 speed = 115200; /* default */
180 flags = fdtbus_get_stdout_flags();
181
182 if (of_getprop_uint32(phandle, "reg-shift", ®_shift)) {
183 /* missing or bad reg-shift property, assume 2 */
184 reg_shift = 2;
185 }
186 if (of_getprop_uint32(phandle, "reg-io-width", ®_iowidth)) {
187 /* missing or bad reg-io-width property, assume 1 */
188 reg_iowidth = 1;
189 }
190
191 memset(&dummy_bsh, 0, sizeof(dummy_bsh));
192 com_init_regs_stride_width(®s, bst, dummy_bsh, addr, reg_shift, reg_iowidth);
193
194 if (comcnattach1(®s, speed, uart_freq, COM_TYPE_DW_APB, flags))
195 panic("Cannot initialize dw-apb-uart console");
196 }
197
198 static const struct fdt_console dw_apb_uart_console = {
199 .match = dw_apb_uart_console_match,
200 .consinit = dw_apb_uart_console_consinit,
201 };
202
203 FDT_CONSOLE(dw_apb_uart, &dw_apb_uart_console);
204