ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / chips / sharp.c
1 /*
2  * MTD chip driver for pre-CFI Sharp flash chips
3  *
4  * Copyright 2000,2001 David A. Schleef <ds@schleef.org>
5  *           2000,2001 Lineo, Inc.
6  *
7  * $Id: sharp.c,v 1.12 2003/05/28 15:39:52 dwmw2 Exp $
8  *
9  * Devices supported:
10  *   LH28F016SCT Symmetrical block flash memory, 2Mx8
11  *   LH28F008SCT Symmetrical block flash memory, 1Mx8
12  *
13  * Documentation:
14  *   http://www.sharpmeg.com/datasheets/memic/flashcmp/
15  *   http://www.sharpmeg.com/datasheets/memic/flashcmp/01symf/16m/016sctl9.pdf
16  *   016sctl9.pdf
17  *
18  * Limitations:
19  *   This driver only supports 4x1 arrangement of chips.
20  *   Not tested on anything but PowerPC.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/mtd/map.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/cfi.h>
32 #include <linux/delay.h>
33
34 #define CMD_RESET               0xffffffff
35 #define CMD_READ_ID             0x90909090
36 #define CMD_READ_STATUS         0x70707070
37 #define CMD_CLEAR_STATUS        0x50505050
38 #define CMD_BLOCK_ERASE_1       0x20202020
39 #define CMD_BLOCK_ERASE_2       0xd0d0d0d0
40 #define CMD_BYTE_WRITE          0x40404040
41 #define CMD_SUSPEND             0xb0b0b0b0
42 #define CMD_RESUME              0xd0d0d0d0
43 #define CMD_SET_BLOCK_LOCK_1    0x60606060
44 #define CMD_SET_BLOCK_LOCK_2    0x01010101
45 #define CMD_SET_MASTER_LOCK_1   0x60606060
46 #define CMD_SET_MASTER_LOCK_2   0xf1f1f1f1
47 #define CMD_CLEAR_BLOCK_LOCKS_1 0x60606060
48 #define CMD_CLEAR_BLOCK_LOCKS_2 0xd0d0d0d0
49
50 #define SR_READY                0x80808080 // 1 = ready
51 #define SR_ERASE_SUSPEND        0x40404040 // 1 = block erase suspended
52 #define SR_ERROR_ERASE          0x20202020 // 1 = error in block erase or clear lock bits
53 #define SR_ERROR_WRITE          0x10101010 // 1 = error in byte write or set lock bit
54 #define SR_VPP                  0x08080808 // 1 = Vpp is low
55 #define SR_WRITE_SUSPEND        0x04040404 // 1 = byte write suspended
56 #define SR_PROTECT              0x02020202 // 1 = lock bit set
57 #define SR_RESERVED             0x01010101
58
59 #define SR_ERRORS (SR_ERROR_ERASE|SR_ERROR_WRITE|SR_VPP|SR_PROTECT)
60
61 /* Configuration options */
62
63 #undef AUTOUNLOCK  /* automatically unlocks blocks before erasing */
64
65 struct mtd_info *sharp_probe(struct map_info *);
66
67 static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd);
68
69 static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
70         size_t *retlen, u_char *buf);
71 static int sharp_write(struct mtd_info *mtd, loff_t from, size_t len,
72         size_t *retlen, const u_char *buf);
73 static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr);
74 static void sharp_sync(struct mtd_info *mtd);
75 static int sharp_suspend(struct mtd_info *mtd);
76 static void sharp_resume(struct mtd_info *mtd);
77 static void sharp_destroy(struct mtd_info *mtd);
78
79 static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
80         unsigned long adr, __u32 datum);
81 static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
82         unsigned long adr);
83 #ifdef AUTOUNLOCK
84 static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
85         unsigned long adr);
86 #endif
87
88
89 struct sharp_info{
90         struct flchip *chip;
91         int bogus;
92         int chipshift;
93         int numchips;
94         struct flchip chips[1];
95 };
96
97 struct mtd_info *sharp_probe(struct map_info *map);
98 static void sharp_destroy(struct mtd_info *mtd);
99
100 static struct mtd_chip_driver sharp_chipdrv = {
101         .probe          = sharp_probe,
102         .destroy        = sharp_destroy,
103         .name           = "sharp",
104         .module         = THIS_MODULE
105 };
106
107
108 struct mtd_info *sharp_probe(struct map_info *map)
109 {
110         struct mtd_info *mtd = NULL;
111         struct sharp_info *sharp = NULL;
112         int width;
113
114         mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
115         if(!mtd)
116                 return NULL;
117
118         sharp = kmalloc(sizeof(*sharp), GFP_KERNEL);
119         if(!sharp) {
120                 kfree(mtd);
121                 return NULL;
122         }
123
124         memset(mtd, 0, sizeof(*mtd));
125
126         width = sharp_probe_map(map,mtd);
127         if(!width){
128                 kfree(mtd);
129                 kfree(sharp);
130                 return NULL;
131         }
132
133         mtd->priv = map;
134         mtd->type = MTD_NORFLASH;
135         mtd->erase = sharp_erase;
136         mtd->read = sharp_read;
137         mtd->write = sharp_write;
138         mtd->sync = sharp_sync;
139         mtd->suspend = sharp_suspend;
140         mtd->resume = sharp_resume;
141         mtd->flags = MTD_CAP_NORFLASH;
142         mtd->name = map->name;
143
144         memset(sharp, 0, sizeof(*sharp));
145         sharp->chipshift = 23;
146         sharp->numchips = 1;
147         sharp->chips[0].start = 0;
148         sharp->chips[0].state = FL_READY;
149         sharp->chips[0].mutex = &sharp->chips[0]._spinlock;
150         sharp->chips[0].word_write_time = 0;
151         init_waitqueue_head(&sharp->chips[0].wq);
152         spin_lock_init(&sharp->chips[0]._spinlock);
153
154         map->fldrv = &sharp_chipdrv;
155         map->fldrv_priv = sharp;
156
157         MOD_INC_USE_COUNT;
158         return mtd;
159 }
160
161 static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd)
162 {
163         unsigned long tmp;
164         unsigned long base = 0;
165         u32 read0, read4;
166         int width = 4;
167
168         tmp = map_read32(map, base+0);
169
170         map_write32(map, CMD_READ_ID, base+0);
171
172         read0=map_read32(map, base+0);
173         read4=map_read32(map, base+4);
174         if(read0 == 0x89898989){
175                 printk("Looks like sharp flash\n");
176                 switch(read4){
177                 case 0xaaaaaaaa:
178                 case 0xa0a0a0a0:
179                         /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/
180                         /* a0 - LH28F016SCT-Z4  2Mx8, 32 64k blocks*/
181                         mtd->erasesize = 0x10000 * width;
182                         mtd->size = 0x200000 * width;
183                         return width;
184                 case 0xa6a6a6a6:
185                         /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/
186                         /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/
187                         mtd->erasesize = 0x10000 * width;
188                         mtd->size = 0x100000 * width;
189                         return width;
190 #if 0
191                 case 0x00000000: /* unknown */
192                         /* XX - LH28F004SCT 512kx8, 8 64k blocks*/
193                         mtd->erasesize = 0x10000 * width;
194                         mtd->size = 0x80000 * width;
195                         return width;
196 #endif
197                 default:
198                         printk("Sort-of looks like sharp flash, 0x%08x 0x%08x\n",
199                                 read0,read4);
200                 }
201         }else if((map_read32(map, base+0) == CMD_READ_ID)){
202                 /* RAM, probably */
203                 printk("Looks like RAM\n");
204                 map_write32(map, tmp, base+0);
205         }else{
206                 printk("Doesn't look like sharp flash, 0x%08x 0x%08x\n",
207                         read0,read4);
208         }
209
210         return 0;
211 }
212
213 /* This function returns with the chip->mutex lock held. */
214 static int sharp_wait(struct map_info *map, struct flchip *chip)
215 {
216         __u16 status;
217         unsigned long timeo = jiffies + HZ;
218         DECLARE_WAITQUEUE(wait, current);
219         int adr = 0;
220
221 retry:
222         spin_lock_bh(chip->mutex);
223
224         switch(chip->state){
225         case FL_READY:
226                 map_write32(map,CMD_READ_STATUS,adr);
227                 chip->state = FL_STATUS;
228         case FL_STATUS:
229                 status = map_read32(map,adr);
230 //printk("status=%08x\n",status);
231
232                 udelay(100);
233                 if((status & SR_READY)!=SR_READY){
234 //printk(".status=%08x\n",status);
235                         udelay(100);
236                 }
237                 break;
238         default:
239                 printk("Waiting for chip\n");
240
241                 set_current_state(TASK_INTERRUPTIBLE);
242                 add_wait_queue(&chip->wq, &wait);
243
244                 spin_unlock_bh(chip->mutex);
245
246                 schedule();
247                 remove_wait_queue(&chip->wq, &wait);
248
249                 if(signal_pending(current))
250                         return -EINTR;
251
252                 timeo = jiffies + HZ;
253
254                 goto retry;
255         }
256
257         map_write32(map,CMD_RESET, adr);
258
259         chip->state = FL_READY;
260
261         return 0;
262 }
263
264 static void sharp_release(struct flchip *chip)
265 {
266         wake_up(&chip->wq);
267         spin_unlock_bh(chip->mutex);
268 }
269
270 static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
271         size_t *retlen, u_char *buf)
272 {
273         struct map_info *map = mtd->priv;
274         struct sharp_info *sharp = map->fldrv_priv;
275         int chipnum;
276         int ret = 0;
277         int ofs = 0;
278
279         chipnum = (from >> sharp->chipshift);
280         ofs = from & ((1 << sharp->chipshift)-1);
281
282         *retlen = 0;
283
284         while(len){
285                 unsigned long thislen;
286
287                 if(chipnum>=sharp->numchips)
288                         break;
289
290                 thislen = len;
291                 if(ofs+thislen >= (1<<sharp->chipshift))
292                         thislen = (1<<sharp->chipshift) - ofs;
293
294                 ret = sharp_wait(map,&sharp->chips[chipnum]);
295                 if(ret<0)
296                         break;
297
298                 map_copy_from(map,buf,ofs,thislen);
299
300                 sharp_release(&sharp->chips[chipnum]);
301
302                 *retlen += thislen;
303                 len -= thislen;
304                 buf += thislen;
305
306                 ofs = 0;
307                 chipnum++;
308         }
309         return ret;
310 }
311
312 static int sharp_write(struct mtd_info *mtd, loff_t to, size_t len,
313         size_t *retlen, const u_char *buf)
314 {
315         struct map_info *map = mtd->priv;
316         struct sharp_info *sharp = map->fldrv_priv;
317         int ret = 0;
318         int i,j;
319         int chipnum;
320         unsigned long ofs;
321         union { u32 l; unsigned char uc[4]; } tbuf;
322
323         *retlen = 0;
324
325         while(len){
326                 tbuf.l = 0xffffffff;
327                 chipnum = to >> sharp->chipshift;
328                 ofs = to & ((1<<sharp->chipshift)-1);
329
330                 j=0;
331                 for(i=ofs&3;i<4 && len;i++){
332                         tbuf.uc[i] = *buf;
333                         buf++;
334                         to++;
335                         len--;
336                         j++;
337                 }
338                 sharp_write_oneword(map, &sharp->chips[chipnum], ofs&~3, tbuf.l);
339                 if(ret<0)
340                         return ret;
341                 (*retlen)+=j;
342         }
343
344         return 0;
345 }
346
347 static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
348         unsigned long adr, __u32 datum)
349 {
350         int ret;
351         int timeo;
352         int try;
353         int i;
354         int status = 0;
355
356         ret = sharp_wait(map,chip);
357
358         for(try=0;try<10;try++){
359                 map_write32(map,CMD_BYTE_WRITE,adr);
360                 /* cpu_to_le32 -> hack to fix the writel be->le conversion */
361                 map_write32(map,cpu_to_le32(datum),adr);
362
363                 chip->state = FL_WRITING;
364
365                 timeo = jiffies + (HZ/2);
366
367                 map_write32(map,CMD_READ_STATUS,adr);
368                 for(i=0;i<100;i++){
369                         status = map_read32(map,adr);
370                         if((status & SR_READY)==SR_READY)
371                                 break;
372                 }
373                 if(i==100){
374                         printk("sharp: timed out writing\n");
375                 }
376
377                 if(!(status&SR_ERRORS))
378                         break;
379
380                 printk("sharp: error writing byte at addr=%08lx status=%08x\n",adr,status);
381
382                 map_write32(map,CMD_CLEAR_STATUS,adr);
383         }
384         map_write32(map,CMD_RESET,adr);
385         chip->state = FL_READY;
386
387         wake_up(&chip->wq);
388         spin_unlock_bh(chip->mutex);
389
390         return 0;
391 }
392
393 static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr)
394 {
395         struct map_info *map = mtd->priv;
396         struct sharp_info *sharp = map->fldrv_priv;
397         unsigned long adr,len;
398         int chipnum, ret=0;
399
400 //printk("sharp_erase()\n");
401         if(instr->addr & (mtd->erasesize - 1))
402                 return -EINVAL;
403         if(instr->len & (mtd->erasesize - 1))
404                 return -EINVAL;
405         if(instr->len + instr->addr > mtd->size)
406                 return -EINVAL;
407
408         chipnum = instr->addr >> sharp->chipshift;
409         adr = instr->addr & ((1<<sharp->chipshift)-1);
410         len = instr->len;
411
412         while(len){
413                 ret = sharp_erase_oneblock(map, &sharp->chips[chipnum], adr);
414                 if(ret)return ret;
415
416                 adr += mtd->erasesize;
417                 len -= mtd->erasesize;
418                 if(adr >> sharp->chipshift){
419                         adr = 0;
420                         chipnum++;
421                         if(chipnum>=sharp->numchips)
422                                 break;
423                 }
424         }
425
426         instr->state = MTD_ERASE_DONE;
427         if(instr->callback)
428                 instr->callback(instr);
429
430         return 0;
431 }
432
433 static int sharp_do_wait_for_ready(struct map_info *map, struct flchip *chip,
434         unsigned long adr)
435 {
436         int ret;
437         unsigned long timeo;
438         int status;
439         DECLARE_WAITQUEUE(wait, current);
440
441         map_write32(map,CMD_READ_STATUS,adr);
442         status = map_read32(map,adr);
443
444         timeo = jiffies + HZ;
445
446         while(time_before(jiffies, timeo)){
447                 map_write32(map,CMD_READ_STATUS,adr);
448                 status = map_read32(map,adr);
449                 if((status & SR_READY)==SR_READY){
450                         ret = 0;
451                         goto out;
452                 }
453                 set_current_state(TASK_INTERRUPTIBLE);
454                 add_wait_queue(&chip->wq, &wait);
455
456                 //spin_unlock_bh(chip->mutex);
457
458                 schedule_timeout(1);
459                 schedule();
460                 remove_wait_queue(&chip->wq, &wait);
461
462                 //spin_lock_bh(chip->mutex);
463                 
464                 if (signal_pending(current)){
465                         ret = -EINTR;
466                         goto out;
467                 }
468                 
469         }
470         ret = -ETIME;
471 out:
472         return ret;
473 }
474
475 static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
476         unsigned long adr)
477 {
478         int ret;
479         //int timeo;
480         int status;
481         //int i;
482
483 //printk("sharp_erase_oneblock()\n");
484
485 #ifdef AUTOUNLOCK
486         /* This seems like a good place to do an unlock */
487         sharp_unlock_oneblock(map,chip,adr);
488 #endif
489
490         map_write32(map,CMD_BLOCK_ERASE_1,adr);
491         map_write32(map,CMD_BLOCK_ERASE_2,adr);
492
493         chip->state = FL_ERASING;
494
495         ret = sharp_do_wait_for_ready(map,chip,adr);
496         if(ret<0)return ret;
497
498         map_write32(map,CMD_READ_STATUS,adr);
499         status = map_read32(map,adr);
500
501         if(!(status&SR_ERRORS)){
502                 map_write32(map,CMD_RESET,adr);
503                 chip->state = FL_READY;
504                 //spin_unlock_bh(chip->mutex);
505                 return 0;
506         }
507
508         printk("sharp: error erasing block at addr=%08lx status=%08x\n",adr,status);
509         map_write32(map,CMD_CLEAR_STATUS,adr);
510
511         //spin_unlock_bh(chip->mutex);
512
513         return -EIO;
514 }
515
516 #ifdef AUTOUNLOCK
517 static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
518         unsigned long adr)
519 {
520         int i;
521         int status;
522
523         map_write32(map,CMD_CLEAR_BLOCK_LOCKS_1,adr);
524         map_write32(map,CMD_CLEAR_BLOCK_LOCKS_2,adr);
525
526         udelay(100);
527
528         status = map_read32(map,adr);
529         printk("status=%08x\n",status);
530
531         for(i=0;i<1000;i++){
532                 //map_write32(map,CMD_READ_STATUS,adr);
533                 status = map_read32(map,adr);
534                 if((status & SR_READY)==SR_READY)
535                         break;
536                 udelay(100);
537         }
538         if(i==1000){
539                 printk("sharp: timed out unlocking block\n");
540         }
541
542         if(!(status&SR_ERRORS)){
543                 map_write32(map,CMD_RESET,adr);
544                 chip->state = FL_READY;
545                 return;
546         }
547
548         printk("sharp: error unlocking block at addr=%08lx status=%08x\n",adr,status);
549         map_write32(map,CMD_CLEAR_STATUS,adr);
550 }
551 #endif
552
553 static void sharp_sync(struct mtd_info *mtd)
554 {
555         //printk("sharp_sync()\n");
556 }
557
558 static int sharp_suspend(struct mtd_info *mtd)
559 {
560         printk("sharp_suspend()\n");
561         return -EINVAL;
562 }
563
564 static void sharp_resume(struct mtd_info *mtd)
565 {
566         printk("sharp_resume()\n");
567         
568 }
569
570 static void sharp_destroy(struct mtd_info *mtd)
571 {
572         printk("sharp_destroy()\n");
573
574 }
575
576 int __init sharp_probe_init(void)
577 {
578         printk("MTD Sharp chip driver <ds@lineo.com>\n");
579
580         register_mtd_chip_driver(&sharp_chipdrv);
581
582         return 0;
583 }
584
585 static void __exit sharp_probe_exit(void)
586 {
587         unregister_mtd_chip_driver(&sharp_chipdrv);
588 }
589
590 module_init(sharp_probe_init);
591 module_exit(sharp_probe_exit);
592
593
594 MODULE_LICENSE("GPL");
595 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
596 MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips");