2 * Common Flash Interface support:
3 * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
5 * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
6 * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
7 * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
9 * 2_by_8 routines added by Simon Munton
11 * 4_by_16 work by Carolyn J. Smith
13 * XIP support hooks by Vitaly Wool (based on code for Intel flash
16 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
20 * $Id: cfi_cmdset_0002.c,v 1.122 2005/11/07 11:14:22 gleixner Exp $
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/init.h>
30 #include <asm/byteorder.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include <linux/mtd/compatmac.h>
37 #include <linux/mtd/map.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/cfi.h>
40 #include <linux/mtd/xip.h>
42 #define AMD_BOOTLOC_BUG
43 #define FORCE_WORD_WRITE 0
45 #define MAX_WORD_RETRIES 3
47 #define MANUFACTURER_AMD 0x0001
48 #define MANUFACTURER_ATMEL 0x001F
49 #define MANUFACTURER_SST 0x00BF
50 #define SST49LF004B 0x0060
51 #define SST49LF040B 0x0050
52 #define SST49LF008A 0x005a
53 #define AT49BV6416 0x00d6
55 static int cfi_amdstd_read (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
56 static int cfi_amdstd_write_words(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
57 static int cfi_amdstd_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
58 static int cfi_amdstd_erase_chip(struct mtd_info
*, struct erase_info
*);
59 static int cfi_amdstd_erase_varsize(struct mtd_info
*, struct erase_info
*);
60 static void cfi_amdstd_sync (struct mtd_info
*);
61 static int cfi_amdstd_suspend (struct mtd_info
*);
62 static void cfi_amdstd_resume (struct mtd_info
*);
63 static int cfi_amdstd_secsi_read (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
65 static void cfi_amdstd_destroy(struct mtd_info
*);
67 struct mtd_info
*cfi_cmdset_0002(struct map_info
*, int);
68 static struct mtd_info
*cfi_amdstd_setup (struct mtd_info
*);
70 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
);
71 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
);
74 static int cfi_atmel_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
75 static int cfi_atmel_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
77 static struct mtd_chip_driver cfi_amdstd_chipdrv
= {
78 .probe
= NULL
, /* Not usable directly */
79 .destroy
= cfi_amdstd_destroy
,
80 .name
= "cfi_cmdset_0002",
85 /* #define DEBUG_CFI_FEATURES */
88 #ifdef DEBUG_CFI_FEATURES
89 static void cfi_tell_features(struct cfi_pri_amdstd
*extp
)
91 const char* erase_suspend
[3] = {
92 "Not supported", "Read only", "Read/write"
94 const char* top_bottom
[6] = {
95 "No WP", "8x8KiB sectors at top & bottom, no WP",
96 "Bottom boot", "Top boot",
97 "Uniform, Bottom WP", "Uniform, Top WP"
100 printk(" Silicon revision: %d\n", extp
->SiliconRevision
>> 1);
101 printk(" Address sensitive unlock: %s\n",
102 (extp
->SiliconRevision
& 1) ? "Not required" : "Required");
104 if (extp
->EraseSuspend
< ARRAY_SIZE(erase_suspend
))
105 printk(" Erase Suspend: %s\n", erase_suspend
[extp
->EraseSuspend
]);
107 printk(" Erase Suspend: Unknown value %d\n", extp
->EraseSuspend
);
109 if (extp
->BlkProt
== 0)
110 printk(" Block protection: Not supported\n");
112 printk(" Block protection: %d sectors per group\n", extp
->BlkProt
);
115 printk(" Temporary block unprotect: %s\n",
116 extp
->TmpBlkUnprotect
? "Supported" : "Not supported");
117 printk(" Block protect/unprotect scheme: %d\n", extp
->BlkProtUnprot
);
118 printk(" Number of simultaneous operations: %d\n", extp
->SimultaneousOps
);
119 printk(" Burst mode: %s\n",
120 extp
->BurstMode
? "Supported" : "Not supported");
121 if (extp
->PageMode
== 0)
122 printk(" Page mode: Not supported\n");
124 printk(" Page mode: %d word page\n", extp
->PageMode
<< 2);
126 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
127 extp
->VppMin
>> 4, extp
->VppMin
& 0xf);
128 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
129 extp
->VppMax
>> 4, extp
->VppMax
& 0xf);
131 if (extp
->TopBottom
< ARRAY_SIZE(top_bottom
))
132 printk(" Top/Bottom Boot Block: %s\n", top_bottom
[extp
->TopBottom
]);
134 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp
->TopBottom
);
138 #ifdef AMD_BOOTLOC_BUG
139 /* Wheee. Bring me the head of someone at AMD. */
140 static void fixup_amd_bootblock(struct mtd_info
*mtd
, void* param
)
142 struct map_info
*map
= mtd
->priv
;
143 struct cfi_private
*cfi
= map
->fldrv_priv
;
144 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
145 __u8 major
= extp
->MajorVersion
;
146 __u8 minor
= extp
->MinorVersion
;
148 if (((major
<< 8) | minor
) < 0x3131) {
149 /* CFI version 1.0 => don't trust bootloc */
150 if (cfi
->id
& 0x80) {
151 printk(KERN_WARNING
"%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map
->name
, cfi
->id
);
152 extp
->TopBottom
= 3; /* top boot */
154 extp
->TopBottom
= 2; /* bottom boot */
160 static void fixup_use_write_buffers(struct mtd_info
*mtd
, void *param
)
162 struct map_info
*map
= mtd
->priv
;
163 struct cfi_private
*cfi
= map
->fldrv_priv
;
164 if (cfi
->cfiq
->BufWriteTimeoutTyp
) {
165 DEBUG(MTD_DEBUG_LEVEL1
, "Using buffer write method\n" );
166 mtd
->write
= cfi_amdstd_write_buffers
;
170 /* Atmel chips don't use the same PRI format as AMD chips */
171 static void fixup_convert_atmel_pri(struct mtd_info
*mtd
, void *param
)
173 struct map_info
*map
= mtd
->priv
;
174 struct cfi_private
*cfi
= map
->fldrv_priv
;
175 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
176 struct cfi_pri_atmel atmel_pri
;
178 memcpy(&atmel_pri
, extp
, sizeof(atmel_pri
));
179 memset((char *)extp
+ 5, 0, sizeof(*extp
) - 5);
181 if (atmel_pri
.Features
& 0x02)
182 extp
->EraseSuspend
= 2;
184 if (atmel_pri
.BottomBoot
)
189 /* burst write mode not supported */
190 cfi
->cfiq
->BufWriteTimeoutTyp
= 0;
191 cfi
->cfiq
->BufWriteTimeoutMax
= 0;
194 static void fixup_use_secsi(struct mtd_info
*mtd
, void *param
)
196 /* Setup for chips with a secsi area */
197 mtd
->read_user_prot_reg
= cfi_amdstd_secsi_read
;
198 mtd
->read_fact_prot_reg
= cfi_amdstd_secsi_read
;
201 static void fixup_use_erase_chip(struct mtd_info
*mtd
, void *param
)
203 struct map_info
*map
= mtd
->priv
;
204 struct cfi_private
*cfi
= map
->fldrv_priv
;
205 if ((cfi
->cfiq
->NumEraseRegions
== 1) &&
206 ((cfi
->cfiq
->EraseRegionInfo
[0] & 0xffff) == 0)) {
207 mtd
->erase
= cfi_amdstd_erase_chip
;
213 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
216 static void fixup_use_atmel_lock(struct mtd_info
*mtd
, void *param
)
218 mtd
->lock
= cfi_atmel_lock
;
219 mtd
->unlock
= cfi_atmel_unlock
;
220 mtd
->flags
|= MTD_POWERUP_LOCK
;
223 static struct cfi_fixup cfi_fixup_table
[] = {
224 { CFI_MFR_ATMEL
, CFI_ID_ANY
, fixup_convert_atmel_pri
, NULL
},
225 #ifdef AMD_BOOTLOC_BUG
226 { CFI_MFR_AMD
, CFI_ID_ANY
, fixup_amd_bootblock
, NULL
},
228 { CFI_MFR_AMD
, 0x0050, fixup_use_secsi
, NULL
, },
229 { CFI_MFR_AMD
, 0x0053, fixup_use_secsi
, NULL
, },
230 { CFI_MFR_AMD
, 0x0055, fixup_use_secsi
, NULL
, },
231 { CFI_MFR_AMD
, 0x0056, fixup_use_secsi
, NULL
, },
232 { CFI_MFR_AMD
, 0x005C, fixup_use_secsi
, NULL
, },
233 { CFI_MFR_AMD
, 0x005F, fixup_use_secsi
, NULL
, },
234 #if !FORCE_WORD_WRITE
235 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_write_buffers
, NULL
, },
239 static struct cfi_fixup jedec_fixup_table
[] = {
240 { MANUFACTURER_SST
, SST49LF004B
, fixup_use_fwh_lock
, NULL
, },
241 { MANUFACTURER_SST
, SST49LF040B
, fixup_use_fwh_lock
, NULL
, },
242 { MANUFACTURER_SST
, SST49LF008A
, fixup_use_fwh_lock
, NULL
, },
246 static struct cfi_fixup fixup_table
[] = {
247 /* The CFI vendor ids and the JEDEC vendor IDs appear
248 * to be common. It is like the devices id's are as
249 * well. This table is to pick all cases where
250 * we know that is the case.
252 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_erase_chip
, NULL
},
253 { CFI_MFR_ATMEL
, AT49BV6416
, fixup_use_atmel_lock
, NULL
},
258 struct mtd_info
*cfi_cmdset_0002(struct map_info
*map
, int primary
)
260 struct cfi_private
*cfi
= map
->fldrv_priv
;
261 struct mtd_info
*mtd
;
264 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
266 printk(KERN_WARNING
"Failed to allocate memory for MTD device\n");
270 mtd
->type
= MTD_NORFLASH
;
272 /* Fill in the default mtd operations */
273 mtd
->erase
= cfi_amdstd_erase_varsize
;
274 mtd
->write
= cfi_amdstd_write_words
;
275 mtd
->read
= cfi_amdstd_read
;
276 mtd
->sync
= cfi_amdstd_sync
;
277 mtd
->suspend
= cfi_amdstd_suspend
;
278 mtd
->resume
= cfi_amdstd_resume
;
279 mtd
->flags
= MTD_CAP_NORFLASH
;
280 mtd
->name
= map
->name
;
283 if (cfi
->cfi_mode
==CFI_MODE_CFI
){
284 unsigned char bootloc
;
286 * It's a real CFI chip, not one for which the probe
287 * routine faked a CFI structure. So we read the feature
290 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
291 struct cfi_pri_amdstd
*extp
;
293 extp
= (struct cfi_pri_amdstd
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "Amd/Fujitsu");
299 if (extp
->MajorVersion
!= '1' ||
300 (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '4')) {
301 printk(KERN_ERR
" Unknown Amd/Fujitsu Extended Query "
302 "version %c.%c.\n", extp
->MajorVersion
,
309 /* Install our own private info structure */
310 cfi
->cmdset_priv
= extp
;
312 /* Apply cfi device specific fixups */
313 cfi_fixup(mtd
, cfi_fixup_table
);
315 #ifdef DEBUG_CFI_FEATURES
316 /* Tell the user about it in lots of lovely detail */
317 cfi_tell_features(extp
);
320 bootloc
= extp
->TopBottom
;
321 if ((bootloc
!= 2) && (bootloc
!= 3)) {
322 printk(KERN_WARNING
"%s: CFI does not contain boot "
323 "bank location. Assuming top.\n", map
->name
);
327 if (bootloc
== 3 && cfi
->cfiq
->NumEraseRegions
> 1) {
328 printk(KERN_WARNING
"%s: Swapping erase regions for broken CFI table.\n", map
->name
);
330 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
/ 2; i
++) {
331 int j
= (cfi
->cfiq
->NumEraseRegions
-1)-i
;
334 swap
= cfi
->cfiq
->EraseRegionInfo
[i
];
335 cfi
->cfiq
->EraseRegionInfo
[i
] = cfi
->cfiq
->EraseRegionInfo
[j
];
336 cfi
->cfiq
->EraseRegionInfo
[j
] = swap
;
339 /* Set the default CFI lock/unlock addresses */
340 cfi
->addr_unlock1
= 0x555;
341 cfi
->addr_unlock2
= 0x2aa;
342 /* Modify the unlock address if we are in compatibility mode */
343 if ( /* x16 in x8 mode */
344 ((cfi
->device_type
== CFI_DEVICETYPE_X8
) &&
345 (cfi
->cfiq
->InterfaceDesc
==
346 CFI_INTERFACE_X8_BY_X16_ASYNC
)) ||
347 /* x32 in x16 mode */
348 ((cfi
->device_type
== CFI_DEVICETYPE_X16
) &&
349 (cfi
->cfiq
->InterfaceDesc
==
350 CFI_INTERFACE_X16_BY_X32_ASYNC
)))
352 cfi
->addr_unlock1
= 0xaaa;
353 cfi
->addr_unlock2
= 0x555;
357 else if (cfi
->cfi_mode
== CFI_MODE_JEDEC
) {
358 /* Apply jedec specific fixups */
359 cfi_fixup(mtd
, jedec_fixup_table
);
361 /* Apply generic fixups */
362 cfi_fixup(mtd
, fixup_table
);
364 for (i
=0; i
< cfi
->numchips
; i
++) {
365 cfi
->chips
[i
].word_write_time
= 1<<cfi
->cfiq
->WordWriteTimeoutTyp
;
366 cfi
->chips
[i
].buffer_write_time
= 1<<cfi
->cfiq
->BufWriteTimeoutTyp
;
367 cfi
->chips
[i
].erase_time
= 1<<cfi
->cfiq
->BlockEraseTimeoutTyp
;
368 cfi
->chips
[i
].ref_point_counter
= 0;
369 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
372 map
->fldrv
= &cfi_amdstd_chipdrv
;
374 return cfi_amdstd_setup(mtd
);
376 EXPORT_SYMBOL_GPL(cfi_cmdset_0002
);
378 static struct mtd_info
*cfi_amdstd_setup(struct mtd_info
*mtd
)
380 struct map_info
*map
= mtd
->priv
;
381 struct cfi_private
*cfi
= map
->fldrv_priv
;
382 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
383 unsigned long offset
= 0;
386 printk(KERN_NOTICE
"number of %s chips: %d\n",
387 (cfi
->cfi_mode
== CFI_MODE_CFI
)?"CFI":"JEDEC",cfi
->numchips
);
388 /* Select the correct geometry setup */
389 mtd
->size
= devsize
* cfi
->numchips
;
391 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
392 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
393 * mtd
->numeraseregions
, GFP_KERNEL
);
394 if (!mtd
->eraseregions
) {
395 printk(KERN_WARNING
"Failed to allocate memory for MTD erase region info\n");
399 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
400 unsigned long ernum
, ersize
;
401 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
402 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
404 if (mtd
->erasesize
< ersize
) {
405 mtd
->erasesize
= ersize
;
407 for (j
=0; j
<cfi
->numchips
; j
++) {
408 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
409 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
410 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
412 offset
+= (ersize
* ernum
);
414 if (offset
!= devsize
) {
416 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
421 for (i
=0; i
<mtd
->numeraseregions
;i
++){
422 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
423 i
,mtd
->eraseregions
[i
].offset
,
424 mtd
->eraseregions
[i
].erasesize
,
425 mtd
->eraseregions
[i
].numblocks
);
429 /* FIXME: erase-suspend-program is broken. See
430 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
431 printk(KERN_NOTICE
"cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
433 __module_get(THIS_MODULE
);
438 kfree(mtd
->eraseregions
);
441 kfree(cfi
->cmdset_priv
);
447 * Return true if the chip is ready.
449 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
450 * non-suspended sector) and is indicated by no toggle bits toggling.
452 * Note that anything more complicated than checking if no bits are toggling
453 * (including checking DQ5 for an error status) is tricky to get working
454 * correctly and is therefore not done (particulary with interleaved chips
455 * as each chip must be checked independantly of the others).
457 static int __xipram
chip_ready(struct map_info
*map
, unsigned long addr
)
461 d
= map_read(map
, addr
);
462 t
= map_read(map
, addr
);
464 return map_word_equal(map
, d
, t
);
468 * Return true if the chip is ready and has the correct value.
470 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
471 * non-suspended sector) and it is indicated by no bits toggling.
473 * Error are indicated by toggling bits or bits held with the wrong value,
474 * or with bits toggling.
476 * Note that anything more complicated than checking if no bits are toggling
477 * (including checking DQ5 for an error status) is tricky to get working
478 * correctly and is therefore not done (particulary with interleaved chips
479 * as each chip must be checked independantly of the others).
482 static int __xipram
chip_good(struct map_info
*map
, unsigned long addr
, map_word expected
)
486 oldd
= map_read(map
, addr
);
487 curd
= map_read(map
, addr
);
489 return map_word_equal(map
, oldd
, curd
) &&
490 map_word_equal(map
, curd
, expected
);
493 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
)
495 DECLARE_WAITQUEUE(wait
, current
);
496 struct cfi_private
*cfi
= map
->fldrv_priv
;
498 struct cfi_pri_amdstd
*cfip
= (struct cfi_pri_amdstd
*)cfi
->cmdset_priv
;
501 timeo
= jiffies
+ HZ
;
503 switch (chip
->state
) {
507 if (chip_ready(map
, adr
))
510 if (time_after(jiffies
, timeo
)) {
511 printk(KERN_ERR
"Waiting for chip to be ready timed out.\n");
512 spin_unlock(chip
->mutex
);
515 spin_unlock(chip
->mutex
);
517 spin_lock(chip
->mutex
);
518 /* Someone else might have been playing with it. */
528 if (mode
== FL_WRITING
) /* FIXME: Erase-suspend-program appears broken. */
531 if (!( mode
== FL_READY
534 || (mode
== FL_WRITING
&& (cfip
->EraseSuspend
& 0x2))
535 || (mode
== FL_WRITING
&& (cfip
->EraseSuspend
& 0x1)
539 /* We could check to see if we're trying to access the sector
540 * that is currently being erased. However, no user will try
541 * anything like that so we just wait for the timeout. */
544 /* It's harmless to issue the Erase-Suspend and Erase-Resume
545 * commands when the erase algorithm isn't in progress. */
546 map_write(map
, CMD(0xB0), chip
->in_progress_block_addr
);
547 chip
->oldstate
= FL_ERASING
;
548 chip
->state
= FL_ERASE_SUSPENDING
;
549 chip
->erase_suspended
= 1;
551 if (chip_ready(map
, adr
))
554 if (time_after(jiffies
, timeo
)) {
555 /* Should have suspended the erase by now.
556 * Send an Erase-Resume command as either
557 * there was an error (so leave the erase
558 * routine to recover from it) or we trying to
559 * use the erase-in-progress sector. */
560 map_write(map
, CMD(0x30), chip
->in_progress_block_addr
);
561 chip
->state
= FL_ERASING
;
562 chip
->oldstate
= FL_READY
;
563 printk(KERN_ERR
"MTD %s(): chip not ready after erase suspend\n", __func__
);
567 spin_unlock(chip
->mutex
);
569 spin_lock(chip
->mutex
);
570 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
571 So we can just loop here. */
573 chip
->state
= FL_READY
;
576 case FL_XIP_WHILE_ERASING
:
577 if (mode
!= FL_READY
&& mode
!= FL_POINT
&&
578 (!cfip
|| !(cfip
->EraseSuspend
&2)))
580 chip
->oldstate
= chip
->state
;
581 chip
->state
= FL_READY
;
585 /* Only if there's no operation suspended... */
586 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
591 set_current_state(TASK_UNINTERRUPTIBLE
);
592 add_wait_queue(&chip
->wq
, &wait
);
593 spin_unlock(chip
->mutex
);
595 remove_wait_queue(&chip
->wq
, &wait
);
596 spin_lock(chip
->mutex
);
602 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
604 struct cfi_private
*cfi
= map
->fldrv_priv
;
606 switch(chip
->oldstate
) {
608 chip
->state
= chip
->oldstate
;
609 map_write(map
, CMD(0x30), chip
->in_progress_block_addr
);
610 chip
->oldstate
= FL_READY
;
611 chip
->state
= FL_ERASING
;
614 case FL_XIP_WHILE_ERASING
:
615 chip
->state
= chip
->oldstate
;
616 chip
->oldstate
= FL_READY
;
621 /* We should really make set_vpp() count, rather than doing this */
625 printk(KERN_ERR
"MTD: put_chip() called with oldstate %d!!\n", chip
->oldstate
);
630 #ifdef CONFIG_MTD_XIP
633 * No interrupt what so ever can be serviced while the flash isn't in array
634 * mode. This is ensured by the xip_disable() and xip_enable() functions
635 * enclosing any code path where the flash is known not to be in array mode.
636 * And within a XIP disabled code path, only functions marked with __xipram
637 * may be called and nothing else (it's a good thing to inspect generated
638 * assembly to make sure inline functions were actually inlined and that gcc
639 * didn't emit calls to its own support functions). Also configuring MTD CFI
640 * support to a single buswidth and a single interleave is also recommended.
643 static void xip_disable(struct map_info
*map
, struct flchip
*chip
,
646 /* TODO: chips with no XIP use should ignore and return */
647 (void) map_read(map
, adr
); /* ensure mmu mapping is up to date */
651 static void __xipram
xip_enable(struct map_info
*map
, struct flchip
*chip
,
654 struct cfi_private
*cfi
= map
->fldrv_priv
;
656 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
657 map_write(map
, CMD(0xf0), adr
);
658 chip
->state
= FL_READY
;
660 (void) map_read(map
, adr
);
666 * When a delay is required for the flash operation to complete, the
667 * xip_udelay() function is polling for both the given timeout and pending
668 * (but still masked) hardware interrupts. Whenever there is an interrupt
669 * pending then the flash erase operation is suspended, array mode restored
670 * and interrupts unmasked. Task scheduling might also happen at that
671 * point. The CPU eventually returns from the interrupt or the call to
672 * schedule() and the suspended flash operation is resumed for the remaining
673 * of the delay period.
675 * Warning: this function _will_ fool interrupt latency tracing tools.
678 static void __xipram
xip_udelay(struct map_info
*map
, struct flchip
*chip
,
679 unsigned long adr
, int usec
)
681 struct cfi_private
*cfi
= map
->fldrv_priv
;
682 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
683 map_word status
, OK
= CMD(0x80);
684 unsigned long suspended
, start
= xip_currtime();
689 if (xip_irqpending() && extp
&&
690 ((chip
->state
== FL_ERASING
&& (extp
->EraseSuspend
& 2))) &&
691 (cfi_interleave_is_1(cfi
) || chip
->oldstate
== FL_READY
)) {
693 * Let's suspend the erase operation when supported.
694 * Note that we currently don't try to suspend
695 * interleaved chips if there is already another
696 * operation suspended (imagine what happens
697 * when one chip was already done with the current
698 * operation while another chip suspended it, then
699 * we resume the whole thing at once). Yes, it
702 map_write(map
, CMD(0xb0), adr
);
703 usec
-= xip_elapsed_since(start
);
704 suspended
= xip_currtime();
706 if (xip_elapsed_since(suspended
) > 100000) {
708 * The chip doesn't want to suspend
709 * after waiting for 100 msecs.
710 * This is a critical error but there
711 * is not much we can do here.
715 status
= map_read(map
, adr
);
716 } while (!map_word_andequal(map
, status
, OK
, OK
));
718 /* Suspend succeeded */
719 oldstate
= chip
->state
;
720 if (!map_word_bitsset(map
, status
, CMD(0x40)))
722 chip
->state
= FL_XIP_WHILE_ERASING
;
723 chip
->erase_suspended
= 1;
724 map_write(map
, CMD(0xf0), adr
);
725 (void) map_read(map
, adr
);
726 asm volatile (".rep 8; nop; .endr");
728 spin_unlock(chip
->mutex
);
729 asm volatile (".rep 8; nop; .endr");
733 * We're back. However someone else might have
734 * decided to go write to the chip if we are in
735 * a suspended erase state. If so let's wait
738 spin_lock(chip
->mutex
);
739 while (chip
->state
!= FL_XIP_WHILE_ERASING
) {
740 DECLARE_WAITQUEUE(wait
, current
);
741 set_current_state(TASK_UNINTERRUPTIBLE
);
742 add_wait_queue(&chip
->wq
, &wait
);
743 spin_unlock(chip
->mutex
);
745 remove_wait_queue(&chip
->wq
, &wait
);
746 spin_lock(chip
->mutex
);
748 /* Disallow XIP again */
751 /* Resume the write or erase operation */
752 map_write(map
, CMD(0x30), adr
);
753 chip
->state
= oldstate
;
754 start
= xip_currtime();
755 } else if (usec
>= 1000000/HZ
) {
757 * Try to save on CPU power when waiting delay
758 * is at least a system timer tick period.
759 * No need to be extremely accurate here.
763 status
= map_read(map
, adr
);
764 } while (!map_word_andequal(map
, status
, OK
, OK
)
765 && xip_elapsed_since(start
) < usec
);
768 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
771 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
772 * the flash is actively programming or erasing since we have to poll for
773 * the operation to complete anyway. We can't do that in a generic way with
774 * a XIP setup so do it before the actual flash operation in this case
775 * and stub it out from INVALIDATE_CACHE_UDELAY.
777 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
778 INVALIDATE_CACHED_RANGE(map, from, size)
780 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
781 UDELAY(map, chip, adr, usec)
786 * Activating this XIP support changes the way the code works a bit. For
787 * example the code to suspend the current process when concurrent access
788 * happens is never executed because xip_udelay() will always return with the
789 * same chip state as it was entered with. This is why there is no care for
790 * the presence of add_wait_queue() or schedule() calls from within a couple
791 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
792 * The queueing and scheduling are always happening within xip_udelay().
794 * Similarly, get_chip() and put_chip() just happen to always be executed
795 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
796 * is in array mode, therefore never executing many cases therein and not
797 * causing any problem with XIP.
802 #define xip_disable(map, chip, adr)
803 #define xip_enable(map, chip, adr)
804 #define XIP_INVAL_CACHED_RANGE(x...)
806 #define UDELAY(map, chip, adr, usec) \
808 spin_unlock(chip->mutex); \
810 spin_lock(chip->mutex); \
813 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
815 spin_unlock(chip->mutex); \
816 INVALIDATE_CACHED_RANGE(map, adr, len); \
818 spin_lock(chip->mutex); \
823 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
825 unsigned long cmd_addr
;
826 struct cfi_private
*cfi
= map
->fldrv_priv
;
831 /* Ensure cmd read/writes are aligned. */
832 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
834 spin_lock(chip
->mutex
);
835 ret
= get_chip(map
, chip
, cmd_addr
, FL_READY
);
837 spin_unlock(chip
->mutex
);
841 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
842 map_write(map
, CMD(0xf0), cmd_addr
);
843 chip
->state
= FL_READY
;
846 map_copy_from(map
, buf
, adr
, len
);
848 put_chip(map
, chip
, cmd_addr
);
850 spin_unlock(chip
->mutex
);
855 static int cfi_amdstd_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
857 struct map_info
*map
= mtd
->priv
;
858 struct cfi_private
*cfi
= map
->fldrv_priv
;
863 /* ofs: offset within the first chip that the first read should start */
865 chipnum
= (from
>> cfi
->chipshift
);
866 ofs
= from
- (chipnum
<< cfi
->chipshift
);
872 unsigned long thislen
;
874 if (chipnum
>= cfi
->numchips
)
877 if ((len
+ ofs
-1) >> cfi
->chipshift
)
878 thislen
= (1<<cfi
->chipshift
) - ofs
;
882 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
897 static inline int do_read_secsi_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
899 DECLARE_WAITQUEUE(wait
, current
);
900 unsigned long timeo
= jiffies
+ HZ
;
901 struct cfi_private
*cfi
= map
->fldrv_priv
;
904 spin_lock(chip
->mutex
);
906 if (chip
->state
!= FL_READY
){
908 printk(KERN_DEBUG
"Waiting for chip to read, status = %d\n", chip
->state
);
910 set_current_state(TASK_UNINTERRUPTIBLE
);
911 add_wait_queue(&chip
->wq
, &wait
);
913 spin_unlock(chip
->mutex
);
916 remove_wait_queue(&chip
->wq
, &wait
);
918 if(signal_pending(current
))
921 timeo
= jiffies
+ HZ
;
928 chip
->state
= FL_READY
;
930 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
931 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
932 cfi_send_gen_cmd(0x88, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
934 map_copy_from(map
, buf
, adr
, len
);
936 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
937 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
938 cfi_send_gen_cmd(0x90, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
939 cfi_send_gen_cmd(0x00, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
942 spin_unlock(chip
->mutex
);
947 static int cfi_amdstd_secsi_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
949 struct map_info
*map
= mtd
->priv
;
950 struct cfi_private
*cfi
= map
->fldrv_priv
;
956 /* ofs: offset within the first chip that the first read should start */
958 /* 8 secsi bytes per chip */
966 unsigned long thislen
;
968 if (chipnum
>= cfi
->numchips
)
971 if ((len
+ ofs
-1) >> 3)
972 thislen
= (1<<3) - ofs
;
976 ret
= do_read_secsi_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
991 static int __xipram
do_write_oneword(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, map_word datum
)
993 struct cfi_private
*cfi
= map
->fldrv_priv
;
994 unsigned long timeo
= jiffies
+ HZ
;
996 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
997 * have a max write time of a few hundreds usec). However, we should
998 * use the maximum timeout value given by the chip at probe time
999 * instead. Unfortunately, struct flchip does have a field for
1000 * maximum timeout, only for typical which can be far too short
1001 * depending of the conditions. The ' + 1' is to avoid having a
1002 * timeout of 0 jiffies if HZ is smaller than 1000.
1004 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1011 spin_lock(chip
->mutex
);
1012 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1014 spin_unlock(chip
->mutex
);
1018 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1019 __func__
, adr
, datum
.x
[0] );
1022 * Check for a NOP for the case when the datum to write is already
1023 * present - it saves time and works around buggy chips that corrupt
1024 * data at other locations when 0xff is written to a location that
1025 * already contains 0xff.
1027 oldd
= map_read(map
, adr
);
1028 if (map_word_equal(map
, oldd
, datum
)) {
1029 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): NOP\n",
1034 XIP_INVAL_CACHED_RANGE(map
, adr
, map_bankwidth(map
));
1036 xip_disable(map
, chip
, adr
);
1038 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1039 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1040 cfi_send_gen_cmd(0xA0, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1041 map_write(map
, datum
, adr
);
1042 chip
->state
= FL_WRITING
;
1044 INVALIDATE_CACHE_UDELAY(map
, chip
,
1045 adr
, map_bankwidth(map
),
1046 chip
->word_write_time
);
1048 /* See comment above for timeout value. */
1049 timeo
= jiffies
+ uWriteTimeout
;
1051 if (chip
->state
!= FL_WRITING
) {
1052 /* Someone's suspended the write. Sleep */
1053 DECLARE_WAITQUEUE(wait
, current
);
1055 set_current_state(TASK_UNINTERRUPTIBLE
);
1056 add_wait_queue(&chip
->wq
, &wait
);
1057 spin_unlock(chip
->mutex
);
1059 remove_wait_queue(&chip
->wq
, &wait
);
1060 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1061 spin_lock(chip
->mutex
);
1065 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
)){
1066 xip_enable(map
, chip
, adr
);
1067 printk(KERN_WARNING
"MTD %s(): software timeout\n", __func__
);
1068 xip_disable(map
, chip
, adr
);
1072 if (chip_ready(map
, adr
))
1075 /* Latency issues. Drop the lock, wait a while and retry */
1076 UDELAY(map
, chip
, adr
, 1);
1078 /* Did we succeed? */
1079 if (!chip_good(map
, adr
, datum
)) {
1080 /* reset on all failures. */
1081 map_write( map
, CMD(0xF0), chip
->start
);
1082 /* FIXME - should have reset delay before continuing */
1084 if (++retry_cnt
<= MAX_WORD_RETRIES
)
1089 xip_enable(map
, chip
, adr
);
1091 chip
->state
= FL_READY
;
1092 put_chip(map
, chip
, adr
);
1093 spin_unlock(chip
->mutex
);
1099 static int cfi_amdstd_write_words(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1100 size_t *retlen
, const u_char
*buf
)
1102 struct map_info
*map
= mtd
->priv
;
1103 struct cfi_private
*cfi
= map
->fldrv_priv
;
1106 unsigned long ofs
, chipstart
;
1107 DECLARE_WAITQUEUE(wait
, current
);
1113 chipnum
= to
>> cfi
->chipshift
;
1114 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1115 chipstart
= cfi
->chips
[chipnum
].start
;
1117 /* If it's not bus-aligned, do the first byte write */
1118 if (ofs
& (map_bankwidth(map
)-1)) {
1119 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
)-1);
1120 int i
= ofs
- bus_ofs
;
1125 spin_lock(cfi
->chips
[chipnum
].mutex
);
1127 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1129 printk(KERN_DEBUG
"Waiting for chip to write, status = %d\n", cfi
->chips
[chipnum
].state
);
1131 set_current_state(TASK_UNINTERRUPTIBLE
);
1132 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1134 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1137 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1139 if(signal_pending(current
))
1145 /* Load 'tmp_buf' with old contents of flash */
1146 tmp_buf
= map_read(map
, bus_ofs
+chipstart
);
1148 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1150 /* Number of bytes to copy from buffer */
1151 n
= min_t(int, len
, map_bankwidth(map
)-i
);
1153 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, i
, n
);
1155 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1165 if (ofs
>> cfi
->chipshift
) {
1168 if (chipnum
== cfi
->numchips
)
1173 /* We are now aligned, write as much as possible */
1174 while(len
>= map_bankwidth(map
)) {
1177 datum
= map_word_load(map
, buf
);
1179 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1184 ofs
+= map_bankwidth(map
);
1185 buf
+= map_bankwidth(map
);
1186 (*retlen
) += map_bankwidth(map
);
1187 len
-= map_bankwidth(map
);
1189 if (ofs
>> cfi
->chipshift
) {
1192 if (chipnum
== cfi
->numchips
)
1194 chipstart
= cfi
->chips
[chipnum
].start
;
1198 /* Write the trailing bytes if any */
1199 if (len
& (map_bankwidth(map
)-1)) {
1203 spin_lock(cfi
->chips
[chipnum
].mutex
);
1205 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1207 printk(KERN_DEBUG
"Waiting for chip to write, status = %d\n", cfi
->chips
[chipnum
].state
);
1209 set_current_state(TASK_UNINTERRUPTIBLE
);
1210 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1212 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1215 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1217 if(signal_pending(current
))
1223 tmp_buf
= map_read(map
, ofs
+ chipstart
);
1225 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1227 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, 0, len
);
1229 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1242 * FIXME: interleaved mode not tested, and probably not supported!
1244 static int __xipram
do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
1245 unsigned long adr
, const u_char
*buf
,
1248 struct cfi_private
*cfi
= map
->fldrv_priv
;
1249 unsigned long timeo
= jiffies
+ HZ
;
1250 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1251 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1253 unsigned long cmd_adr
;
1260 spin_lock(chip
->mutex
);
1261 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1263 spin_unlock(chip
->mutex
);
1267 datum
= map_word_load(map
, buf
);
1269 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1270 __func__
, adr
, datum
.x
[0] );
1272 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1274 xip_disable(map
, chip
, cmd_adr
);
1276 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1277 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1278 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1280 /* Write Buffer Load */
1281 map_write(map
, CMD(0x25), cmd_adr
);
1283 chip
->state
= FL_WRITING_TO_BUFFER
;
1285 /* Write length of data to come */
1286 words
= len
/ map_bankwidth(map
);
1287 map_write(map
, CMD(words
- 1), cmd_adr
);
1290 while(z
< words
* map_bankwidth(map
)) {
1291 datum
= map_word_load(map
, buf
);
1292 map_write(map
, datum
, adr
+ z
);
1294 z
+= map_bankwidth(map
);
1295 buf
+= map_bankwidth(map
);
1297 z
-= map_bankwidth(map
);
1301 /* Write Buffer Program Confirm: GO GO GO */
1302 map_write(map
, CMD(0x29), cmd_adr
);
1303 chip
->state
= FL_WRITING
;
1305 INVALIDATE_CACHE_UDELAY(map
, chip
,
1306 adr
, map_bankwidth(map
),
1307 chip
->word_write_time
);
1309 timeo
= jiffies
+ uWriteTimeout
;
1312 if (chip
->state
!= FL_WRITING
) {
1313 /* Someone's suspended the write. Sleep */
1314 DECLARE_WAITQUEUE(wait
, current
);
1316 set_current_state(TASK_UNINTERRUPTIBLE
);
1317 add_wait_queue(&chip
->wq
, &wait
);
1318 spin_unlock(chip
->mutex
);
1320 remove_wait_queue(&chip
->wq
, &wait
);
1321 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1322 spin_lock(chip
->mutex
);
1326 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
))
1329 if (chip_ready(map
, adr
)) {
1330 xip_enable(map
, chip
, adr
);
1334 /* Latency issues. Drop the lock, wait a while and retry */
1335 UDELAY(map
, chip
, adr
, 1);
1338 /* reset on all failures. */
1339 map_write( map
, CMD(0xF0), chip
->start
);
1340 xip_enable(map
, chip
, adr
);
1341 /* FIXME - should have reset delay before continuing */
1343 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1348 chip
->state
= FL_READY
;
1349 put_chip(map
, chip
, adr
);
1350 spin_unlock(chip
->mutex
);
1356 static int cfi_amdstd_write_buffers(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1357 size_t *retlen
, const u_char
*buf
)
1359 struct map_info
*map
= mtd
->priv
;
1360 struct cfi_private
*cfi
= map
->fldrv_priv
;
1361 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1370 chipnum
= to
>> cfi
->chipshift
;
1371 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1373 /* If it's not bus-aligned, do the first word write */
1374 if (ofs
& (map_bankwidth(map
)-1)) {
1375 size_t local_len
= (-ofs
)&(map_bankwidth(map
)-1);
1376 if (local_len
> len
)
1378 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1379 local_len
, retlen
, buf
);
1386 if (ofs
>> cfi
->chipshift
) {
1389 if (chipnum
== cfi
->numchips
)
1394 /* Write buffer is worth it only if more than one word to write... */
1395 while (len
>= map_bankwidth(map
) * 2) {
1396 /* We must not cross write block boundaries */
1397 int size
= wbufsize
- (ofs
& (wbufsize
-1));
1401 if (size
% map_bankwidth(map
))
1402 size
-= size
% map_bankwidth(map
);
1404 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
1414 if (ofs
>> cfi
->chipshift
) {
1417 if (chipnum
== cfi
->numchips
)
1423 size_t retlen_dregs
= 0;
1425 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1426 len
, &retlen_dregs
, buf
);
1428 *retlen
+= retlen_dregs
;
1437 * Handle devices with one erase region, that only implement
1438 * the chip erase command.
1440 static int __xipram
do_erase_chip(struct map_info
*map
, struct flchip
*chip
)
1442 struct cfi_private
*cfi
= map
->fldrv_priv
;
1443 unsigned long timeo
= jiffies
+ HZ
;
1444 unsigned long int adr
;
1445 DECLARE_WAITQUEUE(wait
, current
);
1448 adr
= cfi
->addr_unlock1
;
1450 spin_lock(chip
->mutex
);
1451 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1453 spin_unlock(chip
->mutex
);
1457 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): ERASE 0x%.8lx\n",
1458 __func__
, chip
->start
);
1460 XIP_INVAL_CACHED_RANGE(map
, adr
, map
->size
);
1462 xip_disable(map
, chip
, adr
);
1464 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1465 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1466 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1467 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1468 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1469 cfi_send_gen_cmd(0x10, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1471 chip
->state
= FL_ERASING
;
1472 chip
->erase_suspended
= 0;
1473 chip
->in_progress_block_addr
= adr
;
1475 INVALIDATE_CACHE_UDELAY(map
, chip
,
1477 chip
->erase_time
*500);
1479 timeo
= jiffies
+ (HZ
*20);
1482 if (chip
->state
!= FL_ERASING
) {
1483 /* Someone's suspended the erase. Sleep */
1484 set_current_state(TASK_UNINTERRUPTIBLE
);
1485 add_wait_queue(&chip
->wq
, &wait
);
1486 spin_unlock(chip
->mutex
);
1488 remove_wait_queue(&chip
->wq
, &wait
);
1489 spin_lock(chip
->mutex
);
1492 if (chip
->erase_suspended
) {
1493 /* This erase was suspended and resumed.
1494 Adjust the timeout */
1495 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1496 chip
->erase_suspended
= 0;
1499 if (chip_ready(map
, adr
))
1502 if (time_after(jiffies
, timeo
)) {
1503 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1508 /* Latency issues. Drop the lock, wait a while and retry */
1509 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1511 /* Did we succeed? */
1512 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1513 /* reset on all failures. */
1514 map_write( map
, CMD(0xF0), chip
->start
);
1515 /* FIXME - should have reset delay before continuing */
1520 chip
->state
= FL_READY
;
1521 xip_enable(map
, chip
, adr
);
1522 put_chip(map
, chip
, adr
);
1523 spin_unlock(chip
->mutex
);
1529 static int __xipram
do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int len
, void *thunk
)
1531 struct cfi_private
*cfi
= map
->fldrv_priv
;
1532 unsigned long timeo
= jiffies
+ HZ
;
1533 DECLARE_WAITQUEUE(wait
, current
);
1538 spin_lock(chip
->mutex
);
1539 ret
= get_chip(map
, chip
, adr
, FL_ERASING
);
1541 spin_unlock(chip
->mutex
);
1545 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): ERASE 0x%.8lx\n",
1548 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1550 xip_disable(map
, chip
, adr
);
1552 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1553 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1554 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1555 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1556 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1557 map_write(map
, CMD(0x30), adr
);
1559 chip
->state
= FL_ERASING
;
1560 chip
->erase_suspended
= 0;
1561 chip
->in_progress_block_addr
= adr
;
1563 INVALIDATE_CACHE_UDELAY(map
, chip
,
1565 chip
->erase_time
*500);
1567 timeo
= jiffies
+ (HZ
*20);
1570 if (chip
->state
!= FL_ERASING
) {
1571 /* Someone's suspended the erase. Sleep */
1572 set_current_state(TASK_UNINTERRUPTIBLE
);
1573 add_wait_queue(&chip
->wq
, &wait
);
1574 spin_unlock(chip
->mutex
);
1576 remove_wait_queue(&chip
->wq
, &wait
);
1577 spin_lock(chip
->mutex
);
1580 if (chip
->erase_suspended
) {
1581 /* This erase was suspended and resumed.
1582 Adjust the timeout */
1583 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1584 chip
->erase_suspended
= 0;
1587 if (chip_ready(map
, adr
)) {
1588 xip_enable(map
, chip
, adr
);
1592 if (time_after(jiffies
, timeo
)) {
1593 xip_enable(map
, chip
, adr
);
1594 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1599 /* Latency issues. Drop the lock, wait a while and retry */
1600 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1602 /* Did we succeed? */
1603 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1604 /* reset on all failures. */
1605 map_write( map
, CMD(0xF0), chip
->start
);
1606 /* FIXME - should have reset delay before continuing */
1611 chip
->state
= FL_READY
;
1612 put_chip(map
, chip
, adr
);
1613 spin_unlock(chip
->mutex
);
1618 static int cfi_amdstd_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
1620 unsigned long ofs
, len
;
1626 ret
= cfi_varsize_frob(mtd
, do_erase_oneblock
, ofs
, len
, NULL
);
1630 instr
->state
= MTD_ERASE_DONE
;
1631 mtd_erase_callback(instr
);
1637 static int cfi_amdstd_erase_chip(struct mtd_info
*mtd
, struct erase_info
*instr
)
1639 struct map_info
*map
= mtd
->priv
;
1640 struct cfi_private
*cfi
= map
->fldrv_priv
;
1643 if (instr
->addr
!= 0)
1646 if (instr
->len
!= mtd
->size
)
1649 ret
= do_erase_chip(map
, &cfi
->chips
[0]);
1653 instr
->state
= MTD_ERASE_DONE
;
1654 mtd_erase_callback(instr
);
1659 static int do_atmel_lock(struct map_info
*map
, struct flchip
*chip
,
1660 unsigned long adr
, int len
, void *thunk
)
1662 struct cfi_private
*cfi
= map
->fldrv_priv
;
1665 spin_lock(chip
->mutex
);
1666 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_LOCKING
);
1669 chip
->state
= FL_LOCKING
;
1671 DEBUG(MTD_DEBUG_LEVEL3
, "MTD %s(): LOCK 0x%08lx len %d\n",
1672 __func__
, adr
, len
);
1674 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1675 cfi
->device_type
, NULL
);
1676 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
1677 cfi
->device_type
, NULL
);
1678 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1679 cfi
->device_type
, NULL
);
1680 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1681 cfi
->device_type
, NULL
);
1682 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
1683 cfi
->device_type
, NULL
);
1684 map_write(map
, CMD(0x40), chip
->start
+ adr
);
1686 chip
->state
= FL_READY
;
1687 put_chip(map
, chip
, adr
+ chip
->start
);
1691 spin_unlock(chip
->mutex
);
1695 static int do_atmel_unlock(struct map_info
*map
, struct flchip
*chip
,
1696 unsigned long adr
, int len
, void *thunk
)
1698 struct cfi_private
*cfi
= map
->fldrv_priv
;
1701 spin_lock(chip
->mutex
);
1702 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_UNLOCKING
);
1705 chip
->state
= FL_UNLOCKING
;
1707 DEBUG(MTD_DEBUG_LEVEL3
, "MTD %s(): LOCK 0x%08lx len %d\n",
1708 __func__
, adr
, len
);
1710 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1711 cfi
->device_type
, NULL
);
1712 map_write(map
, CMD(0x70), adr
);
1714 chip
->state
= FL_READY
;
1715 put_chip(map
, chip
, adr
+ chip
->start
);
1719 spin_unlock(chip
->mutex
);
1723 static int cfi_atmel_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1725 return cfi_varsize_frob(mtd
, do_atmel_lock
, ofs
, len
, NULL
);
1728 static int cfi_atmel_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1730 return cfi_varsize_frob(mtd
, do_atmel_unlock
, ofs
, len
, NULL
);
1734 static void cfi_amdstd_sync (struct mtd_info
*mtd
)
1736 struct map_info
*map
= mtd
->priv
;
1737 struct cfi_private
*cfi
= map
->fldrv_priv
;
1739 struct flchip
*chip
;
1741 DECLARE_WAITQUEUE(wait
, current
);
1743 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1744 chip
= &cfi
->chips
[i
];
1747 spin_lock(chip
->mutex
);
1749 switch(chip
->state
) {
1753 case FL_JEDEC_QUERY
:
1754 chip
->oldstate
= chip
->state
;
1755 chip
->state
= FL_SYNCING
;
1756 /* No need to wake_up() on this state change -
1757 * as the whole point is that nobody can do anything
1758 * with the chip now anyway.
1761 spin_unlock(chip
->mutex
);
1765 /* Not an idle state */
1766 add_wait_queue(&chip
->wq
, &wait
);
1768 spin_unlock(chip
->mutex
);
1772 remove_wait_queue(&chip
->wq
, &wait
);
1778 /* Unlock the chips again */
1780 for (i
--; i
>=0; i
--) {
1781 chip
= &cfi
->chips
[i
];
1783 spin_lock(chip
->mutex
);
1785 if (chip
->state
== FL_SYNCING
) {
1786 chip
->state
= chip
->oldstate
;
1789 spin_unlock(chip
->mutex
);
1794 static int cfi_amdstd_suspend(struct mtd_info
*mtd
)
1796 struct map_info
*map
= mtd
->priv
;
1797 struct cfi_private
*cfi
= map
->fldrv_priv
;
1799 struct flchip
*chip
;
1802 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1803 chip
= &cfi
->chips
[i
];
1805 spin_lock(chip
->mutex
);
1807 switch(chip
->state
) {
1811 case FL_JEDEC_QUERY
:
1812 chip
->oldstate
= chip
->state
;
1813 chip
->state
= FL_PM_SUSPENDED
;
1814 /* No need to wake_up() on this state change -
1815 * as the whole point is that nobody can do anything
1816 * with the chip now anyway.
1818 case FL_PM_SUSPENDED
:
1825 spin_unlock(chip
->mutex
);
1828 /* Unlock the chips again */
1831 for (i
--; i
>=0; i
--) {
1832 chip
= &cfi
->chips
[i
];
1834 spin_lock(chip
->mutex
);
1836 if (chip
->state
== FL_PM_SUSPENDED
) {
1837 chip
->state
= chip
->oldstate
;
1840 spin_unlock(chip
->mutex
);
1848 static void cfi_amdstd_resume(struct mtd_info
*mtd
)
1850 struct map_info
*map
= mtd
->priv
;
1851 struct cfi_private
*cfi
= map
->fldrv_priv
;
1853 struct flchip
*chip
;
1855 for (i
=0; i
<cfi
->numchips
; i
++) {
1857 chip
= &cfi
->chips
[i
];
1859 spin_lock(chip
->mutex
);
1861 if (chip
->state
== FL_PM_SUSPENDED
) {
1862 chip
->state
= FL_READY
;
1863 map_write(map
, CMD(0xF0), chip
->start
);
1867 printk(KERN_ERR
"Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1869 spin_unlock(chip
->mutex
);
1873 static void cfi_amdstd_destroy(struct mtd_info
*mtd
)
1875 struct map_info
*map
= mtd
->priv
;
1876 struct cfi_private
*cfi
= map
->fldrv_priv
;
1878 kfree(cfi
->cmdset_priv
);
1881 kfree(mtd
->eraseregions
);
1884 MODULE_LICENSE("GPL");
1885 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1886 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");