Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / arch / arm / kernel / ecard.c
blob1206e7d9fd669e1176ed1102a0c32513ea3b092e
1 /*
2 * linux/arch/arm/kernel/ecard.c
4 * Copyright 1995-2001 Russell King
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.
10 * Find all installed expansion cards, and handle interrupts from them.
12 * Created from information from Acorns RiscOS3 PRMs
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.
28 #define ECARD_C
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/notifier.h>
41 #include <linux/device.h>
42 #include <linux/init.h>
44 #include <asm/dma.h>
45 #include <asm/ecard.h>
46 #include <asm/hardware.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/pgalloc.h>
50 #include <asm/mmu_context.h>
51 #include <asm/mach/irq.h>
52 #include <asm/tlbflush.h>
54 #ifndef CONFIG_ARCH_RPC
55 #define HAVE_EXPMASK
56 #endif
58 enum req {
59 req_readbytes,
60 req_reset_all
63 struct ecard_request {
64 enum req req;
65 ecard_t *ec;
66 unsigned int address;
67 unsigned int length;
68 unsigned int use_loader;
69 void *buffer;
72 struct expcard_blacklist {
73 unsigned short manufacturer;
74 unsigned short product;
75 const char *type;
78 static ecard_t *cards;
79 static ecard_t *slot_to_expcard[MAX_ECARDS];
80 static unsigned int ectcr;
81 #ifdef HAS_EXPMASK
82 static unsigned int have_expmask;
83 #endif
85 /* List of descriptions of cards which don't have an extended
86 * identification, or chunk directories containing a description.
88 static struct expcard_blacklist __initdata blacklist[] = {
89 { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
92 asmlinkage extern int
93 ecard_loader_reset(volatile unsigned char *pa, loader_t loader);
94 asmlinkage extern int
95 ecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);
97 static const struct ecard_id *
98 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec);
100 static inline unsigned short
101 ecard_getu16(unsigned char *v)
103 return v[0] | v[1] << 8;
106 static inline signed long
107 ecard_gets24(unsigned char *v)
109 return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
112 static inline ecard_t *
113 slot_to_ecard(unsigned int slot)
115 return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
118 /* ===================== Expansion card daemon ======================== */
120 * Since the loader programs on the expansion cards need to be run
121 * in a specific environment, create a separate task with this
122 * environment up, and pass requests to this task as and when we
123 * need to.
125 * This should allow 99% of loaders to be called from Linux.
127 * From a security standpoint, we trust the card vendors. This
128 * may be a misplaced trust.
130 #define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)
131 #define POD_INT_ADDR(x) ((volatile unsigned char *)\
132 ((BUS_ADDR((x)) - IO_BASE) + IO_START))
134 static inline void ecard_task_reset(void)
136 ecard_t *ec;
138 for (ec = cards; ec; ec = ec->next)
139 if (ec->loader)
140 ecard_loader_reset(POD_INT_ADDR(ec->podaddr),
141 ec->loader);
144 static void
145 ecard_task_readbytes(struct ecard_request *req)
147 unsigned char *buf = (unsigned char *)req->buffer;
148 volatile unsigned char *base_addr =
149 (volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr);
150 unsigned int len = req->length;
151 unsigned int off = req->address;
153 if (req->ec->slot_no == 8) {
155 * The card maintains an index which increments the address
156 * into a 4096-byte page on each access. We need to keep
157 * track of the counter.
159 static unsigned int index;
160 unsigned int page;
162 page = (off >> 12) * 4;
163 if (page > 256 * 4)
164 return;
166 off &= 4095;
169 * If we are reading offset 0, or our current index is
170 * greater than the offset, reset the hardware index counter.
172 if (off == 0 || index > off) {
173 *base_addr = 0;
174 index = 0;
178 * Increment the hardware index counter until we get to the
179 * required offset. The read bytes are discarded.
181 while (index < off) {
182 unsigned char byte;
183 byte = base_addr[page];
184 index += 1;
187 while (len--) {
188 *buf++ = base_addr[page];
189 index += 1;
191 } else {
193 if (!req->use_loader || !req->ec->loader) {
194 off *= 4;
195 while (len--) {
196 *buf++ = base_addr[off];
197 off += 4;
199 } else {
200 while(len--) {
202 * The following is required by some
203 * expansion card loader programs.
205 *(unsigned long *)0x108 = 0;
206 *buf++ = ecard_loader_read(off++, base_addr,
207 req->ec->loader);
214 static void ecard_do_request(struct ecard_request *req)
216 switch (req->req) {
217 case req_readbytes:
218 ecard_task_readbytes(req);
219 break;
221 case req_reset_all:
222 ecard_task_reset();
223 break;
227 #ifdef CONFIG_CPU_32
228 #include <linux/completion.h>
230 static pid_t ecard_pid;
231 static wait_queue_head_t ecard_wait;
232 static struct ecard_request *ecard_req;
234 static DECLARE_COMPLETION(ecard_completion);
237 * Set up the expansion card daemon's page tables.
239 static void ecard_init_pgtables(struct mm_struct *mm)
241 struct vm_area_struct vma;
243 /* We want to set up the page tables for the following mapping:
244 * Virtual Physical
245 * 0x03000000 0x03000000
246 * 0x03010000 unmapped
247 * 0x03210000 0x03210000
248 * 0x03400000 unmapped
249 * 0x08000000 0x08000000
250 * 0x10000000 unmapped
252 * FIXME: we don't follow this 100% yet.
254 pgd_t *src_pgd, *dst_pgd;
256 src_pgd = pgd_offset(mm, IO_BASE);
257 dst_pgd = pgd_offset(mm, IO_START);
259 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
261 src_pgd = pgd_offset(mm, EASI_BASE);
262 dst_pgd = pgd_offset(mm, EASI_START);
264 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
266 vma.vm_mm = mm;
268 flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
269 flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
272 static int ecard_init_mm(void)
274 struct mm_struct * mm = mm_alloc();
275 struct mm_struct *active_mm = current->active_mm;
277 if (!mm)
278 return -ENOMEM;
280 current->mm = mm;
281 current->active_mm = mm;
282 activate_mm(active_mm, mm);
283 mmdrop(active_mm);
284 ecard_init_pgtables(mm);
285 return 0;
288 static int
289 ecard_task(void * unused)
291 struct task_struct *tsk = current;
294 * We don't want /any/ signals, not even SIGKILL
296 sigfillset(&tsk->blocked);
297 sigemptyset(&tsk->pending.signal);
298 recalc_sigpending();
299 strcpy(tsk->comm, "kecardd");
300 daemonize();
303 * Allocate a mm. We're not a lazy-TLB kernel task since we need
304 * to set page table entries where the user space would be. Note
305 * that this also creates the page tables. Failure is not an
306 * option here.
308 if (ecard_init_mm())
309 panic("kecardd: unable to alloc mm\n");
311 while (1) {
312 struct ecard_request *req;
314 do {
315 req = xchg(&ecard_req, NULL);
317 if (req == NULL) {
318 sigemptyset(&tsk->pending.signal);
319 interruptible_sleep_on(&ecard_wait);
321 } while (req == NULL);
323 ecard_do_request(req);
324 complete(&ecard_completion);
329 * Wake the expansion card daemon to action our request.
331 * FIXME: The test here is not sufficient to detect if the
332 * kcardd is running.
334 static void
335 ecard_call(struct ecard_request *req)
338 * Make sure we have a context that is able to sleep.
340 if (current == &init_task || in_interrupt())
341 BUG();
343 if (ecard_pid <= 0)
344 ecard_pid = kernel_thread(ecard_task, NULL,
345 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
347 ecard_req = req;
348 wake_up(&ecard_wait);
351 * Now wait for kecardd to run.
353 wait_for_completion(&ecard_completion);
355 #else
357 * On 26-bit processors, we don't need the kcardd thread to access the
358 * expansion card loaders. We do it directly.
360 #define ecard_call(req) ecard_do_request(req)
361 #endif
363 /* ======================= Mid-level card control ===================== */
366 * This function is responsible for resetting the expansion cards to a
367 * sensible state immediately prior to rebooting the system. This function
368 * has process state (keventd), so we can sleep.
370 * Possible "val" values here:
371 * SYS_RESTART - restarting system
372 * SYS_HALT - halting system
373 * SYS_POWER_OFF - powering down system
375 * We ignore all calls, unless it is a SYS_RESTART call - power down/halts
376 * will be followed by a SYS_RESTART if ctrl-alt-del is pressed again.
378 static int ecard_reboot(struct notifier_block *me, unsigned long val, void *v)
380 struct ecard_request req;
382 if (val != SYS_RESTART)
383 return 0;
386 * Disable the expansion card interrupt
388 disable_irq(IRQ_EXPANSIONCARD);
391 * If we have any expansion card loader code which will handle
392 * the reset for us, call it now.
394 req.req = req_reset_all;
395 ecard_call(&req);
398 * Disable the expansion card interrupt again, just to be sure.
400 disable_irq(IRQ_EXPANSIONCARD);
403 * Finally, reset the expansion card interrupt mask to
404 * all enable (RISC OS doesn't set this)
406 #ifdef HAS_EXPMASK
407 have_expmask = ~0;
408 __raw_writeb(have_expmask, EXPMASK_ENABLE);
409 #endif
410 return 0;
413 static struct notifier_block ecard_reboot_notifier = {
414 .notifier_call = ecard_reboot,
419 static void
420 ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
422 struct ecard_request req;
424 req.req = req_readbytes;
425 req.ec = ec;
426 req.address = off;
427 req.length = len;
428 req.use_loader = useld;
429 req.buffer = addr;
431 ecard_call(&req);
434 int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
436 struct ex_chunk_dir excd;
437 int index = 16;
438 int useld = 0;
440 if (!ec->cid.cd)
441 return 0;
443 while(1) {
444 ecard_readbytes(&excd, ec, index, 8, useld);
445 index += 8;
446 if (c_id(&excd) == 0) {
447 if (!useld && ec->loader) {
448 useld = 1;
449 index = 0;
450 continue;
452 return 0;
454 if (c_id(&excd) == 0xf0) { /* link */
455 index = c_start(&excd);
456 continue;
458 if (c_id(&excd) == 0x80) { /* loader */
459 if (!ec->loader) {
460 ec->loader = (loader_t)kmalloc(c_len(&excd),
461 GFP_KERNEL);
462 if (ec->loader)
463 ecard_readbytes(ec->loader, ec,
464 (int)c_start(&excd),
465 c_len(&excd), useld);
466 else
467 return 0;
469 continue;
471 if (c_id(&excd) == id && num-- == 0)
472 break;
475 if (c_id(&excd) & 0x80) {
476 switch (c_id(&excd) & 0x70) {
477 case 0x70:
478 ecard_readbytes((unsigned char *)excd.d.string, ec,
479 (int)c_start(&excd), c_len(&excd),
480 useld);
481 break;
482 case 0x00:
483 break;
486 cd->start_offset = c_start(&excd);
487 memcpy(cd->d.string, excd.d.string, 256);
488 return 1;
491 /* ======================= Interrupt control ============================ */
493 static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
495 #ifdef HAS_EXPMASK
496 if (irqnr < 4 && have_expmask) {
497 have_expmask |= 1 << irqnr;
498 __raw_writeb(have_expmask, EXPMASK_ENABLE);
500 #endif
503 static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
505 #ifdef HAS_EXPMASK
506 if (irqnr < 4 && have_expmask) {
507 have_expmask &= ~(1 << irqnr);
508 __raw_writeb(have_expmask, EXPMASK_ENABLE);
510 #endif
513 static int ecard_def_irq_pending(ecard_t *ec)
515 return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;
518 static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
520 panic("ecard_def_fiq_enable called - impossible");
523 static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
525 panic("ecard_def_fiq_disable called - impossible");
528 static int ecard_def_fiq_pending(ecard_t *ec)
530 return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;
533 static expansioncard_ops_t ecard_default_ops = {
534 ecard_def_irq_enable,
535 ecard_def_irq_disable,
536 ecard_def_irq_pending,
537 ecard_def_fiq_enable,
538 ecard_def_fiq_disable,
539 ecard_def_fiq_pending
543 * Enable and disable interrupts from expansion cards.
544 * (interrupts are disabled for these functions).
546 * They are not meant to be called directly, but via enable/disable_irq.
548 static void ecard_irq_unmask(unsigned int irqnr)
550 ecard_t *ec = slot_to_ecard(irqnr - 32);
552 if (ec) {
553 if (!ec->ops)
554 ec->ops = &ecard_default_ops;
556 if (ec->claimed && ec->ops->irqenable)
557 ec->ops->irqenable(ec, irqnr);
558 else
559 printk(KERN_ERR "ecard: rejecting request to "
560 "enable IRQs for %d\n", irqnr);
564 static void ecard_irq_mask(unsigned int irqnr)
566 ecard_t *ec = slot_to_ecard(irqnr - 32);
568 if (ec) {
569 if (!ec->ops)
570 ec->ops = &ecard_default_ops;
572 if (ec->ops && ec->ops->irqdisable)
573 ec->ops->irqdisable(ec, irqnr);
577 static struct irqchip ecard_chip = {
578 .ack = ecard_irq_mask,
579 .mask = ecard_irq_mask,
580 .unmask = ecard_irq_unmask,
583 void ecard_enablefiq(unsigned int fiqnr)
585 ecard_t *ec = slot_to_ecard(fiqnr);
587 if (ec) {
588 if (!ec->ops)
589 ec->ops = &ecard_default_ops;
591 if (ec->claimed && ec->ops->fiqenable)
592 ec->ops->fiqenable(ec, fiqnr);
593 else
594 printk(KERN_ERR "ecard: rejecting request to "
595 "enable FIQs for %d\n", fiqnr);
599 void ecard_disablefiq(unsigned int fiqnr)
601 ecard_t *ec = slot_to_ecard(fiqnr);
603 if (ec) {
604 if (!ec->ops)
605 ec->ops = &ecard_default_ops;
607 if (ec->ops->fiqdisable)
608 ec->ops->fiqdisable(ec, fiqnr);
612 static void
613 ecard_dump_irq_state(ecard_t *ec)
615 printk(" %d: %sclaimed, ",
616 ec->slot_no,
617 ec->claimed ? "" : "not ");
619 if (ec->ops && ec->ops->irqpending &&
620 ec->ops != &ecard_default_ops)
621 printk("irq %spending\n",
622 ec->ops->irqpending(ec) ? "" : "not ");
623 else
624 printk("irqaddr %p, mask = %02X, status = %02X\n",
625 ec->irqaddr, ec->irqmask, *ec->irqaddr);
628 static void ecard_check_lockup(struct irqdesc *desc)
630 static int last, lockup;
631 ecard_t *ec;
634 * If the timer interrupt has not run since the last million
635 * unrecognised expansion card interrupts, then there is
636 * something seriously wrong. Disable the expansion card
637 * interrupts so at least we can continue.
639 * Maybe we ought to start a timer to re-enable them some time
640 * later?
642 if (last == jiffies) {
643 lockup += 1;
644 if (lockup > 1000000) {
645 printk(KERN_ERR "\nInterrupt lockup detected - "
646 "disabling all expansion card interrupts\n");
648 desc->chip->mask(IRQ_EXPANSIONCARD);
650 printk("Expansion card IRQ state:\n");
652 for (ec = cards; ec; ec = ec->next)
653 ecard_dump_irq_state(ec);
655 } else
656 lockup = 0;
659 * If we did not recognise the source of this interrupt,
660 * warn the user, but don't flood the user with these messages.
662 if (!last || time_after(jiffies, last + 5*HZ)) {
663 last = jiffies;
664 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
668 static void
669 ecard_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
671 ecard_t *ec;
672 int called = 0;
674 desc->chip->mask(irq);
675 for (ec = cards; ec; ec = ec->next) {
676 int pending;
678 if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
679 continue;
681 if (ec->ops && ec->ops->irqpending)
682 pending = ec->ops->irqpending(ec);
683 else
684 pending = ecard_default_ops.irqpending(ec);
686 if (pending) {
687 struct irqdesc *d = irq_desc + ec->irq;
688 d->handle(ec->irq, d, regs);
689 called ++;
692 desc->chip->unmask(irq);
694 if (called == 0)
695 ecard_check_lockup(desc);
698 #ifdef HAS_EXPMASK
699 static unsigned char priority_masks[] =
701 0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
704 static unsigned char first_set[] =
706 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
707 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
710 static void
711 ecard_irqexp_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
713 const unsigned int statusmask = 15;
714 unsigned int status;
716 status = __raw_readb(EXPMASK_STATUS) & statusmask;
717 if (status) {
718 unsigned int slot = first_set[status];
719 ecard_t *ec = slot_to_ecard(slot);
721 if (ec->claimed) {
722 struct irqdesc *d = irqdesc + ec->irq;
724 * this ugly code is so that we can operate a
725 * prioritorising system:
727 * Card 0 highest priority
728 * Card 1
729 * Card 2
730 * Card 3 lowest priority
732 * Serial cards should go in 0/1, ethernet/scsi in 2/3
733 * otherwise you will lose serial data at high speeds!
735 d->handle(ec->irq, d, regs);
736 } else {
737 printk(KERN_WARNING "card%d: interrupt from unclaimed "
738 "card???\n", slot);
739 have_expmask &= ~(1 << slot);
740 __raw_writeb(have_expmask, EXPMASK_ENABLE);
742 } else
743 printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
746 static int __init ecard_probeirqhw(void)
748 ecard_t *ec;
749 int found;
751 __raw_writeb(0x00, EXPMASK_ENABLE);
752 __raw_writeb(0xff, EXPMASK_STATUS);
753 found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
754 __raw_writeb(0xff, EXPMASK_ENABLE);
756 if (found) {
757 printk(KERN_DEBUG "Expansion card interrupt "
758 "management hardware found\n");
760 /* for each card present, set a bit to '1' */
761 have_expmask = 0x80000000;
763 for (ec = cards; ec; ec = ec->next)
764 have_expmask |= 1 << ec->slot_no;
766 __raw_writeb(have_expmask, EXPMASK_ENABLE);
769 return found;
771 #else
772 #define ecard_irqexp_handler NULL
773 #define ecard_probeirqhw() (0)
774 #endif
776 #ifndef IO_EC_MEMC8_BASE
777 #define IO_EC_MEMC8_BASE 0
778 #endif
780 unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
782 unsigned long address = 0;
783 int slot = ec->slot_no;
785 if (ec->slot_no == 8)
786 return IO_EC_MEMC8_BASE;
788 ectcr &= ~(1 << slot);
790 switch (type) {
791 case ECARD_MEMC:
792 if (slot < 4)
793 address = IO_EC_MEMC_BASE + (slot << 12);
794 break;
796 case ECARD_IOC:
797 if (slot < 4)
798 address = IO_EC_IOC_BASE + (slot << 12);
799 #ifdef IO_EC_IOC4_BASE
800 else
801 address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
802 #endif
803 if (address)
804 address += speed << 17;
805 break;
807 #ifdef IO_EC_EASI_BASE
808 case ECARD_EASI:
809 address = IO_EC_EASI_BASE + (slot << 22);
810 if (speed == ECARD_FAST)
811 ectcr |= 1 << slot;
812 break;
813 #endif
814 default:
815 break;
818 #ifdef IOMD_ECTCR
819 iomd_writeb(ectcr, IOMD_ECTCR);
820 #endif
821 return address;
824 static int ecard_prints(char *buffer, ecard_t *ec)
826 char *start = buffer;
828 buffer += sprintf(buffer, " %d: %s ", ec->slot_no,
829 ec->type == ECARD_EASI ? "EASI" : " ");
831 if (ec->cid.id == 0) {
832 struct in_chunk_dir incd;
834 buffer += sprintf(buffer, "[%04X:%04X] ",
835 ec->cid.manufacturer, ec->cid.product);
837 if (!ec->card_desc && ec->cid.cd &&
838 ecard_readchunk(&incd, ec, 0xf5, 0)) {
839 ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
841 if (ec->card_desc)
842 strcpy((char *)ec->card_desc, incd.d.string);
845 buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
846 } else
847 buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
849 return buffer - start;
852 static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
854 ecard_t *ec = cards;
855 off_t at = 0;
856 int len, cnt;
858 cnt = 0;
859 while (ec && count > cnt) {
860 len = ecard_prints(buf, ec);
861 at += len;
862 if (at >= pos) {
863 if (!*start) {
864 *start = buf + (pos - (at - len));
865 cnt = at - pos;
866 } else
867 cnt += len;
868 buf += len;
870 ec = ec->next;
872 return (count > cnt) ? cnt : count;
875 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
877 static void ecard_proc_init(void)
879 proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
880 create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
881 get_ecard_dev_info);
885 * Probe for an expansion card.
887 * If bit 1 of the first byte of the card is set, then the
888 * card does not exist.
890 static int __init
891 ecard_probe(int slot, card_type_t type)
893 ecard_t **ecp;
894 ecard_t *ec;
895 struct ex_ecid cid;
896 int i, rc = -ENOMEM;
898 ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
899 if (!ec)
900 goto nomem;
902 memset(ec, 0, sizeof(ecard_t));
904 ec->slot_no = slot;
905 ec->type = type;
906 ec->irq = NO_IRQ;
907 ec->fiq = NO_IRQ;
908 ec->dma = NO_DMA;
909 ec->card_desc = NULL;
910 ec->ops = &ecard_default_ops;
912 rc = -ENODEV;
913 if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
914 goto nodev;
916 cid.r_zero = 1;
917 ecard_readbytes(&cid, ec, 0, 16, 0);
918 if (cid.r_zero)
919 goto nodev;
921 ec->cid.id = cid.r_id;
922 ec->cid.cd = cid.r_cd;
923 ec->cid.is = cid.r_is;
924 ec->cid.w = cid.r_w;
925 ec->cid.manufacturer = ecard_getu16(cid.r_manu);
926 ec->cid.product = ecard_getu16(cid.r_prod);
927 ec->cid.country = cid.r_country;
928 ec->cid.irqmask = cid.r_irqmask;
929 ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
930 ec->cid.fiqmask = cid.r_fiqmask;
931 ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
932 ec->fiqaddr =
933 ec->irqaddr = (unsigned char *)ioaddr(ec->podaddr);
935 if (ec->cid.is) {
936 ec->irqmask = ec->cid.irqmask;
937 ec->irqaddr += ec->cid.irqoff;
938 ec->fiqmask = ec->cid.fiqmask;
939 ec->fiqaddr += ec->cid.fiqoff;
940 } else {
941 ec->irqmask = 1;
942 ec->fiqmask = 4;
945 for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
946 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
947 blacklist[i].product == ec->cid.product) {
948 ec->card_desc = blacklist[i].type;
949 break;
953 * hook the interrupt handlers
955 if (slot < 8) {
956 ec->irq = 32 + slot;
957 set_irq_chip(ec->irq, &ecard_chip);
958 set_irq_handler(ec->irq, do_level_IRQ);
959 set_irq_flags(ec->irq, IRQF_VALID);
962 #ifdef IO_EC_MEMC8_BASE
963 if (slot == 8)
964 ec->irq = 11;
965 #endif
966 #ifdef CONFIG_ARCH_RPC
967 /* On RiscPC, only first two slots have DMA capability */
968 if (slot < 2)
969 ec->dma = 2 + slot;
970 #endif
972 for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
974 *ecp = ec;
975 slot_to_expcard[slot] = ec;
977 snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
978 strcpy(ec->dev.name, "fixme!");
979 ec->dev.parent = NULL;
980 ec->dev.bus = &ecard_bus_type;
982 device_register(&ec->dev);
984 return 0;
986 nodev:
987 kfree(ec);
988 nomem:
989 return rc;
993 * Initialise the expansion card system.
994 * Locate all hardware - interrupt management and
995 * actual cards.
997 static int __init ecard_init(void)
999 int slot, irqhw;
1002 * Register our reboot notifier
1004 register_reboot_notifier(&ecard_reboot_notifier);
1006 #ifdef CONFIG_CPU_32
1007 init_waitqueue_head(&ecard_wait);
1008 #endif
1010 printk("Probing expansion cards\n");
1012 for (slot = 0; slot < 8; slot ++) {
1013 if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1014 ecard_probe(slot, ECARD_IOC);
1017 #ifdef IO_EC_MEMC8_BASE
1018 ecard_probe(8, ECARD_IOC);
1019 #endif
1021 irqhw = ecard_probeirqhw();
1023 set_irq_chained_handler(IRQ_EXPANSIONCARD,
1024 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
1026 ecard_proc_init();
1028 return 0;
1031 subsys_initcall(ecard_init);
1034 * ECARD "bus"
1036 static const struct ecard_id *
1037 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1039 int i;
1041 for (i = 0; ids[i].manufacturer != 65535; i++)
1042 if (ec->cid.manufacturer == ids[i].manufacturer &&
1043 ec->cid.product == ids[i].product)
1044 return ids + i;
1046 return NULL;
1049 static int ecard_drv_probe(struct device *dev)
1051 struct expansion_card *ec = ECARD_DEV(dev);
1052 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1053 const struct ecard_id *id;
1054 int ret;
1056 id = ecard_match_device(drv->id_table, ec);
1058 ecard_claim(ec);
1059 ret = drv->probe(ec, id);
1060 if (ret)
1061 ecard_release(ec);
1062 return ret;
1065 static int ecard_drv_remove(struct device *dev)
1067 struct expansion_card *ec = ECARD_DEV(dev);
1068 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1070 drv->remove(ec);
1071 ecard_release(ec);
1073 return 0;
1076 int ecard_register_driver(struct ecard_driver *drv)
1078 drv->drv.bus = &ecard_bus_type;
1079 drv->drv.probe = ecard_drv_probe;
1080 drv->drv.remove = ecard_drv_remove;
1082 return driver_register(&drv->drv);
1085 void ecard_remove_driver(struct ecard_driver *drv)
1087 driver_unregister(&drv->drv);
1090 static int ecard_match(struct device *_dev, struct device_driver *_drv)
1092 struct expansion_card *ec = ECARD_DEV(_dev);
1093 struct ecard_driver *drv = ECARD_DRV(_drv);
1094 int ret;
1096 if (drv->id_table) {
1097 ret = ecard_match_device(drv->id_table, ec) != NULL;
1098 } else {
1099 ret = ec->cid.id == drv->id;
1102 return ret;
1105 struct bus_type ecard_bus_type = {
1106 .name = "ecard",
1107 .match = ecard_match,
1110 static int ecard_bus_init(void)
1112 return bus_register(&ecard_bus_type);
1115 postcore_initcall(ecard_bus_init);
1117 EXPORT_SYMBOL(ecard_readchunk);
1118 EXPORT_SYMBOL(ecard_address);
1119 EXPORT_SYMBOL(ecard_register_driver);
1120 EXPORT_SYMBOL(ecard_remove_driver);
1121 EXPORT_SYMBOL(ecard_bus_type);