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