blob: 107c05b3ddbb9da2c540098062f15a3acfcdaea1 [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;
Guillaume LECERF564b8492010-04-24 17:58:17 +0200618
Fabian Frederickfdd9d272015-06-10 18:31:32 +0200619 swap(cfi->cfiq->EraseRegionInfo[i],
620 cfi->cfiq->EraseRegionInfo[j]);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200621 }
622 }
623 /* Set the default CFI lock/unlock addresses */
624 cfi->addr_unlock1 = 0x555;
625 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200627 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200628
629 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
630 kfree(mtd);
631 return NULL;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 } /* CFI mode */
635 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
636 /* Apply jedec specific fixups */
637 cfi_fixup(mtd, jedec_fixup_table);
638 }
639 /* Apply generic fixups */
640 cfi_fixup(mtd, fixup_table);
641
642 for (i=0; i< cfi->numchips; i++) {
643 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
644 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
645 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Bean Huo6534e682014-07-16 09:05:26 -0700646 /*
647 * First calculate the timeout max according to timeout field
648 * of struct cfi_ident that probed from chip's CFI aera, if
649 * available. Specify a minimum of 2000us, in case the CFI data
650 * is wrong.
651 */
652 if (cfi->cfiq->BufWriteTimeoutTyp &&
653 cfi->cfiq->BufWriteTimeoutMax)
654 cfi->chips[i].buffer_write_time_max =
655 1 << (cfi->cfiq->BufWriteTimeoutTyp +
656 cfi->cfiq->BufWriteTimeoutMax);
657 else
658 cfi->chips[i].buffer_write_time_max = 0;
659
660 cfi->chips[i].buffer_write_time_max =
661 max(cfi->chips[i].buffer_write_time_max, 2000);
662
Vijay Sampath83d48092007-03-06 02:39:44 -0800663 cfi->chips[i].ref_point_counter = 0;
664 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000665 }
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return cfi_amdstd_setup(mtd);
670}
Guillaume LECERF80461122010-05-20 16:54:10 +0200671struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200672struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100673EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200674EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200675EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
678{
679 struct map_info *map = mtd->priv;
680 struct cfi_private *cfi = map->fldrv_priv;
681 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
682 unsigned long offset = 0;
683 int i,j;
684
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000685 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000687 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 mtd->size = devsize * cfi->numchips;
689
690 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
691 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
692 * mtd->numeraseregions, GFP_KERNEL);
Jingoo Han5c8b1fb2014-02-06 15:19:35 +0900693 if (!mtd->eraseregions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto setup_err;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
697 unsigned long ernum, ersize;
698 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
699 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (mtd->erasesize < ersize) {
702 mtd->erasesize = ersize;
703 }
704 for (j=0; j<cfi->numchips; j++) {
705 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
706 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
707 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
708 }
709 offset += (ersize * ernum);
710 }
711 if (offset != devsize) {
712 /* Argh */
713 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
714 goto setup_err;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700718 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return mtd;
720
721 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100722 kfree(mtd->eraseregions);
723 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 kfree(cfi->cmdset_priv);
725 kfree(cfi->cfiq);
726 return NULL;
727}
728
729/*
730 * Return true if the chip is ready.
731 *
732 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
733 * non-suspended sector) and is indicated by no toggle bits toggling.
734 *
735 * Note that anything more complicated than checking if no bits are toggling
736 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300737 * correctly and is therefore not done (particularly with interleaved chips
738 * as each chip must be checked independently of the others).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100740static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 map_word d, t;
743
744 d = map_read(map, addr);
745 t = map_read(map, addr);
746
747 return map_word_equal(map, d, t);
748}
749
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100750/*
751 * Return true if the chip is ready and has the correct value.
752 *
753 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
754 * non-suspended sector) and it is indicated by no bits toggling.
755 *
756 * Error are indicated by toggling bits or bits held with the wrong value,
757 * or with bits toggling.
758 *
759 * Note that anything more complicated than checking if no bits are toggling
760 * (including checking DQ5 for an error status) is tricky to get working
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300761 * correctly and is therefore not done (particularly with interleaved chips
762 * as each chip must be checked independently of the others).
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100763 *
764 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100765static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100766{
767 map_word oldd, curd;
768
769 oldd = map_read(map, addr);
770 curd = map_read(map, addr);
771
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000772 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100773 map_word_equal(map, curd, expected);
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
777{
778 DECLARE_WAITQUEUE(wait, current);
779 struct cfi_private *cfi = map->fldrv_priv;
780 unsigned long timeo;
781 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
782
783 resettime:
784 timeo = jiffies + HZ;
785 retry:
786 switch (chip->state) {
787
788 case FL_STATUS:
789 for (;;) {
790 if (chip_ready(map, adr))
791 break;
792
793 if (time_after(jiffies, timeo)) {
794 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EIO;
796 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200797 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200799 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Someone else might have been playing with it. */
801 goto retry;
802 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case FL_READY:
805 case FL_CFI_QUERY:
806 case FL_JEDEC_QUERY:
807 return 0;
808
809 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100810 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
811 !(mode == FL_READY || mode == FL_POINT ||
812 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto sleep;
814
Joakim Tjernlundf48bbba2018-03-01 14:39:41 +0100815 /* Do not allow suspend iff read/write to EB address */
816 if ((adr & chip->in_progress_block_mask) ==
817 chip->in_progress_block_addr)
818 goto sleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* Erase suspend */
821 /* It's harmless to issue the Erase-Suspend and Erase-Resume
822 * commands when the erase algorithm isn't in progress. */
823 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
824 chip->oldstate = FL_ERASING;
825 chip->state = FL_ERASE_SUSPENDING;
826 chip->erase_suspended = 1;
827 for (;;) {
828 if (chip_ready(map, adr))
829 break;
830
831 if (time_after(jiffies, timeo)) {
832 /* Should have suspended the erase by now.
833 * Send an Erase-Resume command as either
834 * there was an error (so leave the erase
835 * routine to recover from it) or we trying to
836 * use the erase-in-progress sector. */
Tadashi Abe100f2342011-05-19 15:58:15 +0900837 put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
839 return -EIO;
840 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000841
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200842 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200844 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
846 So we can just loop here. */
847 }
848 chip->state = FL_READY;
849 return 0;
850
Todd Poynor02b15e32005-06-07 00:04:39 +0100851 case FL_XIP_WHILE_ERASING:
852 if (mode != FL_READY && mode != FL_POINT &&
853 (!cfip || !(cfip->EraseSuspend&2)))
854 goto sleep;
855 chip->oldstate = chip->state;
856 chip->state = FL_READY;
857 return 0;
858
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700859 case FL_SHUTDOWN:
860 /* The machine is rebooting */
861 return -EIO;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 case FL_POINT:
864 /* Only if there's no operation suspended... */
865 if (mode == FL_READY && chip->oldstate == FL_READY)
866 return 0;
867
868 default:
869 sleep:
870 set_current_state(TASK_UNINTERRUPTIBLE);
871 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200872 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 schedule();
874 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200875 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto resettime;
877 }
878}
879
880
881static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
882{
883 struct cfi_private *cfi = map->fldrv_priv;
884
885 switch(chip->oldstate) {
886 case FL_ERASING:
Gerlando Falauto42096282012-07-03 09:09:47 +0200887 cfi_fixup_m29ew_erase_suspend(map,
888 chip->in_progress_block_addr);
Guillaume LECERF08968042010-10-26 10:45:23 +0100889 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Gerlando Falauto42096282012-07-03 09:09:47 +0200890 cfi_fixup_m29ew_delay_after_resume(cfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 chip->oldstate = FL_READY;
892 chip->state = FL_ERASING;
893 break;
894
Todd Poynor02b15e32005-06-07 00:04:39 +0100895 case FL_XIP_WHILE_ERASING:
896 chip->state = chip->oldstate;
897 chip->oldstate = FL_READY;
898 break;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case FL_READY:
901 case FL_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903 default:
904 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
905 }
906 wake_up(&chip->wq);
907}
908
Todd Poynor02b15e32005-06-07 00:04:39 +0100909#ifdef CONFIG_MTD_XIP
910
911/*
912 * No interrupt what so ever can be serviced while the flash isn't in array
913 * mode. This is ensured by the xip_disable() and xip_enable() functions
914 * enclosing any code path where the flash is known not to be in array mode.
915 * And within a XIP disabled code path, only functions marked with __xipram
916 * may be called and nothing else (it's a good thing to inspect generated
917 * assembly to make sure inline functions were actually inlined and that gcc
918 * didn't emit calls to its own support functions). Also configuring MTD CFI
919 * support to a single buswidth and a single interleave is also recommended.
920 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200921
Todd Poynor02b15e32005-06-07 00:04:39 +0100922static void xip_disable(struct map_info *map, struct flchip *chip,
923 unsigned long adr)
924{
925 /* TODO: chips with no XIP use should ignore and return */
926 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
927 local_irq_disable();
928}
929
930static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
931 unsigned long adr)
932{
933 struct cfi_private *cfi = map->fldrv_priv;
934
935 if (chip->state != FL_POINT && chip->state != FL_READY) {
936 map_write(map, CMD(0xf0), adr);
937 chip->state = FL_READY;
938 }
939 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200940 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100941 local_irq_enable();
942}
943
944/*
945 * When a delay is required for the flash operation to complete, the
946 * xip_udelay() function is polling for both the given timeout and pending
947 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000948 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100949 * and interrupts unmasked. Task scheduling might also happen at that
950 * point. The CPU eventually returns from the interrupt or the call to
951 * schedule() and the suspended flash operation is resumed for the remaining
952 * of the delay period.
953 *
954 * Warning: this function _will_ fool interrupt latency tracing tools.
955 */
956
957static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
958 unsigned long adr, int usec)
959{
960 struct cfi_private *cfi = map->fldrv_priv;
961 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
962 map_word status, OK = CMD(0x80);
963 unsigned long suspended, start = xip_currtime();
964 flstate_t oldstate;
965
966 do {
967 cpu_relax();
968 if (xip_irqpending() && extp &&
969 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
970 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
971 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000972 * Let's suspend the erase operation when supported.
973 * Note that we currently don't try to suspend
974 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100975 * operation suspended (imagine what happens
976 * when one chip was already done with the current
977 * operation while another chip suspended it, then
978 * we resume the whole thing at once). Yes, it
979 * can happen!
980 */
981 map_write(map, CMD(0xb0), adr);
982 usec -= xip_elapsed_since(start);
983 suspended = xip_currtime();
984 do {
985 if (xip_elapsed_since(suspended) > 100000) {
986 /*
987 * The chip doesn't want to suspend
988 * after waiting for 100 msecs.
989 * This is a critical error but there
990 * is not much we can do here.
991 */
992 return;
993 }
994 status = map_read(map, adr);
995 } while (!map_word_andequal(map, status, OK, OK));
996
997 /* Suspend succeeded */
998 oldstate = chip->state;
999 if (!map_word_bitsset(map, status, CMD(0x40)))
1000 break;
1001 chip->state = FL_XIP_WHILE_ERASING;
1002 chip->erase_suspended = 1;
1003 map_write(map, CMD(0xf0), adr);
1004 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001005 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +01001006 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001007 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001008 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +01001009 cond_resched();
1010
1011 /*
1012 * We're back. However someone else might have
1013 * decided to go write to the chip if we are in
1014 * a suspended erase state. If so let's wait
1015 * until it's done.
1016 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001017 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001018 while (chip->state != FL_XIP_WHILE_ERASING) {
1019 DECLARE_WAITQUEUE(wait, current);
1020 set_current_state(TASK_UNINTERRUPTIBLE);
1021 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001022 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001023 schedule();
1024 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001025 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +01001026 }
1027 /* Disallow XIP again */
1028 local_irq_disable();
1029
Gerlando Falauto42096282012-07-03 09:09:47 +02001030 /* Correct Erase Suspend Hangups for M29EW */
1031 cfi_fixup_m29ew_erase_suspend(map, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001032 /* Resume the write or erase operation */
Guillaume LECERF08968042010-10-26 10:45:23 +01001033 map_write(map, cfi->sector_erase_cmd, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001034 chip->state = oldstate;
1035 start = xip_currtime();
1036 } else if (usec >= 1000000/HZ) {
1037 /*
1038 * Try to save on CPU power when waiting delay
1039 * is at least a system timer tick period.
1040 * No need to be extremely accurate here.
1041 */
1042 xip_cpu_idle();
1043 }
1044 status = map_read(map, adr);
1045 } while (!map_word_andequal(map, status, OK, OK)
1046 && xip_elapsed_since(start) < usec);
1047}
1048
1049#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1050
1051/*
1052 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1053 * the flash is actively programming or erasing since we have to poll for
1054 * the operation to complete anyway. We can't do that in a generic way with
1055 * a XIP setup so do it before the actual flash operation in this case
1056 * and stub it out from INVALIDATE_CACHE_UDELAY.
1057 */
1058#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1059 INVALIDATE_CACHED_RANGE(map, from, size)
1060
1061#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1062 UDELAY(map, chip, adr, usec)
1063
1064/*
1065 * Extra notes:
1066 *
1067 * Activating this XIP support changes the way the code works a bit. For
1068 * example the code to suspend the current process when concurrent access
1069 * happens is never executed because xip_udelay() will always return with the
1070 * same chip state as it was entered with. This is why there is no care for
1071 * the presence of add_wait_queue() or schedule() calls from within a couple
1072 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1073 * The queueing and scheduling are always happening within xip_udelay().
1074 *
1075 * Similarly, get_chip() and put_chip() just happen to always be executed
1076 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1077 * is in array mode, therefore never executing many cases therein and not
1078 * causing any problem with XIP.
1079 */
1080
1081#else
1082
1083#define xip_disable(map, chip, adr)
1084#define xip_enable(map, chip, adr)
1085#define XIP_INVAL_CACHED_RANGE(x...)
1086
1087#define UDELAY(map, chip, adr, usec) \
1088do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001089 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001090 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001091 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001092} while (0)
1093
1094#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
1095do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001096 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001097 INVALIDATE_CACHED_RANGE(map, adr, len); \
1098 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001099 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +01001100} while (0)
1101
1102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1105{
1106 unsigned long cmd_addr;
1107 struct cfi_private *cfi = map->fldrv_priv;
1108 int ret;
1109
1110 adr += chip->start;
1111
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001112 /* Ensure cmd read/writes are aligned. */
1113 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001115 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ret = get_chip(map, chip, cmd_addr, FL_READY);
1117 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001118 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return ret;
1120 }
1121
1122 if (chip->state != FL_POINT && chip->state != FL_READY) {
1123 map_write(map, CMD(0xf0), cmd_addr);
1124 chip->state = FL_READY;
1125 }
1126
1127 map_copy_from(map, buf, adr, len);
1128
1129 put_chip(map, chip, cmd_addr);
1130
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001131 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 0;
1133}
1134
1135
1136static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1137{
1138 struct map_info *map = mtd->priv;
1139 struct cfi_private *cfi = map->fldrv_priv;
1140 unsigned long ofs;
1141 int chipnum;
1142 int ret = 0;
1143
1144 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 chipnum = (from >> cfi->chipshift);
1146 ofs = from - (chipnum << cfi->chipshift);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 while (len) {
1149 unsigned long thislen;
1150
1151 if (chipnum >= cfi->numchips)
1152 break;
1153
1154 if ((len + ofs -1) >> cfi->chipshift)
1155 thislen = (1<<cfi->chipshift) - ofs;
1156 else
1157 thislen = len;
1158
1159 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1160 if (ret)
1161 break;
1162
1163 *retlen += thislen;
1164 len -= thislen;
1165 buf += thislen;
1166
1167 ofs = 0;
1168 chipnum++;
1169 }
1170 return ret;
1171}
1172
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001173typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Christian Riesch4f5cb242014-03-06 13:18:30 +01001174 loff_t adr, size_t len, u_char *buf, size_t grouplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Christian Rieschfeb86772013-04-26 21:10:28 +02001176static inline void otp_enter(struct map_info *map, struct flchip *chip,
1177 loff_t adr, size_t len)
1178{
1179 struct cfi_private *cfi = map->fldrv_priv;
1180
1181 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1182 cfi->device_type, NULL);
1183 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1184 cfi->device_type, NULL);
1185 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
1186 cfi->device_type, NULL);
1187
1188 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1189}
1190
1191static inline void otp_exit(struct map_info *map, struct flchip *chip,
1192 loff_t adr, size_t len)
1193{
1194 struct cfi_private *cfi = map->fldrv_priv;
1195
1196 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1197 cfi->device_type, NULL);
1198 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1199 cfi->device_type, NULL);
1200 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
1201 cfi->device_type, NULL);
1202 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
1203 cfi->device_type, NULL);
1204
1205 INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1206}
1207
Christian Riesch4f5cb242014-03-06 13:18:30 +01001208static inline int do_read_secsi_onechip(struct map_info *map,
1209 struct flchip *chip, loff_t adr,
1210 size_t len, u_char *buf,
1211 size_t grouplen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 DECLARE_WAITQUEUE(wait, current);
1214 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001217 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 set_current_state(TASK_UNINTERRUPTIBLE);
1221 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001222
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001223 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 schedule();
1226 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 timeo = jiffies + HZ;
1228
1229 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 adr += chip->start;
1233
1234 chip->state = FL_READY;
1235
Christian Rieschfeb86772013-04-26 21:10:28 +02001236 otp_enter(map, chip, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 map_copy_from(map, buf, adr, len);
Christian Rieschfeb86772013-04-26 21:10:28 +02001238 otp_exit(map, chip, adr, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001241 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 return 0;
1244}
1245
1246static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1247{
1248 struct map_info *map = mtd->priv;
1249 struct cfi_private *cfi = map->fldrv_priv;
1250 unsigned long ofs;
1251 int chipnum;
1252 int ret = 0;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* ofs: offset within the first chip that the first read should start */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* 8 secsi bytes per chip */
1256 chipnum=from>>3;
1257 ofs=from & 7;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 while (len) {
1260 unsigned long thislen;
1261
1262 if (chipnum >= cfi->numchips)
1263 break;
1264
1265 if ((len + ofs -1) >> 3)
1266 thislen = (1<<3) - ofs;
1267 else
1268 thislen = len;
1269
Christian Riesch4f5cb242014-03-06 13:18:30 +01001270 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
1271 thislen, buf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (ret)
1273 break;
1274
1275 *retlen += thislen;
1276 len -= thislen;
1277 buf += thislen;
1278
1279 ofs = 0;
1280 chipnum++;
1281 }
1282 return ret;
1283}
1284
Christian Rieschaf744752014-03-06 13:18:29 +01001285static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1286 unsigned long adr, map_word datum,
1287 int mode);
1288
1289static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
Christian Riesch4f5cb242014-03-06 13:18:30 +01001290 size_t len, u_char *buf, size_t grouplen)
Christian Rieschaf744752014-03-06 13:18:29 +01001291{
1292 int ret;
1293 while (len) {
1294 unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
1295 int gap = adr - bus_ofs;
1296 int n = min_t(int, len, map_bankwidth(map) - gap);
Christian Riesch636fdbf2015-03-31 23:29:22 +02001297 map_word datum = map_word_ff(map);
Christian Rieschaf744752014-03-06 13:18:29 +01001298
1299 if (n != map_bankwidth(map)) {
1300 /* partial write of a word, load old contents */
1301 otp_enter(map, chip, bus_ofs, map_bankwidth(map));
1302 datum = map_read(map, bus_ofs);
1303 otp_exit(map, chip, bus_ofs, map_bankwidth(map));
1304 }
1305
1306 datum = map_word_load_partial(map, datum, buf, gap, n);
1307 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
1308 if (ret)
1309 return ret;
1310
1311 adr += n;
1312 buf += n;
1313 len -= n;
1314 }
1315
1316 return 0;
1317}
1318
Christian Riesch4f5cb242014-03-06 13:18:30 +01001319static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
1320 size_t len, u_char *buf, size_t grouplen)
1321{
1322 struct cfi_private *cfi = map->fldrv_priv;
1323 uint8_t lockreg;
1324 unsigned long timeo;
1325 int ret;
1326
1327 /* make sure area matches group boundaries */
1328 if ((adr != 0) || (len != grouplen))
1329 return -EINVAL;
1330
1331 mutex_lock(&chip->mutex);
1332 ret = get_chip(map, chip, chip->start, FL_LOCKING);
1333 if (ret) {
1334 mutex_unlock(&chip->mutex);
1335 return ret;
1336 }
1337 chip->state = FL_LOCKING;
1338
1339 /* Enter lock register command */
1340 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1341 cfi->device_type, NULL);
1342 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1343 cfi->device_type, NULL);
1344 cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
1345 cfi->device_type, NULL);
1346
1347 /* read lock register */
1348 lockreg = cfi_read_query(map, 0);
1349
1350 /* set bit 0 to protect extended memory block */
1351 lockreg &= ~0x01;
1352
1353 /* set bit 0 to protect extended memory block */
1354 /* write lock register */
1355 map_write(map, CMD(0xA0), chip->start);
1356 map_write(map, CMD(lockreg), chip->start);
1357
1358 /* wait for chip to become ready */
1359 timeo = jiffies + msecs_to_jiffies(2);
1360 for (;;) {
1361 if (chip_ready(map, adr))
1362 break;
1363
1364 if (time_after(jiffies, timeo)) {
1365 pr_err("Waiting for chip to be ready timed out.\n");
1366 ret = -EIO;
1367 break;
1368 }
1369 UDELAY(map, chip, 0, 1);
1370 }
1371
1372 /* exit protection commands */
1373 map_write(map, CMD(0x90), chip->start);
1374 map_write(map, CMD(0x00), chip->start);
1375
1376 chip->state = FL_READY;
1377 put_chip(map, chip, chip->start);
1378 mutex_unlock(&chip->mutex);
1379
1380 return ret;
1381}
1382
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001383static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1384 size_t *retlen, u_char *buf,
1385 otp_op_t action, int user_regs)
1386{
1387 struct map_info *map = mtd->priv;
1388 struct cfi_private *cfi = map->fldrv_priv;
1389 int ofs_factor = cfi->interleave * cfi->device_type;
1390 unsigned long base;
1391 int chipnum;
1392 struct flchip *chip;
1393 uint8_t otp, lockreg;
1394 int ret;
1395
1396 size_t user_size, factory_size, otpsize;
1397 loff_t user_offset, factory_offset, otpoffset;
1398 int user_locked = 0, otplocked;
1399
1400 *retlen = 0;
1401
1402 for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
1403 chip = &cfi->chips[chipnum];
1404 factory_size = 0;
1405 user_size = 0;
1406
1407 /* Micron M29EW family */
1408 if (is_m29ew(cfi)) {
1409 base = chip->start;
1410
1411 /* check whether secsi area is factory locked
1412 or user lockable */
1413 mutex_lock(&chip->mutex);
1414 ret = get_chip(map, chip, base, FL_CFI_QUERY);
1415 if (ret) {
1416 mutex_unlock(&chip->mutex);
1417 return ret;
1418 }
1419 cfi_qry_mode_on(base, map, cfi);
1420 otp = cfi_read_query(map, base + 0x3 * ofs_factor);
1421 cfi_qry_mode_off(base, map, cfi);
1422 put_chip(map, chip, base);
1423 mutex_unlock(&chip->mutex);
1424
1425 if (otp & 0x80) {
1426 /* factory locked */
1427 factory_offset = 0;
1428 factory_size = 0x100;
1429 } else {
1430 /* customer lockable */
1431 user_offset = 0;
1432 user_size = 0x100;
1433
1434 mutex_lock(&chip->mutex);
1435 ret = get_chip(map, chip, base, FL_LOCKING);
Brian Norris5d20bad2014-08-13 23:35:24 -07001436 if (ret) {
1437 mutex_unlock(&chip->mutex);
1438 return ret;
1439 }
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001440
1441 /* Enter lock register command */
1442 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
1443 chip->start, map, cfi,
1444 cfi->device_type, NULL);
1445 cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
1446 chip->start, map, cfi,
1447 cfi->device_type, NULL);
1448 cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
1449 chip->start, map, cfi,
1450 cfi->device_type, NULL);
1451 /* read lock register */
1452 lockreg = cfi_read_query(map, 0);
1453 /* exit protection commands */
1454 map_write(map, CMD(0x90), chip->start);
1455 map_write(map, CMD(0x00), chip->start);
1456 put_chip(map, chip, chip->start);
1457 mutex_unlock(&chip->mutex);
1458
1459 user_locked = ((lockreg & 0x01) == 0x00);
1460 }
1461 }
1462
1463 otpsize = user_regs ? user_size : factory_size;
1464 if (!otpsize)
1465 continue;
1466 otpoffset = user_regs ? user_offset : factory_offset;
1467 otplocked = user_regs ? user_locked : 1;
1468
1469 if (!action) {
1470 /* return otpinfo */
1471 struct otp_info *otpinfo;
1472 len -= sizeof(*otpinfo);
1473 if (len <= 0)
1474 return -ENOSPC;
1475 otpinfo = (struct otp_info *)buf;
1476 otpinfo->start = from;
1477 otpinfo->length = otpsize;
1478 otpinfo->locked = otplocked;
1479 buf += sizeof(*otpinfo);
1480 *retlen += sizeof(*otpinfo);
1481 from += otpsize;
1482 } else if ((from < otpsize) && (len > 0)) {
1483 size_t size;
1484 size = (len < otpsize - from) ? len : otpsize - from;
Christian Riesch4f5cb242014-03-06 13:18:30 +01001485 ret = action(map, chip, otpoffset + from, size, buf,
1486 otpsize);
Christian Rieschdc7e9ec2014-03-06 13:18:27 +01001487 if (ret < 0)
1488 return ret;
1489
1490 buf += size;
1491 len -= size;
1492 *retlen += size;
1493 from = 0;
1494 } else {
1495 from -= otpsize;
1496 }
1497 }
1498 return 0;
1499}
1500
1501static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
1502 size_t *retlen, struct otp_info *buf)
1503{
1504 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1505 NULL, 0);
1506}
1507
1508static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
1509 size_t *retlen, struct otp_info *buf)
1510{
1511 return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1512 NULL, 1);
1513}
1514
1515static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1516 size_t len, size_t *retlen,
1517 u_char *buf)
1518{
1519 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1520 buf, do_read_secsi_onechip, 0);
1521}
1522
1523static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1524 size_t len, size_t *retlen,
1525 u_char *buf)
1526{
1527 return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1528 buf, do_read_secsi_onechip, 1);
1529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Christian Rieschaf744752014-03-06 13:18:29 +01001531static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1532 size_t len, size_t *retlen,
1533 u_char *buf)
1534{
1535 return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
1536 do_otp_write, 1);
1537}
1538
Christian Riesch4f5cb242014-03-06 13:18:30 +01001539static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1540 size_t len)
1541{
1542 size_t retlen;
1543 return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
1544 do_otp_lock, 1);
1545}
1546
Christian Rieschaf744752014-03-06 13:18:29 +01001547static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1548 unsigned long adr, map_word datum,
1549 int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct cfi_private *cfi = map->fldrv_priv;
1552 unsigned long timeo = jiffies + HZ;
1553 /*
1554 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1555 * have a max write time of a few hundreds usec). However, we should
1556 * use the maximum timeout value given by the chip at probe time
1557 * instead. Unfortunately, struct flchip does have a field for
1558 * maximum timeout, only for typical which can be far too short
1559 * depending of the conditions. The ' + 1' is to avoid having a
1560 * timeout of 0 jiffies if HZ is smaller than 1000.
1561 */
1562 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1563 int ret = 0;
1564 map_word oldd;
1565 int retry_cnt = 0;
1566
1567 adr += chip->start;
1568
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001569 mutex_lock(&chip->mutex);
Christian Rieschaf744752014-03-06 13:18:29 +01001570 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001572 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return ret;
1574 }
1575
Brian Norris289c0522011-07-19 10:06:09 -07001576 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 __func__, adr, datum.x[0] );
1578
Christian Rieschaf744752014-03-06 13:18:29 +01001579 if (mode == FL_OTP_WRITE)
1580 otp_enter(map, chip, adr, map_bankwidth(map));
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
1583 * Check for a NOP for the case when the datum to write is already
1584 * present - it saves time and works around buggy chips that corrupt
1585 * data at other locations when 0xff is written to a location that
1586 * already contains 0xff.
1587 */
1588 oldd = map_read(map, adr);
1589 if (map_word_equal(map, oldd, datum)) {
Brian Norris289c0522011-07-19 10:06:09 -07001590 pr_debug("MTD %s(): NOP\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 __func__);
1592 goto op_done;
1593 }
1594
Todd Poynor02b15e32005-06-07 00:04:39 +01001595 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001597 xip_disable(map, chip, adr);
Christian Rieschaf744752014-03-06 13:18:29 +01001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 retry:
1600 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1601 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1602 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1603 map_write(map, datum, adr);
Christian Rieschaf744752014-03-06 13:18:29 +01001604 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Todd Poynor02b15e32005-06-07 00:04:39 +01001606 INVALIDATE_CACHE_UDELAY(map, chip,
1607 adr, map_bankwidth(map),
1608 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001611 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 for (;;) {
Christian Rieschaf744752014-03-06 13:18:29 +01001613 if (chip->state != mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* Someone's suspended the write. Sleep */
1615 DECLARE_WAITQUEUE(wait, current);
1616
1617 set_current_state(TASK_UNINTERRUPTIBLE);
1618 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001619 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 schedule();
1621 remove_wait_queue(&chip->wq, &wait);
1622 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001623 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 continue;
1625 }
1626
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001627 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001628 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001629 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001630 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001631 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001634 if (chip_ready(map, adr))
1635 break;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001638 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001640 /* Did we succeed? */
1641 if (!chip_good(map, adr, datum)) {
1642 /* reset on all failures. */
1643 map_write( map, CMD(0xF0), chip->start );
1644 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001646 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001647 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001649 ret = -EIO;
1650 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001651 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 op_done:
Christian Rieschaf744752014-03-06 13:18:29 +01001653 if (mode == FL_OTP_WRITE)
1654 otp_exit(map, chip, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001656 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001658 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 return ret;
1661}
1662
1663
1664static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1665 size_t *retlen, const u_char *buf)
1666{
1667 struct map_info *map = mtd->priv;
1668 struct cfi_private *cfi = map->fldrv_priv;
1669 int ret = 0;
1670 int chipnum;
1671 unsigned long ofs, chipstart;
1672 DECLARE_WAITQUEUE(wait, current);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 chipnum = to >> cfi->chipshift;
1675 ofs = to - (chipnum << cfi->chipshift);
1676 chipstart = cfi->chips[chipnum].start;
1677
1678 /* If it's not bus-aligned, do the first byte write */
1679 if (ofs & (map_bankwidth(map)-1)) {
1680 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1681 int i = ofs - bus_ofs;
1682 int n = 0;
1683 map_word tmp_buf;
1684
1685 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001686 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 set_current_state(TASK_UNINTERRUPTIBLE);
1690 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1691
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001692 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 schedule();
1695 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto retry;
1697 }
1698
1699 /* Load 'tmp_buf' with old contents of flash */
1700 tmp_buf = map_read(map, bus_ofs+chipstart);
1701
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001702 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* Number of bytes to copy from buffer */
1705 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1708
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001709 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001710 bus_ofs, tmp_buf, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001711 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 ofs += n;
1715 buf += n;
1716 (*retlen) += n;
1717 len -= n;
1718
1719 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001720 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 ofs = 0;
1722 if (chipnum == cfi->numchips)
1723 return 0;
1724 }
1725 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* We are now aligned, write as much as possible */
1728 while(len >= map_bankwidth(map)) {
1729 map_word datum;
1730
1731 datum = map_word_load(map, buf);
1732
1733 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001734 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (ret)
1736 return ret;
1737
1738 ofs += map_bankwidth(map);
1739 buf += map_bankwidth(map);
1740 (*retlen) += map_bankwidth(map);
1741 len -= map_bankwidth(map);
1742
1743 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001744 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ofs = 0;
1746 if (chipnum == cfi->numchips)
1747 return 0;
1748 chipstart = cfi->chips[chipnum].start;
1749 }
1750 }
1751
1752 /* Write the trailing bytes if any */
1753 if (len & (map_bankwidth(map)-1)) {
1754 map_word tmp_buf;
1755
1756 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001757 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 set_current_state(TASK_UNINTERRUPTIBLE);
1761 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1762
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001763 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 schedule();
1766 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto retry1;
1768 }
1769
1770 tmp_buf = map_read(map, ofs + chipstart);
1771
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001772 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001775
1776 ret = do_write_oneword(map, &cfi->chips[chipnum],
Christian Rieschaf744752014-03-06 13:18:29 +01001777 ofs, tmp_buf, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001778 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 (*retlen) += len;
1782 }
1783
1784 return 0;
1785}
1786
1787
1788/*
1789 * FIXME: interleaved mode not tested, and probably not supported!
1790 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001791static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001792 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001793 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct cfi_private *cfi = map->fldrv_priv;
1796 unsigned long timeo = jiffies + HZ;
Bean Huo6534e682014-07-16 09:05:26 -07001797 /*
1798 * Timeout is calculated according to CFI data, if available.
1799 * See more comments in cfi_cmdset_0002().
1800 */
1801 unsigned long uWriteTimeout =
1802 usecs_to_jiffies(chip->buffer_write_time_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 int ret = -EIO;
1804 unsigned long cmd_adr;
1805 int z, words;
1806 map_word datum;
1807
1808 adr += chip->start;
1809 cmd_adr = adr;
1810
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001811 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 ret = get_chip(map, chip, adr, FL_WRITING);
1813 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001814 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return ret;
1816 }
1817
1818 datum = map_word_load(map, buf);
1819
Brian Norris289c0522011-07-19 10:06:09 -07001820 pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 __func__, adr, datum.x[0] );
1822
Todd Poynor02b15e32005-06-07 00:04:39 +01001823 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001825 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1828 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /* Write Buffer Load */
1831 map_write(map, CMD(0x25), cmd_adr);
1832
1833 chip->state = FL_WRITING_TO_BUFFER;
1834
1835 /* Write length of data to come */
1836 words = len / map_bankwidth(map);
1837 map_write(map, CMD(words - 1), cmd_adr);
1838 /* Write data */
1839 z = 0;
1840 while(z < words * map_bankwidth(map)) {
1841 datum = map_word_load(map, buf);
1842 map_write(map, datum, adr + z);
1843
1844 z += map_bankwidth(map);
1845 buf += map_bankwidth(map);
1846 }
1847 z -= map_bankwidth(map);
1848
1849 adr += z;
1850
1851 /* Write Buffer Program Confirm: GO GO GO */
1852 map_write(map, CMD(0x29), cmd_adr);
1853 chip->state = FL_WRITING;
1854
Todd Poynor02b15e32005-06-07 00:04:39 +01001855 INVALIDATE_CACHE_UDELAY(map, chip,
1856 adr, map_bankwidth(map),
1857 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001859 timeo = jiffies + uWriteTimeout;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 for (;;) {
1862 if (chip->state != FL_WRITING) {
1863 /* Someone's suspended the write. Sleep */
1864 DECLARE_WAITQUEUE(wait, current);
1865
1866 set_current_state(TASK_UNINTERRUPTIBLE);
1867 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001868 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 schedule();
1870 remove_wait_queue(&chip->wq, &wait);
1871 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001872 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 continue;
1874 }
1875
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001876 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1877 break;
1878
Todd Poynor02b15e32005-06-07 00:04:39 +01001879 if (chip_ready(map, adr)) {
1880 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001885 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Harald Nordgard-Hansen070c3222012-11-23 23:11:03 +01001888 /*
1889 * Recovery from write-buffer programming failures requires
1890 * the write-to-buffer-reset sequence. Since the last part
1891 * of the sequence also works as a normal reset, we can run
1892 * the same commands regardless of why we are here.
1893 * See e.g.
1894 * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
1895 */
1896 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1897 cfi->device_type, NULL);
1898 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1899 cfi->device_type, NULL);
1900 cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
1901 cfi->device_type, NULL);
Todd Poynor02b15e32005-06-07 00:04:39 +01001902 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* FIXME - should have reset delay before continuing */
1904
Huang Shijie25983b12013-04-01 17:40:18 +08001905 printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
1906 __func__, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ret = -EIO;
1909 op_done:
1910 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00001911 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001913 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 return ret;
1916}
1917
1918
1919static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1920 size_t *retlen, const u_char *buf)
1921{
1922 struct map_info *map = mtd->priv;
1923 struct cfi_private *cfi = map->fldrv_priv;
1924 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1925 int ret = 0;
1926 int chipnum;
1927 unsigned long ofs;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 chipnum = to >> cfi->chipshift;
1930 ofs = to - (chipnum << cfi->chipshift);
1931
1932 /* If it's not bus-aligned, do the first word write */
1933 if (ofs & (map_bankwidth(map)-1)) {
1934 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1935 if (local_len > len)
1936 local_len = len;
1937 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1938 local_len, retlen, buf);
1939 if (ret)
1940 return ret;
1941 ofs += local_len;
1942 buf += local_len;
1943 len -= local_len;
1944
1945 if (ofs >> cfi->chipshift) {
1946 chipnum ++;
1947 ofs = 0;
1948 if (chipnum == cfi->numchips)
1949 return 0;
1950 }
1951 }
1952
1953 /* Write buffer is worth it only if more than one word to write... */
1954 while (len >= map_bankwidth(map) * 2) {
1955 /* We must not cross write block boundaries */
1956 int size = wbufsize - (ofs & (wbufsize-1));
1957
1958 if (size > len)
1959 size = len;
1960 if (size % map_bankwidth(map))
1961 size -= size % map_bankwidth(map);
1962
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001963 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 ofs, buf, size);
1965 if (ret)
1966 return ret;
1967
1968 ofs += size;
1969 buf += size;
1970 (*retlen) += size;
1971 len -= size;
1972
1973 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001974 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ofs = 0;
1976 if (chipnum == cfi->numchips)
1977 return 0;
1978 }
1979 }
1980
1981 if (len) {
1982 size_t retlen_dregs = 0;
1983
1984 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1985 len, &retlen_dregs, buf);
1986
1987 *retlen += retlen_dregs;
1988 return ret;
1989 }
1990
1991 return 0;
1992}
1993
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08001994/*
1995 * Wait for the flash chip to become ready to write data
1996 *
1997 * This is only called during the panic_write() path. When panic_write()
1998 * is called, the kernel is in the process of a panic, and will soon be
1999 * dead. Therefore we don't take any locks, and attempt to get access
2000 * to the chip as soon as possible.
2001 */
2002static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
2003 unsigned long adr)
2004{
2005 struct cfi_private *cfi = map->fldrv_priv;
2006 int retries = 10;
2007 int i;
2008
2009 /*
2010 * If the driver thinks the chip is idle, and no toggle bits
2011 * are changing, then the chip is actually idle for sure.
2012 */
2013 if (chip->state == FL_READY && chip_ready(map, adr))
2014 return 0;
2015
2016 /*
2017 * Try several times to reset the chip and then wait for it
2018 * to become idle. The upper limit of a few milliseconds of
2019 * delay isn't a big problem: the kernel is dying anyway. It
2020 * is more important to save the messages.
2021 */
2022 while (retries > 0) {
2023 const unsigned long timeo = (HZ / 1000) + 1;
2024
2025 /* send the reset command */
2026 map_write(map, CMD(0xF0), chip->start);
2027
2028 /* wait for the chip to become ready */
2029 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
2030 if (chip_ready(map, adr))
2031 return 0;
2032
2033 udelay(1);
2034 }
Brian Norris36c6a7ac2014-07-21 19:06:19 -07002035
2036 retries--;
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08002037 }
2038
2039 /* the chip never became ready */
2040 return -EBUSY;
2041}
2042
2043/*
2044 * Write out one word of data to a single flash chip during a kernel panic
2045 *
2046 * This is only called during the panic_write() path. When panic_write()
2047 * is called, the kernel is in the process of a panic, and will soon be
2048 * dead. Therefore we don't take any locks, and attempt to get access
2049 * to the chip as soon as possible.
2050 *
2051 * The implementation of this routine is intentionally similar to
2052 * do_write_oneword(), in order to ease code maintenance.
2053 */
2054static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
2055 unsigned long adr, map_word datum)
2056{
2057 const unsigned long uWriteTimeout = (HZ / 1000) + 1;
2058 struct cfi_private *cfi = map->fldrv_priv;
2059 int retry_cnt = 0;
2060 map_word oldd;
2061 int ret = 0;
2062 int i;
2063
2064 adr += chip->start;
2065
2066 ret = cfi_amdstd_panic_wait(map, chip, adr);
2067 if (ret)
2068 return ret;
2069
2070 pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
2071 __func__, adr, datum.x[0]);
2072
2073 /*
2074 * Check for a NOP for the case when the datum to write is already
2075 * present - it saves time and works around buggy chips that corrupt
2076 * data at other locations when 0xff is written to a location that
2077 * already contains 0xff.
2078 */
2079 oldd = map_read(map, adr);
2080 if (map_word_equal(map, oldd, datum)) {
2081 pr_debug("MTD %s(): NOP\n", __func__);
2082 goto op_done;
2083 }
2084
2085 ENABLE_VPP(map);
2086
2087retry:
2088 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2089 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2090 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2091 map_write(map, datum, adr);
2092
2093 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
2094 if (chip_ready(map, adr))
2095 break;
2096
2097 udelay(1);
2098 }
2099
2100 if (!chip_good(map, adr, datum)) {
2101 /* reset on all failures. */
2102 map_write(map, CMD(0xF0), chip->start);
2103 /* FIXME - should have reset delay before continuing */
2104
2105 if (++retry_cnt <= MAX_WORD_RETRIES)
2106 goto retry;
2107
2108 ret = -EIO;
2109 }
2110
2111op_done:
2112 DISABLE_VPP(map);
2113 return ret;
2114}
2115
2116/*
2117 * Write out some data during a kernel panic
2118 *
2119 * This is used by the mtdoops driver to save the dying messages from a
2120 * kernel which has panic'd.
2121 *
2122 * This routine ignores all of the locking used throughout the rest of the
2123 * driver, in order to ensure that the data gets written out no matter what
2124 * state this driver (and the flash chip itself) was in when the kernel crashed.
2125 *
2126 * The implementation of this routine is intentionally similar to
2127 * cfi_amdstd_write_words(), in order to ease code maintenance.
2128 */
2129static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
2130 size_t *retlen, const u_char *buf)
2131{
2132 struct map_info *map = mtd->priv;
2133 struct cfi_private *cfi = map->fldrv_priv;
2134 unsigned long ofs, chipstart;
2135 int ret = 0;
2136 int chipnum;
2137
Ira W. Snyder30ec5a22012-01-06 11:29:19 -08002138 chipnum = to >> cfi->chipshift;
2139 ofs = to - (chipnum << cfi->chipshift);
2140 chipstart = cfi->chips[chipnum].start;
2141
2142 /* If it's not bus aligned, do the first byte write */
2143 if (ofs & (map_bankwidth(map) - 1)) {
2144 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
2145 int i = ofs - bus_ofs;
2146 int n = 0;
2147 map_word tmp_buf;
2148
2149 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
2150 if (ret)
2151 return ret;
2152
2153 /* Load 'tmp_buf' with old contents of flash */
2154 tmp_buf = map_read(map, bus_ofs + chipstart);
2155
2156 /* Number of bytes to copy from buffer */
2157 n = min_t(int, len, map_bankwidth(map) - i);
2158
2159 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
2160
2161 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2162 bus_ofs, tmp_buf);
2163 if (ret)
2164 return ret;
2165
2166 ofs += n;
2167 buf += n;
2168 (*retlen) += n;
2169 len -= n;
2170
2171 if (ofs >> cfi->chipshift) {
2172 chipnum++;
2173 ofs = 0;
2174 if (chipnum == cfi->numchips)
2175 return 0;
2176 }
2177 }
2178
2179 /* We are now aligned, write as much as possible */
2180 while (len >= map_bankwidth(map)) {
2181 map_word datum;
2182
2183 datum = map_word_load(map, buf);
2184
2185 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2186 ofs, datum);
2187 if (ret)
2188 return ret;
2189
2190 ofs += map_bankwidth(map);
2191 buf += map_bankwidth(map);
2192 (*retlen) += map_bankwidth(map);
2193 len -= map_bankwidth(map);
2194
2195 if (ofs >> cfi->chipshift) {
2196 chipnum++;
2197 ofs = 0;
2198 if (chipnum == cfi->numchips)
2199 return 0;
2200
2201 chipstart = cfi->chips[chipnum].start;
2202 }
2203 }
2204
2205 /* Write the trailing bytes if any */
2206 if (len & (map_bankwidth(map) - 1)) {
2207 map_word tmp_buf;
2208
2209 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
2210 if (ret)
2211 return ret;
2212
2213 tmp_buf = map_read(map, ofs + chipstart);
2214
2215 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
2216
2217 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2218 ofs, tmp_buf);
2219 if (ret)
2220 return ret;
2221
2222 (*retlen) += len;
2223 }
2224
2225 return 0;
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*
2230 * Handle devices with one erase region, that only implement
2231 * the chip erase command.
2232 */
Todd Poynor02b15e32005-06-07 00:04:39 +01002233static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 struct cfi_private *cfi = map->fldrv_priv;
2236 unsigned long timeo = jiffies + HZ;
2237 unsigned long int adr;
2238 DECLARE_WAITQUEUE(wait, current);
2239 int ret = 0;
2240
2241 adr = cfi->addr_unlock1;
2242
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002243 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 ret = get_chip(map, chip, adr, FL_WRITING);
2245 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002246 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return ret;
2248 }
2249
Brian Norris289c0522011-07-19 10:06:09 -07002250 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 __func__, chip->start );
2252
Todd Poynor02b15e32005-06-07 00:04:39 +01002253 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01002255 xip_disable(map, chip, adr);
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2258 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2259 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2260 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2261 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2262 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2263
2264 chip->state = FL_ERASING;
2265 chip->erase_suspended = 0;
2266 chip->in_progress_block_addr = adr;
Joakim Tjernlundf48bbba2018-03-01 14:39:41 +01002267 chip->in_progress_block_mask = ~(map->size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Todd Poynor02b15e32005-06-07 00:04:39 +01002269 INVALIDATE_CACHE_UDELAY(map, chip,
2270 adr, map->size,
2271 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 timeo = jiffies + (HZ*20);
2274
2275 for (;;) {
2276 if (chip->state != FL_ERASING) {
2277 /* Someone's suspended the erase. Sleep */
2278 set_current_state(TASK_UNINTERRUPTIBLE);
2279 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002280 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 schedule();
2282 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002283 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 continue;
2285 }
2286 if (chip->erase_suspended) {
2287 /* This erase was suspended and resumed.
2288 Adjust the timeout */
2289 timeo = jiffies + (HZ*20); /* FIXME */
2290 chip->erase_suspended = 0;
2291 }
2292
2293 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 break;
2295
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002296 if (time_after(jiffies, timeo)) {
2297 printk(KERN_WARNING "MTD %s(): software timeout\n",
2298 __func__ );
2299 break;
2300 }
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01002303 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002305 /* Did we succeed? */
2306 if (!chip_good(map, adr, map_word_ff(map))) {
2307 /* reset on all failures. */
2308 map_write( map, CMD(0xF0), chip->start );
2309 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002311 ret = -EIO;
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01002315 xip_enable(map, chip, adr);
Paul Parsonse7d93772012-03-07 14:11:16 +00002316 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002318 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 return ret;
2321}
2322
2323
Todd Poynor02b15e32005-06-07 00:04:39 +01002324static 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 -07002325{
2326 struct cfi_private *cfi = map->fldrv_priv;
2327 unsigned long timeo = jiffies + HZ;
2328 DECLARE_WAITQUEUE(wait, current);
2329 int ret = 0;
2330
2331 adr += chip->start;
2332
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002333 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 ret = get_chip(map, chip, adr, FL_ERASING);
2335 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002336 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return ret;
2338 }
2339
Brian Norris289c0522011-07-19 10:06:09 -07002340 pr_debug("MTD %s(): ERASE 0x%.8lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 __func__, adr );
2342
Todd Poynor02b15e32005-06-07 00:04:39 +01002343 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01002345 xip_disable(map, chip, adr);
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2348 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2349 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2350 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2351 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Guillaume LECERF08968042010-10-26 10:45:23 +01002352 map_write(map, cfi->sector_erase_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 chip->state = FL_ERASING;
2355 chip->erase_suspended = 0;
2356 chip->in_progress_block_addr = adr;
Joakim Tjernlundf48bbba2018-03-01 14:39:41 +01002357 chip->in_progress_block_mask = ~(len - 1);
Todd Poynor02b15e32005-06-07 00:04:39 +01002358
2359 INVALIDATE_CACHE_UDELAY(map, chip,
2360 adr, len,
2361 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 timeo = jiffies + (HZ*20);
2364
2365 for (;;) {
2366 if (chip->state != FL_ERASING) {
2367 /* Someone's suspended the erase. Sleep */
2368 set_current_state(TASK_UNINTERRUPTIBLE);
2369 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002370 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 schedule();
2372 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002373 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 continue;
2375 }
2376 if (chip->erase_suspended) {
2377 /* This erase was suspended and resumed.
2378 Adjust the timeout */
2379 timeo = jiffies + (HZ*20); /* FIXME */
2380 chip->erase_suspended = 0;
2381 }
2382
Todd Poynor02b15e32005-06-07 00:04:39 +01002383 if (chip_ready(map, adr)) {
2384 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01002386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002388 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01002389 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002390 printk(KERN_WARNING "MTD %s(): software timeout\n",
2391 __func__ );
2392 break;
2393 }
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01002396 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002398 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02002399 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002400 /* reset on all failures. */
2401 map_write( map, CMD(0xF0), chip->start );
2402 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01002404 ret = -EIO;
2405 }
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 chip->state = FL_READY;
Paul Parsonse7d93772012-03-07 14:11:16 +00002408 DISABLE_VPP(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002410 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return ret;
2412}
2413
2414
Ben Dooksce0f33a2007-05-28 19:59:00 +01002415static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 unsigned long ofs, len;
2418 int ret;
2419
2420 ofs = instr->addr;
2421 len = instr->len;
2422
2423 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2424 if (ret)
2425 return ret;
2426
2427 instr->state = MTD_ERASE_DONE;
2428 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return 0;
2431}
2432
2433
2434static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
2435{
2436 struct map_info *map = mtd->priv;
2437 struct cfi_private *cfi = map->fldrv_priv;
2438 int ret = 0;
2439
2440 if (instr->addr != 0)
2441 return -EINVAL;
2442
2443 if (instr->len != mtd->size)
2444 return -EINVAL;
2445
2446 ret = do_erase_chip(map, &cfi->chips[0]);
2447 if (ret)
2448 return ret;
2449
2450 instr->state = MTD_ERASE_DONE;
2451 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return 0;
2454}
2455
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002456static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2457 unsigned long adr, int len, void *thunk)
2458{
2459 struct cfi_private *cfi = map->fldrv_priv;
2460 int ret;
2461
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002462 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002463 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2464 if (ret)
2465 goto out_unlock;
2466 chip->state = FL_LOCKING;
2467
Brian Norris0a32a102011-07-19 10:06:10 -07002468 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002469
2470 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2471 cfi->device_type, NULL);
2472 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2473 cfi->device_type, NULL);
2474 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2475 cfi->device_type, NULL);
2476 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2477 cfi->device_type, NULL);
2478 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2479 cfi->device_type, NULL);
2480 map_write(map, CMD(0x40), chip->start + adr);
2481
2482 chip->state = FL_READY;
2483 put_chip(map, chip, adr + chip->start);
2484 ret = 0;
2485
2486out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002487 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002488 return ret;
2489}
2490
2491static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2492 unsigned long adr, int len, void *thunk)
2493{
2494 struct cfi_private *cfi = map->fldrv_priv;
2495 int ret;
2496
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002497 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002498 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2499 if (ret)
2500 goto out_unlock;
2501 chip->state = FL_UNLOCKING;
2502
Brian Norris0a32a102011-07-19 10:06:10 -07002503 pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002504
2505 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2506 cfi->device_type, NULL);
2507 map_write(map, CMD(0x70), adr);
2508
2509 chip->state = FL_READY;
2510 put_chip(map, chip, adr + chip->start);
2511 ret = 0;
2512
2513out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002514 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002515 return ret;
2516}
2517
Adrian Hunter69423d92008-12-10 13:37:21 +00002518static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002519{
2520 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2521}
2522
Adrian Hunter69423d92008-12-10 13:37:21 +00002523static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02002524{
2525 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2526}
2527
Stefan Roese1648eaa2013-01-18 13:10:05 +01002528/*
2529 * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
2530 */
2531
2532struct ppb_lock {
2533 struct flchip *chip;
2534 loff_t offset;
2535 int locked;
2536};
2537
2538#define MAX_SECTORS 512
2539
2540#define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
2541#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
2542#define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
2543
2544static int __maybe_unused do_ppb_xxlock(struct map_info *map,
2545 struct flchip *chip,
2546 unsigned long adr, int len, void *thunk)
2547{
2548 struct cfi_private *cfi = map->fldrv_priv;
2549 unsigned long timeo;
2550 int ret;
2551
2552 mutex_lock(&chip->mutex);
2553 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2554 if (ret) {
2555 mutex_unlock(&chip->mutex);
2556 return ret;
2557 }
2558
2559 pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
2560
2561 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2562 cfi->device_type, NULL);
2563 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2564 cfi->device_type, NULL);
2565 /* PPB entry command */
2566 cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
2567 cfi->device_type, NULL);
2568
2569 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2570 chip->state = FL_LOCKING;
2571 map_write(map, CMD(0xA0), chip->start + adr);
2572 map_write(map, CMD(0x00), chip->start + adr);
2573 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2574 /*
2575 * Unlocking of one specific sector is not supported, so we
2576 * have to unlock all sectors of this device instead
2577 */
2578 chip->state = FL_UNLOCKING;
2579 map_write(map, CMD(0x80), chip->start);
2580 map_write(map, CMD(0x30), chip->start);
2581 } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
2582 chip->state = FL_JEDEC_QUERY;
2583 /* Return locked status: 0->locked, 1->unlocked */
2584 ret = !cfi_read_query(map, adr);
2585 } else
2586 BUG();
2587
2588 /*
2589 * Wait for some time as unlocking of all sectors takes quite long
2590 */
2591 timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
2592 for (;;) {
2593 if (chip_ready(map, adr))
2594 break;
2595
2596 if (time_after(jiffies, timeo)) {
2597 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
2598 ret = -EIO;
2599 break;
2600 }
2601
2602 UDELAY(map, chip, adr, 1);
2603 }
2604
2605 /* Exit BC commands */
2606 map_write(map, CMD(0x90), chip->start);
2607 map_write(map, CMD(0x00), chip->start);
2608
2609 chip->state = FL_READY;
2610 put_chip(map, chip, adr + chip->start);
2611 mutex_unlock(&chip->mutex);
2612
2613 return ret;
2614}
2615
2616static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
2617 uint64_t len)
2618{
2619 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2620 DO_XXLOCK_ONEBLOCK_LOCK);
2621}
2622
2623static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
2624 uint64_t len)
2625{
2626 struct mtd_erase_region_info *regions = mtd->eraseregions;
2627 struct map_info *map = mtd->priv;
2628 struct cfi_private *cfi = map->fldrv_priv;
2629 struct ppb_lock *sect;
2630 unsigned long adr;
2631 loff_t offset;
2632 uint64_t length;
2633 int chipnum;
2634 int i;
2635 int sectors;
2636 int ret;
2637
2638 /*
2639 * PPB unlocking always unlocks all sectors of the flash chip.
2640 * We need to re-lock all previously locked sectors. So lets
2641 * first check the locking status of all sectors and save
2642 * it for future use.
2643 */
2644 sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
2645 if (!sect)
2646 return -ENOMEM;
2647
2648 /*
2649 * This code to walk all sectors is a slightly modified version
2650 * of the cfi_varsize_frob() code.
2651 */
2652 i = 0;
2653 chipnum = 0;
2654 adr = 0;
2655 sectors = 0;
2656 offset = 0;
2657 length = mtd->size;
2658
2659 while (length) {
2660 int size = regions[i].erasesize;
2661
2662 /*
2663 * Only test sectors that shall not be unlocked. The other
2664 * sectors shall be unlocked, so lets keep their locking
2665 * status at "unlocked" (locked=0) for the final re-locking.
2666 */
2667 if ((adr < ofs) || (adr >= (ofs + len))) {
2668 sect[sectors].chip = &cfi->chips[chipnum];
2669 sect[sectors].offset = offset;
2670 sect[sectors].locked = do_ppb_xxlock(
2671 map, &cfi->chips[chipnum], adr, 0,
2672 DO_XXLOCK_ONEBLOCK_GETLOCK);
2673 }
2674
2675 adr += size;
2676 offset += size;
2677 length -= size;
2678
2679 if (offset == regions[i].offset + size * regions[i].numblocks)
2680 i++;
2681
2682 if (adr >> cfi->chipshift) {
2683 adr = 0;
2684 chipnum++;
2685
2686 if (chipnum >= cfi->numchips)
2687 break;
2688 }
2689
2690 sectors++;
2691 if (sectors >= MAX_SECTORS) {
2692 printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
2693 MAX_SECTORS);
2694 kfree(sect);
2695 return -EINVAL;
2696 }
2697 }
2698
2699 /* Now unlock the whole chip */
2700 ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2701 DO_XXLOCK_ONEBLOCK_UNLOCK);
2702 if (ret) {
2703 kfree(sect);
2704 return ret;
2705 }
2706
2707 /*
2708 * PPB unlocking always unlocks all sectors of the flash chip.
2709 * We need to re-lock all previously locked sectors.
2710 */
2711 for (i = 0; i < sectors; i++) {
2712 if (sect[i].locked)
2713 do_ppb_xxlock(map, sect[i].chip, sect[i].offset, 0,
2714 DO_XXLOCK_ONEBLOCK_LOCK);
2715 }
2716
2717 kfree(sect);
2718 return ret;
2719}
2720
2721static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
2722 uint64_t len)
2723{
2724 return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2725 DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
2726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728static void cfi_amdstd_sync (struct mtd_info *mtd)
2729{
2730 struct map_info *map = mtd->priv;
2731 struct cfi_private *cfi = map->fldrv_priv;
2732 int i;
2733 struct flchip *chip;
2734 int ret = 0;
2735 DECLARE_WAITQUEUE(wait, current);
2736
2737 for (i=0; !ret && i<cfi->numchips; i++) {
2738 chip = &cfi->chips[i];
2739
2740 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002741 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 switch(chip->state) {
2744 case FL_READY:
2745 case FL_STATUS:
2746 case FL_CFI_QUERY:
2747 case FL_JEDEC_QUERY:
2748 chip->oldstate = chip->state;
2749 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002750 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 * as the whole point is that nobody can do anything
2752 * with the chip now anyway.
2753 */
2754 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002755 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 break;
2757
2758 default:
2759 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07002760 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002762
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002763 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 schedule();
2766
2767 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 goto retry;
2770 }
2771 }
2772
2773 /* Unlock the chips again */
2774
2775 for (i--; i >=0; i--) {
2776 chip = &cfi->chips[i];
2777
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002778 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (chip->state == FL_SYNCING) {
2781 chip->state = chip->oldstate;
2782 wake_up(&chip->wq);
2783 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002784 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786}
2787
2788
2789static int cfi_amdstd_suspend(struct mtd_info *mtd)
2790{
2791 struct map_info *map = mtd->priv;
2792 struct cfi_private *cfi = map->fldrv_priv;
2793 int i;
2794 struct flchip *chip;
2795 int ret = 0;
2796
2797 for (i=0; !ret && i<cfi->numchips; i++) {
2798 chip = &cfi->chips[i];
2799
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002800 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 switch(chip->state) {
2803 case FL_READY:
2804 case FL_STATUS:
2805 case FL_CFI_QUERY:
2806 case FL_JEDEC_QUERY:
2807 chip->oldstate = chip->state;
2808 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002809 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 * as the whole point is that nobody can do anything
2811 * with the chip now anyway.
2812 */
2813 case FL_PM_SUSPENDED:
2814 break;
2815
2816 default:
2817 ret = -EAGAIN;
2818 break;
2819 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002820 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822
2823 /* Unlock the chips again */
2824
2825 if (ret) {
2826 for (i--; i >=0; i--) {
2827 chip = &cfi->chips[i];
2828
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002829 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (chip->state == FL_PM_SUSPENDED) {
2832 chip->state = chip->oldstate;
2833 wake_up(&chip->wq);
2834 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002835 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return ret;
2840}
2841
2842
2843static void cfi_amdstd_resume(struct mtd_info *mtd)
2844{
2845 struct map_info *map = mtd->priv;
2846 struct cfi_private *cfi = map->fldrv_priv;
2847 int i;
2848 struct flchip *chip;
2849
2850 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 chip = &cfi->chips[i];
2853
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002854 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (chip->state == FL_PM_SUSPENDED) {
2857 chip->state = FL_READY;
2858 map_write(map, CMD(0xF0), chip->start);
2859 wake_up(&chip->wq);
2860 }
2861 else
2862 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2863
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002864 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866}
2867
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002868
2869/*
2870 * Ensure that the flash device is put back into read array mode before
2871 * unloading the driver or rebooting. On some systems, rebooting while
2872 * the flash is in query/program/erase mode will prevent the CPU from
2873 * fetching the bootloader code, requiring a hard reset or power cycle.
2874 */
2875static int cfi_amdstd_reset(struct mtd_info *mtd)
2876{
2877 struct map_info *map = mtd->priv;
2878 struct cfi_private *cfi = map->fldrv_priv;
2879 int i, ret;
2880 struct flchip *chip;
2881
2882 for (i = 0; i < cfi->numchips; i++) {
2883
2884 chip = &cfi->chips[i];
2885
2886 mutex_lock(&chip->mutex);
2887
2888 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2889 if (!ret) {
2890 map_write(map, CMD(0xF0), chip->start);
2891 chip->state = FL_SHUTDOWN;
2892 put_chip(map, chip, chip->start);
2893 }
2894
2895 mutex_unlock(&chip->mutex);
2896 }
2897
2898 return 0;
2899}
2900
2901
2902static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2903 void *v)
2904{
2905 struct mtd_info *mtd;
2906
2907 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2908 cfi_amdstd_reset(mtd);
2909 return NOTIFY_DONE;
2910}
2911
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static void cfi_amdstd_destroy(struct mtd_info *mtd)
2914{
2915 struct map_info *map = mtd->priv;
2916 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002917
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002918 cfi_amdstd_reset(mtd);
2919 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 kfree(cfi->cmdset_priv);
2921 kfree(cfi->cfiq);
2922 kfree(cfi);
2923 kfree(mtd->eraseregions);
2924}
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926MODULE_LICENSE("GPL");
2927MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2928MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002929MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002930MODULE_ALIAS("cfi_cmdset_0701");