initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / sparc / kernel / pcic.c
blobc982473d1be04bdf2ec978da7c15712eb8c90959
1 /*
2 * pcic.c: MicroSPARC-IIep PCI controller support
4 * Copyright (C) 1998 V. Roganov and G. Raiko
6 * Code is derived from Ultra/PCI PSYCHO controller support, see that
7 * for author info.
9 * Support for diverse IIep based platforms by Pete Zaitcev.
10 * CP-1200 by Eric Brower.
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/init.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/jiffies.h>
21 #include <asm/ebus.h>
22 #include <asm/sbus.h> /* for sanity check... */
23 #include <asm/swift.h> /* for cache flushing. */
24 #include <asm/io.h>
26 #include <linux/ctype.h>
27 #include <linux/pci.h>
28 #include <linux/time.h>
29 #include <linux/timex.h>
30 #include <linux/interrupt.h>
32 #include <asm/irq.h>
33 #include <asm/oplib.h>
34 #include <asm/pcic.h>
35 #include <asm/timer.h>
36 #include <asm/uaccess.h>
39 unsigned int pcic_pin_to_irq(unsigned int pin, char *name);
42 * I studied different documents and many live PROMs both from 2.30
43 * family and 3.xx versions. I came to the amazing conclusion: there is
44 * absolutely no way to route interrupts in IIep systems relying on
45 * information which PROM presents. We must hardcode interrupt routing
46 * schematics. And this actually sucks. -- zaitcev 1999/05/12
48 * To find irq for a device we determine which routing map
49 * is in effect or, in other words, on which machine we are running.
50 * We use PROM name for this although other techniques may be used
51 * in special cases (Gleb reports a PROMless IIep based system).
52 * Once we know the map we take device configuration address and
53 * find PCIC pin number where INT line goes. Then we may either program
54 * preferred irq into the PCIC or supply the preexisting irq to the device.
56 struct pcic_ca2irq {
57 unsigned char busno; /* PCI bus number */
58 unsigned char devfn; /* Configuration address */
59 unsigned char pin; /* PCIC external interrupt pin */
60 unsigned char irq; /* Preferred IRQ (mappable in PCIC) */
61 unsigned int force; /* Enforce preferred IRQ */
64 struct pcic_sn2list {
65 char *sysname;
66 struct pcic_ca2irq *intmap;
67 int mapdim;
71 * JavaEngine-1 apparently has different versions.
73 * According to communications with Sun folks, for P2 build 501-4628-03:
74 * pin 0 - parallel, audio;
75 * pin 1 - Ethernet;
76 * pin 2 - su;
77 * pin 3 - PS/2 kbd and mouse.
79 * OEM manual (805-1486):
80 * pin 0: Ethernet
81 * pin 1: All EBus
82 * pin 2: IGA (unused)
83 * pin 3: Not connected
84 * OEM manual says that 501-4628 & 501-4811 are the same thing,
85 * only the latter has NAND flash in place.
87 * So far unofficial Sun wins over the OEM manual. Poor OEMs...
89 static struct pcic_ca2irq pcic_i_je1a[] = { /* 501-4811-03 */
90 { 0, 0x00, 2, 12, 0 }, /* EBus: hogs all */
91 { 0, 0x01, 1, 6, 1 }, /* Happy Meal */
92 { 0, 0x80, 0, 7, 0 }, /* IGA (unused) */
95 /* XXX JS-E entry is incomplete - PCI Slot 2 address (pin 7)? */
96 static struct pcic_ca2irq pcic_i_jse[] = {
97 { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
98 { 0, 0x01, 1, 6, 0 }, /* hme */
99 { 0, 0x08, 2, 9, 0 }, /* VGA - we hope not used :) */
100 { 0, 0x10, 6, 8, 0 }, /* PCI INTA# in Slot 1 */
101 { 0, 0x18, 7, 12, 0 }, /* PCI INTA# in Slot 2, shared w. RTC */
102 { 0, 0x38, 4, 9, 0 }, /* All ISA devices. Read 8259. */
103 { 0, 0x80, 5, 11, 0 }, /* EIDE */
104 /* {0,0x88, 0,0,0} - unknown device... PMU? Probably no interrupt. */
105 { 0, 0xA0, 4, 9, 0 }, /* USB */
107 * Some pins belong to non-PCI devices, we hardcode them in drivers.
108 * sun4m timers - irq 10, 14
109 * PC style RTC - pin 7, irq 4 ?
110 * Smart card, Parallel - pin 4 shared with USB, ISA
111 * audio - pin 3, irq 5 ?
115 /* SPARCengine-6 was the original release name of CP1200.
116 * The documentation differs between the two versions
118 static struct pcic_ca2irq pcic_i_se6[] = {
119 { 0, 0x08, 0, 2, 0 }, /* SCSI */
120 { 0, 0x01, 1, 6, 0 }, /* HME */
121 { 0, 0x00, 3, 13, 0 }, /* EBus */
125 * Krups (courtesy of Varol Kaptan)
126 * No documentation available, but it was easy to guess
127 * because it was very similar to Espresso.
129 * pin 0 - kbd, mouse, serial;
130 * pin 1 - Ethernet;
131 * pin 2 - igs (we do not use it);
132 * pin 3 - audio;
133 * pin 4,5,6 - unused;
134 * pin 7 - RTC (from P2 onwards as David B. says).
136 static struct pcic_ca2irq pcic_i_jk[] = {
137 { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
138 { 0, 0x01, 1, 6, 0 }, /* hme */
142 * Several entries in this list may point to the same routing map
143 * as several PROMs may be installed on the same physical board.
145 #define SN2L_INIT(name, map) \
146 { name, map, sizeof(map)/sizeof(struct pcic_ca2irq) }
148 static struct pcic_sn2list pcic_known_sysnames[] = {
149 SN2L_INIT("SUNW,JavaEngine1", pcic_i_je1a), /* JE1, PROM 2.32 */
150 SN2L_INIT("SUNW,JS-E", pcic_i_jse), /* PROLL JavaStation-E */
151 SN2L_INIT("SUNW,SPARCengine-6", pcic_i_se6), /* SPARCengine-6/CP-1200 */
152 SN2L_INIT("SUNW,JS-NC", pcic_i_jk), /* PROLL JavaStation-NC */
153 SN2L_INIT("SUNW,JSIIep", pcic_i_jk), /* OBP JavaStation-NC */
154 { NULL, NULL, 0 }
158 * Only one PCIC per IIep,
159 * and since we have no SMP IIep, only one per system.
161 static int pcic0_up;
162 static struct linux_pcic pcic0;
164 unsigned int pcic_regs;
165 volatile int pcic_speculative;
166 volatile int pcic_trapped;
168 static void pci_do_gettimeofday(struct timeval *tv);
169 static int pci_do_settimeofday(struct timespec *tv);
171 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (((unsigned int)bus) << 16) | (((unsigned int)device_fn) << 8) | (where & ~3))
173 static int pcic_read_config_dword(unsigned int busno, unsigned int devfn,
174 int where, u32 *value)
176 struct linux_pcic *pcic;
177 unsigned long flags;
179 pcic = &pcic0;
181 local_irq_save(flags);
182 #if 0 /* does not fail here */
183 pcic_speculative = 1;
184 pcic_trapped = 0;
185 #endif
186 writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
187 #if 0 /* does not fail here */
188 nop();
189 if (pcic_trapped) {
190 local_irq_restore(flags);
191 *value = ~0;
192 return 0;
194 #endif
195 pcic_speculative = 2;
196 pcic_trapped = 0;
197 *value = readl(pcic->pcic_config_space_data + (where&4));
198 nop();
199 if (pcic_trapped) {
200 pcic_speculative = 0;
201 local_irq_restore(flags);
202 *value = ~0;
203 return 0;
205 pcic_speculative = 0;
206 local_irq_restore(flags);
207 return 0;
210 static int pcic_read_config(struct pci_bus *bus, unsigned int devfn,
211 int where, int size, u32 *val)
213 unsigned int v;
215 if (bus->number != 0) return -EINVAL;
216 switch (size) {
217 case 1:
218 pcic_read_config_dword(bus->number, devfn, where&~3, &v);
219 *val = 0xff & (v >> (8*(where & 3)));
220 return 0;
221 case 2:
222 if (where&1) return -EINVAL;
223 pcic_read_config_dword(bus->number, devfn, where&~3, &v);
224 *val = 0xffff & (v >> (8*(where & 3)));
225 return 0;
226 case 4:
227 if (where&3) return -EINVAL;
228 pcic_read_config_dword(bus->number, devfn, where&~3, val);
229 return 0;
231 return -EINVAL;
234 static int pcic_write_config_dword(unsigned int busno, unsigned int devfn,
235 int where, u32 value)
237 struct linux_pcic *pcic;
238 unsigned long flags;
240 pcic = &pcic0;
242 local_irq_save(flags);
243 writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
244 writel(value, pcic->pcic_config_space_data + (where&4));
245 local_irq_restore(flags);
246 return 0;
249 static int pcic_write_config(struct pci_bus *bus, unsigned int devfn,
250 int where, int size, u32 val)
252 unsigned int v;
254 if (bus->number != 0) return -EINVAL;
255 switch (size) {
256 case 1:
257 pcic_read_config_dword(bus->number, devfn, where&~3, &v);
258 v = (v & ~(0xff << (8*(where&3)))) |
259 ((0xff&val) << (8*(where&3)));
260 return pcic_write_config_dword(bus->number, devfn, where&~3, v);
261 case 2:
262 if (where&1) return -EINVAL;
263 pcic_read_config_dword(bus->number, devfn, where&~3, &v);
264 v = (v & ~(0xffff << (8*(where&3)))) |
265 ((0xffff&val) << (8*(where&3)));
266 return pcic_write_config_dword(bus->number, devfn, where&~3, v);
267 case 4:
268 if (where&3) return -EINVAL;
269 return pcic_write_config_dword(bus->number, devfn, where, val);
271 return -EINVAL;
274 static struct pci_ops pcic_ops = {
275 .read = pcic_read_config,
276 .write = pcic_write_config,
280 * On sparc64 pcibios_init() calls pci_controller_probe().
281 * We want PCIC probed little ahead so that interrupt controller
282 * would be operational.
284 int __init pcic_probe(void)
286 struct linux_pcic *pcic;
287 struct linux_prom_registers regs[PROMREG_MAX];
288 struct linux_pbm_info* pbm;
289 char namebuf[64];
290 int node;
291 int err;
293 if (pcic0_up) {
294 prom_printf("PCIC: called twice!\n");
295 prom_halt();
297 pcic = &pcic0;
299 node = prom_getchild (prom_root_node);
300 node = prom_searchsiblings (node, "pci");
301 if (node == 0)
302 return -ENODEV;
304 * Map in PCIC register set, config space, and IO base
306 err = prom_getproperty(node, "reg", (char*)regs, sizeof(regs));
307 if (err == 0 || err == -1) {
308 prom_printf("PCIC: Error, cannot get PCIC registers "
309 "from PROM.\n");
310 prom_halt();
313 pcic0_up = 1;
315 pcic->pcic_res_regs.name = "pcic_registers";
316 pcic->pcic_regs = (unsigned long)
317 ioremap(regs[0].phys_addr, regs[0].reg_size);
318 if (!pcic->pcic_regs) {
319 prom_printf("PCIC: Error, cannot map PCIC registers.\n");
320 prom_halt();
323 pcic->pcic_res_io.name = "pcic_io";
324 if ((pcic->pcic_io = (unsigned long)
325 ioremap(regs[1].phys_addr, 0x10000)) == 0) {
326 prom_printf("PCIC: Error, cannot map PCIC IO Base.\n");
327 prom_halt();
330 pcic->pcic_res_cfg_addr.name = "pcic_cfg_addr";
331 if ((pcic->pcic_config_space_addr = (unsigned long)
332 ioremap(regs[2].phys_addr, regs[2].reg_size * 2)) == 0) {
333 prom_printf("PCIC: Error, cannot map"
334 "PCI Configuration Space Address.\n");
335 prom_halt();
339 * Docs say three least significant bits in address and data
340 * must be the same. Thus, we need adjust size of data.
342 pcic->pcic_res_cfg_data.name = "pcic_cfg_data";
343 if ((pcic->pcic_config_space_data = (unsigned long)
344 ioremap(regs[3].phys_addr, regs[3].reg_size * 2)) == 0) {
345 prom_printf("PCIC: Error, cannot map"
346 "PCI Configuration Space Data.\n");
347 prom_halt();
350 pbm = &pcic->pbm;
351 pbm->prom_node = node;
352 prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
353 strcpy(pbm->prom_name, namebuf);
356 extern volatile int t_nmi[1];
357 extern int pcic_nmi_trap_patch[1];
359 t_nmi[0] = pcic_nmi_trap_patch[0];
360 t_nmi[1] = pcic_nmi_trap_patch[1];
361 t_nmi[2] = pcic_nmi_trap_patch[2];
362 t_nmi[3] = pcic_nmi_trap_patch[3];
363 swift_flush_dcache();
364 pcic_regs = pcic->pcic_regs;
367 prom_getstring(prom_root_node, "name", namebuf, 63); namebuf[63] = 0;
369 struct pcic_sn2list *p;
371 for (p = pcic_known_sysnames; p->sysname != NULL; p++) {
372 if (strcmp(namebuf, p->sysname) == 0)
373 break;
375 pcic->pcic_imap = p->intmap;
376 pcic->pcic_imdim = p->mapdim;
378 if (pcic->pcic_imap == NULL) {
380 * We do not panic here for the sake of embedded systems.
382 printk("PCIC: System %s is unknown, cannot route interrupts\n",
383 namebuf);
386 return 0;
389 static void __init pcic_pbm_scan_bus(struct linux_pcic *pcic)
391 struct linux_pbm_info *pbm = &pcic->pbm;
393 pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, &pcic_ops, pbm);
394 #if 0 /* deadwood transplanted from sparc64 */
395 pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
396 pci_record_assignments(pbm, pbm->pci_bus);
397 pci_assign_unassigned(pbm, pbm->pci_bus);
398 pci_fixup_irq(pbm, pbm->pci_bus);
399 #endif
403 * Main entry point from the PCI subsystem.
405 static int __init pcic_init(void)
407 struct linux_pcic *pcic;
410 * PCIC should be initialized at start of the timer.
411 * So, here we report the presence of PCIC and do some magic passes.
413 if(!pcic0_up)
414 return 0;
415 pcic = &pcic0;
418 * Switch off IOTLB translation.
420 writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE,
421 pcic->pcic_regs+PCI_DVMA_CONTROL);
424 * Increase mapped size for PCI memory space (DMA access).
425 * Should be done in that order (size first, address second).
426 * Why we couldn't set up 4GB and forget about it? XXX
428 writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
429 writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY,
430 pcic->pcic_regs+PCI_BASE_ADDRESS_0);
432 pcic_pbm_scan_bus(pcic);
434 ebus_init();
435 return 0;
438 int pcic_present(void)
440 return pcic0_up;
443 static int __init pdev_to_pnode(struct linux_pbm_info *pbm,
444 struct pci_dev *pdev)
446 struct linux_prom_pci_registers regs[PROMREG_MAX];
447 int err;
448 int node = prom_getchild(pbm->prom_node);
450 while(node) {
451 err = prom_getproperty(node, "reg",
452 (char *)&regs[0], sizeof(regs));
453 if(err != 0 && err != -1) {
454 unsigned long devfn = (regs[0].which_io >> 8) & 0xff;
455 if(devfn == pdev->devfn)
456 return node;
458 node = prom_getsibling(node);
460 return 0;
463 static inline struct pcidev_cookie *pci_devcookie_alloc(void)
465 return kmalloc(sizeof(struct pcidev_cookie), GFP_ATOMIC);
468 static void pcic_map_pci_device(struct linux_pcic *pcic,
469 struct pci_dev *dev, int node)
471 char namebuf[64];
472 unsigned long address;
473 unsigned long flags;
474 int j;
476 if (node == 0 || node == -1) {
477 strcpy(namebuf, "???");
478 } else {
479 prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
482 for (j = 0; j < 6; j++) {
483 address = dev->resource[j].start;
484 if (address == 0) break; /* are sequential */
485 flags = dev->resource[j].flags;
486 if ((flags & IORESOURCE_IO) != 0) {
487 if (address < 0x10000) {
489 * A device responds to I/O cycles on PCI.
490 * We generate these cycles with memory
491 * access into the fixed map (phys 0x30000000).
493 * Since a device driver does not want to
494 * do ioremap() before accessing PC-style I/O,
495 * we supply virtual, ready to access address.
497 * Ebus devices do not come here even if
498 * CheerIO makes a similar conversion.
499 * See ebus.c for details.
501 * Note that check_region()/request_region()
502 * work for these devices.
504 * XXX Neat trick, but it's a *bad* idea
505 * to shit into regions like that.
506 * What if we want to allocate one more
507 * PCI base address...
509 dev->resource[j].start =
510 pcic->pcic_io + address;
511 dev->resource[j].end = 1; /* XXX */
512 dev->resource[j].flags =
513 (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
514 } else {
516 * OOPS... PCI Spec allows this. Sun does
517 * not have any devices getting above 64K
518 * so it must be user with a weird I/O
519 * board in a PCI slot. We must remap it
520 * under 64K but it is not done yet. XXX
522 printk("PCIC: Skipping I/O space at 0x%lx,"
523 "this will Oops if a driver attaches;"
524 "device '%s' at %02x:%02x)\n", address,
525 namebuf, dev->bus->number, dev->devfn);
531 static void
532 pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
534 struct pcic_ca2irq *p;
535 int i, ivec;
536 char namebuf[64];
538 if (node == 0 || node == -1) {
539 strcpy(namebuf, "???");
540 } else {
541 prom_getstring(node, "name", namebuf, sizeof(namebuf));
544 if ((p = pcic->pcic_imap) == 0) {
545 dev->irq = 0;
546 return;
548 for (i = 0; i < pcic->pcic_imdim; i++) {
549 if (p->busno == dev->bus->number && p->devfn == dev->devfn)
550 break;
551 p++;
553 if (i >= pcic->pcic_imdim) {
554 printk("PCIC: device %s devfn %02x:%02x not found in %d\n",
555 namebuf, dev->bus->number, dev->devfn, pcic->pcic_imdim);
556 dev->irq = 0;
557 return;
560 i = p->pin;
561 if (i >= 0 && i < 4) {
562 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
563 dev->irq = ivec >> (i << 2) & 0xF;
564 } else if (i >= 4 && i < 8) {
565 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
566 dev->irq = ivec >> ((i-4) << 2) & 0xF;
567 } else { /* Corrupted map */
568 printk("PCIC: BAD PIN %d\n", i); for (;;) {}
570 /* P3 */ /* printk("PCIC: device %s pin %d ivec 0x%x irq %x\n", namebuf, i, ivec, dev->irq); */
573 * dev->irq=0 means PROM did not bother to program the upper
574 * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
576 if (dev->irq == 0 || p->force) {
577 if (p->irq == 0 || p->irq >= 15) { /* Corrupted map */
578 printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
580 printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
581 p->irq, p->pin, dev->bus->number, dev->devfn);
582 dev->irq = p->irq;
584 i = p->pin;
585 if (i >= 4) {
586 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
587 ivec &= ~(0xF << ((i - 4) << 2));
588 ivec |= p->irq << ((i - 4) << 2);
589 writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
590 } else {
591 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
592 ivec &= ~(0xF << (i << 2));
593 ivec |= p->irq << (i << 2);
594 writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
598 return;
602 * Normally called from {do_}pci_scan_bus...
604 void __init pcibios_fixup_bus(struct pci_bus *bus)
606 struct list_head *walk;
607 int i, has_io, has_mem;
608 unsigned int cmd;
609 struct linux_pcic *pcic;
610 /* struct linux_pbm_info* pbm = &pcic->pbm; */
611 int node;
612 struct pcidev_cookie *pcp;
614 if (!pcic0_up) {
615 printk("pcibios_fixup_bus: no PCIC\n");
616 return;
618 pcic = &pcic0;
621 * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
623 if (bus->number != 0) {
624 printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
625 return;
628 walk = &bus->devices;
629 for (walk = walk->next; walk != &bus->devices; walk = walk->next) {
630 struct pci_dev *dev = pci_dev_b(walk);
633 * Comment from i386 branch:
634 * There are buggy BIOSes that forget to enable I/O and memory
635 * access to PCI devices. We try to fix this, but we need to
636 * be sure that the BIOS didn't forget to assign an address
637 * to the device. [mj]
638 * OBP is a case of such BIOS :-)
640 has_io = has_mem = 0;
641 for(i=0; i<6; i++) {
642 unsigned long f = dev->resource[i].flags;
643 if (f & IORESOURCE_IO) {
644 has_io = 1;
645 } else if (f & IORESOURCE_MEM)
646 has_mem = 1;
648 pcic_read_config(dev->bus, dev->devfn, PCI_COMMAND, 2, &cmd);
649 if (has_io && !(cmd & PCI_COMMAND_IO)) {
650 printk("PCIC: Enabling I/O for device %02x:%02x\n",
651 dev->bus->number, dev->devfn);
652 cmd |= PCI_COMMAND_IO;
653 pcic_write_config(dev->bus, dev->devfn,
654 PCI_COMMAND, 2, cmd);
656 if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
657 printk("PCIC: Enabling memory for device %02x:%02x\n",
658 dev->bus->number, dev->devfn);
659 cmd |= PCI_COMMAND_MEMORY;
660 pcic_write_config(dev->bus, dev->devfn,
661 PCI_COMMAND, 2, cmd);
664 node = pdev_to_pnode(&pcic->pbm, dev);
665 if(node == 0)
666 node = -1;
668 /* cookies */
669 pcp = pci_devcookie_alloc();
670 pcp->pbm = &pcic->pbm;
671 pcp->prom_node = node;
672 dev->sysdata = pcp;
674 /* fixing I/O to look like memory */
675 if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
676 pcic_map_pci_device(pcic, dev, node);
678 pcic_fill_irq(pcic, dev, node);
683 * pcic_pin_to_irq() is exported to ebus.c.
685 unsigned int
686 pcic_pin_to_irq(unsigned int pin, char *name)
688 struct linux_pcic *pcic = &pcic0;
689 unsigned int irq;
690 unsigned int ivec;
692 if (pin < 4) {
693 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
694 irq = ivec >> (pin << 2) & 0xF;
695 } else if (pin < 8) {
696 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
697 irq = ivec >> ((pin-4) << 2) & 0xF;
698 } else { /* Corrupted map */
699 printk("PCIC: BAD PIN %d FOR %s\n", pin, name);
700 for (;;) {} /* XXX Cannot panic properly in case of PROLL */
702 /* P3 */ /* printk("PCIC: dev %s pin %d ivec 0x%x irq %x\n", name, pin, ivec, irq); */
703 return irq;
706 /* Makes compiler happy */
707 static volatile int pcic_timer_dummy;
709 static void pcic_clear_clock_irq(void)
711 pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
714 static irqreturn_t pcic_timer_handler (int irq, void *h, struct pt_regs *regs)
716 write_seqlock(&xtime_lock); /* Dummy, to show that we remember */
717 pcic_clear_clock_irq();
718 do_timer(regs);
719 write_sequnlock(&xtime_lock);
720 return IRQ_HANDLED;
723 #define USECS_PER_JIFFY 10000 /* We have 100HZ "standard" timer for sparc */
724 #define TICK_TIMER_LIMIT ((100*1000000/4)/100)
726 void __init pci_time_init(void)
728 struct linux_pcic *pcic = &pcic0;
729 unsigned long v;
730 int timer_irq, irq;
732 /* A hack until do_gettimeofday prototype is moved to arch specific headers
733 and btfixupped. Patch do_gettimeofday with ba pci_do_gettimeofday; nop */
734 ((unsigned int *)do_gettimeofday)[0] =
735 0x10800000 | ((((unsigned long)pci_do_gettimeofday -
736 (unsigned long)do_gettimeofday) >> 2) & 0x003fffff);
737 ((unsigned int *)do_gettimeofday)[1] = 0x01000000;
738 BTFIXUPSET_CALL(bus_do_settimeofday, pci_do_settimeofday, BTFIXUPCALL_NORM);
739 btfixup();
741 writel (TICK_TIMER_LIMIT, pcic->pcic_regs+PCI_SYS_LIMIT);
742 /* PROM should set appropriate irq */
743 v = readb(pcic->pcic_regs+PCI_COUNTER_IRQ);
744 timer_irq = PCI_COUNTER_IRQ_SYS(v);
745 writel (PCI_COUNTER_IRQ_SET(timer_irq, 0),
746 pcic->pcic_regs+PCI_COUNTER_IRQ);
747 irq = request_irq(timer_irq, pcic_timer_handler,
748 (SA_INTERRUPT | SA_STATIC_ALLOC), "timer", NULL);
749 if (irq) {
750 prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
751 prom_halt();
753 local_irq_enable();
756 static __inline__ unsigned long do_gettimeoffset(void)
759 * We devide all to 100
760 * to have microsecond resolution and to avoid overflow
762 unsigned long count =
763 readl(pcic0.pcic_regs+PCI_SYS_COUNTER) & ~PCI_SYS_COUNTER_OVERFLOW;
764 count = ((count/100)*USECS_PER_JIFFY) / (TICK_TIMER_LIMIT/100);
765 return count;
768 extern unsigned long wall_jiffies;
770 static void pci_do_gettimeofday(struct timeval *tv)
772 unsigned long flags;
773 unsigned long seq;
774 unsigned long usec, sec;
775 unsigned long max_ntp_tick = tick_usec - tickadj;
777 do {
778 unsigned long lost;
780 seq = read_seqbegin_irqsave(&xtime_lock, flags);
781 usec = do_gettimeoffset();
782 lost = jiffies - wall_jiffies;
785 * If time_adjust is negative then NTP is slowing the clock
786 * so make sure not to go into next possible interval.
787 * Better to lose some accuracy than have time go backwards..
789 if (unlikely(time_adjust < 0)) {
790 usec = min(usec, max_ntp_tick);
792 if (lost)
793 usec += lost * max_ntp_tick;
795 else if (unlikely(lost))
796 usec += lost * tick_usec;
798 sec = xtime.tv_sec;
799 usec += (xtime.tv_nsec / 1000);
800 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
802 while (usec >= 1000000) {
803 usec -= 1000000;
804 sec++;
807 tv->tv_sec = sec;
808 tv->tv_usec = usec;
811 static int pci_do_settimeofday(struct timespec *tv)
813 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
814 return -EINVAL;
817 * This is revolting. We need to set "xtime" correctly. However, the
818 * value in this location is the value at the most recent update of
819 * wall time. Discover what correction gettimeofday() would have
820 * made, and then undo it!
822 tv->tv_nsec -= 1000 * (do_gettimeoffset() +
823 (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
824 while (tv->tv_nsec < 0) {
825 tv->tv_nsec += NSEC_PER_SEC;
826 tv->tv_sec--;
829 wall_to_monotonic.tv_sec += xtime.tv_sec - tv->tv_sec;
830 wall_to_monotonic.tv_nsec += xtime.tv_nsec - tv->tv_nsec;
832 if (wall_to_monotonic.tv_nsec > NSEC_PER_SEC) {
833 wall_to_monotonic.tv_nsec -= NSEC_PER_SEC;
834 wall_to_monotonic.tv_sec++;
836 if (wall_to_monotonic.tv_nsec < 0) {
837 wall_to_monotonic.tv_nsec += NSEC_PER_SEC;
838 wall_to_monotonic.tv_sec--;
841 xtime.tv_sec = tv->tv_sec;
842 xtime.tv_nsec = tv->tv_nsec;
843 time_adjust = 0; /* stop active adjtime() */
844 time_status |= STA_UNSYNC;
845 time_maxerror = NTP_PHASE_LIMIT;
846 time_esterror = NTP_PHASE_LIMIT;
847 return 0;
850 #if 0
851 static void watchdog_reset() {
852 writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
854 #endif
857 * Other archs parse arguments here.
859 char * __init pcibios_setup(char *str)
861 return str;
864 void pcibios_align_resource(void *data, struct resource *res,
865 unsigned long size, unsigned long align)
869 int pcibios_enable_device(struct pci_dev *pdev, int mask)
871 return 0;
875 * NMI
877 void pcic_nmi(unsigned int pend, struct pt_regs *regs)
880 pend = flip_dword(pend);
882 if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
884 * XXX On CP-1200 PCI #SERR may happen, we do not know
885 * what to do about it yet.
887 printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
888 pend, (int)regs->pc, pcic_speculative);
889 for (;;) { }
891 pcic_speculative = 0;
892 pcic_trapped = 1;
893 regs->pc = regs->npc;
894 regs->npc += 4;
897 static inline unsigned long get_irqmask(int irq_nr)
899 return 1 << irq_nr;
902 static inline char *pcic_irq_itoa(unsigned int irq)
904 static char buff[16];
905 sprintf(buff, "%d", irq);
906 return buff;
909 static void pcic_disable_irq(unsigned int irq_nr)
911 unsigned long mask, flags;
913 mask = get_irqmask(irq_nr);
914 local_irq_save(flags);
915 writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
916 local_irq_restore(flags);
919 static void pcic_enable_irq(unsigned int irq_nr)
921 unsigned long mask, flags;
923 mask = get_irqmask(irq_nr);
924 local_irq_save(flags);
925 writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
926 local_irq_restore(flags);
929 static void pcic_clear_profile_irq(int cpu)
931 printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
934 static void pcic_load_profile_irq(int cpu, unsigned int limit)
936 printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
939 /* We assume the caller has disabled local interrupts when these are called,
940 * or else very bizarre behavior will result.
942 static void pcic_disable_pil_irq(unsigned int pil)
944 writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
947 static void pcic_enable_pil_irq(unsigned int pil)
949 writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
952 void __init sun4m_pci_init_IRQ(void)
954 BTFIXUPSET_CALL(enable_irq, pcic_enable_irq, BTFIXUPCALL_NORM);
955 BTFIXUPSET_CALL(disable_irq, pcic_disable_irq, BTFIXUPCALL_NORM);
956 BTFIXUPSET_CALL(enable_pil_irq, pcic_enable_pil_irq, BTFIXUPCALL_NORM);
957 BTFIXUPSET_CALL(disable_pil_irq, pcic_disable_pil_irq, BTFIXUPCALL_NORM);
958 BTFIXUPSET_CALL(clear_clock_irq, pcic_clear_clock_irq, BTFIXUPCALL_NORM);
959 BTFIXUPSET_CALL(clear_profile_irq, pcic_clear_profile_irq, BTFIXUPCALL_NORM);
960 BTFIXUPSET_CALL(load_profile_irq, pcic_load_profile_irq, BTFIXUPCALL_NORM);
961 BTFIXUPSET_CALL(__irq_itoa, pcic_irq_itoa, BTFIXUPCALL_NORM);
964 int pcibios_assign_resource(struct pci_dev *pdev, int resource)
966 return -ENXIO;
970 * This probably belongs here rather than ioport.c because
971 * we do not want this crud linked into SBus kernels.
972 * Also, think for a moment about likes of floppy.c that
973 * include architecture specific parts. They may want to redefine ins/outs.
975 * We do not use horroble macroses here because we want to
976 * advance pointer by sizeof(size).
978 void outsb(unsigned long addr, const void *src, unsigned long count) {
979 while (count) {
980 count -= 1;
981 writeb(*(const char *)src, addr);
982 src += 1;
983 addr += 1;
987 void outsw(unsigned long addr, const void *src, unsigned long count) {
988 while (count) {
989 count -= 2;
990 writew(*(const short *)src, addr);
991 src += 2;
992 addr += 2;
996 void outsl(unsigned long addr, const void *src, unsigned long count) {
997 while (count) {
998 count -= 4;
999 writel(*(const long *)src, addr);
1000 src += 4;
1001 addr += 4;
1005 void insb(unsigned long addr, void *dst, unsigned long count) {
1006 while (count) {
1007 count -= 1;
1008 *(unsigned char *)dst = readb(addr);
1009 dst += 1;
1010 addr += 1;
1014 void insw(unsigned long addr, void *dst, unsigned long count) {
1015 while (count) {
1016 count -= 2;
1017 *(unsigned short *)dst = readw(addr);
1018 dst += 2;
1019 addr += 2;
1023 void insl(unsigned long addr, void *dst, unsigned long count) {
1024 while (count) {
1025 count -= 4;
1027 * XXX I am sure we are in for an unaligned trap here.
1029 *(unsigned long *)dst = readl(addr);
1030 dst += 4;
1031 addr += 4;
1035 subsys_initcall(pcic_init);