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 * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
18 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
29 #include <asm/byteorder.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/reboot.h>
36 #include <linux/mtd/map.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/cfi.h>
39 #include <linux/mtd/xip.h>
41 #define AMD_BOOTLOC_BUG
42 #define FORCE_WORD_WRITE 0
44 #define MAX_WORD_RETRIES 3
46 #define SST49LF004B 0x0060
47 #define SST49LF040B 0x0050
48 #define SST49LF008A 0x005a
49 #define AT49BV6416 0x00d6
51 static int cfi_amdstd_read (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
52 static int cfi_amdstd_write_words(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
53 static int cfi_amdstd_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
54 static int cfi_amdstd_erase_chip(struct mtd_info
*, struct erase_info
*);
55 static int cfi_amdstd_erase_varsize(struct mtd_info
*, struct erase_info
*);
56 static void cfi_amdstd_sync (struct mtd_info
*);
57 static int cfi_amdstd_suspend (struct mtd_info
*);
58 static void cfi_amdstd_resume (struct mtd_info
*);
59 static int cfi_amdstd_reboot(struct notifier_block
*, unsigned long, void *);
60 static int cfi_amdstd_secsi_read (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
62 static int cfi_amdstd_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
63 size_t *retlen
, const u_char
*buf
);
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
, uint64_t len
);
75 static int cfi_atmel_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_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
)
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 */
151 pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
152 map
->name
, cfi
->mfr
, cfi
->id
);
154 /* AFAICS all 29LV400 with a bottom boot block have a device ID
155 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
156 * These were badly detected as they have the 0x80 bit set
157 * so treat them as a special case.
159 if (((cfi
->id
== 0xBA) || (cfi
->id
== 0x22BA)) &&
161 /* Macronix added CFI to their 2nd generation
162 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
163 * Fujitsu, Spansion, EON, ESI and older Macronix)
166 * Therefore also check the manufacturer.
167 * This reduces the risk of false detection due to
168 * the 8-bit device ID.
170 (cfi
->mfr
== CFI_MFR_MACRONIX
)) {
171 pr_debug("%s: Macronix MX29LV400C with bottom boot block"
172 " detected\n", map
->name
);
173 extp
->TopBottom
= 2; /* bottom boot */
175 if (cfi
->id
& 0x80) {
176 printk(KERN_WARNING
"%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map
->name
, cfi
->id
);
177 extp
->TopBottom
= 3; /* top boot */
179 extp
->TopBottom
= 2; /* bottom boot */
182 pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
183 " deduced %s from Device ID\n", map
->name
, major
, minor
,
184 extp
->TopBottom
== 2 ? "bottom" : "top");
189 static void fixup_use_write_buffers(struct mtd_info
*mtd
)
191 struct map_info
*map
= mtd
->priv
;
192 struct cfi_private
*cfi
= map
->fldrv_priv
;
193 if (cfi
->cfiq
->BufWriteTimeoutTyp
) {
194 pr_debug("Using buffer write method\n" );
195 mtd
->_write
= cfi_amdstd_write_buffers
;
199 /* Atmel chips don't use the same PRI format as AMD chips */
200 static void fixup_convert_atmel_pri(struct mtd_info
*mtd
)
202 struct map_info
*map
= mtd
->priv
;
203 struct cfi_private
*cfi
= map
->fldrv_priv
;
204 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
205 struct cfi_pri_atmel atmel_pri
;
207 memcpy(&atmel_pri
, extp
, sizeof(atmel_pri
));
208 memset((char *)extp
+ 5, 0, sizeof(*extp
) - 5);
210 if (atmel_pri
.Features
& 0x02)
211 extp
->EraseSuspend
= 2;
213 /* Some chips got it backwards... */
214 if (cfi
->id
== AT49BV6416
) {
215 if (atmel_pri
.BottomBoot
)
220 if (atmel_pri
.BottomBoot
)
226 /* burst write mode not supported */
227 cfi
->cfiq
->BufWriteTimeoutTyp
= 0;
228 cfi
->cfiq
->BufWriteTimeoutMax
= 0;
231 static void fixup_use_secsi(struct mtd_info
*mtd
)
233 /* Setup for chips with a secsi area */
234 mtd
->_read_user_prot_reg
= cfi_amdstd_secsi_read
;
235 mtd
->_read_fact_prot_reg
= cfi_amdstd_secsi_read
;
238 static void fixup_use_erase_chip(struct mtd_info
*mtd
)
240 struct map_info
*map
= mtd
->priv
;
241 struct cfi_private
*cfi
= map
->fldrv_priv
;
242 if ((cfi
->cfiq
->NumEraseRegions
== 1) &&
243 ((cfi
->cfiq
->EraseRegionInfo
[0] & 0xffff) == 0)) {
244 mtd
->_erase
= cfi_amdstd_erase_chip
;
250 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
253 static void fixup_use_atmel_lock(struct mtd_info
*mtd
)
255 mtd
->_lock
= cfi_atmel_lock
;
256 mtd
->_unlock
= cfi_atmel_unlock
;
257 mtd
->flags
|= MTD_POWERUP_LOCK
;
260 static void fixup_old_sst_eraseregion(struct mtd_info
*mtd
)
262 struct map_info
*map
= mtd
->priv
;
263 struct cfi_private
*cfi
= map
->fldrv_priv
;
266 * These flashes report two separate eraseblock regions based on the
267 * sector_erase-size and block_erase-size, although they both operate on the
268 * same memory. This is not allowed according to CFI, so we just pick the
271 cfi
->cfiq
->NumEraseRegions
= 1;
274 static void fixup_sst39vf(struct mtd_info
*mtd
)
276 struct map_info
*map
= mtd
->priv
;
277 struct cfi_private
*cfi
= map
->fldrv_priv
;
279 fixup_old_sst_eraseregion(mtd
);
281 cfi
->addr_unlock1
= 0x5555;
282 cfi
->addr_unlock2
= 0x2AAA;
285 static void fixup_sst39vf_rev_b(struct mtd_info
*mtd
)
287 struct map_info
*map
= mtd
->priv
;
288 struct cfi_private
*cfi
= map
->fldrv_priv
;
290 fixup_old_sst_eraseregion(mtd
);
292 cfi
->addr_unlock1
= 0x555;
293 cfi
->addr_unlock2
= 0x2AA;
295 cfi
->sector_erase_cmd
= CMD(0x50);
298 static void fixup_sst38vf640x_sectorsize(struct mtd_info
*mtd
)
300 struct map_info
*map
= mtd
->priv
;
301 struct cfi_private
*cfi
= map
->fldrv_priv
;
303 fixup_sst39vf_rev_b(mtd
);
306 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
307 * it should report a size of 8KBytes (0x0020*256).
309 cfi
->cfiq
->EraseRegionInfo
[0] = 0x002003ff;
310 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd
->name
);
313 static void fixup_s29gl064n_sectors(struct mtd_info
*mtd
)
315 struct map_info
*map
= mtd
->priv
;
316 struct cfi_private
*cfi
= map
->fldrv_priv
;
318 if ((cfi
->cfiq
->EraseRegionInfo
[0] & 0xffff) == 0x003f) {
319 cfi
->cfiq
->EraseRegionInfo
[0] |= 0x0040;
320 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd
->name
);
324 static void fixup_s29gl032n_sectors(struct mtd_info
*mtd
)
326 struct map_info
*map
= mtd
->priv
;
327 struct cfi_private
*cfi
= map
->fldrv_priv
;
329 if ((cfi
->cfiq
->EraseRegionInfo
[1] & 0xffff) == 0x007e) {
330 cfi
->cfiq
->EraseRegionInfo
[1] &= ~0x0040;
331 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd
->name
);
335 /* Used to fix CFI-Tables of chips without Extended Query Tables */
336 static struct cfi_fixup cfi_nopri_fixup_table
[] = {
337 { CFI_MFR_SST
, 0x234a, fixup_sst39vf
}, /* SST39VF1602 */
338 { CFI_MFR_SST
, 0x234b, fixup_sst39vf
}, /* SST39VF1601 */
339 { CFI_MFR_SST
, 0x235a, fixup_sst39vf
}, /* SST39VF3202 */
340 { CFI_MFR_SST
, 0x235b, fixup_sst39vf
}, /* SST39VF3201 */
341 { CFI_MFR_SST
, 0x235c, fixup_sst39vf_rev_b
}, /* SST39VF3202B */
342 { CFI_MFR_SST
, 0x235d, fixup_sst39vf_rev_b
}, /* SST39VF3201B */
343 { CFI_MFR_SST
, 0x236c, fixup_sst39vf_rev_b
}, /* SST39VF6402B */
344 { CFI_MFR_SST
, 0x236d, fixup_sst39vf_rev_b
}, /* SST39VF6401B */
348 static struct cfi_fixup cfi_fixup_table
[] = {
349 { CFI_MFR_ATMEL
, CFI_ID_ANY
, fixup_convert_atmel_pri
},
350 #ifdef AMD_BOOTLOC_BUG
351 { CFI_MFR_AMD
, CFI_ID_ANY
, fixup_amd_bootblock
},
352 { CFI_MFR_AMIC
, CFI_ID_ANY
, fixup_amd_bootblock
},
353 { CFI_MFR_MACRONIX
, CFI_ID_ANY
, fixup_amd_bootblock
},
355 { CFI_MFR_AMD
, 0x0050, fixup_use_secsi
},
356 { CFI_MFR_AMD
, 0x0053, fixup_use_secsi
},
357 { CFI_MFR_AMD
, 0x0055, fixup_use_secsi
},
358 { CFI_MFR_AMD
, 0x0056, fixup_use_secsi
},
359 { CFI_MFR_AMD
, 0x005C, fixup_use_secsi
},
360 { CFI_MFR_AMD
, 0x005F, fixup_use_secsi
},
361 { CFI_MFR_AMD
, 0x0c01, fixup_s29gl064n_sectors
},
362 { CFI_MFR_AMD
, 0x1301, fixup_s29gl064n_sectors
},
363 { CFI_MFR_AMD
, 0x1a00, fixup_s29gl032n_sectors
},
364 { CFI_MFR_AMD
, 0x1a01, fixup_s29gl032n_sectors
},
365 { CFI_MFR_SST
, 0x536a, fixup_sst38vf640x_sectorsize
}, /* SST38VF6402 */
366 { CFI_MFR_SST
, 0x536b, fixup_sst38vf640x_sectorsize
}, /* SST38VF6401 */
367 { CFI_MFR_SST
, 0x536c, fixup_sst38vf640x_sectorsize
}, /* SST38VF6404 */
368 { CFI_MFR_SST
, 0x536d, fixup_sst38vf640x_sectorsize
}, /* SST38VF6403 */
369 #if !FORCE_WORD_WRITE
370 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_write_buffers
},
374 static struct cfi_fixup jedec_fixup_table
[] = {
375 { CFI_MFR_SST
, SST49LF004B
, fixup_use_fwh_lock
},
376 { CFI_MFR_SST
, SST49LF040B
, fixup_use_fwh_lock
},
377 { CFI_MFR_SST
, SST49LF008A
, fixup_use_fwh_lock
},
381 static struct cfi_fixup fixup_table
[] = {
382 /* The CFI vendor ids and the JEDEC vendor IDs appear
383 * to be common. It is like the devices id's are as
384 * well. This table is to pick all cases where
385 * we know that is the case.
387 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_erase_chip
},
388 { CFI_MFR_ATMEL
, AT49BV6416
, fixup_use_atmel_lock
},
393 static void cfi_fixup_major_minor(struct cfi_private
*cfi
,
394 struct cfi_pri_amdstd
*extp
)
396 if (cfi
->mfr
== CFI_MFR_SAMSUNG
) {
397 if ((extp
->MajorVersion
== '0' && extp
->MinorVersion
== '0') ||
398 (extp
->MajorVersion
== '3' && extp
->MinorVersion
== '3')) {
400 * Samsung K8P2815UQB and K8D6x16UxM chips
401 * report major=0 / minor=0.
402 * K8D3x16UxC chips report major=3 / minor=3.
404 printk(KERN_NOTICE
" Fixing Samsung's Amd/Fujitsu"
405 " Extended Query version to 1.%c\n",
407 extp
->MajorVersion
= '1';
412 * SST 38VF640x chips report major=0xFF / minor=0xFF.
414 if (cfi
->mfr
== CFI_MFR_SST
&& (cfi
->id
>> 4) == 0x0536) {
415 extp
->MajorVersion
= '1';
416 extp
->MinorVersion
= '0';
420 struct mtd_info
*cfi_cmdset_0002(struct map_info
*map
, int primary
)
422 struct cfi_private
*cfi
= map
->fldrv_priv
;
423 struct mtd_info
*mtd
;
426 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
428 printk(KERN_WARNING
"Failed to allocate memory for MTD device\n");
432 mtd
->type
= MTD_NORFLASH
;
434 /* Fill in the default mtd operations */
435 mtd
->_erase
= cfi_amdstd_erase_varsize
;
436 mtd
->_write
= cfi_amdstd_write_words
;
437 mtd
->_read
= cfi_amdstd_read
;
438 mtd
->_sync
= cfi_amdstd_sync
;
439 mtd
->_suspend
= cfi_amdstd_suspend
;
440 mtd
->_resume
= cfi_amdstd_resume
;
441 mtd
->flags
= MTD_CAP_NORFLASH
;
442 mtd
->name
= map
->name
;
444 mtd
->writebufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
446 pr_debug("MTD %s(): write buffer size %d\n", __func__
,
449 mtd
->_panic_write
= cfi_amdstd_panic_write
;
450 mtd
->reboot_notifier
.notifier_call
= cfi_amdstd_reboot
;
452 if (cfi
->cfi_mode
==CFI_MODE_CFI
){
453 unsigned char bootloc
;
454 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
455 struct cfi_pri_amdstd
*extp
;
457 extp
= (struct cfi_pri_amdstd
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "Amd/Fujitsu");
460 * It's a real CFI chip, not one for which the probe
461 * routine faked a CFI structure.
463 cfi_fixup_major_minor(cfi
, extp
);
466 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
467 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
468 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
469 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
470 * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
472 if (extp
->MajorVersion
!= '1' ||
473 (extp
->MajorVersion
== '1' && (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '5'))) {
474 printk(KERN_ERR
" Unknown Amd/Fujitsu Extended Query "
475 "version %c.%c (%#02x/%#02x).\n",
476 extp
->MajorVersion
, extp
->MinorVersion
,
477 extp
->MajorVersion
, extp
->MinorVersion
);
483 printk(KERN_INFO
" Amd/Fujitsu Extended Query version %c.%c.\n",
484 extp
->MajorVersion
, extp
->MinorVersion
);
486 /* Install our own private info structure */
487 cfi
->cmdset_priv
= extp
;
489 /* Apply cfi device specific fixups */
490 cfi_fixup(mtd
, cfi_fixup_table
);
492 #ifdef DEBUG_CFI_FEATURES
493 /* Tell the user about it in lots of lovely detail */
494 cfi_tell_features(extp
);
497 bootloc
= extp
->TopBottom
;
498 if ((bootloc
< 2) || (bootloc
> 5)) {
499 printk(KERN_WARNING
"%s: CFI contains unrecognised boot "
500 "bank location (%d). Assuming bottom.\n",
505 if (bootloc
== 3 && cfi
->cfiq
->NumEraseRegions
> 1) {
506 printk(KERN_WARNING
"%s: Swapping erase regions for top-boot CFI table.\n", map
->name
);
508 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
/ 2; i
++) {
509 int j
= (cfi
->cfiq
->NumEraseRegions
-1)-i
;
512 swap
= cfi
->cfiq
->EraseRegionInfo
[i
];
513 cfi
->cfiq
->EraseRegionInfo
[i
] = cfi
->cfiq
->EraseRegionInfo
[j
];
514 cfi
->cfiq
->EraseRegionInfo
[j
] = swap
;
517 /* Set the default CFI lock/unlock addresses */
518 cfi
->addr_unlock1
= 0x555;
519 cfi
->addr_unlock2
= 0x2aa;
521 cfi_fixup(mtd
, cfi_nopri_fixup_table
);
523 if (!cfi
->addr_unlock1
|| !cfi
->addr_unlock2
) {
529 else if (cfi
->cfi_mode
== CFI_MODE_JEDEC
) {
530 /* Apply jedec specific fixups */
531 cfi_fixup(mtd
, jedec_fixup_table
);
533 /* Apply generic fixups */
534 cfi_fixup(mtd
, fixup_table
);
536 for (i
=0; i
< cfi
->numchips
; i
++) {
537 cfi
->chips
[i
].word_write_time
= 1<<cfi
->cfiq
->WordWriteTimeoutTyp
;
538 cfi
->chips
[i
].buffer_write_time
= 1<<cfi
->cfiq
->BufWriteTimeoutTyp
;
539 cfi
->chips
[i
].erase_time
= 1<<cfi
->cfiq
->BlockEraseTimeoutTyp
;
540 cfi
->chips
[i
].ref_point_counter
= 0;
541 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
544 map
->fldrv
= &cfi_amdstd_chipdrv
;
546 return cfi_amdstd_setup(mtd
);
548 struct mtd_info
*cfi_cmdset_0006(struct map_info
*map
, int primary
) __attribute__((alias("cfi_cmdset_0002")));
549 struct mtd_info
*cfi_cmdset_0701(struct map_info
*map
, int primary
) __attribute__((alias("cfi_cmdset_0002")));
550 EXPORT_SYMBOL_GPL(cfi_cmdset_0002
);
551 EXPORT_SYMBOL_GPL(cfi_cmdset_0006
);
552 EXPORT_SYMBOL_GPL(cfi_cmdset_0701
);
554 static struct mtd_info
*cfi_amdstd_setup(struct mtd_info
*mtd
)
556 struct map_info
*map
= mtd
->priv
;
557 struct cfi_private
*cfi
= map
->fldrv_priv
;
558 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
559 unsigned long offset
= 0;
562 printk(KERN_NOTICE
"number of %s chips: %d\n",
563 (cfi
->cfi_mode
== CFI_MODE_CFI
)?"CFI":"JEDEC",cfi
->numchips
);
564 /* Select the correct geometry setup */
565 mtd
->size
= devsize
* cfi
->numchips
;
567 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
568 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
569 * mtd
->numeraseregions
, GFP_KERNEL
);
570 if (!mtd
->eraseregions
) {
571 printk(KERN_WARNING
"Failed to allocate memory for MTD erase region info\n");
575 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
576 unsigned long ernum
, ersize
;
577 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
578 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
580 if (mtd
->erasesize
< ersize
) {
581 mtd
->erasesize
= ersize
;
583 for (j
=0; j
<cfi
->numchips
; j
++) {
584 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
585 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
586 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
588 offset
+= (ersize
* ernum
);
590 if (offset
!= devsize
) {
592 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
596 __module_get(THIS_MODULE
);
597 register_reboot_notifier(&mtd
->reboot_notifier
);
601 kfree(mtd
->eraseregions
);
603 kfree(cfi
->cmdset_priv
);
609 * Return true if the chip is ready.
611 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
612 * non-suspended sector) and is indicated by no toggle bits toggling.
614 * Note that anything more complicated than checking if no bits are toggling
615 * (including checking DQ5 for an error status) is tricky to get working
616 * correctly and is therefore not done (particularly with interleaved chips
617 * as each chip must be checked independently of the others).
619 static int __xipram
chip_ready(struct map_info
*map
, unsigned long addr
)
623 d
= map_read(map
, addr
);
624 t
= map_read(map
, addr
);
626 return map_word_equal(map
, d
, t
);
630 * Return true if the chip is ready and has the correct value.
632 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
633 * non-suspended sector) and it is indicated by no bits toggling.
635 * Error are indicated by toggling bits or bits held with the wrong value,
636 * or with bits toggling.
638 * Note that anything more complicated than checking if no bits are toggling
639 * (including checking DQ5 for an error status) is tricky to get working
640 * correctly and is therefore not done (particularly with interleaved chips
641 * as each chip must be checked independently of the others).
644 static int __xipram
chip_good(struct map_info
*map
, unsigned long addr
, map_word expected
)
648 oldd
= map_read(map
, addr
);
649 curd
= map_read(map
, addr
);
651 return map_word_equal(map
, oldd
, curd
) &&
652 map_word_equal(map
, curd
, expected
);
655 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
)
657 DECLARE_WAITQUEUE(wait
, current
);
658 struct cfi_private
*cfi
= map
->fldrv_priv
;
660 struct cfi_pri_amdstd
*cfip
= (struct cfi_pri_amdstd
*)cfi
->cmdset_priv
;
663 timeo
= jiffies
+ HZ
;
665 switch (chip
->state
) {
669 if (chip_ready(map
, adr
))
672 if (time_after(jiffies
, timeo
)) {
673 printk(KERN_ERR
"Waiting for chip to be ready timed out.\n");
676 mutex_unlock(&chip
->mutex
);
678 mutex_lock(&chip
->mutex
);
679 /* Someone else might have been playing with it. */
689 if (!cfip
|| !(cfip
->EraseSuspend
& (0x1|0x2)) ||
690 !(mode
== FL_READY
|| mode
== FL_POINT
||
691 (mode
== FL_WRITING
&& (cfip
->EraseSuspend
& 0x2))))
694 /* We could check to see if we're trying to access the sector
695 * that is currently being erased. However, no user will try
696 * anything like that so we just wait for the timeout. */
699 /* It's harmless to issue the Erase-Suspend and Erase-Resume
700 * commands when the erase algorithm isn't in progress. */
701 map_write(map
, CMD(0xB0), chip
->in_progress_block_addr
);
702 chip
->oldstate
= FL_ERASING
;
703 chip
->state
= FL_ERASE_SUSPENDING
;
704 chip
->erase_suspended
= 1;
706 if (chip_ready(map
, adr
))
709 if (time_after(jiffies
, timeo
)) {
710 /* Should have suspended the erase by now.
711 * Send an Erase-Resume command as either
712 * there was an error (so leave the erase
713 * routine to recover from it) or we trying to
714 * use the erase-in-progress sector. */
715 put_chip(map
, chip
, adr
);
716 printk(KERN_ERR
"MTD %s(): chip not ready after erase suspend\n", __func__
);
720 mutex_unlock(&chip
->mutex
);
722 mutex_lock(&chip
->mutex
);
723 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
724 So we can just loop here. */
726 chip
->state
= FL_READY
;
729 case FL_XIP_WHILE_ERASING
:
730 if (mode
!= FL_READY
&& mode
!= FL_POINT
&&
731 (!cfip
|| !(cfip
->EraseSuspend
&2)))
733 chip
->oldstate
= chip
->state
;
734 chip
->state
= FL_READY
;
738 /* The machine is rebooting */
742 /* Only if there's no operation suspended... */
743 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
748 set_current_state(TASK_UNINTERRUPTIBLE
);
749 add_wait_queue(&chip
->wq
, &wait
);
750 mutex_unlock(&chip
->mutex
);
752 remove_wait_queue(&chip
->wq
, &wait
);
753 mutex_lock(&chip
->mutex
);
759 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
761 struct cfi_private
*cfi
= map
->fldrv_priv
;
763 switch(chip
->oldstate
) {
765 map_write(map
, cfi
->sector_erase_cmd
, chip
->in_progress_block_addr
);
766 chip
->oldstate
= FL_READY
;
767 chip
->state
= FL_ERASING
;
770 case FL_XIP_WHILE_ERASING
:
771 chip
->state
= chip
->oldstate
;
772 chip
->oldstate
= FL_READY
;
779 printk(KERN_ERR
"MTD: put_chip() called with oldstate %d!!\n", chip
->oldstate
);
784 #ifdef CONFIG_MTD_XIP
787 * No interrupt what so ever can be serviced while the flash isn't in array
788 * mode. This is ensured by the xip_disable() and xip_enable() functions
789 * enclosing any code path where the flash is known not to be in array mode.
790 * And within a XIP disabled code path, only functions marked with __xipram
791 * may be called and nothing else (it's a good thing to inspect generated
792 * assembly to make sure inline functions were actually inlined and that gcc
793 * didn't emit calls to its own support functions). Also configuring MTD CFI
794 * support to a single buswidth and a single interleave is also recommended.
797 static void xip_disable(struct map_info
*map
, struct flchip
*chip
,
800 /* TODO: chips with no XIP use should ignore and return */
801 (void) map_read(map
, adr
); /* ensure mmu mapping is up to date */
805 static void __xipram
xip_enable(struct map_info
*map
, struct flchip
*chip
,
808 struct cfi_private
*cfi
= map
->fldrv_priv
;
810 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
811 map_write(map
, CMD(0xf0), adr
);
812 chip
->state
= FL_READY
;
814 (void) map_read(map
, adr
);
820 * When a delay is required for the flash operation to complete, the
821 * xip_udelay() function is polling for both the given timeout and pending
822 * (but still masked) hardware interrupts. Whenever there is an interrupt
823 * pending then the flash erase operation is suspended, array mode restored
824 * and interrupts unmasked. Task scheduling might also happen at that
825 * point. The CPU eventually returns from the interrupt or the call to
826 * schedule() and the suspended flash operation is resumed for the remaining
827 * of the delay period.
829 * Warning: this function _will_ fool interrupt latency tracing tools.
832 static void __xipram
xip_udelay(struct map_info
*map
, struct flchip
*chip
,
833 unsigned long adr
, int usec
)
835 struct cfi_private
*cfi
= map
->fldrv_priv
;
836 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
837 map_word status
, OK
= CMD(0x80);
838 unsigned long suspended
, start
= xip_currtime();
843 if (xip_irqpending() && extp
&&
844 ((chip
->state
== FL_ERASING
&& (extp
->EraseSuspend
& 2))) &&
845 (cfi_interleave_is_1(cfi
) || chip
->oldstate
== FL_READY
)) {
847 * Let's suspend the erase operation when supported.
848 * Note that we currently don't try to suspend
849 * interleaved chips if there is already another
850 * operation suspended (imagine what happens
851 * when one chip was already done with the current
852 * operation while another chip suspended it, then
853 * we resume the whole thing at once). Yes, it
856 map_write(map
, CMD(0xb0), adr
);
857 usec
-= xip_elapsed_since(start
);
858 suspended
= xip_currtime();
860 if (xip_elapsed_since(suspended
) > 100000) {
862 * The chip doesn't want to suspend
863 * after waiting for 100 msecs.
864 * This is a critical error but there
865 * is not much we can do here.
869 status
= map_read(map
, adr
);
870 } while (!map_word_andequal(map
, status
, OK
, OK
));
872 /* Suspend succeeded */
873 oldstate
= chip
->state
;
874 if (!map_word_bitsset(map
, status
, CMD(0x40)))
876 chip
->state
= FL_XIP_WHILE_ERASING
;
877 chip
->erase_suspended
= 1;
878 map_write(map
, CMD(0xf0), adr
);
879 (void) map_read(map
, adr
);
882 mutex_unlock(&chip
->mutex
);
887 * We're back. However someone else might have
888 * decided to go write to the chip if we are in
889 * a suspended erase state. If so let's wait
892 mutex_lock(&chip
->mutex
);
893 while (chip
->state
!= FL_XIP_WHILE_ERASING
) {
894 DECLARE_WAITQUEUE(wait
, current
);
895 set_current_state(TASK_UNINTERRUPTIBLE
);
896 add_wait_queue(&chip
->wq
, &wait
);
897 mutex_unlock(&chip
->mutex
);
899 remove_wait_queue(&chip
->wq
, &wait
);
900 mutex_lock(&chip
->mutex
);
902 /* Disallow XIP again */
905 /* Resume the write or erase operation */
906 map_write(map
, cfi
->sector_erase_cmd
, adr
);
907 chip
->state
= oldstate
;
908 start
= xip_currtime();
909 } else if (usec
>= 1000000/HZ
) {
911 * Try to save on CPU power when waiting delay
912 * is at least a system timer tick period.
913 * No need to be extremely accurate here.
917 status
= map_read(map
, adr
);
918 } while (!map_word_andequal(map
, status
, OK
, OK
)
919 && xip_elapsed_since(start
) < usec
);
922 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
925 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
926 * the flash is actively programming or erasing since we have to poll for
927 * the operation to complete anyway. We can't do that in a generic way with
928 * a XIP setup so do it before the actual flash operation in this case
929 * and stub it out from INVALIDATE_CACHE_UDELAY.
931 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
932 INVALIDATE_CACHED_RANGE(map, from, size)
934 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
935 UDELAY(map, chip, adr, usec)
940 * Activating this XIP support changes the way the code works a bit. For
941 * example the code to suspend the current process when concurrent access
942 * happens is never executed because xip_udelay() will always return with the
943 * same chip state as it was entered with. This is why there is no care for
944 * the presence of add_wait_queue() or schedule() calls from within a couple
945 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
946 * The queueing and scheduling are always happening within xip_udelay().
948 * Similarly, get_chip() and put_chip() just happen to always be executed
949 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
950 * is in array mode, therefore never executing many cases therein and not
951 * causing any problem with XIP.
956 #define xip_disable(map, chip, adr)
957 #define xip_enable(map, chip, adr)
958 #define XIP_INVAL_CACHED_RANGE(x...)
960 #define UDELAY(map, chip, adr, usec) \
962 mutex_unlock(&chip->mutex); \
964 mutex_lock(&chip->mutex); \
967 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
969 mutex_unlock(&chip->mutex); \
970 INVALIDATE_CACHED_RANGE(map, adr, len); \
972 mutex_lock(&chip->mutex); \
977 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
979 unsigned long cmd_addr
;
980 struct cfi_private
*cfi
= map
->fldrv_priv
;
985 /* Ensure cmd read/writes are aligned. */
986 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
988 mutex_lock(&chip
->mutex
);
989 ret
= get_chip(map
, chip
, cmd_addr
, FL_READY
);
991 mutex_unlock(&chip
->mutex
);
995 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
996 map_write(map
, CMD(0xf0), cmd_addr
);
997 chip
->state
= FL_READY
;
1000 map_copy_from(map
, buf
, adr
, len
);
1002 put_chip(map
, chip
, cmd_addr
);
1004 mutex_unlock(&chip
->mutex
);
1009 static int cfi_amdstd_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
1011 struct map_info
*map
= mtd
->priv
;
1012 struct cfi_private
*cfi
= map
->fldrv_priv
;
1017 /* ofs: offset within the first chip that the first read should start */
1018 chipnum
= (from
>> cfi
->chipshift
);
1019 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1022 unsigned long thislen
;
1024 if (chipnum
>= cfi
->numchips
)
1027 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1028 thislen
= (1<<cfi
->chipshift
) - ofs
;
1032 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
1047 static inline int do_read_secsi_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
1049 DECLARE_WAITQUEUE(wait
, current
);
1050 unsigned long timeo
= jiffies
+ HZ
;
1051 struct cfi_private
*cfi
= map
->fldrv_priv
;
1054 mutex_lock(&chip
->mutex
);
1056 if (chip
->state
!= FL_READY
){
1057 set_current_state(TASK_UNINTERRUPTIBLE
);
1058 add_wait_queue(&chip
->wq
, &wait
);
1060 mutex_unlock(&chip
->mutex
);
1063 remove_wait_queue(&chip
->wq
, &wait
);
1064 timeo
= jiffies
+ HZ
;
1071 chip
->state
= FL_READY
;
1073 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1074 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1075 cfi_send_gen_cmd(0x88, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1077 map_copy_from(map
, buf
, adr
, len
);
1079 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1080 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1081 cfi_send_gen_cmd(0x90, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1082 cfi_send_gen_cmd(0x00, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1085 mutex_unlock(&chip
->mutex
);
1090 static int cfi_amdstd_secsi_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
1092 struct map_info
*map
= mtd
->priv
;
1093 struct cfi_private
*cfi
= map
->fldrv_priv
;
1098 /* ofs: offset within the first chip that the first read should start */
1099 /* 8 secsi bytes per chip */
1104 unsigned long thislen
;
1106 if (chipnum
>= cfi
->numchips
)
1109 if ((len
+ ofs
-1) >> 3)
1110 thislen
= (1<<3) - ofs
;
1114 ret
= do_read_secsi_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
1129 static int __xipram
do_write_oneword(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, map_word datum
)
1131 struct cfi_private
*cfi
= map
->fldrv_priv
;
1132 unsigned long timeo
= jiffies
+ HZ
;
1134 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1135 * have a max write time of a few hundreds usec). However, we should
1136 * use the maximum timeout value given by the chip at probe time
1137 * instead. Unfortunately, struct flchip does have a field for
1138 * maximum timeout, only for typical which can be far too short
1139 * depending of the conditions. The ' + 1' is to avoid having a
1140 * timeout of 0 jiffies if HZ is smaller than 1000.
1142 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1149 mutex_lock(&chip
->mutex
);
1150 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1152 mutex_unlock(&chip
->mutex
);
1156 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1157 __func__
, adr
, datum
.x
[0] );
1160 * Check for a NOP for the case when the datum to write is already
1161 * present - it saves time and works around buggy chips that corrupt
1162 * data at other locations when 0xff is written to a location that
1163 * already contains 0xff.
1165 oldd
= map_read(map
, adr
);
1166 if (map_word_equal(map
, oldd
, datum
)) {
1167 pr_debug("MTD %s(): NOP\n",
1172 XIP_INVAL_CACHED_RANGE(map
, adr
, map_bankwidth(map
));
1174 xip_disable(map
, chip
, adr
);
1176 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1177 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1178 cfi_send_gen_cmd(0xA0, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1179 map_write(map
, datum
, adr
);
1180 chip
->state
= FL_WRITING
;
1182 INVALIDATE_CACHE_UDELAY(map
, chip
,
1183 adr
, map_bankwidth(map
),
1184 chip
->word_write_time
);
1186 /* See comment above for timeout value. */
1187 timeo
= jiffies
+ uWriteTimeout
;
1189 if (chip
->state
!= FL_WRITING
) {
1190 /* Someone's suspended the write. Sleep */
1191 DECLARE_WAITQUEUE(wait
, current
);
1193 set_current_state(TASK_UNINTERRUPTIBLE
);
1194 add_wait_queue(&chip
->wq
, &wait
);
1195 mutex_unlock(&chip
->mutex
);
1197 remove_wait_queue(&chip
->wq
, &wait
);
1198 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1199 mutex_lock(&chip
->mutex
);
1203 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
)){
1204 xip_enable(map
, chip
, adr
);
1205 printk(KERN_WARNING
"MTD %s(): software timeout\n", __func__
);
1206 xip_disable(map
, chip
, adr
);
1210 if (chip_ready(map
, adr
))
1213 /* Latency issues. Drop the lock, wait a while and retry */
1214 UDELAY(map
, chip
, adr
, 1);
1216 /* Did we succeed? */
1217 if (!chip_good(map
, adr
, datum
)) {
1218 /* reset on all failures. */
1219 map_write( map
, CMD(0xF0), chip
->start
);
1220 /* FIXME - should have reset delay before continuing */
1222 if (++retry_cnt
<= MAX_WORD_RETRIES
)
1227 xip_enable(map
, chip
, adr
);
1229 chip
->state
= FL_READY
;
1231 put_chip(map
, chip
, adr
);
1232 mutex_unlock(&chip
->mutex
);
1238 static int cfi_amdstd_write_words(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1239 size_t *retlen
, const u_char
*buf
)
1241 struct map_info
*map
= mtd
->priv
;
1242 struct cfi_private
*cfi
= map
->fldrv_priv
;
1245 unsigned long ofs
, chipstart
;
1246 DECLARE_WAITQUEUE(wait
, current
);
1248 chipnum
= to
>> cfi
->chipshift
;
1249 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1250 chipstart
= cfi
->chips
[chipnum
].start
;
1252 /* If it's not bus-aligned, do the first byte write */
1253 if (ofs
& (map_bankwidth(map
)-1)) {
1254 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
)-1);
1255 int i
= ofs
- bus_ofs
;
1260 mutex_lock(&cfi
->chips
[chipnum
].mutex
);
1262 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1263 set_current_state(TASK_UNINTERRUPTIBLE
);
1264 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1266 mutex_unlock(&cfi
->chips
[chipnum
].mutex
);
1269 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1273 /* Load 'tmp_buf' with old contents of flash */
1274 tmp_buf
= map_read(map
, bus_ofs
+chipstart
);
1276 mutex_unlock(&cfi
->chips
[chipnum
].mutex
);
1278 /* Number of bytes to copy from buffer */
1279 n
= min_t(int, len
, map_bankwidth(map
)-i
);
1281 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, i
, n
);
1283 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1293 if (ofs
>> cfi
->chipshift
) {
1296 if (chipnum
== cfi
->numchips
)
1301 /* We are now aligned, write as much as possible */
1302 while(len
>= map_bankwidth(map
)) {
1305 datum
= map_word_load(map
, buf
);
1307 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1312 ofs
+= map_bankwidth(map
);
1313 buf
+= map_bankwidth(map
);
1314 (*retlen
) += map_bankwidth(map
);
1315 len
-= map_bankwidth(map
);
1317 if (ofs
>> cfi
->chipshift
) {
1320 if (chipnum
== cfi
->numchips
)
1322 chipstart
= cfi
->chips
[chipnum
].start
;
1326 /* Write the trailing bytes if any */
1327 if (len
& (map_bankwidth(map
)-1)) {
1331 mutex_lock(&cfi
->chips
[chipnum
].mutex
);
1333 if (cfi
->chips
[chipnum
].state
!= FL_READY
) {
1334 set_current_state(TASK_UNINTERRUPTIBLE
);
1335 add_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1337 mutex_unlock(&cfi
->chips
[chipnum
].mutex
);
1340 remove_wait_queue(&cfi
->chips
[chipnum
].wq
, &wait
);
1344 tmp_buf
= map_read(map
, ofs
+ chipstart
);
1346 mutex_unlock(&cfi
->chips
[chipnum
].mutex
);
1348 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, 0, len
);
1350 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1363 * FIXME: interleaved mode not tested, and probably not supported!
1365 static int __xipram
do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
1366 unsigned long adr
, const u_char
*buf
,
1369 struct cfi_private
*cfi
= map
->fldrv_priv
;
1370 unsigned long timeo
= jiffies
+ HZ
;
1371 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1372 unsigned long uWriteTimeout
= ( HZ
/ 1000 ) + 1;
1374 unsigned long cmd_adr
;
1381 mutex_lock(&chip
->mutex
);
1382 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1384 mutex_unlock(&chip
->mutex
);
1388 datum
= map_word_load(map
, buf
);
1390 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1391 __func__
, adr
, datum
.x
[0] );
1393 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1395 xip_disable(map
, chip
, cmd_adr
);
1397 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1398 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1400 /* Write Buffer Load */
1401 map_write(map
, CMD(0x25), cmd_adr
);
1403 chip
->state
= FL_WRITING_TO_BUFFER
;
1405 /* Write length of data to come */
1406 words
= len
/ map_bankwidth(map
);
1407 map_write(map
, CMD(words
- 1), cmd_adr
);
1410 while(z
< words
* map_bankwidth(map
)) {
1411 datum
= map_word_load(map
, buf
);
1412 map_write(map
, datum
, adr
+ z
);
1414 z
+= map_bankwidth(map
);
1415 buf
+= map_bankwidth(map
);
1417 z
-= map_bankwidth(map
);
1421 /* Write Buffer Program Confirm: GO GO GO */
1422 map_write(map
, CMD(0x29), cmd_adr
);
1423 chip
->state
= FL_WRITING
;
1425 INVALIDATE_CACHE_UDELAY(map
, chip
,
1426 adr
, map_bankwidth(map
),
1427 chip
->word_write_time
);
1429 timeo
= jiffies
+ uWriteTimeout
;
1432 if (chip
->state
!= FL_WRITING
) {
1433 /* Someone's suspended the write. Sleep */
1434 DECLARE_WAITQUEUE(wait
, current
);
1436 set_current_state(TASK_UNINTERRUPTIBLE
);
1437 add_wait_queue(&chip
->wq
, &wait
);
1438 mutex_unlock(&chip
->mutex
);
1440 remove_wait_queue(&chip
->wq
, &wait
);
1441 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1442 mutex_lock(&chip
->mutex
);
1446 if (time_after(jiffies
, timeo
) && !chip_ready(map
, adr
))
1449 if (chip_ready(map
, adr
)) {
1450 xip_enable(map
, chip
, adr
);
1454 /* Latency issues. Drop the lock, wait a while and retry */
1455 UDELAY(map
, chip
, adr
, 1);
1458 /* reset on all failures. */
1459 map_write( map
, CMD(0xF0), chip
->start
);
1460 xip_enable(map
, chip
, adr
);
1461 /* FIXME - should have reset delay before continuing */
1463 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1468 chip
->state
= FL_READY
;
1470 put_chip(map
, chip
, adr
);
1471 mutex_unlock(&chip
->mutex
);
1477 static int cfi_amdstd_write_buffers(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1478 size_t *retlen
, const u_char
*buf
)
1480 struct map_info
*map
= mtd
->priv
;
1481 struct cfi_private
*cfi
= map
->fldrv_priv
;
1482 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1487 chipnum
= to
>> cfi
->chipshift
;
1488 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1490 /* If it's not bus-aligned, do the first word write */
1491 if (ofs
& (map_bankwidth(map
)-1)) {
1492 size_t local_len
= (-ofs
)&(map_bankwidth(map
)-1);
1493 if (local_len
> len
)
1495 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1496 local_len
, retlen
, buf
);
1503 if (ofs
>> cfi
->chipshift
) {
1506 if (chipnum
== cfi
->numchips
)
1511 /* Write buffer is worth it only if more than one word to write... */
1512 while (len
>= map_bankwidth(map
) * 2) {
1513 /* We must not cross write block boundaries */
1514 int size
= wbufsize
- (ofs
& (wbufsize
-1));
1518 if (size
% map_bankwidth(map
))
1519 size
-= size
% map_bankwidth(map
);
1521 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
1531 if (ofs
>> cfi
->chipshift
) {
1534 if (chipnum
== cfi
->numchips
)
1540 size_t retlen_dregs
= 0;
1542 ret
= cfi_amdstd_write_words(mtd
, ofs
+ (chipnum
<<cfi
->chipshift
),
1543 len
, &retlen_dregs
, buf
);
1545 *retlen
+= retlen_dregs
;
1553 * Wait for the flash chip to become ready to write data
1555 * This is only called during the panic_write() path. When panic_write()
1556 * is called, the kernel is in the process of a panic, and will soon be
1557 * dead. Therefore we don't take any locks, and attempt to get access
1558 * to the chip as soon as possible.
1560 static int cfi_amdstd_panic_wait(struct map_info
*map
, struct flchip
*chip
,
1563 struct cfi_private
*cfi
= map
->fldrv_priv
;
1568 * If the driver thinks the chip is idle, and no toggle bits
1569 * are changing, then the chip is actually idle for sure.
1571 if (chip
->state
== FL_READY
&& chip_ready(map
, adr
))
1575 * Try several times to reset the chip and then wait for it
1576 * to become idle. The upper limit of a few milliseconds of
1577 * delay isn't a big problem: the kernel is dying anyway. It
1578 * is more important to save the messages.
1580 while (retries
> 0) {
1581 const unsigned long timeo
= (HZ
/ 1000) + 1;
1583 /* send the reset command */
1584 map_write(map
, CMD(0xF0), chip
->start
);
1586 /* wait for the chip to become ready */
1587 for (i
= 0; i
< jiffies_to_usecs(timeo
); i
++) {
1588 if (chip_ready(map
, adr
))
1595 /* the chip never became ready */
1600 * Write out one word of data to a single flash chip during a kernel panic
1602 * This is only called during the panic_write() path. When panic_write()
1603 * is called, the kernel is in the process of a panic, and will soon be
1604 * dead. Therefore we don't take any locks, and attempt to get access
1605 * to the chip as soon as possible.
1607 * The implementation of this routine is intentionally similar to
1608 * do_write_oneword(), in order to ease code maintenance.
1610 static int do_panic_write_oneword(struct map_info
*map
, struct flchip
*chip
,
1611 unsigned long adr
, map_word datum
)
1613 const unsigned long uWriteTimeout
= (HZ
/ 1000) + 1;
1614 struct cfi_private
*cfi
= map
->fldrv_priv
;
1622 ret
= cfi_amdstd_panic_wait(map
, chip
, adr
);
1626 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
1627 __func__
, adr
, datum
.x
[0]);
1630 * Check for a NOP for the case when the datum to write is already
1631 * present - it saves time and works around buggy chips that corrupt
1632 * data at other locations when 0xff is written to a location that
1633 * already contains 0xff.
1635 oldd
= map_read(map
, adr
);
1636 if (map_word_equal(map
, oldd
, datum
)) {
1637 pr_debug("MTD %s(): NOP\n", __func__
);
1644 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1645 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1646 cfi_send_gen_cmd(0xA0, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1647 map_write(map
, datum
, adr
);
1649 for (i
= 0; i
< jiffies_to_usecs(uWriteTimeout
); i
++) {
1650 if (chip_ready(map
, adr
))
1656 if (!chip_good(map
, adr
, datum
)) {
1657 /* reset on all failures. */
1658 map_write(map
, CMD(0xF0), chip
->start
);
1659 /* FIXME - should have reset delay before continuing */
1661 if (++retry_cnt
<= MAX_WORD_RETRIES
)
1673 * Write out some data during a kernel panic
1675 * This is used by the mtdoops driver to save the dying messages from a
1676 * kernel which has panic'd.
1678 * This routine ignores all of the locking used throughout the rest of the
1679 * driver, in order to ensure that the data gets written out no matter what
1680 * state this driver (and the flash chip itself) was in when the kernel crashed.
1682 * The implementation of this routine is intentionally similar to
1683 * cfi_amdstd_write_words(), in order to ease code maintenance.
1685 static int cfi_amdstd_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1686 size_t *retlen
, const u_char
*buf
)
1688 struct map_info
*map
= mtd
->priv
;
1689 struct cfi_private
*cfi
= map
->fldrv_priv
;
1690 unsigned long ofs
, chipstart
;
1694 chipnum
= to
>> cfi
->chipshift
;
1695 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1696 chipstart
= cfi
->chips
[chipnum
].start
;
1698 /* If it's not bus aligned, do the first byte write */
1699 if (ofs
& (map_bankwidth(map
) - 1)) {
1700 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
) - 1);
1701 int i
= ofs
- bus_ofs
;
1705 ret
= cfi_amdstd_panic_wait(map
, &cfi
->chips
[chipnum
], bus_ofs
);
1709 /* Load 'tmp_buf' with old contents of flash */
1710 tmp_buf
= map_read(map
, bus_ofs
+ chipstart
);
1712 /* Number of bytes to copy from buffer */
1713 n
= min_t(int, len
, map_bankwidth(map
) - i
);
1715 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, i
, n
);
1717 ret
= do_panic_write_oneword(map
, &cfi
->chips
[chipnum
],
1727 if (ofs
>> cfi
->chipshift
) {
1730 if (chipnum
== cfi
->numchips
)
1735 /* We are now aligned, write as much as possible */
1736 while (len
>= map_bankwidth(map
)) {
1739 datum
= map_word_load(map
, buf
);
1741 ret
= do_panic_write_oneword(map
, &cfi
->chips
[chipnum
],
1746 ofs
+= map_bankwidth(map
);
1747 buf
+= map_bankwidth(map
);
1748 (*retlen
) += map_bankwidth(map
);
1749 len
-= map_bankwidth(map
);
1751 if (ofs
>> cfi
->chipshift
) {
1754 if (chipnum
== cfi
->numchips
)
1757 chipstart
= cfi
->chips
[chipnum
].start
;
1761 /* Write the trailing bytes if any */
1762 if (len
& (map_bankwidth(map
) - 1)) {
1765 ret
= cfi_amdstd_panic_wait(map
, &cfi
->chips
[chipnum
], ofs
);
1769 tmp_buf
= map_read(map
, ofs
+ chipstart
);
1771 tmp_buf
= map_word_load_partial(map
, tmp_buf
, buf
, 0, len
);
1773 ret
= do_panic_write_oneword(map
, &cfi
->chips
[chipnum
],
1786 * Handle devices with one erase region, that only implement
1787 * the chip erase command.
1789 static int __xipram
do_erase_chip(struct map_info
*map
, struct flchip
*chip
)
1791 struct cfi_private
*cfi
= map
->fldrv_priv
;
1792 unsigned long timeo
= jiffies
+ HZ
;
1793 unsigned long int adr
;
1794 DECLARE_WAITQUEUE(wait
, current
);
1797 adr
= cfi
->addr_unlock1
;
1799 mutex_lock(&chip
->mutex
);
1800 ret
= get_chip(map
, chip
, adr
, FL_WRITING
);
1802 mutex_unlock(&chip
->mutex
);
1806 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
1807 __func__
, chip
->start
);
1809 XIP_INVAL_CACHED_RANGE(map
, adr
, map
->size
);
1811 xip_disable(map
, chip
, adr
);
1813 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1814 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1815 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1816 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1817 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1818 cfi_send_gen_cmd(0x10, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1820 chip
->state
= FL_ERASING
;
1821 chip
->erase_suspended
= 0;
1822 chip
->in_progress_block_addr
= adr
;
1824 INVALIDATE_CACHE_UDELAY(map
, chip
,
1826 chip
->erase_time
*500);
1828 timeo
= jiffies
+ (HZ
*20);
1831 if (chip
->state
!= FL_ERASING
) {
1832 /* Someone's suspended the erase. Sleep */
1833 set_current_state(TASK_UNINTERRUPTIBLE
);
1834 add_wait_queue(&chip
->wq
, &wait
);
1835 mutex_unlock(&chip
->mutex
);
1837 remove_wait_queue(&chip
->wq
, &wait
);
1838 mutex_lock(&chip
->mutex
);
1841 if (chip
->erase_suspended
) {
1842 /* This erase was suspended and resumed.
1843 Adjust the timeout */
1844 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1845 chip
->erase_suspended
= 0;
1848 if (chip_ready(map
, adr
))
1851 if (time_after(jiffies
, timeo
)) {
1852 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1857 /* Latency issues. Drop the lock, wait a while and retry */
1858 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1860 /* Did we succeed? */
1861 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1862 /* reset on all failures. */
1863 map_write( map
, CMD(0xF0), chip
->start
);
1864 /* FIXME - should have reset delay before continuing */
1869 chip
->state
= FL_READY
;
1870 xip_enable(map
, chip
, adr
);
1872 put_chip(map
, chip
, adr
);
1873 mutex_unlock(&chip
->mutex
);
1879 static int __xipram
do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int len
, void *thunk
)
1881 struct cfi_private
*cfi
= map
->fldrv_priv
;
1882 unsigned long timeo
= jiffies
+ HZ
;
1883 DECLARE_WAITQUEUE(wait
, current
);
1888 mutex_lock(&chip
->mutex
);
1889 ret
= get_chip(map
, chip
, adr
, FL_ERASING
);
1891 mutex_unlock(&chip
->mutex
);
1895 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
1898 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1900 xip_disable(map
, chip
, adr
);
1902 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1903 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1904 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1905 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1906 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
, cfi
->device_type
, NULL
);
1907 map_write(map
, cfi
->sector_erase_cmd
, adr
);
1909 chip
->state
= FL_ERASING
;
1910 chip
->erase_suspended
= 0;
1911 chip
->in_progress_block_addr
= adr
;
1913 INVALIDATE_CACHE_UDELAY(map
, chip
,
1915 chip
->erase_time
*500);
1917 timeo
= jiffies
+ (HZ
*20);
1920 if (chip
->state
!= FL_ERASING
) {
1921 /* Someone's suspended the erase. Sleep */
1922 set_current_state(TASK_UNINTERRUPTIBLE
);
1923 add_wait_queue(&chip
->wq
, &wait
);
1924 mutex_unlock(&chip
->mutex
);
1926 remove_wait_queue(&chip
->wq
, &wait
);
1927 mutex_lock(&chip
->mutex
);
1930 if (chip
->erase_suspended
) {
1931 /* This erase was suspended and resumed.
1932 Adjust the timeout */
1933 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1934 chip
->erase_suspended
= 0;
1937 if (chip_ready(map
, adr
)) {
1938 xip_enable(map
, chip
, adr
);
1942 if (time_after(jiffies
, timeo
)) {
1943 xip_enable(map
, chip
, adr
);
1944 printk(KERN_WARNING
"MTD %s(): software timeout\n",
1949 /* Latency issues. Drop the lock, wait a while and retry */
1950 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1952 /* Did we succeed? */
1953 if (!chip_good(map
, adr
, map_word_ff(map
))) {
1954 /* reset on all failures. */
1955 map_write( map
, CMD(0xF0), chip
->start
);
1956 /* FIXME - should have reset delay before continuing */
1961 chip
->state
= FL_READY
;
1963 put_chip(map
, chip
, adr
);
1964 mutex_unlock(&chip
->mutex
);
1969 static int cfi_amdstd_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
1971 unsigned long ofs
, len
;
1977 ret
= cfi_varsize_frob(mtd
, do_erase_oneblock
, ofs
, len
, NULL
);
1981 instr
->state
= MTD_ERASE_DONE
;
1982 mtd_erase_callback(instr
);
1988 static int cfi_amdstd_erase_chip(struct mtd_info
*mtd
, struct erase_info
*instr
)
1990 struct map_info
*map
= mtd
->priv
;
1991 struct cfi_private
*cfi
= map
->fldrv_priv
;
1994 if (instr
->addr
!= 0)
1997 if (instr
->len
!= mtd
->size
)
2000 ret
= do_erase_chip(map
, &cfi
->chips
[0]);
2004 instr
->state
= MTD_ERASE_DONE
;
2005 mtd_erase_callback(instr
);
2010 static int do_atmel_lock(struct map_info
*map
, struct flchip
*chip
,
2011 unsigned long adr
, int len
, void *thunk
)
2013 struct cfi_private
*cfi
= map
->fldrv_priv
;
2016 mutex_lock(&chip
->mutex
);
2017 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_LOCKING
);
2020 chip
->state
= FL_LOCKING
;
2022 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__
, adr
, len
);
2024 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
2025 cfi
->device_type
, NULL
);
2026 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
2027 cfi
->device_type
, NULL
);
2028 cfi_send_gen_cmd(0x80, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
2029 cfi
->device_type
, NULL
);
2030 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
2031 cfi
->device_type
, NULL
);
2032 cfi_send_gen_cmd(0x55, cfi
->addr_unlock2
, chip
->start
, map
, cfi
,
2033 cfi
->device_type
, NULL
);
2034 map_write(map
, CMD(0x40), chip
->start
+ adr
);
2036 chip
->state
= FL_READY
;
2037 put_chip(map
, chip
, adr
+ chip
->start
);
2041 mutex_unlock(&chip
->mutex
);
2045 static int do_atmel_unlock(struct map_info
*map
, struct flchip
*chip
,
2046 unsigned long adr
, int len
, void *thunk
)
2048 struct cfi_private
*cfi
= map
->fldrv_priv
;
2051 mutex_lock(&chip
->mutex
);
2052 ret
= get_chip(map
, chip
, adr
+ chip
->start
, FL_UNLOCKING
);
2055 chip
->state
= FL_UNLOCKING
;
2057 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__
, adr
, len
);
2059 cfi_send_gen_cmd(0xAA, cfi
->addr_unlock1
, chip
->start
, map
, cfi
,
2060 cfi
->device_type
, NULL
);
2061 map_write(map
, CMD(0x70), adr
);
2063 chip
->state
= FL_READY
;
2064 put_chip(map
, chip
, adr
+ chip
->start
);
2068 mutex_unlock(&chip
->mutex
);
2072 static int cfi_atmel_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2074 return cfi_varsize_frob(mtd
, do_atmel_lock
, ofs
, len
, NULL
);
2077 static int cfi_atmel_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
2079 return cfi_varsize_frob(mtd
, do_atmel_unlock
, ofs
, len
, NULL
);
2083 static void cfi_amdstd_sync (struct mtd_info
*mtd
)
2085 struct map_info
*map
= mtd
->priv
;
2086 struct cfi_private
*cfi
= map
->fldrv_priv
;
2088 struct flchip
*chip
;
2090 DECLARE_WAITQUEUE(wait
, current
);
2092 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
2093 chip
= &cfi
->chips
[i
];
2096 mutex_lock(&chip
->mutex
);
2098 switch(chip
->state
) {
2102 case FL_JEDEC_QUERY
:
2103 chip
->oldstate
= chip
->state
;
2104 chip
->state
= FL_SYNCING
;
2105 /* No need to wake_up() on this state change -
2106 * as the whole point is that nobody can do anything
2107 * with the chip now anyway.
2110 mutex_unlock(&chip
->mutex
);
2114 /* Not an idle state */
2115 set_current_state(TASK_UNINTERRUPTIBLE
);
2116 add_wait_queue(&chip
->wq
, &wait
);
2118 mutex_unlock(&chip
->mutex
);
2122 remove_wait_queue(&chip
->wq
, &wait
);
2128 /* Unlock the chips again */
2130 for (i
--; i
>=0; i
--) {
2131 chip
= &cfi
->chips
[i
];
2133 mutex_lock(&chip
->mutex
);
2135 if (chip
->state
== FL_SYNCING
) {
2136 chip
->state
= chip
->oldstate
;
2139 mutex_unlock(&chip
->mutex
);
2144 static int cfi_amdstd_suspend(struct mtd_info
*mtd
)
2146 struct map_info
*map
= mtd
->priv
;
2147 struct cfi_private
*cfi
= map
->fldrv_priv
;
2149 struct flchip
*chip
;
2152 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
2153 chip
= &cfi
->chips
[i
];
2155 mutex_lock(&chip
->mutex
);
2157 switch(chip
->state
) {
2161 case FL_JEDEC_QUERY
:
2162 chip
->oldstate
= chip
->state
;
2163 chip
->state
= FL_PM_SUSPENDED
;
2164 /* No need to wake_up() on this state change -
2165 * as the whole point is that nobody can do anything
2166 * with the chip now anyway.
2168 case FL_PM_SUSPENDED
:
2175 mutex_unlock(&chip
->mutex
);
2178 /* Unlock the chips again */
2181 for (i
--; i
>=0; i
--) {
2182 chip
= &cfi
->chips
[i
];
2184 mutex_lock(&chip
->mutex
);
2186 if (chip
->state
== FL_PM_SUSPENDED
) {
2187 chip
->state
= chip
->oldstate
;
2190 mutex_unlock(&chip
->mutex
);
2198 static void cfi_amdstd_resume(struct mtd_info
*mtd
)
2200 struct map_info
*map
= mtd
->priv
;
2201 struct cfi_private
*cfi
= map
->fldrv_priv
;
2203 struct flchip
*chip
;
2205 for (i
=0; i
<cfi
->numchips
; i
++) {
2207 chip
= &cfi
->chips
[i
];
2209 mutex_lock(&chip
->mutex
);
2211 if (chip
->state
== FL_PM_SUSPENDED
) {
2212 chip
->state
= FL_READY
;
2213 map_write(map
, CMD(0xF0), chip
->start
);
2217 printk(KERN_ERR
"Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2219 mutex_unlock(&chip
->mutex
);
2225 * Ensure that the flash device is put back into read array mode before
2226 * unloading the driver or rebooting. On some systems, rebooting while
2227 * the flash is in query/program/erase mode will prevent the CPU from
2228 * fetching the bootloader code, requiring a hard reset or power cycle.
2230 static int cfi_amdstd_reset(struct mtd_info
*mtd
)
2232 struct map_info
*map
= mtd
->priv
;
2233 struct cfi_private
*cfi
= map
->fldrv_priv
;
2235 struct flchip
*chip
;
2237 for (i
= 0; i
< cfi
->numchips
; i
++) {
2239 chip
= &cfi
->chips
[i
];
2241 mutex_lock(&chip
->mutex
);
2243 ret
= get_chip(map
, chip
, chip
->start
, FL_SHUTDOWN
);
2245 map_write(map
, CMD(0xF0), chip
->start
);
2246 chip
->state
= FL_SHUTDOWN
;
2247 put_chip(map
, chip
, chip
->start
);
2250 mutex_unlock(&chip
->mutex
);
2257 static int cfi_amdstd_reboot(struct notifier_block
*nb
, unsigned long val
,
2260 struct mtd_info
*mtd
;
2262 mtd
= container_of(nb
, struct mtd_info
, reboot_notifier
);
2263 cfi_amdstd_reset(mtd
);
2268 static void cfi_amdstd_destroy(struct mtd_info
*mtd
)
2270 struct map_info
*map
= mtd
->priv
;
2271 struct cfi_private
*cfi
= map
->fldrv_priv
;
2273 cfi_amdstd_reset(mtd
);
2274 unregister_reboot_notifier(&mtd
->reboot_notifier
);
2275 kfree(cfi
->cmdset_priv
);
2278 kfree(mtd
->eraseregions
);
2281 MODULE_LICENSE("GPL");
2282 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2283 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
2284 MODULE_ALIAS("cfi_cmdset_0006");
2285 MODULE_ALIAS("cfi_cmdset_0701");