xref: /openbsd-src/sys/arch/macppc/pci/macobio.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: macobio.c,v 1.21 2015/04/02 11:12:24 mpi Exp $	*/
2 /*	$NetBSD: obio.c,v 1.6 1999/05/01 10:36:08 tsubai Exp $	*/
3 
4 /*-
5  * Copyright (C) 1998	Internet Research Institute, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by
19  *	Internet Research Institute, Inc.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcidevs.h>
43 
44 #include <dev/ofw/openfirm.h>
45 
46 #include <machine/bus.h>
47 #include <machine/autoconf.h>
48 #include <macppc/pci/macobio.h>
49 
50 void macobio_attach(struct device *, struct device *, void *);
51 int macobio_match(struct device *, void *, void *);
52 int macobio_print(void *, const char *);
53 void macobio_modem_power(int enable);
54 
55 struct macobio_softc {
56 	struct device sc_dev;
57 	int sc_node;
58 	struct ppc_bus_space sc_membus_space;
59 	int	sc_id; /* copy of the PCI pa_id */
60 	u_int8_t *obiomem;
61 };
62 struct cfdriver macobio_cd = {
63 	NULL, "macobio", DV_DULL,
64 };
65 
66 
67 struct cfattach macobio_ca = {
68 	sizeof(struct macobio_softc), macobio_match, macobio_attach
69 };
70 
71 int
72 macobio_match(struct device *parent, void *cf, void *aux)
73 {
74 	struct pci_attach_args *pa = aux;
75 
76 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
77 		switch (PCI_PRODUCT(pa->pa_id)) {
78 
79 		case PCI_PRODUCT_APPLE_GC:
80 		case PCI_PRODUCT_APPLE_OHARE:
81 		case PCI_PRODUCT_APPLE_HEATHROW:
82 		case PCI_PRODUCT_APPLE_PADDINGTON:
83 		case PCI_PRODUCT_APPLE_KEYLARGO:
84 		case PCI_PRODUCT_APPLE_INTREPID:
85 		case PCI_PRODUCT_APPLE_PANGEA_MACIO:
86 		case PCI_PRODUCT_APPLE_SHASTA:
87 		case PCI_PRODUCT_APPLE_K2_MACIO:
88 			return 1;
89 		}
90 
91 	return 0;
92 }
93 
94 #define HEATHROW_FCR_OFFSET 0x38
95 u_int32_t *heathrow_FCR = NULL;
96 
97 /*
98  * Attach all the sub-devices we can find
99  */
100 void
101 macobio_attach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct macobio_softc *sc = (struct macobio_softc *)self;
104 	struct pci_attach_args *pa = aux;
105 	struct confargs ca;
106 	int node, child, namelen;
107 	u_int32_t reg[20];
108 	int32_t intr[8];
109 	char name[32];
110 	int need_interrupt_controller = 0;
111 
112 	sc->sc_id = pa->pa_id; /* save of type for later */
113 
114 	switch (PCI_PRODUCT(pa->pa_id)) {
115 
116 	/* XXX should not use name */
117 	case PCI_PRODUCT_APPLE_GC:
118 		node = OF_finddevice("/bandit/gc");
119 		need_interrupt_controller = 1;
120 		break;
121 
122 	case PCI_PRODUCT_APPLE_OHARE:
123 		node = OF_finddevice("/bandit/ohare");
124 		need_interrupt_controller = 1;
125 		break;
126 
127 	case PCI_PRODUCT_APPLE_HEATHROW:
128 	case PCI_PRODUCT_APPLE_PADDINGTON:
129 		node = OF_finddevice("mac-io");
130 		if (node == -1)
131 			node = OF_finddevice("/pci/mac-io");
132 		if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg))
133 			== (sizeof (reg[0]) * 5))
134 		{
135 			/* always ??? */
136 			heathrow_FCR = mapiodev(reg[2] + HEATHROW_FCR_OFFSET,
137 			    4);
138 		}
139 		break;
140 	case PCI_PRODUCT_APPLE_KEYLARGO:
141 	case PCI_PRODUCT_APPLE_INTREPID:
142 	case PCI_PRODUCT_APPLE_PANGEA_MACIO:
143 	case PCI_PRODUCT_APPLE_SHASTA:
144 	case PCI_PRODUCT_APPLE_K2_MACIO:
145 		node = OF_finddevice("mac-io");
146 		if (node == -1)
147 			node = OF_finddevice("/pci/mac-io");
148 		if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg))
149 		    == (sizeof (reg[0]) * 5))
150 			 sc->obiomem = mapiodev(reg[2], 0x100);
151 		break;
152 	default:
153 		printf(": unknown macobio controller\n");
154 		return;
155 	}
156 	sc->sc_node = node;
157 
158 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
159 		return;
160 
161 	ca.ca_baseaddr = reg[2];
162 
163 	sc->sc_membus_space.bus_base = ca.ca_baseaddr;
164 
165 	ca.ca_iot = &sc->sc_membus_space;
166 	ca.ca_dmat = pa->pa_dmat;
167 
168 	printf("\n");
169 
170 	/*
171 	 * This might be a hack, but it makes the interrupt controller
172 	 * attach as expected if a device node existed in the OF tree.
173 	 */
174 	if (need_interrupt_controller) {
175 		/* force attachment of legacy interrupt controllers */
176 		ca.ca_name = "legacy-interrupt-controller";
177 		ca.ca_node = 0;
178 
179 		ca.ca_nreg  = 0;
180 		ca.ca_nintr = 0;
181 
182 		ca.ca_reg = NULL;
183 		ca.ca_intr = NULL;
184 
185 		config_found(self, &ca, macobio_print);
186 	}
187 
188 	for (child = OF_child(node); child; child = OF_peer(child)) {
189 		namelen = OF_getprop(child, "name", name, sizeof(name));
190 		if (namelen < 0)
191 			continue;
192 		if (namelen >= sizeof(name))
193 			continue;
194 
195 		name[namelen] = 0;
196 		ca.ca_name = name;
197 		ca.ca_node = child;
198 
199 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
200 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
201 				sizeof(intr));
202 		if (ca.ca_nintr == -1)
203 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
204 					sizeof(intr));
205 
206 		ca.ca_reg = reg;
207 		ca.ca_intr = intr;
208 
209 		config_found(self, &ca, macobio_print);
210 	}
211 }
212 
213 int
214 macobio_print(void *aux, const char *macobio)
215 {
216 	struct confargs *ca = aux;
217 
218 	if (macobio)
219 		printf("\"%s\" at %s", ca->ca_name, macobio);
220 
221 	if (ca->ca_nreg > 0)
222 		printf(" offset 0x%x", ca->ca_reg[0]);
223 
224 	return UNCONF;
225 }
226 
227 void *
228 mac_intr_establish(void * lcv, int irq, int type, int level,
229     int (*ih_fun)(void *), void *ih_arg, const char *name)
230 {
231 	return (*intr_establish_func)(lcv, irq, type, level, ih_fun,
232 	    ih_arg, name);
233 }
234 void
235 mac_intr_disestablish(void *lcp, void *arg)
236 {
237 	(*intr_disestablish_func)(lcp, arg);
238 }
239 
240 void
241 macobio_enable(int offset, u_int32_t bits)
242 {
243 	struct macobio_softc *sc = macobio_cd.cd_devs[0];
244 	if (sc->obiomem == 0)
245 		return;
246 
247 	bits |=  in32rb(sc->obiomem + offset);
248 	out32rb(sc->obiomem + offset, bits);
249 }
250 void
251 macobio_disable(int offset, u_int32_t bits)
252 {
253 	struct macobio_softc *sc = macobio_cd.cd_devs[0];
254 	if (sc->obiomem == 0)
255 		return;
256 
257 	bits =  in32rb(sc->obiomem + offset) & ~bits;
258 	out32rb(sc->obiomem + offset, bits);
259 }
260 
261 uint8_t
262 macobio_read(int offset)
263 {
264 	struct macobio_softc *sc = macobio_cd.cd_devs[0];
265 	if (sc->obiomem == 0)
266 		return -1;
267 
268 	return in8rb(sc->obiomem + offset);
269 }
270 
271 void
272 macobio_write(int offset, uint8_t bits)
273 {
274 	struct macobio_softc *sc = macobio_cd.cd_devs[0];
275 	if (sc->obiomem == 0)
276 		return;
277 
278 	out8rb(sc->obiomem + offset, bits);
279 }
280 
281 void
282 macobio_modem_power(int enable)
283 {
284 	u_int32_t val;
285 	struct macobio_softc *sc = macobio_cd.cd_devs[0];
286 	if (PCI_PRODUCT(sc->sc_id) == PCI_PRODUCT_APPLE_KEYLARGO ||
287 	    PCI_PRODUCT(sc->sc_id) == PCI_PRODUCT_APPLE_INTREPID) {
288 		val = in32rb(sc->obiomem + 0x40);
289 		if (enable)
290 			val = val & ~((u_int32_t)1<<25);
291 		else
292 			val = val | ((u_int32_t)1<<25);
293 		out32rb(sc->obiomem + 0x40, val);
294 	}
295 	if (PCI_PRODUCT(sc->sc_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
296 		if (enable) {
297 			/* set reset */
298 			out8(sc->obiomem + 0x006a + 0x03, 0x04);
299 			/* power modem on */
300 			out8(sc->obiomem + 0x006a + 0x02, 0x04);
301 			/* unset reset */
302 			out8(sc->obiomem + 0x006a + 0x03, 0x05);
303 		}  else {
304 			/* disable it how? */
305 		}
306 	}
307 }
308