jedec_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * (C) Copyright 2007
  3. * Michael Schwingen, <michael@schwingen.org>
  4. *
  5. * based in great part on jedec_probe.c from linux kernel:
  6. * (C) 2000 Red Hat. GPL'd.
  7. * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. *
  27. */
  28. /* The DEBUG define must be before common to enable debugging */
  29. /*#define DEBUG*/
  30. #include <common.h>
  31. #include <asm/processor.h>
  32. #include <asm/io.h>
  33. #include <asm/byteorder.h>
  34. #include <environment.h>
  35. #define P_ID_AMD_STD CFI_CMDSET_AMD_LEGACY
  36. /* AMD */
  37. #define AM29DL800BB 0x22CB
  38. #define AM29DL800BT 0x224A
  39. #define AM29F400BB 0x22AB
  40. #define AM29F800BB 0x2258
  41. #define AM29F800BT 0x22D6
  42. #define AM29LV400BB 0x22BA
  43. #define AM29LV400BT 0x22B9
  44. #define AM29LV800BB 0x225B
  45. #define AM29LV800BT 0x22DA
  46. #define AM29LV160DT 0x22C4
  47. #define AM29LV160DB 0x2249
  48. #define AM29F017D 0x003D
  49. #define AM29F016D 0x00AD
  50. #define AM29F080 0x00D5
  51. #define AM29F040 0x00A4
  52. #define AM29LV040B 0x004F
  53. #define AM29F032B 0x0041
  54. #define AM29F002T 0x00B0
  55. /* SST */
  56. #define SST39LF800 0x2781
  57. #define SST39LF160 0x2782
  58. #define SST39VF1601 0x234b
  59. #define SST39LF512 0x00D4
  60. #define SST39LF010 0x00D5
  61. #define SST39LF020 0x00D6
  62. #define SST39LF040 0x00D7
  63. #define SST39SF010A 0x00B5
  64. #define SST39SF020A 0x00B6
  65. /* STM */
  66. #define STM29F400BB 0x00D6
  67. /* MXIC */
  68. #define MX29LV040 0x004F
  69. /* WINBOND */
  70. #define W39L040A 0x00D6
  71. /* AMIC */
  72. #define A29L040 0x0092
  73. /* EON */
  74. #define EN29LV040A 0x004F
  75. /*
  76. * Unlock address sets for AMD command sets.
  77. * Intel command sets use the MTD_UADDR_UNNECESSARY.
  78. * Each identifier, except MTD_UADDR_UNNECESSARY, and
  79. * MTD_UADDR_NO_SUPPORT must be defined below in unlock_addrs[].
  80. * MTD_UADDR_NOT_SUPPORTED must be 0 so that structure
  81. * initialization need not require initializing all of the
  82. * unlock addresses for all bit widths.
  83. */
  84. enum uaddr {
  85. MTD_UADDR_NOT_SUPPORTED = 0, /* data width not supported */
  86. MTD_UADDR_0x0555_0x02AA,
  87. MTD_UADDR_0x0555_0x0AAA,
  88. MTD_UADDR_0x5555_0x2AAA,
  89. MTD_UADDR_0x0AAA_0x0555,
  90. MTD_UADDR_DONT_CARE, /* Requires an arbitrary address */
  91. MTD_UADDR_UNNECESSARY, /* Does not require any address */
  92. };
  93. struct unlock_addr {
  94. u32 addr1;
  95. u32 addr2;
  96. };
  97. /*
  98. * I don't like the fact that the first entry in unlock_addrs[]
  99. * exists, but is for MTD_UADDR_NOT_SUPPORTED - and, therefore,
  100. * should not be used. The problem is that structures with
  101. * initializers have extra fields initialized to 0. It is _very_
  102. * desireable to have the unlock address entries for unsupported
  103. * data widths automatically initialized - that means that
  104. * MTD_UADDR_NOT_SUPPORTED must be 0 and the first entry here
  105. * must go unused.
  106. */
  107. static const struct unlock_addr unlock_addrs[] = {
  108. [MTD_UADDR_NOT_SUPPORTED] = {
  109. .addr1 = 0xffff,
  110. .addr2 = 0xffff
  111. },
  112. [MTD_UADDR_0x0555_0x02AA] = {
  113. .addr1 = 0x0555,
  114. .addr2 = 0x02aa
  115. },
  116. [MTD_UADDR_0x0555_0x0AAA] = {
  117. .addr1 = 0x0555,
  118. .addr2 = 0x0aaa
  119. },
  120. [MTD_UADDR_0x5555_0x2AAA] = {
  121. .addr1 = 0x5555,
  122. .addr2 = 0x2aaa
  123. },
  124. [MTD_UADDR_0x0AAA_0x0555] = {
  125. .addr1 = 0x0AAA,
  126. .addr2 = 0x0555
  127. },
  128. [MTD_UADDR_DONT_CARE] = {
  129. .addr1 = 0x0000, /* Doesn't matter which address */
  130. .addr2 = 0x0000 /* is used - must be last entry */
  131. },
  132. [MTD_UADDR_UNNECESSARY] = {
  133. .addr1 = 0x0000,
  134. .addr2 = 0x0000
  135. }
  136. };
  137. struct amd_flash_info {
  138. const __u16 mfr_id;
  139. const __u16 dev_id;
  140. const char *name;
  141. const int DevSize;
  142. const int NumEraseRegions;
  143. const int CmdSet;
  144. const __u8 uaddr[4]; /* unlock addrs for 8, 16, 32, 64 */
  145. const ulong regions[6];
  146. };
  147. #define ERASEINFO(size,blocks) (size<<8)|(blocks-1)
  148. #define SIZE_64KiB 16
  149. #define SIZE_128KiB 17
  150. #define SIZE_256KiB 18
  151. #define SIZE_512KiB 19
  152. #define SIZE_1MiB 20
  153. #define SIZE_2MiB 21
  154. #define SIZE_4MiB 22
  155. #define SIZE_8MiB 23
  156. static const struct amd_flash_info jedec_table[] = {
  157. #ifdef CONFIG_SYS_FLASH_LEGACY_256Kx8
  158. {
  159. .mfr_id = (u16)SST_MANUFACT,
  160. .dev_id = SST39LF020,
  161. .name = "SST 39LF020",
  162. .uaddr = {
  163. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  164. },
  165. .DevSize = SIZE_256KiB,
  166. .CmdSet = P_ID_AMD_STD,
  167. .NumEraseRegions= 1,
  168. .regions = {
  169. ERASEINFO(0x01000,64),
  170. }
  171. },
  172. #endif
  173. #ifdef CONFIG_SYS_FLASH_LEGACY_512Kx8
  174. {
  175. .mfr_id = (u16)AMD_MANUFACT,
  176. .dev_id = AM29LV040B,
  177. .name = "AMD AM29LV040B",
  178. .uaddr = {
  179. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  180. },
  181. .DevSize = SIZE_512KiB,
  182. .CmdSet = P_ID_AMD_STD,
  183. .NumEraseRegions= 1,
  184. .regions = {
  185. ERASEINFO(0x10000,8),
  186. }
  187. },
  188. {
  189. .mfr_id = (u16)SST_MANUFACT,
  190. .dev_id = SST39LF040,
  191. .name = "SST 39LF040",
  192. .uaddr = {
  193. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  194. },
  195. .DevSize = SIZE_512KiB,
  196. .CmdSet = P_ID_AMD_STD,
  197. .NumEraseRegions= 1,
  198. .regions = {
  199. ERASEINFO(0x01000,128),
  200. }
  201. },
  202. {
  203. .mfr_id = (u16)STM_MANUFACT,
  204. .dev_id = STM_ID_M29W040B,
  205. .name = "ST Micro M29W040B",
  206. .uaddr = {
  207. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  208. },
  209. .DevSize = SIZE_512KiB,
  210. .CmdSet = P_ID_AMD_STD,
  211. .NumEraseRegions= 1,
  212. .regions = {
  213. ERASEINFO(0x10000,8),
  214. }
  215. },
  216. {
  217. .mfr_id = (u16)MX_MANUFACT,
  218. .dev_id = MX29LV040,
  219. .name = "MXIC MX29LV040",
  220. .uaddr = {
  221. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  222. },
  223. .DevSize = SIZE_512KiB,
  224. .CmdSet = P_ID_AMD_STD,
  225. .NumEraseRegions= 1,
  226. .regions = {
  227. ERASEINFO(0x10000, 8),
  228. }
  229. },
  230. {
  231. .mfr_id = (u16)WINB_MANUFACT,
  232. .dev_id = W39L040A,
  233. .name = "WINBOND W39L040A",
  234. .uaddr = {
  235. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  236. },
  237. .DevSize = SIZE_512KiB,
  238. .CmdSet = P_ID_AMD_STD,
  239. .NumEraseRegions= 1,
  240. .regions = {
  241. ERASEINFO(0x10000, 8),
  242. }
  243. },
  244. {
  245. .mfr_id = (u16)AMIC_MANUFACT,
  246. .dev_id = A29L040,
  247. .name = "AMIC A29L040",
  248. .uaddr = {
  249. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  250. },
  251. .DevSize = SIZE_512KiB,
  252. .CmdSet = P_ID_AMD_STD,
  253. .NumEraseRegions= 1,
  254. .regions = {
  255. ERASEINFO(0x10000, 8),
  256. }
  257. },
  258. {
  259. .mfr_id = (u16)EON_MANUFACT,
  260. .dev_id = EN29LV040A,
  261. .name = "EON EN29LV040A",
  262. .uaddr = {
  263. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  264. },
  265. .DevSize = SIZE_512KiB,
  266. .CmdSet = P_ID_AMD_STD,
  267. .NumEraseRegions= 1,
  268. .regions = {
  269. ERASEINFO(0x10000, 8),
  270. }
  271. },
  272. #endif
  273. #ifdef CONFIG_SYS_FLASH_LEGACY_512Kx16
  274. {
  275. .mfr_id = (u16)AMD_MANUFACT,
  276. .dev_id = AM29F400BB,
  277. .name = "AMD AM29F400BB",
  278. .uaddr = {
  279. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  280. },
  281. .DevSize = SIZE_512KiB,
  282. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  283. .NumEraseRegions= 4,
  284. .regions = {
  285. ERASEINFO(0x04000, 1),
  286. ERASEINFO(0x02000, 2),
  287. ERASEINFO(0x08000, 1),
  288. ERASEINFO(0x10000, 7),
  289. }
  290. },
  291. {
  292. .mfr_id = (u16)AMD_MANUFACT,
  293. .dev_id = AM29LV400BB,
  294. .name = "AMD AM29LV400BB",
  295. .uaddr = {
  296. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  297. },
  298. .DevSize = SIZE_512KiB,
  299. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  300. .NumEraseRegions= 4,
  301. .regions = {
  302. ERASEINFO(0x04000,1),
  303. ERASEINFO(0x02000,2),
  304. ERASEINFO(0x08000,1),
  305. ERASEINFO(0x10000,7),
  306. }
  307. },
  308. {
  309. .mfr_id = (u16)AMD_MANUFACT,
  310. .dev_id = AM29LV800BB,
  311. .name = "AMD AM29LV800BB",
  312. .uaddr = {
  313. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  314. },
  315. .DevSize = SIZE_1MiB,
  316. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  317. .NumEraseRegions= 4,
  318. .regions = {
  319. ERASEINFO(0x04000, 1),
  320. ERASEINFO(0x02000, 2),
  321. ERASEINFO(0x08000, 1),
  322. ERASEINFO(0x10000, 15),
  323. }
  324. },
  325. {
  326. .mfr_id = (u16)STM_MANUFACT,
  327. .dev_id = STM29F400BB,
  328. .name = "ST Micro M29F400BB",
  329. .uaddr = {
  330. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  331. },
  332. .DevSize = SIZE_512KiB,
  333. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  334. .NumEraseRegions = 4,
  335. .regions = {
  336. ERASEINFO(0x04000, 1),
  337. ERASEINFO(0x02000, 2),
  338. ERASEINFO(0x08000, 1),
  339. ERASEINFO(0x10000, 7),
  340. }
  341. },
  342. #endif
  343. };
  344. static inline void fill_info(flash_info_t *info, const struct amd_flash_info *jedec_entry, ulong base)
  345. {
  346. int i,j;
  347. int sect_cnt;
  348. int size_ratio;
  349. int total_size;
  350. enum uaddr uaddr_idx;
  351. size_ratio = info->portwidth / info->chipwidth;
  352. debug("Found JEDEC Flash: %s\n", jedec_entry->name);
  353. info->vendor = jedec_entry->CmdSet;
  354. /* Todo: do we need device-specific timeouts? */
  355. info->erase_blk_tout = 30000;
  356. info->buffer_write_tout = 1000;
  357. info->write_tout = 100;
  358. info->name = jedec_entry->name;
  359. /* copy unlock addresses from device table to CFI info struct. This
  360. is just here because the addresses are in the table anyway - if
  361. the flash is not detected due to wrong unlock addresses,
  362. flash_detect_legacy would have to try all of them before we even
  363. get here. */
  364. switch(info->chipwidth) {
  365. case FLASH_CFI_8BIT:
  366. uaddr_idx = jedec_entry->uaddr[0];
  367. break;
  368. case FLASH_CFI_16BIT:
  369. uaddr_idx = jedec_entry->uaddr[1];
  370. break;
  371. case FLASH_CFI_32BIT:
  372. uaddr_idx = jedec_entry->uaddr[2];
  373. break;
  374. default:
  375. uaddr_idx = MTD_UADDR_NOT_SUPPORTED;
  376. break;
  377. }
  378. debug("unlock address index %d\n", uaddr_idx);
  379. info->addr_unlock1 = unlock_addrs[uaddr_idx].addr1;
  380. info->addr_unlock2 = unlock_addrs[uaddr_idx].addr2;
  381. debug("unlock addresses are 0x%lx/0x%lx\n",
  382. info->addr_unlock1, info->addr_unlock2);
  383. sect_cnt = 0;
  384. total_size = 0;
  385. for (i = 0; i < jedec_entry->NumEraseRegions; i++) {
  386. ulong erase_region_size = jedec_entry->regions[i] >> 8;
  387. ulong erase_region_count = (jedec_entry->regions[i] & 0xff) + 1;
  388. total_size += erase_region_size * erase_region_count;
  389. debug("erase_region_count = %ld erase_region_size = %ld\n",
  390. erase_region_count, erase_region_size);
  391. for (j = 0; j < erase_region_count; j++) {
  392. if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
  393. printf("ERROR: too many flash sectors\n");
  394. break;
  395. }
  396. info->start[sect_cnt] = base;
  397. base += (erase_region_size * size_ratio);
  398. sect_cnt++;
  399. }
  400. }
  401. info->sector_count = sect_cnt;
  402. info->size = total_size * size_ratio;
  403. }
  404. /*-----------------------------------------------------------------------
  405. * match jedec ids against table. If a match is found, fill flash_info entry
  406. */
  407. int jedec_flash_match(flash_info_t *info, ulong base)
  408. {
  409. int ret = 0;
  410. int i;
  411. ulong mask = 0xFFFF;
  412. if (info->chipwidth == 1)
  413. mask = 0xFF;
  414. for (i = 0; i < ARRAY_SIZE(jedec_table); i++) {
  415. if ((jedec_table[i].mfr_id & mask) == (info->manufacturer_id & mask) &&
  416. (jedec_table[i].dev_id & mask) == (info->device_id & mask)) {
  417. fill_info(info, &jedec_table[i], base);
  418. ret = 1;
  419. break;
  420. }
  421. }
  422. return ret;
  423. }