ns8250_uart.c revision 1.8
11.8Sthorpej/* $NetBSD: ns8250_uart.c,v 1.8 2025/09/06 22:53:49 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017-2020 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill
311.8Sthorpej__KERNEL_RCSID(1, "$NetBSD: ns8250_uart.c,v 1.8 2025/09/06 22:53:49 thorpej Exp $");
321.1Sjmcneill
331.1Sjmcneill#include <sys/param.h>
341.1Sjmcneill#include <sys/bus.h>
351.1Sjmcneill#include <sys/device.h>
361.1Sjmcneill#include <sys/intr.h>
371.1Sjmcneill#include <sys/systm.h>
381.1Sjmcneill#include <sys/time.h>
391.1Sjmcneill#include <sys/termios.h>
401.1Sjmcneill
411.1Sjmcneill#include <dev/ic/comvar.h>
421.1Sjmcneill
431.1Sjmcneill#include <dev/fdt/fdtvar.h>
441.8Sthorpej#include <dev/fdt/fdt_console.h>
451.1Sjmcneill
461.1Sjmcneillstatic int ns8250_uart_match(device_t, cfdata_t, void *);
471.1Sjmcneillstatic void ns8250_uart_attach(device_t, device_t, void *);
481.1Sjmcneill
491.1Sjmcneillstruct ns8250_config {
501.1Sjmcneill	int			type;
511.1Sjmcneill	int			(*enable)(struct com_softc *);
521.1Sjmcneill	void			(*disable)(struct com_softc *);
531.1Sjmcneill};
541.1Sjmcneill
551.1Sjmcneillstatic const struct ns8250_config ns8250_config = {
561.1Sjmcneill	.type = COM_TYPE_NORMAL,
571.1Sjmcneill};
581.1Sjmcneill
591.1Sjmcneillstatic const struct ns8250_config ns16750_config = {
601.1Sjmcneill	.type = COM_TYPE_16750,
611.1Sjmcneill};
621.1Sjmcneill
631.1Sjmcneill#define	NS8250_OCTEON_USR_REG		0x0138
641.1Sjmcneill
651.1Sjmcneillstatic int
661.1Sjmcneillns8250_octeon_enable(struct com_softc *sc)
671.1Sjmcneill{
681.1Sjmcneill	struct com_regs *regsp = &sc->sc_regs;
691.1Sjmcneill
701.1Sjmcneill	/* XXX Clear old busy detect interrupts */
711.1Sjmcneill	bus_space_read_1(regsp->cr_iot, regsp->cr_ioh, NS8250_OCTEON_USR_REG);
721.1Sjmcneill
731.1Sjmcneill	return 0;
741.1Sjmcneill}
751.1Sjmcneill
761.1Sjmcneillstatic const struct ns8250_config octeon_config = {
771.1Sjmcneill	.type = COM_TYPE_16550_NOERS,
781.1Sjmcneill	.enable = ns8250_octeon_enable,
791.1Sjmcneill};
801.1Sjmcneill
811.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
821.4Sthorpej	{ .compat = "cavium,octeon-3860-uart",	.data = &octeon_config },
831.4Sthorpej	{ .compat = "ns8250",			.data = &ns8250_config },
841.4Sthorpej	{ .compat = "ns16450",			.data = &ns8250_config },
851.4Sthorpej	{ .compat = "ns16550a",			.data = &ns8250_config },
861.4Sthorpej	{ .compat = "ns16550",			.data = &ns8250_config },
871.4Sthorpej	{ .compat = "ns16750",			.data = &ns16750_config },
881.6Sthorpej	DEVICE_COMPAT_EOL
891.1Sjmcneill};
901.1Sjmcneill
911.1SjmcneillCFATTACH_DECL_NEW(ns8250_uart, sizeof(struct com_softc),
921.1Sjmcneill	ns8250_uart_match, ns8250_uart_attach, NULL, NULL);
931.1Sjmcneill
941.1Sjmcneillstatic int
951.1Sjmcneillns8250_uart_match(device_t parent, cfdata_t cf, void *aux)
961.1Sjmcneill{
971.1Sjmcneill	struct fdt_attach_args * const faa = aux;
981.1Sjmcneill
991.7Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1001.1Sjmcneill}
1011.1Sjmcneill
1021.1Sjmcneillstatic void
1031.1Sjmcneillns8250_uart_attach(device_t parent, device_t self, void *aux)
1041.1Sjmcneill{
1051.1Sjmcneill	struct com_softc * const sc = device_private(self);
1061.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1071.1Sjmcneill	const int phandle = faa->faa_phandle;
1081.1Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1091.1Sjmcneill	bus_space_handle_t bsh;
1101.1Sjmcneill	char intrstr[128];
1111.1Sjmcneill	struct clk *clk;
1121.1Sjmcneill	bus_addr_t addr;
1131.1Sjmcneill	bus_size_t size;
1141.1Sjmcneill	u_int reg_shift;
1151.1Sjmcneill	int error;
1161.1Sjmcneill	void *ih;
1171.1Sjmcneill
1181.1Sjmcneill	const struct ns8250_config *config =
1191.7Sthorpej	    of_compatible_lookup(phandle, compat_data)->data;
1201.1Sjmcneill
1211.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1221.1Sjmcneill		aprint_error(": couldn't get registers\n");
1231.1Sjmcneill		return;
1241.1Sjmcneill	}
1251.1Sjmcneill
1261.1Sjmcneill	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
1271.1Sjmcneill		/* missing or bad reg-shift property, assume 0 */
1281.1Sjmcneill		reg_shift = 0;
1291.1Sjmcneill	}
1301.1Sjmcneill
1311.1Sjmcneill	sc->sc_dev = self;
1321.1Sjmcneill	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency)) {
1331.1Sjmcneill		clk = fdtbus_clock_get_index(phandle, 0);
1341.1Sjmcneill		if (clk != NULL)
1351.1Sjmcneill			sc->sc_frequency = clk_get_rate(clk);
1361.1Sjmcneill	}
1371.1Sjmcneill	if (sc->sc_frequency == 0) {
1381.1Sjmcneill		aprint_error(": couldn't get frequency\n");
1391.1Sjmcneill		return;
1401.1Sjmcneill	}
1411.1Sjmcneill
1421.1Sjmcneill	sc->sc_type = config->type;
1431.1Sjmcneill	sc->enable = config->enable;
1441.1Sjmcneill	sc->disable = config->disable;
1451.1Sjmcneill
1461.1Sjmcneill	error = bus_space_map(bst, addr, size, 0, &bsh);
1471.1Sjmcneill	if (error) {
1481.1Sjmcneill		aprint_error(": couldn't map %#" PRIx64 ": %d",
1491.1Sjmcneill		    (uint64_t)addr, error);
1501.1Sjmcneill		return;
1511.1Sjmcneill	}
1521.1Sjmcneill
1531.1Sjmcneill	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, reg_shift);
1541.1Sjmcneill
1551.1Sjmcneill	if (config->enable != NULL) {
1561.1Sjmcneill		sc->enable(sc);
1571.1Sjmcneill		sc->enabled = 1;
1581.1Sjmcneill	}
1591.1Sjmcneill
1601.1Sjmcneill	com_attach_subr(sc);
1611.1Sjmcneill
1621.1Sjmcneill	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
1631.1Sjmcneill		aprint_error_dev(self, "failed to decode interrupt\n");
1641.1Sjmcneill		return;
1651.1Sjmcneill	}
1661.1Sjmcneill
1671.3Sjmcneill	ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_SERIAL,
1681.3Sjmcneill	    FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
1691.1Sjmcneill	if (ih == NULL) {
1701.1Sjmcneill		aprint_error_dev(self, "failed to establish interrupt on %s\n",
1711.1Sjmcneill		    intrstr);
1721.1Sjmcneill		return;
1731.1Sjmcneill	}
1741.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1751.1Sjmcneill}
1761.1Sjmcneill
1771.1Sjmcneill/*
1781.1Sjmcneill * Console support
1791.1Sjmcneill */
1801.1Sjmcneill
1811.1Sjmcneillstatic int
1821.1Sjmcneillns8250_uart_console_match(int phandle)
1831.1Sjmcneill{
1841.7Sthorpej	return of_compatible_match(phandle, compat_data);
1851.1Sjmcneill}
1861.1Sjmcneill
1871.1Sjmcneillstatic void
1881.1Sjmcneillns8250_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
1891.1Sjmcneill{
1901.1Sjmcneill	const int phandle = faa->faa_phandle;
1911.2Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1921.2Sjmcneill	bus_space_handle_t dummy_bsh;
1931.2Sjmcneill	struct com_regs regs;
1941.1Sjmcneill	bus_addr_t addr;
1951.1Sjmcneill	tcflag_t flags;
1961.2Sjmcneill	u_int reg_shift;
1971.1Sjmcneill	int speed;
1981.1Sjmcneill
1991.1Sjmcneill	const struct ns8250_config *config =
2001.7Sthorpej	    of_compatible_lookup(phandle, compat_data)->data;
2011.1Sjmcneill
2021.1Sjmcneill	fdtbus_get_reg(phandle, 0, &addr, NULL);
2031.1Sjmcneill	speed = fdtbus_get_stdout_speed();
2041.1Sjmcneill	if (speed < 0)
2051.1Sjmcneill		speed = 115200;	/* default */
2061.1Sjmcneill	flags = fdtbus_get_stdout_flags();
2071.1Sjmcneill
2081.2Sjmcneill	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
2091.2Sjmcneill		/* missing or bad reg-shift property, assume 0 */
2101.2Sjmcneill		reg_shift = 0;
2111.2Sjmcneill	}
2121.2Sjmcneill
2131.2Sjmcneill	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
2141.2Sjmcneill	com_init_regs_stride(&regs, bst, dummy_bsh, addr, reg_shift);
2151.2Sjmcneill
2161.2Sjmcneill	if (comcnattach1(&regs, speed, uart_freq, config->type, flags))
2171.1Sjmcneill		panic("Cannot initialize ns8250 console");
2181.1Sjmcneill}
2191.1Sjmcneill
2201.1Sjmcneillstatic const struct fdt_console ns8250_uart_console = {
2211.1Sjmcneill	.match = ns8250_uart_console_match,
2221.1Sjmcneill	.consinit = ns8250_uart_console_consinit,
2231.1Sjmcneill};
2241.1Sjmcneill
2251.1SjmcneillFDT_CONSOLE(ns8250_uart, &ns8250_uart_console);
226