📄 iflash2+_mtd.c
字号:
/*====================================================================== A simple MTD for Intel Series 2+ Flash devices iflash2+_mtd.c 1.75 2002/06/29 06:27:37 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The initial developer of the original code is David A. Hinds <dahinds@users.sourceforge.net>. Portions created by David A. Hinds are Copyright (C) 1999 David A. Hinds. All Rights Reserved. Alternatively, the contents of this file may be used under the terms of the GNU General Public License version 2 (the "GPL"), in which case the provisions of the GPL are applicable instead of the above. If you wish to allow the use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. For efficiency and simplicity, this driver is very block oriented. Reads and writes must not span erase block boundaries. Erases are limited to one erase block per request. This makes it much easier to manage multiple asynchronous erases efficiently. ======================================================================*/#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/ptrace.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/timex.h>#include <linux/major.h>#include <linux/fs.h>#include <linux/delay.h>#include <asm/io.h>#include <asm/system.h>#include <stdarg.h>#include <pcmcia/version.h>#include <pcmcia/cs_types.h>#include <pcmcia/cs.h>#include <pcmcia/bulkmem.h>#include <pcmcia/cistpl.h>#include <pcmcia/ds.h>#include <pcmcia/mem_op.h>#include "iflash.h"/*====================================================================*//* Module parameters */MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");MODULE_DESCRIPTION("Intel Series 2+ flash PCMCIA MTD driver");MODULE_LICENSE("Dual MPL/GPL");#define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")INT_MODULE_PARM(word_width, 1); /* 1 = 16-bit */INT_MODULE_PARM(mem_speed, 0); /* in ns */INT_MODULE_PARM(vpp_timeout_period, 1000); /* in ms */INT_MODULE_PARM(vpp_settle, 100); /* in ms */INT_MODULE_PARM(write_timeout, 100); /* in ms */INT_MODULE_PARM(erase_timeout, 100); /* in ms */INT_MODULE_PARM(erase_limit, 10000); /* in ms */INT_MODULE_PARM(retry_limit, 8); /* write retries */INT_MODULE_PARM(max_tries, 4096); /* status polling */INT_MODULE_PARM(do_sleep, 1); /* spin vs sleep? */#ifdef PCMCIA_DEBUGINT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);#define DEBUG(n, args...) do { if (pc_debug>(n)) printk(KERN_INFO args); } while (0)static char *version ="iflash2+_mtd.c 1.75 2002/06/29 06:27:37 (David Hinds)";#else#define DEBUG(n, args...) do { } while (0)#endif/*====================================================================*/static void flash_config(dev_link_t *link);static void flash_release(u_long arg);static int flash_event(event_t event, int priority, event_callback_args_t *args);static dev_link_t *flash_attach(void);static void flash_detach(dev_link_t *);#define MAX_CELLS 32/* A flash region is composed of one or more "cells", where we allow simultaneous erases if they are in different cells */typedef struct flash_region_t { region_info_t region; u_int cell_size; struct flash_cell_t { u_int state; u_long erase_time; u_int erase_addr; u_int erase_retries; } cell[MAX_CELLS];} flash_region_t;typedef struct flash_dev_t { dev_link_t link; caddr_t Base; u_int Size; window_handle_t ESRwin; caddr_t ESRbase; int vpp_usage; u_long vpp_start; struct timer_list vpp_timeout; flash_region_t *flash[2*CISTPL_MAX_DEVICES];} flash_dev_t;#define FLASH_PENDING 0x01#define FLASH_ERASING 0x02#define FLASH_ERASE_SUSPEND 0x04static dev_info_t dev_info = "iflash2+_mtd";static dev_link_t *dev_list = NULL;/*====================================================================*/static void cs_error(client_handle_t handle, int func, int ret){ error_info_t err = { func, ret }; CardServices(ReportError, handle, &err);}#ifdef BENCHMARKstatic inline u_long uticks(void){ u_long count; outb_p(0x00, 0x43); count = inb_p(0x40); count |= inb(0x40) << 8; count = ((LATCH-1) - count) * 10000; count = (count + LATCH/2) / LATCH; count += jiffies * 10000; return count;}#endif/*====================================================================== Low level routines for programming the flash card. ======================================================================*/static void sleep_or_spin(wait_queue_head_t *queue){ if (do_sleep) interruptible_sleep_on_timeout(queue, 1); else udelay(50);}static void log_esr(char *s, volatile u_short *esr){ u_short CSR, GSR, BSR; writew(IF_READ_CSR, esr); CSR = readw(esr); writew(IF_READ_ESR, esr); BSR = readw(esr+2); GSR = readw(esr+4); printk("%sCSR = 0x%04x, BSR = 0x%04x, GSR = 0x%04x\n", (s ? s : KERN_NOTICE), CSR, BSR, GSR);}static void abort_cmd(volatile u_short *esr){ u_short i; writew(IF_ABORT, esr); writew(IF_READ_ESR, esr); for (i = 0; i < max_tries; i++) if ((readw(esr+4) & GSR_SLEEP) == GSR_SLEEP) break; if (i == max_tries) printk(KERN_NOTICE "iflash2+_mtd: abort cmd failed!\n"); writew(IF_READ_ARRAY, esr); writew(IF_CLEAR_CSR, esr);}static int set_rdy_mode(volatile u_short *esr, u_short mode){ u_short i, j; DEBUG(3, "iflash2+_mtd: set_rdy_mode(%04x)\n", mode); for (j = 0; j < retry_limit; j++) { writew(IF_READ_ESR, esr); for (i = 0; i < max_tries; i++) if (!(readw(esr+4) & GSR_QUEUE_FULL)) break; if (i == max_tries) goto failed; writew(IF_RDY_MODE, esr); writew(mode, esr); writew(IF_READ_ESR, esr); for (i = 0; i < max_tries; i++) if ((readw(esr+4) & GSR_WR_READY) == GSR_WR_READY) break; if (i == max_tries) goto failed; if (!(readw(esr+4) & GSR_OP_ERR)) return CS_SUCCESS; writew(IF_READ_ARRAY, esr); writew(IF_CLEAR_CSR, esr); }failed: printk(KERN_NOTICE "iflash2+_mtd: set_rdy_mode failed!\n"); log_esr(NULL, esr); return CS_GENERAL_FAILURE;}static int check_write(wait_queue_head_t *queue, volatile u_short *esr){ u_long end = jiffies + write_timeout; writew(IF_READ_ESR, esr); while (((readw(esr+2) & BSR_READY) != BSR_READY) && (jiffies < end)) sleep_or_spin(queue); if ((readw(esr+2) & BSR_READY) != BSR_READY) { printk(KERN_NOTICE "iflash2+_mtd: check_write: timed out!\n"); log_esr(NULL, esr); return CS_GENERAL_FAILURE; } if (readw(esr+2) & BSR_FAILED) { log_esr(KERN_DEBUG "write error: ", esr); return CS_WRITE_FAILURE; } else return CS_SUCCESS;}static int page_setup(wait_queue_head_t *queue, volatile u_short *esr, volatile u_short *address, u_short count){ u_long end = jiffies + write_timeout; u_short nw; writew(IF_READ_ESR, esr); while (((readw(esr+4) & GSR_PAGE_AVAIL) != GSR_PAGE_AVAIL) && (jiffies < end)) sleep_or_spin(queue); if ((readw(esr+4) & GSR_PAGE_AVAIL) != GSR_PAGE_AVAIL) { printk(KERN_NOTICE "iflash2+_mtd: page_setup timed out\n"); log_esr(NULL, esr); return CS_GENERAL_FAILURE; } nw = count >> 1; if (nw == 0) { /* Special case of single byte write */ writeb(LOW(IF_SINGLE_LOAD), address); } else { writew(IF_SEQ_LOAD, address); /* Tricky: split count into low bytes and high bytes */ nw = (count-nw-1) | ((nw-1) << 8); if ((u_long)address & 2) { writew(0, address); writew(nw, address); } else { writew(nw, address); writew(0, address); } } return CS_SUCCESS;}static int page_write(volatile u_short *esr, volatile u_short *address, u_short count){ u_short nw; u_short i; writew(IF_READ_ESR, esr); for (i = 0; i < max_tries; i++) if (!(readw(esr+2) & BSR_QUEUE_FULL)) break; if (i == max_tries) { printk(KERN_NOTICE "iflash2+_mtd: page_write timed out\n"); log_esr(NULL, esr); return CS_GENERAL_FAILURE; } nw = count >> 1; if (nw == 0) { writeb(LOW(IF_PAGE_WRITE), address); writeb(0, address); writeb(0, address); } else { writew(IF_PAGE_WRITE, address); /* Tricky: split count into low bytes and high bytes */ nw = (count-nw-1) | ((nw-1) << 8); if ((u_long)address & 2) { writew(0, address); writew(nw, address); } else { writew(nw, address); writew(0, address); } } return CS_SUCCESS;}static void block_erase(volatile u_short *address){ writew(IF_BLOCK_ERASE, address); writew(IF_CONFIRM, address);}static int check_erase(volatile u_short *address){ u_short CSR; writew(IF_READ_CSR, address); CSR = readw(address); if ((CSR & CSR_WR_READY) != CSR_WR_READY) { return CS_BUSY; } else if (CSR & (CSR_ERA_ERR | CSR_VPP_LOW | CSR_WR_ERR)) { log_esr(KERN_NOTICE "erase error: ", address); return CS_WRITE_FAILURE; } else return CS_SUCCESS;}static int suspend_erase(volatile u_short *esr){ u_short i; DEBUG(1, "iflash2+_mtd: suspending erase...\n"); writew(IF_ERASE_SUSPEND, esr); writew(IF_READ_ESR, esr); for (i = 0; i < max_tries; i++) if ((readw(esr+4) & GSR_WR_READY) == GSR_WR_READY) break; if (i == max_tries) { printk(KERN_NOTICE "iflash2+_mtd: suspend_erase timed out\n"); log_esr(NULL, esr); return CS_GENERAL_FAILURE; } writew(IF_READ_ARRAY, esr); return CS_SUCCESS;}static void resume_erase(volatile u_short *esr){ DEBUG(1, "iflash2+_mtd: resuming erase...\n"); writew(IF_READ_ESR, esr); /* Only give resume signal if the erase is really suspended */ if (readw(esr+4) & GSR_OP_SUSPEND) writew(IF_CONFIRM, esr);}static void reset_block(volatile u_short *esr){ writew(IF_READ_ARRAY, esr); writew(IF_CLEAR_CSR, esr);}/*====================================================================*/static void set_global_lock(window_handle_t win, volatile caddr_t base, int set){ mtd_mod_win_t mod; mod.Attributes = WIN_MEMORY_TYPE_AM; mod.AccessSpeed = 250; mod.CardOffset = 0x4000; MTDHelperEntry(MTDModifyWindow, win, &mod); writeb((set) ? WP_BLKEN : 0, base+CISREG_WP);}/*====================================================================== Vpp management functions. The vpp_setup() function checks to see if Vpp is available for the specified device. If not, it turns on Vpp. The vpp_shutdown() function is scheduled to turn Vpp off after an interval of inactivity. vpp_setup() assumes that it will be called at the top of a request handler, and that it can use the MTD_REQ_TIMEOUT flag to tell if it has already been called for this particular request, so that it can count Vpp users.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -