pci_machdep.c revision 1.16 1 /* $NetBSD: pci_machdep.c,v 1.16 1999/02/19 20:57:15 leo Exp $ */
2
3 /*
4 * Copyright (c) 1996 Leo Weppelman. All rights reserved.
5 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles M. Hannum. 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 by Charles M. Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_kern.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47
48 #include <machine/cpu.h>
49 #include <machine/iomap.h>
50 #include <machine/mfp.h>
51 #include <machine/bswap.h>
52 #include <machine/bus.h>
53
54 #include <atari/atari/device.h>
55
56 /*
57 * Sizes of pci memory and I/O area.
58 */
59 #define PCI_MEM_END 0x10000000 /* 256 MByte */
60 #define PCI_IO_END 0x10000000 /* 256 MByte */
61
62 /*
63 * We preserve some space at the begin of the pci area for 32BIT_1M
64 * devices and standard vga.
65 */
66 #define PCI_MEM_START 0x00100000 /* 1 MByte */
67 #define PCI_IO_START 0x00004000 /* 16 kByte (some PCI cards allow only
68 I/O addresses up to 0xffff) */
69
70 /*
71 * Struct to hold the memory and I/O datas of the pci devices
72 */
73 struct pci_memreg {
74 LIST_ENTRY(pci_memreg) link;
75 int dev;
76 pcitag_t tag;
77 pcireg_t reg, address, mask;
78 u_int32_t size;
79 u_int32_t csr;
80 };
81
82 typedef LIST_HEAD(pci_memreg_head, pci_memreg) PCI_MEMREG;
83
84 int pcibusprint __P((void *auxp, const char *));
85 int pcibusmatch __P((struct device *, struct cfdata *, void *));
86 void pcibusattach __P((struct device *, struct device *, void *));
87
88 static void enable_pci_devices __P((void));
89 static void insert_into_list __P((PCI_MEMREG *head, struct pci_memreg *elem));
90 static int overlap_pci_areas __P((struct pci_memreg *p,
91 struct pci_memreg *self, u_int addr, u_int size, u_int what));
92 static int pci_config_offset __P((pcitag_t));
93
94 struct cfattach pcibus_ca = {
95 sizeof(struct device), pcibusmatch, pcibusattach
96 };
97
98 int
99 pcibusmatch(pdp, cfp, auxp)
100 struct device *pdp;
101 struct cfdata *cfp;
102 void *auxp;
103 {
104 if(atari_realconfig == 0)
105 return (0);
106 if (strcmp((char *)auxp, "pcibus") || cfp->cf_unit != 0)
107 return(0);
108 return(machineid & ATARI_HADES ? 1 : 0);
109 }
110
111 void
112 pcibusattach(pdp, dp, auxp)
113 struct device *pdp, *dp;
114 void *auxp;
115 {
116 struct pcibus_attach_args pba;
117 bus_space_tag_t leb_alloc_bus_space_tag __P((void));
118
119
120 enable_pci_devices();
121
122 pba.pba_busname = "pci";
123 pba.pba_pc = NULL;
124 pba.pba_bus = 0;
125 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
126 pba.pba_dmat = BUS_PCI_DMA_TAG;
127 pba.pba_iot = leb_alloc_bus_space_tag();
128 pba.pba_memt = leb_alloc_bus_space_tag();
129 if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
130 printf("leb_alloc_bus_space_tag failed!\n");
131 return;
132 }
133 pba.pba_iot->base = PCI_IO_PHYS;
134 pba.pba_memt->base = PCI_MEM_PHYS;
135
136 MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
137
138 printf("\n");
139
140 config_found(dp, &pba, pcibusprint);
141 }
142
143 int
144 pcibusprint(auxp, name)
145 void *auxp;
146 const char *name;
147 {
148 if(name == NULL)
149 return(UNCONF);
150 return(QUIET);
151 }
152
153 void
154 pci_attach_hook(parent, self, pba)
155 struct device *parent, *self;
156 struct pcibus_attach_args *pba;
157 {
158 }
159
160 /*
161 * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
162 * We only disable all devices here. Memory and I/O enabling is done
163 * later at pcibusattach.
164 */
165 void
166 init_pci_bus()
167 {
168 pci_chipset_tag_t pc = NULL; /* XXX */
169 pcitag_t tag;
170 pcireg_t csr;
171 int device, id, maxndevs;
172
173 tag = 0;
174 id = 0;
175
176 maxndevs = pci_bus_maxdevs(pc, 0);
177
178 for (device = 0; device < maxndevs; device++) {
179
180 tag = pci_make_tag(pc, 0, device, 0);
181 id = pci_conf_read(pc, tag, PCI_ID_REG);
182 if (id == 0 || id == 0xffffffff)
183 continue;
184
185 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
186 csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
187 csr &= ~PCI_COMMAND_MASTER_ENABLE;
188 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
189 }
190 }
191
192 /*
193 * insert a new element in an existing list that the ID's (size in struct
194 * pci_memreg) are sorted.
195 */
196 static void
197 insert_into_list(head, elem)
198 PCI_MEMREG *head;
199 struct pci_memreg *elem;
200 {
201 struct pci_memreg *p, *q;
202
203 p = LIST_FIRST(head);
204 q = NULL;
205
206 for (; p != NULL && p->size < elem->size; q = p, p = LIST_NEXT(p, link));
207
208 if (q == NULL) {
209 LIST_INSERT_HEAD(head, elem, link);
210 } else {
211 LIST_INSERT_AFTER(q, elem, link);
212 }
213 }
214
215 /*
216 * Test if a new selected area overlaps with an already (probably preselected)
217 * pci area.
218 */
219 static int
220 overlap_pci_areas(p, self, addr, size, what)
221 struct pci_memreg *p, *self;
222 u_int addr, size, what;
223 {
224 struct pci_memreg *q;
225
226 if (p == NULL)
227 return 0;
228
229 q = p;
230 while (q != NULL) {
231 if ((q != self) && (q->csr & what)) {
232 if ((addr >= q->address) && (addr < (q->address + q->size))) {
233 #ifdef DEBUG_PCI_MACHDEP
234 printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
235 self->dev, self->reg, q->dev, q->reg);
236 #endif
237 return 1;
238 }
239 if ((q->address >= addr) && (q->address < (addr + size))) {
240 #ifdef DEBUG_PCI_MACHDEP
241 printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
242 self->dev, self->reg, q->dev, q->reg);
243 #endif
244 return 1;
245 }
246 }
247 q = LIST_NEXT(q, link);
248 }
249 return 0;
250 }
251
252 /*
253 * Enable memory and I/O on pci devices. Care about already enabled devices
254 * (probabaly by the console driver).
255 *
256 * The idea behind the following code is:
257 * We build a by sizes sorted list of the requirements of the different
258 * pci devices. After that we choose the start addresses of that areas
259 * in such a way that they are placed as closed as possible together.
260 */
261 static void
262 enable_pci_devices()
263 {
264 PCI_MEMREG memlist;
265 PCI_MEMREG iolist;
266 struct pci_memreg *p, *q;
267 int dev, reg, id, class;
268 pcitag_t tag;
269 pcireg_t csr, address, mask;
270 pci_chipset_tag_t pc;
271 int sizecnt, membase_1m;
272
273 pc = 0;
274 csr = 0;
275 tag = 0;
276
277 LIST_INIT(&memlist);
278 LIST_INIT(&iolist);
279
280 /*
281 * first step: go through all devices and gather memory and I/O
282 * sizes
283 */
284 for (dev = 0; dev < pci_bus_maxdevs(pc,0); dev++) {
285
286 tag = pci_make_tag(pc, 0, dev, 0);
287 id = pci_conf_read(pc, tag, PCI_ID_REG);
288 if (id == 0 || id == 0xffffffff)
289 continue;
290
291 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
292
293 /*
294 * special case: if a display card is found and memory is enabled
295 * preserve 128k at 0xa0000 as vga memory.
296 */
297 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
298 switch (PCI_CLASS(class)) {
299 case PCI_CLASS_PREHISTORIC:
300 case PCI_CLASS_DISPLAY:
301 if (csr & (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
302 p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
303 M_TEMP, M_WAITOK);
304 memset(p, '\0', sizeof(struct pci_memreg));
305 p->dev = dev;
306 p->csr = csr;
307 p->tag = tag;
308 p->reg = 0; /* there is no register about this */
309 p->size = 0x20000; /* 128kByte */
310 p->mask = 0xfffe0000;
311 p->address = 0xa0000;
312
313 insert_into_list(&memlist, p);
314 }
315 }
316
317 for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
318
319 address = pci_conf_read(pc, tag, reg);
320 pci_conf_write(pc, tag, reg, 0xffffffff);
321 mask = pci_conf_read(pc, tag, reg);
322 pci_conf_write(pc, tag, reg, address);
323 if (mask == 0)
324 continue; /* Register unused */
325
326 p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
327 M_TEMP, M_WAITOK);
328 memset(p, '\0', sizeof(struct pci_memreg));
329 p->dev = dev;
330 p->csr = csr;
331 p->tag = tag;
332 p->reg = reg;
333 p->mask = mask;
334 p->address = 0;
335
336 if (mask & PCI_MAPREG_TYPE_IO) {
337 p->size = PCI_MAPREG_IO_SIZE(mask);
338
339 /*
340 * if I/O is already enabled (probably by the console driver)
341 * save the address in order to take care about it later.
342 */
343 if (csr & PCI_COMMAND_IO_ENABLE)
344 p->address = address;
345
346 insert_into_list(&iolist, p);
347 } else {
348 p->size = PCI_MAPREG_MEM_SIZE(mask);
349
350 /*
351 * if memory is already enabled (probably by the console driver)
352 * save the address in order to take care about it later.
353 */
354 if (csr & PCI_COMMAND_MEM_ENABLE)
355 p->address = address;
356
357 insert_into_list(&memlist, p);
358
359 if (PCI_MAPREG_MEM_TYPE(mask) == PCI_MAPREG_MEM_TYPE_64BIT)
360 reg++;
361 }
362 }
363
364 /*
365 * Both interrupt pin & line are set to the device (== slot)
366 * number. This makes sense on the atari because the
367 * individual slots are hard-wired to a specific MFP-pin.
368 */
369 csr = (dev << PCI_INTERRUPT_PIN_SHIFT);
370 csr |= (dev << PCI_INTERRUPT_LINE_SHIFT);
371 pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
372 }
373
374 /*
375 * second step: calculate the memory and I/O adresses beginning from
376 * PCI_MEM_START and PCI_IO_START. Care about already mapped areas.
377 *
378 * beginn with memory list
379 */
380
381 address = PCI_MEM_START;
382 sizecnt = 0;
383 membase_1m = 0;
384 p = LIST_FIRST(&memlist);
385 while (p != NULL) {
386 if (!(p->csr & PCI_COMMAND_MEM_ENABLE)) {
387 if (PCI_MAPREG_MEM_TYPE(p->mask) == PCI_MAPREG_MEM_TYPE_32BIT_1M) {
388 if (p->size > membase_1m)
389 membase_1m = p->size;
390 do {
391 p->address = membase_1m;
392 membase_1m += p->size;
393 } while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
394 p->size, PCI_COMMAND_MEM_ENABLE));
395 if (membase_1m > 0x00100000) {
396 /*
397 * Should we panic here?
398 */
399 printf("\npcibus0: dev %d reg %d: memory not configured",
400 p->dev, p->reg);
401 p->reg = 0;
402 }
403 } else {
404
405 if (sizecnt && (p->size > sizecnt))
406 sizecnt = ((p->size + sizecnt) & p->mask) &
407 PCI_MAPREG_MEM_ADDR_MASK;
408 if (sizecnt > address) {
409 address = sizecnt;
410 sizecnt = 0;
411 }
412
413 do {
414 p->address = address + sizecnt;
415 sizecnt += p->size;
416 } while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
417 p->size, PCI_COMMAND_MEM_ENABLE));
418
419 if ((address + sizecnt) > PCI_MEM_END) {
420 /*
421 * Should we panic here?
422 */
423 printf("\npcibus0: dev %d reg %d: memory not configured",
424 p->dev, p->reg);
425 p->reg = 0;
426 }
427 }
428 if (p->reg > 0) {
429 pci_conf_write(pc, p->tag, p->reg, p->address);
430 csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
431 csr |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
432 pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
433 }
434 }
435 p = LIST_NEXT(p, link);
436 }
437
438 /*
439 * now the I/O list
440 */
441
442 address = PCI_IO_START;
443 sizecnt = 0;
444 p = LIST_FIRST(&iolist);
445 while (p != NULL) {
446 if (!(p->csr & PCI_COMMAND_IO_ENABLE)) {
447
448 if (sizecnt && (p->size > sizecnt))
449 sizecnt = ((p->size + sizecnt) & p->mask) &
450 PCI_MAPREG_IO_ADDR_MASK;
451 if (sizecnt > address) {
452 address = sizecnt;
453 sizecnt = 0;
454 }
455
456 do {
457 p->address = address + sizecnt;
458 sizecnt += p->size;
459 } while (overlap_pci_areas(LIST_FIRST(&iolist), p, p->address,
460 p->size, PCI_COMMAND_IO_ENABLE));
461
462 if ((address + sizecnt) > PCI_IO_END) {
463 /*
464 * Should we panic here?
465 */
466 printf("\npcibus0: dev %d reg %d: io not configured",
467 p->dev, p->reg);
468 } else {
469 pci_conf_write(pc, p->tag, p->reg, p->address);
470 csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
471 csr |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
472 pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
473 }
474 }
475 p = LIST_NEXT(p, link);
476 }
477
478 #ifdef DEBUG_PCI_MACHDEP
479 printf("\nI/O List:\n");
480 p = LIST_FIRST(&iolist);
481
482 while (p != NULL) {
483 printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
484 p->reg, p->size, p->address);
485 p = LIST_NEXT(p, link);
486 }
487 printf("\nMemlist:");
488 p = LIST_FIRST(&memlist);
489
490 while (p != NULL) {
491 printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
492 p->reg, p->size, p->address);
493 p = LIST_NEXT(p, link);
494 }
495 #endif
496
497 /*
498 * Free the lists
499 */
500 p = LIST_FIRST(&iolist);
501 while (p != NULL) {
502 q = p;
503 LIST_REMOVE(q, link);
504 free(p, M_WAITOK);
505 p = LIST_FIRST(&iolist);
506 }
507 p = LIST_FIRST(&memlist);
508 while (p != NULL) {
509 q = p;
510 LIST_REMOVE(q, link);
511 free(p, M_WAITOK);
512 p = LIST_FIRST(&memlist);
513 }
514 }
515
516 /*
517 * Atari_init.c maps the config areas NBPG bytes apart....
518 */
519 static int pci_config_offset(tag)
520 pcitag_t tag;
521 {
522 int device;
523
524 device = (tag >> 11) & 0x1f;
525 return(device * NBPG);
526 }
527
528 int
529 pci_bus_maxdevs(pc, busno)
530 pci_chipset_tag_t pc;
531 int busno;
532 {
533 return (4);
534 }
535
536 pcitag_t
537 pci_make_tag(pc, bus, device, function)
538 pci_chipset_tag_t pc;
539 int bus, device, function;
540 {
541 return ((bus << 16) | (device << 11) | (function << 8));
542 }
543
544 pcireg_t
545 pci_conf_read(pc, tag, reg)
546 pci_chipset_tag_t pc;
547 pcitag_t tag;
548 int reg;
549 {
550 u_long data;
551
552 data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
553 return (bswap32(data));
554 }
555
556 void
557 pci_conf_write(pc, tag, reg, data)
558 pci_chipset_tag_t pc;
559 pcitag_t tag;
560 int reg;
561 pcireg_t data;
562 {
563 *((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
564 = bswap32(data);
565 }
566
567 int
568 pci_intr_map(pc, intrtag, pin, line, ihp)
569 pci_chipset_tag_t pc;
570 pcitag_t intrtag;
571 int pin, line;
572 pci_intr_handle_t *ihp;
573 {
574 /*
575 * According to the PCI-spec, 255 means `unknown' or `no connection'.
576 * Interpret this as 'no interrupt assigned'.
577 */
578 if (line == 255) {
579 *ihp = -1;
580 return 1;
581 }
582
583 /*
584 * Values are pretty useless because the on the Hades all interrupt
585 * lines for a card are tied together and hardwired to the TT-MFP
586 * I/O port.
587 */
588 *ihp = line;
589 return 0;
590 }
591
592 const char *
593 pci_intr_string(pc, ih)
594 pci_chipset_tag_t pc;
595 pci_intr_handle_t ih;
596 {
597 static char irqstr[8]; /* 4 + 2 + NULL + sanity */
598
599 if (ih == -1)
600 panic("pci_intr_string: bogus handle 0x%x\n", ih);
601
602 sprintf(irqstr, "irq %d", ih);
603 return (irqstr);
604
605 }
606
607 /*
608 * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
609 * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
610 * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
611 * to the slot position.
612 */
613 static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
614
615 static int iifun __P((int, int));
616
617 static int
618 iifun(slot, sr)
619 int slot;
620 int sr;
621 {
622 pci_intr_info_t *iinfo_p;
623 int s;
624
625 iinfo_p = &iinfo[slot];
626
627 /*
628 * Disable the interrupts
629 */
630 MFP2->mf_imrb &= ~iinfo_p->imask;
631
632 if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
633 /*
634 * We're running at a too high priority now.
635 */
636 add_sicallback((si_farg)iifun, (void*)slot, 0);
637 }
638 else {
639 s = splx(iinfo_p->ipl);
640 (void) (iinfo_p->ifunc)(iinfo_p->iarg);
641 splx(s);
642
643 /*
644 * Re-enable interrupts after handling
645 */
646 MFP2->mf_imrb |= iinfo_p->imask;
647 }
648 return 1;
649 }
650
651 void *
652 pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
653 pci_chipset_tag_t pc;
654 pci_intr_handle_t ih;
655 int level;
656 int (*ih_fun) __P((void *));
657 void *ih_arg;
658 {
659 pci_intr_info_t *iinfo_p;
660 struct intrhand *ihand;
661 int slot;
662
663 slot = ih;
664 iinfo_p = &iinfo[slot];
665
666 if (iinfo_p->ipl > 0)
667 panic("pci_intr_establish: interrupt was already established\n");
668
669 ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
670 (hw_ifun_t)iifun, (void *)slot);
671 if (ihand != NULL) {
672 iinfo_p->ipl = level;
673 iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
674 iinfo_p->ifunc = ih_fun;
675 iinfo_p->iarg = ih_arg;
676 iinfo_p->ihand = ihand;
677
678 /*
679 * Enable (unmask) the interrupt
680 */
681 MFP2->mf_imrb |= iinfo_p->imask;
682 MFP2->mf_ierb |= iinfo_p->imask;
683 return(iinfo_p);
684 }
685 return NULL;
686 }
687
688 void
689 pci_intr_disestablish(pc, cookie)
690 pci_chipset_tag_t pc;
691 void *cookie;
692 {
693 pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
694
695 if (iinfo->ipl < 0)
696 panic("pci_intr_disestablish: interrupt was not established\n");
697
698 MFP2->mf_imrb &= ~iinfo->imask;
699 MFP2->mf_ierb &= ~iinfo->imask;
700 (void) intr_disestablish(iinfo_p->ihand);
701 iinfo_p->ipl = -1;
702 }
703