plcom_fdt.c revision 1.6
11.6Smlelstv/* $NetBSD: plcom_fdt.c,v 1.6 2023/01/24 06:56:40 mlelstv Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.6Smlelstv__KERNEL_RCSID(0, "$NetBSD: plcom_fdt.c,v 1.6 2023/01/24 06:56:40 mlelstv Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill#include <sys/kernel.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/fdt/fdtvar.h>
391.1Sjmcneill
401.1Sjmcneill#include <evbarm/dev/plcomreg.h>
411.1Sjmcneill#include <evbarm/dev/plcomvar.h>
421.1Sjmcneill
431.1Sjmcneillstatic int	plcom_fdt_match(device_t, cfdata_t, void *);
441.1Sjmcneillstatic void	plcom_fdt_attach(device_t, device_t, void *);
451.1Sjmcneill
461.5Sthorpejstatic const struct device_compatible_entry compat_data[] = {
471.6Smlelstv	{ .compat = "arm,pl011", .value = PLCOM_TYPE_PL011 },
481.6Smlelstv	{ .compat = "arm,sbsa-uart", .value = PLCOM_TYPE_GENERIC_UART },
491.5Sthorpej	DEVICE_COMPAT_EOL
501.5Sthorpej};
511.1Sjmcneill
521.1SjmcneillCFATTACH_DECL_NEW(plcom_fdt, sizeof(struct plcom_softc),
531.1Sjmcneill	plcom_fdt_match, plcom_fdt_attach, NULL, NULL);
541.1Sjmcneill
551.1Sjmcneillstatic int
561.1Sjmcneillplcom_fdt_match(device_t parent, cfdata_t cf, void *aux)
571.1Sjmcneill{
581.1Sjmcneill	struct fdt_attach_args * const faa = aux;
591.1Sjmcneill
601.5Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
611.1Sjmcneill}
621.1Sjmcneill
631.1Sjmcneillstatic void
641.1Sjmcneillplcom_fdt_attach(device_t parent, device_t self, void *aux)
651.1Sjmcneill{
661.1Sjmcneill	struct plcom_softc * const sc = device_private(self);
671.1Sjmcneill	struct fdt_attach_args * const faa = aux;
681.1Sjmcneill	const int phandle = faa->faa_phandle;
691.2Sjmcneill	char intrstr[128];
701.1Sjmcneill	struct clk *clk;
711.1Sjmcneill	bus_addr_t addr;
721.1Sjmcneill	bus_size_t size;
731.1Sjmcneill	void *ih;
741.6Smlelstv	const u_int *data;
751.6Smlelstv	int len;
761.1Sjmcneill
771.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
781.1Sjmcneill		aprint_error(": missing 'reg' property\n");
791.1Sjmcneill		return;
801.1Sjmcneill	}
811.1Sjmcneill
821.2Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
831.2Sjmcneill		aprint_error(": failed to decode interrupt\n");
841.2Sjmcneill		return;
851.2Sjmcneill	}
861.2Sjmcneill
871.1Sjmcneill	sc->sc_dev = self;
881.1Sjmcneill
891.1Sjmcneill	/* Enable clocks */
901.1Sjmcneill	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
911.1Sjmcneill		if (clk_enable(clk) != 0) {
921.1Sjmcneill			aprint_error(": failed to enable clock #%d\n", i);
931.1Sjmcneill			return;
941.1Sjmcneill		}
951.1Sjmcneill		/* First clock is UARTCLK */
961.1Sjmcneill		if (i == 0)
971.1Sjmcneill			sc->sc_frequency = clk_get_rate(clk);
981.1Sjmcneill	}
991.1Sjmcneill
1001.6Smlelstv	sc->sc_hwflags = 0;
1011.1Sjmcneill	sc->sc_swflags = 0;
1021.1Sjmcneill
1031.6Smlelstv	if ((data = fdtbus_get_prop(phandle, "arm,primecell-periphid", &len)) != NULL)
1041.6Smlelstv                sc->sc_pi.pi_periphid = be32toh(data[0]);
1051.6Smlelstv
1061.6Smlelstv	sc->sc_pi.pi_type = of_compatible_lookup(faa->faa_phandle, compat_data)->value;
1071.1Sjmcneill	sc->sc_pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
1081.1Sjmcneill	sc->sc_pi.pi_iot = faa->faa_bst;
1091.1Sjmcneill	sc->sc_pi.pi_iobase = addr;
1101.1Sjmcneill	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_pi.pi_ioh)) {
1111.1Sjmcneill		aprint_error(": couldn't map device\n");
1121.1Sjmcneill		return;
1131.1Sjmcneill	}
1141.3Sjmcneill
1151.3Sjmcneill	aprint_naive("\n");
1161.3Sjmcneill	aprint_normal(": ARM PL011 UART\n");
1171.3Sjmcneill
1181.1Sjmcneill	plcom_attach_subr(sc);
1191.1Sjmcneill
1201.2Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1211.2Sjmcneill
1221.4Sryo	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, FDT_INTR_MPSAFE,
1231.4Sryo	    plcomintr, sc, device_xname(self));
1241.1Sjmcneill	if (ih == NULL) {
1251.1Sjmcneill		aprint_error_dev(self, "couldn't install interrupt handler\n");
1261.1Sjmcneill		return;
1271.1Sjmcneill	}
1281.1Sjmcneill}
1291.1Sjmcneill
1301.1Sjmcneillstatic int
1311.1Sjmcneillplcom_fdt_console_match(int phandle)
1321.1Sjmcneill{
1331.5Sthorpej	return of_compatible_match(phandle, compat_data);
1341.1Sjmcneill}
1351.1Sjmcneill
1361.1Sjmcneillstatic void
1371.1Sjmcneillplcom_fdt_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
1381.1Sjmcneill{
1391.1Sjmcneill	static struct plcom_instance pi;
1401.1Sjmcneill	bus_addr_t addr;
1411.1Sjmcneill	bus_size_t size;
1421.1Sjmcneill	tcflag_t flags;
1431.1Sjmcneill	int speed;
1441.1Sjmcneill
1451.1Sjmcneill	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0)
1461.1Sjmcneill		return;
1471.1Sjmcneill
1481.1Sjmcneill	pi.pi_type = PLCOM_TYPE_PL011;
1491.1Sjmcneill	pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
1501.1Sjmcneill	pi.pi_iot = faa->faa_bst;
1511.1Sjmcneill	pi.pi_iobase = addr;
1521.1Sjmcneill	pi.pi_size = size;
1531.1Sjmcneill
1541.1Sjmcneill	speed = fdtbus_get_stdout_speed();
1551.1Sjmcneill	if (speed < 0)
1561.1Sjmcneill		speed = 115200;
1571.1Sjmcneill	flags = fdtbus_get_stdout_flags();
1581.1Sjmcneill
1591.1Sjmcneill	plcomcnattach(&pi, speed, uart_freq, flags, -1);
1601.1Sjmcneill}
1611.1Sjmcneill
1621.1Sjmcneillstatic const struct fdt_console plcom_fdt_console = {
1631.1Sjmcneill	.match = plcom_fdt_console_match,
1641.1Sjmcneill	.consinit = plcom_fdt_console_consinit,
1651.1Sjmcneill};
1661.1Sjmcneill
1671.1SjmcneillFDT_CONSOLE(plcom_fdt, &plcom_fdt_console);
168