Home | History | Annotate | Line # | Download | only in fdt
fdt_intr.c revision 1.3.2.2
      1  1.3.2.2  skrll /* $NetBSD: fdt_intr.c,v 1.3.2.2 2015/12/27 12:09:49 skrll Exp $ */
      2  1.3.2.2  skrll 
      3  1.3.2.2  skrll /*-
      4  1.3.2.2  skrll  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.3.2.2  skrll  * All rights reserved.
      6  1.3.2.2  skrll  *
      7  1.3.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.3.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.3.2.2  skrll  * are met:
     10  1.3.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.3.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.3.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.3.2.2  skrll  *
     16  1.3.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.3.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.3.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.3.2.2  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.3.2.2  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.3.2.2  skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.3.2.2  skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.3.2.2  skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.3.2.2  skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.3.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.3.2.2  skrll  * SUCH DAMAGE.
     27  1.3.2.2  skrll  */
     28  1.3.2.2  skrll 
     29  1.3.2.2  skrll #include <sys/cdefs.h>
     30  1.3.2.2  skrll __KERNEL_RCSID(0, "$NetBSD: fdt_intr.c,v 1.3.2.2 2015/12/27 12:09:49 skrll Exp $");
     31  1.3.2.2  skrll 
     32  1.3.2.2  skrll #include <sys/param.h>
     33  1.3.2.2  skrll #include <sys/bus.h>
     34  1.3.2.2  skrll #include <sys/kmem.h>
     35  1.3.2.2  skrll 
     36  1.3.2.2  skrll #include <libfdt.h>
     37  1.3.2.2  skrll #include <dev/fdt/fdtvar.h>
     38  1.3.2.2  skrll 
     39  1.3.2.2  skrll struct fdtbus_interrupt_controller {
     40  1.3.2.2  skrll 	device_t ic_dev;
     41  1.3.2.2  skrll 	int ic_phandle;
     42  1.3.2.2  skrll 	const struct fdtbus_interrupt_controller_func *ic_funcs;
     43  1.3.2.2  skrll 
     44  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic_next;
     45  1.3.2.2  skrll };
     46  1.3.2.2  skrll 
     47  1.3.2.2  skrll static struct fdtbus_interrupt_controller *fdtbus_ic = NULL;
     48  1.3.2.2  skrll 
     49  1.3.2.2  skrll static int
     50  1.3.2.2  skrll fdtbus_get_interrupt_parent(int phandle)
     51  1.3.2.2  skrll {
     52  1.3.2.2  skrll 	u_int interrupt_parent;
     53  1.3.2.2  skrll 
     54  1.3.2.2  skrll 	while (phandle >= 0) {
     55  1.3.2.2  skrll 		if (of_getprop_uint32(phandle, "interrupt-parent",
     56  1.3.2.2  skrll 		    &interrupt_parent) == 0) {
     57  1.3.2.2  skrll 			break;
     58  1.3.2.2  skrll 		}
     59  1.3.2.2  skrll 		if (phandle == 0) {
     60  1.3.2.2  skrll 			return -1;
     61  1.3.2.2  skrll 		}
     62  1.3.2.2  skrll 		phandle = OF_parent(phandle);
     63  1.3.2.2  skrll 	}
     64  1.3.2.2  skrll 	if (phandle < 0) {
     65  1.3.2.2  skrll 		return -1;
     66  1.3.2.2  skrll 	}
     67  1.3.2.2  skrll 
     68  1.3.2.2  skrll 	const void *data = fdtbus_get_data();
     69  1.3.2.2  skrll 	const int off = fdt_node_offset_by_phandle(data, interrupt_parent);
     70  1.3.2.2  skrll 	if (off < 0) {
     71  1.3.2.2  skrll 		return -1;
     72  1.3.2.2  skrll 	}
     73  1.3.2.2  skrll 
     74  1.3.2.2  skrll 	return fdtbus_offset2phandle(off);
     75  1.3.2.2  skrll }
     76  1.3.2.2  skrll 
     77  1.3.2.2  skrll static struct fdtbus_interrupt_controller *
     78  1.3.2.2  skrll fdtbus_get_interrupt_controller(int phandle)
     79  1.3.2.2  skrll {
     80  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic;
     81  1.3.2.2  skrll 
     82  1.3.2.2  skrll 	const int ic_phandle = fdtbus_get_interrupt_parent(phandle);
     83  1.3.2.2  skrll 	if (ic_phandle < 0) {
     84  1.3.2.2  skrll 		return NULL;
     85  1.3.2.2  skrll 	}
     86  1.3.2.2  skrll 
     87  1.3.2.2  skrll 	for (ic = fdtbus_ic; ic; ic = ic->ic_next) {
     88  1.3.2.2  skrll 		if (ic->ic_phandle == ic_phandle) {
     89  1.3.2.2  skrll 			return ic;
     90  1.3.2.2  skrll 		}
     91  1.3.2.2  skrll 	}
     92  1.3.2.2  skrll 
     93  1.3.2.2  skrll 	return NULL;
     94  1.3.2.2  skrll }
     95  1.3.2.2  skrll 
     96  1.3.2.2  skrll int
     97  1.3.2.2  skrll fdtbus_register_interrupt_controller(device_t dev, int phandle,
     98  1.3.2.2  skrll     const struct fdtbus_interrupt_controller_func *funcs)
     99  1.3.2.2  skrll {
    100  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic;
    101  1.3.2.2  skrll 
    102  1.3.2.2  skrll 	ic = kmem_alloc(sizeof(*ic), KM_SLEEP);
    103  1.3.2.2  skrll 	ic->ic_dev = dev;
    104  1.3.2.2  skrll 	ic->ic_phandle = phandle;
    105  1.3.2.2  skrll 	ic->ic_funcs = funcs;
    106  1.3.2.2  skrll 
    107  1.3.2.2  skrll 	ic->ic_next = fdtbus_ic;
    108  1.3.2.2  skrll 	fdtbus_ic = ic;
    109  1.3.2.2  skrll 
    110  1.3.2.2  skrll 	return 0;
    111  1.3.2.2  skrll }
    112  1.3.2.2  skrll 
    113  1.3.2.2  skrll void *
    114  1.3.2.2  skrll fdtbus_intr_establish(int phandle, u_int index, int ipl, int flags,
    115  1.3.2.2  skrll     int (*func)(void *), void *arg)
    116  1.3.2.2  skrll {
    117  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic;
    118  1.3.2.2  skrll 
    119  1.3.2.2  skrll 	ic = fdtbus_get_interrupt_controller(phandle);
    120  1.3.2.2  skrll 	if (ic == NULL)
    121  1.3.2.2  skrll 		return NULL;
    122  1.3.2.2  skrll 
    123  1.3.2.2  skrll 	return ic->ic_funcs->establish(ic->ic_dev, phandle, index, ipl,
    124  1.3.2.2  skrll 	    flags, func, arg);
    125  1.3.2.2  skrll }
    126  1.3.2.2  skrll 
    127  1.3.2.2  skrll void
    128  1.3.2.2  skrll fdtbus_intr_disestablish(int phandle, void *ih)
    129  1.3.2.2  skrll {
    130  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic;
    131  1.3.2.2  skrll 
    132  1.3.2.2  skrll 	ic = fdtbus_get_interrupt_controller(phandle);
    133  1.3.2.2  skrll 	KASSERT(ic != NULL);
    134  1.3.2.2  skrll 
    135  1.3.2.2  skrll 	return ic->ic_funcs->disestablish(ic->ic_dev, ih);
    136  1.3.2.2  skrll }
    137  1.3.2.2  skrll 
    138  1.3.2.2  skrll bool
    139  1.3.2.2  skrll fdtbus_intr_str(int phandle, u_int index, char *buf, size_t buflen)
    140  1.3.2.2  skrll {
    141  1.3.2.2  skrll 	struct fdtbus_interrupt_controller *ic;
    142  1.3.2.2  skrll 
    143  1.3.2.2  skrll 	ic = fdtbus_get_interrupt_controller(phandle);
    144  1.3.2.2  skrll 	if (ic == NULL)
    145  1.3.2.2  skrll 		return false;
    146  1.3.2.2  skrll 
    147  1.3.2.2  skrll 	return ic->ic_funcs->intrstr(ic->ic_dev, phandle, index, buf, buflen);
    148  1.3.2.2  skrll }
    149