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