2 * Common Flash Interface support:
3 * Intel Extended Vendor Command Set (ID 0x0001)
5 * (C) 2000 Red Hat. GPL'd
7 * $Id: cfi_cmdset_0001.c,v 1.186 2005/11/23 22:07:52 nico Exp $
10 * 10/10/2000 Nicolas Pitre <nico@cam.org>
11 * - completely revamped method functions so they are aware and
12 * independent of the flash geometry (buswidth, interleave, etc.)
13 * - scalability vs code size is completely set at compile-time
14 * (see include/linux/mtd/cfi.h for selection)
15 * - optimized write buffer method
16 * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
17 * - reworked lock/unlock/erase support for var size flash
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
26 #include <asm/byteorder.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/reboot.h>
33 #include <linux/mtd/xip.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/compatmac.h>
37 #include <linux/mtd/cfi.h>
39 /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
40 /* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
42 // debugging, turns off buffer write mode if set to 1
43 #define FORCE_WORD_WRITE 0
45 #define MANUFACTURER_INTEL 0x0089
46 #define I82802AB 0x00ad
47 #define I82802AC 0x00ac
48 #define MANUFACTURER_ST 0x0020
49 #define M50LPW080 0x002F
51 static int cfi_intelext_read (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
52 static int cfi_intelext_write_words(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
53 static int cfi_intelext_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
54 static int cfi_intelext_writev(struct mtd_info
*, const struct kvec
*, unsigned long, loff_t
, size_t *);
55 static int cfi_intelext_erase_varsize(struct mtd_info
*, struct erase_info
*);
56 static void cfi_intelext_sync (struct mtd_info
*);
57 static int cfi_intelext_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
58 static int cfi_intelext_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
60 static int cfi_intelext_read_fact_prot_reg (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
61 static int cfi_intelext_read_user_prot_reg (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
62 static int cfi_intelext_write_user_prot_reg (struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
63 static int cfi_intelext_lock_user_prot_reg (struct mtd_info
*, loff_t
, size_t);
64 static int cfi_intelext_get_fact_prot_info (struct mtd_info
*,
65 struct otp_info
*, size_t);
66 static int cfi_intelext_get_user_prot_info (struct mtd_info
*,
67 struct otp_info
*, size_t);
69 static int cfi_intelext_suspend (struct mtd_info
*);
70 static void cfi_intelext_resume (struct mtd_info
*);
71 static int cfi_intelext_reboot (struct notifier_block
*, unsigned long, void *);
73 static void cfi_intelext_destroy(struct mtd_info
*);
75 struct mtd_info
*cfi_cmdset_0001(struct map_info
*, int);
77 static struct mtd_info
*cfi_intelext_setup (struct mtd_info
*);
78 static int cfi_intelext_partition_fixup(struct mtd_info
*, struct cfi_private
**);
80 static int cfi_intelext_point (struct mtd_info
*mtd
, loff_t from
, size_t len
,
81 size_t *retlen
, u_char
**mtdbuf
);
82 static void cfi_intelext_unpoint (struct mtd_info
*mtd
, u_char
*addr
, loff_t from
,
85 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
);
86 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
);
92 * *********** SETUP AND PROBE BITS ***********
95 static struct mtd_chip_driver cfi_intelext_chipdrv
= {
96 .probe
= NULL
, /* Not usable directly */
97 .destroy
= cfi_intelext_destroy
,
98 .name
= "cfi_cmdset_0001",
102 /* #define DEBUG_LOCK_BITS */
103 /* #define DEBUG_CFI_FEATURES */
105 #ifdef DEBUG_CFI_FEATURES
106 static void cfi_tell_features(struct cfi_pri_intelext
*extp
)
109 printk(" Extended Query version %c.%c\n", extp
->MajorVersion
, extp
->MinorVersion
);
110 printk(" Feature/Command Support: %4.4X\n", extp
->FeatureSupport
);
111 printk(" - Chip Erase: %s\n", extp
->FeatureSupport
&1?"supported":"unsupported");
112 printk(" - Suspend Erase: %s\n", extp
->FeatureSupport
&2?"supported":"unsupported");
113 printk(" - Suspend Program: %s\n", extp
->FeatureSupport
&4?"supported":"unsupported");
114 printk(" - Legacy Lock/Unlock: %s\n", extp
->FeatureSupport
&8?"supported":"unsupported");
115 printk(" - Queued Erase: %s\n", extp
->FeatureSupport
&16?"supported":"unsupported");
116 printk(" - Instant block lock: %s\n", extp
->FeatureSupport
&32?"supported":"unsupported");
117 printk(" - Protection Bits: %s\n", extp
->FeatureSupport
&64?"supported":"unsupported");
118 printk(" - Page-mode read: %s\n", extp
->FeatureSupport
&128?"supported":"unsupported");
119 printk(" - Synchronous read: %s\n", extp
->FeatureSupport
&256?"supported":"unsupported");
120 printk(" - Simultaneous operations: %s\n", extp
->FeatureSupport
&512?"supported":"unsupported");
121 printk(" - Extended Flash Array: %s\n", extp
->FeatureSupport
&1024?"supported":"unsupported");
122 for (i
=11; i
<32; i
++) {
123 if (extp
->FeatureSupport
& (1<<i
))
124 printk(" - Unknown Bit %X: supported\n", i
);
127 printk(" Supported functions after Suspend: %2.2X\n", extp
->SuspendCmdSupport
);
128 printk(" - Program after Erase Suspend: %s\n", extp
->SuspendCmdSupport
&1?"supported":"unsupported");
129 for (i
=1; i
<8; i
++) {
130 if (extp
->SuspendCmdSupport
& (1<<i
))
131 printk(" - Unknown Bit %X: supported\n", i
);
134 printk(" Block Status Register Mask: %4.4X\n", extp
->BlkStatusRegMask
);
135 printk(" - Lock Bit Active: %s\n", extp
->BlkStatusRegMask
&1?"yes":"no");
136 printk(" - Lock-Down Bit Active: %s\n", extp
->BlkStatusRegMask
&2?"yes":"no");
137 for (i
=2; i
<3; i
++) {
138 if (extp
->BlkStatusRegMask
& (1<<i
))
139 printk(" - Unknown Bit %X Active: yes\n",i
);
141 printk(" - EFA Lock Bit: %s\n", extp
->BlkStatusRegMask
&16?"yes":"no");
142 printk(" - EFA Lock-Down Bit: %s\n", extp
->BlkStatusRegMask
&32?"yes":"no");
143 for (i
=6; i
<16; i
++) {
144 if (extp
->BlkStatusRegMask
& (1<<i
))
145 printk(" - Unknown Bit %X Active: yes\n",i
);
148 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
149 extp
->VccOptimal
>> 4, extp
->VccOptimal
& 0xf);
150 if (extp
->VppOptimal
)
151 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
152 extp
->VppOptimal
>> 4, extp
->VppOptimal
& 0xf);
156 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
157 /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
158 static void fixup_intel_strataflash(struct mtd_info
*mtd
, void* param
)
160 struct map_info
*map
= mtd
->priv
;
161 struct cfi_private
*cfi
= map
->fldrv_priv
;
162 struct cfi_pri_amdstd
*extp
= cfi
->cmdset_priv
;
164 printk(KERN_WARNING
"cfi_cmdset_0001: Suspend "
165 "erase on write disabled.\n");
166 extp
->SuspendCmdSupport
&= ~1;
170 #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
171 static void fixup_no_write_suspend(struct mtd_info
*mtd
, void* param
)
173 struct map_info
*map
= mtd
->priv
;
174 struct cfi_private
*cfi
= map
->fldrv_priv
;
175 struct cfi_pri_intelext
*cfip
= cfi
->cmdset_priv
;
177 if (cfip
&& (cfip
->FeatureSupport
&4)) {
178 cfip
->FeatureSupport
&= ~4;
179 printk(KERN_WARNING
"cfi_cmdset_0001: write suspend disabled\n");
184 static void fixup_st_m28w320ct(struct mtd_info
*mtd
, void* param
)
186 struct map_info
*map
= mtd
->priv
;
187 struct cfi_private
*cfi
= map
->fldrv_priv
;
189 cfi
->cfiq
->BufWriteTimeoutTyp
= 0; /* Not supported */
190 cfi
->cfiq
->BufWriteTimeoutMax
= 0; /* Not supported */
193 static void fixup_st_m28w320cb(struct mtd_info
*mtd
, void* param
)
195 struct map_info
*map
= mtd
->priv
;
196 struct cfi_private
*cfi
= map
->fldrv_priv
;
198 /* Note this is done after the region info is endian swapped */
199 cfi
->cfiq
->EraseRegionInfo
[1] =
200 (cfi
->cfiq
->EraseRegionInfo
[1] & 0xffff0000) | 0x3e;
203 static void fixup_use_point(struct mtd_info
*mtd
, void *param
)
205 struct map_info
*map
= mtd
->priv
;
206 if (!mtd
->point
&& map_is_linear(map
)) {
207 mtd
->point
= cfi_intelext_point
;
208 mtd
->unpoint
= cfi_intelext_unpoint
;
212 static void fixup_use_write_buffers(struct mtd_info
*mtd
, void *param
)
214 struct map_info
*map
= mtd
->priv
;
215 struct cfi_private
*cfi
= map
->fldrv_priv
;
216 if (cfi
->cfiq
->BufWriteTimeoutTyp
) {
217 printk(KERN_INFO
"Using buffer write method\n" );
218 mtd
->write
= cfi_intelext_write_buffers
;
219 mtd
->writev
= cfi_intelext_writev
;
223 static struct cfi_fixup cfi_fixup_table
[] = {
224 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
225 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_intel_strataflash
, NULL
},
227 #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
228 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_no_write_suspend
, NULL
},
230 #if !FORCE_WORD_WRITE
231 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_write_buffers
, NULL
},
233 { CFI_MFR_ST
, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct
, NULL
},
234 { CFI_MFR_ST
, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb
, NULL
},
238 static struct cfi_fixup jedec_fixup_table
[] = {
239 { MANUFACTURER_INTEL
, I82802AB
, fixup_use_fwh_lock
, NULL
, },
240 { MANUFACTURER_INTEL
, I82802AC
, fixup_use_fwh_lock
, NULL
, },
241 { MANUFACTURER_ST
, M50LPW080
, fixup_use_fwh_lock
, NULL
, },
244 static struct cfi_fixup fixup_table
[] = {
245 /* The CFI vendor ids and the JEDEC vendor IDs appear
246 * to be common. It is like the devices id's are as
247 * well. This table is to pick all cases where
248 * we know that is the case.
250 { CFI_MFR_ANY
, CFI_ID_ANY
, fixup_use_point
, NULL
},
254 static inline struct cfi_pri_intelext
*
255 read_pri_intelext(struct map_info
*map
, __u16 adr
)
257 struct cfi_pri_intelext
*extp
;
258 unsigned int extp_size
= sizeof(*extp
);
261 extp
= (struct cfi_pri_intelext
*)cfi_read_pri(map
, adr
, extp_size
, "Intel/Sharp");
265 if (extp
->MajorVersion
!= '1' ||
266 (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '4')) {
267 printk(KERN_ERR
" Unknown Intel/Sharp Extended Query "
268 "version %c.%c.\n", extp
->MajorVersion
,
274 /* Do some byteswapping if necessary */
275 extp
->FeatureSupport
= le32_to_cpu(extp
->FeatureSupport
);
276 extp
->BlkStatusRegMask
= le16_to_cpu(extp
->BlkStatusRegMask
);
277 extp
->ProtRegAddr
= le16_to_cpu(extp
->ProtRegAddr
);
279 if (extp
->MajorVersion
== '1' && extp
->MinorVersion
>= '3') {
280 unsigned int extra_size
= 0;
283 /* Protection Register info */
284 extra_size
+= (extp
->NumProtectionFields
- 1) *
285 sizeof(struct cfi_intelext_otpinfo
);
287 /* Burst Read info */
289 if (extp_size
< sizeof(*extp
) + extra_size
)
291 extra_size
+= extp
->extra
[extra_size
-1];
293 /* Number of hardware-partitions */
295 if (extp_size
< sizeof(*extp
) + extra_size
)
297 nb_parts
= extp
->extra
[extra_size
- 1];
299 /* skip the sizeof(partregion) field in CFI 1.4 */
300 if (extp
->MinorVersion
>= '4')
303 for (i
= 0; i
< nb_parts
; i
++) {
304 struct cfi_intelext_regioninfo
*rinfo
;
305 rinfo
= (struct cfi_intelext_regioninfo
*)&extp
->extra
[extra_size
];
306 extra_size
+= sizeof(*rinfo
);
307 if (extp_size
< sizeof(*extp
) + extra_size
)
309 rinfo
->NumIdentPartitions
=le16_to_cpu(rinfo
->NumIdentPartitions
);
310 extra_size
+= (rinfo
->NumBlockTypes
- 1)
311 * sizeof(struct cfi_intelext_blockinfo
);
314 if (extp
->MinorVersion
>= '4')
315 extra_size
+= sizeof(struct cfi_intelext_programming_regioninfo
);
317 if (extp_size
< sizeof(*extp
) + extra_size
) {
319 extp_size
= sizeof(*extp
) + extra_size
;
321 if (extp_size
> 4096) {
323 "%s: cfi_pri_intelext is too fat\n",
334 struct mtd_info
*cfi_cmdset_0001(struct map_info
*map
, int primary
)
336 struct cfi_private
*cfi
= map
->fldrv_priv
;
337 struct mtd_info
*mtd
;
340 mtd
= kmalloc(sizeof(*mtd
), GFP_KERNEL
);
342 printk(KERN_ERR
"Failed to allocate memory for MTD device\n");
345 memset(mtd
, 0, sizeof(*mtd
));
347 mtd
->type
= MTD_NORFLASH
;
349 /* Fill in the default mtd operations */
350 mtd
->erase
= cfi_intelext_erase_varsize
;
351 mtd
->read
= cfi_intelext_read
;
352 mtd
->write
= cfi_intelext_write_words
;
353 mtd
->sync
= cfi_intelext_sync
;
354 mtd
->lock
= cfi_intelext_lock
;
355 mtd
->unlock
= cfi_intelext_unlock
;
356 mtd
->suspend
= cfi_intelext_suspend
;
357 mtd
->resume
= cfi_intelext_resume
;
358 mtd
->flags
= MTD_CAP_NORFLASH
;
359 mtd
->name
= map
->name
;
361 mtd
->reboot_notifier
.notifier_call
= cfi_intelext_reboot
;
363 if (cfi
->cfi_mode
== CFI_MODE_CFI
) {
365 * It's a real CFI chip, not one for which the probe
366 * routine faked a CFI structure. So we read the feature
369 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
370 struct cfi_pri_intelext
*extp
;
372 extp
= read_pri_intelext(map
, adr
);
378 /* Install our own private info structure */
379 cfi
->cmdset_priv
= extp
;
381 cfi_fixup(mtd
, cfi_fixup_table
);
383 #ifdef DEBUG_CFI_FEATURES
384 /* Tell the user about it in lots of lovely detail */
385 cfi_tell_features(extp
);
388 if(extp
->SuspendCmdSupport
& 1) {
389 printk(KERN_NOTICE
"cfi_cmdset_0001: Erase suspend on write enabled\n");
392 else if (cfi
->cfi_mode
== CFI_MODE_JEDEC
) {
393 /* Apply jedec specific fixups */
394 cfi_fixup(mtd
, jedec_fixup_table
);
396 /* Apply generic fixups */
397 cfi_fixup(mtd
, fixup_table
);
399 for (i
=0; i
< cfi
->numchips
; i
++) {
400 cfi
->chips
[i
].word_write_time
= 1<<cfi
->cfiq
->WordWriteTimeoutTyp
;
401 cfi
->chips
[i
].buffer_write_time
= 1<<cfi
->cfiq
->BufWriteTimeoutTyp
;
402 cfi
->chips
[i
].erase_time
= 1<<cfi
->cfiq
->BlockEraseTimeoutTyp
;
403 cfi
->chips
[i
].ref_point_counter
= 0;
404 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
407 map
->fldrv
= &cfi_intelext_chipdrv
;
409 return cfi_intelext_setup(mtd
);
411 struct mtd_info
*cfi_cmdset_0003(struct map_info
*map
, int primary
) __attribute__((alias("cfi_cmdset_0001")));
412 struct mtd_info
*cfi_cmdset_0200(struct map_info
*map
, int primary
) __attribute__((alias("cfi_cmdset_0001")));
413 EXPORT_SYMBOL_GPL(cfi_cmdset_0001
);
414 EXPORT_SYMBOL_GPL(cfi_cmdset_0003
);
415 EXPORT_SYMBOL_GPL(cfi_cmdset_0200
);
417 static struct mtd_info
*cfi_intelext_setup(struct mtd_info
*mtd
)
419 struct map_info
*map
= mtd
->priv
;
420 struct cfi_private
*cfi
= map
->fldrv_priv
;
421 unsigned long offset
= 0;
423 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
425 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
427 mtd
->size
= devsize
* cfi
->numchips
;
429 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
430 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
431 * mtd
->numeraseregions
, GFP_KERNEL
);
432 if (!mtd
->eraseregions
) {
433 printk(KERN_ERR
"Failed to allocate memory for MTD erase region info\n");
437 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
438 unsigned long ernum
, ersize
;
439 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
440 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
442 if (mtd
->erasesize
< ersize
) {
443 mtd
->erasesize
= ersize
;
445 for (j
=0; j
<cfi
->numchips
; j
++) {
446 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
447 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
448 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
450 offset
+= (ersize
* ernum
);
453 if (offset
!= devsize
) {
455 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
459 for (i
=0; i
<mtd
->numeraseregions
;i
++){
460 printk(KERN_DEBUG
"erase region %d: offset=0x%x,size=0x%x,blocks=%d\n",
461 i
,mtd
->eraseregions
[i
].offset
,
462 mtd
->eraseregions
[i
].erasesize
,
463 mtd
->eraseregions
[i
].numblocks
);
466 #ifdef CONFIG_MTD_OTP
467 mtd
->read_fact_prot_reg
= cfi_intelext_read_fact_prot_reg
;
468 mtd
->read_user_prot_reg
= cfi_intelext_read_user_prot_reg
;
469 mtd
->write_user_prot_reg
= cfi_intelext_write_user_prot_reg
;
470 mtd
->lock_user_prot_reg
= cfi_intelext_lock_user_prot_reg
;
471 mtd
->get_fact_prot_info
= cfi_intelext_get_fact_prot_info
;
472 mtd
->get_user_prot_info
= cfi_intelext_get_user_prot_info
;
475 /* This function has the potential to distort the reality
476 a bit and therefore should be called last. */
477 if (cfi_intelext_partition_fixup(mtd
, &cfi
) != 0)
480 __module_get(THIS_MODULE
);
481 register_reboot_notifier(&mtd
->reboot_notifier
);
486 kfree(mtd
->eraseregions
);
489 kfree(cfi
->cmdset_priv
);
493 static int cfi_intelext_partition_fixup(struct mtd_info
*mtd
,
494 struct cfi_private
**pcfi
)
496 struct map_info
*map
= mtd
->priv
;
497 struct cfi_private
*cfi
= *pcfi
;
498 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
501 * Probing of multi-partition flash ships.
503 * To support multiple partitions when available, we simply arrange
504 * for each of them to have their own flchip structure even if they
505 * are on the same physical chip. This means completely recreating
506 * a new cfi_private structure right here which is a blatent code
507 * layering violation, but this is still the least intrusive
508 * arrangement at this point. This can be rearranged in the future
509 * if someone feels motivated enough. --nico
511 if (extp
&& extp
->MajorVersion
== '1' && extp
->MinorVersion
>= '3'
512 && extp
->FeatureSupport
& (1 << 9)) {
513 struct cfi_private
*newcfi
;
515 struct flchip_shared
*shared
;
516 int offs
, numregions
, numparts
, partshift
, numvirtchips
, i
, j
;
518 /* Protection Register info */
519 offs
= (extp
->NumProtectionFields
- 1) *
520 sizeof(struct cfi_intelext_otpinfo
);
522 /* Burst Read info */
523 offs
+= extp
->extra
[offs
+1]+2;
525 /* Number of partition regions */
526 numregions
= extp
->extra
[offs
];
529 /* skip the sizeof(partregion) field in CFI 1.4 */
530 if (extp
->MinorVersion
>= '4')
533 /* Number of hardware partitions */
535 for (i
= 0; i
< numregions
; i
++) {
536 struct cfi_intelext_regioninfo
*rinfo
;
537 rinfo
= (struct cfi_intelext_regioninfo
*)&extp
->extra
[offs
];
538 numparts
+= rinfo
->NumIdentPartitions
;
539 offs
+= sizeof(*rinfo
)
540 + (rinfo
->NumBlockTypes
- 1) *
541 sizeof(struct cfi_intelext_blockinfo
);
544 /* Programming Region info */
545 if (extp
->MinorVersion
>= '4') {
546 struct cfi_intelext_programming_regioninfo
*prinfo
;
547 prinfo
= (struct cfi_intelext_programming_regioninfo
*)&extp
->extra
[offs
];
548 MTD_PROGREGION_SIZE(mtd
) = cfi
->interleave
<< prinfo
->ProgRegShift
;
549 MTD_PROGREGION_CTRLMODE_VALID(mtd
) = cfi
->interleave
* prinfo
->ControlValid
;
550 MTD_PROGREGION_CTRLMODE_INVALID(mtd
) = cfi
->interleave
* prinfo
->ControlInvalid
;
551 mtd
->flags
|= MTD_PROGRAM_REGIONS
;
552 printk(KERN_DEBUG
"%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
553 map
->name
, MTD_PROGREGION_SIZE(mtd
),
554 MTD_PROGREGION_CTRLMODE_VALID(mtd
),
555 MTD_PROGREGION_CTRLMODE_INVALID(mtd
));
559 * All functions below currently rely on all chips having
560 * the same geometry so we'll just assume that all hardware
561 * partitions are of the same size too.
563 partshift
= cfi
->chipshift
- __ffs(numparts
);
565 if ((1 << partshift
) < mtd
->erasesize
) {
567 "%s: bad number of hw partitions (%d)\n",
568 __FUNCTION__
, numparts
);
572 numvirtchips
= cfi
->numchips
* numparts
;
573 newcfi
= kmalloc(sizeof(struct cfi_private
) + numvirtchips
* sizeof(struct flchip
), GFP_KERNEL
);
576 shared
= kmalloc(sizeof(struct flchip_shared
) * cfi
->numchips
, GFP_KERNEL
);
581 memcpy(newcfi
, cfi
, sizeof(struct cfi_private
));
582 newcfi
->numchips
= numvirtchips
;
583 newcfi
->chipshift
= partshift
;
585 chip
= &newcfi
->chips
[0];
586 for (i
= 0; i
< cfi
->numchips
; i
++) {
587 shared
[i
].writing
= shared
[i
].erasing
= NULL
;
588 spin_lock_init(&shared
[i
].lock
);
589 for (j
= 0; j
< numparts
; j
++) {
590 *chip
= cfi
->chips
[i
];
591 chip
->start
+= j
<< partshift
;
592 chip
->priv
= &shared
[i
];
593 /* those should be reset too since
594 they create memory references. */
595 init_waitqueue_head(&chip
->wq
);
596 spin_lock_init(&chip
->_spinlock
);
597 chip
->mutex
= &chip
->_spinlock
;
602 printk(KERN_DEBUG
"%s: %d set(s) of %d interleaved chips "
603 "--> %d partitions of %d KiB\n",
604 map
->name
, cfi
->numchips
, cfi
->interleave
,
605 newcfi
->numchips
, 1<<(newcfi
->chipshift
-10));
607 map
->fldrv_priv
= newcfi
;
616 * *********** CHIP ACCESS FUNCTIONS ***********
619 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
)
621 DECLARE_WAITQUEUE(wait
, current
);
622 struct cfi_private
*cfi
= map
->fldrv_priv
;
623 map_word status
, status_OK
= CMD(0x80), status_PWS
= CMD(0x01);
625 struct cfi_pri_intelext
*cfip
= cfi
->cmdset_priv
;
628 timeo
= jiffies
+ HZ
;
630 if (chip
->priv
&& (mode
== FL_WRITING
|| mode
== FL_ERASING
|| mode
== FL_OTP_WRITE
)) {
632 * OK. We have possibility for contension on the write/erase
633 * operations which are global to the real chip and not per
634 * partition. So let's fight it over in the partition which
635 * currently has authority on the operation.
637 * The rules are as follows:
639 * - any write operation must own shared->writing.
641 * - any erase operation must own _both_ shared->writing and
644 * - contension arbitration is handled in the owner's context.
646 * The 'shared' struct can be read and/or written only when
649 struct flchip_shared
*shared
= chip
->priv
;
650 struct flchip
*contender
;
651 spin_lock(&shared
->lock
);
652 contender
= shared
->writing
;
653 if (contender
&& contender
!= chip
) {
655 * The engine to perform desired operation on this
656 * partition is already in use by someone else.
657 * Let's fight over it in the context of the chip
658 * currently using it. If it is possible to suspend,
659 * that other partition will do just that, otherwise
660 * it'll happily send us to sleep. In any case, when
661 * get_chip returns success we're clear to go ahead.
663 int ret
= spin_trylock(contender
->mutex
);
664 spin_unlock(&shared
->lock
);
667 spin_unlock(chip
->mutex
);
668 ret
= get_chip(map
, contender
, contender
->start
, mode
);
669 spin_lock(chip
->mutex
);
671 spin_unlock(contender
->mutex
);
674 timeo
= jiffies
+ HZ
;
675 spin_lock(&shared
->lock
);
676 spin_unlock(contender
->mutex
);
680 shared
->writing
= chip
;
681 if (mode
== FL_ERASING
)
682 shared
->erasing
= chip
;
683 spin_unlock(&shared
->lock
);
686 switch (chip
->state
) {
690 status
= map_read(map
, adr
);
691 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
694 /* At this point we're fine with write operations
695 in other partitions as they don't conflict. */
696 if (chip
->priv
&& map_word_andequal(map
, status
, status_PWS
, status_PWS
))
699 if (time_after(jiffies
, timeo
)) {
700 printk(KERN_ERR
"%s: Waiting for chip to be ready timed out. Status %lx\n",
701 map
->name
, status
.x
[0]);
704 spin_unlock(chip
->mutex
);
706 spin_lock(chip
->mutex
);
707 /* Someone else might have been playing with it. */
718 !(cfip
->FeatureSupport
& 2) ||
719 !(mode
== FL_READY
|| mode
== FL_POINT
||
720 (mode
== FL_WRITING
&& (cfip
->SuspendCmdSupport
& 1))))
725 map_write(map
, CMD(0xB0), adr
);
727 /* If the flash has finished erasing, then 'erase suspend'
728 * appears to make some (28F320) flash devices switch to
729 * 'read' mode. Make sure that we switch to 'read status'
730 * mode so we get the right data. --rmk
732 map_write(map
, CMD(0x70), adr
);
733 chip
->oldstate
= FL_ERASING
;
734 chip
->state
= FL_ERASE_SUSPENDING
;
735 chip
->erase_suspended
= 1;
737 status
= map_read(map
, adr
);
738 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
741 if (time_after(jiffies
, timeo
)) {
742 /* Urgh. Resume and pretend we weren't here. */
743 map_write(map
, CMD(0xd0), adr
);
744 /* Make sure we're in 'read status' mode if it had finished */
745 map_write(map
, CMD(0x70), adr
);
746 chip
->state
= FL_ERASING
;
747 chip
->oldstate
= FL_READY
;
748 printk(KERN_ERR
"%s: Chip not ready after erase "
749 "suspended: status = 0x%lx\n", map
->name
, status
.x
[0]);
753 spin_unlock(chip
->mutex
);
755 spin_lock(chip
->mutex
);
756 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
757 So we can just loop here. */
759 chip
->state
= FL_STATUS
;
762 case FL_XIP_WHILE_ERASING
:
763 if (mode
!= FL_READY
&& mode
!= FL_POINT
&&
764 (mode
!= FL_WRITING
|| !cfip
|| !(cfip
->SuspendCmdSupport
&1)))
766 chip
->oldstate
= chip
->state
;
767 chip
->state
= FL_READY
;
771 /* Only if there's no operation suspended... */
772 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
777 set_current_state(TASK_UNINTERRUPTIBLE
);
778 add_wait_queue(&chip
->wq
, &wait
);
779 spin_unlock(chip
->mutex
);
781 remove_wait_queue(&chip
->wq
, &wait
);
782 spin_lock(chip
->mutex
);
787 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
789 struct cfi_private
*cfi
= map
->fldrv_priv
;
792 struct flchip_shared
*shared
= chip
->priv
;
793 spin_lock(&shared
->lock
);
794 if (shared
->writing
== chip
&& chip
->oldstate
== FL_READY
) {
795 /* We own the ability to write, but we're done */
796 shared
->writing
= shared
->erasing
;
797 if (shared
->writing
&& shared
->writing
!= chip
) {
798 /* give back ownership to who we loaned it from */
799 struct flchip
*loaner
= shared
->writing
;
800 spin_lock(loaner
->mutex
);
801 spin_unlock(&shared
->lock
);
802 spin_unlock(chip
->mutex
);
803 put_chip(map
, loaner
, loaner
->start
);
804 spin_lock(chip
->mutex
);
805 spin_unlock(loaner
->mutex
);
809 shared
->erasing
= NULL
;
810 shared
->writing
= NULL
;
811 } else if (shared
->erasing
== chip
&& shared
->writing
!= chip
) {
813 * We own the ability to erase without the ability
814 * to write, which means the erase was suspended
815 * and some other partition is currently writing.
816 * Don't let the switch below mess things up since
817 * we don't have ownership to resume anything.
819 spin_unlock(&shared
->lock
);
823 spin_unlock(&shared
->lock
);
826 switch(chip
->oldstate
) {
828 chip
->state
= chip
->oldstate
;
829 /* What if one interleaved chip has finished and the
830 other hasn't? The old code would leave the finished
831 one in READY mode. That's bad, and caused -EROFS
832 errors to be returned from do_erase_oneblock because
833 that's the only bit it checked for at the time.
834 As the state machine appears to explicitly allow
835 sending the 0x70 (Read Status) command to an erasing
836 chip and expecting it to be ignored, that's what we
838 map_write(map
, CMD(0xd0), adr
);
839 map_write(map
, CMD(0x70), adr
);
840 chip
->oldstate
= FL_READY
;
841 chip
->state
= FL_ERASING
;
844 case FL_XIP_WHILE_ERASING
:
845 chip
->state
= chip
->oldstate
;
846 chip
->oldstate
= FL_READY
;
852 /* We should really make set_vpp() count, rather than doing this */
856 printk(KERN_ERR
"%s: put_chip() called with oldstate %d!!\n", map
->name
, chip
->oldstate
);
861 #ifdef CONFIG_MTD_XIP
864 * No interrupt what so ever can be serviced while the flash isn't in array
865 * mode. This is ensured by the xip_disable() and xip_enable() functions
866 * enclosing any code path where the flash is known not to be in array mode.
867 * And within a XIP disabled code path, only functions marked with __xipram
868 * may be called and nothing else (it's a good thing to inspect generated
869 * assembly to make sure inline functions were actually inlined and that gcc
870 * didn't emit calls to its own support functions). Also configuring MTD CFI
871 * support to a single buswidth and a single interleave is also recommended.
874 static void xip_disable(struct map_info
*map
, struct flchip
*chip
,
877 /* TODO: chips with no XIP use should ignore and return */
878 (void) map_read(map
, adr
); /* ensure mmu mapping is up to date */
882 static void __xipram
xip_enable(struct map_info
*map
, struct flchip
*chip
,
885 struct cfi_private
*cfi
= map
->fldrv_priv
;
886 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
887 map_write(map
, CMD(0xff), adr
);
888 chip
->state
= FL_READY
;
890 (void) map_read(map
, adr
);
896 * When a delay is required for the flash operation to complete, the
897 * xip_udelay() function is polling for both the given timeout and pending
898 * (but still masked) hardware interrupts. Whenever there is an interrupt
899 * pending then the flash erase or write operation is suspended, array mode
900 * restored and interrupts unmasked. Task scheduling might also happen at that
901 * point. The CPU eventually returns from the interrupt or the call to
902 * schedule() and the suspended flash operation is resumed for the remaining
903 * of the delay period.
905 * Warning: this function _will_ fool interrupt latency tracing tools.
908 static void __xipram
xip_udelay(struct map_info
*map
, struct flchip
*chip
,
909 unsigned long adr
, int usec
)
911 struct cfi_private
*cfi
= map
->fldrv_priv
;
912 struct cfi_pri_intelext
*cfip
= cfi
->cmdset_priv
;
913 map_word status
, OK
= CMD(0x80);
914 unsigned long suspended
, start
= xip_currtime();
915 flstate_t oldstate
, newstate
;
919 if (xip_irqpending() && cfip
&&
920 ((chip
->state
== FL_ERASING
&& (cfip
->FeatureSupport
&2)) ||
921 (chip
->state
== FL_WRITING
&& (cfip
->FeatureSupport
&4))) &&
922 (cfi_interleave_is_1(cfi
) || chip
->oldstate
== FL_READY
)) {
924 * Let's suspend the erase or write operation when
925 * supported. Note that we currently don't try to
926 * suspend interleaved chips if there is already
927 * another operation suspended (imagine what happens
928 * when one chip was already done with the current
929 * operation while another chip suspended it, then
930 * we resume the whole thing at once). Yes, it
933 map_write(map
, CMD(0xb0), adr
);
934 map_write(map
, CMD(0x70), adr
);
935 usec
-= xip_elapsed_since(start
);
936 suspended
= xip_currtime();
938 if (xip_elapsed_since(suspended
) > 100000) {
940 * The chip doesn't want to suspend
941 * after waiting for 100 msecs.
942 * This is a critical error but there
943 * is not much we can do here.
947 status
= map_read(map
, adr
);
948 } while (!map_word_andequal(map
, status
, OK
, OK
));
950 /* Suspend succeeded */
951 oldstate
= chip
->state
;
952 if (oldstate
== FL_ERASING
) {
953 if (!map_word_bitsset(map
, status
, CMD(0x40)))
955 newstate
= FL_XIP_WHILE_ERASING
;
956 chip
->erase_suspended
= 1;
958 if (!map_word_bitsset(map
, status
, CMD(0x04)))
960 newstate
= FL_XIP_WHILE_WRITING
;
961 chip
->write_suspended
= 1;
963 chip
->state
= newstate
;
964 map_write(map
, CMD(0xff), adr
);
965 (void) map_read(map
, adr
);
966 asm volatile (".rep 8; nop; .endr");
968 spin_unlock(chip
->mutex
);
969 asm volatile (".rep 8; nop; .endr");
973 * We're back. However someone else might have
974 * decided to go write to the chip if we are in
975 * a suspended erase state. If so let's wait
978 spin_lock(chip
->mutex
);
979 while (chip
->state
!= newstate
) {
980 DECLARE_WAITQUEUE(wait
, current
);
981 set_current_state(TASK_UNINTERRUPTIBLE
);
982 add_wait_queue(&chip
->wq
, &wait
);
983 spin_unlock(chip
->mutex
);
985 remove_wait_queue(&chip
->wq
, &wait
);
986 spin_lock(chip
->mutex
);
988 /* Disallow XIP again */
991 /* Resume the write or erase operation */
992 map_write(map
, CMD(0xd0), adr
);
993 map_write(map
, CMD(0x70), adr
);
994 chip
->state
= oldstate
;
995 start
= xip_currtime();
996 } else if (usec
>= 1000000/HZ
) {
998 * Try to save on CPU power when waiting delay
999 * is at least a system timer tick period.
1000 * No need to be extremely accurate here.
1004 status
= map_read(map
, adr
);
1005 } while (!map_word_andequal(map
, status
, OK
, OK
)
1006 && xip_elapsed_since(start
) < usec
);
1009 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1012 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1013 * the flash is actively programming or erasing since we have to poll for
1014 * the operation to complete anyway. We can't do that in a generic way with
1015 * a XIP setup so do it before the actual flash operation in this case
1016 * and stub it out from INVALIDATE_CACHE_UDELAY.
1018 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
1019 INVALIDATE_CACHED_RANGE(map, from, size)
1021 #define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
1022 UDELAY(map, chip, cmd_adr, usec)
1027 * Activating this XIP support changes the way the code works a bit. For
1028 * example the code to suspend the current process when concurrent access
1029 * happens is never executed because xip_udelay() will always return with the
1030 * same chip state as it was entered with. This is why there is no care for
1031 * the presence of add_wait_queue() or schedule() calls from within a couple
1032 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1033 * The queueing and scheduling are always happening within xip_udelay().
1035 * Similarly, get_chip() and put_chip() just happen to always be executed
1036 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1037 * is in array mode, therefore never executing many cases therein and not
1038 * causing any problem with XIP.
1043 #define xip_disable(map, chip, adr)
1044 #define xip_enable(map, chip, adr)
1045 #define XIP_INVAL_CACHED_RANGE(x...)
1047 #define UDELAY(map, chip, adr, usec) \
1049 spin_unlock(chip->mutex); \
1051 spin_lock(chip->mutex); \
1054 #define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
1056 spin_unlock(chip->mutex); \
1057 INVALIDATE_CACHED_RANGE(map, adr, len); \
1059 spin_lock(chip->mutex); \
1064 static int do_point_onechip (struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
)
1066 unsigned long cmd_addr
;
1067 struct cfi_private
*cfi
= map
->fldrv_priv
;
1072 /* Ensure cmd read/writes are aligned. */
1073 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
1075 spin_lock(chip
->mutex
);
1077 ret
= get_chip(map
, chip
, cmd_addr
, FL_POINT
);
1080 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
)
1081 map_write(map
, CMD(0xff), cmd_addr
);
1083 chip
->state
= FL_POINT
;
1084 chip
->ref_point_counter
++;
1086 spin_unlock(chip
->mutex
);
1091 static int cfi_intelext_point (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
**mtdbuf
)
1093 struct map_info
*map
= mtd
->priv
;
1094 struct cfi_private
*cfi
= map
->fldrv_priv
;
1099 if (!map
->virt
|| (from
+ len
> mtd
->size
))
1102 *mtdbuf
= (void *)map
->virt
+ from
;
1105 /* Now lock the chip(s) to POINT state */
1107 /* ofs: offset within the first chip that the first read should start */
1108 chipnum
= (from
>> cfi
->chipshift
);
1109 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1112 unsigned long thislen
;
1114 if (chipnum
>= cfi
->numchips
)
1117 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1118 thislen
= (1<<cfi
->chipshift
) - ofs
;
1122 ret
= do_point_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
);
1135 static void cfi_intelext_unpoint (struct mtd_info
*mtd
, u_char
*addr
, loff_t from
, size_t len
)
1137 struct map_info
*map
= mtd
->priv
;
1138 struct cfi_private
*cfi
= map
->fldrv_priv
;
1142 /* Now unlock the chip(s) POINT state */
1144 /* ofs: offset within the first chip that the first read should start */
1145 chipnum
= (from
>> cfi
->chipshift
);
1146 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1149 unsigned long thislen
;
1150 struct flchip
*chip
;
1152 chip
= &cfi
->chips
[chipnum
];
1153 if (chipnum
>= cfi
->numchips
)
1156 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1157 thislen
= (1<<cfi
->chipshift
) - ofs
;
1161 spin_lock(chip
->mutex
);
1162 if (chip
->state
== FL_POINT
) {
1163 chip
->ref_point_counter
--;
1164 if(chip
->ref_point_counter
== 0)
1165 chip
->state
= FL_READY
;
1167 printk(KERN_ERR
"%s: Warning: unpoint called on non pointed region\n", map
->name
); /* Should this give an error? */
1169 put_chip(map
, chip
, chip
->start
);
1170 spin_unlock(chip
->mutex
);
1178 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
1180 unsigned long cmd_addr
;
1181 struct cfi_private
*cfi
= map
->fldrv_priv
;
1186 /* Ensure cmd read/writes are aligned. */
1187 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
1189 spin_lock(chip
->mutex
);
1190 ret
= get_chip(map
, chip
, cmd_addr
, FL_READY
);
1192 spin_unlock(chip
->mutex
);
1196 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
1197 map_write(map
, CMD(0xff), cmd_addr
);
1199 chip
->state
= FL_READY
;
1202 map_copy_from(map
, buf
, adr
, len
);
1204 put_chip(map
, chip
, cmd_addr
);
1206 spin_unlock(chip
->mutex
);
1210 static int cfi_intelext_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
1212 struct map_info
*map
= mtd
->priv
;
1213 struct cfi_private
*cfi
= map
->fldrv_priv
;
1218 /* ofs: offset within the first chip that the first read should start */
1219 chipnum
= (from
>> cfi
->chipshift
);
1220 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1225 unsigned long thislen
;
1227 if (chipnum
>= cfi
->numchips
)
1230 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1231 thislen
= (1<<cfi
->chipshift
) - ofs
;
1235 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
1249 static int __xipram
do_write_oneword(struct map_info
*map
, struct flchip
*chip
,
1250 unsigned long adr
, map_word datum
, int mode
)
1252 struct cfi_private
*cfi
= map
->fldrv_priv
;
1253 map_word status
, status_OK
, write_cmd
;
1254 unsigned long timeo
;
1259 /* Let's determine those according to the interleave only once */
1260 status_OK
= CMD(0x80);
1263 write_cmd
= (cfi
->cfiq
->P_ID
!= 0x0200) ? CMD(0x40) : CMD(0x41);
1266 write_cmd
= CMD(0xc0);
1272 spin_lock(chip
->mutex
);
1273 ret
= get_chip(map
, chip
, adr
, mode
);
1275 spin_unlock(chip
->mutex
);
1279 XIP_INVAL_CACHED_RANGE(map
, adr
, map_bankwidth(map
));
1281 xip_disable(map
, chip
, adr
);
1282 map_write(map
, write_cmd
, adr
);
1283 map_write(map
, datum
, adr
);
1286 INVALIDATE_CACHE_UDELAY(map
, chip
, adr
,
1287 adr
, map_bankwidth(map
),
1288 chip
->word_write_time
);
1290 timeo
= jiffies
+ (HZ
/2);
1293 if (chip
->state
!= mode
) {
1294 /* Someone's suspended the write. Sleep */
1295 DECLARE_WAITQUEUE(wait
, current
);
1297 set_current_state(TASK_UNINTERRUPTIBLE
);
1298 add_wait_queue(&chip
->wq
, &wait
);
1299 spin_unlock(chip
->mutex
);
1301 remove_wait_queue(&chip
->wq
, &wait
);
1302 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1303 spin_lock(chip
->mutex
);
1307 status
= map_read(map
, adr
);
1308 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1311 /* OK Still waiting */
1312 if (time_after(jiffies
, timeo
)) {
1313 map_write(map
, CMD(0x70), adr
);
1314 chip
->state
= FL_STATUS
;
1315 xip_enable(map
, chip
, adr
);
1316 printk(KERN_ERR
"%s: word write error (status timeout)\n", map
->name
);
1321 /* Latency issues. Drop the lock, wait a while and retry */
1323 UDELAY(map
, chip
, adr
, 1);
1326 chip
->word_write_time
--;
1327 if (!chip
->word_write_time
)
1328 chip
->word_write_time
= 1;
1331 chip
->word_write_time
++;
1333 /* Done and happy. */
1334 chip
->state
= FL_STATUS
;
1336 /* check for errors */
1337 if (map_word_bitsset(map
, status
, CMD(0x1a))) {
1338 unsigned long chipstatus
= MERGESTATUS(status
);
1341 map_write(map
, CMD(0x50), adr
);
1342 map_write(map
, CMD(0x70), adr
);
1343 xip_enable(map
, chip
, adr
);
1345 if (chipstatus
& 0x02) {
1347 } else if (chipstatus
& 0x08) {
1348 printk(KERN_ERR
"%s: word write error (bad VPP)\n", map
->name
);
1351 printk(KERN_ERR
"%s: word write error (status 0x%lx)\n", map
->name
, chipstatus
);
1358 xip_enable(map
, chip
, adr
);
1359 out
: put_chip(map
, chip
, adr
);
1360 spin_unlock(chip
->mutex
);
1365 static int cfi_intelext_write_words (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
, const u_char
*buf
)
1367 struct map_info
*map
= mtd
->priv
;
1368 struct cfi_private
*cfi
= map
->fldrv_priv
;
1377 chipnum
= to
>> cfi
->chipshift
;
1378 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1380 /* If it's not bus-aligned, do the first byte write */
1381 if (ofs
& (map_bankwidth(map
)-1)) {
1382 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
)-1);
1383 int gap
= ofs
- bus_ofs
;
1387 n
= min_t(int, len
, map_bankwidth(map
)-gap
);
1388 datum
= map_word_ff(map
);
1389 datum
= map_word_load_partial(map
, datum
, buf
, gap
, n
);
1391 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1392 bus_ofs
, datum
, FL_WRITING
);
1401 if (ofs
>> cfi
->chipshift
) {
1404 if (chipnum
== cfi
->numchips
)
1409 while(len
>= map_bankwidth(map
)) {
1410 map_word datum
= map_word_load(map
, buf
);
1412 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1413 ofs
, datum
, FL_WRITING
);
1417 ofs
+= map_bankwidth(map
);
1418 buf
+= map_bankwidth(map
);
1419 (*retlen
) += map_bankwidth(map
);
1420 len
-= map_bankwidth(map
);
1422 if (ofs
>> cfi
->chipshift
) {
1425 if (chipnum
== cfi
->numchips
)
1430 if (len
& (map_bankwidth(map
)-1)) {
1433 datum
= map_word_ff(map
);
1434 datum
= map_word_load_partial(map
, datum
, buf
, 0, len
);
1436 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1437 ofs
, datum
, FL_WRITING
);
1448 static int __xipram
do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
1449 unsigned long adr
, const struct kvec
**pvec
,
1450 unsigned long *pvec_seek
, int len
)
1452 struct cfi_private
*cfi
= map
->fldrv_priv
;
1453 map_word status
, status_OK
, write_cmd
, datum
;
1454 unsigned long cmd_adr
, timeo
;
1455 int wbufsize
, z
, ret
=0, word_gap
, words
;
1456 const struct kvec
*vec
;
1457 unsigned long vec_seek
;
1459 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1461 cmd_adr
= adr
& ~(wbufsize
-1);
1463 /* Let's determine this according to the interleave only once */
1464 status_OK
= CMD(0x80);
1465 write_cmd
= (cfi
->cfiq
->P_ID
!= 0x0200) ? CMD(0xe8) : CMD(0xe9);
1467 spin_lock(chip
->mutex
);
1468 ret
= get_chip(map
, chip
, cmd_adr
, FL_WRITING
);
1470 spin_unlock(chip
->mutex
);
1474 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1476 xip_disable(map
, chip
, cmd_adr
);
1478 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
1479 [...], the device will not accept any more Write to Buffer commands".
1480 So we must check here and reset those bits if they're set. Otherwise
1481 we're just pissing in the wind */
1482 if (chip
->state
!= FL_STATUS
)
1483 map_write(map
, CMD(0x70), cmd_adr
);
1484 status
= map_read(map
, cmd_adr
);
1485 if (map_word_bitsset(map
, status
, CMD(0x30))) {
1486 xip_enable(map
, chip
, cmd_adr
);
1487 printk(KERN_WARNING
"SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status
.x
[0]);
1488 xip_disable(map
, chip
, cmd_adr
);
1489 map_write(map
, CMD(0x50), cmd_adr
);
1490 map_write(map
, CMD(0x70), cmd_adr
);
1493 chip
->state
= FL_WRITING_TO_BUFFER
;
1497 map_write(map
, write_cmd
, cmd_adr
);
1499 status
= map_read(map
, cmd_adr
);
1500 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1503 UDELAY(map
, chip
, cmd_adr
, 1);
1506 /* Argh. Not ready for write to buffer */
1508 map_write(map
, CMD(0x70), cmd_adr
);
1509 chip
->state
= FL_STATUS
;
1510 Xstatus
= map_read(map
, cmd_adr
);
1511 /* Odd. Clear status bits */
1512 map_write(map
, CMD(0x50), cmd_adr
);
1513 map_write(map
, CMD(0x70), cmd_adr
);
1514 xip_enable(map
, chip
, cmd_adr
);
1515 printk(KERN_ERR
"%s: Chip not ready for buffer write. status = %lx, Xstatus = %lx\n",
1516 map
->name
, status
.x
[0], Xstatus
.x
[0]);
1522 /* Figure out the number of words to write */
1523 word_gap
= (-adr
& (map_bankwidth(map
)-1));
1524 words
= (len
- word_gap
+ map_bankwidth(map
) - 1) / map_bankwidth(map
);
1528 word_gap
= map_bankwidth(map
) - word_gap
;
1530 datum
= map_word_ff(map
);
1533 /* Write length of data to come */
1534 map_write(map
, CMD(words
), cmd_adr
);
1538 vec_seek
= *pvec_seek
;
1540 int n
= map_bankwidth(map
) - word_gap
;
1541 if (n
> vec
->iov_len
- vec_seek
)
1542 n
= vec
->iov_len
- vec_seek
;
1546 if (!word_gap
&& len
< map_bankwidth(map
))
1547 datum
= map_word_ff(map
);
1549 datum
= map_word_load_partial(map
, datum
,
1550 vec
->iov_base
+ vec_seek
,
1555 if (!len
|| word_gap
== map_bankwidth(map
)) {
1556 map_write(map
, datum
, adr
);
1557 adr
+= map_bankwidth(map
);
1562 if (vec_seek
== vec
->iov_len
) {
1568 *pvec_seek
= vec_seek
;
1571 map_write(map
, CMD(0xd0), cmd_adr
);
1572 chip
->state
= FL_WRITING
;
1574 INVALIDATE_CACHE_UDELAY(map
, chip
, cmd_adr
,
1576 chip
->buffer_write_time
);
1578 timeo
= jiffies
+ (HZ
/2);
1581 if (chip
->state
!= FL_WRITING
) {
1582 /* Someone's suspended the write. Sleep */
1583 DECLARE_WAITQUEUE(wait
, current
);
1584 set_current_state(TASK_UNINTERRUPTIBLE
);
1585 add_wait_queue(&chip
->wq
, &wait
);
1586 spin_unlock(chip
->mutex
);
1588 remove_wait_queue(&chip
->wq
, &wait
);
1589 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1590 spin_lock(chip
->mutex
);
1594 status
= map_read(map
, cmd_adr
);
1595 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1598 /* OK Still waiting */
1599 if (time_after(jiffies
, timeo
)) {
1600 map_write(map
, CMD(0x70), cmd_adr
);
1601 chip
->state
= FL_STATUS
;
1602 xip_enable(map
, chip
, cmd_adr
);
1603 printk(KERN_ERR
"%s: buffer write error (status timeout)\n", map
->name
);
1608 /* Latency issues. Drop the lock, wait a while and retry */
1610 UDELAY(map
, chip
, cmd_adr
, 1);
1613 chip
->buffer_write_time
--;
1614 if (!chip
->buffer_write_time
)
1615 chip
->buffer_write_time
= 1;
1618 chip
->buffer_write_time
++;
1620 /* Done and happy. */
1621 chip
->state
= FL_STATUS
;
1623 /* check for errors */
1624 if (map_word_bitsset(map
, status
, CMD(0x1a))) {
1625 unsigned long chipstatus
= MERGESTATUS(status
);
1628 map_write(map
, CMD(0x50), cmd_adr
);
1629 map_write(map
, CMD(0x70), cmd_adr
);
1630 xip_enable(map
, chip
, cmd_adr
);
1632 if (chipstatus
& 0x02) {
1634 } else if (chipstatus
& 0x08) {
1635 printk(KERN_ERR
"%s: buffer write error (bad VPP)\n", map
->name
);
1638 printk(KERN_ERR
"%s: buffer write error (status 0x%lx)\n", map
->name
, chipstatus
);
1645 xip_enable(map
, chip
, cmd_adr
);
1646 out
: put_chip(map
, chip
, cmd_adr
);
1647 spin_unlock(chip
->mutex
);
1651 static int cfi_intelext_writev (struct mtd_info
*mtd
, const struct kvec
*vecs
,
1652 unsigned long count
, loff_t to
, size_t *retlen
)
1654 struct map_info
*map
= mtd
->priv
;
1655 struct cfi_private
*cfi
= map
->fldrv_priv
;
1656 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1659 unsigned long ofs
, vec_seek
, i
;
1662 for (i
= 0; i
< count
; i
++)
1663 len
+= vecs
[i
].iov_len
;
1669 chipnum
= to
>> cfi
->chipshift
;
1670 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1674 /* We must not cross write block boundaries */
1675 int size
= wbufsize
- (ofs
& (wbufsize
-1));
1679 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
1680 ofs
, &vecs
, &vec_seek
, size
);
1688 if (ofs
>> cfi
->chipshift
) {
1691 if (chipnum
== cfi
->numchips
)
1699 static int cfi_intelext_write_buffers (struct mtd_info
*mtd
, loff_t to
,
1700 size_t len
, size_t *retlen
, const u_char
*buf
)
1704 vec
.iov_base
= (void *) buf
;
1707 return cfi_intelext_writev(mtd
, &vec
, 1, to
, retlen
);
1710 static int __xipram
do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
,
1711 unsigned long adr
, int len
, void *thunk
)
1713 struct cfi_private
*cfi
= map
->fldrv_priv
;
1714 map_word status
, status_OK
;
1715 unsigned long timeo
;
1717 DECLARE_WAITQUEUE(wait
, current
);
1722 /* Let's determine this according to the interleave only once */
1723 status_OK
= CMD(0x80);
1726 spin_lock(chip
->mutex
);
1727 ret
= get_chip(map
, chip
, adr
, FL_ERASING
);
1729 spin_unlock(chip
->mutex
);
1733 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1735 xip_disable(map
, chip
, adr
);
1737 /* Clear the status register first */
1738 map_write(map
, CMD(0x50), adr
);
1741 map_write(map
, CMD(0x20), adr
);
1742 map_write(map
, CMD(0xD0), adr
);
1743 chip
->state
= FL_ERASING
;
1744 chip
->erase_suspended
= 0;
1746 INVALIDATE_CACHE_UDELAY(map
, chip
, adr
,
1748 chip
->erase_time
*1000/2);
1750 /* FIXME. Use a timer to check this, and return immediately. */
1751 /* Once the state machine's known to be working I'll do that */
1753 timeo
= jiffies
+ (HZ
*20);
1755 if (chip
->state
!= FL_ERASING
) {
1756 /* Someone's suspended the erase. Sleep */
1757 set_current_state(TASK_UNINTERRUPTIBLE
);
1758 add_wait_queue(&chip
->wq
, &wait
);
1759 spin_unlock(chip
->mutex
);
1761 remove_wait_queue(&chip
->wq
, &wait
);
1762 spin_lock(chip
->mutex
);
1765 if (chip
->erase_suspended
) {
1766 /* This erase was suspended and resumed.
1767 Adjust the timeout */
1768 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1769 chip
->erase_suspended
= 0;
1772 status
= map_read(map
, adr
);
1773 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1776 /* OK Still waiting */
1777 if (time_after(jiffies
, timeo
)) {
1778 map_write(map
, CMD(0x70), adr
);
1779 chip
->state
= FL_STATUS
;
1780 xip_enable(map
, chip
, adr
);
1781 printk(KERN_ERR
"%s: block erase error: (status timeout)\n", map
->name
);
1786 /* Latency issues. Drop the lock, wait a while and retry */
1787 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1790 /* We've broken this before. It doesn't hurt to be safe */
1791 map_write(map
, CMD(0x70), adr
);
1792 chip
->state
= FL_STATUS
;
1793 status
= map_read(map
, adr
);
1795 /* check for errors */
1796 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
1797 unsigned long chipstatus
= MERGESTATUS(status
);
1799 /* Reset the error bits */
1800 map_write(map
, CMD(0x50), adr
);
1801 map_write(map
, CMD(0x70), adr
);
1802 xip_enable(map
, chip
, adr
);
1804 if ((chipstatus
& 0x30) == 0x30) {
1805 printk(KERN_ERR
"%s: block erase error: (bad command sequence, status 0x%lx)\n", map
->name
, chipstatus
);
1807 } else if (chipstatus
& 0x02) {
1808 /* Protection bit set */
1810 } else if (chipstatus
& 0x8) {
1812 printk(KERN_ERR
"%s: block erase error: (bad VPP)\n", map
->name
);
1814 } else if (chipstatus
& 0x20 && retries
--) {
1815 printk(KERN_DEBUG
"block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr
, chipstatus
);
1816 timeo
= jiffies
+ HZ
;
1817 put_chip(map
, chip
, adr
);
1818 spin_unlock(chip
->mutex
);
1821 printk(KERN_ERR
"%s: block erase failed at 0x%08lx (status 0x%lx)\n", map
->name
, adr
, chipstatus
);
1828 xip_enable(map
, chip
, adr
);
1829 out
: put_chip(map
, chip
, adr
);
1830 spin_unlock(chip
->mutex
);
1834 int cfi_intelext_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
1836 unsigned long ofs
, len
;
1842 ret
= cfi_varsize_frob(mtd
, do_erase_oneblock
, ofs
, len
, NULL
);
1846 instr
->state
= MTD_ERASE_DONE
;
1847 mtd_erase_callback(instr
);
1852 static void cfi_intelext_sync (struct mtd_info
*mtd
)
1854 struct map_info
*map
= mtd
->priv
;
1855 struct cfi_private
*cfi
= map
->fldrv_priv
;
1857 struct flchip
*chip
;
1860 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1861 chip
= &cfi
->chips
[i
];
1863 spin_lock(chip
->mutex
);
1864 ret
= get_chip(map
, chip
, chip
->start
, FL_SYNCING
);
1867 chip
->oldstate
= chip
->state
;
1868 chip
->state
= FL_SYNCING
;
1869 /* No need to wake_up() on this state change -
1870 * as the whole point is that nobody can do anything
1871 * with the chip now anyway.
1874 spin_unlock(chip
->mutex
);
1877 /* Unlock the chips again */
1879 for (i
--; i
>=0; i
--) {
1880 chip
= &cfi
->chips
[i
];
1882 spin_lock(chip
->mutex
);
1884 if (chip
->state
== FL_SYNCING
) {
1885 chip
->state
= chip
->oldstate
;
1886 chip
->oldstate
= FL_READY
;
1889 spin_unlock(chip
->mutex
);
1893 #ifdef DEBUG_LOCK_BITS
1894 static int __xipram
do_printlockstatus_oneblock(struct map_info
*map
,
1895 struct flchip
*chip
,
1897 int len
, void *thunk
)
1899 struct cfi_private
*cfi
= map
->fldrv_priv
;
1900 int status
, ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1903 xip_disable(map
, chip
, adr
+(2*ofs_factor
));
1904 map_write(map
, CMD(0x90), adr
+(2*ofs_factor
));
1905 chip
->state
= FL_JEDEC_QUERY
;
1906 status
= cfi_read_query(map
, adr
+(2*ofs_factor
));
1907 xip_enable(map
, chip
, 0);
1908 printk(KERN_DEBUG
"block status register for 0x%08lx is %x\n",
1914 #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1915 #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1917 static int __xipram
do_xxlock_oneblock(struct map_info
*map
, struct flchip
*chip
,
1918 unsigned long adr
, int len
, void *thunk
)
1920 struct cfi_private
*cfi
= map
->fldrv_priv
;
1921 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
1922 map_word status
, status_OK
;
1923 unsigned long timeo
= jiffies
+ HZ
;
1928 /* Let's determine this according to the interleave only once */
1929 status_OK
= CMD(0x80);
1931 spin_lock(chip
->mutex
);
1932 ret
= get_chip(map
, chip
, adr
, FL_LOCKING
);
1934 spin_unlock(chip
->mutex
);
1939 xip_disable(map
, chip
, adr
);
1941 map_write(map
, CMD(0x60), adr
);
1942 if (thunk
== DO_XXLOCK_ONEBLOCK_LOCK
) {
1943 map_write(map
, CMD(0x01), adr
);
1944 chip
->state
= FL_LOCKING
;
1945 } else if (thunk
== DO_XXLOCK_ONEBLOCK_UNLOCK
) {
1946 map_write(map
, CMD(0xD0), adr
);
1947 chip
->state
= FL_UNLOCKING
;
1952 * If Instant Individual Block Locking supported then no need
1956 if (!extp
|| !(extp
->FeatureSupport
& (1 << 5)))
1957 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1959 /* FIXME. Use a timer to check this, and return immediately. */
1960 /* Once the state machine's known to be working I'll do that */
1962 timeo
= jiffies
+ (HZ
*20);
1965 status
= map_read(map
, adr
);
1966 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1969 /* OK Still waiting */
1970 if (time_after(jiffies
, timeo
)) {
1971 map_write(map
, CMD(0x70), adr
);
1972 chip
->state
= FL_STATUS
;
1973 xip_enable(map
, chip
, adr
);
1974 printk(KERN_ERR
"%s: block unlock error: (status timeout)\n", map
->name
);
1975 put_chip(map
, chip
, adr
);
1976 spin_unlock(chip
->mutex
);
1980 /* Latency issues. Drop the lock, wait a while and retry */
1981 UDELAY(map
, chip
, adr
, 1);
1984 /* Done and happy. */
1985 chip
->state
= FL_STATUS
;
1986 xip_enable(map
, chip
, adr
);
1987 put_chip(map
, chip
, adr
);
1988 spin_unlock(chip
->mutex
);
1992 static int cfi_intelext_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1996 #ifdef DEBUG_LOCK_BITS
1997 printk(KERN_DEBUG
"%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
1998 __FUNCTION__
, ofs
, len
);
1999 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2003 ret
= cfi_varsize_frob(mtd
, do_xxlock_oneblock
,
2004 ofs
, len
, DO_XXLOCK_ONEBLOCK_LOCK
);
2006 #ifdef DEBUG_LOCK_BITS
2007 printk(KERN_DEBUG
"%s: lock status after, ret=%d\n",
2009 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2016 static int cfi_intelext_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
2020 #ifdef DEBUG_LOCK_BITS
2021 printk(KERN_DEBUG
"%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2022 __FUNCTION__
, ofs
, len
);
2023 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2027 ret
= cfi_varsize_frob(mtd
, do_xxlock_oneblock
,
2028 ofs
, len
, DO_XXLOCK_ONEBLOCK_UNLOCK
);
2030 #ifdef DEBUG_LOCK_BITS
2031 printk(KERN_DEBUG
"%s: lock status after, ret=%d\n",
2033 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2040 #ifdef CONFIG_MTD_OTP
2042 typedef int (*otp_op_t
)(struct map_info
*map
, struct flchip
*chip
,
2043 u_long data_offset
, u_char
*buf
, u_int size
,
2044 u_long prot_offset
, u_int groupno
, u_int groupsize
);
2047 do_otp_read(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2048 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2050 struct cfi_private
*cfi
= map
->fldrv_priv
;
2053 spin_lock(chip
->mutex
);
2054 ret
= get_chip(map
, chip
, chip
->start
, FL_JEDEC_QUERY
);
2056 spin_unlock(chip
->mutex
);
2060 /* let's ensure we're not reading back cached data from array mode */
2061 INVALIDATE_CACHED_RANGE(map
, chip
->start
+ offset
, size
);
2063 xip_disable(map
, chip
, chip
->start
);
2064 if (chip
->state
!= FL_JEDEC_QUERY
) {
2065 map_write(map
, CMD(0x90), chip
->start
);
2066 chip
->state
= FL_JEDEC_QUERY
;
2068 map_copy_from(map
, buf
, chip
->start
+ offset
, size
);
2069 xip_enable(map
, chip
, chip
->start
);
2071 /* then ensure we don't keep OTP data in the cache */
2072 INVALIDATE_CACHED_RANGE(map
, chip
->start
+ offset
, size
);
2074 put_chip(map
, chip
, chip
->start
);
2075 spin_unlock(chip
->mutex
);
2080 do_otp_write(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2081 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2086 unsigned long bus_ofs
= offset
& ~(map_bankwidth(map
)-1);
2087 int gap
= offset
- bus_ofs
;
2088 int n
= min_t(int, size
, map_bankwidth(map
)-gap
);
2089 map_word datum
= map_word_ff(map
);
2091 datum
= map_word_load_partial(map
, datum
, buf
, gap
, n
);
2092 ret
= do_write_oneword(map
, chip
, bus_ofs
, datum
, FL_OTP_WRITE
);
2105 do_otp_lock(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2106 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2108 struct cfi_private
*cfi
= map
->fldrv_priv
;
2111 /* make sure area matches group boundaries */
2115 datum
= map_word_ff(map
);
2116 datum
= map_word_clr(map
, datum
, CMD(1 << grpno
));
2117 return do_write_oneword(map
, chip
, prot
, datum
, FL_OTP_WRITE
);
2120 static int cfi_intelext_otp_walk(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2121 size_t *retlen
, u_char
*buf
,
2122 otp_op_t action
, int user_regs
)
2124 struct map_info
*map
= mtd
->priv
;
2125 struct cfi_private
*cfi
= map
->fldrv_priv
;
2126 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
2127 struct flchip
*chip
;
2128 struct cfi_intelext_otpinfo
*otp
;
2129 u_long devsize
, reg_prot_offset
, data_offset
;
2130 u_int chip_num
, chip_step
, field
, reg_fact_size
, reg_user_size
;
2131 u_int groups
, groupno
, groupsize
, reg_fact_groups
, reg_user_groups
;
2136 /* Check that we actually have some OTP registers */
2137 if (!extp
|| !(extp
->FeatureSupport
& 64) || !extp
->NumProtectionFields
)
2140 /* we need real chips here not virtual ones */
2141 devsize
= (1 << cfi
->cfiq
->DevSize
) * cfi
->interleave
;
2142 chip_step
= devsize
>> cfi
->chipshift
;
2145 /* Some chips have OTP located in the _top_ partition only.
2146 For example: Intel 28F256L18T (T means top-parameter device) */
2147 if (cfi
->mfr
== MANUFACTURER_INTEL
) {
2152 chip_num
= chip_step
- 1;
2156 for ( ; chip_num
< cfi
->numchips
; chip_num
+= chip_step
) {
2157 chip
= &cfi
->chips
[chip_num
];
2158 otp
= (struct cfi_intelext_otpinfo
*)&extp
->extra
[0];
2160 /* first OTP region */
2162 reg_prot_offset
= extp
->ProtRegAddr
;
2163 reg_fact_groups
= 1;
2164 reg_fact_size
= 1 << extp
->FactProtRegSize
;
2165 reg_user_groups
= 1;
2166 reg_user_size
= 1 << extp
->UserProtRegSize
;
2169 /* flash geometry fixup */
2170 data_offset
= reg_prot_offset
+ 1;
2171 data_offset
*= cfi
->interleave
* cfi
->device_type
;
2172 reg_prot_offset
*= cfi
->interleave
* cfi
->device_type
;
2173 reg_fact_size
*= cfi
->interleave
;
2174 reg_user_size
*= cfi
->interleave
;
2177 groups
= reg_user_groups
;
2178 groupsize
= reg_user_size
;
2179 /* skip over factory reg area */
2180 groupno
= reg_fact_groups
;
2181 data_offset
+= reg_fact_groups
* reg_fact_size
;
2183 groups
= reg_fact_groups
;
2184 groupsize
= reg_fact_size
;
2188 while (len
> 0 && groups
> 0) {
2191 * Special case: if action is NULL
2192 * we fill buf with otp_info records.
2194 struct otp_info
*otpinfo
;
2196 len
-= sizeof(struct otp_info
);
2199 ret
= do_otp_read(map
, chip
,
2201 (u_char
*)&lockword
,
2206 otpinfo
= (struct otp_info
*)buf
;
2207 otpinfo
->start
= from
;
2208 otpinfo
->length
= groupsize
;
2210 !map_word_bitsset(map
, lockword
,
2213 buf
+= sizeof(*otpinfo
);
2214 *retlen
+= sizeof(*otpinfo
);
2215 } else if (from
>= groupsize
) {
2217 data_offset
+= groupsize
;
2219 int size
= groupsize
;
2220 data_offset
+= from
;
2225 ret
= action(map
, chip
, data_offset
,
2226 buf
, size
, reg_prot_offset
,
2227 groupno
, groupsize
);
2233 data_offset
+= size
;
2239 /* next OTP region */
2240 if (++field
== extp
->NumProtectionFields
)
2242 reg_prot_offset
= otp
->ProtRegAddr
;
2243 reg_fact_groups
= otp
->FactGroups
;
2244 reg_fact_size
= 1 << otp
->FactProtRegSize
;
2245 reg_user_groups
= otp
->UserGroups
;
2246 reg_user_size
= 1 << otp
->UserProtRegSize
;
2254 static int cfi_intelext_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2255 size_t len
, size_t *retlen
,
2258 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2259 buf
, do_otp_read
, 0);
2262 static int cfi_intelext_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2263 size_t len
, size_t *retlen
,
2266 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2267 buf
, do_otp_read
, 1);
2270 static int cfi_intelext_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2271 size_t len
, size_t *retlen
,
2274 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2275 buf
, do_otp_write
, 1);
2278 static int cfi_intelext_lock_user_prot_reg(struct mtd_info
*mtd
,
2279 loff_t from
, size_t len
)
2282 return cfi_intelext_otp_walk(mtd
, from
, len
, &retlen
,
2283 NULL
, do_otp_lock
, 1);
2286 static int cfi_intelext_get_fact_prot_info(struct mtd_info
*mtd
,
2287 struct otp_info
*buf
, size_t len
)
2292 ret
= cfi_intelext_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*)buf
, NULL
, 0);
2293 return ret
? : retlen
;
2296 static int cfi_intelext_get_user_prot_info(struct mtd_info
*mtd
,
2297 struct otp_info
*buf
, size_t len
)
2302 ret
= cfi_intelext_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*)buf
, NULL
, 1);
2303 return ret
? : retlen
;
2308 static int cfi_intelext_suspend(struct mtd_info
*mtd
)
2310 struct map_info
*map
= mtd
->priv
;
2311 struct cfi_private
*cfi
= map
->fldrv_priv
;
2313 struct flchip
*chip
;
2316 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
2317 chip
= &cfi
->chips
[i
];
2319 spin_lock(chip
->mutex
);
2321 switch (chip
->state
) {
2325 case FL_JEDEC_QUERY
:
2326 if (chip
->oldstate
== FL_READY
) {
2327 chip
->oldstate
= chip
->state
;
2328 chip
->state
= FL_PM_SUSPENDED
;
2329 /* No need to wake_up() on this state change -
2330 * as the whole point is that nobody can do anything
2331 * with the chip now anyway.
2334 /* There seems to be an operation pending. We must wait for it. */
2335 printk(KERN_NOTICE
"Flash device refused suspend due to pending operation (oldstate %d)\n", chip
->oldstate
);
2340 /* Should we actually wait? Once upon a time these routines weren't
2341 allowed to. Or should we return -EAGAIN, because the upper layers
2342 ought to have already shut down anything which was using the device
2343 anyway? The latter for now. */
2344 printk(KERN_NOTICE
"Flash device refused suspend due to active operation (state %d)\n", chip
->oldstate
);
2346 case FL_PM_SUSPENDED
:
2349 spin_unlock(chip
->mutex
);
2352 /* Unlock the chips again */
2355 for (i
--; i
>=0; i
--) {
2356 chip
= &cfi
->chips
[i
];
2358 spin_lock(chip
->mutex
);
2360 if (chip
->state
== FL_PM_SUSPENDED
) {
2361 /* No need to force it into a known state here,
2362 because we're returning failure, and it didn't
2364 chip
->state
= chip
->oldstate
;
2365 chip
->oldstate
= FL_READY
;
2368 spin_unlock(chip
->mutex
);
2375 static void cfi_intelext_resume(struct mtd_info
*mtd
)
2377 struct map_info
*map
= mtd
->priv
;
2378 struct cfi_private
*cfi
= map
->fldrv_priv
;
2380 struct flchip
*chip
;
2382 for (i
=0; i
<cfi
->numchips
; i
++) {
2384 chip
= &cfi
->chips
[i
];
2386 spin_lock(chip
->mutex
);
2388 /* Go to known state. Chip may have been power cycled */
2389 if (chip
->state
== FL_PM_SUSPENDED
) {
2390 map_write(map
, CMD(0xFF), cfi
->chips
[i
].start
);
2391 chip
->oldstate
= chip
->state
= FL_READY
;
2395 spin_unlock(chip
->mutex
);
2399 static int cfi_intelext_reset(struct mtd_info
*mtd
)
2401 struct map_info
*map
= mtd
->priv
;
2402 struct cfi_private
*cfi
= map
->fldrv_priv
;
2405 for (i
=0; i
< cfi
->numchips
; i
++) {
2406 struct flchip
*chip
= &cfi
->chips
[i
];
2408 /* force the completion of any ongoing operation
2409 and switch to array mode so any bootloader in
2410 flash is accessible for soft reboot. */
2411 spin_lock(chip
->mutex
);
2412 ret
= get_chip(map
, chip
, chip
->start
, FL_SYNCING
);
2414 map_write(map
, CMD(0xff), chip
->start
);
2415 chip
->state
= FL_READY
;
2417 spin_unlock(chip
->mutex
);
2423 static int cfi_intelext_reboot(struct notifier_block
*nb
, unsigned long val
,
2426 struct mtd_info
*mtd
;
2428 mtd
= container_of(nb
, struct mtd_info
, reboot_notifier
);
2429 cfi_intelext_reset(mtd
);
2433 static void cfi_intelext_destroy(struct mtd_info
*mtd
)
2435 struct map_info
*map
= mtd
->priv
;
2436 struct cfi_private
*cfi
= map
->fldrv_priv
;
2437 cfi_intelext_reset(mtd
);
2438 unregister_reboot_notifier(&mtd
->reboot_notifier
);
2439 kfree(cfi
->cmdset_priv
);
2441 kfree(cfi
->chips
[0].priv
);
2443 kfree(mtd
->eraseregions
);
2446 MODULE_LICENSE("GPL");
2447 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2448 MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
2449 MODULE_ALIAS("cfi_cmdset_0003");
2450 MODULE_ALIAS("cfi_cmdset_0200");