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