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