original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.git] / arch / sparc / kernel / pcic.c
1 /* $Id: pcic.c,v 1.22.2.1 2002/01/23 14:35:45 davem Exp $
2  * pcic.c: Sparc/PCI controller support
3  *
4  * Copyright (C) 1998 V. Roganov and G. Raiko
5  *
6  * Code is derived from Ultra/PCI PSYCHO controller support, see that
7  * for author info.
8  *
9  * Support for diverse IIep based platforms by Pete Zaitcev.
10  * CP-1200 by Eric Brower.
11  */
12
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
20 #include <asm/ebus.h>
21 #include <asm/sbus.h> /* for sanity check... */
22 #include <asm/swift.h> /* for cache flushing. */
23 #include <asm/io.h>
24
25 #include <linux/ctype.h>
26 #include <linux/pci.h>
27 #include <linux/timex.h>
28 #include <linux/interrupt.h>
29
30 #include <asm/irq.h>
31 #include <asm/oplib.h>
32 #include <asm/pcic.h>
33 #include <asm/timer.h>
34 #include <asm/uaccess.h>
35
36 #ifndef CONFIG_PCI
37
38 asmlinkage int sys_pciconfig_read(unsigned long bus,
39                                   unsigned long dfn,
40                                   unsigned long off,
41                                   unsigned long len,
42                                   unsigned char *buf)
43 {
44         return -EINVAL;
45 }
46
47 asmlinkage int sys_pciconfig_write(unsigned long bus,
48                                    unsigned long dfn,
49                                    unsigned long off,
50                                    unsigned long len,
51                                    unsigned char *buf)
52 {
53         return -EINVAL;
54 }
55
56 #else
57
58 #ifdef CONFIG_SUN_JSFLASH
59 extern int jsflash_init(void);
60 #endif
61
62 struct pci_fixup pcibios_fixups[] = {
63         { 0 }
64 };
65
66 unsigned int pcic_pin_to_irq(unsigned int pin, char *name);
67
68 /*
69  * I studied different documents and many live PROMs both from 2.30
70  * family and 3.xx versions. I came to the amazing conclusion: there is
71  * absolutely no way to route interrupts in IIep systems relying on
72  * information which PROM presents. We must hardcode interrupt routing
73  * schematics. And this actually sucks.   -- zaitcev 1999/05/12
74  *
75  * To find irq for a device we determine which routing map
76  * is in effect or, in other words, on which machine we are running.
77  * We use PROM name for this although other techniques may be used
78  * in special cases (Gleb reports a PROMless IIep based system).
79  * Once we know the map we take device configuration address and
80  * find PCIC pin number where INT line goes. Then we may either program
81  * preferred irq into the PCIC or supply the preexisting irq to the device.
82  */
83 struct pcic_ca2irq {
84         unsigned char busno;            /* PCI bus number */
85         unsigned char devfn;            /* Configuration address */
86         unsigned char pin;              /* PCIC external interrupt pin */
87         unsigned char irq;              /* Preferred IRQ (mappable in PCIC) */
88         unsigned int force;             /* Enforce preferred IRQ */
89 };
90
91 struct pcic_sn2list {
92         char *sysname;
93         struct pcic_ca2irq *intmap;
94         int mapdim;
95 };
96
97 /*
98  * JavaEngine-1 apparently has different versions.
99  *
100  * According to communications with Sun folks, for P2 build 501-4628-03:
101  * pin 0 - parallel, audio;
102  * pin 1 - Ethernet;
103  * pin 2 - su;
104  * pin 3 - PS/2 kbd and mouse.
105  *
106  * OEM manual (805-1486):
107  * pin 0: Ethernet
108  * pin 1: All EBus
109  * pin 2: IGA (unused)
110  * pin 3: Not connected
111  * OEM manual says that 501-4628 & 501-4811 are the same thing,
112  * only the latter has NAND flash in place.
113  *
114  * So far unofficial Sun wins over the OEM manual. Poor OEMs...
115  */
116 static struct pcic_ca2irq pcic_i_je1a[] = {     /* 501-4811-03 */
117         { 0, 0x00, 2, 12, 0 },          /* EBus: hogs all */
118         { 0, 0x01, 1,  6, 1 },          /* Happy Meal */
119         { 0, 0x80, 0,  7, 0 },          /* IGA (unused) */
120 };
121
122 /* XXX JS-E entry is incomplete - PCI Slot 2 address (pin 7)? */
123 static struct pcic_ca2irq pcic_i_jse[] = {
124         { 0, 0x00, 0, 13, 0 },          /* Ebus - serial and keyboard */
125         { 0, 0x01, 1,  6, 0 },          /* hme */
126         { 0, 0x08, 2,  9, 0 },          /* VGA - we hope not used :) */
127         { 0, 0x10, 6,  8, 0 },          /* PCI INTA# in Slot 1 */
128         { 0, 0x18, 7, 12, 0 },          /* PCI INTA# in Slot 2, shared w. RTC */
129         { 0, 0x38, 4,  9, 0 },          /* All ISA devices. Read 8259. */
130         { 0, 0x80, 5, 11, 0 },          /* EIDE */
131         /* {0,0x88, 0,0,0} - unknown device... PMU? Probably no interrupt. */
132         { 0, 0xA0, 4,  9, 0 },          /* USB */
133         /*
134          * Some pins belong to non-PCI devices, we hardcode them in drivers.
135          * sun4m timers - irq 10, 14
136          * PC style RTC - pin 7, irq 4 ?
137          * Smart card, Parallel - pin 4 shared with USB, ISA
138          * audio - pin 3, irq 5 ?
139          */
140 };
141
142 /* SPARCengine-6 was the original release name of CP1200.
143  * The documentation differs between the two versions
144  */
145 static struct pcic_ca2irq pcic_i_se6[] = {
146         { 0, 0x08, 0,  2, 0 },          /* SCSI */
147         { 0, 0x01, 1,  6, 0 },          /* HME  */
148         { 0, 0x00, 3, 13, 0 },          /* EBus */
149 };
150
151 /*
152  * Krups (courtesy of Varol Kaptan)
153  * No documentation available, but it was easy to guess
154  * because it was very similar to Espresso.
155  *  
156  * pin 0 - kbd, mouse, serial;
157  * pin 1 - Ethernet;
158  * pin 2 - igs (we do not use it);
159  * pin 3 - audio;
160  * pin 4,5,6 - unused;
161  * pin 7 - RTC (from P2 onwards as David B. says).
162  */
163 static struct pcic_ca2irq pcic_i_jk[] = {
164         { 0, 0x00, 0, 13, 0 },          /* Ebus - serial and keyboard */
165         { 0, 0x01, 1,  6, 0 },          /* hme */
166 };
167
168 /*
169  * Several entries in this list may point to the same routing map
170  * as several PROMs may be installed on the same physical board.
171  */
172 #define SN2L_INIT(name, map)    \
173   { name, map, sizeof(map)/sizeof(struct pcic_ca2irq) }
174
175 static struct pcic_sn2list pcic_known_sysnames[] = {
176         SN2L_INIT("SUNW,JavaEngine1", pcic_i_je1a),     /* JE1, PROM 2.32 */
177         SN2L_INIT("SUNW,JS-E", pcic_i_jse),     /* PROLL JavaStation-E */
178         SN2L_INIT("SUNW,SPARCengine-6", pcic_i_se6), /* SPARCengine-6/CP-1200 */
179         SN2L_INIT("SUNW,JS-NC", pcic_i_jk),     /* PROLL JavaStation-NC */
180         SN2L_INIT("SUNW,JSIIep", pcic_i_jk),    /* OBP JavaStation-NC */
181         { NULL, NULL, 0 }
182 };
183
184 /*
185  * Only one PCIC per IIep,
186  * and since we have no SMP IIep, only one per system.
187  */
188 static int pcic0_up;
189 static struct linux_pcic pcic0;
190
191 unsigned int pcic_regs;
192 volatile int pcic_speculative;
193 volatile int pcic_trapped;
194
195 static void pci_do_gettimeofday(struct timeval *tv);
196 static void pci_do_settimeofday(struct timeval *tv);
197
198 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (((unsigned int)bus) << 16) | (((unsigned int)device_fn) << 8) | (where & ~3))
199
200 static int pcic_read_config_dword(struct pci_dev *dev, int where, u32 *value);
201 static int pcic_write_config_dword(struct pci_dev *dev, int where, u32 value);
202
203 static int pcic_read_config_byte(struct pci_dev *dev, int where, u8 *value)
204 {
205         unsigned int v;
206
207         pcic_read_config_dword(dev, where&~3, &v);
208         *value = 0xff & (v >> (8*(where & 3)));
209         return PCIBIOS_SUCCESSFUL;
210 }
211
212 static int pcic_read_config_word(struct pci_dev *dev, int where, u16 *value)
213 {
214         unsigned int v;
215         if (where&1) return PCIBIOS_BAD_REGISTER_NUMBER;
216
217         pcic_read_config_dword(dev, where&~3, &v);
218         *value = 0xffff & (v >> (8*(where & 3)));
219         return PCIBIOS_SUCCESSFUL;
220 }
221
222 static int pcic_read_config_dword(struct pci_dev *dev, int where, u32 *value)
223 {
224         unsigned char bus = dev->bus->number;
225         unsigned char device_fn = dev->devfn;
226         /* unsigned char where; */
227
228         struct linux_pcic *pcic;
229         unsigned long flags;
230
231         if (where&3) return PCIBIOS_BAD_REGISTER_NUMBER;
232         if (bus != 0) return PCIBIOS_DEVICE_NOT_FOUND;
233         pcic = &pcic0;
234
235         save_and_cli(flags);
236 #if 0 /* does not fail here */
237         pcic_speculative = 1;
238         pcic_trapped = 0;
239 #endif
240         writel(CONFIG_CMD(bus,device_fn,where), pcic->pcic_config_space_addr);
241 #if 0 /* does not fail here */
242         nop();
243         if (pcic_trapped) {
244                 restore_flags(flags);
245                 *value = ~0;
246                 return PCIBIOS_SUCCESSFUL;
247         }
248 #endif
249         pcic_speculative = 2;
250         pcic_trapped = 0;
251         *value = readl(pcic->pcic_config_space_data + (where&4));
252         nop();
253         if (pcic_trapped) {
254                 pcic_speculative = 0;
255                 restore_flags(flags);
256                 *value = ~0;
257                 return PCIBIOS_SUCCESSFUL;
258         }
259         pcic_speculative = 0;
260         restore_flags(flags);
261         return PCIBIOS_SUCCESSFUL;
262 }
263
264 static int pcic_write_config_byte(struct pci_dev *dev, int where, u8 value)
265 {
266         unsigned int v;
267
268         pcic_read_config_dword(dev, where&~3, &v);
269         v = (v & ~(0xff << (8*(where&3)))) |
270             ((0xff&(unsigned)value) << (8*(where&3)));
271         return pcic_write_config_dword(dev, where&~3, v);
272 }
273
274 static int pcic_write_config_word(struct pci_dev *dev, int where, u16 value)
275 {
276         unsigned int v;
277
278         if (where&1) return PCIBIOS_BAD_REGISTER_NUMBER;
279         pcic_read_config_dword(dev, where&~3, &v);
280         v = (v & ~(0xffff << (8*(where&3)))) |
281             ((0xffff&(unsigned)value) << (8*(where&3)));
282         return pcic_write_config_dword(dev, where&~3, v);
283 }
284
285 static int pcic_write_config_dword(struct pci_dev *dev, int where, u32 value)
286 {
287         unsigned char bus = dev->bus->number;
288         unsigned char devfn = dev->devfn;
289         struct linux_pcic *pcic;
290         unsigned long flags;
291
292         if (where&3) return PCIBIOS_BAD_REGISTER_NUMBER;
293         if (bus != 0) return PCIBIOS_DEVICE_NOT_FOUND;
294         pcic = &pcic0;
295
296         save_and_cli(flags);
297         writel(CONFIG_CMD(bus,devfn,where), pcic->pcic_config_space_addr);
298         writel(value, pcic->pcic_config_space_data + (where&4));
299         restore_flags(flags);
300         return PCIBIOS_SUCCESSFUL;
301 }
302
303 static struct pci_ops pcic_ops = {
304         pcic_read_config_byte,
305         pcic_read_config_word,
306         pcic_read_config_dword,
307         pcic_write_config_byte,
308         pcic_write_config_word,
309         pcic_write_config_dword,
310 };
311
312 /*
313  * On sparc64 pcibios_init() calls pci_controller_probe().
314  * We want PCIC probed little ahead so that interrupt controller
315  * would be operational.
316  */
317 int __init pcic_probe(void)
318 {
319         struct linux_pcic *pcic;
320         struct linux_prom_registers regs[PROMREG_MAX];
321         struct linux_pbm_info* pbm;
322         char namebuf[64];
323         int node;
324         int err;
325
326         if (pcic0_up) {
327                 prom_printf("PCIC: called twice!\n");
328                 prom_halt();
329         }
330         pcic = &pcic0;
331
332         node = prom_getchild (prom_root_node);
333         node = prom_searchsiblings (node, "pci");
334         if (node == 0)
335                 return -ENODEV;
336         /*
337          * Map in PCIC register set, config space, and IO base
338          */
339         err = prom_getproperty(node, "reg", (char*)regs, sizeof(regs));
340         if (err == 0 || err == -1) {
341                 prom_printf("PCIC: Error, cannot get PCIC registers "
342                             "from PROM.\n");
343                 prom_halt();
344         }
345
346         pcic0_up = 1;
347
348         pcic->pcic_res_regs.name = "pcic_registers";
349         pcic->pcic_regs = (unsigned long)
350             ioremap(regs[0].phys_addr, regs[0].reg_size);
351         if (!pcic->pcic_regs) {
352                 prom_printf("PCIC: Error, cannot map PCIC registers.\n");
353                 prom_halt();
354         }
355
356         pcic->pcic_res_io.name = "pcic_io";
357         if ((pcic->pcic_io = (unsigned long)
358             ioremap(regs[1].phys_addr, 0x10000)) == 0) {
359                 prom_printf("PCIC: Error, cannot map PCIC IO Base.\n");
360                 prom_halt();
361         }
362
363         pcic->pcic_res_cfg_addr.name = "pcic_cfg_addr";
364         if ((pcic->pcic_config_space_addr = (unsigned long)
365             ioremap(regs[2].phys_addr, regs[2].reg_size * 2)) == 0) {
366                 prom_printf("PCIC: Error, cannot map" 
367                             "PCI Configuration Space Address.\n");
368                 prom_halt();
369         }
370
371         /*
372          * Docs say three least significant bits in address and data
373          * must be the same. Thus, we need adjust size of data.
374          */
375         pcic->pcic_res_cfg_data.name = "pcic_cfg_data";
376         if ((pcic->pcic_config_space_data = (unsigned long)
377             ioremap(regs[3].phys_addr, regs[3].reg_size * 2)) == 0) {
378                 prom_printf("PCIC: Error, cannot map" 
379                             "PCI Configuration Space Data.\n");
380                 prom_halt();
381         }
382
383         pbm = &pcic->pbm;
384         pbm->prom_node = node;
385         prom_getstring(node, "name", namebuf, 63);  namebuf[63] = 0;
386         strcpy(pbm->prom_name, namebuf);
387
388         {
389                 extern volatile int t_nmi[1];
390                 extern int pcic_nmi_trap_patch[1];
391
392                 t_nmi[0] = pcic_nmi_trap_patch[0];
393                 t_nmi[1] = pcic_nmi_trap_patch[1];
394                 t_nmi[2] = pcic_nmi_trap_patch[2];
395                 t_nmi[3] = pcic_nmi_trap_patch[3];
396                 swift_flush_dcache();
397                 pcic_regs = pcic->pcic_regs;
398         }
399
400         prom_getstring(prom_root_node, "name", namebuf, 63);  namebuf[63] = 0;
401         {
402                 struct pcic_sn2list *p;
403
404                 for (p = pcic_known_sysnames; p->sysname != NULL; p++) {
405                         if (strcmp(namebuf, p->sysname) == 0)
406                                 break;
407                 }
408                 pcic->pcic_imap = p->intmap;
409                 pcic->pcic_imdim = p->mapdim;
410         }
411         if (pcic->pcic_imap == NULL) {
412                 /*
413                  * We do not panic here for the sake of embedded systems.
414                  */
415                 printk("PCIC: System %s is unknown, cannot route interrupts\n",
416                     namebuf);
417         }
418
419         return 0;
420 }
421
422 static void __init pcic_pbm_scan_bus(struct linux_pcic *pcic)
423 {
424         struct linux_pbm_info *pbm = &pcic->pbm;
425
426         pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, &pcic_ops, pbm);
427 #if 0 /* deadwood transplanted from sparc64 */
428         pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
429         pci_record_assignments(pbm, pbm->pci_bus);
430         pci_assign_unassigned(pbm, pbm->pci_bus);
431         pci_fixup_irq(pbm, pbm->pci_bus);
432 #endif
433 }
434
435 /*
436  * Main entry point from the PCI subsystem.
437  */
438 void __init pcibios_init(void)
439 {
440         struct linux_pcic *pcic;
441
442         /*
443          * PCIC should be initialized at start of the timer.
444          * So, here we report the presence of PCIC and do some magic passes.
445          */
446         if(!pcic0_up)
447                 return;
448         pcic = &pcic0;
449
450         /*
451          *      Switch off IOTLB translation.
452          */
453         writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE, 
454                pcic->pcic_regs+PCI_DVMA_CONTROL);
455
456         /*
457          *      Increase mapped size for PCI memory space (DMA access).
458          *      Should be done in that order (size first, address second).
459          *      Why we couldn't set up 4GB and forget about it? XXX
460          */
461         writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
462         writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY, 
463                pcic->pcic_regs+PCI_BASE_ADDRESS_0);
464
465         pcic_pbm_scan_bus(pcic);
466
467         ebus_init();
468 #ifdef CONFIG_SUN_JSFLASH
469         jsflash_init();
470 #endif
471 }
472
473 int pcic_present(void)
474 {
475         return pcic0_up;
476 }
477
478 static int __init pdev_to_pnode(struct linux_pbm_info *pbm, 
479                                     struct pci_dev *pdev)
480 {
481         struct linux_prom_pci_registers regs[PROMREG_MAX];
482         int err;
483         int node = prom_getchild(pbm->prom_node);
484
485         while(node) {
486                 err = prom_getproperty(node, "reg", 
487                                        (char *)&regs[0], sizeof(regs));
488                 if(err != 0 && err != -1) {
489                         unsigned long devfn = (regs[0].which_io >> 8) & 0xff;
490                         if(devfn == pdev->devfn)
491                                 return node;
492                 }
493                 node = prom_getsibling(node);
494         }
495         return 0;
496 }
497
498 static inline struct pcidev_cookie *pci_devcookie_alloc(void)
499 {
500         return kmalloc(sizeof(struct pcidev_cookie), GFP_ATOMIC);
501 }
502
503 static void pcic_map_pci_device(struct linux_pcic *pcic,
504     struct pci_dev *dev, int node)
505 {
506         char namebuf[64];
507         unsigned long address;
508         unsigned long flags;
509         int j;
510
511         if (node == 0 || node == -1) {
512                 strcpy(namebuf, "???");
513         } else {
514                 prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
515         }
516
517         for (j = 0; j < 6; j++) {
518                 address = dev->resource[j].start;
519                 if (address == 0) break;        /* are sequential */
520                 flags = dev->resource[j].flags;
521                 if ((flags & IORESOURCE_IO) != 0) {
522                         if (address < 0x10000) {
523                                 /*
524                                  * A device responds to I/O cycles on PCI.
525                                  * We generate these cycles with memory
526                                  * access into the fixed map (phys 0x30000000).
527                                  *
528                                  * Since a device driver does not want to
529                                  * do ioremap() before accessing PC-style I/O,
530                                  * we supply virtual, ready to access address.
531                                  *
532                                  * Ebus devices do not come here even if
533                                  * CheerIO makes a similar conversion.
534                                  * See ebus.c for details.
535                                  *
536                                  * Note that check_region()/request_region()
537                                  * work for these devices.
538                                  *
539                                  * XXX Neat trick, but it's a *bad* idea
540                                  * to shit into regions like that.
541                                  * What if we want to allocate one more
542                                  * PCI base address...
543                                  */
544                                 dev->resource[j].start =
545                                     pcic->pcic_io + address;
546                                 dev->resource[j].end = 1;  /* XXX */
547                                 dev->resource[j].flags =
548                                     (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
549                         } else {
550                                 /*
551                                  * OOPS... PCI Spec allows this. Sun does
552                                  * not have any devices getting above 64K
553                                  * so it must be user with a weird I/O
554                                  * board in a PCI slot. We must remap it
555                                  * under 64K but it is not done yet. XXX
556                                  */
557                                 printk("PCIC: Skipping I/O space at 0x%lx,"
558                                     "this will Oops if a driver attaches;"
559                                     "device '%s' at %02x:%02x)\n", address,
560                                     namebuf, dev->bus->number, dev->devfn);
561                         }
562                 }
563         }
564 }
565
566 static void
567 pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
568 {
569         struct pcic_ca2irq *p;
570         int i, ivec;
571         char namebuf[64];
572
573         if (node == 0 || node == -1) {
574                 strcpy(namebuf, "???");
575         } else {
576                 prom_getstring(node, "name", namebuf, sizeof(namebuf));
577         }
578
579         if ((p = pcic->pcic_imap) == 0) {
580                 dev->irq = 0;
581                 return;
582         }
583         for (i = 0; i < pcic->pcic_imdim; i++) {
584                 if (p->busno == dev->bus->number && p->devfn == dev->devfn)
585                         break;
586                 p++;
587         }
588         if (i >= pcic->pcic_imdim) {
589                 printk("PCIC: device %s devfn %02x:%02x not found in %d\n",
590                     namebuf, dev->bus->number, dev->devfn, pcic->pcic_imdim);
591                 dev->irq = 0;
592                 return;
593         }
594
595         i = p->pin;
596         if (i >= 0 && i < 4) {
597                 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
598                 dev->irq = ivec >> (i << 2) & 0xF;
599         } else if (i >= 4 && i < 8) {
600                 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
601                 dev->irq = ivec >> ((i-4) << 2) & 0xF;
602         } else {                                        /* Corrupted map */
603                 printk("PCIC: BAD PIN %d\n", i); for (;;) {}
604         }
605 /* P3 */ /* printk("PCIC: device %s pin %d ivec 0x%x irq %x\n", namebuf, i, ivec, dev->irq); */
606
607         /*
608          * dev->irq=0 means PROM did not bother to program the upper
609          * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
610          */
611         if (dev->irq == 0 || p->force) {
612                 if (p->irq == 0 || p->irq >= 15) {      /* Corrupted map */
613                         printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
614                 }
615                 printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
616                     p->irq, p->pin, dev->bus->number, dev->devfn);
617                 dev->irq = p->irq;
618
619                 i = p->pin;
620                 if (i >= 4) {
621                         ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
622                         ivec &= ~(0xF << ((i - 4) << 2));
623                         ivec |= p->irq << ((i - 4) << 2);
624                         writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
625                 } else {
626                         ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
627                         ivec &= ~(0xF << (i << 2));
628                         ivec |= p->irq << (i << 2);
629                         writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
630                 }
631         }
632
633         return;
634 }
635
636 /*
637  * Normally called from {do_}pci_scan_bus...
638  */
639 void __init pcibios_fixup_bus(struct pci_bus *bus)
640 {
641         struct list_head *walk;
642         int i, has_io, has_mem;
643         unsigned short cmd;
644         struct linux_pcic *pcic;
645         /* struct linux_pbm_info* pbm = &pcic->pbm; */
646         int node;
647         struct pcidev_cookie *pcp;
648
649         if (!pcic0_up) {
650                 printk("pcibios_fixup_bus: no PCIC\n");
651                 return;
652         }
653         pcic = &pcic0;
654
655         /*
656          * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
657          */
658         if (bus->number != 0) {
659                 printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
660                 return;
661         }
662
663         walk = &bus->devices;
664         for (walk = walk->next; walk != &bus->devices; walk = walk->next) {
665                 struct pci_dev *dev = pci_dev_b(walk);
666
667                 /*
668                  * Comment from i386 branch:
669                  *     There are buggy BIOSes that forget to enable I/O and memory
670                  *     access to PCI devices. We try to fix this, but we need to
671                  *     be sure that the BIOS didn't forget to assign an address
672                  *     to the device. [mj]
673                  * OBP is a case of such BIOS :-)
674                  */
675                 has_io = has_mem = 0;
676                 for(i=0; i<6; i++) {
677                         unsigned long f = dev->resource[i].flags;
678                         if (f & IORESOURCE_IO) {
679                                 has_io = 1;
680                         } else if (f & IORESOURCE_MEM)
681                                 has_mem = 1;
682                 }
683                 pcic_read_config_word(dev, PCI_COMMAND, &cmd);
684                 if (has_io && !(cmd & PCI_COMMAND_IO)) {
685                         printk("PCIC: Enabling I/O for device %02x:%02x\n",
686                                 dev->bus->number, dev->devfn);
687                         cmd |= PCI_COMMAND_IO;
688                         pcic_write_config_word(dev, PCI_COMMAND, cmd);
689                 }
690                 if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
691                         printk("PCIC: Enabling memory for device %02x:%02x\n",
692                                 dev->bus->number, dev->devfn);
693                         cmd |= PCI_COMMAND_MEMORY;
694                         pcic_write_config_word(dev, PCI_COMMAND, cmd);
695                 }    
696
697                 node = pdev_to_pnode(&pcic->pbm, dev);
698                 if(node == 0)
699                         node = -1;
700
701                 /* cookies */
702                 pcp = pci_devcookie_alloc();
703                 pcp->pbm = &pcic->pbm;
704                 pcp->prom_node = node;
705                 dev->sysdata = pcp;
706
707                 /* fixing I/O to look like memory */
708                 if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
709                         pcic_map_pci_device(pcic, dev, node);
710
711                 pcic_fill_irq(pcic, dev, node);
712         }
713 }
714
715 /*
716  * pcic_pin_to_irq() is exported to ebus.c.
717  */
718 unsigned int
719 pcic_pin_to_irq(unsigned int pin, char *name)
720 {
721         struct linux_pcic *pcic = &pcic0;
722         unsigned int irq;
723         unsigned int ivec;
724
725         if (pin < 4) {
726                 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
727                 irq = ivec >> (pin << 2) & 0xF;
728         } else if (pin < 8) {
729                 ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
730                 irq = ivec >> ((pin-4) << 2) & 0xF;
731         } else {                                        /* Corrupted map */
732                 printk("PCIC: BAD PIN %d FOR %s\n", pin, name);
733                 for (;;) {}     /* XXX Cannot panic properly in case of PROLL */
734         }
735 /* P3 */ /* printk("PCIC: dev %s pin %d ivec 0x%x irq %x\n", name, pin, ivec, irq); */
736         return irq;
737 }
738
739 /* Makes compiler happy */
740 static volatile int pcic_timer_dummy;
741
742 static void pcic_clear_clock_irq(void)
743 {
744         pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
745 }
746
747 static void pcic_timer_handler (int irq, void *h, struct pt_regs *regs)
748 {
749         pcic_clear_clock_irq();
750         do_timer(regs);
751 }
752
753 #define USECS_PER_JIFFY  10000  /* We have 100HZ "standard" timer for sparc */
754 #define TICK_TIMER_LIMIT ((100*1000000/4)/100)
755
756 void __init pci_time_init(void)
757 {
758         struct linux_pcic *pcic = &pcic0;
759         unsigned long v;
760         int timer_irq, irq;
761
762         /* A hack until do_gettimeofday prototype is moved to arch specific headers
763            and btfixupped. Patch do_gettimeofday with ba pci_do_gettimeofday; nop */
764         ((unsigned int *)do_gettimeofday)[0] = 
765             0x10800000 | ((((unsigned long)pci_do_gettimeofday -
766              (unsigned long)do_gettimeofday) >> 2) & 0x003fffff);
767         ((unsigned int *)do_gettimeofday)[1] = 0x01000000;
768         BTFIXUPSET_CALL(bus_do_settimeofday, pci_do_settimeofday, BTFIXUPCALL_NORM);
769         btfixup();
770
771         writel (TICK_TIMER_LIMIT, pcic->pcic_regs+PCI_SYS_LIMIT);
772         /* PROM should set appropriate irq */
773         v = readb(pcic->pcic_regs+PCI_COUNTER_IRQ);
774         timer_irq = PCI_COUNTER_IRQ_SYS(v);
775         writel (PCI_COUNTER_IRQ_SET(timer_irq, 0),
776                 pcic->pcic_regs+PCI_COUNTER_IRQ);
777         irq = request_irq(timer_irq, pcic_timer_handler,
778                           (SA_INTERRUPT | SA_STATIC_ALLOC), "timer", NULL);
779         if (irq) {
780                 prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
781                 prom_halt();
782         }
783         __sti();
784 }
785
786 static __inline__ unsigned long do_gettimeoffset(void)
787 {
788         struct tasklet_struct *t;
789         unsigned long offset = 0;
790
791         /* 
792          * We devide all to 100
793          * to have microsecond resolution and to avoid overflow
794          */
795         unsigned long count = 
796             readl(pcic0.pcic_regs+PCI_SYS_COUNTER) & ~PCI_SYS_COUNTER_OVERFLOW;
797         count = ((count/100)*USECS_PER_JIFFY) / (TICK_TIMER_LIMIT/100);
798
799         t = &bh_task_vec[TIMER_BH];
800         if (test_bit(TASKLET_STATE_SCHED, &t->state))
801                 offset = 1000000;
802         return offset + count;
803 }
804
805 extern volatile unsigned long wall_jiffies;
806
807 static void pci_do_gettimeofday(struct timeval *tv)
808 {
809         unsigned long flags;
810
811         save_and_cli(flags);
812         *tv = xtime;
813         tv->tv_usec += do_gettimeoffset();
814
815         /*
816          * xtime is atomically updated in timer_bh. The difference
817          * between jiffies and wall_jiffies is nonzero if the timer
818          * bottom half hasnt executed yet.
819          */
820         if ((jiffies - wall_jiffies) != 0)
821                 tv->tv_usec += USECS_PER_JIFFY;
822
823         restore_flags(flags);
824
825         if (tv->tv_usec >= 1000000) {
826                 tv->tv_usec -= 1000000;
827                 tv->tv_sec++;
828         }       
829 }
830
831 static void pci_do_settimeofday(struct timeval *tv)
832 {
833         cli();
834         tv->tv_usec -= do_gettimeoffset();
835         if(tv->tv_usec < 0) {
836                 tv->tv_usec += 1000000;
837                 tv->tv_sec--;
838         }
839         xtime = *tv;
840         time_adjust = 0;                /* stop active adjtime() */
841         time_status |= STA_UNSYNC;
842         time_maxerror = NTP_PHASE_LIMIT;
843         time_esterror = NTP_PHASE_LIMIT;
844         sti();
845 }
846
847 #if 0
848 static void watchdog_reset() {
849         writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
850 }
851 #endif
852
853 /*
854  * Other archs parse arguments here.
855  */
856 char * __init pcibios_setup(char *str)
857 {
858         return str;
859 }
860
861 /*
862  */
863 void pcibios_update_resource(struct pci_dev *pdev, struct resource *res1,
864                              struct resource *res2, int index)
865 {
866 }
867
868 void pcibios_align_resource(void *data, struct resource *res,
869                             unsigned long size, unsigned long align)
870 {
871 }
872
873 int pcibios_enable_device(struct pci_dev *pdev, int mask)
874 {
875         return 0;
876 }
877
878 /*
879  * NMI
880  */
881 void pcic_nmi(unsigned int pend, struct pt_regs *regs)
882 {
883
884         pend = flip_dword(pend);
885
886         if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
887                 /*
888                  * XXX On CP-1200 PCI #SERR may happen, we do not know
889                  * what to do about it yet.
890                  */
891                 printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
892                     pend, (int)regs->pc, pcic_speculative);
893                 for (;;) { }
894         }
895         pcic_speculative = 0;
896         pcic_trapped = 1;
897         regs->pc = regs->npc;
898         regs->npc += 4;
899 }
900
901 static inline unsigned long get_irqmask(int irq_nr)
902 {
903         return 1 << irq_nr;
904 }
905
906 static inline char *pcic_irq_itoa(unsigned int irq)
907 {
908         static char buff[16];
909         sprintf(buff, "%d", irq);
910         return buff;
911 }
912
913 static void pcic_disable_irq(unsigned int irq_nr)
914 {
915         unsigned long mask, flags;
916
917         mask = get_irqmask(irq_nr);
918         save_and_cli(flags);
919         writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
920         restore_flags(flags);
921 }
922
923 static void pcic_enable_irq(unsigned int irq_nr)
924 {
925         unsigned long mask, flags;
926
927         mask = get_irqmask(irq_nr);
928         save_and_cli(flags);
929         writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
930         restore_flags(flags);
931 }
932
933 static void pcic_clear_profile_irq(int cpu)
934 {
935         printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
936 }
937
938 static void pcic_load_profile_irq(int cpu, unsigned int limit)
939 {
940         printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
941 }
942
943 /* We assume the caller is local cli()'d when these are called, or else
944  * very bizarre behavior will result.
945  */
946 static void pcic_disable_pil_irq(unsigned int pil)
947 {
948         writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
949 }
950
951 static void pcic_enable_pil_irq(unsigned int pil)
952 {
953         writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
954 }
955
956 void __init sun4m_pci_init_IRQ(void)
957 {
958         BTFIXUPSET_CALL(enable_irq, pcic_enable_irq, BTFIXUPCALL_NORM);
959         BTFIXUPSET_CALL(disable_irq, pcic_disable_irq, BTFIXUPCALL_NORM);
960         BTFIXUPSET_CALL(enable_pil_irq, pcic_enable_pil_irq, BTFIXUPCALL_NORM);
961         BTFIXUPSET_CALL(disable_pil_irq, pcic_disable_pil_irq, BTFIXUPCALL_NORM);
962         BTFIXUPSET_CALL(clear_clock_irq, pcic_clear_clock_irq, BTFIXUPCALL_NORM);
963         BTFIXUPSET_CALL(clear_profile_irq, pcic_clear_profile_irq, BTFIXUPCALL_NORM);
964         BTFIXUPSET_CALL(load_profile_irq, pcic_load_profile_irq, BTFIXUPCALL_NORM);
965         BTFIXUPSET_CALL(__irq_itoa, pcic_irq_itoa, BTFIXUPCALL_NORM);
966 }
967
968 int pcibios_assign_resource(struct pci_dev *pdev, int resource)
969 {
970         return -ENXIO;
971 }
972
973 /*
974  * This probably belongs here rather than ioport.c because
975  * we do not want this crud linked into SBus kernels.
976  * Also, think for a moment about likes of floppy.c that
977  * include architecture specific parts. They may want to redefine ins/outs.
978  *
979  * We do not use horroble macroses here because we want to
980  * advance pointer by sizeof(size).
981  */
982 void outsb(unsigned long addr, const void *src, unsigned long count) {
983         while (count) {
984                 count -= 1;
985                 writeb(*(const char *)src, addr);
986                 src += 1;
987                 addr += 1;
988         }
989 }
990
991 void outsw(unsigned long addr, const void *src, unsigned long count) {
992         while (count) {
993                 count -= 2;
994                 writew(*(const short *)src, addr);
995                 src += 2;
996                 addr += 2;
997         }
998 }
999
1000 void outsl(unsigned long addr, const void *src, unsigned long count) {
1001         while (count) {
1002                 count -= 4;
1003                 writel(*(const long *)src, addr);
1004                 src += 4;
1005                 addr += 4;
1006         }
1007 }
1008
1009 void insb(unsigned long addr, void *dst, unsigned long count) {
1010         while (count) {
1011                 count -= 1;
1012                 *(unsigned char *)dst = readb(addr);
1013                 dst += 1;
1014                 addr += 1;
1015         }
1016 }
1017
1018 void insw(unsigned long addr, void *dst, unsigned long count) {
1019         while (count) {
1020                 count -= 2;
1021                 *(unsigned short *)dst = readw(addr);
1022                 dst += 2;
1023                 addr += 2;
1024         }
1025 }
1026
1027 void insl(unsigned long addr, void *dst, unsigned long count) {
1028         while (count) {
1029                 count -= 4;
1030                 /*
1031                  * XXX I am sure we are in for an unaligned trap here.
1032                  */
1033                 *(unsigned long *)dst = readl(addr);
1034                 dst += 4;
1035                 addr += 4;
1036         }
1037 }
1038
1039 #endif