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