4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/delay.h>
24 #include <linux/types.h>
25 #include <linux/bug.h>
26 #include <linux/interrupt.h>
27 #include <linux/mtd/flashchip.h>
28 #include <linux/mtd/map.h>
29 #include <linux/mtd/cfi_endian.h>
30 #include <linux/mtd/xip.h>
32 #ifdef CONFIG_MTD_CFI_I1
33 #define cfi_interleave(cfi) 1
34 #define cfi_interleave_is_1(cfi) (cfi_interleave(cfi) == 1)
36 #define cfi_interleave_is_1(cfi) (0)
39 #ifdef CONFIG_MTD_CFI_I2
40 # ifdef cfi_interleave
41 # undef cfi_interleave
42 # define cfi_interleave(cfi) ((cfi)->interleave)
44 # define cfi_interleave(cfi) 2
46 #define cfi_interleave_is_2(cfi) (cfi_interleave(cfi) == 2)
48 #define cfi_interleave_is_2(cfi) (0)
51 #ifdef CONFIG_MTD_CFI_I4
52 # ifdef cfi_interleave
53 # undef cfi_interleave
54 # define cfi_interleave(cfi) ((cfi)->interleave)
56 # define cfi_interleave(cfi) 4
58 #define cfi_interleave_is_4(cfi) (cfi_interleave(cfi) == 4)
60 #define cfi_interleave_is_4(cfi) (0)
63 #ifdef CONFIG_MTD_CFI_I8
64 # ifdef cfi_interleave
65 # undef cfi_interleave
66 # define cfi_interleave(cfi) ((cfi)->interleave)
68 # define cfi_interleave(cfi) 8
70 #define cfi_interleave_is_8(cfi) (cfi_interleave(cfi) == 8)
72 #define cfi_interleave_is_8(cfi) (0)
75 #ifndef cfi_interleave
76 #warning No CONFIG_MTD_CFI_Ix selected. No NOR chip support can work.
77 static inline int cfi_interleave(void *cfi)
84 static inline int cfi_interleave_supported(int i)
87 #ifdef CONFIG_MTD_CFI_I1
90 #ifdef CONFIG_MTD_CFI_I2
93 #ifdef CONFIG_MTD_CFI_I4
96 #ifdef CONFIG_MTD_CFI_I8
107 /* NB: these values must represents the number of bytes needed to meet the
108 * device type (x8, x16, x32). Eg. a 32 bit device is 4 x 8 bytes.
109 * These numbers are used in calculations.
111 #define CFI_DEVICETYPE_X8 (8 / 8)
112 #define CFI_DEVICETYPE_X16 (16 / 8)
113 #define CFI_DEVICETYPE_X32 (32 / 8)
114 #define CFI_DEVICETYPE_X64 (64 / 8)
117 /* Device Interface Code Assignments from the "Common Flash Memory Interface
118 * Publication 100" dated December 1, 2001.
120 #define CFI_INTERFACE_X8_ASYNC 0x0000
121 #define CFI_INTERFACE_X16_ASYNC 0x0001
122 #define CFI_INTERFACE_X8_BY_X16_ASYNC 0x0002
123 #define CFI_INTERFACE_X32_ASYNC 0x0003
124 #define CFI_INTERFACE_X16_BY_X32_ASYNC 0x0005
125 #define CFI_INTERFACE_NOT_ALLOWED 0xffff
128 /* NB: We keep these structures in memory in HOST byteorder, except
129 * where individually noted.
132 /* Basic Query Structure */
143 uint8_t WordWriteTimeoutTyp;
144 uint8_t BufWriteTimeoutTyp;
145 uint8_t BlockEraseTimeoutTyp;
146 uint8_t ChipEraseTimeoutTyp;
147 uint8_t WordWriteTimeoutMax;
148 uint8_t BufWriteTimeoutMax;
149 uint8_t BlockEraseTimeoutMax;
150 uint8_t ChipEraseTimeoutMax;
152 uint16_t InterfaceDesc;
153 uint16_t MaxBufWriteSize;
154 uint8_t NumEraseRegions;
155 uint32_t EraseRegionInfo[0]; /* Not host ordered */
156 } __attribute__((packed));
158 /* Extended Query Structure for both PRI and ALT */
160 struct cfi_extquery {
162 uint8_t MajorVersion;
163 uint8_t MinorVersion;
164 } __attribute__((packed));
166 /* Vendor-Specific PRI for Intel/Sharp Extended Command Set (0x0001) */
168 struct cfi_pri_intelext {
170 uint8_t MajorVersion;
171 uint8_t MinorVersion;
172 uint32_t FeatureSupport; /* if bit 31 is set then an additional uint32_t feature
173 block follows - FIXME - not currently supported */
174 uint8_t SuspendCmdSupport;
175 uint16_t BlkStatusRegMask;
178 uint8_t NumProtectionFields;
179 uint16_t ProtRegAddr;
180 uint8_t FactProtRegSize;
181 uint8_t UserProtRegSize;
183 } __attribute__((packed));
185 struct cfi_intelext_otpinfo {
186 uint32_t ProtRegAddr;
188 uint8_t FactProtRegSize;
190 uint8_t UserProtRegSize;
191 } __attribute__((packed));
193 struct cfi_intelext_blockinfo {
194 uint16_t NumIdentBlocks;
196 uint16_t MinBlockEraseCycles;
199 } __attribute__((packed));
201 struct cfi_intelext_regioninfo {
202 uint16_t NumIdentPartitions;
203 uint8_t NumOpAllowed;
204 uint8_t NumOpAllowedSimProgMode;
205 uint8_t NumOpAllowedSimEraMode;
206 uint8_t NumBlockTypes;
207 struct cfi_intelext_blockinfo BlockTypes[1];
208 } __attribute__((packed));
210 struct cfi_intelext_programming_regioninfo {
211 uint8_t ProgRegShift;
213 uint8_t ControlValid;
215 uint8_t ControlInvalid;
217 } __attribute__((packed));
219 /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
221 struct cfi_pri_amdstd {
223 uint8_t MajorVersion;
224 uint8_t MinorVersion;
225 uint8_t SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
226 uint8_t EraseSuspend;
228 uint8_t TmpBlkUnprotect;
229 uint8_t BlkProtUnprot;
230 uint8_t SimultaneousOps;
236 } __attribute__((packed));
238 /* Vendor-Specific PRI for Atmel chips (command set 0x0002) */
240 struct cfi_pri_atmel {
242 uint8_t MajorVersion;
243 uint8_t MinorVersion;
248 } __attribute__((packed));
250 struct cfi_pri_query {
252 uint32_t ProtField[1]; /* Not host ordered */
253 } __attribute__((packed));
255 struct cfi_bri_query {
256 uint8_t PageModeReadCap;
258 uint32_t ConfField[1]; /* Not host ordered */
259 } __attribute__((packed));
261 #define P_ID_NONE 0x0000
262 #define P_ID_INTEL_EXT 0x0001
263 #define P_ID_AMD_STD 0x0002
264 #define P_ID_INTEL_STD 0x0003
265 #define P_ID_AMD_EXT 0x0004
266 #define P_ID_WINBOND 0x0006
267 #define P_ID_ST_ADV 0x0020
268 #define P_ID_MITSUBISHI_STD 0x0100
269 #define P_ID_MITSUBISHI_EXT 0x0101
270 #define P_ID_SST_PAGE 0x0102
271 #define P_ID_SST_OLD 0x0701
272 #define P_ID_INTEL_PERFORMANCE 0x0200
273 #define P_ID_INTEL_DATA 0x0210
274 #define P_ID_RESERVED 0xffff
277 #define CFI_MODE_CFI 1
278 #define CFI_MODE_JEDEC 0
285 int cfi_mode; /* Are we a JEDEC device pretending to be CFI? */
288 struct mtd_info *(*cmdset_setup)(struct map_info *);
289 struct cfi_ident *cfiq; /* For now only one. We insist that all devs
290 must be of the same type. */
293 map_word sector_erase_cmd;
294 unsigned long chipshift; /* Because they're of the same type */
295 const char *im_name; /* inter_module name for cmdset_setup */
296 struct flchip chips[0]; /* per-chip data structure for each chip */
300 * Returns the command address according to the given geometry.
302 static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs,
303 struct map_info *map, struct cfi_private *cfi)
305 unsigned bankwidth = map_bankwidth(map);
306 unsigned interleave = cfi_interleave(cfi);
307 unsigned type = cfi->device_type;
310 addr = (cmd_ofs * type) * interleave;
312 /* Modify the unlock address if we are in compatibility mode.
313 * For 16bit devices on 8 bit busses
314 * and 32bit devices on 16 bit busses
315 * set the low bit of the alternating bit sequence of the address.
317 if (((type * interleave) > bankwidth) && ((cmd_ofs & 0xff) == 0xaa))
318 addr |= (type >> 1)*interleave;
324 * Transforms the CFI command for the given geometry (bus width & interleave).
325 * It looks too long to be inline, but in the common case it should almost all
326 * get optimised away.
328 static inline map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi)
330 map_word val = { {0} };
331 int wordwidth, words_per_bus, chip_mode, chips_per_word;
332 unsigned long onecmd;
335 /* We do it this way to give the compiler a fighting chance
336 of optimising away all the crap for 'bankwidth' larger than
337 an unsigned long, in the common case where that support is
339 if (map_bankwidth_is_large(map)) {
340 wordwidth = sizeof(unsigned long);
341 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
343 wordwidth = map_bankwidth(map);
347 chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
348 chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
350 /* First, determine what the bit-pattern should be for a single
351 device, according to chip mode and endianness... */
358 onecmd = cpu_to_cfi16(map, cmd);
361 onecmd = cpu_to_cfi32(map, cmd);
365 /* Now replicate it across the size of an unsigned long, or
366 just to the bus width as appropriate */
367 switch (chips_per_word) {
369 #if BITS_PER_LONG >= 64
371 onecmd |= (onecmd << (chip_mode * 32));
374 onecmd |= (onecmd << (chip_mode * 16));
376 onecmd |= (onecmd << (chip_mode * 8));
381 /* And finally, for the multi-word case, replicate it
382 in all words in the structure */
383 for (i=0; i < words_per_bus; i++) {
389 #define CMD(x) cfi_build_cmd((x), map, cfi)
392 static inline unsigned long cfi_merge_status(map_word val, struct map_info *map,
393 struct cfi_private *cfi)
395 int wordwidth, words_per_bus, chip_mode, chips_per_word;
396 unsigned long onestat, res = 0;
399 /* We do it this way to give the compiler a fighting chance
400 of optimising away all the crap for 'bankwidth' larger than
401 an unsigned long, in the common case where that support is
403 if (map_bankwidth_is_large(map)) {
404 wordwidth = sizeof(unsigned long);
405 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
407 wordwidth = map_bankwidth(map);
411 chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
412 chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
415 /* Or all status words together */
416 for (i=1; i < words_per_bus; i++) {
421 switch(chips_per_word) {
423 #if BITS_PER_LONG >= 64
425 res |= (onestat >> (chip_mode * 32));
428 res |= (onestat >> (chip_mode * 16));
430 res |= (onestat >> (chip_mode * 8));
435 /* Last, determine what the bit-pattern should be for a single
436 device, according to chip mode and endianness... */
441 res = cfi16_to_cpu(map, res);
444 res = cfi32_to_cpu(map, res);
451 #define MERGESTATUS(x) cfi_merge_status((x), map, cfi)
455 * Sends a CFI command to a bank of flash for the given geometry.
457 * Returns the offset in flash where the command was written.
458 * If prev_val is non-null, it will be set to the value at the command address,
459 * before the command was written.
461 static inline uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
462 struct map_info *map, struct cfi_private *cfi,
463 int type, map_word *prev_val)
466 uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, map, cfi);
467 val = cfi_build_cmd(cmd, map, cfi);
470 *prev_val = map_read(map, addr);
472 map_write(map, val, addr);
477 static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
479 map_word val = map_read(map, addr);
481 if (map_bankwidth_is_1(map)) {
483 } else if (map_bankwidth_is_2(map)) {
484 return cfi16_to_cpu(map, val.x[0]);
486 /* No point in a 64-bit byteswap since that would just be
487 swapping the responses from different chips, and we are
488 only interested in one chip (a representative sample) */
489 return cfi32_to_cpu(map, val.x[0]);
493 static inline uint16_t cfi_read_query16(struct map_info *map, uint32_t addr)
495 map_word val = map_read(map, addr);
497 if (map_bankwidth_is_1(map)) {
498 return val.x[0] & 0xff;
499 } else if (map_bankwidth_is_2(map)) {
500 return cfi16_to_cpu(map, val.x[0]);
502 /* No point in a 64-bit byteswap since that would just be
503 swapping the responses from different chips, and we are
504 only interested in one chip (a representative sample) */
505 return cfi32_to_cpu(map, val.x[0]);
509 static inline void cfi_udelay(int us)
512 msleep((us+999)/1000);
519 int __xipram cfi_qry_present(struct map_info *map, __u32 base,
520 struct cfi_private *cfi);
521 int __xipram cfi_qry_mode_on(uint32_t base, struct map_info *map,
522 struct cfi_private *cfi);
523 void __xipram cfi_qry_mode_off(uint32_t base, struct map_info *map,
524 struct cfi_private *cfi);
526 struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
531 void (*fixup)(struct mtd_info *mtd);
534 #define CFI_MFR_ANY 0xFFFF
535 #define CFI_ID_ANY 0xFFFF
536 #define CFI_MFR_CONTINUATION 0x007F
538 #define CFI_MFR_AMD 0x0001
539 #define CFI_MFR_AMIC 0x0037
540 #define CFI_MFR_ATMEL 0x001F
541 #define CFI_MFR_EON 0x001C
542 #define CFI_MFR_FUJITSU 0x0004
543 #define CFI_MFR_HYUNDAI 0x00AD
544 #define CFI_MFR_INTEL 0x0089
545 #define CFI_MFR_MACRONIX 0x00C2
546 #define CFI_MFR_NEC 0x0010
547 #define CFI_MFR_PMC 0x009D
548 #define CFI_MFR_SAMSUNG 0x00EC
549 #define CFI_MFR_SHARP 0x00B0
550 #define CFI_MFR_SST 0x00BF
551 #define CFI_MFR_ST 0x0020 /* STMicroelectronics */
552 #define CFI_MFR_TOSHIBA 0x0098
553 #define CFI_MFR_WINBOND 0x00DA
555 void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
557 typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
558 unsigned long adr, int len, void *thunk);
560 int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
561 loff_t ofs, size_t len, void *thunk);
564 #endif /* __MTD_CFI_H__ */