pci_mace.c revision 1.1 1 /* $NetBSD: pci_mace.c,v 1.1 2004/01/18 04:06:43 sekiya Exp $ */
2
3 /*
4 * Copyright (c) 2001,2003 Christopher Sekiya
5 * Copyright (c) 2000 Soren S. Jorvang
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the
19 * NetBSD Project. See http://www.NetBSD.org/ for
20 * information about NetBSD.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pci_mace.c,v 1.1 2004/01/18 04:06:43 sekiya Exp $");
38
39 #include <sys/param.h>
40 #include <sys/device.h>
41 #include <sys/systm.h>
42
43 #include <machine/cpu.h>
44 #include <machine/locore.h>
45 #include <machine/autoconf.h>
46 #include <machine/vmparam.h>
47 #include <machine/bus.h>
48 #include <machine/machtype.h>
49
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcidevs.h>
53
54 #include <sgimips/mace/macereg.h>
55 #include <sgimips/mace/macevar.h>
56
57 #include <sgimips/mace/pcireg_mace.h>
58 #include <sgimips/pci/pci_addr_fixup.h>
59
60 #define PCIBIOS_PRINTV(arg) \
61 do { \
62 printf arg; \
63 } while (0)
64 #define PCIBIOS_PRINTVN(n, arg) \
65 do { \
66 printf arg; \
67 } while (0)
68
69
70 #define PAGE_ALIGN(x) (((x) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
71 #define MEG_ALIGN(x) (((x) + 0x100000 - 1) & ~(0x100000 - 1))
72
73 #include "pci.h"
74
75 struct macepci_softc {
76 struct device sc_dev;
77
78 struct sgimips_pci_chipset sc_pc;
79 };
80
81 static int macepci_match(struct device *, struct cfdata *, void *);
82 static void macepci_attach(struct device *, struct device *, void *);
83 static int macepci_print(void *, const char *);
84 pcireg_t macepci_conf_read(pci_chipset_tag_t, pcitag_t, int);
85 void macepci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
86 int macepci_intr(void *);
87
88 struct pciaddr pciaddr;
89
90 bus_addr_t pciaddr_ioaddr(u_int32_t val);
91
92 int pciaddr_do_resource_allocate(pci_chipset_tag_t pc, pcitag_t tag, int mapreg, void *ctx, int type, bus_addr_t *addr, bus_size_t size);
93
94 unsigned int ioaddr_base = 0x1000;
95 unsigned int memaddr_base = 0x80100000;
96
97 CFATTACH_DECL(macepci, sizeof(struct macepci_softc),
98 macepci_match, macepci_attach, NULL, NULL);
99
100 static int
101 macepci_match(parent, match, aux)
102 struct device *parent;
103 struct cfdata *match;
104 void *aux;
105 {
106
107 if (mach_type == MACH_SGI_IP32)
108 return (1);
109
110 return (0);
111 }
112
113 static void
114 macepci_attach(parent, self, aux)
115 struct device *parent;
116 struct device *self;
117 void *aux;
118 {
119 struct macepci_softc *sc = (struct macepci_softc *)self;
120 pci_chipset_tag_t pc = &sc->sc_pc;
121 struct mace_attach_args *maa = aux;
122 struct pcibus_attach_args pba;
123 u_int32_t control;
124 pcitag_t devtag;
125 int device, rev;
126
127 if (bus_space_subregion(maa->maa_st, maa->maa_sh,
128 maa->maa_offset, 0, &pc->ioh) )
129 panic("macepci_attach: couldn't map");
130
131 pc->iot = maa->maa_st;
132
133 rev = bus_space_read_4(pc->iot, pc->ioh, MACEPCI_REVISION);
134 printf(": rev %d\n", rev);
135
136 pc->pc_conf_read = macepci_conf_read;
137 pc->pc_conf_write = macepci_conf_write;
138
139 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_ERROR_ADDR, 0);
140 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_ERROR_FLAGS, 0);
141
142 /* Turn on PCI error interrupts */
143 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONTROL,
144 MACE_PCI_CONTROL_SERR_ENA |
145 MACE_PCI_CONTROL_PARITY_ERR |
146 MACE_PCI_CONTROL_PARK_LIU |
147 MACE_PCI_CONTROL_OVERRUN_INT |
148 MACE_PCI_CONTROL_PARITY_INT |
149 MACE_PCI_CONTROL_SERR_INT |
150 MACE_PCI_CONTROL_IT_INT |
151 MACE_PCI_CONTROL_RE_INT |
152 MACE_PCI_CONTROL_DPED_INT |
153 MACE_PCI_CONTROL_TAR_INT |
154 MACE_PCI_CONTROL_MAR_INT);
155
156 /* Must fix up all PCI devices, ahc_pci expects proper i/o mapping */
157 for (device = 1; device < 4; device++) {
158 const struct pci_quirkdata *qd;
159 int function, nfuncs;
160 pcireg_t bhlcr, id;
161
162 devtag = pci_make_tag(pc, 0, device, 0);
163 id = pci_conf_read(pc, devtag, PCI_ID_REG);
164
165 /* Invalid vendor ID value? */
166 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
167 continue;
168 /* XXX Not invalid, but we've done this ~forever. */
169 if (PCI_VENDOR(id) == 0)
170 continue;
171
172 qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
173 bhlcr = pci_conf_read(pc, devtag, PCI_BHLC_REG);
174
175 if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
176 (qd != NULL &&
177 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
178 nfuncs = 8;
179 else
180 nfuncs = 1;
181
182 for (function = 0; function < nfuncs; function++) {
183 devtag = pci_make_tag(pc, 0, device, function);
184 id = pci_conf_read(pc, devtag, PCI_ID_REG);
185
186 /* Invalid vendor ID value? */
187 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
188 continue;
189 /* Not invalid, but we've done this ~forever */
190 if (PCI_VENDOR(id) == 0)
191 continue;
192
193 pciaddr_resource_manage(pc, devtag, NULL, NULL);
194 }
195 }
196
197 /*
198 * Enable all MACE PCI interrupts. They will be masked by
199 * the CRIME code.
200 */
201 control = bus_space_read_4(pc->iot, pc->ioh, MACEPCI_CONTROL);
202 control |= CONTROL_INT_MASK;
203 bus_space_write_4(pc->iot, pc->ioh, MACEPCI_CONTROL, control);
204
205 #if NPCI > 0
206 memset(&pba, 0, sizeof pba);
207 pba.pba_busname = "pci";
208 /*XXX*/ pba.pba_iot = SGIMIPS_BUS_SPACE_IO;
209 /*XXX*/ pba.pba_memt = SGIMIPS_BUS_SPACE_MEM;
210 pba.pba_dmat = &pci_bus_dma_tag;
211 pba.pba_dmat64 = NULL;
212 pba.pba_bus = 0;
213 pba.pba_bridgetag = NULL;
214 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
215 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
216 pba.pba_pc = pc;
217
218 #ifdef MACEPCI_IO_WAS_BUGGY
219 if (rev == 0)
220 pba.pba_flags &= ~PCI_FLAGS_IO_ENABLED; /* Buggy? */
221 #endif
222
223 cpu_intr_establish(maa->maa_intr, IPL_NONE, macepci_intr, sc);
224
225 config_found(self, &pba, macepci_print);
226 #endif
227 }
228
229
230 static int
231 macepci_print(aux, pnp)
232 void *aux;
233 const char *pnp;
234 {
235 struct pcibus_attach_args *pba = aux;
236
237 if (pnp != 0)
238 aprint_normal("%s at %s", pba->pba_busname, pnp);
239 else
240 aprint_normal(" bus %d", pba->pba_bus);
241
242 return UNCONF;
243 }
244
245 pcireg_t
246 macepci_conf_read(pc, tag, reg)
247 pci_chipset_tag_t pc;
248 pcitag_t tag;
249 int reg;
250 {
251 pcireg_t data;
252
253 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, (tag | reg));
254 data = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_DATA);
255 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, 0);
256
257 return data;
258 }
259
260 void
261 macepci_conf_write(pc, tag, reg, data)
262 pci_chipset_tag_t pc;
263 pcitag_t tag;
264 int reg;
265 pcireg_t data;
266 {
267 /* XXX O2 soren */
268 if (tag == 0)
269 return;
270
271 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, (tag | reg));
272 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_DATA, data);
273 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, 0);
274 }
275
276
277 /*
278 * Handle PCI error interrupts.
279 */
280 int
281 macepci_intr(arg)
282 void *arg;
283 {
284 struct macepci_softc *sc = (struct macepci_softc *)arg;
285 pci_chipset_tag_t pc = &sc->sc_pc;
286 u_int32_t error, address;
287
288 error = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_ERROR_FLAGS);
289 address = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_ERROR_ADDR);
290 while (error & 0xffc00000) {
291 if (error & MACE_PERR_MASTER_ABORT) {
292 /*
293 * this seems to be a more-or-less normal error
294 * condition (e.g., "pcictl pci0 list" generates
295 * a _lot_ of these errors, so no message for now
296 * while I figure out if I missed a trick somewhere.
297 */
298 error &= ~MACE_PERR_MASTER_ABORT;
299 bus_space_write_4(pc->iot, pc->ioh,
300 MACE_PCI_ERROR_FLAGS, error);
301 }
302
303 if (error & MACE_PERR_TARGET_ABORT) {
304 printf("mace: target abort at %x\n", address);
305 error &= ~MACE_PERR_TARGET_ABORT;
306 bus_space_write_4(pc->iot, pc->ioh,
307 MACE_PCI_ERROR_FLAGS, error);
308 }
309
310 if (error & MACE_PERR_DATA_PARITY_ERR) {
311 printf("mace: parity error at %x\n", address);
312 error &= ~MACE_PERR_DATA_PARITY_ERR;
313 bus_space_write_4(pc->iot, pc->ioh,
314 MACE_PCI_ERROR_FLAGS, error);
315 }
316
317 if (error & MACE_PERR_RETRY_ERR) {
318 printf("mace: retry error at %x\n", address);
319 error &= ~MACE_PERR_RETRY_ERR;
320 bus_space_write_4(pc->iot, pc->ioh,
321 MACE_PCI_ERROR_FLAGS, error);
322 }
323
324 if (error & MACE_PERR_ILLEGAL_CMD) {
325 printf("mace: illegal command at %x\n", address);
326 error &= ~MACE_PERR_ILLEGAL_CMD;
327 bus_space_write_4(pc->iot, pc->ioh,
328 MACE_PCI_ERROR_FLAGS, error);
329 }
330
331 if (error & MACE_PERR_SYSTEM_ERR) {
332 printf("mace: system error at %x\n", address);
333 error &= ~MACE_PERR_SYSTEM_ERR;
334 bus_space_write_4(pc->iot, pc->ioh,
335 MACE_PCI_ERROR_FLAGS, error);
336 }
337
338 if (error & MACE_PERR_INTERRUPT_TEST) {
339 printf("mace: interrupt test at %x\n", address);
340 error &= ~MACE_PERR_INTERRUPT_TEST;
341 bus_space_write_4(pc->iot, pc->ioh,
342 MACE_PCI_ERROR_FLAGS, error);
343 }
344
345 if (error & MACE_PERR_PARITY_ERR) {
346 printf("mace: parity error at %x\n", address);
347 error &= ~MACE_PERR_PARITY_ERR;
348 bus_space_write_4(pc->iot, pc->ioh,
349 MACE_PCI_ERROR_FLAGS, error);
350 }
351
352 if (error & MACE_PERR_RSVD) {
353 printf("mace: reserved condition at %x\n", address);
354 error &= ~MACE_PERR_RSVD;
355 bus_space_write_4(pc->iot, pc->ioh,
356 MACE_PCI_ERROR_FLAGS, error);
357 }
358
359 if (error & MACE_PERR_OVERRUN) {
360 printf("mace: overrun at %x\n", address);
361 error &= ~MACE_PERR_OVERRUN;
362 bus_space_write_4(pc->iot, pc->ioh,
363 MACE_PCI_ERROR_FLAGS, error);
364 }
365 }
366 return 0;
367 }
368
369 /* PCI Address fixup routines */
370
371 void
372 pciaddr_resource_manage(pc, tag, func, ctx)
373 pci_chipset_tag_t pc;
374 pcitag_t tag;
375 pciaddr_resource_manage_func_t func;
376 void *ctx;
377 {
378 pcireg_t val, mask;
379 bus_addr_t addr;
380 bus_size_t size;
381 int error, mapreg, type, reg_start, reg_end, width;
382
383 val = macepci_conf_read(pc, tag, PCI_BHLC_REG);
384 switch (PCI_HDRTYPE_TYPE(val)) {
385 default:
386 printf("WARNING: unknown PCI device header.");
387 pciaddr.nbogus++;
388 return;
389 case 0:
390 reg_start = PCI_MAPREG_START;
391 reg_end = PCI_MAPREG_END;
392 break;
393 case 1: /* PCI-PCI bridge */
394 reg_start = PCI_MAPREG_START;
395 reg_end = PCI_MAPREG_PPB_END;
396 break;
397 case 2: /* PCI-CardBus bridge */
398 reg_start = PCI_MAPREG_START;
399 reg_end = PCI_MAPREG_PCB_END;
400 break;
401 }
402 error = 0;
403
404 for (mapreg = reg_start; mapreg < reg_end; mapreg += width) {
405 /* inquire PCI device bus space requirement */
406 val = macepci_conf_read(pc, tag, mapreg);
407 macepci_conf_write(pc, tag, mapreg, ~0);
408
409 mask = macepci_conf_read(pc, tag, mapreg);
410 macepci_conf_write(pc, tag, mapreg, val);
411
412 type = PCI_MAPREG_TYPE(val);
413 width = 4;
414
415 if (type == PCI_MAPREG_TYPE_MEM) {
416 size = PCI_MAPREG_MEM_SIZE(mask);
417
418 /*
419 * XXXrkb: for MEM64 BARs, to be totally kosher
420 * about the requested size, need to read mask
421 * from top 32bits of BAR and stir that into the
422 * size calculation, like so:
423 *
424 * case PCI_MAPREG_MEM_TYPE_64BIT:
425 * bar64 = pci_conf_read(pb->pc, tag, br + 4);
426 * pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
427 * mask64 = pci_conf_read(pb->pc, tag, br + 4);
428 * pci_conf_write(pb->pc, tag, br + 4, bar64);
429 * size = (u_int64_t) PCI_MAPREG_MEM64_SIZE(
430 * (((u_int64_t) mask64) << 32) | mask);
431 * width = 8;
432 *
433 * Fortunately, anything with all-zeros mask in the
434 * lower 32-bits will have size no less than 1 << 32,
435 * which we're not prepared to deal with, so I don't
436 * feel bad punting on it...
437 */
438 if (PCI_MAPREG_MEM_TYPE(val) ==
439 PCI_MAPREG_MEM_TYPE_64BIT) {
440 /*
441 * XXX We could examine the upper 32 bits
442 * XXX of the BAR here, but we are totally
443 * XXX unprepared to handle a non-zero value,
444 * XXX either here or anywhere else in the
445 * XXX sgimips code (not sure about MI code).
446 * XXX
447 * XXX So just arrange to skip the top 32
448 * XXX bits of the BAR and zero then out
449 * XXX if the BAR is in use.
450 */
451 width = 8;
452
453 if (size != 0)
454 macepci_conf_write(pc, tag,
455 mapreg + 4, 0);
456 }
457 } else {
458 /*
459 * Upper 16 bits must be one. Devices may hardwire
460 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
461 */
462 mask |= 0xffff0000;
463 size = PCI_MAPREG_IO_SIZE(mask);
464 }
465
466 if (size == 0) /* unused register */
467 continue;
468
469 addr = pciaddr_ioaddr(val);
470
471 /* reservation/allocation phase */
472 error += pciaddr_do_resource_allocate(pc, tag, mapreg,
473 ctx, type, &addr, size);
474
475 #if 0
476 PCIBIOS_PRINTV(("\n\t%02xh %s 0x%08x 0x%08x",
477 mapreg, type ? "port" : "mem ",
478 (unsigned int)addr, (unsigned int)size));
479 #endif
480 }
481
482 /* enable/disable PCI device */
483 val = macepci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
484
485 if (error == 0)
486 val |= (PCI_COMMAND_IO_ENABLE |
487 PCI_COMMAND_MEM_ENABLE |
488 PCI_COMMAND_MASTER_ENABLE |
489 PCI_COMMAND_SPECIAL_ENABLE |
490 PCI_COMMAND_INVALIDATE_ENABLE |
491 PCI_COMMAND_PARITY_ENABLE);
492 else
493 val &= ~(PCI_COMMAND_IO_ENABLE |
494 PCI_COMMAND_MEM_ENABLE |
495 PCI_COMMAND_MASTER_ENABLE);
496
497 macepci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, val);
498
499 if (error)
500 pciaddr.nbogus++;
501 }
502
503 bus_addr_t
504 pciaddr_ioaddr(val)
505 u_int32_t val;
506 {
507
508 return ((PCI_MAPREG_TYPE(val) == PCI_MAPREG_TYPE_MEM) ?
509 PCI_MAPREG_MEM_ADDR(val) : PCI_MAPREG_IO_ADDR(val));
510 }
511
512 int
513 pciaddr_do_resource_allocate(pc, tag, mapreg, ctx, type, addr, size)
514 pci_chipset_tag_t pc;
515 pcitag_t tag;
516 void *ctx;
517 int mapreg, type;
518 bus_addr_t *addr;
519 bus_size_t size;
520 {
521
522 switch (type) {
523 case PCI_MAPREG_TYPE_IO:
524 *addr = ioaddr_base;
525 ioaddr_base += PAGE_ALIGN(size);
526 break;
527
528 case PCI_MAPREG_TYPE_MEM:
529 *addr = memaddr_base;
530 memaddr_base += MEG_ALIGN(size);
531 break;
532
533 default:
534 PCIBIOS_PRINTV(("attempt to remap unknown region (addr 0x%lx, "
535 "size 0x%lx, type %d)\n", *addr, size, type));
536 return 0;
537 }
538
539
540 /* write new address to PCI device configuration header */
541 macepci_conf_write(pc, tag, mapreg, *addr);
542
543 /* check */
544 #ifdef PCIBIOSVERBOSE
545 if (!pcibiosverbose)
546 #endif
547 {
548 printf("pci_addr_fixup: ");
549 pciaddr_print_devid(pc, tag);
550 }
551 if (pciaddr_ioaddr(macepci_conf_read(pc, tag, mapreg)) != *addr) {
552 macepci_conf_write(pc, tag, mapreg, 0); /* clear */
553 printf("fixup failed. (new address=%#x)\n", (unsigned)*addr);
554 return (1);
555 }
556 #ifdef PCIBIOSVERBOSE
557 if (!pcibiosverbose)
558 #endif
559 printf("new address 0x%08x (size 0x%x)\n", (unsigned)*addr,
560 (unsigned)size);
561
562 return (0);
563 }
564
565 void
566 pciaddr_print_devid(pc, tag)
567 pci_chipset_tag_t pc;
568 pcitag_t tag;
569 {
570 int bus, device, function;
571 pcireg_t id;
572
573 id = macepci_conf_read(pc, tag, PCI_ID_REG);
574 pci_decompose_tag(pc, tag, &bus, &device, &function);
575 printf("%03d:%02d:%d 0x%04x 0x%04x ", bus, device, function,
576 PCI_VENDOR(id), PCI_PRODUCT(id));
577 }
578
579