]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/mtd/cfi.h
39f1430bd6d5fc2bee1702d96c6d0c11f78e8875
[linux-2.6-omap-h63xx.git] / include / linux / mtd / cfi.h
1
2 /* Common Flash Interface structures
3  * See http://support.intel.com/design/flash/technote/index.htm
4  * $Id: cfi.h,v 1.56 2005/11/07 11:14:54 gleixner Exp $
5  */
6
7 #ifndef __MTD_CFI_H__
8 #define __MTD_CFI_H__
9
10 #include <linux/config.h>
11 #include <linux/version.h>
12 #include <linux/delay.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/mtd/flashchip.h>
16 #include <linux/mtd/map.h>
17 #include <linux/mtd/cfi_endian.h>
18
19 #ifdef CONFIG_MTD_CFI_I1
20 #define cfi_interleave(cfi) 1
21 #define cfi_interleave_is_1(cfi) (cfi_interleave(cfi) == 1)
22 #else
23 #define cfi_interleave_is_1(cfi) (0)
24 #endif
25
26 #ifdef CONFIG_MTD_CFI_I2
27 # ifdef cfi_interleave
28 #  undef cfi_interleave
29 #  define cfi_interleave(cfi) ((cfi)->interleave)
30 # else
31 #  define cfi_interleave(cfi) 2
32 # endif
33 #define cfi_interleave_is_2(cfi) (cfi_interleave(cfi) == 2)
34 #else
35 #define cfi_interleave_is_2(cfi) (0)
36 #endif
37
38 #ifdef CONFIG_MTD_CFI_I4
39 # ifdef cfi_interleave
40 #  undef cfi_interleave
41 #  define cfi_interleave(cfi) ((cfi)->interleave)
42 # else
43 #  define cfi_interleave(cfi) 4
44 # endif
45 #define cfi_interleave_is_4(cfi) (cfi_interleave(cfi) == 4)
46 #else
47 #define cfi_interleave_is_4(cfi) (0)
48 #endif
49
50 #ifdef CONFIG_MTD_CFI_I8
51 # ifdef cfi_interleave
52 #  undef cfi_interleave
53 #  define cfi_interleave(cfi) ((cfi)->interleave)
54 # else
55 #  define cfi_interleave(cfi) 8
56 # endif
57 #define cfi_interleave_is_8(cfi) (cfi_interleave(cfi) == 8)
58 #else
59 #define cfi_interleave_is_8(cfi) (0)
60 #endif
61
62 static inline int cfi_interleave_supported(int i)
63 {
64         switch (i) {
65 #ifdef CONFIG_MTD_CFI_I1
66         case 1:
67 #endif
68 #ifdef CONFIG_MTD_CFI_I2
69         case 2:
70 #endif
71 #ifdef CONFIG_MTD_CFI_I4
72         case 4:
73 #endif
74 #ifdef CONFIG_MTD_CFI_I8
75         case 8:
76 #endif
77                 return 1;
78
79         default:
80                 return 0;
81         }
82 }
83
84
85 /* NB: these values must represents the number of bytes needed to meet the
86  *     device type (x8, x16, x32).  Eg. a 32 bit device is 4 x 8 bytes.
87  *     These numbers are used in calculations.
88  */
89 #define CFI_DEVICETYPE_X8  (8 / 8)
90 #define CFI_DEVICETYPE_X16 (16 / 8)
91 #define CFI_DEVICETYPE_X32 (32 / 8)
92 #define CFI_DEVICETYPE_X64 (64 / 8)
93
94 /* NB: We keep these structures in memory in HOST byteorder, except
95  * where individually noted.
96  */
97
98 /* Basic Query Structure */
99 struct cfi_ident {
100         uint8_t  qry[3];
101         uint16_t P_ID;
102         uint16_t P_ADR;
103         uint16_t A_ID;
104         uint16_t A_ADR;
105         uint8_t  VccMin;
106         uint8_t  VccMax;
107         uint8_t  VppMin;
108         uint8_t  VppMax;
109         uint8_t  WordWriteTimeoutTyp;
110         uint8_t  BufWriteTimeoutTyp;
111         uint8_t  BlockEraseTimeoutTyp;
112         uint8_t  ChipEraseTimeoutTyp;
113         uint8_t  WordWriteTimeoutMax;
114         uint8_t  BufWriteTimeoutMax;
115         uint8_t  BlockEraseTimeoutMax;
116         uint8_t  ChipEraseTimeoutMax;
117         uint8_t  DevSize;
118         uint16_t InterfaceDesc;
119         uint16_t MaxBufWriteSize;
120         uint8_t  NumEraseRegions;
121         uint32_t EraseRegionInfo[0]; /* Not host ordered */
122 } __attribute__((packed));
123
124 /* Extended Query Structure for both PRI and ALT */
125
126 struct cfi_extquery {
127         uint8_t  pri[3];
128         uint8_t  MajorVersion;
129         uint8_t  MinorVersion;
130 } __attribute__((packed));
131
132 /* Vendor-Specific PRI for Intel/Sharp Extended Command Set (0x0001) */
133
134 struct cfi_pri_intelext {
135         uint8_t  pri[3];
136         uint8_t  MajorVersion;
137         uint8_t  MinorVersion;
138         uint32_t FeatureSupport; /* if bit 31 is set then an additional uint32_t feature
139                                     block follows - FIXME - not currently supported */
140         uint8_t  SuspendCmdSupport;
141         uint16_t BlkStatusRegMask;
142         uint8_t  VccOptimal;
143         uint8_t  VppOptimal;
144         uint8_t  NumProtectionFields;
145         uint16_t ProtRegAddr;
146         uint8_t  FactProtRegSize;
147         uint8_t  UserProtRegSize;
148         uint8_t  extra[0];
149 } __attribute__((packed));
150
151 struct cfi_intelext_otpinfo {
152         uint32_t ProtRegAddr;
153         uint16_t FactGroups;
154         uint8_t  FactProtRegSize;
155         uint16_t UserGroups;
156         uint8_t  UserProtRegSize;
157 } __attribute__((packed));
158
159 struct cfi_intelext_blockinfo {
160         uint16_t NumIdentBlocks;
161         uint16_t BlockSize;
162         uint16_t MinBlockEraseCycles;
163         uint8_t  BitsPerCell;
164         uint8_t  BlockCap;
165 } __attribute__((packed));
166
167 struct cfi_intelext_regioninfo {
168         uint16_t NumIdentPartitions;
169         uint8_t  NumOpAllowed;
170         uint8_t  NumOpAllowedSimProgMode;
171         uint8_t  NumOpAllowedSimEraMode;
172         uint8_t  NumBlockTypes;
173         struct cfi_intelext_blockinfo BlockTypes[1];
174 } __attribute__((packed));
175
176 struct cfi_intelext_programming_regioninfo {
177         uint8_t  ProgRegShift;
178         uint8_t  Reserved1;
179         uint8_t  ControlValid;
180         uint8_t  Reserved2;
181         uint8_t  ControlInvalid;
182         uint8_t  Reserved3;
183 } __attribute__((packed));
184
185 /* Vendor-Specific PRI for AMD/Fujitsu Extended Command Set (0x0002) */
186
187 struct cfi_pri_amdstd {
188         uint8_t  pri[3];
189         uint8_t  MajorVersion;
190         uint8_t  MinorVersion;
191         uint8_t  SiliconRevision; /* bits 1-0: Address Sensitive Unlock */
192         uint8_t  EraseSuspend;
193         uint8_t  BlkProt;
194         uint8_t  TmpBlkUnprotect;
195         uint8_t  BlkProtUnprot;
196         uint8_t  SimultaneousOps;
197         uint8_t  BurstMode;
198         uint8_t  PageMode;
199         uint8_t  VppMin;
200         uint8_t  VppMax;
201         uint8_t  TopBottom;
202 } __attribute__((packed));
203
204 struct cfi_pri_query {
205         uint8_t  NumFields;
206         uint32_t ProtField[1]; /* Not host ordered */
207 } __attribute__((packed));
208
209 struct cfi_bri_query {
210         uint8_t  PageModeReadCap;
211         uint8_t  NumFields;
212         uint32_t ConfField[1]; /* Not host ordered */
213 } __attribute__((packed));
214
215 #define P_ID_NONE               0x0000
216 #define P_ID_INTEL_EXT          0x0001
217 #define P_ID_AMD_STD            0x0002
218 #define P_ID_INTEL_STD          0x0003
219 #define P_ID_AMD_EXT            0x0004
220 #define P_ID_WINBOND            0x0006
221 #define P_ID_ST_ADV             0x0020
222 #define P_ID_MITSUBISHI_STD     0x0100
223 #define P_ID_MITSUBISHI_EXT     0x0101
224 #define P_ID_SST_PAGE           0x0102
225 #define P_ID_INTEL_PERFORMANCE  0x0200
226 #define P_ID_INTEL_DATA         0x0210
227 #define P_ID_RESERVED           0xffff
228
229
230 #define CFI_MODE_CFI    1
231 #define CFI_MODE_JEDEC  0
232
233 struct cfi_private {
234         uint16_t cmdset;
235         void *cmdset_priv;
236         int interleave;
237         int device_type;
238         int cfi_mode;           /* Are we a JEDEC device pretending to be CFI? */
239         int addr_unlock1;
240         int addr_unlock2;
241         struct mtd_info *(*cmdset_setup)(struct map_info *);
242         struct cfi_ident *cfiq; /* For now only one. We insist that all devs
243                                   must be of the same type. */
244         int mfr, id;
245         int numchips;
246         unsigned long chipshift; /* Because they're of the same type */
247         const char *im_name;     /* inter_module name for cmdset_setup */
248         struct flchip chips[0];  /* per-chip data structure for each chip */
249 };
250
251 /*
252  * Returns the command address according to the given geometry.
253  */
254 static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs, int interleave, int type)
255 {
256         return (cmd_ofs * type) * interleave;
257 }
258
259 /*
260  * Transforms the CFI command for the given geometry (bus width & interleave).
261  * It looks too long to be inline, but in the common case it should almost all
262  * get optimised away.
263  */
264 static inline map_word cfi_build_cmd(u_long cmd, struct map_info *map, struct cfi_private *cfi)
265 {
266         map_word val = { {0} };
267         int wordwidth, words_per_bus, chip_mode, chips_per_word;
268         unsigned long onecmd;
269         int i;
270
271         /* We do it this way to give the compiler a fighting chance
272            of optimising away all the crap for 'bankwidth' larger than
273            an unsigned long, in the common case where that support is
274            disabled */
275         if (map_bankwidth_is_large(map)) {
276                 wordwidth = sizeof(unsigned long);
277                 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
278         } else {
279                 wordwidth = map_bankwidth(map);
280                 words_per_bus = 1;
281         }
282
283         chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
284         chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
285
286         /* First, determine what the bit-pattern should be for a single
287            device, according to chip mode and endianness... */
288         switch (chip_mode) {
289         default: BUG();
290         case 1:
291                 onecmd = cmd;
292                 break;
293         case 2:
294                 onecmd = cpu_to_cfi16(cmd);
295                 break;
296         case 4:
297                 onecmd = cpu_to_cfi32(cmd);
298                 break;
299         }
300
301         /* Now replicate it across the size of an unsigned long, or
302            just to the bus width as appropriate */
303         switch (chips_per_word) {
304         default: BUG();
305 #if BITS_PER_LONG >= 64
306         case 8:
307                 onecmd |= (onecmd << (chip_mode * 32));
308 #endif
309         case 4:
310                 onecmd |= (onecmd << (chip_mode * 16));
311         case 2:
312                 onecmd |= (onecmd << (chip_mode * 8));
313         case 1:
314                 ;
315         }
316
317         /* And finally, for the multi-word case, replicate it
318            in all words in the structure */
319         for (i=0; i < words_per_bus; i++) {
320                 val.x[i] = onecmd;
321         }
322
323         return val;
324 }
325 #define CMD(x)  cfi_build_cmd((x), map, cfi)
326
327
328 static inline unsigned long cfi_merge_status(map_word val, struct map_info *map,
329                                            struct cfi_private *cfi)
330 {
331         int wordwidth, words_per_bus, chip_mode, chips_per_word;
332         unsigned long onestat, res = 0;
333         int i;
334
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
338            disabled */
339         if (map_bankwidth_is_large(map)) {
340                 wordwidth = sizeof(unsigned long);
341                 words_per_bus = (map_bankwidth(map)) / wordwidth; // i.e. normally 1
342         } else {
343                 wordwidth = map_bankwidth(map);
344                 words_per_bus = 1;
345         }
346
347         chip_mode = map_bankwidth(map) / cfi_interleave(cfi);
348         chips_per_word = wordwidth * cfi_interleave(cfi) / map_bankwidth(map);
349
350         onestat = val.x[0];
351         /* Or all status words together */
352         for (i=1; i < words_per_bus; i++) {
353                 onestat |= val.x[i];
354         }
355
356         res = onestat;
357         switch(chips_per_word) {
358         default: BUG();
359 #if BITS_PER_LONG >= 64
360         case 8:
361                 res |= (onestat >> (chip_mode * 32));
362 #endif
363         case 4:
364                 res |= (onestat >> (chip_mode * 16));
365         case 2:
366                 res |= (onestat >> (chip_mode * 8));
367         case 1:
368                 ;
369         }
370
371         /* Last, determine what the bit-pattern should be for a single
372            device, according to chip mode and endianness... */
373         switch (chip_mode) {
374         case 1:
375                 break;
376         case 2:
377                 res = cfi16_to_cpu(res);
378                 break;
379         case 4:
380                 res = cfi32_to_cpu(res);
381                 break;
382         default: BUG();
383         }
384         return res;
385 }
386
387 #define MERGESTATUS(x) cfi_merge_status((x), map, cfi)
388
389
390 /*
391  * Sends a CFI command to a bank of flash for the given geometry.
392  *
393  * Returns the offset in flash where the command was written.
394  * If prev_val is non-null, it will be set to the value at the command address,
395  * before the command was written.
396  */
397 static inline uint32_t cfi_send_gen_cmd(u_char cmd, uint32_t cmd_addr, uint32_t base,
398                                 struct map_info *map, struct cfi_private *cfi,
399                                 int type, map_word *prev_val)
400 {
401         map_word val;
402         uint32_t addr = base + cfi_build_cmd_addr(cmd_addr, cfi_interleave(cfi), type);
403
404         val = cfi_build_cmd(cmd, map, cfi);
405
406         if (prev_val)
407                 *prev_val = map_read(map, addr);
408
409         map_write(map, val, addr);
410
411         return addr - base;
412 }
413
414 static inline uint8_t cfi_read_query(struct map_info *map, uint32_t addr)
415 {
416         map_word val = map_read(map, addr);
417
418         if (map_bankwidth_is_1(map)) {
419                 return val.x[0];
420         } else if (map_bankwidth_is_2(map)) {
421                 return cfi16_to_cpu(val.x[0]);
422         } else {
423                 /* No point in a 64-bit byteswap since that would just be
424                    swapping the responses from different chips, and we are
425                    only interested in one chip (a representative sample) */
426                 return cfi32_to_cpu(val.x[0]);
427         }
428 }
429
430 static inline void cfi_udelay(int us)
431 {
432         if (us >= 1000) {
433                 msleep((us+999)/1000);
434         } else {
435                 udelay(us);
436                 cond_resched();
437         }
438 }
439
440 struct cfi_extquery *cfi_read_pri(struct map_info *map, uint16_t adr, uint16_t size,
441                              const char* name);
442 struct cfi_fixup {
443         uint16_t mfr;
444         uint16_t id;
445         void (*fixup)(struct mtd_info *mtd, void* param);
446         void* param;
447 };
448
449 #define CFI_MFR_ANY 0xffff
450 #define CFI_ID_ANY  0xffff
451
452 #define CFI_MFR_AMD 0x0001
453 #define CFI_MFR_ST  0x0020      /* STMicroelectronics */
454
455 void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
456
457 typedef int (*varsize_frob_t)(struct map_info *map, struct flchip *chip,
458                               unsigned long adr, int len, void *thunk);
459
460 int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
461         loff_t ofs, size_t len, void *thunk);
462
463
464 #endif /* __MTD_CFI_H__ */