Home | History | Annotate | Line # | Download | only in imx
      1 /* $NetBSD: imx23timrot_fdt.c,v 1.1 2025/10/09 06:15:17 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Yuri Honegger.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: imx23timrot_fdt.c,v 1.1 2025/10/09 06:15:17 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/device.h>
     38 
     39 #include <dev/fdt/fdtvar.h>
     40 
     41 #include <arm/imx/imx23var.h>
     42 #include <arm/imx/imx23_timrotvar.h>
     43 #include <arm/fdt/arm_fdtvar.h>
     44 
     45 static int imx23timrot_fdt_match(device_t, cfdata_t, void *);
     46 static void imx23timrot_fdt_attach(device_t, device_t, void *);
     47 
     48 struct imx23timrot_fdt_softc {
     49 	struct timrot_softc systimer_sc;
     50 	struct timrot_softc stattimer_sc;
     51 };
     52 
     53 CFATTACH_DECL_NEW(imx23timrot_fdt, sizeof(struct imx23timrot_fdt_softc),
     54 		  imx23timrot_fdt_match, imx23timrot_fdt_attach, NULL, NULL);
     55 
     56 static const struct device_compatible_entry compat_data[] = {
     57 	{ .compat = "fsl,imx23-timrot" },
     58 	{ .compat = "fsl,timrot" },
     59 	DEVICE_COMPAT_EOL
     60 };
     61 
     62 static int
     63 imx23timrot_fdt_match(device_t parent, cfdata_t cf, void *aux)
     64 {
     65 	struct fdt_attach_args * const faa = aux;
     66 
     67 	return of_compatible_match(faa->faa_phandle, compat_data);
     68 }
     69 
     70 /*
     71  * The original non-FDT timrot driver instantiates one timrot device per timer.
     72  * Unfortunately, the device tree from linux specifies just one timrot device
     73  * for all timers. This driver therefore attaches "two" timrot devices from one.
     74  */
     75 static void
     76 imx23timrot_fdt_attach(device_t parent, device_t self, void *aux)
     77 {
     78 	struct imx23timrot_fdt_softc * const sc = device_private(self);
     79 	struct fdt_attach_args * const faa = aux;
     80 	const int phandle = faa->faa_phandle;
     81 	char intrstr[128];
     82 
     83 	/*
     84 	 * The actual memory mapping is done inside the
     85 	 * timrot_[sys|stat]timer_init functions.
     86 	 */
     87 	bus_addr_t addr;
     88 	bus_size_t size;
     89 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     90 		aprint_error(": couldn't get register address\n");
     91 		return;
     92 	}
     93 
     94 	/* Use -1 as irq because we establish interrupts ourselves */
     95 	imx23timrot_systimer_init(&sc->systimer_sc, faa->faa_bst, -1);
     96 	imx23timrot_stattimer_init(&sc->stattimer_sc, faa->faa_bst, -1);
     97 
     98 
     99 	/* System Timer Interrupt*/
    100 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    101 		aprint_error(": failed to decode interrupt\n");
    102 		return;
    103 	}
    104 	void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_CLOCK, 0,
    105 					       imx23timrot_systimer_irq, NULL,
    106 					       device_xname(self));
    107 	if (ih == NULL) {
    108 		aprint_error_dev(self,
    109 		    "couldn't install systimer interrupt handler\n");
    110 		return;
    111 	}
    112 	aprint_normal_dev(self, ": systimer on %s", intrstr);
    113 
    114 	/* Stat Timer Interrupt*/
    115 	if (!fdtbus_intr_str(phandle, 1, intrstr, sizeof(intrstr))) {
    116 		aprint_error(": failed to decode interrupt\n");
    117 		return;
    118 	}
    119 	ih = fdtbus_intr_establish_xname(phandle, 1, IPL_CLOCK, 0,
    120 					 imx23timrot_stattimer_irq, NULL,
    121 					 device_xname(self));
    122 	if (ih == NULL) {
    123 		aprint_error_dev(self,
    124 		    "couldn't install stattimer interrupt handler\n");
    125 		return;
    126 	}
    127 	aprint_normal_dev(self, ": stattimer on %s\n", intrstr);
    128 
    129 	aprint_naive("\n");
    130 	aprint_normal("\n");
    131 
    132 	struct apb_attach_args apbaa = {
    133 		.aa_name = "imx23timrot",
    134 		.aa_iot = faa->faa_bst,
    135 		.aa_dmat = faa->faa_dmat,
    136 		.aa_addr = addr,
    137 		.aa_size = size,
    138 		.aa_irq = -1
    139 	};
    140 
    141 	config_found(self, &apbaa, NULL, CFARGS_NONE);
    142 
    143 	arm_fdt_timer_register(imx23timrot_cpu_initclocks);
    144 }
    145