vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / kernel / ecard.c
1 /*
2  *  linux/arch/arm/kernel/ecard.c
3  *
4  *  Copyright 1995-2001 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Find all installed expansion cards, and handle interrupts from them.
11  *
12  *  Created from information from Acorns RiscOS3 PRMs
13  *
14  *  08-Dec-1996 RMK     Added code for the 9'th expansion card - the ether
15  *                      podule slot.
16  *  06-May-1997 RMK     Added blacklist for cards whose loader doesn't work.
17  *  12-Sep-1997 RMK     Created new handling of interrupt enables/disables
18  *                      - cards can now register their own routine to control
19  *                      interrupts (recommended).
20  *  29-Sep-1997 RMK     Expansion card interrupt hardware not being re-enabled
21  *                      on reset from Linux. (Caused cards not to respond
22  *                      under RiscOS without hard reset).
23  *  15-Feb-1998 RMK     Added DMA support
24  *  12-Sep-1998 RMK     Added EASI support
25  *  10-Jan-1999 RMK     Run loaders in a simulated RISC OS environment.
26  *  17-Apr-1999 RMK     Support for EASI Type C cycles.
27  */
28 #define ECARD_C
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/completion.h>
37 #include <linux/reboot.h>
38 #include <linux/mm.h>
39 #include <linux/slab.h>
40 #include <linux/proc_fs.h>
41 #include <linux/device.h>
42 #include <linux/init.h>
43
44 #include <asm/dma.h>
45 #include <asm/ecard.h>
46 #include <asm/hardware.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/mmu_context.h>
50 #include <asm/mach/irq.h>
51 #include <asm/tlbflush.h>
52
53 #ifndef CONFIG_ARCH_RPC
54 #define HAVE_EXPMASK
55 #endif
56
57 struct ecard_request {
58         void            (*fn)(struct ecard_request *);
59         ecard_t         *ec;
60         unsigned int    address;
61         unsigned int    length;
62         unsigned int    use_loader;
63         void            *buffer;
64         struct completion *complete;
65 };
66
67 struct expcard_blacklist {
68         unsigned short   manufacturer;
69         unsigned short   product;
70         const char      *type;
71 };
72
73 static ecard_t *cards;
74 static ecard_t *slot_to_expcard[MAX_ECARDS];
75 static unsigned int ectcr;
76 #ifdef HAS_EXPMASK
77 static unsigned int have_expmask;
78 #endif
79
80 /* List of descriptions of cards which don't have an extended
81  * identification, or chunk directories containing a description.
82  */
83 static struct expcard_blacklist __initdata blacklist[] = {
84         { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
85 };
86
87 asmlinkage extern int
88 ecard_loader_reset(volatile unsigned char *pa, loader_t loader);
89 asmlinkage extern int
90 ecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);
91
92 static const struct ecard_id *
93 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec);
94
95 static inline unsigned short
96 ecard_getu16(unsigned char *v)
97 {
98         return v[0] | v[1] << 8;
99 }
100
101 static inline signed long
102 ecard_gets24(unsigned char *v)
103 {
104         return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
105 }
106
107 static inline ecard_t *
108 slot_to_ecard(unsigned int slot)
109 {
110         return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
111 }
112
113 /* ===================== Expansion card daemon ======================== */
114 /*
115  * Since the loader programs on the expansion cards need to be run
116  * in a specific environment, create a separate task with this
117  * environment up, and pass requests to this task as and when we
118  * need to.
119  *
120  * This should allow 99% of loaders to be called from Linux.
121  *
122  * From a security standpoint, we trust the card vendors.  This
123  * may be a misplaced trust.
124  */
125 #define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)
126 #define POD_INT_ADDR(x) ((volatile unsigned char *)\
127                          ((BUS_ADDR((x)) - IO_BASE) + IO_START))
128
129 static void ecard_task_reset(struct ecard_request *req)
130 {
131         struct expansion_card *ec = req->ec;
132         if (ec->loader)
133                 ecard_loader_reset(POD_INT_ADDR(ec->podaddr), ec->loader);
134 }
135
136 static void ecard_task_readbytes(struct ecard_request *req)
137 {
138         unsigned char *buf = (unsigned char *)req->buffer;
139         volatile unsigned char *base_addr =
140                 (volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr);
141         unsigned int len = req->length;
142         unsigned int off = req->address;
143
144         if (req->ec->slot_no == 8) {
145                 /*
146                  * The card maintains an index which increments the address
147                  * into a 4096-byte page on each access.  We need to keep
148                  * track of the counter.
149                  */
150                 static unsigned int index;
151                 unsigned int page;
152
153                 page = (off >> 12) * 4;
154                 if (page > 256 * 4)
155                         return;
156
157                 off &= 4095;
158
159                 /*
160                  * If we are reading offset 0, or our current index is
161                  * greater than the offset, reset the hardware index counter.
162                  */
163                 if (off == 0 || index > off) {
164                         *base_addr = 0;
165                         index = 0;
166                 }
167
168                 /*
169                  * Increment the hardware index counter until we get to the
170                  * required offset.  The read bytes are discarded.
171                  */
172                 while (index < off) {
173                         unsigned char byte;
174                         byte = base_addr[page];
175                         index += 1;
176                 }
177
178                 while (len--) {
179                         *buf++ = base_addr[page];
180                         index += 1;
181                 }
182         } else {
183
184                 if (!req->use_loader || !req->ec->loader) {
185                         off *= 4;
186                         while (len--) {
187                                 *buf++ = base_addr[off];
188                                 off += 4;
189                         }
190                 } else {
191                         while(len--) {
192                                 /*
193                                  * The following is required by some
194                                  * expansion card loader programs.
195                                  */
196                                 *(unsigned long *)0x108 = 0;
197                                 *buf++ = ecard_loader_read(off++, base_addr,
198                                                            req->ec->loader);
199                         }
200                 }
201         }
202
203 }
204
205 static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
206 static struct ecard_request *ecard_req;
207 static DECLARE_MUTEX(ecard_sem);
208
209 /*
210  * Set up the expansion card daemon's page tables.
211  */
212 static void ecard_init_pgtables(struct mm_struct *mm)
213 {
214         struct vm_area_struct vma;
215
216         /* We want to set up the page tables for the following mapping:
217          *  Virtual     Physical
218          *  0x03000000  0x03000000
219          *  0x03010000  unmapped
220          *  0x03210000  0x03210000
221          *  0x03400000  unmapped
222          *  0x08000000  0x08000000
223          *  0x10000000  unmapped
224          *
225          * FIXME: we don't follow this 100% yet.
226          */
227         pgd_t *src_pgd, *dst_pgd;
228
229         src_pgd = pgd_offset(mm, IO_BASE);
230         dst_pgd = pgd_offset(mm, IO_START);
231
232         memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
233
234         src_pgd = pgd_offset(mm, EASI_BASE);
235         dst_pgd = pgd_offset(mm, EASI_START);
236
237         memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
238
239         vma.vm_mm = mm;
240
241         flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
242         flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
243 }
244
245 static int ecard_init_mm(void)
246 {
247         struct mm_struct * mm = mm_alloc();
248         struct mm_struct *active_mm = current->active_mm;
249
250         if (!mm)
251                 return -ENOMEM;
252
253         current->mm = mm;
254         current->active_mm = mm;
255         activate_mm(active_mm, mm);
256         mmdrop(active_mm);
257         ecard_init_pgtables(mm);
258         return 0;
259 }
260
261 static int
262 ecard_task(void * unused)
263 {
264         daemonize("kecardd");
265
266         /*
267          * Allocate a mm.  We're not a lazy-TLB kernel task since we need
268          * to set page table entries where the user space would be.  Note
269          * that this also creates the page tables.  Failure is not an
270          * option here.
271          */
272         if (ecard_init_mm())
273                 panic("kecardd: unable to alloc mm\n");
274
275         while (1) {
276                 struct ecard_request *req;
277
278                 wait_event_interruptible(ecard_wait, ecard_req != NULL);
279
280                 req = xchg(&ecard_req, NULL);
281                 if (req != NULL) {
282                         req->fn(req);
283                         complete(req->complete);
284                 }
285         }
286 }
287
288 /*
289  * Wake the expansion card daemon to action our request.
290  *
291  * FIXME: The test here is not sufficient to detect if the
292  * kcardd is running.
293  */
294 static void ecard_call(struct ecard_request *req)
295 {
296         DECLARE_COMPLETION(completion);
297
298         req->complete = &completion;
299
300         down(&ecard_sem);
301         ecard_req = req;
302         wake_up(&ecard_wait);
303
304         /*
305          * Now wait for kecardd to run.
306          */
307         wait_for_completion(&completion);
308         up(&ecard_sem);
309 }
310
311 /* ======================= Mid-level card control ===================== */
312
313 static void
314 ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
315 {
316         struct ecard_request req;
317
318         req.fn          = ecard_task_readbytes;
319         req.ec          = ec;
320         req.address     = off;
321         req.length      = len;
322         req.use_loader  = useld;
323         req.buffer      = addr;
324
325         ecard_call(&req);
326 }
327
328 int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
329 {
330         struct ex_chunk_dir excd;
331         int index = 16;
332         int useld = 0;
333
334         if (!ec->cid.cd)
335                 return 0;
336
337         while(1) {
338                 ecard_readbytes(&excd, ec, index, 8, useld);
339                 index += 8;
340                 if (c_id(&excd) == 0) {
341                         if (!useld && ec->loader) {
342                                 useld = 1;
343                                 index = 0;
344                                 continue;
345                         }
346                         return 0;
347                 }
348                 if (c_id(&excd) == 0xf0) { /* link */
349                         index = c_start(&excd);
350                         continue;
351                 }
352                 if (c_id(&excd) == 0x80) { /* loader */
353                         if (!ec->loader) {
354                                 ec->loader = (loader_t)kmalloc(c_len(&excd),
355                                                                GFP_KERNEL);
356                                 if (ec->loader)
357                                         ecard_readbytes(ec->loader, ec,
358                                                         (int)c_start(&excd),
359                                                         c_len(&excd), useld);
360                                 else
361                                         return 0;
362                         }
363                         continue;
364                 }
365                 if (c_id(&excd) == id && num-- == 0)
366                         break;
367         }
368
369         if (c_id(&excd) & 0x80) {
370                 switch (c_id(&excd) & 0x70) {
371                 case 0x70:
372                         ecard_readbytes((unsigned char *)excd.d.string, ec,
373                                         (int)c_start(&excd), c_len(&excd),
374                                         useld);
375                         break;
376                 case 0x00:
377                         break;
378                 }
379         }
380         cd->start_offset = c_start(&excd);
381         memcpy(cd->d.string, excd.d.string, 256);
382         return 1;
383 }
384
385 /* ======================= Interrupt control ============================ */
386
387 static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
388 {
389 #ifdef HAS_EXPMASK
390         if (irqnr < 4 && have_expmask) {
391                 have_expmask |= 1 << irqnr;
392                 __raw_writeb(have_expmask, EXPMASK_ENABLE);
393         }
394 #endif
395 }
396
397 static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
398 {
399 #ifdef HAS_EXPMASK
400         if (irqnr < 4 && have_expmask) {
401                 have_expmask &= ~(1 << irqnr);
402                 __raw_writeb(have_expmask, EXPMASK_ENABLE);
403         }
404 #endif
405 }
406
407 static int ecard_def_irq_pending(ecard_t *ec)
408 {
409         return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;
410 }
411
412 static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
413 {
414         panic("ecard_def_fiq_enable called - impossible");
415 }
416
417 static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
418 {
419         panic("ecard_def_fiq_disable called - impossible");
420 }
421
422 static int ecard_def_fiq_pending(ecard_t *ec)
423 {
424         return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;
425 }
426
427 static expansioncard_ops_t ecard_default_ops = {
428         ecard_def_irq_enable,
429         ecard_def_irq_disable,
430         ecard_def_irq_pending,
431         ecard_def_fiq_enable,
432         ecard_def_fiq_disable,
433         ecard_def_fiq_pending
434 };
435
436 /*
437  * Enable and disable interrupts from expansion cards.
438  * (interrupts are disabled for these functions).
439  *
440  * They are not meant to be called directly, but via enable/disable_irq.
441  */
442 static void ecard_irq_unmask(unsigned int irqnr)
443 {
444         ecard_t *ec = slot_to_ecard(irqnr - 32);
445
446         if (ec) {
447                 if (!ec->ops)
448                         ec->ops = &ecard_default_ops;
449
450                 if (ec->claimed && ec->ops->irqenable)
451                         ec->ops->irqenable(ec, irqnr);
452                 else
453                         printk(KERN_ERR "ecard: rejecting request to "
454                                 "enable IRQs for %d\n", irqnr);
455         }
456 }
457
458 static void ecard_irq_mask(unsigned int irqnr)
459 {
460         ecard_t *ec = slot_to_ecard(irqnr - 32);
461
462         if (ec) {
463                 if (!ec->ops)
464                         ec->ops = &ecard_default_ops;
465
466                 if (ec->ops && ec->ops->irqdisable)
467                         ec->ops->irqdisable(ec, irqnr);
468         }
469 }
470
471 static struct irqchip ecard_chip = {
472         .ack    = ecard_irq_mask,
473         .mask   = ecard_irq_mask,
474         .unmask = ecard_irq_unmask,
475 };
476
477 void ecard_enablefiq(unsigned int fiqnr)
478 {
479         ecard_t *ec = slot_to_ecard(fiqnr);
480
481         if (ec) {
482                 if (!ec->ops)
483                         ec->ops = &ecard_default_ops;
484
485                 if (ec->claimed && ec->ops->fiqenable)
486                         ec->ops->fiqenable(ec, fiqnr);
487                 else
488                         printk(KERN_ERR "ecard: rejecting request to "
489                                 "enable FIQs for %d\n", fiqnr);
490         }
491 }
492
493 void ecard_disablefiq(unsigned int fiqnr)
494 {
495         ecard_t *ec = slot_to_ecard(fiqnr);
496
497         if (ec) {
498                 if (!ec->ops)
499                         ec->ops = &ecard_default_ops;
500
501                 if (ec->ops->fiqdisable)
502                         ec->ops->fiqdisable(ec, fiqnr);
503         }
504 }
505
506 static void ecard_dump_irq_state(void)
507 {
508         ecard_t *ec;
509
510         printk("Expansion card IRQ state:\n");
511
512         for (ec = cards; ec; ec = ec->next) {
513                 if (ec->slot_no == 8)
514                         continue;
515
516                 printk("  %d: %sclaimed, ",
517                        ec->slot_no, ec->claimed ? "" : "not ");
518
519                 if (ec->ops && ec->ops->irqpending &&
520                     ec->ops != &ecard_default_ops)
521                         printk("irq %spending\n",
522                                ec->ops->irqpending(ec) ? "" : "not ");
523                 else
524                         printk("irqaddr %p, mask = %02X, status = %02X\n",
525                                ec->irqaddr, ec->irqmask, *ec->irqaddr);
526         }
527 }
528
529 static void ecard_check_lockup(struct irqdesc *desc)
530 {
531         static unsigned long last;
532         static int lockup;
533
534         /*
535          * If the timer interrupt has not run since the last million
536          * unrecognised expansion card interrupts, then there is
537          * something seriously wrong.  Disable the expansion card
538          * interrupts so at least we can continue.
539          *
540          * Maybe we ought to start a timer to re-enable them some time
541          * later?
542          */
543         if (last == jiffies) {
544                 lockup += 1;
545                 if (lockup > 1000000) {
546                         printk(KERN_ERR "\nInterrupt lockup detected - "
547                                "disabling all expansion card interrupts\n");
548
549                         desc->chip->mask(IRQ_EXPANSIONCARD);
550                         ecard_dump_irq_state();
551                 }
552         } else
553                 lockup = 0;
554
555         /*
556          * If we did not recognise the source of this interrupt,
557          * warn the user, but don't flood the user with these messages.
558          */
559         if (!last || time_after(jiffies, last + 5*HZ)) {
560                 last = jiffies;
561                 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
562                 ecard_dump_irq_state();
563         }
564 }
565
566 static void
567 ecard_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
568 {
569         ecard_t *ec;
570         int called = 0;
571
572         desc->chip->mask(irq);
573         for (ec = cards; ec; ec = ec->next) {
574                 int pending;
575
576                 if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
577                         continue;
578
579                 if (ec->ops && ec->ops->irqpending)
580                         pending = ec->ops->irqpending(ec);
581                 else
582                         pending = ecard_default_ops.irqpending(ec);
583
584                 if (pending) {
585                         struct irqdesc *d = irq_desc + ec->irq;
586                         d->handle(ec->irq, d, regs);
587                         called ++;
588                 }
589         }
590         desc->chip->unmask(irq);
591
592         if (called == 0)
593                 ecard_check_lockup(desc);
594 }
595
596 #ifdef HAS_EXPMASK
597 static unsigned char priority_masks[] =
598 {
599         0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
600 };
601
602 static unsigned char first_set[] =
603 {
604         0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
605         0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
606 };
607
608 static void
609 ecard_irqexp_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
610 {
611         const unsigned int statusmask = 15;
612         unsigned int status;
613
614         status = __raw_readb(EXPMASK_STATUS) & statusmask;
615         if (status) {
616                 unsigned int slot = first_set[status];
617                 ecard_t *ec = slot_to_ecard(slot);
618
619                 if (ec->claimed) {
620                         struct irqdesc *d = irqdesc + ec->irq;
621                         /*
622                          * this ugly code is so that we can operate a
623                          * prioritorising system:
624                          *
625                          * Card 0       highest priority
626                          * Card 1
627                          * Card 2
628                          * Card 3       lowest priority
629                          *
630                          * Serial cards should go in 0/1, ethernet/scsi in 2/3
631                          * otherwise you will lose serial data at high speeds!
632                          */
633                         d->handle(ec->irq, d, regs);
634                 } else {
635                         printk(KERN_WARNING "card%d: interrupt from unclaimed "
636                                "card???\n", slot);
637                         have_expmask &= ~(1 << slot);
638                         __raw_writeb(have_expmask, EXPMASK_ENABLE);
639                 }
640         } else
641                 printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
642 }
643
644 static int __init ecard_probeirqhw(void)
645 {
646         ecard_t *ec;
647         int found;
648
649         __raw_writeb(0x00, EXPMASK_ENABLE);
650         __raw_writeb(0xff, EXPMASK_STATUS);
651         found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
652         __raw_writeb(0xff, EXPMASK_ENABLE);
653
654         if (found) {
655                 printk(KERN_DEBUG "Expansion card interrupt "
656                        "management hardware found\n");
657
658                 /* for each card present, set a bit to '1' */
659                 have_expmask = 0x80000000;
660
661                 for (ec = cards; ec; ec = ec->next)
662                         have_expmask |= 1 << ec->slot_no;
663
664                 __raw_writeb(have_expmask, EXPMASK_ENABLE);
665         }
666
667         return found;
668 }
669 #else
670 #define ecard_irqexp_handler NULL
671 #define ecard_probeirqhw() (0)
672 #endif
673
674 #ifndef IO_EC_MEMC8_BASE
675 #define IO_EC_MEMC8_BASE 0
676 #endif
677
678 unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
679 {
680         unsigned long address = 0;
681         int slot = ec->slot_no;
682
683         if (ec->slot_no == 8)
684                 return IO_EC_MEMC8_BASE;
685
686         ectcr &= ~(1 << slot);
687
688         switch (type) {
689         case ECARD_MEMC:
690                 if (slot < 4)
691                         address = IO_EC_MEMC_BASE + (slot << 12);
692                 break;
693
694         case ECARD_IOC:
695                 if (slot < 4)
696                         address = IO_EC_IOC_BASE + (slot << 12);
697 #ifdef IO_EC_IOC4_BASE
698                 else
699                         address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
700 #endif
701                 if (address)
702                         address +=  speed << 17;
703                 break;
704
705 #ifdef IO_EC_EASI_BASE
706         case ECARD_EASI:
707                 address = IO_EC_EASI_BASE + (slot << 22);
708                 if (speed == ECARD_FAST)
709                         ectcr |= 1 << slot;
710                 break;
711 #endif
712         default:
713                 break;
714         }
715
716 #ifdef IOMD_ECTCR
717         iomd_writeb(ectcr, IOMD_ECTCR);
718 #endif
719         return address;
720 }
721
722 static int ecard_prints(char *buffer, ecard_t *ec)
723 {
724         char *start = buffer;
725
726         buffer += sprintf(buffer, "  %d: %s ", ec->slot_no,
727                           ec->type == ECARD_EASI ? "EASI" : "    ");
728
729         if (ec->cid.id == 0) {
730                 struct in_chunk_dir incd;
731
732                 buffer += sprintf(buffer, "[%04X:%04X] ",
733                         ec->cid.manufacturer, ec->cid.product);
734
735                 if (!ec->card_desc && ec->cid.cd &&
736                     ecard_readchunk(&incd, ec, 0xf5, 0)) {
737                         ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
738
739                         if (ec->card_desc)
740                                 strcpy((char *)ec->card_desc, incd.d.string);
741                 }
742
743                 buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
744         } else
745                 buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
746
747         return buffer - start;
748 }
749
750 static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
751 {
752         ecard_t *ec = cards;
753         off_t at = 0;
754         int len, cnt;
755
756         cnt = 0;
757         while (ec && count > cnt) {
758                 len = ecard_prints(buf, ec);
759                 at += len;
760                 if (at >= pos) {
761                         if (!*start) {
762                                 *start = buf + (pos - (at - len));
763                                 cnt = at - pos;
764                         } else
765                                 cnt += len;
766                         buf += len;
767                 }
768                 ec = ec->next;
769         }
770         return (count > cnt) ? cnt : count;
771 }
772
773 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
774
775 static void ecard_proc_init(void)
776 {
777         proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
778         create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
779                 get_ecard_dev_info);
780 }
781
782 #define ec_set_resource(ec,nr,st,sz,flg)                        \
783         do {                                                    \
784                 (ec)->resource[nr].name = ec->dev.bus_id;       \
785                 (ec)->resource[nr].start = st;                  \
786                 (ec)->resource[nr].end = (st) + (sz) - 1;       \
787                 (ec)->resource[nr].flags = flg;                 \
788         } while (0)
789
790 static void __init ecard_init_resources(struct expansion_card *ec)
791 {
792         unsigned long base = PODSLOT_IOC4_BASE;
793         unsigned int slot = ec->slot_no;
794         int i;
795
796         if (slot < 4) {
797                 ec_set_resource(ec, ECARD_RES_MEMC,
798                                 PODSLOT_MEMC_BASE + (slot << 14),
799                                 PODSLOT_MEMC_SIZE, IORESOURCE_MEM);
800                 base = PODSLOT_IOC0_BASE;
801         }
802
803 #ifdef CONFIG_ARCH_RPC
804         if (slot < 8) {
805                 ec_set_resource(ec, ECARD_RES_EASI,
806                                 PODSLOT_EASI_BASE + (slot << 24),
807                                 PODSLOT_EASI_SIZE, IORESOURCE_MEM);
808         }
809
810         if (slot == 8) {
811                 ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE,
812                                 NETSLOT_SIZE, IORESOURCE_MEM);
813         } else
814 #endif
815
816         for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++) {
817                 ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
818                                 base + (slot << 14) + (i << 19),
819                                 PODSLOT_IOC_SIZE, IORESOURCE_MEM);
820         }
821
822         for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
823                 if (ec->resource[i].start &&
824                     request_resource(&iomem_resource, &ec->resource[i])) {
825                         printk(KERN_ERR "%s: resource(s) not available\n",
826                                 ec->dev.bus_id);
827                         ec->resource[i].end -= ec->resource[i].start;
828                         ec->resource[i].start = 0;
829                 }
830         }
831 }
832
833 static ssize_t ecard_show_irq(struct device *dev, char *buf)
834 {
835         struct expansion_card *ec = ECARD_DEV(dev);
836         return sprintf(buf, "%u\n", ec->irq);
837 }
838
839 static DEVICE_ATTR(irq, S_IRUGO, ecard_show_irq, NULL);
840
841 static ssize_t ecard_show_dma(struct device *dev, char *buf)
842 {
843         struct expansion_card *ec = ECARD_DEV(dev);
844         return sprintf(buf, "%u\n", ec->dma);
845 }
846
847 static DEVICE_ATTR(dma, S_IRUGO, ecard_show_dma, NULL);
848
849 static ssize_t ecard_show_resources(struct device *dev, char *buf)
850 {
851         struct expansion_card *ec = ECARD_DEV(dev);
852         char *str = buf;
853         int i;
854
855         for (i = 0; i < ECARD_NUM_RESOURCES; i++)
856                 str += sprintf(str, "%08lx %08lx %08lx\n",
857                                 ec->resource[i].start,
858                                 ec->resource[i].end,
859                                 ec->resource[i].flags);
860
861         return str - buf;
862 }
863
864 static DEVICE_ATTR(resource, S_IRUGO, ecard_show_resources, NULL);
865
866 static ssize_t ecard_show_vendor(struct device *dev, char *buf)
867 {
868         struct expansion_card *ec = ECARD_DEV(dev);
869         return sprintf(buf, "%u\n", ec->cid.manufacturer);
870 }
871
872 static DEVICE_ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL);
873
874 static ssize_t ecard_show_device(struct device *dev, char *buf)
875 {
876         struct expansion_card *ec = ECARD_DEV(dev);
877         return sprintf(buf, "%u\n", ec->cid.product);
878 }
879
880 static DEVICE_ATTR(device, S_IRUGO, ecard_show_device, NULL);
881
882
883 int ecard_request_resources(struct expansion_card *ec)
884 {
885         int i, err = 0;
886
887         for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
888                 if (ecard_resource_end(ec, i) &&
889                     !request_mem_region(ecard_resource_start(ec, i),
890                                         ecard_resource_len(ec, i),
891                                         ec->dev.driver->name)) {
892                         err = -EBUSY;
893                         break;
894                 }
895         }
896
897         if (err) {
898                 while (i--)
899                         if (ecard_resource_end(ec, i))
900                                 release_mem_region(ecard_resource_start(ec, i),
901                                                    ecard_resource_len(ec, i));
902         }
903         return err;
904 }
905 EXPORT_SYMBOL(ecard_request_resources);
906
907 void ecard_release_resources(struct expansion_card *ec)
908 {
909         int i;
910
911         for (i = 0; i < ECARD_NUM_RESOURCES; i++)
912                 if (ecard_resource_end(ec, i))
913                         release_mem_region(ecard_resource_start(ec, i),
914                                            ecard_resource_len(ec, i));
915 }
916 EXPORT_SYMBOL(ecard_release_resources);
917
918 /*
919  * Probe for an expansion card.
920  *
921  * If bit 1 of the first byte of the card is set, then the
922  * card does not exist.
923  */
924 static int __init
925 ecard_probe(int slot, card_type_t type)
926 {
927         ecard_t **ecp;
928         ecard_t *ec;
929         struct ex_ecid cid;
930         int i, rc = -ENOMEM;
931
932         ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
933         if (!ec)
934                 goto nomem;
935
936         memset(ec, 0, sizeof(ecard_t));
937
938         ec->slot_no     = slot;
939         ec->type        = type;
940         ec->irq         = NO_IRQ;
941         ec->fiq         = NO_IRQ;
942         ec->dma         = NO_DMA;
943         ec->card_desc   = NULL;
944         ec->ops         = &ecard_default_ops;
945
946         rc = -ENODEV;
947         if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
948                 goto nodev;
949
950         cid.r_zero = 1;
951         ecard_readbytes(&cid, ec, 0, 16, 0);
952         if (cid.r_zero)
953                 goto nodev;
954
955         ec->cid.id      = cid.r_id;
956         ec->cid.cd      = cid.r_cd;
957         ec->cid.is      = cid.r_is;
958         ec->cid.w       = cid.r_w;
959         ec->cid.manufacturer = ecard_getu16(cid.r_manu);
960         ec->cid.product = ecard_getu16(cid.r_prod);
961         ec->cid.country = cid.r_country;
962         ec->cid.irqmask = cid.r_irqmask;
963         ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
964         ec->cid.fiqmask = cid.r_fiqmask;
965         ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
966         ec->fiqaddr     =
967         ec->irqaddr     = (unsigned char *)ioaddr(ec->podaddr);
968
969         if (ec->cid.is) {
970                 ec->irqmask = ec->cid.irqmask;
971                 ec->irqaddr += ec->cid.irqoff;
972                 ec->fiqmask = ec->cid.fiqmask;
973                 ec->fiqaddr += ec->cid.fiqoff;
974         } else {
975                 ec->irqmask = 1;
976                 ec->fiqmask = 4;
977         }
978
979         for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
980                 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
981                     blacklist[i].product == ec->cid.product) {
982                         ec->card_desc = blacklist[i].type;
983                         break;
984                 }
985
986         snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
987         ec->dev.parent = NULL;
988         ec->dev.bus    = &ecard_bus_type;
989         ec->dev.dma_mask = &ec->dma_mask;
990         ec->dma_mask = (u64)0xffffffff;
991
992         ecard_init_resources(ec);
993
994         /*
995          * hook the interrupt handlers
996          */
997         if (slot < 8) {
998                 ec->irq = 32 + slot;
999                 set_irq_chip(ec->irq, &ecard_chip);
1000                 set_irq_handler(ec->irq, do_level_IRQ);
1001                 set_irq_flags(ec->irq, IRQF_VALID);
1002         }
1003
1004 #ifdef IO_EC_MEMC8_BASE
1005         if (slot == 8)
1006                 ec->irq = 11;
1007 #endif
1008 #ifdef CONFIG_ARCH_RPC
1009         /* On RiscPC, only first two slots have DMA capability */
1010         if (slot < 2)
1011                 ec->dma = 2 + slot;
1012 #endif
1013
1014         for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
1015
1016         *ecp = ec;
1017         slot_to_expcard[slot] = ec;
1018
1019         device_register(&ec->dev);
1020         device_create_file(&ec->dev, &dev_attr_dma);
1021         device_create_file(&ec->dev, &dev_attr_irq);
1022         device_create_file(&ec->dev, &dev_attr_resource);
1023         device_create_file(&ec->dev, &dev_attr_vendor);
1024         device_create_file(&ec->dev, &dev_attr_device);
1025
1026         return 0;
1027
1028 nodev:
1029         kfree(ec);
1030 nomem:
1031         return rc;
1032 }
1033
1034 /*
1035  * Initialise the expansion card system.
1036  * Locate all hardware - interrupt management and
1037  * actual cards.
1038  */
1039 static int __init ecard_init(void)
1040 {
1041         int slot, irqhw, ret;
1042
1043         ret = kernel_thread(ecard_task, NULL, CLONE_KERNEL);
1044         if (ret < 0) {
1045                 printk(KERN_ERR "Ecard: unable to create kernel thread: %d\n",
1046                        ret);
1047                 return ret;
1048         }
1049
1050         printk("Probing expansion cards\n");
1051
1052         for (slot = 0; slot < 8; slot ++) {
1053                 if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1054                         ecard_probe(slot, ECARD_IOC);
1055         }
1056
1057 #ifdef IO_EC_MEMC8_BASE
1058         ecard_probe(8, ECARD_IOC);
1059 #endif
1060
1061         irqhw = ecard_probeirqhw();
1062
1063         set_irq_chained_handler(IRQ_EXPANSIONCARD,
1064                                 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
1065
1066         ecard_proc_init();
1067
1068         return 0;
1069 }
1070
1071 subsys_initcall(ecard_init);
1072
1073 /*
1074  *      ECARD "bus"
1075  */
1076 static const struct ecard_id *
1077 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1078 {
1079         int i;
1080
1081         for (i = 0; ids[i].manufacturer != 65535; i++)
1082                 if (ec->cid.manufacturer == ids[i].manufacturer &&
1083                     ec->cid.product == ids[i].product)
1084                         return ids + i;
1085
1086         return NULL;
1087 }
1088
1089 static int ecard_drv_probe(struct device *dev)
1090 {
1091         struct expansion_card *ec = ECARD_DEV(dev);
1092         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1093         const struct ecard_id *id;
1094         int ret;
1095
1096         id = ecard_match_device(drv->id_table, ec);
1097
1098         ecard_claim(ec);
1099         ret = drv->probe(ec, id);
1100         if (ret)
1101                 ecard_release(ec);
1102         return ret;
1103 }
1104
1105 static int ecard_drv_remove(struct device *dev)
1106 {
1107         struct expansion_card *ec = ECARD_DEV(dev);
1108         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1109
1110         drv->remove(ec);
1111         ecard_release(ec);
1112
1113         return 0;
1114 }
1115
1116 /*
1117  * Before rebooting, we must make sure that the expansion card is in a
1118  * sensible state, so it can be re-detected.  This means that the first
1119  * page of the ROM must be visible.  We call the expansion cards reset
1120  * handler, if any.
1121  */
1122 static void ecard_drv_shutdown(struct device *dev)
1123 {
1124         struct expansion_card *ec = ECARD_DEV(dev);
1125         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1126         struct ecard_request req;
1127
1128         if (drv->shutdown)
1129                 drv->shutdown(ec);
1130         ecard_release(ec);
1131         req.fn = ecard_task_reset;
1132         req.ec = ec;
1133         ecard_call(&req);
1134 }
1135
1136 int ecard_register_driver(struct ecard_driver *drv)
1137 {
1138         drv->drv.bus = &ecard_bus_type;
1139         drv->drv.probe = ecard_drv_probe;
1140         drv->drv.remove = ecard_drv_remove;
1141         drv->drv.shutdown = ecard_drv_shutdown;
1142
1143         return driver_register(&drv->drv);
1144 }
1145
1146 void ecard_remove_driver(struct ecard_driver *drv)
1147 {
1148         driver_unregister(&drv->drv);
1149 }
1150
1151 static int ecard_match(struct device *_dev, struct device_driver *_drv)
1152 {
1153         struct expansion_card *ec = ECARD_DEV(_dev);
1154         struct ecard_driver *drv = ECARD_DRV(_drv);
1155         int ret;
1156
1157         if (drv->id_table) {
1158                 ret = ecard_match_device(drv->id_table, ec) != NULL;
1159         } else {
1160                 ret = ec->cid.id == drv->id;
1161         }
1162
1163         return ret;
1164 }
1165
1166 struct bus_type ecard_bus_type = {
1167         .name   = "ecard",
1168         .match  = ecard_match,
1169 };
1170
1171 static int ecard_bus_init(void)
1172 {
1173         return bus_register(&ecard_bus_type);
1174 }
1175
1176 postcore_initcall(ecard_bus_init);
1177
1178 EXPORT_SYMBOL(ecard_readchunk);
1179 EXPORT_SYMBOL(ecard_address);
1180 EXPORT_SYMBOL(ecard_register_driver);
1181 EXPORT_SYMBOL(ecard_remove_driver);
1182 EXPORT_SYMBOL(ecard_bus_type);