2 * MTD chip driver for pre-CFI Sharp flash chips
4 * Copyright 2000,2001 David A. Schleef <ds@schleef.org>
5 * 2000,2001 Lineo, Inc.
7 * $Id: sharp.c,v 1.17 2005/11/29 14:28:28 gleixner Exp $
10 * LH28F016SCT Symmetrical block flash memory, 2Mx8
11 * LH28F008SCT Symmetrical block flash memory, 1Mx8
14 * http://www.sharpmeg.com/datasheets/memic/flashcmp/
15 * http://www.sharpmeg.com/datasheets/memic/flashcmp/01symf/16m/016sctl9.pdf
19 * This driver only supports 4x1 arrangement of chips.
20 * Not tested on anything but PowerPC.
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 #include <linux/init.h>
34 #include <linux/slab.h>
36 #define CMD_RESET 0xffffffff
37 #define CMD_READ_ID 0x90909090
38 #define CMD_READ_STATUS 0x70707070
39 #define CMD_CLEAR_STATUS 0x50505050
40 #define CMD_BLOCK_ERASE_1 0x20202020
41 #define CMD_BLOCK_ERASE_2 0xd0d0d0d0
42 #define CMD_BYTE_WRITE 0x40404040
43 #define CMD_SUSPEND 0xb0b0b0b0
44 #define CMD_RESUME 0xd0d0d0d0
45 #define CMD_SET_BLOCK_LOCK_1 0x60606060
46 #define CMD_SET_BLOCK_LOCK_2 0x01010101
47 #define CMD_SET_MASTER_LOCK_1 0x60606060
48 #define CMD_SET_MASTER_LOCK_2 0xf1f1f1f1
49 #define CMD_CLEAR_BLOCK_LOCKS_1 0x60606060
50 #define CMD_CLEAR_BLOCK_LOCKS_2 0xd0d0d0d0
52 #define SR_READY 0x80808080 // 1 = ready
53 #define SR_ERASE_SUSPEND 0x40404040 // 1 = block erase suspended
54 #define SR_ERROR_ERASE 0x20202020 // 1 = error in block erase or clear lock bits
55 #define SR_ERROR_WRITE 0x10101010 // 1 = error in byte write or set lock bit
56 #define SR_VPP 0x08080808 // 1 = Vpp is low
57 #define SR_WRITE_SUSPEND 0x04040404 // 1 = byte write suspended
58 #define SR_PROTECT 0x02020202 // 1 = lock bit set
59 #define SR_RESERVED 0x01010101
61 #define SR_ERRORS (SR_ERROR_ERASE|SR_ERROR_WRITE|SR_VPP|SR_PROTECT)
63 /* Configuration options */
65 #undef AUTOUNLOCK /* automatically unlocks blocks before erasing */
67 static struct mtd_info
*sharp_probe(struct map_info
*);
69 static int sharp_probe_map(struct map_info
*map
,struct mtd_info
*mtd
);
71 static int sharp_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
72 size_t *retlen
, u_char
*buf
);
73 static int sharp_write(struct mtd_info
*mtd
, loff_t from
, size_t len
,
74 size_t *retlen
, const u_char
*buf
);
75 static int sharp_erase(struct mtd_info
*mtd
, struct erase_info
*instr
);
76 static void sharp_sync(struct mtd_info
*mtd
);
77 static int sharp_suspend(struct mtd_info
*mtd
);
78 static void sharp_resume(struct mtd_info
*mtd
);
79 static void sharp_destroy(struct mtd_info
*mtd
);
81 static int sharp_write_oneword(struct map_info
*map
, struct flchip
*chip
,
82 unsigned long adr
, __u32 datum
);
83 static int sharp_erase_oneblock(struct map_info
*map
, struct flchip
*chip
,
86 static void sharp_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
,
96 struct flchip chips
[1];
99 static void sharp_destroy(struct mtd_info
*mtd
);
101 static struct mtd_chip_driver sharp_chipdrv
= {
102 .probe
= sharp_probe
,
103 .destroy
= sharp_destroy
,
105 .module
= THIS_MODULE
109 static struct mtd_info
*sharp_probe(struct map_info
*map
)
111 struct mtd_info
*mtd
= NULL
;
112 struct sharp_info
*sharp
= NULL
;
115 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
119 sharp
= kzalloc(sizeof(*sharp
), GFP_KERNEL
);
125 width
= sharp_probe_map(map
,mtd
);
133 mtd
->type
= MTD_NORFLASH
;
134 mtd
->erase
= sharp_erase
;
135 mtd
->read
= sharp_read
;
136 mtd
->write
= sharp_write
;
137 mtd
->sync
= sharp_sync
;
138 mtd
->suspend
= sharp_suspend
;
139 mtd
->resume
= sharp_resume
;
140 mtd
->flags
= MTD_CAP_NORFLASH
;
142 mtd
->name
= map
->name
;
144 sharp
->chipshift
= 23;
146 sharp
->chips
[0].start
= 0;
147 sharp
->chips
[0].state
= FL_READY
;
148 sharp
->chips
[0].mutex
= &sharp
->chips
[0]._spinlock
;
149 sharp
->chips
[0].word_write_time
= 0;
150 init_waitqueue_head(&sharp
->chips
[0].wq
);
151 spin_lock_init(&sharp
->chips
[0]._spinlock
);
153 map
->fldrv
= &sharp_chipdrv
;
154 map
->fldrv_priv
= sharp
;
156 __module_get(THIS_MODULE
);
160 static inline void sharp_send_cmd(struct map_info
*map
, unsigned long cmd
, unsigned long adr
)
164 map_write(map
, map_cmd
, adr
);
167 static int sharp_probe_map(struct map_info
*map
,struct mtd_info
*mtd
)
169 map_word tmp
, read0
, read4
;
170 unsigned long base
= 0;
173 tmp
= map_read(map
, base
+0);
175 sharp_send_cmd(map
, CMD_READ_ID
, base
+0);
177 read0
= map_read(map
, base
+0);
178 read4
= map_read(map
, base
+4);
179 if(read0
.x
[0] == 0x89898989){
180 printk("Looks like sharp flash\n");
184 /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/
185 /* a0 - LH28F016SCT-Z4 2Mx8, 32 64k blocks*/
186 mtd
->erasesize
= 0x10000 * width
;
187 mtd
->size
= 0x200000 * width
;
190 /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/
191 /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/
192 mtd
->erasesize
= 0x10000 * width
;
193 mtd
->size
= 0x100000 * width
;
196 case 0x00000000: /* unknown */
197 /* XX - LH28F004SCT 512kx8, 8 64k blocks*/
198 mtd
->erasesize
= 0x10000 * width
;
199 mtd
->size
= 0x80000 * width
;
203 printk("Sort-of looks like sharp flash, 0x%08lx 0x%08lx\n",
204 read0
.x
[0], read4
.x
[0]);
206 }else if((map_read(map
, base
+0).x
[0] == CMD_READ_ID
)){
208 printk("Looks like RAM\n");
209 map_write(map
, tmp
, base
+0);
211 printk("Doesn't look like sharp flash, 0x%08lx 0x%08lx\n",
212 read0
.x
[0], read4
.x
[0]);
218 /* This function returns with the chip->mutex lock held. */
219 static int sharp_wait(struct map_info
*map
, struct flchip
*chip
)
223 unsigned long timeo
= jiffies
+ HZ
;
224 DECLARE_WAITQUEUE(wait
, current
);
228 spin_lock_bh(chip
->mutex
);
232 sharp_send_cmd(map
, CMD_READ_STATUS
, adr
);
233 chip
->state
= FL_STATUS
;
236 status
= map_read(map
, adr
);
237 if((status
.x
[0] & SR_READY
)==SR_READY
)
243 printk("Waiting for chip\n");
245 set_current_state(TASK_INTERRUPTIBLE
);
246 add_wait_queue(&chip
->wq
, &wait
);
248 spin_unlock_bh(chip
->mutex
);
251 remove_wait_queue(&chip
->wq
, &wait
);
253 if(signal_pending(current
))
256 timeo
= jiffies
+ HZ
;
261 sharp_send_cmd(map
, CMD_RESET
, adr
);
263 chip
->state
= FL_READY
;
268 static void sharp_release(struct flchip
*chip
)
271 spin_unlock_bh(chip
->mutex
);
274 static int sharp_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
275 size_t *retlen
, u_char
*buf
)
277 struct map_info
*map
= mtd
->priv
;
278 struct sharp_info
*sharp
= map
->fldrv_priv
;
283 chipnum
= (from
>> sharp
->chipshift
);
284 ofs
= from
& ((1 << sharp
->chipshift
)-1);
289 unsigned long thislen
;
291 if(chipnum
>=sharp
->numchips
)
295 if(ofs
+thislen
>= (1<<sharp
->chipshift
))
296 thislen
= (1<<sharp
->chipshift
) - ofs
;
298 ret
= sharp_wait(map
,&sharp
->chips
[chipnum
]);
302 map_copy_from(map
,buf
,ofs
,thislen
);
304 sharp_release(&sharp
->chips
[chipnum
]);
316 static int sharp_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
317 size_t *retlen
, const u_char
*buf
)
319 struct map_info
*map
= mtd
->priv
;
320 struct sharp_info
*sharp
= map
->fldrv_priv
;
325 union { u32 l
; unsigned char uc
[4]; } tbuf
;
331 chipnum
= to
>> sharp
->chipshift
;
332 ofs
= to
& ((1<<sharp
->chipshift
)-1);
335 for(i
=ofs
&3;i
<4 && len
;i
++){
342 sharp_write_oneword(map
, &sharp
->chips
[chipnum
], ofs
&~3, tbuf
.l
);
351 static int sharp_write_oneword(struct map_info
*map
, struct flchip
*chip
,
352 unsigned long adr
, __u32 datum
)
358 map_word data
, status
;
361 ret
= sharp_wait(map
,chip
);
363 for(try=0;try<10;try++){
364 sharp_send_cmd(map
, CMD_BYTE_WRITE
, adr
);
365 /* cpu_to_le32 -> hack to fix the writel be->le conversion */
366 data
.x
[0] = cpu_to_le32(datum
);
367 map_write(map
, data
, adr
);
369 chip
->state
= FL_WRITING
;
371 timeo
= jiffies
+ (HZ
/2);
373 sharp_send_cmd(map
, CMD_READ_STATUS
, adr
);
375 status
= map_read(map
, adr
);
376 if((status
.x
[0] & SR_READY
) == SR_READY
)
380 printk("sharp: timed out writing\n");
383 if(!(status
.x
[0] & SR_ERRORS
))
386 printk("sharp: error writing byte at addr=%08lx status=%08lx\n", adr
, status
.x
[0]);
388 sharp_send_cmd(map
, CMD_CLEAR_STATUS
, adr
);
390 sharp_send_cmd(map
, CMD_RESET
, adr
);
391 chip
->state
= FL_READY
;
394 spin_unlock_bh(chip
->mutex
);
399 static int sharp_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
401 struct map_info
*map
= mtd
->priv
;
402 struct sharp_info
*sharp
= map
->fldrv_priv
;
403 unsigned long adr
,len
;
406 //printk("sharp_erase()\n");
407 if(instr
->addr
& (mtd
->erasesize
- 1))
409 if(instr
->len
& (mtd
->erasesize
- 1))
411 if(instr
->len
+ instr
->addr
> mtd
->size
)
414 chipnum
= instr
->addr
>> sharp
->chipshift
;
415 adr
= instr
->addr
& ((1<<sharp
->chipshift
)-1);
419 ret
= sharp_erase_oneblock(map
, &sharp
->chips
[chipnum
], adr
);
422 adr
+= mtd
->erasesize
;
423 len
-= mtd
->erasesize
;
424 if(adr
>> sharp
->chipshift
){
427 if(chipnum
>=sharp
->numchips
)
432 instr
->state
= MTD_ERASE_DONE
;
433 mtd_erase_callback(instr
);
438 static int sharp_do_wait_for_ready(struct map_info
*map
, struct flchip
*chip
,
444 DECLARE_WAITQUEUE(wait
, current
);
446 sharp_send_cmd(map
, CMD_READ_STATUS
, adr
);
447 status
= map_read(map
, adr
);
449 timeo
= jiffies
+ HZ
;
451 while(time_before(jiffies
, timeo
)){
452 sharp_send_cmd(map
, CMD_READ_STATUS
, adr
);
453 status
= map_read(map
, adr
);
454 if((status
.x
[0] & SR_READY
)==SR_READY
){
458 set_current_state(TASK_INTERRUPTIBLE
);
459 add_wait_queue(&chip
->wq
, &wait
);
461 //spin_unlock_bh(chip->mutex);
465 remove_wait_queue(&chip
->wq
, &wait
);
467 //spin_lock_bh(chip->mutex);
469 if (signal_pending(current
)){
480 static int sharp_erase_oneblock(struct map_info
*map
, struct flchip
*chip
,
488 //printk("sharp_erase_oneblock()\n");
491 /* This seems like a good place to do an unlock */
492 sharp_unlock_oneblock(map
,chip
,adr
);
495 sharp_send_cmd(map
, CMD_BLOCK_ERASE_1
, adr
);
496 sharp_send_cmd(map
, CMD_BLOCK_ERASE_2
, adr
);
498 chip
->state
= FL_ERASING
;
500 ret
= sharp_do_wait_for_ready(map
,chip
,adr
);
503 sharp_send_cmd(map
, CMD_READ_STATUS
, adr
);
504 status
= map_read(map
, adr
);
506 if(!(status
.x
[0] & SR_ERRORS
)){
507 sharp_send_cmd(map
, CMD_RESET
, adr
);
508 chip
->state
= FL_READY
;
509 //spin_unlock_bh(chip->mutex);
513 printk("sharp: error erasing block at addr=%08lx status=%08lx\n", adr
, status
.x
[0]);
514 sharp_send_cmd(map
, CMD_CLEAR_STATUS
, adr
);
516 //spin_unlock_bh(chip->mutex);
522 static void sharp_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
,
528 sharp_send_cmd(map
, CMD_CLEAR_BLOCK_LOCKS_1
, adr
);
529 sharp_send_cmd(map
, CMD_CLEAR_BLOCK_LOCKS_2
, adr
);
533 status
= map_read(map
, adr
);
534 printk("status=%08lx\n", status
.x
[0]);
537 //sharp_send_cmd(map, CMD_READ_STATUS, adr);
538 status
= map_read(map
, adr
);
539 if((status
.x
[0] & SR_READY
) == SR_READY
)
544 printk("sharp: timed out unlocking block\n");
547 if(!(status
.x
[0] & SR_ERRORS
)){
548 sharp_send_cmd(map
, CMD_RESET
, adr
);
549 chip
->state
= FL_READY
;
553 printk("sharp: error unlocking block at addr=%08lx status=%08lx\n", adr
, status
.x
[0]);
554 sharp_send_cmd(map
, CMD_CLEAR_STATUS
, adr
);
558 static void sharp_sync(struct mtd_info
*mtd
)
560 //printk("sharp_sync()\n");
563 static int sharp_suspend(struct mtd_info
*mtd
)
565 printk("sharp_suspend()\n");
569 static void sharp_resume(struct mtd_info
*mtd
)
571 printk("sharp_resume()\n");
575 static void sharp_destroy(struct mtd_info
*mtd
)
577 printk("sharp_destroy()\n");
581 static int __init
sharp_probe_init(void)
583 printk("MTD Sharp chip driver <ds@lineo.com>\n");
585 register_mtd_chip_driver(&sharp_chipdrv
);
590 static void __exit
sharp_probe_exit(void)
592 unregister_mtd_chip_driver(&sharp_chipdrv
);
595 module_init(sharp_probe_init
);
596 module_exit(sharp_probe_exit
);
599 MODULE_LICENSE("GPL");
600 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
601 MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips");