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