Home | History | Annotate | Line # | Download | only in fdt
fdt_syscon.c revision 1.3.2.2
      1  1.3.2.2  pgoyette /* $NetBSD: fdt_syscon.c,v 1.3.2.2 2018/07/28 04:37:44 pgoyette Exp $ */
      2  1.3.2.2  pgoyette 
      3  1.3.2.2  pgoyette /*-
      4  1.3.2.2  pgoyette  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.3.2.2  pgoyette  * All rights reserved.
      6  1.3.2.2  pgoyette  *
      7  1.3.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.3.2.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.3.2.2  pgoyette  * are met:
     10  1.3.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.3.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.3.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.3.2.2  pgoyette  *
     16  1.3.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.3.2.2  pgoyette  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.3.2.2  pgoyette  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.3.2.2  pgoyette  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.3.2.2  pgoyette  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.3.2.2  pgoyette  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.3.2.2  pgoyette  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.3.2.2  pgoyette  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.3.2.2  pgoyette  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.3.2.2  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.3.2.2  pgoyette  * SUCH DAMAGE.
     27  1.3.2.2  pgoyette  */
     28  1.3.2.2  pgoyette 
     29  1.3.2.2  pgoyette #include <sys/cdefs.h>
     30  1.3.2.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: fdt_syscon.c,v 1.3.2.2 2018/07/28 04:37:44 pgoyette Exp $");
     31  1.3.2.2  pgoyette 
     32  1.3.2.2  pgoyette #include <sys/param.h>
     33  1.3.2.2  pgoyette #include <sys/bus.h>
     34  1.3.2.2  pgoyette #include <sys/kmem.h>
     35  1.3.2.2  pgoyette #include <sys/queue.h>
     36  1.3.2.2  pgoyette 
     37  1.3.2.2  pgoyette #include <libfdt.h>
     38  1.3.2.2  pgoyette #include <dev/fdt/fdtvar.h>
     39  1.3.2.2  pgoyette 
     40  1.3.2.2  pgoyette struct syscon;
     41  1.3.2.2  pgoyette 
     42  1.3.2.2  pgoyette struct fdtbus_syscon {
     43  1.3.2.2  pgoyette 	device_t sc_dev;
     44  1.3.2.2  pgoyette 	int sc_phandle;
     45  1.3.2.2  pgoyette 	struct syscon *sc_syscon;
     46  1.3.2.2  pgoyette 
     47  1.3.2.2  pgoyette 	LIST_ENTRY(fdtbus_syscon) sc_next;
     48  1.3.2.2  pgoyette };
     49  1.3.2.2  pgoyette 
     50  1.3.2.2  pgoyette static LIST_HEAD(, fdtbus_syscon) fdtbus_syscons =
     51  1.3.2.2  pgoyette     LIST_HEAD_INITIALIZER(fdtbus_syscons);
     52  1.3.2.2  pgoyette 
     53  1.3.2.2  pgoyette int
     54  1.3.2.2  pgoyette fdtbus_register_syscon(device_t dev, int phandle,
     55  1.3.2.2  pgoyette     struct syscon *syscon)
     56  1.3.2.2  pgoyette {
     57  1.3.2.2  pgoyette 	struct fdtbus_syscon *sc;
     58  1.3.2.2  pgoyette 
     59  1.3.2.2  pgoyette 	sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
     60  1.3.2.2  pgoyette 	sc->sc_dev = dev;
     61  1.3.2.2  pgoyette 	sc->sc_phandle = phandle;
     62  1.3.2.2  pgoyette 	sc->sc_syscon = syscon;
     63  1.3.2.2  pgoyette 
     64  1.3.2.2  pgoyette 	LIST_INSERT_HEAD(&fdtbus_syscons, sc, sc_next);
     65  1.3.2.2  pgoyette 
     66  1.3.2.2  pgoyette 	return 0;
     67  1.3.2.2  pgoyette }
     68  1.3.2.2  pgoyette 
     69  1.3.2.2  pgoyette static struct fdtbus_syscon *
     70  1.3.2.2  pgoyette fdtbus_get_syscon(int phandle)
     71  1.3.2.2  pgoyette {
     72  1.3.2.2  pgoyette 	struct fdtbus_syscon *sc;
     73  1.3.2.2  pgoyette 
     74  1.3.2.2  pgoyette 	LIST_FOREACH(sc, &fdtbus_syscons, sc_next) {
     75  1.3.2.2  pgoyette 		if (sc->sc_phandle == phandle)
     76  1.3.2.2  pgoyette 			return sc;
     77  1.3.2.2  pgoyette 	}
     78  1.3.2.2  pgoyette 
     79  1.3.2.2  pgoyette 	return NULL;
     80  1.3.2.2  pgoyette }
     81  1.3.2.2  pgoyette 
     82  1.3.2.2  pgoyette struct syscon *
     83  1.3.2.2  pgoyette fdtbus_syscon_acquire(int phandle, const char *prop)
     84  1.3.2.2  pgoyette {
     85  1.3.2.2  pgoyette 	struct fdtbus_syscon *sc;
     86  1.3.2.2  pgoyette 	int sc_phandle;
     87  1.3.2.2  pgoyette 
     88  1.3.2.2  pgoyette 	sc_phandle = fdtbus_get_phandle(phandle, prop);
     89  1.3.2.2  pgoyette 	if (sc_phandle < 0)
     90  1.3.2.2  pgoyette 		return NULL;
     91  1.3.2.2  pgoyette 
     92  1.3.2.2  pgoyette 	sc = fdtbus_get_syscon(sc_phandle);
     93  1.3.2.2  pgoyette 	if (sc == NULL)
     94  1.3.2.2  pgoyette 		return NULL;
     95  1.3.2.2  pgoyette 
     96  1.3.2.2  pgoyette 	return sc->sc_syscon;
     97  1.3.2.2  pgoyette }
     98  1.3.2.2  pgoyette 
     99  1.3.2.2  pgoyette struct syscon *
    100  1.3.2.2  pgoyette fdtbus_syscon_lookup(int phandle)
    101  1.3.2.2  pgoyette {
    102  1.3.2.2  pgoyette 	struct fdtbus_syscon *sc;
    103  1.3.2.2  pgoyette 
    104  1.3.2.2  pgoyette 	sc = fdtbus_get_syscon(phandle);
    105  1.3.2.2  pgoyette 	if (sc == NULL)
    106  1.3.2.2  pgoyette 		return NULL;
    107  1.3.2.2  pgoyette 
    108  1.3.2.2  pgoyette 	return sc->sc_syscon;
    109  1.3.2.2  pgoyette }
    110