Home | History | Annotate | Line # | Download | only in dev
uni-n.c revision 1.8
      1 /*	$NetBSD: uni-n.c,v 1.8 2018/03/01 13:55:25 macallan Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2005 Michael Lorenz.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * a driver to match the uni_n node in OF's device tree and attach its children
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: uni-n.c,v 1.8 2018/03/01 13:55:25 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 
     41 #include <dev/ofw/openfirm.h>
     42 #include <dev/ofw/ofw_pci.h>
     43 
     44 #include <machine/autoconf.h>
     45 
     46 static void uni_n_attach(device_t, device_t, void *);
     47 static int uni_n_match(device_t, cfdata_t, void *);
     48 static int uni_n_print(void *, const char *);
     49 
     50 struct uni_n_softc {
     51 	device_t sc_dev;
     52 	struct powerpc_bus_space sc_memt;
     53 	int sc_node;
     54 };
     55 
     56 CFATTACH_DECL_NEW(uni_n, sizeof(struct uni_n_softc),
     57     uni_n_match, uni_n_attach, NULL, NULL);
     58 
     59 int
     60 uni_n_match(device_t parent, cfdata_t cf, void *aux)
     61 {
     62 	struct confargs *ca = aux;
     63 	char compat[32];
     64 	if ((strcmp(ca->ca_name, "uni-n") != 0) &&
     65 	    (strcmp(ca->ca_name, "u4") != 0) &&
     66 	    (strcmp(ca->ca_name, "u3") != 0))
     67 		return 0;
     68 
     69 	memset(compat, 0, sizeof(compat));
     70 #if 0
     71 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     72 	if (strcmp(compat, "uni-north") != 0)
     73 		return 0;
     74 #endif
     75 	return 1;
     76 }
     77 
     78 /*
     79  * Attach all the sub-devices we can find
     80  */
     81 void
     82 uni_n_attach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct uni_n_softc *sc = device_private(self);
     85 	struct confargs *our_ca = aux;
     86 	struct confargs ca;
     87 	int node, child, namelen;
     88 	u_int reg[20];
     89 	int intr[6];
     90 	char name[32];
     91 
     92 	sc->sc_dev = self;
     93 	node = our_ca->ca_node;
     94 	sc->sc_node = node;
     95 	printf(" address 0x%08x\n",our_ca->ca_reg[0]);
     96 
     97 	memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
     98 	sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
     99 	if (ofwoea_map_space(RANGE_TYPE_MACIO, RANGE_MEM, node, &sc->sc_memt,
    100 	    "uni-n mem-space") != 0) {
    101 		panic("Can't init uni-n mem tag");
    102 	}
    103 
    104 	for (child = OF_child(node); child; child = OF_peer(child)) {
    105 		namelen = OF_getprop(child, "name", name, sizeof(name));
    106 		if (namelen < 0)
    107 			continue;
    108 		if (namelen >= sizeof(name))
    109 			continue;
    110 
    111 		name[namelen] = 0;
    112 		ca.ca_name = name;
    113 		ca.ca_node = child;
    114 		ca.ca_tag = &sc->sc_memt;
    115 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    116 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    117 				sizeof(intr));
    118 		if (ca.ca_nintr == -1)
    119 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    120 					sizeof(intr));
    121 
    122 		ca.ca_reg = reg;
    123 		ca.ca_intr = intr;
    124 		config_found(self, &ca, uni_n_print);
    125 	}
    126 }
    127 
    128 int
    129 uni_n_print(void *aux, const char *uni_n)
    130 {
    131 	struct confargs *ca = aux;
    132 	if (uni_n)
    133 		aprint_normal("%s at %s", ca->ca_name, uni_n);
    134 
    135 	if (ca->ca_nreg > 0)
    136 		aprint_normal(" address 0x%x", ca->ca_reg[0]);
    137 
    138 	return UNCONF;
    139 }
    140