cfi_probe.c 12 KB

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