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