a9tmr_fdt.c revision 1.3
11.3Sjmcneill/* $NetBSD: a9tmr_fdt.c,v 1.3 2019/01/22 15:17:33 jmcneill Exp $ */
21.1Shkenken
31.1Shkenken/*-
41.1Shkenken * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51.1Shkenken * All rights reserved.
61.1Shkenken *
71.1Shkenken * Redistribution and use in source and binary forms, with or without
81.1Shkenken * modification, are permitted provided that the following conditions
91.1Shkenken * are met:
101.1Shkenken * 1. Redistributions of source code must retain the above copyright
111.1Shkenken *    notice, this list of conditions and the following disclaimer.
121.1Shkenken * 2. Redistributions in binary form must reproduce the above copyright
131.1Shkenken *    notice, this list of conditions and the following disclaimer in the
141.1Shkenken *    documentation and/or other materials provided with the distribution.
151.1Shkenken *
161.1Shkenken * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Shkenken * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Shkenken * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Shkenken * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Shkenken * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Shkenken * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Shkenken * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Shkenken * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Shkenken * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Shkenken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Shkenken * SUCH DAMAGE.
271.1Shkenken */
281.1Shkenken
291.1Shkenken#include <sys/cdefs.h>
301.3Sjmcneill__KERNEL_RCSID(0, "$NetBSD: a9tmr_fdt.c,v 1.3 2019/01/22 15:17:33 jmcneill Exp $");
311.1Shkenken
321.1Shkenken#include <sys/param.h>
331.1Shkenken#include <sys/bus.h>
341.1Shkenken#include <sys/device.h>
351.1Shkenken#include <sys/intr.h>
361.1Shkenken#include <sys/systm.h>
371.1Shkenken#include <sys/kernel.h>
381.1Shkenken#include <sys/kmem.h>
391.1Shkenken
401.1Shkenken#include <arm/cortex/a9tmr_intr.h>
411.1Shkenken#include <arm/cortex/mpcore_var.h>
421.1Shkenken#include <arm/cortex/a9tmr_var.h>
431.1Shkenken
441.1Shkenken#include <dev/fdt/fdtvar.h>
451.1Shkenken#include <arm/fdt/arm_fdtvar.h>
461.1Shkenken
471.1Shkenkenstatic int	a9tmr_fdt_match(device_t, cfdata_t, void *);
481.1Shkenkenstatic void	a9tmr_fdt_attach(device_t, device_t, void *);
491.1Shkenken
501.1Shkenkenstatic void	a9tmr_fdt_cpu_hatch(void *, struct cpu_info *);
511.3Sjmcneillstatic void	a9tmr_fdt_speed_changed(device_t);
521.1Shkenken
531.3Sjmcneillstruct a9tmr_fdt_softc {
541.3Sjmcneill	device_t	sc_dev;
551.3Sjmcneill	struct clk	*sc_clk;
561.3Sjmcneill};
571.3Sjmcneill
581.3SjmcneillCFATTACH_DECL_NEW(a9tmr_fdt, sizeof(struct a9tmr_fdt_softc),
591.3Sjmcneill    a9tmr_fdt_match, a9tmr_fdt_attach, NULL, NULL);
601.1Shkenken
611.1Shkenkenstatic int
621.1Shkenkena9tmr_fdt_match(device_t parent, cfdata_t cf, void *aux)
631.1Shkenken{
641.1Shkenken	const char * const compatible[] = {
651.2Sjmcneill		"arm,cortex-a5-global-timer",
661.1Shkenken		"arm,cortex-a9-global-timer",
671.1Shkenken		NULL
681.1Shkenken	};
691.1Shkenken	struct fdt_attach_args * const faa = aux;
701.1Shkenken
711.1Shkenken	return of_compatible(faa->faa_phandle, compatible) >= 0;
721.1Shkenken}
731.1Shkenken
741.1Shkenkenstatic void
751.1Shkenkena9tmr_fdt_attach(device_t parent, device_t self, void *aux)
761.1Shkenken{
771.3Sjmcneill	struct a9tmr_fdt_softc * const sc = device_private(self);
781.1Shkenken	struct fdt_attach_args * const faa = aux;
791.1Shkenken	const int phandle = faa->faa_phandle;
801.1Shkenken	bus_space_handle_t bsh;
811.1Shkenken
821.3Sjmcneill	sc->sc_dev = self;
831.3Sjmcneill	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
841.3Sjmcneill	if (sc->sc_clk == NULL) {
851.1Shkenken		aprint_error(": couldn't get clock\n");
861.1Shkenken		return;
871.1Shkenken	}
881.3Sjmcneill	if (clk_enable(sc->sc_clk) != 0) {
891.1Shkenken		aprint_error(": couldn't enable clock\n");
901.1Shkenken		return;
911.1Shkenken	}
921.1Shkenken
931.3Sjmcneill	uint32_t rate = clk_get_rate(sc->sc_clk);
941.1Shkenken	prop_dictionary_t dict = device_properties(self);
951.1Shkenken	prop_dictionary_set_uint32(dict, "frequency", rate);
961.1Shkenken
971.1Shkenken	char intrstr[128];
981.1Shkenken	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
991.1Shkenken		aprint_error(": failed to decode interrupt\n");
1001.1Shkenken		return;
1011.1Shkenken	}
1021.1Shkenken
1031.2Sjmcneill	aprint_naive("\n");
1041.2Sjmcneill	aprint_normal("\n");
1051.2Sjmcneill
1061.1Shkenken	void *ih = fdtbus_intr_establish(phandle, 0, IPL_CLOCK,
1071.1Shkenken	    FDT_INTR_MPSAFE, a9tmr_intr, NULL);
1081.1Shkenken	if (ih == NULL) {
1091.1Shkenken		aprint_error_dev(self, "couldn't install interrupt handler\n");
1101.1Shkenken		return;
1111.1Shkenken	}
1121.1Shkenken	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1131.1Shkenken
1141.1Shkenken	bus_addr_t addr;
1151.1Shkenken	bus_size_t size;
1161.1Shkenken	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1171.1Shkenken		aprint_error(": couldn't get distributor address\n");
1181.1Shkenken		return;
1191.1Shkenken	}
1201.1Shkenken	if (bus_space_map(faa->faa_bst, addr, size, 0, &bsh)) {
1211.1Shkenken		aprint_error(": couldn't map registers\n");
1221.1Shkenken		return;
1231.1Shkenken	}
1241.1Shkenken
1251.1Shkenken	struct mpcore_attach_args mpcaa = {
1261.1Shkenken		.mpcaa_name = "arma9tmr",
1271.1Shkenken		.mpcaa_memt = faa->faa_bst,
1281.1Shkenken		.mpcaa_memh = bsh,
1291.1Shkenken		.mpcaa_irq = -1,
1301.1Shkenken	};
1311.1Shkenken
1321.1Shkenken	config_found(self, &mpcaa, NULL);
1331.1Shkenken
1341.1Shkenken	arm_fdt_cpu_hatch_register(self, a9tmr_fdt_cpu_hatch);
1351.1Shkenken	arm_fdt_timer_register(a9tmr_cpu_initclocks);
1361.3Sjmcneill
1371.3Sjmcneill	pmf_event_register(self, PMFE_SPEED_CHANGED, a9tmr_fdt_speed_changed, true);
1381.1Shkenken}
1391.1Shkenken
1401.1Shkenkenstatic void
1411.1Shkenkena9tmr_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
1421.1Shkenken{
1431.1Shkenken	a9tmr_init_cpu_clock(ci);
1441.1Shkenken}
1451.3Sjmcneill
1461.3Sjmcneillstatic void
1471.3Sjmcneilla9tmr_fdt_speed_changed(device_t dev)
1481.3Sjmcneill{
1491.3Sjmcneill	struct a9tmr_fdt_softc * const sc = device_private(dev);
1501.3Sjmcneill	prop_dictionary_t dict = device_properties(dev);
1511.3Sjmcneill	uint32_t rate;
1521.3Sjmcneill
1531.3Sjmcneill	rate = clk_get_rate(sc->sc_clk);
1541.3Sjmcneill	prop_dictionary_set_uint32(dict, "frequency", rate);
1551.3Sjmcneill
1561.3Sjmcneill	a9tmr_update_freq(rate);
1571.3Sjmcneill}
158