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>
8 * 2_by_8 routines added by Simon Munton
10 * 4_by_16 work by Carolyn J. Smith
12 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
16 * $Id: cfi_cmdset_0002.c,v 1.114 2004/12/11 15:43:53 dedekind Exp $
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
27 #include <asm/byteorder.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/interrupt.h>
33 #include <linux/mtd/compatmac.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/cfi.h>
38 #define AMD_BOOTLOC_BUG
39 #define FORCE_WORD_WRITE 0
41 #define MAX_WORD_RETRIES 3
43 #define MANUFACTURER_AMD 0x0001
44 #define MANUFACTURER_SST 0x00BF
45 #define SST49LF004B 0x0060
47 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
48 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
49 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
50 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
51 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
52 static void cfi_amdstd_sync (struct mtd_info *);
53 static int cfi_amdstd_suspend (struct mtd_info *);
54 static void cfi_amdstd_resume (struct mtd_info *);
55 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
57 static void cfi_amdstd_destroy(struct mtd_info *);
59 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
60 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
62 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
63 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
66 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
67 .probe = NULL, /* Not usable directly */
68 .destroy = cfi_amdstd_destroy,
69 .name = "cfi_cmdset_0002",
74 /* #define DEBUG_CFI_FEATURES */
77 #ifdef DEBUG_CFI_FEATURES
78 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
80 const char* erase_suspend[3] = {
81 "Not supported", "Read only", "Read/write"
83 const char* top_bottom[6] = {
84 "No WP", "8x8KiB sectors at top & bottom, no WP",
85 "Bottom boot", "Top boot",
86 "Uniform, Bottom WP", "Uniform, Top WP"
89 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
90 printk(" Address sensitive unlock: %s\n",
91 (extp->SiliconRevision & 1) ? "Not required" : "Required");
93 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
94 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
96 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
98 if (extp->BlkProt == 0)
99 printk(" Block protection: Not supported\n");
101 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
104 printk(" Temporary block unprotect: %s\n",
105 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
106 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
107 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
108 printk(" Burst mode: %s\n",
109 extp->BurstMode ? "Supported" : "Not supported");
110 if (extp->PageMode == 0)
111 printk(" Page mode: Not supported\n");
113 printk(" Page mode: %d word page\n", extp->PageMode << 2);
115 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
116 extp->VppMin >> 4, extp->VppMin & 0xf);
117 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
118 extp->VppMax >> 4, extp->VppMax & 0xf);
120 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
121 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
123 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
127 #ifdef AMD_BOOTLOC_BUG
128 /* Wheee. Bring me the head of someone at AMD. */
129 static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
131 struct map_info *map = mtd->priv;
132 struct cfi_private *cfi = map->fldrv_priv;
133 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
134 __u8 major = extp->MajorVersion;
135 __u8 minor = extp->MinorVersion;
137 if (((major << 8) | minor) < 0x3131) {
138 /* CFI version 1.0 => don't trust bootloc */
139 if (cfi->id & 0x80) {
140 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
141 extp->TopBottom = 3; /* top boot */
143 extp->TopBottom = 2; /* bottom boot */
149 static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
151 struct map_info *map = mtd->priv;
152 struct cfi_private *cfi = map->fldrv_priv;
153 if (cfi->cfiq->BufWriteTimeoutTyp) {
154 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
155 mtd->write = cfi_amdstd_write_buffers;
159 static void fixup_use_secsi(struct mtd_info *mtd, void *param)
161 /* Setup for chips with a secsi area */
162 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
163 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
166 static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
168 struct map_info *map = mtd->priv;
169 struct cfi_private *cfi = map->fldrv_priv;
170 if ((cfi->cfiq->NumEraseRegions == 1) &&
171 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
172 mtd->erase = cfi_amdstd_erase_chip;
177 static struct cfi_fixup cfi_fixup_table[] = {
178 #ifdef AMD_BOOTLOC_BUG
179 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
181 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
182 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
183 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
184 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
185 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
186 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
187 #if !FORCE_WORD_WRITE
188 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
192 static struct cfi_fixup jedec_fixup_table[] = {
193 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
197 static struct cfi_fixup fixup_table[] = {
198 /* The CFI vendor ids and the JEDEC vendor IDs appear
199 * to be common. It is like the devices id's are as
200 * well. This table is to pick all cases where
201 * we know that is the case.
203 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
208 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
210 struct cfi_private *cfi = map->fldrv_priv;
211 struct mtd_info *mtd;
214 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
216 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
219 memset(mtd, 0, sizeof(*mtd));
221 mtd->type = MTD_NORFLASH;
223 /* Fill in the default mtd operations */
224 mtd->erase = cfi_amdstd_erase_varsize;
225 mtd->write = cfi_amdstd_write_words;
226 mtd->read = cfi_amdstd_read;
227 mtd->sync = cfi_amdstd_sync;
228 mtd->suspend = cfi_amdstd_suspend;
229 mtd->resume = cfi_amdstd_resume;
230 mtd->flags = MTD_CAP_NORFLASH;
231 mtd->name = map->name;
233 if (cfi->cfi_mode==CFI_MODE_CFI){
234 unsigned char bootloc;
236 * It's a real CFI chip, not one for which the probe
237 * routine faked a CFI structure. So we read the feature
240 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
241 struct cfi_pri_amdstd *extp;
243 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
249 /* Install our own private info structure */
250 cfi->cmdset_priv = extp;
252 /* Apply cfi device specific fixups */
253 cfi_fixup(mtd, cfi_fixup_table);
255 #ifdef DEBUG_CFI_FEATURES
256 /* Tell the user about it in lots of lovely detail */
257 cfi_tell_features(extp);
260 bootloc = extp->TopBottom;
261 if ((bootloc != 2) && (bootloc != 3)) {
262 printk(KERN_WARNING "%s: CFI does not contain boot "
263 "bank location. Assuming top.\n", map->name);
267 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
268 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
270 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
271 int j = (cfi->cfiq->NumEraseRegions-1)-i;
274 swap = cfi->cfiq->EraseRegionInfo[i];
275 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
276 cfi->cfiq->EraseRegionInfo[j] = swap;
279 /* Set the default CFI lock/unlock addresses */
280 cfi->addr_unlock1 = 0x555;
281 cfi->addr_unlock2 = 0x2aa;
282 /* Modify the unlock address if we are in compatibility mode */
283 if ( /* x16 in x8 mode */
284 ((cfi->device_type == CFI_DEVICETYPE_X8) &&
285 (cfi->cfiq->InterfaceDesc == 2)) ||
286 /* x32 in x16 mode */
287 ((cfi->device_type == CFI_DEVICETYPE_X16) &&
288 (cfi->cfiq->InterfaceDesc == 4)))
290 cfi->addr_unlock1 = 0xaaa;
291 cfi->addr_unlock2 = 0x555;
295 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
296 /* Apply jedec specific fixups */
297 cfi_fixup(mtd, jedec_fixup_table);
299 /* Apply generic fixups */
300 cfi_fixup(mtd, fixup_table);
302 for (i=0; i< cfi->numchips; i++) {
303 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
304 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
305 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
308 map->fldrv = &cfi_amdstd_chipdrv;
310 return cfi_amdstd_setup(mtd);
314 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
316 struct map_info *map = mtd->priv;
317 struct cfi_private *cfi = map->fldrv_priv;
318 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
319 unsigned long offset = 0;
322 printk(KERN_NOTICE "number of %s chips: %d\n",
323 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
324 /* Select the correct geometry setup */
325 mtd->size = devsize * cfi->numchips;
327 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
328 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
329 * mtd->numeraseregions, GFP_KERNEL);
330 if (!mtd->eraseregions) {
331 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
335 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
336 unsigned long ernum, ersize;
337 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
338 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
340 if (mtd->erasesize < ersize) {
341 mtd->erasesize = ersize;
343 for (j=0; j<cfi->numchips; j++) {
344 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
345 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
346 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
348 offset += (ersize * ernum);
350 if (offset != devsize) {
352 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
357 for (i=0; i<mtd->numeraseregions;i++){
358 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
359 i,mtd->eraseregions[i].offset,
360 mtd->eraseregions[i].erasesize,
361 mtd->eraseregions[i].numblocks);
365 /* FIXME: erase-suspend-program is broken. See
366 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
367 printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
369 __module_get(THIS_MODULE);
374 if(mtd->eraseregions)
375 kfree(mtd->eraseregions);
378 kfree(cfi->cmdset_priv);
384 * Return true if the chip is ready.
386 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
387 * non-suspended sector) and is indicated by no toggle bits toggling.
389 * Note that anything more complicated than checking if no bits are toggling
390 * (including checking DQ5 for an error status) is tricky to get working
391 * correctly and is therefore not done (particulary with interleaved chips
392 * as each chip must be checked independantly of the others).
394 static int chip_ready(struct map_info *map, unsigned long addr)
398 d = map_read(map, addr);
399 t = map_read(map, addr);
401 return map_word_equal(map, d, t);
404 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
406 DECLARE_WAITQUEUE(wait, current);
407 struct cfi_private *cfi = map->fldrv_priv;
409 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
412 timeo = jiffies + HZ;
414 switch (chip->state) {
418 if (chip_ready(map, adr))
421 if (time_after(jiffies, timeo)) {
422 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
423 cfi_spin_unlock(chip->mutex);
426 cfi_spin_unlock(chip->mutex);
428 cfi_spin_lock(chip->mutex);
429 /* Someone else might have been playing with it. */
439 if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
442 if (!(mode == FL_READY || mode == FL_POINT
444 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
445 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1))))
448 /* We could check to see if we're trying to access the sector
449 * that is currently being erased. However, no user will try
450 * anything like that so we just wait for the timeout. */
453 /* It's harmless to issue the Erase-Suspend and Erase-Resume
454 * commands when the erase algorithm isn't in progress. */
455 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
456 chip->oldstate = FL_ERASING;
457 chip->state = FL_ERASE_SUSPENDING;
458 chip->erase_suspended = 1;
460 if (chip_ready(map, adr))
463 if (time_after(jiffies, timeo)) {
464 /* Should have suspended the erase by now.
465 * Send an Erase-Resume command as either
466 * there was an error (so leave the erase
467 * routine to recover from it) or we trying to
468 * use the erase-in-progress sector. */
469 map_write(map, CMD(0x30), chip->in_progress_block_addr);
470 chip->state = FL_ERASING;
471 chip->oldstate = FL_READY;
472 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
476 cfi_spin_unlock(chip->mutex);
478 cfi_spin_lock(chip->mutex);
479 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
480 So we can just loop here. */
482 chip->state = FL_READY;
486 /* Only if there's no operation suspended... */
487 if (mode == FL_READY && chip->oldstate == FL_READY)
492 set_current_state(TASK_UNINTERRUPTIBLE);
493 add_wait_queue(&chip->wq, &wait);
494 cfi_spin_unlock(chip->mutex);
496 remove_wait_queue(&chip->wq, &wait);
497 cfi_spin_lock(chip->mutex);
503 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
505 struct cfi_private *cfi = map->fldrv_priv;
507 switch(chip->oldstate) {
509 chip->state = chip->oldstate;
510 map_write(map, CMD(0x30), chip->in_progress_block_addr);
511 chip->oldstate = FL_READY;
512 chip->state = FL_ERASING;
517 /* We should really make set_vpp() count, rather than doing this */
521 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
527 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
529 unsigned long cmd_addr;
530 struct cfi_private *cfi = map->fldrv_priv;
535 /* Ensure cmd read/writes are aligned. */
536 cmd_addr = adr & ~(map_bankwidth(map)-1);
538 cfi_spin_lock(chip->mutex);
539 ret = get_chip(map, chip, cmd_addr, FL_READY);
541 cfi_spin_unlock(chip->mutex);
545 if (chip->state != FL_POINT && chip->state != FL_READY) {
546 map_write(map, CMD(0xf0), cmd_addr);
547 chip->state = FL_READY;
550 map_copy_from(map, buf, adr, len);
552 put_chip(map, chip, cmd_addr);
554 cfi_spin_unlock(chip->mutex);
559 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
561 struct map_info *map = mtd->priv;
562 struct cfi_private *cfi = map->fldrv_priv;
567 /* ofs: offset within the first chip that the first read should start */
569 chipnum = (from >> cfi->chipshift);
570 ofs = from - (chipnum << cfi->chipshift);
576 unsigned long thislen;
578 if (chipnum >= cfi->numchips)
581 if ((len + ofs -1) >> cfi->chipshift)
582 thislen = (1<<cfi->chipshift) - ofs;
586 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
601 static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
603 DECLARE_WAITQUEUE(wait, current);
604 unsigned long timeo = jiffies + HZ;
605 struct cfi_private *cfi = map->fldrv_priv;
608 cfi_spin_lock(chip->mutex);
610 if (chip->state != FL_READY){
612 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
614 set_current_state(TASK_UNINTERRUPTIBLE);
615 add_wait_queue(&chip->wq, &wait);
617 cfi_spin_unlock(chip->mutex);
620 remove_wait_queue(&chip->wq, &wait);
622 if(signal_pending(current))
625 timeo = jiffies + HZ;
632 chip->state = FL_READY;
634 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
635 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
636 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
638 map_copy_from(map, buf, adr, len);
640 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
641 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
642 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
643 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
646 cfi_spin_unlock(chip->mutex);
651 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
653 struct map_info *map = mtd->priv;
654 struct cfi_private *cfi = map->fldrv_priv;
660 /* ofs: offset within the first chip that the first read should start */
662 /* 8 secsi bytes per chip */
670 unsigned long thislen;
672 if (chipnum >= cfi->numchips)
675 if ((len + ofs -1) >> 3)
676 thislen = (1<<3) - ofs;
680 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
695 static int do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
697 struct cfi_private *cfi = map->fldrv_priv;
698 unsigned long timeo = jiffies + HZ;
700 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
701 * have a max write time of a few hundreds usec). However, we should
702 * use the maximum timeout value given by the chip at probe time
703 * instead. Unfortunately, struct flchip does have a field for
704 * maximum timeout, only for typical which can be far too short
705 * depending of the conditions. The ' + 1' is to avoid having a
706 * timeout of 0 jiffies if HZ is smaller than 1000.
708 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
715 cfi_spin_lock(chip->mutex);
716 ret = get_chip(map, chip, adr, FL_WRITING);
718 cfi_spin_unlock(chip->mutex);
722 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
723 __func__, adr, datum.x[0] );
726 * Check for a NOP for the case when the datum to write is already
727 * present - it saves time and works around buggy chips that corrupt
728 * data at other locations when 0xff is written to a location that
729 * already contains 0xff.
731 oldd = map_read(map, adr);
732 if (map_word_equal(map, oldd, datum)) {
733 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
740 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
741 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
742 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
743 map_write(map, datum, adr);
744 chip->state = FL_WRITING;
746 cfi_spin_unlock(chip->mutex);
747 cfi_udelay(chip->word_write_time);
748 cfi_spin_lock(chip->mutex);
750 /* See comment above for timeout value. */
751 timeo = jiffies + uWriteTimeout;
753 if (chip->state != FL_WRITING) {
754 /* Someone's suspended the write. Sleep */
755 DECLARE_WAITQUEUE(wait, current);
757 set_current_state(TASK_UNINTERRUPTIBLE);
758 add_wait_queue(&chip->wq, &wait);
759 cfi_spin_unlock(chip->mutex);
761 remove_wait_queue(&chip->wq, &wait);
762 timeo = jiffies + (HZ / 2); /* FIXME */
763 cfi_spin_lock(chip->mutex);
767 if (chip_ready(map, adr))
770 if (time_after(jiffies, timeo))
773 /* Latency issues. Drop the lock, wait a while and retry */
774 cfi_spin_unlock(chip->mutex);
776 cfi_spin_lock(chip->mutex);
779 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
781 /* reset on all failures. */
782 map_write( map, CMD(0xF0), chip->start );
783 /* FIXME - should have reset delay before continuing */
784 if (++retry_cnt <= MAX_WORD_RETRIES)
789 chip->state = FL_READY;
790 put_chip(map, chip, adr);
791 cfi_spin_unlock(chip->mutex);
797 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
798 size_t *retlen, const u_char *buf)
800 struct map_info *map = mtd->priv;
801 struct cfi_private *cfi = map->fldrv_priv;
804 unsigned long ofs, chipstart;
805 DECLARE_WAITQUEUE(wait, current);
811 chipnum = to >> cfi->chipshift;
812 ofs = to - (chipnum << cfi->chipshift);
813 chipstart = cfi->chips[chipnum].start;
815 /* If it's not bus-aligned, do the first byte write */
816 if (ofs & (map_bankwidth(map)-1)) {
817 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
818 int i = ofs - bus_ofs;
823 cfi_spin_lock(cfi->chips[chipnum].mutex);
825 if (cfi->chips[chipnum].state != FL_READY) {
827 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
829 set_current_state(TASK_UNINTERRUPTIBLE);
830 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
832 cfi_spin_unlock(cfi->chips[chipnum].mutex);
835 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
837 if(signal_pending(current))
843 /* Load 'tmp_buf' with old contents of flash */
844 tmp_buf = map_read(map, bus_ofs+chipstart);
846 cfi_spin_unlock(cfi->chips[chipnum].mutex);
848 /* Number of bytes to copy from buffer */
849 n = min_t(int, len, map_bankwidth(map)-i);
851 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
853 ret = do_write_oneword(map, &cfi->chips[chipnum],
863 if (ofs >> cfi->chipshift) {
866 if (chipnum == cfi->numchips)
871 /* We are now aligned, write as much as possible */
872 while(len >= map_bankwidth(map)) {
875 datum = map_word_load(map, buf);
877 ret = do_write_oneword(map, &cfi->chips[chipnum],
882 ofs += map_bankwidth(map);
883 buf += map_bankwidth(map);
884 (*retlen) += map_bankwidth(map);
885 len -= map_bankwidth(map);
887 if (ofs >> cfi->chipshift) {
890 if (chipnum == cfi->numchips)
892 chipstart = cfi->chips[chipnum].start;
896 /* Write the trailing bytes if any */
897 if (len & (map_bankwidth(map)-1)) {
901 cfi_spin_lock(cfi->chips[chipnum].mutex);
903 if (cfi->chips[chipnum].state != FL_READY) {
905 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
907 set_current_state(TASK_UNINTERRUPTIBLE);
908 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
910 cfi_spin_unlock(cfi->chips[chipnum].mutex);
913 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
915 if(signal_pending(current))
921 tmp_buf = map_read(map, ofs + chipstart);
923 cfi_spin_unlock(cfi->chips[chipnum].mutex);
925 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
927 ret = do_write_oneword(map, &cfi->chips[chipnum],
940 * FIXME: interleaved mode not tested, and probably not supported!
942 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
943 unsigned long adr, const u_char *buf, int len)
945 struct cfi_private *cfi = map->fldrv_priv;
946 unsigned long timeo = jiffies + HZ;
947 /* see comments in do_write_oneword() regarding uWriteTimeo. */
948 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
950 unsigned long cmd_adr;
957 cfi_spin_lock(chip->mutex);
958 ret = get_chip(map, chip, adr, FL_WRITING);
960 cfi_spin_unlock(chip->mutex);
964 datum = map_word_load(map, buf);
966 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
967 __func__, adr, datum.x[0] );
970 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
971 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
972 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
974 /* Write Buffer Load */
975 map_write(map, CMD(0x25), cmd_adr);
977 chip->state = FL_WRITING_TO_BUFFER;
979 /* Write length of data to come */
980 words = len / map_bankwidth(map);
981 map_write(map, CMD(words - 1), cmd_adr);
984 while(z < words * map_bankwidth(map)) {
985 datum = map_word_load(map, buf);
986 map_write(map, datum, adr + z);
988 z += map_bankwidth(map);
989 buf += map_bankwidth(map);
991 z -= map_bankwidth(map);
995 /* Write Buffer Program Confirm: GO GO GO */
996 map_write(map, CMD(0x29), cmd_adr);
997 chip->state = FL_WRITING;
999 cfi_spin_unlock(chip->mutex);
1000 cfi_udelay(chip->buffer_write_time);
1001 cfi_spin_lock(chip->mutex);
1003 timeo = jiffies + uWriteTimeout;
1006 if (chip->state != FL_WRITING) {
1007 /* Someone's suspended the write. Sleep */
1008 DECLARE_WAITQUEUE(wait, current);
1010 set_current_state(TASK_UNINTERRUPTIBLE);
1011 add_wait_queue(&chip->wq, &wait);
1012 cfi_spin_unlock(chip->mutex);
1014 remove_wait_queue(&chip->wq, &wait);
1015 timeo = jiffies + (HZ / 2); /* FIXME */
1016 cfi_spin_lock(chip->mutex);
1020 if (chip_ready(map, adr))
1023 if( time_after(jiffies, timeo))
1026 /* Latency issues. Drop the lock, wait a while and retry */
1027 cfi_spin_unlock(chip->mutex);
1029 cfi_spin_lock(chip->mutex);
1032 printk(KERN_WARNING "MTD %s(): software timeout\n",
1035 /* reset on all failures. */
1036 map_write( map, CMD(0xF0), chip->start );
1037 /* FIXME - should have reset delay before continuing */
1041 chip->state = FL_READY;
1042 put_chip(map, chip, adr);
1043 cfi_spin_unlock(chip->mutex);
1049 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1050 size_t *retlen, const u_char *buf)
1052 struct map_info *map = mtd->priv;
1053 struct cfi_private *cfi = map->fldrv_priv;
1054 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1063 chipnum = to >> cfi->chipshift;
1064 ofs = to - (chipnum << cfi->chipshift);
1066 /* If it's not bus-aligned, do the first word write */
1067 if (ofs & (map_bankwidth(map)-1)) {
1068 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1069 if (local_len > len)
1071 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1072 local_len, retlen, buf);
1079 if (ofs >> cfi->chipshift) {
1082 if (chipnum == cfi->numchips)
1087 /* Write buffer is worth it only if more than one word to write... */
1088 while (len >= map_bankwidth(map) * 2) {
1089 /* We must not cross write block boundaries */
1090 int size = wbufsize - (ofs & (wbufsize-1));
1094 if (size % map_bankwidth(map))
1095 size -= size % map_bankwidth(map);
1097 ret = do_write_buffer(map, &cfi->chips[chipnum],
1107 if (ofs >> cfi->chipshift) {
1110 if (chipnum == cfi->numchips)
1116 size_t retlen_dregs = 0;
1118 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1119 len, &retlen_dregs, buf);
1121 *retlen += retlen_dregs;
1130 * Handle devices with one erase region, that only implement
1131 * the chip erase command.
1133 static inline int do_erase_chip(struct map_info *map, struct flchip *chip)
1135 struct cfi_private *cfi = map->fldrv_priv;
1136 unsigned long timeo = jiffies + HZ;
1137 unsigned long int adr;
1138 DECLARE_WAITQUEUE(wait, current);
1141 adr = cfi->addr_unlock1;
1143 cfi_spin_lock(chip->mutex);
1144 ret = get_chip(map, chip, adr, FL_WRITING);
1146 cfi_spin_unlock(chip->mutex);
1150 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1151 __func__, chip->start );
1154 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1155 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1156 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1157 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1158 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1159 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1161 chip->state = FL_ERASING;
1162 chip->erase_suspended = 0;
1163 chip->in_progress_block_addr = adr;
1165 cfi_spin_unlock(chip->mutex);
1166 msleep(chip->erase_time/2);
1167 cfi_spin_lock(chip->mutex);
1169 timeo = jiffies + (HZ*20);
1172 if (chip->state != FL_ERASING) {
1173 /* Someone's suspended the erase. Sleep */
1174 set_current_state(TASK_UNINTERRUPTIBLE);
1175 add_wait_queue(&chip->wq, &wait);
1176 cfi_spin_unlock(chip->mutex);
1178 remove_wait_queue(&chip->wq, &wait);
1179 cfi_spin_lock(chip->mutex);
1182 if (chip->erase_suspended) {
1183 /* This erase was suspended and resumed.
1184 Adjust the timeout */
1185 timeo = jiffies + (HZ*20); /* FIXME */
1186 chip->erase_suspended = 0;
1189 if (chip_ready(map, adr))
1192 if (time_after(jiffies, timeo))
1195 /* Latency issues. Drop the lock, wait a while and retry */
1196 cfi_spin_unlock(chip->mutex);
1197 set_current_state(TASK_UNINTERRUPTIBLE);
1198 schedule_timeout(1);
1199 cfi_spin_lock(chip->mutex);
1202 printk(KERN_WARNING "MTD %s(): software timeout\n",
1205 /* reset on all failures. */
1206 map_write( map, CMD(0xF0), chip->start );
1207 /* FIXME - should have reset delay before continuing */
1211 chip->state = FL_READY;
1212 put_chip(map, chip, adr);
1213 cfi_spin_unlock(chip->mutex);
1219 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
1221 struct cfi_private *cfi = map->fldrv_priv;
1222 unsigned long timeo = jiffies + HZ;
1223 DECLARE_WAITQUEUE(wait, current);
1228 cfi_spin_lock(chip->mutex);
1229 ret = get_chip(map, chip, adr, FL_ERASING);
1231 cfi_spin_unlock(chip->mutex);
1235 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1239 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1240 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1241 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1242 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1243 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1244 map_write(map, CMD(0x30), adr);
1246 chip->state = FL_ERASING;
1247 chip->erase_suspended = 0;
1248 chip->in_progress_block_addr = adr;
1250 cfi_spin_unlock(chip->mutex);
1251 msleep(chip->erase_time/2);
1252 cfi_spin_lock(chip->mutex);
1254 timeo = jiffies + (HZ*20);
1257 if (chip->state != FL_ERASING) {
1258 /* Someone's suspended the erase. Sleep */
1259 set_current_state(TASK_UNINTERRUPTIBLE);
1260 add_wait_queue(&chip->wq, &wait);
1261 cfi_spin_unlock(chip->mutex);
1263 remove_wait_queue(&chip->wq, &wait);
1264 cfi_spin_lock(chip->mutex);
1267 if (chip->erase_suspended) {
1268 /* This erase was suspended and resumed.
1269 Adjust the timeout */
1270 timeo = jiffies + (HZ*20); /* FIXME */
1271 chip->erase_suspended = 0;
1274 if (chip_ready(map, adr))
1277 if (time_after(jiffies, timeo))
1280 /* Latency issues. Drop the lock, wait a while and retry */
1281 cfi_spin_unlock(chip->mutex);
1282 set_current_state(TASK_UNINTERRUPTIBLE);
1283 schedule_timeout(1);
1284 cfi_spin_lock(chip->mutex);
1287 printk(KERN_WARNING "MTD %s(): software timeout\n",
1290 /* reset on all failures. */
1291 map_write( map, CMD(0xF0), chip->start );
1292 /* FIXME - should have reset delay before continuing */
1296 chip->state = FL_READY;
1297 put_chip(map, chip, adr);
1298 cfi_spin_unlock(chip->mutex);
1303 int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1305 unsigned long ofs, len;
1311 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1315 instr->state = MTD_ERASE_DONE;
1316 mtd_erase_callback(instr);
1322 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1324 struct map_info *map = mtd->priv;
1325 struct cfi_private *cfi = map->fldrv_priv;
1328 if (instr->addr != 0)
1331 if (instr->len != mtd->size)
1334 ret = do_erase_chip(map, &cfi->chips[0]);
1338 instr->state = MTD_ERASE_DONE;
1339 mtd_erase_callback(instr);
1345 static void cfi_amdstd_sync (struct mtd_info *mtd)
1347 struct map_info *map = mtd->priv;
1348 struct cfi_private *cfi = map->fldrv_priv;
1350 struct flchip *chip;
1352 DECLARE_WAITQUEUE(wait, current);
1354 for (i=0; !ret && i<cfi->numchips; i++) {
1355 chip = &cfi->chips[i];
1358 cfi_spin_lock(chip->mutex);
1360 switch(chip->state) {
1364 case FL_JEDEC_QUERY:
1365 chip->oldstate = chip->state;
1366 chip->state = FL_SYNCING;
1367 /* No need to wake_up() on this state change -
1368 * as the whole point is that nobody can do anything
1369 * with the chip now anyway.
1372 cfi_spin_unlock(chip->mutex);
1376 /* Not an idle state */
1377 add_wait_queue(&chip->wq, &wait);
1379 cfi_spin_unlock(chip->mutex);
1383 remove_wait_queue(&chip->wq, &wait);
1389 /* Unlock the chips again */
1391 for (i--; i >=0; i--) {
1392 chip = &cfi->chips[i];
1394 cfi_spin_lock(chip->mutex);
1396 if (chip->state == FL_SYNCING) {
1397 chip->state = chip->oldstate;
1400 cfi_spin_unlock(chip->mutex);
1405 static int cfi_amdstd_suspend(struct mtd_info *mtd)
1407 struct map_info *map = mtd->priv;
1408 struct cfi_private *cfi = map->fldrv_priv;
1410 struct flchip *chip;
1413 for (i=0; !ret && i<cfi->numchips; i++) {
1414 chip = &cfi->chips[i];
1416 cfi_spin_lock(chip->mutex);
1418 switch(chip->state) {
1422 case FL_JEDEC_QUERY:
1423 chip->oldstate = chip->state;
1424 chip->state = FL_PM_SUSPENDED;
1425 /* No need to wake_up() on this state change -
1426 * as the whole point is that nobody can do anything
1427 * with the chip now anyway.
1429 case FL_PM_SUSPENDED:
1436 cfi_spin_unlock(chip->mutex);
1439 /* Unlock the chips again */
1442 for (i--; i >=0; i--) {
1443 chip = &cfi->chips[i];
1445 cfi_spin_lock(chip->mutex);
1447 if (chip->state == FL_PM_SUSPENDED) {
1448 chip->state = chip->oldstate;
1451 cfi_spin_unlock(chip->mutex);
1459 static void cfi_amdstd_resume(struct mtd_info *mtd)
1461 struct map_info *map = mtd->priv;
1462 struct cfi_private *cfi = map->fldrv_priv;
1464 struct flchip *chip;
1466 for (i=0; i<cfi->numchips; i++) {
1468 chip = &cfi->chips[i];
1470 cfi_spin_lock(chip->mutex);
1472 if (chip->state == FL_PM_SUSPENDED) {
1473 chip->state = FL_READY;
1474 map_write(map, CMD(0xF0), chip->start);
1478 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1480 cfi_spin_unlock(chip->mutex);
1484 static void cfi_amdstd_destroy(struct mtd_info *mtd)
1486 struct map_info *map = mtd->priv;
1487 struct cfi_private *cfi = map->fldrv_priv;
1488 kfree(cfi->cmdset_priv);
1491 kfree(mtd->eraseregions);
1494 static char im_name[]="cfi_cmdset_0002";
1497 static int __init cfi_amdstd_init(void)
1499 inter_module_register(im_name, THIS_MODULE, &cfi_cmdset_0002);
1504 static void __exit cfi_amdstd_exit(void)
1506 inter_module_unregister(im_name);
1510 module_init(cfi_amdstd_init);
1511 module_exit(cfi_amdstd_exit);
1513 MODULE_LICENSE("GPL");
1514 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1515 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");