blob: d02592e6a0f02a89195ef1aea302445d98585b0e [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;
320 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
321 }
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;
331 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
332 }
333}
334
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200335/* Used to fix CFI-Tables of chips without Extended Query Tables */
336static struct cfi_fixup cfi_nopri_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100337 { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
338 { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
339 { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
340 { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
341 { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
342 { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
343 { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
344 { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
345 { 0, 0, NULL }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct cfi_fixup cfi_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100349 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#ifdef AMD_BOOTLOC_BUG
Guillaume LECERFcc318222010-11-17 12:35:50 +0100351 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
Steffen Sledz1065cda2011-03-10 09:05:12 +0100352 { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
Guillaume LECERFcc318222010-11-17 12:35:50 +0100353 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100355 { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
356 { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
357 { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
358 { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
359 { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
360 { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
361 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
362 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
363 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
364 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
365 { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
366 { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
367 { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
368 { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#if !FORCE_WORD_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100370 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100372 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100375 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
376 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
377 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
378 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
381static struct cfi_fixup fixup_table[] = {
382 /* The CFI vendor ids and the JEDEC vendor IDs appear
383 * to be common. It is like the devices id's are as
384 * well. This table is to pick all cases where
385 * we know that is the case.
386 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100387 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
388 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
389 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
392
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100393static void cfi_fixup_major_minor(struct cfi_private *cfi,
394 struct cfi_pri_amdstd *extp)
395{
Guillaume LECERFe6372762010-12-17 10:59:41 +0100396 if (cfi->mfr == CFI_MFR_SAMSUNG) {
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100397 if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
398 (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
Guillaume LECERFe6372762010-12-17 10:59:41 +0100399 /*
400 * Samsung K8P2815UQB and K8D6x16UxM chips
401 * report major=0 / minor=0.
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100402 * K8D3x16UxC chips report major=3 / minor=3.
Guillaume LECERFe6372762010-12-17 10:59:41 +0100403 */
404 printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
405 " Extended Query version to 1.%c\n",
406 extp->MinorVersion);
407 extp->MajorVersion = '1';
408 }
409 }
410
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100411 /*
412 * SST 38VF640x chips report major=0xFF / minor=0xFF.
413 */
414 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
415 extp->MajorVersion = '1';
416 extp->MinorVersion = '0';
417 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
421{
422 struct cfi_private *cfi = map->fldrv_priv;
423 struct mtd_info *mtd;
424 int i;
425
Burman Yan95b93a02006-11-15 21:10:29 +0200426 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!mtd) {
428 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
429 return NULL;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 mtd->priv = map;
432 mtd->type = MTD_NORFLASH;
433
434 /* Fill in the default mtd operations */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200435 mtd->_erase = cfi_amdstd_erase_varsize;
436 mtd->_write = cfi_amdstd_write_words;
437 mtd->_read = cfi_amdstd_read;
438 mtd->_sync = cfi_amdstd_sync;
439 mtd->_suspend = cfi_amdstd_suspend;
440 mtd->_resume = cfi_amdstd_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 mtd->flags = MTD_CAP_NORFLASH;
442 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400443 mtd->writesize = 1;
Anatolij Gustschin13ce77f2011-02-10 16:01:46 +0100444 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
Anatolij Gustschind261c722010-12-16 23:42:15 +0100445
Brian Norris0a32a102011-07-19 10:06:10 -0700446 pr_debug("MTD %s(): write buffer size %d\n", __func__,
447 mtd->writebufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200449 mtd->_panic_write = cfi_amdstd_panic_write;
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700450 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (cfi->cfi_mode==CFI_MODE_CFI){
453 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
455 struct cfi_pri_amdstd *extp;
456
457 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200458 if (extp) {
459 /*
460 * It's a real CFI chip, not one for which the probe
461 * routine faked a CFI structure.
462 */
463 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200465 /*
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200466 * 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 +0200467 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
468 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
Guillaume LECERF5da19532010-08-05 13:55:24 +0200469 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200470 * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200471 */
Guillaume LECERF564b8492010-04-24 17:58:17 +0200472 if (extp->MajorVersion != '1' ||
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200473 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
Guillaume LECERF564b8492010-04-24 17:58:17 +0200474 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200475 "version %c.%c (%#02x/%#02x).\n",
476 extp->MajorVersion, extp->MinorVersion,
477 extp->MajorVersion, extp->MinorVersion);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200478 kfree(extp);
479 kfree(mtd);
480 return NULL;
481 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100482
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200483 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
484 extp->MajorVersion, extp->MinorVersion);
485
Guillaume LECERF564b8492010-04-24 17:58:17 +0200486 /* Install our own private info structure */
487 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100488
Guillaume LECERF564b8492010-04-24 17:58:17 +0200489 /* Apply cfi device specific fixups */
490 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200493 /* Tell the user about it in lots of lovely detail */
494 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Guillaume LECERF564b8492010-04-24 17:58:17 +0200497 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100498 if ((bootloc < 2) || (bootloc > 5)) {
499 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
500 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100501 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200502 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200504
505 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100506 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200507
508 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
509 int j = (cfi->cfiq->NumEraseRegions-1)-i;
510 __u32 swap;
511
512 swap = cfi->cfiq->EraseRegionInfo[i];
513 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
514 cfi->cfiq->EraseRegionInfo[j] = swap;
515 }
516 }
517 /* Set the default CFI lock/unlock addresses */
518 cfi->addr_unlock1 = 0x555;
519 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200521 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200522
523 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
524 kfree(mtd);
525 return NULL;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 } /* CFI mode */
529 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
530 /* Apply jedec specific fixups */
531 cfi_fixup(mtd, jedec_fixup_table);
532 }
533 /* Apply generic fixups */
534 cfi_fixup(mtd, fixup_table);
535
536 for (i=0; i< cfi->numchips; i++) {
537 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
538 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
539 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800540 cfi->chips[i].ref_point_counter = 0;
541 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return cfi_amdstd_setup(mtd);
547}
Guillaume LECERF80461122010-05-20 16:54:10 +0200548struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200549struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100550EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200551EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200552EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
555{
556 struct map_info *map = mtd->priv;
557 struct cfi_private *cfi = map->fldrv_priv;
558 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
559 unsigned long offset = 0;
560 int i,j;
561
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000562 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000564 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 mtd->size = devsize * cfi->numchips;
566
567 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
568 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
569 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000570 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
572 goto setup_err;
573 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
576 unsigned long ernum, ersize;
577 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
578 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (mtd->erasesize < ersize) {
581 mtd->erasesize = ersize;
582 }
583 for (j=0; j<cfi->numchips; j++) {
584 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
585 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
586 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
587 }
588 offset += (ersize * ernum);
589 }
590 if (offset != devsize) {
591 /* Argh */
592 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
593 goto setup_err;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700597 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return mtd;
599
600 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100601 kfree(mtd->eraseregions);
602 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 kfree(cfi->cmdset_priv);
604 kfree(cfi->cfiq);
605 return NULL;
606}
607
608/*
609 * Return true if the chip is ready.
610 *
611 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
612 * non-suspended sector) and is indicated by no toggle bits toggling.
613 *
614 * Note that anything more complicated than checking if no bits are toggling
615 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300616 * correctly and is therefore not done (particularly with interleaved chips
617 * as each chip must be checked independently of the others).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100619static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 map_word d, t;
622
623 d = map_read(map, addr);
624 t = map_read(map, addr);
625
626 return map_word_equal(map, d, t);
627}
628
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100629/*
630 * Return true if the chip is ready and has the correct value.
631 *
632 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
633 * non-suspended sector) and it is indicated by no bits toggling.
634 *
635 * Error are indicated by toggling bits or bits held with the wrong value,
636 * or with bits toggling.
637 *
638 * Note that anything more complicated than checking if no bits are toggling
639 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300640 * correctly and is therefore not done (particularly with interleaved chips
641 * as each chip must be checked independently of the others).
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100642 *
643 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100644static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100645{
646 map_word oldd, curd;
647
648 oldd = map_read(map, addr);
649 curd = map_read(map, addr);
650
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000651 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100652 map_word_equal(map, curd, expected);
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
656{
657 DECLARE_WAITQUEUE(wait, current);
658 struct cfi_private *cfi = map->fldrv_priv;
659 unsigned long timeo;
660 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
661
662 resettime:
663 timeo = jiffies + HZ;
664 retry:
665 switch (chip->state) {
666
667 case FL_STATUS:
668 for (;;) {
669 if (chip_ready(map, adr))
670 break;
671
672 if (time_after(jiffies, timeo)) {
673 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EIO;
675 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200676 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200678 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Someone else might have been playing with it. */
680 goto retry;
681 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 case FL_READY:
684 case FL_CFI_QUERY:
685 case FL_JEDEC_QUERY:
686 return 0;
687
688 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100689 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
690 !(mode == FL_READY || mode == FL_POINT ||
691 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 goto sleep;
693
694 /* We could check to see if we're trying to access the sector
695 * that is currently being erased. However, no user will try
696 * anything like that so we just wait for the timeout. */
697
698 /* Erase suspend */
699 /* It's harmless to issue the Erase-Suspend and Erase-Resume
700 * commands when the erase algorithm isn't in progress. */
701 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
702 chip->oldstate = FL_ERASING;
703 chip->state = FL_ERASE_SUSPENDING;
704 chip->erase_suspended = 1;
705 for (;;) {
706 if (chip_ready(map, adr))
707 break;
708
709 if (time_after(jiffies, timeo)) {
710 /* Should have suspended the erase by now.
711 * Send an Erase-Resume command as either
712 * there was an error (so leave the erase
713 * routine to recover from it) or we trying to
714 * use the erase-in-progress sector. */
Tadashi Abe100f2342011-05-19 15:58:15 +0900715 put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
717 return -EIO;
718 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000719
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200720 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200722 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
724 So we can just loop here. */
725 }
726 chip->state = FL_READY;
727 return 0;
728
Todd Poynor02b15e32005-06-07 00:04:39 +0100729 case FL_XIP_WHILE_ERASING:
730 if (mode != FL_READY && mode != FL_POINT &&
731 (!cfip || !(cfip->EraseSuspend&2)))
732 goto sleep;
733 chip->oldstate = chip->state;
734 chip->state = FL_READY;
735 return 0;
736
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700737 case FL_SHUTDOWN:
738 /* The machine is rebooting */
739 return -EIO;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case FL_POINT:
742 /* Only if there's no operation suspended... */
743 if (mode == FL_READY && chip->oldstate == FL_READY)
744 return 0;
745
746 default:
747 sleep:
748 set_current_state(TASK_UNINTERRUPTIBLE);
749 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200750 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 schedule();
752 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200753 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto resettime;
755 }
756}
757
758
759static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
760{
761 struct cfi_private *cfi = map->fldrv_priv;
762
763 switch(chip->oldstate) {
764 case FL_ERASING:
Guillaume LECERF08968042010-10-26 10:45:23 +0100765 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 chip->oldstate = FL_READY;
767 chip->state = FL_ERASING;
768 break;
769
Todd Poynor02b15e32005-06-07 00:04:39 +0100770 case FL_XIP_WHILE_ERASING:
771 chip->state = chip->oldstate;
772 chip->oldstate = FL_READY;
773 break;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case FL_READY:
776 case FL_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 default:
779 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
780 }
781 wake_up(&chip->wq);
782}
783
Todd Poynor02b15e32005-06-07 00:04:39 +0100784#ifdef CONFIG_MTD_XIP
785
786/*
787 * No interrupt what so ever can be serviced while the flash isn't in array
788 * mode. This is ensured by the xip_disable() and xip_enable() functions
789 * enclosing any code path where the flash is known not to be in array mode.
790 * And within a XIP disabled code path, only functions marked with __xipram
791 * may be called and nothing else (it's a good thing to inspect generated
792 * assembly to make sure inline functions were actually inlined and that gcc
793 * didn't emit calls to its own support functions). Also configuring MTD CFI
794 * support to a single buswidth and a single interleave is also recommended.
795 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200796
Todd Poynor02b15e32005-06-07 00:04:39 +0100797static void xip_disable(struct map_info *map, struct flchip *chip,
798 unsigned long adr)
799{
800 /* TODO: chips with no XIP use should ignore and return */
801 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
802 local_irq_disable();
803}
804
805static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
806 unsigned long adr)
807{
808 struct cfi_private *cfi = map->fldrv_priv;
809
810 if (chip->state != FL_POINT && chip->state != FL_READY) {
811 map_write(map, CMD(0xf0), adr);
812 chip->state = FL_READY;
813 }
814 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200815 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100816 local_irq_enable();
817}
818
819/*
820 * When a delay is required for the flash operation to complete, the
821 * xip_udelay() function is polling for both the given timeout and pending
822 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000823 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100824 * and interrupts unmasked. Task scheduling might also happen at that
825 * point. The CPU eventually returns from the interrupt or the call to
826 * schedule() and the suspended flash operation is resumed for the remaining
827 * of the delay period.
828 *
829 * Warning: this function _will_ fool interrupt latency tracing tools.
830 */
831
832static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
833 unsigned long adr, int usec)
834{
835 struct cfi_private *cfi = map->fldrv_priv;
836 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
837 map_word status, OK = CMD(0x80);
838 unsigned long suspended, start = xip_currtime();
839 flstate_t oldstate;
840
841 do {
842 cpu_relax();
843 if (xip_irqpending() && extp &&
844 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
845 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
846 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000847 * Let's suspend the erase operation when supported.
848 * Note that we currently don't try to suspend
849 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100850 * operation suspended (imagine what happens
851 * when one chip was already done with the current
852 * operation while another chip suspended it, then
853 * we resume the whole thing at once). Yes, it
854 * can happen!
855 */
856 map_write(map, CMD(0xb0), adr);
857 usec -= xip_elapsed_since(start);
858 suspended = xip_currtime();
859 do {
860 if (xip_elapsed_since(suspended) > 100000) {
861 /*
862 * The chip doesn't want to suspend
863 * after waiting for 100 msecs.
864 * This is a critical error but there
865 * is not much we can do here.
866 */
867 return;
868 }
869 status = map_read(map, adr);
870 } while (!map_word_andequal(map, status, OK, OK));
871
872 /* Suspend succeeded */
873 oldstate = chip->state;
874 if (!map_word_bitsset(map, status, CMD(0x40)))
875 break;
876 chip->state = FL_XIP_WHILE_ERASING;
877 chip->erase_suspended = 1;
878 map_write(map, CMD(0xf0), adr);
879 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200880 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100881 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200882 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200883 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100884 cond_resched();
885
886 /*
887 * We're back. However someone else might have
888 * decided to go write to the chip if we are in
889 * a suspended erase state. If so let's wait
890 * until it's done.
891 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200892 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100893 while (chip->state != FL_XIP_WHILE_ERASING) {
894 DECLARE_WAITQUEUE(wait, current);
895 set_current_state(TASK_UNINTERRUPTIBLE);
896 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200897 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100898 schedule();
899 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200900 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100901 }
902 /* Disallow XIP again */
903 local_irq_disable();
904
905 /* Resume the write or erase operation */
Guillaume LECERF08968042010-10-26 10:45:23 +0100906 map_write(map, cfi->sector_erase_cmd, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +0100907 chip->state = oldstate;
908 start = xip_currtime();
909 } else if (usec >= 1000000/HZ) {
910 /*
911 * Try to save on CPU power when waiting delay
912 * is at least a system timer tick period.
913 * No need to be extremely accurate here.
914 */
915 xip_cpu_idle();
916 }
917 status = map_read(map, adr);
918 } while (!map_word_andequal(map, status, OK, OK)
919 && xip_elapsed_since(start) < usec);
920}
921
922#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
923
924/*
925 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
926 * the flash is actively programming or erasing since we have to poll for
927 * the operation to complete anyway. We can't do that in a generic way with
928 * a XIP setup so do it before the actual flash operation in this case
929 * and stub it out from INVALIDATE_CACHE_UDELAY.
930 */
931#define XIP_INVAL_CACHED_RANGE(map, from, size) \
932 INVALIDATE_CACHED_RANGE(map, from, size)
933
934#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
935 UDELAY(map, chip, adr, usec)
936
937/*
938 * Extra notes:
939 *
940 * Activating this XIP support changes the way the code works a bit. For
941 * example the code to suspend the current process when concurrent access
942 * happens is never executed because xip_udelay() will always return with the
943 * same chip state as it was entered with. This is why there is no care for
944 * the presence of add_wait_queue() or schedule() calls from within a couple
945 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
946 * The queueing and scheduling are always happening within xip_udelay().
947 *
948 * Similarly, get_chip() and put_chip() just happen to always be executed
949 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
950 * is in array mode, therefore never executing many cases therein and not
951 * causing any problem with XIP.
952 */
953
954#else
955
956#define xip_disable(map, chip, adr)
957#define xip_enable(map, chip, adr)
958#define XIP_INVAL_CACHED_RANGE(x...)
959
960#define UDELAY(map, chip, adr, usec) \
961do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200962 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100963 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200964 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100965} while (0)
966
967#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
968do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200969 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100970 INVALIDATE_CACHED_RANGE(map, adr, len); \
971 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200972 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100973} while (0)
974
975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
978{
979 unsigned long cmd_addr;
980 struct cfi_private *cfi = map->fldrv_priv;
981 int ret;
982
983 adr += chip->start;
984
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000985 /* Ensure cmd read/writes are aligned. */
986 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200988 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ret = get_chip(map, chip, cmd_addr, FL_READY);
990 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200991 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return ret;
993 }
994
995 if (chip->state != FL_POINT && chip->state != FL_READY) {
996 map_write(map, CMD(0xf0), cmd_addr);
997 chip->state = FL_READY;
998 }
999
1000 map_copy_from(map, buf, adr, len);
1001
1002 put_chip(map, chip, cmd_addr);
1003
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001004 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006}
1007
1008
1009static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1010{
1011 struct map_info *map = mtd->priv;
1012 struct cfi_private *cfi = map->fldrv_priv;
1013 unsigned long ofs;
1014 int chipnum;
1015 int ret = 0;
1016
1017 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 chipnum = (from >> cfi->chipshift);
1019 ofs = from - (chipnum << cfi->chipshift);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 while (len) {
1022 unsigned long thislen;
1023
1024 if (chipnum >= cfi->numchips)
1025 break;
1026
1027 if ((len + ofs -1) >> cfi->chipshift)
1028 thislen = (1<<cfi->chipshift) - ofs;
1029 else
1030 thislen = len;
1031
1032 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1033 if (ret)
1034 break;
1035
1036 *retlen += thislen;
1037 len -= thislen;
1038 buf += thislen;
1039
1040 ofs = 0;
1041 chipnum++;
1042 }
1043 return ret;
1044}
1045
1046
1047static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1048{
1049 DECLARE_WAITQUEUE(wait, current);
1050 unsigned long timeo = jiffies + HZ;
1051 struct cfi_private *cfi = map->fldrv_priv;
1052
1053 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001054 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 set_current_state(TASK_UNINTERRUPTIBLE);
1058 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001059
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001060 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 schedule();
1063 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 timeo = jiffies + HZ;
1065
1066 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 adr += chip->start;
1070
1071 chip->state = FL_READY;
1072
1073 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1074 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1075 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 map_copy_from(map, buf, adr, len);
1078
1079 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1080 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1081 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1082 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001085 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 return 0;
1088}
1089
1090static int cfi_amdstd_secsi_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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* 8 secsi bytes per chip */
1100 chipnum=from>>3;
1101 ofs=from & 7;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 while (len) {
1104 unsigned long thislen;
1105
1106 if (chipnum >= cfi->numchips)
1107 break;
1108
1109 if ((len + ofs -1) >> 3)
1110 thislen = (1<<3) - ofs;
1111 else
1112 thislen = len;
1113
1114 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1115 if (ret)
1116 break;
1117
1118 *retlen += thislen;
1119 len -= thislen;
1120 buf += thislen;
1121
1122 ofs = 0;
1123 chipnum++;
1124 }
1125 return ret;
1126}
1127
1128
Todd Poynor02b15e32005-06-07 00:04:39 +01001129static 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 -07001130{
1131 struct cfi_private *cfi = map->fldrv_priv;
1132 unsigned long timeo = jiffies + HZ;
1133 /*
1134 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1135 * have a max write time of a few hundreds usec). However, we should
1136 * use the maximum timeout value given by the chip at probe time
1137 * instead. Unfortunately, struct flchip does have a field for
1138 * maximum timeout, only for typical which can be far too short
1139 * depending of the conditions. The ' + 1' is to avoid having a
1140 * timeout of 0 jiffies if HZ is smaller than 1000.
1141 */
1142 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1143 int ret = 0;
1144 map_word oldd;
1145 int retry_cnt = 0;
1146
1147 adr += chip->start;
1148
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001149 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ret = get_chip(map, chip, adr, FL_WRITING);
1151 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001152 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return ret;
1154 }
1155
Brian Norris289c0522011-07-19 10:06:09 -07001156 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 __func__, adr, datum.x[0] );
1158
1159 /*
1160 * Check for a NOP for the case when the datum to write is already
1161 * present - it saves time and works around buggy chips that corrupt
1162 * data at other locations when 0xff is written to a location that
1163 * already contains 0xff.
1164 */
1165 oldd = map_read(map, adr);
1166 if (map_word_equal(map, oldd, datum)) {
Brian Norris289c0522011-07-19 10:06:09 -07001167 pr_debug("MTD %s(): NOP\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 __func__);
1169 goto op_done;
1170 }
1171
Todd Poynor02b15e32005-06-07 00:04:39 +01001172 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001174 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 retry:
1176 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1177 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1178 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1179 map_write(map, datum, adr);
1180 chip->state = FL_WRITING;
1181
Todd Poynor02b15e32005-06-07 00:04:39 +01001182 INVALIDATE_CACHE_UDELAY(map, chip,
1183 adr, map_bankwidth(map),
1184 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001187 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 for (;;) {
1189 if (chip->state != FL_WRITING) {
1190 /* Someone's suspended the write. Sleep */
1191 DECLARE_WAITQUEUE(wait, current);
1192
1193 set_current_state(TASK_UNINTERRUPTIBLE);
1194 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001195 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 schedule();
1197 remove_wait_queue(&chip->wq, &wait);
1198 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001199 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 continue;
1201 }
1202
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001203 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001204 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001205 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001206 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001207 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001210 if (chip_ready(map, adr))
1211 break;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001214 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001216 /* Did we succeed? */
1217 if (!chip_good(map, adr, datum)) {
1218 /* reset on all failures. */
1219 map_write( map, CMD(0xF0), chip->start );
1220 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001222 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001223 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001225 ret = -EIO;
1226 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001227 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 op_done:
1229 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001230 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001232 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 return ret;
1235}
1236
1237
1238static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1239 size_t *retlen, const u_char *buf)
1240{
1241 struct map_info *map = mtd->priv;
1242 struct cfi_private *cfi = map->fldrv_priv;
1243 int ret = 0;
1244 int chipnum;
1245 unsigned long ofs, chipstart;
1246 DECLARE_WAITQUEUE(wait, current);
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 chipnum = to >> cfi->chipshift;
1249 ofs = to - (chipnum << cfi->chipshift);
1250 chipstart = cfi->chips[chipnum].start;
1251
1252 /* If it's not bus-aligned, do the first byte write */
1253 if (ofs & (map_bankwidth(map)-1)) {
1254 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1255 int i = ofs - bus_ofs;
1256 int n = 0;
1257 map_word tmp_buf;
1258
1259 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001260 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 set_current_state(TASK_UNINTERRUPTIBLE);
1264 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1265
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001266 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 schedule();
1269 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto retry;
1271 }
1272
1273 /* Load 'tmp_buf' with old contents of flash */
1274 tmp_buf = map_read(map, bus_ofs+chipstart);
1275
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001276 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* Number of bytes to copy from buffer */
1279 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1282
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001283 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001285 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ofs += n;
1289 buf += n;
1290 (*retlen) += n;
1291 len -= n;
1292
1293 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001294 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ofs = 0;
1296 if (chipnum == cfi->numchips)
1297 return 0;
1298 }
1299 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /* We are now aligned, write as much as possible */
1302 while(len >= map_bankwidth(map)) {
1303 map_word datum;
1304
1305 datum = map_word_load(map, buf);
1306
1307 ret = do_write_oneword(map, &cfi->chips[chipnum],
1308 ofs, datum);
1309 if (ret)
1310 return ret;
1311
1312 ofs += map_bankwidth(map);
1313 buf += map_bankwidth(map);
1314 (*retlen) += map_bankwidth(map);
1315 len -= map_bankwidth(map);
1316
1317 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001318 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ofs = 0;
1320 if (chipnum == cfi->numchips)
1321 return 0;
1322 chipstart = cfi->chips[chipnum].start;
1323 }
1324 }
1325
1326 /* Write the trailing bytes if any */
1327 if (len & (map_bankwidth(map)-1)) {
1328 map_word tmp_buf;
1329
1330 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001331 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 set_current_state(TASK_UNINTERRUPTIBLE);
1335 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1336
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001337 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 schedule();
1340 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto retry1;
1342 }
1343
1344 tmp_buf = map_read(map, ofs + chipstart);
1345
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001346 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001349
1350 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001352 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 (*retlen) += len;
1356 }
1357
1358 return 0;
1359}
1360
1361
1362/*
1363 * FIXME: interleaved mode not tested, and probably not supported!
1364 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001365static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001366 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001367 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct cfi_private *cfi = map->fldrv_priv;
1370 unsigned long timeo = jiffies + HZ;
1371 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1372 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1373 int ret = -EIO;
1374 unsigned long cmd_adr;
1375 int z, words;
1376 map_word datum;
1377
1378 adr += chip->start;
1379 cmd_adr = adr;
1380
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001381 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ret = get_chip(map, chip, adr, FL_WRITING);
1383 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001384 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return ret;
1386 }
1387
1388 datum = map_word_load(map, buf);
1389
Brian Norris289c0522011-07-19 10:06:09 -07001390 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 __func__, adr, datum.x[0] );
1392
Todd Poynor02b15e32005-06-07 00:04:39 +01001393 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001395 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1398 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /* Write Buffer Load */
1401 map_write(map, CMD(0x25), cmd_adr);
1402
1403 chip->state = FL_WRITING_TO_BUFFER;
1404
1405 /* Write length of data to come */
1406 words = len / map_bankwidth(map);
1407 map_write(map, CMD(words - 1), cmd_adr);
1408 /* Write data */
1409 z = 0;
1410 while(z < words * map_bankwidth(map)) {
1411 datum = map_word_load(map, buf);
1412 map_write(map, datum, adr + z);
1413
1414 z += map_bankwidth(map);
1415 buf += map_bankwidth(map);
1416 }
1417 z -= map_bankwidth(map);
1418
1419 adr += z;
1420
1421 /* Write Buffer Program Confirm: GO GO GO */
1422 map_write(map, CMD(0x29), cmd_adr);
1423 chip->state = FL_WRITING;
1424
Todd Poynor02b15e32005-06-07 00:04:39 +01001425 INVALIDATE_CACHE_UDELAY(map, chip,
1426 adr, map_bankwidth(map),
1427 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001429 timeo = jiffies + uWriteTimeout;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 for (;;) {
1432 if (chip->state != FL_WRITING) {
1433 /* Someone's suspended the write. Sleep */
1434 DECLARE_WAITQUEUE(wait, current);
1435
1436 set_current_state(TASK_UNINTERRUPTIBLE);
1437 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001438 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 schedule();
1440 remove_wait_queue(&chip->wq, &wait);
1441 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001442 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 continue;
1444 }
1445
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001446 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1447 break;
1448
Todd Poynor02b15e32005-06-07 00:04:39 +01001449 if (chip_ready(map, adr)) {
1450 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001455 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /* reset on all failures. */
1459 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001460 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* FIXME - should have reset delay before continuing */
1462
Todd Poynor02b15e32005-06-07 00:04:39 +01001463 printk(KERN_WARNING "MTD %s(): software timeout\n",
1464 __func__ );
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ret = -EIO;
1467 op_done:
1468 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001469 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001471 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 return ret;
1474}
1475
1476
1477static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1478 size_t *retlen, const u_char *buf)
1479{
1480 struct map_info *map = mtd->priv;
1481 struct cfi_private *cfi = map->fldrv_priv;
1482 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1483 int ret = 0;
1484 int chipnum;
1485 unsigned long ofs;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 chipnum = to >> cfi->chipshift;
1488 ofs = to - (chipnum << cfi->chipshift);
1489
1490 /* If it's not bus-aligned, do the first word write */
1491 if (ofs & (map_bankwidth(map)-1)) {
1492 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1493 if (local_len > len)
1494 local_len = len;
1495 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1496 local_len, retlen, buf);
1497 if (ret)
1498 return ret;
1499 ofs += local_len;
1500 buf += local_len;
1501 len -= local_len;
1502
1503 if (ofs >> cfi->chipshift) {
1504 chipnum ++;
1505 ofs = 0;
1506 if (chipnum == cfi->numchips)
1507 return 0;
1508 }
1509 }
1510
1511 /* Write buffer is worth it only if more than one word to write... */
1512 while (len >= map_bankwidth(map) * 2) {
1513 /* We must not cross write block boundaries */
1514 int size = wbufsize - (ofs & (wbufsize-1));
1515
1516 if (size > len)
1517 size = len;
1518 if (size % map_bankwidth(map))
1519 size -= size % map_bankwidth(map);
1520
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001521 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 ofs, buf, size);
1523 if (ret)
1524 return ret;
1525
1526 ofs += size;
1527 buf += size;
1528 (*retlen) += size;
1529 len -= size;
1530
1531 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001532 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 ofs = 0;
1534 if (chipnum == cfi->numchips)
1535 return 0;
1536 }
1537 }
1538
1539 if (len) {
1540 size_t retlen_dregs = 0;
1541
1542 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1543 len, &retlen_dregs, buf);
1544
1545 *retlen += retlen_dregs;
1546 return ret;
1547 }
1548
1549 return 0;
1550}
1551
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001552/*
1553 * Wait for the flash chip to become ready to write data
1554 *
1555 * This is only called during the panic_write() path. When panic_write()
1556 * is called, the kernel is in the process of a panic, and will soon be
1557 * dead. Therefore we don't take any locks, and attempt to get access
1558 * to the chip as soon as possible.
1559 */
1560static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
1561 unsigned long adr)
1562{
1563 struct cfi_private *cfi = map->fldrv_priv;
1564 int retries = 10;
1565 int i;
1566
1567 /*
1568 * If the driver thinks the chip is idle, and no toggle bits
1569 * are changing, then the chip is actually idle for sure.
1570 */
1571 if (chip->state == FL_READY && chip_ready(map, adr))
1572 return 0;
1573
1574 /*
1575 * Try several times to reset the chip and then wait for it
1576 * to become idle. The upper limit of a few milliseconds of
1577 * delay isn't a big problem: the kernel is dying anyway. It
1578 * is more important to save the messages.
1579 */
1580 while (retries > 0) {
1581 const unsigned long timeo = (HZ / 1000) + 1;
1582
1583 /* send the reset command */
1584 map_write(map, CMD(0xF0), chip->start);
1585
1586 /* wait for the chip to become ready */
1587 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
1588 if (chip_ready(map, adr))
1589 return 0;
1590
1591 udelay(1);
1592 }
1593 }
1594
1595 /* the chip never became ready */
1596 return -EBUSY;
1597}
1598
1599/*
1600 * Write out one word of data to a single flash chip during a kernel panic
1601 *
1602 * This is only called during the panic_write() path. When panic_write()
1603 * is called, the kernel is in the process of a panic, and will soon be
1604 * dead. Therefore we don't take any locks, and attempt to get access
1605 * to the chip as soon as possible.
1606 *
1607 * The implementation of this routine is intentionally similar to
1608 * do_write_oneword(), in order to ease code maintenance.
1609 */
1610static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
1611 unsigned long adr, map_word datum)
1612{
1613 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
1614 struct cfi_private *cfi = map->fldrv_priv;
1615 int retry_cnt = 0;
1616 map_word oldd;
1617 int ret = 0;
1618 int i;
1619
1620 adr += chip->start;
1621
1622 ret = cfi_amdstd_panic_wait(map, chip, adr);
1623 if (ret)
1624 return ret;
1625
1626 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
1627 __func__, adr, datum.x[0]);
1628
1629 /*
1630 * Check for a NOP for the case when the datum to write is already
1631 * present - it saves time and works around buggy chips that corrupt
1632 * data at other locations when 0xff is written to a location that
1633 * already contains 0xff.
1634 */
1635 oldd = map_read(map, adr);
1636 if (map_word_equal(map, oldd, datum)) {
1637 pr_debug("MTD %s(): NOP\n", __func__);
1638 goto op_done;
1639 }
1640
1641 ENABLE_VPP(map);
1642
1643retry:
1644 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1645 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1646 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1647 map_write(map, datum, adr);
1648
1649 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
1650 if (chip_ready(map, adr))
1651 break;
1652
1653 udelay(1);
1654 }
1655
1656 if (!chip_good(map, adr, datum)) {
1657 /* reset on all failures. */
1658 map_write(map, CMD(0xF0), chip->start);
1659 /* FIXME - should have reset delay before continuing */
1660
1661 if (++retry_cnt <= MAX_WORD_RETRIES)
1662 goto retry;
1663
1664 ret = -EIO;
1665 }
1666
1667op_done:
1668 DISABLE_VPP(map);
1669 return ret;
1670}
1671
1672/*
1673 * Write out some data during a kernel panic
1674 *
1675 * This is used by the mtdoops driver to save the dying messages from a
1676 * kernel which has panic'd.
1677 *
1678 * This routine ignores all of the locking used throughout the rest of the
1679 * driver, in order to ensure that the data gets written out no matter what
1680 * state this driver (and the flash chip itself) was in when the kernel crashed.
1681 *
1682 * The implementation of this routine is intentionally similar to
1683 * cfi_amdstd_write_words(), in order to ease code maintenance.
1684 */
1685static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
1686 size_t *retlen, const u_char *buf)
1687{
1688 struct map_info *map = mtd->priv;
1689 struct cfi_private *cfi = map->fldrv_priv;
1690 unsigned long ofs, chipstart;
1691 int ret = 0;
1692 int chipnum;
1693
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001694 chipnum = to >> cfi->chipshift;
1695 ofs = to - (chipnum << cfi->chipshift);
1696 chipstart = cfi->chips[chipnum].start;
1697
1698 /* If it's not bus aligned, do the first byte write */
1699 if (ofs & (map_bankwidth(map) - 1)) {
1700 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
1701 int i = ofs - bus_ofs;
1702 int n = 0;
1703 map_word tmp_buf;
1704
1705 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
1706 if (ret)
1707 return ret;
1708
1709 /* Load 'tmp_buf' with old contents of flash */
1710 tmp_buf = map_read(map, bus_ofs + chipstart);
1711
1712 /* Number of bytes to copy from buffer */
1713 n = min_t(int, len, map_bankwidth(map) - i);
1714
1715 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1716
1717 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1718 bus_ofs, tmp_buf);
1719 if (ret)
1720 return ret;
1721
1722 ofs += n;
1723 buf += n;
1724 (*retlen) += n;
1725 len -= n;
1726
1727 if (ofs >> cfi->chipshift) {
1728 chipnum++;
1729 ofs = 0;
1730 if (chipnum == cfi->numchips)
1731 return 0;
1732 }
1733 }
1734
1735 /* We are now aligned, write as much as possible */
1736 while (len >= map_bankwidth(map)) {
1737 map_word datum;
1738
1739 datum = map_word_load(map, buf);
1740
1741 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1742 ofs, datum);
1743 if (ret)
1744 return ret;
1745
1746 ofs += map_bankwidth(map);
1747 buf += map_bankwidth(map);
1748 (*retlen) += map_bankwidth(map);
1749 len -= map_bankwidth(map);
1750
1751 if (ofs >> cfi->chipshift) {
1752 chipnum++;
1753 ofs = 0;
1754 if (chipnum == cfi->numchips)
1755 return 0;
1756
1757 chipstart = cfi->chips[chipnum].start;
1758 }
1759 }
1760
1761 /* Write the trailing bytes if any */
1762 if (len & (map_bankwidth(map) - 1)) {
1763 map_word tmp_buf;
1764
1765 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
1766 if (ret)
1767 return ret;
1768
1769 tmp_buf = map_read(map, ofs + chipstart);
1770
1771 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1772
1773 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
1774 ofs, tmp_buf);
1775 if (ret)
1776 return ret;
1777
1778 (*retlen) += len;
1779 }
1780
1781 return 0;
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785/*
1786 * Handle devices with one erase region, that only implement
1787 * the chip erase command.
1788 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001789static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct cfi_private *cfi = map->fldrv_priv;
1792 unsigned long timeo = jiffies + HZ;
1793 unsigned long int adr;
1794 DECLARE_WAITQUEUE(wait, current);
1795 int ret = 0;
1796
1797 adr = cfi->addr_unlock1;
1798
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001799 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 ret = get_chip(map, chip, adr, FL_WRITING);
1801 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001802 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return ret;
1804 }
1805
Brian Norris289c0522011-07-19 10:06:09 -07001806 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 __func__, chip->start );
1808
Todd Poynor02b15e32005-06-07 00:04:39 +01001809 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001811 xip_disable(map, chip, adr);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1814 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1815 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1816 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1817 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1818 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1819
1820 chip->state = FL_ERASING;
1821 chip->erase_suspended = 0;
1822 chip->in_progress_block_addr = adr;
1823
Todd Poynor02b15e32005-06-07 00:04:39 +01001824 INVALIDATE_CACHE_UDELAY(map, chip,
1825 adr, map->size,
1826 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 timeo = jiffies + (HZ*20);
1829
1830 for (;;) {
1831 if (chip->state != FL_ERASING) {
1832 /* Someone's suspended the erase. Sleep */
1833 set_current_state(TASK_UNINTERRUPTIBLE);
1834 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001835 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 schedule();
1837 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001838 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 continue;
1840 }
1841 if (chip->erase_suspended) {
1842 /* This erase was suspended and resumed.
1843 Adjust the timeout */
1844 timeo = jiffies + (HZ*20); /* FIXME */
1845 chip->erase_suspended = 0;
1846 }
1847
1848 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001851 if (time_after(jiffies, timeo)) {
1852 printk(KERN_WARNING "MTD %s(): software timeout\n",
1853 __func__ );
1854 break;
1855 }
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001858 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001860 /* Did we succeed? */
1861 if (!chip_good(map, adr, map_word_ff(map))) {
1862 /* reset on all failures. */
1863 map_write( map, CMD(0xF0), chip->start );
1864 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001866 ret = -EIO;
1867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001870 xip_enable(map, chip, adr);
Paul Parsonse7d93772012-03-07 14:11:16 +00001871 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001873 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 return ret;
1876}
1877
1878
Todd Poynor02b15e32005-06-07 00:04:39 +01001879static 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 -07001880{
1881 struct cfi_private *cfi = map->fldrv_priv;
1882 unsigned long timeo = jiffies + HZ;
1883 DECLARE_WAITQUEUE(wait, current);
1884 int ret = 0;
1885
1886 adr += chip->start;
1887
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001888 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ret = get_chip(map, chip, adr, FL_ERASING);
1890 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001891 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return ret;
1893 }
1894
Brian Norris289c0522011-07-19 10:06:09 -07001895 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 __func__, adr );
1897
Todd Poynor02b15e32005-06-07 00:04:39 +01001898 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001900 xip_disable(map, chip, adr);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1903 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1904 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1905 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1906 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Guillaume LECERF08968042010-10-26 10:45:23 +01001907 map_write(map, cfi->sector_erase_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 chip->state = FL_ERASING;
1910 chip->erase_suspended = 0;
1911 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001912
1913 INVALIDATE_CACHE_UDELAY(map, chip,
1914 adr, len,
1915 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 timeo = jiffies + (HZ*20);
1918
1919 for (;;) {
1920 if (chip->state != FL_ERASING) {
1921 /* Someone's suspended the erase. Sleep */
1922 set_current_state(TASK_UNINTERRUPTIBLE);
1923 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001924 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 schedule();
1926 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001927 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 continue;
1929 }
1930 if (chip->erase_suspended) {
1931 /* This erase was suspended and resumed.
1932 Adjust the timeout */
1933 timeo = jiffies + (HZ*20); /* FIXME */
1934 chip->erase_suspended = 0;
1935 }
1936
Todd Poynor02b15e32005-06-07 00:04:39 +01001937 if (chip_ready(map, adr)) {
1938 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001942 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001943 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001944 printk(KERN_WARNING "MTD %s(): software timeout\n",
1945 __func__ );
1946 break;
1947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001950 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001952 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001953 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001954 /* reset on all failures. */
1955 map_write( map, CMD(0xF0), chip->start );
1956 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001958 ret = -EIO;
1959 }
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001962 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001964 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return ret;
1966}
1967
1968
Ben Dooksce0f33a2007-05-28 19:59:00 +01001969static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 unsigned long ofs, len;
1972 int ret;
1973
1974 ofs = instr->addr;
1975 len = instr->len;
1976
1977 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1978 if (ret)
1979 return ret;
1980
1981 instr->state = MTD_ERASE_DONE;
1982 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return 0;
1985}
1986
1987
1988static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1989{
1990 struct map_info *map = mtd->priv;
1991 struct cfi_private *cfi = map->fldrv_priv;
1992 int ret = 0;
1993
1994 if (instr->addr != 0)
1995 return -EINVAL;
1996
1997 if (instr->len != mtd->size)
1998 return -EINVAL;
1999
2000 ret = do_erase_chip(map, &cfi->chips[0]);
2001 if (ret)
2002 return ret;
2003
2004 instr->state = MTD_ERASE_DONE;
2005 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 0;
2008}
2009
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002010static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2011 unsigned long adr, int len, void *thunk)
2012{
2013 struct cfi_private *cfi = map->fldrv_priv;
2014 int ret;
2015
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002016 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002017 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2018 if (ret)
2019 goto out_unlock;
2020 chip->state = FL_LOCKING;
2021
Brian Norris0a32a102011-07-19 10:06:10 -07002022 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002023
2024 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2025 cfi->device_type, NULL);
2026 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2027 cfi->device_type, NULL);
2028 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2029 cfi->device_type, NULL);
2030 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2031 cfi->device_type, NULL);
2032 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2033 cfi->device_type, NULL);
2034 map_write(map, CMD(0x40), chip->start + adr);
2035
2036 chip->state = FL_READY;
2037 put_chip(map, chip, adr + chip->start);
2038 ret = 0;
2039
2040out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002041 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002042 return ret;
2043}
2044
2045static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2046 unsigned long adr, int len, void *thunk)
2047{
2048 struct cfi_private *cfi = map->fldrv_priv;
2049 int ret;
2050
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002051 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002052 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2053 if (ret)
2054 goto out_unlock;
2055 chip->state = FL_UNLOCKING;
2056
Brian Norris0a32a102011-07-19 10:06:10 -07002057 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002058
2059 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2060 cfi->device_type, NULL);
2061 map_write(map, CMD(0x70), adr);
2062
2063 chip->state = FL_READY;
2064 put_chip(map, chip, adr + chip->start);
2065 ret = 0;
2066
2067out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002068 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002069 return ret;
2070}
2071
Adrian Hunter69423d92008-12-10 13:37:21 +00002072static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002073{
2074 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2075}
2076
Adrian Hunter69423d92008-12-10 13:37:21 +00002077static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002078{
2079 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083static void cfi_amdstd_sync (struct mtd_info *mtd)
2084{
2085 struct map_info *map = mtd->priv;
2086 struct cfi_private *cfi = map->fldrv_priv;
2087 int i;
2088 struct flchip *chip;
2089 int ret = 0;
2090 DECLARE_WAITQUEUE(wait, current);
2091
2092 for (i=0; !ret && i<cfi->numchips; i++) {
2093 chip = &cfi->chips[i];
2094
2095 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002096 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 switch(chip->state) {
2099 case FL_READY:
2100 case FL_STATUS:
2101 case FL_CFI_QUERY:
2102 case FL_JEDEC_QUERY:
2103 chip->oldstate = chip->state;
2104 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002105 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * as the whole point is that nobody can do anything
2107 * with the chip now anyway.
2108 */
2109 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002110 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 break;
2112
2113 default:
2114 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07002115 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002117
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002118 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 schedule();
2121
2122 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 goto retry;
2125 }
2126 }
2127
2128 /* Unlock the chips again */
2129
2130 for (i--; i >=0; i--) {
2131 chip = &cfi->chips[i];
2132
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002133 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (chip->state == FL_SYNCING) {
2136 chip->state = chip->oldstate;
2137 wake_up(&chip->wq);
2138 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002139 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141}
2142
2143
2144static int cfi_amdstd_suspend(struct mtd_info *mtd)
2145{
2146 struct map_info *map = mtd->priv;
2147 struct cfi_private *cfi = map->fldrv_priv;
2148 int i;
2149 struct flchip *chip;
2150 int ret = 0;
2151
2152 for (i=0; !ret && i<cfi->numchips; i++) {
2153 chip = &cfi->chips[i];
2154
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002155 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 switch(chip->state) {
2158 case FL_READY:
2159 case FL_STATUS:
2160 case FL_CFI_QUERY:
2161 case FL_JEDEC_QUERY:
2162 chip->oldstate = chip->state;
2163 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002164 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * as the whole point is that nobody can do anything
2166 * with the chip now anyway.
2167 */
2168 case FL_PM_SUSPENDED:
2169 break;
2170
2171 default:
2172 ret = -EAGAIN;
2173 break;
2174 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002175 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
2178 /* Unlock the chips again */
2179
2180 if (ret) {
2181 for (i--; i >=0; i--) {
2182 chip = &cfi->chips[i];
2183
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002184 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (chip->state == FL_PM_SUSPENDED) {
2187 chip->state = chip->oldstate;
2188 wake_up(&chip->wq);
2189 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002190 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return ret;
2195}
2196
2197
2198static void cfi_amdstd_resume(struct mtd_info *mtd)
2199{
2200 struct map_info *map = mtd->priv;
2201 struct cfi_private *cfi = map->fldrv_priv;
2202 int i;
2203 struct flchip *chip;
2204
2205 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 chip = &cfi->chips[i];
2208
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002209 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (chip->state == FL_PM_SUSPENDED) {
2212 chip->state = FL_READY;
2213 map_write(map, CMD(0xF0), chip->start);
2214 wake_up(&chip->wq);
2215 }
2216 else
2217 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2218
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002219 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221}
2222
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002223
2224/*
2225 * Ensure that the flash device is put back into read array mode before
2226 * unloading the driver or rebooting. On some systems, rebooting while
2227 * the flash is in query/program/erase mode will prevent the CPU from
2228 * fetching the bootloader code, requiring a hard reset or power cycle.
2229 */
2230static int cfi_amdstd_reset(struct mtd_info *mtd)
2231{
2232 struct map_info *map = mtd->priv;
2233 struct cfi_private *cfi = map->fldrv_priv;
2234 int i, ret;
2235 struct flchip *chip;
2236
2237 for (i = 0; i < cfi->numchips; i++) {
2238
2239 chip = &cfi->chips[i];
2240
2241 mutex_lock(&chip->mutex);
2242
2243 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2244 if (!ret) {
2245 map_write(map, CMD(0xF0), chip->start);
2246 chip->state = FL_SHUTDOWN;
2247 put_chip(map, chip, chip->start);
2248 }
2249
2250 mutex_unlock(&chip->mutex);
2251 }
2252
2253 return 0;
2254}
2255
2256
2257static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2258 void *v)
2259{
2260 struct mtd_info *mtd;
2261
2262 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2263 cfi_amdstd_reset(mtd);
2264 return NOTIFY_DONE;
2265}
2266
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static void cfi_amdstd_destroy(struct mtd_info *mtd)
2269{
2270 struct map_info *map = mtd->priv;
2271 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002272
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002273 cfi_amdstd_reset(mtd);
2274 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 kfree(cfi->cmdset_priv);
2276 kfree(cfi->cfiq);
2277 kfree(cfi);
2278 kfree(mtd->eraseregions);
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281MODULE_LICENSE("GPL");
2282MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2283MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002284MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002285MODULE_ALIAS("cfi_cmdset_0701");