Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6
[sfrench/cifs-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 void pcibios_fixup_bus(struct pci_bus *b)
355 {
356 }
357
358 /*****************************************************************************/
359
360 void pcibios_align_resource(void *data, struct resource *res,
361                                 resource_size_t size, resource_size_t align)
362 {
363 }
364
365 /*****************************************************************************/
366
367 int pcibios_enable_device(struct pci_dev *dev, int mask)
368 {
369         int slot;
370
371         slot = PCI_SLOT(dev->devfn);
372         if ((dev->bus == 0) && (pci_slotmask & (1 << slot)))
373                 pcibios_enable_slot(slot);
374         return(0);
375 }
376
377 /*****************************************************************************/
378
379 void pcibios_update_resource(struct pci_dev *dev, struct resource *root, struct resource *r, int resource)
380 {
381         printk(KERN_WARNING "%s(%d): no support for changing PCI resources...\n",
382                 __FILE__, __LINE__);
383 }
384
385
386 /*****************************************************************************/
387
388 /*
389  *      Local routines to interrcept the standard I/O and vector handling
390  *      code. Don't include this 'till now - initialization code above needs
391  *      access to the real code too.
392  */
393 #include <asm/mcfpci.h>
394
395 /*****************************************************************************/
396
397 void pci_outb(unsigned char val, unsigned int addr)
398 {
399         volatile unsigned long  *rp;
400         volatile unsigned char  *bp;
401
402 #ifdef DEBUGIO
403         printk(KERN_DEBUG "pci_outb(val=%02x,addr=%x)\n", val, addr);
404 #endif
405
406         rp = (volatile unsigned long *) COMEM_BASE;
407         bp = (volatile unsigned char *) COMEM_BASE;
408         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
409         addr = (addr & ~0x3) + (~addr & 0x03);
410         bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
411 }
412
413 /*****************************************************************************/
414
415 void pci_outw(unsigned short val, unsigned int addr)
416 {
417         volatile unsigned long  *rp;
418         volatile unsigned short *sp;
419
420 #ifdef DEBUGIO
421         printk(KERN_DEBUG "pci_outw(val=%04x,addr=%x)\n", val, addr);
422 #endif
423
424         rp = (volatile unsigned long *) COMEM_BASE;
425         sp = (volatile unsigned short *) COMEM_BASE;
426         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
427         addr = (addr & ~0x3) + (~addr & 0x02);
428         if (pci_byteswap)
429                 val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
430         sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
431 }
432
433 /*****************************************************************************/
434
435 void pci_outl(unsigned int val, unsigned int addr)
436 {
437         volatile unsigned long  *rp;
438         volatile unsigned int   *lp;
439
440 #ifdef DEBUGIO
441         printk(KERN_DEBUG "pci_outl(val=%08x,addr=%x)\n", val, addr);
442 #endif
443
444         rp = (volatile unsigned long *) COMEM_BASE;
445         lp = (volatile unsigned int *) COMEM_BASE;
446         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
447
448         if (pci_byteswap)
449                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
450                         ((val & 0x00ff0000) >> 8) | (val >> 24);
451
452         lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
453 }
454
455 /*****************************************************************************/
456
457 unsigned long   pci_blmask[] = {
458         0x000000e0,
459         0x000000d0,
460         0x000000b0,
461         0x00000070
462 };
463
464 unsigned char pci_inb(unsigned int addr)
465 {
466         volatile unsigned long  *rp;
467         volatile unsigned char  *bp;
468         unsigned long           r;
469         unsigned char           val;
470
471 #ifdef DEBUGIO
472         printk(KERN_DEBUG "pci_inb(addr=%x)\n", addr);
473 #endif
474
475         rp = (volatile unsigned long *) COMEM_BASE;
476         bp = (volatile unsigned char *) COMEM_BASE;
477
478         r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_blmask[(addr & 0x3)];
479         rp[LREG(COMEM_DAHBASE)] = r;
480
481         addr = (addr & ~0x3) + (~addr & 0x3);
482         val = bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
483         return(val);
484 }
485
486 /*****************************************************************************/
487
488 unsigned long   pci_bwmask[] = {
489         0x000000c0,
490         0x000000c0,
491         0x00000030,
492         0x00000030
493 };
494
495 unsigned short pci_inw(unsigned int addr)
496 {
497         volatile unsigned long  *rp;
498         volatile unsigned short *sp;
499         unsigned long           r;
500         unsigned short          val;
501
502 #ifdef DEBUGIO
503         printk(KERN_DEBUG "pci_inw(addr=%x)", addr);
504 #endif
505
506         rp = (volatile unsigned long *) COMEM_BASE;
507         r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_bwmask[(addr & 0x3)];
508         rp[LREG(COMEM_DAHBASE)] = r;
509
510         sp = (volatile unsigned short *) COMEM_BASE;
511         addr = (addr & ~0x3) + (~addr & 0x02);
512         val = sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
513         if (pci_byteswap)
514                 val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
515 #ifdef DEBUGIO
516         printk(KERN_DEBUG "=%04x\n", val);
517 #endif
518         return(val);
519 }
520
521 /*****************************************************************************/
522
523 unsigned int pci_inl(unsigned int addr)
524 {
525         volatile unsigned long  *rp;
526         volatile unsigned int   *lp;
527         unsigned int            val;
528
529 #ifdef DEBUGIO
530         printk(KERN_DEBUG "pci_inl(addr=%x)", addr);
531 #endif
532
533         rp = (volatile unsigned long *) COMEM_BASE;
534         lp = (volatile unsigned int *) COMEM_BASE;
535         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(addr);
536         val = lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
537
538         if (pci_byteswap)
539                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
540                         ((val & 0x00ff0000) >> 8) | (val >> 24);
541
542 #ifdef DEBUGIO
543         printk(KERN_DEBUG "=%08x\n", val);
544 #endif
545         return(val);
546 }
547
548 /*****************************************************************************/
549
550 void pci_outsb(void *addr, void *buf, int len)
551 {
552         volatile unsigned long  *rp;
553         volatile unsigned char  *bp;
554         unsigned char           *dp = (unsigned char *) buf;
555         unsigned int            a = (unsigned int) addr;
556
557 #ifdef DEBUGIO
558         printk(KERN_DEBUG "pci_outsb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
559 #endif
560
561         rp = (volatile unsigned long *) COMEM_BASE;
562         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
563
564         a = (a & ~0x3) + (~a & 0x03);
565         bp = (volatile unsigned char *)
566                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
567
568         while (len--)
569                 *bp = *dp++;
570 }
571
572 /*****************************************************************************/
573
574 void pci_outsw(void *addr, void *buf, int len)
575 {
576         volatile unsigned long  *rp;
577         volatile unsigned short *wp;
578         unsigned short          w, *dp = (unsigned short *) buf;
579         unsigned int            a = (unsigned int) addr;
580
581 #ifdef DEBUGIO
582         printk(KERN_DEBUG "pci_outsw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
583 #endif
584
585         rp = (volatile unsigned long *) COMEM_BASE;
586         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
587
588         a = (a & ~0x3) + (~a & 0x2);
589         wp = (volatile unsigned short *)
590                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
591
592         while (len--) {
593                 w = *dp++;
594                 if (pci_byteswap)
595                         w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
596                 *wp = w;
597         }
598 }
599
600 /*****************************************************************************/
601
602 void pci_outsl(void *addr, void *buf, int len)
603 {
604         volatile unsigned long  *rp;
605         volatile unsigned long  *lp;
606         unsigned long           l, *dp = (unsigned long *) buf;
607         unsigned int            a = (unsigned int) addr;
608
609 #ifdef DEBUGIO
610         printk(KERN_DEBUG "pci_outsl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
611 #endif
612
613         rp = (volatile unsigned long *) COMEM_BASE;
614         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
615
616         lp = (volatile unsigned long *)
617                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
618
619         while (len--) {
620                 l = *dp++;
621                 if (pci_byteswap)
622                         l = (l << 24) | ((l & 0x0000ff00) << 8) |
623                                 ((l & 0x00ff0000) >> 8) | (l >> 24);
624                 *lp = l;
625         }
626 }
627
628 /*****************************************************************************/
629
630 void pci_insb(void *addr, void *buf, int len)
631 {
632         volatile unsigned long  *rp;
633         volatile unsigned char  *bp;
634         unsigned char           *dp = (unsigned char *) buf;
635         unsigned int            a = (unsigned int) addr;
636
637 #ifdef DEBUGIO
638         printk(KERN_DEBUG "pci_insb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
639 #endif
640
641         rp = (volatile unsigned long *) COMEM_BASE;
642         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
643
644         a = (a & ~0x3) + (~a & 0x03);
645         bp = (volatile unsigned char *)
646                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
647
648         while (len--)
649                 *dp++ = *bp;
650 }
651
652 /*****************************************************************************/
653
654 void pci_insw(void *addr, void *buf, int len)
655 {
656         volatile unsigned long  *rp;
657         volatile unsigned short *wp;
658         unsigned short          w, *dp = (unsigned short *) buf;
659         unsigned int            a = (unsigned int) addr;
660
661 #ifdef DEBUGIO
662         printk(KERN_DEBUG "pci_insw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
663 #endif
664
665         rp = (volatile unsigned long *) COMEM_BASE;
666         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
667
668         a = (a & ~0x3) + (~a & 0x2);
669         wp = (volatile unsigned short *)
670                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
671
672         while (len--) {
673                 w = *wp;
674                 if (pci_byteswap)
675                         w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
676                 *dp++ = w;
677         }
678 }
679
680 /*****************************************************************************/
681
682 void pci_insl(void *addr, void *buf, int len)
683 {
684         volatile unsigned long  *rp;
685         volatile unsigned long  *lp;
686         unsigned long           l, *dp = (unsigned long *) buf;
687         unsigned int            a = (unsigned int) addr;
688
689 #ifdef DEBUGIO
690         printk(KERN_DEBUG "pci_insl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
691 #endif
692
693         rp = (volatile unsigned long *) COMEM_BASE;
694         rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
695
696         lp = (volatile unsigned long *)
697                 (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
698
699         while (len--) {
700                 l = *lp;
701                 if (pci_byteswap)
702                         l = (l << 24) | ((l & 0x0000ff00) << 8) |
703                                 ((l & 0x00ff0000) >> 8) | (l >> 24);
704                 *dp++ = l;
705         }
706 }
707
708 /*****************************************************************************/
709
710 struct pci_localirqlist {
711         void            (*handler)(int, void *, struct pt_regs *);
712         const char      *device;
713         void            *dev_id;
714 };
715
716 struct pci_localirqlist pci_irqlist[COMEM_MAXPCI];
717
718 /*****************************************************************************/
719
720 int pci_request_irq(unsigned int irq,
721         void (*handler)(int, void *, struct pt_regs *),
722         unsigned long flags, const char *device, void *dev_id)
723 {
724         int     i;
725
726 #ifdef DEBUGIO
727         printk(KERN_DEBUG "pci_request_irq(irq=%d,handler=%x,flags=%x,device=%s,"
728                 "dev_id=%x)\n", irq, (int) handler, (int) flags, device,
729                 (int) dev_id);
730 #endif
731
732         /* Check if this interrupt handler is already lodged */
733         for (i = 0; (i < COMEM_MAXPCI); i++) {
734                 if (pci_irqlist[i].handler == handler)
735                         return(0);
736         }
737
738         /* Find a free spot to put this handler */
739         for (i = 0; (i < COMEM_MAXPCI); i++) {
740                 if (pci_irqlist[i].handler == 0) {
741                         pci_irqlist[i].handler = handler;
742                         pci_irqlist[i].device = device;
743                         pci_irqlist[i].dev_id = dev_id;
744                         return(0);
745                 }
746         }
747
748         /* Couldn't fit?? */
749         return(1);
750 }
751
752 /*****************************************************************************/
753
754 void pci_free_irq(unsigned int irq, void *dev_id)
755 {
756         int     i;
757
758 #ifdef DEBUGIO
759         printk(KERN_DEBUG "pci_free_irq(irq=%d,dev_id=%x)\n", irq, (int) dev_id);
760 #endif
761
762         if (dev_id == (void *) NULL)
763                 return;
764
765         /* Check if this interrupt handler is lodged */
766         for (i = 0; (i < COMEM_MAXPCI); i++) {
767                 if (pci_irqlist[i].dev_id == dev_id) {
768                         pci_irqlist[i].handler = NULL;
769                         pci_irqlist[i].device = NULL;
770                         pci_irqlist[i].dev_id = NULL;
771                         break;
772                 }
773         }
774 }
775
776 /*****************************************************************************/
777
778 void pci_interrupt(int irq, void *id, struct pt_regs *fp)
779 {
780         int     i;
781
782 #ifdef DEBUGIO
783         printk(KERN_DEBUG "pci_interrupt(irq=%d,id=%x,fp=%x)\n", irq, (int) id, (int) fp);
784 #endif
785
786         for (i = 0; (i < COMEM_MAXPCI); i++) {
787                 if (pci_irqlist[i].handler)
788                         (*pci_irqlist[i].handler)(irq,pci_irqlist[i].dev_id,fp);
789         }
790 }
791
792 /*****************************************************************************/
793
794 /*
795  *      The shared memory region is broken up into contiguous 512 byte
796  *      regions for easy allocation... This is not an optimal solution
797  *      but it makes allocation and freeing regions really easy.
798  */
799
800 #define PCI_MEMSLOTSIZE         512
801 #define PCI_MEMSLOTS            (COMEM_SHMEMSIZE / PCI_MEMSLOTSIZE)
802
803 char    pci_shmemmap[PCI_MEMSLOTS];
804
805
806 void *pci_bmalloc(int size)
807 {
808         int     i, j, nrslots;
809
810 #ifdef DEBUGIO
811         printk(KERN_DEBUG "pci_bmalloc(size=%d)\n", size);
812 #endif
813
814         if (size <= 0)
815                 return((void *) NULL);
816
817         nrslots = (size - 1) / PCI_MEMSLOTSIZE;
818
819         for (i = 0; (i < (PCI_MEMSLOTS-nrslots)); i++) {
820                 if (pci_shmemmap[i] == 0) {
821                         for (j = i+1; (j < (i+nrslots)); j++) {
822                                 if (pci_shmemmap[j])
823                                         goto restart;
824                         }
825
826                         for (j = i; (j <= i+nrslots); j++)
827                                 pci_shmemmap[j] = 1;
828                         break;
829                 }
830 restart:
831         }
832
833         return((void *) (COMEM_BASE + COMEM_SHMEM + (i * PCI_MEMSLOTSIZE)));
834 }
835
836 /*****************************************************************************/
837
838 void pci_bmfree(void *mp, int size)
839 {
840         int     i, j, nrslots;
841
842 #ifdef DEBUGIO
843         printk(KERN_DEBUG "pci_bmfree(mp=%x,size=%d)\n", (int) mp, size);
844 #endif
845
846         nrslots = size / PCI_MEMSLOTSIZE;
847         i = (((unsigned long) mp) - (COMEM_BASE + COMEM_SHMEM)) /
848                 PCI_MEMSLOTSIZE;
849
850         for (j = i; (j < (i+nrslots)); j++)
851                 pci_shmemmap[j] = 0;
852 }
853
854 /*****************************************************************************/
855
856 unsigned long pci_virt_to_bus(volatile void *address)
857 {
858         unsigned long   l;
859
860 #ifdef DEBUGIO
861         printk(KERN_DEBUG "pci_virt_to_bus(address=%x)", (int) address);
862 #endif
863
864         l = ((unsigned long) address) - COMEM_BASE;
865 #ifdef DEBUGIO
866         printk(KERN_DEBUG "=%x\n", (int) (l+pci_shmemaddr));
867 #endif
868         return(l + pci_shmemaddr);
869 }
870
871 /*****************************************************************************/
872
873 void *pci_bus_to_virt(unsigned long address)
874 {
875         unsigned long   l;
876
877 #ifdef DEBUGIO
878         printk(KERN_DEBUG "pci_bus_to_virt(address=%x)", (int) address);
879 #endif
880
881         l = address - pci_shmemaddr;
882 #ifdef DEBUGIO
883         printk(KERN_DEBUG "=%x\n", (int) (address + COMEM_BASE));
884 #endif
885         return((void *) (address + COMEM_BASE));
886 }
887
888 /*****************************************************************************/
889
890 void pci_bmcpyto(void *dst, void *src, int len)
891 {
892         unsigned long   *dp, *sp, val;
893         unsigned char   *dcp, *scp;
894         int             i, j;
895
896 #ifdef DEBUGIO
897         printk(KERN_DEBUG "pci_bmcpyto(dst=%x,src=%x,len=%d)\n", (int)dst, (int)src, len);
898 #endif
899
900         dp = (unsigned long *) dst;
901         sp = (unsigned long *) src;
902         i = len >> 2;
903
904 #if 0
905         printk(KERN_INFO "DATA:");
906         scp = (unsigned char *) sp;
907         for (i = 0; (i < len); i++) {
908                 if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
909                 printk(KERN_INFO "%02x ", *scp++);
910         }
911         printk(KERN_INFO "\n");
912 #endif
913
914         for (j = 0; (i >= 0); i--, j++) {
915                 val = *sp++;
916                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
917                         ((val & 0x00ff0000) >> 8) | (val >> 24);
918                 *dp++ = val;
919         }
920
921         if (len & 0x3) {
922                 dcp = (unsigned char *) dp;
923                 scp = ((unsigned char *) sp) + 3;
924                 for (i = 0; (i < (len & 0x3)); i++)
925                         *dcp++ = *scp--;
926         }
927 }
928
929 /*****************************************************************************/
930
931 void pci_bmcpyfrom(void *dst, void *src, int len)
932 {
933         unsigned long   *dp, *sp, val;
934         unsigned char   *dcp, *scp;
935         int             i;
936
937 #ifdef DEBUGIO
938         printk(KERN_DEBUG "pci_bmcpyfrom(dst=%x,src=%x,len=%d)\n",(int)dst,(int)src,len);
939 #endif
940
941         dp = (unsigned long *) dst;
942         sp = (unsigned long *) src;
943         i = len >> 2;
944
945         for (; (i >= 0); i--) {
946                 val = *sp++;
947                 val = (val << 24) | ((val & 0x0000ff00) << 8) |
948                         ((val & 0x00ff0000) >> 8) | (val >> 24);
949                 *dp++ = val;
950         }
951
952         if (len & 0x3) {
953                 dcp = ((unsigned char *) dp) + 3;
954                 scp = (unsigned char *) sp;
955                 for (i = 0; (i < (len & 0x3)); i++)
956                         *dcp++ = *scp--;
957         }
958
959 #if 0
960         printk(KERN_INFO "DATA:");
961         dcp = (unsigned char *) dst;
962         for (i = 0; (i < len); i++) {
963                 if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
964                 printk(KERN_INFO "%02x ", *dcp++);
965         }
966         printk(KERN_INFO "\n");
967 #endif
968 }
969
970 /*****************************************************************************/
971
972 void *pci_alloc_consistent(struct pci_dev *dev, size_t size, dma_addr_t *dma_addr)
973 {
974         void *mp;
975         if ((mp = pci_bmalloc(size)) != NULL) {
976                 dma_addr = mp - (COMEM_BASE + COMEM_SHMEM);
977                 return(mp);
978         }
979         *dma_addr = (dma_addr_t) NULL;
980         return(NULL);
981 }
982
983 /*****************************************************************************/
984
985 void pci_free_consistent(struct pci_dev *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr)
986 {
987         pci_bmfree(cpu_addr, size);
988 }
989
990 /*****************************************************************************/