cfi_probe.c 13 KB

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