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>
28 #include <asm/byteorder.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/reboot.h>
36 #include <linux/of_platform.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
47 #define SST49LF004B 0x0060
48 #define SST49LF040B 0x0050
49 #define SST49LF008A 0x005a
50 #define AT49BV6416 0x00d6
52 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
53 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
55 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
56 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
57 static void cfi_amdstd_sync (struct mtd_info *);
58 static int cfi_amdstd_suspend (struct mtd_info *);
59 static void cfi_amdstd_resume (struct mtd_info *);
60 static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
61 static int cfi_amdstd_get_fact_prot_info(struct mtd_info *, size_t,
62 size_t *, struct otp_info *);
63 static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t,
64 size_t *, struct otp_info *);
65 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
66 static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t,
68 static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t,
70 static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t,
72 static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t);
74 static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
75 size_t *retlen, const u_char *buf);
77 static void cfi_amdstd_destroy(struct mtd_info *);
79 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
80 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
82 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
83 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
86 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
87 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
89 static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
90 static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
91 static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
93 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
94 .probe = NULL, /* Not usable directly */
95 .destroy = cfi_amdstd_destroy,
96 .name = "cfi_cmdset_0002",
101 /* #define DEBUG_CFI_FEATURES */
104 #ifdef DEBUG_CFI_FEATURES
105 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
107 const char* erase_suspend[3] = {
108 "Not supported", "Read only", "Read/write"
110 const char* top_bottom[6] = {
111 "No WP", "8x8KiB sectors at top & bottom, no WP",
112 "Bottom boot", "Top boot",
113 "Uniform, Bottom WP", "Uniform, Top WP"
116 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
117 printk(" Address sensitive unlock: %s\n",
118 (extp->SiliconRevision & 1) ? "Not required" : "Required");
120 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
121 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
123 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
125 if (extp->BlkProt == 0)
126 printk(" Block protection: Not supported\n");
128 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
131 printk(" Temporary block unprotect: %s\n",
132 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
133 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
134 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
135 printk(" Burst mode: %s\n",
136 extp->BurstMode ? "Supported" : "Not supported");
137 if (extp->PageMode == 0)
138 printk(" Page mode: Not supported\n");
140 printk(" Page mode: %d word page\n", extp->PageMode << 2);
142 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
143 extp->VppMin >> 4, extp->VppMin & 0xf);
144 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
145 extp->VppMax >> 4, extp->VppMax & 0xf);
147 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
148 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
150 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
154 #ifdef AMD_BOOTLOC_BUG
155 /* Wheee. Bring me the head of someone at AMD. */
156 static void fixup_amd_bootblock(struct mtd_info *mtd)
158 struct map_info *map = mtd->priv;
159 struct cfi_private *cfi = map->fldrv_priv;
160 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
161 __u8 major = extp->MajorVersion;
162 __u8 minor = extp->MinorVersion;
164 if (((major << 8) | minor) < 0x3131) {
165 /* CFI version 1.0 => don't trust bootloc */
167 pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
168 map->name, cfi->mfr, cfi->id);
170 /* AFAICS all 29LV400 with a bottom boot block have a device ID
171 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
172 * These were badly detected as they have the 0x80 bit set
173 * so treat them as a special case.
175 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
177 /* Macronix added CFI to their 2nd generation
178 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
179 * Fujitsu, Spansion, EON, ESI and older Macronix)
182 * Therefore also check the manufacturer.
183 * This reduces the risk of false detection due to
184 * the 8-bit device ID.
186 (cfi->mfr == CFI_MFR_MACRONIX)) {
187 pr_debug("%s: Macronix MX29LV400C with bottom boot block"
188 " detected\n", map->name);
189 extp->TopBottom = 2; /* bottom boot */
191 if (cfi->id & 0x80) {
192 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
193 extp->TopBottom = 3; /* top boot */
195 extp->TopBottom = 2; /* bottom boot */
198 pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
199 " deduced %s from Device ID\n", map->name, major, minor,
200 extp->TopBottom == 2 ? "bottom" : "top");
205 static void fixup_use_write_buffers(struct mtd_info *mtd)
207 struct map_info *map = mtd->priv;
208 struct cfi_private *cfi = map->fldrv_priv;
209 if (cfi->cfiq->BufWriteTimeoutTyp) {
210 pr_debug("Using buffer write method\n" );
211 mtd->_write = cfi_amdstd_write_buffers;
215 /* Atmel chips don't use the same PRI format as AMD chips */
216 static void fixup_convert_atmel_pri(struct mtd_info *mtd)
218 struct map_info *map = mtd->priv;
219 struct cfi_private *cfi = map->fldrv_priv;
220 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
221 struct cfi_pri_atmel atmel_pri;
223 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
224 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
226 if (atmel_pri.Features & 0x02)
227 extp->EraseSuspend = 2;
229 /* Some chips got it backwards... */
230 if (cfi->id == AT49BV6416) {
231 if (atmel_pri.BottomBoot)
236 if (atmel_pri.BottomBoot)
242 /* burst write mode not supported */
243 cfi->cfiq->BufWriteTimeoutTyp = 0;
244 cfi->cfiq->BufWriteTimeoutMax = 0;
247 static void fixup_use_secsi(struct mtd_info *mtd)
249 /* Setup for chips with a secsi area */
250 mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
251 mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
254 static void fixup_use_erase_chip(struct mtd_info *mtd)
256 struct map_info *map = mtd->priv;
257 struct cfi_private *cfi = map->fldrv_priv;
258 if ((cfi->cfiq->NumEraseRegions == 1) &&
259 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
260 mtd->_erase = cfi_amdstd_erase_chip;
266 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
269 static void fixup_use_atmel_lock(struct mtd_info *mtd)
271 mtd->_lock = cfi_atmel_lock;
272 mtd->_unlock = cfi_atmel_unlock;
273 mtd->flags |= MTD_POWERUP_LOCK;
276 static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
278 struct map_info *map = mtd->priv;
279 struct cfi_private *cfi = map->fldrv_priv;
282 * These flashes report two separate eraseblock regions based on the
283 * sector_erase-size and block_erase-size, although they both operate on the
284 * same memory. This is not allowed according to CFI, so we just pick the
287 cfi->cfiq->NumEraseRegions = 1;
290 static void fixup_sst39vf(struct mtd_info *mtd)
292 struct map_info *map = mtd->priv;
293 struct cfi_private *cfi = map->fldrv_priv;
295 fixup_old_sst_eraseregion(mtd);
297 cfi->addr_unlock1 = 0x5555;
298 cfi->addr_unlock2 = 0x2AAA;
301 static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
303 struct map_info *map = mtd->priv;
304 struct cfi_private *cfi = map->fldrv_priv;
306 fixup_old_sst_eraseregion(mtd);
308 cfi->addr_unlock1 = 0x555;
309 cfi->addr_unlock2 = 0x2AA;
311 cfi->sector_erase_cmd = CMD(0x50);
314 static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
316 struct map_info *map = mtd->priv;
317 struct cfi_private *cfi = map->fldrv_priv;
319 fixup_sst39vf_rev_b(mtd);
322 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
323 * it should report a size of 8KBytes (0x0020*256).
325 cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
326 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
329 static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
331 struct map_info *map = mtd->priv;
332 struct cfi_private *cfi = map->fldrv_priv;
334 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
335 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
336 pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
340 static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
342 struct map_info *map = mtd->priv;
343 struct cfi_private *cfi = map->fldrv_priv;
345 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
346 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
347 pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
351 static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
353 struct map_info *map = mtd->priv;
354 struct cfi_private *cfi = map->fldrv_priv;
357 * S29NS512P flash uses more than 8bits to report number of sectors,
358 * which is not permitted by CFI.
360 cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
361 pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
364 /* Used to fix CFI-Tables of chips without Extended Query Tables */
365 static struct cfi_fixup cfi_nopri_fixup_table[] = {
366 { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
367 { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
368 { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
369 { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
370 { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
371 { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
372 { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
373 { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
377 static struct cfi_fixup cfi_fixup_table[] = {
378 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
379 #ifdef AMD_BOOTLOC_BUG
380 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
381 { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
382 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
384 { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
385 { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
386 { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
387 { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
388 { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
389 { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
390 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
391 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
392 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
393 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
394 { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
395 { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
396 { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
397 { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
398 { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
399 #if !FORCE_WORD_WRITE
400 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
404 static struct cfi_fixup jedec_fixup_table[] = {
405 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
406 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
407 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
411 static struct cfi_fixup fixup_table[] = {
412 /* The CFI vendor ids and the JEDEC vendor IDs appear
413 * to be common. It is like the devices id's are as
414 * well. This table is to pick all cases where
415 * we know that is the case.
417 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
418 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
423 static void cfi_fixup_major_minor(struct cfi_private *cfi,
424 struct cfi_pri_amdstd *extp)
426 if (cfi->mfr == CFI_MFR_SAMSUNG) {
427 if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
428 (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
430 * Samsung K8P2815UQB and K8D6x16UxM chips
431 * report major=0 / minor=0.
432 * K8D3x16UxC chips report major=3 / minor=3.
434 printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
435 " Extended Query version to 1.%c\n",
437 extp->MajorVersion = '1';
442 * SST 38VF640x chips report major=0xFF / minor=0xFF.
444 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
445 extp->MajorVersion = '1';
446 extp->MinorVersion = '0';
450 static int is_m29ew(struct cfi_private *cfi)
452 if (cfi->mfr == CFI_MFR_INTEL &&
453 ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
454 (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
460 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
461 * Some revisions of the M29EW suffer from erase suspend hang ups. In
462 * particular, it can occur when the sequence
463 * Erase Confirm -> Suspend -> Program -> Resume
464 * causes a lockup due to internal timing issues. The consequence is that the
465 * erase cannot be resumed without inserting a dummy command after programming
466 * and prior to resuming. [...] The work-around is to issue a dummy write cycle
467 * that writes an F0 command code before the RESUME command.
469 static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
472 struct cfi_private *cfi = map->fldrv_priv;
473 /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
475 map_write(map, CMD(0xF0), adr);
479 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
481 * Some revisions of the M29EW (for example, A1 and A2 step revisions)
482 * are affected by a problem that could cause a hang up when an ERASE SUSPEND
483 * command is issued after an ERASE RESUME operation without waiting for a
484 * minimum delay. The result is that once the ERASE seems to be completed
485 * (no bits are toggling), the contents of the Flash memory block on which
486 * the erase was ongoing could be inconsistent with the expected values
487 * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
488 * values), causing a consequent failure of the ERASE operation.
489 * The occurrence of this issue could be high, especially when file system
490 * operations on the Flash are intensive. As a result, it is recommended
491 * that a patch be applied. Intensive file system operations can cause many
492 * calls to the garbage routine to free Flash space (also by erasing physical
493 * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
494 * commands can occur. The problem disappears when a delay is inserted after
495 * the RESUME command by using the udelay() function available in Linux.
496 * The DELAY value must be tuned based on the customer's platform.
497 * The maximum value that fixes the problem in all cases is 500us.
498 * But, in our experience, a delay of 30 µs to 50 µs is sufficient
500 * We have chosen 500µs because this latency is acceptable.
502 static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
505 * Resolving the Delay After Resume Issue see Micron TN-13-07
506 * Worst case delay must be 500µs but 30-50µs should be ok as well
512 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
514 struct cfi_private *cfi = map->fldrv_priv;
515 struct device_node __maybe_unused *np = map->device_node;
516 struct mtd_info *mtd;
519 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
523 mtd->type = MTD_NORFLASH;
525 /* Fill in the default mtd operations */
526 mtd->_erase = cfi_amdstd_erase_varsize;
527 mtd->_write = cfi_amdstd_write_words;
528 mtd->_read = cfi_amdstd_read;
529 mtd->_sync = cfi_amdstd_sync;
530 mtd->_suspend = cfi_amdstd_suspend;
531 mtd->_resume = cfi_amdstd_resume;
532 mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg;
533 mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg;
534 mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info;
535 mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info;
536 mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg;
537 mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg;
538 mtd->flags = MTD_CAP_NORFLASH;
539 mtd->name = map->name;
541 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
543 pr_debug("MTD %s(): write buffer size %d\n", __func__,
546 mtd->_panic_write = cfi_amdstd_panic_write;
547 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
549 if (cfi->cfi_mode==CFI_MODE_CFI){
550 unsigned char bootloc;
551 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
552 struct cfi_pri_amdstd *extp;
554 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
557 * It's a real CFI chip, not one for which the probe
558 * routine faked a CFI structure.
560 cfi_fixup_major_minor(cfi, extp);
563 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
564 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
565 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
566 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
567 * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
569 if (extp->MajorVersion != '1' ||
570 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
571 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
572 "version %c.%c (%#02x/%#02x).\n",
573 extp->MajorVersion, extp->MinorVersion,
574 extp->MajorVersion, extp->MinorVersion);
580 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
581 extp->MajorVersion, extp->MinorVersion);
583 /* Install our own private info structure */
584 cfi->cmdset_priv = extp;
586 /* Apply cfi device specific fixups */
587 cfi_fixup(mtd, cfi_fixup_table);
589 #ifdef DEBUG_CFI_FEATURES
590 /* Tell the user about it in lots of lovely detail */
591 cfi_tell_features(extp);
595 if (np && of_property_read_bool(
596 np, "use-advanced-sector-protection")
597 && extp->BlkProtUnprot == 8) {
598 printk(KERN_INFO " Advanced Sector Protection (PPB Locking) supported\n");
599 mtd->_lock = cfi_ppb_lock;
600 mtd->_unlock = cfi_ppb_unlock;
601 mtd->_is_locked = cfi_ppb_is_locked;
605 bootloc = extp->TopBottom;
606 if ((bootloc < 2) || (bootloc > 5)) {
607 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
608 "bank location (%d). Assuming bottom.\n",
613 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
614 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
616 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
617 int j = (cfi->cfiq->NumEraseRegions-1)-i;
619 swap(cfi->cfiq->EraseRegionInfo[i],
620 cfi->cfiq->EraseRegionInfo[j]);
623 /* Set the default CFI lock/unlock addresses */
624 cfi->addr_unlock1 = 0x555;
625 cfi->addr_unlock2 = 0x2aa;
627 cfi_fixup(mtd, cfi_nopri_fixup_table);
629 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
635 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
636 /* Apply jedec specific fixups */
637 cfi_fixup(mtd, jedec_fixup_table);
639 /* Apply generic fixups */
640 cfi_fixup(mtd, fixup_table);
642 for (i=0; i< cfi->numchips; i++) {
643 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
644 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
645 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
647 * First calculate the timeout max according to timeout field
648 * of struct cfi_ident that probed from chip's CFI aera, if
649 * available. Specify a minimum of 2000us, in case the CFI data
652 if (cfi->cfiq->BufWriteTimeoutTyp &&
653 cfi->cfiq->BufWriteTimeoutMax)
654 cfi->chips[i].buffer_write_time_max =
655 1 << (cfi->cfiq->BufWriteTimeoutTyp +
656 cfi->cfiq->BufWriteTimeoutMax);
658 cfi->chips[i].buffer_write_time_max = 0;
660 cfi->chips[i].buffer_write_time_max =
661 max(cfi->chips[i].buffer_write_time_max, 2000);
663 cfi->chips[i].ref_point_counter = 0;
664 init_waitqueue_head(&(cfi->chips[i].wq));
667 map->fldrv = &cfi_amdstd_chipdrv;
669 return cfi_amdstd_setup(mtd);
671 struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
672 struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
673 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
674 EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
675 EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
677 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
679 struct map_info *map = mtd->priv;
680 struct cfi_private *cfi = map->fldrv_priv;
681 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
682 unsigned long offset = 0;
685 printk(KERN_NOTICE "number of %s chips: %d\n",
686 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
687 /* Select the correct geometry setup */
688 mtd->size = devsize * cfi->numchips;
690 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
691 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
692 * mtd->numeraseregions, GFP_KERNEL);
693 if (!mtd->eraseregions)
696 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
697 unsigned long ernum, ersize;
698 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
699 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
701 if (mtd->erasesize < ersize) {
702 mtd->erasesize = ersize;
704 for (j=0; j<cfi->numchips; j++) {
705 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
706 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
707 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
709 offset += (ersize * ernum);
711 if (offset != devsize) {
713 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
717 __module_get(THIS_MODULE);
718 register_reboot_notifier(&mtd->reboot_notifier);
722 kfree(mtd->eraseregions);
724 kfree(cfi->cmdset_priv);
729 * Return true if the chip is ready.
731 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
732 * non-suspended sector) and is indicated by no toggle bits toggling.
734 * Note that anything more complicated than checking if no bits are toggling
735 * (including checking DQ5 for an error status) is tricky to get working
736 * correctly and is therefore not done (particularly with interleaved chips
737 * as each chip must be checked independently of the others).
739 static int __xipram chip_ready(struct map_info *map, unsigned long addr)
743 d = map_read(map, addr);
744 t = map_read(map, addr);
746 return map_word_equal(map, d, t);
750 * Return true if the chip is ready and has the correct value.
752 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
753 * non-suspended sector) and it is indicated by no bits toggling.
755 * Error are indicated by toggling bits or bits held with the wrong value,
756 * or with bits toggling.
758 * Note that anything more complicated than checking if no bits are toggling
759 * (including checking DQ5 for an error status) is tricky to get working
760 * correctly and is therefore not done (particularly with interleaved chips
761 * as each chip must be checked independently of the others).
764 static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
768 oldd = map_read(map, addr);
769 curd = map_read(map, addr);
771 return map_word_equal(map, oldd, curd) &&
772 map_word_equal(map, curd, expected);
775 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
777 DECLARE_WAITQUEUE(wait, current);
778 struct cfi_private *cfi = map->fldrv_priv;
780 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
783 timeo = jiffies + HZ;
785 switch (chip->state) {
789 if (chip_ready(map, adr))
792 if (time_after(jiffies, timeo)) {
793 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
796 mutex_unlock(&chip->mutex);
798 mutex_lock(&chip->mutex);
799 /* Someone else might have been playing with it. */
809 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
810 !(mode == FL_READY || mode == FL_POINT ||
811 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
814 /* Do not allow suspend iff read/write to EB address */
815 if ((adr & chip->in_progress_block_mask) ==
816 chip->in_progress_block_addr)
820 /* It's harmless to issue the Erase-Suspend and Erase-Resume
821 * commands when the erase algorithm isn't in progress. */
822 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
823 chip->oldstate = FL_ERASING;
824 chip->state = FL_ERASE_SUSPENDING;
825 chip->erase_suspended = 1;
827 if (chip_ready(map, adr))
830 if (time_after(jiffies, timeo)) {
831 /* Should have suspended the erase by now.
832 * Send an Erase-Resume command as either
833 * there was an error (so leave the erase
834 * routine to recover from it) or we trying to
835 * use the erase-in-progress sector. */
836 put_chip(map, chip, adr);
837 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
841 mutex_unlock(&chip->mutex);
843 mutex_lock(&chip->mutex);
844 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
845 So we can just loop here. */
847 chip->state = FL_READY;
850 case FL_XIP_WHILE_ERASING:
851 if (mode != FL_READY && mode != FL_POINT &&
852 (!cfip || !(cfip->EraseSuspend&2)))
854 chip->oldstate = chip->state;
855 chip->state = FL_READY;
859 /* The machine is rebooting */
863 /* Only if there's no operation suspended... */
864 if (mode == FL_READY && chip->oldstate == FL_READY)
869 set_current_state(TASK_UNINTERRUPTIBLE);
870 add_wait_queue(&chip->wq, &wait);
871 mutex_unlock(&chip->mutex);
873 remove_wait_queue(&chip->wq, &wait);
874 mutex_lock(&chip->mutex);
880 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
882 struct cfi_private *cfi = map->fldrv_priv;
884 switch(chip->oldstate) {
886 cfi_fixup_m29ew_erase_suspend(map,
887 chip->in_progress_block_addr);
888 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
889 cfi_fixup_m29ew_delay_after_resume(cfi);
890 chip->oldstate = FL_READY;
891 chip->state = FL_ERASING;
894 case FL_XIP_WHILE_ERASING:
895 chip->state = chip->oldstate;
896 chip->oldstate = FL_READY;
903 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
908 #ifdef CONFIG_MTD_XIP
911 * No interrupt what so ever can be serviced while the flash isn't in array
912 * mode. This is ensured by the xip_disable() and xip_enable() functions
913 * enclosing any code path where the flash is known not to be in array mode.
914 * And within a XIP disabled code path, only functions marked with __xipram
915 * may be called and nothing else (it's a good thing to inspect generated
916 * assembly to make sure inline functions were actually inlined and that gcc
917 * didn't emit calls to its own support functions). Also configuring MTD CFI
918 * support to a single buswidth and a single interleave is also recommended.
921 static void xip_disable(struct map_info *map, struct flchip *chip,
924 /* TODO: chips with no XIP use should ignore and return */
925 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
929 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
932 struct cfi_private *cfi = map->fldrv_priv;
934 if (chip->state != FL_POINT && chip->state != FL_READY) {
935 map_write(map, CMD(0xf0), adr);
936 chip->state = FL_READY;
938 (void) map_read(map, adr);
944 * When a delay is required for the flash operation to complete, the
945 * xip_udelay() function is polling for both the given timeout and pending
946 * (but still masked) hardware interrupts. Whenever there is an interrupt
947 * pending then the flash erase operation is suspended, array mode restored
948 * and interrupts unmasked. Task scheduling might also happen at that
949 * point. The CPU eventually returns from the interrupt or the call to
950 * schedule() and the suspended flash operation is resumed for the remaining
951 * of the delay period.
953 * Warning: this function _will_ fool interrupt latency tracing tools.
956 static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
957 unsigned long adr, int usec)
959 struct cfi_private *cfi = map->fldrv_priv;
960 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
961 map_word status, OK = CMD(0x80);
962 unsigned long suspended, start = xip_currtime();
967 if (xip_irqpending() && extp &&
968 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
969 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
971 * Let's suspend the erase operation when supported.
972 * Note that we currently don't try to suspend
973 * interleaved chips if there is already another
974 * operation suspended (imagine what happens
975 * when one chip was already done with the current
976 * operation while another chip suspended it, then
977 * we resume the whole thing at once). Yes, it
980 map_write(map, CMD(0xb0), adr);
981 usec -= xip_elapsed_since(start);
982 suspended = xip_currtime();
984 if (xip_elapsed_since(suspended) > 100000) {
986 * The chip doesn't want to suspend
987 * after waiting for 100 msecs.
988 * This is a critical error but there
989 * is not much we can do here.
993 status = map_read(map, adr);
994 } while (!map_word_andequal(map, status, OK, OK));
996 /* Suspend succeeded */
997 oldstate = chip->state;
998 if (!map_word_bitsset(map, status, CMD(0x40)))
1000 chip->state = FL_XIP_WHILE_ERASING;
1001 chip->erase_suspended = 1;
1002 map_write(map, CMD(0xf0), adr);
1003 (void) map_read(map, adr);
1006 mutex_unlock(&chip->mutex);
1011 * We're back. However someone else might have
1012 * decided to go write to the chip if we are in
1013 * a suspended erase state. If so let's wait
1016 mutex_lock(&chip->mutex);
1017 while (chip->state != FL_XIP_WHILE_ERASING) {
1018 DECLARE_WAITQUEUE(wait, current);
1019 set_current_state(TASK_UNINTERRUPTIBLE);
1020 add_wait_queue(&chip->wq, &wait);
1021 mutex_unlock(&chip->mutex);
1023 remove_wait_queue(&chip->wq, &wait);
1024 mutex_lock(&chip->mutex);
1026 /* Disallow XIP again */
1027 local_irq_disable();
1029 /* Correct Erase Suspend Hangups for M29EW */
1030 cfi_fixup_m29ew_erase_suspend(map, adr);
1031 /* Resume the write or erase operation */
1032 map_write(map, cfi->sector_erase_cmd, adr);
1033 chip->state = oldstate;
1034 start = xip_currtime();
1035 } else if (usec >= 1000000/HZ) {
1037 * Try to save on CPU power when waiting delay
1038 * is at least a system timer tick period.
1039 * No need to be extremely accurate here.
1043 status = map_read(map, adr);
1044 } while (!map_word_andequal(map, status, OK, OK)
1045 && xip_elapsed_since(start) < usec);
1048 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1051 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1052 * the flash is actively programming or erasing since we have to poll for
1053 * the operation to complete anyway. We can't do that in a generic way with
1054 * a XIP setup so do it before the actual flash operation in this case
1055 * and stub it out from INVALIDATE_CACHE_UDELAY.
1057 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
1058 INVALIDATE_CACHED_RANGE(map, from, size)
1060 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1061 UDELAY(map, chip, adr, usec)
1066 * Activating this XIP support changes the way the code works a bit. For
1067 * example the code to suspend the current process when concurrent access
1068 * happens is never executed because xip_udelay() will always return with the
1069 * same chip state as it was entered with. This is why there is no care for
1070 * the presence of add_wait_queue() or schedule() calls from within a couple
1071 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1072 * The queueing and scheduling are always happening within xip_udelay().
1074 * Similarly, get_chip() and put_chip() just happen to always be executed
1075 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1076 * is in array mode, therefore never executing many cases therein and not
1077 * causing any problem with XIP.
1082 #define xip_disable(map, chip, adr)
1083 #define xip_enable(map, chip, adr)
1084 #define XIP_INVAL_CACHED_RANGE(x...)
1086 #define UDELAY(map, chip, adr, usec) \
1088 mutex_unlock(&chip->mutex); \
1090 mutex_lock(&chip->mutex); \
1093 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1095 mutex_unlock(&chip->mutex); \
1096 INVALIDATE_CACHED_RANGE(map, adr, len); \
1098 mutex_lock(&chip->mutex); \
1103 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1105 unsigned long cmd_addr;
1106 struct cfi_private *cfi = map->fldrv_priv;
1111 /* Ensure cmd read/writes are aligned. */
1112 cmd_addr = adr & ~(map_bankwidth(map)-1);
1114 mutex_lock(&chip->mutex);
1115 ret = get_chip(map, chip, cmd_addr, FL_READY);
1117 mutex_unlock(&chip->mutex);
1121 if (chip->state != FL_POINT && chip->state != FL_READY) {
1122 map_write(map, CMD(0xf0), cmd_addr);
1123 chip->state = FL_READY;
1126 map_copy_from(map, buf, adr, len);
1128 put_chip(map, chip, cmd_addr);
1130 mutex_unlock(&chip->mutex);
1135 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1137 struct map_info *map = mtd->priv;
1138 struct cfi_private *cfi = map->fldrv_priv;
1143 /* ofs: offset within the first chip that the first read should start */
1144 chipnum = (from >> cfi->chipshift);
1145 ofs = from - (chipnum << cfi->chipshift);
1148 unsigned long thislen;
1150 if (chipnum >= cfi->numchips)
1153 if ((len + ofs -1) >> cfi->chipshift)
1154 thislen = (1<<cfi->chipshift) - ofs;
1158 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1172 typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
1173 loff_t adr, size_t len, u_char *buf, size_t grouplen);
1175 static inline void otp_enter(struct map_info *map, struct flchip *chip,
1176 loff_t adr, size_t len)
1178 struct cfi_private *cfi = map->fldrv_priv;
1180 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1181 cfi->device_type, NULL);
1182 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1183 cfi->device_type, NULL);
1184 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
1185 cfi->device_type, NULL);
1187 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1190 static inline void otp_exit(struct map_info *map, struct flchip *chip,
1191 loff_t adr, size_t len)
1193 struct cfi_private *cfi = map->fldrv_priv;
1195 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1196 cfi->device_type, NULL);
1197 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1198 cfi->device_type, NULL);
1199 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
1200 cfi->device_type, NULL);
1201 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
1202 cfi->device_type, NULL);
1204 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1207 static inline int do_read_secsi_onechip(struct map_info *map,
1208 struct flchip *chip, loff_t adr,
1209 size_t len, u_char *buf,
1212 DECLARE_WAITQUEUE(wait, current);
1213 unsigned long timeo = jiffies + HZ;
1216 mutex_lock(&chip->mutex);
1218 if (chip->state != FL_READY){
1219 set_current_state(TASK_UNINTERRUPTIBLE);
1220 add_wait_queue(&chip->wq, &wait);
1222 mutex_unlock(&chip->mutex);
1225 remove_wait_queue(&chip->wq, &wait);
1226 timeo = jiffies + HZ;
1233 chip->state = FL_READY;
1235 otp_enter(map, chip, adr, len);
1236 map_copy_from(map, buf, adr, len);
1237 otp_exit(map, chip, adr, len);
1240 mutex_unlock(&chip->mutex);
1245 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1247 struct map_info *map = mtd->priv;
1248 struct cfi_private *cfi = map->fldrv_priv;
1253 /* ofs: offset within the first chip that the first read should start */
1254 /* 8 secsi bytes per chip */
1259 unsigned long thislen;
1261 if (chipnum >= cfi->numchips)
1264 if ((len + ofs -1) >> 3)
1265 thislen = (1<<3) - ofs;
1269 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
1284 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1285 unsigned long adr, map_word datum,
1288 static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
1289 size_t len, u_char *buf, size_t grouplen)
1293 unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
1294 int gap = adr - bus_ofs;
1295 int n = min_t(int, len, map_bankwidth(map) - gap);
1296 map_word datum = map_word_ff(map);
1298 if (n != map_bankwidth(map)) {
1299 /* partial write of a word, load old contents */
1300 otp_enter(map, chip, bus_ofs, map_bankwidth(map));
1301 datum = map_read(map, bus_ofs);
1302 otp_exit(map, chip, bus_ofs, map_bankwidth(map));
1305 datum = map_word_load_partial(map, datum, buf, gap, n);
1306 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
1318 static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
1319 size_t len, u_char *buf, size_t grouplen)
1321 struct cfi_private *cfi = map->fldrv_priv;
1323 unsigned long timeo;
1326 /* make sure area matches group boundaries */
1327 if ((adr != 0) || (len != grouplen))
1330 mutex_lock(&chip->mutex);
1331 ret = get_chip(map, chip, chip->start, FL_LOCKING);
1333 mutex_unlock(&chip->mutex);
1336 chip->state = FL_LOCKING;
1338 /* Enter lock register command */
1339 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1340 cfi->device_type, NULL);
1341 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1342 cfi->device_type, NULL);
1343 cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
1344 cfi->device_type, NULL);
1346 /* read lock register */
1347 lockreg = cfi_read_query(map, 0);
1349 /* set bit 0 to protect extended memory block */
1352 /* set bit 0 to protect extended memory block */
1353 /* write lock register */
1354 map_write(map, CMD(0xA0), chip->start);
1355 map_write(map, CMD(lockreg), chip->start);
1357 /* wait for chip to become ready */
1358 timeo = jiffies + msecs_to_jiffies(2);
1360 if (chip_ready(map, adr))
1363 if (time_after(jiffies, timeo)) {
1364 pr_err("Waiting for chip to be ready timed out.\n");
1368 UDELAY(map, chip, 0, 1);
1371 /* exit protection commands */
1372 map_write(map, CMD(0x90), chip->start);
1373 map_write(map, CMD(0x00), chip->start);
1375 chip->state = FL_READY;
1376 put_chip(map, chip, chip->start);
1377 mutex_unlock(&chip->mutex);
1382 static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1383 size_t *retlen, u_char *buf,
1384 otp_op_t action, int user_regs)
1386 struct map_info *map = mtd->priv;
1387 struct cfi_private *cfi = map->fldrv_priv;
1388 int ofs_factor = cfi->interleave * cfi->device_type;
1391 struct flchip *chip;
1392 uint8_t otp, lockreg;
1395 size_t user_size, factory_size, otpsize;
1396 loff_t user_offset, factory_offset, otpoffset;
1397 int user_locked = 0, otplocked;
1401 for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
1402 chip = &cfi->chips[chipnum];
1406 /* Micron M29EW family */
1407 if (is_m29ew(cfi)) {
1410 /* check whether secsi area is factory locked
1412 mutex_lock(&chip->mutex);
1413 ret = get_chip(map, chip, base, FL_CFI_QUERY);
1415 mutex_unlock(&chip->mutex);
1418 cfi_qry_mode_on(base, map, cfi);
1419 otp = cfi_read_query(map, base + 0x3 * ofs_factor);
1420 cfi_qry_mode_off(base, map, cfi);
1421 put_chip(map, chip, base);
1422 mutex_unlock(&chip->mutex);
1425 /* factory locked */
1427 factory_size = 0x100;
1429 /* customer lockable */
1433 mutex_lock(&chip->mutex);
1434 ret = get_chip(map, chip, base, FL_LOCKING);
1436 mutex_unlock(&chip->mutex);
1440 /* Enter lock register command */
1441 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
1442 chip->start, map, cfi,
1443 cfi->device_type, NULL);
1444 cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
1445 chip->start, map, cfi,
1446 cfi->device_type, NULL);
1447 cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
1448 chip->start, map, cfi,
1449 cfi->device_type, NULL);
1450 /* read lock register */
1451 lockreg = cfi_read_query(map, 0);
1452 /* exit protection commands */
1453 map_write(map, CMD(0x90), chip->start);
1454 map_write(map, CMD(0x00), chip->start);
1455 put_chip(map, chip, chip->start);
1456 mutex_unlock(&chip->mutex);
1458 user_locked = ((lockreg & 0x01) == 0x00);
1462 otpsize = user_regs ? user_size : factory_size;
1465 otpoffset = user_regs ? user_offset : factory_offset;
1466 otplocked = user_regs ? user_locked : 1;
1469 /* return otpinfo */
1470 struct otp_info *otpinfo;
1471 len -= sizeof(*otpinfo);
1474 otpinfo = (struct otp_info *)buf;
1475 otpinfo->start = from;
1476 otpinfo->length = otpsize;
1477 otpinfo->locked = otplocked;
1478 buf += sizeof(*otpinfo);
1479 *retlen += sizeof(*otpinfo);
1481 } else if ((from < otpsize) && (len > 0)) {
1483 size = (len < otpsize - from) ? len : otpsize - from;
1484 ret = action(map, chip, otpoffset + from, size, buf,
1500 static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
1501 size_t *retlen, struct otp_info *buf)
1503 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1507 static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
1508 size_t *retlen, struct otp_info *buf)
1510 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1514 static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1515 size_t len, size_t *retlen,
1518 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1519 buf, do_read_secsi_onechip, 0);
1522 static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1523 size_t len, size_t *retlen,
1526 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1527 buf, do_read_secsi_onechip, 1);
1530 static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1531 size_t len, size_t *retlen,
1534 return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
1538 static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1542 return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
1546 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1547 unsigned long adr, map_word datum,
1550 struct cfi_private *cfi = map->fldrv_priv;
1551 unsigned long timeo = jiffies + HZ;
1553 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1554 * have a max write time of a few hundreds usec). However, we should
1555 * use the maximum timeout value given by the chip at probe time
1556 * instead. Unfortunately, struct flchip does have a field for
1557 * maximum timeout, only for typical which can be far too short
1558 * depending of the conditions. The ' + 1' is to avoid having a
1559 * timeout of 0 jiffies if HZ is smaller than 1000.
1561 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1568 mutex_lock(&chip->mutex);
1569 ret = get_chip(map, chip, adr, mode);
1571 mutex_unlock(&chip->mutex);
1575 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1576 __func__, adr, datum.x[0] );
1578 if (mode == FL_OTP_WRITE)
1579 otp_enter(map, chip, adr, map_bankwidth(map));
1582 * Check for a NOP for the case when the datum to write is already
1583 * present - it saves time and works around buggy chips that corrupt
1584 * data at other locations when 0xff is written to a location that
1585 * already contains 0xff.
1587 oldd = map_read(map, adr);
1588 if (map_word_equal(map, oldd, datum)) {
1589 pr_debug("MTD %s(): NOP\n",
1594 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1596 xip_disable(map, chip, adr);
1599 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1600 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1601 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1602 map_write(map, datum, adr);
1605 INVALIDATE_CACHE_UDELAY(map, chip,
1606 adr, map_bankwidth(map),
1607 chip->word_write_time);
1609 /* See comment above for timeout value. */
1610 timeo = jiffies + uWriteTimeout;
1612 if (chip->state != mode) {
1613 /* Someone's suspended the write. Sleep */
1614 DECLARE_WAITQUEUE(wait, current);
1616 set_current_state(TASK_UNINTERRUPTIBLE);
1617 add_wait_queue(&chip->wq, &wait);
1618 mutex_unlock(&chip->mutex);
1620 remove_wait_queue(&chip->wq, &wait);
1621 timeo = jiffies + (HZ / 2); /* FIXME */
1622 mutex_lock(&chip->mutex);
1627 * We check "time_after" and "!chip_good" before checking
1628 * "chip_good" to avoid the failure due to scheduling.
1630 if (time_after(jiffies, timeo) && !chip_good(map, adr, datum)) {
1631 xip_enable(map, chip, adr);
1632 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
1633 xip_disable(map, chip, adr);
1638 if (chip_good(map, adr, datum))
1641 /* Latency issues. Drop the lock, wait a while and retry */
1642 UDELAY(map, chip, adr, 1);
1645 /* Did we succeed? */
1647 /* reset on all failures. */
1648 map_write( map, CMD(0xF0), chip->start );
1649 /* FIXME - should have reset delay before continuing */
1651 if (++retry_cnt <= MAX_RETRIES) {
1656 xip_enable(map, chip, adr);
1658 if (mode == FL_OTP_WRITE)
1659 otp_exit(map, chip, adr, map_bankwidth(map));
1660 chip->state = FL_READY;
1662 put_chip(map, chip, adr);
1663 mutex_unlock(&chip->mutex);
1669 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1670 size_t *retlen, const u_char *buf)
1672 struct map_info *map = mtd->priv;
1673 struct cfi_private *cfi = map->fldrv_priv;
1676 unsigned long ofs, chipstart;
1677 DECLARE_WAITQUEUE(wait, current);
1679 chipnum = to >> cfi->chipshift;
1680 ofs = to - (chipnum << cfi->chipshift);
1681 chipstart = cfi->chips[chipnum].start;
1683 /* If it's not bus-aligned, do the first byte write */
1684 if (ofs & (map_bankwidth(map)-1)) {
1685 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1686 int i = ofs - bus_ofs;
1691 mutex_lock(&cfi->chips[chipnum].mutex);
1693 if (cfi->chips[chipnum].state != FL_READY) {
1694 set_current_state(TASK_UNINTERRUPTIBLE);
1695 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1697 mutex_unlock(&cfi->chips[chipnum].mutex);
1700 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1704 /* Load 'tmp_buf' with old contents of flash */
1705 tmp_buf = map_read(map, bus_ofs+chipstart);
1707 mutex_unlock(&cfi->chips[chipnum].mutex);
1709 /* Number of bytes to copy from buffer */
1710 n = min_t(int, len, map_bankwidth(map)-i);
1712 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1714 ret = do_write_oneword(map, &cfi->chips[chipnum],
1715 bus_ofs, tmp_buf, FL_WRITING);
1724 if (ofs >> cfi->chipshift) {
1727 if (chipnum == cfi->numchips)
1732 /* We are now aligned, write as much as possible */
1733 while(len >= map_bankwidth(map)) {
1736 datum = map_word_load(map, buf);
1738 ret = do_write_oneword(map, &cfi->chips[chipnum],
1739 ofs, datum, FL_WRITING);
1743 ofs += map_bankwidth(map);
1744 buf += map_bankwidth(map);
1745 (*retlen) += map_bankwidth(map);
1746 len -= map_bankwidth(map);
1748 if (ofs >> cfi->chipshift) {
1751 if (chipnum == cfi->numchips)
1753 chipstart = cfi->chips[chipnum].start;
1757 /* Write the trailing bytes if any */
1758 if (len & (map_bankwidth(map)-1)) {
1762 mutex_lock(&cfi->chips[chipnum].mutex);
1764 if (cfi->chips[chipnum].state != FL_READY) {
1765 set_current_state(TASK_UNINTERRUPTIBLE);
1766 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1768 mutex_unlock(&cfi->chips[chipnum].mutex);
1771 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1775 tmp_buf = map_read(map, ofs + chipstart);
1777 mutex_unlock(&cfi->chips[chipnum].mutex);
1779 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1781 ret = do_write_oneword(map, &cfi->chips[chipnum],
1782 ofs, tmp_buf, FL_WRITING);
1794 * FIXME: interleaved mode not tested, and probably not supported!
1796 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1797 unsigned long adr, const u_char *buf,
1800 struct cfi_private *cfi = map->fldrv_priv;
1801 unsigned long timeo = jiffies + HZ;
1803 * Timeout is calculated according to CFI data, if available.
1804 * See more comments in cfi_cmdset_0002().
1806 unsigned long uWriteTimeout =
1807 usecs_to_jiffies(chip->buffer_write_time_max);
1809 unsigned long cmd_adr;
1816 mutex_lock(&chip->mutex);
1817 ret = get_chip(map, chip, adr, FL_WRITING);
1819 mutex_unlock(&chip->mutex);
1823 datum = map_word_load(map, buf);
1825 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1826 __func__, adr, datum.x[0] );
1828 XIP_INVAL_CACHED_RANGE(map, adr, len);
1830 xip_disable(map, chip, cmd_adr);
1832 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1833 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1835 /* Write Buffer Load */
1836 map_write(map, CMD(0x25), cmd_adr);
1838 chip->state = FL_WRITING_TO_BUFFER;
1840 /* Write length of data to come */
1841 words = len / map_bankwidth(map);
1842 map_write(map, CMD(words - 1), cmd_adr);
1845 while(z < words * map_bankwidth(map)) {
1846 datum = map_word_load(map, buf);
1847 map_write(map, datum, adr + z);
1849 z += map_bankwidth(map);
1850 buf += map_bankwidth(map);
1852 z -= map_bankwidth(map);
1856 /* Write Buffer Program Confirm: GO GO GO */
1857 map_write(map, CMD(0x29), cmd_adr);
1858 chip->state = FL_WRITING;
1860 INVALIDATE_CACHE_UDELAY(map, chip,
1861 adr, map_bankwidth(map),
1862 chip->word_write_time);
1864 timeo = jiffies + uWriteTimeout;
1867 if (chip->state != FL_WRITING) {
1868 /* Someone's suspended the write. Sleep */
1869 DECLARE_WAITQUEUE(wait, current);
1871 set_current_state(TASK_UNINTERRUPTIBLE);
1872 add_wait_queue(&chip->wq, &wait);
1873 mutex_unlock(&chip->mutex);
1875 remove_wait_queue(&chip->wq, &wait);
1876 timeo = jiffies + (HZ / 2); /* FIXME */
1877 mutex_lock(&chip->mutex);
1882 * We check "time_after" and "!chip_good" before checking "chip_good" to avoid
1883 * the failure due to scheduling.
1885 if (time_after(jiffies, timeo) && !chip_good(map, adr, datum))
1888 if (chip_good(map, adr, datum)) {
1889 xip_enable(map, chip, adr);
1893 /* Latency issues. Drop the lock, wait a while and retry */
1894 UDELAY(map, chip, adr, 1);
1898 * Recovery from write-buffer programming failures requires
1899 * the write-to-buffer-reset sequence. Since the last part
1900 * of the sequence also works as a normal reset, we can run
1901 * the same commands regardless of why we are here.
1903 * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
1905 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1906 cfi->device_type, NULL);
1907 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1908 cfi->device_type, NULL);
1909 cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
1910 cfi->device_type, NULL);
1911 xip_enable(map, chip, adr);
1912 /* FIXME - should have reset delay before continuing */
1914 printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
1919 chip->state = FL_READY;
1921 put_chip(map, chip, adr);
1922 mutex_unlock(&chip->mutex);
1928 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1929 size_t *retlen, const u_char *buf)
1931 struct map_info *map = mtd->priv;
1932 struct cfi_private *cfi = map->fldrv_priv;
1933 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1938 chipnum = to >> cfi->chipshift;
1939 ofs = to - (chipnum << cfi->chipshift);
1941 /* If it's not bus-aligned, do the first word write */
1942 if (ofs & (map_bankwidth(map)-1)) {
1943 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1944 if (local_len > len)
1946 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1947 local_len, retlen, buf);
1954 if (ofs >> cfi->chipshift) {
1957 if (chipnum == cfi->numchips)
1962 /* Write buffer is worth it only if more than one word to write... */
1963 while (len >= map_bankwidth(map) * 2) {
1964 /* We must not cross write block boundaries */
1965 int size = wbufsize - (ofs & (wbufsize-1));
1969 if (size % map_bankwidth(map))
1970 size -= size % map_bankwidth(map);
1972 ret = do_write_buffer(map, &cfi->chips[chipnum],
1982 if (ofs >> cfi->chipshift) {
1985 if (chipnum == cfi->numchips)
1991 size_t retlen_dregs = 0;
1993 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1994 len, &retlen_dregs, buf);
1996 *retlen += retlen_dregs;
2004 * Wait for the flash chip to become ready to write data
2006 * This is only called during the panic_write() path. When panic_write()
2007 * is called, the kernel is in the process of a panic, and will soon be
2008 * dead. Therefore we don't take any locks, and attempt to get access
2009 * to the chip as soon as possible.
2011 static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
2014 struct cfi_private *cfi = map->fldrv_priv;
2019 * If the driver thinks the chip is idle, and no toggle bits
2020 * are changing, then the chip is actually idle for sure.
2022 if (chip->state == FL_READY && chip_ready(map, adr))
2026 * Try several times to reset the chip and then wait for it
2027 * to become idle. The upper limit of a few milliseconds of
2028 * delay isn't a big problem: the kernel is dying anyway. It
2029 * is more important to save the messages.
2031 while (retries > 0) {
2032 const unsigned long timeo = (HZ / 1000) + 1;
2034 /* send the reset command */
2035 map_write(map, CMD(0xF0), chip->start);
2037 /* wait for the chip to become ready */
2038 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
2039 if (chip_ready(map, adr))
2048 /* the chip never became ready */
2053 * Write out one word of data to a single flash chip during a kernel panic
2055 * This is only called during the panic_write() path. When panic_write()
2056 * is called, the kernel is in the process of a panic, and will soon be
2057 * dead. Therefore we don't take any locks, and attempt to get access
2058 * to the chip as soon as possible.
2060 * The implementation of this routine is intentionally similar to
2061 * do_write_oneword(), in order to ease code maintenance.
2063 static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
2064 unsigned long adr, map_word datum)
2066 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
2067 struct cfi_private *cfi = map->fldrv_priv;
2075 ret = cfi_amdstd_panic_wait(map, chip, adr);
2079 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
2080 __func__, adr, datum.x[0]);
2083 * Check for a NOP for the case when the datum to write is already
2084 * present - it saves time and works around buggy chips that corrupt
2085 * data at other locations when 0xff is written to a location that
2086 * already contains 0xff.
2088 oldd = map_read(map, adr);
2089 if (map_word_equal(map, oldd, datum)) {
2090 pr_debug("MTD %s(): NOP\n", __func__);
2097 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2098 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2099 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2100 map_write(map, datum, adr);
2102 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
2103 if (chip_ready(map, adr))
2109 if (!chip_good(map, adr, datum)) {
2110 /* reset on all failures. */
2111 map_write(map, CMD(0xF0), chip->start);
2112 /* FIXME - should have reset delay before continuing */
2114 if (++retry_cnt <= MAX_RETRIES)
2126 * Write out some data during a kernel panic
2128 * This is used by the mtdoops driver to save the dying messages from a
2129 * kernel which has panic'd.
2131 * This routine ignores all of the locking used throughout the rest of the
2132 * driver, in order to ensure that the data gets written out no matter what
2133 * state this driver (and the flash chip itself) was in when the kernel crashed.
2135 * The implementation of this routine is intentionally similar to
2136 * cfi_amdstd_write_words(), in order to ease code maintenance.
2138 static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
2139 size_t *retlen, const u_char *buf)
2141 struct map_info *map = mtd->priv;
2142 struct cfi_private *cfi = map->fldrv_priv;
2143 unsigned long ofs, chipstart;
2147 chipnum = to >> cfi->chipshift;
2148 ofs = to - (chipnum << cfi->chipshift);
2149 chipstart = cfi->chips[chipnum].start;
2151 /* If it's not bus aligned, do the first byte write */
2152 if (ofs & (map_bankwidth(map) - 1)) {
2153 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
2154 int i = ofs - bus_ofs;
2158 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
2162 /* Load 'tmp_buf' with old contents of flash */
2163 tmp_buf = map_read(map, bus_ofs + chipstart);
2165 /* Number of bytes to copy from buffer */
2166 n = min_t(int, len, map_bankwidth(map) - i);
2168 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
2170 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2180 if (ofs >> cfi->chipshift) {
2183 if (chipnum == cfi->numchips)
2188 /* We are now aligned, write as much as possible */
2189 while (len >= map_bankwidth(map)) {
2192 datum = map_word_load(map, buf);
2194 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2199 ofs += map_bankwidth(map);
2200 buf += map_bankwidth(map);
2201 (*retlen) += map_bankwidth(map);
2202 len -= map_bankwidth(map);
2204 if (ofs >> cfi->chipshift) {
2207 if (chipnum == cfi->numchips)
2210 chipstart = cfi->chips[chipnum].start;
2214 /* Write the trailing bytes if any */
2215 if (len & (map_bankwidth(map) - 1)) {
2218 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
2222 tmp_buf = map_read(map, ofs + chipstart);
2224 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
2226 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2239 * Handle devices with one erase region, that only implement
2240 * the chip erase command.
2242 static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
2244 struct cfi_private *cfi = map->fldrv_priv;
2245 unsigned long timeo = jiffies + HZ;
2246 unsigned long int adr;
2247 DECLARE_WAITQUEUE(wait, current);
2251 adr = cfi->addr_unlock1;
2253 mutex_lock(&chip->mutex);
2254 ret = get_chip(map, chip, adr, FL_WRITING);
2256 mutex_unlock(&chip->mutex);
2260 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
2261 __func__, chip->start );
2263 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
2265 xip_disable(map, chip, adr);
2268 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2269 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2270 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2271 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2272 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2273 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2275 chip->state = FL_ERASING;
2276 chip->erase_suspended = 0;
2277 chip->in_progress_block_addr = adr;
2278 chip->in_progress_block_mask = ~(map->size - 1);
2280 INVALIDATE_CACHE_UDELAY(map, chip,
2282 chip->erase_time*500);
2284 timeo = jiffies + (HZ*20);
2287 if (chip->state != FL_ERASING) {
2288 /* Someone's suspended the erase. Sleep */
2289 set_current_state(TASK_UNINTERRUPTIBLE);
2290 add_wait_queue(&chip->wq, &wait);
2291 mutex_unlock(&chip->mutex);
2293 remove_wait_queue(&chip->wq, &wait);
2294 mutex_lock(&chip->mutex);
2297 if (chip->erase_suspended) {
2298 /* This erase was suspended and resumed.
2299 Adjust the timeout */
2300 timeo = jiffies + (HZ*20); /* FIXME */
2301 chip->erase_suspended = 0;
2304 if (chip_good(map, adr, map_word_ff(map)))
2307 if (time_after(jiffies, timeo)) {
2308 printk(KERN_WARNING "MTD %s(): software timeout\n",
2314 /* Latency issues. Drop the lock, wait a while and retry */
2315 UDELAY(map, chip, adr, 1000000/HZ);
2317 /* Did we succeed? */
2319 /* reset on all failures. */
2320 map_write( map, CMD(0xF0), chip->start );
2321 /* FIXME - should have reset delay before continuing */
2323 if (++retry_cnt <= MAX_RETRIES) {
2329 chip->state = FL_READY;
2330 xip_enable(map, chip, adr);
2332 put_chip(map, chip, adr);
2333 mutex_unlock(&chip->mutex);
2339 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
2341 struct cfi_private *cfi = map->fldrv_priv;
2342 unsigned long timeo = jiffies + HZ;
2343 DECLARE_WAITQUEUE(wait, current);
2349 mutex_lock(&chip->mutex);
2350 ret = get_chip(map, chip, adr, FL_ERASING);
2352 mutex_unlock(&chip->mutex);
2356 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
2359 XIP_INVAL_CACHED_RANGE(map, adr, len);
2361 xip_disable(map, chip, adr);
2364 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2365 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2366 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2367 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2368 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2369 map_write(map, cfi->sector_erase_cmd, adr);
2371 chip->state = FL_ERASING;
2372 chip->erase_suspended = 0;
2373 chip->in_progress_block_addr = adr;
2374 chip->in_progress_block_mask = ~(len - 1);
2376 INVALIDATE_CACHE_UDELAY(map, chip,
2378 chip->erase_time*500);
2380 timeo = jiffies + (HZ*20);
2383 if (chip->state != FL_ERASING) {
2384 /* Someone's suspended the erase. Sleep */
2385 set_current_state(TASK_UNINTERRUPTIBLE);
2386 add_wait_queue(&chip->wq, &wait);
2387 mutex_unlock(&chip->mutex);
2389 remove_wait_queue(&chip->wq, &wait);
2390 mutex_lock(&chip->mutex);
2393 if (chip->erase_suspended) {
2394 /* This erase was suspended and resumed.
2395 Adjust the timeout */
2396 timeo = jiffies + (HZ*20); /* FIXME */
2397 chip->erase_suspended = 0;
2400 if (chip_good(map, adr, map_word_ff(map))) {
2401 xip_enable(map, chip, adr);
2405 if (time_after(jiffies, timeo)) {
2406 xip_enable(map, chip, adr);
2407 printk(KERN_WARNING "MTD %s(): software timeout\n",
2413 /* Latency issues. Drop the lock, wait a while and retry */
2414 UDELAY(map, chip, adr, 1000000/HZ);
2416 /* Did we succeed? */
2418 /* reset on all failures. */
2419 map_write( map, CMD(0xF0), chip->start );
2420 /* FIXME - should have reset delay before continuing */
2422 if (++retry_cnt <= MAX_RETRIES) {
2428 chip->state = FL_READY;
2430 put_chip(map, chip, adr);
2431 mutex_unlock(&chip->mutex);
2436 static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
2438 unsigned long ofs, len;
2444 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2448 instr->state = MTD_ERASE_DONE;
2449 mtd_erase_callback(instr);
2455 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
2457 struct map_info *map = mtd->priv;
2458 struct cfi_private *cfi = map->fldrv_priv;
2461 if (instr->addr != 0)
2464 if (instr->len != mtd->size)
2467 ret = do_erase_chip(map, &cfi->chips[0]);
2471 instr->state = MTD_ERASE_DONE;
2472 mtd_erase_callback(instr);
2477 static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2478 unsigned long adr, int len, void *thunk)
2480 struct cfi_private *cfi = map->fldrv_priv;
2483 mutex_lock(&chip->mutex);
2484 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2487 chip->state = FL_LOCKING;
2489 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
2491 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2492 cfi->device_type, NULL);
2493 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2494 cfi->device_type, NULL);
2495 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2496 cfi->device_type, NULL);
2497 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2498 cfi->device_type, NULL);
2499 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2500 cfi->device_type, NULL);
2501 map_write(map, CMD(0x40), chip->start + adr);
2503 chip->state = FL_READY;
2504 put_chip(map, chip, adr + chip->start);
2508 mutex_unlock(&chip->mutex);
2512 static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2513 unsigned long adr, int len, void *thunk)
2515 struct cfi_private *cfi = map->fldrv_priv;
2518 mutex_lock(&chip->mutex);
2519 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2522 chip->state = FL_UNLOCKING;
2524 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
2526 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2527 cfi->device_type, NULL);
2528 map_write(map, CMD(0x70), adr);
2530 chip->state = FL_READY;
2531 put_chip(map, chip, adr + chip->start);
2535 mutex_unlock(&chip->mutex);
2539 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2541 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2544 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2546 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2550 * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
2554 struct flchip *chip;
2559 #define MAX_SECTORS 512
2561 #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
2562 #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
2563 #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
2565 static int __maybe_unused do_ppb_xxlock(struct map_info *map,
2566 struct flchip *chip,
2567 unsigned long adr, int len, void *thunk)
2569 struct cfi_private *cfi = map->fldrv_priv;
2570 unsigned long timeo;
2574 mutex_lock(&chip->mutex);
2575 ret = get_chip(map, chip, adr, FL_LOCKING);
2577 mutex_unlock(&chip->mutex);
2581 pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
2583 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2584 cfi->device_type, NULL);
2585 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2586 cfi->device_type, NULL);
2587 /* PPB entry command */
2588 cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
2589 cfi->device_type, NULL);
2591 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2592 chip->state = FL_LOCKING;
2593 map_write(map, CMD(0xA0), adr);
2594 map_write(map, CMD(0x00), adr);
2595 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2597 * Unlocking of one specific sector is not supported, so we
2598 * have to unlock all sectors of this device instead
2600 chip->state = FL_UNLOCKING;
2601 map_write(map, CMD(0x80), chip->start);
2602 map_write(map, CMD(0x30), chip->start);
2603 } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
2604 chip->state = FL_JEDEC_QUERY;
2605 /* Return locked status: 0->locked, 1->unlocked */
2606 ret = !cfi_read_query(map, adr);
2611 * Wait for some time as unlocking of all sectors takes quite long
2613 timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
2615 if (chip_ready(map, adr))
2618 if (time_after(jiffies, timeo)) {
2619 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
2624 UDELAY(map, chip, adr, 1);
2627 /* Exit BC commands */
2628 map_write(map, CMD(0x90), chip->start);
2629 map_write(map, CMD(0x00), chip->start);
2631 chip->state = FL_READY;
2632 put_chip(map, chip, adr);
2633 mutex_unlock(&chip->mutex);
2638 static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
2641 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2642 DO_XXLOCK_ONEBLOCK_LOCK);
2645 static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
2648 struct mtd_erase_region_info *regions = mtd->eraseregions;
2649 struct map_info *map = mtd->priv;
2650 struct cfi_private *cfi = map->fldrv_priv;
2651 struct ppb_lock *sect;
2661 * PPB unlocking always unlocks all sectors of the flash chip.
2662 * We need to re-lock all previously locked sectors. So lets
2663 * first check the locking status of all sectors and save
2664 * it for future use.
2666 sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
2671 * This code to walk all sectors is a slightly modified version
2672 * of the cfi_varsize_frob() code.
2682 int size = regions[i].erasesize;
2685 * Only test sectors that shall not be unlocked. The other
2686 * sectors shall be unlocked, so lets keep their locking
2687 * status at "unlocked" (locked=0) for the final re-locking.
2689 if ((offset < ofs) || (offset >= (ofs + len))) {
2690 sect[sectors].chip = &cfi->chips[chipnum];
2691 sect[sectors].adr = adr;
2692 sect[sectors].locked = do_ppb_xxlock(
2693 map, &cfi->chips[chipnum], adr, 0,
2694 DO_XXLOCK_ONEBLOCK_GETLOCK);
2701 if (offset == regions[i].offset + size * regions[i].numblocks)
2704 if (adr >> cfi->chipshift) {
2705 if (offset >= (ofs + len))
2710 if (chipnum >= cfi->numchips)
2715 if (sectors >= MAX_SECTORS) {
2716 printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
2723 /* Now unlock the whole chip */
2724 ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2725 DO_XXLOCK_ONEBLOCK_UNLOCK);
2732 * PPB unlocking always unlocks all sectors of the flash chip.
2733 * We need to re-lock all previously locked sectors.
2735 for (i = 0; i < sectors; i++) {
2737 do_ppb_xxlock(map, sect[i].chip, sect[i].adr, 0,
2738 DO_XXLOCK_ONEBLOCK_LOCK);
2745 static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
2748 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2749 DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
2752 static void cfi_amdstd_sync (struct mtd_info *mtd)
2754 struct map_info *map = mtd->priv;
2755 struct cfi_private *cfi = map->fldrv_priv;
2757 struct flchip *chip;
2759 DECLARE_WAITQUEUE(wait, current);
2761 for (i=0; !ret && i<cfi->numchips; i++) {
2762 chip = &cfi->chips[i];
2765 mutex_lock(&chip->mutex);
2767 switch(chip->state) {
2771 case FL_JEDEC_QUERY:
2772 chip->oldstate = chip->state;
2773 chip->state = FL_SYNCING;
2774 /* No need to wake_up() on this state change -
2775 * as the whole point is that nobody can do anything
2776 * with the chip now anyway.
2779 mutex_unlock(&chip->mutex);
2783 /* Not an idle state */
2784 set_current_state(TASK_UNINTERRUPTIBLE);
2785 add_wait_queue(&chip->wq, &wait);
2787 mutex_unlock(&chip->mutex);
2791 remove_wait_queue(&chip->wq, &wait);
2797 /* Unlock the chips again */
2799 for (i--; i >=0; i--) {
2800 chip = &cfi->chips[i];
2802 mutex_lock(&chip->mutex);
2804 if (chip->state == FL_SYNCING) {
2805 chip->state = chip->oldstate;
2808 mutex_unlock(&chip->mutex);
2813 static int cfi_amdstd_suspend(struct mtd_info *mtd)
2815 struct map_info *map = mtd->priv;
2816 struct cfi_private *cfi = map->fldrv_priv;
2818 struct flchip *chip;
2821 for (i=0; !ret && i<cfi->numchips; i++) {
2822 chip = &cfi->chips[i];
2824 mutex_lock(&chip->mutex);
2826 switch(chip->state) {
2830 case FL_JEDEC_QUERY:
2831 chip->oldstate = chip->state;
2832 chip->state = FL_PM_SUSPENDED;
2833 /* No need to wake_up() on this state change -
2834 * as the whole point is that nobody can do anything
2835 * with the chip now anyway.
2837 case FL_PM_SUSPENDED:
2844 mutex_unlock(&chip->mutex);
2847 /* Unlock the chips again */
2850 for (i--; i >=0; i--) {
2851 chip = &cfi->chips[i];
2853 mutex_lock(&chip->mutex);
2855 if (chip->state == FL_PM_SUSPENDED) {
2856 chip->state = chip->oldstate;
2859 mutex_unlock(&chip->mutex);
2867 static void cfi_amdstd_resume(struct mtd_info *mtd)
2869 struct map_info *map = mtd->priv;
2870 struct cfi_private *cfi = map->fldrv_priv;
2872 struct flchip *chip;
2874 for (i=0; i<cfi->numchips; i++) {
2876 chip = &cfi->chips[i];
2878 mutex_lock(&chip->mutex);
2880 if (chip->state == FL_PM_SUSPENDED) {
2881 chip->state = FL_READY;
2882 map_write(map, CMD(0xF0), chip->start);
2886 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2888 mutex_unlock(&chip->mutex);
2894 * Ensure that the flash device is put back into read array mode before
2895 * unloading the driver or rebooting. On some systems, rebooting while
2896 * the flash is in query/program/erase mode will prevent the CPU from
2897 * fetching the bootloader code, requiring a hard reset or power cycle.
2899 static int cfi_amdstd_reset(struct mtd_info *mtd)
2901 struct map_info *map = mtd->priv;
2902 struct cfi_private *cfi = map->fldrv_priv;
2904 struct flchip *chip;
2906 for (i = 0; i < cfi->numchips; i++) {
2908 chip = &cfi->chips[i];
2910 mutex_lock(&chip->mutex);
2912 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2914 map_write(map, CMD(0xF0), chip->start);
2915 chip->state = FL_SHUTDOWN;
2916 put_chip(map, chip, chip->start);
2919 mutex_unlock(&chip->mutex);
2926 static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2929 struct mtd_info *mtd;
2931 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2932 cfi_amdstd_reset(mtd);
2937 static void cfi_amdstd_destroy(struct mtd_info *mtd)
2939 struct map_info *map = mtd->priv;
2940 struct cfi_private *cfi = map->fldrv_priv;
2942 cfi_amdstd_reset(mtd);
2943 unregister_reboot_notifier(&mtd->reboot_notifier);
2944 kfree(cfi->cmdset_priv);
2947 kfree(mtd->eraseregions);
2950 MODULE_LICENSE("GPL");
2951 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2952 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
2953 MODULE_ALIAS("cfi_cmdset_0006");
2954 MODULE_ALIAS("cfi_cmdset_0701");