pci_mace.c revision 1.3 1 /* $NetBSD: pci_mace.c,v 1.3 2004/08/30 15:05:18 drochner 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.3 2004/08/30 15:05:18 drochner 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 pcireg_t macepci_conf_read(pci_chipset_tag_t, pcitag_t, int);
84 void macepci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
85 int macepci_intr(void *);
86
87 struct pciaddr pciaddr;
88
89 bus_addr_t pciaddr_ioaddr(u_int32_t val);
90
91 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);
92
93 unsigned int ioaddr_base = 0x1000;
94 unsigned int memaddr_base = 0x80100000;
95
96 CFATTACH_DECL(macepci, sizeof(struct macepci_softc),
97 macepci_match, macepci_attach, NULL, NULL);
98
99 static int
100 macepci_match(parent, match, aux)
101 struct device *parent;
102 struct cfdata *match;
103 void *aux;
104 {
105
106 return (1);
107 }
108
109 static void
110 macepci_attach(parent, self, aux)
111 struct device *parent;
112 struct device *self;
113 void *aux;
114 {
115 struct macepci_softc *sc = (struct macepci_softc *)self;
116 pci_chipset_tag_t pc = &sc->sc_pc;
117 struct mace_attach_args *maa = aux;
118 struct pcibus_attach_args pba;
119 u_int32_t control;
120 pcitag_t devtag;
121 int device, rev;
122
123 if (bus_space_subregion(maa->maa_st, maa->maa_sh,
124 maa->maa_offset, 0, &pc->ioh) )
125 panic("macepci_attach: couldn't map");
126
127 pc->iot = maa->maa_st;
128
129 rev = bus_space_read_4(pc->iot, pc->ioh, MACEPCI_REVISION);
130 printf(": rev %d\n", rev);
131
132 pc->pc_conf_read = macepci_conf_read;
133 pc->pc_conf_write = macepci_conf_write;
134
135 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_ERROR_ADDR, 0);
136 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_ERROR_FLAGS, 0);
137
138 /* Turn on PCI error interrupts */
139 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONTROL,
140 MACE_PCI_CONTROL_SERR_ENA |
141 MACE_PCI_CONTROL_PARITY_ERR |
142 MACE_PCI_CONTROL_PARK_LIU |
143 MACE_PCI_CONTROL_OVERRUN_INT |
144 MACE_PCI_CONTROL_PARITY_INT |
145 MACE_PCI_CONTROL_SERR_INT |
146 MACE_PCI_CONTROL_IT_INT |
147 MACE_PCI_CONTROL_RE_INT |
148 MACE_PCI_CONTROL_DPED_INT |
149 MACE_PCI_CONTROL_TAR_INT |
150 MACE_PCI_CONTROL_MAR_INT);
151
152 /* Must fix up all PCI devices, ahc_pci expects proper i/o mapping */
153 for (device = 1; device < 4; device++) {
154 const struct pci_quirkdata *qd;
155 int function, nfuncs;
156 pcireg_t bhlcr, id;
157
158 devtag = pci_make_tag(pc, 0, device, 0);
159 id = pci_conf_read(pc, devtag, PCI_ID_REG);
160
161 /* Invalid vendor ID value? */
162 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
163 continue;
164 /* XXX Not invalid, but we've done this ~forever. */
165 if (PCI_VENDOR(id) == 0)
166 continue;
167
168 qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
169 bhlcr = pci_conf_read(pc, devtag, PCI_BHLC_REG);
170
171 if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
172 (qd != NULL &&
173 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
174 nfuncs = 8;
175 else
176 nfuncs = 1;
177
178 for (function = 0; function < nfuncs; function++) {
179 devtag = pci_make_tag(pc, 0, device, function);
180 id = pci_conf_read(pc, devtag, PCI_ID_REG);
181
182 /* Invalid vendor ID value? */
183 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
184 continue;
185 /* Not invalid, but we've done this ~forever */
186 if (PCI_VENDOR(id) == 0)
187 continue;
188
189 pciaddr_resource_manage(pc, devtag, NULL, NULL);
190 }
191 }
192
193 /*
194 * Enable all MACE PCI interrupts. They will be masked by
195 * the CRIME code.
196 */
197 control = bus_space_read_4(pc->iot, pc->ioh, MACEPCI_CONTROL);
198 control |= CONTROL_INT_MASK;
199 bus_space_write_4(pc->iot, pc->ioh, MACEPCI_CONTROL, control);
200
201 #if NPCI > 0
202 memset(&pba, 0, sizeof pba);
203 /*XXX*/ pba.pba_iot = SGIMIPS_BUS_SPACE_IO;
204 /*XXX*/ pba.pba_memt = SGIMIPS_BUS_SPACE_MEM;
205 pba.pba_dmat = &pci_bus_dma_tag;
206 pba.pba_dmat64 = NULL;
207 pba.pba_bus = 0;
208 pba.pba_bridgetag = NULL;
209 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
210 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
211 pba.pba_pc = pc;
212
213 #ifdef MACEPCI_IO_WAS_BUGGY
214 if (rev == 0)
215 pba.pba_flags &= ~PCI_FLAGS_IO_ENABLED; /* Buggy? */
216 #endif
217
218 cpu_intr_establish(maa->maa_intr, IPL_NONE, macepci_intr, sc);
219
220 config_found_ia(self, "pcibus", &pba, pcibusprint);
221 #endif
222 }
223
224 pcireg_t
225 macepci_conf_read(pc, tag, reg)
226 pci_chipset_tag_t pc;
227 pcitag_t tag;
228 int reg;
229 {
230 pcireg_t data;
231
232 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, (tag | reg));
233 data = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_DATA);
234 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, 0);
235
236 return data;
237 }
238
239 void
240 macepci_conf_write(pc, tag, reg, data)
241 pci_chipset_tag_t pc;
242 pcitag_t tag;
243 int reg;
244 pcireg_t data;
245 {
246 /* XXX O2 soren */
247 if (tag == 0)
248 return;
249
250 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, (tag | reg));
251 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_DATA, data);
252 bus_space_write_4(pc->iot, pc->ioh, MACE_PCI_CONFIG_ADDR, 0);
253 }
254
255
256 /*
257 * Handle PCI error interrupts.
258 */
259 int
260 macepci_intr(arg)
261 void *arg;
262 {
263 struct macepci_softc *sc = (struct macepci_softc *)arg;
264 pci_chipset_tag_t pc = &sc->sc_pc;
265 u_int32_t error, address;
266
267 error = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_ERROR_FLAGS);
268 address = bus_space_read_4(pc->iot, pc->ioh, MACE_PCI_ERROR_ADDR);
269 while (error & 0xffc00000) {
270 if (error & MACE_PERR_MASTER_ABORT) {
271 /*
272 * this seems to be a more-or-less normal error
273 * condition (e.g., "pcictl pci0 list" generates
274 * a _lot_ of these errors, so no message for now
275 * while I figure out if I missed a trick somewhere.
276 */
277 error &= ~MACE_PERR_MASTER_ABORT;
278 bus_space_write_4(pc->iot, pc->ioh,
279 MACE_PCI_ERROR_FLAGS, error);
280 }
281
282 if (error & MACE_PERR_TARGET_ABORT) {
283 printf("mace: target abort at %x\n", address);
284 error &= ~MACE_PERR_TARGET_ABORT;
285 bus_space_write_4(pc->iot, pc->ioh,
286 MACE_PCI_ERROR_FLAGS, error);
287 }
288
289 if (error & MACE_PERR_DATA_PARITY_ERR) {
290 printf("mace: parity error at %x\n", address);
291 error &= ~MACE_PERR_DATA_PARITY_ERR;
292 bus_space_write_4(pc->iot, pc->ioh,
293 MACE_PCI_ERROR_FLAGS, error);
294 }
295
296 if (error & MACE_PERR_RETRY_ERR) {
297 printf("mace: retry error at %x\n", address);
298 error &= ~MACE_PERR_RETRY_ERR;
299 bus_space_write_4(pc->iot, pc->ioh,
300 MACE_PCI_ERROR_FLAGS, error);
301 }
302
303 if (error & MACE_PERR_ILLEGAL_CMD) {
304 printf("mace: illegal command at %x\n", address);
305 error &= ~MACE_PERR_ILLEGAL_CMD;
306 bus_space_write_4(pc->iot, pc->ioh,
307 MACE_PCI_ERROR_FLAGS, error);
308 }
309
310 if (error & MACE_PERR_SYSTEM_ERR) {
311 printf("mace: system error at %x\n", address);
312 error &= ~MACE_PERR_SYSTEM_ERR;
313 bus_space_write_4(pc->iot, pc->ioh,
314 MACE_PCI_ERROR_FLAGS, error);
315 }
316
317 if (error & MACE_PERR_INTERRUPT_TEST) {
318 printf("mace: interrupt test at %x\n", address);
319 error &= ~MACE_PERR_INTERRUPT_TEST;
320 bus_space_write_4(pc->iot, pc->ioh,
321 MACE_PCI_ERROR_FLAGS, error);
322 }
323
324 if (error & MACE_PERR_PARITY_ERR) {
325 printf("mace: parity error at %x\n", address);
326 error &= ~MACE_PERR_PARITY_ERR;
327 bus_space_write_4(pc->iot, pc->ioh,
328 MACE_PCI_ERROR_FLAGS, error);
329 }
330
331 if (error & MACE_PERR_RSVD) {
332 printf("mace: reserved condition at %x\n", address);
333 error &= ~MACE_PERR_RSVD;
334 bus_space_write_4(pc->iot, pc->ioh,
335 MACE_PCI_ERROR_FLAGS, error);
336 }
337
338 if (error & MACE_PERR_OVERRUN) {
339 printf("mace: overrun at %x\n", address);
340 error &= ~MACE_PERR_OVERRUN;
341 bus_space_write_4(pc->iot, pc->ioh,
342 MACE_PCI_ERROR_FLAGS, error);
343 }
344 }
345 return 0;
346 }
347
348 /* PCI Address fixup routines */
349
350 void
351 pciaddr_resource_manage(pc, tag, func, ctx)
352 pci_chipset_tag_t pc;
353 pcitag_t tag;
354 pciaddr_resource_manage_func_t func;
355 void *ctx;
356 {
357 pcireg_t val, mask;
358 bus_addr_t addr;
359 bus_size_t size;
360 int error, mapreg, type, reg_start, reg_end, width;
361
362 val = macepci_conf_read(pc, tag, PCI_BHLC_REG);
363 switch (PCI_HDRTYPE_TYPE(val)) {
364 default:
365 printf("WARNING: unknown PCI device header.");
366 pciaddr.nbogus++;
367 return;
368 case 0:
369 reg_start = PCI_MAPREG_START;
370 reg_end = PCI_MAPREG_END;
371 break;
372 case 1: /* PCI-PCI bridge */
373 reg_start = PCI_MAPREG_START;
374 reg_end = PCI_MAPREG_PPB_END;
375 break;
376 case 2: /* PCI-CardBus bridge */
377 reg_start = PCI_MAPREG_START;
378 reg_end = PCI_MAPREG_PCB_END;
379 break;
380 }
381 error = 0;
382
383 for (mapreg = reg_start; mapreg < reg_end; mapreg += width) {
384 /* inquire PCI device bus space requirement */
385 val = macepci_conf_read(pc, tag, mapreg);
386 macepci_conf_write(pc, tag, mapreg, ~0);
387
388 mask = macepci_conf_read(pc, tag, mapreg);
389 macepci_conf_write(pc, tag, mapreg, val);
390
391 type = PCI_MAPREG_TYPE(val);
392 width = 4;
393
394 if (type == PCI_MAPREG_TYPE_MEM) {
395 size = PCI_MAPREG_MEM_SIZE(mask);
396
397 /*
398 * XXXrkb: for MEM64 BARs, to be totally kosher
399 * about the requested size, need to read mask
400 * from top 32bits of BAR and stir that into the
401 * size calculation, like so:
402 *
403 * case PCI_MAPREG_MEM_TYPE_64BIT:
404 * bar64 = pci_conf_read(pb->pc, tag, br + 4);
405 * pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
406 * mask64 = pci_conf_read(pb->pc, tag, br + 4);
407 * pci_conf_write(pb->pc, tag, br + 4, bar64);
408 * size = (u_int64_t) PCI_MAPREG_MEM64_SIZE(
409 * (((u_int64_t) mask64) << 32) | mask);
410 * width = 8;
411 *
412 * Fortunately, anything with all-zeros mask in the
413 * lower 32-bits will have size no less than 1 << 32,
414 * which we're not prepared to deal with, so I don't
415 * feel bad punting on it...
416 */
417 if (PCI_MAPREG_MEM_TYPE(val) ==
418 PCI_MAPREG_MEM_TYPE_64BIT) {
419 /*
420 * XXX We could examine the upper 32 bits
421 * XXX of the BAR here, but we are totally
422 * XXX unprepared to handle a non-zero value,
423 * XXX either here or anywhere else in the
424 * XXX sgimips code (not sure about MI code).
425 * XXX
426 * XXX So just arrange to skip the top 32
427 * XXX bits of the BAR and zero then out
428 * XXX if the BAR is in use.
429 */
430 width = 8;
431
432 if (size != 0)
433 macepci_conf_write(pc, tag,
434 mapreg + 4, 0);
435 }
436 } else {
437 /*
438 * Upper 16 bits must be one. Devices may hardwire
439 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
440 */
441 mask |= 0xffff0000;
442 size = PCI_MAPREG_IO_SIZE(mask);
443 }
444
445 if (size == 0) /* unused register */
446 continue;
447
448 addr = pciaddr_ioaddr(val);
449
450 /* reservation/allocation phase */
451 error += pciaddr_do_resource_allocate(pc, tag, mapreg,
452 ctx, type, &addr, size);
453
454 #if 0
455 PCIBIOS_PRINTV(("\n\t%02xh %s 0x%08x 0x%08x",
456 mapreg, type ? "port" : "mem ",
457 (unsigned int)addr, (unsigned int)size));
458 #endif
459 }
460
461 /* enable/disable PCI device */
462 val = macepci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
463
464 if (error == 0)
465 val |= (PCI_COMMAND_IO_ENABLE |
466 PCI_COMMAND_MEM_ENABLE |
467 PCI_COMMAND_MASTER_ENABLE |
468 PCI_COMMAND_SPECIAL_ENABLE |
469 PCI_COMMAND_INVALIDATE_ENABLE |
470 PCI_COMMAND_PARITY_ENABLE);
471 else
472 val &= ~(PCI_COMMAND_IO_ENABLE |
473 PCI_COMMAND_MEM_ENABLE |
474 PCI_COMMAND_MASTER_ENABLE);
475
476 macepci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, val);
477
478 if (error)
479 pciaddr.nbogus++;
480 }
481
482 bus_addr_t
483 pciaddr_ioaddr(val)
484 u_int32_t val;
485 {
486
487 return ((PCI_MAPREG_TYPE(val) == PCI_MAPREG_TYPE_MEM) ?
488 PCI_MAPREG_MEM_ADDR(val) : PCI_MAPREG_IO_ADDR(val));
489 }
490
491 int
492 pciaddr_do_resource_allocate(pc, tag, mapreg, ctx, type, addr, size)
493 pci_chipset_tag_t pc;
494 pcitag_t tag;
495 void *ctx;
496 int mapreg, type;
497 bus_addr_t *addr;
498 bus_size_t size;
499 {
500
501 switch (type) {
502 case PCI_MAPREG_TYPE_IO:
503 *addr = ioaddr_base;
504 ioaddr_base += PAGE_ALIGN(size);
505 break;
506
507 case PCI_MAPREG_TYPE_MEM:
508 *addr = memaddr_base;
509 memaddr_base += MEG_ALIGN(size);
510 break;
511
512 default:
513 PCIBIOS_PRINTV(("attempt to remap unknown region (addr 0x%lx, "
514 "size 0x%lx, type %d)\n", *addr, size, type));
515 return 0;
516 }
517
518
519 /* write new address to PCI device configuration header */
520 macepci_conf_write(pc, tag, mapreg, *addr);
521
522 /* check */
523 #ifdef PCIBIOSVERBOSE
524 if (!pcibiosverbose)
525 #endif
526 {
527 printf("pci_addr_fixup: ");
528 pciaddr_print_devid(pc, tag);
529 }
530 if (pciaddr_ioaddr(macepci_conf_read(pc, tag, mapreg)) != *addr) {
531 macepci_conf_write(pc, tag, mapreg, 0); /* clear */
532 printf("fixup failed. (new address=%#x)\n", (unsigned)*addr);
533 return (1);
534 }
535 #ifdef PCIBIOSVERBOSE
536 if (!pcibiosverbose)
537 #endif
538 printf("new address 0x%08x (size 0x%x)\n", (unsigned)*addr,
539 (unsigned)size);
540
541 return (0);
542 }
543
544 void
545 pciaddr_print_devid(pc, tag)
546 pci_chipset_tag_t pc;
547 pcitag_t tag;
548 {
549 int bus, device, function;
550 pcireg_t id;
551
552 id = macepci_conf_read(pc, tag, PCI_ID_REG);
553 pci_decompose_tag(pc, tag, &bus, &device, &function);
554 printf("%03d:%02d:%d 0x%04x 0x%04x ", bus, device, function,
555 PCI_VENDOR(id), PCI_PRODUCT(id));
556 }
557
558