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.111 2004/11/16 18:29:00 dwmw2 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 /* Test to see if toggling has stopped. */
768 oldd = map_read(map, adr);
769 curd = map_read(map, adr);
770 if (map_word_equal(map, curd, oldd)) {
771 /* Do we have the correct value? */
772 if (map_word_equal(map, curd, datum)) {
775 /* Nope something has gone wrong. */
779 if (time_after(jiffies, timeo)) {
780 printk(KERN_WARNING "MTD %s(): software timeout\n",
785 /* Latency issues. Drop the lock, wait a while and retry */
786 cfi_spin_unlock(chip->mutex);
788 cfi_spin_lock(chip->mutex);
791 /* reset on all failures. */
792 map_write( map, CMD(0xF0), chip->start );
793 /* FIXME - should have reset delay before continuing */
794 if (++retry_cnt <= MAX_WORD_RETRIES)
799 chip->state = FL_READY;
800 put_chip(map, chip, adr);
801 cfi_spin_unlock(chip->mutex);
807 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
808 size_t *retlen, const u_char *buf)
810 struct map_info *map = mtd->priv;
811 struct cfi_private *cfi = map->fldrv_priv;
814 unsigned long ofs, chipstart;
815 DECLARE_WAITQUEUE(wait, current);
821 chipnum = to >> cfi->chipshift;
822 ofs = to - (chipnum << cfi->chipshift);
823 chipstart = cfi->chips[chipnum].start;
825 /* If it's not bus-aligned, do the first byte write */
826 if (ofs & (map_bankwidth(map)-1)) {
827 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
828 int i = ofs - bus_ofs;
833 cfi_spin_lock(cfi->chips[chipnum].mutex);
835 if (cfi->chips[chipnum].state != FL_READY) {
837 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
839 set_current_state(TASK_UNINTERRUPTIBLE);
840 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
842 cfi_spin_unlock(cfi->chips[chipnum].mutex);
845 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
847 if(signal_pending(current))
853 /* Load 'tmp_buf' with old contents of flash */
854 tmp_buf = map_read(map, bus_ofs+chipstart);
856 cfi_spin_unlock(cfi->chips[chipnum].mutex);
858 /* Number of bytes to copy from buffer */
859 n = min_t(int, len, map_bankwidth(map)-i);
861 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
863 ret = do_write_oneword(map, &cfi->chips[chipnum],
873 if (ofs >> cfi->chipshift) {
876 if (chipnum == cfi->numchips)
881 /* We are now aligned, write as much as possible */
882 while(len >= map_bankwidth(map)) {
885 datum = map_word_load(map, buf);
887 ret = do_write_oneword(map, &cfi->chips[chipnum],
892 ofs += map_bankwidth(map);
893 buf += map_bankwidth(map);
894 (*retlen) += map_bankwidth(map);
895 len -= map_bankwidth(map);
897 if (ofs >> cfi->chipshift) {
900 if (chipnum == cfi->numchips)
902 chipstart = cfi->chips[chipnum].start;
906 /* Write the trailing bytes if any */
907 if (len & (map_bankwidth(map)-1)) {
911 cfi_spin_lock(cfi->chips[chipnum].mutex);
913 if (cfi->chips[chipnum].state != FL_READY) {
915 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
917 set_current_state(TASK_UNINTERRUPTIBLE);
918 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
920 cfi_spin_unlock(cfi->chips[chipnum].mutex);
923 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
925 if(signal_pending(current))
931 tmp_buf = map_read(map, ofs + chipstart);
933 cfi_spin_unlock(cfi->chips[chipnum].mutex);
935 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
937 ret = do_write_oneword(map, &cfi->chips[chipnum],
950 * FIXME: interleaved mode not tested, and probably not supported!
952 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
953 unsigned long adr, const u_char *buf, int len)
955 struct cfi_private *cfi = map->fldrv_priv;
956 unsigned long timeo = jiffies + HZ;
957 /* see comments in do_write_oneword() regarding uWriteTimeo. */
958 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
960 unsigned long cmd_adr;
967 cfi_spin_lock(chip->mutex);
968 ret = get_chip(map, chip, adr, FL_WRITING);
970 cfi_spin_unlock(chip->mutex);
974 datum = map_word_load(map, buf);
976 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
977 __func__, adr, datum.x[0] );
980 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
981 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
982 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
984 /* Write Buffer Load */
985 map_write(map, CMD(0x25), cmd_adr);
987 chip->state = FL_WRITING_TO_BUFFER;
989 /* Write length of data to come */
990 words = len / map_bankwidth(map);
991 map_write(map, CMD(words - 1), cmd_adr);
994 while(z < words * map_bankwidth(map)) {
995 datum = map_word_load(map, buf);
996 map_write(map, datum, adr + z);
998 z += map_bankwidth(map);
999 buf += map_bankwidth(map);
1001 z -= map_bankwidth(map);
1005 /* Write Buffer Program Confirm: GO GO GO */
1006 map_write(map, CMD(0x29), cmd_adr);
1007 chip->state = FL_WRITING;
1009 cfi_spin_unlock(chip->mutex);
1010 cfi_udelay(chip->buffer_write_time);
1011 cfi_spin_lock(chip->mutex);
1013 timeo = jiffies + uWriteTimeout;
1016 if (chip->state != FL_WRITING) {
1017 /* Someone's suspended the write. Sleep */
1018 DECLARE_WAITQUEUE(wait, current);
1020 set_current_state(TASK_UNINTERRUPTIBLE);
1021 add_wait_queue(&chip->wq, &wait);
1022 cfi_spin_unlock(chip->mutex);
1024 remove_wait_queue(&chip->wq, &wait);
1025 timeo = jiffies + (HZ / 2); /* FIXME */
1026 cfi_spin_lock(chip->mutex);
1030 if (chip_ready(map, adr))
1033 if( time_after(jiffies, timeo))
1036 /* Latency issues. Drop the lock, wait a while and retry */
1037 cfi_spin_unlock(chip->mutex);
1039 cfi_spin_lock(chip->mutex);
1042 printk(KERN_WARNING "MTD %s(): software timeout\n",
1045 /* reset on all failures. */
1046 map_write( map, CMD(0xF0), chip->start );
1047 /* FIXME - should have reset delay before continuing */
1051 chip->state = FL_READY;
1052 put_chip(map, chip, adr);
1053 cfi_spin_unlock(chip->mutex);
1059 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1060 size_t *retlen, const u_char *buf)
1062 struct map_info *map = mtd->priv;
1063 struct cfi_private *cfi = map->fldrv_priv;
1064 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1073 chipnum = to >> cfi->chipshift;
1074 ofs = to - (chipnum << cfi->chipshift);
1076 /* If it's not bus-aligned, do the first word write */
1077 if (ofs & (map_bankwidth(map)-1)) {
1078 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1079 if (local_len > len)
1081 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1082 local_len, retlen, buf);
1089 if (ofs >> cfi->chipshift) {
1092 if (chipnum == cfi->numchips)
1097 /* Write buffer is worth it only if more than one word to write... */
1098 while (len >= map_bankwidth(map) * 2) {
1099 /* We must not cross write block boundaries */
1100 int size = wbufsize - (ofs & (wbufsize-1));
1104 if (size % map_bankwidth(map))
1105 size -= size % map_bankwidth(map);
1107 ret = do_write_buffer(map, &cfi->chips[chipnum],
1117 if (ofs >> cfi->chipshift) {
1120 if (chipnum == cfi->numchips)
1126 size_t retlen_dregs = 0;
1128 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1129 len, &retlen_dregs, buf);
1131 *retlen += retlen_dregs;
1140 * Handle devices with one erase region, that only implement
1141 * the chip erase command.
1143 static inline int do_erase_chip(struct map_info *map, struct flchip *chip)
1145 struct cfi_private *cfi = map->fldrv_priv;
1146 unsigned long timeo = jiffies + HZ;
1147 unsigned long int adr;
1148 DECLARE_WAITQUEUE(wait, current);
1151 adr = cfi->addr_unlock1;
1153 cfi_spin_lock(chip->mutex);
1154 ret = get_chip(map, chip, adr, FL_WRITING);
1156 cfi_spin_unlock(chip->mutex);
1160 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1161 __func__, chip->start );
1164 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1165 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1166 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1167 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1168 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1169 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1171 chip->state = FL_ERASING;
1172 chip->erase_suspended = 0;
1173 chip->in_progress_block_addr = adr;
1175 cfi_spin_unlock(chip->mutex);
1176 set_current_state(TASK_UNINTERRUPTIBLE);
1177 schedule_timeout((chip->erase_time*HZ)/(2*1000));
1178 cfi_spin_lock(chip->mutex);
1180 timeo = jiffies + (HZ*20);
1183 if (chip->state != FL_ERASING) {
1184 /* Someone's suspended the erase. Sleep */
1185 set_current_state(TASK_UNINTERRUPTIBLE);
1186 add_wait_queue(&chip->wq, &wait);
1187 cfi_spin_unlock(chip->mutex);
1189 remove_wait_queue(&chip->wq, &wait);
1190 cfi_spin_lock(chip->mutex);
1193 if (chip->erase_suspended) {
1194 /* This erase was suspended and resumed.
1195 Adjust the timeout */
1196 timeo = jiffies + (HZ*20); /* FIXME */
1197 chip->erase_suspended = 0;
1200 if (chip_ready(map, adr))
1203 if (time_after(jiffies, timeo))
1206 /* Latency issues. Drop the lock, wait a while and retry */
1207 cfi_spin_unlock(chip->mutex);
1208 set_current_state(TASK_UNINTERRUPTIBLE);
1209 schedule_timeout(1);
1210 cfi_spin_lock(chip->mutex);
1213 printk(KERN_WARNING "MTD %s(): software timeout\n",
1216 /* reset on all failures. */
1217 map_write( map, CMD(0xF0), chip->start );
1218 /* FIXME - should have reset delay before continuing */
1222 chip->state = FL_READY;
1223 put_chip(map, chip, adr);
1224 cfi_spin_unlock(chip->mutex);
1230 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
1232 struct cfi_private *cfi = map->fldrv_priv;
1233 unsigned long timeo = jiffies + HZ;
1234 DECLARE_WAITQUEUE(wait, current);
1239 cfi_spin_lock(chip->mutex);
1240 ret = get_chip(map, chip, adr, FL_ERASING);
1242 cfi_spin_unlock(chip->mutex);
1246 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1250 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1251 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1252 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1253 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1254 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1255 map_write(map, CMD(0x30), adr);
1257 chip->state = FL_ERASING;
1258 chip->erase_suspended = 0;
1259 chip->in_progress_block_addr = adr;
1261 cfi_spin_unlock(chip->mutex);
1262 set_current_state(TASK_UNINTERRUPTIBLE);
1263 schedule_timeout((chip->erase_time*HZ)/(2*1000));
1264 cfi_spin_lock(chip->mutex);
1266 timeo = jiffies + (HZ*20);
1269 if (chip->state != FL_ERASING) {
1270 /* Someone's suspended the erase. Sleep */
1271 set_current_state(TASK_UNINTERRUPTIBLE);
1272 add_wait_queue(&chip->wq, &wait);
1273 cfi_spin_unlock(chip->mutex);
1275 remove_wait_queue(&chip->wq, &wait);
1276 cfi_spin_lock(chip->mutex);
1279 if (chip->erase_suspended) {
1280 /* This erase was suspended and resumed.
1281 Adjust the timeout */
1282 timeo = jiffies + (HZ*20); /* FIXME */
1283 chip->erase_suspended = 0;
1286 if (chip_ready(map, adr))
1289 if (time_after(jiffies, timeo))
1292 /* Latency issues. Drop the lock, wait a while and retry */
1293 cfi_spin_unlock(chip->mutex);
1294 set_current_state(TASK_UNINTERRUPTIBLE);
1295 schedule_timeout(1);
1296 cfi_spin_lock(chip->mutex);
1299 printk(KERN_WARNING "MTD %s(): software timeout\n",
1302 /* reset on all failures. */
1303 map_write( map, CMD(0xF0), chip->start );
1304 /* FIXME - should have reset delay before continuing */
1308 chip->state = FL_READY;
1309 put_chip(map, chip, adr);
1310 cfi_spin_unlock(chip->mutex);
1315 int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1317 unsigned long ofs, len;
1323 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1327 instr->state = MTD_ERASE_DONE;
1328 mtd_erase_callback(instr);
1334 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1336 struct map_info *map = mtd->priv;
1337 struct cfi_private *cfi = map->fldrv_priv;
1340 if (instr->addr != 0)
1343 if (instr->len != mtd->size)
1346 ret = do_erase_chip(map, &cfi->chips[0]);
1350 instr->state = MTD_ERASE_DONE;
1351 mtd_erase_callback(instr);
1357 static void cfi_amdstd_sync (struct mtd_info *mtd)
1359 struct map_info *map = mtd->priv;
1360 struct cfi_private *cfi = map->fldrv_priv;
1362 struct flchip *chip;
1364 DECLARE_WAITQUEUE(wait, current);
1366 for (i=0; !ret && i<cfi->numchips; i++) {
1367 chip = &cfi->chips[i];
1370 cfi_spin_lock(chip->mutex);
1372 switch(chip->state) {
1376 case FL_JEDEC_QUERY:
1377 chip->oldstate = chip->state;
1378 chip->state = FL_SYNCING;
1379 /* No need to wake_up() on this state change -
1380 * as the whole point is that nobody can do anything
1381 * with the chip now anyway.
1384 cfi_spin_unlock(chip->mutex);
1388 /* Not an idle state */
1389 add_wait_queue(&chip->wq, &wait);
1391 cfi_spin_unlock(chip->mutex);
1395 remove_wait_queue(&chip->wq, &wait);
1401 /* Unlock the chips again */
1403 for (i--; i >=0; i--) {
1404 chip = &cfi->chips[i];
1406 cfi_spin_lock(chip->mutex);
1408 if (chip->state == FL_SYNCING) {
1409 chip->state = chip->oldstate;
1412 cfi_spin_unlock(chip->mutex);
1417 static int cfi_amdstd_suspend(struct mtd_info *mtd)
1419 struct map_info *map = mtd->priv;
1420 struct cfi_private *cfi = map->fldrv_priv;
1422 struct flchip *chip;
1425 for (i=0; !ret && i<cfi->numchips; i++) {
1426 chip = &cfi->chips[i];
1428 cfi_spin_lock(chip->mutex);
1430 switch(chip->state) {
1434 case FL_JEDEC_QUERY:
1435 chip->oldstate = chip->state;
1436 chip->state = FL_PM_SUSPENDED;
1437 /* No need to wake_up() on this state change -
1438 * as the whole point is that nobody can do anything
1439 * with the chip now anyway.
1441 case FL_PM_SUSPENDED:
1448 cfi_spin_unlock(chip->mutex);
1451 /* Unlock the chips again */
1454 for (i--; i >=0; i--) {
1455 chip = &cfi->chips[i];
1457 cfi_spin_lock(chip->mutex);
1459 if (chip->state == FL_PM_SUSPENDED) {
1460 chip->state = chip->oldstate;
1463 cfi_spin_unlock(chip->mutex);
1471 static void cfi_amdstd_resume(struct mtd_info *mtd)
1473 struct map_info *map = mtd->priv;
1474 struct cfi_private *cfi = map->fldrv_priv;
1476 struct flchip *chip;
1478 for (i=0; i<cfi->numchips; i++) {
1480 chip = &cfi->chips[i];
1482 cfi_spin_lock(chip->mutex);
1484 if (chip->state == FL_PM_SUSPENDED) {
1485 chip->state = FL_READY;
1486 map_write(map, CMD(0xF0), chip->start);
1490 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1492 cfi_spin_unlock(chip->mutex);
1496 static void cfi_amdstd_destroy(struct mtd_info *mtd)
1498 struct map_info *map = mtd->priv;
1499 struct cfi_private *cfi = map->fldrv_priv;
1500 kfree(cfi->cmdset_priv);
1503 kfree(mtd->eraseregions);
1506 static char im_name[]="cfi_cmdset_0002";
1509 static int __init cfi_amdstd_init(void)
1511 inter_module_register(im_name, THIS_MODULE, &cfi_cmdset_0002);
1516 static void __exit cfi_amdstd_exit(void)
1518 inter_module_unregister(im_name);
1522 module_init(cfi_amdstd_init);
1523 module_exit(cfi_amdstd_exit);
1525 MODULE_LICENSE("GPL");
1526 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1527 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");