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