[ARM] ecard: silence new warning caused by previous commit
[linux-2.6-block.git] / arch / arm / kernel / ecard.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
33#include <linux/sched.h>
34#include <linux/interrupt.h>
35#include <linux/completion.h>
36#include <linux/reboot.h>
37#include <linux/mm.h>
38#include <linux/slab.h>
39#include <linux/proc_fs.h>
40#include <linux/device.h>
41#include <linux/init.h>
00431707 42#include <linux/mutex.h>
134c99e9 43#include <linux/kthread.h>
1da177e4
LT
44
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;
00431707 210static DEFINE_MUTEX(ecard_mutex);
1da177e4
LT
211
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{
1da177e4
LT
267 /*
268 * Allocate a mm. We're not a lazy-TLB kernel task since we need
269 * to set page table entries where the user space would be. Note
270 * that this also creates the page tables. Failure is not an
271 * option here.
272 */
273 if (ecard_init_mm())
274 panic("kecardd: unable to alloc mm\n");
275
276 while (1) {
277 struct ecard_request *req;
278
279 wait_event_interruptible(ecard_wait, ecard_req != NULL);
280
281 req = xchg(&ecard_req, NULL);
282 if (req != NULL) {
283 req->fn(req);
284 complete(req->complete);
285 }
286 }
287}
288
289/*
290 * Wake the expansion card daemon to action our request.
291 *
292 * FIXME: The test here is not sufficient to detect if the
293 * kcardd is running.
294 */
295static void ecard_call(struct ecard_request *req)
296{
6e9a4738 297 DECLARE_COMPLETION_ONSTACK(completion);
1da177e4
LT
298
299 req->complete = &completion;
300
00431707 301 mutex_lock(&ecard_mutex);
1da177e4
LT
302 ecard_req = req;
303 wake_up(&ecard_wait);
304
305 /*
306 * Now wait for kecardd to run.
307 */
308 wait_for_completion(&completion);
00431707 309 mutex_unlock(&ecard_mutex);
1da177e4
LT
310}
311
312/* ======================= Mid-level card control ===================== */
313
314static void
315ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
316{
317 struct ecard_request req;
318
319 req.fn = ecard_task_readbytes;
320 req.ec = ec;
321 req.address = off;
322 req.length = len;
323 req.use_loader = useld;
324 req.buffer = addr;
325
326 ecard_call(&req);
327}
328
329int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
330{
331 struct ex_chunk_dir excd;
332 int index = 16;
333 int useld = 0;
334
335 if (!ec->cid.cd)
336 return 0;
337
338 while(1) {
339 ecard_readbytes(&excd, ec, index, 8, useld);
340 index += 8;
341 if (c_id(&excd) == 0) {
342 if (!useld && ec->loader) {
343 useld = 1;
344 index = 0;
345 continue;
346 }
347 return 0;
348 }
349 if (c_id(&excd) == 0xf0) { /* link */
350 index = c_start(&excd);
351 continue;
352 }
353 if (c_id(&excd) == 0x80) { /* loader */
354 if (!ec->loader) {
5cbded58 355 ec->loader = kmalloc(c_len(&excd),
1da177e4
LT
356 GFP_KERNEL);
357 if (ec->loader)
358 ecard_readbytes(ec->loader, ec,
359 (int)c_start(&excd),
360 c_len(&excd), useld);
361 else
362 return 0;
363 }
364 continue;
365 }
366 if (c_id(&excd) == id && num-- == 0)
367 break;
368 }
369
370 if (c_id(&excd) & 0x80) {
371 switch (c_id(&excd) & 0x70) {
372 case 0x70:
373 ecard_readbytes((unsigned char *)excd.d.string, ec,
374 (int)c_start(&excd), c_len(&excd),
375 useld);
376 break;
377 case 0x00:
378 break;
379 }
380 }
381 cd->start_offset = c_start(&excd);
382 memcpy(cd->d.string, excd.d.string, 256);
383 return 1;
384}
385
386/* ======================= Interrupt control ============================ */
387
388static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
389{
390#ifdef HAS_EXPMASK
391 if (irqnr < 4 && have_expmask) {
392 have_expmask |= 1 << irqnr;
393 __raw_writeb(have_expmask, EXPMASK_ENABLE);
394 }
395#endif
396}
397
398static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
399{
400#ifdef HAS_EXPMASK
401 if (irqnr < 4 && have_expmask) {
402 have_expmask &= ~(1 << irqnr);
403 __raw_writeb(have_expmask, EXPMASK_ENABLE);
404 }
405#endif
406}
407
408static int ecard_def_irq_pending(ecard_t *ec)
409{
410 return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
411}
412
413static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
414{
415 panic("ecard_def_fiq_enable called - impossible");
416}
417
418static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
419{
420 panic("ecard_def_fiq_disable called - impossible");
421}
422
423static int ecard_def_fiq_pending(ecard_t *ec)
424{
425 return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
426}
427
428static expansioncard_ops_t ecard_default_ops = {
429 ecard_def_irq_enable,
430 ecard_def_irq_disable,
431 ecard_def_irq_pending,
432 ecard_def_fiq_enable,
433 ecard_def_fiq_disable,
434 ecard_def_fiq_pending
435};
436
437/*
438 * Enable and disable interrupts from expansion cards.
439 * (interrupts are disabled for these functions).
440 *
441 * They are not meant to be called directly, but via enable/disable_irq.
442 */
443static void ecard_irq_unmask(unsigned int irqnr)
444{
445 ecard_t *ec = slot_to_ecard(irqnr - 32);
446
447 if (ec) {
448 if (!ec->ops)
449 ec->ops = &ecard_default_ops;
450
451 if (ec->claimed && ec->ops->irqenable)
452 ec->ops->irqenable(ec, irqnr);
453 else
454 printk(KERN_ERR "ecard: rejecting request to "
455 "enable IRQs for %d\n", irqnr);
456 }
457}
458
459static void ecard_irq_mask(unsigned int irqnr)
460{
461 ecard_t *ec = slot_to_ecard(irqnr - 32);
462
463 if (ec) {
464 if (!ec->ops)
465 ec->ops = &ecard_default_ops;
466
467 if (ec->ops && ec->ops->irqdisable)
468 ec->ops->irqdisable(ec, irqnr);
469 }
470}
471
38c677cb
DB
472static struct irq_chip ecard_chip = {
473 .name = "ECARD",
1da177e4
LT
474 .ack = ecard_irq_mask,
475 .mask = ecard_irq_mask,
476 .unmask = ecard_irq_unmask,
477};
478
479void ecard_enablefiq(unsigned int fiqnr)
480{
481 ecard_t *ec = slot_to_ecard(fiqnr);
482
483 if (ec) {
484 if (!ec->ops)
485 ec->ops = &ecard_default_ops;
486
487 if (ec->claimed && ec->ops->fiqenable)
488 ec->ops->fiqenable(ec, fiqnr);
489 else
490 printk(KERN_ERR "ecard: rejecting request to "
491 "enable FIQs for %d\n", fiqnr);
492 }
493}
494
495void ecard_disablefiq(unsigned int fiqnr)
496{
497 ecard_t *ec = slot_to_ecard(fiqnr);
498
499 if (ec) {
500 if (!ec->ops)
501 ec->ops = &ecard_default_ops;
502
503 if (ec->ops->fiqdisable)
504 ec->ops->fiqdisable(ec, fiqnr);
505 }
506}
507
508static void ecard_dump_irq_state(void)
509{
510 ecard_t *ec;
511
512 printk("Expansion card IRQ state:\n");
513
514 for (ec = cards; ec; ec = ec->next) {
515 if (ec->slot_no == 8)
516 continue;
517
518 printk(" %d: %sclaimed, ",
519 ec->slot_no, ec->claimed ? "" : "not ");
520
521 if (ec->ops && ec->ops->irqpending &&
522 ec->ops != &ecard_default_ops)
523 printk("irq %spending\n",
524 ec->ops->irqpending(ec) ? "" : "not ");
525 else
526 printk("irqaddr %p, mask = %02X, status = %02X\n",
527 ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
528 }
529}
530
10dd5ce2 531static void ecard_check_lockup(struct irq_desc *desc)
1da177e4
LT
532{
533 static unsigned long last;
534 static int lockup;
535
536 /*
537 * If the timer interrupt has not run since the last million
538 * unrecognised expansion card interrupts, then there is
539 * something seriously wrong. Disable the expansion card
540 * interrupts so at least we can continue.
541 *
542 * Maybe we ought to start a timer to re-enable them some time
543 * later?
544 */
545 if (last == jiffies) {
546 lockup += 1;
547 if (lockup > 1000000) {
548 printk(KERN_ERR "\nInterrupt lockup detected - "
549 "disabling all expansion card interrupts\n");
550
551 desc->chip->mask(IRQ_EXPANSIONCARD);
552 ecard_dump_irq_state();
553 }
554 } else
555 lockup = 0;
556
557 /*
558 * If we did not recognise the source of this interrupt,
559 * warn the user, but don't flood the user with these messages.
560 */
561 if (!last || time_after(jiffies, last + 5*HZ)) {
562 last = jiffies;
563 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
564 ecard_dump_irq_state();
565 }
566}
567
568static void
10dd5ce2 569ecard_irq_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
570{
571 ecard_t *ec;
572 int called = 0;
573
574 desc->chip->mask(irq);
575 for (ec = cards; ec; ec = ec->next) {
576 int pending;
577
578 if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
579 continue;
580
581 if (ec->ops && ec->ops->irqpending)
582 pending = ec->ops->irqpending(ec);
583 else
584 pending = ecard_default_ops.irqpending(ec);
585
586 if (pending) {
10dd5ce2 587 struct irq_desc *d = irq_desc + ec->irq;
0cd61b68 588 desc_handle_irq(ec->irq, d);
1da177e4
LT
589 called ++;
590 }
591 }
592 desc->chip->unmask(irq);
593
594 if (called == 0)
595 ecard_check_lockup(desc);
596}
597
598#ifdef HAS_EXPMASK
599static unsigned char priority_masks[] =
600{
601 0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
602};
603
604static unsigned char first_set[] =
605{
606 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
607 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
608};
609
610static void
10dd5ce2 611ecard_irqexp_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
612{
613 const unsigned int statusmask = 15;
614 unsigned int status;
615
616 status = __raw_readb(EXPMASK_STATUS) & statusmask;
617 if (status) {
618 unsigned int slot = first_set[status];
619 ecard_t *ec = slot_to_ecard(slot);
620
621 if (ec->claimed) {
33e39f1d 622 struct irq_desc *d = irq_desc + ec->irq;
1da177e4
LT
623 /*
624 * this ugly code is so that we can operate a
625 * prioritorising system:
626 *
627 * Card 0 highest priority
628 * Card 1
629 * Card 2
630 * Card 3 lowest priority
631 *
632 * Serial cards should go in 0/1, ethernet/scsi in 2/3
633 * otherwise you will lose serial data at high speeds!
634 */
0cd61b68 635 desc_handle_irq(ec->irq, d);
1da177e4
LT
636 } else {
637 printk(KERN_WARNING "card%d: interrupt from unclaimed "
638 "card???\n", slot);
639 have_expmask &= ~(1 << slot);
640 __raw_writeb(have_expmask, EXPMASK_ENABLE);
641 }
642 } else
643 printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
644}
645
646static int __init ecard_probeirqhw(void)
647{
648 ecard_t *ec;
649 int found;
650
651 __raw_writeb(0x00, EXPMASK_ENABLE);
652 __raw_writeb(0xff, EXPMASK_STATUS);
653 found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
654 __raw_writeb(0xff, EXPMASK_ENABLE);
655
656 if (found) {
657 printk(KERN_DEBUG "Expansion card interrupt "
658 "management hardware found\n");
659
660 /* for each card present, set a bit to '1' */
661 have_expmask = 0x80000000;
662
663 for (ec = cards; ec; ec = ec->next)
664 have_expmask |= 1 << ec->slot_no;
665
666 __raw_writeb(have_expmask, EXPMASK_ENABLE);
667 }
668
669 return found;
670}
671#else
672#define ecard_irqexp_handler NULL
673#define ecard_probeirqhw() (0)
674#endif
675
676#ifndef IO_EC_MEMC8_BASE
677#define IO_EC_MEMC8_BASE 0
678#endif
679
680unsigned int __ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
681{
682 unsigned long address = 0;
683 int slot = ec->slot_no;
684
685 if (ec->slot_no == 8)
686 return IO_EC_MEMC8_BASE;
687
688 ectcr &= ~(1 << slot);
689
690 switch (type) {
691 case ECARD_MEMC:
692 if (slot < 4)
693 address = IO_EC_MEMC_BASE + (slot << 12);
694 break;
695
696 case ECARD_IOC:
697 if (slot < 4)
698 address = IO_EC_IOC_BASE + (slot << 12);
699#ifdef IO_EC_IOC4_BASE
700 else
701 address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
702#endif
703 if (address)
704 address += speed << 17;
705 break;
706
707#ifdef IO_EC_EASI_BASE
708 case ECARD_EASI:
709 address = IO_EC_EASI_BASE + (slot << 22);
710 if (speed == ECARD_FAST)
711 ectcr |= 1 << slot;
712 break;
713#endif
714 default:
715 break;
716 }
717
718#ifdef IOMD_ECTCR
719 iomd_writeb(ectcr, IOMD_ECTCR);
720#endif
721 return address;
722}
723
724static int ecard_prints(char *buffer, ecard_t *ec)
725{
726 char *start = buffer;
727
728 buffer += sprintf(buffer, " %d: %s ", ec->slot_no,
729 ec->type == ECARD_EASI ? "EASI" : " ");
730
731 if (ec->cid.id == 0) {
732 struct in_chunk_dir incd;
733
734 buffer += sprintf(buffer, "[%04X:%04X] ",
735 ec->cid.manufacturer, ec->cid.product);
736
737 if (!ec->card_desc && ec->cid.cd &&
738 ecard_readchunk(&incd, ec, 0xf5, 0)) {
739 ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
740
741 if (ec->card_desc)
742 strcpy((char *)ec->card_desc, incd.d.string);
743 }
744
745 buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
746 } else
747 buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
748
749 return buffer - start;
750}
751
752static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
753{
754 ecard_t *ec = cards;
755 off_t at = 0;
756 int len, cnt;
757
758 cnt = 0;
759 while (ec && count > cnt) {
760 len = ecard_prints(buf, ec);
761 at += len;
762 if (at >= pos) {
763 if (!*start) {
764 *start = buf + (pos - (at - len));
765 cnt = at - pos;
766 } else
767 cnt += len;
768 buf += len;
769 }
770 ec = ec->next;
771 }
772 return (count > cnt) ? cnt : count;
773}
774
775static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
776
777static void ecard_proc_init(void)
778{
779 proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
780 create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
781 get_ecard_dev_info);
782}
783
784#define ec_set_resource(ec,nr,st,sz) \
785 do { \
786 (ec)->resource[nr].name = ec->dev.bus_id; \
787 (ec)->resource[nr].start = st; \
788 (ec)->resource[nr].end = (st) + (sz) - 1; \
789 (ec)->resource[nr].flags = IORESOURCE_MEM; \
790 } while (0)
791
792static void __init ecard_free_card(struct expansion_card *ec)
793{
794 int i;
795
796 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
797 if (ec->resource[i].flags)
798 release_resource(&ec->resource[i]);
799
800 kfree(ec);
801}
802
803static struct expansion_card *__init ecard_alloc_card(int type, int slot)
804{
805 struct expansion_card *ec;
806 unsigned long base;
807 int i;
808
d2a02b93 809 ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
1da177e4
LT
810 if (!ec) {
811 ec = ERR_PTR(-ENOMEM);
812 goto nomem;
813 }
814
1da177e4
LT
815 ec->slot_no = slot;
816 ec->type = type;
817 ec->irq = NO_IRQ;
818 ec->fiq = NO_IRQ;
819 ec->dma = NO_DMA;
820 ec->ops = &ecard_default_ops;
821
822 snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
823 ec->dev.parent = NULL;
824 ec->dev.bus = &ecard_bus_type;
825 ec->dev.dma_mask = &ec->dma_mask;
826 ec->dma_mask = (u64)0xffffffff;
69f4f331 827 ec->dev.coherent_dma_mask = ec->dma_mask;
1da177e4
LT
828
829 if (slot < 4) {
830 ec_set_resource(ec, ECARD_RES_MEMC,
831 PODSLOT_MEMC_BASE + (slot << 14),
832 PODSLOT_MEMC_SIZE);
833 base = PODSLOT_IOC0_BASE + (slot << 14);
834 } else
835 base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
836
837#ifdef CONFIG_ARCH_RPC
838 if (slot < 8) {
839 ec_set_resource(ec, ECARD_RES_EASI,
840 PODSLOT_EASI_BASE + (slot << 24),
841 PODSLOT_EASI_SIZE);
842 }
843
844 if (slot == 8) {
845 ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
846 } else
847#endif
848
849 for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
850 ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
851 base + (i << 19), PODSLOT_IOC_SIZE);
852
853 for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
854 if (ec->resource[i].flags &&
855 request_resource(&iomem_resource, &ec->resource[i])) {
856 printk(KERN_ERR "%s: resource(s) not available\n",
857 ec->dev.bus_id);
858 ec->resource[i].end -= ec->resource[i].start;
859 ec->resource[i].start = 0;
860 ec->resource[i].flags = 0;
861 }
862 }
863
864 nomem:
865 return ec;
866}
867
ff381d22 868static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
869{
870 struct expansion_card *ec = ECARD_DEV(dev);
871 return sprintf(buf, "%u\n", ec->irq);
872}
873
ff381d22 874static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
875{
876 struct expansion_card *ec = ECARD_DEV(dev);
877 return sprintf(buf, "%u\n", ec->dma);
878}
879
ff381d22 880static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
881{
882 struct expansion_card *ec = ECARD_DEV(dev);
883 char *str = buf;
884 int i;
885
886 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
c9e4143c 887 str += sprintf(str, "%08x %08x %08lx\n",
1da177e4
LT
888 ec->resource[i].start,
889 ec->resource[i].end,
890 ec->resource[i].flags);
891
892 return str - buf;
893}
894
ff381d22 895static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
896{
897 struct expansion_card *ec = ECARD_DEV(dev);
898 return sprintf(buf, "%u\n", ec->cid.manufacturer);
899}
900
ff381d22 901static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
902{
903 struct expansion_card *ec = ECARD_DEV(dev);
904 return sprintf(buf, "%u\n", ec->cid.product);
905}
906
ff381d22 907static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
908{
909 struct expansion_card *ec = ECARD_DEV(dev);
910 return sprintf(buf, "%s\n", ec->type == ECARD_EASI ? "EASI" : "IOC");
911}
912
913static struct device_attribute ecard_dev_attrs[] = {
914 __ATTR(device, S_IRUGO, ecard_show_device, NULL),
915 __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
916 __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
917 __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
918 __ATTR(type, S_IRUGO, ecard_show_type, NULL),
919 __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
920 __ATTR_NULL,
921};
922
923
924int ecard_request_resources(struct expansion_card *ec)
925{
926 int i, err = 0;
927
928 for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
929 if (ecard_resource_end(ec, i) &&
930 !request_mem_region(ecard_resource_start(ec, i),
931 ecard_resource_len(ec, i),
932 ec->dev.driver->name)) {
933 err = -EBUSY;
934 break;
935 }
936 }
937
938 if (err) {
939 while (i--)
940 if (ecard_resource_end(ec, i))
941 release_mem_region(ecard_resource_start(ec, i),
942 ecard_resource_len(ec, i));
943 }
944 return err;
945}
946EXPORT_SYMBOL(ecard_request_resources);
947
948void ecard_release_resources(struct expansion_card *ec)
949{
950 int i;
951
952 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
953 if (ecard_resource_end(ec, i))
954 release_mem_region(ecard_resource_start(ec, i),
955 ecard_resource_len(ec, i));
956}
957EXPORT_SYMBOL(ecard_release_resources);
958
959/*
960 * Probe for an expansion card.
961 *
962 * If bit 1 of the first byte of the card is set, then the
963 * card does not exist.
964 */
965static int __init
966ecard_probe(int slot, card_type_t type)
967{
968 ecard_t **ecp;
969 ecard_t *ec;
970 struct ex_ecid cid;
971 int i, rc;
972
973 ec = ecard_alloc_card(type, slot);
974 if (IS_ERR(ec)) {
975 rc = PTR_ERR(ec);
976 goto nomem;
977 }
978
979 rc = -ENODEV;
980 if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
981 goto nodev;
982
983 cid.r_zero = 1;
984 ecard_readbytes(&cid, ec, 0, 16, 0);
985 if (cid.r_zero)
986 goto nodev;
987
988 ec->cid.id = cid.r_id;
989 ec->cid.cd = cid.r_cd;
990 ec->cid.is = cid.r_is;
991 ec->cid.w = cid.r_w;
992 ec->cid.manufacturer = ecard_getu16(cid.r_manu);
993 ec->cid.product = ecard_getu16(cid.r_prod);
994 ec->cid.country = cid.r_country;
995 ec->cid.irqmask = cid.r_irqmask;
996 ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
997 ec->cid.fiqmask = cid.r_fiqmask;
998 ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
999 ec->fiqaddr =
1000 ec->irqaddr = (void __iomem *)ioaddr(ec->podaddr);
1001
1002 if (ec->cid.is) {
1003 ec->irqmask = ec->cid.irqmask;
1004 ec->irqaddr += ec->cid.irqoff;
1005 ec->fiqmask = ec->cid.fiqmask;
1006 ec->fiqaddr += ec->cid.fiqoff;
1007 } else {
1008 ec->irqmask = 1;
1009 ec->fiqmask = 4;
1010 }
1011
eeea82ff 1012 for (i = 0; i < ARRAY_SIZE(blacklist); i++)
1da177e4
LT
1013 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
1014 blacklist[i].product == ec->cid.product) {
1015 ec->card_desc = blacklist[i].type;
1016 break;
1017 }
1018
1019 /*
1020 * hook the interrupt handlers
1021 */
1022 if (slot < 8) {
1023 ec->irq = 32 + slot;
1024 set_irq_chip(ec->irq, &ecard_chip);
10dd5ce2 1025 set_irq_handler(ec->irq, handle_level_irq);
1da177e4
LT
1026 set_irq_flags(ec->irq, IRQF_VALID);
1027 }
1028
1029#ifdef IO_EC_MEMC8_BASE
1030 if (slot == 8)
1031 ec->irq = 11;
1032#endif
1033#ifdef CONFIG_ARCH_RPC
1034 /* On RiscPC, only first two slots have DMA capability */
1035 if (slot < 2)
1036 ec->dma = 2 + slot;
1037#endif
1038
1039 for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
1040
1041 *ecp = ec;
1042 slot_to_expcard[slot] = ec;
1043
1044 device_register(&ec->dev);
1045
1046 return 0;
1047
1048 nodev:
1049 ecard_free_card(ec);
1050 nomem:
1051 return rc;
1052}
1053
1054/*
1055 * Initialise the expansion card system.
1056 * Locate all hardware - interrupt management and
1057 * actual cards.
1058 */
1059static int __init ecard_init(void)
1060{
134c99e9
EB
1061 struct task_struct *task;
1062 int slot, irqhw;
1da177e4 1063
134c99e9
EB
1064 task = kthread_run(ecard_task, NULL, "kecardd");
1065 if (IS_ERR(task)) {
e6aeb47d 1066 printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
134c99e9
EB
1067 PTR_ERR(task));
1068 return PTR_ERR(task);
1da177e4
LT
1069 }
1070
1071 printk("Probing expansion cards\n");
1072
1073 for (slot = 0; slot < 8; slot ++) {
1074 if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1075 ecard_probe(slot, ECARD_IOC);
1076 }
1077
1078#ifdef IO_EC_MEMC8_BASE
1079 ecard_probe(8, ECARD_IOC);
1080#endif
1081
1082 irqhw = ecard_probeirqhw();
1083
1084 set_irq_chained_handler(IRQ_EXPANSIONCARD,
1085 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
1086
1087 ecard_proc_init();
1088
1089 return 0;
1090}
1091
1092subsys_initcall(ecard_init);
1093
1094/*
1095 * ECARD "bus"
1096 */
1097static const struct ecard_id *
1098ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1099{
1100 int i;
1101
1102 for (i = 0; ids[i].manufacturer != 65535; i++)
1103 if (ec->cid.manufacturer == ids[i].manufacturer &&
1104 ec->cid.product == ids[i].product)
1105 return ids + i;
1106
1107 return NULL;
1108}
1109
1110static int ecard_drv_probe(struct device *dev)
1111{
1112 struct expansion_card *ec = ECARD_DEV(dev);
1113 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1114 const struct ecard_id *id;
1115 int ret;
1116
1117 id = ecard_match_device(drv->id_table, ec);
1118
1119 ecard_claim(ec);
1120 ret = drv->probe(ec, id);
1121 if (ret)
1122 ecard_release(ec);
1123 return ret;
1124}
1125
1126static int ecard_drv_remove(struct device *dev)
1127{
1128 struct expansion_card *ec = ECARD_DEV(dev);
1129 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1130
1131 drv->remove(ec);
1132 ecard_release(ec);
1133
1134 return 0;
1135}
1136
1137/*
1138 * Before rebooting, we must make sure that the expansion card is in a
1139 * sensible state, so it can be re-detected. This means that the first
1140 * page of the ROM must be visible. We call the expansion cards reset
1141 * handler, if any.
1142 */
1143static void ecard_drv_shutdown(struct device *dev)
1144{
1145 struct expansion_card *ec = ECARD_DEV(dev);
1146 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1147 struct ecard_request req;
1148
e08b7541
RK
1149 if (dev->driver) {
1150 if (drv->shutdown)
1151 drv->shutdown(ec);
1152 ecard_release(ec);
1153 }
1da177e4
LT
1154
1155 /*
1156 * If this card has a loader, call the reset handler.
1157 */
1158 if (ec->loader) {
1159 req.fn = ecard_task_reset;
1160 req.ec = ec;
1161 ecard_call(&req);
1162 }
1163}
1164
1165int ecard_register_driver(struct ecard_driver *drv)
1166{
1167 drv->drv.bus = &ecard_bus_type;
1da177e4
LT
1168
1169 return driver_register(&drv->drv);
1170}
1171
1172void ecard_remove_driver(struct ecard_driver *drv)
1173{
1174 driver_unregister(&drv->drv);
1175}
1176
1177static int ecard_match(struct device *_dev, struct device_driver *_drv)
1178{
1179 struct expansion_card *ec = ECARD_DEV(_dev);
1180 struct ecard_driver *drv = ECARD_DRV(_drv);
1181 int ret;
1182
1183 if (drv->id_table) {
1184 ret = ecard_match_device(drv->id_table, ec) != NULL;
1185 } else {
1186 ret = ec->cid.id == drv->id;
1187 }
1188
1189 return ret;
1190}
1191
1192struct bus_type ecard_bus_type = {
1193 .name = "ecard",
1194 .dev_attrs = ecard_dev_attrs,
1195 .match = ecard_match,
e08b7541
RK
1196 .probe = ecard_drv_probe,
1197 .remove = ecard_drv_remove,
1198 .shutdown = ecard_drv_shutdown,
1da177e4
LT
1199};
1200
1201static int ecard_bus_init(void)
1202{
1203 return bus_register(&ecard_bus_type);
1204}
1205
1206postcore_initcall(ecard_bus_init);
1207
1208EXPORT_SYMBOL(ecard_readchunk);
1209EXPORT_SYMBOL(__ecard_address);
1210EXPORT_SYMBOL(ecard_register_driver);
1211EXPORT_SYMBOL(ecard_remove_driver);
1212EXPORT_SYMBOL(ecard_bus_type);