PageRenderTime 141ms CodeModel.GetById 20ms RepoModel.GetById 3ms app.codeStats 0ms

/release/src-rt/linux/linux-2.6/drivers/mtd/chips/cfi_probe.c

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