pci_machdep.c revision 1.44 1 1.44 pk /* $NetBSD: pci_machdep.c,v 1.44 2004/03/21 16:09:13 pk Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.5 mrg * Copyright (c) 1999, 2000 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * functions expected by the MI PCI code.
33 1.1 mrg */
34 1.38 lukem
35 1.38 lukem #include <sys/cdefs.h>
36 1.44 pk __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.44 2004/03/21 16:09:13 pk Exp $");
37 1.1 mrg
38 1.1 mrg #include <sys/types.h>
39 1.1 mrg #include <sys/param.h>
40 1.1 mrg #include <sys/time.h>
41 1.1 mrg #include <sys/systm.h>
42 1.1 mrg #include <sys/errno.h>
43 1.1 mrg #include <sys/device.h>
44 1.1 mrg #include <sys/malloc.h>
45 1.1 mrg
46 1.1 mrg #define _SPARC_BUS_DMA_PRIVATE
47 1.1 mrg #include <machine/bus.h>
48 1.1 mrg #include <machine/autoconf.h>
49 1.22 eeh #include <machine/openfirm.h>
50 1.1 mrg #include <dev/pci/pcivar.h>
51 1.1 mrg #include <dev/pci/pcireg.h>
52 1.1 mrg
53 1.19 mrg #include <dev/ofw/ofw_pci.h>
54 1.19 mrg
55 1.1 mrg #include <sparc64/dev/iommureg.h>
56 1.1 mrg #include <sparc64/dev/iommuvar.h>
57 1.1 mrg #include <sparc64/dev/psychoreg.h>
58 1.1 mrg #include <sparc64/dev/psychovar.h>
59 1.37 martin #include <sparc64/sparc64/cache.h>
60 1.39 petrov
61 1.39 petrov #ifdef DEBUG
62 1.39 petrov #define SPDB_CONF 0x01
63 1.39 petrov #define SPDB_INTR 0x04
64 1.39 petrov #define SPDB_INTMAP 0x08
65 1.39 petrov #define SPDB_INTFIX 0x10
66 1.39 petrov #define SPDB_PROBE 0x20
67 1.39 petrov int sparc_pci_debug = 0x0;
68 1.39 petrov #define DPRINTF(l, s) do { if (sparc_pci_debug & l) printf s; } while (0)
69 1.39 petrov #else
70 1.39 petrov #define DPRINTF(l, s)
71 1.39 petrov #endif
72 1.1 mrg
73 1.1 mrg /* this is a base to be copied */
74 1.1 mrg struct sparc_pci_chipset _sparc_pci_chipset = {
75 1.1 mrg NULL,
76 1.1 mrg };
77 1.2 mrg
78 1.41 petrov static int pci_find_ino(struct pci_attach_args *, pci_intr_handle_t *);
79 1.36 martin
80 1.30 thorpej static pcitag_t
81 1.30 thorpej ofpci_make_tag(pci_chipset_tag_t pc, int node, int b, int d, int f)
82 1.30 thorpej {
83 1.30 thorpej pcitag_t tag;
84 1.30 thorpej
85 1.30 thorpej tag = PCITAG_CREATE(node, b, d, f);
86 1.30 thorpej
87 1.30 thorpej /* Enable all the different spaces for this device */
88 1.30 thorpej pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
89 1.30 thorpej PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_MASTER_ENABLE|
90 1.30 thorpej PCI_COMMAND_IO_ENABLE);
91 1.30 thorpej return (tag);
92 1.30 thorpej }
93 1.30 thorpej
94 1.1 mrg /*
95 1.1 mrg * functions provided to the MI code.
96 1.1 mrg */
97 1.1 mrg
98 1.1 mrg void
99 1.1 mrg pci_attach_hook(parent, self, pba)
100 1.1 mrg struct device *parent;
101 1.1 mrg struct device *self;
102 1.1 mrg struct pcibus_attach_args *pba;
103 1.1 mrg {
104 1.1 mrg }
105 1.1 mrg
106 1.1 mrg int
107 1.1 mrg pci_bus_maxdevs(pc, busno)
108 1.1 mrg pci_chipset_tag_t pc;
109 1.1 mrg int busno;
110 1.1 mrg {
111 1.1 mrg
112 1.1 mrg return 32;
113 1.1 mrg }
114 1.19 mrg
115 1.1 mrg pcitag_t
116 1.1 mrg pci_make_tag(pc, b, d, f)
117 1.1 mrg pci_chipset_tag_t pc;
118 1.1 mrg int b;
119 1.1 mrg int d;
120 1.1 mrg int f;
121 1.1 mrg {
122 1.35 nakayama struct psycho_pbm *pp = pc->cookie;
123 1.44 pk struct ofw_pci_register reg, *regp;
124 1.22 eeh pcitag_t tag;
125 1.35 nakayama int (*valid) __P((void *));
126 1.22 eeh int node, len;
127 1.22 eeh #ifdef DEBUG
128 1.22 eeh char name[80];
129 1.40 martin memset(name, 0, sizeof(name));
130 1.22 eeh #endif
131 1.1 mrg
132 1.35 nakayama /*
133 1.35 nakayama * Refer to the PCI/CardBus bus node first.
134 1.35 nakayama * It returns a tag if node is present and bus is valid.
135 1.35 nakayama */
136 1.35 nakayama if (0 <= b && b < 256) {
137 1.35 nakayama node = (*pp->pp_busnode)[b].node;
138 1.35 nakayama valid = (*pp->pp_busnode)[b].valid;
139 1.35 nakayama if (node != 0 && d == 0 &&
140 1.35 nakayama (valid == NULL || (*valid)((*pp->pp_busnode)[b].arg)))
141 1.35 nakayama return ofpci_make_tag(pc, node, b, d, f);
142 1.35 nakayama }
143 1.35 nakayama
144 1.22 eeh /*
145 1.22 eeh * Hunt for the node that corresponds to this device
146 1.22 eeh *
147 1.22 eeh * We could cache this info in an array in the parent
148 1.22 eeh * device... except then we have problems with devices
149 1.22 eeh * attached below pci-pci bridges, and we would need to
150 1.22 eeh * add special code to the pci-pci bridge to cache this
151 1.22 eeh * info.
152 1.22 eeh */
153 1.1 mrg
154 1.22 eeh tag = PCITAG_CREATE(-1, b, d, f);
155 1.22 eeh node = pc->rootnode;
156 1.22 eeh /*
157 1.22 eeh * First make sure we're on the right bus. If our parent
158 1.22 eeh * has a bus-range property and we're not in the range,
159 1.22 eeh * then we're obviously on the wrong bus. So go up one
160 1.22 eeh * level.
161 1.22 eeh */
162 1.22 eeh #ifdef DEBUG
163 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
164 1.42 pk printf("curnode %x %s\n", node,
165 1.42 pk prom_getpropstringA(node, "name", name, sizeof(name)));
166 1.22 eeh }
167 1.22 eeh #endif
168 1.22 eeh #if 0
169 1.22 eeh while ((OF_getprop(OF_parent(node), "bus-range", (void *)&busrange,
170 1.22 eeh sizeof(busrange)) == sizeof(busrange)) &&
171 1.22 eeh (b < busrange[0] || b > busrange[1])) {
172 1.22 eeh /* Out of range, go up one */
173 1.22 eeh node = OF_parent(node);
174 1.22 eeh #ifdef DEBUG
175 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
176 1.42 pk printf("going up to node %x %s\n", node,
177 1.42 pk prom_getpropstringA(node, "name", name, sizeof(name)));
178 1.22 eeh }
179 1.22 eeh #endif
180 1.22 eeh }
181 1.22 eeh #endif
182 1.22 eeh /*
183 1.22 eeh * Now traverse all peers until we find the node or we find
184 1.22 eeh * the right bridge.
185 1.22 eeh *
186 1.22 eeh * XXX We go up one and down one to make sure nobody's missed.
187 1.22 eeh * but this should not be necessary.
188 1.22 eeh */
189 1.42 pk for (node = ((node)); node; node = prom_nextsibling(node)) {
190 1.1 mrg
191 1.22 eeh #ifdef DEBUG
192 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
193 1.43 pk printf("checking node %x %s\n", node,
194 1.42 pk prom_getpropstringA(node, "name", name, sizeof(name)));
195 1.42 pk
196 1.22 eeh }
197 1.22 eeh #endif
198 1.1 mrg
199 1.22 eeh #if 1
200 1.1 mrg /*
201 1.22 eeh * Check for PCI-PCI bridges. If the device we want is
202 1.22 eeh * in the bus-range for that bridge, work our way down.
203 1.1 mrg */
204 1.44 pk while (1) {
205 1.44 pk int busrange[2], *brp;
206 1.44 pk len = 2;
207 1.44 pk brp = busrange;
208 1.44 pk if (prom_getprop(node, "bus-range", sizeof(busrange),
209 1.44 pk &len, &brp) != 0)
210 1.44 pk break;
211 1.44 pk if (len != 2 || b < busrange[0] || b > busrange[1])
212 1.44 pk break;
213 1.22 eeh /* Go down 1 level */
214 1.42 pk node = prom_firstchild(node);
215 1.22 eeh #ifdef DEBUG
216 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
217 1.43 pk printf("going down to node %x %s\n", node,
218 1.42 pk prom_getpropstringA(node, "name",
219 1.42 pk name, sizeof(name)));
220 1.22 eeh }
221 1.22 eeh #endif
222 1.1 mrg }
223 1.44 pk #endif /*1*/
224 1.22 eeh /*
225 1.22 eeh * We only really need the first `reg' property.
226 1.22 eeh *
227 1.22 eeh * For simplicity, we'll query the `reg' when we
228 1.22 eeh * need it. Otherwise we could malloc() it, but
229 1.22 eeh * that gets more complicated.
230 1.22 eeh */
231 1.44 pk regp = ®
232 1.44 pk len = sizeof reg;
233 1.44 pk switch (prom_getprop(node, "reg", sizeof(reg), &len, ®p)) {
234 1.44 pk default:
235 1.44 pk panic("pci_probe_bus: OF_getprop len botch");
236 1.44 pk case ENOENT:
237 1.22 eeh continue;
238 1.44 pk case 0:
239 1.44 pk break;
240 1.44 pk }
241 1.22 eeh
242 1.22 eeh if (b != OFW_PCI_PHYS_HI_BUS(reg.phys_hi))
243 1.22 eeh continue;
244 1.22 eeh if (d != OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi))
245 1.22 eeh continue;
246 1.22 eeh if (f != OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi))
247 1.22 eeh continue;
248 1.22 eeh
249 1.22 eeh /* Got a match */
250 1.27 eeh tag = ofpci_make_tag(pc, node, b, d, f);
251 1.22 eeh
252 1.22 eeh return (tag);
253 1.1 mrg }
254 1.22 eeh /* No device found -- return a dead tag */
255 1.27 eeh return (tag);
256 1.28 thorpej }
257 1.28 thorpej
258 1.28 thorpej void
259 1.28 thorpej pci_decompose_tag(pc, tag, bp, dp, fp)
260 1.28 thorpej pci_chipset_tag_t pc;
261 1.28 thorpej pcitag_t tag;
262 1.28 thorpej int *bp, *dp, *fp;
263 1.28 thorpej {
264 1.28 thorpej
265 1.28 thorpej if (bp != NULL)
266 1.28 thorpej *bp = PCITAG_BUS(tag);
267 1.28 thorpej if (dp != NULL)
268 1.28 thorpej *dp = PCITAG_DEV(tag);
269 1.28 thorpej if (fp != NULL)
270 1.28 thorpej *fp = PCITAG_FUN(tag);
271 1.27 eeh }
272 1.27 eeh
273 1.30 thorpej int
274 1.30 thorpej pci_enumerate_bus(struct pci_softc *sc,
275 1.30 thorpej int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
276 1.27 eeh {
277 1.30 thorpej struct ofw_pci_register reg;
278 1.30 thorpej pci_chipset_tag_t pc = sc->sc_pc;
279 1.27 eeh pcitag_t tag;
280 1.35 nakayama pcireg_t class, csr, bhlc, ic;
281 1.30 thorpej int node, b, d, f, ret;
282 1.37 martin int bus_frequency, lt, cl, cacheline;
283 1.30 thorpej char name[30];
284 1.32 eeh extern int pci_config_dump;
285 1.30 thorpej
286 1.31 eeh if (sc->sc_bridgetag)
287 1.31 eeh node = PCITAG_NODE(*sc->sc_bridgetag);
288 1.31 eeh else
289 1.31 eeh node = pc->rootnode;
290 1.31 eeh
291 1.42 pk bus_frequency =
292 1.42 pk prom_getpropint(node, "clock-frequency", 33000000) / 1000000;
293 1.35 nakayama
294 1.37 martin /*
295 1.37 martin * Make sure the cache line size is at least as big as the
296 1.37 martin * ecache line and the streaming cache (64 byte).
297 1.37 martin */
298 1.37 martin cacheline = max(cacheinfo.ec_linesize, 64);
299 1.37 martin KASSERT((cacheline/64)*64 == cacheline &&
300 1.37 martin (cacheline/cacheinfo.ec_linesize)*cacheinfo.ec_linesize == cacheline &&
301 1.37 martin (cacheline/4)*4 == cacheline);
302 1.37 martin
303 1.32 eeh /* Turn on parity for the bus. */
304 1.32 eeh tag = ofpci_make_tag(pc, node, sc->sc_bus, 0, 0);
305 1.32 eeh csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
306 1.32 eeh csr |= PCI_COMMAND_PARITY_ENABLE;
307 1.32 eeh pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
308 1.32 eeh
309 1.35 nakayama /*
310 1.35 nakayama * Initialize the latency timer register.
311 1.35 nakayama * The value 0x40 is from Solaris.
312 1.35 nakayama */
313 1.35 nakayama bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
314 1.35 nakayama bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
315 1.35 nakayama bhlc |= 0x40 << PCI_LATTIMER_SHIFT;
316 1.35 nakayama pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
317 1.35 nakayama
318 1.32 eeh if (pci_config_dump) pci_conf_print(pc, tag, NULL);
319 1.32 eeh
320 1.42 pk for (node = prom_firstchild(node); node != 0 && node != -1;
321 1.42 pk node = prom_nextsibling(node)) {
322 1.30 thorpej name[0] = name[29] = 0;
323 1.42 pk prom_getpropstringA(node, "name", name, sizeof(name));
324 1.27 eeh
325 1.30 thorpej if (OF_getprop(node, "class-code", &class, sizeof(class)) !=
326 1.30 thorpej sizeof(class))
327 1.30 thorpej continue;
328 1.30 thorpej if (OF_getprop(node, "reg", ®, sizeof(reg)) < sizeof(reg))
329 1.30 thorpej panic("pci_enumerate_bus: \"%s\" regs too small", name);
330 1.27 eeh
331 1.30 thorpej b = OFW_PCI_PHYS_HI_BUS(reg.phys_hi);
332 1.30 thorpej d = OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi);
333 1.30 thorpej f = OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi);
334 1.30 thorpej
335 1.30 thorpej if (sc->sc_bus != b) {
336 1.30 thorpej printf("%s: WARNING: incorrect bus # for \"%s\" "
337 1.30 thorpej "(%d/%d/%d)\n", sc->sc_dev.dv_xname, name, b, d, f);
338 1.30 thorpej continue;
339 1.30 thorpej }
340 1.27 eeh
341 1.30 thorpej tag = ofpci_make_tag(pc, node, b, d, f);
342 1.32 eeh
343 1.32 eeh /*
344 1.32 eeh * Turn on parity and fast-back-to-back for the device.
345 1.32 eeh */
346 1.32 eeh csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
347 1.32 eeh if (csr & PCI_STATUS_BACKTOBACK_SUPPORT)
348 1.32 eeh csr |= PCI_COMMAND_BACKTOBACK_ENABLE;
349 1.32 eeh csr |= PCI_COMMAND_PARITY_ENABLE;
350 1.32 eeh pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
351 1.32 eeh
352 1.35 nakayama /*
353 1.35 nakayama * Initialize the latency timer register for busmaster
354 1.35 nakayama * devices to work properly.
355 1.35 nakayama * latency-timer = min-grant * bus-freq / 4 (from FreeBSD)
356 1.35 nakayama * Also initialize the cache line size register.
357 1.35 nakayama * Solaris anytime sets this register to the value 0x10.
358 1.35 nakayama */
359 1.35 nakayama bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
360 1.35 nakayama ic = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
361 1.35 nakayama
362 1.35 nakayama lt = min(PCI_MIN_GNT(ic) * bus_frequency / 4, 255);
363 1.35 nakayama if (lt == 0 || lt < PCI_LATTIMER(bhlc))
364 1.35 nakayama lt = PCI_LATTIMER(bhlc);
365 1.35 nakayama
366 1.35 nakayama cl = PCI_CACHELINE(bhlc);
367 1.35 nakayama if (cl == 0)
368 1.37 martin cl = cacheline;
369 1.35 nakayama
370 1.35 nakayama bhlc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
371 1.35 nakayama (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
372 1.35 nakayama bhlc |= (lt << PCI_LATTIMER_SHIFT) |
373 1.35 nakayama (cl << PCI_CACHELINE_SHIFT);
374 1.35 nakayama pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
375 1.35 nakayama
376 1.30 thorpej ret = pci_probe_device(sc, tag, match, pap);
377 1.30 thorpej if (match != NULL && ret != 0)
378 1.30 thorpej return (ret);
379 1.30 thorpej }
380 1.30 thorpej return (0);
381 1.1 mrg }
382 1.1 mrg
383 1.1 mrg /* assume we are mapped little-endian/side-effect */
384 1.1 mrg pcireg_t
385 1.1 mrg pci_conf_read(pc, tag, reg)
386 1.1 mrg pci_chipset_tag_t pc;
387 1.1 mrg pcitag_t tag;
388 1.1 mrg int reg;
389 1.1 mrg {
390 1.1 mrg struct psycho_pbm *pp = pc->cookie;
391 1.1 mrg struct psycho_softc *sc = pp->pp_sc;
392 1.22 eeh pcireg_t val = (pcireg_t)~0;
393 1.22 eeh
394 1.22 eeh DPRINTF(SPDB_CONF, ("pci_conf_read: tag %lx reg %x ",
395 1.22 eeh (long)tag, reg));
396 1.22 eeh if (PCITAG_NODE(tag) != -1) {
397 1.22 eeh DPRINTF(SPDB_CONF, ("asi=%x addr=%qx (offset=%x) ...",
398 1.26 eeh sc->sc_configaddr._asi,
399 1.26 eeh (long long)(sc->sc_configaddr._ptr +
400 1.22 eeh PCITAG_OFFSET(tag) + reg),
401 1.22 eeh (int)PCITAG_OFFSET(tag) + reg));
402 1.1 mrg
403 1.1 mrg val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr,
404 1.22 eeh PCITAG_OFFSET(tag) + reg);
405 1.1 mrg }
406 1.22 eeh #ifdef DEBUG
407 1.24 mrg else DPRINTF(SPDB_CONF, ("pci_conf_read: bogus pcitag %x\n",
408 1.24 mrg (int)PCITAG_OFFSET(tag)));
409 1.22 eeh #endif
410 1.1 mrg DPRINTF(SPDB_CONF, (" returning %08x\n", (u_int)val));
411 1.1 mrg
412 1.1 mrg return (val);
413 1.1 mrg }
414 1.1 mrg
415 1.1 mrg void
416 1.1 mrg pci_conf_write(pc, tag, reg, data)
417 1.1 mrg pci_chipset_tag_t pc;
418 1.1 mrg pcitag_t tag;
419 1.1 mrg int reg;
420 1.1 mrg pcireg_t data;
421 1.1 mrg {
422 1.1 mrg struct psycho_pbm *pp = pc->cookie;
423 1.1 mrg struct psycho_softc *sc = pp->pp_sc;
424 1.1 mrg
425 1.22 eeh DPRINTF(SPDB_CONF, ("pci_conf_write: tag %lx; reg %x; data %x; ",
426 1.22 eeh (long)PCITAG_OFFSET(tag), reg, (int)data));
427 1.1 mrg DPRINTF(SPDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n",
428 1.26 eeh sc->sc_configaddr._asi,
429 1.26 eeh (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg),
430 1.22 eeh (int)PCITAG_OFFSET(tag) + reg));
431 1.1 mrg
432 1.24 mrg /* If we don't know it, just punt it. */
433 1.24 mrg if (PCITAG_NODE(tag) == -1) {
434 1.24 mrg DPRINTF(SPDB_CONF, ("pci_conf_write: bad addr"));
435 1.24 mrg return;
436 1.24 mrg }
437 1.1 mrg
438 1.22 eeh bus_space_write_4(sc->sc_configtag, sc->sc_configaddr,
439 1.22 eeh PCITAG_OFFSET(tag) + reg, data);
440 1.1 mrg }
441 1.1 mrg
442 1.41 petrov static int
443 1.41 petrov pci_find_ino(pa, ihp)
444 1.41 petrov struct pci_attach_args *pa;
445 1.41 petrov pci_intr_handle_t *ihp;
446 1.41 petrov {
447 1.41 petrov struct psycho_pbm *pp = pa->pa_pc->cookie;
448 1.41 petrov struct psycho_softc *sc = pp->pp_sc;
449 1.41 petrov u_int dev;
450 1.41 petrov u_int ino;
451 1.41 petrov
452 1.41 petrov ino = *ihp;
453 1.41 petrov
454 1.41 petrov if ((ino & ~INTMAP_PCIINT) == 0) {
455 1.41 petrov
456 1.41 petrov if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
457 1.41 petrov pp->pp_id == PSYCHO_PBM_B)
458 1.41 petrov dev = pa->pa_device - 2;
459 1.41 petrov else
460 1.41 petrov dev = pa->pa_device - 1;
461 1.41 petrov
462 1.41 petrov DPRINTF(SPDB_CONF, ("pci_find_ino: mode %d, pbm %d, dev %d\n",
463 1.41 petrov sc->sc_mode, pp->pp_id, dev));
464 1.41 petrov
465 1.41 petrov if (ino == 0 || ino > 4) {
466 1.41 petrov u_int32_t intreg;
467 1.41 petrov
468 1.41 petrov intreg = pci_conf_read(pa->pa_pc, pa->pa_tag,
469 1.41 petrov PCI_INTERRUPT_REG);
470 1.41 petrov
471 1.41 petrov ino = PCI_INTERRUPT_PIN(intreg) - 1;
472 1.41 petrov } else
473 1.41 petrov ino -= 1;
474 1.41 petrov
475 1.41 petrov ino &= INTMAP_PCIINT;
476 1.41 petrov
477 1.41 petrov ino |= sc->sc_ign;
478 1.41 petrov ino |= ((pp->pp_id == PSYCHO_PBM_B) ? INTMAP_PCIBUS : 0);
479 1.41 petrov ino |= (dev << 2) & INTMAP_PCISLOT;
480 1.41 petrov
481 1.41 petrov *ihp = ino;
482 1.41 petrov }
483 1.41 petrov
484 1.41 petrov return (0);
485 1.41 petrov }
486 1.41 petrov
487 1.1 mrg /*
488 1.1 mrg * interrupt mapping foo.
489 1.20 mrg * XXX: how does this deal with multiple interrupts for a device?
490 1.1 mrg */
491 1.1 mrg int
492 1.16 sommerfe pci_intr_map(pa, ihp)
493 1.16 sommerfe struct pci_attach_args *pa;
494 1.1 mrg pci_intr_handle_t *ihp;
495 1.1 mrg {
496 1.22 eeh pcitag_t tag = pa->pa_tag;
497 1.44 pk int interrupts, *intp;
498 1.22 eeh int len, node = PCITAG_NODE(tag);
499 1.22 eeh char devtype[30];
500 1.22 eeh
501 1.44 pk intp = &interrupts;
502 1.44 pk len = 1;
503 1.44 pk if (prom_getprop(node, "interrupts", sizeof(interrupts),
504 1.44 pk &len, &intp) != 0 || len != 1) {
505 1.22 eeh DPRINTF(SPDB_INTMAP,
506 1.22 eeh ("pci_intr_map: could not read interrupts\n"));
507 1.22 eeh return (ENODEV);
508 1.22 eeh }
509 1.20 mrg
510 1.22 eeh if (OF_mapintr(node, &interrupts, sizeof(interrupts),
511 1.23 eeh sizeof(interrupts)) < 0) {
512 1.22 eeh printf("OF_mapintr failed\n");
513 1.41 petrov pci_find_ino(pa, &interrupts);
514 1.22 eeh }
515 1.44 pk
516 1.22 eeh /* Try to find an IPL for this type of device. */
517 1.44 pk prom_getpropstringA(node, "device_type", devtype, sizeof(devtype));
518 1.44 pk for (len = 0; intrmap[len].in_class != NULL; len++)
519 1.44 pk if (strcmp(intrmap[len].in_class, devtype) == 0) {
520 1.44 pk interrupts |= INTLEVENCODE(intrmap[len].in_lev);
521 1.44 pk break;
522 1.44 pk }
523 1.20 mrg
524 1.22 eeh /* XXXX -- we use the ino. What if there is a valid IGN? */
525 1.22 eeh *ihp = interrupts;
526 1.22 eeh return (0);
527 1.1 mrg }
528 1.1 mrg
529 1.1 mrg const char *
530 1.1 mrg pci_intr_string(pc, ih)
531 1.1 mrg pci_chipset_tag_t pc;
532 1.1 mrg pci_intr_handle_t ih;
533 1.1 mrg {
534 1.1 mrg static char str[16];
535 1.1 mrg
536 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_string: ih %u", ih));
537 1.22 eeh sprintf(str, "ivec %x", ih);
538 1.1 mrg DPRINTF(SPDB_INTR, ("; returning %s\n", str));
539 1.1 mrg
540 1.1 mrg return (str);
541 1.8 cgd }
542 1.8 cgd
543 1.8 cgd const struct evcnt *
544 1.8 cgd pci_intr_evcnt(pc, ih)
545 1.8 cgd pci_chipset_tag_t pc;
546 1.8 cgd pci_intr_handle_t ih;
547 1.8 cgd {
548 1.8 cgd
549 1.8 cgd /* XXX for now, no evcnt parent reported */
550 1.8 cgd return NULL;
551 1.1 mrg }
552 1.1 mrg
553 1.1 mrg void *
554 1.1 mrg pci_intr_establish(pc, ih, level, func, arg)
555 1.1 mrg pci_chipset_tag_t pc;
556 1.1 mrg pci_intr_handle_t ih;
557 1.1 mrg int level;
558 1.1 mrg int (*func) __P((void *));
559 1.1 mrg void *arg;
560 1.1 mrg {
561 1.1 mrg void *cookie;
562 1.1 mrg struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie;
563 1.1 mrg
564 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_establish: ih %lu; level %d", (u_long)ih, level));
565 1.34 pk cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg);
566 1.1 mrg
567 1.1 mrg DPRINTF(SPDB_INTR, ("; returning handle %p\n", cookie));
568 1.1 mrg return (cookie);
569 1.1 mrg }
570 1.1 mrg
571 1.1 mrg void
572 1.1 mrg pci_intr_disestablish(pc, cookie)
573 1.1 mrg pci_chipset_tag_t pc;
574 1.1 mrg void *cookie;
575 1.1 mrg {
576 1.1 mrg
577 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_disestablish: cookie %p\n", cookie));
578 1.1 mrg
579 1.1 mrg /* XXX */
580 1.1 mrg panic("can't disestablish PCI interrupts yet");
581 1.1 mrg }
582