📄 ecard.c
字号:
/* * linux/arch/arm/kernel/ecard.c * * Copyright 1995-2001 Russell King * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Find all installed expansion cards, and handle interrupts from them. * * Created from information from Acorns RiscOS3 PRMs * * 08-Dec-1996 RMK Added code for the 9'th expansion card - the ether * podule slot. * 06-May-1997 RMK Added blacklist for cards whose loader doesn't work. * 12-Sep-1997 RMK Created new handling of interrupt enables/disables * - cards can now register their own routine to control * interrupts (recommended). * 29-Sep-1997 RMK Expansion card interrupt hardware not being re-enabled * on reset from Linux. (Caused cards not to respond * under RiscOS without hard reset). * 15-Feb-1998 RMK Added DMA support * 12-Sep-1998 RMK Added EASI support * 10-Jan-1999 RMK Run loaders in a simulated RISC OS environment. * 17-Apr-1999 RMK Support for EASI Type C cycles. */#define ECARD_C#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/interrupt.h>#include <linux/reboot.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/proc_fs.h>#include <linux/notifier.h>#include <linux/init.h>#include <asm/dma.h>#include <asm/ecard.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/pgalloc.h>#include <asm/mmu_context.h>#include <asm/mach/irq.h>#ifndef CONFIG_ARCH_RPC#define HAVE_EXPMASK#endifenum req { req_readbytes, req_reset_all};struct ecard_request { enum req req; ecard_t *ec; unsigned int address; unsigned int length; unsigned int use_loader; void *buffer;};struct expcard_blacklist { unsigned short manufacturer; unsigned short product; const char *type;};static ecard_t *cards;static ecard_t *slot_to_expcard[MAX_ECARDS];static unsigned int ectcr;#ifdef HAS_EXPMASKstatic unsigned int have_expmask;#endif/* List of descriptions of cards which don't have an extended * identification, or chunk directories containing a description. */static struct expcard_blacklist __initdata blacklist[] = { { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }};asmlinkage extern intecard_loader_reset(volatile unsigned char *pa, loader_t loader);asmlinkage extern intecard_loader_read(int off, volatile unsigned char *pa, loader_t loader);extern int setup_arm_irq(int, struct irqaction *);extern void do_ecard_IRQ(int, struct pt_regs *);static voidecard_irq_noexpmask(int intr_no, void *dev_id, struct pt_regs *regs);static struct irqaction irqexpansioncard = { ecard_irq_noexpmask, SA_INTERRUPT, 0, "expansion cards", NULL, NULL};static inline unsigned shortecard_getu16(unsigned char *v){ return v[0] | v[1] << 8;}static inline signed longecard_gets24(unsigned char *v){ return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);}static inline ecard_t *slot_to_ecard(unsigned int slot){ return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;}/* ===================== Expansion card daemon ======================== *//* * Since the loader programs on the expansion cards need to be run * in a specific environment, create a separate task with this * environment up, and pass requests to this task as and when we * need to. * * This should allow 99% of loaders to be called from Linux. * * From a security standpoint, we trust the card vendors. This * may be a misplaced trust. */#define BUS_ADDR(x) ((((unsigned long)(x)) << 2) + IO_BASE)#define POD_INT_ADDR(x) ((volatile unsigned char *)\ ((BUS_ADDR((x)) - IO_BASE) + IO_START))static inline void ecard_task_reset(void){ ecard_t *ec; for (ec = cards; ec; ec = ec->next) if (ec->loader) ecard_loader_reset(POD_INT_ADDR(ec->podaddr), ec->loader);}static voidecard_task_readbytes(struct ecard_request *req){ unsigned char *buf = (unsigned char *)req->buffer; volatile unsigned char *base_addr = (volatile unsigned char *)POD_INT_ADDR(req->ec->podaddr); unsigned int len = req->length; unsigned int off = req->address; if (req->ec->slot_no == 8) { /* * The card maintains an index which increments the address * into a 4096-byte page on each access. We need to keep * track of the counter. */ static unsigned int index; unsigned int page; page = (off >> 12) * 4; if (page > 256 * 4) return; off &= 4095; /* * If we are reading offset 0, or our current index is * greater than the offset, reset the hardware index counter. */ if (off == 0 || index > off) { *base_addr = 0; index = 0; } /* * Increment the hardware index counter until we get to the * required offset. The read bytes are discarded. */ while (index < off) { unsigned char byte; byte = base_addr[page]; index += 1; } while (len--) { *buf++ = base_addr[page]; index += 1; } } else { if (!req->use_loader || !req->ec->loader) { off *= 4; while (len--) { *buf++ = base_addr[off]; off += 4; } } else { while(len--) { /* * The following is required by some * expansion card loader programs. */ *(unsigned long *)0x108 = 0; *buf++ = ecard_loader_read(off++, base_addr, req->ec->loader); } } }}static void ecard_do_request(struct ecard_request *req){ switch (req->req) { case req_readbytes: ecard_task_readbytes(req); break; case req_reset_all: ecard_task_reset(); break; }}#ifdef CONFIG_CPU_32#include <linux/completion.h>static pid_t ecard_pid;static wait_queue_head_t ecard_wait;static struct ecard_request *ecard_req;static DECLARE_COMPLETION(ecard_completion);/* * Set up the expansion card daemon's page tables. */static void ecard_init_pgtables(struct mm_struct *mm){ /* We want to set up the page tables for the following mapping: * Virtual Physical * 0x03000000 0x03000000 * 0x03010000 unmapped * 0x03210000 0x03210000 * 0x03400000 unmapped * 0x08000000 0x08000000 * 0x10000000 unmapped * * FIXME: we don't follow this 100% yet. */ pgd_t *src_pgd, *dst_pgd; unsigned int dst_addr = IO_START; src_pgd = pgd_offset(mm, IO_BASE); dst_pgd = pgd_offset(mm, dst_addr); while (dst_addr < IO_START + IO_SIZE) { *dst_pgd++ = *src_pgd++; dst_addr += PGDIR_SIZE; } dst_addr = EASI_START; src_pgd = pgd_offset(mm, EASI_BASE); dst_pgd = pgd_offset(mm, dst_addr); while (dst_addr < EASI_START + EASI_SIZE) { *dst_pgd++ = *src_pgd++; dst_addr += PGDIR_SIZE; } flush_tlb_range(mm, IO_START, IO_START + IO_SIZE); flush_tlb_range(mm, EASI_START, EASI_START + EASI_SIZE);}static int ecard_init_mm(void){ struct mm_struct * mm = mm_alloc(); struct mm_struct *active_mm = current->active_mm; if (!mm) return -ENOMEM; current->mm = mm; current->active_mm = mm; activate_mm(active_mm, mm); mmdrop(active_mm); ecard_init_pgtables(mm); return 0;}static intecard_task(void * unused){ struct task_struct *tsk = current; /* * We don't want /any/ signals, not even SIGKILL */ sigfillset(&tsk->blocked); sigemptyset(&tsk->pending.signal); recalc_sigpending(tsk); strcpy(tsk->comm, "kecardd"); daemonize(); /* * Allocate a mm. We're not a lazy-TLB kernel task since we need * to set page table entries where the user space would be. Note * that this also creates the page tables. Failure is not an * option here. */ if (ecard_init_mm()) panic("kecardd: unable to alloc mm\n"); while (1) { struct ecard_request *req; do { req = xchg(&ecard_req, NULL); if (req == NULL) { sigemptyset(&tsk->pending.signal); interruptible_sleep_on(&ecard_wait); } } while (req == NULL); ecard_do_request(req); complete(&ecard_completion); }}/* * Wake the expansion card daemon to action our request. * * FIXME: The test here is not sufficient to detect if the * kcardd is running. */static voidecard_call(struct ecard_request *req){ /* * Make sure we have a context that is able to sleep. */ if (current == &init_task || in_interrupt()) BUG(); if (ecard_pid <= 0) ecard_pid = kernel_thread(ecard_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); ecard_req = req; wake_up(&ecard_wait); /* * Now wait for kecardd to run. */ wait_for_completion(&ecard_completion);}#else/* * On 26-bit processors, we don't need the kcardd thread to access the * expansion card loaders. We do it directly. */#define ecard_call(req) ecard_do_request(req)#endif/* ======================= Mid-level card control ===================== *//* * This function is responsible for resetting the expansion cards to a * sensible state immediately prior to rebooting the system. This function * has process state (keventd), so we can sleep. * * Possible "val" values here: * SYS_RESTART - restarting system * SYS_HALT - halting system * SYS_POWER_OFF - powering down system * * We ignore all calls, unless it is a SYS_RESTART call - power down/halts * will be followed by a SYS_RESTART if ctrl-alt-del is pressed again. */static int ecard_reboot(struct notifier_block *me, unsigned long val, void *v){ struct ecard_request req; if (val != SYS_RESTART) return 0; /* * Disable the expansion card interrupt */ disable_irq(IRQ_EXPANSIONCARD); /* * If we have any expansion card loader code which will handle * the reset for us, call it now. */ req.req = req_reset_all; ecard_call(&req); /* * Disable the expansion card interrupt again, just to be sure. */ disable_irq(IRQ_EXPANSIONCARD); /* * Finally, reset the expansion card interrupt mask to * all enable (RISC OS doesn't set this) */#ifdef HAS_EXPMASK have_expmask = ~0; __raw_writeb(have_expmask, EXPMASK_ENABLE);#endif return 0;}static struct notifier_block ecard_reboot_notifier = { notifier_call: ecard_reboot,};static voidecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld){ struct ecard_request req; req.req = req_readbytes; req.ec = ec; req.address = off; req.length = len; req.use_loader = useld; req.buffer = addr; ecard_call(&req);}int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num){ struct ex_chunk_dir excd; int index = 16; int useld = 0; if (!ec->cid.cd) return 0; while(1) { ecard_readbytes(&excd, ec, index, 8, useld); index += 8; if (c_id(&excd) == 0) { if (!useld && ec->loader) { useld = 1; index = 0; continue; } return 0; } if (c_id(&excd) == 0xf0) { /* link */ index = c_start(&excd); continue; } if (c_id(&excd) == 0x80) { /* loader */ if (!ec->loader) { ec->loader = (loader_t)kmalloc(c_len(&excd), GFP_KERNEL); if (ec->loader) ecard_readbytes(ec->loader, ec, (int)c_start(&excd), c_len(&excd), useld); else return 0; } continue; } if (c_id(&excd) == id && num-- == 0) break; } if (c_id(&excd) & 0x80) { switch (c_id(&excd) & 0x70) { case 0x70: ecard_readbytes((unsigned char *)excd.d.string, ec, (int)c_start(&excd), c_len(&excd), useld); break; case 0x00: break; } } cd->start_offset = c_start(&excd); memcpy(cd->d.string, excd.d.string, 256); return 1;}/* ======================= Interrupt control ============================ */static void ecard_def_irq_enable(ecard_t *ec, int irqnr){#ifdef HAS_EXPMASK if (irqnr < 4 && have_expmask) { have_expmask |= 1 << irqnr; __raw_writeb(have_expmask, EXPMASK_ENABLE); }#endif}static void ecard_def_irq_disable(ecard_t *ec, int irqnr){#ifdef HAS_EXPMASK if (irqnr < 4 && have_expmask) { have_expmask &= ~(1 << irqnr); __raw_writeb(have_expmask, EXPMASK_ENABLE); }#endif}static int ecard_def_irq_pending(ecard_t *ec){ return !ec->irqmask || ec->irqaddr[0] & ec->irqmask;}static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr){ panic("ecard_def_fiq_enable called - impossible");}static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr){ panic("ecard_def_fiq_disable called - impossible");}static int ecard_def_fiq_pending(ecard_t *ec){ return !ec->fiqmask || ec->fiqaddr[0] & ec->fiqmask;}static expansioncard_ops_t ecard_default_ops = { ecard_def_irq_enable, ecard_def_irq_disable, ecard_def_irq_pending, ecard_def_fiq_enable, ecard_def_fiq_disable, ecard_def_fiq_pending};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -