VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / nwflash.c
1 /*
2  * Flash memory interface rev.5 driver for the Intel
3  * Flash chips used on the NetWinder.
4  *
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
11  *
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...
15  */
16
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
31 #include <asm/hardware/dec21285.h>
32 #include <asm/io.h>
33 #include <asm/leds.h>
34 #include <asm/mach-types.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37
38 /*****************************************************************************/
39 #include <asm/nwflash.h>
40
41 #define NWFLASH_VERSION "6.4"
42
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);
47
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
52
53 static int flashdebug;          //if set - we will display progress msgs
54
55 static int gbWriteEnable;
56 static int gbWriteBase64Enable;
57 static volatile unsigned char *FLASH_BASE;
58 static int gbFlashSize = KFLASH_SIZE;
59 static DECLARE_MUTEX(nwflash_sem);
60
61 extern spinlock_t gpio_lock;
62
63 /*
64  * the delay routine - it is often required to let the flash "breeze"...
65  */
66 void flash_wait(int timeout)
67 {
68         current->state = TASK_INTERRUPTIBLE;
69         schedule_timeout(timeout);
70 }
71
72 static int get_flash_id(void)
73 {
74         volatile unsigned int c1, c2;
75
76         /*
77          * try to get flash chip ID
78          */
79         kick_open();
80         c2 = inb(0x80);
81         *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x90;
82         udelay(15);
83         c1 = *(volatile unsigned char *) FLASH_BASE;
84         c2 = inb(0x80);
85
86         /*
87          * on 4 Meg flash the second byte is actually at offset 2...
88          */
89         if (c1 == 0xB0)
90                 c2 = *(volatile unsigned char *) (FLASH_BASE + 2);
91         else
92                 c2 = *(volatile unsigned char *) (FLASH_BASE + 1);
93
94         c2 += (c1 << 8);
95
96         /*
97          * set it back to read mode
98          */
99         *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
100
101         if (c2 == KFLASH_ID4)
102                 gbFlashSize = KFLASH_SIZE4;
103
104         return c2;
105 }
106
107 static int flash_ioctl(struct inode *inodep, struct file *filep, unsigned int cmd, unsigned long arg)
108 {
109         switch (cmd) {
110         case CMD_WRITE_DISABLE:
111                 gbWriteBase64Enable = 0;
112                 gbWriteEnable = 0;
113                 break;
114
115         case CMD_WRITE_ENABLE:
116                 gbWriteEnable = 1;
117                 break;
118
119         case CMD_WRITE_BASE64K_ENABLE:
120                 gbWriteBase64Enable = 1;
121                 break;
122
123         default:
124                 gbWriteBase64Enable = 0;
125                 gbWriteEnable = 0;
126                 return -EINVAL;
127         }
128         return 0;
129 }
130
131 static ssize_t flash_read(struct file *file, char __user *buf, size_t size,
132                           loff_t *ppos)
133 {
134         unsigned long p = *ppos;
135         unsigned int count = size;
136         int ret = 0;
137
138         if (flashdebug)
139                 printk(KERN_DEBUG "flash_read: flash_read: offset=0x%lX, "
140                        "buffer=%p, count=0x%X.\n", p, buf, count);
141
142         if (count)
143                 ret = -ENXIO;
144
145         if (p < gbFlashSize) {
146                 if (count > gbFlashSize - p)
147                         count = gbFlashSize - p;
148
149                 /*
150                  * We now lock against reads and writes. --rmk
151                  */
152                 if (down_interruptible(&nwflash_sem))
153                         return -ERESTARTSYS;
154
155                 ret = copy_to_user(buf, (void *)(FLASH_BASE + p), count);
156                 if (ret == 0) {
157                         ret = count;
158                         *ppos += count;
159                 } else
160                         ret = -EFAULT;
161                 up(&nwflash_sem);
162         }
163         return ret;
164 }
165
166 static ssize_t flash_write(struct file *file, const char __user *buf,
167                            size_t size, loff_t * ppos)
168 {
169         unsigned long p = *ppos;
170         unsigned int count = size;
171         int written;
172         int nBlock, temp, rc;
173         int i, j;
174
175         if (flashdebug)
176                 printk("flash_write: offset=0x%lX, buffer=0x%p, count=0x%X.\n",
177                        p, buf, count);
178
179         if (!gbWriteEnable)
180                 return -EINVAL;
181
182         if (p < 64 * 1024 && (!gbWriteBase64Enable))
183                 return -EINVAL;
184
185         /*
186          * check for out of range pos or count
187          */
188         if (p >= gbFlashSize)
189                 return count ? -ENXIO : 0;
190
191         if (count > gbFlashSize - p)
192                 count = gbFlashSize - p;
193                         
194         if (verify_area(VERIFY_READ, buf, count))
195                 return -EFAULT;
196
197         /*
198          * We now lock against reads and writes. --rmk
199          */
200         if (down_interruptible(&nwflash_sem))
201                 return -ERESTARTSYS;
202
203         written = 0;
204
205         leds_event(led_claim);
206         leds_event(led_green_on);
207
208         nBlock = (int) p >> 16; //block # of 64K bytes
209
210         /*
211          * # of 64K blocks to erase and write
212          */
213         temp = ((int) (p + count) >> 16) - nBlock + 1;
214
215         /*
216          * write ends at exactly 64k boundary?
217          */
218         if (((int) (p + count) & 0xFFFF) == 0)
219                 temp -= 1;
220
221         if (flashdebug)
222                 printk(KERN_DEBUG "flash_write: writing %d block(s) "
223                         "starting at %d.\n", temp, nBlock);
224
225         for (; temp; temp--, nBlock++) {
226                 if (flashdebug)
227                         printk(KERN_DEBUG "flash_write: erasing block %d.\n", nBlock);
228
229                 /*
230                  * first we have to erase the block(s), where we will write...
231                  */
232                 i = 0;
233                 j = 0;
234           RetryBlock:
235                 do {
236                         rc = erase_block(nBlock);
237                         i++;
238                 } while (rc && i < 10);
239
240                 if (rc) {
241                         printk(KERN_ERR "flash_write: erase error %x\n", rc);
242                         break;
243                 }
244                 if (flashdebug)
245                         printk(KERN_DEBUG "flash_write: writing offset %lX, "
246                                "from buf %p, bytes left %X.\n", p, buf,
247                                count - written);
248
249                 /*
250                  * write_block will limit write to space left in this block
251                  */
252                 rc = write_block(p, buf, count - written);
253                 j++;
254
255                 /*
256                  * if somehow write verify failed? Can't happen??
257                  */
258                 if (!rc) {
259                         /*
260                          * retry up to 10 times
261                          */
262                         if (j < 10)
263                                 goto RetryBlock;
264                         else
265                                 /*
266                                  * else quit with error...
267                                  */
268                                 rc = -1;
269
270                 }
271                 if (rc < 0) {
272                         printk(KERN_ERR "flash_write: write error %X\n", rc);
273                         break;
274                 }
275                 p += rc;
276                 buf += rc;
277                 written += rc;
278                 *ppos += rc;
279
280                 if (flashdebug)
281                         printk(KERN_DEBUG "flash_write: written 0x%X bytes OK.\n", written);
282         }
283
284         /*
285          * restore reg on exit
286          */
287         leds_event(led_release);
288
289         up(&nwflash_sem);
290
291         return written;
292 }
293
294
295 /*
296  * The memory devices use the full 32/64 bits of the offset, and so we cannot
297  * check against negative addresses: they are ok. The return value is weird,
298  * though, in that case (0).
299  *
300  * also note that seeking relative to the "end of file" isn't supported:
301  * it has no meaning, so it returns -EINVAL.
302  */
303 static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
304 {
305         loff_t ret;
306
307         lock_kernel();
308         if (flashdebug)
309                 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
310                        (unsigned int) offset, orig);
311
312         switch (orig) {
313         case 0:
314                 if (offset < 0) {
315                         ret = -EINVAL;
316                         break;
317                 }
318
319                 if ((unsigned int) offset > gbFlashSize) {
320                         ret = -EINVAL;
321                         break;
322                 }
323
324                 file->f_pos = (unsigned int) offset;
325                 ret = file->f_pos;
326                 break;
327         case 1:
328                 if ((file->f_pos + offset) > gbFlashSize) {
329                         ret = -EINVAL;
330                         break;
331                 }
332                 if ((file->f_pos + offset) < 0) {
333                         ret = -EINVAL;
334                         break;
335                 }
336                 file->f_pos += offset;
337                 ret = file->f_pos;
338                 break;
339         default:
340                 ret = -EINVAL;
341         }
342         unlock_kernel();
343         return ret;
344 }
345
346
347 /*
348  * assume that main Write routine did the parameter checking...
349  * so just go ahead and erase, what requested!
350  */
351
352 static int erase_block(int nBlock)
353 {
354         volatile unsigned int c1;
355         volatile unsigned char *pWritePtr;
356         unsigned long timeout;
357         int temp, temp1;
358
359         /*
360          * orange LED == erase
361          */
362         leds_event(led_amber_on);
363
364         /*
365          * reset footbridge to the correct offset 0 (...0..3)
366          */
367         *CSR_ROMWRITEREG = 0;
368
369         /*
370          * dummy ROM read
371          */
372         c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
373
374         kick_open();
375         /*
376          * reset status if old errors
377          */
378         *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
379
380         /*
381          * erase a block...
382          * aim at the middle of a current block...
383          */
384         pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + 0x8000 + (nBlock << 16)));
385         /*
386          * dummy read
387          */
388         c1 = *pWritePtr;
389
390         kick_open();
391         /*
392          * erase
393          */
394         *(volatile unsigned char *) pWritePtr = 0x20;
395
396         /*
397          * confirm
398          */
399         *(volatile unsigned char *) pWritePtr = 0xD0;
400
401         /*
402          * wait 10 ms
403          */
404         flash_wait(HZ / 100);
405
406         /*
407          * wait while erasing in process (up to 10 sec)
408          */
409         timeout = jiffies + 10 * HZ;
410         c1 = 0;
411         while (!(c1 & 0x80) && time_before(jiffies, timeout)) {
412                 flash_wait(HZ / 100);
413                 /*
414                  * read any address
415                  */
416                 c1 = *(volatile unsigned char *) (pWritePtr);
417                 //              printk("Flash_erase: status=%X.\n",c1);
418         }
419
420         /*
421          * set flash for normal read access
422          */
423         kick_open();
424 //      *(volatile unsigned char*)(FLASH_BASE+0x8000) = 0xFF;
425         *(volatile unsigned char *) pWritePtr = 0xFF;   //back to normal operation
426
427         /*
428          * check if erase errors were reported
429          */
430         if (c1 & 0x20) {
431                 printk(KERN_ERR "flash_erase: err at %p\n", pWritePtr);
432
433                 /*
434                  * reset error
435                  */
436                 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
437                 return -2;
438         }
439
440         /*
441          * just to make sure - verify if erased OK...
442          */
443         flash_wait(HZ / 100);
444
445         pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + (nBlock << 16)));
446
447         for (temp = 0; temp < 16 * 1024; temp++, pWritePtr += 4) {
448                 if ((temp1 = *(volatile unsigned int *) pWritePtr) != 0xFFFFFFFF) {
449                         printk(KERN_ERR "flash_erase: verify err at %p = %X\n",
450                                pWritePtr, temp1);
451                         return -1;
452                 }
453         }
454
455         return 0;
456
457 }
458
459 /*
460  * write_block will limit number of bytes written to the space in this block
461  */
462 static int write_block(unsigned long p, const char __user *buf, int count)
463 {
464         volatile unsigned int c1;
465         volatile unsigned int c2;
466         unsigned char *pWritePtr;
467         unsigned int uAddress;
468         unsigned int offset;
469         unsigned long timeout;
470         unsigned long timeout1;
471
472         /*
473          * red LED == write
474          */
475         leds_event(led_amber_off);
476         leds_event(led_red_on);
477
478         pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
479
480         /*
481          * check if write will end in this block....
482          */
483         offset = p & 0xFFFF;
484
485         if (offset + count > 0x10000)
486                 count = 0x10000 - offset;
487
488         /*
489          * wait up to 30 sec for this block
490          */
491         timeout = jiffies + 30 * HZ;
492
493         for (offset = 0; offset < count; offset++, pWritePtr++) {
494                 uAddress = (unsigned int) pWritePtr;
495                 uAddress &= 0xFFFFFFFC;
496                 if (__get_user(c2, buf + offset))
497                         return -EFAULT;
498
499           WriteRetry:
500                 /*
501                  * dummy read
502                  */
503                 c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
504
505                 /*
506                  * kick open the write gate
507                  */
508                 kick_open();
509
510                 /*
511                  * program footbridge to the correct offset...0..3
512                  */
513                 *CSR_ROMWRITEREG = (unsigned int) pWritePtr & 3;
514
515                 /*
516                  * write cmd
517                  */
518                 *(volatile unsigned char *) (uAddress) = 0x40;
519
520                 /*
521                  * data to write
522                  */
523                 *(volatile unsigned char *) (uAddress) = c2;
524
525                 /*
526                  * get status
527                  */
528                 *(volatile unsigned char *) (FLASH_BASE + 0x10000) = 0x70;
529
530                 c1 = 0;
531
532                 /*
533                  * wait up to 1 sec for this byte
534                  */
535                 timeout1 = jiffies + 1 * HZ;
536
537                 /*
538                  * while not ready...
539                  */
540                 while (!(c1 & 0x80) && time_before(jiffies, timeout1))
541                         c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
542
543                 /*
544                  * if timeout getting status
545                  */
546                 if (time_after_eq(jiffies, timeout1)) {
547                         kick_open();
548                         /*
549                          * reset err
550                          */
551                         *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
552
553                         goto WriteRetry;
554                 }
555                 /*
556                  * switch on read access, as a default flash operation mode
557                  */
558                 kick_open();
559                 /*
560                  * read access
561                  */
562                 *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
563
564                 /*
565                  * if hardware reports an error writing, and not timeout - 
566                  * reset the chip and retry
567                  */
568                 if (c1 & 0x10) {
569                         kick_open();
570                         /*
571                          * reset err
572                          */
573                         *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
574
575                         /*
576                          * before timeout?
577                          */
578                         if (time_before(jiffies, timeout)) {
579                                 if (flashdebug)
580                                         printk(KERN_DEBUG "write_block: Retrying write at 0x%X)n",
581                                                pWritePtr - FLASH_BASE);
582
583                                 /*
584                                  * no LED == waiting
585                                  */
586                                 leds_event(led_amber_off);
587                                 /*
588                                  * wait couple ms
589                                  */
590                                 flash_wait(HZ / 100);
591                                 /*
592                                  * red LED == write
593                                  */
594                                 leds_event(led_red_on);
595
596                                 goto WriteRetry;
597                         } else {
598                                 printk(KERN_ERR "write_block: timeout at 0x%X\n",
599                                        pWritePtr - FLASH_BASE);
600                                 /*
601                                  * return error -2
602                                  */
603                                 return -2;
604
605                         }
606                 }
607         }
608
609         /*
610          * green LED == read/verify
611          */
612         leds_event(led_amber_off);
613         leds_event(led_green_on);
614
615         flash_wait(HZ / 100);
616
617         pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
618
619         for (offset = 0; offset < count; offset++) {
620                 char c, c1;
621                 if (__get_user(c, buf))
622                         return -EFAULT;
623                 buf++;
624                 if ((c1 = *pWritePtr++) != c) {
625                         printk(KERN_ERR "write_block: verify error at 0x%X (%02X!=%02X)\n",
626                                pWritePtr - FLASH_BASE, c1, c);
627                         return 0;
628                 }
629         }
630
631         return count;
632 }
633
634
635 static void kick_open(void)
636 {
637         unsigned long flags;
638
639         /*
640          * we want to write a bit pattern XXX1 to Xilinx to enable
641          * the write gate, which will be open for about the next 2ms.
642          */
643         spin_lock_irqsave(&gpio_lock, flags);
644         cpld_modify(1, 1);
645         spin_unlock_irqrestore(&gpio_lock, flags);
646
647         /*
648          * let the ISA bus to catch on...
649          */
650         udelay(25);
651 }
652
653 static struct file_operations flash_fops =
654 {
655         .owner          = THIS_MODULE,
656         .llseek         = flash_llseek,
657         .read           = flash_read,
658         .write          = flash_write,
659         .ioctl          = flash_ioctl,
660 };
661
662 static struct miscdevice flash_miscdev =
663 {
664         FLASH_MINOR,
665         "nwflash",
666         &flash_fops
667 };
668
669 static int __init nwflash_init(void)
670 {
671         int ret = -ENODEV;
672
673         if (machine_is_netwinder()) {
674                 int id;
675
676                 FLASH_BASE = ioremap(DC21285_FLASH, KFLASH_SIZE4);
677                 if (!FLASH_BASE)
678                         goto out;
679
680                 id = get_flash_id();
681                 if ((id != KFLASH_ID) && (id != KFLASH_ID4)) {
682                         ret = -ENXIO;
683                         iounmap((void *)FLASH_BASE);
684                         printk("Flash: incorrect ID 0x%04X.\n", id);
685                         goto out;
686                 }
687
688                 printk("Flash ROM driver v.%s, flash device ID 0x%04X, size %d Mb.\n",
689                        NWFLASH_VERSION, id, gbFlashSize / (1024 * 1024));
690
691                 ret = misc_register(&flash_miscdev);
692                 if (ret < 0) {
693                         iounmap((void *)FLASH_BASE);
694                 }
695         }
696 out:
697         return ret;
698 }
699
700 static void __exit nwflash_exit(void)
701 {
702         misc_deregister(&flash_miscdev);
703         iounmap((void *)FLASH_BASE);
704 }
705
706 MODULE_LICENSE("GPL");
707
708 MODULE_PARM(flashdebug, "i");
709
710 module_init(nwflash_init);
711 module_exit(nwflash_exit);