📄 iflash2_mtd.c
字号:
/*====================================================================== A simple MTD for Intel Series 2 and Series 100 Flash devices iflash2_mtd.c 1.65 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/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/cisreg.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(erase_timeout, 100); /* in ms */INT_MODULE_PARM(erase_limit, 10000); /* in ms */INT_MODULE_PARM(retry_limit, 4); /* write retries */INT_MODULE_PARM(max_tries, 4096); /* status polling */#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.65 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; u_int vpp; 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);}/*====================================================================== Low level routines for programming the flash card. ======================================================================*/static void abort_cmd(dev_link_t *link, volatile caddr_t base, int cell, mtd_mod_win_t *old){ u_char *addr = base + CISREG_SLEEP + ((cell>>3)<<1); /* Map the CIS register page into memory */ mtd_mod_win_t mod = { WIN_MEMORY_TYPE_AM, 250, 0x4000 }; DEBUG(1, "iflash2_mtd: abort\n"); MTDHelperEntry(MTDModifyWindow, link->win, &mod); /* Put the cell to sleep, then wake it up again */ writeb(readb(addr) | (1 << (cell & 7)), addr); udelay(10L); writeb(readb(addr) & ~(1 << (cell & 7)), addr); udelay(10L); /* Restore the original memory window mapping */ MTDHelperEntry(MTDModifyWindow, link->win, old);}static inline int byte_write(volatile u_char *address, u_char data){ register u_char CSR = 0; register u_short i; for (i = 0; i < max_tries; i++) { CSR = readb(address); if (CSR & LOW(CSR_WR_READY)) { writeb(LOW(IF_WRITE), address); writeb(data, address); return CS_SUCCESS; } } printk(KERN_NOTICE "iflash2_mtd: byte_write timed out at 0x%p, " "CSR = 0x%x\n", address, CSR); return CS_WRITE_FAILURE;}static inline int word_write(volatile u_short *address, u_short data){ register u_short CSR = 0, i; for (i = 0; i < max_tries; i++) { CSR = readw(address); if ((CSR & CSR_WR_READY) == CSR_WR_READY) { writew(IF_WRITE, address); writew(data, address); return CS_SUCCESS; } } printk(KERN_NOTICE "iflash2_mtd: word_write timed out at 0x%p, " "CSR = 0x%x\n", address, CSR); return CS_WRITE_FAILURE;}static int check_write(volatile u_short *address){ u_short CSR = 0, i; writew(IF_READ_CSR, address); for (i = 0; i < max_tries; i++) { CSR = readw(address); if ((CSR & CSR_WR_READY) == CSR_WR_READY) break; } if (i == max_tries) { printk(KERN_NOTICE "iflash2_mtd: check_write timed out!" " CSR = 0x%x\n", CSR); return CS_GENERAL_FAILURE; } if (CSR & (CSR_WR_ERR | CSR_VPP_LOW)) { printk(KERN_NOTICE "iflash2_mtd: write error: CSR = 0x%x\n", CSR); return CS_WRITE_FAILURE; } else 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)) { printk(KERN_NOTICE "iflash2_mtd: erase failed: CSR = 0x%x\n", CSR); return CS_WRITE_FAILURE; } else return CS_SUCCESS;}static int suspend_erase(volatile u_short *address){ u_short CSR = 0; u_int i; writew(IF_ERASE_SUSPEND, address); writew(IF_READ_CSR, address); for (i = 0; i < max_tries; i++) { CSR = readw(address); if ((CSR & CSR_WR_READY) == CSR_WR_READY) break; } if (i == max_tries) { printk(KERN_NOTICE "iflash2_mtd: suspend_erase timed out: " "CSR = 0x%x\n", CSR); return CS_GENERAL_FAILURE; } writew(IF_READ_ARRAY, address); return CS_SUCCESS;}static void resume_erase(volatile u_short *address){ u_short CSR; writew(IF_READ_CSR, address); CSR = readw(address); /* Only give resume signal if the erase is really suspended */ if (CSR & CSR_ERA_SUSPEND) writew(IF_CONFIRM, address);}static void reset_block(volatile u_short *address){ u_short CSR, i; writew(IF_CLEAR_CSR, address); for (i = 0; i < 100; i++) { writew(IF_READ_CSR, address); CSR = readw(address); if (CSR != 0xffff) break; mdelay(1); }#ifdef PCMCIA_DEBUG if (i) printk(KERN_DEBUG "iflash2_mtd: reset after %d iterations\n", i);#endif writew(IF_READ_ARRAY, address);}/*====================================================================== 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. A handler should call vpp_shutdown() once for each request that does a vpp_setup(). ======================================================================*/static int vpp_setup(dev_link_t *link, mtd_request_t *req){ flash_dev_t *dev = (flash_dev_t *)link->priv; mtd_vpp_req_t vpp_req; /* First, do we need to do this? */ if (!dev->vpp) return 0; /* First time for this request? */ if (!(req->Function & MTD_REQ_TIMEOUT)) { /* If no other users, kill shutdown timer and apply power */ if (++dev->vpp_usage == 1) { if (!del_timer(&dev->vpp_timeout)) { DEBUG(1, "iflash2_mtd: raising Vpp...\n"); dev->vpp_start = jiffies; vpp_req.Vpp1 = vpp_req.Vpp2 = dev->vpp; MTDHelperEntry(MTDSetVpp, link->handle, &vpp_req); } } } /* Wait for Vpp to settle if it was just applied */ if (jiffies < dev->vpp_start + vpp_settle) { req->Status = MTD_WAITTIMER; req->Timeout = vpp_settle * 1000 / HZ; return 1; } return 0;}static void vpp_off(u_long arg){ dev_link_t *link = (dev_link_t *)arg; flash_dev_t *dev = (flash_dev_t *)link->priv; mtd_vpp_req_t req; if (!dev->vpp) return; DEBUG(1, "iflash2_mtd: lowering Vpp...\n"); dev->vpp_timeout.expires = 0; req.Vpp1 = req.Vpp2 = 0; MTDHelperEntry(MTDSetVpp, link->handle, &req);}static void vpp_shutdown(dev_link_t *link){ flash_dev_t *dev = (flash_dev_t *)link->priv; if (!dev->vpp) return; dev->vpp_usage--; if (dev->vpp_usage == 0) { dev->vpp_timeout.expires = jiffies + vpp_timeout_period; add_timer(&dev->vpp_timeout); }}/*====================================================================== flash_attach() creates an "instance" of the driver, allocating local data structures for one device. The device is registered with Card Services.======================================================================*/static dev_link_t *flash_attach(void){ client_reg_t client_reg; dev_link_t *link; flash_dev_t *dev; int ret; DEBUG(0, "iflash2_mtd: flash_attach()\n"); /* Create new memory card device */ dev = kmalloc(sizeof(*dev), GFP_KERNEL); if (!dev) return NULL; memset(dev, 0, sizeof(*dev)); link = &dev->link; link->priv = dev; link->release.function = &flash_release; link->release.data = (u_long)link; dev->vpp_timeout.function = vpp_off; dev->vpp_timeout.data = (u_long)link; /* Register with Card Services */ link->next = dev_list; dev_list = link; client_reg.dev_info = &dev_info; client_reg.Attributes = INFO_MTD_CLIENT | INFO_CARD_SHARE; client_reg.EventMask = CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET | CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME; client_reg.event_handler = &flash_event; client_reg.Version = 0x0210; client_reg.event_callback_args.client_data = link; ret = CardServices(RegisterClient, &link->handle, &client_reg); if (ret != 0) { cs_error(link->handle, RegisterClient, ret); flash_detach(link); return NULL; } return link;} /* flash_attach *//*====================================================================== This deletes a driver "instance". The device is de-registered with Card Services. If it has been released, all local data structures are freed. Otherwise, the structures will be freed when the device is released.======================================================================*/static void flash_detach(dev_link_t *link){ dev_link_t **linkp; int ret; DEBUG(0, "iflash2_mtd: flash_detach(0x%p)\n", link); /* Locate device structure */ for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) if (*linkp == link) break; if (*linkp == NULL) return; del_timer(&link->release); if (link->state & DEV_CONFIG) flash_release((u_long)link); if (link->handle) { ret = CardServices(DeregisterClient, link->handle); if (ret != CS_SUCCESS) cs_error(link->handle, DeregisterClient, ret); } /* Unlink device structure, free bits */ *linkp = link->next; kfree(link->priv); } /* flash_detach *//*====================================================================== flash_config() is scheduled to run after a CARD_INSERTION event is received, to bind the MTD to appropriate memory regions. ======================================================================*/static void printk_size(u_int sz){ if (sz & 0x3ff) printk("%u bytes", sz); else if (sz & 0x0fffff) printk("%u kb", sz >> 10); else printk("%u mb", sz >> 20);}static void flash_config(dev_link_t *link){ client_handle_t handle = link->handle; flash_dev_t *dev = link->priv; win_req_t req; mtd_reg_t reg; region_info_t region; int i, attr, ret; DEBUG(0, "iflash2_mtd: flash_config(0x%p)\n", link); /* Allocate a small memory window */ if (word_width) req.Attributes = WIN_DATA_WIDTH_16; else req.Attributes = WIN_DATA_WIDTH_8; req.Base = req.Size = 0; req.AccessSpeed = mem_speed; link->win = (window_handle_t)handle; ret = MTDHelperEntry(MTDRequestWindow, &link->win, &req); if (ret != 0) { cs_error(handle, RequestWindow, ret); link->state &= ~DEV_CONFIG_PENDING; flash_release((u_long)link); return; } dev->Base = ioremap(req.Base, req.Size); dev->Size = req.Size; link->state |= DEV_CONFIG; /* Grab info for all the memory regions we can access */ i = 0; for (attr = 0; attr < 2; attr++) { region.Attributes = attr ? REGION_TYPE_AM : REGION_TYPE_CM; ret = CardServices(GetFirstRegion, handle, ®ion); while (ret == CS_SUCCESS) { reg.Attributes = region.Attributes; reg.Offset = region.CardOffset; dev->flash[i] = kmalloc(sizeof(struct flash_region_t), GFP_KERNEL); if (!dev->flash[i]) break; reg.MediaID = (u_long)dev->flash[i]; ret = CardServices(RegisterMTD, handle, ®); if (ret != 0) { kfree(dev->flash[i]); break; } printk(KERN_INFO "iflash2_mtd: %s at 0x%x, ", attr ? "attr" : "common", region.CardOffset); printk_size(region.RegionSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -