patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / m68knommu / kernel / comempci.c
1 /*****************************************************************************/
2
3 /*
4  *      comemlite.c -- PCI access code for embedded CO-MEM Lite PCI controller.
5  *
6  *      (C) Copyright 1999-2003, Greg Ungerer (gerg@snapgear.com).
7  *      (C) Copyright 2000, Lineo (www.lineo.com)
8  */
9
10 /*****************************************************************************/
11
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/ptrace.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/sched.h>
20 #include <asm/coldfire.h>
21 #include <asm/mcfsim.h>
22 #include <asm/irq.h>
23 #include <asm/anchor.h>
24
25 #ifdef CONFIG_eLIA
26 #include <asm/elia.h>
27 #endif
28
29 /*****************************************************************************/
30
31 /*
32  *      Debug configuration defines. DEBUGRES sets debugging output for
33  *      the resource allocation phase. DEBUGPCI traces on pcibios_ function
34  *      calls, and DEBUGIO traces all accesses to devices on the PCI bus.
35  */
36 /*#define       DEBUGRES        1*/
37 /*#define       DEBUGPCI        1*/
38 /*#define       DEBUGIO         1*/
39
40 /*****************************************************************************/
41
42 /*
43  *      PCI markers for bus present and active slots.
44  */
45 int             pci_bus_is_present = 0;
46 unsigned long   pci_slotmask = 0;
47
48 /*
49  *      We may or may not need to swap the bytes of PCI bus tranfers.
50  *      The endianess is re-roder automatically by the CO-MEM, but it
51  *      will get the wrong byte order for a pure data stream.
52  */
53 #define pci_byteswap    0
54
55
56 /*
57  *      Resource tracking. The CO-MEM part creates a virtual address
58  *      space that all the PCI devices live in - it is not in any way
59  *      directly mapped into the ColdFire address space. So we can
60  *      really assign any resources we like to devices, as long as
61  *      they do not clash with other PCI devices.
62  */
63 unsigned int    pci_iobase = PCIBIOS_MIN_IO;    /* Arbitrary start address */
64 unsigned int    pci_membase = PCIBIOS_MIN_MEM;  /* Arbitrary start address */
65
66 #define PCI_MINIO       0x100                   /* 256 byte minimum I/O */
67 #define PCI_MINMEM      0x00010000              /* 64k minimum chunk */
68
69 /*
70  *      The CO-MEM's shared memory segment is visible inside the PCI
71  *      memory address space. We need to keep track of the address that
72  *      this is mapped at, to setup the bus masters pointers.
73  */
74 unsigned int    pci_shmemaddr;
75
76 /*****************************************************************************/
77
78 void    pci_interrupt(int irq, void *id, struct pt_regs *fp);
79
80 /*****************************************************************************/
81
82 /*
83  *      Some platforms have custom ways of reseting the PCI bus.
84  */
85
86 void pci_resetbus(void)
87 {
88 #ifdef CONFIG_eLIA
89         int     i;
90
91 #ifdef DEBUGPCI
92         printk(KERN_DEBUG "pci_resetbus()\n");
93 #endif
94
95         *((volatile unsigned short *) (MCF_MBAR+MCFSIM_PADDR)) |= eLIA_PCIRESET;
96         for (i = 0; (i < 1000); i++) {
97                 *((volatile unsigned short *) (MCF_MBAR + MCFSIM_PADAT)) = 
98                         (ppdata | eLIA_PCIRESET);
99         }
100
101
102         *((volatile unsigned short *) (MCF_MBAR + MCFSIM_PADAT)) = ppdata;
103 #endif
104 }
105
106 /*****************************************************************************/
107
108 int pcibios_assign_resource_slot(int slot)
109 {
110         volatile unsigned long  *rp;
111         volatile unsigned char  *ip;
112         unsigned int            idsel, addr, val, align, i;
113         int                     bar;
114
115 #ifdef DEBUGPCI
116         printk(KERN_INFO "pcibios_assign_resource_slot(slot=%x)\n", slot);
117 #endif
118
119         rp = (volatile unsigned long *) COMEM_BASE;
120         idsel = COMEM_DA_ADDR(0x1 << (slot + 16));
121
122         /* Try to assign resource to each BAR */
123         for (bar = 0; (bar < 6); bar++) {
124                 addr = COMEM_PCIBUS + PCI_BASE_ADDRESS_0 + (bar * 4);
125                 rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
126                 val = rp[LREG(addr)];
127 #ifdef DEBUGRES
128                 printk(KERN_DEBUG "-----------------------------------"
129                         "-------------------------------------\n");
130                 printk(KERN_DEBUG "BAR[%d]: read=%08x ", bar, val);
131 #endif
132
133                 rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
134                 rp[LREG(addr)] = 0xffffffff;
135
136                 rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
137                 val = rp[LREG(addr)];
138 #ifdef DEBUGRES
139                 printk(KERN_DEBUG "write=%08x ", val);
140 #endif
141                 if (val == 0) {
142 #ifdef DEBUGRES
143                         printk(KERN_DEBUG "\n");
144 #endif
145                         continue;
146                 }
147
148                 /* Determine space required by BAR */
149                 /* FIXME: this should go backwords from 0x80000000... */
150                 for (i = 0; (i < 32); i++) {
151                         if ((0x1 << i) & (val & 0xfffffffc))
152                                 break;
153                 }
154
155 #ifdef DEBUGRES
156                 printk(KERN_DEBUG "size=%08x(%d)\n", (0x1 << i), i);
157 #endif
158                 i = 0x1 << i;
159
160                 /* Assign a resource */
161                 if (val & PCI_BASE_ADDRESS_SPACE_IO) {
162                         if (i < PCI_MINIO)
163                                 i = PCI_MINIO;
164 #ifdef DEBUGRES
165                         printk(KERN_DEBUG "BAR[%d]: IO size=%08x iobase=%08x\n",
166                                 bar, i, pci_iobase);
167 #endif
168                         if (i > 0xffff) {
169                                 /* Invalid size?? */
170                                 val = 0 | PCI_BASE_ADDRESS_SPACE_IO;
171 #ifdef DEBUGRES
172                                 printk(KERN_DEBUG "BAR[%d]: too big for IO??\n", bar);
173 #endif
174                         } else {
175                                 /* Check for un-alignment */
176                                 if ((align = pci_iobase % i))
177                                         pci_iobase += (i - align);
178                                 val = pci_iobase | PCI_BASE_ADDRESS_SPACE_IO;
179                                 pci_iobase += i;
180                         }
181                 } else {
182                         if (i < PCI_MINMEM)
183                                 i = PCI_MINMEM;
184 #ifdef DEBUGRES
185                         printk(KERN_DEBUG "BAR[%d]: MEMORY size=%08x membase=%08x\n",
186                                 bar, i, pci_membase);
187 #endif
188                         /* Check for un-alignment */
189                         if ((align = pci_membase % i))
190                                 pci_membase += (i - align);
191                         val = pci_membase | PCI_BASE_ADDRESS_SPACE_MEMORY;
192                         pci_membase += i;
193                 }
194
195                 /* Write resource back into BAR register */
196                 rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
197                 rp[LREG(addr)] = val;
198 #ifdef DEBUGRES
199                 printk(KERN_DEBUG "BAR[%d]: assigned bar=%08x\n", bar, val);
200 #endif
201         }
202
203 #ifdef DEBUGRES
204         printk(KERN_DEBUG "-----------------------------------"
205                         "-------------------------------------\n");
206 #endif
207
208         /* Assign IRQ if one is wanted... */
209         ip = (volatile unsigned char *) (COMEM_BASE + COMEM_PCIBUS);
210         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
211
212         addr = (PCI_INTERRUPT_PIN & 0xfc) + (~PCI_INTERRUPT_PIN & 0x03);
213         if (ip[addr]) {
214                 rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
215                 addr = (PCI_INTERRUPT_LINE & 0xfc)+(~PCI_INTERRUPT_LINE & 0x03);
216                 ip[addr] = 25;
217 #ifdef DEBUGRES
218                 printk(KERN_DEBUG "IRQ LINE=25\n");
219 #endif
220         }
221
222         return(0);
223 }
224
225 /*****************************************************************************/
226
227 int pcibios_enable_slot(int slot)
228 {
229         volatile unsigned long  *rp;
230         volatile unsigned short *wp;
231         unsigned int            idsel, addr;
232         unsigned short          cmd;
233
234 #ifdef DEBUGPCI
235         printk(KERN_DEBUG "pcibios_enbale_slot(slot=%x)\n", slot);
236 #endif
237
238         rp = (volatile unsigned long *) COMEM_BASE;
239         wp = (volatile unsigned short *) COMEM_BASE;
240         idsel = COMEM_DA_ADDR(0x1 << (slot + 16));
241
242         /* Get current command settings */
243         addr = COMEM_PCIBUS + PCI_COMMAND;
244         addr = (addr & ~0x3) + (~addr & 0x02);
245         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
246         cmd = wp[WREG(addr)];
247         /*val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);*/
248
249         /* Enable I/O and memory accesses to this device */
250         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
251         cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
252         wp[WREG(addr)] = cmd;
253
254         return(0);
255 }
256
257 /*****************************************************************************/
258
259 void pcibios_assign_resources(void)
260 {
261         volatile unsigned long  *rp;
262         unsigned long           sel, id;
263         int                     slot;
264
265         rp = (volatile unsigned long *) COMEM_BASE;
266
267         /*
268          *      Do a quick scan of the PCI bus and see what is here.
269          */
270         for (slot = COMEM_MINDEV; (slot <= COMEM_MAXDEV); slot++) {
271                 sel = COMEM_DA_CFGRD | COMEM_DA_ADDR(0x1 << (slot + 16));
272                 rp[LREG(COMEM_DAHBASE)] = sel;
273                 rp[LREG(COMEM_PCIBUS)] = 0; /* Clear bus */
274                 id = rp[LREG(COMEM_PCIBUS)];
275                 if ((id != 0) && ((id & 0xffff0000) != (sel & 0xffff0000))) {
276                         printk(KERN_INFO "PCI: slot=%d id=%08x\n", slot, (int) id);
277                         pci_slotmask |= 0x1 << slot;
278                         pcibios_assign_resource_slot(slot);
279                         pcibios_enable_slot(slot);
280                 }
281         }
282 }
283
284 /*****************************************************************************/
285
286 int pcibios_init(void)
287 {
288         volatile unsigned long  *rp;
289         unsigned long           sel, id;
290         int                     slot;
291
292 #ifdef DEBUGPCI
293         printk(KERN_DEBUG "pcibios_init()\n");
294 #endif
295
296         pci_resetbus();
297
298         /*
299          *      Do some sort of basic check to see if the CO-MEM part
300          *      is present... This works ok, but I think we really need
301          *      something better...
302          */
303         rp = (volatile unsigned long *) COMEM_BASE;
304         if ((rp[LREG(COMEM_LBUSCFG)] & 0xff) != 0x50) {
305                 printk(KERN_INFO "PCI: no PCI bus present\n");
306                 return(0);
307         }
308
309 #ifdef COMEM_BRIDGEDEV
310         /*
311          *      Setup the PCI bridge device first. It needs resources too,
312          *      so that bus masters can get to its shared memory.
313          */
314         slot = COMEM_BRIDGEDEV;
315         sel = COMEM_DA_CFGRD | COMEM_DA_ADDR(0x1 << (slot + 16));
316         rp[LREG(COMEM_DAHBASE)] = sel;
317         rp[LREG(COMEM_PCIBUS)] = 0; /* Clear bus */
318         id = rp[LREG(COMEM_PCIBUS)];
319         if ((id == 0) || ((id & 0xffff0000) == (sel & 0xffff0000))) {
320                 printk(KERN_INFO "PCI: no PCI bus bridge present\n");
321                 return(0);
322         }
323
324         printk(KERN_INFO "PCI: bridge device at slot=%d id=%08x\n", slot, (int) id);
325         pci_slotmask |= 0x1 << slot;
326         pci_shmemaddr = pci_membase;
327         pcibios_assign_resource_slot(slot);
328         pcibios_enable_slot(slot);
329 #endif
330
331         pci_bus_is_present = 1;
332
333         /* Get PCI irq for local vectoring */
334         if (request_irq(COMEM_IRQ, pci_interrupt, 0, "PCI bridge", NULL)) {
335                 printk(KERN_WARNING "PCI: failed to acquire interrupt %d\n", COMEM_IRQ);
336         } else {
337                 mcf_autovector(COMEM_IRQ);
338         }
339
340         pcibios_assign_resources();
341
342         return(0);
343 }
344
345 /*****************************************************************************/
346
347 char *pcibios_setup(char *option)
348 {
349         /* Nothing for us to handle. */
350         return(option);
351 }
352 /*****************************************************************************/
353
354 struct pci_fixup pcibios_fixups[] = { { 0 } };
355
356 void pcibios_fixup_bus(struct pci_bus *b)
357 {
358 }
359
360 /*****************************************************************************/
361
362 void pcibios_align_resource(void *data, struct resource *res, unsigned long size, unsigned long align)
363 {
364 }
365
366 /*****************************************************************************/
367
368 int pcibios_enable_device(struct pci_dev *dev, int mask)
369 {
370         int slot;
371
372         slot = PCI_SLOT(dev->devfn);
373         if ((dev->bus == 0) && (pci_slotmask & (1 << slot)))
374                 pcibios_enable_slot(slot);
375         return(0);
376 }
377
378 /*****************************************************************************/
379
380 void pcibios_update_resource(struct pci_dev *dev, struct resource *root, struct resource *r, int resource)
381 {
382         printk(KERN_WARNING "%s(%d): no support for changing PCI resources...\n",
383                 __FILE__, __LINE__);
384 }
385
386
387 /*****************************************************************************/
388
389 /*
390  *      Local routines to interrcept the standard I/O and vector handling
391  *      code. Don't include this 'till now - initialization code above needs
392  *      access to the real code too.
393  */
394 #include <asm/mcfpci.h>
395
396 /*****************************************************************************/
397
398 void pci_outb(unsigned char val, unsigned int addr)
399 {
400         volatile unsigned long  *rp;
401         volatile unsigned char  *bp;
402
403 #ifdef DEBUGIO
404         printk(KERN_DEBUG "pci_outb(val=%02x,addr=%x)\n", val, addr);
405 #endif
406
407         rp = (volatile unsigned long *) COMEM_BASE;
408         bp = (volatile unsigned char *) COMEM_BASE;
409         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
410         addr = (addr & ~0x3) + (~addr & 0x03);
411         bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
412 }
413
414 /*****************************************************************************/
415
416 void pci_outw(unsigned short val, unsigned int addr)
417 {
418         volatile unsigned long  *rp;
419         volatile unsigned short *sp;
420
421 #ifdef DEBUGIO
422         printk(KERN_DEBUG "pci_outw(val=%04x,addr=%x)\n", val, addr);
423 #endif
424
425         rp = (volatile unsigned long *) COMEM_BASE;
426         sp = (volatile unsigned short *) COMEM_BASE;
427         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
428         addr = (addr & ~0x3) + (~addr & 0x02);
429         if (pci_byteswap)
430                 val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
431         sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
432 }
433
434 /*****************************************************************************/
435
436 void pci_outl(unsigned int val, unsigned int addr)
437 {
438         volatile unsigned long  *rp;
439         volatile unsigned int   *lp;
440
441 #ifdef DEBUGIO
442         printk(KERN_DEBUG "pci_outl(val=%08x,addr=%x)\n", val, addr);
443 #endif
444
445         rp = (volatile unsigned long *) COMEM_BASE;
446         lp = (volatile unsigned int *) COMEM_BASE;
447         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
448
449         if (pci_byteswap)
450                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
451                         ((val & 0x00ff0000) >> 8) | (val >> 24);
452
453         lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
454 }
455
456 /*****************************************************************************/
457
458 unsigned long   pci_blmask[] = {
459         0x000000e0,
460         0x000000d0,
461         0x000000b0,
462         0x00000070
463 };
464
465 unsigned char pci_inb(unsigned int addr)
466 {
467         volatile unsigned long  *rp;
468         volatile unsigned char  *bp;
469         unsigned long           r;
470         unsigned char           val;
471
472 #ifdef DEBUGIO
473         printk(KERN_DEBUG "pci_inb(addr=%x)\n", addr);
474 #endif
475
476         rp = (volatile unsigned long *) COMEM_BASE;
477         bp = (volatile unsigned char *) COMEM_BASE;
478
479         r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_blmask[(addr & 0x3)];
480         rp[LREG(COMEM_DAHBASE)] = r;
481
482         addr = (addr & ~0x3) + (~addr & 0x3);
483         val = bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
484         return(val);
485 }
486
487 /*****************************************************************************/
488
489 unsigned long   pci_bwmask[] = {
490         0x000000c0,
491         0x000000c0,
492         0x00000030,
493         0x00000030
494 };
495
496 unsigned short pci_inw(unsigned int addr)
497 {
498         volatile unsigned long  *rp;
499         volatile unsigned short *sp;
500         unsigned long           r;
501         unsigned short          val;
502
503 #ifdef DEBUGIO
504         printk(KERN_DEBUG "pci_inw(addr=%x)", addr);
505 #endif
506
507         rp = (volatile unsigned long *) COMEM_BASE;
508         r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_bwmask[(addr & 0x3)];
509         rp[LREG(COMEM_DAHBASE)] = r;
510
511         sp = (volatile unsigned short *) COMEM_BASE;
512         addr = (addr & ~0x3) + (~addr & 0x02);
513         val = sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
514         if (pci_byteswap)
515                 val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
516 #ifdef DEBUGIO
517         printk(KERN_DEBUG "=%04x\n", val);
518 #endif
519         return(val);
520 }
521
522 /*****************************************************************************/
523
524 unsigned int pci_inl(unsigned int addr)
525 {
526         volatile unsigned long  *rp;
527         volatile unsigned int   *lp;
528         unsigned int            val;
529
530 #ifdef DEBUGIO
531         printk(KERN_DEBUG "pci_inl(addr=%x)", addr);
532 #endif
533
534         rp = (volatile unsigned long *) COMEM_BASE;
535         lp = (volatile unsigned int *) COMEM_BASE;
536         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(addr);
537         val = lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
538
539         if (pci_byteswap)
540                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
541                         ((val & 0x00ff0000) >> 8) | (val >> 24);
542
543 #ifdef DEBUGIO
544         printk(KERN_DEBUG "=%08x\n", val);
545 #endif
546         return(val);
547 }
548
549 /*****************************************************************************/
550
551 void pci_outsb(void *addr, void *buf, int len)
552 {
553         volatile unsigned long  *rp;
554         volatile unsigned char  *bp;
555         unsigned char           *dp = (unsigned char *) buf;
556         unsigned int            a = (unsigned int) addr;
557
558 #ifdef DEBUGIO
559         printk(KERN_DEBUG "pci_outsb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
560 #endif
561
562         rp = (volatile unsigned long *) COMEM_BASE;
563         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
564
565         a = (a & ~0x3) + (~a & 0x03);
566         bp = (volatile unsigned char *)
567                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
568
569         while (len--)
570                 *bp = *dp++;
571 }
572
573 /*****************************************************************************/
574
575 void pci_outsw(void *addr, void *buf, int len)
576 {
577         volatile unsigned long  *rp;
578         volatile unsigned short *wp;
579         unsigned short          w, *dp = (unsigned short *) buf;
580         unsigned int            a = (unsigned int) addr;
581
582 #ifdef DEBUGIO
583         printk(KERN_DEBUG "pci_outsw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
584 #endif
585
586         rp = (volatile unsigned long *) COMEM_BASE;
587         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
588
589         a = (a & ~0x3) + (~a & 0x2);
590         wp = (volatile unsigned short *)
591                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
592
593         while (len--) {
594                 w = *dp++;
595                 if (pci_byteswap)
596                         w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
597                 *wp = w;
598         }
599 }
600
601 /*****************************************************************************/
602
603 void pci_outsl(void *addr, void *buf, int len)
604 {
605         volatile unsigned long  *rp;
606         volatile unsigned long  *lp;
607         unsigned long           l, *dp = (unsigned long *) buf;
608         unsigned int            a = (unsigned int) addr;
609
610 #ifdef DEBUGIO
611         printk(KERN_DEBUG "pci_outsl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
612 #endif
613
614         rp = (volatile unsigned long *) COMEM_BASE;
615         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
616
617         lp = (volatile unsigned long *)
618                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
619
620         while (len--) {
621                 l = *dp++;
622                 if (pci_byteswap)
623                         l = (l << 24) | ((l & 0x0000ff00) << 8) |
624                                 ((l & 0x00ff0000) >> 8) | (l >> 24);
625                 *lp = l;
626         }
627 }
628
629 /*****************************************************************************/
630
631 void pci_insb(void *addr, void *buf, int len)
632 {
633         volatile unsigned long  *rp;
634         volatile unsigned char  *bp;
635         unsigned char           *dp = (unsigned char *) buf;
636         unsigned int            a = (unsigned int) addr;
637
638 #ifdef DEBUGIO
639         printk(KERN_DEBUG "pci_insb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
640 #endif
641
642         rp = (volatile unsigned long *) COMEM_BASE;
643         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
644
645         a = (a & ~0x3) + (~a & 0x03);
646         bp = (volatile unsigned char *)
647                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
648
649         while (len--)
650                 *dp++ = *bp;
651 }
652
653 /*****************************************************************************/
654
655 void pci_insw(void *addr, void *buf, int len)
656 {
657         volatile unsigned long  *rp;
658         volatile unsigned short *wp;
659         unsigned short          w, *dp = (unsigned short *) buf;
660         unsigned int            a = (unsigned int) addr;
661
662 #ifdef DEBUGIO
663         printk(KERN_DEBUG "pci_insw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
664 #endif
665
666         rp = (volatile unsigned long *) COMEM_BASE;
667         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
668
669         a = (a & ~0x3) + (~a & 0x2);
670         wp = (volatile unsigned short *)
671                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
672
673         while (len--) {
674                 w = *wp;
675                 if (pci_byteswap)
676                         w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
677                 *dp++ = w;
678         }
679 }
680
681 /*****************************************************************************/
682
683 void pci_insl(void *addr, void *buf, int len)
684 {
685         volatile unsigned long  *rp;
686         volatile unsigned long  *lp;
687         unsigned long           l, *dp = (unsigned long *) buf;
688         unsigned int            a = (unsigned int) addr;
689
690 #ifdef DEBUGIO
691         printk(KERN_DEBUG "pci_insl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
692 #endif
693
694         rp = (volatile unsigned long *) COMEM_BASE;
695         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
696
697         lp = (volatile unsigned long *)
698                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
699
700         while (len--) {
701                 l = *lp;
702                 if (pci_byteswap)
703                         l = (l << 24) | ((l & 0x0000ff00) << 8) |
704                                 ((l & 0x00ff0000) >> 8) | (l >> 24);
705                 *dp++ = l;
706         }
707 }
708
709 /*****************************************************************************/
710
711 struct pci_localirqlist {
712         void            (*handler)(int, void *, struct pt_regs *);
713         const char      *device;
714         void            *dev_id;
715 };
716
717 struct pci_localirqlist pci_irqlist[COMEM_MAXPCI];
718
719 /*****************************************************************************/
720
721 int pci_request_irq(unsigned int irq,
722         void (*handler)(int, void *, struct pt_regs *),
723         unsigned long flags, const char *device, void *dev_id)
724 {
725         int     i;
726
727 #ifdef DEBUGIO
728         printk(KERN_DEBUG "pci_request_irq(irq=%d,handler=%x,flags=%x,device=%s,"
729                 "dev_id=%x)\n", irq, (int) handler, (int) flags, device,
730                 (int) dev_id);
731 #endif
732
733         /* Check if this interrupt handler is already lodged */
734         for (i = 0; (i < COMEM_MAXPCI); i++) {
735                 if (pci_irqlist[i].handler == handler)
736                         return(0);
737         }
738
739         /* Find a free spot to put this handler */
740         for (i = 0; (i < COMEM_MAXPCI); i++) {
741                 if (pci_irqlist[i].handler == 0) {
742                         pci_irqlist[i].handler = handler;
743                         pci_irqlist[i].device = device;
744                         pci_irqlist[i].dev_id = dev_id;
745                         return(0);
746                 }
747         }
748
749         /* Couldn't fit?? */
750         return(1);
751 }
752
753 /*****************************************************************************/
754
755 void pci_free_irq(unsigned int irq, void *dev_id)
756 {
757         int     i;
758
759 #ifdef DEBUGIO
760         printk(KERN_DEBUG "pci_free_irq(irq=%d,dev_id=%x)\n", irq, (int) dev_id);
761 #endif
762
763         if (dev_id == (void *) NULL)
764                 return;
765
766         /* Check if this interrupt handler is lodged */
767         for (i = 0; (i < COMEM_MAXPCI); i++) {
768                 if (pci_irqlist[i].dev_id == dev_id) {
769                         pci_irqlist[i].handler = NULL;
770                         pci_irqlist[i].device = NULL;
771                         pci_irqlist[i].dev_id = NULL;
772                         break;
773                 }
774         }
775 }
776
777 /*****************************************************************************/
778
779 void pci_interrupt(int irq, void *id, struct pt_regs *fp)
780 {
781         int     i;
782
783 #ifdef DEBUGIO
784         printk(KERN_DEBUG "pci_interrupt(irq=%d,id=%x,fp=%x)\n", irq, (int) id, (int) fp);
785 #endif
786
787         for (i = 0; (i < COMEM_MAXPCI); i++) {
788                 if (pci_irqlist[i].handler)
789                         (*pci_irqlist[i].handler)(irq,pci_irqlist[i].dev_id,fp);
790         }
791 }
792
793 /*****************************************************************************/
794
795 /*
796  *      The shared memory region is broken up into contiguous 512 byte
797  *      regions for easy allocation... This is not an optimal solution
798  *      but it makes allocation and freeing regions really easy.
799  */
800
801 #define PCI_MEMSLOTSIZE         512
802 #define PCI_MEMSLOTS            (COMEM_SHMEMSIZE / PCI_MEMSLOTSIZE)
803
804 char    pci_shmemmap[PCI_MEMSLOTS];
805
806
807 void *pci_bmalloc(int size)
808 {
809         int     i, j, nrslots;
810
811 #ifdef DEBUGIO
812         printk(KERN_DEBUG "pci_bmalloc(size=%d)\n", size);
813 #endif
814
815         if (size <= 0)
816                 return((void *) NULL);
817
818         nrslots = (size - 1) / PCI_MEMSLOTSIZE;
819
820         for (i = 0; (i < (PCI_MEMSLOTS-nrslots)); i++) {
821                 if (pci_shmemmap[i] == 0) {
822                         for (j = i+1; (j < (i+nrslots)); j++) {
823                                 if (pci_shmemmap[j])
824                                         goto restart;
825                         }
826
827                         for (j = i; (j <= i+nrslots); j++)
828                                 pci_shmemmap[j] = 1;
829                         break;
830                 }
831 restart:
832         }
833
834         return((void *) (COMEM_BASE + COMEM_SHMEM + (i * PCI_MEMSLOTSIZE)));
835 }
836
837 /*****************************************************************************/
838
839 void pci_bmfree(void *mp, int size)
840 {
841         int     i, j, nrslots;
842
843 #ifdef DEBUGIO
844         printk(KERN_DEBUG "pci_bmfree(mp=%x,size=%d)\n", (int) mp, size);
845 #endif
846
847         nrslots = size / PCI_MEMSLOTSIZE;
848         i = (((unsigned long) mp) - (COMEM_BASE + COMEM_SHMEM)) /
849                 PCI_MEMSLOTSIZE;
850
851         for (j = i; (j < (i+nrslots)); j++)
852                 pci_shmemmap[j] = 0;
853 }
854
855 /*****************************************************************************/
856
857 unsigned long pci_virt_to_bus(volatile void *address)
858 {
859         unsigned long   l;
860
861 #ifdef DEBUGIO
862         printk(KERN_DEBUG "pci_virt_to_bus(address=%x)", (int) address);
863 #endif
864
865         l = ((unsigned long) address) - COMEM_BASE;
866 #ifdef DEBUGIO
867         printk(KERN_DEBUG "=%x\n", (int) (l+pci_shmemaddr));
868 #endif
869         return(l + pci_shmemaddr);
870 }
871
872 /*****************************************************************************/
873
874 void *pci_bus_to_virt(unsigned long address)
875 {
876         unsigned long   l;
877
878 #ifdef DEBUGIO
879         printk(KERN_DEBUG "pci_bus_to_virt(address=%x)", (int) address);
880 #endif
881
882         l = address - pci_shmemaddr;
883 #ifdef DEBUGIO
884         printk(KERN_DEBUG "=%x\n", (int) (address + COMEM_BASE));
885 #endif
886         return((void *) (address + COMEM_BASE));
887 }
888
889 /*****************************************************************************/
890
891 void pci_bmcpyto(void *dst, void *src, int len)
892 {
893         unsigned long   *dp, *sp, val;
894         unsigned char   *dcp, *scp;
895         int             i, j;
896
897 #ifdef DEBUGIO
898         printk(KERN_DEBUG "pci_bmcpyto(dst=%x,src=%x,len=%d)\n", (int)dst, (int)src, len);
899 #endif
900
901         dp = (unsigned long *) dst;
902         sp = (unsigned long *) src;
903         i = len >> 2;
904
905 #if 0
906         printk(KERN_INFO "DATA:");
907         scp = (unsigned char *) sp;
908         for (i = 0; (i < len); i++) {
909                 if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
910                 printk(KERN_INFO "%02x ", *scp++);
911         }
912         printk(KERN_INFO "\n");
913 #endif
914
915         for (j = 0; (i >= 0); i--, j++) {
916                 val = *sp++;
917                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
918                         ((val & 0x00ff0000) >> 8) | (val >> 24);
919                 *dp++ = val;
920         }
921
922         if (len & 0x3) {
923                 dcp = (unsigned char *) dp;
924                 scp = ((unsigned char *) sp) + 3;
925                 for (i = 0; (i < (len & 0x3)); i++)
926                         *dcp++ = *scp--;
927         }
928 }
929
930 /*****************************************************************************/
931
932 void pci_bmcpyfrom(void *dst, void *src, int len)
933 {
934         unsigned long   *dp, *sp, val;
935         unsigned char   *dcp, *scp;
936         int             i;
937
938 #ifdef DEBUGIO
939         printk(KERN_DEBUG "pci_bmcpyfrom(dst=%x,src=%x,len=%d)\n",(int)dst,(int)src,len);
940 #endif
941
942         dp = (unsigned long *) dst;
943         sp = (unsigned long *) src;
944         i = len >> 2;
945
946         for (; (i >= 0); i--) {
947                 val = *sp++;
948                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
949                         ((val & 0x00ff0000) >> 8) | (val >> 24);
950                 *dp++ = val;
951         }
952
953         if (len & 0x3) {
954                 dcp = ((unsigned char *) dp) + 3;
955                 scp = (unsigned char *) sp;
956                 for (i = 0; (i < (len & 0x3)); i++)
957                         *dcp++ = *scp--;
958         }
959
960 #if 0
961         printk(KERN_INFO "DATA:");
962         dcp = (unsigned char *) dst;
963         for (i = 0; (i < len); i++) {
964                 if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
965                 printk(KERN_INFO "%02x ", *dcp++);
966         }
967         printk(KERN_INFO "\n");
968 #endif
969 }
970
971 /*****************************************************************************/
972
973 void *pci_alloc_consistent(struct pci_dev *dev, size_t size, dma_addr_t *dma_addr)
974 {
975         void *mp;
976         if ((mp = pci_bmalloc(size)) != NULL) {
977                 dma_addr = mp - (COMEM_BASE + COMEM_SHMEM);
978                 return(mp);
979         }
980         *dma_addr = (dma_addr_t) NULL;
981         return(NULL);
982 }
983
984 /*****************************************************************************/
985
986 void pci_free_consistent(struct pci_dev *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr)
987 {
988         pci_bmfree(cpu_addr, size);
989 }
990
991 /*****************************************************************************/