2 * drivers/sbus/char/jsflash.c
4 * Copyright (C) 1991, 1992 Linus Torvalds (drivers/char/mem.c)
5 * Copyright (C) 1997 Eddie C. Dost (drivers/sbus/char/flash.c)
6 * Copyright (C) 1997-2000 Pavel Machek <pavel@ucw.cz> (drivers/block/nbd.c)
7 * Copyright (C) 1999-2000 Pete Zaitcev
9 * This driver is used to program OS into a Flash SIMM on
10 * Krups and Espresso platforms.
12 * TODO: do not allow erase/programming if file systems are mounted.
13 * TODO: Erase/program both banks of a 8MB SIMM.
15 * It is anticipated that programming an OS Flash will be a routine
16 * procedure. In the same time it is exeedingly dangerous because
17 * a user can program its OBP flash with OS image and effectively
20 * This driver uses an interface different from Eddie's flash.c
21 * as a silly safeguard.
23 * XXX The flash.c manipulates page caching characteristics in a certain
24 * dubious way; also it assumes that remap_page_range() can remap
25 * PCI bus locations, which may be false. ioremap() must be used
26 * instead. We should discuss this.
29 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/miscdevice.h>
33 #include <linux/slab.h>
34 #include <linux/fcntl.h>
35 #include <linux/poll.h>
36 #include <linux/init.h>
37 #include <linux/string.h>
38 #include <linux/smp_lock.h>
41 * <linux/blk.h> is controlled from the outside with these definitions.
43 #define MAJOR_NR JSFD_MAJOR
45 #define DEVICE_NAME "jsfd"
46 #define DEVICE_REQUEST jsfd_do_request
47 #define DEVICE_NR(device) (MINOR(device))
48 #define DEVICE_ON(device)
49 #define DEVICE_OFF(device)
50 #define DEVICE_NO_RANDOM
52 #include <linux/blk.h>
55 #include <asm/uaccess.h>
56 #include <asm/pgtable.h>
59 #include <asm/oplib.h>
61 #include <asm/jsflash.h> /* ioctl arguments. <linux/> ?? */
62 #define JSFIDSZ (sizeof(struct jsflash_ident_arg))
63 #define JSFPRGSZ (sizeof(struct jsflash_program_arg))
66 * Our device numbers have no business in system headers.
67 * The only thing a user knows is the device name /dev/jsflash.
69 * Block devices are laid out like this:
70 * minor+0 - Bootstrap, for 8MB SIMM 0x20400000[0x800000]
71 * minor+1 - Filesystem to mount, normally 0x20400400[0x7ffc00]
72 * minor+2 - Whole flash area for any case... 0x20000000[0x01000000]
73 * Total 3 minors per flash device.
75 * It is easier to have static size vectors, so we define
76 * a total minor range JSF_MAX, which must cover all minors.
78 /* character device */
79 #define JSF_MINOR 178 /* 178 is registered with hpa */
81 #define JSF_MAX 3 /* 3 minors wasted total so far. */
82 #define JSF_NPART 3 /* 3 minors per flash device */
83 #define JSF_PART_BITS 2 /* 2 bits of minors to cover JSF_NPART */
84 #define JSF_PART_MASK 0x3 /* 2 bits mask */
88 * We could ioremap(), but it's easier this way.
90 static unsigned int jsf_inl(unsigned long addr)
94 __asm__ __volatile__("lda [%1] %2, %0\n\t" :
96 "r" (addr), "i" (ASI_M_BYPASS));
100 static void jsf_outl(unsigned long addr, __u32 data)
103 __asm__ __volatile__("sta %0, [%1] %2\n\t" : :
104 "r" (data), "r" (addr), "i" (ASI_M_BYPASS) :
121 unsigned long busy; /* In use? */
122 struct jsflash_ident_arg id;
123 /* int mbase; */ /* Minor base, typically zero */
124 struct jsfd_part dv[JSF_NPART];
128 * We do not map normal memory or obio as a safety precaution.
129 * But offsets are real, for ease of userland programming.
131 #define JSF_BASE_TOP 0x30000000
132 #define JSF_BASE_ALL 0x20000000
134 #define JSF_BASE_JK 0x20400000
138 static int jsfd_blksizes[JSF_MAX];
139 static int jsfd_sizes[JSF_MAX];
140 static u64 jsfd_bytesizes[JSF_MAX];
143 * Let's pretend we may have several of these...
145 static struct jsflash jsf0;
148 * Wait for AMD to finish its embedded algorithm.
149 * We use the Toggle bit DQ6 (0x40) because it does not
150 * depend on the data value as /DATA bit DQ7 does.
152 * XXX Do we need any timeout here? So far it never hanged, beware broken hw.
154 static void jsf_wait(unsigned long p) {
160 if ((x1 & 0x40404040) == (x2 & 0x40404040)) return;
165 * Programming will only work if Flash is clean,
166 * we leave it to the programmer application.
168 * AMD must be programmed one byte at a time;
169 * thus, Simple Tech SIMM must be written 4 bytes at a time.
171 * Write waits for the chip to become ready after the write
172 * was finished. This is done so that application would read
173 * consistent data after the write is done.
175 static void jsf_write4(unsigned long fa, u32 data) {
177 jsf_outl(fa, 0xAAAAAAAA); /* Unlock 1 Write 1 */
178 jsf_outl(fa, 0x55555555); /* Unlock 1 Write 2 */
179 jsf_outl(fa, 0xA0A0A0A0); /* Byte Program */
187 static void jsfd_read(char *buf, unsigned long p, size_t togo) {
202 static void jsfd_do_request(request_queue_t *q)
206 struct jsfd_part *jdp;
207 unsigned long offset;
211 INIT_REQUEST; /* if (QUEUE_EMPTY) return; */
214 dev = MINOR(req->rq_dev);
215 if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {
219 jdp = &jsf0.dv[dev & JSF_PART_MASK];
221 offset = req->sector << 9;
222 len = req->current_nr_sectors << 9;
223 if ((offset + len) > jdp->dsize) {
228 if (req->cmd == WRITE) {
229 printk(KERN_ERR "jsfd: write\n");
233 if (req->cmd != READ) {
234 printk(KERN_ERR "jsfd: bad req->cmd %d\n", req->cmd);
239 if ((jdp->dbase & 0xff000000) != 0x20000000) {
240 printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
245 /* printk("jsfd%d: read buf %p off %x len %x\n", dev, req->buffer, (int)offset, (int)len); */ /* P3 */
246 jsfd_read(req->buffer, jdp->dbase + offset, len);
253 * The memory devices use the full 32/64 bits of the offset, and so we cannot
254 * check against negative addresses: they are ok. The return value is weird,
255 * though, in that case (0).
257 * also note that seeking relative to the "end of file" isn't supported:
258 * it has no meaning, so it returns -EINVAL.
260 static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
264 file->f_pos = offset;
267 file->f_pos += offset;
275 * OS SIMM Cannot be read in other size but a 32bits word.
277 static ssize_t jsf_read(struct file * file, char * buf,
278 size_t togo, loff_t *ppos)
280 unsigned long p = *ppos;
288 if (verify_area(VERIFY_WRITE, buf, togo))
291 if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
295 if ((p + togo) < p /* wrap */
296 || (p + togo) >= JSF_BASE_TOP) {
297 togo = JSF_BASE_TOP - p;
300 if (p < JSF_BASE_ALL && togo != 0) {
301 #if 0 /* __bzero XXX */
302 size_t x = JSF_BASE_ALL - p;
303 if (x > togo) x = togo;
310 * Implementation of clear_user() calls __bzero
311 * without regard to modversions,
312 * so we cannot build a module.
321 copy_to_user(tmp, b.s, 4);
327 * XXX Small togo may remain if 1 byte is ordered.
328 * It would be nice if we did a word size read and unpacked it.
335 static ssize_t jsf_write(struct file * file, const char * buf,
336 size_t count, loff_t *ppos)
343 static int jsf_ioctl_erase(unsigned long arg)
347 /* p = jsf0.base; hits wrong bank */
350 jsf_outl(p, 0xAAAAAAAA); /* Unlock 1 Write 1 */
351 jsf_outl(p, 0x55555555); /* Unlock 1 Write 2 */
352 jsf_outl(p, 0x80808080); /* Erase setup */
353 jsf_outl(p, 0xAAAAAAAA); /* Unlock 2 Write 1 */
354 jsf_outl(p, 0x55555555); /* Unlock 2 Write 2 */
355 jsf_outl(p, 0x10101010); /* Chip erase */
359 * This code is ok, except that counter based timeout
360 * has no place in this world. Let's just drop timeouts...
365 for (i = 0; i < 1000000; i++) {
367 if ((x & 0x80808080) == 0x80808080) break;
369 if ((x & 0x80808080) != 0x80808080) {
370 printk("jsf0: erase timeout with 0x%08x\n", x);
372 printk("jsf0: erase done with 0x%08x\n", x);
383 * Program a block of flash.
384 * Very simple because we can do it byte by byte anyway.
386 static int jsf_ioctl_program(unsigned long arg)
388 struct jsflash_program_arg abuf;
397 if (verify_area(VERIFY_READ, (void *)arg, JSFPRGSZ))
399 copy_from_user(&abuf, (char *)arg, JSFPRGSZ);
402 if ((togo & 3) || (p & 3)) return -EINVAL;
404 uptr = (char *) (unsigned long) abuf.data;
405 if (verify_area(VERIFY_READ, uptr, togo))
409 copy_from_user(&b.s[0], uptr, 4);
418 static int jsf_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
423 if (!capable(CAP_SYS_ADMIN))
427 if (verify_area(VERIFY_WRITE, (void *)arg, JSFIDSZ))
429 copy_to_user(arg, &jsf0.id, JSFIDSZ);
433 error = jsf_ioctl_erase(arg);
435 case JSFLASH_PROGRAM:
436 error = jsf_ioctl_program(arg);
443 static int jsfd_ioctl(struct inode *inode, struct file *file,
444 unsigned int cmd, unsigned long arg)
448 if (!capable(CAP_SYS_ADMIN))
452 if ((dev = MINOR(inode->i_rdev)) >= JSF_MAX) return -ENODEV;
456 return put_user(jsfd_bytesizes[dev] >> 9, (unsigned long *) arg);
458 return put_user(jsfd_bytesizes[dev], (u64 *) arg);
464 return blk_ioctl(inode->i_rdev, cmd, arg);
467 /* case BLKFLSBUF: */ /* Program, then read, what happens? Stale? */
473 static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
478 static int jsf_open(struct inode * inode, struct file * filp)
481 if (jsf0.base == 0) return -ENXIO;
482 if (test_and_set_bit(0, (void *)&jsf0.busy) != 0)
485 return 0; /* XXX What security? */
488 static int jsfd_open(struct inode *inode, struct file *file)
490 struct jsfd_part *jdp;
495 dev = MINOR(inode->i_rdev);
496 if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {
497 printk(KERN_ALERT "jsfd_open: illegal minor %d\n", dev);
507 static int jsf_release(struct inode *inode, struct file *file)
513 static int jsfd_release(struct inode *inode, struct file *file)
515 struct jsfd_part *jdp;
520 dev = MINOR(inode->i_rdev);
521 if (dev >= JSF_MAX || (dev & JSF_PART_MASK) >= JSF_NPART) {
522 printk(KERN_ALERT "jsfd_release: illegal minor %d\n", dev);
527 if (jdp->refcnt <= 0) {
528 printk(KERN_ALERT "jsfd_release: bad ref on minor %d\n", dev);
532 /* N.B. Doesn't lo->file need an fput?? */
536 static struct file_operations jsf_fops = {
544 release: jsf_release,
547 static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
549 static struct block_device_operations jsfd_fops = {
552 release: jsfd_release,
558 int jsflash_init(void)
564 struct linux_prom_registers reg0;
566 node = prom_getchild(prom_root_node);
567 node = prom_searchsiblings(node, "flash-memory");
568 if (node != 0 && node != -1) {
569 if (prom_getproperty(node, "reg",
570 (char *)®0, sizeof(reg0)) == -1) {
571 printk("jsflash: no \"reg\" property\n");
574 if (reg0.which_io != 0) {
575 printk("jsflash: bus number nonzero: 0x%x:%x\n",
576 reg0.which_io, reg0.phys_addr);
580 * Flash may be somewhere else, for instance on Ebus.
581 * So, don't do the following check for IIep flash space.
584 if ((reg0.phys_addr >> 24) != 0x20) {
585 printk("jsflash: suspicious address: 0x%x:%x\n",
586 reg0.which_io, reg0.phys_addr);
590 if ((int)reg0.reg_size <= 0) {
591 printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
595 /* XXX Remove this code once PROLL ID12 got widespread */
596 printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
597 prom_getproperty(prom_root_node, "banner-name", banner, 128);
598 if (strcmp (banner, "JavaStation-NC") != 0 &&
599 strcmp (banner, "JavaStation-E") != 0) {
603 reg0.phys_addr = 0x20400000;
604 reg0.reg_size = 0x00800000;
607 /* Let us be really paranoid for modifications to probing code. */
608 /* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
609 if (sparc_cpu_model != sun4m) {
610 /* We must be on sun4m because we use MMU Bypass ASI. */
614 if (jsf0.base == 0) {
617 jsf->base = reg0.phys_addr;
618 jsf->size = reg0.reg_size;
620 /* XXX Redo the userland interface. */
621 jsf->id.off = JSF_BASE_ALL;
622 jsf->id.size = 0x01000000; /* 16M - all segments */
623 strcpy(jsf->id.name, "Krups_all");
625 jsf->dv[0].dbase = jsf->base;
626 jsf->dv[0].dsize = jsf->size;
627 jsf->dv[1].dbase = jsf->base + 1024;
628 jsf->dv[1].dsize = jsf->size - 1024;
629 jsf->dv[2].dbase = JSF_BASE_ALL;
630 jsf->dv[2].dsize = 0x01000000;
632 printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
633 (int) (jsf->size / (1024*1024)));
636 if ((rc = misc_register(&jsf_dev)) != 0) {
637 printk(KERN_ERR "jsf: unable to get misc minor %d\n",
646 int jsfd_init(void) {
648 struct jsfd_part *jdp;
651 if (jsf0.base == 0) {
655 if (register_blkdev(JSFD_MAJOR, "jsfd", &jsfd_fops)) {
656 printk("jsfd_init: unable to get major number %d\n",
661 blksize_size[JSFD_MAJOR] = jsfd_blksizes;
662 blk_size[JSFD_MAJOR] = jsfd_sizes;
664 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
665 /* blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR), 0); */
666 for (i = 0; i < JSF_MAX; i++) {
667 if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
668 jsf = &jsf0; /* actually, &jsfv[i >> JSF_PART_BITS] */
669 jdp = &jsf->dv[i&JSF_PART_MASK];
673 jsfd_blksizes[i] = 1024;
674 jsfd_bytesizes[i] = jdp->dsize;
675 jsfd_sizes[i] = jsfd_bytesizes[i] >> 10;
676 register_disk(NULL, MKDEV(JSFD_MAJOR, i), 1, &jsfd_fops,
677 jsfd_bytesizes[i] >> 9);
678 set_device_ro(MKDEV(JSFD_MAJOR, i), 1);
684 MODULE_LICENSE("GPL");
686 int init_module(void) {
689 if ((rc = jsflash_init()) == 0) {
696 void cleanup_module(void) {
698 /* for (all probed units) { } */
700 printk("jsf0: cleaning busy unit\n");
704 misc_deregister(&jsf_dev);
705 if (unregister_blkdev(JSFD_MAJOR, "jsfd") != 0)
706 printk("jsfd: cleanup_module failed\n");
707 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));