Home | History | Annotate | Line # | Download | only in fdt
      1 /* $NetBSD: ns8250_uart.c,v 1.9 2026/08/07 14:31:36 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017-2020 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: ns8250_uart.c,v 1.9 2026/08/07 14:31:36 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 ns8250_uart_match(device_t, cfdata_t, void *);
     47 static void ns8250_uart_attach(device_t, device_t, void *);
     48 
     49 struct ns8250_config {
     50 	int			type;
     51 	int			(*enable)(struct com_softc *);
     52 	void			(*disable)(struct com_softc *);
     53 };
     54 
     55 static const struct ns8250_config ns8250_config = {
     56 	.type = COM_TYPE_NORMAL,
     57 };
     58 
     59 static const struct ns8250_config ns16750_config = {
     60 	.type = COM_TYPE_16750,
     61 };
     62 
     63 #define	NS8250_OCTEON_USR_REG		0x0138
     64 
     65 static int
     66 ns8250_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 
     76 static const struct ns8250_config octeon_config = {
     77 	.type = COM_TYPE_16550_NOERS,
     78 	.enable = ns8250_octeon_enable,
     79 };
     80 
     81 static 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 
     91 CFATTACH_DECL_NEW(ns8250_uart, sizeof(struct com_softc),
     92 	ns8250_uart_match, ns8250_uart_attach, NULL, NULL);
     93 
     94 static int
     95 ns8250_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 
    102 static void
    103 ns8250_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 	reg_shift = fdtbus_get_reg_shift(phandle, 0);
    127 
    128 	sc->sc_dev = self;
    129 	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency)) {
    130 		clk = fdtbus_clock_get_index(phandle, 0);
    131 		if (clk != NULL)
    132 			sc->sc_frequency = clk_get_rate(clk);
    133 	}
    134 	if (sc->sc_frequency == 0) {
    135 		aprint_error(": couldn't get frequency\n");
    136 		return;
    137 	}
    138 
    139 	sc->sc_type = config->type;
    140 	sc->enable = config->enable;
    141 	sc->disable = config->disable;
    142 
    143 	error = bus_space_map(bst, addr, size, 0, &bsh);
    144 	if (error) {
    145 		aprint_error(": couldn't map %#" PRIx64 ": %d",
    146 		    (uint64_t)addr, error);
    147 		return;
    148 	}
    149 
    150 	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, reg_shift);
    151 
    152 	if (config->enable != NULL) {
    153 		sc->enable(sc);
    154 		sc->enabled = 1;
    155 	}
    156 
    157 	com_attach_subr(sc);
    158 
    159 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    160 		aprint_error_dev(self, "failed to decode interrupt\n");
    161 		return;
    162 	}
    163 
    164 	ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_SERIAL,
    165 	    FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
    166 	if (ih == NULL) {
    167 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    168 		    intrstr);
    169 		return;
    170 	}
    171 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    172 }
    173 
    174 /*
    175  * Console support
    176  */
    177 
    178 static int
    179 ns8250_uart_console_match(int phandle)
    180 {
    181 	return of_compatible_match(phandle, compat_data);
    182 }
    183 
    184 static void
    185 ns8250_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    186 {
    187 	const int phandle = faa->faa_phandle;
    188 	bus_space_tag_t bst = faa->faa_bst;
    189 	bus_space_handle_t dummy_bsh;
    190 	struct com_regs regs;
    191 	bus_addr_t addr;
    192 	tcflag_t flags;
    193 	u_int reg_shift;
    194 	int speed;
    195 
    196 	const struct ns8250_config *config =
    197 	    of_compatible_lookup(phandle, compat_data)->data;
    198 
    199 	fdtbus_get_reg(phandle, 0, &addr, NULL);
    200 	speed = fdtbus_get_stdout_speed();
    201 	if (speed < 0)
    202 		speed = 115200;	/* default */
    203 	flags = fdtbus_get_stdout_flags();
    204 
    205 	reg_shift = fdtbus_get_reg_shift(phandle, 0);
    206 
    207 	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
    208 	com_init_regs_stride(&regs, bst, dummy_bsh, addr, reg_shift);
    209 
    210 	if (comcnattach1(&regs, speed, uart_freq, config->type, flags))
    211 		panic("Cannot initialize ns8250 console");
    212 }
    213 
    214 static const struct fdt_console ns8250_uart_console = {
    215 	.match = ns8250_uart_console_match,
    216 	.consinit = ns8250_uart_console_consinit,
    217 };
    218 
    219 FDT_CONSOLE(ns8250_uart, &ns8250_uart_console);
    220