1/* 2 Common Flash Interface probe code. 3 (C) 2000 Red Hat. GPL'd. 4*/ 5 6#include <linux/module.h> 7#include <linux/types.h> 8#include <linux/kernel.h> 9#include <linux/init.h> 10#include <asm/io.h> 11#include <asm/byteorder.h> 12#include <linux/errno.h> 13#include <linux/slab.h> 14#include <linux/interrupt.h> 15 16#include <linux/mtd/xip.h> 17#include <linux/mtd/map.h> 18#include <linux/mtd/cfi.h> 19#include <linux/mtd/gen_probe.h> 20 21//#define DEBUG_CFI 22 23#ifdef DEBUG_CFI 24static void print_cfi_ident(struct cfi_ident *); 25#endif 26 27static int cfi_probe_chip(struct map_info *map, __u32 base, 28 unsigned long *chip_map, struct cfi_private *cfi); 29static int cfi_chip_setup(struct map_info *map, struct cfi_private *cfi); 30 31struct mtd_info *cfi_probe(struct map_info *map); 32 33#ifdef CONFIG_MTD_XIP 34 35/* only needed for short periods, so this is rather simple */ 36#define xip_disable() local_irq_disable() 37 38#define xip_allowed(base, map) \ 39do { \ 40 (void) map_read(map, base); \ 41 xip_iprefetch(); \ 42 local_irq_enable(); \ 43} while (0) 44 45#define xip_enable(base, map, cfi) \ 46do { \ 47 cfi_qry_mode_off(base, map, cfi); \ 48 xip_allowed(base, map); \ 49} while (0) 50 51#define xip_disable_qry(base, map, cfi) \ 52do { \ 53 xip_disable(); \ 54 cfi_qry_mode_on(base, map, cfi); \ 55} while (0) 56 57#else 58 59#define xip_disable() do { } while (0) 60#define xip_allowed(base, map) do { } while (0) 61#define xip_enable(base, map, cfi) do { } while (0) 62#define xip_disable_qry(base, map, cfi) do { } while (0) 63 64#endif 65 66/* check for QRY. 67 in: interleave,type,mode 68 ret: table index, <0 for error 69 */ 70 71static int __xipram cfi_probe_chip(struct map_info *map, __u32 base, 72 unsigned long *chip_map, struct cfi_private *cfi) 73{ 74 int i; 75 76 if ((base + 0) >= map->size) { 77 printk(KERN_NOTICE 78 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n", 79 (unsigned long)base, map->size -1); 80 return 0; 81 } 82 if ((base + 0xff) >= map->size) { 83 printk(KERN_NOTICE 84 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n", 85 (unsigned long)base + 0x55, map->size -1); 86 return 0; 87 } 88 89 xip_disable(); 90 if (!cfi_qry_mode_on(base, map, cfi)) { 91 xip_enable(base, map, cfi); 92 return 0; 93 } 94 95 if (!cfi->numchips) { 96 /* This is the first time we're called. Set up the CFI 97 stuff accordingly and return */ 98 return cfi_chip_setup(map, cfi); 99 } 100 101 /* Check each previous chip to see if it's an alias */ 102 for (i=0; i < (base >> cfi->chipshift); i++) { 103 unsigned long start; 104 if(!test_bit(i, chip_map)) { 105 /* Skip location; no valid chip at this address */ 106 continue; 107 } 108 start = i << cfi->chipshift; 109 /* This chip should be in read mode if it's one 110 we've already touched. */ 111 if (cfi_qry_present(map, start, cfi)) { 112 /* Eep. This chip also had the QRY marker. 113 * Is it an alias for the new one? */ 114 cfi_qry_mode_off(start, map, cfi); 115 116 /* If the QRY marker goes away, it's an alias */ 117 if (!cfi_qry_present(map, start, cfi)) { 118 xip_allowed(base, map); 119 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n", 120 map->name, base, start); 121 return 0; 122 } 123 /* Yes, it's actually got QRY for data. Most 124 * unfortunate. Stick the new chip in read mode 125 * too and if it's the same, assume it's an alias. */ 126 cfi_qry_mode_off(base, map, cfi); 127 128 if (cfi_qry_present(map, base, cfi)) { 129 xip_allowed(base, map); 130 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n", 131 map->name, base, start); 132 return 0; 133 } 134 } 135 } 136 137 /* OK, if we got to here, then none of the previous chips appear to 138 be aliases for the current one. */ 139 set_bit((base >> cfi->chipshift), chip_map); /* Update chip map */ 140 cfi->numchips++; 141 142 /* Put it back into Read Mode */ 143 cfi_qry_mode_off(base, map, cfi); 144 xip_allowed(base, map); 145 146 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n", 147 map->name, cfi->interleave, cfi->device_type*8, base, 148 map->bankwidth*8); 149 150 return 1; 151} 152 153static int __xipram cfi_chip_setup(struct map_info *map, 154 struct cfi_private *cfi) 155{ 156 int ofs_factor = cfi->interleave*cfi->device_type; 157 __u32 base = 0; 158 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor); 159 int i; 160 int addr_unlock1 = 0x555, addr_unlock2 = 0x2AA; 161 162 xip_enable(base, map, cfi); 163#ifdef DEBUG_CFI 164 printk("Number of erase regions: %d\n", num_erase_regions); 165#endif 166 if (!num_erase_regions) 167 return 0; 168 169 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL); 170 if (!cfi->cfiq) { 171 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name); 172 return 0; 173 } 174 175 memset(cfi->cfiq,0,sizeof(struct cfi_ident)); 176 177 cfi->cfi_mode = CFI_MODE_CFI; 178 179 /* Read the CFI info structure */ 180 xip_disable_qry(base, map, cfi); 181 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++) 182 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor); 183 184 /* Do any necessary byteswapping */ 185 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID); 186 187 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR); 188 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID); 189 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR); 190 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc); 191 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize); 192 193#ifdef DEBUG_CFI 194 /* Dump the information therein */ 195 print_cfi_ident(cfi->cfiq); 196#endif 197 198 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) { 199 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]); 200 201#ifdef DEBUG_CFI 202 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n", 203 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff, 204 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1); 205#endif 206 } 207 208 if (cfi->cfiq->P_ID == P_ID_SST_OLD) { 209 addr_unlock1 = 0x5555; 210 addr_unlock2 = 0x2AAA; 211 } 212 213 /* 214 * Note we put the device back into Read Mode BEFORE going into Auto 215 * Select Mode, as some devices support nesting of modes, others 216 * don't. This way should always work. 217 * On cmdset 0001 the writes of 0xaa and 0x55 are not needed, and 218 * so should be treated as nops or illegal (and so put the device 219 * back into Read Mode, which is a nop in this case). 220 */ 221 cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL); 222 cfi_send_gen_cmd(0xaa, addr_unlock1, base, map, cfi, cfi->device_type, NULL); 223 cfi_send_gen_cmd(0x55, addr_unlock2, base, map, cfi, cfi->device_type, NULL); 224 cfi_send_gen_cmd(0x90, addr_unlock1, base, map, cfi, cfi->device_type, NULL); 225 cfi->mfr = cfi_read_query16(map, base); 226 cfi->id = cfi_read_query16(map, base + ofs_factor); 227 228 /* Get AMD/Spansion extended JEDEC ID */ 229 if (cfi->mfr == CFI_MFR_AMD && (cfi->id & 0xff) == 0x7e) 230 cfi->id = cfi_read_query(map, base + 0xe * ofs_factor) << 8 | 231 cfi_read_query(map, base + 0xf * ofs_factor); 232 233 /* Put it back into Read Mode */ 234 cfi_qry_mode_off(base, map, cfi); 235 xip_allowed(base, map); 236 237 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank. Manufacturer ID %#08x Chip ID %#08x\n", 238 map->name, cfi->interleave, cfi->device_type*8, base, 239 map->bankwidth*8, cfi->mfr, cfi->id); 240 241 return 1; 242} 243 244#ifdef DEBUG_CFI 245static char *vendorname(__u16 vendor) 246{ 247 switch (vendor) { 248 case P_ID_NONE: 249 return "None"; 250 251 case P_ID_INTEL_EXT: 252 return "Intel/Sharp Extended"; 253 254 case P_ID_AMD_STD: 255 return "AMD/Fujitsu Standard"; 256 257 case P_ID_INTEL_STD: 258 return "Intel/Sharp Standard"; 259 260 case P_ID_AMD_EXT: 261 return "AMD/Fujitsu Extended"; 262 263 case P_ID_WINBOND: 264 return "Winbond Standard"; 265 266 case P_ID_ST_ADV: 267 return "ST Advanced"; 268 269 case P_ID_MITSUBISHI_STD: 270 return "Mitsubishi Standard"; 271 272 case P_ID_MITSUBISHI_EXT: 273 return "Mitsubishi Extended"; 274 275 case P_ID_SST_PAGE: 276 return "SST Page Write"; 277 278 case P_ID_SST_OLD: 279 return "SST 39VF160x/39VF320x"; 280 281 case P_ID_INTEL_PERFORMANCE: 282 return "Intel Performance Code"; 283 284 case P_ID_INTEL_DATA: 285 return "Intel Data"; 286 287 case P_ID_RESERVED: 288 return "Not Allowed / Reserved for Future Use"; 289 290 default: 291 return "Unknown"; 292 } 293} 294 295 296static void print_cfi_ident(struct cfi_ident *cfip) 297{ 298 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID)); 299 if (cfip->P_ADR) 300 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR); 301 else 302 printk("No Primary Algorithm Table\n"); 303 304 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID)); 305 if (cfip->A_ADR) 306 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR); 307 else 308 printk("No Alternate Algorithm Table\n"); 309 310 311 printk("Vcc Minimum: %2d.%d V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf); 312 printk("Vcc Maximum: %2d.%d V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf); 313 if (cfip->VppMin) { 314 printk("Vpp Minimum: %2d.%d V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf); 315 printk("Vpp Maximum: %2d.%d V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf); 316 } 317 else 318 printk("No Vpp line\n"); 319 320 printk("Typical byte/word write timeout: %d ��s\n", 1<<cfip->WordWriteTimeoutTyp); 321 printk("Maximum byte/word write timeout: %d ��s\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp)); 322 323 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) { 324 printk("Typical full buffer write timeout: %d ��s\n", 1<<cfip->BufWriteTimeoutTyp); 325 printk("Maximum full buffer write timeout: %d ��s\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp)); 326 } 327 else 328 printk("Full buffer write not supported\n"); 329 330 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp); 331 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp)); 332 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) { 333 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp); 334 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp)); 335 } 336 else 337 printk("Chip erase not supported\n"); 338 339 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20)); 340 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc); 341 switch(cfip->InterfaceDesc) { 342 case CFI_INTERFACE_X8_ASYNC: 343 printk(" - x8-only asynchronous interface\n"); 344 break; 345 346 case CFI_INTERFACE_X16_ASYNC: 347 printk(" - x16-only asynchronous interface\n"); 348 break; 349 350 case CFI_INTERFACE_X8_BY_X16_ASYNC: 351 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n"); 352 break; 353 354 case CFI_INTERFACE_X32_ASYNC: 355 printk(" - x32-only asynchronous interface\n"); 356 break; 357 358 case CFI_INTERFACE_X16_BY_X32_ASYNC: 359 printk(" - supports x16 and x32 via Word# with asynchronous interface\n"); 360 break; 361 362 case CFI_INTERFACE_NOT_ALLOWED: 363 printk(" - Not Allowed / Reserved\n"); 364 break; 365 366 default: 367 printk(" - Unknown\n"); 368 break; 369 } 370 371 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize); 372 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions); 373 374} 375#endif /* DEBUG_CFI */ 376 377static struct chip_probe cfi_chip_probe = { 378 .name = "CFI", 379 .probe_chip = cfi_probe_chip 380}; 381 382struct mtd_info *cfi_probe(struct map_info *map) 383{ 384 /* 385 * Just use the generic probe stuff to call our CFI-specific 386 * chip_probe routine in all the possible permutations, etc. 387 */ 388 return mtd_do_chip_probe(map, &cfi_chip_probe); 389} 390 391static struct mtd_chip_driver cfi_chipdrv = { 392 .probe = cfi_probe, 393 .name = "cfi_probe", 394 .module = THIS_MODULE 395}; 396 397static int __init cfi_probe_init(void) 398{ 399 register_mtd_chip_driver(&cfi_chipdrv); 400 return 0; 401} 402 403static void __exit cfi_probe_exit(void) 404{ 405 unregister_mtd_chip_driver(&cfi_chipdrv); 406} 407 408module_init(cfi_probe_init); 409module_exit(cfi_probe_exit); 410 411MODULE_LICENSE("GPL"); 412MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al."); 413MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips"); 414