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