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