Home | History | Annotate | Line # | Download | only in dev
uni-n.c revision 1.3
      1 /*	$NetBSD: uni-n.c,v 1.3 2009/03/14 15:36:09 dsl 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.3 2009/03/14 15:36:09 dsl 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 
     43 #include <machine/autoconf.h>
     44 
     45 static void uni_n_attach(struct device *, struct device *, void *);
     46 static int uni_n_match(struct device *, struct cfdata *, void *);
     47 static int uni_n_print(void *, const char *);
     48 
     49 struct uni_n_softc {
     50 	struct device sc_dev;
     51 	int sc_node;
     52 };
     53 
     54 CFATTACH_DECL(uni_n, sizeof(struct uni_n_softc),
     55     uni_n_match, uni_n_attach, NULL, NULL);
     56 
     57 int
     58 uni_n_match(struct device *parent, struct cfdata *cf, void *aux)
     59 {
     60 	struct confargs *ca = aux;
     61 	char compat[32];
     62 	if (strcmp(ca->ca_name, "uni-n") != 0)
     63 		return 0;
     64 
     65 	memset(compat, 0, sizeof(compat));
     66 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
     67 	if (strcmp(compat, "uni-north") != 0)
     68 		return 0;
     69 
     70 	return 1;
     71 }
     72 
     73 /*
     74  * Attach all the sub-devices we can find
     75  */
     76 void
     77 uni_n_attach(parent, self, aux)
     78 	struct device *parent, *self;
     79 	void *aux;
     80 {
     81 	struct uni_n_softc *sc = (struct uni_n_softc *)self;
     82 	struct confargs *our_ca = aux;
     83 	struct confargs ca;
     84 	int node, child, namelen;
     85 	u_int reg[20];
     86 	int intr[6];
     87 	char name[32];
     88 
     89 	node = OF_finddevice("/uni-n");
     90 	sc->sc_node = node;
     91 	printf(" address 0x%08x\n",our_ca->ca_reg[0]);
     92 
     93 	for (child = OF_child(node); child; child = OF_peer(child)) {
     94 		namelen = OF_getprop(child, "name", name, sizeof(name));
     95 		if (namelen < 0)
     96 			continue;
     97 		if (namelen >= sizeof(name))
     98 			continue;
     99 
    100 		name[namelen] = 0;
    101 		ca.ca_name = name;
    102 		ca.ca_node = child;
    103 
    104 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    105 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    106 				sizeof(intr));
    107 		if (ca.ca_nintr == -1)
    108 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    109 					sizeof(intr));
    110 
    111 		ca.ca_reg = reg;
    112 		ca.ca_intr = intr;
    113 
    114 		config_found(self, &ca, uni_n_print);
    115 	}
    116 }
    117 
    118 int
    119 uni_n_print(void *aux, const char *uni_n)
    120 {
    121 	struct confargs *ca = aux;
    122 	if (uni_n)
    123 		aprint_normal("%s at %s", ca->ca_name, uni_n);
    124 
    125 	if (ca->ca_nreg > 0)
    126 		aprint_normal(" address 0x%x", ca->ca_reg[0]);
    127 
    128 	return UNCONF;
    129 }
    130