pci_machdep.c revision 1.30 1 1.30 thorpej /* $NetBSD: pci_machdep.c,v 1.30 2002/05/16 01:01:45 thorpej 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.1 mrg
35 1.1 mrg #ifdef DEBUG
36 1.1 mrg #define SPDB_CONF 0x01
37 1.1 mrg #define SPDB_INTR 0x04
38 1.1 mrg #define SPDB_INTMAP 0x08
39 1.1 mrg #define SPDB_INTFIX 0x10
40 1.22 eeh #define SPDB_PROBE 0x20
41 1.4 mrg int sparc_pci_debug = 0x0;
42 1.1 mrg #define DPRINTF(l, s) do { if (sparc_pci_debug & l) printf s; } while (0)
43 1.1 mrg #else
44 1.1 mrg #define DPRINTF(l, s)
45 1.1 mrg #endif
46 1.1 mrg
47 1.1 mrg #include <sys/types.h>
48 1.1 mrg #include <sys/param.h>
49 1.1 mrg #include <sys/time.h>
50 1.1 mrg #include <sys/systm.h>
51 1.1 mrg #include <sys/errno.h>
52 1.1 mrg #include <sys/device.h>
53 1.1 mrg #include <sys/malloc.h>
54 1.1 mrg
55 1.1 mrg #define _SPARC_BUS_DMA_PRIVATE
56 1.1 mrg #include <machine/bus.h>
57 1.1 mrg #include <machine/autoconf.h>
58 1.22 eeh #include <machine/openfirm.h>
59 1.1 mrg
60 1.1 mrg #include <dev/pci/pcivar.h>
61 1.1 mrg #include <dev/pci/pcireg.h>
62 1.1 mrg
63 1.19 mrg #include <dev/ofw/ofw_pci.h>
64 1.19 mrg
65 1.1 mrg #include <sparc64/dev/iommureg.h>
66 1.1 mrg #include <sparc64/dev/iommuvar.h>
67 1.1 mrg #include <sparc64/dev/psychoreg.h>
68 1.1 mrg #include <sparc64/dev/psychovar.h>
69 1.1 mrg
70 1.1 mrg /* this is a base to be copied */
71 1.1 mrg struct sparc_pci_chipset _sparc_pci_chipset = {
72 1.1 mrg NULL,
73 1.1 mrg };
74 1.2 mrg
75 1.30 thorpej static pcitag_t
76 1.30 thorpej ofpci_make_tag(pci_chipset_tag_t pc, int node, int b, int d, int f)
77 1.30 thorpej {
78 1.30 thorpej pcitag_t tag;
79 1.30 thorpej
80 1.30 thorpej tag = PCITAG_CREATE(node, b, d, f);
81 1.30 thorpej
82 1.30 thorpej /* Enable all the different spaces for this device */
83 1.30 thorpej pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
84 1.30 thorpej PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_MASTER_ENABLE|
85 1.30 thorpej PCI_COMMAND_IO_ENABLE);
86 1.30 thorpej return (tag);
87 1.30 thorpej }
88 1.30 thorpej
89 1.1 mrg /*
90 1.1 mrg * functions provided to the MI code.
91 1.1 mrg */
92 1.1 mrg
93 1.1 mrg void
94 1.1 mrg pci_attach_hook(parent, self, pba)
95 1.1 mrg struct device *parent;
96 1.1 mrg struct device *self;
97 1.1 mrg struct pcibus_attach_args *pba;
98 1.1 mrg {
99 1.30 thorpej struct sparc_pci_chipset *npc;
100 1.30 thorpej pcitag_t btag;
101 1.30 thorpej
102 1.30 thorpej /*
103 1.30 thorpej * If we're attaching at a bridge, create a new PCI chipset
104 1.30 thorpej * tag that points at the bridge's node.
105 1.30 thorpej */
106 1.30 thorpej if (pba->pba_bridgetag != NULL) {
107 1.30 thorpej btag = *pba->pba_bridgetag;
108 1.30 thorpej npc = malloc(sizeof(*npc), M_DEVBUF, M_WAITOK);
109 1.30 thorpej *npc = *pba->pba_pc;
110 1.30 thorpej npc->curnode = PCITAG_NODE(btag);
111 1.30 thorpej pba->pba_pc = npc;
112 1.30 thorpej }
113 1.1 mrg }
114 1.1 mrg
115 1.1 mrg int
116 1.1 mrg pci_bus_maxdevs(pc, busno)
117 1.1 mrg pci_chipset_tag_t pc;
118 1.1 mrg int busno;
119 1.1 mrg {
120 1.1 mrg
121 1.1 mrg return 32;
122 1.1 mrg }
123 1.19 mrg
124 1.1 mrg pcitag_t
125 1.1 mrg pci_make_tag(pc, b, d, f)
126 1.1 mrg pci_chipset_tag_t pc;
127 1.1 mrg int b;
128 1.1 mrg int d;
129 1.1 mrg int f;
130 1.1 mrg {
131 1.22 eeh struct ofw_pci_register reg;
132 1.22 eeh pcitag_t tag;
133 1.22 eeh int busrange[2];
134 1.22 eeh int node, len;
135 1.22 eeh #ifdef DEBUG
136 1.22 eeh char name[80];
137 1.22 eeh bzero(name, sizeof(name));
138 1.22 eeh #endif
139 1.1 mrg
140 1.22 eeh /*
141 1.22 eeh * Hunt for the node that corresponds to this device
142 1.22 eeh *
143 1.22 eeh * We could cache this info in an array in the parent
144 1.22 eeh * device... except then we have problems with devices
145 1.22 eeh * attached below pci-pci bridges, and we would need to
146 1.22 eeh * add special code to the pci-pci bridge to cache this
147 1.22 eeh * info.
148 1.22 eeh */
149 1.1 mrg
150 1.22 eeh tag = PCITAG_CREATE(-1, b, d, f);
151 1.22 eeh node = pc->rootnode;
152 1.22 eeh /*
153 1.22 eeh * First make sure we're on the right bus. If our parent
154 1.22 eeh * has a bus-range property and we're not in the range,
155 1.22 eeh * then we're obviously on the wrong bus. So go up one
156 1.22 eeh * level.
157 1.22 eeh */
158 1.22 eeh #ifdef DEBUG
159 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
160 1.22 eeh OF_getprop(node, "name", &name, sizeof(name));
161 1.22 eeh printf("curnode %x %s\n", node, name);
162 1.22 eeh }
163 1.22 eeh #endif
164 1.22 eeh #if 0
165 1.22 eeh while ((OF_getprop(OF_parent(node), "bus-range", (void *)&busrange,
166 1.22 eeh sizeof(busrange)) == sizeof(busrange)) &&
167 1.22 eeh (b < busrange[0] || b > busrange[1])) {
168 1.22 eeh /* Out of range, go up one */
169 1.22 eeh node = OF_parent(node);
170 1.22 eeh #ifdef DEBUG
171 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
172 1.22 eeh OF_getprop(node, "name", &name, sizeof(name));
173 1.22 eeh printf("going up to node %x %s\n", node, name);
174 1.22 eeh }
175 1.22 eeh #endif
176 1.22 eeh }
177 1.22 eeh #endif
178 1.22 eeh /*
179 1.22 eeh * Now traverse all peers until we find the node or we find
180 1.22 eeh * the right bridge.
181 1.22 eeh *
182 1.22 eeh * XXX We go up one and down one to make sure nobody's missed.
183 1.22 eeh * but this should not be necessary.
184 1.22 eeh */
185 1.22 eeh for (node = ((node)); node; node = OF_peer(node)) {
186 1.1 mrg
187 1.22 eeh #ifdef DEBUG
188 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
189 1.22 eeh OF_getprop(node, "name", &name, sizeof(name));
190 1.22 eeh printf("checking node %x %s\n", node, name);
191 1.22 eeh }
192 1.22 eeh #endif
193 1.1 mrg
194 1.22 eeh #if 1
195 1.1 mrg /*
196 1.22 eeh * Check for PCI-PCI bridges. If the device we want is
197 1.22 eeh * in the bus-range for that bridge, work our way down.
198 1.1 mrg */
199 1.22 eeh while ((OF_getprop(node, "bus-range", (void *)&busrange,
200 1.22 eeh sizeof(busrange)) == sizeof(busrange)) &&
201 1.22 eeh (b >= busrange[0] && b <= busrange[1])) {
202 1.22 eeh /* Go down 1 level */
203 1.22 eeh node = OF_child(node);
204 1.22 eeh #ifdef DEBUG
205 1.22 eeh if (sparc_pci_debug & SPDB_PROBE) {
206 1.22 eeh OF_getprop(node, "name", &name, sizeof(name));
207 1.22 eeh printf("going down to node %x %s\n",
208 1.22 eeh node, name);
209 1.22 eeh }
210 1.22 eeh #endif
211 1.1 mrg }
212 1.22 eeh #endif
213 1.22 eeh /*
214 1.22 eeh * We only really need the first `reg' property.
215 1.22 eeh *
216 1.22 eeh * For simplicity, we'll query the `reg' when we
217 1.22 eeh * need it. Otherwise we could malloc() it, but
218 1.22 eeh * that gets more complicated.
219 1.22 eeh */
220 1.22 eeh len = OF_getproplen(node, "reg");
221 1.22 eeh if (len < sizeof(reg))
222 1.22 eeh continue;
223 1.22 eeh if (OF_getprop(node, "reg", (void *)®, sizeof(reg)) != len)
224 1.22 eeh panic("pci_probe_bus: OF_getprop len botch");
225 1.22 eeh
226 1.22 eeh if (b != OFW_PCI_PHYS_HI_BUS(reg.phys_hi))
227 1.22 eeh continue;
228 1.22 eeh if (d != OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi))
229 1.22 eeh continue;
230 1.22 eeh if (f != OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi))
231 1.22 eeh continue;
232 1.22 eeh
233 1.22 eeh /* Got a match */
234 1.27 eeh tag = ofpci_make_tag(pc, node, b, d, f);
235 1.22 eeh
236 1.22 eeh return (tag);
237 1.1 mrg }
238 1.22 eeh /* No device found -- return a dead tag */
239 1.27 eeh return (tag);
240 1.28 thorpej }
241 1.28 thorpej
242 1.28 thorpej void
243 1.28 thorpej pci_decompose_tag(pc, tag, bp, dp, fp)
244 1.28 thorpej pci_chipset_tag_t pc;
245 1.28 thorpej pcitag_t tag;
246 1.28 thorpej int *bp, *dp, *fp;
247 1.28 thorpej {
248 1.28 thorpej
249 1.28 thorpej if (bp != NULL)
250 1.28 thorpej *bp = PCITAG_BUS(tag);
251 1.28 thorpej if (dp != NULL)
252 1.28 thorpej *dp = PCITAG_DEV(tag);
253 1.28 thorpej if (fp != NULL)
254 1.28 thorpej *fp = PCITAG_FUN(tag);
255 1.27 eeh }
256 1.27 eeh
257 1.30 thorpej int
258 1.30 thorpej pci_enumerate_bus(struct pci_softc *sc,
259 1.30 thorpej int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
260 1.27 eeh {
261 1.30 thorpej struct ofw_pci_register reg;
262 1.30 thorpej pci_chipset_tag_t pc = sc->sc_pc;
263 1.27 eeh pcitag_t tag;
264 1.30 thorpej pcireg_t class;
265 1.30 thorpej int node, b, d, f, ret;
266 1.30 thorpej char name[30];
267 1.30 thorpej
268 1.30 thorpej for (node = OF_child(pc->curnode); node != 0 && node != -1;
269 1.30 thorpej node = OF_peer(node)) {
270 1.30 thorpej name[0] = name[29] = 0;
271 1.30 thorpej OF_getprop(node, "name", name, sizeof(name));
272 1.27 eeh
273 1.30 thorpej if (OF_getprop(node, "class-code", &class, sizeof(class)) !=
274 1.30 thorpej sizeof(class))
275 1.30 thorpej continue;
276 1.30 thorpej if (OF_getprop(node, "reg", ®, sizeof(reg)) < sizeof(reg))
277 1.30 thorpej panic("pci_enumerate_bus: \"%s\" regs too small", name);
278 1.27 eeh
279 1.30 thorpej b = OFW_PCI_PHYS_HI_BUS(reg.phys_hi);
280 1.30 thorpej d = OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi);
281 1.30 thorpej f = OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi);
282 1.30 thorpej
283 1.30 thorpej if (sc->sc_bus != b) {
284 1.30 thorpej printf("%s: WARNING: incorrect bus # for \"%s\" "
285 1.30 thorpej "(%d/%d/%d)\n", sc->sc_dev.dv_xname, name, b, d, f);
286 1.30 thorpej continue;
287 1.30 thorpej }
288 1.27 eeh
289 1.30 thorpej tag = ofpci_make_tag(pc, node, b, d, f);
290 1.30 thorpej ret = pci_probe_device(sc, tag, match, pap);
291 1.30 thorpej if (match != NULL && ret != 0)
292 1.30 thorpej return (ret);
293 1.30 thorpej }
294 1.30 thorpej return (0);
295 1.1 mrg }
296 1.1 mrg
297 1.1 mrg /* assume we are mapped little-endian/side-effect */
298 1.1 mrg pcireg_t
299 1.1 mrg pci_conf_read(pc, tag, reg)
300 1.1 mrg pci_chipset_tag_t pc;
301 1.1 mrg pcitag_t tag;
302 1.1 mrg int reg;
303 1.1 mrg {
304 1.1 mrg struct psycho_pbm *pp = pc->cookie;
305 1.1 mrg struct psycho_softc *sc = pp->pp_sc;
306 1.22 eeh pcireg_t val = (pcireg_t)~0;
307 1.22 eeh
308 1.22 eeh DPRINTF(SPDB_CONF, ("pci_conf_read: tag %lx reg %x ",
309 1.22 eeh (long)tag, reg));
310 1.22 eeh if (PCITAG_NODE(tag) != -1) {
311 1.22 eeh DPRINTF(SPDB_CONF, ("asi=%x addr=%qx (offset=%x) ...",
312 1.26 eeh sc->sc_configaddr._asi,
313 1.26 eeh (long long)(sc->sc_configaddr._ptr +
314 1.22 eeh PCITAG_OFFSET(tag) + reg),
315 1.22 eeh (int)PCITAG_OFFSET(tag) + reg));
316 1.1 mrg
317 1.1 mrg val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr,
318 1.22 eeh PCITAG_OFFSET(tag) + reg);
319 1.1 mrg }
320 1.22 eeh #ifdef DEBUG
321 1.24 mrg else DPRINTF(SPDB_CONF, ("pci_conf_read: bogus pcitag %x\n",
322 1.24 mrg (int)PCITAG_OFFSET(tag)));
323 1.22 eeh #endif
324 1.1 mrg DPRINTF(SPDB_CONF, (" returning %08x\n", (u_int)val));
325 1.1 mrg
326 1.1 mrg return (val);
327 1.1 mrg }
328 1.1 mrg
329 1.1 mrg void
330 1.1 mrg pci_conf_write(pc, tag, reg, data)
331 1.1 mrg pci_chipset_tag_t pc;
332 1.1 mrg pcitag_t tag;
333 1.1 mrg int reg;
334 1.1 mrg pcireg_t data;
335 1.1 mrg {
336 1.1 mrg struct psycho_pbm *pp = pc->cookie;
337 1.1 mrg struct psycho_softc *sc = pp->pp_sc;
338 1.1 mrg
339 1.22 eeh DPRINTF(SPDB_CONF, ("pci_conf_write: tag %lx; reg %x; data %x; ",
340 1.22 eeh (long)PCITAG_OFFSET(tag), reg, (int)data));
341 1.1 mrg DPRINTF(SPDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n",
342 1.26 eeh sc->sc_configaddr._asi,
343 1.26 eeh (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg),
344 1.22 eeh (int)PCITAG_OFFSET(tag) + reg));
345 1.1 mrg
346 1.24 mrg /* If we don't know it, just punt it. */
347 1.24 mrg if (PCITAG_NODE(tag) == -1) {
348 1.24 mrg DPRINTF(SPDB_CONF, ("pci_conf_write: bad addr"));
349 1.24 mrg return;
350 1.24 mrg }
351 1.1 mrg
352 1.22 eeh bus_space_write_4(sc->sc_configtag, sc->sc_configaddr,
353 1.22 eeh PCITAG_OFFSET(tag) + reg, data);
354 1.1 mrg }
355 1.1 mrg
356 1.1 mrg /*
357 1.1 mrg * interrupt mapping foo.
358 1.20 mrg * XXX: how does this deal with multiple interrupts for a device?
359 1.1 mrg */
360 1.1 mrg int
361 1.16 sommerfe pci_intr_map(pa, ihp)
362 1.16 sommerfe struct pci_attach_args *pa;
363 1.1 mrg pci_intr_handle_t *ihp;
364 1.1 mrg {
365 1.22 eeh pcitag_t tag = pa->pa_tag;
366 1.22 eeh int interrupts;
367 1.22 eeh int len, node = PCITAG_NODE(tag);
368 1.22 eeh char devtype[30];
369 1.22 eeh
370 1.22 eeh len = OF_getproplen(node, "interrupts");
371 1.22 eeh if (len < sizeof(interrupts)) {
372 1.22 eeh DPRINTF(SPDB_INTMAP,
373 1.22 eeh ("pci_intr_map: interrupts len %d too small\n", len));
374 1.22 eeh return (ENODEV);
375 1.22 eeh }
376 1.22 eeh if (OF_getprop(node, "interrupts", (void *)&interrupts,
377 1.22 eeh sizeof(interrupts)) != len) {
378 1.22 eeh DPRINTF(SPDB_INTMAP,
379 1.22 eeh ("pci_intr_map: could not read interrupts\n"));
380 1.22 eeh return (ENODEV);
381 1.22 eeh }
382 1.20 mrg
383 1.22 eeh if (OF_mapintr(node, &interrupts, sizeof(interrupts),
384 1.23 eeh sizeof(interrupts)) < 0) {
385 1.22 eeh printf("OF_mapintr failed\n");
386 1.22 eeh }
387 1.22 eeh /* Try to find an IPL for this type of device. */
388 1.22 eeh if (OF_getprop(node, "device_type", &devtype, sizeof(devtype)) > 0) {
389 1.22 eeh for (len = 0; intrmap[len].in_class; len++)
390 1.22 eeh if (strcmp(intrmap[len].in_class, devtype) == 0) {
391 1.22 eeh interrupts |= INTLEVENCODE(intrmap[len].in_lev);
392 1.22 eeh break;
393 1.22 eeh }
394 1.22 eeh }
395 1.20 mrg
396 1.22 eeh /* XXXX -- we use the ino. What if there is a valid IGN? */
397 1.22 eeh *ihp = interrupts;
398 1.22 eeh return (0);
399 1.1 mrg }
400 1.1 mrg
401 1.1 mrg const char *
402 1.1 mrg pci_intr_string(pc, ih)
403 1.1 mrg pci_chipset_tag_t pc;
404 1.1 mrg pci_intr_handle_t ih;
405 1.1 mrg {
406 1.1 mrg static char str[16];
407 1.1 mrg
408 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_string: ih %u", ih));
409 1.22 eeh sprintf(str, "ivec %x", ih);
410 1.1 mrg DPRINTF(SPDB_INTR, ("; returning %s\n", str));
411 1.1 mrg
412 1.1 mrg return (str);
413 1.8 cgd }
414 1.8 cgd
415 1.8 cgd const struct evcnt *
416 1.8 cgd pci_intr_evcnt(pc, ih)
417 1.8 cgd pci_chipset_tag_t pc;
418 1.8 cgd pci_intr_handle_t ih;
419 1.8 cgd {
420 1.8 cgd
421 1.8 cgd /* XXX for now, no evcnt parent reported */
422 1.8 cgd return NULL;
423 1.1 mrg }
424 1.1 mrg
425 1.1 mrg void *
426 1.1 mrg pci_intr_establish(pc, ih, level, func, arg)
427 1.1 mrg pci_chipset_tag_t pc;
428 1.1 mrg pci_intr_handle_t ih;
429 1.1 mrg int level;
430 1.1 mrg int (*func) __P((void *));
431 1.1 mrg void *arg;
432 1.1 mrg {
433 1.1 mrg void *cookie;
434 1.1 mrg struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie;
435 1.1 mrg
436 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_establish: ih %lu; level %d", (u_long)ih, level));
437 1.13 pk cookie = bus_intr_establish(pp->pp_memt, ih, level, 0, func, arg);
438 1.1 mrg
439 1.1 mrg DPRINTF(SPDB_INTR, ("; returning handle %p\n", cookie));
440 1.1 mrg return (cookie);
441 1.1 mrg }
442 1.1 mrg
443 1.1 mrg void
444 1.1 mrg pci_intr_disestablish(pc, cookie)
445 1.1 mrg pci_chipset_tag_t pc;
446 1.1 mrg void *cookie;
447 1.1 mrg {
448 1.1 mrg
449 1.1 mrg DPRINTF(SPDB_INTR, ("pci_intr_disestablish: cookie %p\n", cookie));
450 1.1 mrg
451 1.1 mrg /* XXX */
452 1.1 mrg panic("can't disestablish PCI interrupts yet");
453 1.1 mrg }
454