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