File: /usr/src/linux/arch/arm/kernel/ecard.c

1     /*
2      *  linux/arch/arm/kernel/ecard.c
3      *
4      *  Copyright 1995-2001 Russell King
5      *
6      * This program is free software; you can redistribute it and/or modify
7      * it under the terms of the GNU General Public License version 2 as
8      * published by the Free Software Foundation.
9      *
10      *  Find all installed expansion cards, and handle interrupts from them.
11      *
12      *  Created from information from Acorns RiscOS3 PRMs
13      *
14      *  08-Dec-1996	RMK	Added code for the 9'th expansion card - the ether
15      *			podule slot.
16      *  06-May-1997	RMK	Added blacklist for cards whose loader doesn't work.
17      *  12-Sep-1997	RMK	Created new handling of interrupt enables/disables
18      *			- cards can now register their own routine to control
19      *			interrupts (recommended).
20      *  29-Sep-1997	RMK	Expansion card interrupt hardware not being re-enabled
21      *			on reset from Linux. (Caused cards not to respond
22      *			under RiscOS without hard reset).
23      *  15-Feb-1998	RMK	Added DMA support
24      *  12-Sep-1998	RMK	Added EASI support
25      *  10-Jan-1999	RMK	Run loaders in a simulated RISC OS environment.
26      *  17-Apr-1999	RMK	Support for EASI Type C cycles.
27      */
28     #define ECARD_C
29     
30     #include <linux/config.h>
31     #include <linux/module.h>
32     #include <linux/kernel.h>
33     #include <linux/types.h>
34     #include <linux/sched.h>
35     #include <linux/interrupt.h>
36     #include <linux/reboot.h>
37     #include <linux/mm.h>
38     #include <linux/slab.h>
39     #include <linux/proc_fs.h>
40     #include <linux/notifier.h>
41     #include <linux/init.h>
42     
43     #include <asm/dma.h>
44     #include <asm/ecard.h>
45     #include <asm/hardware.h>
46     #include <asm/io.h>
47     #include <asm/irq.h>
48     #include <asm/pgalloc.h>
49     #include <asm/mmu_context.h>
50     #include <asm/mach/irq.h>
51     
52     #ifndef CONFIG_ARCH_RPC
53     #define HAVE_EXPMASK
54     #endif
55     
56     enum req {
57     	req_readbytes,
58     	req_reset_all
59     };
60     
61     struct ecard_request {
62     	enum req	req;
63     	ecard_t		*ec;
64     	unsigned int	address;
65     	unsigned int	length;
66     	unsigned int	use_loader;
67     	void		*buffer;
68     };
69     
70     struct expcard_blacklist {
71     	unsigned short	 manufacturer;
72     	unsigned short	 product;
73     	const char	*type;
74     };
75     
76     static ecard_t *cards;
77     static ecard_t *slot_to_expcard[MAX_ECARDS];
78     static unsigned int ectcr;
79     #ifdef HAS_EXPMASK
80     static unsigned int have_expmask;
81     #endif
82     
83     /* List of descriptions of cards which don't have an extended
84      * identification, or chunk directories containing a description.
85      */
86     static struct expcard_blacklist __initdata blacklist[] = {
87     	{ MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
88     };
89     
90     asmlinkage extern int
91     ecard_loader_reset(volatile unsigned char *pa, loader_t loader);
92     asmlinkage extern int
93     ecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);
94     extern int setup_arm_irq(int, struct irqaction *);
95     extern void do_ecard_IRQ(int, struct pt_regs *);
96     
97     
98     static void
99     ecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs);
100     
101     static struct irqaction irqexpansioncard = {
102     	ecard_irq_noexpmask, SA_INTERRUPT, 0, "expansion cards", NULL, NULL
103     };
104     
105     static inline unsigned short
106     ecard_getu16(unsigned char *v)
107     {
108     	return v[0] | v[1] << 8;
109     }
110     
111     static inline signed long
112     ecard_gets24(unsigned char *v)
113     {
114     	return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
115     }
116     
117     static inline ecard_t *
118     slot_to_ecard(unsigned int slot)
119     {
120     	return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
121     }
122     
123     /* ===================== Expansion card daemon ======================== */
124     /*
125      * Since the loader programs on the expansion cards need to be run
126      * in a specific environment, create a separate task with this
127      * environment up, and pass requests to this task as and when we
128      * need to.
129      *
130      * This should allow 99% of loaders to be called from Linux.
131      *
132      * From a security standpoint, we trust the card vendors.  This
133      * may be a misplaced trust.
134      */
135     #define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)
136     #define POD_INT_ADDR(x)	((volatile unsigned char *)\
137     			 ((BUS_ADDR((x)) - IO_BASE) + IO_START))
138     
139     static inline void ecard_task_reset(void)
140     {
141     	ecard_t *ec;
142     
143     	for (ec = cards; ec; ec = ec->next)
144     		if (ec->loader)
145     			ecard_loader_reset(POD_INT_ADDR(ec->podaddr),
146     					   ec->loader);
147     }
148     
149     static void
150     ecard_task_readbytes(struct ecard_request *req)
151     {
152     	unsigned char *buf = (unsigned char *)req->buffer;
153     	volatile unsigned char *base_addr =
154     		(volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr);
155     	unsigned int len = req->length;
156     	unsigned int off = req->address;
157     
158     	if (req->ec->slot_no == 8) {
159     		/*
160     		 * The card maintains an index which increments the address
161     		 * into a 4096-byte page on each access.  We need to keep
162     		 * track of the counter.
163     		 */
164     		static unsigned int index;
165     		unsigned int page;
166     
167     		page = (off >> 12) * 4;
168     		if (page > 256 * 4)
169     			return;
170     
171     		off &= 4095;
172     
173     		/*
174     		 * If we are reading offset 0, or our current index is
175     		 * greater than the offset, reset the hardware index counter.
176     		 */
177     		if (off == 0 || index > off) {
178     			*base_addr = 0;
179     			index = 0;
180     		}
181     
182     		/*
183     		 * Increment the hardware index counter until we get to the
184     		 * required offset.  The read bytes are discarded.
185     		 */
186     		while (index < off) {
187     			unsigned char byte;
188     			byte = base_addr[page];
189     			index += 1;
190     		}
191     
192     		while (len--) {
193     			*buf++ = base_addr[page];
194     			index += 1;
195     		}
196     	} else {
197     
198     		if (!req->use_loader || !req->ec->loader) {
199     			off *= 4;
200     			while (len--) {
201     				*buf++ = base_addr[off];
202     				off += 4;
203     			}
204     		} else {
205     			while(len--) {
206     				/*
207     				 * The following is required by some
208     				 * expansion card loader programs.
209     				 */
210     				*(unsigned long *)0x108 = 0;
211     				*buf++ = ecard_loader_read(off++, base_addr,
212     							   req->ec->loader);
213     			}
214     		}
215     	}
216     
217     }
218     
219     static void ecard_do_request(struct ecard_request *req)
220     {
221     	switch (req->req) {
222     	case req_readbytes:
223     		ecard_task_readbytes(req);
224     		break;
225     
226     	case req_reset_all:
227     		ecard_task_reset();
228     		break;
229     	}
230     }
231     
232     #ifdef CONFIG_CPU_32
233     #include <linux/completion.h>
234     
235     static pid_t ecard_pid;
236     static wait_queue_head_t ecard_wait;
237     static struct ecard_request *ecard_req;
238     
239     static DECLARE_COMPLETION(ecard_completion);
240     
241     /*
242      * Set up the expansion card daemon's page tables.
243      */
244     static void ecard_init_pgtables(struct mm_struct *mm)
245     {
246     	/* We want to set up the page tables for the following mapping:
247     	 *  Virtual	Physical
248     	 *  0x03000000	0x03000000
249     	 *  0x03010000	unmapped
250     	 *  0x03210000	0x03210000
251     	 *  0x03400000	unmapped
252     	 *  0x08000000	0x08000000
253     	 *  0x10000000	unmapped
254     	 *
255     	 * FIXME: we don't follow this 100% yet.
256     	 */
257     	pgd_t *src_pgd, *dst_pgd;
258     	unsigned int dst_addr = IO_START;
259     
260     	src_pgd = pgd_offset(mm, IO_BASE);
261     	dst_pgd = pgd_offset(mm, dst_addr);
262     
263     	while (dst_addr < IO_START + IO_SIZE) {
264     		*dst_pgd++ = *src_pgd++;
265     		dst_addr += PGDIR_SIZE;
266     	}
267     
268     	dst_addr = EASI_START;
269     	src_pgd = pgd_offset(mm, EASI_BASE);
270     	dst_pgd = pgd_offset(mm, dst_addr);
271     
272     	while (dst_addr < EASI_START + EASI_SIZE) {
273     		*dst_pgd++ = *src_pgd++;
274     		dst_addr += PGDIR_SIZE;
275     	}
276     
277     	flush_tlb_range(mm, IO_START, IO_START + IO_SIZE);
278     	flush_tlb_range(mm, EASI_START, EASI_START + EASI_SIZE);
279     }
280     
281     static int ecard_init_mm(void)
282     {
283     	struct mm_struct * mm = mm_alloc();
284     	struct mm_struct *active_mm = current->active_mm;
285     
286     	if (!mm)
287     		return -ENOMEM;
288     
289     	current->mm = mm;
290     	current->active_mm = mm;
291     	activate_mm(active_mm, mm);
292     	mmdrop(active_mm);
293     	ecard_init_pgtables(mm);
294     	return 0;
295     }
296     
297     static int
298     ecard_task(void * unused)
299     {
300     	struct task_struct *tsk = current;
301     
302     	/*
303     	 * We don't want /any/ signals, not even SIGKILL
304     	 */
305     	sigfillset(&tsk->blocked);
306     	sigemptyset(&tsk->pending.signal);
307     	recalc_sigpending(tsk);
308     	strcpy(tsk->comm, "kecardd");
309     	daemonize();
310     
311     	/*
312     	 * Allocate a mm.  We're not a lazy-TLB kernel task since we need
313     	 * to set page table entries where the user space would be.  Note
314     	 * that this also creates the page tables.  Failure is not an
315     	 * option here.
316     	 */
317     	if (ecard_init_mm())
318     		panic("kecardd: unable to alloc mm\n");
319     
320     	while (1) {
321     		struct ecard_request *req;
322     
323     		do {
324     			req = xchg(&ecard_req, NULL);
325     
326     			if (req == NULL) {
327     				sigemptyset(&tsk->pending.signal);
328     				interruptible_sleep_on(&ecard_wait);
329     			}
330     		} while (req == NULL);
331     
332     		ecard_do_request(req);
333     		complete(&ecard_completion);
334     	}
335     }
336     
337     /*
338      * Wake the expansion card daemon to action our request.
339      *
340      * FIXME: The test here is not sufficient to detect if the
341      * kcardd is running.
342      */
343     static void
344     ecard_call(struct ecard_request *req)
345     {
346     	/*
347     	 * Make sure we have a context that is able to sleep.
348     	 */
349     	if (current == &init_task || in_interrupt())
350     		BUG();
351     
352     	if (ecard_pid <= 0)
353     		ecard_pid = kernel_thread(ecard_task, NULL,
354     				CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
355     
356     	ecard_req = req;
357     	wake_up(&ecard_wait);
358     
359     	/*
360     	 * Now wait for kecardd to run.
361     	 */
362     	wait_for_completion(&ecard_completion);
363     }
364     #else
365     /*
366      * On 26-bit processors, we don't need the kcardd thread to access the
367      * expansion card loaders.  We do it directly.
368      */
369     #define ecard_call(req)	ecard_do_request(req)
370     #endif
371     
372     /* ======================= Mid-level card control ===================== */
373     
374     /*
375      * This function is responsible for resetting the expansion cards to a
376      * sensible state immediately prior to rebooting the system.  This function
377      * has process state (keventd), so we can sleep.
378      *
379      * Possible "val" values here:
380      *  SYS_RESTART   -  restarting system
381      *  SYS_HALT      - halting system
382      *  SYS_POWER_OFF - powering down system
383      *
384      * We ignore all calls, unless it is a SYS_RESTART call - power down/halts
385      * will be followed by a SYS_RESTART if ctrl-alt-del is pressed again.
386      */
387     static int ecard_reboot(struct notifier_block *me, unsigned long val, void *v)
388     {
389     	struct ecard_request req;
390     
391     	if (val != SYS_RESTART)
392     		return 0;
393     
394     	/*
395     	 * Disable the expansion card interrupt
396     	 */
397     	disable_irq(IRQ_EXPANSIONCARD);
398     
399     	/*
400     	 * If we have any expansion card loader code which will handle
401     	 * the reset for us, call it now.
402     	 */
403     	req.req = req_reset_all;
404     	ecard_call(&req);
405     
406     	/*
407     	 * Disable the expansion card interrupt again, just to be sure.
408     	 */
409     	disable_irq(IRQ_EXPANSIONCARD);
410     
411     	/*
412     	 * Finally, reset the expansion card interrupt mask to
413     	 * all enable (RISC OS doesn't set this)
414     	 */
415     #ifdef HAS_EXPMASK
416     	have_expmask = ~0;
417     	__raw_writeb(have_expmask, EXPMASK_ENABLE);
418     #endif
419     	return 0;
420     }
421     
422     static struct notifier_block ecard_reboot_notifier = {
423     	notifier_call:	ecard_reboot,
424     };
425     
426     
427     
428     static void
429     ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
430     {
431     	struct ecard_request req;
432     
433     	req.req		= req_readbytes;
434     	req.ec		= ec;
435     	req.address	= off;
436     	req.length	= len;
437     	req.use_loader	= useld;
438     	req.buffer	= addr;
439     
440     	ecard_call(&req);
441     }
442     
443     int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
444     {
445     	struct ex_chunk_dir excd;
446     	int index = 16;
447     	int useld = 0;
448     
449     	if (!ec->cid.cd)
450     		return 0;
451     
452     	while(1) {
453     		ecard_readbytes(&excd, ec, index, 8, useld);
454     		index += 8;
455     		if (c_id(&excd) == 0) {
456     			if (!useld && ec->loader) {
457     				useld = 1;
458     				index = 0;
459     				continue;
460     			}
461     			return 0;
462     		}
463     		if (c_id(&excd) == 0xf0) { /* link */
464     			index = c_start(&excd);
465     			continue;
466     		}
467     		if (c_id(&excd) == 0x80) { /* loader */
468     			if (!ec->loader) {
469     				ec->loader = (loader_t)kmalloc(c_len(&excd),
470     							       GFP_KERNEL);
471     				if (ec->loader)
472     					ecard_readbytes(ec->loader, ec,
473     							(int)c_start(&excd),
474     							c_len(&excd), useld);
475     				else
476     					return 0;
477     			}
478     			continue;
479     		}
480     		if (c_id(&excd) == id && num-- == 0)
481     			break;
482     	}
483     
484     	if (c_id(&excd) & 0x80) {
485     		switch (c_id(&excd) & 0x70) {
486     		case 0x70:
487     			ecard_readbytes((unsigned char *)excd.d.string, ec,
488     					(int)c_start(&excd), c_len(&excd),
489     					useld);
490     			break;
491     		case 0x00:
492     			break;
493     		}
494     	}
495     	cd->start_offset = c_start(&excd);
496     	memcpy(cd->d.string, excd.d.string, 256);
497     	return 1;
498     }
499     
500     /* ======================= Interrupt control ============================ */
501     
502     static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
503     {
504     #ifdef HAS_EXPMASK
505     	if (irqnr < 4 && have_expmask) {
506     		have_expmask |= 1 << irqnr;
507     		__raw_writeb(have_expmask, EXPMASK_ENABLE);
508     	}
509     #endif
510     }
511     
512     static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
513     {
514     #ifdef HAS_EXPMASK
515     	if (irqnr < 4 && have_expmask) {
516     		have_expmask &= ~(1 << irqnr);
517     		__raw_writeb(have_expmask, EXPMASK_ENABLE);
518     	}
519     #endif
520     }
521     
522     static int ecard_def_irq_pending(ecard_t *ec)
523     {
524     	return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;
525     }
526     
527     static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
528     {
529     	panic("ecard_def_fiq_enable called - impossible");
530     }
531     
532     static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
533     {
534     	panic("ecard_def_fiq_disable called - impossible");
535     }
536     
537     static int ecard_def_fiq_pending(ecard_t *ec)
538     {
539     	return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;
540     }
541     
542     static expansioncard_ops_t ecard_default_ops = {
543     	ecard_def_irq_enable,
544     	ecard_def_irq_disable,
545     	ecard_def_irq_pending,
546     	ecard_def_fiq_enable,
547     	ecard_def_fiq_disable,
548     	ecard_def_fiq_pending
549     };
550     
551     /*
552      * Enable and disable interrupts from expansion cards.
553      * (interrupts are disabled for these functions).
554      *
555      * They are not meant to be called directly, but via enable/disable_irq.
556      */
557     static void ecard_enableirq(unsigned int irqnr)
558     {
559     	ecard_t *ec = slot_to_ecard(irqnr - 32);
560     
561     	if (ec) {
562     		if (!ec->ops)
563     			ec->ops = &ecard_default_ops;
564     
565     		if (ec->claimed && ec->ops->irqenable)
566     			ec->ops->irqenable(ec, irqnr);
567     		else
568     			printk(KERN_ERR "ecard: rejecting request to "
569     				"enable IRQs for %d\n", irqnr);
570     	}
571     }
572     
573     static void ecard_disableirq(unsigned int irqnr)
574     {
575     	ecard_t *ec = slot_to_ecard(irqnr - 32);
576     
577     	if (ec) {
578     		if (!ec->ops)
579     			ec->ops = &ecard_default_ops;
580     
581     		if (ec->ops && ec->ops->irqdisable)
582     			ec->ops->irqdisable(ec, irqnr);
583     	}
584     }
585     
586     void ecard_enablefiq(unsigned int fiqnr)
587     {
588     	ecard_t *ec = slot_to_ecard(fiqnr);
589     
590     	if (ec) {
591     		if (!ec->ops)
592     			ec->ops = &ecard_default_ops;
593     
594     		if (ec->claimed && ec->ops->fiqenable)
595     			ec->ops->fiqenable(ec, fiqnr);
596     		else
597     			printk(KERN_ERR "ecard: rejecting request to "
598     				"enable FIQs for %d\n", fiqnr);
599     	}
600     }
601     
602     void ecard_disablefiq(unsigned int fiqnr)
603     {
604     	ecard_t *ec = slot_to_ecard(fiqnr);
605     
606     	if (ec) {
607     		if (!ec->ops)
608     			ec->ops = &ecard_default_ops;
609     
610     		if (ec->ops->fiqdisable)
611     			ec->ops->fiqdisable(ec, fiqnr);
612     	}
613     }
614     
615     static void
616     ecard_dump_irq_state(ecard_t *ec)
617     {
618     	printk("  %d: %sclaimed, ",
619     	       ec->slot_no,
620     	       ec->claimed ? "" : "not ");
621     
622     	if (ec->ops && ec->ops->irqpending &&
623     	    ec->ops != &ecard_default_ops)
624     		printk("irq %spending\n",
625     		       ec->ops->irqpending(ec) ? "" : "not ");
626     	else
627     		printk("irqaddr %p, mask = %02X, status = %02X\n",
628     		       ec->irqaddr, ec->irqmask, *ec->irqaddr);
629     }
630     
631     static void
632     ecard_check_lockup(void)
633     {
634     	static int last, lockup;
635     	ecard_t *ec;
636     
637     	/*
638     	 * If the timer interrupt has not run since the last million
639     	 * unrecognised expansion card interrupts, then there is
640     	 * something seriously wrong.  Disable the expansion card
641     	 * interrupts so at least we can continue.
642     	 *
643     	 * Maybe we ought to start a timer to re-enable them some time
644     	 * later?
645     	 */
646     	if (last == jiffies) {
647     		lockup += 1;
648     		if (lockup > 1000000) {
649     			printk(KERN_ERR "\nInterrupt lockup detected - "
650     			       "disabling all expansion card interrupts\n");
651     
652     			disable_irq(IRQ_EXPANSIONCARD);
653     
654     			printk("Expansion card IRQ state:\n");
655     
656     			for (ec = cards; ec; ec = ec->next)
657     				ecard_dump_irq_state(ec);
658     		}
659     	} else
660     		lockup = 0;
661     
662     	/*
663     	 * If we did not recognise the source of this interrupt,
664     	 * warn the user, but don't flood the user with these messages.
665     	 */
666     	if (!last || time_after(jiffies, last + 5*HZ)) {
667     		last = jiffies;
668     		printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
669     	}
670     }
671     
672     static void
673     ecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs)
674     {
675     	ecard_t *ec;
676     	int called = 0;
677     
678     	for (ec = cards; ec; ec = ec->next) {
679     		int pending;
680     
681     		if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
682     			continue;
683     
684     		if (ec->ops && ec->ops->irqpending)
685     			pending = ec->ops->irqpending(ec);
686     		else
687     			pending = ecard_default_ops.irqpending(ec);
688     
689     		if (pending) {
690     			do_ecard_IRQ(ec->irq, regs);
691     			called ++;
692     		}
693     	}
694     	cli();
695     
696     	if (called == 0)
697     		ecard_check_lockup();
698     }
699     
700     #ifdef HAS_EXPMASK
701     static unsigned char priority_masks[] =
702     {
703     	0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
704     };
705     
706     static unsigned char first_set[] =
707     {
708     	0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
709     	0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
710     };
711     
712     static void
713     ecard_irq_expmask(int intr_no, void *dev_id, struct pt_regs *regs)
714     {
715     	const unsigned int statusmask = 15;
716     	unsigned int status;
717     
718     	status = __raw_readb(EXPMASK_STATUS) & statusmask;
719     	if (status) {
720     		unsigned int slot;
721     		ecard_t *ec;
722     again:
723     		slot = first_set[status];
724     		ec = slot_to_ecard(slot);
725     		if (ec->claimed) {
726     			unsigned int oldexpmask;
727     			/*
728     			 * this ugly code is so that we can operate a
729     			 * prioritorising system:
730     			 *
731     			 * Card 0 	highest priority
732     			 * Card 1
733     			 * Card 2
734     			 * Card 3	lowest priority
735     			 *
736     			 * Serial cards should go in 0/1, ethernet/scsi in 2/3
737     			 * otherwise you will lose serial data at high speeds!
738     			 */
739     			oldexpmask = have_expmask;
740     			have_expmask &= priority_masks[slot];
741     			__raw_writeb(have_expmask, EXPMASK_ENABLE);
742     			sti();
743     			do_ecard_IRQ(ec->irq, regs);
744     			cli();
745     			have_expmask = oldexpmask;
746     			__raw_writeb(have_expmask, EXPMASK_ENABLE);
747     			status = __raw_readb(EXPMASK_STATUS) & statusmask;
748     			if (status)
749     				goto again;
750     		} else {
751     			printk(KERN_WARNING "card%d: interrupt from unclaimed "
752     			       "card???\n", slot);
753     			have_expmask &= ~(1 << slot);
754     			__raw_writeb(have_expmask, EXPMASK_ENABLE);
755     		}
756     	} else
757     		printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
758     }
759     
760     static void __init
761     ecard_probeirqhw(void)
762     {
763     	ecard_t *ec;
764     	int found;
765     
766     	__raw_writeb(0x00, EXPMASK_ENABLE);
767     	__raw_writeb(0xff, EXPMASK_STATUS);
768     	found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
769     	__raw_writeb(0xff, EXPMASK_ENABLE);
770     
771     	if (!found)
772     		return;
773     
774     	printk(KERN_DEBUG "Expansion card interrupt "
775     	       "management hardware found\n");
776     
777     	irqexpansioncard.handler = ecard_irq_expmask;
778     
779     	/* for each card present, set a bit to '1' */
780     	have_expmask = 0x80000000;
781     
782     	for (ec = cards; ec; ec = ec->next)
783     		have_expmask |= 1 << ec->slot_no;
784     
785     	__raw_writeb(have_expmask, EXPMASK_ENABLE);
786     }
787     #else
788     #define ecard_probeirqhw()
789     #endif
790     
791     #ifndef IO_EC_MEMC8_BASE
792     #define IO_EC_MEMC8_BASE 0
793     #endif
794     
795     unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
796     {
797     	unsigned long address = 0;
798     	int slot = ec->slot_no;
799     
800     	if (ec->slot_no == 8)
801     		return IO_EC_MEMC8_BASE;
802     
803     	ectcr &= ~(1 << slot);
804     
805     	switch (type) {
806     	case ECARD_MEMC:
807     		if (slot < 4)
808     			address = IO_EC_MEMC_BASE + (slot << 12);
809     		break;
810     
811     	case ECARD_IOC:
812     		if (slot < 4)
813     			address = IO_EC_IOC_BASE + (slot << 12);
814     #ifdef IO_EC_IOC4_BASE
815     		else
816     			address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
817     #endif
818     		if (address)
819     			address +=  speed << 17;
820     		break;
821     
822     #ifdef IO_EC_EASI_BASE
823     	case ECARD_EASI:
824     		address = IO_EC_EASI_BASE + (slot << 22);
825     		if (speed == ECARD_FAST)
826     			ectcr |= 1 << slot;
827     		break;
828     #endif
829     	default:
830     		break;
831     	}
832     
833     #ifdef IOMD_ECTCR
834     	iomd_writeb(ectcr, IOMD_ECTCR);
835     #endif
836     	return address;
837     }
838     
839     static int ecard_prints(char *buffer, ecard_t *ec)
840     {
841     	char *start = buffer;
842     
843     	buffer += sprintf(buffer, "  %d: %s ", ec->slot_no,
844     			  ec->type == ECARD_EASI ? "EASI" : "    ");
845     
846     	if (ec->cid.id == 0) {
847     		struct in_chunk_dir incd;
848     
849     		buffer += sprintf(buffer, "[%04X:%04X] ",
850     			ec->cid.manufacturer, ec->cid.product);
851     
852     		if (!ec->card_desc && ec->cid.cd &&
853     		    ecard_readchunk(&incd, ec, 0xf5, 0)) {
854     			ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
855     
856     			if (ec->card_desc)
857     				strcpy((char *)ec->card_desc, incd.d.string);
858     		}
859     
860     		buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
861     	} else
862     		buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
863     
864     	return buffer - start;
865     }
866     
867     static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
868     {
869     	ecard_t *ec = cards;
870     	off_t at = 0;
871     	int len, cnt;
872     
873     	cnt = 0;
874     	while (ec && count > cnt) {
875     		len = ecard_prints(buf, ec);
876     		at += len;
877     		if (at >= pos) {
878     			if (!*start) {
879     				*start = buf + (pos - (at - len));
880     				cnt = at - pos;
881     			} else
882     				cnt += len;
883     			buf += len;
884     		}
885     		ec = ec->next;
886     	}
887     	return (count > cnt) ? cnt : count;
888     }
889     
890     static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
891     
892     static void ecard_proc_init(void)
893     {
894     	proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
895     	create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
896     		get_ecard_dev_info);
897     }
898     
899     /*
900      * Probe for an expansion card.
901      *
902      * If bit 1 of the first byte of the card is set, then the
903      * card does not exist.
904      */
905     static int __init
906     ecard_probe(int slot, card_type_t type)
907     {
908     	ecard_t **ecp;
909     	ecard_t *ec;
910     	struct ex_ecid cid;
911     	int i, rc = -ENOMEM;
912     
913     	ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
914     	if (!ec)
915     		goto nomem;
916     
917     	memset(ec, 0, sizeof(ecard_t));
918     
919     	ec->slot_no	= slot;
920     	ec->type	= type;
921     	ec->irq		= NO_IRQ;
922     	ec->fiq		= NO_IRQ;
923     	ec->dma		= NO_DMA;
924     	ec->card_desc	= NULL;
925     	ec->ops		= &ecard_default_ops;
926     
927     	rc = -ENODEV;
928     	if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
929     		goto nodev;
930     
931     	cid.r_zero = 1;
932     	ecard_readbytes(&cid, ec, 0, 16, 0);
933     	if (cid.r_zero)
934     		goto nodev;
935     
936     	ec->cid.id	= cid.r_id;
937     	ec->cid.cd	= cid.r_cd;
938     	ec->cid.is	= cid.r_is;
939     	ec->cid.w	= cid.r_w;
940     	ec->cid.manufacturer = ecard_getu16(cid.r_manu);
941     	ec->cid.product = ecard_getu16(cid.r_prod);
942     	ec->cid.country = cid.r_country;
943     	ec->cid.irqmask = cid.r_irqmask;
944     	ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
945     	ec->cid.fiqmask = cid.r_fiqmask;
946     	ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
947     	ec->fiqaddr	=
948     	ec->irqaddr	= (unsigned char *)ioaddr(ec->podaddr);
949     
950     	if (ec->cid.is) {
951     		ec->irqmask = ec->cid.irqmask;
952     		ec->irqaddr += ec->cid.irqoff;
953     		ec->fiqmask = ec->cid.fiqmask;
954     		ec->fiqaddr += ec->cid.fiqoff;
955     	} else {
956     		ec->irqmask = 1;
957     		ec->fiqmask = 4;
958     	}
959     
960     	for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
961     		if (blacklist[i].manufacturer == ec->cid.manufacturer &&
962     		    blacklist[i].product == ec->cid.product) {
963     			ec->card_desc = blacklist[i].type;
964     			break;
965     		}
966     
967     	ec->irq = 32 + slot;
968     #ifdef IO_EC_MEMC8_BASE
969     	if (slot == 8)
970     		ec->irq = 11;
971     #endif
972     	/*
973     	 * hook the interrupt handlers
974     	 */
975     	if (ec->irq != 0 && ec->irq >= 32) {
976     		irq_desc[ec->irq].mask_ack = ecard_disableirq;
977     		irq_desc[ec->irq].mask     = ecard_disableirq;
978     		irq_desc[ec->irq].unmask   = ecard_enableirq;
979     		irq_desc[ec->irq].valid    = 1;
980     	}
981     
982     #ifdef CONFIG_ARCH_RPC
983     	/* On RiscPC, only first two slots have DMA capability */
984     	if (slot < 2)
985     		ec->dma = 2 + slot;
986     #endif
987     
988     	for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
989     
990     	*ecp = ec;
991     	slot_to_expcard[slot] = ec;
992     	return 0;
993     
994     nodev:
995     	kfree(ec);
996     nomem:
997     	return rc;
998     }
999     
1000     static ecard_t *finding_pos;
1001     
1002     void ecard_startfind(void)
1003     {
1004     	finding_pos = NULL;
1005     }
1006     
1007     ecard_t *ecard_find(int cid, const card_ids *cids)
1008     {
1009     	if (!finding_pos)
1010     		finding_pos = cards;
1011     	else
1012     		finding_pos = finding_pos->next;
1013     
1014     	for (; finding_pos; finding_pos = finding_pos->next) {
1015     		if (finding_pos->claimed)
1016     			continue;
1017     
1018     		if (!cids) {
1019     			if ((finding_pos->cid.id ^ cid) == 0)
1020     				break;
1021     		} else {
1022     			unsigned int manufacturer, product;
1023     			int i;
1024     
1025     			manufacturer = finding_pos->cid.manufacturer;
1026     			product = finding_pos->cid.product;
1027     
1028     			for (i = 0; cids[i].manufacturer != 65535; i++)
1029     				if (manufacturer == cids[i].manufacturer &&
1030     				    product == cids[i].product)
1031     					break;
1032     
1033     			if (cids[i].manufacturer != 65535)
1034     				break;
1035     		}
1036     	}
1037     
1038     	return finding_pos;
1039     }
1040     
1041     static void __init ecard_free_all(void)
1042     {
1043     	ecard_t *ec, *ecn;
1044     
1045     	for (ec = cards; ec; ec = ecn) {
1046     		ecn = ec->next;
1047     
1048     		kfree(ec);
1049     	}
1050     
1051     	cards = NULL;
1052     
1053     	memset(slot_to_expcard, 0, sizeof(slot_to_expcard));
1054     }
1055     
1056     /*
1057      * Initialise the expansion card system.
1058      * Locate all hardware - interrupt management and
1059      * actual cards.
1060      */
1061     void __init ecard_init(void)
1062     {
1063     	int slot;
1064     
1065     	/*
1066     	 * Register our reboot notifier
1067     	 */
1068     	register_reboot_notifier(&ecard_reboot_notifier);
1069     
1070     #ifdef CONFIG_CPU_32
1071     	init_waitqueue_head(&ecard_wait);
1072     #endif
1073     
1074     	printk("Probing expansion cards\n");
1075     
1076     	for (slot = 0; slot < 8; slot ++) {
1077     		if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1078     			ecard_probe(slot, ECARD_IOC);
1079     	}
1080     
1081     #ifdef IO_EC_MEMC8_BASE
1082     	ecard_probe(8, ECARD_IOC);
1083     #endif
1084     
1085     	ecard_probeirqhw();
1086     
1087     	if (setup_arm_irq(IRQ_EXPANSIONCARD, &irqexpansioncard)) {
1088     		printk(KERN_ERR "Unable to claim IRQ%d for expansion cards\n",
1089     		       IRQ_EXPANSIONCARD);
1090     		ecard_free_all();
1091     	}
1092     
1093     	ecard_proc_init();
1094     }
1095     
1096     EXPORT_SYMBOL(ecard_startfind);
1097     EXPORT_SYMBOL(ecard_find);
1098     EXPORT_SYMBOL(ecard_readchunk);
1099     EXPORT_SYMBOL(ecard_address);
1100