cfi_probe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. cfi->sector_erase_cmd = CMD(0x30);
  149. /* Read the CFI info structure */
  150. xip_disable_qry(base, map, cfi);
  151. for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++)
  152. ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
  153. /* Do any necessary byteswapping */
  154. cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
  155. cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
  156. cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
  157. cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
  158. cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
  159. cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
  160. #ifdef DEBUG_CFI
  161. /* Dump the information therein */
  162. print_cfi_ident(cfi->cfiq);
  163. #endif
  164. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  165. cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
  166. #ifdef DEBUG_CFI
  167. printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
  168. i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
  169. (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
  170. #endif
  171. }
  172. if (cfi->cfiq->P_ID == P_ID_SST_OLD) {
  173. addr_unlock1 = 0x5555;
  174. addr_unlock2 = 0x2AAA;
  175. }
  176. /*
  177. * Note we put the device back into Read Mode BEFORE going into Auto
  178. * Select Mode, as some devices support nesting of modes, others
  179. * don't. This way should always work.
  180. * On cmdset 0001 the writes of 0xaa and 0x55 are not needed, and
  181. * so should be treated as nops or illegal (and so put the device
  182. * back into Read Mode, which is a nop in this case).
  183. */
  184. cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL);
  185. cfi_send_gen_cmd(0xaa, addr_unlock1, base, map, cfi, cfi->device_type, NULL);
  186. cfi_send_gen_cmd(0x55, addr_unlock2, base, map, cfi, cfi->device_type, NULL);
  187. cfi_send_gen_cmd(0x90, addr_unlock1, base, map, cfi, cfi->device_type, NULL);
  188. cfi->mfr = cfi_read_query16(map, base);
  189. cfi->id = cfi_read_query16(map, base + ofs_factor);
  190. /* Get AMD/Spansion extended JEDEC ID */
  191. if (cfi->mfr == CFI_MFR_AMD && (cfi->id & 0xff) == 0x7e)
  192. cfi->id = cfi_read_query(map, base + 0xe * ofs_factor) << 8 |
  193. cfi_read_query(map, base + 0xf * ofs_factor);
  194. /* Put it back into Read Mode */
  195. cfi_qry_mode_off(base, map, cfi);
  196. xip_allowed(base, map);
  197. printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank. Manufacturer ID %#08x Chip ID %#08x\n",
  198. map->name, cfi->interleave, cfi->device_type*8, base,
  199. map->bankwidth*8, cfi->mfr, cfi->id);
  200. return 1;
  201. }
  202. #ifdef DEBUG_CFI
  203. static char *vendorname(__u16 vendor)
  204. {
  205. switch (vendor) {
  206. case P_ID_NONE:
  207. return "None";
  208. case P_ID_INTEL_EXT:
  209. return "Intel/Sharp Extended";
  210. case P_ID_AMD_STD:
  211. return "AMD/Fujitsu Standard";
  212. case P_ID_INTEL_STD:
  213. return "Intel/Sharp Standard";
  214. case P_ID_AMD_EXT:
  215. return "AMD/Fujitsu Extended";
  216. case P_ID_WINBOND:
  217. return "Winbond Standard";
  218. case P_ID_ST_ADV:
  219. return "ST Advanced";
  220. case P_ID_MITSUBISHI_STD:
  221. return "Mitsubishi Standard";
  222. case P_ID_MITSUBISHI_EXT:
  223. return "Mitsubishi Extended";
  224. case P_ID_SST_PAGE:
  225. return "SST Page Write";
  226. case P_ID_SST_OLD:
  227. return "SST 39VF160x/39VF320x";
  228. case P_ID_INTEL_PERFORMANCE:
  229. return "Intel Performance Code";
  230. case P_ID_INTEL_DATA:
  231. return "Intel Data";
  232. case P_ID_RESERVED:
  233. return "Not Allowed / Reserved for Future Use";
  234. default:
  235. return "Unknown";
  236. }
  237. }
  238. static void print_cfi_ident(struct cfi_ident *cfip)
  239. {
  240. #if 0
  241. if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
  242. printk("Invalid CFI ident structure.\n");
  243. return;
  244. }
  245. #endif
  246. printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
  247. if (cfip->P_ADR)
  248. printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
  249. else
  250. printk("No Primary Algorithm Table\n");
  251. printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
  252. if (cfip->A_ADR)
  253. printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
  254. else
  255. printk("No Alternate Algorithm Table\n");
  256. printk("Vcc Minimum: %2d.%d V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
  257. printk("Vcc Maximum: %2d.%d V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
  258. if (cfip->VppMin) {
  259. printk("Vpp Minimum: %2d.%d V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
  260. printk("Vpp Maximum: %2d.%d V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
  261. }
  262. else
  263. printk("No Vpp line\n");
  264. printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
  265. printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
  266. if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
  267. printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
  268. printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
  269. }
  270. else
  271. printk("Full buffer write not supported\n");
  272. printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
  273. printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
  274. if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
  275. printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
  276. printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
  277. }
  278. else
  279. printk("Chip erase not supported\n");
  280. printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
  281. printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
  282. switch(cfip->InterfaceDesc) {
  283. case CFI_INTERFACE_X8_ASYNC:
  284. printk(" - x8-only asynchronous interface\n");
  285. break;
  286. case CFI_INTERFACE_X16_ASYNC:
  287. printk(" - x16-only asynchronous interface\n");
  288. break;
  289. case CFI_INTERFACE_X8_BY_X16_ASYNC:
  290. printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
  291. break;
  292. case CFI_INTERFACE_X32_ASYNC:
  293. printk(" - x32-only asynchronous interface\n");
  294. break;
  295. case CFI_INTERFACE_X16_BY_X32_ASYNC:
  296. printk(" - supports x16 and x32 via Word# with asynchronous interface\n");
  297. break;
  298. case CFI_INTERFACE_NOT_ALLOWED:
  299. printk(" - Not Allowed / Reserved\n");
  300. break;
  301. default:
  302. printk(" - Unknown\n");
  303. break;
  304. }
  305. printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
  306. printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
  307. }
  308. #endif /* DEBUG_CFI */
  309. static struct chip_probe cfi_chip_probe = {
  310. .name = "CFI",
  311. .probe_chip = cfi_probe_chip
  312. };
  313. struct mtd_info *cfi_probe(struct map_info *map)
  314. {
  315. /*
  316. * Just use the generic probe stuff to call our CFI-specific
  317. * chip_probe routine in all the possible permutations, etc.
  318. */
  319. return mtd_do_chip_probe(map, &cfi_chip_probe);
  320. }
  321. static struct mtd_chip_driver cfi_chipdrv = {
  322. .probe = cfi_probe,
  323. .name = "cfi_probe",
  324. .module = THIS_MODULE
  325. };
  326. static int __init cfi_probe_init(void)
  327. {
  328. register_mtd_chip_driver(&cfi_chipdrv);
  329. return 0;
  330. }
  331. static void __exit cfi_probe_exit(void)
  332. {
  333. unregister_mtd_chip_driver(&cfi_chipdrv);
  334. }
  335. module_init(cfi_probe_init);
  336. module_exit(cfi_probe_exit);
  337. MODULE_LICENSE("GPL");
  338. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  339. MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");