driver core: remove DEVICE_ID_SIZE define
[deliverable/linux.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>
9b001212 40#include <linux/seq_file.h>
1da177e4
LT
41#include <linux/device.h>
42#include <linux/init.h>
00431707 43#include <linux/mutex.h>
134c99e9 44#include <linux/kthread.h>
10bdaaa0 45#include <linux/io.h>
1da177e4
LT
46
47#include <asm/dma.h>
48#include <asm/ecard.h>
49#include <asm/hardware.h>
1da177e4
LT
50#include <asm/irq.h>
51#include <asm/mmu_context.h>
52#include <asm/mach/irq.h>
53#include <asm/tlbflush.h>
54
c0b04d1b
RK
55#include "ecard.h"
56
1da177e4
LT
57#ifndef CONFIG_ARCH_RPC
58#define HAVE_EXPMASK
59#endif
60
61struct ecard_request {
62 void (*fn)(struct ecard_request *);
63 ecard_t *ec;
64 unsigned int address;
65 unsigned int length;
66 unsigned int use_loader;
67 void *buffer;
68 struct completion *complete;
69};
70
71struct expcard_blacklist {
72 unsigned short manufacturer;
73 unsigned short product;
74 const char *type;
75};
76
77static ecard_t *cards;
78static ecard_t *slot_to_expcard[MAX_ECARDS];
79static unsigned int ectcr;
80#ifdef HAS_EXPMASK
81static unsigned int have_expmask;
82#endif
83
84/* List of descriptions of cards which don't have an extended
85 * identification, or chunk directories containing a description.
86 */
87static struct expcard_blacklist __initdata blacklist[] = {
88 { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
89};
90
91asmlinkage extern int
92ecard_loader_reset(unsigned long base, loader_t loader);
93asmlinkage extern int
94ecard_loader_read(int off, unsigned long base, loader_t loader);
95
96static inline unsigned short ecard_getu16(unsigned char *v)
97{
98 return v[0] | v[1] << 8;
99}
100
101static inline signed long ecard_gets24(unsigned char *v)
102{
103 return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
104}
105
106static inline ecard_t *slot_to_ecard(unsigned int slot)
107{
108 return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
109}
110
111/* ===================== Expansion card daemon ======================== */
112/*
113 * Since the loader programs on the expansion cards need to be run
114 * in a specific environment, create a separate task with this
115 * environment up, and pass requests to this task as and when we
116 * need to.
117 *
118 * This should allow 99% of loaders to be called from Linux.
119 *
120 * From a security standpoint, we trust the card vendors. This
121 * may be a misplaced trust.
122 */
123static void ecard_task_reset(struct ecard_request *req)
124{
125 struct expansion_card *ec = req->ec;
126 struct resource *res;
127
128 res = ec->slot_no == 8
129 ? &ec->resource[ECARD_RES_MEMC]
5559bca8 130 : ec->easi
1da177e4
LT
131 ? &ec->resource[ECARD_RES_EASI]
132 : &ec->resource[ECARD_RES_IOCSYNC];
133
134 ecard_loader_reset(res->start, ec->loader);
135}
136
137static void ecard_task_readbytes(struct ecard_request *req)
138{
139 struct expansion_card *ec = req->ec;
140 unsigned char *buf = req->buffer;
141 unsigned int len = req->length;
142 unsigned int off = req->address;
143
144 if (ec->slot_no == 8) {
145 void __iomem *base = (void __iomem *)
146 ec->resource[ECARD_RES_MEMC].start;
147
148 /*
149 * The card maintains an index which increments the address
150 * into a 4096-byte page on each access. We need to keep
151 * track of the counter.
152 */
153 static unsigned int index;
154 unsigned int page;
155
156 page = (off >> 12) * 4;
157 if (page > 256 * 4)
158 return;
159
160 off &= 4095;
161
162 /*
163 * If we are reading offset 0, or our current index is
164 * greater than the offset, reset the hardware index counter.
165 */
166 if (off == 0 || index > off) {
167 writeb(0, base);
168 index = 0;
169 }
170
171 /*
172 * Increment the hardware index counter until we get to the
173 * required offset. The read bytes are discarded.
174 */
175 while (index < off) {
176 readb(base + page);
177 index += 1;
178 }
179
180 while (len--) {
181 *buf++ = readb(base + page);
182 index += 1;
183 }
184 } else {
5559bca8 185 unsigned long base = (ec->easi
1da177e4
LT
186 ? &ec->resource[ECARD_RES_EASI]
187 : &ec->resource[ECARD_RES_IOCSYNC])->start;
188 void __iomem *pbase = (void __iomem *)base;
189
190 if (!req->use_loader || !ec->loader) {
191 off *= 4;
192 while (len--) {
193 *buf++ = readb(pbase + off);
194 off += 4;
195 }
196 } else {
197 while(len--) {
198 /*
199 * The following is required by some
200 * expansion card loader programs.
201 */
202 *(unsigned long *)0x108 = 0;
203 *buf++ = ecard_loader_read(off++, base,
204 ec->loader);
205 }
206 }
207 }
208
209}
210
211static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
212static struct ecard_request *ecard_req;
00431707 213static DEFINE_MUTEX(ecard_mutex);
1da177e4
LT
214
215/*
216 * Set up the expansion card daemon's page tables.
217 */
218static void ecard_init_pgtables(struct mm_struct *mm)
219{
220 struct vm_area_struct vma;
221
222 /* We want to set up the page tables for the following mapping:
223 * Virtual Physical
224 * 0x03000000 0x03000000
225 * 0x03010000 unmapped
226 * 0x03210000 0x03210000
227 * 0x03400000 unmapped
228 * 0x08000000 0x08000000
229 * 0x10000000 unmapped
230 *
231 * FIXME: we don't follow this 100% yet.
232 */
233 pgd_t *src_pgd, *dst_pgd;
234
235 src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
236 dst_pgd = pgd_offset(mm, IO_START);
237
238 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
239
240 src_pgd = pgd_offset(mm, EASI_BASE);
241 dst_pgd = pgd_offset(mm, EASI_START);
242
243 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
244
245 vma.vm_mm = mm;
246
247 flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
248 flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
249}
250
251static int ecard_init_mm(void)
252{
253 struct mm_struct * mm = mm_alloc();
254 struct mm_struct *active_mm = current->active_mm;
255
256 if (!mm)
257 return -ENOMEM;
258
259 current->mm = mm;
260 current->active_mm = mm;
261 activate_mm(active_mm, mm);
262 mmdrop(active_mm);
263 ecard_init_pgtables(mm);
264 return 0;
265}
266
267static int
268ecard_task(void * unused)
269{
1da177e4
LT
270 /*
271 * Allocate a mm. We're not a lazy-TLB kernel task since we need
272 * to set page table entries where the user space would be. Note
273 * that this also creates the page tables. Failure is not an
274 * option here.
275 */
276 if (ecard_init_mm())
277 panic("kecardd: unable to alloc mm\n");
278
279 while (1) {
280 struct ecard_request *req;
281
282 wait_event_interruptible(ecard_wait, ecard_req != NULL);
283
284 req = xchg(&ecard_req, NULL);
285 if (req != NULL) {
286 req->fn(req);
287 complete(req->complete);
288 }
289 }
290}
291
292/*
293 * Wake the expansion card daemon to action our request.
294 *
295 * FIXME: The test here is not sufficient to detect if the
296 * kcardd is running.
297 */
298static void ecard_call(struct ecard_request *req)
299{
6e9a4738 300 DECLARE_COMPLETION_ONSTACK(completion);
1da177e4
LT
301
302 req->complete = &completion;
303
00431707 304 mutex_lock(&ecard_mutex);
1da177e4
LT
305 ecard_req = req;
306 wake_up(&ecard_wait);
307
308 /*
309 * Now wait for kecardd to run.
310 */
311 wait_for_completion(&completion);
00431707 312 mutex_unlock(&ecard_mutex);
1da177e4
LT
313}
314
315/* ======================= Mid-level card control ===================== */
316
317static void
318ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
319{
320 struct ecard_request req;
321
322 req.fn = ecard_task_readbytes;
323 req.ec = ec;
324 req.address = off;
325 req.length = len;
326 req.use_loader = useld;
327 req.buffer = addr;
328
329 ecard_call(&req);
330}
331
332int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
333{
334 struct ex_chunk_dir excd;
335 int index = 16;
336 int useld = 0;
337
338 if (!ec->cid.cd)
339 return 0;
340
341 while(1) {
342 ecard_readbytes(&excd, ec, index, 8, useld);
343 index += 8;
344 if (c_id(&excd) == 0) {
345 if (!useld && ec->loader) {
346 useld = 1;
347 index = 0;
348 continue;
349 }
350 return 0;
351 }
352 if (c_id(&excd) == 0xf0) { /* link */
353 index = c_start(&excd);
354 continue;
355 }
356 if (c_id(&excd) == 0x80) { /* loader */
357 if (!ec->loader) {
5cbded58 358 ec->loader = kmalloc(c_len(&excd),
1da177e4
LT
359 GFP_KERNEL);
360 if (ec->loader)
361 ecard_readbytes(ec->loader, ec,
362 (int)c_start(&excd),
363 c_len(&excd), useld);
364 else
365 return 0;
366 }
367 continue;
368 }
369 if (c_id(&excd) == id && num-- == 0)
370 break;
371 }
372
373 if (c_id(&excd) & 0x80) {
374 switch (c_id(&excd) & 0x70) {
375 case 0x70:
376 ecard_readbytes((unsigned char *)excd.d.string, ec,
377 (int)c_start(&excd), c_len(&excd),
378 useld);
379 break;
380 case 0x00:
381 break;
382 }
383 }
384 cd->start_offset = c_start(&excd);
385 memcpy(cd->d.string, excd.d.string, 256);
386 return 1;
387}
388
389/* ======================= Interrupt control ============================ */
390
391static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
392{
393#ifdef HAS_EXPMASK
394 if (irqnr < 4 && have_expmask) {
395 have_expmask |= 1 << irqnr;
396 __raw_writeb(have_expmask, EXPMASK_ENABLE);
397 }
398#endif
399}
400
401static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
402{
403#ifdef HAS_EXPMASK
404 if (irqnr < 4 && have_expmask) {
405 have_expmask &= ~(1 << irqnr);
406 __raw_writeb(have_expmask, EXPMASK_ENABLE);
407 }
408#endif
409}
410
411static int ecard_def_irq_pending(ecard_t *ec)
412{
413 return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
414}
415
416static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
417{
418 panic("ecard_def_fiq_enable called - impossible");
419}
420
421static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
422{
423 panic("ecard_def_fiq_disable called - impossible");
424}
425
426static int ecard_def_fiq_pending(ecard_t *ec)
427{
428 return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
429}
430
431static expansioncard_ops_t ecard_default_ops = {
432 ecard_def_irq_enable,
433 ecard_def_irq_disable,
434 ecard_def_irq_pending,
435 ecard_def_fiq_enable,
436 ecard_def_fiq_disable,
437 ecard_def_fiq_pending
438};
439
440/*
441 * Enable and disable interrupts from expansion cards.
442 * (interrupts are disabled for these functions).
443 *
444 * They are not meant to be called directly, but via enable/disable_irq.
445 */
446static void ecard_irq_unmask(unsigned int irqnr)
447{
448 ecard_t *ec = slot_to_ecard(irqnr - 32);
449
450 if (ec) {
451 if (!ec->ops)
452 ec->ops = &ecard_default_ops;
453
454 if (ec->claimed && ec->ops->irqenable)
455 ec->ops->irqenable(ec, irqnr);
456 else
457 printk(KERN_ERR "ecard: rejecting request to "
458 "enable IRQs for %d\n", irqnr);
459 }
460}
461
462static void ecard_irq_mask(unsigned int irqnr)
463{
464 ecard_t *ec = slot_to_ecard(irqnr - 32);
465
466 if (ec) {
467 if (!ec->ops)
468 ec->ops = &ecard_default_ops;
469
470 if (ec->ops && ec->ops->irqdisable)
471 ec->ops->irqdisable(ec, irqnr);
472 }
473}
474
38c677cb
DB
475static struct irq_chip ecard_chip = {
476 .name = "ECARD",
1da177e4
LT
477 .ack = ecard_irq_mask,
478 .mask = ecard_irq_mask,
479 .unmask = ecard_irq_unmask,
480};
481
482void ecard_enablefiq(unsigned int fiqnr)
483{
484 ecard_t *ec = slot_to_ecard(fiqnr);
485
486 if (ec) {
487 if (!ec->ops)
488 ec->ops = &ecard_default_ops;
489
490 if (ec->claimed && ec->ops->fiqenable)
491 ec->ops->fiqenable(ec, fiqnr);
492 else
493 printk(KERN_ERR "ecard: rejecting request to "
494 "enable FIQs for %d\n", fiqnr);
495 }
496}
497
498void ecard_disablefiq(unsigned int fiqnr)
499{
500 ecard_t *ec = slot_to_ecard(fiqnr);
501
502 if (ec) {
503 if (!ec->ops)
504 ec->ops = &ecard_default_ops;
505
506 if (ec->ops->fiqdisable)
507 ec->ops->fiqdisable(ec, fiqnr);
508 }
509}
510
511static void ecard_dump_irq_state(void)
512{
513 ecard_t *ec;
514
515 printk("Expansion card IRQ state:\n");
516
517 for (ec = cards; ec; ec = ec->next) {
518 if (ec->slot_no == 8)
519 continue;
520
521 printk(" %d: %sclaimed, ",
522 ec->slot_no, ec->claimed ? "" : "not ");
523
524 if (ec->ops && ec->ops->irqpending &&
525 ec->ops != &ecard_default_ops)
526 printk("irq %spending\n",
527 ec->ops->irqpending(ec) ? "" : "not ");
528 else
529 printk("irqaddr %p, mask = %02X, status = %02X\n",
530 ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
531 }
532}
533
10dd5ce2 534static void ecard_check_lockup(struct irq_desc *desc)
1da177e4
LT
535{
536 static unsigned long last;
537 static int lockup;
538
539 /*
540 * If the timer interrupt has not run since the last million
541 * unrecognised expansion card interrupts, then there is
542 * something seriously wrong. Disable the expansion card
543 * interrupts so at least we can continue.
544 *
545 * Maybe we ought to start a timer to re-enable them some time
546 * later?
547 */
548 if (last == jiffies) {
549 lockup += 1;
550 if (lockup > 1000000) {
551 printk(KERN_ERR "\nInterrupt lockup detected - "
552 "disabling all expansion card interrupts\n");
553
554 desc->chip->mask(IRQ_EXPANSIONCARD);
555 ecard_dump_irq_state();
556 }
557 } else
558 lockup = 0;
559
560 /*
561 * If we did not recognise the source of this interrupt,
562 * warn the user, but don't flood the user with these messages.
563 */
564 if (!last || time_after(jiffies, last + 5*HZ)) {
565 last = jiffies;
566 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
567 ecard_dump_irq_state();
568 }
569}
570
571static void
10dd5ce2 572ecard_irq_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
573{
574 ecard_t *ec;
575 int called = 0;
576
577 desc->chip->mask(irq);
578 for (ec = cards; ec; ec = ec->next) {
579 int pending;
580
581 if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
582 continue;
583
584 if (ec->ops && ec->ops->irqpending)
585 pending = ec->ops->irqpending(ec);
586 else
587 pending = ecard_default_ops.irqpending(ec);
588
589 if (pending) {
10dd5ce2 590 struct irq_desc *d = irq_desc + ec->irq;
0cd61b68 591 desc_handle_irq(ec->irq, d);
1da177e4
LT
592 called ++;
593 }
594 }
595 desc->chip->unmask(irq);
596
597 if (called == 0)
598 ecard_check_lockup(desc);
599}
600
601#ifdef HAS_EXPMASK
602static unsigned char priority_masks[] =
603{
604 0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
605};
606
607static unsigned char first_set[] =
608{
609 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
610 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
611};
612
613static void
10dd5ce2 614ecard_irqexp_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
615{
616 const unsigned int statusmask = 15;
617 unsigned int status;
618
619 status = __raw_readb(EXPMASK_STATUS) & statusmask;
620 if (status) {
621 unsigned int slot = first_set[status];
622 ecard_t *ec = slot_to_ecard(slot);
623
624 if (ec->claimed) {
33e39f1d 625 struct irq_desc *d = irq_desc + ec->irq;
1da177e4
LT
626 /*
627 * this ugly code is so that we can operate a
628 * prioritorising system:
629 *
630 * Card 0 highest priority
631 * Card 1
632 * Card 2
633 * Card 3 lowest priority
634 *
635 * Serial cards should go in 0/1, ethernet/scsi in 2/3
636 * otherwise you will lose serial data at high speeds!
637 */
0cd61b68 638 desc_handle_irq(ec->irq, d);
1da177e4
LT
639 } else {
640 printk(KERN_WARNING "card%d: interrupt from unclaimed "
641 "card???\n", slot);
642 have_expmask &= ~(1 << slot);
643 __raw_writeb(have_expmask, EXPMASK_ENABLE);
644 }
645 } else
646 printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
647}
648
649static int __init ecard_probeirqhw(void)
650{
651 ecard_t *ec;
652 int found;
653
654 __raw_writeb(0x00, EXPMASK_ENABLE);
655 __raw_writeb(0xff, EXPMASK_STATUS);
656 found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
657 __raw_writeb(0xff, EXPMASK_ENABLE);
658
659 if (found) {
660 printk(KERN_DEBUG "Expansion card interrupt "
661 "management hardware found\n");
662
663 /* for each card present, set a bit to '1' */
664 have_expmask = 0x80000000;
665
666 for (ec = cards; ec; ec = ec->next)
667 have_expmask |= 1 << ec->slot_no;
668
669 __raw_writeb(have_expmask, EXPMASK_ENABLE);
670 }
671
672 return found;
673}
674#else
675#define ecard_irqexp_handler NULL
676#define ecard_probeirqhw() (0)
677#endif
678
679#ifndef IO_EC_MEMC8_BASE
680#define IO_EC_MEMC8_BASE 0
681#endif
682
9ecba1f2 683static unsigned int __ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
1da177e4
LT
684{
685 unsigned long address = 0;
686 int slot = ec->slot_no;
687
688 if (ec->slot_no == 8)
689 return IO_EC_MEMC8_BASE;
690
691 ectcr &= ~(1 << slot);
692
693 switch (type) {
694 case ECARD_MEMC:
695 if (slot < 4)
696 address = IO_EC_MEMC_BASE + (slot << 12);
697 break;
698
699 case ECARD_IOC:
700 if (slot < 4)
701 address = IO_EC_IOC_BASE + (slot << 12);
702#ifdef IO_EC_IOC4_BASE
703 else
704 address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
705#endif
706 if (address)
707 address += speed << 17;
708 break;
709
710#ifdef IO_EC_EASI_BASE
711 case ECARD_EASI:
712 address = IO_EC_EASI_BASE + (slot << 22);
713 if (speed == ECARD_FAST)
714 ectcr |= 1 << slot;
715 break;
716#endif
717 default:
718 break;
719 }
720
721#ifdef IOMD_ECTCR
722 iomd_writeb(ectcr, IOMD_ECTCR);
723#endif
724 return address;
725}
726
9b001212 727static int ecard_prints(struct seq_file *m, ecard_t *ec)
1da177e4 728{
9b001212 729 seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
1da177e4
LT
730
731 if (ec->cid.id == 0) {
732 struct in_chunk_dir incd;
733
9b001212 734 seq_printf(m, "[%04X:%04X] ",
1da177e4
LT
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
9b001212 745 seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
1da177e4 746 } else
9b001212 747 seq_printf(m, "Simple card %d\n", ec->cid.id);
1da177e4 748
9b001212 749 return 0;
1da177e4
LT
750}
751
9b001212 752static int ecard_devices_proc_show(struct seq_file *m, void *v)
1da177e4
LT
753{
754 ecard_t *ec = cards;
9b001212
AD
755
756 while (ec) {
757 ecard_prints(m, ec);
1da177e4
LT
758 ec = ec->next;
759 }
9b001212 760 return 0;
1da177e4
LT
761}
762
9b001212
AD
763static int ecard_devices_proc_open(struct inode *inode, struct file *file)
764{
765 return single_open(file, ecard_devices_proc_show, NULL);
766}
767
768static const struct file_operations bus_ecard_proc_fops = {
769 .owner = THIS_MODULE,
770 .open = ecard_devices_proc_open,
771 .read = seq_read,
772 .llseek = seq_lseek,
773 .release = single_release,
774};
775
1da177e4
LT
776static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
777
778static void ecard_proc_init(void)
779{
9c37066d 780 proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
9b001212 781 proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
1da177e4
LT
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 815 ec->slot_no = slot;
5559bca8 816 ec->easi = type == ECARD_EASI;
1da177e4
LT
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);
5559bca8 910 return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
1da177e4
LT
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
c7b87f3d
RK
959void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
960{
961 ec->irq_data = irq_data;
962 barrier();
963 ec->ops = ops;
964}
965EXPORT_SYMBOL(ecard_setirq);
966
10bdaaa0
RK
967void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
968 unsigned long offset, unsigned long maxsize)
969{
970 unsigned long start = ecard_resource_start(ec, res);
971 unsigned long end = ecard_resource_end(ec, res);
972
973 if (offset > (end - start))
974 return NULL;
975
976 start += offset;
977 if (maxsize && end - start > maxsize)
978 end = start + maxsize;
979
980 return devm_ioremap(&ec->dev, start, end - start);
981}
982EXPORT_SYMBOL(ecardm_iomap);
983
1da177e4
LT
984/*
985 * Probe for an expansion card.
986 *
987 * If bit 1 of the first byte of the card is set, then the
988 * card does not exist.
989 */
990static int __init
991ecard_probe(int slot, card_type_t type)
992{
993 ecard_t **ecp;
994 ecard_t *ec;
995 struct ex_ecid cid;
996 int i, rc;
997
998 ec = ecard_alloc_card(type, slot);
999 if (IS_ERR(ec)) {
1000 rc = PTR_ERR(ec);
1001 goto nomem;
1002 }
1003
1004 rc = -ENODEV;
9ecba1f2 1005 if ((ec->podaddr = __ecard_address(ec, type, ECARD_SYNC)) == 0)
1da177e4
LT
1006 goto nodev;
1007
1008 cid.r_zero = 1;
1009 ecard_readbytes(&cid, ec, 0, 16, 0);
1010 if (cid.r_zero)
1011 goto nodev;
1012
1013 ec->cid.id = cid.r_id;
1014 ec->cid.cd = cid.r_cd;
1015 ec->cid.is = cid.r_is;
1016 ec->cid.w = cid.r_w;
1017 ec->cid.manufacturer = ecard_getu16(cid.r_manu);
1018 ec->cid.product = ecard_getu16(cid.r_prod);
1019 ec->cid.country = cid.r_country;
1020 ec->cid.irqmask = cid.r_irqmask;
1021 ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
1022 ec->cid.fiqmask = cid.r_fiqmask;
1023 ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
1024 ec->fiqaddr =
1025 ec->irqaddr = (void __iomem *)ioaddr(ec->podaddr);
1026
1027 if (ec->cid.is) {
1028 ec->irqmask = ec->cid.irqmask;
1029 ec->irqaddr += ec->cid.irqoff;
1030 ec->fiqmask = ec->cid.fiqmask;
1031 ec->fiqaddr += ec->cid.fiqoff;
1032 } else {
1033 ec->irqmask = 1;
1034 ec->fiqmask = 4;
1035 }
1036
eeea82ff 1037 for (i = 0; i < ARRAY_SIZE(blacklist); i++)
1da177e4
LT
1038 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
1039 blacklist[i].product == ec->cid.product) {
1040 ec->card_desc = blacklist[i].type;
1041 break;
1042 }
1043
1044 /*
1045 * hook the interrupt handlers
1046 */
1047 if (slot < 8) {
1048 ec->irq = 32 + slot;
1049 set_irq_chip(ec->irq, &ecard_chip);
10dd5ce2 1050 set_irq_handler(ec->irq, handle_level_irq);
1da177e4
LT
1051 set_irq_flags(ec->irq, IRQF_VALID);
1052 }
1053
1054#ifdef IO_EC_MEMC8_BASE
1055 if (slot == 8)
1056 ec->irq = 11;
1057#endif
1058#ifdef CONFIG_ARCH_RPC
1059 /* On RiscPC, only first two slots have DMA capability */
1060 if (slot < 2)
1061 ec->dma = 2 + slot;
1062#endif
1063
1064 for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
1065
1066 *ecp = ec;
1067 slot_to_expcard[slot] = ec;
1068
1069 device_register(&ec->dev);
1070
1071 return 0;
1072
1073 nodev:
1074 ecard_free_card(ec);
1075 nomem:
1076 return rc;
1077}
1078
1079/*
1080 * Initialise the expansion card system.
1081 * Locate all hardware - interrupt management and
1082 * actual cards.
1083 */
1084static int __init ecard_init(void)
1085{
134c99e9
EB
1086 struct task_struct *task;
1087 int slot, irqhw;
1da177e4 1088
134c99e9
EB
1089 task = kthread_run(ecard_task, NULL, "kecardd");
1090 if (IS_ERR(task)) {
e6aeb47d 1091 printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
134c99e9
EB
1092 PTR_ERR(task));
1093 return PTR_ERR(task);
1da177e4
LT
1094 }
1095
1096 printk("Probing expansion cards\n");
1097
1098 for (slot = 0; slot < 8; slot ++) {
1099 if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
1100 ecard_probe(slot, ECARD_IOC);
1101 }
1102
1103#ifdef IO_EC_MEMC8_BASE
1104 ecard_probe(8, ECARD_IOC);
1105#endif
1106
1107 irqhw = ecard_probeirqhw();
1108
1109 set_irq_chained_handler(IRQ_EXPANSIONCARD,
1110 irqhw ? ecard_irqexp_handler : ecard_irq_handler);
1111
1112 ecard_proc_init();
1113
1114 return 0;
1115}
1116
1117subsys_initcall(ecard_init);
1118
1119/*
1120 * ECARD "bus"
1121 */
1122static const struct ecard_id *
1123ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1124{
1125 int i;
1126
1127 for (i = 0; ids[i].manufacturer != 65535; i++)
1128 if (ec->cid.manufacturer == ids[i].manufacturer &&
1129 ec->cid.product == ids[i].product)
1130 return ids + i;
1131
1132 return NULL;
1133}
1134
1135static int ecard_drv_probe(struct device *dev)
1136{
1137 struct expansion_card *ec = ECARD_DEV(dev);
1138 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1139 const struct ecard_id *id;
1140 int ret;
1141
1142 id = ecard_match_device(drv->id_table, ec);
1143
9ecba1f2 1144 ec->claimed = 1;
1da177e4
LT
1145 ret = drv->probe(ec, id);
1146 if (ret)
9ecba1f2 1147 ec->claimed = 0;
1da177e4
LT
1148 return ret;
1149}
1150
1151static int ecard_drv_remove(struct device *dev)
1152{
1153 struct expansion_card *ec = ECARD_DEV(dev);
1154 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1155
1156 drv->remove(ec);
9ecba1f2 1157 ec->claimed = 0;
1da177e4 1158
c7b87f3d
RK
1159 /*
1160 * Restore the default operations. We ensure that the
1161 * ops are set before we change the data.
1162 */
1163 ec->ops = &ecard_default_ops;
1164 barrier();
1165 ec->irq_data = NULL;
1166
1da177e4
LT
1167 return 0;
1168}
1169
1170/*
1171 * Before rebooting, we must make sure that the expansion card is in a
1172 * sensible state, so it can be re-detected. This means that the first
1173 * page of the ROM must be visible. We call the expansion cards reset
1174 * handler, if any.
1175 */
1176static void ecard_drv_shutdown(struct device *dev)
1177{
1178 struct expansion_card *ec = ECARD_DEV(dev);
1179 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1180 struct ecard_request req;
1181
e08b7541
RK
1182 if (dev->driver) {
1183 if (drv->shutdown)
1184 drv->shutdown(ec);
9ecba1f2 1185 ec->claimed = 0;
e08b7541 1186 }
1da177e4
LT
1187
1188 /*
1189 * If this card has a loader, call the reset handler.
1190 */
1191 if (ec->loader) {
1192 req.fn = ecard_task_reset;
1193 req.ec = ec;
1194 ecard_call(&req);
1195 }
1196}
1197
1198int ecard_register_driver(struct ecard_driver *drv)
1199{
1200 drv->drv.bus = &ecard_bus_type;
1da177e4
LT
1201
1202 return driver_register(&drv->drv);
1203}
1204
1205void ecard_remove_driver(struct ecard_driver *drv)
1206{
1207 driver_unregister(&drv->drv);
1208}
1209
1210static int ecard_match(struct device *_dev, struct device_driver *_drv)
1211{
1212 struct expansion_card *ec = ECARD_DEV(_dev);
1213 struct ecard_driver *drv = ECARD_DRV(_drv);
1214 int ret;
1215
1216 if (drv->id_table) {
1217 ret = ecard_match_device(drv->id_table, ec) != NULL;
1218 } else {
1219 ret = ec->cid.id == drv->id;
1220 }
1221
1222 return ret;
1223}
1224
1225struct bus_type ecard_bus_type = {
1226 .name = "ecard",
1227 .dev_attrs = ecard_dev_attrs,
1228 .match = ecard_match,
e08b7541
RK
1229 .probe = ecard_drv_probe,
1230 .remove = ecard_drv_remove,
1231 .shutdown = ecard_drv_shutdown,
1da177e4
LT
1232};
1233
1234static int ecard_bus_init(void)
1235{
1236 return bus_register(&ecard_bus_type);
1237}
1238
1239postcore_initcall(ecard_bus_init);
1240
1241EXPORT_SYMBOL(ecard_readchunk);
1da177e4
LT
1242EXPORT_SYMBOL(ecard_register_driver);
1243EXPORT_SYMBOL(ecard_remove_driver);
1244EXPORT_SYMBOL(ecard_bus_type);
This page took 0.345096 seconds and 5 git commands to generate.