xref: /netbsd-src/sys/arch/macppc/dev/obio.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: obio.c,v 1.23 2005/12/11 12:18:03 christos 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.23 2005/12/11 12:18:03 christos 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 __P((struct device *, struct device *, void *));
50 static int obio_match __P((struct device *, struct cfdata *, void *));
51 static int obio_print __P((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(parent, cf, aux)
64 	struct device *parent;
65 	struct cfdata *cf;
66 	void *aux;
67 {
68 	struct pci_attach_args *pa = aux;
69 
70 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
71 		switch (PCI_PRODUCT(pa->pa_id)) {
72 		case PCI_PRODUCT_APPLE_GC:
73 		case PCI_PRODUCT_APPLE_OHARE:
74 		case PCI_PRODUCT_APPLE_HEATHROW:
75 		case PCI_PRODUCT_APPLE_PADDINGTON:
76 		case PCI_PRODUCT_APPLE_KEYLARGO:
77 		case PCI_PRODUCT_APPLE_PANGEA_MACIO:
78 		case PCI_PRODUCT_APPLE_INTREPID:
79 			return 1;
80 		}
81 
82 	return 0;
83 }
84 
85 /*
86  * Attach all the sub-devices we can find
87  */
88 void
89 obio_attach(parent, self, aux)
90 	struct device *parent, *self;
91 	void *aux;
92 {
93 	struct obio_softc *sc = (struct obio_softc *)self;
94 	struct pci_attach_args *pa = aux;
95 	struct confargs ca;
96 	int node, child, namelen;
97 	u_int reg[20];
98 	int intr[6], parent_intr = 0, parent_nintr = 0;
99 	char name[32];
100 	char compat[32];
101 
102 	switch (PCI_PRODUCT(pa->pa_id)) {
103 
104 	case PCI_PRODUCT_APPLE_GC:
105 	case PCI_PRODUCT_APPLE_OHARE:
106 	case PCI_PRODUCT_APPLE_HEATHROW:
107 	case PCI_PRODUCT_APPLE_PADDINGTON:
108 	case PCI_PRODUCT_APPLE_KEYLARGO:
109 	case PCI_PRODUCT_APPLE_PANGEA_MACIO:
110 	case PCI_PRODUCT_APPLE_INTREPID:
111 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
112 		if (node == -1)
113 			node = OF_finddevice("mac-io");
114 			if (node == -1)
115 				node = OF_finddevice("/pci/mac-io");
116 		break;
117 
118 	default:
119 		printf("obio_attach: unknown obio controller\n");
120 		return;
121 	}
122 
123 	sc->sc_node = node;
124 
125 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
126 		return;
127 	ca.ca_baseaddr = reg[2];
128 
129 	printf(": addr 0x%x\n", ca.ca_baseaddr);
130 
131 	/* Enable internal modem (KeyLargo) */
132 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
133 		printf("enabling KeyLargo internal modem\n");
134 		out32rb(ca.ca_baseaddr + 0x40,
135 			in32rb(ca.ca_baseaddr + 0x40) & ~((u_int32_t)1<<25));
136 	}
137 
138 	/* Enable internal modem (Pangea) */
139 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
140 		out8(ca.ca_baseaddr + 0x006a + 0x03, 0x04); /* set reset */
141 		out8(ca.ca_baseaddr + 0x006a + 0x02, 0x04); /* power modem on */
142 		out8(ca.ca_baseaddr + 0x006a + 0x03, 0x05); /* unset reset */
143 	}
144 
145 	/* Gatwick and Paddington use same product ID */
146 	namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
147 
148 	if (strcmp(compat, "gatwick") == 0) {
149 		parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
150 					sizeof(intr));
151 		parent_intr = intr[0];
152 	} else {
153   		/* Enable CD and microphone sound input. */
154 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
155 			out8(ca.ca_baseaddr + 0x37, 0x03);
156 	}
157 
158 	for (child = OF_child(node); child; child = OF_peer(child)) {
159 		namelen = OF_getprop(child, "name", name, sizeof(name));
160 		if (namelen < 0)
161 			continue;
162 		if (namelen >= sizeof(name))
163 			continue;
164 
165 		name[namelen] = 0;
166 		ca.ca_name = name;
167 		ca.ca_node = child;
168 
169 		ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
170 
171 		if (strcmp(compat, "gatwick") != 0) {
172 			ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
173 					sizeof(intr));
174 			if (ca.ca_nintr == -1)
175 				ca.ca_nintr = OF_getprop(child, "interrupts", intr,
176 						sizeof(intr));
177 		} else {
178 			intr[0] = parent_intr;
179 			ca.ca_nintr = parent_nintr;
180 		}
181 		ca.ca_reg = reg;
182 		ca.ca_intr = intr;
183 
184 		config_found(self, &ca, obio_print);
185 	}
186 }
187 
188 static const char *skiplist[] = {
189 	"interrupt-controller",
190 	"gpio",
191 	"escc-legacy",
192 	"timer",
193 	"i2c",
194 	"power-mgt"
195 };
196 
197 #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
198 
199 int
200 obio_print(aux, obio)
201 	void *aux;
202 	const char *obio;
203 {
204 	struct confargs *ca = aux;
205 	int i;
206 
207 	for (i = 0; i < N_LIST; i++)
208 		if (strcmp(ca->ca_name, skiplist[i]) == 0)
209 			return QUIET;
210 
211 	if (obio)
212 		aprint_normal("%s at %s", ca->ca_name, obio);
213 
214 	if (ca->ca_nreg > 0)
215 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
216 
217 	return UNCONF;
218 }
219