ns8250_uart.c revision 1.6
11.6Sthorpej/* $NetBSD: ns8250_uart.c,v 1.6 2021/01/27 02:24:10 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.6Sthorpej__KERNEL_RCSID(1, "$NetBSD: ns8250_uart.c,v 1.6 2021/01/27 02:24:10 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.1Sjmcneill
451.1Sjmcneillstatic int ns8250_uart_match(device_t, cfdata_t, void *);
461.1Sjmcneillstatic void ns8250_uart_attach(device_t, device_t, void *);
471.1Sjmcneill
481.1Sjmcneillstruct ns8250_config {
491.1Sjmcneill	int			type;
501.1Sjmcneill	int			(*enable)(struct com_softc *);
511.1Sjmcneill	void			(*disable)(struct com_softc *);
521.1Sjmcneill};
531.1Sjmcneill
541.1Sjmcneillstatic const struct ns8250_config ns8250_config = {
551.1Sjmcneill	.type = COM_TYPE_NORMAL,
561.1Sjmcneill};
571.1Sjmcneill
581.1Sjmcneillstatic const struct ns8250_config ns16750_config = {
591.1Sjmcneill	.type = COM_TYPE_16750,
601.1Sjmcneill};
611.1Sjmcneill
621.1Sjmcneill#define	NS8250_OCTEON_USR_REG		0x0138
631.1Sjmcneill
641.1Sjmcneillstatic int
651.1Sjmcneillns8250_octeon_enable(struct com_softc *sc)
661.1Sjmcneill{
671.1Sjmcneill	struct com_regs *regsp = &sc->sc_regs;
681.1Sjmcneill
691.1Sjmcneill	/* XXX Clear old busy detect interrupts */
701.1Sjmcneill	bus_space_read_1(regsp->cr_iot, regsp->cr_ioh, NS8250_OCTEON_USR_REG);
711.1Sjmcneill
721.1Sjmcneill	return 0;
731.1Sjmcneill}
741.1Sjmcneill
751.1Sjmcneillstatic const struct ns8250_config octeon_config = {
761.1Sjmcneill	.type = COM_TYPE_16550_NOERS,
771.1Sjmcneill	.enable = ns8250_octeon_enable,
781.1Sjmcneill};
791.1Sjmcneill
801.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
811.4Sthorpej	{ .compat = "cavium,octeon-3860-uart",	.data = &octeon_config },
821.4Sthorpej	{ .compat = "ns8250",			.data = &ns8250_config },
831.4Sthorpej	{ .compat = "ns16450",			.data = &ns8250_config },
841.4Sthorpej	{ .compat = "ns16550a",			.data = &ns8250_config },
851.4Sthorpej	{ .compat = "ns16550",			.data = &ns8250_config },
861.4Sthorpej	{ .compat = "ns16750",			.data = &ns16750_config },
871.6Sthorpej	DEVICE_COMPAT_EOL
881.1Sjmcneill};
891.1Sjmcneill
901.1SjmcneillCFATTACH_DECL_NEW(ns8250_uart, sizeof(struct com_softc),
911.1Sjmcneill	ns8250_uart_match, ns8250_uart_attach, NULL, NULL);
921.1Sjmcneill
931.1Sjmcneillstatic int
941.1Sjmcneillns8250_uart_match(device_t parent, cfdata_t cf, void *aux)
951.1Sjmcneill{
961.1Sjmcneill	struct fdt_attach_args * const faa = aux;
971.1Sjmcneill
981.1Sjmcneill	return of_match_compat_data(faa->faa_phandle, compat_data);
991.1Sjmcneill}
1001.1Sjmcneill
1011.1Sjmcneillstatic void
1021.1Sjmcneillns8250_uart_attach(device_t parent, device_t self, void *aux)
1031.1Sjmcneill{
1041.1Sjmcneill	struct com_softc * const sc = device_private(self);
1051.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1061.1Sjmcneill	const int phandle = faa->faa_phandle;
1071.1Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1081.1Sjmcneill	bus_space_handle_t bsh;
1091.1Sjmcneill	char intrstr[128];
1101.1Sjmcneill	struct clk *clk;
1111.1Sjmcneill	bus_addr_t addr;
1121.1Sjmcneill	bus_size_t size;
1131.1Sjmcneill	u_int reg_shift;
1141.1Sjmcneill	int error;
1151.1Sjmcneill	void *ih;
1161.1Sjmcneill
1171.1Sjmcneill	const struct ns8250_config *config =
1181.4Sthorpej	    of_search_compatible(phandle, compat_data)->data;
1191.1Sjmcneill
1201.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1211.1Sjmcneill		aprint_error(": couldn't get registers\n");
1221.1Sjmcneill		return;
1231.1Sjmcneill	}
1241.1Sjmcneill
1251.1Sjmcneill	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
1261.1Sjmcneill		/* missing or bad reg-shift property, assume 0 */
1271.1Sjmcneill		reg_shift = 0;
1281.1Sjmcneill	}
1291.1Sjmcneill
1301.1Sjmcneill	sc->sc_dev = self;
1311.1Sjmcneill	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency)) {
1321.1Sjmcneill		clk = fdtbus_clock_get_index(phandle, 0);
1331.1Sjmcneill		if (clk != NULL)
1341.1Sjmcneill			sc->sc_frequency = clk_get_rate(clk);
1351.1Sjmcneill	}
1361.1Sjmcneill	if (sc->sc_frequency == 0) {
1371.1Sjmcneill		aprint_error(": couldn't get frequency\n");
1381.1Sjmcneill		return;
1391.1Sjmcneill	}
1401.1Sjmcneill
1411.1Sjmcneill	sc->sc_type = config->type;
1421.1Sjmcneill	sc->enable = config->enable;
1431.1Sjmcneill	sc->disable = config->disable;
1441.1Sjmcneill
1451.1Sjmcneill	error = bus_space_map(bst, addr, size, 0, &bsh);
1461.1Sjmcneill	if (error) {
1471.1Sjmcneill		aprint_error(": couldn't map %#" PRIx64 ": %d",
1481.1Sjmcneill		    (uint64_t)addr, error);
1491.1Sjmcneill		return;
1501.1Sjmcneill	}
1511.1Sjmcneill
1521.1Sjmcneill	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, reg_shift);
1531.1Sjmcneill
1541.1Sjmcneill	if (config->enable != NULL) {
1551.1Sjmcneill		sc->enable(sc);
1561.1Sjmcneill		sc->enabled = 1;
1571.1Sjmcneill	}
1581.1Sjmcneill
1591.1Sjmcneill	com_attach_subr(sc);
1601.1Sjmcneill
1611.1Sjmcneill	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
1621.1Sjmcneill		aprint_error_dev(self, "failed to decode interrupt\n");
1631.1Sjmcneill		return;
1641.1Sjmcneill	}
1651.1Sjmcneill
1661.3Sjmcneill	ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_SERIAL,
1671.3Sjmcneill	    FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
1681.1Sjmcneill	if (ih == NULL) {
1691.1Sjmcneill		aprint_error_dev(self, "failed to establish interrupt on %s\n",
1701.1Sjmcneill		    intrstr);
1711.1Sjmcneill		return;
1721.1Sjmcneill	}
1731.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1741.1Sjmcneill}
1751.1Sjmcneill
1761.1Sjmcneill/*
1771.1Sjmcneill * Console support
1781.1Sjmcneill */
1791.1Sjmcneill
1801.1Sjmcneillstatic int
1811.1Sjmcneillns8250_uart_console_match(int phandle)
1821.1Sjmcneill{
1831.1Sjmcneill	return of_match_compat_data(phandle, compat_data);
1841.1Sjmcneill}
1851.1Sjmcneill
1861.1Sjmcneillstatic void
1871.1Sjmcneillns8250_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
1881.1Sjmcneill{
1891.1Sjmcneill	const int phandle = faa->faa_phandle;
1901.2Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1911.2Sjmcneill	bus_space_handle_t dummy_bsh;
1921.2Sjmcneill	struct com_regs regs;
1931.1Sjmcneill	bus_addr_t addr;
1941.1Sjmcneill	tcflag_t flags;
1951.2Sjmcneill	u_int reg_shift;
1961.1Sjmcneill	int speed;
1971.1Sjmcneill
1981.1Sjmcneill	const struct ns8250_config *config =
1991.4Sthorpej	    of_search_compatible(phandle, compat_data)->data;
2001.1Sjmcneill
2011.1Sjmcneill	fdtbus_get_reg(phandle, 0, &addr, NULL);
2021.1Sjmcneill	speed = fdtbus_get_stdout_speed();
2031.1Sjmcneill	if (speed < 0)
2041.1Sjmcneill		speed = 115200;	/* default */
2051.1Sjmcneill	flags = fdtbus_get_stdout_flags();
2061.1Sjmcneill
2071.2Sjmcneill	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
2081.2Sjmcneill		/* missing or bad reg-shift property, assume 0 */
2091.2Sjmcneill		reg_shift = 0;
2101.2Sjmcneill	}
2111.2Sjmcneill
2121.2Sjmcneill	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
2131.2Sjmcneill	com_init_regs_stride(&regs, bst, dummy_bsh, addr, reg_shift);
2141.2Sjmcneill
2151.2Sjmcneill	if (comcnattach1(&regs, speed, uart_freq, config->type, flags))
2161.1Sjmcneill		panic("Cannot initialize ns8250 console");
2171.1Sjmcneill}
2181.1Sjmcneill
2191.1Sjmcneillstatic const struct fdt_console ns8250_uart_console = {
2201.1Sjmcneill	.match = ns8250_uart_console_match,
2211.1Sjmcneill	.consinit = ns8250_uart_console_consinit,
2221.1Sjmcneill};
2231.1Sjmcneill
2241.1SjmcneillFDT_CONSOLE(ns8250_uart, &ns8250_uart_console);
225