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