[PATCH] orinoco: bump version to 0.15
[linux-2.6/sactl.git] / drivers / char / nwflash.c
blob8865387d3448d4451aacf6fd72f66314a6dab6a1
1 /*
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>
19 #include <linux/fs.h>
20 #include <linux/errno.h>
21 #include <linux/mm.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>
33 #include <asm/io.h>
34 #include <asm/leds.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
71 kick_open();
72 c2 = inb(0x80);
73 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x90;
74 udelay(15);
75 c1 = *(volatile unsigned char *) FLASH_BASE;
76 c2 = inb(0x80);
79 * on 4 Meg flash the second byte is actually at offset 2...
81 if (c1 == 0xB0)
82 c2 = *(volatile unsigned char *) (FLASH_BASE + 2);
83 else
84 c2 = *(volatile unsigned char *) (FLASH_BASE + 1);
86 c2 += (c1 << 8);
89 * set it back to read mode
91 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
93 if (c2 == KFLASH_ID4)
94 gbFlashSize = KFLASH_SIZE4;
96 return c2;
99 static int flash_ioctl(struct inode *inodep, struct file *filep, unsigned int cmd, unsigned long arg)
101 switch (cmd) {
102 case CMD_WRITE_DISABLE:
103 gbWriteBase64Enable = 0;
104 gbWriteEnable = 0;
105 break;
107 case CMD_WRITE_ENABLE:
108 gbWriteEnable = 1;
109 break;
111 case CMD_WRITE_BASE64K_ENABLE:
112 gbWriteBase64Enable = 1;
113 break;
115 default:
116 gbWriteBase64Enable = 0;
117 gbWriteEnable = 0;
118 return -EINVAL;
120 return 0;
123 static ssize_t flash_read(struct file *file, char __user *buf, size_t size,
124 loff_t *ppos)
126 unsigned long p = *ppos;
127 unsigned int count = size;
128 int ret = 0;
130 if (flashdebug)
131 printk(KERN_DEBUG "flash_read: flash_read: offset=0x%lX, "
132 "buffer=%p, count=0x%X.\n", p, buf, count);
134 if (count)
135 ret = -ENXIO;
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))
145 return -ERESTARTSYS;
147 ret = copy_to_user(buf, (void *)(FLASH_BASE + p), count);
148 if (ret == 0) {
149 ret = count;
150 *ppos += count;
151 } else
152 ret = -EFAULT;
153 mutex_unlock(&nwflash_mutex);
155 return ret;
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;
163 int written;
164 int nBlock, temp, rc;
165 int i, j;
167 if (flashdebug)
168 printk("flash_write: offset=0x%lX, buffer=0x%p, count=0x%X.\n",
169 p, buf, count);
171 if (!gbWriteEnable)
172 return -EINVAL;
174 if (p < 64 * 1024 && (!gbWriteBase64Enable))
175 return -EINVAL;
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))
187 return -EFAULT;
190 * We now lock against reads and writes. --rmk
192 if (mutex_lock_interruptible(&nwflash_mutex))
193 return -ERESTARTSYS;
195 written = 0;
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)
211 temp -= 1;
213 if (flashdebug)
214 printk(KERN_DEBUG "flash_write: writing %d block(s) "
215 "starting at %d.\n", temp, nBlock);
217 for (; temp; temp--, nBlock++) {
218 if (flashdebug)
219 printk(KERN_DEBUG "flash_write: erasing block %d.\n", nBlock);
222 * first we have to erase the block(s), where we will write...
224 i = 0;
225 j = 0;
226 RetryBlock:
227 do {
228 rc = erase_block(nBlock);
229 i++;
230 } while (rc && i < 10);
232 if (rc) {
233 printk(KERN_ERR "flash_write: erase error %x\n", rc);
234 break;
236 if (flashdebug)
237 printk(KERN_DEBUG "flash_write: writing offset %lX, "
238 "from buf %p, bytes left %X.\n", p, buf,
239 count - written);
242 * write_block will limit write to space left in this block
244 rc = write_block(p, buf, count - written);
245 j++;
248 * if somehow write verify failed? Can't happen??
250 if (!rc) {
252 * retry up to 10 times
254 if (j < 10)
255 goto RetryBlock;
256 else
258 * else quit with error...
260 rc = -1;
263 if (rc < 0) {
264 printk(KERN_ERR "flash_write: write error %X\n", rc);
265 break;
267 p += rc;
268 buf += rc;
269 written += rc;
270 *ppos += rc;
272 if (flashdebug)
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);
283 return written;
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)
297 loff_t ret;
299 lock_kernel();
300 if (flashdebug)
301 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
302 (unsigned int) offset, orig);
304 switch (orig) {
305 case 0:
306 if (offset < 0) {
307 ret = -EINVAL;
308 break;
311 if ((unsigned int) offset > gbFlashSize) {
312 ret = -EINVAL;
313 break;
316 file->f_pos = (unsigned int) offset;
317 ret = file->f_pos;
318 break;
319 case 1:
320 if ((file->f_pos + offset) > gbFlashSize) {
321 ret = -EINVAL;
322 break;
324 if ((file->f_pos + offset) < 0) {
325 ret = -EINVAL;
326 break;
328 file->f_pos += offset;
329 ret = file->f_pos;
330 break;
331 default:
332 ret = -EINVAL;
334 unlock_kernel();
335 return ret;
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;
349 int temp, temp1;
352 * orange LED == erase
354 leds_event(led_amber_on);
357 * reset footbridge to the correct offset 0 (...0..3)
359 *CSR_ROMWRITEREG = 0;
362 * dummy ROM read
364 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
366 kick_open();
368 * reset status if old errors
370 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
373 * erase a block...
374 * aim at the middle of a current block...
376 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + 0x8000 + (nBlock << 16)));
378 * dummy read
380 c1 = *pWritePtr;
382 kick_open();
384 * erase
386 *(volatile unsigned char *) pWritePtr = 0x20;
389 * confirm
391 *(volatile unsigned char *) pWritePtr = 0xD0;
394 * wait 10 ms
396 msleep(10);
399 * wait while erasing in process (up to 10 sec)
401 timeout = jiffies + 10 * HZ;
402 c1 = 0;
403 while (!(c1 & 0x80) && time_before(jiffies, timeout)) {
404 msleep(10);
406 * read any address
408 c1 = *(volatile unsigned char *) (pWritePtr);
409 // printk("Flash_erase: status=%X.\n",c1);
413 * set flash for normal read access
415 kick_open();
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
422 if (c1 & 0x20) {
423 printk(KERN_ERR "flash_erase: err at %p\n", pWritePtr);
426 * reset error
428 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
429 return -2;
433 * just to make sure - verify if erased OK...
435 msleep(10);
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",
442 pWritePtr, temp1);
443 return -1;
447 return 0;
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;
460 unsigned int offset;
461 unsigned long timeout;
462 unsigned long timeout1;
465 * red LED == write
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....
475 offset = p & 0xFFFF;
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))
489 return -EFAULT;
491 WriteRetry:
493 * dummy read
495 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
498 * kick open the write gate
500 kick_open();
503 * program footbridge to the correct offset...0..3
505 *CSR_ROMWRITEREG = (unsigned int) pWritePtr & 3;
508 * write cmd
510 *(volatile unsigned char *) (uAddress) = 0x40;
513 * data to write
515 *(volatile unsigned char *) (uAddress) = c2;
518 * get status
520 *(volatile unsigned char *) (FLASH_BASE + 0x10000) = 0x70;
522 c1 = 0;
525 * wait up to 1 sec for this byte
527 timeout1 = jiffies + 1 * HZ;
530 * while not ready...
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)) {
539 kick_open();
541 * reset err
543 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
545 goto WriteRetry;
548 * switch on read access, as a default flash operation mode
550 kick_open();
552 * read access
554 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
557 * if hardware reports an error writing, and not timeout -
558 * reset the chip and retry
560 if (c1 & 0x10) {
561 kick_open();
563 * reset err
565 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
568 * before timeout?
570 if (time_before(jiffies, timeout)) {
571 if (flashdebug)
572 printk(KERN_DEBUG "write_block: Retrying write at 0x%X)n",
573 pWritePtr - FLASH_BASE);
576 * no LED == waiting
578 leds_event(led_amber_off);
580 * wait couple ms
582 msleep(10);
584 * red LED == write
586 leds_event(led_red_on);
588 goto WriteRetry;
589 } else {
590 printk(KERN_ERR "write_block: timeout at 0x%X\n",
591 pWritePtr - FLASH_BASE);
593 * return error -2
595 return -2;
602 * green LED == read/verify
604 leds_event(led_amber_off);
605 leds_event(led_green_on);
607 msleep(10);
609 pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
611 for (offset = 0; offset < count; offset++) {
612 char c, c1;
613 if (__get_user(c, buf))
614 return -EFAULT;
615 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);
619 return 0;
623 return count;
627 static void kick_open(void)
629 unsigned long flags;
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);
636 cpld_modify(1, 1);
637 spin_unlock_irqrestore(&gpio_lock, flags);
640 * let the ISA bus to catch on...
642 udelay(25);
645 static struct file_operations flash_fops =
647 .owner = THIS_MODULE,
648 .llseek = flash_llseek,
649 .read = flash_read,
650 .write = flash_write,
651 .ioctl = flash_ioctl,
654 static struct miscdevice flash_miscdev =
656 FLASH_MINOR,
657 "nwflash",
658 &flash_fops
661 static int __init nwflash_init(void)
663 int ret = -ENODEV;
665 if (machine_is_netwinder()) {
666 int id;
668 FLASH_BASE = ioremap(DC21285_FLASH, KFLASH_SIZE4);
669 if (!FLASH_BASE)
670 goto out;
672 id = get_flash_id();
673 if ((id != KFLASH_ID) && (id != KFLASH_ID4)) {
674 ret = -ENXIO;
675 iounmap((void *)FLASH_BASE);
676 printk("Flash: incorrect ID 0x%04X.\n", id);
677 goto out;
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);
684 if (ret < 0) {
685 iounmap((void *)FLASH_BASE);
688 out:
689 return ret;
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);