patch-2.3.99-pre6 linux/drivers/sbus/char/jsflash.c

Next file: linux/drivers/sbus/char/pcikbd.c
Previous file: linux/drivers/pcmcia/ds.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.99-pre5/linux/drivers/sbus/char/jsflash.c linux/drivers/sbus/char/jsflash.c
@@ -3,11 +3,15 @@
  *
  *  Copyright (C) 1991, 1992  Linus Torvalds	(drivers/char/mem.c)
  *  Copyright (C) 1997  Eddie C. Dost		(drivers/sbus/char/flash.c)
- *  Copyright (C) 1999  Pete Zaitcev
+ *  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
@@ -31,23 +35,25 @@
 #include <linux/poll.h>
 #include <linux/init.h>
 #include <linux/string.h>
-#if 0	/* P3 from mem.c */
-#include <linux/mm.h>
-#include <linux/vmalloc.h>
-#include <linux/mman.h>
-#include <linux/random.h>
-#include <linux/raw.h>
-#include <linux/capability.h>
-#endif
+
+/*
+ * <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>
-#if 0	/* P3 from mem.c */
-#include <asm/system.h>
-#include <asm/sbus.h>
-#include <asm/ebus.h>
-#endif
 #include <asm/pcic.h>
 #include <asm/oplib.h>
 
@@ -58,8 +64,23 @@
 /*
  * 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.
  */
-#define JSF_MINOR	178
+/* 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.
@@ -86,11 +107,20 @@
 /*
  * 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];
 };
 
 /*
@@ -103,6 +133,12 @@
 #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;
@@ -112,7 +148,7 @@
  * 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?
+ * 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;
@@ -146,6 +182,73 @@
 }
 
 /*
+ */
+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).
@@ -301,7 +404,7 @@
 	if (verify_area(VERIFY_READ, uptr, togo))
 		return -EFAULT;
 	while (togo != 0) {
-		--togo;
+		togo -= 4;
 		copy_from_user(&b.s[0], uptr, 4);
 		jsf_write4(p, b.n);
 		p += 4;
@@ -316,6 +419,8 @@
 {
 	int error = -ENOTTY;
 
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
 	switch (cmd) {
 	case JSFLASH_IDENT:
 		if (verify_area(VERIFY_WRITE, (void *)arg, JSFIDSZ))
@@ -334,6 +439,34 @@
 	return error;
 }
 
+static int jsfd_ioctl(struct inode *inode, struct file *file,
+    unsigned int cmd, unsigned long arg)
+{
+	int dev;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+	if (!inode)
+		return -EINVAL;
+	if ((dev = MINOR(inode->i_rdev)) >= JSF_MAX) return -ENODEV;
+
+	switch (cmd) {
+	case BLKGETSIZE:
+		return put_user(jsfd_bytesizes[dev] >> 9, (long *) arg);
+
+#if 0
+	case BLKROSET:
+	case BLKROGET:
+	case BLKSSZGET:
+		return blk_ioctl(inode->i_rdev, cmd, arg);
+#endif
+
+	/* case BLKFLSBUF: */	/* Program, then read, what happens? Stale? */
+	default: ;
+	}
+	return -ENOTTY;
+}
+
 static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
 {
 	return -ENXIO;
@@ -350,6 +483,26 @@
 	return 0;	/* XXX What security? */
 }
 
+static int jsfd_open(struct inode *inode, struct file *file)
+{
+	struct jsfd_part *jdp;
+	int dev;
+
+	if (!inode)
+		return -EINVAL;
+	dev = MINOR(inode->i_rdev);
+	if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {
+		printk(KERN_ALERT "jsfd_open: illegal minor %d\n", dev);
+		return -ENODEV;
+	}
+
+	jdp = &jsf0.dv[dev];
+	jdp->refcnt++;
+
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
 static int jsf_release(struct inode *inode, struct file *file)
 {
 
@@ -359,6 +512,30 @@
 	return 0;
 }
 
+static int jsfd_release(struct inode *inode, struct file *file)
+{
+	struct jsfd_part *jdp;
+	int dev;
+
+	if (!inode)
+		return -ENODEV;
+	dev = MINOR(inode->i_rdev);
+	if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {
+		printk(KERN_ALERT "jsfd_release: illegal minor %d\n", dev);
+		return -ENODEV;
+	}
+
+	jdp = &jsf0.dv[dev];
+	if (jdp->refcnt <= 0) {
+		printk(KERN_ALERT "jsfd_release: bad ref on minor %d\n", dev);
+	} else {
+		--jdp->refcnt;
+	}
+	/* N.B. Doesn't lo->file need an fput?? */
+	MOD_DEC_USE_COUNT;
+	return 0;
+}
+
 static struct file_operations jsf_fops = {
 	llseek:		jsf_lseek,
 	read:		jsf_read,
@@ -371,37 +548,90 @@
 
 static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
 
+static struct block_device_operations jsfd_fops = {
+	open:		jsfd_open,
+	release:	jsfd_release,
+	ioctl:		jsfd_ioctl,
+};
+
 EXPORT_NO_SYMBOLS;
 
-#ifdef MODULE
-int init_module(void)
-#else
-int __init jsflash_init(void)
-#endif
+int jsflash_init(void)
 {
 	int rc;
+	struct jsflash *jsf;
+	int node;
 	char banner[128];
+	struct linux_prom_registers reg0;
 
-	/* FIXME: Really autodetect things */
-	prom_getproperty(prom_root_node, "banner-name", banner, 128);
-	if (strcmp (banner, "JavaStation-NC") && strcmp (banner, "JavaStation-E"))
-		return -ENXIO;
-
-	/* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
-	if (sparc_cpu_model == sun4m && jsf0.base == 0) {
-		/* XXX Autodetect */
+	node = prom_getchild(prom_root_node);
+	node = prom_searchsiblings(node, "flash-memory");
+	if (node != 0 && node != -1) {
+		if (prom_getproperty(node, "reg",
+		    (char *)&reg0, sizeof(reg0)) == -1) {
+			printk("jsflash: no \"reg\" property\n");
+			return -ENXIO;
+		}
+		if (reg0.which_io != 0) {
+			printk("jsflash: bus number nonzero: 0x%x:%x\n",
+			    reg0.which_io, reg0.phys_addr);
+			return -ENXIO;
+		}
 		/*
-		 * We do not want to use PROM properties;
-		 * They are faked by PROLL anyways.
+		 * Flash may be somewhere else, for instance on Ebus.
+		 * So, don't do the following check for IIep flash space.
 		 */
-		jsf0.base = JSF_BASE_JK;
-		jsf0.size = 0x00800000;		/* 8M */
+#if 0
+		if ((reg0.phys_addr >> 24) != 0x20) {
+			printk("jsflash: suspicious address: 0x%x:%x\n",
+			    reg0.which_io, reg0.phys_addr);
+			return -ENXIO;
+		}
+#endif
+		if ((int)reg0.reg_size <= 0) {
+			printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
+			return -ENXIO;
+		}
+	} else {
+		/* XXX Remove this code once PROLL ID12 got widespread */
+		printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
+		prom_getproperty(prom_root_node, "banner-name", banner, 128);
+		if (strcmp (banner, "JavaStation-NC") != 0 &&
+		    strcmp (banner, "JavaStation-E") != 0) {
+			return -ENXIO;
+		}
+		reg0.which_io = 0;
+		reg0.phys_addr = 0x20400000;
+		reg0.reg_size  = 0x00800000;
+	}
+
+	/* Let us be really paranoid for modifications to probing code. */
+	/* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
+	if (sparc_cpu_model != sun4m) {
+		/* We must be on sun4m because we use MMU Bypass ASI. */
+		return -ENXIO;
+	}
+
+	if (jsf0.base == 0) {
+		jsf = &jsf0;
+
+		jsf->base = reg0.phys_addr;
+		jsf->size = reg0.reg_size;
 
-		jsf0.id.off = JSF_BASE_ALL;
-		jsf0.id.size = 0x01000000;	/* 16M - all segments */
-		strcpy(jsf0.id.name, "Krups_all");
+		/* XXX Redo the userland interface. */
+		jsf->id.off = JSF_BASE_ALL;
+		jsf->id.size = 0x01000000;	/* 16M - all segments */
+		strcpy(jsf->id.name, "Krups_all");
+
+		jsf->dv[0].dbase = jsf->base;
+		jsf->dv[0].dsize = jsf->size;
+		jsf->dv[1].dbase = jsf->base + 1024;
+		jsf->dv[1].dsize = jsf->size - 1024;
+		jsf->dv[2].dbase = JSF_BASE_ALL;
+		jsf->dv[2].dsize = 0x01000000;
 
-		printk("Espresso Flash @0x%lx\n", jsf0.base);
+		printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
+		    (int) (jsf->size / (1024*1024)));
 	}
 
 	if ((rc = misc_register(&jsf_dev)) != 0) {
@@ -410,12 +640,63 @@
 		jsf0.base = 0;
 		return rc;
 	}
+
+	return 0;
+}
+
+int jsfd_init(void) {
+	struct jsflash *jsf;
+	struct jsfd_part *jdp;
+	int i;
+
+	if (jsf0.base == 0) {
+		printk("jsfd_init: no flash\n"); /* P3 */
+		return -EIO;
+	}
+
+	if (register_blkdev(JSFD_MAJOR, "jsfd", &jsfd_fops)) {
+		printk("jsfd_init: unable to get major number %d\n",
+		    JSFD_MAJOR);
+		return -EIO;
+	}
+
+	printk("jsfd0: at major %d\n", MAJOR_NR); /* P3 */
+
+	blksize_size[JSFD_MAJOR] = jsfd_blksizes;
+	blk_size[JSFD_MAJOR] = jsfd_sizes;
+
+	blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
+	/* blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR), 0); */
+	for (i = 0; i < JSF_MAX; i++) {
+		if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
+		jsf = &jsf0;	/* actually, &jsfv[i >> JSF_PART_BITS] */
+		jdp = &jsf->dv[i&JSF_PART_MASK];
+
+		jdp->refcnt = 0;
+
+		jsfd_blksizes[i] = 1024;
+		jsfd_bytesizes[i] = jdp->dsize;
+		jsfd_sizes[i] = jsfd_bytesizes[i] >> 10;
+		register_disk(NULL, MKDEV(JSFD_MAJOR, i), 1, &jsfd_fops,
+				jsfd_bytesizes[i] >> 9);
+		set_device_ro(MKDEV(JSFD_MAJOR, i), 1);
+	}
 	return 0;
 }
 
 #ifdef MODULE
-void cleanup_module(void)
-{
+
+int init_module(void) {
+	int rc;
+
+	if ((rc = jsflash_init()) == 0) {
+		jsfd_init();
+		return 0;
+	}
+	return rc;
+}
+
+void cleanup_module(void) {
 
 	/* for (all probed units) {  } */
 	if (jsf0.busy)
@@ -424,5 +705,7 @@
 	jsf0.busy = 0;
 
 	misc_deregister(&jsf_dev);
+	if (unregister_blkdev(JSFD_MAJOR, "jsfd") != 0)
+		printk("jsfd: cleanup_module failed\n");
 }
 #endif

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)