cfi_probe.c 13 KB

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