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