xref: /netbsd-src/sys/arch/arm/fdt/plcom_fdt.c (revision 820c39cec25def994102cd51c1430bc4f78b2f31)
1*820c39ceSmlelstv /* $NetBSD: plcom_fdt.c,v 1.6 2023/01/24 06:56:40 mlelstv Exp $ */
21f383b39Sjmcneill 
31f383b39Sjmcneill /*-
41f383b39Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51f383b39Sjmcneill  * All rights reserved.
61f383b39Sjmcneill  *
71f383b39Sjmcneill  * Redistribution and use in source and binary forms, with or without
81f383b39Sjmcneill  * modification, are permitted provided that the following conditions
91f383b39Sjmcneill  * are met:
101f383b39Sjmcneill  * 1. Redistributions of source code must retain the above copyright
111f383b39Sjmcneill  *    notice, this list of conditions and the following disclaimer.
121f383b39Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
131f383b39Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
141f383b39Sjmcneill  *    documentation and/or other materials provided with the distribution.
151f383b39Sjmcneill  *
161f383b39Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171f383b39Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181f383b39Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191f383b39Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201f383b39Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211f383b39Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221f383b39Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231f383b39Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241f383b39Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251f383b39Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261f383b39Sjmcneill  * SUCH DAMAGE.
271f383b39Sjmcneill  */
281f383b39Sjmcneill 
291f383b39Sjmcneill #include <sys/cdefs.h>
30*820c39ceSmlelstv __KERNEL_RCSID(0, "$NetBSD: plcom_fdt.c,v 1.6 2023/01/24 06:56:40 mlelstv Exp $");
311f383b39Sjmcneill 
321f383b39Sjmcneill #include <sys/param.h>
331f383b39Sjmcneill #include <sys/bus.h>
341f383b39Sjmcneill #include <sys/device.h>
351f383b39Sjmcneill #include <sys/systm.h>
361f383b39Sjmcneill #include <sys/kernel.h>
371f383b39Sjmcneill 
381f383b39Sjmcneill #include <dev/fdt/fdtvar.h>
391f383b39Sjmcneill 
401f383b39Sjmcneill #include <evbarm/dev/plcomreg.h>
411f383b39Sjmcneill #include <evbarm/dev/plcomvar.h>
421f383b39Sjmcneill 
431f383b39Sjmcneill static int	plcom_fdt_match(device_t, cfdata_t, void *);
441f383b39Sjmcneill static void	plcom_fdt_attach(device_t, device_t, void *);
451f383b39Sjmcneill 
466e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
47*820c39ceSmlelstv 	{ .compat = "arm,pl011", .value = PLCOM_TYPE_PL011 },
48*820c39ceSmlelstv 	{ .compat = "arm,sbsa-uart", .value = PLCOM_TYPE_GENERIC_UART },
496e54367aSthorpej 	DEVICE_COMPAT_EOL
506e54367aSthorpej };
511f383b39Sjmcneill 
521f383b39Sjmcneill CFATTACH_DECL_NEW(plcom_fdt, sizeof(struct plcom_softc),
531f383b39Sjmcneill 	plcom_fdt_match, plcom_fdt_attach, NULL, NULL);
541f383b39Sjmcneill 
551f383b39Sjmcneill static int
plcom_fdt_match(device_t parent,cfdata_t cf,void * aux)561f383b39Sjmcneill plcom_fdt_match(device_t parent, cfdata_t cf, void *aux)
571f383b39Sjmcneill {
581f383b39Sjmcneill 	struct fdt_attach_args * const faa = aux;
591f383b39Sjmcneill 
606e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
611f383b39Sjmcneill }
621f383b39Sjmcneill 
631f383b39Sjmcneill static void
plcom_fdt_attach(device_t parent,device_t self,void * aux)641f383b39Sjmcneill plcom_fdt_attach(device_t parent, device_t self, void *aux)
651f383b39Sjmcneill {
661f383b39Sjmcneill 	struct plcom_softc * const sc = device_private(self);
671f383b39Sjmcneill 	struct fdt_attach_args * const faa = aux;
681f383b39Sjmcneill 	const int phandle = faa->faa_phandle;
69051740ccSjmcneill 	char intrstr[128];
701f383b39Sjmcneill 	struct clk *clk;
711f383b39Sjmcneill 	bus_addr_t addr;
721f383b39Sjmcneill 	bus_size_t size;
731f383b39Sjmcneill 	void *ih;
74*820c39ceSmlelstv 	const u_int *data;
75*820c39ceSmlelstv 	int len;
761f383b39Sjmcneill 
771f383b39Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
781f383b39Sjmcneill 		aprint_error(": missing 'reg' property\n");
791f383b39Sjmcneill 		return;
801f383b39Sjmcneill 	}
811f383b39Sjmcneill 
82051740ccSjmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
83051740ccSjmcneill 		aprint_error(": failed to decode interrupt\n");
84051740ccSjmcneill 		return;
85051740ccSjmcneill 	}
86051740ccSjmcneill 
871f383b39Sjmcneill 	sc->sc_dev = self;
881f383b39Sjmcneill 
891f383b39Sjmcneill 	/* Enable clocks */
901f383b39Sjmcneill 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
911f383b39Sjmcneill 		if (clk_enable(clk) != 0) {
921f383b39Sjmcneill 			aprint_error(": failed to enable clock #%d\n", i);
931f383b39Sjmcneill 			return;
941f383b39Sjmcneill 		}
951f383b39Sjmcneill 		/* First clock is UARTCLK */
961f383b39Sjmcneill 		if (i == 0)
971f383b39Sjmcneill 			sc->sc_frequency = clk_get_rate(clk);
981f383b39Sjmcneill 	}
991f383b39Sjmcneill 
100*820c39ceSmlelstv 	sc->sc_hwflags = 0;
1011f383b39Sjmcneill 	sc->sc_swflags = 0;
1021f383b39Sjmcneill 
103*820c39ceSmlelstv 	if ((data = fdtbus_get_prop(phandle, "arm,primecell-periphid", &len)) != NULL)
104*820c39ceSmlelstv                 sc->sc_pi.pi_periphid = be32toh(data[0]);
105*820c39ceSmlelstv 
106*820c39ceSmlelstv 	sc->sc_pi.pi_type = of_compatible_lookup(faa->faa_phandle, compat_data)->value;
1071f383b39Sjmcneill 	sc->sc_pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
1081f383b39Sjmcneill 	sc->sc_pi.pi_iot = faa->faa_bst;
1091f383b39Sjmcneill 	sc->sc_pi.pi_iobase = addr;
1101f383b39Sjmcneill 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_pi.pi_ioh)) {
1111f383b39Sjmcneill 		aprint_error(": couldn't map device\n");
1121f383b39Sjmcneill 		return;
1131f383b39Sjmcneill 	}
11455dba9eeSjmcneill 
11555dba9eeSjmcneill 	aprint_naive("\n");
11655dba9eeSjmcneill 	aprint_normal(": ARM PL011 UART\n");
11755dba9eeSjmcneill 
1181f383b39Sjmcneill 	plcom_attach_subr(sc);
1191f383b39Sjmcneill 
120051740ccSjmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
121051740ccSjmcneill 
12264e248edSryo 	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, FDT_INTR_MPSAFE,
12364e248edSryo 	    plcomintr, sc, device_xname(self));
1241f383b39Sjmcneill 	if (ih == NULL) {
1251f383b39Sjmcneill 		aprint_error_dev(self, "couldn't install interrupt handler\n");
1261f383b39Sjmcneill 		return;
1271f383b39Sjmcneill 	}
1281f383b39Sjmcneill }
1291f383b39Sjmcneill 
1301f383b39Sjmcneill static int
plcom_fdt_console_match(int phandle)1311f383b39Sjmcneill plcom_fdt_console_match(int phandle)
1321f383b39Sjmcneill {
1336e54367aSthorpej 	return of_compatible_match(phandle, compat_data);
1341f383b39Sjmcneill }
1351f383b39Sjmcneill 
1361f383b39Sjmcneill static void
plcom_fdt_console_consinit(struct fdt_attach_args * faa,u_int uart_freq)1371f383b39Sjmcneill plcom_fdt_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
1381f383b39Sjmcneill {
1391f383b39Sjmcneill 	static struct plcom_instance pi;
1401f383b39Sjmcneill 	bus_addr_t addr;
1411f383b39Sjmcneill 	bus_size_t size;
1421f383b39Sjmcneill 	tcflag_t flags;
1431f383b39Sjmcneill 	int speed;
1441f383b39Sjmcneill 
1451f383b39Sjmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0)
1461f383b39Sjmcneill 		return;
1471f383b39Sjmcneill 
1481f383b39Sjmcneill 	pi.pi_type = PLCOM_TYPE_PL011;
1491f383b39Sjmcneill 	pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
1501f383b39Sjmcneill 	pi.pi_iot = faa->faa_bst;
1511f383b39Sjmcneill 	pi.pi_iobase = addr;
1521f383b39Sjmcneill 	pi.pi_size = size;
1531f383b39Sjmcneill 
1541f383b39Sjmcneill 	speed = fdtbus_get_stdout_speed();
1551f383b39Sjmcneill 	if (speed < 0)
1561f383b39Sjmcneill 		speed = 115200;
1571f383b39Sjmcneill 	flags = fdtbus_get_stdout_flags();
1581f383b39Sjmcneill 
1591f383b39Sjmcneill 	plcomcnattach(&pi, speed, uart_freq, flags, -1);
1601f383b39Sjmcneill }
1611f383b39Sjmcneill 
1621f383b39Sjmcneill static const struct fdt_console plcom_fdt_console = {
1631f383b39Sjmcneill 	.match = plcom_fdt_console_match,
1641f383b39Sjmcneill 	.consinit = plcom_fdt_console_consinit,
1651f383b39Sjmcneill };
1661f383b39Sjmcneill 
1671f383b39Sjmcneill FDT_CONSOLE(plcom_fdt, &plcom_fdt_console);
168