blob: 9d68ab919f3101276d2b34bbe060fed7d9743ea8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common Flash Interface support:
3 * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
4 *
5 * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
6 * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
Todd Poynor02b15e32005-06-07 00:04:39 +01007 * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * 2_by_8 routines added by Simon Munton
10 *
11 * 4_by_16 work by Carolyn J. Smith
12 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +000013 * XIP support hooks by Vitaly Wool (based on code for Intel flash
Todd Poynor02b15e32005-06-07 00:04:39 +010014 * by Nicolas Pitre)
Thomas Gleixner1f948b42005-11-07 11:15:37 +000015 *
Christopher Moore87e92c02008-10-17 05:32:22 +020016 * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
19 *
20 * This code is GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/init.h>
28#include <asm/io.h>
29#include <asm/byteorder.h>
30
31#include <linux/errno.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070035#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mtd/map.h>
37#include <linux/mtd/mtd.h>
38#include <linux/mtd/cfi.h>
Todd Poynor02b15e32005-06-07 00:04:39 +010039#include <linux/mtd/xip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define AMD_BOOTLOC_BUG
42#define FORCE_WORD_WRITE 0
43
44#define MAX_WORD_RETRIES 3
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070047#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010048#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020049#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
52static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
55static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
56static void cfi_amdstd_sync (struct mtd_info *);
57static int cfi_amdstd_suspend (struct mtd_info *);
58static void cfi_amdstd_resume (struct mtd_info *);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070059static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
61
62static void cfi_amdstd_destroy(struct mtd_info *);
63
64struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
65static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
66
67static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
68static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
69#include "fwh_lock.h"
70
Adrian Hunter69423d92008-12-10 13:37:21 +000071static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
72static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static struct mtd_chip_driver cfi_amdstd_chipdrv = {
75 .probe = NULL, /* Not usable directly */
76 .destroy = cfi_amdstd_destroy,
77 .name = "cfi_cmdset_0002",
78 .module = THIS_MODULE
79};
80
81
82/* #define DEBUG_CFI_FEATURES */
83
84
85#ifdef DEBUG_CFI_FEATURES
86static void cfi_tell_features(struct cfi_pri_amdstd *extp)
87{
88 const char* erase_suspend[3] = {
89 "Not supported", "Read only", "Read/write"
90 };
91 const char* top_bottom[6] = {
92 "No WP", "8x8KiB sectors at top & bottom, no WP",
93 "Bottom boot", "Top boot",
94 "Uniform, Bottom WP", "Uniform, Top WP"
95 };
96
97 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +000098 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 (extp->SiliconRevision & 1) ? "Not required" : "Required");
100
101 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
102 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
103 else
104 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
105
106 if (extp->BlkProt == 0)
107 printk(" Block protection: Not supported\n");
108 else
109 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
110
111
112 printk(" Temporary block unprotect: %s\n",
113 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
114 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
115 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
116 printk(" Burst mode: %s\n",
117 extp->BurstMode ? "Supported" : "Not supported");
118 if (extp->PageMode == 0)
119 printk(" Page mode: Not supported\n");
120 else
121 printk(" Page mode: %d word page\n", extp->PageMode << 2);
122
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000123 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000125 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 extp->VppMax >> 4, extp->VppMax & 0xf);
127
128 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
129 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
130 else
131 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
132}
133#endif
134
135#ifdef AMD_BOOTLOC_BUG
136/* Wheee. Bring me the head of someone at AMD. */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100137static void fixup_amd_bootblock(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct map_info *map = mtd->priv;
140 struct cfi_private *cfi = map->fldrv_priv;
141 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
142 __u8 major = extp->MajorVersion;
143 __u8 minor = extp->MinorVersion;
144
145 if (((major << 8) | minor) < 0x3131) {
146 /* CFI version 1.0 => don't trust bootloc */
Christopher Moore87e92c02008-10-17 05:32:22 +0200147
148 DEBUG(MTD_DEBUG_LEVEL1,
149 "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
150 map->name, cfi->mfr, cfi->id);
151
152 /* AFAICS all 29LV400 with a bottom boot block have a device ID
153 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
154 * These were badly detected as they have the 0x80 bit set
155 * so treat them as a special case.
156 */
157 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
158
159 /* Macronix added CFI to their 2nd generation
160 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
161 * Fujitsu, Spansion, EON, ESI and older Macronix)
162 * has CFI.
163 *
164 * Therefore also check the manufacturer.
165 * This reduces the risk of false detection due to
166 * the 8-bit device ID.
167 */
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100168 (cfi->mfr == CFI_MFR_MACRONIX)) {
Christopher Moore87e92c02008-10-17 05:32:22 +0200169 DEBUG(MTD_DEBUG_LEVEL1,
170 "%s: Macronix MX29LV400C with bottom boot block"
171 " detected\n", map->name);
172 extp->TopBottom = 2; /* bottom boot */
173 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (cfi->id & 0x80) {
175 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
176 extp->TopBottom = 3; /* top boot */
177 } else {
178 extp->TopBottom = 2; /* bottom boot */
179 }
Christopher Moore87e92c02008-10-17 05:32:22 +0200180
181 DEBUG(MTD_DEBUG_LEVEL1,
182 "%s: AMD CFI PRI V%c.%c has no boot block field;"
183 " deduced %s from Device ID\n", map->name, major, minor,
184 extp->TopBottom == 2 ? "bottom" : "top");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186}
187#endif
188
Guillaume LECERFcc318222010-11-17 12:35:50 +0100189static void fixup_use_write_buffers(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct map_info *map = mtd->priv;
192 struct cfi_private *cfi = map->fldrv_priv;
193 if (cfi->cfiq->BufWriteTimeoutTyp) {
194 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
195 mtd->write = cfi_amdstd_write_buffers;
196 }
197}
198
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200199/* Atmel chips don't use the same PRI format as AMD chips */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100200static void fixup_convert_atmel_pri(struct mtd_info *mtd)
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200201{
202 struct map_info *map = mtd->priv;
203 struct cfi_private *cfi = map->fldrv_priv;
204 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
205 struct cfi_pri_atmel atmel_pri;
206
207 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200208 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200209
210 if (atmel_pri.Features & 0x02)
211 extp->EraseSuspend = 2;
212
Haavard Skinnemoenbe8f78b2008-09-30 13:55:33 +0200213 /* Some chips got it backwards... */
214 if (cfi->id == AT49BV6416) {
215 if (atmel_pri.BottomBoot)
216 extp->TopBottom = 3;
217 else
218 extp->TopBottom = 2;
219 } else {
220 if (atmel_pri.BottomBoot)
221 extp->TopBottom = 2;
222 else
223 extp->TopBottom = 3;
224 }
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100225
226 /* burst write mode not supported */
227 cfi->cfiq->BufWriteTimeoutTyp = 0;
228 cfi->cfiq->BufWriteTimeoutMax = 0;
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200229}
230
Guillaume LECERFcc318222010-11-17 12:35:50 +0100231static void fixup_use_secsi(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /* Setup for chips with a secsi area */
234 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
235 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
236}
237
Guillaume LECERFcc318222010-11-17 12:35:50 +0100238static void fixup_use_erase_chip(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct map_info *map = mtd->priv;
241 struct cfi_private *cfi = map->fldrv_priv;
242 if ((cfi->cfiq->NumEraseRegions == 1) &&
243 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
244 mtd->erase = cfi_amdstd_erase_chip;
245 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200249/*
250 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
251 * locked by default.
252 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100253static void fixup_use_atmel_lock(struct mtd_info *mtd)
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200254{
255 mtd->lock = cfi_atmel_lock;
256 mtd->unlock = cfi_atmel_unlock;
Justin Treone619a752008-01-30 10:25:49 -0800257 mtd->flags |= MTD_POWERUP_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200258}
259
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200260static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
261{
262 struct map_info *map = mtd->priv;
263 struct cfi_private *cfi = map->fldrv_priv;
264
265 /*
266 * These flashes report two seperate eraseblock regions based on the
267 * sector_erase-size and block_erase-size, although they both operate on the
268 * same memory. This is not allowed according to CFI, so we just pick the
269 * sector_erase-size.
270 */
271 cfi->cfiq->NumEraseRegions = 1;
272}
273
Guillaume LECERFcc318222010-11-17 12:35:50 +0100274static void fixup_sst39vf(struct mtd_info *mtd)
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200275{
276 struct map_info *map = mtd->priv;
277 struct cfi_private *cfi = map->fldrv_priv;
278
279 fixup_old_sst_eraseregion(mtd);
280
281 cfi->addr_unlock1 = 0x5555;
282 cfi->addr_unlock2 = 0x2AAA;
283}
284
Guillaume LECERFcc318222010-11-17 12:35:50 +0100285static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200286{
287 struct map_info *map = mtd->priv;
288 struct cfi_private *cfi = map->fldrv_priv;
289
290 fixup_old_sst_eraseregion(mtd);
291
292 cfi->addr_unlock1 = 0x555;
293 cfi->addr_unlock2 = 0x2AA;
Guillaume LECERF08968042010-10-26 10:45:23 +0100294
295 cfi->sector_erase_cmd = CMD(0x50);
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200296}
297
Guillaume LECERFcc318222010-11-17 12:35:50 +0100298static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100299{
300 struct map_info *map = mtd->priv;
301 struct cfi_private *cfi = map->fldrv_priv;
302
Guillaume LECERFcc318222010-11-17 12:35:50 +0100303 fixup_sst39vf_rev_b(mtd);
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100304
305 /*
306 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
307 * it should report a size of 8KBytes (0x0020*256).
308 */
309 cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
310 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
311}
312
Guillaume LECERFcc318222010-11-17 12:35:50 +0100313static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700314{
315 struct map_info *map = mtd->priv;
316 struct cfi_private *cfi = map->fldrv_priv;
317
318 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
319 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
320 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
321 }
322}
323
Guillaume LECERFcc318222010-11-17 12:35:50 +0100324static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
Trent Piepho70b07252008-03-30 21:19:30 -0700325{
326 struct map_info *map = mtd->priv;
327 struct cfi_private *cfi = map->fldrv_priv;
328
329 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
330 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
331 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
332 }
333}
334
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200335/* Used to fix CFI-Tables of chips without Extended Query Tables */
336static struct cfi_fixup cfi_nopri_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100337 { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
338 { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
339 { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
340 { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
341 { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
342 { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
343 { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
344 { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
345 { 0, 0, NULL }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct cfi_fixup cfi_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100349 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#ifdef AMD_BOOTLOC_BUG
Guillaume LECERFcc318222010-11-17 12:35:50 +0100351 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
352 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100354 { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
355 { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
356 { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
357 { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
358 { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
359 { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
360 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
361 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
362 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
363 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
364 { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
365 { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
366 { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
367 { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#if !FORCE_WORD_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100369 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100371 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100374 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
375 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
376 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
377 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380static struct cfi_fixup fixup_table[] = {
381 /* The CFI vendor ids and the JEDEC vendor IDs appear
382 * to be common. It is like the devices id's are as
383 * well. This table is to pick all cases where
384 * we know that is the case.
385 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100386 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
387 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
388 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
390
391
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100392static void cfi_fixup_major_minor(struct cfi_private *cfi,
393 struct cfi_pri_amdstd *extp)
394{
395 if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
396 extp->MajorVersion == '0')
397 extp->MajorVersion = '1';
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100398 /*
399 * SST 38VF640x chips report major=0xFF / minor=0xFF.
400 */
401 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
402 extp->MajorVersion = '1';
403 extp->MinorVersion = '0';
404 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
408{
409 struct cfi_private *cfi = map->fldrv_priv;
410 struct mtd_info *mtd;
411 int i;
412
Burman Yan95b93a02006-11-15 21:10:29 +0200413 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!mtd) {
415 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
416 return NULL;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 mtd->priv = map;
419 mtd->type = MTD_NORFLASH;
420
421 /* Fill in the default mtd operations */
422 mtd->erase = cfi_amdstd_erase_varsize;
423 mtd->write = cfi_amdstd_write_words;
424 mtd->read = cfi_amdstd_read;
425 mtd->sync = cfi_amdstd_sync;
426 mtd->suspend = cfi_amdstd_suspend;
427 mtd->resume = cfi_amdstd_resume;
428 mtd->flags = MTD_CAP_NORFLASH;
429 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400430 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700432 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (cfi->cfi_mode==CFI_MODE_CFI){
435 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
437 struct cfi_pri_amdstd *extp;
438
439 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200440 if (extp) {
441 /*
442 * It's a real CFI chip, not one for which the probe
443 * routine faked a CFI structure.
444 */
445 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200447 /*
Guillaume LECERF5da19532010-08-05 13:55:24 +0200448 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200449 * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
450 * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
Guillaume LECERF5da19532010-08-05 13:55:24 +0200451 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200452 */
Guillaume LECERF564b8492010-04-24 17:58:17 +0200453 if (extp->MajorVersion != '1' ||
Guillaume LECERF5da19532010-08-05 13:55:24 +0200454 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '4'))) {
Guillaume LECERF564b8492010-04-24 17:58:17 +0200455 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200456 "version %c.%c (%#02x/%#02x).\n",
457 extp->MajorVersion, extp->MinorVersion,
458 extp->MajorVersion, extp->MinorVersion);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200459 kfree(extp);
460 kfree(mtd);
461 return NULL;
462 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100463
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200464 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
465 extp->MajorVersion, extp->MinorVersion);
466
Guillaume LECERF564b8492010-04-24 17:58:17 +0200467 /* Install our own private info structure */
468 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100469
Guillaume LECERF564b8492010-04-24 17:58:17 +0200470 /* Apply cfi device specific fixups */
471 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200474 /* Tell the user about it in lots of lovely detail */
475 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Guillaume LECERF564b8492010-04-24 17:58:17 +0200478 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100479 if ((bootloc < 2) || (bootloc > 5)) {
480 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
481 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100482 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200483 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200485
486 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100487 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200488
489 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
490 int j = (cfi->cfiq->NumEraseRegions-1)-i;
491 __u32 swap;
492
493 swap = cfi->cfiq->EraseRegionInfo[i];
494 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
495 cfi->cfiq->EraseRegionInfo[j] = swap;
496 }
497 }
498 /* Set the default CFI lock/unlock addresses */
499 cfi->addr_unlock1 = 0x555;
500 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200502 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200503
504 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
505 kfree(mtd);
506 return NULL;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 } /* CFI mode */
510 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
511 /* Apply jedec specific fixups */
512 cfi_fixup(mtd, jedec_fixup_table);
513 }
514 /* Apply generic fixups */
515 cfi_fixup(mtd, fixup_table);
516
517 for (i=0; i< cfi->numchips; i++) {
518 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
519 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
520 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800521 cfi->chips[i].ref_point_counter = 0;
522 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000523 }
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return cfi_amdstd_setup(mtd);
528}
Guillaume LECERF80461122010-05-20 16:54:10 +0200529struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200530struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100531EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200532EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200533EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
536{
537 struct map_info *map = mtd->priv;
538 struct cfi_private *cfi = map->fldrv_priv;
539 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
540 unsigned long offset = 0;
541 int i,j;
542
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000543 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000545 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 mtd->size = devsize * cfi->numchips;
547
548 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
549 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
550 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000551 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
553 goto setup_err;
554 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
557 unsigned long ernum, ersize;
558 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
559 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (mtd->erasesize < ersize) {
562 mtd->erasesize = ersize;
563 }
564 for (j=0; j<cfi->numchips; j++) {
565 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
566 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
567 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
568 }
569 offset += (ersize * ernum);
570 }
571 if (offset != devsize) {
572 /* Argh */
573 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
574 goto setup_err;
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700578 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return mtd;
580
581 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100582 kfree(mtd->eraseregions);
583 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 kfree(cfi->cmdset_priv);
585 kfree(cfi->cfiq);
586 return NULL;
587}
588
589/*
590 * Return true if the chip is ready.
591 *
592 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
593 * non-suspended sector) and is indicated by no toggle bits toggling.
594 *
595 * Note that anything more complicated than checking if no bits are toggling
596 * (including checking DQ5 for an error status) is tricky to get working
597 * correctly and is therefore not done (particulary with interleaved chips
598 * as each chip must be checked independantly of the others).
599 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100600static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 map_word d, t;
603
604 d = map_read(map, addr);
605 t = map_read(map, addr);
606
607 return map_word_equal(map, d, t);
608}
609
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100610/*
611 * Return true if the chip is ready and has the correct value.
612 *
613 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
614 * non-suspended sector) and it is indicated by no bits toggling.
615 *
616 * Error are indicated by toggling bits or bits held with the wrong value,
617 * or with bits toggling.
618 *
619 * Note that anything more complicated than checking if no bits are toggling
620 * (including checking DQ5 for an error status) is tricky to get working
621 * correctly and is therefore not done (particulary with interleaved chips
622 * as each chip must be checked independantly of the others).
623 *
624 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100625static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100626{
627 map_word oldd, curd;
628
629 oldd = map_read(map, addr);
630 curd = map_read(map, addr);
631
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000632 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100633 map_word_equal(map, curd, expected);
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
637{
638 DECLARE_WAITQUEUE(wait, current);
639 struct cfi_private *cfi = map->fldrv_priv;
640 unsigned long timeo;
641 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
642
643 resettime:
644 timeo = jiffies + HZ;
645 retry:
646 switch (chip->state) {
647
648 case FL_STATUS:
649 for (;;) {
650 if (chip_ready(map, adr))
651 break;
652
653 if (time_after(jiffies, timeo)) {
654 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EIO;
656 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200657 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200659 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* Someone else might have been playing with it. */
661 goto retry;
662 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 case FL_READY:
665 case FL_CFI_QUERY:
666 case FL_JEDEC_QUERY:
667 return 0;
668
669 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100670 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
671 !(mode == FL_READY || mode == FL_POINT ||
672 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto sleep;
674
675 /* We could check to see if we're trying to access the sector
676 * that is currently being erased. However, no user will try
677 * anything like that so we just wait for the timeout. */
678
679 /* Erase suspend */
680 /* It's harmless to issue the Erase-Suspend and Erase-Resume
681 * commands when the erase algorithm isn't in progress. */
682 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
683 chip->oldstate = FL_ERASING;
684 chip->state = FL_ERASE_SUSPENDING;
685 chip->erase_suspended = 1;
686 for (;;) {
687 if (chip_ready(map, adr))
688 break;
689
690 if (time_after(jiffies, timeo)) {
691 /* Should have suspended the erase by now.
692 * Send an Erase-Resume command as either
693 * there was an error (so leave the erase
694 * routine to recover from it) or we trying to
695 * use the erase-in-progress sector. */
Guillaume LECERF08968042010-10-26 10:45:23 +0100696 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 chip->state = FL_ERASING;
698 chip->oldstate = FL_READY;
699 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
700 return -EIO;
701 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000702
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200703 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200705 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
707 So we can just loop here. */
708 }
709 chip->state = FL_READY;
710 return 0;
711
Todd Poynor02b15e32005-06-07 00:04:39 +0100712 case FL_XIP_WHILE_ERASING:
713 if (mode != FL_READY && mode != FL_POINT &&
714 (!cfip || !(cfip->EraseSuspend&2)))
715 goto sleep;
716 chip->oldstate = chip->state;
717 chip->state = FL_READY;
718 return 0;
719
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700720 case FL_SHUTDOWN:
721 /* The machine is rebooting */
722 return -EIO;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 case FL_POINT:
725 /* Only if there's no operation suspended... */
726 if (mode == FL_READY && chip->oldstate == FL_READY)
727 return 0;
728
729 default:
730 sleep:
731 set_current_state(TASK_UNINTERRUPTIBLE);
732 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200733 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 schedule();
735 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200736 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto resettime;
738 }
739}
740
741
742static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
743{
744 struct cfi_private *cfi = map->fldrv_priv;
745
746 switch(chip->oldstate) {
747 case FL_ERASING:
748 chip->state = chip->oldstate;
Guillaume LECERF08968042010-10-26 10:45:23 +0100749 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 chip->oldstate = FL_READY;
751 chip->state = FL_ERASING;
752 break;
753
Todd Poynor02b15e32005-06-07 00:04:39 +0100754 case FL_XIP_WHILE_ERASING:
755 chip->state = chip->oldstate;
756 chip->oldstate = FL_READY;
757 break;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case FL_READY:
760 case FL_STATUS:
761 /* We should really make set_vpp() count, rather than doing this */
762 DISABLE_VPP(map);
763 break;
764 default:
765 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
766 }
767 wake_up(&chip->wq);
768}
769
Todd Poynor02b15e32005-06-07 00:04:39 +0100770#ifdef CONFIG_MTD_XIP
771
772/*
773 * No interrupt what so ever can be serviced while the flash isn't in array
774 * mode. This is ensured by the xip_disable() and xip_enable() functions
775 * enclosing any code path where the flash is known not to be in array mode.
776 * And within a XIP disabled code path, only functions marked with __xipram
777 * may be called and nothing else (it's a good thing to inspect generated
778 * assembly to make sure inline functions were actually inlined and that gcc
779 * didn't emit calls to its own support functions). Also configuring MTD CFI
780 * support to a single buswidth and a single interleave is also recommended.
781 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200782
Todd Poynor02b15e32005-06-07 00:04:39 +0100783static void xip_disable(struct map_info *map, struct flchip *chip,
784 unsigned long adr)
785{
786 /* TODO: chips with no XIP use should ignore and return */
787 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
788 local_irq_disable();
789}
790
791static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
792 unsigned long adr)
793{
794 struct cfi_private *cfi = map->fldrv_priv;
795
796 if (chip->state != FL_POINT && chip->state != FL_READY) {
797 map_write(map, CMD(0xf0), adr);
798 chip->state = FL_READY;
799 }
800 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200801 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100802 local_irq_enable();
803}
804
805/*
806 * When a delay is required for the flash operation to complete, the
807 * xip_udelay() function is polling for both the given timeout and pending
808 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000809 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100810 * and interrupts unmasked. Task scheduling might also happen at that
811 * point. The CPU eventually returns from the interrupt or the call to
812 * schedule() and the suspended flash operation is resumed for the remaining
813 * of the delay period.
814 *
815 * Warning: this function _will_ fool interrupt latency tracing tools.
816 */
817
818static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
819 unsigned long adr, int usec)
820{
821 struct cfi_private *cfi = map->fldrv_priv;
822 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
823 map_word status, OK = CMD(0x80);
824 unsigned long suspended, start = xip_currtime();
825 flstate_t oldstate;
826
827 do {
828 cpu_relax();
829 if (xip_irqpending() && extp &&
830 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
831 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
832 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000833 * Let's suspend the erase operation when supported.
834 * Note that we currently don't try to suspend
835 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100836 * operation suspended (imagine what happens
837 * when one chip was already done with the current
838 * operation while another chip suspended it, then
839 * we resume the whole thing at once). Yes, it
840 * can happen!
841 */
842 map_write(map, CMD(0xb0), adr);
843 usec -= xip_elapsed_since(start);
844 suspended = xip_currtime();
845 do {
846 if (xip_elapsed_since(suspended) > 100000) {
847 /*
848 * The chip doesn't want to suspend
849 * after waiting for 100 msecs.
850 * This is a critical error but there
851 * is not much we can do here.
852 */
853 return;
854 }
855 status = map_read(map, adr);
856 } while (!map_word_andequal(map, status, OK, OK));
857
858 /* Suspend succeeded */
859 oldstate = chip->state;
860 if (!map_word_bitsset(map, status, CMD(0x40)))
861 break;
862 chip->state = FL_XIP_WHILE_ERASING;
863 chip->erase_suspended = 1;
864 map_write(map, CMD(0xf0), adr);
865 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200866 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100867 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200868 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200869 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100870 cond_resched();
871
872 /*
873 * We're back. However someone else might have
874 * decided to go write to the chip if we are in
875 * a suspended erase state. If so let's wait
876 * until it's done.
877 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200878 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100879 while (chip->state != FL_XIP_WHILE_ERASING) {
880 DECLARE_WAITQUEUE(wait, current);
881 set_current_state(TASK_UNINTERRUPTIBLE);
882 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200883 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100884 schedule();
885 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200886 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100887 }
888 /* Disallow XIP again */
889 local_irq_disable();
890
891 /* Resume the write or erase operation */
Guillaume LECERF08968042010-10-26 10:45:23 +0100892 map_write(map, cfi->sector_erase_cmd, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +0100893 chip->state = oldstate;
894 start = xip_currtime();
895 } else if (usec >= 1000000/HZ) {
896 /*
897 * Try to save on CPU power when waiting delay
898 * is at least a system timer tick period.
899 * No need to be extremely accurate here.
900 */
901 xip_cpu_idle();
902 }
903 status = map_read(map, adr);
904 } while (!map_word_andequal(map, status, OK, OK)
905 && xip_elapsed_since(start) < usec);
906}
907
908#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
909
910/*
911 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
912 * the flash is actively programming or erasing since we have to poll for
913 * the operation to complete anyway. We can't do that in a generic way with
914 * a XIP setup so do it before the actual flash operation in this case
915 * and stub it out from INVALIDATE_CACHE_UDELAY.
916 */
917#define XIP_INVAL_CACHED_RANGE(map, from, size) \
918 INVALIDATE_CACHED_RANGE(map, from, size)
919
920#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
921 UDELAY(map, chip, adr, usec)
922
923/*
924 * Extra notes:
925 *
926 * Activating this XIP support changes the way the code works a bit. For
927 * example the code to suspend the current process when concurrent access
928 * happens is never executed because xip_udelay() will always return with the
929 * same chip state as it was entered with. This is why there is no care for
930 * the presence of add_wait_queue() or schedule() calls from within a couple
931 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
932 * The queueing and scheduling are always happening within xip_udelay().
933 *
934 * Similarly, get_chip() and put_chip() just happen to always be executed
935 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
936 * is in array mode, therefore never executing many cases therein and not
937 * causing any problem with XIP.
938 */
939
940#else
941
942#define xip_disable(map, chip, adr)
943#define xip_enable(map, chip, adr)
944#define XIP_INVAL_CACHED_RANGE(x...)
945
946#define UDELAY(map, chip, adr, usec) \
947do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200948 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100949 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200950 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100951} while (0)
952
953#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
954do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200955 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100956 INVALIDATE_CACHED_RANGE(map, adr, len); \
957 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200958 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100959} while (0)
960
961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
964{
965 unsigned long cmd_addr;
966 struct cfi_private *cfi = map->fldrv_priv;
967 int ret;
968
969 adr += chip->start;
970
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000971 /* Ensure cmd read/writes are aligned. */
972 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200974 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ret = get_chip(map, chip, cmd_addr, FL_READY);
976 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200977 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return ret;
979 }
980
981 if (chip->state != FL_POINT && chip->state != FL_READY) {
982 map_write(map, CMD(0xf0), cmd_addr);
983 chip->state = FL_READY;
984 }
985
986 map_copy_from(map, buf, adr, len);
987
988 put_chip(map, chip, cmd_addr);
989
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200990 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return 0;
992}
993
994
995static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
996{
997 struct map_info *map = mtd->priv;
998 struct cfi_private *cfi = map->fldrv_priv;
999 unsigned long ofs;
1000 int chipnum;
1001 int ret = 0;
1002
1003 /* ofs: offset within the first chip that the first read should start */
1004
1005 chipnum = (from >> cfi->chipshift);
1006 ofs = from - (chipnum << cfi->chipshift);
1007
1008
1009 *retlen = 0;
1010
1011 while (len) {
1012 unsigned long thislen;
1013
1014 if (chipnum >= cfi->numchips)
1015 break;
1016
1017 if ((len + ofs -1) >> cfi->chipshift)
1018 thislen = (1<<cfi->chipshift) - ofs;
1019 else
1020 thislen = len;
1021
1022 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1023 if (ret)
1024 break;
1025
1026 *retlen += thislen;
1027 len -= thislen;
1028 buf += thislen;
1029
1030 ofs = 0;
1031 chipnum++;
1032 }
1033 return ret;
1034}
1035
1036
1037static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1038{
1039 DECLARE_WAITQUEUE(wait, current);
1040 unsigned long timeo = jiffies + HZ;
1041 struct cfi_private *cfi = map->fldrv_priv;
1042
1043 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001044 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 set_current_state(TASK_UNINTERRUPTIBLE);
1048 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001049
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001050 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 schedule();
1053 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 timeo = jiffies + HZ;
1055
1056 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 adr += chip->start;
1060
1061 chip->state = FL_READY;
1062
1063 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1064 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1065 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 map_copy_from(map, buf, adr, len);
1068
1069 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1070 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1071 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1072 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001075 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 return 0;
1078}
1079
1080static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1081{
1082 struct map_info *map = mtd->priv;
1083 struct cfi_private *cfi = map->fldrv_priv;
1084 unsigned long ofs;
1085 int chipnum;
1086 int ret = 0;
1087
1088
1089 /* ofs: offset within the first chip that the first read should start */
1090
1091 /* 8 secsi bytes per chip */
1092 chipnum=from>>3;
1093 ofs=from & 7;
1094
1095
1096 *retlen = 0;
1097
1098 while (len) {
1099 unsigned long thislen;
1100
1101 if (chipnum >= cfi->numchips)
1102 break;
1103
1104 if ((len + ofs -1) >> 3)
1105 thislen = (1<<3) - ofs;
1106 else
1107 thislen = len;
1108
1109 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1110 if (ret)
1111 break;
1112
1113 *retlen += thislen;
1114 len -= thislen;
1115 buf += thislen;
1116
1117 ofs = 0;
1118 chipnum++;
1119 }
1120 return ret;
1121}
1122
1123
Todd Poynor02b15e32005-06-07 00:04:39 +01001124static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct cfi_private *cfi = map->fldrv_priv;
1127 unsigned long timeo = jiffies + HZ;
1128 /*
1129 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1130 * have a max write time of a few hundreds usec). However, we should
1131 * use the maximum timeout value given by the chip at probe time
1132 * instead. Unfortunately, struct flchip does have a field for
1133 * maximum timeout, only for typical which can be far too short
1134 * depending of the conditions. The ' + 1' is to avoid having a
1135 * timeout of 0 jiffies if HZ is smaller than 1000.
1136 */
1137 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1138 int ret = 0;
1139 map_word oldd;
1140 int retry_cnt = 0;
1141
1142 adr += chip->start;
1143
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001144 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ret = get_chip(map, chip, adr, FL_WRITING);
1146 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001147 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return ret;
1149 }
1150
1151 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1152 __func__, adr, datum.x[0] );
1153
1154 /*
1155 * Check for a NOP for the case when the datum to write is already
1156 * present - it saves time and works around buggy chips that corrupt
1157 * data at other locations when 0xff is written to a location that
1158 * already contains 0xff.
1159 */
1160 oldd = map_read(map, adr);
1161 if (map_word_equal(map, oldd, datum)) {
1162 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1163 __func__);
1164 goto op_done;
1165 }
1166
Todd Poynor02b15e32005-06-07 00:04:39 +01001167 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001169 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 retry:
1171 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1172 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1173 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1174 map_write(map, datum, adr);
1175 chip->state = FL_WRITING;
1176
Todd Poynor02b15e32005-06-07 00:04:39 +01001177 INVALIDATE_CACHE_UDELAY(map, chip,
1178 adr, map_bankwidth(map),
1179 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001182 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 for (;;) {
1184 if (chip->state != FL_WRITING) {
1185 /* Someone's suspended the write. Sleep */
1186 DECLARE_WAITQUEUE(wait, current);
1187
1188 set_current_state(TASK_UNINTERRUPTIBLE);
1189 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001190 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 schedule();
1192 remove_wait_queue(&chip->wq, &wait);
1193 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001194 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 continue;
1196 }
1197
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001198 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001199 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001200 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001201 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001202 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001205 if (chip_ready(map, adr))
1206 break;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001209 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001211 /* Did we succeed? */
1212 if (!chip_good(map, adr, datum)) {
1213 /* reset on all failures. */
1214 map_write( map, CMD(0xF0), chip->start );
1215 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001217 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001218 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001220 ret = -EIO;
1221 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001222 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 op_done:
1224 chip->state = FL_READY;
1225 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001226 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 return ret;
1229}
1230
1231
1232static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1233 size_t *retlen, const u_char *buf)
1234{
1235 struct map_info *map = mtd->priv;
1236 struct cfi_private *cfi = map->fldrv_priv;
1237 int ret = 0;
1238 int chipnum;
1239 unsigned long ofs, chipstart;
1240 DECLARE_WAITQUEUE(wait, current);
1241
1242 *retlen = 0;
1243 if (!len)
1244 return 0;
1245
1246 chipnum = to >> cfi->chipshift;
1247 ofs = to - (chipnum << cfi->chipshift);
1248 chipstart = cfi->chips[chipnum].start;
1249
1250 /* If it's not bus-aligned, do the first byte write */
1251 if (ofs & (map_bankwidth(map)-1)) {
1252 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1253 int i = ofs - bus_ofs;
1254 int n = 0;
1255 map_word tmp_buf;
1256
1257 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001258 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 set_current_state(TASK_UNINTERRUPTIBLE);
1262 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1263
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001264 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 schedule();
1267 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto retry;
1269 }
1270
1271 /* Load 'tmp_buf' with old contents of flash */
1272 tmp_buf = map_read(map, bus_ofs+chipstart);
1273
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001274 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* Number of bytes to copy from buffer */
1277 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1280
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001281 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001283 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 ofs += n;
1287 buf += n;
1288 (*retlen) += n;
1289 len -= n;
1290
1291 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001292 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ofs = 0;
1294 if (chipnum == cfi->numchips)
1295 return 0;
1296 }
1297 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* We are now aligned, write as much as possible */
1300 while(len >= map_bankwidth(map)) {
1301 map_word datum;
1302
1303 datum = map_word_load(map, buf);
1304
1305 ret = do_write_oneword(map, &cfi->chips[chipnum],
1306 ofs, datum);
1307 if (ret)
1308 return ret;
1309
1310 ofs += map_bankwidth(map);
1311 buf += map_bankwidth(map);
1312 (*retlen) += map_bankwidth(map);
1313 len -= map_bankwidth(map);
1314
1315 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001316 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ofs = 0;
1318 if (chipnum == cfi->numchips)
1319 return 0;
1320 chipstart = cfi->chips[chipnum].start;
1321 }
1322 }
1323
1324 /* Write the trailing bytes if any */
1325 if (len & (map_bankwidth(map)-1)) {
1326 map_word tmp_buf;
1327
1328 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001329 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 set_current_state(TASK_UNINTERRUPTIBLE);
1333 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1334
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001335 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 schedule();
1338 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto retry1;
1340 }
1341
1342 tmp_buf = map_read(map, ofs + chipstart);
1343
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001344 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001347
1348 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001350 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 (*retlen) += len;
1354 }
1355
1356 return 0;
1357}
1358
1359
1360/*
1361 * FIXME: interleaved mode not tested, and probably not supported!
1362 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001363static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001364 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001365 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct cfi_private *cfi = map->fldrv_priv;
1368 unsigned long timeo = jiffies + HZ;
1369 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1370 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1371 int ret = -EIO;
1372 unsigned long cmd_adr;
1373 int z, words;
1374 map_word datum;
1375
1376 adr += chip->start;
1377 cmd_adr = adr;
1378
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001379 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ret = get_chip(map, chip, adr, FL_WRITING);
1381 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001382 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return ret;
1384 }
1385
1386 datum = map_word_load(map, buf);
1387
1388 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1389 __func__, adr, datum.x[0] );
1390
Todd Poynor02b15e32005-06-07 00:04:39 +01001391 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001393 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1396 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /* Write Buffer Load */
1399 map_write(map, CMD(0x25), cmd_adr);
1400
1401 chip->state = FL_WRITING_TO_BUFFER;
1402
1403 /* Write length of data to come */
1404 words = len / map_bankwidth(map);
1405 map_write(map, CMD(words - 1), cmd_adr);
1406 /* Write data */
1407 z = 0;
1408 while(z < words * map_bankwidth(map)) {
1409 datum = map_word_load(map, buf);
1410 map_write(map, datum, adr + z);
1411
1412 z += map_bankwidth(map);
1413 buf += map_bankwidth(map);
1414 }
1415 z -= map_bankwidth(map);
1416
1417 adr += z;
1418
1419 /* Write Buffer Program Confirm: GO GO GO */
1420 map_write(map, CMD(0x29), cmd_adr);
1421 chip->state = FL_WRITING;
1422
Todd Poynor02b15e32005-06-07 00:04:39 +01001423 INVALIDATE_CACHE_UDELAY(map, chip,
1424 adr, map_bankwidth(map),
1425 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001427 timeo = jiffies + uWriteTimeout;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 for (;;) {
1430 if (chip->state != FL_WRITING) {
1431 /* Someone's suspended the write. Sleep */
1432 DECLARE_WAITQUEUE(wait, current);
1433
1434 set_current_state(TASK_UNINTERRUPTIBLE);
1435 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001436 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 schedule();
1438 remove_wait_queue(&chip->wq, &wait);
1439 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001440 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 continue;
1442 }
1443
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001444 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1445 break;
1446
Todd Poynor02b15e32005-06-07 00:04:39 +01001447 if (chip_ready(map, adr)) {
1448 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001453 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /* reset on all failures. */
1457 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001458 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* FIXME - should have reset delay before continuing */
1460
Todd Poynor02b15e32005-06-07 00:04:39 +01001461 printk(KERN_WARNING "MTD %s(): software timeout\n",
1462 __func__ );
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ret = -EIO;
1465 op_done:
1466 chip->state = FL_READY;
1467 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001468 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 return ret;
1471}
1472
1473
1474static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1475 size_t *retlen, const u_char *buf)
1476{
1477 struct map_info *map = mtd->priv;
1478 struct cfi_private *cfi = map->fldrv_priv;
1479 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1480 int ret = 0;
1481 int chipnum;
1482 unsigned long ofs;
1483
1484 *retlen = 0;
1485 if (!len)
1486 return 0;
1487
1488 chipnum = to >> cfi->chipshift;
1489 ofs = to - (chipnum << cfi->chipshift);
1490
1491 /* If it's not bus-aligned, do the first word write */
1492 if (ofs & (map_bankwidth(map)-1)) {
1493 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1494 if (local_len > len)
1495 local_len = len;
1496 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1497 local_len, retlen, buf);
1498 if (ret)
1499 return ret;
1500 ofs += local_len;
1501 buf += local_len;
1502 len -= local_len;
1503
1504 if (ofs >> cfi->chipshift) {
1505 chipnum ++;
1506 ofs = 0;
1507 if (chipnum == cfi->numchips)
1508 return 0;
1509 }
1510 }
1511
1512 /* Write buffer is worth it only if more than one word to write... */
1513 while (len >= map_bankwidth(map) * 2) {
1514 /* We must not cross write block boundaries */
1515 int size = wbufsize - (ofs & (wbufsize-1));
1516
1517 if (size > len)
1518 size = len;
1519 if (size % map_bankwidth(map))
1520 size -= size % map_bankwidth(map);
1521
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001522 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ofs, buf, size);
1524 if (ret)
1525 return ret;
1526
1527 ofs += size;
1528 buf += size;
1529 (*retlen) += size;
1530 len -= size;
1531
1532 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001533 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ofs = 0;
1535 if (chipnum == cfi->numchips)
1536 return 0;
1537 }
1538 }
1539
1540 if (len) {
1541 size_t retlen_dregs = 0;
1542
1543 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1544 len, &retlen_dregs, buf);
1545
1546 *retlen += retlen_dregs;
1547 return ret;
1548 }
1549
1550 return 0;
1551}
1552
1553
1554/*
1555 * Handle devices with one erase region, that only implement
1556 * the chip erase command.
1557 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001558static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 struct cfi_private *cfi = map->fldrv_priv;
1561 unsigned long timeo = jiffies + HZ;
1562 unsigned long int adr;
1563 DECLARE_WAITQUEUE(wait, current);
1564 int ret = 0;
1565
1566 adr = cfi->addr_unlock1;
1567
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001568 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ret = get_chip(map, chip, adr, FL_WRITING);
1570 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001571 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return ret;
1573 }
1574
1575 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1576 __func__, chip->start );
1577
Todd Poynor02b15e32005-06-07 00:04:39 +01001578 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001580 xip_disable(map, chip, adr);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1583 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1584 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1585 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1586 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1587 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1588
1589 chip->state = FL_ERASING;
1590 chip->erase_suspended = 0;
1591 chip->in_progress_block_addr = adr;
1592
Todd Poynor02b15e32005-06-07 00:04:39 +01001593 INVALIDATE_CACHE_UDELAY(map, chip,
1594 adr, map->size,
1595 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 timeo = jiffies + (HZ*20);
1598
1599 for (;;) {
1600 if (chip->state != FL_ERASING) {
1601 /* Someone's suspended the erase. Sleep */
1602 set_current_state(TASK_UNINTERRUPTIBLE);
1603 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001604 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 schedule();
1606 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001607 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 continue;
1609 }
1610 if (chip->erase_suspended) {
1611 /* This erase was suspended and resumed.
1612 Adjust the timeout */
1613 timeo = jiffies + (HZ*20); /* FIXME */
1614 chip->erase_suspended = 0;
1615 }
1616
1617 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 break;
1619
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001620 if (time_after(jiffies, timeo)) {
1621 printk(KERN_WARNING "MTD %s(): software timeout\n",
1622 __func__ );
1623 break;
1624 }
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001627 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001629 /* Did we succeed? */
1630 if (!chip_good(map, adr, map_word_ff(map))) {
1631 /* reset on all failures. */
1632 map_write( map, CMD(0xF0), chip->start );
1633 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001635 ret = -EIO;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001639 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001641 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 return ret;
1644}
1645
1646
Todd Poynor02b15e32005-06-07 00:04:39 +01001647static 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 -07001648{
1649 struct cfi_private *cfi = map->fldrv_priv;
1650 unsigned long timeo = jiffies + HZ;
1651 DECLARE_WAITQUEUE(wait, current);
1652 int ret = 0;
1653
1654 adr += chip->start;
1655
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001656 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ret = get_chip(map, chip, adr, FL_ERASING);
1658 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001659 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return ret;
1661 }
1662
1663 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1664 __func__, adr );
1665
Todd Poynor02b15e32005-06-07 00:04:39 +01001666 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001668 xip_disable(map, chip, adr);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1671 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1672 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1673 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1674 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Guillaume LECERF08968042010-10-26 10:45:23 +01001675 map_write(map, cfi->sector_erase_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 chip->state = FL_ERASING;
1678 chip->erase_suspended = 0;
1679 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001680
1681 INVALIDATE_CACHE_UDELAY(map, chip,
1682 adr, len,
1683 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 timeo = jiffies + (HZ*20);
1686
1687 for (;;) {
1688 if (chip->state != FL_ERASING) {
1689 /* Someone's suspended the erase. Sleep */
1690 set_current_state(TASK_UNINTERRUPTIBLE);
1691 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001692 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 schedule();
1694 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001695 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 continue;
1697 }
1698 if (chip->erase_suspended) {
1699 /* This erase was suspended and resumed.
1700 Adjust the timeout */
1701 timeo = jiffies + (HZ*20); /* FIXME */
1702 chip->erase_suspended = 0;
1703 }
1704
Todd Poynor02b15e32005-06-07 00:04:39 +01001705 if (chip_ready(map, adr)) {
1706 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001710 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001711 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001712 printk(KERN_WARNING "MTD %s(): software timeout\n",
1713 __func__ );
1714 break;
1715 }
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001718 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001720 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001721 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001722 /* reset on all failures. */
1723 map_write( map, CMD(0xF0), chip->start );
1724 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001726 ret = -EIO;
1727 }
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 chip->state = FL_READY;
1730 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001731 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return ret;
1733}
1734
1735
Ben Dooksce0f33a2007-05-28 19:59:00 +01001736static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 unsigned long ofs, len;
1739 int ret;
1740
1741 ofs = instr->addr;
1742 len = instr->len;
1743
1744 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1745 if (ret)
1746 return ret;
1747
1748 instr->state = MTD_ERASE_DONE;
1749 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753
1754
1755static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1756{
1757 struct map_info *map = mtd->priv;
1758 struct cfi_private *cfi = map->fldrv_priv;
1759 int ret = 0;
1760
1761 if (instr->addr != 0)
1762 return -EINVAL;
1763
1764 if (instr->len != mtd->size)
1765 return -EINVAL;
1766
1767 ret = do_erase_chip(map, &cfi->chips[0]);
1768 if (ret)
1769 return ret;
1770
1771 instr->state = MTD_ERASE_DONE;
1772 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775}
1776
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001777static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1778 unsigned long adr, int len, void *thunk)
1779{
1780 struct cfi_private *cfi = map->fldrv_priv;
1781 int ret;
1782
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001783 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001784 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1785 if (ret)
1786 goto out_unlock;
1787 chip->state = FL_LOCKING;
1788
1789 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1790 __func__, adr, len);
1791
1792 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1793 cfi->device_type, NULL);
1794 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1795 cfi->device_type, NULL);
1796 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1797 cfi->device_type, NULL);
1798 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1799 cfi->device_type, NULL);
1800 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1801 cfi->device_type, NULL);
1802 map_write(map, CMD(0x40), chip->start + adr);
1803
1804 chip->state = FL_READY;
1805 put_chip(map, chip, adr + chip->start);
1806 ret = 0;
1807
1808out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001809 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001810 return ret;
1811}
1812
1813static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1814 unsigned long adr, int len, void *thunk)
1815{
1816 struct cfi_private *cfi = map->fldrv_priv;
1817 int ret;
1818
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001819 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001820 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1821 if (ret)
1822 goto out_unlock;
1823 chip->state = FL_UNLOCKING;
1824
1825 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1826 __func__, adr, len);
1827
1828 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1829 cfi->device_type, NULL);
1830 map_write(map, CMD(0x70), adr);
1831
1832 chip->state = FL_READY;
1833 put_chip(map, chip, adr + chip->start);
1834 ret = 0;
1835
1836out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001837 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001838 return ret;
1839}
1840
Adrian Hunter69423d92008-12-10 13:37:21 +00001841static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001842{
1843 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1844}
1845
Adrian Hunter69423d92008-12-10 13:37:21 +00001846static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001847{
1848 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1849}
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852static void cfi_amdstd_sync (struct mtd_info *mtd)
1853{
1854 struct map_info *map = mtd->priv;
1855 struct cfi_private *cfi = map->fldrv_priv;
1856 int i;
1857 struct flchip *chip;
1858 int ret = 0;
1859 DECLARE_WAITQUEUE(wait, current);
1860
1861 for (i=0; !ret && i<cfi->numchips; i++) {
1862 chip = &cfi->chips[i];
1863
1864 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001865 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 switch(chip->state) {
1868 case FL_READY:
1869 case FL_STATUS:
1870 case FL_CFI_QUERY:
1871 case FL_JEDEC_QUERY:
1872 chip->oldstate = chip->state;
1873 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001874 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * as the whole point is that nobody can do anything
1876 * with the chip now anyway.
1877 */
1878 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001879 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break;
1881
1882 default:
1883 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07001884 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001886
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001887 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 schedule();
1890
1891 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto retry;
1894 }
1895 }
1896
1897 /* Unlock the chips again */
1898
1899 for (i--; i >=0; i--) {
1900 chip = &cfi->chips[i];
1901
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001902 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (chip->state == FL_SYNCING) {
1905 chip->state = chip->oldstate;
1906 wake_up(&chip->wq);
1907 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001908 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910}
1911
1912
1913static int cfi_amdstd_suspend(struct mtd_info *mtd)
1914{
1915 struct map_info *map = mtd->priv;
1916 struct cfi_private *cfi = map->fldrv_priv;
1917 int i;
1918 struct flchip *chip;
1919 int ret = 0;
1920
1921 for (i=0; !ret && i<cfi->numchips; i++) {
1922 chip = &cfi->chips[i];
1923
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001924 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 switch(chip->state) {
1927 case FL_READY:
1928 case FL_STATUS:
1929 case FL_CFI_QUERY:
1930 case FL_JEDEC_QUERY:
1931 chip->oldstate = chip->state;
1932 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001933 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * as the whole point is that nobody can do anything
1935 * with the chip now anyway.
1936 */
1937 case FL_PM_SUSPENDED:
1938 break;
1939
1940 default:
1941 ret = -EAGAIN;
1942 break;
1943 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001944 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
1947 /* Unlock the chips again */
1948
1949 if (ret) {
1950 for (i--; i >=0; i--) {
1951 chip = &cfi->chips[i];
1952
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001953 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (chip->state == FL_PM_SUSPENDED) {
1956 chip->state = chip->oldstate;
1957 wake_up(&chip->wq);
1958 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001959 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return ret;
1964}
1965
1966
1967static void cfi_amdstd_resume(struct mtd_info *mtd)
1968{
1969 struct map_info *map = mtd->priv;
1970 struct cfi_private *cfi = map->fldrv_priv;
1971 int i;
1972 struct flchip *chip;
1973
1974 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 chip = &cfi->chips[i];
1977
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001978 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (chip->state == FL_PM_SUSPENDED) {
1981 chip->state = FL_READY;
1982 map_write(map, CMD(0xF0), chip->start);
1983 wake_up(&chip->wq);
1984 }
1985 else
1986 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1987
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001988 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990}
1991
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07001992
1993/*
1994 * Ensure that the flash device is put back into read array mode before
1995 * unloading the driver or rebooting. On some systems, rebooting while
1996 * the flash is in query/program/erase mode will prevent the CPU from
1997 * fetching the bootloader code, requiring a hard reset or power cycle.
1998 */
1999static int cfi_amdstd_reset(struct mtd_info *mtd)
2000{
2001 struct map_info *map = mtd->priv;
2002 struct cfi_private *cfi = map->fldrv_priv;
2003 int i, ret;
2004 struct flchip *chip;
2005
2006 for (i = 0; i < cfi->numchips; i++) {
2007
2008 chip = &cfi->chips[i];
2009
2010 mutex_lock(&chip->mutex);
2011
2012 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2013 if (!ret) {
2014 map_write(map, CMD(0xF0), chip->start);
2015 chip->state = FL_SHUTDOWN;
2016 put_chip(map, chip, chip->start);
2017 }
2018
2019 mutex_unlock(&chip->mutex);
2020 }
2021
2022 return 0;
2023}
2024
2025
2026static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2027 void *v)
2028{
2029 struct mtd_info *mtd;
2030
2031 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2032 cfi_amdstd_reset(mtd);
2033 return NOTIFY_DONE;
2034}
2035
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037static void cfi_amdstd_destroy(struct mtd_info *mtd)
2038{
2039 struct map_info *map = mtd->priv;
2040 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002041
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002042 cfi_amdstd_reset(mtd);
2043 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 kfree(cfi->cmdset_priv);
2045 kfree(cfi->cfiq);
2046 kfree(cfi);
2047 kfree(mtd->eraseregions);
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050MODULE_LICENSE("GPL");
2051MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2052MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002053MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002054MODULE_ALIAS("cfi_cmdset_0701");