pci_machdep.c revision 1.1.4.4 1 1.1.4.4 skrll /* $NetBSD: pci_machdep.c,v 1.1.4.4 2004/09/21 13:20:41 skrll Exp $ */
2 1.1.4.2 skrll
3 1.1.4.2 skrll /*
4 1.1.4.2 skrll * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1.4.2 skrll * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 1.1.4.2 skrll *
7 1.1.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 skrll * modification, are permitted provided that the following conditions
9 1.1.4.2 skrll * are met:
10 1.1.4.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 skrll * documentation and/or other materials provided with the distribution.
15 1.1.4.2 skrll * 3. All advertising materials mentioning features or use of this software
16 1.1.4.2 skrll * must display the following acknowledgement:
17 1.1.4.2 skrll * This product includes software developed by Charles M. Hannum.
18 1.1.4.2 skrll * 4. The name of the author may not be used to endorse or promote products
19 1.1.4.2 skrll * derived from this software without specific prior written permission.
20 1.1.4.2 skrll *
21 1.1.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1.4.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1.4.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1.4.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1.4.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1.4.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1.4.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1.4.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1.4.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1.4.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1.4.2 skrll */
32 1.1.4.2 skrll
33 1.1.4.2 skrll /*
34 1.1.4.2 skrll * Machine-specific functions for PCI autoconfiguration.
35 1.1.4.2 skrll *
36 1.1.4.2 skrll * On PCs, there are two methods of generating PCI configuration cycles.
37 1.1.4.2 skrll * We try to detect the appropriate mechanism for this machine and set
38 1.1.4.2 skrll * up a few function pointers to access the correct method directly.
39 1.1.4.2 skrll *
40 1.1.4.2 skrll * The configuration method can be hard-coded in the config file by
41 1.1.4.2 skrll * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42 1.1.4.2 skrll * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43 1.1.4.2 skrll */
44 1.1.4.2 skrll
45 1.1.4.2 skrll #include <sys/cdefs.h>
46 1.1.4.4 skrll __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.1.4.4 2004/09/21 13:20:41 skrll Exp $");
47 1.1.4.2 skrll
48 1.1.4.2 skrll #include <sys/types.h>
49 1.1.4.2 skrll #include <sys/param.h>
50 1.1.4.2 skrll #include <sys/time.h>
51 1.1.4.2 skrll #include <sys/systm.h>
52 1.1.4.2 skrll #include <sys/errno.h>
53 1.1.4.2 skrll #include <sys/device.h>
54 1.1.4.2 skrll #include <sys/extent.h>
55 1.1.4.2 skrll
56 1.1.4.2 skrll #include <uvm/uvm_extern.h>
57 1.1.4.2 skrll
58 1.1.4.2 skrll #include <machine/bus.h>
59 1.1.4.2 skrll #include <machine/intr.h>
60 1.1.4.2 skrll
61 1.1.4.2 skrll #include <dev/pci/pcivar.h>
62 1.1.4.2 skrll #include <dev/pci/pcireg.h>
63 1.1.4.2 skrll #include <dev/pci/pcidevs.h>
64 1.1.4.2 skrll #include <dev/pci/pciconf.h>
65 1.1.4.2 skrll
66 1.1.4.2 skrll #include <powerpc/ibm4xx/ibm405gp.h>
67 1.1.4.2 skrll #include <powerpc/ibm4xx/dev/pcicreg.h>
68 1.1.4.2 skrll
69 1.1.4.2 skrll static struct powerpc_bus_space pci_iot = {
70 1.1.4.2 skrll _BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
71 1.1.4.2 skrll 0x00000000,
72 1.1.4.2 skrll IBM405GP_PCIC0_BASE, /* extent base */
73 1.1.4.2 skrll IBM405GP_PCIC0_BASE + 8, /* extent limit */
74 1.1.4.2 skrll };
75 1.1.4.2 skrll
76 1.1.4.2 skrll static bus_space_handle_t pci_ioh;
77 1.1.4.2 skrll
78 1.1.4.2 skrll void
79 1.1.4.2 skrll pci_machdep_init(void)
80 1.1.4.2 skrll {
81 1.1.4.2 skrll
82 1.1.4.2 skrll if (pci_ioh == 0 &&
83 1.1.4.2 skrll (bus_space_init(&pci_iot, "pcicfg", NULL, 0) ||
84 1.1.4.2 skrll bus_space_map(&pci_iot, IBM405GP_PCIC0_BASE, 8, 0, &pci_ioh)))
85 1.1.4.2 skrll panic("Cannot map PCI registers");
86 1.1.4.2 skrll }
87 1.1.4.2 skrll
88 1.1.4.2 skrll void
89 1.1.4.2 skrll pci_attach_hook(struct device *parent, struct device *self,
90 1.1.4.2 skrll struct pcibus_attach_args *pba)
91 1.1.4.2 skrll {
92 1.1.4.2 skrll
93 1.1.4.2 skrll #ifdef PCI_CONFIGURE_VERBOSE
94 1.1.4.2 skrll printf("pci_attach_hook\n");
95 1.1.4.2 skrll ibm4xx_show_pci_map();
96 1.1.4.2 skrll #endif
97 1.1.4.2 skrll ibm4xx_setup_pci();
98 1.1.4.2 skrll #ifdef PCI_CONFIGURE_VERBOSE
99 1.1.4.2 skrll ibm4xx_show_pci_map();
100 1.1.4.2 skrll #endif
101 1.1.4.2 skrll }
102 1.1.4.2 skrll
103 1.1.4.2 skrll int
104 1.1.4.2 skrll pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
105 1.1.4.2 skrll {
106 1.1.4.2 skrll
107 1.1.4.2 skrll /*
108 1.1.4.2 skrll * Bus number is irrelevant. Configuration Mechanism 1 is in
109 1.1.4.2 skrll * use, can have devices 0-32 (i.e. the `normal' range).
110 1.1.4.2 skrll */
111 1.1.4.2 skrll return 5;
112 1.1.4.2 skrll }
113 1.1.4.2 skrll
114 1.1.4.2 skrll pcitag_t
115 1.1.4.2 skrll pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
116 1.1.4.2 skrll {
117 1.1.4.2 skrll pcitag_t tag;
118 1.1.4.2 skrll
119 1.1.4.2 skrll if (bus >= 256 || device >= 32 || function >= 8)
120 1.1.4.2 skrll panic("pci_make_tag: bad request");
121 1.1.4.2 skrll
122 1.1.4.2 skrll /* XXX magic number */
123 1.1.4.2 skrll tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
124 1.1.4.2 skrll
125 1.1.4.2 skrll return tag;
126 1.1.4.2 skrll }
127 1.1.4.2 skrll
128 1.1.4.2 skrll void
129 1.1.4.2 skrll pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
130 1.1.4.2 skrll {
131 1.1.4.2 skrll
132 1.1.4.2 skrll if (bp != NULL)
133 1.1.4.2 skrll *bp = (tag >> 16) & 0xff;
134 1.1.4.2 skrll if (dp != NULL)
135 1.1.4.2 skrll *dp = (tag >> 11) & 0x1f;
136 1.1.4.2 skrll if (fp != NULL)
137 1.1.4.2 skrll *fp = (tag >> 8) & 0x07;
138 1.1.4.2 skrll }
139 1.1.4.2 skrll
140 1.1.4.2 skrll pcireg_t
141 1.1.4.2 skrll pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
142 1.1.4.2 skrll {
143 1.1.4.2 skrll pcireg_t data;
144 1.1.4.2 skrll
145 1.1.4.2 skrll /* 405GT BIOS disables interrupts here. Should we? --Art */
146 1.1.4.2 skrll bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, tag | reg);
147 1.1.4.2 skrll data = bus_space_read_4(&pci_iot, pci_ioh, PCIC_CFGDATA);
148 1.1.4.2 skrll /* 405GP pass2 errata #6 */
149 1.1.4.2 skrll bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, 0);
150 1.1.4.2 skrll return data;
151 1.1.4.2 skrll }
152 1.1.4.2 skrll
153 1.1.4.2 skrll void
154 1.1.4.2 skrll pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
155 1.1.4.2 skrll {
156 1.1.4.2 skrll
157 1.1.4.2 skrll bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, tag | reg);
158 1.1.4.2 skrll bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGDATA, data);
159 1.1.4.2 skrll /* 405GP pass2 errata #6 */
160 1.1.4.2 skrll bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, 0);
161 1.1.4.2 skrll }
162 1.1.4.2 skrll
163 1.1.4.2 skrll
164 1.1.4.2 skrll int
165 1.1.4.2 skrll pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
166 1.1.4.2 skrll {
167 1.1.4.2 skrll int pin = pa->pa_intrpin;
168 1.1.4.2 skrll int dev = pa->pa_device;
169 1.1.4.2 skrll
170 1.1.4.2 skrll if (pin == 0) {
171 1.1.4.2 skrll /* No IRQ used. */
172 1.1.4.2 skrll goto bad;
173 1.1.4.2 skrll }
174 1.1.4.2 skrll
175 1.1.4.2 skrll if (pin > 4) {
176 1.1.4.2 skrll printf("pci_intr_map: bad interrupt pin %d\n", pin);
177 1.1.4.2 skrll goto bad;
178 1.1.4.2 skrll }
179 1.1.4.2 skrll
180 1.1.4.2 skrll /*
181 1.1.4.2 skrll * We need to map the interrupt pin to the interrupt bit in the UIC
182 1.1.4.2 skrll * associated with it. This is highly machine-dependent.
183 1.1.4.2 skrll */
184 1.1.4.2 skrll switch(dev) {
185 1.1.4.2 skrll case 1:
186 1.1.4.2 skrll case 2:
187 1.1.4.2 skrll case 3:
188 1.1.4.2 skrll case 4:
189 1.1.4.2 skrll *ihp = 27 + dev;
190 1.1.4.2 skrll break;
191 1.1.4.2 skrll default:
192 1.1.4.2 skrll printf("Hmm.. PCI device %d should not exist on this board\n",
193 1.1.4.2 skrll dev);
194 1.1.4.2 skrll goto bad;
195 1.1.4.2 skrll }
196 1.1.4.2 skrll return 0;
197 1.1.4.2 skrll
198 1.1.4.2 skrll bad:
199 1.1.4.2 skrll *ihp = -1;
200 1.1.4.2 skrll return 1;
201 1.1.4.2 skrll }
202 1.1.4.2 skrll
203 1.1.4.2 skrll const char *
204 1.1.4.2 skrll pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
205 1.1.4.2 skrll {
206 1.1.4.2 skrll static char irqstr[8]; /* 4 + 2 + NUL + sanity */
207 1.1.4.2 skrll
208 1.1.4.2 skrll if (ih == 0 || ih >= ICU_LEN)
209 1.1.4.2 skrll panic("pci_intr_string: bogus handle 0x%x", ih);
210 1.1.4.2 skrll
211 1.1.4.2 skrll sprintf(irqstr, "irq %d", ih);
212 1.1.4.2 skrll return (irqstr);
213 1.1.4.2 skrll
214 1.1.4.2 skrll }
215 1.1.4.2 skrll
216 1.1.4.2 skrll const struct evcnt *
217 1.1.4.2 skrll pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
218 1.1.4.2 skrll {
219 1.1.4.2 skrll
220 1.1.4.2 skrll /* XXX for now, no evcnt parent reported */
221 1.1.4.2 skrll return NULL;
222 1.1.4.2 skrll }
223 1.1.4.2 skrll
224 1.1.4.2 skrll void *
225 1.1.4.2 skrll pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
226 1.1.4.2 skrll int (*func)(void *), void *arg)
227 1.1.4.2 skrll {
228 1.1.4.2 skrll
229 1.1.4.2 skrll if (ih == 0 || ih >= ICU_LEN)
230 1.1.4.2 skrll panic("pci_intr_establish: bogus handle 0x%x", ih);
231 1.1.4.2 skrll
232 1.1.4.2 skrll return intr_establish(ih, IST_LEVEL, level, func, arg);
233 1.1.4.2 skrll }
234 1.1.4.2 skrll
235 1.1.4.2 skrll void
236 1.1.4.2 skrll pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
237 1.1.4.2 skrll {
238 1.1.4.2 skrll
239 1.1.4.2 skrll intr_disestablish(cookie);
240 1.1.4.2 skrll }
241 1.1.4.2 skrll
242 1.1.4.2 skrll void
243 1.1.4.2 skrll pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
244 1.1.4.2 skrll int swiz, int *iline)
245 1.1.4.2 skrll {
246 1.1.4.2 skrll
247 1.1.4.2 skrll if (bus == 0) {
248 1.1.4.2 skrll switch(dev) {
249 1.1.4.2 skrll case 1:
250 1.1.4.2 skrll case 2:
251 1.1.4.2 skrll case 3:
252 1.1.4.2 skrll case 4:
253 1.1.4.2 skrll *iline = 31 - dev;
254 1.1.4.2 skrll }
255 1.1.4.2 skrll } else {
256 1.1.4.2 skrll *iline = 20 + ((swiz + dev + 1) & 3);
257 1.1.4.2 skrll }
258 1.1.4.2 skrll }
259 1.1.4.2 skrll
260 1.1.4.2 skrll /* Avoid overconfiguration */
261 1.1.4.2 skrll int
262 1.1.4.2 skrll pci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func, pcireg_t id)
263 1.1.4.2 skrll {
264 1.1.4.2 skrll
265 1.1.4.2 skrll if ((PCI_VENDOR(id) == PCI_VENDOR_IBM && PCI_PRODUCT(id) == PCI_PRODUCT_IBM_405GP) ||
266 1.1.4.2 skrll (PCI_VENDOR(id) == PCI_VENDOR_INTEL && PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_80960_RP)) {
267 1.1.4.2 skrll /* Don't configure the bridge and PCI probe. */
268 1.1.4.2 skrll return 0;
269 1.1.4.2 skrll }
270 1.1.4.2 skrll return PCI_CONF_ALL & ~PCI_CONF_MAP_ROM;
271 1.1.4.2 skrll }
272