Home | History | Annotate | Line # | Download | only in generic
      1 /*	$NetBSD: genintc.c,v 1.1 2026/07/19 01:48:21 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2025, 2026 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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: genintc.c,v 1.1 2026/07/19 01:48:21 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 #include <sys/intr.h>
     39 #include <sys/systm.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 #include <dev/fdt/syscon.h>
     43 
     44 static const struct device_compatible_entry compat_data[] = {
     45 	{ .compat = "motorola,68000-generic-interrupts" },
     46 	DEVICE_COMPAT_EOL
     47 };
     48 
     49 /*
     50  * Generic m68k interrupt "controller".  Supports vectored and auto-vectored
     51  * interrupts.  Can use a syscon to enable / disable interrupts globally.
     52  *
     53  * Our FDT bindings:
     54  *
     55  *	#interrupt-cells = 2
     56  *	0 - CPU interrupt priority level
     57  *	1 - interrupt vector (0 == auto-vector)
     58  */
     59 
     60 static void *
     61 genintc_intr_establish(device_t dev, u_int *specifier, int ipl,
     62     int flags, int (*func)(void *), void *arg, const char *xname)
     63 {
     64 	const u_int cpu_ipl = be32toh(specifier[0]);
     65 	const u_int vector = be32toh(specifier[1]);
     66 
     67 	if (cpu_ipl < 1 || cpu_ipl > 6) {
     68 		return NULL;
     69 	}
     70 
     71 	return m68k_intr_establish(func,
     72 				   arg,
     73 				   NULL,	/* evcnt */
     74 				   vector,
     75 				   cpu_ipl,
     76 				   0,		/* isrpri */
     77 				   0);		/* flags */
     78 }
     79 
     80 static void
     81 genintc_intr_disestablish(device_t dev, void *ih)
     82 {
     83 	(void)m68k_intr_disestablish(ih);
     84 }
     85 
     86 static bool
     87 genintc_intr_string(device_t dev, u_int *specifier, char *buf, size_t buflen)
     88 {
     89 	const u_int cpu_ipl = be32toh(specifier[0]);
     90 	const u_int vector = be32toh(specifier[1]);
     91 
     92 	if (cpu_ipl < 1 || cpu_ipl > 6) {
     93 		return false;
     94 	}
     95 
     96 	if (vector) {
     97 		snprintf(buf, buflen, "vector 0x%x ipl %u",
     98 		    vector, cpu_ipl);
     99 	} else {
    100 		snprintf(buf, buflen, "auto-vector ipl %u",
    101 		    cpu_ipl);
    102 	}
    103 
    104 	return true;
    105 }
    106 
    107 static const struct fdtbus_interrupt_controller_func genintc_fdt_funcs = {
    108 	.establish = genintc_intr_establish,
    109 	.disestablish = genintc_intr_disestablish,
    110 	.intrstr = genintc_intr_string,
    111 };
    112 
    113 static int
    114 genintc_match(device_t parent, cfdata_t cf, void *aux)
    115 {
    116 	struct fdt_attach_args * const faa = aux;
    117 
    118 	return of_compatible_match(faa->faa_phandle, compat_data);
    119 }
    120 
    121 static void
    122 genintc_attach(device_t parent, device_t self, void *aux)
    123 {
    124 	struct fdt_attach_args * const faa = aux;
    125 	const int phandle = faa->faa_phandle;
    126 	int error;
    127 
    128 	aprint_naive("\n");
    129 	aprint_normal("\n");
    130 
    131 	error = fdtbus_register_interrupt_controller(self, phandle,
    132 	    &genintc_fdt_funcs);
    133 	if (error) {
    134 		aprint_error_dev(self,
    135 		    "couldn't register with fdtbus (error=%d)\n", error);
    136 	}
    137 
    138 	/*
    139 	 * If the device tree specifies an interrupt-enable controller,
    140 	 * go ahead and enable them now.
    141 	 */
    142 	struct syscon *ena =
    143 	    fdtbus_syscon_acquire(phandle, "interrupt-enable-controller");
    144 	if (ena != NULL) {
    145 		uint32_t reg, mask, ena_val, val;
    146 
    147 		if (of_getprop_uint32(phandle,
    148 				      "interrupt-enable-reg-mask", &mask)) {
    149 			mask = 0xffffffff;
    150 		}
    151 		if (of_getprop_uint32(phandle,
    152 				      "interrupt-enable-reg",
    153 				      &reg) == 0 &&
    154 		    of_getprop_uint32(phandle,
    155 				      "interrupt-enable-value",
    156 				      &ena_val) == 0) {
    157 			aprint_verbose_dev(self,
    158 			    "enabling interrupts in system controller\n");
    159 			syscon_lock(ena);
    160 			val = syscon_read_4(ena, reg);
    161 			val = (val & mask) | ena_val;
    162 			syscon_write_4(ena, reg, val);
    163 			syscon_unlock(ena);
    164 		}
    165 	}
    166 }
    167 
    168 CFATTACH_DECL_NEW(genintc, 0,
    169     genintc_match, genintc_attach, NULL, NULL);
    170