Thu Jul 16 11:42:54 2020 UTC ()
Add glue for 8250 compatible UARTs.


(jmcneill)
diff -r1.52 -r1.53 src/sys/dev/fdt/files.fdt
diff -r0 -r1.1 src/sys/dev/fdt/ns8250_uart.c

cvs diff -r1.52 -r1.53 src/sys/dev/fdt/files.fdt (switch to unified diff)

--- src/sys/dev/fdt/files.fdt 2020/03/24 22:09:50 1.52
+++ src/sys/dev/fdt/files.fdt 2020/07/16 11:42:53 1.53
@@ -1,184 +1,188 @@ @@ -1,184 +1,188 @@
1# $NetBSD: files.fdt,v 1.52 2020/03/24 22:09:50 tnn Exp $ 1# $NetBSD: files.fdt,v 1.53 2020/07/16 11:42:53 jmcneill Exp $
2 2
3include "external/bsd/libfdt/conf/files.libfdt" 3include "external/bsd/libfdt/conf/files.libfdt"
4 4
5defflag opt_fdt.h FDTBASE : libfdt, ofw_subr 5defflag opt_fdt.h FDTBASE : libfdt, ofw_subr
6defflag opt_fdt.h FDT: FDTBASE 6defflag opt_fdt.h FDT: FDTBASE
7 7
8define fdt { [pass = 10] } : clk, pwm 8define fdt { [pass = 10] } : clk, pwm
9 9
10device simplebus { } : fdt 10device simplebus { } : fdt
11attach simplebus at fdt 11attach simplebus at fdt
12file dev/fdt/fdtbus.c fdt 12file dev/fdt/fdtbus.c fdt
13 13
14device fregulator 14device fregulator
15attach fregulator at fdt 15attach fregulator at fdt
16file dev/fdt/fixedregulator.c fregulator 16file dev/fdt/fixedregulator.c fregulator
17 17
18device gregulator 18device gregulator
19attach gregulator at fdt 19attach gregulator at fdt
20file dev/fdt/gpioregulator.c gregulator 20file dev/fdt/gpioregulator.c gregulator
21 21
22device fclock: clk 22device fclock: clk
23attach fclock at fdt 23attach fclock at fdt
24file dev/fdt/fixedclock.c fclock 24file dev/fdt/fixedclock.c fclock
25 25
26device ffclock: clk 26device ffclock: clk
27attach ffclock at fdt 27attach ffclock at fdt
28file dev/fdt/fixedfactorclock.c ffclock 28file dev/fdt/fixedfactorclock.c ffclock
29 29
30device gpiokeys: sysmon_envsys, sysmon_power, wskbddev, linux_keymap 30device gpiokeys: sysmon_envsys, sysmon_power, wskbddev, linux_keymap
31attach gpiokeys at fdt 31attach gpiokeys at fdt
32file dev/fdt/gpiokeys.c gpiokeys 32file dev/fdt/gpiokeys.c gpiokeys
33 33
34device gpioleds: leds 34device gpioleds: leds
35attach gpioleds at fdt 35attach gpioleds at fdt
36file dev/fdt/gpioleds.c gpioleds 36file dev/fdt/gpioleds.c gpioleds
37 37
38file dev/fdt/fdt_port.c fdt_port 38file dev/fdt/fdt_port.c fdt_port
39 39
40device connector: fdt_port 40device connector: fdt_port
41attach connector at fdt with fdt_connector 41attach connector at fdt with fdt_connector
42file dev/fdt/connector_fdt.c fdt_connector 42file dev/fdt/connector_fdt.c fdt_connector
43 43
44device panel: fdt_port 44device panel: fdt_port
45attach panel at fdt with fdt_panel 45attach panel at fdt with fdt_panel
46file dev/fdt/panel_fdt.c fdt_panel 46file dev/fdt/panel_fdt.c fdt_panel
47 47
48attach panel at fdt with panel_fdt: drmkms 48attach panel at fdt with panel_fdt: drmkms
49file dev/fdt/fdt_panel.c panel_fdt 49file dev/fdt/fdt_panel.c panel_fdt
50 50
51device dispcon: fdt_port, drmkms, ddc_read_edid 51device dispcon: fdt_port, drmkms, ddc_read_edid
52attach dispcon at fdt with dispcon_hdmi 52attach dispcon at fdt with dispcon_hdmi
53file dev/fdt/hdmi_connector.c dispcon_hdmi 53file dev/fdt/hdmi_connector.c dispcon_hdmi
54 54
55file dev/fdt/fdt_openfirm.c fdtbase 55file dev/fdt/fdt_openfirm.c fdtbase
56file dev/fdt/fdt_subr.c fdtbase 56file dev/fdt/fdt_subr.c fdtbase
57file dev/fdt/fdt_clock.c fdt 57file dev/fdt/fdt_clock.c fdt
58file dev/fdt/fdt_dai.c fdt 58file dev/fdt/fdt_dai.c fdt
59file dev/fdt/fdt_dma.c fdt 59file dev/fdt/fdt_dma.c fdt
60file dev/fdt/fdt_gpio.c fdt 60file dev/fdt/fdt_gpio.c fdt
61file dev/fdt/fdt_i2c.c fdt 61file dev/fdt/fdt_i2c.c fdt
62file dev/fdt/fdt_intr.c fdt 62file dev/fdt/fdt_intr.c fdt
63file dev/fdt/fdt_mmc_pwrseq.c fdt 63file dev/fdt/fdt_mmc_pwrseq.c fdt
64file dev/fdt/fdt_phy.c fdt 64file dev/fdt/fdt_phy.c fdt
65file dev/fdt/fdt_power.c fdt 65file dev/fdt/fdt_power.c fdt
66file dev/fdt/fdt_pwm.c fdt 66file dev/fdt/fdt_pwm.c fdt
67file dev/fdt/fdt_regulator.c fdt 67file dev/fdt/fdt_regulator.c fdt
68file dev/fdt/fdt_reset.c fdt 68file dev/fdt/fdt_reset.c fdt
69file dev/fdt/fdt_rtc.c fdt 69file dev/fdt/fdt_rtc.c fdt
70file dev/fdt/fdt_spi.c fdt 70file dev/fdt/fdt_spi.c fdt
71file dev/fdt/fdt_syscon.c fdt 71file dev/fdt/fdt_syscon.c fdt
72file dev/fdt/fdt_pinctrl.c fdt 72file dev/fdt/fdt_pinctrl.c fdt
73 73
74device cpus { } : fdt 74device cpus { } : fdt
75attach cpus at fdt 75attach cpus at fdt
76file dev/fdt/cpus.c cpus 76file dev/fdt/cpus.c cpus
77 77
78device mmcpwrseq 78device mmcpwrseq
79attach mmcpwrseq at fdt with mmcpwrseq_simple 79attach mmcpwrseq at fdt with mmcpwrseq_simple
80file dev/fdt/mmc_pwrseq_simple.c mmcpwrseq_simple 80file dev/fdt/mmc_pwrseq_simple.c mmcpwrseq_simple
81attach mmcpwrseq at fdt with mmcpwrseq_emmc 81attach mmcpwrseq at fdt with mmcpwrseq_emmc
82file dev/fdt/mmc_pwrseq_emmc.c mmcpwrseq_emmc 82file dev/fdt/mmc_pwrseq_emmc.c mmcpwrseq_emmc
83 83
84device syscon { } : fdt 84device syscon { } : fdt
85attach syscon at fdt 85attach syscon at fdt
86file dev/fdt/syscon.c syscon 86file dev/fdt/syscon.c syscon
87 87
88device pinctrl 88device pinctrl
89attach pinctrl at fdt with pinctrl_single 89attach pinctrl at fdt with pinctrl_single
90file dev/fdt/pinctrl_single.c pinctrl_single 90file dev/fdt/pinctrl_single.c pinctrl_single
91 91
92device pwmbacklight 92device pwmbacklight
93attach pwmbacklight at fdt 93attach pwmbacklight at fdt
94file dev/fdt/pwm_backlight.c pwmbacklight 94file dev/fdt/pwm_backlight.c pwmbacklight
95 95
96device pwmfan 96device pwmfan
97attach pwmfan at fdt 97attach pwmfan at fdt
98file dev/fdt/pwm_fan.c pwmfan 98file dev/fdt/pwm_fan.c pwmfan
99 99
100device ausoc: audiobus 100device ausoc: audiobus
101attach ausoc at fdt 101attach ausoc at fdt
102file dev/fdt/ausoc.c ausoc 102file dev/fdt/ausoc.c ausoc
103 103
104device spdiftx 104device spdiftx
105attach spdiftx at fdt 105attach spdiftx at fdt
106file dev/fdt/spdif_tx.c spdiftx 106file dev/fdt/spdif_tx.c spdiftx
107 107
108define fdt_display_timing 108define fdt_display_timing
109file dev/fdt/display_timing.c fdt_display_timing 109file dev/fdt/display_timing.c fdt_display_timing
110 110
111# Display controller 111# Display controller
112attach genfb at fdt with simplefb 112attach genfb at fdt with simplefb
113file dev/fdt/simplefb.c simplefb 113file dev/fdt/simplefb.c simplefb
114 114
 115# NS 8250 compatible UART
 116attach com at fdt with ns8250_uart
 117file dev/fdt/ns8250_uart.c ns8250_uart
 118
115# Designware APB UART 119# Designware APB UART
116attach com at fdt with dw_apb_uart 120attach com at fdt with dw_apb_uart
117file dev/fdt/dw_apb_uart.c dw_apb_uart 121file dev/fdt/dw_apb_uart.c dw_apb_uart
118 122
119# EHCI 123# EHCI
120attach ehci at fdt with ehci_fdt 124attach ehci at fdt with ehci_fdt
121file dev/fdt/ehci_fdt.c ehci_fdt 125file dev/fdt/ehci_fdt.c ehci_fdt
122 126
123# OHCI 127# OHCI
124attach ohci at fdt with ohci_fdt 128attach ohci at fdt with ohci_fdt
125file dev/fdt/ohci_fdt.c ohci_fdt 129file dev/fdt/ohci_fdt.c ohci_fdt
126 130
127# Designware USB2 OTG 131# Designware USB2 OTG
128attach dwctwo at fdt with dwc2_fdt 132attach dwctwo at fdt with dwc2_fdt
129file dev/fdt/dwc2_fdt.c dwc2_fdt 133file dev/fdt/dwc2_fdt.c dwc2_fdt
130 134
131# Designware USB3 XHCI 135# Designware USB3 XHCI
132attach xhci at fdt with dwc3_fdt 136attach xhci at fdt with dwc3_fdt
133file dev/fdt/dwc3_fdt.c dwc3_fdt 137file dev/fdt/dwc3_fdt.c dwc3_fdt
134 138
135# Designware SD/MMC 139# Designware SD/MMC
136attach dwcmmc at fdt with dwcmmc_fdt 140attach dwcmmc at fdt with dwcmmc_fdt
137file dev/fdt/dwcmmc_fdt.c dwcmmc_fdt 141file dev/fdt/dwcmmc_fdt.c dwcmmc_fdt
138 142
139# Designware Watchdog Timer 143# Designware Watchdog Timer
140device dwcwdt: sysmon_wdog 144device dwcwdt: sysmon_wdog
141attach dwcwdt at fdt with dwcwdt_fdt 145attach dwcwdt at fdt with dwcwdt_fdt
142file dev/fdt/dwcwdt_fdt.c dwcwdt_fdt 146file dev/fdt/dwcwdt_fdt.c dwcwdt_fdt
143 147
144# Virtio virtio,mmio 148# Virtio virtio,mmio
145attach virtio at fdt with virtio_mmio_fdt: virtio_mmio 149attach virtio at fdt with virtio_mmio_fdt: virtio_mmio
146file dev/fdt/virtio_mmio_fdt.c virtio_mmio_fdt 150file dev/fdt/virtio_mmio_fdt.c virtio_mmio_fdt
147 151
148# QEMU Firmware Configuration (fw_cfg) device 152# QEMU Firmware Configuration (fw_cfg) device
149attach qemufwcfg at fdt with qemufwcfg_fdt 153attach qemufwcfg at fdt with qemufwcfg_fdt
150file dev/fdt/qemufwcfg_fdt.c qemufwcfg_fdt 154file dev/fdt/qemufwcfg_fdt.c qemufwcfg_fdt
151 155
152# ARM PrimeCell General Purpose Input/Output (PL061) 156# ARM PrimeCell General Purpose Input/Output (PL061)
153attach plgpio at fdt with plgpio_fdt 157attach plgpio at fdt with plgpio_fdt
154file dev/fdt/pl061gpio_fdt.c plgpio_fdt 158file dev/fdt/pl061gpio_fdt.c plgpio_fdt
155 159
156# AHCI SATA controller 160# AHCI SATA controller
157attach ahcisata at fdt with ahcisata_fdt 161attach ahcisata at fdt with ahcisata_fdt
158file dev/fdt/ahcisata_fdt.c ahcisata_fdt 162file dev/fdt/ahcisata_fdt.c ahcisata_fdt
159 163
160# Designware I2C 164# Designware I2C
161attach dwiic at fdt with dwiic_fdt 165attach dwiic at fdt with dwiic_fdt
162file dev/fdt/dwiic_fdt.c dwiic_fdt 166file dev/fdt/dwiic_fdt.c dwiic_fdt
163 167
164# AMD Cryptographic Coprocessor 168# AMD Cryptographic Coprocessor
165attach amdccp at fdt with amdccp_fdt 169attach amdccp at fdt with amdccp_fdt
166file dev/fdt/amdccp_fdt.c amdccp_fdt 170file dev/fdt/amdccp_fdt.c amdccp_fdt
167 171
168# Arasan SDHCI controller 172# Arasan SDHCI controller
169attach sdhc at fdt with arasan_sdhc_fdt 173attach sdhc at fdt with arasan_sdhc_fdt
170file dev/fdt/arasan_sdhc_fdt.c arasan_sdhc_fdt 174file dev/fdt/arasan_sdhc_fdt.c arasan_sdhc_fdt
171 175
172# Generic USB PHY 176# Generic USB PHY
173device usbnopphy 177device usbnopphy
174attach usbnopphy at fdt 178attach usbnopphy at fdt
175file dev/fdt/usbnopphy.c usbnopphy 179file dev/fdt/usbnopphy.c usbnopphy
176 180
177# Simple Amplifier Audio Driver 181# Simple Amplifier Audio Driver
178device simpleamp 182device simpleamp
179attach simpleamp at fdt 183attach simpleamp at fdt
180file dev/fdt/simple_amplifier.c simpleamp 184file dev/fdt/simple_amplifier.c simpleamp
181 185
182# Broadcom GENET v5 186# Broadcom GENET v5
183attach genet at fdt with genet_fdt 187attach genet at fdt with genet_fdt
184file dev/fdt/genet_fdt.c genet_fdt 188file dev/fdt/genet_fdt.c genet_fdt

File Added: src/sys/dev/fdt/ns8250_uart.c
/* $NetBSD: ns8250_uart.c,v 1.1 2020/07/16 11:42:53 jmcneill Exp $ */

/*-
 * Copyright (c) 2017-2020 Jared McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>

__KERNEL_RCSID(1, "$NetBSD: ns8250_uart.c,v 1.1 2020/07/16 11:42:53 jmcneill Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/termios.h>

#include <dev/ic/comvar.h>

#include <dev/fdt/fdtvar.h>

static int ns8250_uart_match(device_t, cfdata_t, void *);
static void ns8250_uart_attach(device_t, device_t, void *);

struct ns8250_config {
	int			type;
	int			(*enable)(struct com_softc *);
	void			(*disable)(struct com_softc *);
};

static const struct ns8250_config ns8250_config = {
	.type = COM_TYPE_NORMAL,
};

static const struct ns8250_config ns16750_config = {
	.type = COM_TYPE_16750,
};

#define	NS8250_OCTEON_USR_REG		0x0138

static int
ns8250_octeon_enable(struct com_softc *sc)
{
	struct com_regs *regsp = &sc->sc_regs;

	/* XXX Clear old busy detect interrupts */
	bus_space_read_1(regsp->cr_iot, regsp->cr_ioh, NS8250_OCTEON_USR_REG);

	return 0;
}

static const struct ns8250_config octeon_config = {
	.type = COM_TYPE_16550_NOERS,
	.enable = ns8250_octeon_enable,
};

static const struct of_compat_data compat_data[] = {
	{ "cavium,octeon-3860-uart",	(uintptr_t)&octeon_config },
	{ "ns8250",			(uintptr_t)&ns8250_config },
	{ "ns16450",			(uintptr_t)&ns8250_config },
	{ "ns16550a",			(uintptr_t)&ns8250_config },
	{ "ns16550",			(uintptr_t)&ns8250_config },
	{ "ns16750",			(uintptr_t)&ns16750_config },
	{ NULL }
};

CFATTACH_DECL_NEW(ns8250_uart, sizeof(struct com_softc),
	ns8250_uart_match, ns8250_uart_attach, NULL, NULL);

static int
ns8250_uart_match(device_t parent, cfdata_t cf, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_match_compat_data(faa->faa_phandle, compat_data);
}

static void
ns8250_uart_attach(device_t parent, device_t self, void *aux)
{
	struct com_softc * const sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	const int phandle = faa->faa_phandle;
	bus_space_tag_t bst = faa->faa_bst;
	bus_space_handle_t bsh;
	char intrstr[128];
	struct clk *clk;
	bus_addr_t addr;
	bus_size_t size;
	u_int reg_shift;
	int error;
	void *ih;

	const struct ns8250_config *config =
	    (void *)of_search_compatible(phandle, compat_data)->data;

	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
		aprint_error(": couldn't get registers\n");
		return;
	}

	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
		/* missing or bad reg-shift property, assume 0 */
		reg_shift = 0;
	}

	sc->sc_dev = self;
	if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency)) {
		clk = fdtbus_clock_get_index(phandle, 0);
		if (clk != NULL)
			sc->sc_frequency = clk_get_rate(clk);
	}
	if (sc->sc_frequency == 0) {
		aprint_error(": couldn't get frequency\n");
		return;
	}

	sc->sc_type = config->type;
	sc->enable = config->enable;
	sc->disable = config->disable;

	error = bus_space_map(bst, addr, size, 0, &bsh);
	if (error) {
		aprint_error(": couldn't map %#" PRIx64 ": %d",
		    (uint64_t)addr, error);
		return;
	}

	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, reg_shift);

	if (config->enable != NULL) {
		sc->enable(sc);
		sc->enabled = 1;
	}

	com_attach_subr(sc);

	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
		aprint_error_dev(self, "failed to decode interrupt\n");
		return;
	}

	ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_SERIAL,
	    FDT_INTR_MPSAFE, comintr, sc);
	if (ih == NULL) {
		aprint_error_dev(self, "failed to establish interrupt on %s\n",
		    intrstr);
		return;
	}
	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
}

/*
 * Console support
 */

static int
ns8250_uart_console_match(int phandle)
{
	return of_match_compat_data(phandle, compat_data);
}

static void
ns8250_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
{
	const int phandle = faa->faa_phandle;
	bus_space_tag_t bst = faa->faa_a4x_bst;
	bus_addr_t addr;
	tcflag_t flags;
	int speed;

	const struct ns8250_config *config =
	    (void *)of_search_compatible(phandle, compat_data)->data;

	fdtbus_get_reg(phandle, 0, &addr, NULL);
	speed = fdtbus_get_stdout_speed();
	if (speed < 0)
		speed = 115200;	/* default */
	flags = fdtbus_get_stdout_flags();

	if (comcnattach(bst, addr, speed, uart_freq, config->type, flags))
		panic("Cannot initialize ns8250 console");
}

static const struct fdt_console ns8250_uart_console = {
	.match = ns8250_uart_console_match,
	.consinit = ns8250_uart_console_consinit,
};

FDT_CONSOLE(ns8250_uart, &ns8250_uart_console);