ns8250_uart.c revision 1.8
1/* $NetBSD: ns8250_uart.c,v 1.8 2025/09/06 22:53:49 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2017-2020 Jared McNeill <jmcneill@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: ns8250_uart.c,v 1.8 2025/09/06 22:53:49 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
46static int ns8250_uart_match(device_t, cfdata_t, void *);
47static void ns8250_uart_attach(device_t, device_t, void *);
48
49struct ns8250_config {
50	int			type;
51	int			(*enable)(struct com_softc *);
52	void			(*disable)(struct com_softc *);
53};
54
55static const struct ns8250_config ns8250_config = {
56	.type = COM_TYPE_NORMAL,
57};
58
59static const struct ns8250_config ns16750_config = {
60	.type = COM_TYPE_16750,
61};
62
63#define	NS8250_OCTEON_USR_REG		0x0138
64
65static int
66ns8250_octeon_enable(struct com_softc *sc)
67{
68	struct com_regs *regsp = &sc->sc_regs;
69
70	/* XXX Clear old busy detect interrupts */
71	bus_space_read_1(regsp->cr_iot, regsp->cr_ioh, NS8250_OCTEON_USR_REG);
72
73	return 0;
74}
75
76static const struct ns8250_config octeon_config = {
77	.type = COM_TYPE_16550_NOERS,
78	.enable = ns8250_octeon_enable,
79};
80
81static const struct device_compatible_entry compat_data[] = {
82	{ .compat = "cavium,octeon-3860-uart",	.data = &octeon_config },
83	{ .compat = "ns8250",			.data = &ns8250_config },
84	{ .compat = "ns16450",			.data = &ns8250_config },
85	{ .compat = "ns16550a",			.data = &ns8250_config },
86	{ .compat = "ns16550",			.data = &ns8250_config },
87	{ .compat = "ns16750",			.data = &ns16750_config },
88	DEVICE_COMPAT_EOL
89};
90
91CFATTACH_DECL_NEW(ns8250_uart, sizeof(struct com_softc),
92	ns8250_uart_match, ns8250_uart_attach, NULL, NULL);
93
94static int
95ns8250_uart_match(device_t parent, cfdata_t cf, void *aux)
96{
97	struct fdt_attach_args * const faa = aux;
98
99	return of_compatible_match(faa->faa_phandle, compat_data);
100}
101
102static void
103ns8250_uart_attach(device_t parent, device_t self, void *aux)
104{
105	struct com_softc * const sc = device_private(self);
106	struct fdt_attach_args * const faa = aux;
107	const int phandle = faa->faa_phandle;
108	bus_space_tag_t bst = faa->faa_bst;
109	bus_space_handle_t bsh;
110	char intrstr[128];
111	struct clk *clk;
112	bus_addr_t addr;
113	bus_size_t size;
114	u_int reg_shift;
115	int error;
116	void *ih;
117
118	const struct ns8250_config *config =
119	    of_compatible_lookup(phandle, compat_data)->data;
120
121	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
122		aprint_error(": couldn't get registers\n");
123		return;
124	}
125
126	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
127		/* missing or bad reg-shift property, assume 0 */
128		reg_shift = 0;
129	}
130
131	sc->sc_dev = self;
132	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency)) {
133		clk = fdtbus_clock_get_index(phandle, 0);
134		if (clk != NULL)
135			sc->sc_frequency = clk_get_rate(clk);
136	}
137	if (sc->sc_frequency == 0) {
138		aprint_error(": couldn't get frequency\n");
139		return;
140	}
141
142	sc->sc_type = config->type;
143	sc->enable = config->enable;
144	sc->disable = config->disable;
145
146	error = bus_space_map(bst, addr, size, 0, &bsh);
147	if (error) {
148		aprint_error(": couldn't map %#" PRIx64 ": %d",
149		    (uint64_t)addr, error);
150		return;
151	}
152
153	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, reg_shift);
154
155	if (config->enable != NULL) {
156		sc->enable(sc);
157		sc->enabled = 1;
158	}
159
160	com_attach_subr(sc);
161
162	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
163		aprint_error_dev(self, "failed to decode interrupt\n");
164		return;
165	}
166
167	ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_SERIAL,
168	    FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
169	if (ih == NULL) {
170		aprint_error_dev(self, "failed to establish interrupt on %s\n",
171		    intrstr);
172		return;
173	}
174	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
175}
176
177/*
178 * Console support
179 */
180
181static int
182ns8250_uart_console_match(int phandle)
183{
184	return of_compatible_match(phandle, compat_data);
185}
186
187static void
188ns8250_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
189{
190	const int phandle = faa->faa_phandle;
191	bus_space_tag_t bst = faa->faa_bst;
192	bus_space_handle_t dummy_bsh;
193	struct com_regs regs;
194	bus_addr_t addr;
195	tcflag_t flags;
196	u_int reg_shift;
197	int speed;
198
199	const struct ns8250_config *config =
200	    of_compatible_lookup(phandle, compat_data)->data;
201
202	fdtbus_get_reg(phandle, 0, &addr, NULL);
203	speed = fdtbus_get_stdout_speed();
204	if (speed < 0)
205		speed = 115200;	/* default */
206	flags = fdtbus_get_stdout_flags();
207
208	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
209		/* missing or bad reg-shift property, assume 0 */
210		reg_shift = 0;
211	}
212
213	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
214	com_init_regs_stride(&regs, bst, dummy_bsh, addr, reg_shift);
215
216	if (comcnattach1(&regs, speed, uart_freq, config->type, flags))
217		panic("Cannot initialize ns8250 console");
218}
219
220static const struct fdt_console ns8250_uart_console = {
221	.match = ns8250_uart_console_match,
222	.consinit = ns8250_uart_console_consinit,
223};
224
225FDT_CONSOLE(ns8250_uart, &ns8250_uart_console);
226