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 void fixup_s29gl064n_sectors(struct mtd_info
*mtd
, void *param
)
225 struct map_info
*map
= mtd
->priv
;
226 struct cfi_private
*cfi
= map
->fldrv_priv
;
228 if ((cfi
->cfiq
->EraseRegionInfo
[0] & 0xffff) == 0x003f) {
229 cfi
->cfiq
->EraseRegionInfo
[0] |= 0x0040;
230 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd
->name
);
234 static void fixup_s29gl032n_sectors(struct mtd_info
*mtd
, void *param
)
236 struct map_info
*map
= mtd
->priv
;
237 struct cfi_private
*cfi
= map
->fldrv_priv
;
239 if ((cfi
->cfiq
->EraseRegionInfo
[1] & 0xffff) == 0x007e) {
240 cfi
->cfiq
->EraseRegionInfo
[1] &= ~0x0040;
241 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd
->name
);
245 static struct cfi_fixup cfi_fixup_table
[] = {
246 { CFI_MFR_ATMEL
, CFI_ID_ANY
, fixup_convert_atmel_pri
, NULL
},
247 #ifdef AMD_BOOTLOC_BUG
248 { CFI_MFR_AMD
, CFI_ID_ANY
, fixup_amd_bootblock
, NULL
},
250 { CFI_MFR_AMD
, 0x0050, fixup_use_secsi
, NULL
, },
251 { CFI_MFR_AMD
, 0x0053, fixup_use_secsi
, NULL
, },
252 { CFI_MFR_AMD
, 0x0055, fixup_use_secsi
, NULL
, },
253 { CFI_MFR_AMD
, 0x0056, fixup_use_secsi
, NULL
, },
254 { CFI_MFR_AMD
, 0x005C, fixup_use_secsi
, NULL
, },
255 { CFI_MFR_AMD
, 0x005F, fixup_use_secsi
, NULL
, },
256 { CFI_MFR_AMD
, 0x0c01, fixup_s29gl064n_sectors
, NULL
, },
257 { CFI_MFR_AMD
, 0x1301, fixup_s29gl064n_sectors
, NULL
, },
258 { CFI_MFR_AMD
, 0x1a00, fixup_s29gl032n_sectors
, NULL
, },
259 { CFI_MFR_AMD
, 0x1a01, fixup_s29gl032n_sectors
, NULL
, },
260 #if !FORCE_WORD_WRITE
261 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_write_buffers
, NULL
, },
265 static struct cfi_fixup jedec_fixup_table
[] = {
266 { MANUFACTURER_SST
, SST49LF004B
, fixup_use_fwh_lock
, NULL
, },
267 { MANUFACTURER_SST
, SST49LF040B
, fixup_use_fwh_lock
, NULL
, },
268 { MANUFACTURER_SST
, SST49LF008A
, fixup_use_fwh_lock
, NULL
, },
272 static struct cfi_fixup fixup_table
[] = {
273 /* The CFI vendor ids and the JEDEC vendor IDs appear
274 * to be common. It is like the devices id's are as
275 * well. This table is to pick all cases where
276 * we know that is the case.
278 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_erase_chip
, NULL
},
279 { CFI_MFR_ATMEL
, AT49BV6416
, fixup_use_atmel_lock
, NULL
},
284 struct mtd_info
*cfi_cmdset_0002(struct map_info
*map
, int primary
)
286 struct cfi_private
*cfi
= map
->fldrv_priv
;
287 struct mtd_info
*mtd
;
290 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
292 printk(KERN_WARNING
"Failed to allocate memory for MTD device\n");
296 mtd
->type
= MTD_NORFLASH
;
298 /* Fill in the default mtd operations */
299 mtd
->erase
= cfi_amdstd_erase_varsize
;
300 mtd
->write
= cfi_amdstd_write_words
;
301 mtd
->read
= cfi_amdstd_read
;
302 mtd
->sync
= cfi_amdstd_sync
;
303 mtd
->suspend
= cfi_amdstd_suspend
;
304 mtd
->resume
= cfi_amdstd_resume
;
305 mtd
->flags
= MTD_CAP_NORFLASH
;
306 mtd
->name
= map
->name
;
309 if (cfi
->cfi_mode
==CFI_MODE_CFI
){
310 unsigned char bootloc
;
312 * It's a real CFI chip, not one for which the probe
313 * routine faked a CFI structure. So we read the feature
316 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
317 struct cfi_pri_amdstd
*extp
;
319 extp
= (struct cfi_pri_amdstd
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "Amd/Fujitsu");
325 if (extp
->MajorVersion
!= '1' ||
326 (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '4')) {
327 printk(KERN_ERR
" Unknown Amd/Fujitsu Extended Query "
328 "version %c.%c.\n", extp
->MajorVersion
,
335 /* Install our own private info structure */
336 cfi
->cmdset_priv
= extp
;
338 /* Apply cfi device specific fixups */
339 cfi_fixup(mtd
, cfi_fixup_table
);
341 #ifdef DEBUG_CFI_FEATURES
342 /* Tell the user about it in lots of lovely detail */
343 cfi_tell_features(extp
);
346 bootloc
= extp
->TopBottom
;
347 if ((bootloc
!= 2) && (bootloc
!= 3)) {
348 printk(KERN_WARNING
"%s: CFI does not contain boot "
349 "bank location. Assuming top.\n", map
->name
);
353 if (bootloc
== 3 && cfi
->cfiq
->NumEraseRegions
> 1) {
354 printk(KERN_WARNING
"%s: Swapping erase regions for broken CFI table.\n", map
->name
);
356 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
/ 2; i
++) {
357 int j
= (cfi
->cfiq
->NumEraseRegions
-1)-i
;
360 swap
= cfi
->cfiq
->EraseRegionInfo
[i
];
361 cfi
->cfiq
->EraseRegionInfo
[i
] = cfi
->cfiq
->EraseRegionInfo
[j
];
362 cfi
->cfiq
->EraseRegionInfo
[j
] = swap
;
365 /* Set the default CFI lock/unlock addresses */
366 cfi
->addr_unlock1
= 0x555;
367 cfi
->addr_unlock2
= 0x2aa;
368 /* Modify the unlock address if we are in compatibility mode */
369 if ( /* x16 in x8 mode */
370 ((cfi
->device_type
== CFI_DEVICETYPE_X8
) &&
371 (cfi
->cfiq
->InterfaceDesc
==
372 CFI_INTERFACE_X8_BY_X16_ASYNC
)) ||
373 /* x32 in x16 mode */
374 ((cfi
->device_type
== CFI_DEVICETYPE_X16
) &&
375 (cfi
->cfiq
->InterfaceDesc
==
376 CFI_INTERFACE_X16_BY_X32_ASYNC
)))
378 cfi
->addr_unlock1
= 0xaaa;
379 cfi
->addr_unlock2
= 0x555;
383 else if (cfi
->cfi_mode
== CFI_MODE_JEDEC
) {
384 /* Apply jedec specific fixups */
385 cfi_fixup(mtd
, jedec_fixup_table
);
387 /* Apply generic fixups */
388 cfi_fixup(mtd
, fixup_table
);
390 for (i
=0; i
< cfi
->numchips
; i
++) {
391 cfi
->chips
[i
].word_write_time
= 1<<cfi
->cfiq
->WordWriteTimeoutTyp
;
392 cfi
->chips
[i
].buffer_write_time
= 1<<cfi
->cfiq
->BufWriteTimeoutTyp
;
393 cfi
->chips
[i
].erase_time
= 1<<cfi
->cfiq
->BlockEraseTimeoutTyp
;
394 cfi
->chips
[i
].ref_point_counter
= 0;
395 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
398 map
->fldrv
= &cfi_amdstd_chipdrv
;
400 return cfi_amdstd_setup(mtd
);
402 EXPORT_SYMBOL_GPL(cfi_cmdset_0002
);
404 static struct mtd_info
*cfi_amdstd_setup(struct mtd_info
*mtd
)
406 struct map_info
*map
= mtd
->priv
;
407 struct cfi_private
*cfi
= map
->fldrv_priv
;
408 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
409 unsigned long offset
= 0;
412 printk(KERN_NOTICE
"number of %s chips: %d\n",
413 (cfi
->cfi_mode
== CFI_MODE_CFI
)?"CFI":"JEDEC",cfi
->numchips
);
414 /* Select the correct geometry setup */
415 mtd
->size
= devsize
* cfi
->numchips
;
417 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
418 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
419 * mtd
->numeraseregions
, GFP_KERNEL
);
420 if (!mtd
->eraseregions
) {
421 printk(KERN_WARNING
"Failed to allocate memory for MTD erase region info\n");
425 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
426 unsigned long ernum
, ersize
;
427 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
428 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
430 if (mtd
->erasesize
< ersize
) {
431 mtd
->erasesize
= ersize
;
433 for (j
=0; j
<cfi
->numchips
; j
++) {
434 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
435 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
436 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
438 offset
+= (ersize
* ernum
);
440 if (offset
!= devsize
) {
442 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
447 for (i
=0; i
<mtd
->numeraseregions
;i
++){
448 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
449 i
,mtd
->eraseregions
[i
].offset
,
450 mtd
->eraseregions
[i
].erasesize
,
451 mtd
->eraseregions
[i
].numblocks
);
455 /* FIXME: erase-suspend-program is broken. See
456 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
457 printk(KERN_NOTICE
"cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
459 __module_get(THIS_MODULE
);
464 kfree(mtd
->eraseregions
);
467 kfree(cfi
->cmdset_priv
);
473 * Return true if the chip is ready.
475 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
476 * non-suspended sector) and is indicated by no toggle bits toggling.
478 * Note that anything more complicated than checking if no bits are toggling
479 * (including checking DQ5 for an error status) is tricky to get working
480 * correctly and is therefore not done (particulary with interleaved chips
481 * as each chip must be checked independantly of the others).
483 static int __xipram
chip_ready(struct map_info
*map
, unsigned long addr
)
487 d
= map_read(map
, addr
);
488 t
= map_read(map
, addr
);
490 return map_word_equal(map
, d
, t
);
494 * Return true if the chip is ready and has the correct value.
496 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
497 * non-suspended sector) and it is indicated by no bits toggling.
499 * Error are indicated by toggling bits or bits held with the wrong value,
500 * or with bits toggling.
502 * Note that anything more complicated than checking if no bits are toggling
503 * (including checking DQ5 for an error status) is tricky to get working
504 * correctly and is therefore not done (particulary with interleaved chips
505 * as each chip must be checked independantly of the others).
508 static int __xipram
chip_good(struct map_info
*map
, unsigned long addr
, map_word expected
)
512 oldd
= map_read(map
, addr
);
513 curd
= map_read(map
, addr
);
515 return map_word_equal(map
, oldd
, curd
) &&
516 map_word_equal(map
, curd
, expected
);
519 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
)
521 DECLARE_WAITQUEUE(wait
, current
);
522 struct cfi_private
*cfi
= map
->fldrv_priv
;
524 struct cfi_pri_amdstd
*cfip
= (struct cfi_pri_amdstd
*)cfi
->cmdset_priv
;
527 timeo
= jiffies
+ HZ
;
529 switch (chip
->state
) {
533 if (chip_ready(map
, adr
))
536 if (time_after(jiffies
, timeo
)) {
537 printk(KERN_ERR
"Waiting for chip to be ready timed out.\n");
538 spin_unlock(chip
->mutex
);
541 spin_unlock(chip
->mutex
);
543 spin_lock(chip
->mutex
);
544 /* Someone else might have been playing with it. */
554 if (mode
== FL_WRITING
) /* FIXME: Erase-suspend-program appears broken. */
557 if (!( mode
== FL_READY
560 || (mode
== FL_WRITING
&& (cfip
->EraseSuspend
& 0x2))
561 || (mode
== FL_WRITING
&& (cfip
->EraseSuspend
& 0x1)
565 /* We could check to see if we're trying to access the sector
566 * that is currently being erased. However, no user will try
567 * anything like that so we just wait for the timeout. */
570 /* It's harmless to issue the Erase-Suspend and Erase-Resume
571 * commands when the erase algorithm isn't in progress. */
572 map_write(map
, CMD(0xB0), chip
->in_progress_block_addr
);
573 chip
->oldstate
= FL_ERASING
;
574 chip
->state
= FL_ERASE_SUSPENDING
;
575 chip
->erase_suspended
= 1;
577 if (chip_ready(map
, adr
))
580 if (time_after(jiffies
, timeo
)) {
581 /* Should have suspended the erase by now.
582 * Send an Erase-Resume command as either
583 * there was an error (so leave the erase
584 * routine to recover from it) or we trying to
585 * use the erase-in-progress sector. */
586 map_write(map
, CMD(0x30), chip
->in_progress_block_addr
);
587 chip
->state
= FL_ERASING
;
588 chip
->oldstate
= FL_READY
;
589 printk(KERN_ERR
"MTD %s(): chip not ready after erase suspend\n", __func__
);
593 spin_unlock(chip
->mutex
);
595 spin_lock(chip
->mutex
);
596 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
597 So we can just loop here. */
599 chip
->state
= FL_READY
;
602 case FL_XIP_WHILE_ERASING
:
603 if (mode
!= FL_READY
&& mode
!= FL_POINT
&&
604 (!cfip
|| !(cfip
->EraseSuspend
&2)))
606 chip
->oldstate
= chip
->state
;
607 chip
->state
= FL_READY
;
611 /* Only if there's no operation suspended... */
612 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
617 set_current_state(TASK_UNINTERRUPTIBLE
);
618 add_wait_queue(&chip
->wq
, &wait
);
619 spin_unlock(chip
->mutex
);
621 remove_wait_queue(&chip
->wq
, &wait
);
622 spin_lock(chip
->mutex
);
628 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
630 struct cfi_private
*cfi
= map
->fldrv_priv
;
632 switch(chip
->oldstate
) {
634 chip
->state
= chip
->oldstate
;
635 map_write(map
, CMD(0x30), chip
->in_progress_block_addr
);
636 chip
->oldstate
= FL_READY
;
637 chip
->state
= FL_ERASING
;
640 case FL_XIP_WHILE_ERASING
:
641 chip
->state
= chip
->oldstate
;
642 chip
->oldstate
= FL_READY
;
647 /* We should really make set_vpp() count, rather than doing this */
651 printk(KERN_ERR
"MTD: put_chip() called with oldstate %d!!\n", chip
->oldstate
);
656 #ifdef CONFIG_MTD_XIP
659 * No interrupt what so ever can be serviced while the flash isn't in array
660 * mode. This is ensured by the xip_disable() and xip_enable() functions
661 * enclosing any code path where the flash is known not to be in array mode.
662 * And within a XIP disabled code path, only functions marked with __xipram
663 * may be called and nothing else (it's a good thing to inspect generated
664 * assembly to make sure inline functions were actually inlined and that gcc
665 * didn't emit calls to its own support functions). Also configuring MTD CFI
666 * support to a single buswidth and a single interleave is also recommended.
669 static void xip_disable(struct map_info
*map
, struct flchip
*chip
,
672 /* TODO: chips with no XIP use should ignore and return */
673 (void) map_read(map
, adr
); /* ensure mmu mapping is up to date */
677 static void __xipram
xip_enable(struct map_info
*map
, struct flchip
*chip
,
680 struct cfi_private
*cfi
= map
->fldrv_priv
;
682 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
683 map_write(map
, CMD(0xf0), adr
);
684 chip
->state
= FL_READY
;
686 (void) map_read(map
, adr
);
692 * When a delay is required for the flash operation to complete, the
693 * xip_udelay() function is polling for both the given timeout and pending
694 * (but still masked) hardware interrupts. Whenever there is an interrupt
695 * pending then the flash erase operation is suspended, array mode restored
696 * and interrupts unmasked. Task scheduling might also happen at that
697 * point. The CPU eventually returns from the interrupt or the call to
698 * schedule() and the suspended flash operation is resumed for the remaining
699 * of the delay period.
701 * Warning: this function _will_ fool interrupt latency tracing tools.
704 static void __xipram
xip_udelay(struct map_info
*map
, struct flchip
*chip
,
705 unsigned long adr
, int usec
)
707 struct cfi_private
*cfi
= map
->fldrv_priv
;
708 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
709 map_word status
, OK
= CMD(0x80);
710 unsigned long suspended
, start
= xip_currtime();
715 if (xip_irqpending() && extp
&&
716 ((chip
->state
== FL_ERASING
&& (extp
->EraseSuspend
& 2))) &&
717 (cfi_interleave_is_1(cfi
) || chip
->oldstate
== FL_READY
)) {
719 * Let's suspend the erase operation when supported.
720 * Note that we currently don't try to suspend
721 * interleaved chips if there is already another
722 * operation suspended (imagine what happens
723 * when one chip was already done with the current
724 * operation while another chip suspended it, then
725 * we resume the whole thing at once). Yes, it
728 map_write(map
, CMD(0xb0), adr
);
729 usec
-= xip_elapsed_since(start
);
730 suspended
= xip_currtime();
732 if (xip_elapsed_since(suspended
) > 100000) {
734 * The chip doesn't want to suspend
735 * after waiting for 100 msecs.
736 * This is a critical error but there
737 * is not much we can do here.
741 status
= map_read(map
, adr
);
742 } while (!map_word_andequal(map
, status
, OK
, OK
));
744 /* Suspend succeeded */
745 oldstate
= chip
->state
;
746 if (!map_word_bitsset(map
, status
, CMD(0x40)))
748 chip
->state
= FL_XIP_WHILE_ERASING
;
749 chip
->erase_suspended
= 1;
750 map_write(map
, CMD(0xf0), adr
);
751 (void) map_read(map
, adr
);
754 spin_unlock(chip
->mutex
);
759 * We're back. However someone else might have
760 * decided to go write to the chip if we are in
761 * a suspended erase state. If so let's wait
764 spin_lock(chip
->mutex
);
765 while (chip
->state
!= FL_XIP_WHILE_ERASING
) {
766 DECLARE_WAITQUEUE(wait
, current
);
767 set_current_state(TASK_UNINTERRUPTIBLE
);
768 add_wait_queue(&chip
->wq
, &wait
);
769 spin_unlock(chip
->mutex
);
771 remove_wait_queue(&chip
->wq
, &wait
);
772 spin_lock(chip
->mutex
);
774 /* Disallow XIP again */
777 /* Resume the write or erase operation */
778 map_write(map
, CMD(0x30), adr
);
779 chip
->state
= oldstate
;
780 start
= xip_currtime();
781 } else if (usec
>= 1000000/HZ
) {
783 * Try to save on CPU power when waiting delay
784 * is at least a system timer tick period.
785 * No need to be extremely accurate here.
789 status
= map_read(map
, adr
);
790 } while (!map_word_andequal(map
, status
, OK
, OK
)
791 && xip_elapsed_since(start
) < usec
);
794 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
797 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
798 * the flash is actively programming or erasing since we have to poll for
799 * the operation to complete anyway. We can't do that in a generic way with
800 * a XIP setup so do it before the actual flash operation in this case
801 * and stub it out from INVALIDATE_CACHE_UDELAY.
803 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
804 INVALIDATE_CACHED_RANGE(map, from, size)
806 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
807 UDELAY(map, chip, adr, usec)
812 * Activating this XIP support changes the way the code works a bit. For
813 * example the code to suspend the current process when concurrent access
814 * happens is never executed because xip_udelay() will always return with the
815 * same chip state as it was entered with. This is why there is no care for
816 * the presence of add_wait_queue() or schedule() calls from within a couple
817 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
818 * The queueing and scheduling are always happening within xip_udelay().
820 * Similarly, get_chip() and put_chip() just happen to always be executed
821 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
822 * is in array mode, therefore never executing many cases therein and not
823 * causing any problem with XIP.
828 #define xip_disable(map, chip, adr)
829 #define xip_enable(map, chip, adr)
830 #define XIP_INVAL_CACHED_RANGE(x...)
832 #define UDELAY(map, chip, adr, usec) \
834 spin_unlock(chip->mutex); \
836 spin_lock(chip->mutex); \
839 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
841 spin_unlock(chip->mutex); \
842 INVALIDATE_CACHED_RANGE(map, adr, len); \
844 spin_lock(chip->mutex); \
849 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
851 unsigned long cmd_addr
;
852 struct cfi_private
*cfi
= map
->fldrv_priv
;
857 /* Ensure cmd read/writes are aligned. */
858 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
860 spin_lock(chip
->mutex
);
861 ret
= get_chip(map
, chip
, cmd_addr
, FL_READY
);
863 spin_unlock(chip
->mutex
);
867 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
868 map_write(map
, CMD(0xf0), cmd_addr
);
869 chip
->state
= FL_READY
;
872 map_copy_from(map
, buf
, adr
, len
);
874 put_chip(map
, chip
, cmd_addr
);
876 spin_unlock(chip
->mutex
);
881 static int cfi_amdstd_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
883 struct map_info
*map
= mtd
->priv
;
884 struct cfi_private
*cfi
= map
->fldrv_priv
;
889 /* ofs: offset within the first chip that the first read should start */
891 chipnum
= (from
>> cfi
->chipshift
);
892 ofs
= from
- (chipnum
<< cfi
->chipshift
);
898 unsigned long thislen
;
900 if (chipnum
>= cfi
->numchips
)
903 if ((len
+ ofs
-1) >> cfi
->chipshift
)
904 thislen
= (1<<cfi
->chipshift
) - ofs
;
908 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
923 static inline int do_read_secsi_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
925 DECLARE_WAITQUEUE(wait
, current
);
926 unsigned long timeo
= jiffies
+ HZ
;
927 struct cfi_private
*cfi
= map
->fldrv_priv
;
930 spin_lock(chip
->mutex
);
932 if (chip
->state
!= FL_READY
){
934 printk(KERN_DEBUG
"Waiting for chip to read, status = %d\n", chip
->state
);
936 set_current_state(TASK_UNINTERRUPTIBLE
);
937 add_wait_queue(&chip
->wq
, &wait
);
939 spin_unlock(chip
->mutex
);
942 remove_wait_queue(&chip
->wq
, &wait
);
944 if(signal_pending(current
))
947 timeo
= jiffies
+ HZ
;
954 chip
->state
= FL_READY
;
956 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
957 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
958 cfi_send_gen_cmd(0x88, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
960 map_copy_from(map
, buf
, adr
, len
);
962 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
963 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
964 cfi_send_gen_cmd(0x90, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
965 cfi_send_gen_cmd(0x00, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
968 spin_unlock(chip
->mutex
);
973 static int cfi_amdstd_secsi_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
975 struct map_info
*map
= mtd
->priv
;
976 struct cfi_private
*cfi
= map
->fldrv_priv
;
982 /* ofs: offset within the first chip that the first read should start */
984 /* 8 secsi bytes per chip */
992 unsigned long thislen
;
994 if (chipnum
>= cfi
->numchips
)
997 if ((len
+ ofs
-1) >> 3)
998 thislen
= (1<<3) - ofs
;
1002 ret
= do_read_secsi_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
1017 static int __xipram
do_write_oneword(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, map_word datum
)
1019 struct cfi_private
*cfi
= map
->fldrv_priv
;
1020 unsigned long timeo
= jiffies
+ HZ
;
1022 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1023 * have a max write time of a few hundreds usec). However, we should
1024 * use the maximum timeout value given by the chip at probe time
1025 * instead. Unfortunately, struct flchip does have a field for
1026 * maximum timeout, only for typical which can be far too short
1027 * depending of the conditions. The ' + 1' is to avoid having a
1028 * timeout of 0 jiffies if HZ is smaller than 1000.
1030 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1037 spin_lock(chip
->mutex
);
1038 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1040 spin_unlock(chip
->mutex
);
1044 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1045 __func__
, adr
, datum
.x
[0] );
1048 * Check for a NOP for the case when the datum to write is already
1049 * present - it saves time and works around buggy chips that corrupt
1050 * data at other locations when 0xff is written to a location that
1051 * already contains 0xff.
1053 oldd
= map_read(map
, adr
);
1054 if (map_word_equal(map
, oldd
, datum
)) {
1055 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): NOP\n",
1060 XIP_INVAL_CACHED_RANGE(map
, adr
, map_bankwidth(map
));
1062 xip_disable(map
, chip
, adr
);
1064 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1065 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1066 cfi_send_gen_cmd(0xA0, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1067 map_write(map
, datum
, adr
);
1068 chip
->state
= FL_WRITING
;
1070 INVALIDATE_CACHE_UDELAY(map
, chip
,
1071 adr
, map_bankwidth(map
),
1072 chip
->word_write_time
);
1074 /* See comment above for timeout value. */
1075 timeo
= jiffies
+ uWriteTimeout
;
1077 if (chip
->state
!= FL_WRITING
) {
1078 /* Someone's suspended the write. Sleep */
1079 DECLARE_WAITQUEUE(wait
, current
);
1081 set_current_state(TASK_UNINTERRUPTIBLE
);
1082 add_wait_queue(&chip
->wq
, &wait
);
1083 spin_unlock(chip
->mutex
);
1085 remove_wait_queue(&chip
->wq
, &wait
);
1086 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1087 spin_lock(chip
->mutex
);
1091 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
)){
1092 xip_enable(map
, chip
, adr
);
1093 printk(KERN_WARNING
"MTD %s(): software timeout\n", __func__
);
1094 xip_disable(map
, chip
, adr
);
1098 if (chip_ready(map
, adr
))
1101 /* Latency issues. Drop the lock, wait a while and retry */
1102 UDELAY(map
, chip
, adr
, 1);
1104 /* Did we succeed? */
1105 if (!chip_good(map
, adr
, datum
)) {
1106 /* reset on all failures. */
1107 map_write( map
, CMD(0xF0), chip
->start
);
1108 /* FIXME - should have reset delay before continuing */
1110 if (++retry_cnt
<= MAX_WORD_RETRIES
)
1115 xip_enable(map
, chip
, adr
);
1117 chip
->state
= FL_READY
;
1118 put_chip(map
, chip
, adr
);
1119 spin_unlock(chip
->mutex
);
1125 static int cfi_amdstd_write_words(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1126 size_t *retlen
, const u_char
*buf
)
1128 struct map_info
*map
= mtd
->priv
;
1129 struct cfi_private
*cfi
= map
->fldrv_priv
;
1132 unsigned long ofs
, chipstart
;
1133 DECLARE_WAITQUEUE(wait
, current
);
1139 chipnum
= to
>> cfi
->chipshift
;
1140 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1141 chipstart
= cfi
->chips
[chipnum
].start
;
1143 /* If it's not bus-aligned, do the first byte write */
1144 if (ofs
& (map_bankwidth(map
)-1)) {
1145 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
)-1);
1146 int i
= ofs
- bus_ofs
;
1151 spin_lock(cfi
->chips
[chipnum
].mutex
);
1153 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1155 printk(KERN_DEBUG
"Waiting for chip to write, status = %d\n", cfi
->chips
[chipnum
].state
);
1157 set_current_state(TASK_UNINTERRUPTIBLE
);
1158 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1160 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1163 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1165 if(signal_pending(current
))
1171 /* Load 'tmp_buf' with old contents of flash */
1172 tmp_buf
= map_read(map
, bus_ofs
+chipstart
);
1174 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1176 /* Number of bytes to copy from buffer */
1177 n
= min_t(int, len
, map_bankwidth(map
)-i
);
1179 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, i
, n
);
1181 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1191 if (ofs
>> cfi
->chipshift
) {
1194 if (chipnum
== cfi
->numchips
)
1199 /* We are now aligned, write as much as possible */
1200 while(len
>= map_bankwidth(map
)) {
1203 datum
= map_word_load(map
, buf
);
1205 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1210 ofs
+= map_bankwidth(map
);
1211 buf
+= map_bankwidth(map
);
1212 (*retlen
) += map_bankwidth(map
);
1213 len
-= map_bankwidth(map
);
1215 if (ofs
>> cfi
->chipshift
) {
1218 if (chipnum
== cfi
->numchips
)
1220 chipstart
= cfi
->chips
[chipnum
].start
;
1224 /* Write the trailing bytes if any */
1225 if (len
& (map_bankwidth(map
)-1)) {
1229 spin_lock(cfi
->chips
[chipnum
].mutex
);
1231 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1233 printk(KERN_DEBUG
"Waiting for chip to write, status = %d\n", cfi
->chips
[chipnum
].state
);
1235 set_current_state(TASK_UNINTERRUPTIBLE
);
1236 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1238 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1241 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1243 if(signal_pending(current
))
1249 tmp_buf
= map_read(map
, ofs
+ chipstart
);
1251 spin_unlock(cfi
->chips
[chipnum
].mutex
);
1253 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, 0, len
);
1255 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1268 * FIXME: interleaved mode not tested, and probably not supported!
1270 static int __xipram
do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
1271 unsigned long adr
, const u_char
*buf
,
1274 struct cfi_private
*cfi
= map
->fldrv_priv
;
1275 unsigned long timeo
= jiffies
+ HZ
;
1276 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1277 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1279 unsigned long cmd_adr
;
1286 spin_lock(chip
->mutex
);
1287 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1289 spin_unlock(chip
->mutex
);
1293 datum
= map_word_load(map
, buf
);
1295 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1296 __func__
, adr
, datum
.x
[0] );
1298 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1300 xip_disable(map
, chip
, cmd_adr
);
1302 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1303 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1304 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1306 /* Write Buffer Load */
1307 map_write(map
, CMD(0x25), cmd_adr
);
1309 chip
->state
= FL_WRITING_TO_BUFFER
;
1311 /* Write length of data to come */
1312 words
= len
/ map_bankwidth(map
);
1313 map_write(map
, CMD(words
- 1), cmd_adr
);
1316 while(z
< words
* map_bankwidth(map
)) {
1317 datum
= map_word_load(map
, buf
);
1318 map_write(map
, datum
, adr
+ z
);
1320 z
+= map_bankwidth(map
);
1321 buf
+= map_bankwidth(map
);
1323 z
-= map_bankwidth(map
);
1327 /* Write Buffer Program Confirm: GO GO GO */
1328 map_write(map
, CMD(0x29), cmd_adr
);
1329 chip
->state
= FL_WRITING
;
1331 INVALIDATE_CACHE_UDELAY(map
, chip
,
1332 adr
, map_bankwidth(map
),
1333 chip
->word_write_time
);
1335 timeo
= jiffies
+ uWriteTimeout
;
1338 if (chip
->state
!= FL_WRITING
) {
1339 /* Someone's suspended the write. Sleep */
1340 DECLARE_WAITQUEUE(wait
, current
);
1342 set_current_state(TASK_UNINTERRUPTIBLE
);
1343 add_wait_queue(&chip
->wq
, &wait
);
1344 spin_unlock(chip
->mutex
);
1346 remove_wait_queue(&chip
->wq
, &wait
);
1347 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1348 spin_lock(chip
->mutex
);
1352 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
))
1355 if (chip_ready(map
, adr
)) {
1356 xip_enable(map
, chip
, adr
);
1360 /* Latency issues. Drop the lock, wait a while and retry */
1361 UDELAY(map
, chip
, adr
, 1);
1364 /* reset on all failures. */
1365 map_write( map
, CMD(0xF0), chip
->start
);
1366 xip_enable(map
, chip
, adr
);
1367 /* FIXME - should have reset delay before continuing */
1369 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1374 chip
->state
= FL_READY
;
1375 put_chip(map
, chip
, adr
);
1376 spin_unlock(chip
->mutex
);
1382 static int cfi_amdstd_write_buffers(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1383 size_t *retlen
, const u_char
*buf
)
1385 struct map_info
*map
= mtd
->priv
;
1386 struct cfi_private
*cfi
= map
->fldrv_priv
;
1387 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1396 chipnum
= to
>> cfi
->chipshift
;
1397 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1399 /* If it's not bus-aligned, do the first word write */
1400 if (ofs
& (map_bankwidth(map
)-1)) {
1401 size_t local_len
= (-ofs
)&(map_bankwidth(map
)-1);
1402 if (local_len
> len
)
1404 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1405 local_len
, retlen
, buf
);
1412 if (ofs
>> cfi
->chipshift
) {
1415 if (chipnum
== cfi
->numchips
)
1420 /* Write buffer is worth it only if more than one word to write... */
1421 while (len
>= map_bankwidth(map
) * 2) {
1422 /* We must not cross write block boundaries */
1423 int size
= wbufsize
- (ofs
& (wbufsize
-1));
1427 if (size
% map_bankwidth(map
))
1428 size
-= size
% map_bankwidth(map
);
1430 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
1440 if (ofs
>> cfi
->chipshift
) {
1443 if (chipnum
== cfi
->numchips
)
1449 size_t retlen_dregs
= 0;
1451 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1452 len
, &retlen_dregs
, buf
);
1454 *retlen
+= retlen_dregs
;
1463 * Handle devices with one erase region, that only implement
1464 * the chip erase command.
1466 static int __xipram
do_erase_chip(struct map_info
*map
, struct flchip
*chip
)
1468 struct cfi_private
*cfi
= map
->fldrv_priv
;
1469 unsigned long timeo
= jiffies
+ HZ
;
1470 unsigned long int adr
;
1471 DECLARE_WAITQUEUE(wait
, current
);
1474 adr
= cfi
->addr_unlock1
;
1476 spin_lock(chip
->mutex
);
1477 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1479 spin_unlock(chip
->mutex
);
1483 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): ERASE 0x%.8lx\n",
1484 __func__
, chip
->start
);
1486 XIP_INVAL_CACHED_RANGE(map
, adr
, map
->size
);
1488 xip_disable(map
, chip
, adr
);
1490 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1491 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1492 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1493 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1494 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1495 cfi_send_gen_cmd(0x10, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1497 chip
->state
= FL_ERASING
;
1498 chip
->erase_suspended
= 0;
1499 chip
->in_progress_block_addr
= adr
;
1501 INVALIDATE_CACHE_UDELAY(map
, chip
,
1503 chip
->erase_time
*500);
1505 timeo
= jiffies
+ (HZ
*20);
1508 if (chip
->state
!= FL_ERASING
) {
1509 /* Someone's suspended the erase. Sleep */
1510 set_current_state(TASK_UNINTERRUPTIBLE
);
1511 add_wait_queue(&chip
->wq
, &wait
);
1512 spin_unlock(chip
->mutex
);
1514 remove_wait_queue(&chip
->wq
, &wait
);
1515 spin_lock(chip
->mutex
);
1518 if (chip
->erase_suspended
) {
1519 /* This erase was suspended and resumed.
1520 Adjust the timeout */
1521 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1522 chip
->erase_suspended
= 0;
1525 if (chip_ready(map
, adr
))
1528 if (time_after(jiffies
, timeo
)) {
1529 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1534 /* Latency issues. Drop the lock, wait a while and retry */
1535 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1537 /* Did we succeed? */
1538 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1539 /* reset on all failures. */
1540 map_write( map
, CMD(0xF0), chip
->start
);
1541 /* FIXME - should have reset delay before continuing */
1546 chip
->state
= FL_READY
;
1547 xip_enable(map
, chip
, adr
);
1548 put_chip(map
, chip
, adr
);
1549 spin_unlock(chip
->mutex
);
1555 static int __xipram
do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int len
, void *thunk
)
1557 struct cfi_private
*cfi
= map
->fldrv_priv
;
1558 unsigned long timeo
= jiffies
+ HZ
;
1559 DECLARE_WAITQUEUE(wait
, current
);
1564 spin_lock(chip
->mutex
);
1565 ret
= get_chip(map
, chip
, adr
, FL_ERASING
);
1567 spin_unlock(chip
->mutex
);
1571 DEBUG( MTD_DEBUG_LEVEL3
, "MTD %s(): ERASE 0x%.8lx\n",
1574 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1576 xip_disable(map
, chip
, adr
);
1578 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1579 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1580 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1581 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1582 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1583 map_write(map
, CMD(0x30), adr
);
1585 chip
->state
= FL_ERASING
;
1586 chip
->erase_suspended
= 0;
1587 chip
->in_progress_block_addr
= adr
;
1589 INVALIDATE_CACHE_UDELAY(map
, chip
,
1591 chip
->erase_time
*500);
1593 timeo
= jiffies
+ (HZ
*20);
1596 if (chip
->state
!= FL_ERASING
) {
1597 /* Someone's suspended the erase. Sleep */
1598 set_current_state(TASK_UNINTERRUPTIBLE
);
1599 add_wait_queue(&chip
->wq
, &wait
);
1600 spin_unlock(chip
->mutex
);
1602 remove_wait_queue(&chip
->wq
, &wait
);
1603 spin_lock(chip
->mutex
);
1606 if (chip
->erase_suspended
) {
1607 /* This erase was suspended and resumed.
1608 Adjust the timeout */
1609 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1610 chip
->erase_suspended
= 0;
1613 if (chip_ready(map
, adr
)) {
1614 xip_enable(map
, chip
, adr
);
1618 if (time_after(jiffies
, timeo
)) {
1619 xip_enable(map
, chip
, adr
);
1620 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1625 /* Latency issues. Drop the lock, wait a while and retry */
1626 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1628 /* Did we succeed? */
1629 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1630 /* reset on all failures. */
1631 map_write( map
, CMD(0xF0), chip
->start
);
1632 /* FIXME - should have reset delay before continuing */
1637 chip
->state
= FL_READY
;
1638 put_chip(map
, chip
, adr
);
1639 spin_unlock(chip
->mutex
);
1644 static int cfi_amdstd_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
1646 unsigned long ofs
, len
;
1652 ret
= cfi_varsize_frob(mtd
, do_erase_oneblock
, ofs
, len
, NULL
);
1656 instr
->state
= MTD_ERASE_DONE
;
1657 mtd_erase_callback(instr
);
1663 static int cfi_amdstd_erase_chip(struct mtd_info
*mtd
, struct erase_info
*instr
)
1665 struct map_info
*map
= mtd
->priv
;
1666 struct cfi_private
*cfi
= map
->fldrv_priv
;
1669 if (instr
->addr
!= 0)
1672 if (instr
->len
!= mtd
->size
)
1675 ret
= do_erase_chip(map
, &cfi
->chips
[0]);
1679 instr
->state
= MTD_ERASE_DONE
;
1680 mtd_erase_callback(instr
);
1685 static int do_atmel_lock(struct map_info
*map
, struct flchip
*chip
,
1686 unsigned long adr
, int len
, void *thunk
)
1688 struct cfi_private
*cfi
= map
->fldrv_priv
;
1691 spin_lock(chip
->mutex
);
1692 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_LOCKING
);
1695 chip
->state
= FL_LOCKING
;
1697 DEBUG(MTD_DEBUG_LEVEL3
, "MTD %s(): LOCK 0x%08lx len %d\n",
1698 __func__
, adr
, len
);
1700 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1701 cfi
->device_type
, NULL
);
1702 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
1703 cfi
->device_type
, NULL
);
1704 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1705 cfi
->device_type
, NULL
);
1706 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1707 cfi
->device_type
, NULL
);
1708 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
1709 cfi
->device_type
, NULL
);
1710 map_write(map
, CMD(0x40), chip
->start
+ adr
);
1712 chip
->state
= FL_READY
;
1713 put_chip(map
, chip
, adr
+ chip
->start
);
1717 spin_unlock(chip
->mutex
);
1721 static int do_atmel_unlock(struct map_info
*map
, struct flchip
*chip
,
1722 unsigned long adr
, int len
, void *thunk
)
1724 struct cfi_private
*cfi
= map
->fldrv_priv
;
1727 spin_lock(chip
->mutex
);
1728 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_UNLOCKING
);
1731 chip
->state
= FL_UNLOCKING
;
1733 DEBUG(MTD_DEBUG_LEVEL3
, "MTD %s(): LOCK 0x%08lx len %d\n",
1734 __func__
, adr
, len
);
1736 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
1737 cfi
->device_type
, NULL
);
1738 map_write(map
, CMD(0x70), adr
);
1740 chip
->state
= FL_READY
;
1741 put_chip(map
, chip
, adr
+ chip
->start
);
1745 spin_unlock(chip
->mutex
);
1749 static int cfi_atmel_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1751 return cfi_varsize_frob(mtd
, do_atmel_lock
, ofs
, len
, NULL
);
1754 static int cfi_atmel_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1756 return cfi_varsize_frob(mtd
, do_atmel_unlock
, ofs
, len
, NULL
);
1760 static void cfi_amdstd_sync (struct mtd_info
*mtd
)
1762 struct map_info
*map
= mtd
->priv
;
1763 struct cfi_private
*cfi
= map
->fldrv_priv
;
1765 struct flchip
*chip
;
1767 DECLARE_WAITQUEUE(wait
, current
);
1769 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1770 chip
= &cfi
->chips
[i
];
1773 spin_lock(chip
->mutex
);
1775 switch(chip
->state
) {
1779 case FL_JEDEC_QUERY
:
1780 chip
->oldstate
= chip
->state
;
1781 chip
->state
= FL_SYNCING
;
1782 /* No need to wake_up() on this state change -
1783 * as the whole point is that nobody can do anything
1784 * with the chip now anyway.
1787 spin_unlock(chip
->mutex
);
1791 /* Not an idle state */
1792 set_current_state(TASK_UNINTERRUPTIBLE
);
1793 add_wait_queue(&chip
->wq
, &wait
);
1795 spin_unlock(chip
->mutex
);
1799 remove_wait_queue(&chip
->wq
, &wait
);
1805 /* Unlock the chips again */
1807 for (i
--; i
>=0; i
--) {
1808 chip
= &cfi
->chips
[i
];
1810 spin_lock(chip
->mutex
);
1812 if (chip
->state
== FL_SYNCING
) {
1813 chip
->state
= chip
->oldstate
;
1816 spin_unlock(chip
->mutex
);
1821 static int cfi_amdstd_suspend(struct mtd_info
*mtd
)
1823 struct map_info
*map
= mtd
->priv
;
1824 struct cfi_private
*cfi
= map
->fldrv_priv
;
1826 struct flchip
*chip
;
1829 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1830 chip
= &cfi
->chips
[i
];
1832 spin_lock(chip
->mutex
);
1834 switch(chip
->state
) {
1838 case FL_JEDEC_QUERY
:
1839 chip
->oldstate
= chip
->state
;
1840 chip
->state
= FL_PM_SUSPENDED
;
1841 /* No need to wake_up() on this state change -
1842 * as the whole point is that nobody can do anything
1843 * with the chip now anyway.
1845 case FL_PM_SUSPENDED
:
1852 spin_unlock(chip
->mutex
);
1855 /* Unlock the chips again */
1858 for (i
--; i
>=0; i
--) {
1859 chip
= &cfi
->chips
[i
];
1861 spin_lock(chip
->mutex
);
1863 if (chip
->state
== FL_PM_SUSPENDED
) {
1864 chip
->state
= chip
->oldstate
;
1867 spin_unlock(chip
->mutex
);
1875 static void cfi_amdstd_resume(struct mtd_info
*mtd
)
1877 struct map_info
*map
= mtd
->priv
;
1878 struct cfi_private
*cfi
= map
->fldrv_priv
;
1880 struct flchip
*chip
;
1882 for (i
=0; i
<cfi
->numchips
; i
++) {
1884 chip
= &cfi
->chips
[i
];
1886 spin_lock(chip
->mutex
);
1888 if (chip
->state
== FL_PM_SUSPENDED
) {
1889 chip
->state
= FL_READY
;
1890 map_write(map
, CMD(0xF0), chip
->start
);
1894 printk(KERN_ERR
"Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1896 spin_unlock(chip
->mutex
);
1900 static void cfi_amdstd_destroy(struct mtd_info
*mtd
)
1902 struct map_info
*map
= mtd
->priv
;
1903 struct cfi_private
*cfi
= map
->fldrv_priv
;
1905 kfree(cfi
->cmdset_priv
);
1908 kfree(mtd
->eraseregions
);
1911 MODULE_LICENSE("GPL");
1912 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1913 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");