⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsflash.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * drivers/sbus/char/jsflash.c * *  Copyright (C) 1991, 1992  Linus Torvalds	(drivers/char/mem.c) *  Copyright (C) 1997  Eddie C. Dost		(drivers/sbus/char/flash.c) *  Copyright (C) 1997-2000 Pavel Machek <pavel@ucw.cz>   (drivers/block/nbd.c) *  Copyright (C) 1999-2000 Pete Zaitcev * * This driver is used to program OS into a Flash SIMM on * Krups and Espresso platforms. * * TODO: do not allow erase/programming if file systems are mounted. * TODO: Erase/program both banks of a 8MB SIMM. * * It is anticipated that programming an OS Flash will be a routine * procedure. In the same time it is exeedingly dangerous because * a user can program its OBP flash with OS image and effectively * kill the machine. * * This driver uses an interface different from Eddie's flash.c * as a silly safeguard. * * XXX The flash.c manipulates page caching characteristics in a certain * dubious way; also it assumes that remap_page_range() can remap * PCI bus locations, which may be false. ioremap() must be used * instead. We should discuss this. */#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/miscdevice.h>#include <linux/malloc.h>#include <linux/fcntl.h>#include <linux/poll.h>#include <linux/init.h>#include <linux/string.h>#include <linux/smp_lock.h>/* * <linux/blk.h> is controlled from the outside with these definitions. */#define MAJOR_NR	JSFD_MAJOR#define DEVICE_NAME "jsfd"#define DEVICE_REQUEST jsfd_do_request#define DEVICE_NR(device) (MINOR(device))#define DEVICE_ON(device)#define DEVICE_OFF(device)#define DEVICE_NO_RANDOM#include <linux/blk.h>#include <asm/uaccess.h>#include <asm/pgtable.h>#include <asm/io.h>#include <asm/pcic.h>#include <asm/oplib.h>#include <asm/jsflash.h>		/* ioctl arguments. <linux/> ?? */#define JSFIDSZ		(sizeof(struct jsflash_ident_arg))#define JSFPRGSZ	(sizeof(struct jsflash_program_arg))/* * Our device numbers have no business in system headers. * The only thing a user knows is the device name /dev/jsflash. * * Block devices are laid out like this: *   minor+0	- Bootstrap, for 8MB SIMM 0x20400000[0x800000] *   minor+1	- Filesystem to mount, normally 0x20400400[0x7ffc00] *   minor+2	- Whole flash area for any case... 0x20000000[0x01000000] * Total 3 minors per flash device. * * It is easier to have static size vectors, so we define * a total minor range JSF_MAX, which must cover all minors. *//* character device */#define JSF_MINOR	178	/* 178 is registered with hpa *//* block device */#define JSF_MAX		 3	/* 3 minors wasted total so far. */#define JSF_NPART	 3	/* 3 minors per flash device */#define JSF_PART_BITS	 2	/* 2 bits of minors to cover JSF_NPART */#define JSF_PART_MASK	 0x3	/* 2 bits mask *//* * Access functions. * We could ioremap(), but it's easier this way. */static unsigned int jsf_inl(unsigned long addr){	unsigned long retval;	__asm__ __volatile__("lda [%1] %2, %0\n\t" :				"=r" (retval) :				"r" (addr), "i" (ASI_M_BYPASS));        return retval;}static void jsf_outl(unsigned long addr, __u32 data){	__asm__ __volatile__("sta %0, [%1] %2\n\t" : :				"r" (data), "r" (addr), "i" (ASI_M_BYPASS) :				"memory");}/* * soft carrier */struct jsfd_part {	unsigned long dbase;	unsigned long dsize;	int refcnt;};struct jsflash {	unsigned long base;	unsigned long size;	unsigned long busy;		/* In use? */	struct jsflash_ident_arg id;	/* int mbase; */		/* Minor base, typically zero */	struct jsfd_part dv[JSF_NPART];};/* * We do not map normal memory or obio as a safety precaution. * But offsets are real, for ease of userland programming. */#define JSF_BASE_TOP	0x30000000#define JSF_BASE_ALL	0x20000000#define JSF_BASE_JK	0x20400000/* */static int jsfd_blksizes[JSF_MAX];static int jsfd_sizes[JSF_MAX];static u64 jsfd_bytesizes[JSF_MAX];/* * Let's pretend we may have several of these... */static struct jsflash jsf0;/* * Wait for AMD to finish its embedded algorithm. * We use the Toggle bit DQ6 (0x40) because it does not * depend on the data value as /DATA bit DQ7 does. * * XXX Do we need any timeout here? So far it never hanged, beware broken hw. */static void jsf_wait(unsigned long p) {	unsigned int x1, x2;	for (;;) {		x1 = jsf_inl(p);		x2 = jsf_inl(p);		if ((x1 & 0x40404040) == (x2 & 0x40404040)) return;	}}/* * Programming will only work if Flash is clean, * we leave it to the programmer application. * * AMD must be programmed one byte at a time; * thus, Simple Tech SIMM must be written 4 bytes at a time. * * Write waits for the chip to become ready after the write * was finished. This is done so that application would read * consistent data after the write is done. */static void jsf_write4(unsigned long fa, u32 data) {	jsf_outl(fa, 0xAAAAAAAA);		/* Unlock 1 Write 1 */	jsf_outl(fa, 0x55555555);		/* Unlock 1 Write 2 */	jsf_outl(fa, 0xA0A0A0A0);		/* Byte Program */	jsf_outl(fa, data);	jsf_wait(fa);}/* */static void jsfd_read(char *buf, unsigned long p, size_t togo) {	union byte4 {		char s[4];		unsigned int n;	} b;	while (togo >= 4) {		togo -= 4;		b.n = jsf_inl(p);		memcpy(buf, b.s, 4);		p += 4;		buf += 4;	}}static void jsfd_do_request(request_queue_t *q){	struct request *req;	int dev;	struct jsfd_part *jdp;	unsigned long offset;	size_t len;	for (;;) {		INIT_REQUEST;	/* if (QUEUE_EMPTY) return; */		req = CURRENT;		dev = MINOR(req->rq_dev);		if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {			end_request(0);			continue;		}		jdp = &jsf0.dv[dev & JSF_PART_MASK];		offset = req->sector << 9;		len = req->current_nr_sectors << 9;		if ((offset + len) > jdp->dsize) {               		end_request(0);			continue;		}		if (req->cmd == WRITE) {			printk(KERN_ERR "jsfd: write\n");			end_request(0);			continue;		}		if (req->cmd != READ) {			printk(KERN_ERR "jsfd: bad req->cmd %d\n", req->cmd);			end_request(0);			continue;		}		if ((jdp->dbase & 0xff000000) != 0x20000000) {			printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);			end_request(0);			continue;		}/* printk("jsfd%d: read buf %p off %x len %x\n", dev, req->buffer, (int)offset, (int)len); */ /* P3 */		jsfd_read(req->buffer, jdp->dbase + offset, len);		end_request(1);	}}/* * The memory devices use the full 32/64 bits of the offset, and so we cannot * check against negative addresses: they are ok. The return value is weird, * though, in that case (0). * * also note that seeking relative to the "end of file" isn't supported: * it has no meaning, so it returns -EINVAL. */static loff_t jsf_lseek(struct file * file, loff_t offset, int orig){	switch (orig) {		case 0:			file->f_pos = offset;			return file->f_pos;		case 1:			file->f_pos += offset;			return file->f_pos;		default:			return -EINVAL;	}}/* * OS SIMM Cannot be read in other size but a 32bits word. */static ssize_t jsf_read(struct file * file, char * buf,     size_t togo, loff_t *ppos){	unsigned long p = *ppos;	char *tmp = buf;	union byte4 {		char s[4];		unsigned int n;	} b;	if (verify_area(VERIFY_WRITE, buf, togo))		return -EFAULT; 	if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {		return 0;	}	if ((p + togo) < p	/* wrap */	   || (p + togo) >= JSF_BASE_TOP) {		togo = JSF_BASE_TOP - p;	}	if (p < JSF_BASE_ALL && togo != 0) {#if 0 /* __bzero XXX */		size_t x = JSF_BASE_ALL - p;		if (x > togo) x = togo;		clear_user(tmp, x);		tmp += x;		p += x;		togo -= x;#else		/*		 * Implementation of clear_user() calls __bzero		 * without regard to modversions,		 * so we cannot build a module.		 */		return 0;#endif	}	while (togo >= 4) {		togo -= 4;		b.n = jsf_inl(p);		copy_to_user(tmp, b.s, 4);		tmp += 4;		p += 4;	}	/*	 * XXX Small togo may remain if 1 byte is ordered.	 * It would be nice if we did a word size read and unpacked it.	 */	*ppos = p;	return tmp-buf;}static ssize_t jsf_write(struct file * file, const char * buf,    size_t count, loff_t *ppos){	return -ENOSPC;}/* */static int jsf_ioctl_erase(unsigned long arg){	unsigned long p;	/* p = jsf0.base;	hits wrong bank */	p = 0x20400000;	jsf_outl(p, 0xAAAAAAAA);		/* Unlock 1 Write 1 */	jsf_outl(p, 0x55555555);		/* Unlock 1 Write 2 */	jsf_outl(p, 0x80808080);		/* Erase setup */	jsf_outl(p, 0xAAAAAAAA);		/* Unlock 2 Write 1 */	jsf_outl(p, 0x55555555);		/* Unlock 2 Write 2 */	jsf_outl(p, 0x10101010);		/* Chip erase */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -