xref: /netbsd-src/sys/arch/powerpc/pci/pcib.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: pcib.c,v 1.2 2007/10/17 19:56:44 garbled Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: pcib.c,v 1.2 2007/10/17 19:56:44 garbled Exp $");
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 
47 #include <machine/bus.h>
48 
49 #include <dev/isa/isavar.h>
50 
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 
54 #include <dev/pci/pcidevs.h>
55 
56 #include "isa.h"
57 #include "isadma.h"
58 
59 int	pcibmatch(struct device *, struct cfdata *, void *);
60 void	pcibattach(struct device *, struct device *, void *);
61 
62 struct pcib_softc {
63 	struct device sc_dev;
64 	struct powerpc_isa_chipset *sc_chipset;
65 };
66 
67 extern struct powerpc_isa_chipset genppc_ict;
68 extern struct genppc_pci_chipset *genppc_pct;
69 
70 CFATTACH_DECL(pcib, sizeof(struct pcib_softc),
71     pcibmatch, pcibattach, NULL, NULL);
72 
73 void	pcib_callback(struct device *);
74 
75 int
76 pcibmatch(struct device *parent, struct cfdata *cf, void *aux)
77 {
78 	struct pci_attach_args *pa = aux;
79 
80 	/*
81 	 * Match PCI-ISA bridge.
82 	 */
83 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
84 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
85 		return (1);
86 	}
87 
88 	/*
89 	 * some special cases:
90 	 */
91 	switch (PCI_VENDOR(pa->pa_id)) {
92 	case PCI_VENDOR_INTEL:
93 		switch (PCI_PRODUCT(pa->pa_id)) {
94 		case PCI_PRODUCT_INTEL_SIO:
95 			/*
96 			 * The Intel SIO identifies itself as a
97 			 * miscellaneous prehistoric.
98 			 */
99 			return (1);
100 		}
101 		break;
102 	}
103 
104 	return (0);
105 }
106 
107 void
108 pcibattach(struct device *parent, struct device *self, void *aux)
109 {
110 	struct pci_attach_args *pa = aux;
111 	char devinfo[256];
112 	u_int32_t v;
113 	int lvlmask = 0;
114 #ifdef prep
115 	prop_bool_t rav;
116 #endif
117 
118 	/*
119 	 * Just print out a description and defer configuration
120 	 * until all PCI devices have been attached.
121 	 */
122 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
123 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
124 	    PCI_REVISION(pa->pa_class));
125 
126 	v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40);
127 	if ((v & 0x20) == 0) {
128 		aprint_verbose("%s: PIRQ[0-3] not used\n", self->dv_xname);
129 	} else {
130 		v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x60);
131 		if ((v & 0x80808080) == 0x80808080) {
132 			aprint_verbose("%s: PIRQ[0-3] disabled\n",
133 			    self->dv_xname);
134 		} else {
135 			int i;
136 			aprint_verbose("%s:", self->dv_xname);
137 			for (i = 0; i < 4; i++, v >>= 8) {
138 				if ((v & 0x80) == 0 && (v & 0x0f) != 0) {
139 					aprint_verbose(" PIRQ[%d]=%d", i,
140 					    v & 0x0f);
141 					lvlmask |= (1 << (v & 0x0f));
142 				}
143 			}
144 			aprint_verbose("\n");
145 		}
146 	}
147 
148 	/*
149 	 * If we have an 83C553F-G sitting on a RAVEN host bridge,
150 	 * then we need to rewire some interrupts.
151 	 * The IDE Interrupt Routing Control Register lives at 0x43,
152 	 * and defaults to 0xEF, which means the primary controller
153 	 * interrupts on ivr-14, and the secondary on ivr-15. We
154 	 * reset it to 0xEE to fire them both at ivr-14.
155 	 * We have to rewrite the interrupt map, because the bridge map told
156 	 * us that the interrupt is MPIC 0, which is the bridge intr for
157 	 * the 8259.
158 	 * Additionally, sometimes the PCI Interrupt Routing Control Register
159 	 * is improperly initialized, causing all sorts of wierd interrupt
160 	 * issues on the machine.  The manual says it should default to
161 	 * 0000h (index 45-44h) however it would appear that PPCBUG is
162 	 * setting it up differently.  Reset it to 0000h.
163 	 */
164 
165 #ifdef prep
166 	rav = prop_dictionary_get(device_properties(parent),
167 	    "prep-raven-pchb");
168 
169 	if (rav != NULL && prop_bool_true(rav) &&
170 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SYMPHONY &&
171 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYMPHONY_83C553) {
172 
173 		prop_dictionary_t dict, devsub;
174 		prop_number_t pinsub;
175 		struct genppc_pci_chipset_businfo *pbi;
176 
177 		v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40) & 0x00ffffff;
178 		v |= 0xee000000;
179 		pci_conf_write(pa->pa_pc, pa->pa_tag, 0x40, v);
180 
181 		v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x44) & 0xffff0000;
182 		pci_conf_write(pa->pa_pc, pa->pa_tag, 0x44, v);
183 
184 		pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
185 		dict = prop_dictionary_get(pbi->pbi_properties,
186 		    "prep-pci-intrmap");
187 		devsub = prop_dictionary_get(dict, "devfunc-11");
188 		pinsub = prop_number_create_integer(14);
189 		prop_dictionary_set(devsub, "pin-A", pinsub);
190 		prop_object_release(pinsub);
191 		aprint_verbose("%s: setting pciide irq to 14\n",
192 		    self->dv_xname);
193 		/* irq 14 is level */
194 		lvlmask = 0x0040;
195 	}
196 #endif /* prep */
197 
198 	config_defer(self, pcib_callback);
199 }
200 
201 void
202 pcib_callback(struct device *self)
203 {
204 	struct pcib_softc *sc = (struct pcib_softc *)self;
205 #if NISA > 0
206 	struct isabus_attach_args iba;
207 
208 	/*
209 	 * Attach the ISA bus behind this bridge.
210 	 */
211 	memset(&iba, 0, sizeof(iba));
212 	sc->sc_chipset = &genppc_ict;
213 	iba.iba_ic = sc->sc_chipset;
214 	iba.iba_iot = &genppc_isa_io_space_tag;
215 	iba.iba_memt = &genppc_isa_mem_space_tag;
216 #if NISADMA > 0
217 	iba.iba_dmat = &isa_bus_dma_tag;
218 #endif
219 	config_found_ia(&sc->sc_dev, "isabus", &iba, isabusprint);
220 #endif
221 }
222