Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.26.14.2
      1 /*	$NetBSD: obio.c,v 1.26.14.2 2007/06/18 03:00:17 macallan Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Internet Research Institute, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by
     18  *	Internet Research Institute, Inc.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.26.14.2 2007/06/18 03:00:17 macallan Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 
     42 #include <dev/pci/pcivar.h>
     43 #include <dev/pci/pcidevs.h>
     44 
     45 #include <dev/ofw/openfirm.h>
     46 
     47 #include <machine/autoconf.h>
     48 
     49 static void obio_attach(struct device *, struct device *, void *);
     50 static int obio_match(struct device *, struct cfdata *, void *);
     51 static int obio_print(void *, const char *);
     52 
     53 struct obio_softc {
     54 	struct device sc_dev;
     55 	int sc_node;
     56 };
     57 
     58 
     59 CFATTACH_DECL(obio, sizeof(struct obio_softc),
     60     obio_match, obio_attach, NULL, NULL);
     61 
     62 int
     63 obio_match(struct device *parent, struct cfdata *cf, void *aux)
     64 {
     65 	struct pci_attach_args *pa = aux;
     66 
     67 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
     68 		switch (PCI_PRODUCT(pa->pa_id)) {
     69 		case PCI_PRODUCT_APPLE_GC:
     70 		case PCI_PRODUCT_APPLE_OHARE:
     71 		case PCI_PRODUCT_APPLE_HEATHROW:
     72 		case PCI_PRODUCT_APPLE_PADDINGTON:
     73 		case PCI_PRODUCT_APPLE_KEYLARGO:
     74 		case PCI_PRODUCT_APPLE_PANGEA_MACIO:
     75 		case PCI_PRODUCT_APPLE_INTREPID:
     76 		case PCI_PRODUCT_APPLE_K2:
     77 			return 1;
     78 		}
     79 
     80 	return 0;
     81 }
     82 
     83 /*
     84  * Attach all the sub-devices we can find
     85  */
     86 void
     87 obio_attach(struct device *parent, struct device *self, void *aux)
     88 {
     89 	struct obio_softc *sc = (struct obio_softc *)self;
     90 	struct pci_attach_args *pa = aux;
     91 	struct confargs ca;
     92 	bus_space_handle_t bsh;
     93 	int node, child, namelen, error;
     94 	u_int reg[20];
     95 	int intr[6], parent_intr = 0, parent_nintr = 0;
     96 	char name[32];
     97 	char compat[32];
     98 
     99 	switch (PCI_PRODUCT(pa->pa_id)) {
    100 
    101 	case PCI_PRODUCT_APPLE_GC:
    102 	case PCI_PRODUCT_APPLE_OHARE:
    103 	case PCI_PRODUCT_APPLE_HEATHROW:
    104 	case PCI_PRODUCT_APPLE_PADDINGTON:
    105 	case PCI_PRODUCT_APPLE_KEYLARGO:
    106 	case PCI_PRODUCT_APPLE_PANGEA_MACIO:
    107 	case PCI_PRODUCT_APPLE_INTREPID:
    108 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    109 		if (node == -1)
    110 			node = OF_finddevice("mac-io");
    111 			if (node == -1)
    112 				node = OF_finddevice("/pci/mac-io");
    113 		break;
    114 	case PCI_PRODUCT_APPLE_K2:
    115 		node = OF_finddevice("mac-io");
    116 		break;
    117 
    118 	default:
    119 		node = -1;
    120 		break;
    121 	}
    122 	if (node == -1)
    123 		panic("macio not found or unknown");
    124 
    125 	sc->sc_node = node;
    126 
    127 #if defined (PMAC_G5)
    128 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
    129 	{
    130 		return;
    131 	}
    132 #else
    133 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
    134 		return;
    135 #endif /* PMAC_G5 */
    136 
    137 	ca.ca_baseaddr = reg[2];
    138 	ca.ca_tag = pa->pa_memt;
    139 	error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, 0x80, 0, &bsh);
    140 	if (error)
    141 		panic(": failed to map mac-io %#x", ca.ca_baseaddr);
    142 
    143 	printf(": addr 0x%x\n", ca.ca_baseaddr);
    144 
    145 	/* Enable internal modem (KeyLargo) */
    146 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
    147 		aprint_normal("%s: enabling KeyLargo internal modem\n",
    148 		    self->dv_xname);
    149 		bus_space_write_4(ca.ca_tag, bsh, 0x40,
    150 		    bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
    151 	}
    152 
    153 	/* Enable internal modem (Pangea) */
    154 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
    155 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04); /* set reset */
    156 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04); /* power modem on */
    157 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05); /* unset reset */
    158 	}
    159 
    160 	/* Gatwick and Paddington use same product ID */
    161 	namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
    162 
    163 	if (strcmp(compat, "gatwick") == 0) {
    164 		parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
    165 					sizeof(intr));
    166 		parent_intr = intr[0];
    167 	} else {
    168   		/* Enable CD and microphone sound input. */
    169 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
    170 			bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
    171 	}
    172 
    173 	bus_space_unmap(ca.ca_tag, bsh, 0x80);
    174 
    175 	for (child = OF_child(node); child; child = OF_peer(child)) {
    176 		namelen = OF_getprop(child, "name", name, sizeof(name));
    177 		if (namelen < 0)
    178 			continue;
    179 		if (namelen >= sizeof(name))
    180 			continue;
    181 
    182 		name[namelen] = 0;
    183 		ca.ca_name = name;
    184 		ca.ca_node = child;
    185 		ca.ca_tag = pa->pa_memt;
    186 
    187 		ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
    188 
    189 		if (strcmp(compat, "gatwick") != 0) {
    190 			ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    191 					sizeof(intr));
    192 			if (ca.ca_nintr == -1)
    193 				ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    194 						sizeof(intr));
    195 		} else {
    196 			intr[0] = parent_intr;
    197 			ca.ca_nintr = parent_nintr;
    198 		}
    199 		ca.ca_reg = reg;
    200 		ca.ca_intr = intr;
    201 
    202 		config_found(self, &ca, obio_print);
    203 	}
    204 }
    205 
    206 static const char * const skiplist[] = {
    207 	"interrupt-controller",
    208 	"gpio",
    209 	"escc-legacy",
    210 	"timer",
    211 	"i2c",
    212 	"power-mgt",
    213 	"escc"
    214 
    215 };
    216 
    217 #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
    218 
    219 int
    220 obio_print(aux, obio)
    221 	void *aux;
    222 	const char *obio;
    223 {
    224 	struct confargs *ca = aux;
    225 	int i;
    226 
    227 	for (i = 0; i < N_LIST; i++)
    228 		if (strcmp(ca->ca_name, skiplist[i]) == 0)
    229 			return QUIET;
    230 
    231 	if (obio)
    232 		aprint_normal("%s at %s", ca->ca_name, obio);
    233 
    234 	if (ca->ca_nreg > 0)
    235 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    236 
    237 	return UNCONF;
    238 }
    239