Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/kernel/ecard.c
0004  *
0005  *  Copyright 1995-2001 Russell King
0006  *
0007  *  Find all installed expansion cards, and handle interrupts from them.
0008  *
0009  *  Created from information from Acorns RiscOS3 PRMs
0010  *
0011  *  08-Dec-1996 RMK Added code for the 9'th expansion card - the ether
0012  *          podule slot.
0013  *  06-May-1997 RMK Added blacklist for cards whose loader doesn't work.
0014  *  12-Sep-1997 RMK Created new handling of interrupt enables/disables
0015  *          - cards can now register their own routine to control
0016  *          interrupts (recommended).
0017  *  29-Sep-1997 RMK Expansion card interrupt hardware not being re-enabled
0018  *          on reset from Linux. (Caused cards not to respond
0019  *          under RiscOS without hard reset).
0020  *  15-Feb-1998 RMK Added DMA support
0021  *  12-Sep-1998 RMK Added EASI support
0022  *  10-Jan-1999 RMK Run loaders in a simulated RISC OS environment.
0023  *  17-Apr-1999 RMK Support for EASI Type C cycles.
0024  */
0025 #define ECARD_C
0026 
0027 #include <linux/module.h>
0028 #include <linux/kernel.h>
0029 #include <linux/types.h>
0030 #include <linux/sched.h>
0031 #include <linux/sched/mm.h>
0032 #include <linux/interrupt.h>
0033 #include <linux/completion.h>
0034 #include <linux/reboot.h>
0035 #include <linux/mm.h>
0036 #include <linux/slab.h>
0037 #include <linux/proc_fs.h>
0038 #include <linux/seq_file.h>
0039 #include <linux/device.h>
0040 #include <linux/init.h>
0041 #include <linux/mutex.h>
0042 #include <linux/kthread.h>
0043 #include <linux/irq.h>
0044 #include <linux/io.h>
0045 
0046 #include <asm/dma.h>
0047 #include <asm/ecard.h>
0048 #include <mach/hardware.h>
0049 #include <asm/irq.h>
0050 #include <asm/mmu_context.h>
0051 #include <asm/mach/irq.h>
0052 #include <asm/tlbflush.h>
0053 
0054 #include "ecard.h"
0055 
0056 struct ecard_request {
0057     void        (*fn)(struct ecard_request *);
0058     ecard_t     *ec;
0059     unsigned int    address;
0060     unsigned int    length;
0061     unsigned int    use_loader;
0062     void        *buffer;
0063     struct completion *complete;
0064 };
0065 
0066 struct expcard_quirklist {
0067     unsigned short   manufacturer;
0068     unsigned short   product;
0069     const char  *type;
0070     void (*init)(ecard_t *ec);
0071 };
0072 
0073 static ecard_t *cards;
0074 static ecard_t *slot_to_expcard[MAX_ECARDS];
0075 static unsigned int ectcr;
0076 
0077 static void atomwide_3p_quirk(ecard_t *ec);
0078 
0079 /* List of descriptions of cards which don't have an extended
0080  * identification, or chunk directories containing a description.
0081  */
0082 static struct expcard_quirklist quirklist[] __initdata = {
0083     { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" },
0084     { MANU_ATOMWIDE, PROD_ATOMWIDE_3PSERIAL, NULL, atomwide_3p_quirk },
0085 };
0086 
0087 asmlinkage extern int
0088 ecard_loader_reset(unsigned long base, loader_t loader);
0089 asmlinkage extern int
0090 ecard_loader_read(int off, unsigned long base, loader_t loader);
0091 
0092 static inline unsigned short ecard_getu16(unsigned char *v)
0093 {
0094     return v[0] | v[1] << 8;
0095 }
0096 
0097 static inline signed long ecard_gets24(unsigned char *v)
0098 {
0099     return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
0100 }
0101 
0102 static inline ecard_t *slot_to_ecard(unsigned int slot)
0103 {
0104     return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
0105 }
0106 
0107 /* ===================== Expansion card daemon ======================== */
0108 /*
0109  * Since the loader programs on the expansion cards need to be run
0110  * in a specific environment, create a separate task with this
0111  * environment up, and pass requests to this task as and when we
0112  * need to.
0113  *
0114  * This should allow 99% of loaders to be called from Linux.
0115  *
0116  * From a security standpoint, we trust the card vendors.  This
0117  * may be a misplaced trust.
0118  */
0119 static void ecard_task_reset(struct ecard_request *req)
0120 {
0121     struct expansion_card *ec = req->ec;
0122     struct resource *res;
0123 
0124     res = ec->slot_no == 8
0125         ? &ec->resource[ECARD_RES_MEMC]
0126         : ec->easi
0127           ? &ec->resource[ECARD_RES_EASI]
0128           : &ec->resource[ECARD_RES_IOCSYNC];
0129 
0130     ecard_loader_reset(res->start, ec->loader);
0131 }
0132 
0133 static void ecard_task_readbytes(struct ecard_request *req)
0134 {
0135     struct expansion_card *ec = req->ec;
0136     unsigned char *buf = req->buffer;
0137     unsigned int len = req->length;
0138     unsigned int off = req->address;
0139 
0140     if (ec->slot_no == 8) {
0141         void __iomem *base = (void __iomem *)
0142                 ec->resource[ECARD_RES_MEMC].start;
0143 
0144         /*
0145          * The card maintains an index which increments the address
0146          * into a 4096-byte page on each access.  We need to keep
0147          * track of the counter.
0148          */
0149         static unsigned int index;
0150         unsigned int page;
0151 
0152         page = (off >> 12) * 4;
0153         if (page > 256 * 4)
0154             return;
0155 
0156         off &= 4095;
0157 
0158         /*
0159          * If we are reading offset 0, or our current index is
0160          * greater than the offset, reset the hardware index counter.
0161          */
0162         if (off == 0 || index > off) {
0163             writeb(0, base);
0164             index = 0;
0165         }
0166 
0167         /*
0168          * Increment the hardware index counter until we get to the
0169          * required offset.  The read bytes are discarded.
0170          */
0171         while (index < off) {
0172             readb(base + page);
0173             index += 1;
0174         }
0175 
0176         while (len--) {
0177             *buf++ = readb(base + page);
0178             index += 1;
0179         }
0180     } else {
0181         unsigned long base = (ec->easi
0182              ? &ec->resource[ECARD_RES_EASI]
0183              : &ec->resource[ECARD_RES_IOCSYNC])->start;
0184         void __iomem *pbase = (void __iomem *)base;
0185 
0186         if (!req->use_loader || !ec->loader) {
0187             off *= 4;
0188             while (len--) {
0189                 *buf++ = readb(pbase + off);
0190                 off += 4;
0191             }
0192         } else {
0193             while(len--) {
0194                 /*
0195                  * The following is required by some
0196                  * expansion card loader programs.
0197                  */
0198                 *(unsigned long *)0x108 = 0;
0199                 *buf++ = ecard_loader_read(off++, base,
0200                                ec->loader);
0201             }
0202         }
0203     }
0204 
0205 }
0206 
0207 static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
0208 static struct ecard_request *ecard_req;
0209 static DEFINE_MUTEX(ecard_mutex);
0210 
0211 /*
0212  * Set up the expansion card daemon's page tables.
0213  */
0214 static void ecard_init_pgtables(struct mm_struct *mm)
0215 {
0216     struct vm_area_struct vma = TLB_FLUSH_VMA(mm, VM_EXEC);
0217 
0218     /* We want to set up the page tables for the following mapping:
0219      *  Virtual Physical
0220      *  0x03000000  0x03000000
0221      *  0x03010000  unmapped
0222      *  0x03210000  0x03210000
0223      *  0x03400000  unmapped
0224      *  0x08000000  0x08000000
0225      *  0x10000000  unmapped
0226      *
0227      * FIXME: we don't follow this 100% yet.
0228      */
0229     pgd_t *src_pgd, *dst_pgd;
0230 
0231     src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
0232     dst_pgd = pgd_offset(mm, IO_START);
0233 
0234     memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
0235 
0236     src_pgd = pgd_offset(mm, (unsigned long)EASI_BASE);
0237     dst_pgd = pgd_offset(mm, EASI_START);
0238 
0239     memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
0240 
0241     flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
0242     flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
0243 }
0244 
0245 static int ecard_init_mm(void)
0246 {
0247     struct mm_struct * mm = mm_alloc();
0248     struct mm_struct *active_mm = current->active_mm;
0249 
0250     if (!mm)
0251         return -ENOMEM;
0252 
0253     current->mm = mm;
0254     current->active_mm = mm;
0255     activate_mm(active_mm, mm);
0256     mmdrop(active_mm);
0257     ecard_init_pgtables(mm);
0258     return 0;
0259 }
0260 
0261 static int
0262 ecard_task(void * unused)
0263 {
0264     /*
0265      * Allocate a mm.  We're not a lazy-TLB kernel task since we need
0266      * to set page table entries where the user space would be.  Note
0267      * that this also creates the page tables.  Failure is not an
0268      * option here.
0269      */
0270     if (ecard_init_mm())
0271         panic("kecardd: unable to alloc mm\n");
0272 
0273     while (1) {
0274         struct ecard_request *req;
0275 
0276         wait_event_interruptible(ecard_wait, ecard_req != NULL);
0277 
0278         req = xchg(&ecard_req, NULL);
0279         if (req != NULL) {
0280             req->fn(req);
0281             complete(req->complete);
0282         }
0283     }
0284 }
0285 
0286 /*
0287  * Wake the expansion card daemon to action our request.
0288  *
0289  * FIXME: The test here is not sufficient to detect if the
0290  * kcardd is running.
0291  */
0292 static void ecard_call(struct ecard_request *req)
0293 {
0294     DECLARE_COMPLETION_ONSTACK(completion);
0295 
0296     req->complete = &completion;
0297 
0298     mutex_lock(&ecard_mutex);
0299     ecard_req = req;
0300     wake_up(&ecard_wait);
0301 
0302     /*
0303      * Now wait for kecardd to run.
0304      */
0305     wait_for_completion(&completion);
0306     mutex_unlock(&ecard_mutex);
0307 }
0308 
0309 /* ======================= Mid-level card control ===================== */
0310 
0311 static void
0312 ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
0313 {
0314     struct ecard_request req;
0315 
0316     req.fn      = ecard_task_readbytes;
0317     req.ec      = ec;
0318     req.address = off;
0319     req.length  = len;
0320     req.use_loader  = useld;
0321     req.buffer  = addr;
0322 
0323     ecard_call(&req);
0324 }
0325 
0326 int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
0327 {
0328     struct ex_chunk_dir excd;
0329     int index = 16;
0330     int useld = 0;
0331 
0332     if (!ec->cid.cd)
0333         return 0;
0334 
0335     while(1) {
0336         ecard_readbytes(&excd, ec, index, 8, useld);
0337         index += 8;
0338         if (c_id(&excd) == 0) {
0339             if (!useld && ec->loader) {
0340                 useld = 1;
0341                 index = 0;
0342                 continue;
0343             }
0344             return 0;
0345         }
0346         if (c_id(&excd) == 0xf0) { /* link */
0347             index = c_start(&excd);
0348             continue;
0349         }
0350         if (c_id(&excd) == 0x80) { /* loader */
0351             if (!ec->loader) {
0352                 ec->loader = kmalloc(c_len(&excd),
0353                                    GFP_KERNEL);
0354                 if (ec->loader)
0355                     ecard_readbytes(ec->loader, ec,
0356                             (int)c_start(&excd),
0357                             c_len(&excd), useld);
0358                 else
0359                     return 0;
0360             }
0361             continue;
0362         }
0363         if (c_id(&excd) == id && num-- == 0)
0364             break;
0365     }
0366 
0367     if (c_id(&excd) & 0x80) {
0368         switch (c_id(&excd) & 0x70) {
0369         case 0x70:
0370             ecard_readbytes((unsigned char *)excd.d.string, ec,
0371                     (int)c_start(&excd), c_len(&excd),
0372                     useld);
0373             break;
0374         case 0x00:
0375             break;
0376         }
0377     }
0378     cd->start_offset = c_start(&excd);
0379     memcpy(cd->d.string, excd.d.string, 256);
0380     return 1;
0381 }
0382 
0383 /* ======================= Interrupt control ============================ */
0384 
0385 static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
0386 {
0387 }
0388 
0389 static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
0390 {
0391 }
0392 
0393 static int ecard_def_irq_pending(ecard_t *ec)
0394 {
0395     return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
0396 }
0397 
0398 static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
0399 {
0400     panic("ecard_def_fiq_enable called - impossible");
0401 }
0402 
0403 static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
0404 {
0405     panic("ecard_def_fiq_disable called - impossible");
0406 }
0407 
0408 static int ecard_def_fiq_pending(ecard_t *ec)
0409 {
0410     return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
0411 }
0412 
0413 static expansioncard_ops_t ecard_default_ops = {
0414     ecard_def_irq_enable,
0415     ecard_def_irq_disable,
0416     ecard_def_irq_pending,
0417     ecard_def_fiq_enable,
0418     ecard_def_fiq_disable,
0419     ecard_def_fiq_pending
0420 };
0421 
0422 /*
0423  * Enable and disable interrupts from expansion cards.
0424  * (interrupts are disabled for these functions).
0425  *
0426  * They are not meant to be called directly, but via enable/disable_irq.
0427  */
0428 static void ecard_irq_unmask(struct irq_data *d)
0429 {
0430     ecard_t *ec = irq_data_get_irq_chip_data(d);
0431 
0432     if (ec) {
0433         if (!ec->ops)
0434             ec->ops = &ecard_default_ops;
0435 
0436         if (ec->claimed && ec->ops->irqenable)
0437             ec->ops->irqenable(ec, d->irq);
0438         else
0439             printk(KERN_ERR "ecard: rejecting request to "
0440                 "enable IRQs for %d\n", d->irq);
0441     }
0442 }
0443 
0444 static void ecard_irq_mask(struct irq_data *d)
0445 {
0446     ecard_t *ec = irq_data_get_irq_chip_data(d);
0447 
0448     if (ec) {
0449         if (!ec->ops)
0450             ec->ops = &ecard_default_ops;
0451 
0452         if (ec->ops && ec->ops->irqdisable)
0453             ec->ops->irqdisable(ec, d->irq);
0454     }
0455 }
0456 
0457 static struct irq_chip ecard_chip = {
0458     .name       = "ECARD",
0459     .irq_ack    = ecard_irq_mask,
0460     .irq_mask   = ecard_irq_mask,
0461     .irq_unmask = ecard_irq_unmask,
0462 };
0463 
0464 void ecard_enablefiq(unsigned int fiqnr)
0465 {
0466     ecard_t *ec = slot_to_ecard(fiqnr);
0467 
0468     if (ec) {
0469         if (!ec->ops)
0470             ec->ops = &ecard_default_ops;
0471 
0472         if (ec->claimed && ec->ops->fiqenable)
0473             ec->ops->fiqenable(ec, fiqnr);
0474         else
0475             printk(KERN_ERR "ecard: rejecting request to "
0476                 "enable FIQs for %d\n", fiqnr);
0477     }
0478 }
0479 
0480 void ecard_disablefiq(unsigned int fiqnr)
0481 {
0482     ecard_t *ec = slot_to_ecard(fiqnr);
0483 
0484     if (ec) {
0485         if (!ec->ops)
0486             ec->ops = &ecard_default_ops;
0487 
0488         if (ec->ops->fiqdisable)
0489             ec->ops->fiqdisable(ec, fiqnr);
0490     }
0491 }
0492 
0493 static void ecard_dump_irq_state(void)
0494 {
0495     ecard_t *ec;
0496 
0497     printk("Expansion card IRQ state:\n");
0498 
0499     for (ec = cards; ec; ec = ec->next) {
0500         const char *claimed;
0501 
0502         if (ec->slot_no == 8)
0503             continue;
0504 
0505         claimed = ec->claimed ? "" : "not ";
0506 
0507         if (ec->ops && ec->ops->irqpending &&
0508             ec->ops != &ecard_default_ops)
0509             printk("  %d: %sclaimed irq %spending\n",
0510                    ec->slot_no, claimed,
0511                    ec->ops->irqpending(ec) ? "" : "not ");
0512         else
0513             printk("  %d: %sclaimed irqaddr %p, mask = %02X, status = %02X\n",
0514                    ec->slot_no, claimed,
0515                    ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
0516     }
0517 }
0518 
0519 static void ecard_check_lockup(struct irq_desc *desc)
0520 {
0521     static unsigned long last;
0522     static int lockup;
0523 
0524     /*
0525      * If the timer interrupt has not run since the last million
0526      * unrecognised expansion card interrupts, then there is
0527      * something seriously wrong.  Disable the expansion card
0528      * interrupts so at least we can continue.
0529      *
0530      * Maybe we ought to start a timer to re-enable them some time
0531      * later?
0532      */
0533     if (last == jiffies) {
0534         lockup += 1;
0535         if (lockup > 1000000) {
0536             printk(KERN_ERR "\nInterrupt lockup detected - "
0537                    "disabling all expansion card interrupts\n");
0538 
0539             desc->irq_data.chip->irq_mask(&desc->irq_data);
0540             ecard_dump_irq_state();
0541         }
0542     } else
0543         lockup = 0;
0544 
0545     /*
0546      * If we did not recognise the source of this interrupt,
0547      * warn the user, but don't flood the user with these messages.
0548      */
0549     if (!last || time_after(jiffies, last + 5*HZ)) {
0550         last = jiffies;
0551         printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
0552         ecard_dump_irq_state();
0553     }
0554 }
0555 
0556 static void ecard_irq_handler(struct irq_desc *desc)
0557 {
0558     ecard_t *ec;
0559     int called = 0;
0560 
0561     desc->irq_data.chip->irq_mask(&desc->irq_data);
0562     for (ec = cards; ec; ec = ec->next) {
0563         int pending;
0564 
0565         if (!ec->claimed || !ec->irq || ec->slot_no == 8)
0566             continue;
0567 
0568         if (ec->ops && ec->ops->irqpending)
0569             pending = ec->ops->irqpending(ec);
0570         else
0571             pending = ecard_default_ops.irqpending(ec);
0572 
0573         if (pending) {
0574             generic_handle_irq(ec->irq);
0575             called ++;
0576         }
0577     }
0578     desc->irq_data.chip->irq_unmask(&desc->irq_data);
0579 
0580     if (called == 0)
0581         ecard_check_lockup(desc);
0582 }
0583 
0584 static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
0585 {
0586     void __iomem *address = NULL;
0587     int slot = ec->slot_no;
0588 
0589     if (ec->slot_no == 8)
0590         return ECARD_MEMC8_BASE;
0591 
0592     ectcr &= ~(1 << slot);
0593 
0594     switch (type) {
0595     case ECARD_MEMC:
0596         if (slot < 4)
0597             address = ECARD_MEMC_BASE + (slot << 14);
0598         break;
0599 
0600     case ECARD_IOC:
0601         if (slot < 4)
0602             address = ECARD_IOC_BASE + (slot << 14);
0603         else
0604             address = ECARD_IOC4_BASE + ((slot - 4) << 14);
0605         if (address)
0606             address += speed << 19;
0607         break;
0608 
0609     case ECARD_EASI:
0610         address = ECARD_EASI_BASE + (slot << 24);
0611         if (speed == ECARD_FAST)
0612             ectcr |= 1 << slot;
0613         break;
0614 
0615     default:
0616         break;
0617     }
0618 
0619 #ifdef IOMD_ECTCR
0620     iomd_writeb(ectcr, IOMD_ECTCR);
0621 #endif
0622     return address;
0623 }
0624 
0625 static int ecard_prints(struct seq_file *m, ecard_t *ec)
0626 {
0627     seq_printf(m, "  %d: %s ", ec->slot_no, ec->easi ? "EASI" : "    ");
0628 
0629     if (ec->cid.id == 0) {
0630         struct in_chunk_dir incd;
0631 
0632         seq_printf(m, "[%04X:%04X] ",
0633             ec->cid.manufacturer, ec->cid.product);
0634 
0635         if (!ec->card_desc && ec->cid.cd &&
0636             ecard_readchunk(&incd, ec, 0xf5, 0)) {
0637             ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
0638 
0639             if (ec->card_desc)
0640                 strcpy((char *)ec->card_desc, incd.d.string);
0641         }
0642 
0643         seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
0644     } else
0645         seq_printf(m, "Simple card %d\n", ec->cid.id);
0646 
0647     return 0;
0648 }
0649 
0650 static int ecard_devices_proc_show(struct seq_file *m, void *v)
0651 {
0652     ecard_t *ec = cards;
0653 
0654     while (ec) {
0655         ecard_prints(m, ec);
0656         ec = ec->next;
0657     }
0658     return 0;
0659 }
0660 
0661 static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
0662 
0663 static void ecard_proc_init(void)
0664 {
0665     proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
0666     proc_create_single("devices", 0, proc_bus_ecard_dir,
0667             ecard_devices_proc_show);
0668 }
0669 
0670 #define ec_set_resource(ec,nr,st,sz)                \
0671     do {                            \
0672         (ec)->resource[nr].name = dev_name(&ec->dev);   \
0673         (ec)->resource[nr].start = st;          \
0674         (ec)->resource[nr].end = (st) + (sz) - 1;   \
0675         (ec)->resource[nr].flags = IORESOURCE_MEM;  \
0676     } while (0)
0677 
0678 static void __init ecard_free_card(struct expansion_card *ec)
0679 {
0680     int i;
0681 
0682     for (i = 0; i < ECARD_NUM_RESOURCES; i++)
0683         if (ec->resource[i].flags)
0684             release_resource(&ec->resource[i]);
0685 
0686     kfree(ec);
0687 }
0688 
0689 static struct expansion_card *__init ecard_alloc_card(int type, int slot)
0690 {
0691     struct expansion_card *ec;
0692     unsigned long base;
0693     int i;
0694 
0695     ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
0696     if (!ec) {
0697         ec = ERR_PTR(-ENOMEM);
0698         goto nomem;
0699     }
0700 
0701     ec->slot_no = slot;
0702     ec->easi = type == ECARD_EASI;
0703     ec->irq = 0;
0704     ec->fiq = 0;
0705     ec->dma = NO_DMA;
0706     ec->ops = &ecard_default_ops;
0707 
0708     dev_set_name(&ec->dev, "ecard%d", slot);
0709     ec->dev.parent = NULL;
0710     ec->dev.bus = &ecard_bus_type;
0711     ec->dev.dma_mask = &ec->dma_mask;
0712     ec->dma_mask = (u64)0xffffffff;
0713     ec->dev.coherent_dma_mask = ec->dma_mask;
0714 
0715     if (slot < 4) {
0716         ec_set_resource(ec, ECARD_RES_MEMC,
0717                 PODSLOT_MEMC_BASE + (slot << 14),
0718                 PODSLOT_MEMC_SIZE);
0719         base = PODSLOT_IOC0_BASE + (slot << 14);
0720     } else
0721         base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
0722 
0723 #ifdef CONFIG_ARCH_RPC
0724     if (slot < 8) {
0725         ec_set_resource(ec, ECARD_RES_EASI,
0726                 PODSLOT_EASI_BASE + (slot << 24),
0727                 PODSLOT_EASI_SIZE);
0728     }
0729 
0730     if (slot == 8) {
0731         ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
0732     } else
0733 #endif
0734 
0735     for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
0736         ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
0737                 base + (i << 19), PODSLOT_IOC_SIZE);
0738 
0739     for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
0740         if (ec->resource[i].flags &&
0741             request_resource(&iomem_resource, &ec->resource[i])) {
0742             dev_err(&ec->dev, "resource(s) not available\n");
0743             ec->resource[i].end -= ec->resource[i].start;
0744             ec->resource[i].start = 0;
0745             ec->resource[i].flags = 0;
0746         }
0747     }
0748 
0749  nomem:
0750     return ec;
0751 }
0752 
0753 static ssize_t irq_show(struct device *dev, struct device_attribute *attr, char *buf)
0754 {
0755     struct expansion_card *ec = ECARD_DEV(dev);
0756     return sprintf(buf, "%u\n", ec->irq);
0757 }
0758 static DEVICE_ATTR_RO(irq);
0759 
0760 static ssize_t dma_show(struct device *dev, struct device_attribute *attr, char *buf)
0761 {
0762     struct expansion_card *ec = ECARD_DEV(dev);
0763     return sprintf(buf, "%u\n", ec->dma);
0764 }
0765 static DEVICE_ATTR_RO(dma);
0766 
0767 static ssize_t resource_show(struct device *dev, struct device_attribute *attr, char *buf)
0768 {
0769     struct expansion_card *ec = ECARD_DEV(dev);
0770     char *str = buf;
0771     int i;
0772 
0773     for (i = 0; i < ECARD_NUM_RESOURCES; i++)
0774         str += sprintf(str, "%08x %08x %08lx\n",
0775                 ec->resource[i].start,
0776                 ec->resource[i].end,
0777                 ec->resource[i].flags);
0778 
0779     return str - buf;
0780 }
0781 static DEVICE_ATTR_RO(resource);
0782 
0783 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
0784 {
0785     struct expansion_card *ec = ECARD_DEV(dev);
0786     return sprintf(buf, "%u\n", ec->cid.manufacturer);
0787 }
0788 static DEVICE_ATTR_RO(vendor);
0789 
0790 static ssize_t device_show(struct device *dev, struct device_attribute *attr, char *buf)
0791 {
0792     struct expansion_card *ec = ECARD_DEV(dev);
0793     return sprintf(buf, "%u\n", ec->cid.product);
0794 }
0795 static DEVICE_ATTR_RO(device);
0796 
0797 static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
0798 {
0799     struct expansion_card *ec = ECARD_DEV(dev);
0800     return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
0801 }
0802 static DEVICE_ATTR_RO(type);
0803 
0804 static struct attribute *ecard_dev_attrs[] = {
0805     &dev_attr_device.attr,
0806     &dev_attr_dma.attr,
0807     &dev_attr_irq.attr,
0808     &dev_attr_resource.attr,
0809     &dev_attr_type.attr,
0810     &dev_attr_vendor.attr,
0811     NULL,
0812 };
0813 ATTRIBUTE_GROUPS(ecard_dev);
0814 
0815 int ecard_request_resources(struct expansion_card *ec)
0816 {
0817     int i, err = 0;
0818 
0819     for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
0820         if (ecard_resource_end(ec, i) &&
0821             !request_mem_region(ecard_resource_start(ec, i),
0822                     ecard_resource_len(ec, i),
0823                     ec->dev.driver->name)) {
0824             err = -EBUSY;
0825             break;
0826         }
0827     }
0828 
0829     if (err) {
0830         while (i--)
0831             if (ecard_resource_end(ec, i))
0832                 release_mem_region(ecard_resource_start(ec, i),
0833                            ecard_resource_len(ec, i));
0834     }
0835     return err;
0836 }
0837 EXPORT_SYMBOL(ecard_request_resources);
0838 
0839 void ecard_release_resources(struct expansion_card *ec)
0840 {
0841     int i;
0842 
0843     for (i = 0; i < ECARD_NUM_RESOURCES; i++)
0844         if (ecard_resource_end(ec, i))
0845             release_mem_region(ecard_resource_start(ec, i),
0846                        ecard_resource_len(ec, i));
0847 }
0848 EXPORT_SYMBOL(ecard_release_resources);
0849 
0850 void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
0851 {
0852     ec->irq_data = irq_data;
0853     barrier();
0854     ec->ops = ops;
0855 }
0856 EXPORT_SYMBOL(ecard_setirq);
0857 
0858 void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
0859                unsigned long offset, unsigned long maxsize)
0860 {
0861     unsigned long start = ecard_resource_start(ec, res);
0862     unsigned long end = ecard_resource_end(ec, res);
0863 
0864     if (offset > (end - start))
0865         return NULL;
0866 
0867     start += offset;
0868     if (maxsize && end - start > maxsize)
0869         end = start + maxsize;
0870     
0871     return devm_ioremap(&ec->dev, start, end - start);
0872 }
0873 EXPORT_SYMBOL(ecardm_iomap);
0874 
0875 static void atomwide_3p_quirk(ecard_t *ec)
0876 {
0877     void __iomem *addr = __ecard_address(ec, ECARD_IOC, ECARD_SYNC);
0878     unsigned int i;
0879 
0880     /* Disable interrupts on each port */
0881     for (i = 0x2000; i <= 0x2800; i += 0x0400)
0882         writeb(0, addr + i + 4);    
0883 }
0884 
0885 /*
0886  * Probe for an expansion card.
0887  *
0888  * If bit 1 of the first byte of the card is set, then the
0889  * card does not exist.
0890  */
0891 static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
0892 {
0893     ecard_t **ecp;
0894     ecard_t *ec;
0895     struct ex_ecid cid;
0896     void __iomem *addr;
0897     int i, rc;
0898 
0899     ec = ecard_alloc_card(type, slot);
0900     if (IS_ERR(ec)) {
0901         rc = PTR_ERR(ec);
0902         goto nomem;
0903     }
0904 
0905     rc = -ENODEV;
0906     if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
0907         goto nodev;
0908 
0909     cid.r_zero = 1;
0910     ecard_readbytes(&cid, ec, 0, 16, 0);
0911     if (cid.r_zero)
0912         goto nodev;
0913 
0914     ec->cid.id  = cid.r_id;
0915     ec->cid.cd  = cid.r_cd;
0916     ec->cid.is  = cid.r_is;
0917     ec->cid.w   = cid.r_w;
0918     ec->cid.manufacturer = ecard_getu16(cid.r_manu);
0919     ec->cid.product = ecard_getu16(cid.r_prod);
0920     ec->cid.country = cid.r_country;
0921     ec->cid.irqmask = cid.r_irqmask;
0922     ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
0923     ec->cid.fiqmask = cid.r_fiqmask;
0924     ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
0925     ec->fiqaddr =
0926     ec->irqaddr = addr;
0927 
0928     if (ec->cid.is) {
0929         ec->irqmask = ec->cid.irqmask;
0930         ec->irqaddr += ec->cid.irqoff;
0931         ec->fiqmask = ec->cid.fiqmask;
0932         ec->fiqaddr += ec->cid.fiqoff;
0933     } else {
0934         ec->irqmask = 1;
0935         ec->fiqmask = 4;
0936     }
0937 
0938     for (i = 0; i < ARRAY_SIZE(quirklist); i++)
0939         if (quirklist[i].manufacturer == ec->cid.manufacturer &&
0940             quirklist[i].product == ec->cid.product) {
0941             if (quirklist[i].type)
0942                 ec->card_desc = quirklist[i].type;
0943             if (quirklist[i].init)
0944                 quirklist[i].init(ec);
0945             break;
0946         }
0947 
0948     ec->irq = irq;
0949 
0950     /*
0951      * hook the interrupt handlers
0952      */
0953     if (slot < 8) {
0954         irq_set_chip_and_handler(ec->irq, &ecard_chip,
0955                      handle_level_irq);
0956         irq_set_chip_data(ec->irq, ec);
0957         irq_clear_status_flags(ec->irq, IRQ_NOREQUEST);
0958     }
0959 
0960 #ifdef CONFIG_ARCH_RPC
0961     /* On RiscPC, only first two slots have DMA capability */
0962     if (slot < 2)
0963         ec->dma = 2 + slot;
0964 #endif
0965 
0966     for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
0967 
0968     *ecp = ec;
0969     slot_to_expcard[slot] = ec;
0970 
0971     rc = device_register(&ec->dev);
0972     if (rc)
0973         goto nodev;
0974 
0975     return 0;
0976 
0977  nodev:
0978     ecard_free_card(ec);
0979  nomem:
0980     return rc;
0981 }
0982 
0983 /*
0984  * Initialise the expansion card system.
0985  * Locate all hardware - interrupt management and
0986  * actual cards.
0987  */
0988 static int __init ecard_init(void)
0989 {
0990     struct task_struct *task;
0991     int slot, irqbase;
0992 
0993     irqbase = irq_alloc_descs(-1, 0, 8, -1);
0994     if (irqbase < 0)
0995         return irqbase;
0996 
0997     task = kthread_run(ecard_task, NULL, "kecardd");
0998     if (IS_ERR(task)) {
0999         printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
1000                PTR_ERR(task));
1001         irq_free_descs(irqbase, 8);
1002         return PTR_ERR(task);
1003     }
1004 
1005     printk("Probing expansion cards\n");
1006 
1007     for (slot = 0; slot < 8; slot ++) {
1008         if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
1009             ecard_probe(slot, irqbase + slot, ECARD_IOC);
1010     }
1011 
1012     ecard_probe(8, 11, ECARD_IOC);
1013 
1014     irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
1015 
1016     ecard_proc_init();
1017 
1018     return 0;
1019 }
1020 
1021 subsys_initcall(ecard_init);
1022 
1023 /*
1024  *  ECARD "bus"
1025  */
1026 static const struct ecard_id *
1027 ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1028 {
1029     int i;
1030 
1031     for (i = 0; ids[i].manufacturer != 65535; i++)
1032         if (ec->cid.manufacturer == ids[i].manufacturer &&
1033             ec->cid.product == ids[i].product)
1034             return ids + i;
1035 
1036     return NULL;
1037 }
1038 
1039 static int ecard_drv_probe(struct device *dev)
1040 {
1041     struct expansion_card *ec = ECARD_DEV(dev);
1042     struct ecard_driver *drv = ECARD_DRV(dev->driver);
1043     const struct ecard_id *id;
1044     int ret;
1045 
1046     id = ecard_match_device(drv->id_table, ec);
1047 
1048     ec->claimed = 1;
1049     ret = drv->probe(ec, id);
1050     if (ret)
1051         ec->claimed = 0;
1052     return ret;
1053 }
1054 
1055 static void ecard_drv_remove(struct device *dev)
1056 {
1057     struct expansion_card *ec = ECARD_DEV(dev);
1058     struct ecard_driver *drv = ECARD_DRV(dev->driver);
1059 
1060     drv->remove(ec);
1061     ec->claimed = 0;
1062 
1063     /*
1064      * Restore the default operations.  We ensure that the
1065      * ops are set before we change the data.
1066      */
1067     ec->ops = &ecard_default_ops;
1068     barrier();
1069     ec->irq_data = NULL;
1070 }
1071 
1072 /*
1073  * Before rebooting, we must make sure that the expansion card is in a
1074  * sensible state, so it can be re-detected.  This means that the first
1075  * page of the ROM must be visible.  We call the expansion cards reset
1076  * handler, if any.
1077  */
1078 static void ecard_drv_shutdown(struct device *dev)
1079 {
1080     struct expansion_card *ec = ECARD_DEV(dev);
1081     struct ecard_driver *drv = ECARD_DRV(dev->driver);
1082     struct ecard_request req;
1083 
1084     if (dev->driver) {
1085         if (drv->shutdown)
1086             drv->shutdown(ec);
1087         ec->claimed = 0;
1088     }
1089 
1090     /*
1091      * If this card has a loader, call the reset handler.
1092      */
1093     if (ec->loader) {
1094         req.fn = ecard_task_reset;
1095         req.ec = ec;
1096         ecard_call(&req);
1097     }
1098 }
1099 
1100 int ecard_register_driver(struct ecard_driver *drv)
1101 {
1102     drv->drv.bus = &ecard_bus_type;
1103 
1104     return driver_register(&drv->drv);
1105 }
1106 
1107 void ecard_remove_driver(struct ecard_driver *drv)
1108 {
1109     driver_unregister(&drv->drv);
1110 }
1111 
1112 static int ecard_match(struct device *_dev, struct device_driver *_drv)
1113 {
1114     struct expansion_card *ec = ECARD_DEV(_dev);
1115     struct ecard_driver *drv = ECARD_DRV(_drv);
1116     int ret;
1117 
1118     if (drv->id_table) {
1119         ret = ecard_match_device(drv->id_table, ec) != NULL;
1120     } else {
1121         ret = ec->cid.id == drv->id;
1122     }
1123 
1124     return ret;
1125 }
1126 
1127 struct bus_type ecard_bus_type = {
1128     .name       = "ecard",
1129     .dev_groups = ecard_dev_groups,
1130     .match      = ecard_match,
1131     .probe      = ecard_drv_probe,
1132     .remove     = ecard_drv_remove,
1133     .shutdown   = ecard_drv_shutdown,
1134 };
1135 
1136 static int ecard_bus_init(void)
1137 {
1138     return bus_register(&ecard_bus_type);
1139 }
1140 
1141 postcore_initcall(ecard_bus_init);
1142 
1143 EXPORT_SYMBOL(ecard_readchunk);
1144 EXPORT_SYMBOL(ecard_register_driver);
1145 EXPORT_SYMBOL(ecard_remove_driver);
1146 EXPORT_SYMBOL(ecard_bus_type);