blob: eb8ce0cf285575088fba018de81d4ee14f96ab63 [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. */
137static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
138{
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
189static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
190{
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 */
200static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
201{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void fixup_use_secsi(struct mtd_info *mtd, void *param)
232{
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
238static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
239{
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 */
253static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
254{
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
274static void fixup_sst39vf(struct mtd_info *mtd, void *param)
275{
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 LECERF5a0563f2010-04-24 17:58:27 +0200285static void fixup_sst39vf_rev_b(struct mtd_info *mtd, void *param)
286{
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;
294}
295
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100296static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd, void *param)
297{
298 struct map_info *map = mtd->priv;
299 struct cfi_private *cfi = map->fldrv_priv;
300
301 fixup_sst39vf_rev_b(mtd, param);
302
303 /*
304 * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
305 * it should report a size of 8KBytes (0x0020*256).
306 */
307 cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
308 pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
309}
310
Trent Piepho70b07252008-03-30 21:19:30 -0700311static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
312{
313 struct map_info *map = mtd->priv;
314 struct cfi_private *cfi = map->fldrv_priv;
315
316 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
317 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
318 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
319 }
320}
321
322static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
323{
324 struct map_info *map = mtd->priv;
325 struct cfi_private *cfi = map->fldrv_priv;
326
327 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
328 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
329 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
330 }
331}
332
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200333/* Used to fix CFI-Tables of chips without Extended Query Tables */
334static struct cfi_fixup cfi_nopri_fixup_table[] = {
Wolfram Sangd031c222010-10-16 14:56:22 +0200335 { CFI_MFR_SST, 0x234A, fixup_sst39vf, NULL, }, /* SST39VF1602 */
336 { CFI_MFR_SST, 0x234B, fixup_sst39vf, NULL, }, /* SST39VF1601 */
337 { CFI_MFR_SST, 0x235A, fixup_sst39vf, NULL, }, /* SST39VF3202 */
338 { CFI_MFR_SST, 0x235B, fixup_sst39vf, NULL, }, /* SST39VF3201 */
339 { CFI_MFR_SST, 0x235C, fixup_sst39vf_rev_b, NULL, }, /* SST39VF3202B */
340 { CFI_MFR_SST, 0x235D, fixup_sst39vf_rev_b, NULL, }, /* SST39VF3201B */
341 { CFI_MFR_SST, 0x236C, fixup_sst39vf_rev_b, NULL, }, /* SST39VF6402B */
342 { CFI_MFR_SST, 0x236D, fixup_sst39vf_rev_b, NULL, }, /* SST39VF6401B */
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200343 { 0, 0, NULL, NULL }
344};
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100347 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#ifdef AMD_BOOTLOC_BUG
349 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100350 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
352 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
353 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
354 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
355 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
356 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
357 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
Trent Piepho70b07252008-03-30 21:19:30 -0700358 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
359 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
360 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
361 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100362 { CFI_MFR_SST, 0x536A, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6402 */
363 { CFI_MFR_SST, 0x536B, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6401 */
364 { CFI_MFR_SST, 0x536C, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6404 */
365 { CFI_MFR_SST, 0x536D, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#if !FORCE_WORD_WRITE
367 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
368#endif
369 { 0, 0, NULL, NULL }
370};
371static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100372 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
373 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
374 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 { 0, 0, NULL, NULL }
376};
377
378static struct cfi_fixup fixup_table[] = {
379 /* The CFI vendor ids and the JEDEC vendor IDs appear
380 * to be common. It is like the devices id's are as
381 * well. This table is to pick all cases where
382 * we know that is the case.
383 */
384 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200385 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 { 0, 0, NULL, NULL }
387};
388
389
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100390static void cfi_fixup_major_minor(struct cfi_private *cfi,
391 struct cfi_pri_amdstd *extp)
392{
393 if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
394 extp->MajorVersion == '0')
395 extp->MajorVersion = '1';
Guillaume LECERF9fc05fc2010-10-26 11:31:55 +0100396 /*
397 * SST 38VF640x chips report major=0xFF / minor=0xFF.
398 */
399 if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
400 extp->MajorVersion = '1';
401 extp->MinorVersion = '0';
402 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
406{
407 struct cfi_private *cfi = map->fldrv_priv;
408 struct mtd_info *mtd;
409 int i;
410
Burman Yan95b93a02006-11-15 21:10:29 +0200411 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!mtd) {
413 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
414 return NULL;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 mtd->priv = map;
417 mtd->type = MTD_NORFLASH;
418
419 /* Fill in the default mtd operations */
420 mtd->erase = cfi_amdstd_erase_varsize;
421 mtd->write = cfi_amdstd_write_words;
422 mtd->read = cfi_amdstd_read;
423 mtd->sync = cfi_amdstd_sync;
424 mtd->suspend = cfi_amdstd_suspend;
425 mtd->resume = cfi_amdstd_resume;
426 mtd->flags = MTD_CAP_NORFLASH;
427 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400428 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700430 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (cfi->cfi_mode==CFI_MODE_CFI){
433 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
435 struct cfi_pri_amdstd *extp;
436
437 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200438 if (extp) {
439 /*
440 * It's a real CFI chip, not one for which the probe
441 * routine faked a CFI structure.
442 */
443 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200445 /*
Guillaume LECERF5da19532010-08-05 13:55:24 +0200446 * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4
447 * see: http://www.amd.com/us-en/assets/content_type/DownloadableAssets/cfi_r20.pdf, page 19
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200448 * http://www.amd.com/us-en/assets/content_type/DownloadableAssets/cfi_100_20011201.pdf
Guillaume LECERF5da19532010-08-05 13:55:24 +0200449 * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200450 */
Guillaume LECERF564b8492010-04-24 17:58:17 +0200451 if (extp->MajorVersion != '1' ||
Guillaume LECERF5da19532010-08-05 13:55:24 +0200452 (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '4'))) {
Guillaume LECERF564b8492010-04-24 17:58:17 +0200453 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200454 "version %c.%c (%#02x/%#02x).\n",
455 extp->MajorVersion, extp->MinorVersion,
456 extp->MajorVersion, extp->MinorVersion);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200457 kfree(extp);
458 kfree(mtd);
459 return NULL;
460 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100461
Guillaume LECERFe17f47a2010-07-02 14:39:10 +0200462 printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
463 extp->MajorVersion, extp->MinorVersion);
464
Guillaume LECERF564b8492010-04-24 17:58:17 +0200465 /* Install our own private info structure */
466 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100467
Guillaume LECERF564b8492010-04-24 17:58:17 +0200468 /* Apply cfi device specific fixups */
469 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200472 /* Tell the user about it in lots of lovely detail */
473 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000474#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Guillaume LECERF564b8492010-04-24 17:58:17 +0200476 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100477 if ((bootloc < 2) || (bootloc > 5)) {
478 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
479 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100480 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200481 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200483
484 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100485 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200486
487 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
488 int j = (cfi->cfiq->NumEraseRegions-1)-i;
489 __u32 swap;
490
491 swap = cfi->cfiq->EraseRegionInfo[i];
492 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
493 cfi->cfiq->EraseRegionInfo[j] = swap;
494 }
495 }
496 /* Set the default CFI lock/unlock addresses */
497 cfi->addr_unlock1 = 0x555;
498 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200500 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200501
502 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
503 kfree(mtd);
504 return NULL;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 } /* CFI mode */
508 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
509 /* Apply jedec specific fixups */
510 cfi_fixup(mtd, jedec_fixup_table);
511 }
512 /* Apply generic fixups */
513 cfi_fixup(mtd, fixup_table);
514
515 for (i=0; i< cfi->numchips; i++) {
516 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
517 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
518 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800519 cfi->chips[i].ref_point_counter = 0;
520 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return cfi_amdstd_setup(mtd);
526}
Guillaume LECERF80461122010-05-20 16:54:10 +0200527struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse1e804ce2010-05-20 16:54:05 +0200528struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100529EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Guillaume LECERF80461122010-05-20 16:54:10 +0200530EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
David Woodhouse1e804ce2010-05-20 16:54:05 +0200531EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
534{
535 struct map_info *map = mtd->priv;
536 struct cfi_private *cfi = map->fldrv_priv;
537 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
538 unsigned long offset = 0;
539 int i,j;
540
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000541 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000543 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mtd->size = devsize * cfi->numchips;
545
546 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
547 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
548 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000549 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
551 goto setup_err;
552 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
555 unsigned long ernum, ersize;
556 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
557 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (mtd->erasesize < ersize) {
560 mtd->erasesize = ersize;
561 }
562 for (j=0; j<cfi->numchips; j++) {
563 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
564 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
565 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
566 }
567 offset += (ersize * ernum);
568 }
569 if (offset != devsize) {
570 /* Argh */
571 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
572 goto setup_err;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700576 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return mtd;
578
579 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100580 kfree(mtd->eraseregions);
581 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(cfi->cmdset_priv);
583 kfree(cfi->cfiq);
584 return NULL;
585}
586
587/*
588 * Return true if the chip is ready.
589 *
590 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
591 * non-suspended sector) and is indicated by no toggle bits toggling.
592 *
593 * Note that anything more complicated than checking if no bits are toggling
594 * (including checking DQ5 for an error status) is tricky to get working
595 * correctly and is therefore not done (particulary with interleaved chips
596 * as each chip must be checked independantly of the others).
597 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100598static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 map_word d, t;
601
602 d = map_read(map, addr);
603 t = map_read(map, addr);
604
605 return map_word_equal(map, d, t);
606}
607
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100608/*
609 * Return true if the chip is ready and has the correct value.
610 *
611 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
612 * non-suspended sector) and it is indicated by no bits toggling.
613 *
614 * Error are indicated by toggling bits or bits held with the wrong value,
615 * or with bits toggling.
616 *
617 * Note that anything more complicated than checking if no bits are toggling
618 * (including checking DQ5 for an error status) is tricky to get working
619 * correctly and is therefore not done (particulary with interleaved chips
620 * as each chip must be checked independantly of the others).
621 *
622 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100623static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100624{
625 map_word oldd, curd;
626
627 oldd = map_read(map, addr);
628 curd = map_read(map, addr);
629
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000630 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100631 map_word_equal(map, curd, expected);
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
635{
636 DECLARE_WAITQUEUE(wait, current);
637 struct cfi_private *cfi = map->fldrv_priv;
638 unsigned long timeo;
639 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
640
641 resettime:
642 timeo = jiffies + HZ;
643 retry:
644 switch (chip->state) {
645
646 case FL_STATUS:
647 for (;;) {
648 if (chip_ready(map, adr))
649 break;
650
651 if (time_after(jiffies, timeo)) {
652 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EIO;
654 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200655 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200657 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* Someone else might have been playing with it. */
659 goto retry;
660 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 case FL_READY:
663 case FL_CFI_QUERY:
664 case FL_JEDEC_QUERY:
665 return 0;
666
667 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100668 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
669 !(mode == FL_READY || mode == FL_POINT ||
670 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto sleep;
672
673 /* We could check to see if we're trying to access the sector
674 * that is currently being erased. However, no user will try
675 * anything like that so we just wait for the timeout. */
676
677 /* Erase suspend */
678 /* It's harmless to issue the Erase-Suspend and Erase-Resume
679 * commands when the erase algorithm isn't in progress. */
680 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
681 chip->oldstate = FL_ERASING;
682 chip->state = FL_ERASE_SUSPENDING;
683 chip->erase_suspended = 1;
684 for (;;) {
685 if (chip_ready(map, adr))
686 break;
687
688 if (time_after(jiffies, timeo)) {
689 /* Should have suspended the erase by now.
690 * Send an Erase-Resume command as either
691 * there was an error (so leave the erase
692 * routine to recover from it) or we trying to
693 * use the erase-in-progress sector. */
694 map_write(map, CMD(0x30), chip->in_progress_block_addr);
695 chip->state = FL_ERASING;
696 chip->oldstate = FL_READY;
697 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
698 return -EIO;
699 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000700
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200701 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200703 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
705 So we can just loop here. */
706 }
707 chip->state = FL_READY;
708 return 0;
709
Todd Poynor02b15e32005-06-07 00:04:39 +0100710 case FL_XIP_WHILE_ERASING:
711 if (mode != FL_READY && mode != FL_POINT &&
712 (!cfip || !(cfip->EraseSuspend&2)))
713 goto sleep;
714 chip->oldstate = chip->state;
715 chip->state = FL_READY;
716 return 0;
717
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700718 case FL_SHUTDOWN:
719 /* The machine is rebooting */
720 return -EIO;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 case FL_POINT:
723 /* Only if there's no operation suspended... */
724 if (mode == FL_READY && chip->oldstate == FL_READY)
725 return 0;
726
727 default:
728 sleep:
729 set_current_state(TASK_UNINTERRUPTIBLE);
730 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200731 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 schedule();
733 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200734 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto resettime;
736 }
737}
738
739
740static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
741{
742 struct cfi_private *cfi = map->fldrv_priv;
743
744 switch(chip->oldstate) {
745 case FL_ERASING:
746 chip->state = chip->oldstate;
747 map_write(map, CMD(0x30), chip->in_progress_block_addr);
748 chip->oldstate = FL_READY;
749 chip->state = FL_ERASING;
750 break;
751
Todd Poynor02b15e32005-06-07 00:04:39 +0100752 case FL_XIP_WHILE_ERASING:
753 chip->state = chip->oldstate;
754 chip->oldstate = FL_READY;
755 break;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case FL_READY:
758 case FL_STATUS:
759 /* We should really make set_vpp() count, rather than doing this */
760 DISABLE_VPP(map);
761 break;
762 default:
763 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
764 }
765 wake_up(&chip->wq);
766}
767
Todd Poynor02b15e32005-06-07 00:04:39 +0100768#ifdef CONFIG_MTD_XIP
769
770/*
771 * No interrupt what so ever can be serviced while the flash isn't in array
772 * mode. This is ensured by the xip_disable() and xip_enable() functions
773 * enclosing any code path where the flash is known not to be in array mode.
774 * And within a XIP disabled code path, only functions marked with __xipram
775 * may be called and nothing else (it's a good thing to inspect generated
776 * assembly to make sure inline functions were actually inlined and that gcc
777 * didn't emit calls to its own support functions). Also configuring MTD CFI
778 * support to a single buswidth and a single interleave is also recommended.
779 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200780
Todd Poynor02b15e32005-06-07 00:04:39 +0100781static void xip_disable(struct map_info *map, struct flchip *chip,
782 unsigned long adr)
783{
784 /* TODO: chips with no XIP use should ignore and return */
785 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
786 local_irq_disable();
787}
788
789static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
790 unsigned long adr)
791{
792 struct cfi_private *cfi = map->fldrv_priv;
793
794 if (chip->state != FL_POINT && chip->state != FL_READY) {
795 map_write(map, CMD(0xf0), adr);
796 chip->state = FL_READY;
797 }
798 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200799 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100800 local_irq_enable();
801}
802
803/*
804 * When a delay is required for the flash operation to complete, the
805 * xip_udelay() function is polling for both the given timeout and pending
806 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000807 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100808 * and interrupts unmasked. Task scheduling might also happen at that
809 * point. The CPU eventually returns from the interrupt or the call to
810 * schedule() and the suspended flash operation is resumed for the remaining
811 * of the delay period.
812 *
813 * Warning: this function _will_ fool interrupt latency tracing tools.
814 */
815
816static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
817 unsigned long adr, int usec)
818{
819 struct cfi_private *cfi = map->fldrv_priv;
820 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
821 map_word status, OK = CMD(0x80);
822 unsigned long suspended, start = xip_currtime();
823 flstate_t oldstate;
824
825 do {
826 cpu_relax();
827 if (xip_irqpending() && extp &&
828 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
829 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
830 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000831 * Let's suspend the erase operation when supported.
832 * Note that we currently don't try to suspend
833 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100834 * operation suspended (imagine what happens
835 * when one chip was already done with the current
836 * operation while another chip suspended it, then
837 * we resume the whole thing at once). Yes, it
838 * can happen!
839 */
840 map_write(map, CMD(0xb0), adr);
841 usec -= xip_elapsed_since(start);
842 suspended = xip_currtime();
843 do {
844 if (xip_elapsed_since(suspended) > 100000) {
845 /*
846 * The chip doesn't want to suspend
847 * after waiting for 100 msecs.
848 * This is a critical error but there
849 * is not much we can do here.
850 */
851 return;
852 }
853 status = map_read(map, adr);
854 } while (!map_word_andequal(map, status, OK, OK));
855
856 /* Suspend succeeded */
857 oldstate = chip->state;
858 if (!map_word_bitsset(map, status, CMD(0x40)))
859 break;
860 chip->state = FL_XIP_WHILE_ERASING;
861 chip->erase_suspended = 1;
862 map_write(map, CMD(0xf0), adr);
863 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200864 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100865 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200866 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200867 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100868 cond_resched();
869
870 /*
871 * We're back. However someone else might have
872 * decided to go write to the chip if we are in
873 * a suspended erase state. If so let's wait
874 * until it's done.
875 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200876 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100877 while (chip->state != FL_XIP_WHILE_ERASING) {
878 DECLARE_WAITQUEUE(wait, current);
879 set_current_state(TASK_UNINTERRUPTIBLE);
880 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200881 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100882 schedule();
883 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200884 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100885 }
886 /* Disallow XIP again */
887 local_irq_disable();
888
889 /* Resume the write or erase operation */
890 map_write(map, CMD(0x30), adr);
891 chip->state = oldstate;
892 start = xip_currtime();
893 } else if (usec >= 1000000/HZ) {
894 /*
895 * Try to save on CPU power when waiting delay
896 * is at least a system timer tick period.
897 * No need to be extremely accurate here.
898 */
899 xip_cpu_idle();
900 }
901 status = map_read(map, adr);
902 } while (!map_word_andequal(map, status, OK, OK)
903 && xip_elapsed_since(start) < usec);
904}
905
906#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
907
908/*
909 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
910 * the flash is actively programming or erasing since we have to poll for
911 * the operation to complete anyway. We can't do that in a generic way with
912 * a XIP setup so do it before the actual flash operation in this case
913 * and stub it out from INVALIDATE_CACHE_UDELAY.
914 */
915#define XIP_INVAL_CACHED_RANGE(map, from, size) \
916 INVALIDATE_CACHED_RANGE(map, from, size)
917
918#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
919 UDELAY(map, chip, adr, usec)
920
921/*
922 * Extra notes:
923 *
924 * Activating this XIP support changes the way the code works a bit. For
925 * example the code to suspend the current process when concurrent access
926 * happens is never executed because xip_udelay() will always return with the
927 * same chip state as it was entered with. This is why there is no care for
928 * the presence of add_wait_queue() or schedule() calls from within a couple
929 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
930 * The queueing and scheduling are always happening within xip_udelay().
931 *
932 * Similarly, get_chip() and put_chip() just happen to always be executed
933 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
934 * is in array mode, therefore never executing many cases therein and not
935 * causing any problem with XIP.
936 */
937
938#else
939
940#define xip_disable(map, chip, adr)
941#define xip_enable(map, chip, adr)
942#define XIP_INVAL_CACHED_RANGE(x...)
943
944#define UDELAY(map, chip, adr, usec) \
945do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200946 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100947 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200948 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100949} while (0)
950
951#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
952do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200953 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100954 INVALIDATE_CACHED_RANGE(map, adr, len); \
955 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200956 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100957} while (0)
958
959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
962{
963 unsigned long cmd_addr;
964 struct cfi_private *cfi = map->fldrv_priv;
965 int ret;
966
967 adr += chip->start;
968
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000969 /* Ensure cmd read/writes are aligned. */
970 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200972 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ret = get_chip(map, chip, cmd_addr, FL_READY);
974 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200975 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return ret;
977 }
978
979 if (chip->state != FL_POINT && chip->state != FL_READY) {
980 map_write(map, CMD(0xf0), cmd_addr);
981 chip->state = FL_READY;
982 }
983
984 map_copy_from(map, buf, adr, len);
985
986 put_chip(map, chip, cmd_addr);
987
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200988 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
990}
991
992
993static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
994{
995 struct map_info *map = mtd->priv;
996 struct cfi_private *cfi = map->fldrv_priv;
997 unsigned long ofs;
998 int chipnum;
999 int ret = 0;
1000
1001 /* ofs: offset within the first chip that the first read should start */
1002
1003 chipnum = (from >> cfi->chipshift);
1004 ofs = from - (chipnum << cfi->chipshift);
1005
1006
1007 *retlen = 0;
1008
1009 while (len) {
1010 unsigned long thislen;
1011
1012 if (chipnum >= cfi->numchips)
1013 break;
1014
1015 if ((len + ofs -1) >> cfi->chipshift)
1016 thislen = (1<<cfi->chipshift) - ofs;
1017 else
1018 thislen = len;
1019
1020 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1021 if (ret)
1022 break;
1023
1024 *retlen += thislen;
1025 len -= thislen;
1026 buf += thislen;
1027
1028 ofs = 0;
1029 chipnum++;
1030 }
1031 return ret;
1032}
1033
1034
1035static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1036{
1037 DECLARE_WAITQUEUE(wait, current);
1038 unsigned long timeo = jiffies + HZ;
1039 struct cfi_private *cfi = map->fldrv_priv;
1040
1041 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001042 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (chip->state != FL_READY){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 set_current_state(TASK_UNINTERRUPTIBLE);
1046 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001047
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001048 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 schedule();
1051 remove_wait_queue(&chip->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 timeo = jiffies + HZ;
1053
1054 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 adr += chip->start;
1058
1059 chip->state = FL_READY;
1060
1061 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1062 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1063 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 map_copy_from(map, buf, adr, len);
1066
1067 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1068 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1069 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1070 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001073 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 return 0;
1076}
1077
1078static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1079{
1080 struct map_info *map = mtd->priv;
1081 struct cfi_private *cfi = map->fldrv_priv;
1082 unsigned long ofs;
1083 int chipnum;
1084 int ret = 0;
1085
1086
1087 /* ofs: offset within the first chip that the first read should start */
1088
1089 /* 8 secsi bytes per chip */
1090 chipnum=from>>3;
1091 ofs=from & 7;
1092
1093
1094 *retlen = 0;
1095
1096 while (len) {
1097 unsigned long thislen;
1098
1099 if (chipnum >= cfi->numchips)
1100 break;
1101
1102 if ((len + ofs -1) >> 3)
1103 thislen = (1<<3) - ofs;
1104 else
1105 thislen = len;
1106
1107 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1108 if (ret)
1109 break;
1110
1111 *retlen += thislen;
1112 len -= thislen;
1113 buf += thislen;
1114
1115 ofs = 0;
1116 chipnum++;
1117 }
1118 return ret;
1119}
1120
1121
Todd Poynor02b15e32005-06-07 00:04:39 +01001122static 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 -07001123{
1124 struct cfi_private *cfi = map->fldrv_priv;
1125 unsigned long timeo = jiffies + HZ;
1126 /*
1127 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1128 * have a max write time of a few hundreds usec). However, we should
1129 * use the maximum timeout value given by the chip at probe time
1130 * instead. Unfortunately, struct flchip does have a field for
1131 * maximum timeout, only for typical which can be far too short
1132 * depending of the conditions. The ' + 1' is to avoid having a
1133 * timeout of 0 jiffies if HZ is smaller than 1000.
1134 */
1135 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1136 int ret = 0;
1137 map_word oldd;
1138 int retry_cnt = 0;
1139
1140 adr += chip->start;
1141
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001142 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ret = get_chip(map, chip, adr, FL_WRITING);
1144 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001145 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return ret;
1147 }
1148
1149 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1150 __func__, adr, datum.x[0] );
1151
1152 /*
1153 * Check for a NOP for the case when the datum to write is already
1154 * present - it saves time and works around buggy chips that corrupt
1155 * data at other locations when 0xff is written to a location that
1156 * already contains 0xff.
1157 */
1158 oldd = map_read(map, adr);
1159 if (map_word_equal(map, oldd, datum)) {
1160 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1161 __func__);
1162 goto op_done;
1163 }
1164
Todd Poynor02b15e32005-06-07 00:04:39 +01001165 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001167 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 retry:
1169 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1170 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1171 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1172 map_write(map, datum, adr);
1173 chip->state = FL_WRITING;
1174
Todd Poynor02b15e32005-06-07 00:04:39 +01001175 INVALIDATE_CACHE_UDELAY(map, chip,
1176 adr, map_bankwidth(map),
1177 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001180 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 for (;;) {
1182 if (chip->state != FL_WRITING) {
1183 /* Someone's suspended the write. Sleep */
1184 DECLARE_WAITQUEUE(wait, current);
1185
1186 set_current_state(TASK_UNINTERRUPTIBLE);
1187 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001188 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 schedule();
1190 remove_wait_queue(&chip->wq, &wait);
1191 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001192 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 continue;
1194 }
1195
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001196 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001197 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001198 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001199 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001200 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001203 if (chip_ready(map, adr))
1204 break;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001207 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001209 /* Did we succeed? */
1210 if (!chip_good(map, adr, datum)) {
1211 /* reset on all failures. */
1212 map_write( map, CMD(0xF0), chip->start );
1213 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001215 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001216 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001218 ret = -EIO;
1219 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001220 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 op_done:
1222 chip->state = FL_READY;
1223 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001224 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return ret;
1227}
1228
1229
1230static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1231 size_t *retlen, const u_char *buf)
1232{
1233 struct map_info *map = mtd->priv;
1234 struct cfi_private *cfi = map->fldrv_priv;
1235 int ret = 0;
1236 int chipnum;
1237 unsigned long ofs, chipstart;
1238 DECLARE_WAITQUEUE(wait, current);
1239
1240 *retlen = 0;
1241 if (!len)
1242 return 0;
1243
1244 chipnum = to >> cfi->chipshift;
1245 ofs = to - (chipnum << cfi->chipshift);
1246 chipstart = cfi->chips[chipnum].start;
1247
1248 /* If it's not bus-aligned, do the first byte write */
1249 if (ofs & (map_bankwidth(map)-1)) {
1250 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1251 int i = ofs - bus_ofs;
1252 int n = 0;
1253 map_word tmp_buf;
1254
1255 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001256 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 set_current_state(TASK_UNINTERRUPTIBLE);
1260 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1261
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001262 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 schedule();
1265 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto retry;
1267 }
1268
1269 /* Load 'tmp_buf' with old contents of flash */
1270 tmp_buf = map_read(map, bus_ofs+chipstart);
1271
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001272 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* Number of bytes to copy from buffer */
1275 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1278
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001279 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001281 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 ofs += n;
1285 buf += n;
1286 (*retlen) += n;
1287 len -= n;
1288
1289 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001290 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 ofs = 0;
1292 if (chipnum == cfi->numchips)
1293 return 0;
1294 }
1295 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* We are now aligned, write as much as possible */
1298 while(len >= map_bankwidth(map)) {
1299 map_word datum;
1300
1301 datum = map_word_load(map, buf);
1302
1303 ret = do_write_oneword(map, &cfi->chips[chipnum],
1304 ofs, datum);
1305 if (ret)
1306 return ret;
1307
1308 ofs += map_bankwidth(map);
1309 buf += map_bankwidth(map);
1310 (*retlen) += map_bankwidth(map);
1311 len -= map_bankwidth(map);
1312
1313 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001314 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ofs = 0;
1316 if (chipnum == cfi->numchips)
1317 return 0;
1318 chipstart = cfi->chips[chipnum].start;
1319 }
1320 }
1321
1322 /* Write the trailing bytes if any */
1323 if (len & (map_bankwidth(map)-1)) {
1324 map_word tmp_buf;
1325
1326 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001327 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (cfi->chips[chipnum].state != FL_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 set_current_state(TASK_UNINTERRUPTIBLE);
1331 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1332
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001333 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 schedule();
1336 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto retry1;
1338 }
1339
1340 tmp_buf = map_read(map, ofs + chipstart);
1341
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001342 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001345
1346 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001348 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 (*retlen) += len;
1352 }
1353
1354 return 0;
1355}
1356
1357
1358/*
1359 * FIXME: interleaved mode not tested, and probably not supported!
1360 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001361static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001362 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001363 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct cfi_private *cfi = map->fldrv_priv;
1366 unsigned long timeo = jiffies + HZ;
1367 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1368 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1369 int ret = -EIO;
1370 unsigned long cmd_adr;
1371 int z, words;
1372 map_word datum;
1373
1374 adr += chip->start;
1375 cmd_adr = adr;
1376
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001377 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ret = get_chip(map, chip, adr, FL_WRITING);
1379 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001380 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return ret;
1382 }
1383
1384 datum = map_word_load(map, buf);
1385
1386 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1387 __func__, adr, datum.x[0] );
1388
Todd Poynor02b15e32005-06-07 00:04:39 +01001389 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001391 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1394 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* Write Buffer Load */
1397 map_write(map, CMD(0x25), cmd_adr);
1398
1399 chip->state = FL_WRITING_TO_BUFFER;
1400
1401 /* Write length of data to come */
1402 words = len / map_bankwidth(map);
1403 map_write(map, CMD(words - 1), cmd_adr);
1404 /* Write data */
1405 z = 0;
1406 while(z < words * map_bankwidth(map)) {
1407 datum = map_word_load(map, buf);
1408 map_write(map, datum, adr + z);
1409
1410 z += map_bankwidth(map);
1411 buf += map_bankwidth(map);
1412 }
1413 z -= map_bankwidth(map);
1414
1415 adr += z;
1416
1417 /* Write Buffer Program Confirm: GO GO GO */
1418 map_write(map, CMD(0x29), cmd_adr);
1419 chip->state = FL_WRITING;
1420
Todd Poynor02b15e32005-06-07 00:04:39 +01001421 INVALIDATE_CACHE_UDELAY(map, chip,
1422 adr, map_bankwidth(map),
1423 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001425 timeo = jiffies + uWriteTimeout;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 for (;;) {
1428 if (chip->state != FL_WRITING) {
1429 /* Someone's suspended the write. Sleep */
1430 DECLARE_WAITQUEUE(wait, current);
1431
1432 set_current_state(TASK_UNINTERRUPTIBLE);
1433 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001434 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 schedule();
1436 remove_wait_queue(&chip->wq, &wait);
1437 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001438 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 continue;
1440 }
1441
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001442 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1443 break;
1444
Todd Poynor02b15e32005-06-07 00:04:39 +01001445 if (chip_ready(map, adr)) {
1446 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001451 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* reset on all failures. */
1455 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001456 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* FIXME - should have reset delay before continuing */
1458
Todd Poynor02b15e32005-06-07 00:04:39 +01001459 printk(KERN_WARNING "MTD %s(): software timeout\n",
1460 __func__ );
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ret = -EIO;
1463 op_done:
1464 chip->state = FL_READY;
1465 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001466 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 return ret;
1469}
1470
1471
1472static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1473 size_t *retlen, const u_char *buf)
1474{
1475 struct map_info *map = mtd->priv;
1476 struct cfi_private *cfi = map->fldrv_priv;
1477 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1478 int ret = 0;
1479 int chipnum;
1480 unsigned long ofs;
1481
1482 *retlen = 0;
1483 if (!len)
1484 return 0;
1485
1486 chipnum = to >> cfi->chipshift;
1487 ofs = to - (chipnum << cfi->chipshift);
1488
1489 /* If it's not bus-aligned, do the first word write */
1490 if (ofs & (map_bankwidth(map)-1)) {
1491 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1492 if (local_len > len)
1493 local_len = len;
1494 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1495 local_len, retlen, buf);
1496 if (ret)
1497 return ret;
1498 ofs += local_len;
1499 buf += local_len;
1500 len -= local_len;
1501
1502 if (ofs >> cfi->chipshift) {
1503 chipnum ++;
1504 ofs = 0;
1505 if (chipnum == cfi->numchips)
1506 return 0;
1507 }
1508 }
1509
1510 /* Write buffer is worth it only if more than one word to write... */
1511 while (len >= map_bankwidth(map) * 2) {
1512 /* We must not cross write block boundaries */
1513 int size = wbufsize - (ofs & (wbufsize-1));
1514
1515 if (size > len)
1516 size = len;
1517 if (size % map_bankwidth(map))
1518 size -= size % map_bankwidth(map);
1519
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001520 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ofs, buf, size);
1522 if (ret)
1523 return ret;
1524
1525 ofs += size;
1526 buf += size;
1527 (*retlen) += size;
1528 len -= size;
1529
1530 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001531 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ofs = 0;
1533 if (chipnum == cfi->numchips)
1534 return 0;
1535 }
1536 }
1537
1538 if (len) {
1539 size_t retlen_dregs = 0;
1540
1541 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1542 len, &retlen_dregs, buf);
1543
1544 *retlen += retlen_dregs;
1545 return ret;
1546 }
1547
1548 return 0;
1549}
1550
1551
1552/*
1553 * Handle devices with one erase region, that only implement
1554 * the chip erase command.
1555 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001556static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct cfi_private *cfi = map->fldrv_priv;
1559 unsigned long timeo = jiffies + HZ;
1560 unsigned long int adr;
1561 DECLARE_WAITQUEUE(wait, current);
1562 int ret = 0;
1563
1564 adr = cfi->addr_unlock1;
1565
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001566 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ret = get_chip(map, chip, adr, FL_WRITING);
1568 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001569 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return ret;
1571 }
1572
1573 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1574 __func__, chip->start );
1575
Todd Poynor02b15e32005-06-07 00:04:39 +01001576 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001578 xip_disable(map, chip, adr);
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1581 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1582 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1583 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1584 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1585 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1586
1587 chip->state = FL_ERASING;
1588 chip->erase_suspended = 0;
1589 chip->in_progress_block_addr = adr;
1590
Todd Poynor02b15e32005-06-07 00:04:39 +01001591 INVALIDATE_CACHE_UDELAY(map, chip,
1592 adr, map->size,
1593 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 timeo = jiffies + (HZ*20);
1596
1597 for (;;) {
1598 if (chip->state != FL_ERASING) {
1599 /* Someone's suspended the erase. Sleep */
1600 set_current_state(TASK_UNINTERRUPTIBLE);
1601 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001602 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 schedule();
1604 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001605 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 continue;
1607 }
1608 if (chip->erase_suspended) {
1609 /* This erase was suspended and resumed.
1610 Adjust the timeout */
1611 timeo = jiffies + (HZ*20); /* FIXME */
1612 chip->erase_suspended = 0;
1613 }
1614
1615 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001618 if (time_after(jiffies, timeo)) {
1619 printk(KERN_WARNING "MTD %s(): software timeout\n",
1620 __func__ );
1621 break;
1622 }
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001625 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001627 /* Did we succeed? */
1628 if (!chip_good(map, adr, map_word_ff(map))) {
1629 /* reset on all failures. */
1630 map_write( map, CMD(0xF0), chip->start );
1631 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001633 ret = -EIO;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001637 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001639 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 return ret;
1642}
1643
1644
Todd Poynor02b15e32005-06-07 00:04:39 +01001645static 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 -07001646{
1647 struct cfi_private *cfi = map->fldrv_priv;
1648 unsigned long timeo = jiffies + HZ;
1649 DECLARE_WAITQUEUE(wait, current);
1650 int ret = 0;
1651
1652 adr += chip->start;
1653
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001654 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ret = get_chip(map, chip, adr, FL_ERASING);
1656 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001657 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return ret;
1659 }
1660
1661 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1662 __func__, adr );
1663
Todd Poynor02b15e32005-06-07 00:04:39 +01001664 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001666 xip_disable(map, chip, adr);
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1669 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1670 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1671 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1672 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1673 map_write(map, CMD(0x30), adr);
1674
1675 chip->state = FL_ERASING;
1676 chip->erase_suspended = 0;
1677 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001678
1679 INVALIDATE_CACHE_UDELAY(map, chip,
1680 adr, len,
1681 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 timeo = jiffies + (HZ*20);
1684
1685 for (;;) {
1686 if (chip->state != FL_ERASING) {
1687 /* Someone's suspended the erase. Sleep */
1688 set_current_state(TASK_UNINTERRUPTIBLE);
1689 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001690 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 schedule();
1692 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001693 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 continue;
1695 }
1696 if (chip->erase_suspended) {
1697 /* This erase was suspended and resumed.
1698 Adjust the timeout */
1699 timeo = jiffies + (HZ*20); /* FIXME */
1700 chip->erase_suspended = 0;
1701 }
1702
Todd Poynor02b15e32005-06-07 00:04:39 +01001703 if (chip_ready(map, adr)) {
1704 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001708 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001709 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001710 printk(KERN_WARNING "MTD %s(): software timeout\n",
1711 __func__ );
1712 break;
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001716 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001718 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001719 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001720 /* reset on all failures. */
1721 map_write( map, CMD(0xF0), chip->start );
1722 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001724 ret = -EIO;
1725 }
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 chip->state = FL_READY;
1728 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001729 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return ret;
1731}
1732
1733
Ben Dooksce0f33a2007-05-28 19:59:00 +01001734static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 unsigned long ofs, len;
1737 int ret;
1738
1739 ofs = instr->addr;
1740 len = instr->len;
1741
1742 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1743 if (ret)
1744 return ret;
1745
1746 instr->state = MTD_ERASE_DONE;
1747 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750}
1751
1752
1753static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1754{
1755 struct map_info *map = mtd->priv;
1756 struct cfi_private *cfi = map->fldrv_priv;
1757 int ret = 0;
1758
1759 if (instr->addr != 0)
1760 return -EINVAL;
1761
1762 if (instr->len != mtd->size)
1763 return -EINVAL;
1764
1765 ret = do_erase_chip(map, &cfi->chips[0]);
1766 if (ret)
1767 return ret;
1768
1769 instr->state = MTD_ERASE_DONE;
1770 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773}
1774
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001775static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1776 unsigned long adr, int len, void *thunk)
1777{
1778 struct cfi_private *cfi = map->fldrv_priv;
1779 int ret;
1780
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001781 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001782 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1783 if (ret)
1784 goto out_unlock;
1785 chip->state = FL_LOCKING;
1786
1787 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1788 __func__, adr, len);
1789
1790 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1791 cfi->device_type, NULL);
1792 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1793 cfi->device_type, NULL);
1794 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1795 cfi->device_type, NULL);
1796 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1797 cfi->device_type, NULL);
1798 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1799 cfi->device_type, NULL);
1800 map_write(map, CMD(0x40), chip->start + adr);
1801
1802 chip->state = FL_READY;
1803 put_chip(map, chip, adr + chip->start);
1804 ret = 0;
1805
1806out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001807 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001808 return ret;
1809}
1810
1811static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1812 unsigned long adr, int len, void *thunk)
1813{
1814 struct cfi_private *cfi = map->fldrv_priv;
1815 int ret;
1816
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001817 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001818 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1819 if (ret)
1820 goto out_unlock;
1821 chip->state = FL_UNLOCKING;
1822
1823 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1824 __func__, adr, len);
1825
1826 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1827 cfi->device_type, NULL);
1828 map_write(map, CMD(0x70), adr);
1829
1830 chip->state = FL_READY;
1831 put_chip(map, chip, adr + chip->start);
1832 ret = 0;
1833
1834out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001835 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001836 return ret;
1837}
1838
Adrian Hunter69423d92008-12-10 13:37:21 +00001839static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001840{
1841 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1842}
1843
Adrian Hunter69423d92008-12-10 13:37:21 +00001844static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001845{
1846 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850static void cfi_amdstd_sync (struct mtd_info *mtd)
1851{
1852 struct map_info *map = mtd->priv;
1853 struct cfi_private *cfi = map->fldrv_priv;
1854 int i;
1855 struct flchip *chip;
1856 int ret = 0;
1857 DECLARE_WAITQUEUE(wait, current);
1858
1859 for (i=0; !ret && i<cfi->numchips; i++) {
1860 chip = &cfi->chips[i];
1861
1862 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001863 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 switch(chip->state) {
1866 case FL_READY:
1867 case FL_STATUS:
1868 case FL_CFI_QUERY:
1869 case FL_JEDEC_QUERY:
1870 chip->oldstate = chip->state;
1871 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001872 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * as the whole point is that nobody can do anything
1874 * with the chip now anyway.
1875 */
1876 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001877 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879
1880 default:
1881 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07001882 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001884
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001885 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 schedule();
1888
1889 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto retry;
1892 }
1893 }
1894
1895 /* Unlock the chips again */
1896
1897 for (i--; i >=0; i--) {
1898 chip = &cfi->chips[i];
1899
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001900 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (chip->state == FL_SYNCING) {
1903 chip->state = chip->oldstate;
1904 wake_up(&chip->wq);
1905 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001906 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908}
1909
1910
1911static int cfi_amdstd_suspend(struct mtd_info *mtd)
1912{
1913 struct map_info *map = mtd->priv;
1914 struct cfi_private *cfi = map->fldrv_priv;
1915 int i;
1916 struct flchip *chip;
1917 int ret = 0;
1918
1919 for (i=0; !ret && i<cfi->numchips; i++) {
1920 chip = &cfi->chips[i];
1921
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001922 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 switch(chip->state) {
1925 case FL_READY:
1926 case FL_STATUS:
1927 case FL_CFI_QUERY:
1928 case FL_JEDEC_QUERY:
1929 chip->oldstate = chip->state;
1930 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001931 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * as the whole point is that nobody can do anything
1933 * with the chip now anyway.
1934 */
1935 case FL_PM_SUSPENDED:
1936 break;
1937
1938 default:
1939 ret = -EAGAIN;
1940 break;
1941 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001942 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 /* Unlock the chips again */
1946
1947 if (ret) {
1948 for (i--; i >=0; i--) {
1949 chip = &cfi->chips[i];
1950
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001951 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (chip->state == FL_PM_SUSPENDED) {
1954 chip->state = chip->oldstate;
1955 wake_up(&chip->wq);
1956 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001957 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return ret;
1962}
1963
1964
1965static void cfi_amdstd_resume(struct mtd_info *mtd)
1966{
1967 struct map_info *map = mtd->priv;
1968 struct cfi_private *cfi = map->fldrv_priv;
1969 int i;
1970 struct flchip *chip;
1971
1972 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 chip = &cfi->chips[i];
1975
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001976 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (chip->state == FL_PM_SUSPENDED) {
1979 chip->state = FL_READY;
1980 map_write(map, CMD(0xF0), chip->start);
1981 wake_up(&chip->wq);
1982 }
1983 else
1984 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1985
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001986 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988}
1989
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07001990
1991/*
1992 * Ensure that the flash device is put back into read array mode before
1993 * unloading the driver or rebooting. On some systems, rebooting while
1994 * the flash is in query/program/erase mode will prevent the CPU from
1995 * fetching the bootloader code, requiring a hard reset or power cycle.
1996 */
1997static int cfi_amdstd_reset(struct mtd_info *mtd)
1998{
1999 struct map_info *map = mtd->priv;
2000 struct cfi_private *cfi = map->fldrv_priv;
2001 int i, ret;
2002 struct flchip *chip;
2003
2004 for (i = 0; i < cfi->numchips; i++) {
2005
2006 chip = &cfi->chips[i];
2007
2008 mutex_lock(&chip->mutex);
2009
2010 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2011 if (!ret) {
2012 map_write(map, CMD(0xF0), chip->start);
2013 chip->state = FL_SHUTDOWN;
2014 put_chip(map, chip, chip->start);
2015 }
2016
2017 mutex_unlock(&chip->mutex);
2018 }
2019
2020 return 0;
2021}
2022
2023
2024static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2025 void *v)
2026{
2027 struct mtd_info *mtd;
2028
2029 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2030 cfi_amdstd_reset(mtd);
2031 return NOTIFY_DONE;
2032}
2033
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035static void cfi_amdstd_destroy(struct mtd_info *mtd)
2036{
2037 struct map_info *map = mtd->priv;
2038 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002039
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002040 cfi_amdstd_reset(mtd);
2041 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 kfree(cfi->cmdset_priv);
2043 kfree(cfi->cfiq);
2044 kfree(cfi);
2045 kfree(mtd->eraseregions);
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048MODULE_LICENSE("GPL");
2049MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2050MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
Guillaume LECERF80461122010-05-20 16:54:10 +02002051MODULE_ALIAS("cfi_cmdset_0006");
David Woodhouse1e804ce2010-05-20 16:54:05 +02002052MODULE_ALIAS("cfi_cmdset_0701");