blob: b86197286f2406d4c419736a4252ab59e0403c11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common Flash Interface support:
3 * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
4 *
5 * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
6 * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
Todd Poynor02b15e32005-06-07 00:04:39 +01007 * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * 2_by_8 routines added by Simon Munton
10 *
11 * 4_by_16 work by Carolyn J. Smith
12 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +000013 * XIP support hooks by Vitaly Wool (based on code for Intel flash
Todd Poynor02b15e32005-06-07 00:04:39 +010014 * by Nicolas Pitre)
Thomas Gleixner1f948b42005-11-07 11:15:37 +000015 *
Christopher Moore87e92c02008-10-17 05:32:22 +020016 * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
19 *
20 * This code is GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/init.h>
28#include <asm/io.h>
29#include <asm/byteorder.h>
30
31#include <linux/errno.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070035#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mtd/map.h>
37#include <linux/mtd/mtd.h>
38#include <linux/mtd/cfi.h>
Todd Poynor02b15e32005-06-07 00:04:39 +010039#include <linux/mtd/xip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define AMD_BOOTLOC_BUG
42#define FORCE_WORD_WRITE 0
43
44#define MAX_WORD_RETRIES 3
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070047#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010048#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020049#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
52static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
55static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
56static void cfi_amdstd_sync (struct mtd_info *);
57static int cfi_amdstd_suspend (struct mtd_info *);
58static void cfi_amdstd_resume (struct mtd_info *);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070059static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
61
Ira W. Snyder30ec5a22012-01-06 11:29:19 -080062static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
63 size_t *retlen, const u_char *buf);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void cfi_amdstd_destroy(struct mtd_info *);
66
67struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
68static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
69
70static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
71static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
72#include "fwh_lock.h"
73
Adrian Hunter69423d92008-12-10 13:37:21 +000074static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
75static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +020076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct mtd_chip_driver cfi_amdstd_chipdrv = {
78 .probe = NULL, /* Not usable directly */
79 .destroy = cfi_amdstd_destroy,
80 .name = "cfi_cmdset_0002",
81 .module = THIS_MODULE
82};
83
84
85/* #define DEBUG_CFI_FEATURES */
86
87
88#ifdef DEBUG_CFI_FEATURES
89static void cfi_tell_features(struct cfi_pri_amdstd *extp)
90{
91 const char* erase_suspend[3] = {
92 "Not supported", "Read only", "Read/write"
93 };
94 const char* top_bottom[6] = {
95 "No WP", "8x8KiB sectors at top & bottom, no WP",
96 "Bottom boot", "Top boot",
97 "Uniform, Bottom WP", "Uniform, Top WP"
98 };
99
100 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000101 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 (extp->SiliconRevision & 1) ? "Not required" : "Required");
103
104 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
105 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
106 else
107 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
108
109 if (extp->BlkProt == 0)
110 printk(" Block protection: Not supported\n");
111 else
112 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
113
114
115 printk(" Temporary block unprotect: %s\n",
116 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
117 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
118 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
119 printk(" Burst mode: %s\n",
120 extp->BurstMode ? "Supported" : "Not supported");
121 if (extp->PageMode == 0)
122 printk(" Page mode: Not supported\n");
123 else
124 printk(" Page mode: %d word page\n", extp->PageMode << 2);
125
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000126 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000128 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 extp->VppMax >> 4, extp->VppMax & 0xf);
130
131 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
132 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
133 else
134 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
135}
136#endif
137
138#ifdef AMD_BOOTLOC_BUG
139/* Wheee. Bring me the head of someone at AMD. */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100140static void fixup_amd_bootblock(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct map_info *map = mtd->priv;
143 struct cfi_private *cfi = map->fldrv_priv;
144 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
145 __u8 major = extp->MajorVersion;
146 __u8 minor = extp->MinorVersion;
147
148 if (((major << 8) | minor) < 0x3131) {
149 /* CFI version 1.0 => don't trust bootloc */
Christopher Moore87e92c02008-10-17 05:32:22 +0200150
Brian Norris289c0522011-07-19 10:06:09 -0700151 pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
Christopher Moore87e92c02008-10-17 05:32:22 +0200152 map->name, cfi->mfr, cfi->id);
153
154 /* AFAICS all 29LV400 with a bottom boot block have a device ID
155 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
156 * These were badly detected as they have the 0x80 bit set
157 * so treat them as a special case.
158 */
159 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
160
161 /* Macronix added CFI to their 2nd generation
162 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
163 * Fujitsu, Spansion, EON, ESI and older Macronix)
164 * has CFI.
165 *
166 * Therefore also check the manufacturer.
167 * This reduces the risk of false detection due to
168 * the 8-bit device ID.
169 */
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100170 (cfi->mfr == CFI_MFR_MACRONIX)) {
Brian Norris289c0522011-07-19 10:06:09 -0700171 pr_debug("%s: Macronix MX29LV400C with bottom boot block"
Christopher Moore87e92c02008-10-17 05:32:22 +0200172 " detected\n", map->name);
173 extp->TopBottom = 2; /* bottom boot */
174 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (cfi->id & 0x80) {
176 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
177 extp->TopBottom = 3; /* top boot */
178 } else {
179 extp->TopBottom = 2; /* bottom boot */
180 }
Christopher Moore87e92c02008-10-17 05:32:22 +0200181
Brian Norris289c0522011-07-19 10:06:09 -0700182 pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
Christopher Moore87e92c02008-10-17 05:32:22 +0200183 " deduced %s from Device ID\n", map->name, major, minor,
184 extp->TopBottom == 2 ? "bottom" : "top");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186}
187#endif
188
Guillaume LECERFcc318222010-11-17 12:35:50 +0100189static void fixup_use_write_buffers(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct map_info *map = mtd->priv;
192 struct cfi_private *cfi = map->fldrv_priv;
193 if (cfi->cfiq->BufWriteTimeoutTyp) {
Brian Norris289c0522011-07-19 10:06:09 -0700194 pr_debug("Using buffer write method\n" );
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200195 mtd->_write = cfi_amdstd_write_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197}
198
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200199/* Atmel chips don't use the same PRI format as AMD chips */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100200static void fixup_convert_atmel_pri(struct mtd_info *mtd)
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200201{
202 struct map_info *map = mtd->priv;
203 struct cfi_private *cfi = map->fldrv_priv;
204 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
205 struct cfi_pri_atmel atmel_pri;
206
207 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200208 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200209
210 if (atmel_pri.Features & 0x02)
211 extp->EraseSuspend = 2;
212
Haavard Skinnemoenbe8f78b2008-09-30 13:55:33 +0200213 /* Some chips got it backwards... */
214 if (cfi->id == AT49BV6416) {
215 if (atmel_pri.BottomBoot)
216 extp->TopBottom = 3;
217 else
218 extp->TopBottom = 2;
219 } else {
220 if (atmel_pri.BottomBoot)
221 extp->TopBottom = 2;
222 else
223 extp->TopBottom = 3;
224 }
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100225
226 /* burst write mode not supported */
227 cfi->cfiq->BufWriteTimeoutTyp = 0;
228 cfi->cfiq->BufWriteTimeoutMax = 0;
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200229}
230
Guillaume LECERFcc318222010-11-17 12:35:50 +0100231static void fixup_use_secsi(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /* Setup for chips with a secsi area */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200234 mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
235 mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Guillaume LECERFcc318222010-11-17 12:35:50 +0100238static void fixup_use_erase_chip(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct map_info *map = mtd->priv;
241 struct cfi_private *cfi = map->fldrv_priv;
242 if ((cfi->cfiq->NumEraseRegions == 1) &&
243 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200244 mtd->_erase = cfi_amdstd_erase_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200249/*
250 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
251 * locked by default.
252 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100253static void fixup_use_atmel_lock(struct mtd_info *mtd)
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200254{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200255 mtd->_lock = cfi_atmel_lock;
256 mtd->_unlock = cfi_atmel_unlock;
Justin Treone619a752008-01-30 10:25:49 -0800257 mtd->flags |= MTD_POWERUP_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200258}
259
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200260static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
261{
262 struct map_info *map = mtd->priv;
263 struct cfi_private *cfi = map->fldrv_priv;
264
265 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300266 * These flashes report two separate eraseblock regions based on the
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200267 * sector_erase-size and block_erase-size, although they both operate on the
268 * same memory. This is not allowed according to CFI, so we just pick the
269 * sector_erase-size.
270 */
271 cfi->cfiq->NumEraseRegions = 1;
272}
273
Guillaume LECERFcc318222010-11-17 12:35:50 +0100274static void fixup_sst39vf(struct mtd_info *mtd)
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200275{
276 struct map_info *map = mtd->priv;
277 struct cfi_private *cfi = map->fldrv_priv;
278
279 fixup_old_sst_eraseregion(mtd);
280
281 cfi->addr_unlock1 = 0x5555;
282 cfi->addr_unlock2 = 0x2AAA;
283}
284
Guillaume LECERFcc318222010-11-17 12:35:50 +0100285static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200286{
287 struct map_info *map = mtd->priv;
288 struct cfi_private *cfi = map->fldrv_priv;
289
290 fixup_old_sst_eraseregion(mtd);
291
292 cfi->addr_unlock1 = 0x555;
293 cfi->addr_unlock2 = 0x2AA;
Guillaume LECERF08968042010-10-26 10:45:23 +0100294
295 cfi->sector_erase_cmd = CMD(0x50);
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200296}
297
Guillaume LECERFcc318222010-11-17 12:35:50 +0100298static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100299{
300 struct map_info *map = mtd->priv;
301 struct cfi_private *cfi = map->fldrv_priv;
302
Guillaume LECERFcc318222010-11-17 12:35:50 +0100303 fixup_sst39vf_rev_b(mtd);
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100304
305 /*
306 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
307 * it should report a size of 8KBytes (0x0020*256).
308 */
309 cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
310 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
311}
312
Guillaume LECERFcc318222010-11-17 12:35:50 +0100313static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700314{
315 struct map_info *map = mtd->priv;
316 struct cfi_private *cfi = map->fldrv_priv;
317
318 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
319 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
David Woodhouse5df41de2012-05-13 23:34:24 -0500320 pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
Trent Piepho70b07252008-03-30 21:19:30 -0700321 }
322}
323
Guillaume LECERFcc318222010-11-17 12:35:50 +0100324static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700325{
326 struct map_info *map = mtd->priv;
327 struct cfi_private *cfi = map->fldrv_priv;
328
329 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
330 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
David Woodhouse5df41de2012-05-13 23:34:24 -0500331 pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
Trent Piepho70b07252008-03-30 21:19:30 -0700332 }
333}
334
Javier Martin43dc03c2012-05-11 12:15:41 +0200335static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
336{
337 struct map_info *map = mtd->priv;
338 struct cfi_private *cfi = map->fldrv_priv;
339
340 /*
341 * S29NS512P flash uses more than 8bits to report number of sectors,
342 * which is not permitted by CFI.
343 */
344 cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
David Woodhouse5df41de2012-05-13 23:34:24 -0500345 pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
Javier Martin43dc03c2012-05-11 12:15:41 +0200346}
347
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200348/* Used to fix CFI-Tables of chips without Extended Query Tables */
349static struct cfi_fixup cfi_nopri_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100350 { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
351 { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
352 { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
353 { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
354 { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
355 { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
356 { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
357 { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
358 { 0, 0, NULL }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200359};
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static struct cfi_fixup cfi_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100362 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#ifdef AMD_BOOTLOC_BUG
Guillaume LECERFcc318222010-11-17 12:35:50 +0100364 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
Steffen Sledz1065cda2011-03-10 09:05:12 +0100365 { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
Guillaume LECERFcc318222010-11-17 12:35:50 +0100366 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100368 { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
369 { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
370 { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
371 { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
372 { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
373 { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
374 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
375 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
376 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
377 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
Javier Martin43dc03c2012-05-11 12:15:41 +0200378 { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
Guillaume LECERFcc318222010-11-17 12:35:50 +0100379 { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
380 { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
381 { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
382 { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#if !FORCE_WORD_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100384 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100386 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100389 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
390 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
391 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
392 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393};
394
395static struct cfi_fixup fixup_table[] = {
396 /* The CFI vendor ids and the JEDEC vendor IDs appear
397 * to be common. It is like the devices id's are as
398 * well. This table is to pick all cases where
399 * we know that is the case.
400 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100401 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
402 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
403 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100407static void cfi_fixup_major_minor(struct cfi_private *cfi,
408 struct cfi_pri_amdstd *extp)
409{
Guillaume LECERFe6372762010-12-17 10:59:41 +0100410 if (cfi->mfr == CFI_MFR_SAMSUNG) {
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100411 if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
412 (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
Guillaume LECERFe6372762010-12-17 10:59:41 +0100413 /*
414 * Samsung K8P2815UQB and K8D6x16UxM chips
415 * report major=0 / minor=0.
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100416 * K8D3x16UxC chips report major=3 / minor=3.
Guillaume LECERFe6372762010-12-17 10:59:41 +0100417 */
418 printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
419 " Extended Query version to 1.%c\n",
420 extp->MinorVersion);
421 extp->MajorVersion = '1';
422 }
423 }
424
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100425 /*
426 * SST 38VF640x chips report major=0xFF / minor=0xFF.
427 */
428 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
429 extp->MajorVersion = '1';
430 extp->MinorVersion = '0';
431 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100432}
433
Gerlando Falauto42096282012-07-03 09:09:47 +0200434static int is_m29ew(struct cfi_private *cfi)
435{
436 if (cfi->mfr == CFI_MFR_INTEL &&
437 ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
438 (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
439 return 1;
440 return 0;
441}
442
443/*
444 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
445 * Some revisions of the M29EW suffer from erase suspend hang ups. In
446 * particular, it can occur when the sequence
447 * Erase Confirm -> Suspend -> Program -> Resume
448 * causes a lockup due to internal timing issues. The consequence is that the
449 * erase cannot be resumed without inserting a dummy command after programming
450 * and prior to resuming. [...] The work-around is to issue a dummy write cycle
451 * that writes an F0 command code before the RESUME command.
452 */
453static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
454 unsigned long adr)
455{
456 struct cfi_private *cfi = map->fldrv_priv;
457 /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
458 if (is_m29ew(cfi))
459 map_write(map, CMD(0xF0), adr);
460}
461
462/*
463 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
464 *
465 * Some revisions of the M29EW (for example, A1 and A2 step revisions)
466 * are affected by a problem that could cause a hang up when an ERASE SUSPEND
467 * command is issued after an ERASE RESUME operation without waiting for a
468 * minimum delay. The result is that once the ERASE seems to be completed
469 * (no bits are toggling), the contents of the Flash memory block on which
470 * the erase was ongoing could be inconsistent with the expected values
471 * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
472 * values), causing a consequent failure of the ERASE operation.
473 * The occurrence of this issue could be high, especially when file system
474 * operations on the Flash are intensive. As a result, it is recommended
475 * that a patch be applied. Intensive file system operations can cause many
476 * calls to the garbage routine to free Flash space (also by erasing physical
477 * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
478 * commands can occur. The problem disappears when a delay is inserted after
479 * the RESUME command by using the udelay() function available in Linux.
480 * The DELAY value must be tuned based on the customer's platform.
481 * The maximum value that fixes the problem in all cases is 500us.
482 * But, in our experience, a delay of 30 µs to 50 µs is sufficient
483 * in most cases.
484 * We have chosen 500µs because this latency is acceptable.
485 */
486static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
487{
488 /*
489 * Resolving the Delay After Resume Issue see Micron TN-13-07
490 * Worst case delay must be 500µs but 30-50µs should be ok as well
491 */
492 if (is_m29ew(cfi))
493 cfi_udelay(500);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
497{
498 struct cfi_private *cfi = map->fldrv_priv;
499 struct mtd_info *mtd;
500 int i;
501
Burman Yan95b93a02006-11-15 21:10:29 +0200502 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!mtd) {
504 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
505 return NULL;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 mtd->priv = map;
508 mtd->type = MTD_NORFLASH;
509
510 /* Fill in the default mtd operations */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200511 mtd->_erase = cfi_amdstd_erase_varsize;
512 mtd->_write = cfi_amdstd_write_words;
513 mtd->_read = cfi_amdstd_read;
514 mtd->_sync = cfi_amdstd_sync;
515 mtd->_suspend = cfi_amdstd_suspend;
516 mtd->_resume = cfi_amdstd_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mtd->flags = MTD_CAP_NORFLASH;
518 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400519 mtd->writesize = 1;
Anatolij Gustschin13ce77f2011-02-10 16:01:46 +0100520 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
Anatolij Gustschind261c722010-12-16 23:42:15 +0100521
Brian Norris0a32a102011-07-19 10:06:10 -0700522 pr_debug("MTD %s(): write buffer size %d\n", __func__,
523 mtd->writebufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200525 mtd->_panic_write = cfi_amdstd_panic_write;
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700526 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (cfi->cfi_mode==CFI_MODE_CFI){
529 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
531 struct cfi_pri_amdstd *extp;
532
533 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200534 if (extp) {
535 /*
536 * It's a real CFI chip, not one for which the probe
537 * routine faked a CFI structure.
538 */
539 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200541 /*
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200542 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200543 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
544 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
Guillaume LECERF5da19532010-08-05 13:55:24 +0200545 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200546 * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200547 */
Guillaume LECERF564b8492010-04-24 17:58:17 +0200548 if (extp->MajorVersion != '1' ||
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200549 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
Guillaume LECERF564b8492010-04-24 17:58:17 +0200550 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200551 "version %c.%c (%#02x/%#02x).\n",
552 extp->MajorVersion, extp->MinorVersion,
553 extp->MajorVersion, extp->MinorVersion);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200554 kfree(extp);
555 kfree(mtd);
556 return NULL;
557 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100558
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200559 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
560 extp->MajorVersion, extp->MinorVersion);
561
Guillaume LECERF564b8492010-04-24 17:58:17 +0200562 /* Install our own private info structure */
563 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100564
Guillaume LECERF564b8492010-04-24 17:58:17 +0200565 /* Apply cfi device specific fixups */
566 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200569 /* Tell the user about it in lots of lovely detail */
570 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Guillaume LECERF564b8492010-04-24 17:58:17 +0200573 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100574 if ((bootloc < 2) || (bootloc > 5)) {
575 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
576 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100577 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200578 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200580
581 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100582 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200583
584 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
585 int j = (cfi->cfiq->NumEraseRegions-1)-i;
586 __u32 swap;
587
588 swap = cfi->cfiq->EraseRegionInfo[i];
589 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
590 cfi->cfiq->EraseRegionInfo[j] = swap;
591 }
592 }
593 /* Set the default CFI lock/unlock addresses */
594 cfi->addr_unlock1 = 0x555;
595 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200597 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200598
599 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
600 kfree(mtd);
601 return NULL;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 } /* CFI mode */
605 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
606 /* Apply jedec specific fixups */
607 cfi_fixup(mtd, jedec_fixup_table);
608 }
609 /* Apply generic fixups */
610 cfi_fixup(mtd, fixup_table);
611
612 for (i=0; i< cfi->numchips; i++) {
613 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
614 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
615 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800616 cfi->chips[i].ref_point_counter = 0;
617 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000618 }
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return cfi_amdstd_setup(mtd);
623}
Guillaume LECERF80461122010-05-20 16:54:10 +0200624struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200625struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100626EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200627EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200628EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
631{
632 struct map_info *map = mtd->priv;
633 struct cfi_private *cfi = map->fldrv_priv;
634 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
635 unsigned long offset = 0;
636 int i,j;
637
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000638 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000640 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 mtd->size = devsize * cfi->numchips;
642
643 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
644 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
645 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000646 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
648 goto setup_err;
649 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
652 unsigned long ernum, ersize;
653 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
654 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (mtd->erasesize < ersize) {
657 mtd->erasesize = ersize;
658 }
659 for (j=0; j<cfi->numchips; j++) {
660 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
661 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
662 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
663 }
664 offset += (ersize * ernum);
665 }
666 if (offset != devsize) {
667 /* Argh */
668 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
669 goto setup_err;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700673 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return mtd;
675
676 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100677 kfree(mtd->eraseregions);
678 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 kfree(cfi->cmdset_priv);
680 kfree(cfi->cfiq);
681 return NULL;
682}
683
684/*
685 * Return true if the chip is ready.
686 *
687 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
688 * non-suspended sector) and is indicated by no toggle bits toggling.
689 *
690 * Note that anything more complicated than checking if no bits are toggling
691 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300692 * correctly and is therefore not done (particularly with interleaved chips
693 * as each chip must be checked independently of the others).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100695static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 map_word d, t;
698
699 d = map_read(map, addr);
700 t = map_read(map, addr);
701
702 return map_word_equal(map, d, t);
703}
704
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100705/*
706 * Return true if the chip is ready and has the correct value.
707 *
708 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
709 * non-suspended sector) and it is indicated by no bits toggling.
710 *
711 * Error are indicated by toggling bits or bits held with the wrong value,
712 * or with bits toggling.
713 *
714 * Note that anything more complicated than checking if no bits are toggling
715 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300716 * correctly and is therefore not done (particularly with interleaved chips
717 * as each chip must be checked independently of the others).
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100718 *
719 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100720static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100721{
722 map_word oldd, curd;
723
724 oldd = map_read(map, addr);
725 curd = map_read(map, addr);
726
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000727 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100728 map_word_equal(map, curd, expected);
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
732{
733 DECLARE_WAITQUEUE(wait, current);
734 struct cfi_private *cfi = map->fldrv_priv;
735 unsigned long timeo;
736 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
737
738 resettime:
739 timeo = jiffies + HZ;
740 retry:
741 switch (chip->state) {
742
743 case FL_STATUS:
744 for (;;) {
745 if (chip_ready(map, adr))
746 break;
747
748 if (time_after(jiffies, timeo)) {
749 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EIO;
751 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200752 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200754 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Someone else might have been playing with it. */
756 goto retry;
757 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case FL_READY:
760 case FL_CFI_QUERY:
761 case FL_JEDEC_QUERY:
762 return 0;
763
764 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100765 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
766 !(mode == FL_READY || mode == FL_POINT ||
767 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto sleep;
769
770 /* We could check to see if we're trying to access the sector
771 * that is currently being erased. However, no user will try
772 * anything like that so we just wait for the timeout. */
773
774 /* Erase suspend */
775 /* It's harmless to issue the Erase-Suspend and Erase-Resume
776 * commands when the erase algorithm isn't in progress. */
777 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
778 chip->oldstate = FL_ERASING;
779 chip->state = FL_ERASE_SUSPENDING;
780 chip->erase_suspended = 1;
781 for (;;) {
782 if (chip_ready(map, adr))
783 break;
784
785 if (time_after(jiffies, timeo)) {
786 /* Should have suspended the erase by now.
787 * Send an Erase-Resume command as either
788 * there was an error (so leave the erase
789 * routine to recover from it) or we trying to
790 * use the erase-in-progress sector. */
Tadashi Abe100f2342011-05-19 15:58:15 +0900791 put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
793 return -EIO;
794 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000795
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200796 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200798 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
800 So we can just loop here. */
801 }
802 chip->state = FL_READY;
803 return 0;
804
Todd Poynor02b15e32005-06-07 00:04:39 +0100805 case FL_XIP_WHILE_ERASING:
806 if (mode != FL_READY && mode != FL_POINT &&
807 (!cfip || !(cfip->EraseSuspend&2)))
808 goto sleep;
809 chip->oldstate = chip->state;
810 chip->state = FL_READY;
811 return 0;
812
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700813 case FL_SHUTDOWN:
814 /* The machine is rebooting */
815 return -EIO;
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 case FL_POINT:
818 /* Only if there's no operation suspended... */
819 if (mode == FL_READY && chip->oldstate == FL_READY)
820 return 0;
821
822 default:
823 sleep:
824 set_current_state(TASK_UNINTERRUPTIBLE);
825 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200826 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 schedule();
828 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200829 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto resettime;
831 }
832}
833
834
835static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
836{
837 struct cfi_private *cfi = map->fldrv_priv;
838
839 switch(chip->oldstate) {
840 case FL_ERASING:
Gerlando Falauto42096282012-07-03 09:09:47 +0200841 cfi_fixup_m29ew_erase_suspend(map,
842 chip->in_progress_block_addr);
Guillaume LECERF08968042010-10-26 10:45:23 +0100843 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Gerlando Falauto42096282012-07-03 09:09:47 +0200844 cfi_fixup_m29ew_delay_after_resume(cfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 chip->oldstate = FL_READY;
846 chip->state = FL_ERASING;
847 break;
848
Todd Poynor02b15e32005-06-07 00:04:39 +0100849 case FL_XIP_WHILE_ERASING:
850 chip->state = chip->oldstate;
851 chip->oldstate = FL_READY;
852 break;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case FL_READY:
855 case FL_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 default:
858 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
859 }
860 wake_up(&chip->wq);
861}
862
Todd Poynor02b15e32005-06-07 00:04:39 +0100863#ifdef CONFIG_MTD_XIP
864
865/*
866 * No interrupt what so ever can be serviced while the flash isn't in array
867 * mode. This is ensured by the xip_disable() and xip_enable() functions
868 * enclosing any code path where the flash is known not to be in array mode.
869 * And within a XIP disabled code path, only functions marked with __xipram
870 * may be called and nothing else (it's a good thing to inspect generated
871 * assembly to make sure inline functions were actually inlined and that gcc
872 * didn't emit calls to its own support functions). Also configuring MTD CFI
873 * support to a single buswidth and a single interleave is also recommended.
874 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200875
Todd Poynor02b15e32005-06-07 00:04:39 +0100876static void xip_disable(struct map_info *map, struct flchip *chip,
877 unsigned long adr)
878{
879 /* TODO: chips with no XIP use should ignore and return */
880 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
881 local_irq_disable();
882}
883
884static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
885 unsigned long adr)
886{
887 struct cfi_private *cfi = map->fldrv_priv;
888
889 if (chip->state != FL_POINT && chip->state != FL_READY) {
890 map_write(map, CMD(0xf0), adr);
891 chip->state = FL_READY;
892 }
893 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200894 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100895 local_irq_enable();
896}
897
898/*
899 * When a delay is required for the flash operation to complete, the
900 * xip_udelay() function is polling for both the given timeout and pending
901 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000902 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100903 * and interrupts unmasked. Task scheduling might also happen at that
904 * point. The CPU eventually returns from the interrupt or the call to
905 * schedule() and the suspended flash operation is resumed for the remaining
906 * of the delay period.
907 *
908 * Warning: this function _will_ fool interrupt latency tracing tools.
909 */
910
911static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
912 unsigned long adr, int usec)
913{
914 struct cfi_private *cfi = map->fldrv_priv;
915 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
916 map_word status, OK = CMD(0x80);
917 unsigned long suspended, start = xip_currtime();
918 flstate_t oldstate;
919
920 do {
921 cpu_relax();
922 if (xip_irqpending() && extp &&
923 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
924 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
925 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000926 * Let's suspend the erase operation when supported.
927 * Note that we currently don't try to suspend
928 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100929 * operation suspended (imagine what happens
930 * when one chip was already done with the current
931 * operation while another chip suspended it, then
932 * we resume the whole thing at once). Yes, it
933 * can happen!
934 */
935 map_write(map, CMD(0xb0), adr);
936 usec -= xip_elapsed_since(start);
937 suspended = xip_currtime();
938 do {
939 if (xip_elapsed_since(suspended) > 100000) {
940 /*
941 * The chip doesn't want to suspend
942 * after waiting for 100 msecs.
943 * This is a critical error but there
944 * is not much we can do here.
945 */
946 return;
947 }
948 status = map_read(map, adr);
949 } while (!map_word_andequal(map, status, OK, OK));
950
951 /* Suspend succeeded */
952 oldstate = chip->state;
953 if (!map_word_bitsset(map, status, CMD(0x40)))
954 break;
955 chip->state = FL_XIP_WHILE_ERASING;
956 chip->erase_suspended = 1;
957 map_write(map, CMD(0xf0), adr);
958 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200959 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100960 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200961 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200962 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100963 cond_resched();
964
965 /*
966 * We're back. However someone else might have
967 * decided to go write to the chip if we are in
968 * a suspended erase state. If so let's wait
969 * until it's done.
970 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200971 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100972 while (chip->state != FL_XIP_WHILE_ERASING) {
973 DECLARE_WAITQUEUE(wait, current);
974 set_current_state(TASK_UNINTERRUPTIBLE);
975 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200976 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100977 schedule();
978 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200979 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100980 }
981 /* Disallow XIP again */
982 local_irq_disable();
983
Gerlando Falauto42096282012-07-03 09:09:47 +0200984 /* Correct Erase Suspend Hangups for M29EW */
985 cfi_fixup_m29ew_erase_suspend(map, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +0100986 /* Resume the write or erase operation */
Guillaume LECERF08968042010-10-26 10:45:23 +0100987 map_write(map, cfi->sector_erase_cmd, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +0100988 chip->state = oldstate;
989 start = xip_currtime();
990 } else if (usec >= 1000000/HZ) {
991 /*
992 * Try to save on CPU power when waiting delay
993 * is at least a system timer tick period.
994 * No need to be extremely accurate here.
995 */
996 xip_cpu_idle();
997 }
998 status = map_read(map, adr);
999 } while (!map_word_andequal(map, status, OK, OK)
1000 && xip_elapsed_since(start) < usec);
1001}
1002
1003#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1004
1005/*
1006 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1007 * the flash is actively programming or erasing since we have to poll for
1008 * the operation to complete anyway. We can't do that in a generic way with
1009 * a XIP setup so do it before the actual flash operation in this case
1010 * and stub it out from INVALIDATE_CACHE_UDELAY.
1011 */
1012#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1013 INVALIDATE_CACHED_RANGE(map, from, size)
1014
1015#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1016 UDELAY(map, chip, adr, usec)
1017
1018/*
1019 * Extra notes:
1020 *
1021 * Activating this XIP support changes the way the code works a bit. For
1022 * example the code to suspend the current process when concurrent access
1023 * happens is never executed because xip_udelay() will always return with the
1024 * same chip state as it was entered with. This is why there is no care for
1025 * the presence of add_wait_queue() or schedule() calls from within a couple
1026 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1027 * The queueing and scheduling are always happening within xip_udelay().
1028 *
1029 * Similarly, get_chip() and put_chip() just happen to always be executed
1030 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1031 * is in array mode, therefore never executing many cases therein and not
1032 * causing any problem with XIP.
1033 */
1034
1035#else
1036
1037#define xip_disable(map, chip, adr)
1038#define xip_enable(map, chip, adr)
1039#define XIP_INVAL_CACHED_RANGE(x...)
1040
1041#define UDELAY(map, chip, adr, usec) \
1042do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001043 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001044 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001045 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001046} while (0)
1047
1048#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1049do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001050 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001051 INVALIDATE_CACHED_RANGE(map, adr, len); \
1052 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001053 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001054} while (0)
1055
1056#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1059{
1060 unsigned long cmd_addr;
1061 struct cfi_private *cfi = map->fldrv_priv;
1062 int ret;
1063
1064 adr += chip->start;
1065
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001066 /* Ensure cmd read/writes are aligned. */
1067 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001069 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ret = get_chip(map, chip, cmd_addr, FL_READY);
1071 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001072 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return ret;
1074 }
1075
1076 if (chip->state != FL_POINT && chip->state != FL_READY) {
1077 map_write(map, CMD(0xf0), cmd_addr);
1078 chip->state = FL_READY;
1079 }
1080
1081 map_copy_from(map, buf, adr, len);
1082
1083 put_chip(map, chip, cmd_addr);
1084
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001085 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 0;
1087}
1088
1089
1090static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1091{
1092 struct map_info *map = mtd->priv;
1093 struct cfi_private *cfi = map->fldrv_priv;
1094 unsigned long ofs;
1095 int chipnum;
1096 int ret = 0;
1097
1098 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 chipnum = (from >> cfi->chipshift);
1100 ofs = from - (chipnum << cfi->chipshift);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 while (len) {
1103 unsigned long thislen;
1104
1105 if (chipnum >= cfi->numchips)
1106 break;
1107
1108 if ((len + ofs -1) >> cfi->chipshift)
1109 thislen = (1<<cfi->chipshift) - ofs;
1110 else
1111 thislen = len;
1112
1113 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1114 if (ret)
1115 break;
1116
1117 *retlen += thislen;
1118 len -= thislen;
1119 buf += thislen;
1120
1121 ofs = 0;
1122 chipnum++;
1123 }
1124 return ret;
1125}
1126
1127
1128static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1129{
1130 DECLARE_WAITQUEUE(wait, current);
1131 unsigned long timeo = jiffies + HZ;
1132 struct cfi_private *cfi = map->fldrv_priv;
1133
1134 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001135 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 set_current_state(TASK_UNINTERRUPTIBLE);
1139 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001140
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001141 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 schedule();
1144 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 timeo = jiffies + HZ;
1146
1147 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 adr += chip->start;
1151
1152 chip->state = FL_READY;
1153
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(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 map_copy_from(map, buf, adr, len);
1159
1160 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1161 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1162 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1163 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001166 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 return 0;
1169}
1170
1171static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1172{
1173 struct map_info *map = mtd->priv;
1174 struct cfi_private *cfi = map->fldrv_priv;
1175 unsigned long ofs;
1176 int chipnum;
1177 int ret = 0;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* 8 secsi bytes per chip */
1181 chipnum=from>>3;
1182 ofs=from & 7;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 while (len) {
1185 unsigned long thislen;
1186
1187 if (chipnum >= cfi->numchips)
1188 break;
1189
1190 if ((len + ofs -1) >> 3)
1191 thislen = (1<<3) - ofs;
1192 else
1193 thislen = len;
1194
1195 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1196 if (ret)
1197 break;
1198
1199 *retlen += thislen;
1200 len -= thislen;
1201 buf += thislen;
1202
1203 ofs = 0;
1204 chipnum++;
1205 }
1206 return ret;
1207}
1208
1209
Todd Poynor02b15e32005-06-07 00:04:39 +01001210static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct cfi_private *cfi = map->fldrv_priv;
1213 unsigned long timeo = jiffies + HZ;
1214 /*
1215 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1216 * have a max write time of a few hundreds usec). However, we should
1217 * use the maximum timeout value given by the chip at probe time
1218 * instead. Unfortunately, struct flchip does have a field for
1219 * maximum timeout, only for typical which can be far too short
1220 * depending of the conditions. The ' + 1' is to avoid having a
1221 * timeout of 0 jiffies if HZ is smaller than 1000.
1222 */
1223 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1224 int ret = 0;
1225 map_word oldd;
1226 int retry_cnt = 0;
1227
1228 adr += chip->start;
1229
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001230 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 ret = get_chip(map, chip, adr, FL_WRITING);
1232 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001233 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return ret;
1235 }
1236
Brian Norris289c0522011-07-19 10:06:09 -07001237 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 __func__, adr, datum.x[0] );
1239
1240 /*
1241 * Check for a NOP for the case when the datum to write is already
1242 * present - it saves time and works around buggy chips that corrupt
1243 * data at other locations when 0xff is written to a location that
1244 * already contains 0xff.
1245 */
1246 oldd = map_read(map, adr);
1247 if (map_word_equal(map, oldd, datum)) {
Brian Norris289c0522011-07-19 10:06:09 -07001248 pr_debug("MTD %s(): NOP\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 __func__);
1250 goto op_done;
1251 }
1252
Todd Poynor02b15e32005-06-07 00:04:39 +01001253 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001255 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 retry:
1257 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1258 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1259 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1260 map_write(map, datum, adr);
1261 chip->state = FL_WRITING;
1262
Todd Poynor02b15e32005-06-07 00:04:39 +01001263 INVALIDATE_CACHE_UDELAY(map, chip,
1264 adr, map_bankwidth(map),
1265 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001268 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 for (;;) {
1270 if (chip->state != FL_WRITING) {
1271 /* Someone's suspended the write. Sleep */
1272 DECLARE_WAITQUEUE(wait, current);
1273
1274 set_current_state(TASK_UNINTERRUPTIBLE);
1275 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001276 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 schedule();
1278 remove_wait_queue(&chip->wq, &wait);
1279 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001280 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 continue;
1282 }
1283
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001284 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001285 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001286 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001287 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001288 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001291 if (chip_ready(map, adr))
1292 break;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001295 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001297 /* Did we succeed? */
1298 if (!chip_good(map, adr, datum)) {
1299 /* reset on all failures. */
1300 map_write( map, CMD(0xF0), chip->start );
1301 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001303 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001304 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001306 ret = -EIO;
1307 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001308 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 op_done:
1310 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001311 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001313 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 return ret;
1316}
1317
1318
1319static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1320 size_t *retlen, const u_char *buf)
1321{
1322 struct map_info *map = mtd->priv;
1323 struct cfi_private *cfi = map->fldrv_priv;
1324 int ret = 0;
1325 int chipnum;
1326 unsigned long ofs, chipstart;
1327 DECLARE_WAITQUEUE(wait, current);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 chipnum = to >> cfi->chipshift;
1330 ofs = to - (chipnum << cfi->chipshift);
1331 chipstart = cfi->chips[chipnum].start;
1332
1333 /* If it's not bus-aligned, do the first byte write */
1334 if (ofs & (map_bankwidth(map)-1)) {
1335 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1336 int i = ofs - bus_ofs;
1337 int n = 0;
1338 map_word tmp_buf;
1339
1340 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001341 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 set_current_state(TASK_UNINTERRUPTIBLE);
1345 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1346
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001347 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 schedule();
1350 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 goto retry;
1352 }
1353
1354 /* Load 'tmp_buf' with old contents of flash */
1355 tmp_buf = map_read(map, bus_ofs+chipstart);
1356
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001357 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* Number of bytes to copy from buffer */
1360 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1363
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001364 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001366 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ofs += n;
1370 buf += n;
1371 (*retlen) += n;
1372 len -= n;
1373
1374 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001375 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ofs = 0;
1377 if (chipnum == cfi->numchips)
1378 return 0;
1379 }
1380 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* We are now aligned, write as much as possible */
1383 while(len >= map_bankwidth(map)) {
1384 map_word datum;
1385
1386 datum = map_word_load(map, buf);
1387
1388 ret = do_write_oneword(map, &cfi->chips[chipnum],
1389 ofs, datum);
1390 if (ret)
1391 return ret;
1392
1393 ofs += map_bankwidth(map);
1394 buf += map_bankwidth(map);
1395 (*retlen) += map_bankwidth(map);
1396 len -= map_bankwidth(map);
1397
1398 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001399 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ofs = 0;
1401 if (chipnum == cfi->numchips)
1402 return 0;
1403 chipstart = cfi->chips[chipnum].start;
1404 }
1405 }
1406
1407 /* Write the trailing bytes if any */
1408 if (len & (map_bankwidth(map)-1)) {
1409 map_word tmp_buf;
1410
1411 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001412 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 set_current_state(TASK_UNINTERRUPTIBLE);
1416 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1417
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001418 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 schedule();
1421 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto retry1;
1423 }
1424
1425 tmp_buf = map_read(map, ofs + chipstart);
1426
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001427 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001430
1431 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001433 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 (*retlen) += len;
1437 }
1438
1439 return 0;
1440}
1441
1442
1443/*
1444 * FIXME: interleaved mode not tested, and probably not supported!
1445 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001446static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001447 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001448 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct cfi_private *cfi = map->fldrv_priv;
1451 unsigned long timeo = jiffies + HZ;
1452 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1453 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1454 int ret = -EIO;
1455 unsigned long cmd_adr;
1456 int z, words;
1457 map_word datum;
1458
1459 adr += chip->start;
1460 cmd_adr = adr;
1461
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001462 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ret = get_chip(map, chip, adr, FL_WRITING);
1464 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001465 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return ret;
1467 }
1468
1469 datum = map_word_load(map, buf);
1470
Brian Norris289c0522011-07-19 10:06:09 -07001471 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 __func__, adr, datum.x[0] );
1473
Todd Poynor02b15e32005-06-07 00:04:39 +01001474 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001476 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1479 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /* Write Buffer Load */
1482 map_write(map, CMD(0x25), cmd_adr);
1483
1484 chip->state = FL_WRITING_TO_BUFFER;
1485
1486 /* Write length of data to come */
1487 words = len / map_bankwidth(map);
1488 map_write(map, CMD(words - 1), cmd_adr);
1489 /* Write data */
1490 z = 0;
1491 while(z < words * map_bankwidth(map)) {
1492 datum = map_word_load(map, buf);
1493 map_write(map, datum, adr + z);
1494
1495 z += map_bankwidth(map);
1496 buf += map_bankwidth(map);
1497 }
1498 z -= map_bankwidth(map);
1499
1500 adr += z;
1501
1502 /* Write Buffer Program Confirm: GO GO GO */
1503 map_write(map, CMD(0x29), cmd_adr);
1504 chip->state = FL_WRITING;
1505
Todd Poynor02b15e32005-06-07 00:04:39 +01001506 INVALIDATE_CACHE_UDELAY(map, chip,
1507 adr, map_bankwidth(map),
1508 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001510 timeo = jiffies + uWriteTimeout;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 for (;;) {
1513 if (chip->state != FL_WRITING) {
1514 /* Someone's suspended the write. Sleep */
1515 DECLARE_WAITQUEUE(wait, current);
1516
1517 set_current_state(TASK_UNINTERRUPTIBLE);
1518 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001519 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 schedule();
1521 remove_wait_queue(&chip->wq, &wait);
1522 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001523 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 continue;
1525 }
1526
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001527 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1528 break;
1529
Todd Poynor02b15e32005-06-07 00:04:39 +01001530 if (chip_ready(map, adr)) {
1531 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001536 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Harald Nordgard-Hansen070c3222012-11-23 23:11:03 +01001539 /*
1540 * Recovery from write-buffer programming failures requires
1541 * the write-to-buffer-reset sequence. Since the last part
1542 * of the sequence also works as a normal reset, we can run
1543 * the same commands regardless of why we are here.
1544 * See e.g.
1545 * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
1546 */
1547 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1548 cfi->device_type, NULL);
1549 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1550 cfi->device_type, NULL);
1551 cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
1552 cfi->device_type, NULL);
Todd Poynor02b15e32005-06-07 00:04:39 +01001553 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* FIXME - should have reset delay before continuing */
1555
Todd Poynor02b15e32005-06-07 00:04:39 +01001556 printk(KERN_WARNING "MTD %s(): software timeout\n",
1557 __func__ );
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ret = -EIO;
1560 op_done:
1561 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001562 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001564 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 return ret;
1567}
1568
1569
1570static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1571 size_t *retlen, const u_char *buf)
1572{
1573 struct map_info *map = mtd->priv;
1574 struct cfi_private *cfi = map->fldrv_priv;
1575 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1576 int ret = 0;
1577 int chipnum;
1578 unsigned long ofs;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 chipnum = to >> cfi->chipshift;
1581 ofs = to - (chipnum << cfi->chipshift);
1582
1583 /* If it's not bus-aligned, do the first word write */
1584 if (ofs & (map_bankwidth(map)-1)) {
1585 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1586 if (local_len > len)
1587 local_len = len;
1588 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1589 local_len, retlen, buf);
1590 if (ret)
1591 return ret;
1592 ofs += local_len;
1593 buf += local_len;
1594 len -= local_len;
1595
1596 if (ofs >> cfi->chipshift) {
1597 chipnum ++;
1598 ofs = 0;
1599 if (chipnum == cfi->numchips)
1600 return 0;
1601 }
1602 }
1603
1604 /* Write buffer is worth it only if more than one word to write... */
1605 while (len >= map_bankwidth(map) * 2) {
1606 /* We must not cross write block boundaries */
1607 int size = wbufsize - (ofs & (wbufsize-1));
1608
1609 if (size > len)
1610 size = len;
1611 if (size % map_bankwidth(map))
1612 size -= size % map_bankwidth(map);
1613
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001614 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ofs, buf, size);
1616 if (ret)
1617 return ret;
1618
1619 ofs += size;
1620 buf += size;
1621 (*retlen) += size;
1622 len -= size;
1623
1624 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001625 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ofs = 0;
1627 if (chipnum == cfi->numchips)
1628 return 0;
1629 }
1630 }
1631
1632 if (len) {
1633 size_t retlen_dregs = 0;
1634
1635 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1636 len, &retlen_dregs, buf);
1637
1638 *retlen += retlen_dregs;
1639 return ret;
1640 }
1641
1642 return 0;
1643}
1644
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001645/*
1646 * Wait for the flash chip to become ready to write data
1647 *
1648 * This is only called during the panic_write() path. When panic_write()
1649 * is called, the kernel is in the process of a panic, and will soon be
1650 * dead. Therefore we don't take any locks, and attempt to get access
1651 * to the chip as soon as possible.
1652 */
1653static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
1654 unsigned long adr)
1655{
1656 struct cfi_private *cfi = map->fldrv_priv;
1657 int retries = 10;
1658 int i;
1659
1660 /*
1661 * If the driver thinks the chip is idle, and no toggle bits
1662 * are changing, then the chip is actually idle for sure.
1663 */
1664 if (chip->state == FL_READY && chip_ready(map, adr))
1665 return 0;
1666
1667 /*
1668 * Try several times to reset the chip and then wait for it
1669 * to become idle. The upper limit of a few milliseconds of
1670 * delay isn't a big problem: the kernel is dying anyway. It
1671 * is more important to save the messages.
1672 */
1673 while (retries > 0) {
1674 const unsigned long timeo = (HZ / 1000) + 1;
1675
1676 /* send the reset command */
1677 map_write(map, CMD(0xF0), chip->start);
1678
1679 /* wait for the chip to become ready */
1680 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
1681 if (chip_ready(map, adr))
1682 return 0;
1683
1684 udelay(1);
1685 }
1686 }
1687
1688 /* the chip never became ready */
1689 return -EBUSY;
1690}
1691
1692/*
1693 * Write out one word of data to a single flash chip during a kernel panic
1694 *
1695 * This is only called during the panic_write() path. When panic_write()
1696 * is called, the kernel is in the process of a panic, and will soon be
1697 * dead. Therefore we don't take any locks, and attempt to get access
1698 * to the chip as soon as possible.
1699 *
1700 * The implementation of this routine is intentionally similar to
1701 * do_write_oneword(), in order to ease code maintenance.
1702 */
1703static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
1704 unsigned long adr, map_word datum)
1705{
1706 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
1707 struct cfi_private *cfi = map->fldrv_priv;
1708 int retry_cnt = 0;
1709 map_word oldd;
1710 int ret = 0;
1711 int i;
1712
1713 adr += chip->start;
1714
1715 ret = cfi_amdstd_panic_wait(map, chip, adr);
1716 if (ret)
1717 return ret;
1718
1719 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
1720 __func__, adr, datum.x[0]);
1721
1722 /*
1723 * Check for a NOP for the case when the datum to write is already
1724 * present - it saves time and works around buggy chips that corrupt
1725 * data at other locations when 0xff is written to a location that
1726 * already contains 0xff.
1727 */
1728 oldd = map_read(map, adr);
1729 if (map_word_equal(map, oldd, datum)) {
1730 pr_debug("MTD %s(): NOP\n", __func__);
1731 goto op_done;
1732 }
1733
1734 ENABLE_VPP(map);
1735
1736retry:
1737 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1738 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1739 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1740 map_write(map, datum, adr);
1741
1742 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
1743 if (chip_ready(map, adr))
1744 break;
1745
1746 udelay(1);
1747 }
1748
1749 if (!chip_good(map, adr, datum)) {
1750 /* reset on all failures. */
1751 map_write(map, CMD(0xF0), chip->start);
1752 /* FIXME - should have reset delay before continuing */
1753
1754 if (++retry_cnt <= MAX_WORD_RETRIES)
1755 goto retry;
1756
1757 ret = -EIO;
1758 }
1759
1760op_done:
1761 DISABLE_VPP(map);
1762 return ret;
1763}
1764
1765/*
1766 * Write out some data during a kernel panic
1767 *
1768 * This is used by the mtdoops driver to save the dying messages from a
1769 * kernel which has panic'd.
1770 *
1771 * This routine ignores all of the locking used throughout the rest of the
1772 * driver, in order to ensure that the data gets written out no matter what
1773 * state this driver (and the flash chip itself) was in when the kernel crashed.
1774 *
1775 * The implementation of this routine is intentionally similar to
1776 * cfi_amdstd_write_words(), in order to ease code maintenance.
1777 */
1778static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1779 size_t *retlen, const u_char *buf)
1780{
1781 struct map_info *map = mtd->priv;
1782 struct cfi_private *cfi = map->fldrv_priv;
1783 unsigned long ofs, chipstart;
1784 int ret = 0;
1785 int chipnum;
1786
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001787 chipnum = to >> cfi->chipshift;
1788 ofs = to - (chipnum << cfi->chipshift);
1789 chipstart = cfi->chips[chipnum].start;
1790
1791 /* If it's not bus aligned, do the first byte write */
1792 if (ofs & (map_bankwidth(map) - 1)) {
1793 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
1794 int i = ofs - bus_ofs;
1795 int n = 0;
1796 map_word tmp_buf;
1797
1798 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
1799 if (ret)
1800 return ret;
1801
1802 /* Load 'tmp_buf' with old contents of flash */
1803 tmp_buf = map_read(map, bus_ofs + chipstart);
1804
1805 /* Number of bytes to copy from buffer */
1806 n = min_t(int, len, map_bankwidth(map) - i);
1807
1808 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1809
1810 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1811 bus_ofs, tmp_buf);
1812 if (ret)
1813 return ret;
1814
1815 ofs += n;
1816 buf += n;
1817 (*retlen) += n;
1818 len -= n;
1819
1820 if (ofs >> cfi->chipshift) {
1821 chipnum++;
1822 ofs = 0;
1823 if (chipnum == cfi->numchips)
1824 return 0;
1825 }
1826 }
1827
1828 /* We are now aligned, write as much as possible */
1829 while (len >= map_bankwidth(map)) {
1830 map_word datum;
1831
1832 datum = map_word_load(map, buf);
1833
1834 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1835 ofs, datum);
1836 if (ret)
1837 return ret;
1838
1839 ofs += map_bankwidth(map);
1840 buf += map_bankwidth(map);
1841 (*retlen) += map_bankwidth(map);
1842 len -= map_bankwidth(map);
1843
1844 if (ofs >> cfi->chipshift) {
1845 chipnum++;
1846 ofs = 0;
1847 if (chipnum == cfi->numchips)
1848 return 0;
1849
1850 chipstart = cfi->chips[chipnum].start;
1851 }
1852 }
1853
1854 /* Write the trailing bytes if any */
1855 if (len & (map_bankwidth(map) - 1)) {
1856 map_word tmp_buf;
1857
1858 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
1859 if (ret)
1860 return ret;
1861
1862 tmp_buf = map_read(map, ofs + chipstart);
1863
1864 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1865
1866 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1867 ofs, tmp_buf);
1868 if (ret)
1869 return ret;
1870
1871 (*retlen) += len;
1872 }
1873
1874 return 0;
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878/*
1879 * Handle devices with one erase region, that only implement
1880 * the chip erase command.
1881 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001882static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 struct cfi_private *cfi = map->fldrv_priv;
1885 unsigned long timeo = jiffies + HZ;
1886 unsigned long int adr;
1887 DECLARE_WAITQUEUE(wait, current);
1888 int ret = 0;
1889
1890 adr = cfi->addr_unlock1;
1891
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001892 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ret = get_chip(map, chip, adr, FL_WRITING);
1894 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001895 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return ret;
1897 }
1898
Brian Norris289c0522011-07-19 10:06:09 -07001899 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 __func__, chip->start );
1901
Todd Poynor02b15e32005-06-07 00:04:39 +01001902 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001904 xip_disable(map, chip, adr);
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1907 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1908 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1909 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1910 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1911 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1912
1913 chip->state = FL_ERASING;
1914 chip->erase_suspended = 0;
1915 chip->in_progress_block_addr = adr;
1916
Todd Poynor02b15e32005-06-07 00:04:39 +01001917 INVALIDATE_CACHE_UDELAY(map, chip,
1918 adr, map->size,
1919 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 timeo = jiffies + (HZ*20);
1922
1923 for (;;) {
1924 if (chip->state != FL_ERASING) {
1925 /* Someone's suspended the erase. Sleep */
1926 set_current_state(TASK_UNINTERRUPTIBLE);
1927 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001928 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 schedule();
1930 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001931 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 continue;
1933 }
1934 if (chip->erase_suspended) {
1935 /* This erase was suspended and resumed.
1936 Adjust the timeout */
1937 timeo = jiffies + (HZ*20); /* FIXME */
1938 chip->erase_suspended = 0;
1939 }
1940
1941 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001944 if (time_after(jiffies, timeo)) {
1945 printk(KERN_WARNING "MTD %s(): software timeout\n",
1946 __func__ );
1947 break;
1948 }
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001951 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001953 /* Did we succeed? */
1954 if (!chip_good(map, adr, map_word_ff(map))) {
1955 /* reset on all failures. */
1956 map_write( map, CMD(0xF0), chip->start );
1957 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001959 ret = -EIO;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001963 xip_enable(map, chip, adr);
Paul Parsonse7d93772012-03-07 14:11:16 +00001964 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001966 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 return ret;
1969}
1970
1971
Todd Poynor02b15e32005-06-07 00:04:39 +01001972static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 struct cfi_private *cfi = map->fldrv_priv;
1975 unsigned long timeo = jiffies + HZ;
1976 DECLARE_WAITQUEUE(wait, current);
1977 int ret = 0;
1978
1979 adr += chip->start;
1980
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001981 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 ret = get_chip(map, chip, adr, FL_ERASING);
1983 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001984 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return ret;
1986 }
1987
Brian Norris289c0522011-07-19 10:06:09 -07001988 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 __func__, adr );
1990
Todd Poynor02b15e32005-06-07 00:04:39 +01001991 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001993 xip_disable(map, chip, adr);
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1996 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1997 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1998 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1999 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Guillaume LECERF08968042010-10-26 10:45:23 +01002000 map_write(map, cfi->sector_erase_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 chip->state = FL_ERASING;
2003 chip->erase_suspended = 0;
2004 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01002005
2006 INVALIDATE_CACHE_UDELAY(map, chip,
2007 adr, len,
2008 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 timeo = jiffies + (HZ*20);
2011
2012 for (;;) {
2013 if (chip->state != FL_ERASING) {
2014 /* Someone's suspended the erase. Sleep */
2015 set_current_state(TASK_UNINTERRUPTIBLE);
2016 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002017 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 schedule();
2019 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002020 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 continue;
2022 }
2023 if (chip->erase_suspended) {
2024 /* This erase was suspended and resumed.
2025 Adjust the timeout */
2026 timeo = jiffies + (HZ*20); /* FIXME */
2027 chip->erase_suspended = 0;
2028 }
2029
Todd Poynor02b15e32005-06-07 00:04:39 +01002030 if (chip_ready(map, adr)) {
2031 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01002033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002035 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01002036 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002037 printk(KERN_WARNING "MTD %s(): software timeout\n",
2038 __func__ );
2039 break;
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01002043 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002045 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02002046 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002047 /* reset on all failures. */
2048 map_write( map, CMD(0xF0), chip->start );
2049 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002051 ret = -EIO;
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00002055 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002057 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return ret;
2059}
2060
2061
Ben Dooksce0f33a2007-05-28 19:59:00 +01002062static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 unsigned long ofs, len;
2065 int ret;
2066
2067 ofs = instr->addr;
2068 len = instr->len;
2069
2070 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2071 if (ret)
2072 return ret;
2073
2074 instr->state = MTD_ERASE_DONE;
2075 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
2080
2081static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
2082{
2083 struct map_info *map = mtd->priv;
2084 struct cfi_private *cfi = map->fldrv_priv;
2085 int ret = 0;
2086
2087 if (instr->addr != 0)
2088 return -EINVAL;
2089
2090 if (instr->len != mtd->size)
2091 return -EINVAL;
2092
2093 ret = do_erase_chip(map, &cfi->chips[0]);
2094 if (ret)
2095 return ret;
2096
2097 instr->state = MTD_ERASE_DONE;
2098 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return 0;
2101}
2102
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002103static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2104 unsigned long adr, int len, void *thunk)
2105{
2106 struct cfi_private *cfi = map->fldrv_priv;
2107 int ret;
2108
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002109 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002110 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2111 if (ret)
2112 goto out_unlock;
2113 chip->state = FL_LOCKING;
2114
Brian Norris0a32a102011-07-19 10:06:10 -07002115 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002116
2117 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2118 cfi->device_type, NULL);
2119 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2120 cfi->device_type, NULL);
2121 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2122 cfi->device_type, NULL);
2123 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2124 cfi->device_type, NULL);
2125 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2126 cfi->device_type, NULL);
2127 map_write(map, CMD(0x40), chip->start + adr);
2128
2129 chip->state = FL_READY;
2130 put_chip(map, chip, adr + chip->start);
2131 ret = 0;
2132
2133out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002134 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002135 return ret;
2136}
2137
2138static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2139 unsigned long adr, int len, void *thunk)
2140{
2141 struct cfi_private *cfi = map->fldrv_priv;
2142 int ret;
2143
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002144 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002145 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2146 if (ret)
2147 goto out_unlock;
2148 chip->state = FL_UNLOCKING;
2149
Brian Norris0a32a102011-07-19 10:06:10 -07002150 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002151
2152 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2153 cfi->device_type, NULL);
2154 map_write(map, CMD(0x70), adr);
2155
2156 chip->state = FL_READY;
2157 put_chip(map, chip, adr + chip->start);
2158 ret = 0;
2159
2160out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002161 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002162 return ret;
2163}
2164
Adrian Hunter69423d92008-12-10 13:37:21 +00002165static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002166{
2167 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2168}
2169
Adrian Hunter69423d92008-12-10 13:37:21 +00002170static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002171{
2172 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176static void cfi_amdstd_sync (struct mtd_info *mtd)
2177{
2178 struct map_info *map = mtd->priv;
2179 struct cfi_private *cfi = map->fldrv_priv;
2180 int i;
2181 struct flchip *chip;
2182 int ret = 0;
2183 DECLARE_WAITQUEUE(wait, current);
2184
2185 for (i=0; !ret && i<cfi->numchips; i++) {
2186 chip = &cfi->chips[i];
2187
2188 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002189 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 switch(chip->state) {
2192 case FL_READY:
2193 case FL_STATUS:
2194 case FL_CFI_QUERY:
2195 case FL_JEDEC_QUERY:
2196 chip->oldstate = chip->state;
2197 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002198 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 * as the whole point is that nobody can do anything
2200 * with the chip now anyway.
2201 */
2202 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002203 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 break;
2205
2206 default:
2207 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07002208 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002210
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002211 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 schedule();
2214
2215 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto retry;
2218 }
2219 }
2220
2221 /* Unlock the chips again */
2222
2223 for (i--; i >=0; i--) {
2224 chip = &cfi->chips[i];
2225
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002226 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (chip->state == FL_SYNCING) {
2229 chip->state = chip->oldstate;
2230 wake_up(&chip->wq);
2231 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002232 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234}
2235
2236
2237static int cfi_amdstd_suspend(struct mtd_info *mtd)
2238{
2239 struct map_info *map = mtd->priv;
2240 struct cfi_private *cfi = map->fldrv_priv;
2241 int i;
2242 struct flchip *chip;
2243 int ret = 0;
2244
2245 for (i=0; !ret && i<cfi->numchips; i++) {
2246 chip = &cfi->chips[i];
2247
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002248 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 switch(chip->state) {
2251 case FL_READY:
2252 case FL_STATUS:
2253 case FL_CFI_QUERY:
2254 case FL_JEDEC_QUERY:
2255 chip->oldstate = chip->state;
2256 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002257 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * as the whole point is that nobody can do anything
2259 * with the chip now anyway.
2260 */
2261 case FL_PM_SUSPENDED:
2262 break;
2263
2264 default:
2265 ret = -EAGAIN;
2266 break;
2267 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002268 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270
2271 /* Unlock the chips again */
2272
2273 if (ret) {
2274 for (i--; i >=0; i--) {
2275 chip = &cfi->chips[i];
2276
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002277 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (chip->state == FL_PM_SUSPENDED) {
2280 chip->state = chip->oldstate;
2281 wake_up(&chip->wq);
2282 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002283 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return ret;
2288}
2289
2290
2291static void cfi_amdstd_resume(struct mtd_info *mtd)
2292{
2293 struct map_info *map = mtd->priv;
2294 struct cfi_private *cfi = map->fldrv_priv;
2295 int i;
2296 struct flchip *chip;
2297
2298 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 chip = &cfi->chips[i];
2301
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002302 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (chip->state == FL_PM_SUSPENDED) {
2305 chip->state = FL_READY;
2306 map_write(map, CMD(0xF0), chip->start);
2307 wake_up(&chip->wq);
2308 }
2309 else
2310 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2311
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002312 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314}
2315
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002316
2317/*
2318 * Ensure that the flash device is put back into read array mode before
2319 * unloading the driver or rebooting. On some systems, rebooting while
2320 * the flash is in query/program/erase mode will prevent the CPU from
2321 * fetching the bootloader code, requiring a hard reset or power cycle.
2322 */
2323static int cfi_amdstd_reset(struct mtd_info *mtd)
2324{
2325 struct map_info *map = mtd->priv;
2326 struct cfi_private *cfi = map->fldrv_priv;
2327 int i, ret;
2328 struct flchip *chip;
2329
2330 for (i = 0; i < cfi->numchips; i++) {
2331
2332 chip = &cfi->chips[i];
2333
2334 mutex_lock(&chip->mutex);
2335
2336 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2337 if (!ret) {
2338 map_write(map, CMD(0xF0), chip->start);
2339 chip->state = FL_SHUTDOWN;
2340 put_chip(map, chip, chip->start);
2341 }
2342
2343 mutex_unlock(&chip->mutex);
2344 }
2345
2346 return 0;
2347}
2348
2349
2350static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2351 void *v)
2352{
2353 struct mtd_info *mtd;
2354
2355 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2356 cfi_amdstd_reset(mtd);
2357 return NOTIFY_DONE;
2358}
2359
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361static void cfi_amdstd_destroy(struct mtd_info *mtd)
2362{
2363 struct map_info *map = mtd->priv;
2364 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002365
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002366 cfi_amdstd_reset(mtd);
2367 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 kfree(cfi->cmdset_priv);
2369 kfree(cfi->cfiq);
2370 kfree(cfi);
2371 kfree(mtd->eraseregions);
2372}
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374MODULE_LICENSE("GPL");
2375MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2376MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002377MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002378MODULE_ALIAS("cfi_cmdset_0701");