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