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