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 /* This routine is made available to other mtd code via
335 * inter_module_register. It must only be accessed through
336 * inter_module_get which will bump the use count of this module. The
337 * addresses passed back in cfi are valid as long as the use count of
338 * this module is non-zero, i.e. between inter_module_get and
339 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
341 struct mtd_info
*cfi_cmdset_0001(struct map_info
*map
, int primary
)
343 struct cfi_private
*cfi
= map
->fldrv_priv
;
344 struct mtd_info
*mtd
;
347 mtd
= kmalloc(sizeof(*mtd
), GFP_KERNEL
);
349 printk(KERN_ERR
"Failed to allocate memory for MTD device\n");
352 memset(mtd
, 0, sizeof(*mtd
));
354 mtd
->type
= MTD_NORFLASH
;
356 /* Fill in the default mtd operations */
357 mtd
->erase
= cfi_intelext_erase_varsize
;
358 mtd
->read
= cfi_intelext_read
;
359 mtd
->write
= cfi_intelext_write_words
;
360 mtd
->sync
= cfi_intelext_sync
;
361 mtd
->lock
= cfi_intelext_lock
;
362 mtd
->unlock
= cfi_intelext_unlock
;
363 mtd
->suspend
= cfi_intelext_suspend
;
364 mtd
->resume
= cfi_intelext_resume
;
365 mtd
->flags
= MTD_CAP_NORFLASH
;
366 mtd
->name
= map
->name
;
368 mtd
->reboot_notifier
.notifier_call
= cfi_intelext_reboot
;
370 if (cfi
->cfi_mode
== CFI_MODE_CFI
) {
372 * It's a real CFI chip, not one for which the probe
373 * routine faked a CFI structure. So we read the feature
376 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
377 struct cfi_pri_intelext
*extp
;
379 extp
= read_pri_intelext(map
, adr
);
385 /* Install our own private info structure */
386 cfi
->cmdset_priv
= extp
;
388 cfi_fixup(mtd
, cfi_fixup_table
);
390 #ifdef DEBUG_CFI_FEATURES
391 /* Tell the user about it in lots of lovely detail */
392 cfi_tell_features(extp
);
395 if(extp
->SuspendCmdSupport
& 1) {
396 printk(KERN_NOTICE
"cfi_cmdset_0001: Erase suspend on write enabled\n");
399 else if (cfi
->cfi_mode
== CFI_MODE_JEDEC
) {
400 /* Apply jedec specific fixups */
401 cfi_fixup(mtd
, jedec_fixup_table
);
403 /* Apply generic fixups */
404 cfi_fixup(mtd
, fixup_table
);
406 for (i
=0; i
< cfi
->numchips
; i
++) {
407 cfi
->chips
[i
].word_write_time
= 1<<cfi
->cfiq
->WordWriteTimeoutTyp
;
408 cfi
->chips
[i
].buffer_write_time
= 1<<cfi
->cfiq
->BufWriteTimeoutTyp
;
409 cfi
->chips
[i
].erase_time
= 1<<cfi
->cfiq
->BlockEraseTimeoutTyp
;
410 cfi
->chips
[i
].ref_point_counter
= 0;
411 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
414 map
->fldrv
= &cfi_intelext_chipdrv
;
416 return cfi_intelext_setup(mtd
);
419 static struct mtd_info
*cfi_intelext_setup(struct mtd_info
*mtd
)
421 struct map_info
*map
= mtd
->priv
;
422 struct cfi_private
*cfi
= map
->fldrv_priv
;
423 unsigned long offset
= 0;
425 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
427 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
429 mtd
->size
= devsize
* cfi
->numchips
;
431 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
432 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
433 * mtd
->numeraseregions
, GFP_KERNEL
);
434 if (!mtd
->eraseregions
) {
435 printk(KERN_ERR
"Failed to allocate memory for MTD erase region info\n");
439 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
440 unsigned long ernum
, ersize
;
441 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
442 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
444 if (mtd
->erasesize
< ersize
) {
445 mtd
->erasesize
= ersize
;
447 for (j
=0; j
<cfi
->numchips
; j
++) {
448 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
449 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
450 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
452 offset
+= (ersize
* ernum
);
455 if (offset
!= devsize
) {
457 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
461 for (i
=0; i
<mtd
->numeraseregions
;i
++){
462 printk(KERN_DEBUG
"erase region %d: offset=0x%x,size=0x%x,blocks=%d\n",
463 i
,mtd
->eraseregions
[i
].offset
,
464 mtd
->eraseregions
[i
].erasesize
,
465 mtd
->eraseregions
[i
].numblocks
);
468 #ifdef CONFIG_MTD_OTP
469 mtd
->read_fact_prot_reg
= cfi_intelext_read_fact_prot_reg
;
470 mtd
->read_user_prot_reg
= cfi_intelext_read_user_prot_reg
;
471 mtd
->write_user_prot_reg
= cfi_intelext_write_user_prot_reg
;
472 mtd
->lock_user_prot_reg
= cfi_intelext_lock_user_prot_reg
;
473 mtd
->get_fact_prot_info
= cfi_intelext_get_fact_prot_info
;
474 mtd
->get_user_prot_info
= cfi_intelext_get_user_prot_info
;
477 /* This function has the potential to distort the reality
478 a bit and therefore should be called last. */
479 if (cfi_intelext_partition_fixup(mtd
, &cfi
) != 0)
482 __module_get(THIS_MODULE
);
483 register_reboot_notifier(&mtd
->reboot_notifier
);
488 kfree(mtd
->eraseregions
);
491 kfree(cfi
->cmdset_priv
);
495 static int cfi_intelext_partition_fixup(struct mtd_info
*mtd
,
496 struct cfi_private
**pcfi
)
498 struct map_info
*map
= mtd
->priv
;
499 struct cfi_private
*cfi
= *pcfi
;
500 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
503 * Probing of multi-partition flash ships.
505 * To support multiple partitions when available, we simply arrange
506 * for each of them to have their own flchip structure even if they
507 * are on the same physical chip. This means completely recreating
508 * a new cfi_private structure right here which is a blatent code
509 * layering violation, but this is still the least intrusive
510 * arrangement at this point. This can be rearranged in the future
511 * if someone feels motivated enough. --nico
513 if (extp
&& extp
->MajorVersion
== '1' && extp
->MinorVersion
>= '3'
514 && extp
->FeatureSupport
& (1 << 9)) {
515 struct cfi_private
*newcfi
;
517 struct flchip_shared
*shared
;
518 int offs
, numregions
, numparts
, partshift
, numvirtchips
, i
, j
;
520 /* Protection Register info */
521 offs
= (extp
->NumProtectionFields
- 1) *
522 sizeof(struct cfi_intelext_otpinfo
);
524 /* Burst Read info */
525 offs
+= extp
->extra
[offs
+1]+2;
527 /* Number of partition regions */
528 numregions
= extp
->extra
[offs
];
531 /* skip the sizeof(partregion) field in CFI 1.4 */
532 if (extp
->MinorVersion
>= '4')
535 /* Number of hardware partitions */
537 for (i
= 0; i
< numregions
; i
++) {
538 struct cfi_intelext_regioninfo
*rinfo
;
539 rinfo
= (struct cfi_intelext_regioninfo
*)&extp
->extra
[offs
];
540 numparts
+= rinfo
->NumIdentPartitions
;
541 offs
+= sizeof(*rinfo
)
542 + (rinfo
->NumBlockTypes
- 1) *
543 sizeof(struct cfi_intelext_blockinfo
);
546 /* Programming Region info */
547 if (extp
->MinorVersion
>= '4') {
548 struct cfi_intelext_programming_regioninfo
*prinfo
;
549 prinfo
= (struct cfi_intelext_programming_regioninfo
*)&extp
->extra
[offs
];
550 MTD_PROGREGION_SIZE(mtd
) = cfi
->interleave
<< prinfo
->ProgRegShift
;
551 MTD_PROGREGION_CTRLMODE_VALID(mtd
) = cfi
->interleave
* prinfo
->ControlValid
;
552 MTD_PROGREGION_CTRLMODE_INVALID(mtd
) = cfi
->interleave
* prinfo
->ControlInvalid
;
553 mtd
->flags
|= MTD_PROGRAM_REGIONS
;
554 printk(KERN_DEBUG
"%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
555 map
->name
, MTD_PROGREGION_SIZE(mtd
),
556 MTD_PROGREGION_CTRLMODE_VALID(mtd
),
557 MTD_PROGREGION_CTRLMODE_INVALID(mtd
));
561 * All functions below currently rely on all chips having
562 * the same geometry so we'll just assume that all hardware
563 * partitions are of the same size too.
565 partshift
= cfi
->chipshift
- __ffs(numparts
);
567 if ((1 << partshift
) < mtd
->erasesize
) {
569 "%s: bad number of hw partitions (%d)\n",
570 __FUNCTION__
, numparts
);
574 numvirtchips
= cfi
->numchips
* numparts
;
575 newcfi
= kmalloc(sizeof(struct cfi_private
) + numvirtchips
* sizeof(struct flchip
), GFP_KERNEL
);
578 shared
= kmalloc(sizeof(struct flchip_shared
) * cfi
->numchips
, GFP_KERNEL
);
583 memcpy(newcfi
, cfi
, sizeof(struct cfi_private
));
584 newcfi
->numchips
= numvirtchips
;
585 newcfi
->chipshift
= partshift
;
587 chip
= &newcfi
->chips
[0];
588 for (i
= 0; i
< cfi
->numchips
; i
++) {
589 shared
[i
].writing
= shared
[i
].erasing
= NULL
;
590 spin_lock_init(&shared
[i
].lock
);
591 for (j
= 0; j
< numparts
; j
++) {
592 *chip
= cfi
->chips
[i
];
593 chip
->start
+= j
<< partshift
;
594 chip
->priv
= &shared
[i
];
595 /* those should be reset too since
596 they create memory references. */
597 init_waitqueue_head(&chip
->wq
);
598 spin_lock_init(&chip
->_spinlock
);
599 chip
->mutex
= &chip
->_spinlock
;
604 printk(KERN_DEBUG
"%s: %d set(s) of %d interleaved chips "
605 "--> %d partitions of %d KiB\n",
606 map
->name
, cfi
->numchips
, cfi
->interleave
,
607 newcfi
->numchips
, 1<<(newcfi
->chipshift
-10));
609 map
->fldrv_priv
= newcfi
;
618 * *********** CHIP ACCESS FUNCTIONS ***********
621 static int get_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
, int mode
)
623 DECLARE_WAITQUEUE(wait
, current
);
624 struct cfi_private
*cfi
= map
->fldrv_priv
;
625 map_word status
, status_OK
= CMD(0x80), status_PWS
= CMD(0x01);
627 struct cfi_pri_intelext
*cfip
= cfi
->cmdset_priv
;
630 timeo
= jiffies
+ HZ
;
632 if (chip
->priv
&& (mode
== FL_WRITING
|| mode
== FL_ERASING
|| mode
== FL_OTP_WRITE
)) {
634 * OK. We have possibility for contension on the write/erase
635 * operations which are global to the real chip and not per
636 * partition. So let's fight it over in the partition which
637 * currently has authority on the operation.
639 * The rules are as follows:
641 * - any write operation must own shared->writing.
643 * - any erase operation must own _both_ shared->writing and
646 * - contension arbitration is handled in the owner's context.
648 * The 'shared' struct can be read and/or written only when
651 struct flchip_shared
*shared
= chip
->priv
;
652 struct flchip
*contender
;
653 spin_lock(&shared
->lock
);
654 contender
= shared
->writing
;
655 if (contender
&& contender
!= chip
) {
657 * The engine to perform desired operation on this
658 * partition is already in use by someone else.
659 * Let's fight over it in the context of the chip
660 * currently using it. If it is possible to suspend,
661 * that other partition will do just that, otherwise
662 * it'll happily send us to sleep. In any case, when
663 * get_chip returns success we're clear to go ahead.
665 int ret
= spin_trylock(contender
->mutex
);
666 spin_unlock(&shared
->lock
);
669 spin_unlock(chip
->mutex
);
670 ret
= get_chip(map
, contender
, contender
->start
, mode
);
671 spin_lock(chip
->mutex
);
673 spin_unlock(contender
->mutex
);
676 timeo
= jiffies
+ HZ
;
677 spin_lock(&shared
->lock
);
678 spin_unlock(contender
->mutex
);
682 shared
->writing
= chip
;
683 if (mode
== FL_ERASING
)
684 shared
->erasing
= chip
;
685 spin_unlock(&shared
->lock
);
688 switch (chip
->state
) {
692 status
= map_read(map
, adr
);
693 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
696 /* At this point we're fine with write operations
697 in other partitions as they don't conflict. */
698 if (chip
->priv
&& map_word_andequal(map
, status
, status_PWS
, status_PWS
))
701 if (time_after(jiffies
, timeo
)) {
702 printk(KERN_ERR
"%s: Waiting for chip to be ready timed out. Status %lx\n",
703 map
->name
, status
.x
[0]);
706 spin_unlock(chip
->mutex
);
708 spin_lock(chip
->mutex
);
709 /* Someone else might have been playing with it. */
720 !(cfip
->FeatureSupport
& 2) ||
721 !(mode
== FL_READY
|| mode
== FL_POINT
||
722 (mode
== FL_WRITING
&& (cfip
->SuspendCmdSupport
& 1))))
727 map_write(map
, CMD(0xB0), adr
);
729 /* If the flash has finished erasing, then 'erase suspend'
730 * appears to make some (28F320) flash devices switch to
731 * 'read' mode. Make sure that we switch to 'read status'
732 * mode so we get the right data. --rmk
734 map_write(map
, CMD(0x70), adr
);
735 chip
->oldstate
= FL_ERASING
;
736 chip
->state
= FL_ERASE_SUSPENDING
;
737 chip
->erase_suspended
= 1;
739 status
= map_read(map
, adr
);
740 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
743 if (time_after(jiffies
, timeo
)) {
744 /* Urgh. Resume and pretend we weren't here. */
745 map_write(map
, CMD(0xd0), adr
);
746 /* Make sure we're in 'read status' mode if it had finished */
747 map_write(map
, CMD(0x70), adr
);
748 chip
->state
= FL_ERASING
;
749 chip
->oldstate
= FL_READY
;
750 printk(KERN_ERR
"%s: Chip not ready after erase "
751 "suspended: status = 0x%lx\n", map
->name
, status
.x
[0]);
755 spin_unlock(chip
->mutex
);
757 spin_lock(chip
->mutex
);
758 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
759 So we can just loop here. */
761 chip
->state
= FL_STATUS
;
764 case FL_XIP_WHILE_ERASING
:
765 if (mode
!= FL_READY
&& mode
!= FL_POINT
&&
766 (mode
!= FL_WRITING
|| !cfip
|| !(cfip
->SuspendCmdSupport
&1)))
768 chip
->oldstate
= chip
->state
;
769 chip
->state
= FL_READY
;
773 /* Only if there's no operation suspended... */
774 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
779 set_current_state(TASK_UNINTERRUPTIBLE
);
780 add_wait_queue(&chip
->wq
, &wait
);
781 spin_unlock(chip
->mutex
);
783 remove_wait_queue(&chip
->wq
, &wait
);
784 spin_lock(chip
->mutex
);
789 static void put_chip(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
791 struct cfi_private
*cfi
= map
->fldrv_priv
;
794 struct flchip_shared
*shared
= chip
->priv
;
795 spin_lock(&shared
->lock
);
796 if (shared
->writing
== chip
&& chip
->oldstate
== FL_READY
) {
797 /* We own the ability to write, but we're done */
798 shared
->writing
= shared
->erasing
;
799 if (shared
->writing
&& shared
->writing
!= chip
) {
800 /* give back ownership to who we loaned it from */
801 struct flchip
*loaner
= shared
->writing
;
802 spin_lock(loaner
->mutex
);
803 spin_unlock(&shared
->lock
);
804 spin_unlock(chip
->mutex
);
805 put_chip(map
, loaner
, loaner
->start
);
806 spin_lock(chip
->mutex
);
807 spin_unlock(loaner
->mutex
);
811 shared
->erasing
= NULL
;
812 shared
->writing
= NULL
;
813 } else if (shared
->erasing
== chip
&& shared
->writing
!= chip
) {
815 * We own the ability to erase without the ability
816 * to write, which means the erase was suspended
817 * and some other partition is currently writing.
818 * Don't let the switch below mess things up since
819 * we don't have ownership to resume anything.
821 spin_unlock(&shared
->lock
);
825 spin_unlock(&shared
->lock
);
828 switch(chip
->oldstate
) {
830 chip
->state
= chip
->oldstate
;
831 /* What if one interleaved chip has finished and the
832 other hasn't? The old code would leave the finished
833 one in READY mode. That's bad, and caused -EROFS
834 errors to be returned from do_erase_oneblock because
835 that's the only bit it checked for at the time.
836 As the state machine appears to explicitly allow
837 sending the 0x70 (Read Status) command to an erasing
838 chip and expecting it to be ignored, that's what we
840 map_write(map
, CMD(0xd0), adr
);
841 map_write(map
, CMD(0x70), adr
);
842 chip
->oldstate
= FL_READY
;
843 chip
->state
= FL_ERASING
;
846 case FL_XIP_WHILE_ERASING
:
847 chip
->state
= chip
->oldstate
;
848 chip
->oldstate
= FL_READY
;
854 /* We should really make set_vpp() count, rather than doing this */
858 printk(KERN_ERR
"%s: put_chip() called with oldstate %d!!\n", map
->name
, chip
->oldstate
);
863 #ifdef CONFIG_MTD_XIP
866 * No interrupt what so ever can be serviced while the flash isn't in array
867 * mode. This is ensured by the xip_disable() and xip_enable() functions
868 * enclosing any code path where the flash is known not to be in array mode.
869 * And within a XIP disabled code path, only functions marked with __xipram
870 * may be called and nothing else (it's a good thing to inspect generated
871 * assembly to make sure inline functions were actually inlined and that gcc
872 * didn't emit calls to its own support functions). Also configuring MTD CFI
873 * support to a single buswidth and a single interleave is also recommended.
876 static void xip_disable(struct map_info
*map
, struct flchip
*chip
,
879 /* TODO: chips with no XIP use should ignore and return */
880 (void) map_read(map
, adr
); /* ensure mmu mapping is up to date */
884 static void __xipram
xip_enable(struct map_info
*map
, struct flchip
*chip
,
887 struct cfi_private
*cfi
= map
->fldrv_priv
;
888 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
889 map_write(map
, CMD(0xff), adr
);
890 chip
->state
= FL_READY
;
892 (void) map_read(map
, adr
);
898 * When a delay is required for the flash operation to complete, the
899 * xip_udelay() function is polling for both the given timeout and pending
900 * (but still masked) hardware interrupts. Whenever there is an interrupt
901 * pending then the flash erase or write operation is suspended, array mode
902 * restored and interrupts unmasked. Task scheduling might also happen at that
903 * point. The CPU eventually returns from the interrupt or the call to
904 * schedule() and the suspended flash operation is resumed for the remaining
905 * of the delay period.
907 * Warning: this function _will_ fool interrupt latency tracing tools.
910 static void __xipram
xip_udelay(struct map_info
*map
, struct flchip
*chip
,
911 unsigned long adr
, int usec
)
913 struct cfi_private
*cfi
= map
->fldrv_priv
;
914 struct cfi_pri_intelext
*cfip
= cfi
->cmdset_priv
;
915 map_word status
, OK
= CMD(0x80);
916 unsigned long suspended
, start
= xip_currtime();
917 flstate_t oldstate
, newstate
;
921 if (xip_irqpending() && cfip
&&
922 ((chip
->state
== FL_ERASING
&& (cfip
->FeatureSupport
&2)) ||
923 (chip
->state
== FL_WRITING
&& (cfip
->FeatureSupport
&4))) &&
924 (cfi_interleave_is_1(cfi
) || chip
->oldstate
== FL_READY
)) {
926 * Let's suspend the erase or write operation when
927 * supported. Note that we currently don't try to
928 * suspend interleaved chips if there is already
929 * another operation suspended (imagine what happens
930 * when one chip was already done with the current
931 * operation while another chip suspended it, then
932 * we resume the whole thing at once). Yes, it
935 map_write(map
, CMD(0xb0), adr
);
936 map_write(map
, CMD(0x70), adr
);
937 usec
-= xip_elapsed_since(start
);
938 suspended
= xip_currtime();
940 if (xip_elapsed_since(suspended
) > 100000) {
942 * The chip doesn't want to suspend
943 * after waiting for 100 msecs.
944 * This is a critical error but there
945 * is not much we can do here.
949 status
= map_read(map
, adr
);
950 } while (!map_word_andequal(map
, status
, OK
, OK
));
952 /* Suspend succeeded */
953 oldstate
= chip
->state
;
954 if (oldstate
== FL_ERASING
) {
955 if (!map_word_bitsset(map
, status
, CMD(0x40)))
957 newstate
= FL_XIP_WHILE_ERASING
;
958 chip
->erase_suspended
= 1;
960 if (!map_word_bitsset(map
, status
, CMD(0x04)))
962 newstate
= FL_XIP_WHILE_WRITING
;
963 chip
->write_suspended
= 1;
965 chip
->state
= newstate
;
966 map_write(map
, CMD(0xff), adr
);
967 (void) map_read(map
, adr
);
968 asm volatile (".rep 8; nop; .endr");
970 spin_unlock(chip
->mutex
);
971 asm volatile (".rep 8; nop; .endr");
975 * We're back. However someone else might have
976 * decided to go write to the chip if we are in
977 * a suspended erase state. If so let's wait
980 spin_lock(chip
->mutex
);
981 while (chip
->state
!= newstate
) {
982 DECLARE_WAITQUEUE(wait
, current
);
983 set_current_state(TASK_UNINTERRUPTIBLE
);
984 add_wait_queue(&chip
->wq
, &wait
);
985 spin_unlock(chip
->mutex
);
987 remove_wait_queue(&chip
->wq
, &wait
);
988 spin_lock(chip
->mutex
);
990 /* Disallow XIP again */
993 /* Resume the write or erase operation */
994 map_write(map
, CMD(0xd0), adr
);
995 map_write(map
, CMD(0x70), adr
);
996 chip
->state
= oldstate
;
997 start
= xip_currtime();
998 } else if (usec
>= 1000000/HZ
) {
1000 * Try to save on CPU power when waiting delay
1001 * is at least a system timer tick period.
1002 * No need to be extremely accurate here.
1006 status
= map_read(map
, adr
);
1007 } while (!map_word_andequal(map
, status
, OK
, OK
)
1008 && xip_elapsed_since(start
) < usec
);
1011 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1014 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1015 * the flash is actively programming or erasing since we have to poll for
1016 * the operation to complete anyway. We can't do that in a generic way with
1017 * a XIP setup so do it before the actual flash operation in this case
1018 * and stub it out from INVALIDATE_CACHE_UDELAY.
1020 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
1021 INVALIDATE_CACHED_RANGE(map, from, size)
1023 #define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
1024 UDELAY(map, chip, cmd_adr, usec)
1029 * Activating this XIP support changes the way the code works a bit. For
1030 * example the code to suspend the current process when concurrent access
1031 * happens is never executed because xip_udelay() will always return with the
1032 * same chip state as it was entered with. This is why there is no care for
1033 * the presence of add_wait_queue() or schedule() calls from within a couple
1034 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1035 * The queueing and scheduling are always happening within xip_udelay().
1037 * Similarly, get_chip() and put_chip() just happen to always be executed
1038 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1039 * is in array mode, therefore never executing many cases therein and not
1040 * causing any problem with XIP.
1045 #define xip_disable(map, chip, adr)
1046 #define xip_enable(map, chip, adr)
1047 #define XIP_INVAL_CACHED_RANGE(x...)
1049 #define UDELAY(map, chip, adr, usec) \
1051 spin_unlock(chip->mutex); \
1053 spin_lock(chip->mutex); \
1056 #define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
1058 spin_unlock(chip->mutex); \
1059 INVALIDATE_CACHED_RANGE(map, adr, len); \
1061 spin_lock(chip->mutex); \
1066 static int do_point_onechip (struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
)
1068 unsigned long cmd_addr
;
1069 struct cfi_private
*cfi
= map
->fldrv_priv
;
1074 /* Ensure cmd read/writes are aligned. */
1075 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
1077 spin_lock(chip
->mutex
);
1079 ret
= get_chip(map
, chip
, cmd_addr
, FL_POINT
);
1082 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
)
1083 map_write(map
, CMD(0xff), cmd_addr
);
1085 chip
->state
= FL_POINT
;
1086 chip
->ref_point_counter
++;
1088 spin_unlock(chip
->mutex
);
1093 static int cfi_intelext_point (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
**mtdbuf
)
1095 struct map_info
*map
= mtd
->priv
;
1096 struct cfi_private
*cfi
= map
->fldrv_priv
;
1101 if (!map
->virt
|| (from
+ len
> mtd
->size
))
1104 *mtdbuf
= (void *)map
->virt
+ from
;
1107 /* Now lock the chip(s) to POINT state */
1109 /* ofs: offset within the first chip that the first read should start */
1110 chipnum
= (from
>> cfi
->chipshift
);
1111 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1114 unsigned long thislen
;
1116 if (chipnum
>= cfi
->numchips
)
1119 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1120 thislen
= (1<<cfi
->chipshift
) - ofs
;
1124 ret
= do_point_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
);
1137 static void cfi_intelext_unpoint (struct mtd_info
*mtd
, u_char
*addr
, loff_t from
, size_t len
)
1139 struct map_info
*map
= mtd
->priv
;
1140 struct cfi_private
*cfi
= map
->fldrv_priv
;
1144 /* Now unlock the chip(s) POINT state */
1146 /* ofs: offset within the first chip that the first read should start */
1147 chipnum
= (from
>> cfi
->chipshift
);
1148 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1151 unsigned long thislen
;
1152 struct flchip
*chip
;
1154 chip
= &cfi
->chips
[chipnum
];
1155 if (chipnum
>= cfi
->numchips
)
1158 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1159 thislen
= (1<<cfi
->chipshift
) - ofs
;
1163 spin_lock(chip
->mutex
);
1164 if (chip
->state
== FL_POINT
) {
1165 chip
->ref_point_counter
--;
1166 if(chip
->ref_point_counter
== 0)
1167 chip
->state
= FL_READY
;
1169 printk(KERN_ERR
"%s: Warning: unpoint called on non pointed region\n", map
->name
); /* Should this give an error? */
1171 put_chip(map
, chip
, chip
->start
);
1172 spin_unlock(chip
->mutex
);
1180 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
1182 unsigned long cmd_addr
;
1183 struct cfi_private
*cfi
= map
->fldrv_priv
;
1188 /* Ensure cmd read/writes are aligned. */
1189 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
1191 spin_lock(chip
->mutex
);
1192 ret
= get_chip(map
, chip
, cmd_addr
, FL_READY
);
1194 spin_unlock(chip
->mutex
);
1198 if (chip
->state
!= FL_POINT
&& chip
->state
!= FL_READY
) {
1199 map_write(map
, CMD(0xff), cmd_addr
);
1201 chip
->state
= FL_READY
;
1204 map_copy_from(map
, buf
, adr
, len
);
1206 put_chip(map
, chip
, cmd_addr
);
1208 spin_unlock(chip
->mutex
);
1212 static int cfi_intelext_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
1214 struct map_info
*map
= mtd
->priv
;
1215 struct cfi_private
*cfi
= map
->fldrv_priv
;
1220 /* ofs: offset within the first chip that the first read should start */
1221 chipnum
= (from
>> cfi
->chipshift
);
1222 ofs
= from
- (chipnum
<< cfi
->chipshift
);
1227 unsigned long thislen
;
1229 if (chipnum
>= cfi
->numchips
)
1232 if ((len
+ ofs
-1) >> cfi
->chipshift
)
1233 thislen
= (1<<cfi
->chipshift
) - ofs
;
1237 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
1251 static int __xipram
do_write_oneword(struct map_info
*map
, struct flchip
*chip
,
1252 unsigned long adr
, map_word datum
, int mode
)
1254 struct cfi_private
*cfi
= map
->fldrv_priv
;
1255 map_word status
, status_OK
, write_cmd
;
1256 unsigned long timeo
;
1261 /* Let's determine those according to the interleave only once */
1262 status_OK
= CMD(0x80);
1265 write_cmd
= (cfi
->cfiq
->P_ID
!= 0x0200) ? CMD(0x40) : CMD(0x41);
1268 write_cmd
= CMD(0xc0);
1274 spin_lock(chip
->mutex
);
1275 ret
= get_chip(map
, chip
, adr
, mode
);
1277 spin_unlock(chip
->mutex
);
1281 XIP_INVAL_CACHED_RANGE(map
, adr
, map_bankwidth(map
));
1283 xip_disable(map
, chip
, adr
);
1284 map_write(map
, write_cmd
, adr
);
1285 map_write(map
, datum
, adr
);
1288 INVALIDATE_CACHE_UDELAY(map
, chip
, adr
,
1289 adr
, map_bankwidth(map
),
1290 chip
->word_write_time
);
1292 timeo
= jiffies
+ (HZ
/2);
1295 if (chip
->state
!= mode
) {
1296 /* Someone's suspended the write. Sleep */
1297 DECLARE_WAITQUEUE(wait
, current
);
1299 set_current_state(TASK_UNINTERRUPTIBLE
);
1300 add_wait_queue(&chip
->wq
, &wait
);
1301 spin_unlock(chip
->mutex
);
1303 remove_wait_queue(&chip
->wq
, &wait
);
1304 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1305 spin_lock(chip
->mutex
);
1309 status
= map_read(map
, adr
);
1310 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1313 /* OK Still waiting */
1314 if (time_after(jiffies
, timeo
)) {
1315 map_write(map
, CMD(0x70), adr
);
1316 chip
->state
= FL_STATUS
;
1317 xip_enable(map
, chip
, adr
);
1318 printk(KERN_ERR
"%s: word write error (status timeout)\n", map
->name
);
1323 /* Latency issues. Drop the lock, wait a while and retry */
1325 UDELAY(map
, chip
, adr
, 1);
1328 chip
->word_write_time
--;
1329 if (!chip
->word_write_time
)
1330 chip
->word_write_time
= 1;
1333 chip
->word_write_time
++;
1335 /* Done and happy. */
1336 chip
->state
= FL_STATUS
;
1338 /* check for errors */
1339 if (map_word_bitsset(map
, status
, CMD(0x1a))) {
1340 unsigned long chipstatus
= MERGESTATUS(status
);
1343 map_write(map
, CMD(0x50), adr
);
1344 map_write(map
, CMD(0x70), adr
);
1345 xip_enable(map
, chip
, adr
);
1347 if (chipstatus
& 0x02) {
1349 } else if (chipstatus
& 0x08) {
1350 printk(KERN_ERR
"%s: word write error (bad VPP)\n", map
->name
);
1353 printk(KERN_ERR
"%s: word write error (status 0x%lx)\n", map
->name
, chipstatus
);
1360 xip_enable(map
, chip
, adr
);
1361 out
: put_chip(map
, chip
, adr
);
1362 spin_unlock(chip
->mutex
);
1367 static int cfi_intelext_write_words (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
, const u_char
*buf
)
1369 struct map_info
*map
= mtd
->priv
;
1370 struct cfi_private
*cfi
= map
->fldrv_priv
;
1379 chipnum
= to
>> cfi
->chipshift
;
1380 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1382 /* If it's not bus-aligned, do the first byte write */
1383 if (ofs
& (map_bankwidth(map
)-1)) {
1384 unsigned long bus_ofs
= ofs
& ~(map_bankwidth(map
)-1);
1385 int gap
= ofs
- bus_ofs
;
1389 n
= min_t(int, len
, map_bankwidth(map
)-gap
);
1390 datum
= map_word_ff(map
);
1391 datum
= map_word_load_partial(map
, datum
, buf
, gap
, n
);
1393 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1394 bus_ofs
, datum
, FL_WRITING
);
1403 if (ofs
>> cfi
->chipshift
) {
1406 if (chipnum
== cfi
->numchips
)
1411 while(len
>= map_bankwidth(map
)) {
1412 map_word datum
= map_word_load(map
, buf
);
1414 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1415 ofs
, datum
, FL_WRITING
);
1419 ofs
+= map_bankwidth(map
);
1420 buf
+= map_bankwidth(map
);
1421 (*retlen
) += map_bankwidth(map
);
1422 len
-= map_bankwidth(map
);
1424 if (ofs
>> cfi
->chipshift
) {
1427 if (chipnum
== cfi
->numchips
)
1432 if (len
& (map_bankwidth(map
)-1)) {
1435 datum
= map_word_ff(map
);
1436 datum
= map_word_load_partial(map
, datum
, buf
, 0, len
);
1438 ret
= do_write_oneword(map
, &cfi
->chips
[chipnum
],
1439 ofs
, datum
, FL_WRITING
);
1450 static int __xipram
do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
1451 unsigned long adr
, const struct kvec
**pvec
,
1452 unsigned long *pvec_seek
, int len
)
1454 struct cfi_private
*cfi
= map
->fldrv_priv
;
1455 map_word status
, status_OK
, write_cmd
, datum
;
1456 unsigned long cmd_adr
, timeo
;
1457 int wbufsize
, z
, ret
=0, word_gap
, words
;
1458 const struct kvec
*vec
;
1459 unsigned long vec_seek
;
1461 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1463 cmd_adr
= adr
& ~(wbufsize
-1);
1465 /* Let's determine this according to the interleave only once */
1466 status_OK
= CMD(0x80);
1467 write_cmd
= (cfi
->cfiq
->P_ID
!= 0x0200) ? CMD(0xe8) : CMD(0xe9);
1469 spin_lock(chip
->mutex
);
1470 ret
= get_chip(map
, chip
, cmd_adr
, FL_WRITING
);
1472 spin_unlock(chip
->mutex
);
1476 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1478 xip_disable(map
, chip
, cmd_adr
);
1480 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
1481 [...], the device will not accept any more Write to Buffer commands".
1482 So we must check here and reset those bits if they're set. Otherwise
1483 we're just pissing in the wind */
1484 if (chip
->state
!= FL_STATUS
)
1485 map_write(map
, CMD(0x70), cmd_adr
);
1486 status
= map_read(map
, cmd_adr
);
1487 if (map_word_bitsset(map
, status
, CMD(0x30))) {
1488 xip_enable(map
, chip
, cmd_adr
);
1489 printk(KERN_WARNING
"SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status
.x
[0]);
1490 xip_disable(map
, chip
, cmd_adr
);
1491 map_write(map
, CMD(0x50), cmd_adr
);
1492 map_write(map
, CMD(0x70), cmd_adr
);
1495 chip
->state
= FL_WRITING_TO_BUFFER
;
1499 map_write(map
, write_cmd
, cmd_adr
);
1501 status
= map_read(map
, cmd_adr
);
1502 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1505 UDELAY(map
, chip
, cmd_adr
, 1);
1508 /* Argh. Not ready for write to buffer */
1510 map_write(map
, CMD(0x70), cmd_adr
);
1511 chip
->state
= FL_STATUS
;
1512 Xstatus
= map_read(map
, cmd_adr
);
1513 /* Odd. Clear status bits */
1514 map_write(map
, CMD(0x50), cmd_adr
);
1515 map_write(map
, CMD(0x70), cmd_adr
);
1516 xip_enable(map
, chip
, cmd_adr
);
1517 printk(KERN_ERR
"%s: Chip not ready for buffer write. status = %lx, Xstatus = %lx\n",
1518 map
->name
, status
.x
[0], Xstatus
.x
[0]);
1524 /* Figure out the number of words to write */
1525 word_gap
= (-adr
& (map_bankwidth(map
)-1));
1526 words
= (len
- word_gap
+ map_bankwidth(map
) - 1) / map_bankwidth(map
);
1530 word_gap
= map_bankwidth(map
) - word_gap
;
1532 datum
= map_word_ff(map
);
1535 /* Write length of data to come */
1536 map_write(map
, CMD(words
), cmd_adr
);
1540 vec_seek
= *pvec_seek
;
1542 int n
= map_bankwidth(map
) - word_gap
;
1543 if (n
> vec
->iov_len
- vec_seek
)
1544 n
= vec
->iov_len
- vec_seek
;
1548 if (!word_gap
&& len
< map_bankwidth(map
))
1549 datum
= map_word_ff(map
);
1551 datum
= map_word_load_partial(map
, datum
,
1552 vec
->iov_base
+ vec_seek
,
1557 if (!len
|| word_gap
== map_bankwidth(map
)) {
1558 map_write(map
, datum
, adr
);
1559 adr
+= map_bankwidth(map
);
1564 if (vec_seek
== vec
->iov_len
) {
1570 *pvec_seek
= vec_seek
;
1573 map_write(map
, CMD(0xd0), cmd_adr
);
1574 chip
->state
= FL_WRITING
;
1576 INVALIDATE_CACHE_UDELAY(map
, chip
, cmd_adr
,
1578 chip
->buffer_write_time
);
1580 timeo
= jiffies
+ (HZ
/2);
1583 if (chip
->state
!= FL_WRITING
) {
1584 /* Someone's suspended the write. Sleep */
1585 DECLARE_WAITQUEUE(wait
, current
);
1586 set_current_state(TASK_UNINTERRUPTIBLE
);
1587 add_wait_queue(&chip
->wq
, &wait
);
1588 spin_unlock(chip
->mutex
);
1590 remove_wait_queue(&chip
->wq
, &wait
);
1591 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
1592 spin_lock(chip
->mutex
);
1596 status
= map_read(map
, cmd_adr
);
1597 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1600 /* OK Still waiting */
1601 if (time_after(jiffies
, timeo
)) {
1602 map_write(map
, CMD(0x70), cmd_adr
);
1603 chip
->state
= FL_STATUS
;
1604 xip_enable(map
, chip
, cmd_adr
);
1605 printk(KERN_ERR
"%s: buffer write error (status timeout)\n", map
->name
);
1610 /* Latency issues. Drop the lock, wait a while and retry */
1612 UDELAY(map
, chip
, cmd_adr
, 1);
1615 chip
->buffer_write_time
--;
1616 if (!chip
->buffer_write_time
)
1617 chip
->buffer_write_time
= 1;
1620 chip
->buffer_write_time
++;
1622 /* Done and happy. */
1623 chip
->state
= FL_STATUS
;
1625 /* check for errors */
1626 if (map_word_bitsset(map
, status
, CMD(0x1a))) {
1627 unsigned long chipstatus
= MERGESTATUS(status
);
1630 map_write(map
, CMD(0x50), cmd_adr
);
1631 map_write(map
, CMD(0x70), cmd_adr
);
1632 xip_enable(map
, chip
, cmd_adr
);
1634 if (chipstatus
& 0x02) {
1636 } else if (chipstatus
& 0x08) {
1637 printk(KERN_ERR
"%s: buffer write error (bad VPP)\n", map
->name
);
1640 printk(KERN_ERR
"%s: buffer write error (status 0x%lx)\n", map
->name
, chipstatus
);
1647 xip_enable(map
, chip
, cmd_adr
);
1648 out
: put_chip(map
, chip
, cmd_adr
);
1649 spin_unlock(chip
->mutex
);
1653 static int cfi_intelext_writev (struct mtd_info
*mtd
, const struct kvec
*vecs
,
1654 unsigned long count
, loff_t to
, size_t *retlen
)
1656 struct map_info
*map
= mtd
->priv
;
1657 struct cfi_private
*cfi
= map
->fldrv_priv
;
1658 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
1661 unsigned long ofs
, vec_seek
, i
;
1664 for (i
= 0; i
< count
; i
++)
1665 len
+= vecs
[i
].iov_len
;
1671 chipnum
= to
>> cfi
->chipshift
;
1672 ofs
= to
- (chipnum
<< cfi
->chipshift
);
1676 /* We must not cross write block boundaries */
1677 int size
= wbufsize
- (ofs
& (wbufsize
-1));
1681 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
1682 ofs
, &vecs
, &vec_seek
, size
);
1690 if (ofs
>> cfi
->chipshift
) {
1693 if (chipnum
== cfi
->numchips
)
1701 static int cfi_intelext_write_buffers (struct mtd_info
*mtd
, loff_t to
,
1702 size_t len
, size_t *retlen
, const u_char
*buf
)
1706 vec
.iov_base
= (void *) buf
;
1709 return cfi_intelext_writev(mtd
, &vec
, 1, to
, retlen
);
1712 static int __xipram
do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
,
1713 unsigned long adr
, int len
, void *thunk
)
1715 struct cfi_private
*cfi
= map
->fldrv_priv
;
1716 map_word status
, status_OK
;
1717 unsigned long timeo
;
1719 DECLARE_WAITQUEUE(wait
, current
);
1724 /* Let's determine this according to the interleave only once */
1725 status_OK
= CMD(0x80);
1728 spin_lock(chip
->mutex
);
1729 ret
= get_chip(map
, chip
, adr
, FL_ERASING
);
1731 spin_unlock(chip
->mutex
);
1735 XIP_INVAL_CACHED_RANGE(map
, adr
, len
);
1737 xip_disable(map
, chip
, adr
);
1739 /* Clear the status register first */
1740 map_write(map
, CMD(0x50), adr
);
1743 map_write(map
, CMD(0x20), adr
);
1744 map_write(map
, CMD(0xD0), adr
);
1745 chip
->state
= FL_ERASING
;
1746 chip
->erase_suspended
= 0;
1748 INVALIDATE_CACHE_UDELAY(map
, chip
, adr
,
1750 chip
->erase_time
*1000/2);
1752 /* FIXME. Use a timer to check this, and return immediately. */
1753 /* Once the state machine's known to be working I'll do that */
1755 timeo
= jiffies
+ (HZ
*20);
1757 if (chip
->state
!= FL_ERASING
) {
1758 /* Someone's suspended the erase. Sleep */
1759 set_current_state(TASK_UNINTERRUPTIBLE
);
1760 add_wait_queue(&chip
->wq
, &wait
);
1761 spin_unlock(chip
->mutex
);
1763 remove_wait_queue(&chip
->wq
, &wait
);
1764 spin_lock(chip
->mutex
);
1767 if (chip
->erase_suspended
) {
1768 /* This erase was suspended and resumed.
1769 Adjust the timeout */
1770 timeo
= jiffies
+ (HZ
*20); /* FIXME */
1771 chip
->erase_suspended
= 0;
1774 status
= map_read(map
, adr
);
1775 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1778 /* OK Still waiting */
1779 if (time_after(jiffies
, timeo
)) {
1780 map_write(map
, CMD(0x70), adr
);
1781 chip
->state
= FL_STATUS
;
1782 xip_enable(map
, chip
, adr
);
1783 printk(KERN_ERR
"%s: block erase error: (status timeout)\n", map
->name
);
1788 /* Latency issues. Drop the lock, wait a while and retry */
1789 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1792 /* We've broken this before. It doesn't hurt to be safe */
1793 map_write(map
, CMD(0x70), adr
);
1794 chip
->state
= FL_STATUS
;
1795 status
= map_read(map
, adr
);
1797 /* check for errors */
1798 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
1799 unsigned long chipstatus
= MERGESTATUS(status
);
1801 /* Reset the error bits */
1802 map_write(map
, CMD(0x50), adr
);
1803 map_write(map
, CMD(0x70), adr
);
1804 xip_enable(map
, chip
, adr
);
1806 if ((chipstatus
& 0x30) == 0x30) {
1807 printk(KERN_ERR
"%s: block erase error: (bad command sequence, status 0x%lx)\n", map
->name
, chipstatus
);
1809 } else if (chipstatus
& 0x02) {
1810 /* Protection bit set */
1812 } else if (chipstatus
& 0x8) {
1814 printk(KERN_ERR
"%s: block erase error: (bad VPP)\n", map
->name
);
1816 } else if (chipstatus
& 0x20 && retries
--) {
1817 printk(KERN_DEBUG
"block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr
, chipstatus
);
1818 timeo
= jiffies
+ HZ
;
1819 put_chip(map
, chip
, adr
);
1820 spin_unlock(chip
->mutex
);
1823 printk(KERN_ERR
"%s: block erase failed at 0x%08lx (status 0x%lx)\n", map
->name
, adr
, chipstatus
);
1830 xip_enable(map
, chip
, adr
);
1831 out
: put_chip(map
, chip
, adr
);
1832 spin_unlock(chip
->mutex
);
1836 int cfi_intelext_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
1838 unsigned long ofs
, len
;
1844 ret
= cfi_varsize_frob(mtd
, do_erase_oneblock
, ofs
, len
, NULL
);
1848 instr
->state
= MTD_ERASE_DONE
;
1849 mtd_erase_callback(instr
);
1854 static void cfi_intelext_sync (struct mtd_info
*mtd
)
1856 struct map_info
*map
= mtd
->priv
;
1857 struct cfi_private
*cfi
= map
->fldrv_priv
;
1859 struct flchip
*chip
;
1862 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1863 chip
= &cfi
->chips
[i
];
1865 spin_lock(chip
->mutex
);
1866 ret
= get_chip(map
, chip
, chip
->start
, FL_SYNCING
);
1869 chip
->oldstate
= chip
->state
;
1870 chip
->state
= FL_SYNCING
;
1871 /* No need to wake_up() on this state change -
1872 * as the whole point is that nobody can do anything
1873 * with the chip now anyway.
1876 spin_unlock(chip
->mutex
);
1879 /* Unlock the chips again */
1881 for (i
--; i
>=0; i
--) {
1882 chip
= &cfi
->chips
[i
];
1884 spin_lock(chip
->mutex
);
1886 if (chip
->state
== FL_SYNCING
) {
1887 chip
->state
= chip
->oldstate
;
1888 chip
->oldstate
= FL_READY
;
1891 spin_unlock(chip
->mutex
);
1895 #ifdef DEBUG_LOCK_BITS
1896 static int __xipram
do_printlockstatus_oneblock(struct map_info
*map
,
1897 struct flchip
*chip
,
1899 int len
, void *thunk
)
1901 struct cfi_private
*cfi
= map
->fldrv_priv
;
1902 int status
, ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1905 xip_disable(map
, chip
, adr
+(2*ofs_factor
));
1906 map_write(map
, CMD(0x90), adr
+(2*ofs_factor
));
1907 chip
->state
= FL_JEDEC_QUERY
;
1908 status
= cfi_read_query(map
, adr
+(2*ofs_factor
));
1909 xip_enable(map
, chip
, 0);
1910 printk(KERN_DEBUG
"block status register for 0x%08lx is %x\n",
1916 #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1917 #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1919 static int __xipram
do_xxlock_oneblock(struct map_info
*map
, struct flchip
*chip
,
1920 unsigned long adr
, int len
, void *thunk
)
1922 struct cfi_private
*cfi
= map
->fldrv_priv
;
1923 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
1924 map_word status
, status_OK
;
1925 unsigned long timeo
= jiffies
+ HZ
;
1930 /* Let's determine this according to the interleave only once */
1931 status_OK
= CMD(0x80);
1933 spin_lock(chip
->mutex
);
1934 ret
= get_chip(map
, chip
, adr
, FL_LOCKING
);
1936 spin_unlock(chip
->mutex
);
1941 xip_disable(map
, chip
, adr
);
1943 map_write(map
, CMD(0x60), adr
);
1944 if (thunk
== DO_XXLOCK_ONEBLOCK_LOCK
) {
1945 map_write(map
, CMD(0x01), adr
);
1946 chip
->state
= FL_LOCKING
;
1947 } else if (thunk
== DO_XXLOCK_ONEBLOCK_UNLOCK
) {
1948 map_write(map
, CMD(0xD0), adr
);
1949 chip
->state
= FL_UNLOCKING
;
1954 * If Instant Individual Block Locking supported then no need
1958 if (!extp
|| !(extp
->FeatureSupport
& (1 << 5)))
1959 UDELAY(map
, chip
, adr
, 1000000/HZ
);
1961 /* FIXME. Use a timer to check this, and return immediately. */
1962 /* Once the state machine's known to be working I'll do that */
1964 timeo
= jiffies
+ (HZ
*20);
1967 status
= map_read(map
, adr
);
1968 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1971 /* OK Still waiting */
1972 if (time_after(jiffies
, timeo
)) {
1973 map_write(map
, CMD(0x70), adr
);
1974 chip
->state
= FL_STATUS
;
1975 xip_enable(map
, chip
, adr
);
1976 printk(KERN_ERR
"%s: block unlock error: (status timeout)\n", map
->name
);
1977 put_chip(map
, chip
, adr
);
1978 spin_unlock(chip
->mutex
);
1982 /* Latency issues. Drop the lock, wait a while and retry */
1983 UDELAY(map
, chip
, adr
, 1);
1986 /* Done and happy. */
1987 chip
->state
= FL_STATUS
;
1988 xip_enable(map
, chip
, adr
);
1989 put_chip(map
, chip
, adr
);
1990 spin_unlock(chip
->mutex
);
1994 static int cfi_intelext_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1998 #ifdef DEBUG_LOCK_BITS
1999 printk(KERN_DEBUG
"%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2000 __FUNCTION__
, ofs
, len
);
2001 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2005 ret
= cfi_varsize_frob(mtd
, do_xxlock_oneblock
,
2006 ofs
, len
, DO_XXLOCK_ONEBLOCK_LOCK
);
2008 #ifdef DEBUG_LOCK_BITS
2009 printk(KERN_DEBUG
"%s: lock status after, ret=%d\n",
2011 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2018 static int cfi_intelext_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
2022 #ifdef DEBUG_LOCK_BITS
2023 printk(KERN_DEBUG
"%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2024 __FUNCTION__
, ofs
, len
);
2025 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2029 ret
= cfi_varsize_frob(mtd
, do_xxlock_oneblock
,
2030 ofs
, len
, DO_XXLOCK_ONEBLOCK_UNLOCK
);
2032 #ifdef DEBUG_LOCK_BITS
2033 printk(KERN_DEBUG
"%s: lock status after, ret=%d\n",
2035 cfi_varsize_frob(mtd
, do_printlockstatus_oneblock
,
2042 #ifdef CONFIG_MTD_OTP
2044 typedef int (*otp_op_t
)(struct map_info
*map
, struct flchip
*chip
,
2045 u_long data_offset
, u_char
*buf
, u_int size
,
2046 u_long prot_offset
, u_int groupno
, u_int groupsize
);
2049 do_otp_read(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2050 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2052 struct cfi_private
*cfi
= map
->fldrv_priv
;
2055 spin_lock(chip
->mutex
);
2056 ret
= get_chip(map
, chip
, chip
->start
, FL_JEDEC_QUERY
);
2058 spin_unlock(chip
->mutex
);
2062 /* let's ensure we're not reading back cached data from array mode */
2063 INVALIDATE_CACHED_RANGE(map
, chip
->start
+ offset
, size
);
2065 xip_disable(map
, chip
, chip
->start
);
2066 if (chip
->state
!= FL_JEDEC_QUERY
) {
2067 map_write(map
, CMD(0x90), chip
->start
);
2068 chip
->state
= FL_JEDEC_QUERY
;
2070 map_copy_from(map
, buf
, chip
->start
+ offset
, size
);
2071 xip_enable(map
, chip
, chip
->start
);
2073 /* then ensure we don't keep OTP data in the cache */
2074 INVALIDATE_CACHED_RANGE(map
, chip
->start
+ offset
, size
);
2076 put_chip(map
, chip
, chip
->start
);
2077 spin_unlock(chip
->mutex
);
2082 do_otp_write(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2083 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2088 unsigned long bus_ofs
= offset
& ~(map_bankwidth(map
)-1);
2089 int gap
= offset
- bus_ofs
;
2090 int n
= min_t(int, size
, map_bankwidth(map
)-gap
);
2091 map_word datum
= map_word_ff(map
);
2093 datum
= map_word_load_partial(map
, datum
, buf
, gap
, n
);
2094 ret
= do_write_oneword(map
, chip
, bus_ofs
, datum
, FL_OTP_WRITE
);
2107 do_otp_lock(struct map_info
*map
, struct flchip
*chip
, u_long offset
,
2108 u_char
*buf
, u_int size
, u_long prot
, u_int grpno
, u_int grpsz
)
2110 struct cfi_private
*cfi
= map
->fldrv_priv
;
2113 /* make sure area matches group boundaries */
2117 datum
= map_word_ff(map
);
2118 datum
= map_word_clr(map
, datum
, CMD(1 << grpno
));
2119 return do_write_oneword(map
, chip
, prot
, datum
, FL_OTP_WRITE
);
2122 static int cfi_intelext_otp_walk(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2123 size_t *retlen
, u_char
*buf
,
2124 otp_op_t action
, int user_regs
)
2126 struct map_info
*map
= mtd
->priv
;
2127 struct cfi_private
*cfi
= map
->fldrv_priv
;
2128 struct cfi_pri_intelext
*extp
= cfi
->cmdset_priv
;
2129 struct flchip
*chip
;
2130 struct cfi_intelext_otpinfo
*otp
;
2131 u_long devsize
, reg_prot_offset
, data_offset
;
2132 u_int chip_num
, chip_step
, field
, reg_fact_size
, reg_user_size
;
2133 u_int groups
, groupno
, groupsize
, reg_fact_groups
, reg_user_groups
;
2138 /* Check that we actually have some OTP registers */
2139 if (!extp
|| !(extp
->FeatureSupport
& 64) || !extp
->NumProtectionFields
)
2142 /* we need real chips here not virtual ones */
2143 devsize
= (1 << cfi
->cfiq
->DevSize
) * cfi
->interleave
;
2144 chip_step
= devsize
>> cfi
->chipshift
;
2147 /* Some chips have OTP located in the _top_ partition only.
2148 For example: Intel 28F256L18T (T means top-parameter device) */
2149 if (cfi
->mfr
== MANUFACTURER_INTEL
) {
2154 chip_num
= chip_step
- 1;
2158 for ( ; chip_num
< cfi
->numchips
; chip_num
+= chip_step
) {
2159 chip
= &cfi
->chips
[chip_num
];
2160 otp
= (struct cfi_intelext_otpinfo
*)&extp
->extra
[0];
2162 /* first OTP region */
2164 reg_prot_offset
= extp
->ProtRegAddr
;
2165 reg_fact_groups
= 1;
2166 reg_fact_size
= 1 << extp
->FactProtRegSize
;
2167 reg_user_groups
= 1;
2168 reg_user_size
= 1 << extp
->UserProtRegSize
;
2171 /* flash geometry fixup */
2172 data_offset
= reg_prot_offset
+ 1;
2173 data_offset
*= cfi
->interleave
* cfi
->device_type
;
2174 reg_prot_offset
*= cfi
->interleave
* cfi
->device_type
;
2175 reg_fact_size
*= cfi
->interleave
;
2176 reg_user_size
*= cfi
->interleave
;
2179 groups
= reg_user_groups
;
2180 groupsize
= reg_user_size
;
2181 /* skip over factory reg area */
2182 groupno
= reg_fact_groups
;
2183 data_offset
+= reg_fact_groups
* reg_fact_size
;
2185 groups
= reg_fact_groups
;
2186 groupsize
= reg_fact_size
;
2190 while (len
> 0 && groups
> 0) {
2193 * Special case: if action is NULL
2194 * we fill buf with otp_info records.
2196 struct otp_info
*otpinfo
;
2198 len
-= sizeof(struct otp_info
);
2201 ret
= do_otp_read(map
, chip
,
2203 (u_char
*)&lockword
,
2208 otpinfo
= (struct otp_info
*)buf
;
2209 otpinfo
->start
= from
;
2210 otpinfo
->length
= groupsize
;
2212 !map_word_bitsset(map
, lockword
,
2215 buf
+= sizeof(*otpinfo
);
2216 *retlen
+= sizeof(*otpinfo
);
2217 } else if (from
>= groupsize
) {
2219 data_offset
+= groupsize
;
2221 int size
= groupsize
;
2222 data_offset
+= from
;
2227 ret
= action(map
, chip
, data_offset
,
2228 buf
, size
, reg_prot_offset
,
2229 groupno
, groupsize
);
2235 data_offset
+= size
;
2241 /* next OTP region */
2242 if (++field
== extp
->NumProtectionFields
)
2244 reg_prot_offset
= otp
->ProtRegAddr
;
2245 reg_fact_groups
= otp
->FactGroups
;
2246 reg_fact_size
= 1 << otp
->FactProtRegSize
;
2247 reg_user_groups
= otp
->UserGroups
;
2248 reg_user_size
= 1 << otp
->UserProtRegSize
;
2256 static int cfi_intelext_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2257 size_t len
, size_t *retlen
,
2260 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2261 buf
, do_otp_read
, 0);
2264 static int cfi_intelext_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2265 size_t len
, size_t *retlen
,
2268 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2269 buf
, do_otp_read
, 1);
2272 static int cfi_intelext_write_user_prot_reg(struct mtd_info
*mtd
, loff_t from
,
2273 size_t len
, size_t *retlen
,
2276 return cfi_intelext_otp_walk(mtd
, from
, len
, retlen
,
2277 buf
, do_otp_write
, 1);
2280 static int cfi_intelext_lock_user_prot_reg(struct mtd_info
*mtd
,
2281 loff_t from
, size_t len
)
2284 return cfi_intelext_otp_walk(mtd
, from
, len
, &retlen
,
2285 NULL
, do_otp_lock
, 1);
2288 static int cfi_intelext_get_fact_prot_info(struct mtd_info
*mtd
,
2289 struct otp_info
*buf
, size_t len
)
2294 ret
= cfi_intelext_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*)buf
, NULL
, 0);
2295 return ret
? : retlen
;
2298 static int cfi_intelext_get_user_prot_info(struct mtd_info
*mtd
,
2299 struct otp_info
*buf
, size_t len
)
2304 ret
= cfi_intelext_otp_walk(mtd
, 0, len
, &retlen
, (u_char
*)buf
, NULL
, 1);
2305 return ret
? : retlen
;
2310 static int cfi_intelext_suspend(struct mtd_info
*mtd
)
2312 struct map_info
*map
= mtd
->priv
;
2313 struct cfi_private
*cfi
= map
->fldrv_priv
;
2315 struct flchip
*chip
;
2318 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
2319 chip
= &cfi
->chips
[i
];
2321 spin_lock(chip
->mutex
);
2323 switch (chip
->state
) {
2327 case FL_JEDEC_QUERY
:
2328 if (chip
->oldstate
== FL_READY
) {
2329 chip
->oldstate
= chip
->state
;
2330 chip
->state
= FL_PM_SUSPENDED
;
2331 /* No need to wake_up() on this state change -
2332 * as the whole point is that nobody can do anything
2333 * with the chip now anyway.
2336 /* There seems to be an operation pending. We must wait for it. */
2337 printk(KERN_NOTICE
"Flash device refused suspend due to pending operation (oldstate %d)\n", chip
->oldstate
);
2342 /* Should we actually wait? Once upon a time these routines weren't
2343 allowed to. Or should we return -EAGAIN, because the upper layers
2344 ought to have already shut down anything which was using the device
2345 anyway? The latter for now. */
2346 printk(KERN_NOTICE
"Flash device refused suspend due to active operation (state %d)\n", chip
->oldstate
);
2348 case FL_PM_SUSPENDED
:
2351 spin_unlock(chip
->mutex
);
2354 /* Unlock the chips again */
2357 for (i
--; i
>=0; i
--) {
2358 chip
= &cfi
->chips
[i
];
2360 spin_lock(chip
->mutex
);
2362 if (chip
->state
== FL_PM_SUSPENDED
) {
2363 /* No need to force it into a known state here,
2364 because we're returning failure, and it didn't
2366 chip
->state
= chip
->oldstate
;
2367 chip
->oldstate
= FL_READY
;
2370 spin_unlock(chip
->mutex
);
2377 static void cfi_intelext_resume(struct mtd_info
*mtd
)
2379 struct map_info
*map
= mtd
->priv
;
2380 struct cfi_private
*cfi
= map
->fldrv_priv
;
2382 struct flchip
*chip
;
2384 for (i
=0; i
<cfi
->numchips
; i
++) {
2386 chip
= &cfi
->chips
[i
];
2388 spin_lock(chip
->mutex
);
2390 /* Go to known state. Chip may have been power cycled */
2391 if (chip
->state
== FL_PM_SUSPENDED
) {
2392 map_write(map
, CMD(0xFF), cfi
->chips
[i
].start
);
2393 chip
->oldstate
= chip
->state
= FL_READY
;
2397 spin_unlock(chip
->mutex
);
2401 static int cfi_intelext_reset(struct mtd_info
*mtd
)
2403 struct map_info
*map
= mtd
->priv
;
2404 struct cfi_private
*cfi
= map
->fldrv_priv
;
2407 for (i
=0; i
< cfi
->numchips
; i
++) {
2408 struct flchip
*chip
= &cfi
->chips
[i
];
2410 /* force the completion of any ongoing operation
2411 and switch to array mode so any bootloader in
2412 flash is accessible for soft reboot. */
2413 spin_lock(chip
->mutex
);
2414 ret
= get_chip(map
, chip
, chip
->start
, FL_SYNCING
);
2416 map_write(map
, CMD(0xff), chip
->start
);
2417 chip
->state
= FL_READY
;
2419 spin_unlock(chip
->mutex
);
2425 static int cfi_intelext_reboot(struct notifier_block
*nb
, unsigned long val
,
2428 struct mtd_info
*mtd
;
2430 mtd
= container_of(nb
, struct mtd_info
, reboot_notifier
);
2431 cfi_intelext_reset(mtd
);
2435 static void cfi_intelext_destroy(struct mtd_info
*mtd
)
2437 struct map_info
*map
= mtd
->priv
;
2438 struct cfi_private
*cfi
= map
->fldrv_priv
;
2439 cfi_intelext_reset(mtd
);
2440 unregister_reboot_notifier(&mtd
->reboot_notifier
);
2441 kfree(cfi
->cmdset_priv
);
2443 kfree(cfi
->chips
[0].priv
);
2445 kfree(mtd
->eraseregions
);
2448 static char im_name_0001
[] = "cfi_cmdset_0001";
2449 static char im_name_0003
[] = "cfi_cmdset_0003";
2450 static char im_name_0200
[] = "cfi_cmdset_0200";
2452 static int __init
cfi_intelext_init(void)
2454 inter_module_register(im_name_0001
, THIS_MODULE
, &cfi_cmdset_0001
);
2455 inter_module_register(im_name_0003
, THIS_MODULE
, &cfi_cmdset_0001
);
2456 inter_module_register(im_name_0200
, THIS_MODULE
, &cfi_cmdset_0001
);
2460 static void __exit
cfi_intelext_exit(void)
2462 inter_module_unregister(im_name_0001
);
2463 inter_module_unregister(im_name_0003
);
2464 inter_module_unregister(im_name_0200
);
2467 module_init(cfi_intelext_init
);
2468 module_exit(cfi_intelext_exit
);
2470 MODULE_LICENSE("GPL");
2471 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2472 MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");