cfi_probe.c 13 KB

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