pci_machdep.c revision 1.41 1 /* $NetBSD: pci_machdep.c,v 1.41 2004/01/21 07:16:07 petrov 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.41 2004/01/21 07:16:07 petrov 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_bus_frequency(int node);
79 static int pci_find_ino(struct pci_attach_args *, pci_intr_handle_t *);
80
81 static pcitag_t
82 ofpci_make_tag(pci_chipset_tag_t pc, int node, int b, int d, int f)
83 {
84 pcitag_t tag;
85
86 tag = PCITAG_CREATE(node, b, d, f);
87
88 /* Enable all the different spaces for this device */
89 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
90 PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_MASTER_ENABLE|
91 PCI_COMMAND_IO_ENABLE);
92 return (tag);
93 }
94
95 /*
96 * functions provided to the MI code.
97 */
98
99 void
100 pci_attach_hook(parent, self, pba)
101 struct device *parent;
102 struct device *self;
103 struct pcibus_attach_args *pba;
104 {
105 }
106
107 int
108 pci_bus_maxdevs(pc, busno)
109 pci_chipset_tag_t pc;
110 int busno;
111 {
112
113 return 32;
114 }
115
116 pcitag_t
117 pci_make_tag(pc, b, d, f)
118 pci_chipset_tag_t pc;
119 int b;
120 int d;
121 int f;
122 {
123 struct psycho_pbm *pp = pc->cookie;
124 struct ofw_pci_register reg;
125 pcitag_t tag;
126 int (*valid) __P((void *));
127 int busrange[2];
128 int node, len;
129 #ifdef DEBUG
130 char name[80];
131 memset(name, 0, sizeof(name));
132 #endif
133
134 /*
135 * Refer to the PCI/CardBus bus node first.
136 * It returns a tag if node is present and bus is valid.
137 */
138 if (0 <= b && b < 256) {
139 node = (*pp->pp_busnode)[b].node;
140 valid = (*pp->pp_busnode)[b].valid;
141 if (node != 0 && d == 0 &&
142 (valid == NULL || (*valid)((*pp->pp_busnode)[b].arg)))
143 return ofpci_make_tag(pc, node, b, d, f);
144 }
145
146 /*
147 * Hunt for the node that corresponds to this device
148 *
149 * We could cache this info in an array in the parent
150 * device... except then we have problems with devices
151 * attached below pci-pci bridges, and we would need to
152 * add special code to the pci-pci bridge to cache this
153 * info.
154 */
155
156 tag = PCITAG_CREATE(-1, b, d, f);
157 node = pc->rootnode;
158 /*
159 * First make sure we're on the right bus. If our parent
160 * has a bus-range property and we're not in the range,
161 * then we're obviously on the wrong bus. So go up one
162 * level.
163 */
164 #ifdef DEBUG
165 if (sparc_pci_debug & SPDB_PROBE) {
166 OF_getprop(node, "name", &name, sizeof(name));
167 printf("curnode %x %s\n", node, name);
168 }
169 #endif
170 #if 0
171 while ((OF_getprop(OF_parent(node), "bus-range", (void *)&busrange,
172 sizeof(busrange)) == sizeof(busrange)) &&
173 (b < busrange[0] || b > busrange[1])) {
174 /* Out of range, go up one */
175 node = OF_parent(node);
176 #ifdef DEBUG
177 if (sparc_pci_debug & SPDB_PROBE) {
178 OF_getprop(node, "name", &name, sizeof(name));
179 printf("going up to node %x %s\n", node, name);
180 }
181 #endif
182 }
183 #endif
184 /*
185 * Now traverse all peers until we find the node or we find
186 * the right bridge.
187 *
188 * XXX We go up one and down one to make sure nobody's missed.
189 * but this should not be necessary.
190 */
191 for (node = ((node)); node; node = OF_peer(node)) {
192
193 #ifdef DEBUG
194 if (sparc_pci_debug & SPDB_PROBE) {
195 OF_getprop(node, "name", &name, sizeof(name));
196 printf("checking node %x %s\n", node, name);
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 = OF_child(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, name);
215 }
216 #endif
217 }
218 #endif
219 /*
220 * We only really need the first `reg' property.
221 *
222 * For simplicity, we'll query the `reg' when we
223 * need it. Otherwise we could malloc() it, but
224 * that gets more complicated.
225 */
226 len = OF_getproplen(node, "reg");
227 if (len < sizeof(reg))
228 continue;
229 if (OF_getprop(node, "reg", (void *)®, sizeof(reg)) != len)
230 panic("pci_probe_bus: OF_getprop len botch");
231
232 if (b != OFW_PCI_PHYS_HI_BUS(reg.phys_hi))
233 continue;
234 if (d != OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi))
235 continue;
236 if (f != OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi))
237 continue;
238
239 /* Got a match */
240 tag = ofpci_make_tag(pc, node, b, d, f);
241
242 return (tag);
243 }
244 /* No device found -- return a dead tag */
245 return (tag);
246 }
247
248 void
249 pci_decompose_tag(pc, tag, bp, dp, fp)
250 pci_chipset_tag_t pc;
251 pcitag_t tag;
252 int *bp, *dp, *fp;
253 {
254
255 if (bp != NULL)
256 *bp = PCITAG_BUS(tag);
257 if (dp != NULL)
258 *dp = PCITAG_DEV(tag);
259 if (fp != NULL)
260 *fp = PCITAG_FUN(tag);
261 }
262
263 static int
264 pci_bus_frequency(int node)
265 {
266 int len, bus_frequency;
267
268 len = OF_getproplen(node, "clock-frequency");
269 if (len < sizeof(bus_frequency)) {
270 DPRINTF(SPDB_PROBE,
271 ("pci_bus_frequency: clock-frequency len %d too small\n",
272 len));
273 return 33;
274 }
275 if (OF_getprop(node, "clock-frequency", &bus_frequency,
276 sizeof(bus_frequency)) != len) {
277 DPRINTF(SPDB_PROBE,
278 ("pci_bus_frequency: could not read clock-frequency\n"));
279 return 33;
280 }
281 return bus_frequency / 1000000;
282 }
283
284 int
285 pci_enumerate_bus(struct pci_softc *sc,
286 int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
287 {
288 struct ofw_pci_register reg;
289 pci_chipset_tag_t pc = sc->sc_pc;
290 pcitag_t tag;
291 pcireg_t class, csr, bhlc, ic;
292 int node, b, d, f, ret;
293 int bus_frequency, lt, cl, cacheline;
294 char name[30];
295 extern int pci_config_dump;
296
297 if (sc->sc_bridgetag)
298 node = PCITAG_NODE(*sc->sc_bridgetag);
299 else
300 node = pc->rootnode;
301
302 bus_frequency = pci_bus_frequency(node);
303
304 /*
305 * Make sure the cache line size is at least as big as the
306 * ecache line and the streaming cache (64 byte).
307 */
308 cacheline = max(cacheinfo.ec_linesize, 64);
309 KASSERT((cacheline/64)*64 == cacheline &&
310 (cacheline/cacheinfo.ec_linesize)*cacheinfo.ec_linesize == cacheline &&
311 (cacheline/4)*4 == cacheline);
312
313 /* Turn on parity for the bus. */
314 tag = ofpci_make_tag(pc, node, sc->sc_bus, 0, 0);
315 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
316 csr |= PCI_COMMAND_PARITY_ENABLE;
317 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
318
319 /*
320 * Initialize the latency timer register.
321 * The value 0x40 is from Solaris.
322 */
323 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
324 bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
325 bhlc |= 0x40 << PCI_LATTIMER_SHIFT;
326 pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
327
328 if (pci_config_dump) pci_conf_print(pc, tag, NULL);
329
330 for (node = OF_child(node); node != 0 && node != -1;
331 node = OF_peer(node)) {
332 name[0] = name[29] = 0;
333 OF_getprop(node, "name", name, sizeof(name));
334
335 if (OF_getprop(node, "class-code", &class, sizeof(class)) !=
336 sizeof(class))
337 continue;
338 if (OF_getprop(node, "reg", ®, sizeof(reg)) < sizeof(reg))
339 panic("pci_enumerate_bus: \"%s\" regs too small", name);
340
341 b = OFW_PCI_PHYS_HI_BUS(reg.phys_hi);
342 d = OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi);
343 f = OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi);
344
345 if (sc->sc_bus != b) {
346 printf("%s: WARNING: incorrect bus # for \"%s\" "
347 "(%d/%d/%d)\n", sc->sc_dev.dv_xname, name, b, d, f);
348 continue;
349 }
350
351 tag = ofpci_make_tag(pc, node, b, d, f);
352
353 /*
354 * Turn on parity and fast-back-to-back for the device.
355 */
356 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
357 if (csr & PCI_STATUS_BACKTOBACK_SUPPORT)
358 csr |= PCI_COMMAND_BACKTOBACK_ENABLE;
359 csr |= PCI_COMMAND_PARITY_ENABLE;
360 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
361
362 /*
363 * Initialize the latency timer register for busmaster
364 * devices to work properly.
365 * latency-timer = min-grant * bus-freq / 4 (from FreeBSD)
366 * Also initialize the cache line size register.
367 * Solaris anytime sets this register to the value 0x10.
368 */
369 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
370 ic = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
371
372 lt = min(PCI_MIN_GNT(ic) * bus_frequency / 4, 255);
373 if (lt == 0 || lt < PCI_LATTIMER(bhlc))
374 lt = PCI_LATTIMER(bhlc);
375
376 cl = PCI_CACHELINE(bhlc);
377 if (cl == 0)
378 cl = cacheline;
379
380 bhlc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
381 (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
382 bhlc |= (lt << PCI_LATTIMER_SHIFT) |
383 (cl << PCI_CACHELINE_SHIFT);
384 pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
385
386 ret = pci_probe_device(sc, tag, match, pap);
387 if (match != NULL && ret != 0)
388 return (ret);
389 }
390 return (0);
391 }
392
393 /* assume we are mapped little-endian/side-effect */
394 pcireg_t
395 pci_conf_read(pc, tag, reg)
396 pci_chipset_tag_t pc;
397 pcitag_t tag;
398 int reg;
399 {
400 struct psycho_pbm *pp = pc->cookie;
401 struct psycho_softc *sc = pp->pp_sc;
402 pcireg_t val = (pcireg_t)~0;
403
404 DPRINTF(SPDB_CONF, ("pci_conf_read: tag %lx reg %x ",
405 (long)tag, reg));
406 if (PCITAG_NODE(tag) != -1) {
407 DPRINTF(SPDB_CONF, ("asi=%x addr=%qx (offset=%x) ...",
408 sc->sc_configaddr._asi,
409 (long long)(sc->sc_configaddr._ptr +
410 PCITAG_OFFSET(tag) + reg),
411 (int)PCITAG_OFFSET(tag) + reg));
412
413 val = bus_space_read_4(sc->sc_configtag, sc->sc_configaddr,
414 PCITAG_OFFSET(tag) + reg);
415 }
416 #ifdef DEBUG
417 else DPRINTF(SPDB_CONF, ("pci_conf_read: bogus pcitag %x\n",
418 (int)PCITAG_OFFSET(tag)));
419 #endif
420 DPRINTF(SPDB_CONF, (" returning %08x\n", (u_int)val));
421
422 return (val);
423 }
424
425 void
426 pci_conf_write(pc, tag, reg, data)
427 pci_chipset_tag_t pc;
428 pcitag_t tag;
429 int reg;
430 pcireg_t data;
431 {
432 struct psycho_pbm *pp = pc->cookie;
433 struct psycho_softc *sc = pp->pp_sc;
434
435 DPRINTF(SPDB_CONF, ("pci_conf_write: tag %lx; reg %x; data %x; ",
436 (long)PCITAG_OFFSET(tag), reg, (int)data));
437 DPRINTF(SPDB_CONF, ("asi = %x; readaddr = %qx (offset = %x)\n",
438 sc->sc_configaddr._asi,
439 (long long)(sc->sc_configaddr._ptr + PCITAG_OFFSET(tag) + reg),
440 (int)PCITAG_OFFSET(tag) + reg));
441
442 /* If we don't know it, just punt it. */
443 if (PCITAG_NODE(tag) == -1) {
444 DPRINTF(SPDB_CONF, ("pci_conf_write: bad addr"));
445 return;
446 }
447
448 bus_space_write_4(sc->sc_configtag, sc->sc_configaddr,
449 PCITAG_OFFSET(tag) + reg, data);
450 }
451
452 static int
453 pci_find_ino(pa, ihp)
454 struct pci_attach_args *pa;
455 pci_intr_handle_t *ihp;
456 {
457 struct psycho_pbm *pp = pa->pa_pc->cookie;
458 struct psycho_softc *sc = pp->pp_sc;
459 u_int dev;
460 u_int ino;
461
462 ino = *ihp;
463
464 if ((ino & ~INTMAP_PCIINT) == 0) {
465
466 if (sc->sc_mode == PSYCHO_MODE_PSYCHO &&
467 pp->pp_id == PSYCHO_PBM_B)
468 dev = pa->pa_device - 2;
469 else
470 dev = pa->pa_device - 1;
471
472 DPRINTF(SPDB_CONF, ("pci_find_ino: mode %d, pbm %d, dev %d\n",
473 sc->sc_mode, pp->pp_id, dev));
474
475 if (ino == 0 || ino > 4) {
476 u_int32_t intreg;
477
478 intreg = pci_conf_read(pa->pa_pc, pa->pa_tag,
479 PCI_INTERRUPT_REG);
480
481 ino = PCI_INTERRUPT_PIN(intreg) - 1;
482 } else
483 ino -= 1;
484
485 ino &= INTMAP_PCIINT;
486
487 ino |= sc->sc_ign;
488 ino |= ((pp->pp_id == PSYCHO_PBM_B) ? INTMAP_PCIBUS : 0);
489 ino |= (dev << 2) & INTMAP_PCISLOT;
490
491 *ihp = ino;
492 }
493
494 return (0);
495 }
496
497 /*
498 * interrupt mapping foo.
499 * XXX: how does this deal with multiple interrupts for a device?
500 */
501 int
502 pci_intr_map(pa, ihp)
503 struct pci_attach_args *pa;
504 pci_intr_handle_t *ihp;
505 {
506 pcitag_t tag = pa->pa_tag;
507 int interrupts;
508 int len, node = PCITAG_NODE(tag);
509 char devtype[30];
510
511 len = OF_getproplen(node, "interrupts");
512 if (len < sizeof(interrupts)) {
513 DPRINTF(SPDB_INTMAP,
514 ("pci_intr_map: interrupts len %d too small\n", len));
515 return (ENODEV);
516 }
517 if (OF_getprop(node, "interrupts", (void *)&interrupts,
518 sizeof(interrupts)) != len) {
519 DPRINTF(SPDB_INTMAP,
520 ("pci_intr_map: could not read interrupts\n"));
521 return (ENODEV);
522 }
523
524 if (OF_mapintr(node, &interrupts, sizeof(interrupts),
525 sizeof(interrupts)) < 0) {
526 printf("OF_mapintr failed\n");
527 pci_find_ino(pa, &interrupts);
528 }
529 /* Try to find an IPL for this type of device. */
530 if (OF_getprop(node, "device_type", &devtype, sizeof(devtype)) > 0) {
531 for (len = 0; intrmap[len].in_class; len++)
532 if (strcmp(intrmap[len].in_class, devtype) == 0) {
533 interrupts |= INTLEVENCODE(intrmap[len].in_lev);
534 break;
535 }
536 }
537
538 /* XXXX -- we use the ino. What if there is a valid IGN? */
539 *ihp = interrupts;
540 return (0);
541 }
542
543 const char *
544 pci_intr_string(pc, ih)
545 pci_chipset_tag_t pc;
546 pci_intr_handle_t ih;
547 {
548 static char str[16];
549
550 DPRINTF(SPDB_INTR, ("pci_intr_string: ih %u", ih));
551 sprintf(str, "ivec %x", ih);
552 DPRINTF(SPDB_INTR, ("; returning %s\n", str));
553
554 return (str);
555 }
556
557 const struct evcnt *
558 pci_intr_evcnt(pc, ih)
559 pci_chipset_tag_t pc;
560 pci_intr_handle_t ih;
561 {
562
563 /* XXX for now, no evcnt parent reported */
564 return NULL;
565 }
566
567 void *
568 pci_intr_establish(pc, ih, level, func, arg)
569 pci_chipset_tag_t pc;
570 pci_intr_handle_t ih;
571 int level;
572 int (*func) __P((void *));
573 void *arg;
574 {
575 void *cookie;
576 struct psycho_pbm *pp = (struct psycho_pbm *)pc->cookie;
577
578 DPRINTF(SPDB_INTR, ("pci_intr_establish: ih %lu; level %d", (u_long)ih, level));
579 cookie = bus_intr_establish(pp->pp_memt, ih, level, func, arg);
580
581 DPRINTF(SPDB_INTR, ("; returning handle %p\n", cookie));
582 return (cookie);
583 }
584
585 void
586 pci_intr_disestablish(pc, cookie)
587 pci_chipset_tag_t pc;
588 void *cookie;
589 {
590
591 DPRINTF(SPDB_INTR, ("pci_intr_disestablish: cookie %p\n", cookie));
592
593 /* XXX */
594 panic("can't disestablish PCI interrupts yet");
595 }
596