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