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/list.h>
42 #include <linux/timer.h>
43 #include <linux/init.h>
44 #include <linux/ioport.h>
45 
46 #include <asm/dma.h>
47 #include <asm/ecard.h>
48 #include <asm/hardware.h>
49 #include <asm/io.h>
50 #include <asm/irq.h>
51 #include <asm/pgalloc.h>
52 #include <asm/mmu_context.h>
53 #include <asm/mach/irq.h>
54 
55 #ifndef CONFIG_ARCH_RPC
56 #define HAVE_EXPMASK
57 #endif
58 
59 enum req {
60 	req_readbytes,
61 	req_reset
62 };
63 
64 struct ecard_request {
65 	enum req	req;
66 	ecard_t		*ec;
67 	unsigned int	address;
68 	unsigned int	length;
69 	unsigned int	use_loader;
70 	void		*buffer;
71 };
72 
73 struct expcard_blacklist {
74 	unsigned short	 manufacturer;
75 	unsigned short	 product;
76 	const char	*type;
77 };
78 
79 static ecard_t *cards;
80 static ecard_t *slot_to_expcard[MAX_ECARDS];
81 static unsigned int ectcr;
82 #ifdef HAS_EXPMASK
83 static unsigned int have_expmask;
84 #endif
85 
86 /* List of descriptions of cards which don't have an extended
87  * identification, or chunk directories containing a description.
88  */
89 static struct expcard_blacklist __initdata blacklist[] = {
90 	{ MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
91 };
92 
93 asmlinkage extern int
94 ecard_loader_reset(volatile unsigned char *pa, loader_t loader);
95 asmlinkage extern int
96 ecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);
97 extern int setup_arm_irq(int, struct irqaction *);
98 extern void do_ecard_IRQ(int, struct pt_regs *);
99 
100 
101 static void
102 ecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs);
103 
104 static struct irqaction irqexpansioncard = {
105 	ecard_irq_noexpmask, SA_INTERRUPT, 0, "expansion cards", NULL, NULL
106 };
107 
108 static inline unsigned short
ecard_getu16(unsigned char * v)109 ecard_getu16(unsigned char *v)
110 {
111 	return v[0] | v[1] << 8;
112 }
113 
114 static inline signed long
ecard_gets24(unsigned char * v)115 ecard_gets24(unsigned char *v)
116 {
117 	return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
118 }
119 
120 static inline ecard_t *
slot_to_ecard(unsigned int slot)121 slot_to_ecard(unsigned int slot)
122 {
123 	return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
124 }
125 
126 /* ===================== Expansion card daemon ======================== */
127 /*
128  * Since the loader programs on the expansion cards need to be run
129  * in a specific environment, create a separate task with this
130  * environment up, and pass requests to this task as and when we
131  * need to.
132  *
133  * This should allow 99% of loaders to be called from Linux.
134  *
135  * From a security standpoint, we trust the card vendors.  This
136  * may be a misplaced trust.
137  */
138 #define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)
139 #define POD_INT_ADDR(x)	((volatile unsigned char *)\
140 			 ((BUS_ADDR((x)) - IO_BASE) + IO_START))
141 
ecard_task_reset(struct ecard_request * req)142 static inline void ecard_task_reset(struct ecard_request *req)
143 {
144 	struct expansion_card *ec = req->ec;
145 	if (ec->loader)
146 		ecard_loader_reset(POD_INT_ADDR(ec->podaddr), ec->loader);
147 }
148 
149 static void
ecard_task_readbytes(struct ecard_request * req)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 
ecard_do_request(struct ecard_request * req)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:
227 		ecard_task_reset(req);
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  */
ecard_init_pgtables(struct mm_struct * mm)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 
ecard_init_mm(void)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
ecard_task(void * unused)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
ecard_call(struct ecard_request * req)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 static void
ecard_readbytes(void * addr,ecard_t * ec,int off,int len,int useld)375 ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
376 {
377 	struct ecard_request req;
378 
379 	req.req		= req_readbytes;
380 	req.ec		= ec;
381 	req.address	= off;
382 	req.length	= len;
383 	req.use_loader	= useld;
384 	req.buffer	= addr;
385 
386 	ecard_call(&req);
387 }
388 
ecard_readchunk(struct in_chunk_dir * cd,ecard_t * ec,int id,int num)389 int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
390 {
391 	struct ex_chunk_dir excd;
392 	int index = 16;
393 	int useld = 0;
394 
395 	if (!ec->cid.cd)
396 		return 0;
397 
398 	while(1) {
399 		ecard_readbytes(&excd, ec, index, 8, useld);
400 		index += 8;
401 		if (c_id(&excd) == 0) {
402 			if (!useld && ec->loader) {
403 				useld = 1;
404 				index = 0;
405 				continue;
406 			}
407 			return 0;
408 		}
409 		if (c_id(&excd) == 0xf0) { /* link */
410 			index = c_start(&excd);
411 			continue;
412 		}
413 		if (c_id(&excd) == 0x80) { /* loader */
414 			if (!ec->loader) {
415 				ec->loader = (loader_t)kmalloc(c_len(&excd),
416 							       GFP_KERNEL);
417 				if (ec->loader)
418 					ecard_readbytes(ec->loader, ec,
419 							(int)c_start(&excd),
420 							c_len(&excd), useld);
421 				else
422 					return 0;
423 			}
424 			continue;
425 		}
426 		if (c_id(&excd) == id && num-- == 0)
427 			break;
428 	}
429 
430 	if (c_id(&excd) & 0x80) {
431 		switch (c_id(&excd) & 0x70) {
432 		case 0x70:
433 			ecard_readbytes((unsigned char *)excd.d.string, ec,
434 					(int)c_start(&excd), c_len(&excd),
435 					useld);
436 			break;
437 		case 0x00:
438 			break;
439 		}
440 	}
441 	cd->start_offset = c_start(&excd);
442 	memcpy(cd->d.string, excd.d.string, 256);
443 	return 1;
444 }
445 
446 /* ======================= Interrupt control ============================ */
447 
ecard_def_irq_enable(ecard_t * ec,int irqnr)448 static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
449 {
450 #ifdef HAS_EXPMASK
451 	if (irqnr < 4 && have_expmask) {
452 		have_expmask |= 1 << irqnr;
453 		__raw_writeb(have_expmask, EXPMASK_ENABLE);
454 	}
455 #endif
456 }
457 
ecard_def_irq_disable(ecard_t * ec,int irqnr)458 static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
459 {
460 #ifdef HAS_EXPMASK
461 	if (irqnr < 4 && have_expmask) {
462 		have_expmask &= ~(1 << irqnr);
463 		__raw_writeb(have_expmask, EXPMASK_ENABLE);
464 	}
465 #endif
466 }
467 
ecard_def_irq_pending(ecard_t * ec)468 static int ecard_def_irq_pending(ecard_t *ec)
469 {
470 	return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;
471 }
472 
ecard_def_fiq_enable(ecard_t * ec,int fiqnr)473 static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
474 {
475 	panic("ecard_def_fiq_enable called - impossible");
476 }
477 
ecard_def_fiq_disable(ecard_t * ec,int fiqnr)478 static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
479 {
480 	panic("ecard_def_fiq_disable called - impossible");
481 }
482 
ecard_def_fiq_pending(ecard_t * ec)483 static int ecard_def_fiq_pending(ecard_t *ec)
484 {
485 	return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;
486 }
487 
488 static expansioncard_ops_t ecard_default_ops = {
489 	ecard_def_irq_enable,
490 	ecard_def_irq_disable,
491 	ecard_def_irq_pending,
492 	ecard_def_fiq_enable,
493 	ecard_def_fiq_disable,
494 	ecard_def_fiq_pending
495 };
496 
497 /*
498  * Enable and disable interrupts from expansion cards.
499  * (interrupts are disabled for these functions).
500  *
501  * They are not meant to be called directly, but via enable/disable_irq.
502  */
ecard_enableirq(unsigned int irqnr)503 static void ecard_enableirq(unsigned int irqnr)
504 {
505 	ecard_t *ec = slot_to_ecard(irqnr - 32);
506 
507 	if (ec) {
508 		if (!ec->ops)
509 			ec->ops = &ecard_default_ops;
510 
511 		if (ec->claimed && ec->ops->irqenable)
512 			ec->ops->irqenable(ec, irqnr);
513 		else
514 			printk(KERN_ERR "ecard: rejecting request to "
515 				"enable IRQs for %d\n", irqnr);
516 	}
517 }
518 
ecard_disableirq(unsigned int irqnr)519 static void ecard_disableirq(unsigned int irqnr)
520 {
521 	ecard_t *ec = slot_to_ecard(irqnr - 32);
522 
523 	if (ec) {
524 		if (!ec->ops)
525 			ec->ops = &ecard_default_ops;
526 
527 		if (ec->ops && ec->ops->irqdisable)
528 			ec->ops->irqdisable(ec, irqnr);
529 	}
530 }
531 
ecard_enablefiq(unsigned int fiqnr)532 void ecard_enablefiq(unsigned int fiqnr)
533 {
534 	ecard_t *ec = slot_to_ecard(fiqnr);
535 
536 	if (ec) {
537 		if (!ec->ops)
538 			ec->ops = &ecard_default_ops;
539 
540 		if (ec->claimed && ec->ops->fiqenable)
541 			ec->ops->fiqenable(ec, fiqnr);
542 		else
543 			printk(KERN_ERR "ecard: rejecting request to "
544 				"enable FIQs for %d\n", fiqnr);
545 	}
546 }
547 
ecard_disablefiq(unsigned int fiqnr)548 void ecard_disablefiq(unsigned int fiqnr)
549 {
550 	ecard_t *ec = slot_to_ecard(fiqnr);
551 
552 	if (ec) {
553 		if (!ec->ops)
554 			ec->ops = &ecard_default_ops;
555 
556 		if (ec->ops->fiqdisable)
557 			ec->ops->fiqdisable(ec, fiqnr);
558 	}
559 }
560 
561 static void
ecard_dump_irq_state(ecard_t * ec)562 ecard_dump_irq_state(ecard_t *ec)
563 {
564 	printk("  %d: %sclaimed, ",
565 	       ec->slot_no,
566 	       ec->claimed ? "" : "not ");
567 
568 	if (ec->ops && ec->ops->irqpending &&
569 	    ec->ops != &ecard_default_ops)
570 		printk("irq %spending\n",
571 		       ec->ops->irqpending(ec) ? "" : "not ");
572 	else
573 		printk("irqaddr %p, mask = %02X, status = %02X\n",
574 		       ec->irqaddr, ec->irqmask, *ec->irqaddr);
575 }
576 
ecard_check_lockup(void)577 static void ecard_check_lockup(void)
578 {
579 	static int last, lockup;
580 	ecard_t *ec;
581 
582 	/*
583 	 * If the timer interrupt has not run since the last million
584 	 * unrecognised expansion card interrupts, then there is
585 	 * something seriously wrong.  Disable the expansion card
586 	 * interrupts so at least we can continue.
587 	 *
588 	 * Maybe we ought to start a timer to re-enable them some time
589 	 * later?
590 	 */
591 	if (last == jiffies) {
592 		lockup += 1;
593 		if (lockup > 1000000) {
594 			printk(KERN_ERR "\nInterrupt lockup detected - "
595 			       "disabling all expansion card interrupts\n");
596 
597 			disable_irq(IRQ_EXPANSIONCARD);
598 
599 			printk("Expansion card IRQ state:\n");
600 
601 			for (ec = cards; ec; ec = ec->next)
602 				ecard_dump_irq_state(ec);
603 		}
604 	} else
605 		lockup = 0;
606 
607 	/*
608 	 * If we did not recognise the source of this interrupt,
609 	 * warn the user, but don't flood the user with these messages.
610 	 */
611 	if (!last || time_after(jiffies, last + 5*HZ)) {
612 		last = jiffies;
613 		printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
614 	}
615 }
616 
617 static void
ecard_irq_noexpmask(int intr_no,void * dev_id,struct pt_regs * regs)618 ecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs)
619 {
620 	ecard_t *ec;
621 	int called = 0;
622 
623 	for (ec = cards; ec; ec = ec->next) {
624 		int pending;
625 
626 		if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
627 			continue;
628 
629 		if (ec->ops && ec->ops->irqpending)
630 			pending = ec->ops->irqpending(ec);
631 		else
632 			pending = ecard_default_ops.irqpending(ec);
633 
634 		if (pending) {
635 			do_ecard_IRQ(ec->irq, regs);
636 			called ++;
637 		}
638 	}
639 	cli();
640 
641 	if (called == 0)
642 		ecard_check_lockup();
643 }
644 
645 #ifdef HAS_EXPMASK
646 static unsigned char priority_masks[] =
647 {
648 	0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
649 };
650 
651 static unsigned char first_set[] =
652 {
653 	0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
654 	0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
655 };
656 
657 static void
ecard_irq_expmask(int intr_no,void * dev_id,struct pt_regs * regs)658 ecard_irq_expmask(int intr_no, void *dev_id, struct pt_regs *regs)
659 {
660 	const unsigned int statusmask = 15;
661 	unsigned int status;
662 
663 	status = __raw_readb(EXPMASK_STATUS) & statusmask;
664 	if (status) {
665 		unsigned int slot;
666 		ecard_t *ec;
667 again:
668 		slot = first_set[status];
669 		ec = slot_to_ecard(slot);
670 		if (ec->claimed) {
671 			unsigned int oldexpmask;
672 			/*
673 			 * this ugly code is so that we can operate a
674 			 * prioritorising system:
675 			 *
676 			 * Card 0 	highest priority
677 			 * Card 1
678 			 * Card 2
679 			 * Card 3	lowest priority
680 			 *
681 			 * Serial cards should go in 0/1, ethernet/scsi in 2/3
682 			 * otherwise you will lose serial data at high speeds!
683 			 */
684 			oldexpmask = have_expmask;
685 			have_expmask &= priority_masks[slot];
686 			__raw_writeb(have_expmask, EXPMASK_ENABLE);
687 			sti();
688 			do_ecard_IRQ(ec->irq, regs);
689 			cli();
690 			have_expmask = oldexpmask;
691 			__raw_writeb(have_expmask, EXPMASK_ENABLE);
692 			status = __raw_readb(EXPMASK_STATUS) & statusmask;
693 			if (status)
694 				goto again;
695 		} else {
696 			printk(KERN_WARNING "card%d: interrupt from unclaimed "
697 			       "card???\n", slot);
698 			have_expmask &= ~(1 << slot);
699 			__raw_writeb(have_expmask, EXPMASK_ENABLE);
700 		}
701 	} else
702 		printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
703 }
704 
ecard_probeirqhw(void)705 static void __init ecard_probeirqhw(void)
706 {
707 	ecard_t *ec;
708 	int found;
709 
710 	__raw_writeb(0x00, EXPMASK_ENABLE);
711 	__raw_writeb(0xff, EXPMASK_STATUS);
712 	found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
713 	__raw_writeb(0xff, EXPMASK_ENABLE);
714 
715 	if (!found)
716 		return;
717 
718 	printk(KERN_DEBUG "Expansion card interrupt "
719 	       "management hardware found\n");
720 
721 	irqexpansioncard.handler = ecard_irq_expmask;
722 
723 	/* for each card present, set a bit to '1' */
724 	have_expmask = 0x80000000;
725 
726 	for (ec = cards; ec; ec = ec->next)
727 		have_expmask |= 1 << ec->slot_no;
728 
729 	__raw_writeb(have_expmask, EXPMASK_ENABLE);
730 }
731 #else
732 #define ecard_probeirqhw() do { } while (0)
733 #endif
734 
735 #ifndef IO_EC_MEMC8_BASE
736 #define IO_EC_MEMC8_BASE 0
737 #endif
738 
ecard_address(ecard_t * ec,card_type_t type,card_speed_t speed)739 unsigned int ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
740 {
741 	unsigned long address = 0;
742 	int slot = ec->slot_no;
743 
744 	if (ec->slot_no == 8)
745 		return IO_EC_MEMC8_BASE;
746 
747 	ectcr &= ~(1 << slot);
748 
749 	switch (type) {
750 	case ECARD_MEMC:
751 		if (slot < 4)
752 			address = IO_EC_MEMC_BASE + (slot << 12);
753 		break;
754 
755 	case ECARD_IOC:
756 		if (slot < 4)
757 			address = IO_EC_IOC_BASE + (slot << 12);
758 #ifdef IO_EC_IOC4_BASE
759 		else
760 			address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
761 #endif
762 		if (address)
763 			address +=  speed << 17;
764 		break;
765 
766 #ifdef IO_EC_EASI_BASE
767 	case ECARD_EASI:
768 		address = IO_EC_EASI_BASE + (slot << 22);
769 		if (speed == ECARD_FAST)
770 			ectcr |= 1 << slot;
771 		break;
772 #endif
773 	default:
774 		break;
775 	}
776 
777 #ifdef IOMD_ECTCR
778 	iomd_writeb(ectcr, IOMD_ECTCR);
779 #endif
780 	return address;
781 }
782 
ecard_prints(char * buffer,ecard_t * ec)783 static int ecard_prints(char *buffer, ecard_t *ec)
784 {
785 	char *start = buffer;
786 
787 	buffer += sprintf(buffer, "  %d: %s ", ec->slot_no,
788 			  ec->type == ECARD_EASI ? "EASI" : "    ");
789 
790 	if (ec->cid.id == 0) {
791 		struct in_chunk_dir incd;
792 
793 		buffer += sprintf(buffer, "[%04X:%04X] ",
794 			ec->cid.manufacturer, ec->cid.product);
795 
796 		if (!ec->card_desc && ec->cid.cd &&
797 		    ecard_readchunk(&incd, ec, 0xf5, 0)) {
798 			ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
799 
800 			if (ec->card_desc)
801 				strcpy((char *)ec->card_desc, incd.d.string);
802 		}
803 
804 		buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
805 	} else
806 		buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
807 
808 	return buffer - start;
809 }
810 
get_ecard_dev_info(char * buf,char ** start,off_t pos,int count)811 static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
812 {
813 	ecard_t *ec = cards;
814 	off_t at = 0;
815 	int len, cnt;
816 
817 	cnt = 0;
818 	while (ec && count > cnt) {
819 		len = ecard_prints(buf, ec);
820 		at += len;
821 		if (at >= pos) {
822 			if (!*start) {
823 				*start = buf + (pos - (at - len));
824 				cnt = at - pos;
825 			} else
826 				cnt += len;
827 			buf += len;
828 		}
829 		ec = ec->next;
830 	}
831 	return (count > cnt) ? cnt : count;
832 }
833 
834 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
835 
ecard_proc_init(void)836 static void ecard_proc_init(void)
837 {
838 	proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
839 	create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
840 		get_ecard_dev_info);
841 }
842 
843 #define ec_set_resource(ec,nr,st,sz,flg)			\
844 	do {							\
845 		(ec)->resource[nr].name = ec->name;	 	\
846 		(ec)->resource[nr].start = st;			\
847 		(ec)->resource[nr].end = (st) + (sz) - 1;	\
848 		(ec)->resource[nr].flags = flg;			\
849 	} while (0)
850 
ecard_init_resources(struct expansion_card * ec)851 static void __init ecard_init_resources(struct expansion_card *ec)
852 {
853 	unsigned long base = PODSLOT_IOC4_BASE;
854 	unsigned int slot = ec->slot_no;
855 	int i;
856 
857 	if (slot < 4) {
858 		ec_set_resource(ec, ECARD_RES_MEMC,
859 				PODSLOT_MEMC_BASE + (slot << 14),
860 				PODSLOT_MEMC_SIZE, IORESOURCE_MEM);
861 		base = PODSLOT_IOC0_BASE;
862 	}
863 
864 #ifdef CONFIG_ARCH_RPC
865 	if (slot < 8) {
866 		ec_set_resource(ec, ECARD_RES_EASI,
867 				PODSLOT_EASI_BASE + (slot << 24),
868 				PODSLOT_EASI_SIZE, IORESOURCE_MEM);
869 	}
870 
871 	if (slot == 8) {
872 		ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE,
873 				NETSLOT_SIZE, IORESOURCE_MEM);
874 	} else
875 #endif
876 
877 	for (i = 0; i < ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++) {
878 		ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
879 				base + (slot << 14) + (i << 19),
880 				PODSLOT_IOC_SIZE, IORESOURCE_MEM);
881 	}
882 
883 	for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
884 		if (ec->resource[i].start &&
885 		    request_resource(&iomem_resource, &ec->resource[i])) {
886 			printk(KERN_ERR "ecard%d: resource(s) not available\n",
887 				ec->slot_no);
888 			ec->resource[i].end -= ec->resource[i].start;
889 			ec->resource[i].start = 0;
890 		}
891 	}
892 }
893 
894 /*
895  * Probe for an expansion card.
896  *
897  * If bit 1 of the first byte of the card is set, then the
898  * card does not exist.
899  */
900 static int __init
ecard_probe(int slot,card_type_t type)901 ecard_probe(int slot, card_type_t type)
902 {
903 	ecard_t **ecp;
904 	ecard_t *ec;
905 	struct ex_ecid cid;
906 	int i, rc = -ENOMEM;
907 
908 	ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
909 	if (!ec)
910 		goto nomem;
911 
912 	memset(ec, 0, sizeof(ecard_t));
913 
914 	ec->slot_no	= slot;
915 	ec->type	= type;
916 	ec->irq		= NO_IRQ;
917 	ec->fiq		= NO_IRQ;
918 	ec->dma		= NO_DMA;
919 	ec->card_desc	= NULL;
920 	ec->ops		= &ecard_default_ops;
921 
922 	rc = -ENODEV;
923 	if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
924 		goto nodev;
925 
926 	cid.r_zero = 1;
927 	ecard_readbytes(&cid, ec, 0, 16, 0);
928 	if (cid.r_zero)
929 		goto nodev;
930 
931 	ec->cid.id	= cid.r_id;
932 	ec->cid.cd	= cid.r_cd;
933 	ec->cid.is	= cid.r_is;
934 	ec->cid.w	= cid.r_w;
935 	ec->cid.manufacturer = ecard_getu16(cid.r_manu);
936 	ec->cid.product = ecard_getu16(cid.r_prod);
937 	ec->cid.country = cid.r_country;
938 	ec->cid.irqmask = cid.r_irqmask;
939 	ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
940 	ec->cid.fiqmask = cid.r_fiqmask;
941 	ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
942 	ec->fiqaddr	=
943 	ec->irqaddr	= (unsigned char *)ioaddr(ec->podaddr);
944 
945 	if (ec->cid.is) {
946 		ec->irqmask = ec->cid.irqmask;
947 		ec->irqaddr += ec->cid.irqoff;
948 		ec->fiqmask = ec->cid.fiqmask;
949 		ec->fiqaddr += ec->cid.fiqoff;
950 	} else {
951 		ec->irqmask = 1;
952 		ec->fiqmask = 4;
953 	}
954 
955 	for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
956 		if (blacklist[i].manufacturer == ec->cid.manufacturer &&
957 		    blacklist[i].product == ec->cid.product) {
958 			ec->card_desc = blacklist[i].type;
959 			break;
960 		}
961 
962 	snprintf(ec->name, sizeof(ec->name), "ecard %04x:%04x",
963 		 ec->cid.manufacturer, ec->cid.product);
964 
965 	ecard_init_resources(ec);
966 
967 	/*
968 	 * hook the interrupt handlers
969 	 */
970 	if (slot < 8) {
971 		ec->irq = 32 + slot;
972 		irq_desc[ec->irq].mask_ack = ecard_disableirq;
973 		irq_desc[ec->irq].mask     = ecard_disableirq;
974 		irq_desc[ec->irq].unmask   = ecard_enableirq;
975 		irq_desc[ec->irq].valid    = 1;
976 	}
977 
978 #ifdef IO_EC_MEMC8_BASE
979 	if (slot == 8)
980 		ec->irq = 11;
981 #endif
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 
ecard_startfind(void)1002 void ecard_startfind(void)
1003 {
1004 	finding_pos = NULL;
1005 }
1006 
ecard_find(int cid,const card_ids * cids)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 || finding_pos->driver)
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 
ecard_free_all(void)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  */
ecard_init(void)1061 void __init ecard_init(void)
1062 {
1063 	int slot;
1064 
1065 #ifdef CONFIG_CPU_32
1066 	init_waitqueue_head(&ecard_wait);
1067 #endif
1068 
1069 	printk("Probing expansion cards\n");
1070 
1071 	for (slot = 0; slot < 8; slot ++) {
1072 		if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1073 			ecard_probe(slot, ECARD_IOC);
1074 	}
1075 
1076 #ifdef IO_EC_MEMC8_BASE
1077 	ecard_probe(8, ECARD_IOC);
1078 #endif
1079 
1080 	ecard_probeirqhw();
1081 
1082 	if (setup_arm_irq(IRQ_EXPANSIONCARD, &irqexpansioncard)) {
1083 		printk(KERN_ERR "Unable to claim IRQ%d for expansion cards\n",
1084 		       IRQ_EXPANSIONCARD);
1085 		ecard_free_all();
1086 	}
1087 
1088 	ecard_proc_init();
1089 }
1090 
1091 /*
1092  *	ECARD driver functions
1093  */
1094 static const struct ecard_id *
ecard_match_device(const struct ecard_id * ids,struct expansion_card * ec)1095 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1096 {
1097 	int i;
1098 
1099 	for (i = 0; ids[i].manufacturer != 65535; i++)
1100 		if (ec->cid.manufacturer == ids[i].manufacturer &&
1101 		    ec->cid.product == ids[i].product)
1102 			return ids + i;
1103 
1104 	return NULL;
1105 }
1106 
ecard_drv_probe(struct expansion_card * ec,const struct ecard_id * id)1107 static int ecard_drv_probe(struct expansion_card *ec, const struct ecard_id *id)
1108 {
1109 	struct ecard_driver *drv = ec->driver;
1110 	int ret;
1111 
1112 	ecard_claim(ec);
1113 	ret = drv->probe(ec, id);
1114 	if (ret)
1115 		ecard_release(ec);
1116 	return ret;
1117 }
1118 
ecard_drv_remove(struct expansion_card * ec)1119 static int ecard_drv_remove(struct expansion_card *ec)
1120 {
1121 	struct ecard_driver *drv = ec->driver;
1122 
1123 	drv->remove(ec);
1124 	ecard_release(ec);
1125 
1126 	return 0;
1127 }
1128 
1129 /*
1130  * Before rebooting, we must make sure that the expansion card is in a
1131  * sensible state, so it can be re-detected.  This means that the first
1132  * page of the ROM must be visible.  We call the expansion cards reset
1133  * handler, if any.
1134  */
ecard_drv_shutdown(struct expansion_card * ec)1135 static void ecard_drv_shutdown(struct expansion_card *ec)
1136 {
1137 	struct ecard_driver *drv = ec->driver;
1138 	struct ecard_request req;
1139 
1140 	if (drv && drv->shutdown)
1141 		drv->shutdown(ec);
1142 	ecard_release(ec);
1143 	req.req = req_reset;
1144 	req.ec = ec;
1145 	ecard_call(&req);
1146 }
1147 
ecard_register_driver(struct ecard_driver * drv)1148 int ecard_register_driver(struct ecard_driver *drv)
1149 {
1150 	struct expansion_card *ec;
1151 	int ret, found = 0;
1152 
1153 	for (ec = cards; ec; ec = ec->next) {
1154 		const struct ecard_id *id;
1155 
1156 		if (ec->driver || ec->claimed)
1157 			continue;
1158 
1159 		if (drv->id_table) {
1160 			id = ecard_match_device(drv->id_table, ec);
1161 			ret = id != NULL;
1162 		} else {
1163 			id = NULL;
1164 			ret = ec->cid.id == drv->id;
1165 		}
1166 
1167 		if (ret) {
1168 			ec->driver = drv;
1169 			ret = ecard_drv_probe(ec, id);
1170 			if (ret) {
1171 				ec->driver = NULL;
1172 			} else {
1173 				found++;
1174 			}
1175 		}
1176 	}
1177 
1178 	return found ? 0 : -ENODEV;
1179 }
1180 
ecard_remove_driver(struct ecard_driver * drv)1181 void ecard_remove_driver(struct ecard_driver *drv)
1182 {
1183 	struct expansion_card *ec;
1184 
1185 	for (ec = cards; ec; ec = ec->next)
1186 		if (ec->driver == drv) {
1187 			ecard_drv_remove(ec);
1188 			ec->driver = NULL;
1189 		}
1190 }
1191 
1192 /*
1193  * This function is responsible for resetting the expansion cards to a
1194  * sensible state immediately prior to rebooting the system.  This function
1195  * has process state (keventd), so we can sleep.
1196  *
1197  * Possible "val" values here:
1198  *  SYS_RESTART   -  restarting system
1199  *  SYS_HALT      - halting system
1200  *  SYS_POWER_OFF - powering down system
1201  *
1202  * We ignore all calls, unless it is a SYS_RESTART call - power down/halts
1203  * will be followed by a SYS_RESTART if ctrl-alt-del is pressed again.
1204  */
ecard_reboot(struct notifier_block * me,unsigned long val,void * v)1205 static int ecard_reboot(struct notifier_block *me, unsigned long val, void *v)
1206 {
1207 	struct expansion_card *ec;
1208 
1209 	if (val != SYS_RESTART)
1210 		return 0;
1211 
1212 	for (ec = cards; ec; ec = ec->next)
1213 		if (ec->driver || ec->claimed)
1214 			ecard_drv_shutdown(ec);
1215 
1216 	/*
1217 	 * Disable the expansion card interrupt
1218 	 */
1219 	disable_irq(IRQ_EXPANSIONCARD);
1220 
1221 	/*
1222 	 * Finally, reset the expansion card interrupt mask to
1223 	 * all enable (RISC OS doesn't set this)
1224 	 */
1225 #ifdef HAS_EXPMASK
1226 	have_expmask = ~0;
1227 	__raw_writeb(have_expmask, EXPMASK_ENABLE);
1228 #endif
1229 	return 0;
1230 }
1231 
1232 static struct notifier_block ecard_reboot_notifier = {
1233 	.notifier_call	= ecard_reboot,
1234 };
1235 
ecard_bus_init(void)1236 static int ecard_bus_init(void)
1237 {
1238 	register_reboot_notifier(&ecard_reboot_notifier);
1239 	return 0;
1240 }
1241 
1242 __initcall(ecard_bus_init);
1243 
1244 EXPORT_SYMBOL(ecard_startfind);
1245 EXPORT_SYMBOL(ecard_find);
1246 EXPORT_SYMBOL(ecard_readchunk);
1247 EXPORT_SYMBOL(ecard_address);
1248 EXPORT_SYMBOL(ecard_register_driver);
1249 EXPORT_SYMBOL(ecard_remove_driver);
1250