cfi_probe.c 13 KB

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