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