2 * Flash memory interface rev.5 driver for the Intel
3 * Flash chips used on the NetWinder.
5 * 20/08/2000 RMK use __ioremap to map flash into virtual memory
6 * make a few more places use "volatile"
7 * 22/05/2001 RMK - Lock read against write
8 * - merge printk level changes (with mods) from Alan Cox.
9 * - use *ppos as the file position, not file->f_pos.
10 * - fix check for out of range pos and r/w size
12 * Please note that we are tampering with the only flash chip in the
13 * machine, which contains the bootup code. We therefore have the
14 * power to convert these machines into doorstops...
17 #include <linux/module.h>
18 #include <linux/types.h>
20 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/proc_fs.h>
24 #include <linux/sched.h>
25 #include <linux/miscdevice.h>
26 #include <linux/spinlock.h>
27 #include <linux/rwsem.h>
28 #include <linux/init.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mutex.h>
32 #include <asm/hardware/dec21285.h>
35 #include <asm/mach-types.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
39 /*****************************************************************************/
40 #include <asm/nwflash.h>
42 #define NWFLASH_VERSION "6.4"
44 static void kick_open(void);
45 static int get_flash_id(void);
46 static int erase_block(int nBlock);
47 static int write_block(unsigned long p, const char __user *buf, int count);
49 #define KFLASH_SIZE 1024*1024 //1 Meg
50 #define KFLASH_SIZE4 4*1024*1024 //4 Meg
51 #define KFLASH_ID 0x89A6 //Intel flash
52 #define KFLASH_ID4 0xB0D4 //Intel flash 4Meg
54 static int flashdebug; //if set - we will display progress msgs
56 static int gbWriteEnable;
57 static int gbWriteBase64Enable;
58 static volatile unsigned char *FLASH_BASE;
59 static int gbFlashSize = KFLASH_SIZE;
60 static DEFINE_MUTEX(nwflash_mutex);
62 extern spinlock_t gpio_lock;
64 static int get_flash_id(void)
66 volatile unsigned int c1, c2;
69 * try to get flash chip ID
73 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x90;
75 c1 = *(volatile unsigned char *) FLASH_BASE;
79 * on 4 Meg flash the second byte is actually at offset 2...
82 c2 = *(volatile unsigned char *) (FLASH_BASE + 2);
84 c2 = *(volatile unsigned char *) (FLASH_BASE + 1);
89 * set it back to read mode
91 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
94 gbFlashSize = KFLASH_SIZE4;
99 static int flash_ioctl(struct inode *inodep, struct file *filep, unsigned int cmd, unsigned long arg)
102 case CMD_WRITE_DISABLE:
103 gbWriteBase64Enable = 0;
107 case CMD_WRITE_ENABLE:
111 case CMD_WRITE_BASE64K_ENABLE:
112 gbWriteBase64Enable = 1;
116 gbWriteBase64Enable = 0;
123 static ssize_t flash_read(struct file *file, char __user *buf, size_t size,
126 unsigned long p = *ppos;
127 unsigned int count = size;
131 printk(KERN_DEBUG "flash_read: flash_read: offset=0x%lX, "
132 "buffer=%p, count=0x%X.\n", p, buf, count);
137 if (p < gbFlashSize) {
138 if (count > gbFlashSize - p)
139 count = gbFlashSize - p;
142 * We now lock against reads and writes. --rmk
144 if (mutex_lock_interruptible(&nwflash_mutex))
147 ret = copy_to_user(buf, (void *)(FLASH_BASE + p), count);
153 mutex_unlock(&nwflash_mutex);
158 static ssize_t flash_write(struct file *file, const char __user *buf,
159 size_t size, loff_t * ppos)
161 unsigned long p = *ppos;
162 unsigned int count = size;
164 int nBlock, temp, rc;
168 printk("flash_write: offset=0x%lX, buffer=0x%p, count=0x%X.\n",
174 if (p < 64 * 1024 && (!gbWriteBase64Enable))
178 * check for out of range pos or count
180 if (p >= gbFlashSize)
181 return count ? -ENXIO : 0;
183 if (count > gbFlashSize - p)
184 count = gbFlashSize - p;
186 if (!access_ok(VERIFY_READ, buf, count))
190 * We now lock against reads and writes. --rmk
192 if (mutex_lock_interruptible(&nwflash_mutex))
197 leds_event(led_claim);
198 leds_event(led_green_on);
200 nBlock = (int) p >> 16; //block # of 64K bytes
203 * # of 64K blocks to erase and write
205 temp = ((int) (p + count) >> 16) - nBlock + 1;
208 * write ends at exactly 64k boundary?
210 if (((int) (p + count) & 0xFFFF) == 0)
214 printk(KERN_DEBUG "flash_write: writing %d block(s) "
215 "starting at %d.\n", temp, nBlock);
217 for (; temp; temp--, nBlock++) {
219 printk(KERN_DEBUG "flash_write: erasing block %d.\n", nBlock);
222 * first we have to erase the block(s), where we will write...
228 rc = erase_block(nBlock);
230 } while (rc && i < 10);
233 printk(KERN_ERR "flash_write: erase error %x\n", rc);
237 printk(KERN_DEBUG "flash_write: writing offset %lX, "
238 "from buf %p, bytes left %X.\n", p, buf,
242 * write_block will limit write to space left in this block
244 rc = write_block(p, buf, count - written);
248 * if somehow write verify failed? Can't happen??
252 * retry up to 10 times
258 * else quit with error...
264 printk(KERN_ERR "flash_write: write error %X\n", rc);
273 printk(KERN_DEBUG "flash_write: written 0x%X bytes OK.\n", written);
277 * restore reg on exit
279 leds_event(led_release);
281 mutex_unlock(&nwflash_mutex);
288 * The memory devices use the full 32/64 bits of the offset, and so we cannot
289 * check against negative addresses: they are ok. The return value is weird,
290 * though, in that case (0).
292 * also note that seeking relative to the "end of file" isn't supported:
293 * it has no meaning, so it returns -EINVAL.
295 static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
301 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
302 (unsigned int) offset, orig);
311 if ((unsigned int) offset > gbFlashSize) {
316 file->f_pos = (unsigned int) offset;
320 if ((file->f_pos + offset) > gbFlashSize) {
324 if ((file->f_pos + offset) < 0) {
328 file->f_pos += offset;
340 * assume that main Write routine did the parameter checking...
341 * so just go ahead and erase, what requested!
344 static int erase_block(int nBlock)
346 volatile unsigned int c1;
347 volatile unsigned char *pWritePtr;
348 unsigned long timeout;
352 * orange LED == erase
354 leds_event(led_amber_on);
357 * reset footbridge to the correct offset 0 (...0..3)
359 *CSR_ROMWRITEREG = 0;
364 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
368 * reset status if old errors
370 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
374 * aim at the middle of a current block...
376 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + 0x8000 + (nBlock << 16)));
386 *(volatile unsigned char *) pWritePtr = 0x20;
391 *(volatile unsigned char *) pWritePtr = 0xD0;
399 * wait while erasing in process (up to 10 sec)
401 timeout = jiffies + 10 * HZ;
403 while (!(c1 & 0x80) && time_before(jiffies, timeout)) {
408 c1 = *(volatile unsigned char *) (pWritePtr);
409 // printk("Flash_erase: status=%X.\n",c1);
413 * set flash for normal read access
416 // *(volatile unsigned char*)(FLASH_BASE+0x8000) = 0xFF;
417 *(volatile unsigned char *) pWritePtr = 0xFF; //back to normal operation
420 * check if erase errors were reported
423 printk(KERN_ERR "flash_erase: err at %p\n", pWritePtr);
428 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
433 * just to make sure - verify if erased OK...
437 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + (nBlock << 16)));
439 for (temp = 0; temp < 16 * 1024; temp++, pWritePtr += 4) {
440 if ((temp1 = *(volatile unsigned int *) pWritePtr) != 0xFFFFFFFF) {
441 printk(KERN_ERR "flash_erase: verify err at %p = %X\n",
452 * write_block will limit number of bytes written to the space in this block
454 static int write_block(unsigned long p, const char __user *buf, int count)
456 volatile unsigned int c1;
457 volatile unsigned int c2;
458 unsigned char *pWritePtr;
459 unsigned int uAddress;
461 unsigned long timeout;
462 unsigned long timeout1;
467 leds_event(led_amber_off);
468 leds_event(led_red_on);
470 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
473 * check if write will end in this block....
477 if (offset + count > 0x10000)
478 count = 0x10000 - offset;
481 * wait up to 30 sec for this block
483 timeout = jiffies + 30 * HZ;
485 for (offset = 0; offset < count; offset++, pWritePtr++) {
486 uAddress = (unsigned int) pWritePtr;
487 uAddress &= 0xFFFFFFFC;
488 if (__get_user(c2, buf + offset))
495 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
498 * kick open the write gate
503 * program footbridge to the correct offset...0..3
505 *CSR_ROMWRITEREG = (unsigned int) pWritePtr & 3;
510 *(volatile unsigned char *) (uAddress) = 0x40;
515 *(volatile unsigned char *) (uAddress) = c2;
520 *(volatile unsigned char *) (FLASH_BASE + 0x10000) = 0x70;
525 * wait up to 1 sec for this byte
527 timeout1 = jiffies + 1 * HZ;
532 while (!(c1 & 0x80) && time_before(jiffies, timeout1))
533 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
536 * if timeout getting status
538 if (time_after_eq(jiffies, timeout1)) {
543 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
548 * switch on read access, as a default flash operation mode
554 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
557 * if hardware reports an error writing, and not timeout -
558 * reset the chip and retry
565 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
570 if (time_before(jiffies, timeout)) {
572 printk(KERN_DEBUG "write_block: Retrying write at 0x%X)n",
573 pWritePtr - FLASH_BASE);
578 leds_event(led_amber_off);
586 leds_event(led_red_on);
590 printk(KERN_ERR "write_block: timeout at 0x%X\n",
591 pWritePtr - FLASH_BASE);
602 * green LED == read/verify
604 leds_event(led_amber_off);
605 leds_event(led_green_on);
609 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
611 for (offset = 0; offset < count; offset++) {
613 if (__get_user(c, buf))
616 if ((c1 = *pWritePtr++) != c) {
617 printk(KERN_ERR "write_block: verify error at 0x%X (%02X!=%02X)\n",
618 pWritePtr - FLASH_BASE, c1, c);
627 static void kick_open(void)
632 * we want to write a bit pattern XXX1 to Xilinx to enable
633 * the write gate, which will be open for about the next 2ms.
635 spin_lock_irqsave(&gpio_lock, flags);
637 spin_unlock_irqrestore(&gpio_lock, flags);
640 * let the ISA bus to catch on...
645 static struct file_operations flash_fops =
647 .owner = THIS_MODULE,
648 .llseek = flash_llseek,
650 .write = flash_write,
651 .ioctl = flash_ioctl,
654 static struct miscdevice flash_miscdev =
661 static int __init nwflash_init(void)
665 if (machine_is_netwinder()) {
668 FLASH_BASE = ioremap(DC21285_FLASH, KFLASH_SIZE4);
673 if ((id != KFLASH_ID) && (id != KFLASH_ID4)) {
675 iounmap((void *)FLASH_BASE);
676 printk("Flash: incorrect ID 0x%04X.\n", id);
680 printk("Flash ROM driver v.%s, flash device ID 0x%04X, size %d Mb.\n",
681 NWFLASH_VERSION, id, gbFlashSize / (1024 * 1024));
683 ret = misc_register(&flash_miscdev);
685 iounmap((void *)FLASH_BASE);
692 static void __exit nwflash_exit(void)
694 misc_deregister(&flash_miscdev);
695 iounmap((void *)FLASH_BASE);
698 MODULE_LICENSE("GPL");
700 module_param(flashdebug, bool, 0644);
702 module_init(nwflash_init);
703 module_exit(nwflash_exit);