Home | History | Annotate | Line # | Download | only in imx
      1 /* $NetBSD: imx23icoll_fdt.c,v 1.1 2025/10/09 06:15:16 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 #define _INTR_PRIVATE
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: imx23icoll_fdt.c,v 1.1 2025/10/09 06:15:16 skrll Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/device.h>
     39 
     40 #include <dev/fdt/fdtvar.h>
     41 
     42 #include <arm/fdt/arm_fdtvar.h>
     43 #include <arm/imx/imx23var.h>
     44 #include <arm/imx/imx23_icollvar.h>
     45 
     46 static int imx23icoll_fdt_match(device_t, cfdata_t, void *);
     47 static void imx23icoll_fdt_attach(device_t, device_t, void *);
     48 
     49 static void *	imx23icoll_fdt_establish(device_t, u_int *, int, int,
     50 		    int (*)(void *), void *, const char *);
     51 static void	imx23icoll_fdt_disestablish(device_t, void *);
     52 static bool	imx23icoll_fdt_intrstr(device_t, u_int *, char *, size_t);
     53 
     54 struct imx23icoll_fdt_softc {
     55 	struct icoll_softc sc_icoll;
     56 };
     57 
     58 CFATTACH_DECL_NEW(imx23icoll_fdt, sizeof(struct imx23icoll_fdt_softc),
     59 		  imx23icoll_fdt_match, imx23icoll_fdt_attach, NULL, NULL);
     60 
     61 struct fdtbus_interrupt_controller_func imx23icoll_fdt_funcs = {
     62 	.establish = imx23icoll_fdt_establish,
     63 	.disestablish = imx23icoll_fdt_disestablish,
     64 	.intrstr = imx23icoll_fdt_intrstr
     65 };
     66 
     67 static const struct device_compatible_entry compat_data[] = {
     68 	{ .compat = "fsl,imx23-icoll" },
     69 	{ .compat = "fsl,icoll" },
     70 	DEVICE_COMPAT_EOL
     71 };
     72 
     73 static int
     74 imx23icoll_fdt_match(device_t parent, cfdata_t cf, void *aux)
     75 {
     76 	struct fdt_attach_args * const faa = aux;
     77 
     78 	return of_compatible_match(faa->faa_phandle, compat_data);
     79 }
     80 
     81 static void
     82 imx23icoll_fdt_attach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct imx23icoll_fdt_softc * const sc = device_private(self);
     85 	struct fdt_attach_args * const faa = aux;
     86 	const int phandle = faa->faa_phandle;
     87 
     88 	/*
     89 	 * Initialize icoll controller
     90 	 */
     91 	bus_addr_t addr;
     92 	bus_size_t size;
     93 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     94 		aprint_error(": couldn't get register address\n");
     95 		return;
     96 	}
     97 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_icoll.sc_hdl)) {
     98 		aprint_error(": couldn't map registers\n");
     99 		return;
    100 	}
    101 
    102 	imx23icoll_init(&sc->sc_icoll, self, faa->faa_bst);
    103 
    104 	/*
    105 	 * Register fdt interrupt controller
    106 	 */
    107 	int error = fdtbus_register_interrupt_controller(self, phandle,
    108 							 &imx23icoll_fdt_funcs);
    109 	if (error) {
    110 		aprint_error(
    111 		    "imx23icoll_fdt: couldn't register with fdtbus: %d\n",
    112 		    error);
    113 		return;
    114 	}
    115 
    116 	aprint_naive("\n");
    117 	aprint_normal("\n");
    118 
    119 	struct apb_attach_args apbaa = {
    120 		.aa_name = "imx23icoll",
    121 		.aa_iot = faa->faa_bst,
    122 		.aa_dmat = faa->faa_dmat,
    123 		.aa_addr = addr,
    124 		.aa_size = size,
    125 		.aa_irq = -1
    126 	};
    127 
    128 	config_found(self, &apbaa, NULL, CFARGS_NONE);
    129 
    130 	arm_fdt_irq_set_handler((void (*)(void *))imx23_intr_dispatch);
    131 }
    132 
    133 static bool
    134 imx23icoll_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    135 {
    136 	const u_int irq = be32toh(*specifier);
    137 
    138 	snprintf(buf, buflen, "icoll irq %d", irq);
    139 
    140 	return true;
    141 }
    142 
    143 static void *
    144 imx23icoll_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    145 			 int (*func)(void *), void *arg, const char *xname)
    146 {
    147 	const u_int irq = be32toh(*specifier);
    148 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    149 
    150 	return intr_establish_xname(irq, ipl, IST_LEVEL | mpsafe, func, arg,
    151 				    xname);
    152 }
    153 
    154 static void
    155 imx23icoll_fdt_disestablish(device_t dev, void *ih)
    156 {
    157 	intr_disestablish(ih);
    158 }
    159