pci_machdep.c revision 1.7.2.2 1 1.7.2.2 nathanw /* $NetBSD: pci_machdep.c,v 1.7.2.2 2002/04/17 00:04:18 nathanw Exp $ */
2 1.7.2.2 nathanw
3 1.7.2.2 nathanw /*
4 1.7.2.2 nathanw * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.7.2.2 nathanw * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 1.7.2.2 nathanw *
7 1.7.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.7.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.7.2.2 nathanw * are met:
10 1.7.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.7.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.7.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.7.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.7.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.7.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
16 1.7.2.2 nathanw * must display the following acknowledgement:
17 1.7.2.2 nathanw * This product includes software developed by Charles M. Hannum.
18 1.7.2.2 nathanw * 4. The name of the author may not be used to endorse or promote products
19 1.7.2.2 nathanw * derived from this software without specific prior written permission.
20 1.7.2.2 nathanw *
21 1.7.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.7.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.7.2.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.7.2.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.7.2.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.7.2.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.7.2.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.7.2.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.7.2.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.7.2.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.7.2.2 nathanw */
32 1.7.2.2 nathanw
33 1.7.2.2 nathanw /*
34 1.7.2.2 nathanw * Machine-specific functions for PCI autoconfiguration.
35 1.7.2.2 nathanw *
36 1.7.2.2 nathanw * On PCs, there are two methods of generating PCI configuration cycles.
37 1.7.2.2 nathanw * We try to detect the appropriate mechanism for this machine and set
38 1.7.2.2 nathanw * up a few function pointers to access the correct method directly.
39 1.7.2.2 nathanw *
40 1.7.2.2 nathanw * The configuration method can be hard-coded in the config file by
41 1.7.2.2 nathanw * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42 1.7.2.2 nathanw * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43 1.7.2.2 nathanw */
44 1.7.2.2 nathanw #include "opt_openpic.h"
45 1.7.2.2 nathanw
46 1.7.2.2 nathanw #include <sys/types.h>
47 1.7.2.2 nathanw #include <sys/param.h>
48 1.7.2.2 nathanw #include <sys/device.h>
49 1.7.2.2 nathanw #include <sys/errno.h>
50 1.7.2.2 nathanw #include <sys/extent.h>
51 1.7.2.2 nathanw #include <sys/malloc.h>
52 1.7.2.2 nathanw #include <sys/queue.h>
53 1.7.2.2 nathanw #include <sys/systm.h>
54 1.7.2.2 nathanw #include <sys/time.h>
55 1.7.2.2 nathanw
56 1.7.2.2 nathanw #include <uvm/uvm.h>
57 1.7.2.2 nathanw
58 1.7.2.2 nathanw #define _POWERPC_BUS_DMA_PRIVATE
59 1.7.2.2 nathanw #include <machine/bus.h>
60 1.7.2.2 nathanw #include <machine/pio.h>
61 1.7.2.2 nathanw #include <machine/intr.h>
62 1.7.2.2 nathanw #include <machine/openpicreg.h>
63 1.7.2.2 nathanw
64 1.7.2.2 nathanw #include <dev/isa/isavar.h>
65 1.7.2.2 nathanw #include <dev/pci/pcivar.h>
66 1.7.2.2 nathanw #include <dev/pci/pcireg.h>
67 1.7.2.2 nathanw #include <dev/pci/pciconf.h>
68 1.7.2.2 nathanw
69 1.7.2.2 nathanw #include <sandpoint/isa/icu.h>
70 1.7.2.2 nathanw
71 1.7.2.2 nathanw struct powerpc_bus_dma_tag pci_bus_dma_tag = {
72 1.7.2.2 nathanw 0, /* _bounce_thresh */
73 1.7.2.2 nathanw _bus_dmamap_create,
74 1.7.2.2 nathanw _bus_dmamap_destroy,
75 1.7.2.2 nathanw _bus_dmamap_load,
76 1.7.2.2 nathanw _bus_dmamap_load_mbuf,
77 1.7.2.2 nathanw _bus_dmamap_load_uio,
78 1.7.2.2 nathanw _bus_dmamap_load_raw,
79 1.7.2.2 nathanw _bus_dmamap_unload,
80 1.7.2.2 nathanw NULL, /* _dmamap_sync */
81 1.7.2.2 nathanw _bus_dmamem_alloc,
82 1.7.2.2 nathanw _bus_dmamem_free,
83 1.7.2.2 nathanw _bus_dmamem_map,
84 1.7.2.2 nathanw _bus_dmamem_unmap,
85 1.7.2.2 nathanw _bus_dmamem_mmap,
86 1.7.2.2 nathanw };
87 1.7.2.2 nathanw
88 1.7.2.2 nathanw #define PCI_CONFIG_ENABLE 0x80000000UL
89 1.7.2.2 nathanw
90 1.7.2.2 nathanw void
91 1.7.2.2 nathanw pci_attach_hook(parent, self, pba)
92 1.7.2.2 nathanw struct device *parent, *self;
93 1.7.2.2 nathanw struct pcibus_attach_args *pba;
94 1.7.2.2 nathanw {
95 1.7.2.2 nathanw }
96 1.7.2.2 nathanw
97 1.7.2.2 nathanw int
98 1.7.2.2 nathanw pci_bus_maxdevs(pc, busno)
99 1.7.2.2 nathanw pci_chipset_tag_t pc;
100 1.7.2.2 nathanw int busno;
101 1.7.2.2 nathanw {
102 1.7.2.2 nathanw
103 1.7.2.2 nathanw /*
104 1.7.2.2 nathanw * Bus number is irrelevant. Configuration Mechanism 1 is in
105 1.7.2.2 nathanw * use, can have devices 0-32 (i.e. the `normal' range).
106 1.7.2.2 nathanw */
107 1.7.2.2 nathanw return (32);
108 1.7.2.2 nathanw }
109 1.7.2.2 nathanw
110 1.7.2.2 nathanw pcitag_t
111 1.7.2.2 nathanw pci_make_tag(pc, bus, device, function)
112 1.7.2.2 nathanw pci_chipset_tag_t pc;
113 1.7.2.2 nathanw int bus, device, function;
114 1.7.2.2 nathanw {
115 1.7.2.2 nathanw pcitag_t tag;
116 1.7.2.2 nathanw
117 1.7.2.2 nathanw if (bus >= 256 || device >= 32 || function >= 8)
118 1.7.2.2 nathanw panic("pci_make_tag: bad request");
119 1.7.2.2 nathanw
120 1.7.2.2 nathanw tag = PCI_CONFIG_ENABLE |
121 1.7.2.2 nathanw (bus << 16) | (device << 11) | (function << 8);
122 1.7.2.2 nathanw return tag;
123 1.7.2.2 nathanw }
124 1.7.2.2 nathanw
125 1.7.2.2 nathanw void
126 1.7.2.2 nathanw pci_decompose_tag(pc, tag, bp, dp, fp)
127 1.7.2.2 nathanw pci_chipset_tag_t pc;
128 1.7.2.2 nathanw pcitag_t tag;
129 1.7.2.2 nathanw int *bp, *dp, *fp;
130 1.7.2.2 nathanw {
131 1.7.2.2 nathanw
132 1.7.2.2 nathanw if (bp != NULL)
133 1.7.2.2 nathanw *bp = (tag >> 16) & 0xff;
134 1.7.2.2 nathanw if (dp != NULL)
135 1.7.2.2 nathanw *dp = (tag >> 11) & 0x1f;
136 1.7.2.2 nathanw if (fp != NULL)
137 1.7.2.2 nathanw *fp = (tag >> 8) & 0x7;
138 1.7.2.2 nathanw return;
139 1.7.2.2 nathanw }
140 1.7.2.2 nathanw
141 1.7.2.2 nathanw /*
142 1.7.2.2 nathanw * The Kahlua documentation says that "reg" should be left-shifted by two
143 1.7.2.2 nathanw * and be in bits 2-7. Apparently not. It doesn't work that way, and the
144 1.7.2.2 nathanw * DINK32 ROM doesn't do it that way (I peeked at 0xfec00000 after running
145 1.7.2.2 nathanw * the DINK32 "pcf" command).
146 1.7.2.2 nathanw */
147 1.7.2.2 nathanw #define SP_PCI(tag, reg) ((tag) | (reg))
148 1.7.2.2 nathanw
149 1.7.2.2 nathanw pcireg_t
150 1.7.2.2 nathanw pci_conf_read(pc, tag, reg)
151 1.7.2.2 nathanw pci_chipset_tag_t pc;
152 1.7.2.2 nathanw pcitag_t tag;
153 1.7.2.2 nathanw int reg;
154 1.7.2.2 nathanw {
155 1.7.2.2 nathanw pcireg_t data;
156 1.7.2.2 nathanw
157 1.7.2.2 nathanw out32rb(SANDPOINT_PCI_CONFIG_ADDR, SP_PCI(tag,reg));
158 1.7.2.2 nathanw data = in32rb(SANDPOINT_PCI_CONFIG_DATA);
159 1.7.2.2 nathanw out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
160 1.7.2.2 nathanw return data;
161 1.7.2.2 nathanw }
162 1.7.2.2 nathanw
163 1.7.2.2 nathanw void
164 1.7.2.2 nathanw pci_conf_write(pc, tag, reg, data)
165 1.7.2.2 nathanw pci_chipset_tag_t pc;
166 1.7.2.2 nathanw pcitag_t tag;
167 1.7.2.2 nathanw int reg;
168 1.7.2.2 nathanw pcireg_t data;
169 1.7.2.2 nathanw {
170 1.7.2.2 nathanw out32rb(SANDPOINT_PCI_CONFIG_ADDR, SP_PCI(tag, reg));
171 1.7.2.2 nathanw out32rb(SANDPOINT_PCI_CONFIG_DATA, data);
172 1.7.2.2 nathanw out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
173 1.7.2.2 nathanw }
174 1.7.2.2 nathanw
175 1.7.2.2 nathanw int
176 1.7.2.2 nathanw pci_intr_map(pa, ihp)
177 1.7.2.2 nathanw struct pci_attach_args *pa;
178 1.7.2.2 nathanw pci_intr_handle_t *ihp;
179 1.7.2.2 nathanw {
180 1.7.2.2 nathanw int pin = pa->pa_intrpin;
181 1.7.2.2 nathanw int line = pa->pa_intrline;
182 1.7.2.2 nathanw
183 1.7.2.2 nathanw if (pin == 0) {
184 1.7.2.2 nathanw /* No IRQ used. */
185 1.7.2.2 nathanw goto bad;
186 1.7.2.2 nathanw }
187 1.7.2.2 nathanw
188 1.7.2.2 nathanw if (pin > 4) {
189 1.7.2.2 nathanw printf("pci_intr_map: bad interrupt pin %d\n", pin);
190 1.7.2.2 nathanw goto bad;
191 1.7.2.2 nathanw }
192 1.7.2.2 nathanw
193 1.7.2.2 nathanw /*
194 1.7.2.2 nathanw * Section 6.2.4, `Miscellaneous Functions', says that 255 means
195 1.7.2.2 nathanw * `unknown' or `no connection' on a PC. We assume that a device with
196 1.7.2.2 nathanw * `no connection' either doesn't have an interrupt (in which case the
197 1.7.2.2 nathanw * pin number should be 0, and would have been noticed above), or
198 1.7.2.2 nathanw * wasn't configured by the BIOS (in which case we punt, since there's
199 1.7.2.2 nathanw * no real way we can know how the interrupt lines are mapped in the
200 1.7.2.2 nathanw * hardware).
201 1.7.2.2 nathanw *
202 1.7.2.2 nathanw * XXX
203 1.7.2.2 nathanw * Since IRQ 0 is only used by the clock, and we can't actually be sure
204 1.7.2.2 nathanw * that the BIOS did its job, we also recognize that as meaning that
205 1.7.2.2 nathanw * the BIOS has not configured the device.
206 1.7.2.2 nathanw */
207 1.7.2.2 nathanw if (line == 255) {
208 1.7.2.2 nathanw printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
209 1.7.2.2 nathanw goto bad;
210 1.7.2.2 nathanw }
211 1.7.2.2 nathanw if (line == 11) {
212 1.7.2.2 nathanw switch (pin) {
213 1.7.2.2 nathanw case PCI_INTERRUPT_PIN_A:
214 1.7.2.2 nathanw *ihp = SANDPOINT_INTR_WINBOND_A;
215 1.7.2.2 nathanw break;
216 1.7.2.2 nathanw case PCI_INTERRUPT_PIN_B:
217 1.7.2.2 nathanw *ihp = SANDPOINT_INTR_WINBOND_B;
218 1.7.2.2 nathanw break;
219 1.7.2.2 nathanw case PCI_INTERRUPT_PIN_C:
220 1.7.2.2 nathanw *ihp = SANDPOINT_INTR_WINBOND_C;
221 1.7.2.2 nathanw break;
222 1.7.2.2 nathanw case PCI_INTERRUPT_PIN_D:
223 1.7.2.2 nathanw *ihp = SANDPOINT_INTR_WINBOND_D;
224 1.7.2.2 nathanw break;
225 1.7.2.2 nathanw default:
226 1.7.2.2 nathanw printf("pci_intr_map: bad interrupt line %d,%c\n",
227 1.7.2.2 nathanw line, pin + '@');
228 1.7.2.2 nathanw goto bad;
229 1.7.2.2 nathanw break;
230 1.7.2.2 nathanw }
231 1.7.2.2 nathanw } else {
232 1.7.2.2 nathanw /*
233 1.7.2.2 nathanw * Sandpoint has 4 PCI slots.
234 1.7.2.2 nathanw * Sandpoint rev. X2 has them in a weird order. Counting
235 1.7.2.2 nathanw * from center out toward the edge, we have:
236 1.7.2.2 nathanw * Slot 1 (dev 14?) (labelled 1)
237 1.7.2.2 nathanw * Slot 0 (dev 13?) (labelled 2)
238 1.7.2.2 nathanw * Slot 3 (dev 16) (labelled 3)
239 1.7.2.2 nathanw * Slot 2 (dev 15) (labelled 4)
240 1.7.2.2 nathanw * To keep things confusing, we will consistently use a zero-
241 1.7.2.2 nathanw * based numbering scheme where Motorola's is usually 1-based.
242 1.7.2.2 nathanw */
243 1.7.2.2 nathanw if (line < 13 || line > 16) {
244 1.7.2.2 nathanw printf("pci_intr_map: bad interrupt line %d,%c\n",
245 1.7.2.2 nathanw line, pin + '@');
246 1.7.2.2 nathanw goto bad;
247 1.7.2.2 nathanw }
248 1.7.2.2 nathanw
249 1.7.2.2 nathanw /*
250 1.7.2.2 nathanw * In the PCI configuration code, we simply assign the dev
251 1.7.2.2 nathanw * number to the interrupt line. We extract it here for the
252 1.7.2.2 nathanw * interrupt, but subtract off the lowest dev (13) to get
253 1.7.2.2 nathanw * the IRQ.
254 1.7.2.2 nathanw */
255 1.7.2.2 nathanw #if defined(OPENPIC_SERIAL_MODE)
256 1.7.2.2 nathanw *ihp = line - 11;
257 1.7.2.2 nathanw #else
258 1.7.2.2 nathanw *ihp = line - 13;
259 1.7.2.2 nathanw #endif
260 1.7.2.2 nathanw }
261 1.7.2.2 nathanw return 0;
262 1.7.2.2 nathanw
263 1.7.2.2 nathanw bad:
264 1.7.2.2 nathanw *ihp = -1;
265 1.7.2.2 nathanw return 1;
266 1.7.2.2 nathanw }
267 1.7.2.2 nathanw
268 1.7.2.2 nathanw const char *
269 1.7.2.2 nathanw pci_intr_string(pc, ih)
270 1.7.2.2 nathanw pci_chipset_tag_t pc;
271 1.7.2.2 nathanw pci_intr_handle_t ih;
272 1.7.2.2 nathanw {
273 1.7.2.2 nathanw static char irqstr[8]; /* 4 + 2 + NULL + sanity */
274 1.7.2.2 nathanw
275 1.7.2.2 nathanw if (ih < 0 || ih >= ICU_LEN)
276 1.7.2.2 nathanw panic("pci_intr_string: bogus handle 0x%x\n", ih);
277 1.7.2.2 nathanw
278 1.7.2.2 nathanw sprintf(irqstr, "irq %d", ih);
279 1.7.2.2 nathanw return (irqstr);
280 1.7.2.2 nathanw
281 1.7.2.2 nathanw }
282 1.7.2.2 nathanw
283 1.7.2.2 nathanw const struct evcnt *
284 1.7.2.2 nathanw pci_intr_evcnt(pc, ih)
285 1.7.2.2 nathanw pci_chipset_tag_t pc;
286 1.7.2.2 nathanw pci_intr_handle_t ih;
287 1.7.2.2 nathanw {
288 1.7.2.2 nathanw
289 1.7.2.2 nathanw /* XXX for now, no evcnt parent reported */
290 1.7.2.2 nathanw return NULL;
291 1.7.2.2 nathanw }
292 1.7.2.2 nathanw
293 1.7.2.2 nathanw void *
294 1.7.2.2 nathanw pci_intr_establish(pc, ih, level, func, arg)
295 1.7.2.2 nathanw pci_chipset_tag_t pc;
296 1.7.2.2 nathanw pci_intr_handle_t ih;
297 1.7.2.2 nathanw int level, (*func) __P((void *));
298 1.7.2.2 nathanw void *arg;
299 1.7.2.2 nathanw {
300 1.7.2.2 nathanw #if 0
301 1.7.2.2 nathanw if (ih < SANDPOINT_INTR_PCI0 || ih > SANDPOINT_INTR_PCI3)
302 1.7.2.2 nathanw panic("pci_intr_establish: bogus handle 0x%x\n", ih);
303 1.7.2.2 nathanw #endif
304 1.7.2.2 nathanw
305 1.7.2.2 nathanw /*
306 1.7.2.2 nathanw * ih is the value assigned in pci_intr_map(), above.
307 1.7.2.2 nathanw * For the Sandpoint, this is the zero-based slot #,
308 1.7.2.2 nathanw * configured when the bus is set up.
309 1.7.2.2 nathanw */
310 1.7.2.2 nathanw return intr_establish(ih, IST_LEVEL, level, func, arg);
311 1.7.2.2 nathanw }
312 1.7.2.2 nathanw
313 1.7.2.2 nathanw void
314 1.7.2.2 nathanw pci_intr_disestablish(pc, cookie)
315 1.7.2.2 nathanw pci_chipset_tag_t pc;
316 1.7.2.2 nathanw void *cookie;
317 1.7.2.2 nathanw {
318 1.7.2.2 nathanw intr_disestablish(cookie);
319 1.7.2.2 nathanw }
320 1.7.2.2 nathanw
321 1.7.2.2 nathanw void
322 1.7.2.2 nathanw pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin, int swiz,
323 1.7.2.2 nathanw int *iline)
324 1.7.2.2 nathanw {
325 1.7.2.2 nathanw if (bus == 0) {
326 1.7.2.2 nathanw *iline = dev;
327 1.7.2.2 nathanw } else {
328 1.7.2.2 nathanw /*
329 1.7.2.2 nathanw * If we are not on bus zero, we're behind a bridge, so we
330 1.7.2.2 nathanw * swizzle.
331 1.7.2.2 nathanw *
332 1.7.2.2 nathanw * The documentation lies about this. In slot 3 (numbering
333 1.7.2.2 nathanw * from 0) aka device 16, INTD# becomes an interrupt for
334 1.7.2.2 nathanw * slot 2. INTC# becomes an interrupt for slot 1, etc.
335 1.7.2.2 nathanw * In slot 2 aka device 16, INTD# becomes an interrupt for
336 1.7.2.2 nathanw * slot 1, etc.
337 1.7.2.2 nathanw *
338 1.7.2.2 nathanw * Verified for INTD# on device 16, INTC# on device 16,
339 1.7.2.2 nathanw * INTD# on device 15, INTD# on device 13, and INTC# on
340 1.7.2.2 nathanw * device 14. I presume that the rest follow the same
341 1.7.2.2 nathanw * pattern.
342 1.7.2.2 nathanw *
343 1.7.2.2 nathanw * Slot 0 is device 13, and is the base for the rest.
344 1.7.2.2 nathanw */
345 1.7.2.2 nathanw *iline = 13 + ((swiz + dev + 3) & 3);
346 1.7.2.2 nathanw }
347 1.7.2.2 nathanw }
348