a9tmr_fdt.c revision 1.1
11.1Shkenken/* $NetBSD: a9tmr_fdt.c,v 1.1 2018/06/05 08:03:28 hkenken 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.1Shkenken__KERNEL_RCSID(0, "$NetBSD: a9tmr_fdt.c,v 1.1 2018/06/05 08:03:28 hkenken 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.1Shkenken
521.1ShkenkenCFATTACH_DECL_NEW(a9tmr_fdt, 0, a9tmr_fdt_match, a9tmr_fdt_attach, NULL, NULL);
531.1Shkenken
541.1Shkenkenstatic int
551.1Shkenkena9tmr_fdt_match(device_t parent, cfdata_t cf, void *aux)
561.1Shkenken{
571.1Shkenken	const char * const compatible[] = {
581.1Shkenken		"arm,cortex-a9-global-timer",
591.1Shkenken		NULL
601.1Shkenken	};
611.1Shkenken	struct fdt_attach_args * const faa = aux;
621.1Shkenken
631.1Shkenken	return of_compatible(faa->faa_phandle, compatible) >= 0;
641.1Shkenken}
651.1Shkenken
661.1Shkenkenstatic void
671.1Shkenkena9tmr_fdt_attach(device_t parent, device_t self, void *aux)
681.1Shkenken{
691.1Shkenken	struct fdt_attach_args * const faa = aux;
701.1Shkenken	const int phandle = faa->faa_phandle;
711.1Shkenken	bus_space_handle_t bsh;
721.1Shkenken
731.1Shkenken	struct clk *clk = fdtbus_clock_get_index(phandle, 0);
741.1Shkenken	if (clk == NULL) {
751.1Shkenken		aprint_error(": couldn't get clock\n");
761.1Shkenken		return;
771.1Shkenken	}
781.1Shkenken	if (clk_enable(clk) != 0) {
791.1Shkenken		aprint_error(": couldn't enable clock\n");
801.1Shkenken		return;
811.1Shkenken	}
821.1Shkenken
831.1Shkenken	uint32_t rate = clk_get_rate(clk);
841.1Shkenken	prop_dictionary_t dict = device_properties(self);
851.1Shkenken	prop_dictionary_set_uint32(dict, "frequency", rate);
861.1Shkenken
871.1Shkenken	char intrstr[128];
881.1Shkenken	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
891.1Shkenken		aprint_error(": failed to decode interrupt\n");
901.1Shkenken		return;
911.1Shkenken	}
921.1Shkenken
931.1Shkenken	void *ih = fdtbus_intr_establish(phandle, 0, IPL_CLOCK,
941.1Shkenken	    FDT_INTR_MPSAFE, a9tmr_intr, NULL);
951.1Shkenken	if (ih == NULL) {
961.1Shkenken		aprint_error_dev(self, "couldn't install interrupt handler\n");
971.1Shkenken		return;
981.1Shkenken	}
991.1Shkenken	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1001.1Shkenken
1011.1Shkenken	bus_addr_t addr;
1021.1Shkenken	bus_size_t size;
1031.1Shkenken	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1041.1Shkenken		aprint_error(": couldn't get distributor address\n");
1051.1Shkenken		return;
1061.1Shkenken	}
1071.1Shkenken	if (bus_space_map(faa->faa_bst, addr, size, 0, &bsh)) {
1081.1Shkenken		aprint_error(": couldn't map registers\n");
1091.1Shkenken		return;
1101.1Shkenken	}
1111.1Shkenken
1121.1Shkenken	struct mpcore_attach_args mpcaa = {
1131.1Shkenken		.mpcaa_name = "arma9tmr",
1141.1Shkenken		.mpcaa_memt = faa->faa_bst,
1151.1Shkenken		.mpcaa_memh = bsh,
1161.1Shkenken		.mpcaa_irq = -1,
1171.1Shkenken	};
1181.1Shkenken
1191.1Shkenken	config_found(self, &mpcaa, NULL);
1201.1Shkenken
1211.1Shkenken	arm_fdt_cpu_hatch_register(self, a9tmr_fdt_cpu_hatch);
1221.1Shkenken	arm_fdt_timer_register(a9tmr_cpu_initclocks);
1231.1Shkenken}
1241.1Shkenken
1251.1Shkenkenstatic void
1261.1Shkenkena9tmr_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
1271.1Shkenken{
1281.1Shkenken	a9tmr_init_cpu_clock(ci);
1291.1Shkenken}
130