blob: bf313be6ee265ce90486bd92bbe90db32030a287 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
28#include <asm/byteorder.h>
29
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/delay.h>
33#include <linux/interrupt.h>
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070034#include <linux/reboot.h>
Stefan Roese1648eaa2013-01-18 13:10:05 +010035#include <linux/of.h>
36#include <linux/of_platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mtd/map.h>
38#include <linux/mtd/mtd.h>
39#include <linux/mtd/cfi.h>
Todd Poynor02b15e32005-06-07 00:04:39 +010040#include <linux/mtd/xip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define AMD_BOOTLOC_BUG
43#define FORCE_WORD_WRITE 0
44
45#define MAX_WORD_RETRIES 3
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070048#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010049#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020050#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
53static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
55static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
56static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
57static void cfi_amdstd_sync (struct mtd_info *);
58static int cfi_amdstd_suspend (struct mtd_info *);
59static void cfi_amdstd_resume (struct mtd_info *);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070060static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
Christian Rieschdc7e9ec2014-03-06 13:18:27 +010061static int cfi_amdstd_get_fact_prot_info(struct mtd_info *, size_t,
62 size_t *, struct otp_info *);
63static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t,
64 size_t *, struct otp_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Christian Rieschdc7e9ec2014-03-06 13:18:27 +010066static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t,
67 size_t *, u_char *);
68static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t,
69 size_t *, u_char *);
Christian Rieschaf744752014-03-06 13:18:29 +010070static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t,
71 size_t *, u_char *);
Christian Riesch4f5cb242014-03-06 13:18:30 +010072static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Ira W. Snyder30ec5a22012-01-06 11:29:19 -080074static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
75 size_t *retlen, const u_char *buf);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void cfi_amdstd_destroy(struct mtd_info *);
78
79struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
80static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
81
82static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
83static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
84#include "fwh_lock.h"
85
Adrian Hunter69423d92008-12-10 13:37:21 +000086static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
87static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +020088
Stefan Roese1648eaa2013-01-18 13:10:05 +010089static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
90static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
91static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static struct mtd_chip_driver cfi_amdstd_chipdrv = {
94 .probe = NULL, /* Not usable directly */
95 .destroy = cfi_amdstd_destroy,
96 .name = "cfi_cmdset_0002",
97 .module = THIS_MODULE
98};
99
100
101/* #define DEBUG_CFI_FEATURES */
102
103
104#ifdef DEBUG_CFI_FEATURES
105static void cfi_tell_features(struct cfi_pri_amdstd *extp)
106{
107 const char* erase_suspend[3] = {
108 "Not supported", "Read only", "Read/write"
109 };
110 const char* top_bottom[6] = {
111 "No WP", "8x8KiB sectors at top & bottom, no WP",
112 "Bottom boot", "Top boot",
113 "Uniform, Bottom WP", "Uniform, Top WP"
114 };
115
116 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000117 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 (extp->SiliconRevision & 1) ? "Not required" : "Required");
119
120 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
121 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
122 else
123 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
124
125 if (extp->BlkProt == 0)
126 printk(" Block protection: Not supported\n");
127 else
128 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
129
130
131 printk(" Temporary block unprotect: %s\n",
132 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
133 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
134 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
135 printk(" Burst mode: %s\n",
136 extp->BurstMode ? "Supported" : "Not supported");
137 if (extp->PageMode == 0)
138 printk(" Page mode: Not supported\n");
139 else
140 printk(" Page mode: %d word page\n", extp->PageMode << 2);
141
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000142 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000144 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 extp->VppMax >> 4, extp->VppMax & 0xf);
146
147 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
148 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
149 else
150 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
151}
152#endif
153
154#ifdef AMD_BOOTLOC_BUG
155/* Wheee. Bring me the head of someone at AMD. */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100156static void fixup_amd_bootblock(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct map_info *map = mtd->priv;
159 struct cfi_private *cfi = map->fldrv_priv;
160 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
161 __u8 major = extp->MajorVersion;
162 __u8 minor = extp->MinorVersion;
163
164 if (((major << 8) | minor) < 0x3131) {
165 /* CFI version 1.0 => don't trust bootloc */
Christopher Moore87e92c02008-10-17 05:32:22 +0200166
Brian Norris289c0522011-07-19 10:06:09 -0700167 pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
Christopher Moore87e92c02008-10-17 05:32:22 +0200168 map->name, cfi->mfr, cfi->id);
169
170 /* AFAICS all 29LV400 with a bottom boot block have a device ID
171 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
172 * These were badly detected as they have the 0x80 bit set
173 * so treat them as a special case.
174 */
175 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
176
177 /* Macronix added CFI to their 2nd generation
178 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
179 * Fujitsu, Spansion, EON, ESI and older Macronix)
180 * has CFI.
181 *
182 * Therefore also check the manufacturer.
183 * This reduces the risk of false detection due to
184 * the 8-bit device ID.
185 */
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100186 (cfi->mfr == CFI_MFR_MACRONIX)) {
Brian Norris289c0522011-07-19 10:06:09 -0700187 pr_debug("%s: Macronix MX29LV400C with bottom boot block"
Christopher Moore87e92c02008-10-17 05:32:22 +0200188 " detected\n", map->name);
189 extp->TopBottom = 2; /* bottom boot */
190 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (cfi->id & 0x80) {
192 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
193 extp->TopBottom = 3; /* top boot */
194 } else {
195 extp->TopBottom = 2; /* bottom boot */
196 }
Christopher Moore87e92c02008-10-17 05:32:22 +0200197
Brian Norris289c0522011-07-19 10:06:09 -0700198 pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
Christopher Moore87e92c02008-10-17 05:32:22 +0200199 " deduced %s from Device ID\n", map->name, major, minor,
200 extp->TopBottom == 2 ? "bottom" : "top");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
203#endif
204
Guillaume LECERFcc318222010-11-17 12:35:50 +0100205static void fixup_use_write_buffers(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct map_info *map = mtd->priv;
208 struct cfi_private *cfi = map->fldrv_priv;
209 if (cfi->cfiq->BufWriteTimeoutTyp) {
Brian Norris289c0522011-07-19 10:06:09 -0700210 pr_debug("Using buffer write method\n" );
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200211 mtd->_write = cfi_amdstd_write_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213}
214
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200215/* Atmel chips don't use the same PRI format as AMD chips */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100216static void fixup_convert_atmel_pri(struct mtd_info *mtd)
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200217{
218 struct map_info *map = mtd->priv;
219 struct cfi_private *cfi = map->fldrv_priv;
220 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
221 struct cfi_pri_atmel atmel_pri;
222
223 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200224 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200225
226 if (atmel_pri.Features & 0x02)
227 extp->EraseSuspend = 2;
228
Haavard Skinnemoenbe8f78b2008-09-30 13:55:33 +0200229 /* Some chips got it backwards... */
230 if (cfi->id == AT49BV6416) {
231 if (atmel_pri.BottomBoot)
232 extp->TopBottom = 3;
233 else
234 extp->TopBottom = 2;
235 } else {
236 if (atmel_pri.BottomBoot)
237 extp->TopBottom = 2;
238 else
239 extp->TopBottom = 3;
240 }
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100241
242 /* burst write mode not supported */
243 cfi->cfiq->BufWriteTimeoutTyp = 0;
244 cfi->cfiq->BufWriteTimeoutMax = 0;
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200245}
246
Guillaume LECERFcc318222010-11-17 12:35:50 +0100247static void fixup_use_secsi(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 /* Setup for chips with a secsi area */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200250 mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
251 mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Guillaume LECERFcc318222010-11-17 12:35:50 +0100254static void fixup_use_erase_chip(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct map_info *map = mtd->priv;
257 struct cfi_private *cfi = map->fldrv_priv;
258 if ((cfi->cfiq->NumEraseRegions == 1) &&
259 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200260 mtd->_erase = cfi_amdstd_erase_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200265/*
266 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
267 * locked by default.
268 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100269static void fixup_use_atmel_lock(struct mtd_info *mtd)
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200270{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200271 mtd->_lock = cfi_atmel_lock;
272 mtd->_unlock = cfi_atmel_unlock;
Justin Treone619a752008-01-30 10:25:49 -0800273 mtd->flags |= MTD_POWERUP_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200274}
275
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200276static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
277{
278 struct map_info *map = mtd->priv;
279 struct cfi_private *cfi = map->fldrv_priv;
280
281 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300282 * These flashes report two separate eraseblock regions based on the
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200283 * sector_erase-size and block_erase-size, although they both operate on the
284 * same memory. This is not allowed according to CFI, so we just pick the
285 * sector_erase-size.
286 */
287 cfi->cfiq->NumEraseRegions = 1;
288}
289
Guillaume LECERFcc318222010-11-17 12:35:50 +0100290static void fixup_sst39vf(struct mtd_info *mtd)
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200291{
292 struct map_info *map = mtd->priv;
293 struct cfi_private *cfi = map->fldrv_priv;
294
295 fixup_old_sst_eraseregion(mtd);
296
297 cfi->addr_unlock1 = 0x5555;
298 cfi->addr_unlock2 = 0x2AAA;
299}
300
Guillaume LECERFcc318222010-11-17 12:35:50 +0100301static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200302{
303 struct map_info *map = mtd->priv;
304 struct cfi_private *cfi = map->fldrv_priv;
305
306 fixup_old_sst_eraseregion(mtd);
307
308 cfi->addr_unlock1 = 0x555;
309 cfi->addr_unlock2 = 0x2AA;
Guillaume LECERF08968042010-10-26 10:45:23 +0100310
311 cfi->sector_erase_cmd = CMD(0x50);
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200312}
313
Guillaume LECERFcc318222010-11-17 12:35:50 +0100314static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100315{
316 struct map_info *map = mtd->priv;
317 struct cfi_private *cfi = map->fldrv_priv;
318
Guillaume LECERFcc318222010-11-17 12:35:50 +0100319 fixup_sst39vf_rev_b(mtd);
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100320
321 /*
322 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
323 * it should report a size of 8KBytes (0x0020*256).
324 */
325 cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
326 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
327}
328
Guillaume LECERFcc318222010-11-17 12:35:50 +0100329static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700330{
331 struct map_info *map = mtd->priv;
332 struct cfi_private *cfi = map->fldrv_priv;
333
334 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
335 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
David Woodhouse5df41de2012-05-13 23:34:24 -0500336 pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
Trent Piepho70b07252008-03-30 21:19:30 -0700337 }
338}
339
Guillaume LECERFcc318222010-11-17 12:35:50 +0100340static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700341{
342 struct map_info *map = mtd->priv;
343 struct cfi_private *cfi = map->fldrv_priv;
344
345 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
346 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
David Woodhouse5df41de2012-05-13 23:34:24 -0500347 pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
Trent Piepho70b07252008-03-30 21:19:30 -0700348 }
349}
350
Javier Martin43dc03c2012-05-11 12:15:41 +0200351static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
352{
353 struct map_info *map = mtd->priv;
354 struct cfi_private *cfi = map->fldrv_priv;
355
356 /*
357 * S29NS512P flash uses more than 8bits to report number of sectors,
358 * which is not permitted by CFI.
359 */
360 cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
David Woodhouse5df41de2012-05-13 23:34:24 -0500361 pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
Javier Martin43dc03c2012-05-11 12:15:41 +0200362}
363
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200364/* Used to fix CFI-Tables of chips without Extended Query Tables */
365static struct cfi_fixup cfi_nopri_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100366 { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
367 { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
368 { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
369 { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
370 { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
371 { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
372 { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
373 { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
374 { 0, 0, NULL }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200375};
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static struct cfi_fixup cfi_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100378 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#ifdef AMD_BOOTLOC_BUG
Guillaume LECERFcc318222010-11-17 12:35:50 +0100380 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
Steffen Sledz1065cda2011-03-10 09:05:12 +0100381 { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
Guillaume LECERFcc318222010-11-17 12:35:50 +0100382 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100384 { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
385 { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
386 { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
387 { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
388 { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
389 { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
390 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
391 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
392 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
393 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
Javier Martin43dc03c2012-05-11 12:15:41 +0200394 { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
Guillaume LECERFcc318222010-11-17 12:35:50 +0100395 { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
396 { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
397 { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
398 { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#if !FORCE_WORD_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100400 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100402 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100405 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
406 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
407 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
408 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
411static struct cfi_fixup fixup_table[] = {
412 /* The CFI vendor ids and the JEDEC vendor IDs appear
413 * to be common. It is like the devices id's are as
414 * well. This table is to pick all cases where
415 * we know that is the case.
416 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100417 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
418 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
419 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
422
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100423static void cfi_fixup_major_minor(struct cfi_private *cfi,
424 struct cfi_pri_amdstd *extp)
425{
Guillaume LECERFe6372762010-12-17 10:59:41 +0100426 if (cfi->mfr == CFI_MFR_SAMSUNG) {
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100427 if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
428 (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
Guillaume LECERFe6372762010-12-17 10:59:41 +0100429 /*
430 * Samsung K8P2815UQB and K8D6x16UxM chips
431 * report major=0 / minor=0.
Guillaume LECERFe8953b72010-12-17 10:59:47 +0100432 * K8D3x16UxC chips report major=3 / minor=3.
Guillaume LECERFe6372762010-12-17 10:59:41 +0100433 */
434 printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
435 " Extended Query version to 1.%c\n",
436 extp->MinorVersion);
437 extp->MajorVersion = '1';
438 }
439 }
440
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100441 /*
442 * SST 38VF640x chips report major=0xFF / minor=0xFF.
443 */
444 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
445 extp->MajorVersion = '1';
446 extp->MinorVersion = '0';
447 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100448}
449
Gerlando Falauto42096282012-07-03 09:09:47 +0200450static int is_m29ew(struct cfi_private *cfi)
451{
452 if (cfi->mfr == CFI_MFR_INTEL &&
453 ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
454 (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
455 return 1;
456 return 0;
457}
458
459/*
460 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
461 * Some revisions of the M29EW suffer from erase suspend hang ups. In
462 * particular, it can occur when the sequence
463 * Erase Confirm -> Suspend -> Program -> Resume
464 * causes a lockup due to internal timing issues. The consequence is that the
465 * erase cannot be resumed without inserting a dummy command after programming
466 * and prior to resuming. [...] The work-around is to issue a dummy write cycle
467 * that writes an F0 command code before the RESUME command.
468 */
469static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
470 unsigned long adr)
471{
472 struct cfi_private *cfi = map->fldrv_priv;
473 /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
474 if (is_m29ew(cfi))
475 map_write(map, CMD(0xF0), adr);
476}
477
478/*
479 * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
480 *
481 * Some revisions of the M29EW (for example, A1 and A2 step revisions)
482 * are affected by a problem that could cause a hang up when an ERASE SUSPEND
483 * command is issued after an ERASE RESUME operation without waiting for a
484 * minimum delay. The result is that once the ERASE seems to be completed
485 * (no bits are toggling), the contents of the Flash memory block on which
486 * the erase was ongoing could be inconsistent with the expected values
487 * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
488 * values), causing a consequent failure of the ERASE operation.
489 * The occurrence of this issue could be high, especially when file system
490 * operations on the Flash are intensive. As a result, it is recommended
491 * that a patch be applied. Intensive file system operations can cause many
492 * calls to the garbage routine to free Flash space (also by erasing physical
493 * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
494 * commands can occur. The problem disappears when a delay is inserted after
495 * the RESUME command by using the udelay() function available in Linux.
496 * The DELAY value must be tuned based on the customer's platform.
497 * The maximum value that fixes the problem in all cases is 500us.
498 * But, in our experience, a delay of 30 µs to 50 µs is sufficient
499 * in most cases.
500 * We have chosen 500µs because this latency is acceptable.
501 */
502static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
503{
504 /*
505 * Resolving the Delay After Resume Issue see Micron TN-13-07
506 * Worst case delay must be 500µs but 30-50µs should be ok as well
507 */
508 if (is_m29ew(cfi))
509 cfi_udelay(500);
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
513{
514 struct cfi_private *cfi = map->fldrv_priv;
Stefan Roese1648eaa2013-01-18 13:10:05 +0100515 struct device_node __maybe_unused *np = map->device_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct mtd_info *mtd;
517 int i;
518
Burman Yan95b93a02006-11-15 21:10:29 +0200519 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Jingoo Han5c8b1fb2014-02-06 15:19:35 +0900520 if (!mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 mtd->priv = map;
523 mtd->type = MTD_NORFLASH;
524
525 /* Fill in the default mtd operations */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200526 mtd->_erase = cfi_amdstd_erase_varsize;
527 mtd->_write = cfi_amdstd_write_words;
528 mtd->_read = cfi_amdstd_read;
529 mtd->_sync = cfi_amdstd_sync;
530 mtd->_suspend = cfi_amdstd_suspend;
531 mtd->_resume = cfi_amdstd_resume;
Christian Rieschdc7e9ec2014-03-06 13:18:27 +0100532 mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg;
533 mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg;
534 mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info;
535 mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info;
Christian Rieschaf744752014-03-06 13:18:29 +0100536 mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg;
Christian Riesch4f5cb242014-03-06 13:18:30 +0100537 mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 mtd->flags = MTD_CAP_NORFLASH;
539 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400540 mtd->writesize = 1;
Anatolij Gustschin13ce77f2011-02-10 16:01:46 +0100541 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
Anatolij Gustschind261c722010-12-16 23:42:15 +0100542
Brian Norris0a32a102011-07-19 10:06:10 -0700543 pr_debug("MTD %s(): write buffer size %d\n", __func__,
544 mtd->writebufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200546 mtd->_panic_write = cfi_amdstd_panic_write;
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700547 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (cfi->cfi_mode==CFI_MODE_CFI){
550 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
552 struct cfi_pri_amdstd *extp;
553
554 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200555 if (extp) {
556 /*
557 * It's a real CFI chip, not one for which the probe
558 * routine faked a CFI structure.
559 */
560 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200562 /*
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200563 * 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 +0200564 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
565 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
Guillaume LECERF5da19532010-08-05 13:55:24 +0200566 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200567 * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200568 */
Guillaume LECERF564b8492010-04-24 17:58:17 +0200569 if (extp->MajorVersion != '1' ||
Gernot Hoylerc9ddab22011-04-11 15:53:35 +0200570 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
Guillaume LECERF564b8492010-04-24 17:58:17 +0200571 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200572 "version %c.%c (%#02x/%#02x).\n",
573 extp->MajorVersion, extp->MinorVersion,
574 extp->MajorVersion, extp->MinorVersion);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200575 kfree(extp);
576 kfree(mtd);
577 return NULL;
578 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100579
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200580 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
581 extp->MajorVersion, extp->MinorVersion);
582
Guillaume LECERF564b8492010-04-24 17:58:17 +0200583 /* Install our own private info structure */
584 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100585
Guillaume LECERF564b8492010-04-24 17:58:17 +0200586 /* Apply cfi device specific fixups */
587 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200590 /* Tell the user about it in lots of lovely detail */
591 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Stefan Roese1648eaa2013-01-18 13:10:05 +0100594#ifdef CONFIG_OF
595 if (np && of_property_read_bool(
596 np, "use-advanced-sector-protection")
597 && extp->BlkProtUnprot == 8) {
598 printk(KERN_INFO " Advanced Sector Protection (PPB Locking) supported\n");
599 mtd->_lock = cfi_ppb_lock;
600 mtd->_unlock = cfi_ppb_unlock;
601 mtd->_is_locked = cfi_ppb_is_locked;
602 }
603#endif
604
Guillaume LECERF564b8492010-04-24 17:58:17 +0200605 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100606 if ((bootloc < 2) || (bootloc > 5)) {
607 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
608 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100609 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200610 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200612
613 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100614 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200615
616 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
617 int j = (cfi->cfiq->NumEraseRegions-1)-i;
618 __u32 swap;
619
620 swap = cfi->cfiq->EraseRegionInfo[i];
621 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
622 cfi->cfiq->EraseRegionInfo[j] = swap;
623 }
624 }
625 /* Set the default CFI lock/unlock addresses */
626 cfi->addr_unlock1 = 0x555;
627 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200629 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200630
631 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
632 kfree(mtd);
633 return NULL;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 } /* CFI mode */
637 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
638 /* Apply jedec specific fixups */
639 cfi_fixup(mtd, jedec_fixup_table);
640 }
641 /* Apply generic fixups */
642 cfi_fixup(mtd, fixup_table);
643
644 for (i=0; i< cfi->numchips; i++) {
645 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
646 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
647 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800648 cfi->chips[i].ref_point_counter = 0;
649 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return cfi_amdstd_setup(mtd);
655}
Guillaume LECERF80461122010-05-20 16:54:10 +0200656struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200657struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100658EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200659EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200660EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
663{
664 struct map_info *map = mtd->priv;
665 struct cfi_private *cfi = map->fldrv_priv;
666 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
667 unsigned long offset = 0;
668 int i,j;
669
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000670 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000672 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 mtd->size = devsize * cfi->numchips;
674
675 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
676 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
677 * mtd->numeraseregions, GFP_KERNEL);
Jingoo Han5c8b1fb2014-02-06 15:19:35 +0900678 if (!mtd->eraseregions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto setup_err;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
682 unsigned long ernum, ersize;
683 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
684 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (mtd->erasesize < ersize) {
687 mtd->erasesize = ersize;
688 }
689 for (j=0; j<cfi->numchips; j++) {
690 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
691 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
692 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
693 }
694 offset += (ersize * ernum);
695 }
696 if (offset != devsize) {
697 /* Argh */
698 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
699 goto setup_err;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700703 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return mtd;
705
706 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100707 kfree(mtd->eraseregions);
708 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 kfree(cfi->cmdset_priv);
710 kfree(cfi->cfiq);
711 return NULL;
712}
713
714/*
715 * Return true if the chip is ready.
716 *
717 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
718 * non-suspended sector) and is indicated by no toggle bits toggling.
719 *
720 * Note that anything more complicated than checking if no bits are toggling
721 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300722 * correctly and is therefore not done (particularly with interleaved chips
723 * as each chip must be checked independently of the others).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100725static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 map_word d, t;
728
729 d = map_read(map, addr);
730 t = map_read(map, addr);
731
732 return map_word_equal(map, d, t);
733}
734
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100735/*
736 * Return true if the chip is ready and has the correct value.
737 *
738 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
739 * non-suspended sector) and it is indicated by no bits toggling.
740 *
741 * Error are indicated by toggling bits or bits held with the wrong value,
742 * or with bits toggling.
743 *
744 * Note that anything more complicated than checking if no bits are toggling
745 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300746 * correctly and is therefore not done (particularly with interleaved chips
747 * as each chip must be checked independently of the others).
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100748 *
749 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100750static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100751{
752 map_word oldd, curd;
753
754 oldd = map_read(map, addr);
755 curd = map_read(map, addr);
756
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000757 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100758 map_word_equal(map, curd, expected);
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
762{
763 DECLARE_WAITQUEUE(wait, current);
764 struct cfi_private *cfi = map->fldrv_priv;
765 unsigned long timeo;
766 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
767
768 resettime:
769 timeo = jiffies + HZ;
770 retry:
771 switch (chip->state) {
772
773 case FL_STATUS:
774 for (;;) {
775 if (chip_ready(map, adr))
776 break;
777
778 if (time_after(jiffies, timeo)) {
779 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EIO;
781 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200782 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200784 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Someone else might have been playing with it. */
786 goto retry;
787 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 case FL_READY:
790 case FL_CFI_QUERY:
791 case FL_JEDEC_QUERY:
792 return 0;
793
794 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100795 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
796 !(mode == FL_READY || mode == FL_POINT ||
797 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto sleep;
799
800 /* We could check to see if we're trying to access the sector
801 * that is currently being erased. However, no user will try
802 * anything like that so we just wait for the timeout. */
803
804 /* Erase suspend */
805 /* It's harmless to issue the Erase-Suspend and Erase-Resume
806 * commands when the erase algorithm isn't in progress. */
807 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
808 chip->oldstate = FL_ERASING;
809 chip->state = FL_ERASE_SUSPENDING;
810 chip->erase_suspended = 1;
811 for (;;) {
812 if (chip_ready(map, adr))
813 break;
814
815 if (time_after(jiffies, timeo)) {
816 /* Should have suspended the erase by now.
817 * Send an Erase-Resume command as either
818 * there was an error (so leave the erase
819 * routine to recover from it) or we trying to
820 * use the erase-in-progress sector. */
Tadashi Abe100f2342011-05-19 15:58:15 +0900821 put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
823 return -EIO;
824 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000825
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200826 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200828 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
830 So we can just loop here. */
831 }
832 chip->state = FL_READY;
833 return 0;
834
Todd Poynor02b15e32005-06-07 00:04:39 +0100835 case FL_XIP_WHILE_ERASING:
836 if (mode != FL_READY && mode != FL_POINT &&
837 (!cfip || !(cfip->EraseSuspend&2)))
838 goto sleep;
839 chip->oldstate = chip->state;
840 chip->state = FL_READY;
841 return 0;
842
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700843 case FL_SHUTDOWN:
844 /* The machine is rebooting */
845 return -EIO;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 case FL_POINT:
848 /* Only if there's no operation suspended... */
849 if (mode == FL_READY && chip->oldstate == FL_READY)
850 return 0;
851
852 default:
853 sleep:
854 set_current_state(TASK_UNINTERRUPTIBLE);
855 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200856 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 schedule();
858 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200859 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto resettime;
861 }
862}
863
864
865static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
866{
867 struct cfi_private *cfi = map->fldrv_priv;
868
869 switch(chip->oldstate) {
870 case FL_ERASING:
Gerlando Falauto42096282012-07-03 09:09:47 +0200871 cfi_fixup_m29ew_erase_suspend(map,
872 chip->in_progress_block_addr);
Guillaume LECERF08968042010-10-26 10:45:23 +0100873 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Gerlando Falauto42096282012-07-03 09:09:47 +0200874 cfi_fixup_m29ew_delay_after_resume(cfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 chip->oldstate = FL_READY;
876 chip->state = FL_ERASING;
877 break;
878
Todd Poynor02b15e32005-06-07 00:04:39 +0100879 case FL_XIP_WHILE_ERASING:
880 chip->state = chip->oldstate;
881 chip->oldstate = FL_READY;
882 break;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case FL_READY:
885 case FL_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 default:
888 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
889 }
890 wake_up(&chip->wq);
891}
892
Todd Poynor02b15e32005-06-07 00:04:39 +0100893#ifdef CONFIG_MTD_XIP
894
895/*
896 * No interrupt what so ever can be serviced while the flash isn't in array
897 * mode. This is ensured by the xip_disable() and xip_enable() functions
898 * enclosing any code path where the flash is known not to be in array mode.
899 * And within a XIP disabled code path, only functions marked with __xipram
900 * may be called and nothing else (it's a good thing to inspect generated
901 * assembly to make sure inline functions were actually inlined and that gcc
902 * didn't emit calls to its own support functions). Also configuring MTD CFI
903 * support to a single buswidth and a single interleave is also recommended.
904 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200905
Todd Poynor02b15e32005-06-07 00:04:39 +0100906static void xip_disable(struct map_info *map, struct flchip *chip,
907 unsigned long adr)
908{
909 /* TODO: chips with no XIP use should ignore and return */
910 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
911 local_irq_disable();
912}
913
914static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
915 unsigned long adr)
916{
917 struct cfi_private *cfi = map->fldrv_priv;
918
919 if (chip->state != FL_POINT && chip->state != FL_READY) {
920 map_write(map, CMD(0xf0), adr);
921 chip->state = FL_READY;
922 }
923 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200924 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100925 local_irq_enable();
926}
927
928/*
929 * When a delay is required for the flash operation to complete, the
930 * xip_udelay() function is polling for both the given timeout and pending
931 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000932 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100933 * and interrupts unmasked. Task scheduling might also happen at that
934 * point. The CPU eventually returns from the interrupt or the call to
935 * schedule() and the suspended flash operation is resumed for the remaining
936 * of the delay period.
937 *
938 * Warning: this function _will_ fool interrupt latency tracing tools.
939 */
940
941static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
942 unsigned long adr, int usec)
943{
944 struct cfi_private *cfi = map->fldrv_priv;
945 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
946 map_word status, OK = CMD(0x80);
947 unsigned long suspended, start = xip_currtime();
948 flstate_t oldstate;
949
950 do {
951 cpu_relax();
952 if (xip_irqpending() && extp &&
953 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
954 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
955 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000956 * Let's suspend the erase operation when supported.
957 * Note that we currently don't try to suspend
958 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100959 * operation suspended (imagine what happens
960 * when one chip was already done with the current
961 * operation while another chip suspended it, then
962 * we resume the whole thing at once). Yes, it
963 * can happen!
964 */
965 map_write(map, CMD(0xb0), adr);
966 usec -= xip_elapsed_since(start);
967 suspended = xip_currtime();
968 do {
969 if (xip_elapsed_since(suspended) > 100000) {
970 /*
971 * The chip doesn't want to suspend
972 * after waiting for 100 msecs.
973 * This is a critical error but there
974 * is not much we can do here.
975 */
976 return;
977 }
978 status = map_read(map, adr);
979 } while (!map_word_andequal(map, status, OK, OK));
980
981 /* Suspend succeeded */
982 oldstate = chip->state;
983 if (!map_word_bitsset(map, status, CMD(0x40)))
984 break;
985 chip->state = FL_XIP_WHILE_ERASING;
986 chip->erase_suspended = 1;
987 map_write(map, CMD(0xf0), adr);
988 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200989 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100990 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200991 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200992 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100993 cond_resched();
994
995 /*
996 * We're back. However someone else might have
997 * decided to go write to the chip if we are in
998 * a suspended erase state. If so let's wait
999 * until it's done.
1000 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001001 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001002 while (chip->state != FL_XIP_WHILE_ERASING) {
1003 DECLARE_WAITQUEUE(wait, current);
1004 set_current_state(TASK_UNINTERRUPTIBLE);
1005 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001006 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001007 schedule();
1008 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001009 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001010 }
1011 /* Disallow XIP again */
1012 local_irq_disable();
1013
Gerlando Falauto42096282012-07-03 09:09:47 +02001014 /* Correct Erase Suspend Hangups for M29EW */
1015 cfi_fixup_m29ew_erase_suspend(map, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001016 /* Resume the write or erase operation */
Guillaume LECERF08968042010-10-26 10:45:23 +01001017 map_write(map, cfi->sector_erase_cmd, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001018 chip->state = oldstate;
1019 start = xip_currtime();
1020 } else if (usec >= 1000000/HZ) {
1021 /*
1022 * Try to save on CPU power when waiting delay
1023 * is at least a system timer tick period.
1024 * No need to be extremely accurate here.
1025 */
1026 xip_cpu_idle();
1027 }
1028 status = map_read(map, adr);
1029 } while (!map_word_andequal(map, status, OK, OK)
1030 && xip_elapsed_since(start) < usec);
1031}
1032
1033#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1034
1035/*
1036 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1037 * the flash is actively programming or erasing since we have to poll for
1038 * the operation to complete anyway. We can't do that in a generic way with
1039 * a XIP setup so do it before the actual flash operation in this case
1040 * and stub it out from INVALIDATE_CACHE_UDELAY.
1041 */
1042#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1043 INVALIDATE_CACHED_RANGE(map, from, size)
1044
1045#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1046 UDELAY(map, chip, adr, usec)
1047
1048/*
1049 * Extra notes:
1050 *
1051 * Activating this XIP support changes the way the code works a bit. For
1052 * example the code to suspend the current process when concurrent access
1053 * happens is never executed because xip_udelay() will always return with the
1054 * same chip state as it was entered with. This is why there is no care for
1055 * the presence of add_wait_queue() or schedule() calls from within a couple
1056 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1057 * The queueing and scheduling are always happening within xip_udelay().
1058 *
1059 * Similarly, get_chip() and put_chip() just happen to always be executed
1060 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1061 * is in array mode, therefore never executing many cases therein and not
1062 * causing any problem with XIP.
1063 */
1064
1065#else
1066
1067#define xip_disable(map, chip, adr)
1068#define xip_enable(map, chip, adr)
1069#define XIP_INVAL_CACHED_RANGE(x...)
1070
1071#define UDELAY(map, chip, adr, usec) \
1072do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001073 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001074 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001075 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001076} while (0)
1077
1078#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1079do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001080 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001081 INVALIDATE_CACHED_RANGE(map, adr, len); \
1082 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001083 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001084} while (0)
1085
1086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1089{
1090 unsigned long cmd_addr;
1091 struct cfi_private *cfi = map->fldrv_priv;
1092 int ret;
1093
1094 adr += chip->start;
1095
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001096 /* Ensure cmd read/writes are aligned. */
1097 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001099 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ret = get_chip(map, chip, cmd_addr, FL_READY);
1101 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001102 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return ret;
1104 }
1105
1106 if (chip->state != FL_POINT && chip->state != FL_READY) {
1107 map_write(map, CMD(0xf0), cmd_addr);
1108 chip->state = FL_READY;
1109 }
1110
1111 map_copy_from(map, buf, adr, len);
1112
1113 put_chip(map, chip, cmd_addr);
1114
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001115 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117}
1118
1119
1120static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1121{
1122 struct map_info *map = mtd->priv;
1123 struct cfi_private *cfi = map->fldrv_priv;
1124 unsigned long ofs;
1125 int chipnum;
1126 int ret = 0;
1127
1128 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 chipnum = (from >> cfi->chipshift);
1130 ofs = from - (chipnum << cfi->chipshift);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 while (len) {
1133 unsigned long thislen;
1134
1135 if (chipnum >= cfi->numchips)
1136 break;
1137
1138 if ((len + ofs -1) >> cfi->chipshift)
1139 thislen = (1<<cfi->chipshift) - ofs;
1140 else
1141 thislen = len;
1142
1143 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1144 if (ret)
1145 break;
1146
1147 *retlen += thislen;
1148 len -= thislen;
1149 buf += thislen;
1150
1151 ofs = 0;
1152 chipnum++;
1153 }
1154 return ret;
1155}
1156
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001157typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Christian Riesch4f5cb242014-03-06 13:18:30 +01001158 loff_t adr, size_t len, u_char *buf, size_t grouplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christian Rieschfeb86772013-04-26 21:10:28 +02001160static inline void otp_enter(struct map_info *map, struct flchip *chip,
1161 loff_t adr, size_t len)
1162{
1163 struct cfi_private *cfi = map->fldrv_priv;
1164
1165 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1166 cfi->device_type, NULL);
1167 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1168 cfi->device_type, NULL);
1169 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
1170 cfi->device_type, NULL);
1171
1172 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1173}
1174
1175static inline void otp_exit(struct map_info *map, struct flchip *chip,
1176 loff_t adr, size_t len)
1177{
1178 struct cfi_private *cfi = map->fldrv_priv;
1179
1180 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1181 cfi->device_type, NULL);
1182 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1183 cfi->device_type, NULL);
1184 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
1185 cfi->device_type, NULL);
1186 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
1187 cfi->device_type, NULL);
1188
1189 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1190}
1191
Christian Riesch4f5cb242014-03-06 13:18:30 +01001192static inline int do_read_secsi_onechip(struct map_info *map,
1193 struct flchip *chip, loff_t adr,
1194 size_t len, u_char *buf,
1195 size_t grouplen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 DECLARE_WAITQUEUE(wait, current);
1198 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001201 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 set_current_state(TASK_UNINTERRUPTIBLE);
1205 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001206
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001207 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 schedule();
1210 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 timeo = jiffies + HZ;
1212
1213 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 adr += chip->start;
1217
1218 chip->state = FL_READY;
1219
Christian Rieschfeb86772013-04-26 21:10:28 +02001220 otp_enter(map, chip, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 map_copy_from(map, buf, adr, len);
Christian Rieschfeb86772013-04-26 21:10:28 +02001222 otp_exit(map, chip, adr, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001225 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 return 0;
1228}
1229
1230static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1231{
1232 struct map_info *map = mtd->priv;
1233 struct cfi_private *cfi = map->fldrv_priv;
1234 unsigned long ofs;
1235 int chipnum;
1236 int ret = 0;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* 8 secsi bytes per chip */
1240 chipnum=from>>3;
1241 ofs=from & 7;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 while (len) {
1244 unsigned long thislen;
1245
1246 if (chipnum >= cfi->numchips)
1247 break;
1248
1249 if ((len + ofs -1) >> 3)
1250 thislen = (1<<3) - ofs;
1251 else
1252 thislen = len;
1253
Christian Riesch4f5cb242014-03-06 13:18:30 +01001254 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
1255 thislen, buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (ret)
1257 break;
1258
1259 *retlen += thislen;
1260 len -= thislen;
1261 buf += thislen;
1262
1263 ofs = 0;
1264 chipnum++;
1265 }
1266 return ret;
1267}
1268
Christian Rieschaf744752014-03-06 13:18:29 +01001269static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1270 unsigned long adr, map_word datum,
1271 int mode);
1272
1273static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
Christian Riesch4f5cb242014-03-06 13:18:30 +01001274 size_t len, u_char *buf, size_t grouplen)
Christian Rieschaf744752014-03-06 13:18:29 +01001275{
1276 int ret;
1277 while (len) {
1278 unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
1279 int gap = adr - bus_ofs;
1280 int n = min_t(int, len, map_bankwidth(map) - gap);
1281 map_word datum;
1282
1283 if (n != map_bankwidth(map)) {
1284 /* partial write of a word, load old contents */
1285 otp_enter(map, chip, bus_ofs, map_bankwidth(map));
1286 datum = map_read(map, bus_ofs);
1287 otp_exit(map, chip, bus_ofs, map_bankwidth(map));
1288 }
1289
1290 datum = map_word_load_partial(map, datum, buf, gap, n);
1291 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
1292 if (ret)
1293 return ret;
1294
1295 adr += n;
1296 buf += n;
1297 len -= n;
1298 }
1299
1300 return 0;
1301}
1302
Christian Riesch4f5cb242014-03-06 13:18:30 +01001303static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
1304 size_t len, u_char *buf, size_t grouplen)
1305{
1306 struct cfi_private *cfi = map->fldrv_priv;
1307 uint8_t lockreg;
1308 unsigned long timeo;
1309 int ret;
1310
1311 /* make sure area matches group boundaries */
1312 if ((adr != 0) || (len != grouplen))
1313 return -EINVAL;
1314
1315 mutex_lock(&chip->mutex);
1316 ret = get_chip(map, chip, chip->start, FL_LOCKING);
1317 if (ret) {
1318 mutex_unlock(&chip->mutex);
1319 return ret;
1320 }
1321 chip->state = FL_LOCKING;
1322
1323 /* Enter lock register command */
1324 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1325 cfi->device_type, NULL);
1326 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1327 cfi->device_type, NULL);
1328 cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
1329 cfi->device_type, NULL);
1330
1331 /* read lock register */
1332 lockreg = cfi_read_query(map, 0);
1333
1334 /* set bit 0 to protect extended memory block */
1335 lockreg &= ~0x01;
1336
1337 /* set bit 0 to protect extended memory block */
1338 /* write lock register */
1339 map_write(map, CMD(0xA0), chip->start);
1340 map_write(map, CMD(lockreg), chip->start);
1341
1342 /* wait for chip to become ready */
1343 timeo = jiffies + msecs_to_jiffies(2);
1344 for (;;) {
1345 if (chip_ready(map, adr))
1346 break;
1347
1348 if (time_after(jiffies, timeo)) {
1349 pr_err("Waiting for chip to be ready timed out.\n");
1350 ret = -EIO;
1351 break;
1352 }
1353 UDELAY(map, chip, 0, 1);
1354 }
1355
1356 /* exit protection commands */
1357 map_write(map, CMD(0x90), chip->start);
1358 map_write(map, CMD(0x00), chip->start);
1359
1360 chip->state = FL_READY;
1361 put_chip(map, chip, chip->start);
1362 mutex_unlock(&chip->mutex);
1363
1364 return ret;
1365}
1366
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001367static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1368 size_t *retlen, u_char *buf,
1369 otp_op_t action, int user_regs)
1370{
1371 struct map_info *map = mtd->priv;
1372 struct cfi_private *cfi = map->fldrv_priv;
1373 int ofs_factor = cfi->interleave * cfi->device_type;
1374 unsigned long base;
1375 int chipnum;
1376 struct flchip *chip;
1377 uint8_t otp, lockreg;
1378 int ret;
1379
1380 size_t user_size, factory_size, otpsize;
1381 loff_t user_offset, factory_offset, otpoffset;
1382 int user_locked = 0, otplocked;
1383
1384 *retlen = 0;
1385
1386 for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
1387 chip = &cfi->chips[chipnum];
1388 factory_size = 0;
1389 user_size = 0;
1390
1391 /* Micron M29EW family */
1392 if (is_m29ew(cfi)) {
1393 base = chip->start;
1394
1395 /* check whether secsi area is factory locked
1396 or user lockable */
1397 mutex_lock(&chip->mutex);
1398 ret = get_chip(map, chip, base, FL_CFI_QUERY);
1399 if (ret) {
1400 mutex_unlock(&chip->mutex);
1401 return ret;
1402 }
1403 cfi_qry_mode_on(base, map, cfi);
1404 otp = cfi_read_query(map, base + 0x3 * ofs_factor);
1405 cfi_qry_mode_off(base, map, cfi);
1406 put_chip(map, chip, base);
1407 mutex_unlock(&chip->mutex);
1408
1409 if (otp & 0x80) {
1410 /* factory locked */
1411 factory_offset = 0;
1412 factory_size = 0x100;
1413 } else {
1414 /* customer lockable */
1415 user_offset = 0;
1416 user_size = 0x100;
1417
1418 mutex_lock(&chip->mutex);
1419 ret = get_chip(map, chip, base, FL_LOCKING);
1420
1421 /* Enter lock register command */
1422 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
1423 chip->start, map, cfi,
1424 cfi->device_type, NULL);
1425 cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
1426 chip->start, map, cfi,
1427 cfi->device_type, NULL);
1428 cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
1429 chip->start, map, cfi,
1430 cfi->device_type, NULL);
1431 /* read lock register */
1432 lockreg = cfi_read_query(map, 0);
1433 /* exit protection commands */
1434 map_write(map, CMD(0x90), chip->start);
1435 map_write(map, CMD(0x00), chip->start);
1436 put_chip(map, chip, chip->start);
1437 mutex_unlock(&chip->mutex);
1438
1439 user_locked = ((lockreg & 0x01) == 0x00);
1440 }
1441 }
1442
1443 otpsize = user_regs ? user_size : factory_size;
1444 if (!otpsize)
1445 continue;
1446 otpoffset = user_regs ? user_offset : factory_offset;
1447 otplocked = user_regs ? user_locked : 1;
1448
1449 if (!action) {
1450 /* return otpinfo */
1451 struct otp_info *otpinfo;
1452 len -= sizeof(*otpinfo);
1453 if (len <= 0)
1454 return -ENOSPC;
1455 otpinfo = (struct otp_info *)buf;
1456 otpinfo->start = from;
1457 otpinfo->length = otpsize;
1458 otpinfo->locked = otplocked;
1459 buf += sizeof(*otpinfo);
1460 *retlen += sizeof(*otpinfo);
1461 from += otpsize;
1462 } else if ((from < otpsize) && (len > 0)) {
1463 size_t size;
1464 size = (len < otpsize - from) ? len : otpsize - from;
Christian Riesch4f5cb242014-03-06 13:18:30 +01001465 ret = action(map, chip, otpoffset + from, size, buf,
1466 otpsize);
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001467 if (ret < 0)
1468 return ret;
1469
1470 buf += size;
1471 len -= size;
1472 *retlen += size;
1473 from = 0;
1474 } else {
1475 from -= otpsize;
1476 }
1477 }
1478 return 0;
1479}
1480
1481static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
1482 size_t *retlen, struct otp_info *buf)
1483{
1484 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1485 NULL, 0);
1486}
1487
1488static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
1489 size_t *retlen, struct otp_info *buf)
1490{
1491 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1492 NULL, 1);
1493}
1494
1495static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1496 size_t len, size_t *retlen,
1497 u_char *buf)
1498{
1499 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1500 buf, do_read_secsi_onechip, 0);
1501}
1502
1503static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1504 size_t len, size_t *retlen,
1505 u_char *buf)
1506{
1507 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1508 buf, do_read_secsi_onechip, 1);
1509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Christian Rieschaf744752014-03-06 13:18:29 +01001511static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1512 size_t len, size_t *retlen,
1513 u_char *buf)
1514{
1515 return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
1516 do_otp_write, 1);
1517}
1518
Christian Riesch4f5cb242014-03-06 13:18:30 +01001519static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1520 size_t len)
1521{
1522 size_t retlen;
1523 return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
1524 do_otp_lock, 1);
1525}
1526
Christian Rieschaf744752014-03-06 13:18:29 +01001527static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1528 unsigned long adr, map_word datum,
1529 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct cfi_private *cfi = map->fldrv_priv;
1532 unsigned long timeo = jiffies + HZ;
1533 /*
1534 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1535 * have a max write time of a few hundreds usec). However, we should
1536 * use the maximum timeout value given by the chip at probe time
1537 * instead. Unfortunately, struct flchip does have a field for
1538 * maximum timeout, only for typical which can be far too short
1539 * depending of the conditions. The ' + 1' is to avoid having a
1540 * timeout of 0 jiffies if HZ is smaller than 1000.
1541 */
1542 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1543 int ret = 0;
1544 map_word oldd;
1545 int retry_cnt = 0;
1546
1547 adr += chip->start;
1548
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001549 mutex_lock(&chip->mutex);
Christian Rieschaf744752014-03-06 13:18:29 +01001550 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001552 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return ret;
1554 }
1555
Brian Norris289c0522011-07-19 10:06:09 -07001556 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 __func__, adr, datum.x[0] );
1558
Christian Rieschaf744752014-03-06 13:18:29 +01001559 if (mode == FL_OTP_WRITE)
1560 otp_enter(map, chip, adr, map_bankwidth(map));
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Check for a NOP for the case when the datum to write is already
1564 * present - it saves time and works around buggy chips that corrupt
1565 * data at other locations when 0xff is written to a location that
1566 * already contains 0xff.
1567 */
1568 oldd = map_read(map, adr);
1569 if (map_word_equal(map, oldd, datum)) {
Brian Norris289c0522011-07-19 10:06:09 -07001570 pr_debug("MTD %s(): NOP\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 __func__);
1572 goto op_done;
1573 }
1574
Todd Poynor02b15e32005-06-07 00:04:39 +01001575 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001577 xip_disable(map, chip, adr);
Christian Rieschaf744752014-03-06 13:18:29 +01001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 retry:
1580 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1581 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1582 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1583 map_write(map, datum, adr);
Christian Rieschaf744752014-03-06 13:18:29 +01001584 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Todd Poynor02b15e32005-06-07 00:04:39 +01001586 INVALIDATE_CACHE_UDELAY(map, chip,
1587 adr, map_bankwidth(map),
1588 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001591 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 for (;;) {
Christian Rieschaf744752014-03-06 13:18:29 +01001593 if (chip->state != mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* Someone's suspended the write. Sleep */
1595 DECLARE_WAITQUEUE(wait, current);
1596
1597 set_current_state(TASK_UNINTERRUPTIBLE);
1598 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001599 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 schedule();
1601 remove_wait_queue(&chip->wq, &wait);
1602 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001603 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 continue;
1605 }
1606
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001607 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001608 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001609 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001610 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001611 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001614 if (chip_ready(map, adr))
1615 break;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001618 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001620 /* Did we succeed? */
1621 if (!chip_good(map, adr, datum)) {
1622 /* reset on all failures. */
1623 map_write( map, CMD(0xF0), chip->start );
1624 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001626 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001627 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001629 ret = -EIO;
1630 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001631 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 op_done:
Christian Rieschaf744752014-03-06 13:18:29 +01001633 if (mode == FL_OTP_WRITE)
1634 otp_exit(map, chip, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001636 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001638 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 return ret;
1641}
1642
1643
1644static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1645 size_t *retlen, const u_char *buf)
1646{
1647 struct map_info *map = mtd->priv;
1648 struct cfi_private *cfi = map->fldrv_priv;
1649 int ret = 0;
1650 int chipnum;
1651 unsigned long ofs, chipstart;
1652 DECLARE_WAITQUEUE(wait, current);
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 chipnum = to >> cfi->chipshift;
1655 ofs = to - (chipnum << cfi->chipshift);
1656 chipstart = cfi->chips[chipnum].start;
1657
1658 /* If it's not bus-aligned, do the first byte write */
1659 if (ofs & (map_bankwidth(map)-1)) {
1660 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1661 int i = ofs - bus_ofs;
1662 int n = 0;
1663 map_word tmp_buf;
1664
1665 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001666 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 set_current_state(TASK_UNINTERRUPTIBLE);
1670 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1671
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001672 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 schedule();
1675 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto retry;
1677 }
1678
1679 /* Load 'tmp_buf' with old contents of flash */
1680 tmp_buf = map_read(map, bus_ofs+chipstart);
1681
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001682 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* Number of bytes to copy from buffer */
1685 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1688
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001689 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001690 bus_ofs, tmp_buf, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001691 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 ofs += n;
1695 buf += n;
1696 (*retlen) += n;
1697 len -= n;
1698
1699 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001700 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 ofs = 0;
1702 if (chipnum == cfi->numchips)
1703 return 0;
1704 }
1705 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /* We are now aligned, write as much as possible */
1708 while(len >= map_bankwidth(map)) {
1709 map_word datum;
1710
1711 datum = map_word_load(map, buf);
1712
1713 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001714 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (ret)
1716 return ret;
1717
1718 ofs += map_bankwidth(map);
1719 buf += map_bankwidth(map);
1720 (*retlen) += map_bankwidth(map);
1721 len -= map_bankwidth(map);
1722
1723 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001724 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ofs = 0;
1726 if (chipnum == cfi->numchips)
1727 return 0;
1728 chipstart = cfi->chips[chipnum].start;
1729 }
1730 }
1731
1732 /* Write the trailing bytes if any */
1733 if (len & (map_bankwidth(map)-1)) {
1734 map_word tmp_buf;
1735
1736 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001737 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 set_current_state(TASK_UNINTERRUPTIBLE);
1741 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1742
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001743 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 schedule();
1746 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto retry1;
1748 }
1749
1750 tmp_buf = map_read(map, ofs + chipstart);
1751
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001752 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001755
1756 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001757 ofs, tmp_buf, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001758 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 (*retlen) += len;
1762 }
1763
1764 return 0;
1765}
1766
1767
1768/*
1769 * FIXME: interleaved mode not tested, and probably not supported!
1770 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001771static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001772 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001773 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct cfi_private *cfi = map->fldrv_priv;
1776 unsigned long timeo = jiffies + HZ;
1777 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1778 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1779 int ret = -EIO;
1780 unsigned long cmd_adr;
1781 int z, words;
1782 map_word datum;
1783
1784 adr += chip->start;
1785 cmd_adr = adr;
1786
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001787 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ret = get_chip(map, chip, adr, FL_WRITING);
1789 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001790 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return ret;
1792 }
1793
1794 datum = map_word_load(map, buf);
1795
Brian Norris289c0522011-07-19 10:06:09 -07001796 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 __func__, adr, datum.x[0] );
1798
Todd Poynor02b15e32005-06-07 00:04:39 +01001799 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001801 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1804 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* Write Buffer Load */
1807 map_write(map, CMD(0x25), cmd_adr);
1808
1809 chip->state = FL_WRITING_TO_BUFFER;
1810
1811 /* Write length of data to come */
1812 words = len / map_bankwidth(map);
1813 map_write(map, CMD(words - 1), cmd_adr);
1814 /* Write data */
1815 z = 0;
1816 while(z < words * map_bankwidth(map)) {
1817 datum = map_word_load(map, buf);
1818 map_write(map, datum, adr + z);
1819
1820 z += map_bankwidth(map);
1821 buf += map_bankwidth(map);
1822 }
1823 z -= map_bankwidth(map);
1824
1825 adr += z;
1826
1827 /* Write Buffer Program Confirm: GO GO GO */
1828 map_write(map, CMD(0x29), cmd_adr);
1829 chip->state = FL_WRITING;
1830
Todd Poynor02b15e32005-06-07 00:04:39 +01001831 INVALIDATE_CACHE_UDELAY(map, chip,
1832 adr, map_bankwidth(map),
1833 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001835 timeo = jiffies + uWriteTimeout;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 for (;;) {
1838 if (chip->state != FL_WRITING) {
1839 /* Someone's suspended the write. Sleep */
1840 DECLARE_WAITQUEUE(wait, current);
1841
1842 set_current_state(TASK_UNINTERRUPTIBLE);
1843 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001844 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 schedule();
1846 remove_wait_queue(&chip->wq, &wait);
1847 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001848 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 continue;
1850 }
1851
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001852 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1853 break;
1854
Todd Poynor02b15e32005-06-07 00:04:39 +01001855 if (chip_ready(map, adr)) {
1856 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001861 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
Harald Nordgard-Hansen070c3222012-11-23 23:11:03 +01001864 /*
1865 * Recovery from write-buffer programming failures requires
1866 * the write-to-buffer-reset sequence. Since the last part
1867 * of the sequence also works as a normal reset, we can run
1868 * the same commands regardless of why we are here.
1869 * See e.g.
1870 * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
1871 */
1872 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1873 cfi->device_type, NULL);
1874 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1875 cfi->device_type, NULL);
1876 cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
1877 cfi->device_type, NULL);
Todd Poynor02b15e32005-06-07 00:04:39 +01001878 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* FIXME - should have reset delay before continuing */
1880
Huang Shijie25983b12013-04-01 17:40:18 +08001881 printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
1882 __func__, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 ret = -EIO;
1885 op_done:
1886 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001887 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001889 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 return ret;
1892}
1893
1894
1895static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1896 size_t *retlen, const u_char *buf)
1897{
1898 struct map_info *map = mtd->priv;
1899 struct cfi_private *cfi = map->fldrv_priv;
1900 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1901 int ret = 0;
1902 int chipnum;
1903 unsigned long ofs;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 chipnum = to >> cfi->chipshift;
1906 ofs = to - (chipnum << cfi->chipshift);
1907
1908 /* If it's not bus-aligned, do the first word write */
1909 if (ofs & (map_bankwidth(map)-1)) {
1910 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1911 if (local_len > len)
1912 local_len = len;
1913 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1914 local_len, retlen, buf);
1915 if (ret)
1916 return ret;
1917 ofs += local_len;
1918 buf += local_len;
1919 len -= local_len;
1920
1921 if (ofs >> cfi->chipshift) {
1922 chipnum ++;
1923 ofs = 0;
1924 if (chipnum == cfi->numchips)
1925 return 0;
1926 }
1927 }
1928
1929 /* Write buffer is worth it only if more than one word to write... */
1930 while (len >= map_bankwidth(map) * 2) {
1931 /* We must not cross write block boundaries */
1932 int size = wbufsize - (ofs & (wbufsize-1));
1933
1934 if (size > len)
1935 size = len;
1936 if (size % map_bankwidth(map))
1937 size -= size % map_bankwidth(map);
1938
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001939 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ofs, buf, size);
1941 if (ret)
1942 return ret;
1943
1944 ofs += size;
1945 buf += size;
1946 (*retlen) += size;
1947 len -= size;
1948
1949 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001950 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 ofs = 0;
1952 if (chipnum == cfi->numchips)
1953 return 0;
1954 }
1955 }
1956
1957 if (len) {
1958 size_t retlen_dregs = 0;
1959
1960 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1961 len, &retlen_dregs, buf);
1962
1963 *retlen += retlen_dregs;
1964 return ret;
1965 }
1966
1967 return 0;
1968}
1969
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001970/*
1971 * Wait for the flash chip to become ready to write data
1972 *
1973 * This is only called during the panic_write() path. When panic_write()
1974 * is called, the kernel is in the process of a panic, and will soon be
1975 * dead. Therefore we don't take any locks, and attempt to get access
1976 * to the chip as soon as possible.
1977 */
1978static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
1979 unsigned long adr)
1980{
1981 struct cfi_private *cfi = map->fldrv_priv;
1982 int retries = 10;
1983 int i;
1984
1985 /*
1986 * If the driver thinks the chip is idle, and no toggle bits
1987 * are changing, then the chip is actually idle for sure.
1988 */
1989 if (chip->state == FL_READY && chip_ready(map, adr))
1990 return 0;
1991
1992 /*
1993 * Try several times to reset the chip and then wait for it
1994 * to become idle. The upper limit of a few milliseconds of
1995 * delay isn't a big problem: the kernel is dying anyway. It
1996 * is more important to save the messages.
1997 */
1998 while (retries > 0) {
1999 const unsigned long timeo = (HZ / 1000) + 1;
2000
2001 /* send the reset command */
2002 map_write(map, CMD(0xF0), chip->start);
2003
2004 /* wait for the chip to become ready */
2005 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
2006 if (chip_ready(map, adr))
2007 return 0;
2008
2009 udelay(1);
2010 }
2011 }
2012
2013 /* the chip never became ready */
2014 return -EBUSY;
2015}
2016
2017/*
2018 * Write out one word of data to a single flash chip during a kernel panic
2019 *
2020 * This is only called during the panic_write() path. When panic_write()
2021 * is called, the kernel is in the process of a panic, and will soon be
2022 * dead. Therefore we don't take any locks, and attempt to get access
2023 * to the chip as soon as possible.
2024 *
2025 * The implementation of this routine is intentionally similar to
2026 * do_write_oneword(), in order to ease code maintenance.
2027 */
2028static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
2029 unsigned long adr, map_word datum)
2030{
2031 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
2032 struct cfi_private *cfi = map->fldrv_priv;
2033 int retry_cnt = 0;
2034 map_word oldd;
2035 int ret = 0;
2036 int i;
2037
2038 adr += chip->start;
2039
2040 ret = cfi_amdstd_panic_wait(map, chip, adr);
2041 if (ret)
2042 return ret;
2043
2044 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
2045 __func__, adr, datum.x[0]);
2046
2047 /*
2048 * Check for a NOP for the case when the datum to write is already
2049 * present - it saves time and works around buggy chips that corrupt
2050 * data at other locations when 0xff is written to a location that
2051 * already contains 0xff.
2052 */
2053 oldd = map_read(map, adr);
2054 if (map_word_equal(map, oldd, datum)) {
2055 pr_debug("MTD %s(): NOP\n", __func__);
2056 goto op_done;
2057 }
2058
2059 ENABLE_VPP(map);
2060
2061retry:
2062 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2063 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2064 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2065 map_write(map, datum, adr);
2066
2067 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
2068 if (chip_ready(map, adr))
2069 break;
2070
2071 udelay(1);
2072 }
2073
2074 if (!chip_good(map, adr, datum)) {
2075 /* reset on all failures. */
2076 map_write(map, CMD(0xF0), chip->start);
2077 /* FIXME - should have reset delay before continuing */
2078
2079 if (++retry_cnt <= MAX_WORD_RETRIES)
2080 goto retry;
2081
2082 ret = -EIO;
2083 }
2084
2085op_done:
2086 DISABLE_VPP(map);
2087 return ret;
2088}
2089
2090/*
2091 * Write out some data during a kernel panic
2092 *
2093 * This is used by the mtdoops driver to save the dying messages from a
2094 * kernel which has panic'd.
2095 *
2096 * This routine ignores all of the locking used throughout the rest of the
2097 * driver, in order to ensure that the data gets written out no matter what
2098 * state this driver (and the flash chip itself) was in when the kernel crashed.
2099 *
2100 * The implementation of this routine is intentionally similar to
2101 * cfi_amdstd_write_words(), in order to ease code maintenance.
2102 */
2103static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
2104 size_t *retlen, const u_char *buf)
2105{
2106 struct map_info *map = mtd->priv;
2107 struct cfi_private *cfi = map->fldrv_priv;
2108 unsigned long ofs, chipstart;
2109 int ret = 0;
2110 int chipnum;
2111
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08002112 chipnum = to >> cfi->chipshift;
2113 ofs = to - (chipnum << cfi->chipshift);
2114 chipstart = cfi->chips[chipnum].start;
2115
2116 /* If it's not bus aligned, do the first byte write */
2117 if (ofs & (map_bankwidth(map) - 1)) {
2118 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
2119 int i = ofs - bus_ofs;
2120 int n = 0;
2121 map_word tmp_buf;
2122
2123 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
2124 if (ret)
2125 return ret;
2126
2127 /* Load 'tmp_buf' with old contents of flash */
2128 tmp_buf = map_read(map, bus_ofs + chipstart);
2129
2130 /* Number of bytes to copy from buffer */
2131 n = min_t(int, len, map_bankwidth(map) - i);
2132
2133 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
2134
2135 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2136 bus_ofs, tmp_buf);
2137 if (ret)
2138 return ret;
2139
2140 ofs += n;
2141 buf += n;
2142 (*retlen) += n;
2143 len -= n;
2144
2145 if (ofs >> cfi->chipshift) {
2146 chipnum++;
2147 ofs = 0;
2148 if (chipnum == cfi->numchips)
2149 return 0;
2150 }
2151 }
2152
2153 /* We are now aligned, write as much as possible */
2154 while (len >= map_bankwidth(map)) {
2155 map_word datum;
2156
2157 datum = map_word_load(map, buf);
2158
2159 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2160 ofs, datum);
2161 if (ret)
2162 return ret;
2163
2164 ofs += map_bankwidth(map);
2165 buf += map_bankwidth(map);
2166 (*retlen) += map_bankwidth(map);
2167 len -= map_bankwidth(map);
2168
2169 if (ofs >> cfi->chipshift) {
2170 chipnum++;
2171 ofs = 0;
2172 if (chipnum == cfi->numchips)
2173 return 0;
2174
2175 chipstart = cfi->chips[chipnum].start;
2176 }
2177 }
2178
2179 /* Write the trailing bytes if any */
2180 if (len & (map_bankwidth(map) - 1)) {
2181 map_word tmp_buf;
2182
2183 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
2184 if (ret)
2185 return ret;
2186
2187 tmp_buf = map_read(map, ofs + chipstart);
2188
2189 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
2190
2191 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2192 ofs, tmp_buf);
2193 if (ret)
2194 return ret;
2195
2196 (*retlen) += len;
2197 }
2198
2199 return 0;
2200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203/*
2204 * Handle devices with one erase region, that only implement
2205 * the chip erase command.
2206 */
Todd Poynor02b15e32005-06-07 00:04:39 +01002207static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 struct cfi_private *cfi = map->fldrv_priv;
2210 unsigned long timeo = jiffies + HZ;
2211 unsigned long int adr;
2212 DECLARE_WAITQUEUE(wait, current);
2213 int ret = 0;
2214
2215 adr = cfi->addr_unlock1;
2216
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002217 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 ret = get_chip(map, chip, adr, FL_WRITING);
2219 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002220 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return ret;
2222 }
2223
Brian Norris289c0522011-07-19 10:06:09 -07002224 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 __func__, chip->start );
2226
Todd Poynor02b15e32005-06-07 00:04:39 +01002227 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01002229 xip_disable(map, chip, adr);
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2232 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2233 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2234 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2235 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2236 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2237
2238 chip->state = FL_ERASING;
2239 chip->erase_suspended = 0;
2240 chip->in_progress_block_addr = adr;
2241
Todd Poynor02b15e32005-06-07 00:04:39 +01002242 INVALIDATE_CACHE_UDELAY(map, chip,
2243 adr, map->size,
2244 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 timeo = jiffies + (HZ*20);
2247
2248 for (;;) {
2249 if (chip->state != FL_ERASING) {
2250 /* Someone's suspended the erase. Sleep */
2251 set_current_state(TASK_UNINTERRUPTIBLE);
2252 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002253 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 schedule();
2255 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002256 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 continue;
2258 }
2259 if (chip->erase_suspended) {
2260 /* This erase was suspended and resumed.
2261 Adjust the timeout */
2262 timeo = jiffies + (HZ*20); /* FIXME */
2263 chip->erase_suspended = 0;
2264 }
2265
2266 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 break;
2268
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002269 if (time_after(jiffies, timeo)) {
2270 printk(KERN_WARNING "MTD %s(): software timeout\n",
2271 __func__ );
2272 break;
2273 }
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01002276 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002278 /* Did we succeed? */
2279 if (!chip_good(map, adr, map_word_ff(map))) {
2280 /* reset on all failures. */
2281 map_write( map, CMD(0xF0), chip->start );
2282 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002284 ret = -EIO;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01002288 xip_enable(map, chip, adr);
Paul Parsonse7d93772012-03-07 14:11:16 +00002289 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002291 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 return ret;
2294}
2295
2296
Todd Poynor02b15e32005-06-07 00:04:39 +01002297static 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 -07002298{
2299 struct cfi_private *cfi = map->fldrv_priv;
2300 unsigned long timeo = jiffies + HZ;
2301 DECLARE_WAITQUEUE(wait, current);
2302 int ret = 0;
2303
2304 adr += chip->start;
2305
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002306 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 ret = get_chip(map, chip, adr, FL_ERASING);
2308 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002309 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return ret;
2311 }
2312
Brian Norris289c0522011-07-19 10:06:09 -07002313 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 __func__, adr );
2315
Todd Poynor02b15e32005-06-07 00:04:39 +01002316 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01002318 xip_disable(map, chip, adr);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2321 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2322 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2323 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2324 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Guillaume LECERF08968042010-10-26 10:45:23 +01002325 map_write(map, cfi->sector_erase_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 chip->state = FL_ERASING;
2328 chip->erase_suspended = 0;
2329 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01002330
2331 INVALIDATE_CACHE_UDELAY(map, chip,
2332 adr, len,
2333 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 timeo = jiffies + (HZ*20);
2336
2337 for (;;) {
2338 if (chip->state != FL_ERASING) {
2339 /* Someone's suspended the erase. Sleep */
2340 set_current_state(TASK_UNINTERRUPTIBLE);
2341 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002342 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 schedule();
2344 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002345 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 continue;
2347 }
2348 if (chip->erase_suspended) {
2349 /* This erase was suspended and resumed.
2350 Adjust the timeout */
2351 timeo = jiffies + (HZ*20); /* FIXME */
2352 chip->erase_suspended = 0;
2353 }
2354
Todd Poynor02b15e32005-06-07 00:04:39 +01002355 if (chip_ready(map, adr)) {
2356 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01002358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002360 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01002361 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002362 printk(KERN_WARNING "MTD %s(): software timeout\n",
2363 __func__ );
2364 break;
2365 }
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01002368 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002370 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02002371 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002372 /* reset on all failures. */
2373 map_write( map, CMD(0xF0), chip->start );
2374 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002376 ret = -EIO;
2377 }
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00002380 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002382 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return ret;
2384}
2385
2386
Ben Dooksce0f33a2007-05-28 19:59:00 +01002387static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 unsigned long ofs, len;
2390 int ret;
2391
2392 ofs = instr->addr;
2393 len = instr->len;
2394
2395 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2396 if (ret)
2397 return ret;
2398
2399 instr->state = MTD_ERASE_DONE;
2400 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return 0;
2403}
2404
2405
2406static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
2407{
2408 struct map_info *map = mtd->priv;
2409 struct cfi_private *cfi = map->fldrv_priv;
2410 int ret = 0;
2411
2412 if (instr->addr != 0)
2413 return -EINVAL;
2414
2415 if (instr->len != mtd->size)
2416 return -EINVAL;
2417
2418 ret = do_erase_chip(map, &cfi->chips[0]);
2419 if (ret)
2420 return ret;
2421
2422 instr->state = MTD_ERASE_DONE;
2423 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 0;
2426}
2427
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002428static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2429 unsigned long adr, int len, void *thunk)
2430{
2431 struct cfi_private *cfi = map->fldrv_priv;
2432 int ret;
2433
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002434 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002435 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2436 if (ret)
2437 goto out_unlock;
2438 chip->state = FL_LOCKING;
2439
Brian Norris0a32a102011-07-19 10:06:10 -07002440 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002441
2442 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2443 cfi->device_type, NULL);
2444 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2445 cfi->device_type, NULL);
2446 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2447 cfi->device_type, NULL);
2448 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2449 cfi->device_type, NULL);
2450 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2451 cfi->device_type, NULL);
2452 map_write(map, CMD(0x40), chip->start + adr);
2453
2454 chip->state = FL_READY;
2455 put_chip(map, chip, adr + chip->start);
2456 ret = 0;
2457
2458out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002459 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002460 return ret;
2461}
2462
2463static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2464 unsigned long adr, int len, void *thunk)
2465{
2466 struct cfi_private *cfi = map->fldrv_priv;
2467 int ret;
2468
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002469 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002470 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2471 if (ret)
2472 goto out_unlock;
2473 chip->state = FL_UNLOCKING;
2474
Brian Norris0a32a102011-07-19 10:06:10 -07002475 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002476
2477 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2478 cfi->device_type, NULL);
2479 map_write(map, CMD(0x70), adr);
2480
2481 chip->state = FL_READY;
2482 put_chip(map, chip, adr + chip->start);
2483 ret = 0;
2484
2485out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002486 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002487 return ret;
2488}
2489
Adrian Hunter69423d92008-12-10 13:37:21 +00002490static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002491{
2492 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2493}
2494
Adrian Hunter69423d92008-12-10 13:37:21 +00002495static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002496{
2497 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2498}
2499
Stefan Roese1648eaa2013-01-18 13:10:05 +01002500/*
2501 * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
2502 */
2503
2504struct ppb_lock {
2505 struct flchip *chip;
2506 loff_t offset;
2507 int locked;
2508};
2509
2510#define MAX_SECTORS 512
2511
2512#define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
2513#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
2514#define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
2515
2516static int __maybe_unused do_ppb_xxlock(struct map_info *map,
2517 struct flchip *chip,
2518 unsigned long adr, int len, void *thunk)
2519{
2520 struct cfi_private *cfi = map->fldrv_priv;
2521 unsigned long timeo;
2522 int ret;
2523
2524 mutex_lock(&chip->mutex);
2525 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2526 if (ret) {
2527 mutex_unlock(&chip->mutex);
2528 return ret;
2529 }
2530
2531 pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
2532
2533 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2534 cfi->device_type, NULL);
2535 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2536 cfi->device_type, NULL);
2537 /* PPB entry command */
2538 cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
2539 cfi->device_type, NULL);
2540
2541 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2542 chip->state = FL_LOCKING;
2543 map_write(map, CMD(0xA0), chip->start + adr);
2544 map_write(map, CMD(0x00), chip->start + adr);
2545 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2546 /*
2547 * Unlocking of one specific sector is not supported, so we
2548 * have to unlock all sectors of this device instead
2549 */
2550 chip->state = FL_UNLOCKING;
2551 map_write(map, CMD(0x80), chip->start);
2552 map_write(map, CMD(0x30), chip->start);
2553 } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
2554 chip->state = FL_JEDEC_QUERY;
2555 /* Return locked status: 0->locked, 1->unlocked */
2556 ret = !cfi_read_query(map, adr);
2557 } else
2558 BUG();
2559
2560 /*
2561 * Wait for some time as unlocking of all sectors takes quite long
2562 */
2563 timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
2564 for (;;) {
2565 if (chip_ready(map, adr))
2566 break;
2567
2568 if (time_after(jiffies, timeo)) {
2569 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
2570 ret = -EIO;
2571 break;
2572 }
2573
2574 UDELAY(map, chip, adr, 1);
2575 }
2576
2577 /* Exit BC commands */
2578 map_write(map, CMD(0x90), chip->start);
2579 map_write(map, CMD(0x00), chip->start);
2580
2581 chip->state = FL_READY;
2582 put_chip(map, chip, adr + chip->start);
2583 mutex_unlock(&chip->mutex);
2584
2585 return ret;
2586}
2587
2588static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
2589 uint64_t len)
2590{
2591 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2592 DO_XXLOCK_ONEBLOCK_LOCK);
2593}
2594
2595static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
2596 uint64_t len)
2597{
2598 struct mtd_erase_region_info *regions = mtd->eraseregions;
2599 struct map_info *map = mtd->priv;
2600 struct cfi_private *cfi = map->fldrv_priv;
2601 struct ppb_lock *sect;
2602 unsigned long adr;
2603 loff_t offset;
2604 uint64_t length;
2605 int chipnum;
2606 int i;
2607 int sectors;
2608 int ret;
2609
2610 /*
2611 * PPB unlocking always unlocks all sectors of the flash chip.
2612 * We need to re-lock all previously locked sectors. So lets
2613 * first check the locking status of all sectors and save
2614 * it for future use.
2615 */
2616 sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
2617 if (!sect)
2618 return -ENOMEM;
2619
2620 /*
2621 * This code to walk all sectors is a slightly modified version
2622 * of the cfi_varsize_frob() code.
2623 */
2624 i = 0;
2625 chipnum = 0;
2626 adr = 0;
2627 sectors = 0;
2628 offset = 0;
2629 length = mtd->size;
2630
2631 while (length) {
2632 int size = regions[i].erasesize;
2633
2634 /*
2635 * Only test sectors that shall not be unlocked. The other
2636 * sectors shall be unlocked, so lets keep their locking
2637 * status at "unlocked" (locked=0) for the final re-locking.
2638 */
2639 if ((adr < ofs) || (adr >= (ofs + len))) {
2640 sect[sectors].chip = &cfi->chips[chipnum];
2641 sect[sectors].offset = offset;
2642 sect[sectors].locked = do_ppb_xxlock(
2643 map, &cfi->chips[chipnum], adr, 0,
2644 DO_XXLOCK_ONEBLOCK_GETLOCK);
2645 }
2646
2647 adr += size;
2648 offset += size;
2649 length -= size;
2650
2651 if (offset == regions[i].offset + size * regions[i].numblocks)
2652 i++;
2653
2654 if (adr >> cfi->chipshift) {
2655 adr = 0;
2656 chipnum++;
2657
2658 if (chipnum >= cfi->numchips)
2659 break;
2660 }
2661
2662 sectors++;
2663 if (sectors >= MAX_SECTORS) {
2664 printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
2665 MAX_SECTORS);
2666 kfree(sect);
2667 return -EINVAL;
2668 }
2669 }
2670
2671 /* Now unlock the whole chip */
2672 ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2673 DO_XXLOCK_ONEBLOCK_UNLOCK);
2674 if (ret) {
2675 kfree(sect);
2676 return ret;
2677 }
2678
2679 /*
2680 * PPB unlocking always unlocks all sectors of the flash chip.
2681 * We need to re-lock all previously locked sectors.
2682 */
2683 for (i = 0; i < sectors; i++) {
2684 if (sect[i].locked)
2685 do_ppb_xxlock(map, sect[i].chip, sect[i].offset, 0,
2686 DO_XXLOCK_ONEBLOCK_LOCK);
2687 }
2688
2689 kfree(sect);
2690 return ret;
2691}
2692
2693static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
2694 uint64_t len)
2695{
2696 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2697 DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
2698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700static void cfi_amdstd_sync (struct mtd_info *mtd)
2701{
2702 struct map_info *map = mtd->priv;
2703 struct cfi_private *cfi = map->fldrv_priv;
2704 int i;
2705 struct flchip *chip;
2706 int ret = 0;
2707 DECLARE_WAITQUEUE(wait, current);
2708
2709 for (i=0; !ret && i<cfi->numchips; i++) {
2710 chip = &cfi->chips[i];
2711
2712 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002713 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 switch(chip->state) {
2716 case FL_READY:
2717 case FL_STATUS:
2718 case FL_CFI_QUERY:
2719 case FL_JEDEC_QUERY:
2720 chip->oldstate = chip->state;
2721 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002722 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * as the whole point is that nobody can do anything
2724 * with the chip now anyway.
2725 */
2726 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002727 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 break;
2729
2730 default:
2731 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07002732 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002734
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002735 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 schedule();
2738
2739 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 goto retry;
2742 }
2743 }
2744
2745 /* Unlock the chips again */
2746
2747 for (i--; i >=0; i--) {
2748 chip = &cfi->chips[i];
2749
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002750 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 if (chip->state == FL_SYNCING) {
2753 chip->state = chip->oldstate;
2754 wake_up(&chip->wq);
2755 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002756 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758}
2759
2760
2761static int cfi_amdstd_suspend(struct mtd_info *mtd)
2762{
2763 struct map_info *map = mtd->priv;
2764 struct cfi_private *cfi = map->fldrv_priv;
2765 int i;
2766 struct flchip *chip;
2767 int ret = 0;
2768
2769 for (i=0; !ret && i<cfi->numchips; i++) {
2770 chip = &cfi->chips[i];
2771
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002772 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 switch(chip->state) {
2775 case FL_READY:
2776 case FL_STATUS:
2777 case FL_CFI_QUERY:
2778 case FL_JEDEC_QUERY:
2779 chip->oldstate = chip->state;
2780 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002781 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 * as the whole point is that nobody can do anything
2783 * with the chip now anyway.
2784 */
2785 case FL_PM_SUSPENDED:
2786 break;
2787
2788 default:
2789 ret = -EAGAIN;
2790 break;
2791 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002792 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794
2795 /* Unlock the chips again */
2796
2797 if (ret) {
2798 for (i--; i >=0; i--) {
2799 chip = &cfi->chips[i];
2800
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002801 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (chip->state == FL_PM_SUSPENDED) {
2804 chip->state = chip->oldstate;
2805 wake_up(&chip->wq);
2806 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002807 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
2809 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return ret;
2812}
2813
2814
2815static void cfi_amdstd_resume(struct mtd_info *mtd)
2816{
2817 struct map_info *map = mtd->priv;
2818 struct cfi_private *cfi = map->fldrv_priv;
2819 int i;
2820 struct flchip *chip;
2821
2822 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 chip = &cfi->chips[i];
2825
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002826 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (chip->state == FL_PM_SUSPENDED) {
2829 chip->state = FL_READY;
2830 map_write(map, CMD(0xF0), chip->start);
2831 wake_up(&chip->wq);
2832 }
2833 else
2834 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2835
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002836 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838}
2839
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002840
2841/*
2842 * Ensure that the flash device is put back into read array mode before
2843 * unloading the driver or rebooting. On some systems, rebooting while
2844 * the flash is in query/program/erase mode will prevent the CPU from
2845 * fetching the bootloader code, requiring a hard reset or power cycle.
2846 */
2847static int cfi_amdstd_reset(struct mtd_info *mtd)
2848{
2849 struct map_info *map = mtd->priv;
2850 struct cfi_private *cfi = map->fldrv_priv;
2851 int i, ret;
2852 struct flchip *chip;
2853
2854 for (i = 0; i < cfi->numchips; i++) {
2855
2856 chip = &cfi->chips[i];
2857
2858 mutex_lock(&chip->mutex);
2859
2860 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2861 if (!ret) {
2862 map_write(map, CMD(0xF0), chip->start);
2863 chip->state = FL_SHUTDOWN;
2864 put_chip(map, chip, chip->start);
2865 }
2866
2867 mutex_unlock(&chip->mutex);
2868 }
2869
2870 return 0;
2871}
2872
2873
2874static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2875 void *v)
2876{
2877 struct mtd_info *mtd;
2878
2879 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2880 cfi_amdstd_reset(mtd);
2881 return NOTIFY_DONE;
2882}
2883
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885static void cfi_amdstd_destroy(struct mtd_info *mtd)
2886{
2887 struct map_info *map = mtd->priv;
2888 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002889
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002890 cfi_amdstd_reset(mtd);
2891 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 kfree(cfi->cmdset_priv);
2893 kfree(cfi->cfiq);
2894 kfree(cfi);
2895 kfree(mtd->eraseregions);
2896}
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898MODULE_LICENSE("GPL");
2899MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2900MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002901MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002902MODULE_ALIAS("cfi_cmdset_0701");