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/miscdevice.h>
25 #include <linux/spinlock.h>
26 #include <linux/rwsem.h>
27 #include <linux/init.h>
28 #include <linux/mutex.h>
29 #include <linux/jiffies.h>
31 #include <asm/hardware/dec21285.h>
34 #include <asm/mach-types.h>
35 #include <asm/uaccess.h>
37 /*****************************************************************************/
38 #include <asm/nwflash.h>
40 #define NWFLASH_VERSION "6.4"
42 static DEFINE_MUTEX(flash_mutex);
43 static void kick_open(void);
44 static int get_flash_id(void);
45 static int erase_block(int nBlock);
46 static int write_block(unsigned long p, const char __user *buf, int count);
48 #define KFLASH_SIZE 1024*1024 //1 Meg
49 #define KFLASH_SIZE4 4*1024*1024 //4 Meg
50 #define KFLASH_ID 0x89A6 //Intel flash
51 #define KFLASH_ID4 0xB0D4 //Intel flash 4Meg
53 static bool flashdebug; //if set - we will display progress msgs
55 static int gbWriteEnable;
56 static int gbWriteBase64Enable;
57 static volatile unsigned char *FLASH_BASE;
58 static int gbFlashSize = KFLASH_SIZE;
59 static DEFINE_MUTEX(nwflash_mutex);
61 static int get_flash_id(void)
63 volatile unsigned int c1, c2;
66 * try to get flash chip ID
70 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x90;
72 c1 = *(volatile unsigned char *) FLASH_BASE;
76 * on 4 Meg flash the second byte is actually at offset 2...
79 c2 = *(volatile unsigned char *) (FLASH_BASE + 2);
81 c2 = *(volatile unsigned char *) (FLASH_BASE + 1);
86 * set it back to read mode
88 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
91 gbFlashSize = KFLASH_SIZE4;
96 static long flash_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
98 mutex_lock(&flash_mutex);
100 case CMD_WRITE_DISABLE:
101 gbWriteBase64Enable = 0;
105 case CMD_WRITE_ENABLE:
109 case CMD_WRITE_BASE64K_ENABLE:
110 gbWriteBase64Enable = 1;
114 gbWriteBase64Enable = 0;
116 mutex_unlock(&flash_mutex);
119 mutex_unlock(&flash_mutex);
123 static ssize_t flash_read(struct file *file, char __user *buf, size_t size,
129 printk(KERN_DEBUG "flash_read: flash_read: offset=0x%llx, "
130 "buffer=%p, count=0x%zx.\n", *ppos, buf, size);
132 * We now lock against reads and writes. --rmk
134 if (mutex_lock_interruptible(&nwflash_mutex))
137 ret = simple_read_from_buffer(buf, size, ppos, (void *)FLASH_BASE, gbFlashSize);
138 mutex_unlock(&nwflash_mutex);
143 static ssize_t flash_write(struct file *file, const char __user *buf,
144 size_t size, loff_t * ppos)
146 unsigned long p = *ppos;
147 unsigned int count = size;
149 int nBlock, temp, rc;
153 printk("flash_write: offset=0x%lX, buffer=0x%p, count=0x%X.\n",
159 if (p < 64 * 1024 && (!gbWriteBase64Enable))
163 * check for out of range pos or count
165 if (p >= gbFlashSize)
166 return count ? -ENXIO : 0;
168 if (count > gbFlashSize - p)
169 count = gbFlashSize - p;
171 if (!access_ok(VERIFY_READ, buf, count))
175 * We now lock against reads and writes. --rmk
177 if (mutex_lock_interruptible(&nwflash_mutex))
182 leds_event(led_claim);
183 leds_event(led_green_on);
185 nBlock = (int) p >> 16; //block # of 64K bytes
188 * # of 64K blocks to erase and write
190 temp = ((int) (p + count) >> 16) - nBlock + 1;
193 * write ends at exactly 64k boundary?
195 if (((int) (p + count) & 0xFFFF) == 0)
199 printk(KERN_DEBUG "flash_write: writing %d block(s) "
200 "starting at %d.\n", temp, nBlock);
202 for (; temp; temp--, nBlock++) {
204 printk(KERN_DEBUG "flash_write: erasing block %d.\n", nBlock);
207 * first we have to erase the block(s), where we will write...
213 rc = erase_block(nBlock);
215 } while (rc && i < 10);
218 printk(KERN_ERR "flash_write: erase error %x\n", rc);
222 printk(KERN_DEBUG "flash_write: writing offset %lX, "
223 "from buf %p, bytes left %X.\n", p, buf,
227 * write_block will limit write to space left in this block
229 rc = write_block(p, buf, count - written);
233 * if somehow write verify failed? Can't happen??
237 * retry up to 10 times
243 * else quit with error...
249 printk(KERN_ERR "flash_write: write error %X\n", rc);
258 printk(KERN_DEBUG "flash_write: written 0x%X bytes OK.\n", written);
262 * restore reg on exit
264 leds_event(led_release);
266 mutex_unlock(&nwflash_mutex);
273 * The memory devices use the full 32/64 bits of the offset, and so we cannot
274 * check against negative addresses: they are ok. The return value is weird,
275 * though, in that case (0).
277 * also note that seeking relative to the "end of file" isn't supported:
278 * it has no meaning, so it returns -EINVAL.
280 static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
284 mutex_lock(&flash_mutex);
286 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
287 (unsigned int) offset, orig);
296 if ((unsigned int) offset > gbFlashSize) {
301 file->f_pos = (unsigned int) offset;
305 if ((file->f_pos + offset) > gbFlashSize) {
309 if ((file->f_pos + offset) < 0) {
313 file->f_pos += offset;
319 mutex_unlock(&flash_mutex);
325 * assume that main Write routine did the parameter checking...
326 * so just go ahead and erase, what requested!
329 static int erase_block(int nBlock)
331 volatile unsigned int c1;
332 volatile unsigned char *pWritePtr;
333 unsigned long timeout;
337 * orange LED == erase
339 leds_event(led_amber_on);
342 * reset footbridge to the correct offset 0 (...0..3)
344 *CSR_ROMWRITEREG = 0;
349 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
353 * reset status if old errors
355 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
359 * aim at the middle of a current block...
361 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + 0x8000 + (nBlock << 16)));
371 *(volatile unsigned char *) pWritePtr = 0x20;
376 *(volatile unsigned char *) pWritePtr = 0xD0;
384 * wait while erasing in process (up to 10 sec)
386 timeout = jiffies + 10 * HZ;
388 while (!(c1 & 0x80) && time_before(jiffies, timeout)) {
393 c1 = *(volatile unsigned char *) (pWritePtr);
394 // printk("Flash_erase: status=%X.\n",c1);
398 * set flash for normal read access
401 // *(volatile unsigned char*)(FLASH_BASE+0x8000) = 0xFF;
402 *(volatile unsigned char *) pWritePtr = 0xFF; //back to normal operation
405 * check if erase errors were reported
408 printk(KERN_ERR "flash_erase: err at %p\n", pWritePtr);
413 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
418 * just to make sure - verify if erased OK...
422 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + (nBlock << 16)));
424 for (temp = 0; temp < 16 * 1024; temp++, pWritePtr += 4) {
425 if ((temp1 = *(volatile unsigned int *) pWritePtr) != 0xFFFFFFFF) {
426 printk(KERN_ERR "flash_erase: verify err at %p = %X\n",
437 * write_block will limit number of bytes written to the space in this block
439 static int write_block(unsigned long p, const char __user *buf, int count)
441 volatile unsigned int c1;
442 volatile unsigned int c2;
443 unsigned char *pWritePtr;
444 unsigned int uAddress;
446 unsigned long timeout;
447 unsigned long timeout1;
452 leds_event(led_amber_off);
453 leds_event(led_red_on);
455 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
458 * check if write will end in this block....
462 if (offset + count > 0x10000)
463 count = 0x10000 - offset;
466 * wait up to 30 sec for this block
468 timeout = jiffies + 30 * HZ;
470 for (offset = 0; offset < count; offset++, pWritePtr++) {
471 uAddress = (unsigned int) pWritePtr;
472 uAddress &= 0xFFFFFFFC;
473 if (__get_user(c2, buf + offset))
480 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
483 * kick open the write gate
488 * program footbridge to the correct offset...0..3
490 *CSR_ROMWRITEREG = (unsigned int) pWritePtr & 3;
495 *(volatile unsigned char *) (uAddress) = 0x40;
500 *(volatile unsigned char *) (uAddress) = c2;
505 *(volatile unsigned char *) (FLASH_BASE + 0x10000) = 0x70;
510 * wait up to 1 sec for this byte
512 timeout1 = jiffies + 1 * HZ;
517 while (!(c1 & 0x80) && time_before(jiffies, timeout1))
518 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
521 * if timeout getting status
523 if (time_after_eq(jiffies, timeout1)) {
528 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
533 * switch on read access, as a default flash operation mode
539 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
542 * if hardware reports an error writing, and not timeout -
543 * reset the chip and retry
550 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
555 if (time_before(jiffies, timeout)) {
557 printk(KERN_DEBUG "write_block: Retrying write at 0x%X)n",
558 pWritePtr - FLASH_BASE);
563 leds_event(led_amber_off);
571 leds_event(led_red_on);
575 printk(KERN_ERR "write_block: timeout at 0x%X\n",
576 pWritePtr - FLASH_BASE);
587 * green LED == read/verify
589 leds_event(led_amber_off);
590 leds_event(led_green_on);
594 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
596 for (offset = 0; offset < count; offset++) {
598 if (__get_user(c, buf))
601 if ((c1 = *pWritePtr++) != c) {
602 printk(KERN_ERR "write_block: verify error at 0x%X (%02X!=%02X)\n",
603 pWritePtr - FLASH_BASE, c1, c);
612 static void kick_open(void)
617 * we want to write a bit pattern XXX1 to Xilinx to enable
618 * the write gate, which will be open for about the next 2ms.
620 spin_lock_irqsave(&nw_gpio_lock, flags);
621 nw_cpld_modify(CPLD_FLASH_WR_ENABLE, CPLD_FLASH_WR_ENABLE);
622 spin_unlock_irqrestore(&nw_gpio_lock, flags);
625 * let the ISA bus to catch on...
630 static const struct file_operations flash_fops =
632 .owner = THIS_MODULE,
633 .llseek = flash_llseek,
635 .write = flash_write,
636 .unlocked_ioctl = flash_ioctl,
639 static struct miscdevice flash_miscdev =
646 static int __init nwflash_init(void)
650 if (machine_is_netwinder()) {
653 FLASH_BASE = ioremap(DC21285_FLASH, KFLASH_SIZE4);
658 if ((id != KFLASH_ID) && (id != KFLASH_ID4)) {
660 iounmap((void *)FLASH_BASE);
661 printk("Flash: incorrect ID 0x%04X.\n", id);
665 printk("Flash ROM driver v.%s, flash device ID 0x%04X, size %d Mb.\n",
666 NWFLASH_VERSION, id, gbFlashSize / (1024 * 1024));
668 ret = misc_register(&flash_miscdev);
670 iounmap((void *)FLASH_BASE);
677 static void __exit nwflash_exit(void)
679 misc_deregister(&flash_miscdev);
680 iounmap((void *)FLASH_BASE);
683 MODULE_LICENSE("GPL");
685 module_param(flashdebug, bool, 0644);
687 module_init(nwflash_init);
688 module_exit(nwflash_exit);