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