vserver 1.9.3
[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
507 ecard_dump_irq_state(ecard_t *ec)
508 {
509         printk("  %d: %sclaimed, ",
510                ec->slot_no,
511                ec->claimed ? "" : "not ");
512
513         if (ec->ops && ec->ops->irqpending &&
514             ec->ops != &ecard_default_ops)
515                 printk("irq %spending\n",
516                        ec->ops->irqpending(ec) ? "" : "not ");
517         else
518                 printk("irqaddr %p, mask = %02X, status = %02X\n",
519                        ec->irqaddr, ec->irqmask, *ec->irqaddr);
520 }
521
522 static void ecard_check_lockup(struct irqdesc *desc)
523 {
524         static unsigned long last;
525         static int lockup;
526         ecard_t *ec;
527
528         /*
529          * If the timer interrupt has not run since the last million
530          * unrecognised expansion card interrupts, then there is
531          * something seriously wrong.  Disable the expansion card
532          * interrupts so at least we can continue.
533          *
534          * Maybe we ought to start a timer to re-enable them some time
535          * later?
536          */
537         if (last == jiffies) {
538                 lockup += 1;
539                 if (lockup > 1000000) {
540                         printk(KERN_ERR "\nInterrupt lockup detected - "
541                                "disabling all expansion card interrupts\n");
542
543                         desc->chip->mask(IRQ_EXPANSIONCARD);
544
545                         printk("Expansion card IRQ state:\n");
546
547                         for (ec = cards; ec; ec = ec->next)
548                                 ecard_dump_irq_state(ec);
549                 }
550         } else
551                 lockup = 0;
552
553         /*
554          * If we did not recognise the source of this interrupt,
555          * warn the user, but don't flood the user with these messages.
556          */
557         if (!last || time_after(jiffies, last + 5*HZ)) {
558                 last = jiffies;
559                 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
560         }
561 }
562
563 static void
564 ecard_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
565 {
566         ecard_t *ec;
567         int called = 0;
568
569         desc->chip->mask(irq);
570         for (ec = cards; ec; ec = ec->next) {
571                 int pending;
572
573                 if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
574                         continue;
575
576                 if (ec->ops && ec->ops->irqpending)
577                         pending = ec->ops->irqpending(ec);
578                 else
579                         pending = ecard_default_ops.irqpending(ec);
580
581                 if (pending) {
582                         struct irqdesc *d = irq_desc + ec->irq;
583                         d->handle(ec->irq, d, regs);
584                         called ++;
585                 }
586         }
587         desc->chip->unmask(irq);
588
589         if (called == 0)
590                 ecard_check_lockup(desc);
591 }
592
593 #ifdef HAS_EXPMASK
594 static unsigned char priority_masks[] =
595 {
596         0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
597 };
598
599 static unsigned char first_set[] =
600 {
601         0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
602         0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
603 };
604
605 static void
606 ecard_irqexp_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
607 {
608         const unsigned int statusmask = 15;
609         unsigned int status;
610
611         status = __raw_readb(EXPMASK_STATUS) & statusmask;
612         if (status) {
613                 unsigned int slot = first_set[status];
614                 ecard_t *ec = slot_to_ecard(slot);
615
616                 if (ec->claimed) {
617                         struct irqdesc *d = irqdesc + ec->irq;
618                         /*
619                          * this ugly code is so that we can operate a
620                          * prioritorising system:
621                          *
622                          * Card 0       highest priority
623                          * Card 1
624                          * Card 2
625                          * Card 3       lowest priority
626                          *
627                          * Serial cards should go in 0/1, ethernet/scsi in 2/3
628                          * otherwise you will lose serial data at high speeds!
629                          */
630                         d->handle(ec->irq, d, regs);
631                 } else {
632                         printk(KERN_WARNING "card%d: interrupt from unclaimed "
633                                "card???\n", slot);
634                         have_expmask &= ~(1 << slot);
635                         __raw_writeb(have_expmask, EXPMASK_ENABLE);
636                 }
637         } else
638                 printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
639 }
640
641 static int __init ecard_probeirqhw(void)
642 {
643         ecard_t *ec;
644         int found;
645
646         __raw_writeb(0x00, EXPMASK_ENABLE);
647         __raw_writeb(0xff, EXPMASK_STATUS);
648         found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
649         __raw_writeb(0xff, EXPMASK_ENABLE);
650
651         if (found) {
652                 printk(KERN_DEBUG "Expansion card interrupt "
653                        "management hardware found\n");
654
655                 /* for each card present, set a bit to '1' */
656                 have_expmask = 0x80000000;
657
658                 for (ec = cards; ec; ec = ec->next)
659                         have_expmask |= 1 << ec->slot_no;
660
661                 __raw_writeb(have_expmask, EXPMASK_ENABLE);
662         }
663
664         return found;
665 }
666 #else
667 #define ecard_irqexp_handler NULL
668 #define ecard_probeirqhw() (0)
669 #endif
670
671 #ifndef IO_EC_MEMC8_BASE
672 #define IO_EC_MEMC8_BASE 0
673 #endif
674
675 unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
676 {
677         unsigned long address = 0;
678         int slot = ec->slot_no;
679
680         if (ec->slot_no == 8)
681                 return IO_EC_MEMC8_BASE;
682
683         ectcr &= ~(1 << slot);
684
685         switch (type) {
686         case ECARD_MEMC:
687                 if (slot < 4)
688                         address = IO_EC_MEMC_BASE + (slot << 12);
689                 break;
690
691         case ECARD_IOC:
692                 if (slot < 4)
693                         address = IO_EC_IOC_BASE + (slot << 12);
694 #ifdef IO_EC_IOC4_BASE
695                 else
696                         address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
697 #endif
698                 if (address)
699                         address +=  speed << 17;
700                 break;
701
702 #ifdef IO_EC_EASI_BASE
703         case ECARD_EASI:
704                 address = IO_EC_EASI_BASE + (slot << 22);
705                 if (speed == ECARD_FAST)
706                         ectcr |= 1 << slot;
707                 break;
708 #endif
709         default:
710                 break;
711         }
712
713 #ifdef IOMD_ECTCR
714         iomd_writeb(ectcr, IOMD_ECTCR);
715 #endif
716         return address;
717 }
718
719 static int ecard_prints(char *buffer, ecard_t *ec)
720 {
721         char *start = buffer;
722
723         buffer += sprintf(buffer, "  %d: %s ", ec->slot_no,
724                           ec->type == ECARD_EASI ? "EASI" : "    ");
725
726         if (ec->cid.id == 0) {
727                 struct in_chunk_dir incd;
728
729                 buffer += sprintf(buffer, "[%04X:%04X] ",
730                         ec->cid.manufacturer, ec->cid.product);
731
732                 if (!ec->card_desc && ec->cid.cd &&
733                     ecard_readchunk(&incd, ec, 0xf5, 0)) {
734                         ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
735
736                         if (ec->card_desc)
737                                 strcpy((char *)ec->card_desc, incd.d.string);
738                 }
739
740                 buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
741         } else
742                 buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
743
744         return buffer - start;
745 }
746
747 static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
748 {
749         ecard_t *ec = cards;
750         off_t at = 0;
751         int len, cnt;
752
753         cnt = 0;
754         while (ec && count > cnt) {
755                 len = ecard_prints(buf, ec);
756                 at += len;
757                 if (at >= pos) {
758                         if (!*start) {
759                                 *start = buf + (pos - (at - len));
760                                 cnt = at - pos;
761                         } else
762                                 cnt += len;
763                         buf += len;
764                 }
765                 ec = ec->next;
766         }
767         return (count > cnt) ? cnt : count;
768 }
769
770 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
771
772 static void ecard_proc_init(void)
773 {
774         proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
775         create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
776                 get_ecard_dev_info);
777 }
778
779 #define ec_set_resource(ec,nr,st,sz,flg)                        \
780         do {                                                    \
781                 (ec)->resource[nr].name = ec->dev.bus_id;       \
782                 (ec)->resource[nr].start = st;                  \
783                 (ec)->resource[nr].end = (st) + (sz) - 1;       \
784                 (ec)->resource[nr].flags = flg;                 \
785         } while (0)
786
787 static void __init ecard_init_resources(struct expansion_card *ec)
788 {
789         unsigned long base = PODSLOT_IOC4_BASE;
790         unsigned int slot = ec->slot_no;
791         int i;
792
793         if (slot < 4) {
794                 ec_set_resource(ec, ECARD_RES_MEMC,
795                                 PODSLOT_MEMC_BASE + (slot << 14),
796                                 PODSLOT_MEMC_SIZE, IORESOURCE_MEM);
797                 base = PODSLOT_IOC0_BASE;
798         }
799
800 #ifdef CONFIG_ARCH_RPC
801         if (slot < 8) {
802                 ec_set_resource(ec, ECARD_RES_EASI,
803                                 PODSLOT_EASI_BASE + (slot << 24),
804                                 PODSLOT_EASI_SIZE, IORESOURCE_MEM);
805         }
806
807         if (slot == 8) {
808                 ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE,
809                                 NETSLOT_SIZE, IORESOURCE_MEM);
810         } else
811 #endif
812
813         for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++) {
814                 ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
815                                 base + (slot << 14) + (i << 19),
816                                 PODSLOT_IOC_SIZE, IORESOURCE_MEM);
817         }
818
819         for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
820                 if (ec->resource[i].start &&
821                     request_resource(&iomem_resource, &ec->resource[i])) {
822                         printk(KERN_ERR "%s: resource(s) not available\n",
823                                 ec->dev.bus_id);
824                         ec->resource[i].end -= ec->resource[i].start;
825                         ec->resource[i].start = 0;
826                 }
827         }
828 }
829
830 static ssize_t ecard_show_irq(struct device *dev, char *buf)
831 {
832         struct expansion_card *ec = ECARD_DEV(dev);
833         return sprintf(buf, "%u\n", ec->irq);
834 }
835
836 static DEVICE_ATTR(irq, S_IRUGO, ecard_show_irq, NULL);
837
838 static ssize_t ecard_show_dma(struct device *dev, char *buf)
839 {
840         struct expansion_card *ec = ECARD_DEV(dev);
841         return sprintf(buf, "%u\n", ec->dma);
842 }
843
844 static DEVICE_ATTR(dma, S_IRUGO, ecard_show_dma, NULL);
845
846 static ssize_t ecard_show_resources(struct device *dev, char *buf)
847 {
848         struct expansion_card *ec = ECARD_DEV(dev);
849         char *str = buf;
850         int i;
851
852         for (i = 0; i < ECARD_NUM_RESOURCES; i++)
853                 str += sprintf(str, "%08lx %08lx %08lx\n",
854                                 ec->resource[i].start,
855                                 ec->resource[i].end,
856                                 ec->resource[i].flags);
857
858         return str - buf;
859 }
860
861 static DEVICE_ATTR(resource, S_IRUGO, ecard_show_resources, NULL);
862
863 static ssize_t ecard_show_vendor(struct device *dev, char *buf)
864 {
865         struct expansion_card *ec = ECARD_DEV(dev);
866         return sprintf(buf, "%u\n", ec->cid.manufacturer);
867 }
868
869 static DEVICE_ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL);
870
871 static ssize_t ecard_show_device(struct device *dev, char *buf)
872 {
873         struct expansion_card *ec = ECARD_DEV(dev);
874         return sprintf(buf, "%u\n", ec->cid.product);
875 }
876
877 static DEVICE_ATTR(device, S_IRUGO, ecard_show_device, NULL);
878
879
880 int ecard_request_resources(struct expansion_card *ec)
881 {
882         int i, err = 0;
883
884         for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
885                 if (ecard_resource_end(ec, i) &&
886                     !request_mem_region(ecard_resource_start(ec, i),
887                                         ecard_resource_len(ec, i),
888                                         ec->dev.driver->name)) {
889                         err = -EBUSY;
890                         break;
891                 }
892         }
893
894         if (err) {
895                 while (i--)
896                         if (ecard_resource_end(ec, i))
897                                 release_mem_region(ecard_resource_start(ec, i),
898                                                    ecard_resource_len(ec, i));
899         }
900         return err;
901 }
902 EXPORT_SYMBOL(ecard_request_resources);
903
904 void ecard_release_resources(struct expansion_card *ec)
905 {
906         int i;
907
908         for (i = 0; i < ECARD_NUM_RESOURCES; i++)
909                 if (ecard_resource_end(ec, i))
910                         release_mem_region(ecard_resource_start(ec, i),
911                                            ecard_resource_len(ec, i));
912 }
913 EXPORT_SYMBOL(ecard_release_resources);
914
915 /*
916  * Probe for an expansion card.
917  *
918  * If bit 1 of the first byte of the card is set, then the
919  * card does not exist.
920  */
921 static int __init
922 ecard_probe(int slot, card_type_t type)
923 {
924         ecard_t **ecp;
925         ecard_t *ec;
926         struct ex_ecid cid;
927         int i, rc = -ENOMEM;
928
929         ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
930         if (!ec)
931                 goto nomem;
932
933         memset(ec, 0, sizeof(ecard_t));
934
935         ec->slot_no     = slot;
936         ec->type        = type;
937         ec->irq         = NO_IRQ;
938         ec->fiq         = NO_IRQ;
939         ec->dma         = NO_DMA;
940         ec->card_desc   = NULL;
941         ec->ops         = &ecard_default_ops;
942
943         rc = -ENODEV;
944         if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
945                 goto nodev;
946
947         cid.r_zero = 1;
948         ecard_readbytes(&cid, ec, 0, 16, 0);
949         if (cid.r_zero)
950                 goto nodev;
951
952         ec->cid.id      = cid.r_id;
953         ec->cid.cd      = cid.r_cd;
954         ec->cid.is      = cid.r_is;
955         ec->cid.w       = cid.r_w;
956         ec->cid.manufacturer = ecard_getu16(cid.r_manu);
957         ec->cid.product = ecard_getu16(cid.r_prod);
958         ec->cid.country = cid.r_country;
959         ec->cid.irqmask = cid.r_irqmask;
960         ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
961         ec->cid.fiqmask = cid.r_fiqmask;
962         ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
963         ec->fiqaddr     =
964         ec->irqaddr     = (unsigned char *)ioaddr(ec->podaddr);
965
966         if (ec->cid.is) {
967                 ec->irqmask = ec->cid.irqmask;
968                 ec->irqaddr += ec->cid.irqoff;
969                 ec->fiqmask = ec->cid.fiqmask;
970                 ec->fiqaddr += ec->cid.fiqoff;
971         } else {
972                 ec->irqmask = 1;
973                 ec->fiqmask = 4;
974         }
975
976         for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
977                 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
978                     blacklist[i].product == ec->cid.product) {
979                         ec->card_desc = blacklist[i].type;
980                         break;
981                 }
982
983         snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
984         ec->dev.parent = NULL;
985         ec->dev.bus    = &ecard_bus_type;
986         ec->dev.dma_mask = &ec->dma_mask;
987         ec->dma_mask = (u64)0xffffffff;
988
989         ecard_init_resources(ec);
990
991         /*
992          * hook the interrupt handlers
993          */
994         if (slot < 8) {
995                 ec->irq = 32 + slot;
996                 set_irq_chip(ec->irq, &ecard_chip);
997                 set_irq_handler(ec->irq, do_level_IRQ);
998                 set_irq_flags(ec->irq, IRQF_VALID);
999         }
1000
1001 #ifdef IO_EC_MEMC8_BASE
1002         if (slot == 8)
1003                 ec->irq = 11;
1004 #endif
1005 #ifdef CONFIG_ARCH_RPC
1006         /* On RiscPC, only first two slots have DMA capability */
1007         if (slot < 2)
1008                 ec->dma = 2 + slot;
1009 #endif
1010
1011         for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
1012
1013         *ecp = ec;
1014         slot_to_expcard[slot] = ec;
1015
1016         device_register(&ec->dev);
1017         device_create_file(&ec->dev, &dev_attr_dma);
1018         device_create_file(&ec->dev, &dev_attr_irq);
1019         device_create_file(&ec->dev, &dev_attr_resource);
1020         device_create_file(&ec->dev, &dev_attr_vendor);
1021         device_create_file(&ec->dev, &dev_attr_device);
1022
1023         return 0;
1024
1025 nodev:
1026         kfree(ec);
1027 nomem:
1028         return rc;
1029 }
1030
1031 /*
1032  * Initialise the expansion card system.
1033  * Locate all hardware - interrupt management and
1034  * actual cards.
1035  */
1036 static int __init ecard_init(void)
1037 {
1038         int slot, irqhw, ret;
1039
1040         ret = kernel_thread(ecard_task, NULL, CLONE_KERNEL);
1041         if (ret < 0) {
1042                 printk(KERN_ERR "Ecard: unable to create kernel thread: %d\n",
1043                        ret);
1044                 return ret;
1045         }
1046
1047         printk("Probing expansion cards\n");
1048
1049         for (slot = 0; slot < 8; slot ++) {
1050                 if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1051                         ecard_probe(slot, ECARD_IOC);
1052         }
1053
1054 #ifdef IO_EC_MEMC8_BASE
1055         ecard_probe(8, ECARD_IOC);
1056 #endif
1057
1058         irqhw = ecard_probeirqhw();
1059
1060         set_irq_chained_handler(IRQ_EXPANSIONCARD,
1061                                 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
1062
1063         ecard_proc_init();
1064
1065         return 0;
1066 }
1067
1068 subsys_initcall(ecard_init);
1069
1070 /*
1071  *      ECARD "bus"
1072  */
1073 static const struct ecard_id *
1074 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1075 {
1076         int i;
1077
1078         for (i = 0; ids[i].manufacturer != 65535; i++)
1079                 if (ec->cid.manufacturer == ids[i].manufacturer &&
1080                     ec->cid.product == ids[i].product)
1081                         return ids + i;
1082
1083         return NULL;
1084 }
1085
1086 static int ecard_drv_probe(struct device *dev)
1087 {
1088         struct expansion_card *ec = ECARD_DEV(dev);
1089         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1090         const struct ecard_id *id;
1091         int ret;
1092
1093         id = ecard_match_device(drv->id_table, ec);
1094
1095         ecard_claim(ec);
1096         ret = drv->probe(ec, id);
1097         if (ret)
1098                 ecard_release(ec);
1099         return ret;
1100 }
1101
1102 static int ecard_drv_remove(struct device *dev)
1103 {
1104         struct expansion_card *ec = ECARD_DEV(dev);
1105         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1106
1107         drv->remove(ec);
1108         ecard_release(ec);
1109
1110         return 0;
1111 }
1112
1113 /*
1114  * Before rebooting, we must make sure that the expansion card is in a
1115  * sensible state, so it can be re-detected.  This means that the first
1116  * page of the ROM must be visible.  We call the expansion cards reset
1117  * handler, if any.
1118  */
1119 static void ecard_drv_shutdown(struct device *dev)
1120 {
1121         struct expansion_card *ec = ECARD_DEV(dev);
1122         struct ecard_driver *drv = ECARD_DRV(dev->driver);
1123         struct ecard_request req;
1124
1125         if (drv->shutdown)
1126                 drv->shutdown(ec);
1127         ecard_release(ec);
1128         req.fn = ecard_task_reset;
1129         req.ec = ec;
1130         ecard_call(&req);
1131 }
1132
1133 int ecard_register_driver(struct ecard_driver *drv)
1134 {
1135         drv->drv.bus = &ecard_bus_type;
1136         drv->drv.probe = ecard_drv_probe;
1137         drv->drv.remove = ecard_drv_remove;
1138         drv->drv.shutdown = ecard_drv_shutdown;
1139
1140         return driver_register(&drv->drv);
1141 }
1142
1143 void ecard_remove_driver(struct ecard_driver *drv)
1144 {
1145         driver_unregister(&drv->drv);
1146 }
1147
1148 static int ecard_match(struct device *_dev, struct device_driver *_drv)
1149 {
1150         struct expansion_card *ec = ECARD_DEV(_dev);
1151         struct ecard_driver *drv = ECARD_DRV(_drv);
1152         int ret;
1153
1154         if (drv->id_table) {
1155                 ret = ecard_match_device(drv->id_table, ec) != NULL;
1156         } else {
1157                 ret = ec->cid.id == drv->id;
1158         }
1159
1160         return ret;
1161 }
1162
1163 struct bus_type ecard_bus_type = {
1164         .name   = "ecard",
1165         .match  = ecard_match,
1166 };
1167
1168 static int ecard_bus_init(void)
1169 {
1170         return bus_register(&ecard_bus_type);
1171 }
1172
1173 postcore_initcall(ecard_bus_init);
1174
1175 EXPORT_SYMBOL(ecard_readchunk);
1176 EXPORT_SYMBOL(ecard_address);
1177 EXPORT_SYMBOL(ecard_register_driver);
1178 EXPORT_SYMBOL(ecard_remove_driver);
1179 EXPORT_SYMBOL(ecard_bus_type);