jedec_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. /* MXIC */
  66. #define MX29LV040 0x004F
  67. /* WINBOND */
  68. #define W39L040A 0x00D6
  69. /* AMIC */
  70. #define A29L040 0x0092
  71. /* EON */
  72. #define EN29LV040A 0x004F
  73. /*
  74. * Unlock address sets for AMD command sets.
  75. * Intel command sets use the MTD_UADDR_UNNECESSARY.
  76. * Each identifier, except MTD_UADDR_UNNECESSARY, and
  77. * MTD_UADDR_NO_SUPPORT must be defined below in unlock_addrs[].
  78. * MTD_UADDR_NOT_SUPPORTED must be 0 so that structure
  79. * initialization need not require initializing all of the
  80. * unlock addresses for all bit widths.
  81. */
  82. enum uaddr {
  83. MTD_UADDR_NOT_SUPPORTED = 0, /* data width not supported */
  84. MTD_UADDR_0x0555_0x02AA,
  85. MTD_UADDR_0x0555_0x0AAA,
  86. MTD_UADDR_0x5555_0x2AAA,
  87. MTD_UADDR_0x0AAA_0x0555,
  88. MTD_UADDR_DONT_CARE, /* Requires an arbitrary address */
  89. MTD_UADDR_UNNECESSARY, /* Does not require any address */
  90. };
  91. struct unlock_addr {
  92. u32 addr1;
  93. u32 addr2;
  94. };
  95. /*
  96. * I don't like the fact that the first entry in unlock_addrs[]
  97. * exists, but is for MTD_UADDR_NOT_SUPPORTED - and, therefore,
  98. * should not be used. The problem is that structures with
  99. * initializers have extra fields initialized to 0. It is _very_
  100. * desireable to have the unlock address entries for unsupported
  101. * data widths automatically initialized - that means that
  102. * MTD_UADDR_NOT_SUPPORTED must be 0 and the first entry here
  103. * must go unused.
  104. */
  105. static const struct unlock_addr unlock_addrs[] = {
  106. [MTD_UADDR_NOT_SUPPORTED] = {
  107. .addr1 = 0xffff,
  108. .addr2 = 0xffff
  109. },
  110. [MTD_UADDR_0x0555_0x02AA] = {
  111. .addr1 = 0x0555,
  112. .addr2 = 0x02aa
  113. },
  114. [MTD_UADDR_0x0555_0x0AAA] = {
  115. .addr1 = 0x0555,
  116. .addr2 = 0x0aaa
  117. },
  118. [MTD_UADDR_0x5555_0x2AAA] = {
  119. .addr1 = 0x5555,
  120. .addr2 = 0x2aaa
  121. },
  122. [MTD_UADDR_0x0AAA_0x0555] = {
  123. .addr1 = 0x0AAA,
  124. .addr2 = 0x0555
  125. },
  126. [MTD_UADDR_DONT_CARE] = {
  127. .addr1 = 0x0000, /* Doesn't matter which address */
  128. .addr2 = 0x0000 /* is used - must be last entry */
  129. },
  130. [MTD_UADDR_UNNECESSARY] = {
  131. .addr1 = 0x0000,
  132. .addr2 = 0x0000
  133. }
  134. };
  135. struct amd_flash_info {
  136. const __u16 mfr_id;
  137. const __u16 dev_id;
  138. const char *name;
  139. const int DevSize;
  140. const int NumEraseRegions;
  141. const int CmdSet;
  142. const __u8 uaddr[4]; /* unlock addrs for 8, 16, 32, 64 */
  143. const ulong regions[6];
  144. };
  145. #define ERASEINFO(size,blocks) (size<<8)|(blocks-1)
  146. #define SIZE_64KiB 16
  147. #define SIZE_128KiB 17
  148. #define SIZE_256KiB 18
  149. #define SIZE_512KiB 19
  150. #define SIZE_1MiB 20
  151. #define SIZE_2MiB 21
  152. #define SIZE_4MiB 22
  153. #define SIZE_8MiB 23
  154. static const struct amd_flash_info jedec_table[] = {
  155. #ifdef CONFIG_SYS_FLASH_LEGACY_256Kx8
  156. {
  157. .mfr_id = (u16)SST_MANUFACT,
  158. .dev_id = SST39LF020,
  159. .name = "SST 39LF020",
  160. .uaddr = {
  161. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  162. },
  163. .DevSize = SIZE_256KiB,
  164. .CmdSet = P_ID_AMD_STD,
  165. .NumEraseRegions= 1,
  166. .regions = {
  167. ERASEINFO(0x01000,64),
  168. }
  169. },
  170. #endif
  171. #ifdef CONFIG_SYS_FLASH_LEGACY_512Kx8
  172. {
  173. .mfr_id = (u16)AMD_MANUFACT,
  174. .dev_id = AM29LV040B,
  175. .name = "AMD AM29LV040B",
  176. .uaddr = {
  177. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  178. },
  179. .DevSize = SIZE_512KiB,
  180. .CmdSet = P_ID_AMD_STD,
  181. .NumEraseRegions= 1,
  182. .regions = {
  183. ERASEINFO(0x10000,8),
  184. }
  185. },
  186. {
  187. .mfr_id = (u16)SST_MANUFACT,
  188. .dev_id = SST39LF040,
  189. .name = "SST 39LF040",
  190. .uaddr = {
  191. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  192. },
  193. .DevSize = SIZE_512KiB,
  194. .CmdSet = P_ID_AMD_STD,
  195. .NumEraseRegions= 1,
  196. .regions = {
  197. ERASEINFO(0x01000,128),
  198. }
  199. },
  200. {
  201. .mfr_id = (u16)STM_MANUFACT,
  202. .dev_id = STM_ID_M29W040B,
  203. .name = "ST Micro M29W040B",
  204. .uaddr = {
  205. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  206. },
  207. .DevSize = SIZE_512KiB,
  208. .CmdSet = P_ID_AMD_STD,
  209. .NumEraseRegions= 1,
  210. .regions = {
  211. ERASEINFO(0x10000,8),
  212. }
  213. },
  214. {
  215. .mfr_id = (u16)MX_MANUFACT,
  216. .dev_id = MX29LV040,
  217. .name = "MXIC MX29LV040",
  218. .uaddr = {
  219. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  220. },
  221. .DevSize = SIZE_512KiB,
  222. .CmdSet = P_ID_AMD_STD,
  223. .NumEraseRegions= 1,
  224. .regions = {
  225. ERASEINFO(0x10000, 8),
  226. }
  227. },
  228. {
  229. .mfr_id = (u16)WINB_MANUFACT,
  230. .dev_id = W39L040A,
  231. .name = "WINBOND W39L040A",
  232. .uaddr = {
  233. [0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
  234. },
  235. .DevSize = SIZE_512KiB,
  236. .CmdSet = P_ID_AMD_STD,
  237. .NumEraseRegions= 1,
  238. .regions = {
  239. ERASEINFO(0x10000, 8),
  240. }
  241. },
  242. {
  243. .mfr_id = (u16)AMIC_MANUFACT,
  244. .dev_id = A29L040,
  245. .name = "AMIC A29L040",
  246. .uaddr = {
  247. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  248. },
  249. .DevSize = SIZE_512KiB,
  250. .CmdSet = P_ID_AMD_STD,
  251. .NumEraseRegions= 1,
  252. .regions = {
  253. ERASEINFO(0x10000, 8),
  254. }
  255. },
  256. {
  257. .mfr_id = (u16)EON_MANUFACT,
  258. .dev_id = EN29LV040A,
  259. .name = "EON EN29LV040A",
  260. .uaddr = {
  261. [0] = MTD_UADDR_0x0555_0x02AA /* x8 */
  262. },
  263. .DevSize = SIZE_512KiB,
  264. .CmdSet = P_ID_AMD_STD,
  265. .NumEraseRegions= 1,
  266. .regions = {
  267. ERASEINFO(0x10000, 8),
  268. }
  269. },
  270. #endif
  271. #ifdef CONFIG_SYS_FLASH_LEGACY_512Kx16
  272. {
  273. .mfr_id = (u16)AMD_MANUFACT,
  274. .dev_id = AM29F400BB,
  275. .name = "AMD AM29F400BB",
  276. .uaddr = {
  277. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  278. },
  279. .DevSize = SIZE_512KiB,
  280. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  281. .NumEraseRegions= 4,
  282. .regions = {
  283. ERASEINFO(0x04000, 1),
  284. ERASEINFO(0x02000, 2),
  285. ERASEINFO(0x08000, 1),
  286. ERASEINFO(0x10000, 7),
  287. }
  288. },
  289. {
  290. .mfr_id = (u16)AMD_MANUFACT,
  291. .dev_id = AM29LV400BB,
  292. .name = "AMD AM29LV400BB",
  293. .uaddr = {
  294. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  295. },
  296. .DevSize = SIZE_512KiB,
  297. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  298. .NumEraseRegions= 4,
  299. .regions = {
  300. ERASEINFO(0x04000,1),
  301. ERASEINFO(0x02000,2),
  302. ERASEINFO(0x08000,1),
  303. ERASEINFO(0x10000,7),
  304. }
  305. },
  306. {
  307. .mfr_id = (u16)AMD_MANUFACT,
  308. .dev_id = AM29LV800BB,
  309. .name = "AMD AM29LV800BB",
  310. .uaddr = {
  311. [1] = MTD_UADDR_0x0555_0x02AA /* x16 */
  312. },
  313. .DevSize = SIZE_1MiB,
  314. .CmdSet = CFI_CMDSET_AMD_LEGACY,
  315. .NumEraseRegions= 4,
  316. .regions = {
  317. ERASEINFO(0x04000, 1),
  318. ERASEINFO(0x02000, 2),
  319. ERASEINFO(0x08000, 1),
  320. ERASEINFO(0x10000, 15),
  321. }
  322. },
  323. #endif
  324. };
  325. static inline void fill_info(flash_info_t *info, const struct amd_flash_info *jedec_entry, ulong base)
  326. {
  327. int i,j;
  328. int sect_cnt;
  329. int size_ratio;
  330. int total_size;
  331. enum uaddr uaddr_idx;
  332. size_ratio = info->portwidth / info->chipwidth;
  333. debug("Found JEDEC Flash: %s\n", jedec_entry->name);
  334. info->vendor = jedec_entry->CmdSet;
  335. /* Todo: do we need device-specific timeouts? */
  336. info->erase_blk_tout = 30000;
  337. info->buffer_write_tout = 1000;
  338. info->write_tout = 100;
  339. info->name = jedec_entry->name;
  340. /* copy unlock addresses from device table to CFI info struct. This
  341. is just here because the addresses are in the table anyway - if
  342. the flash is not detected due to wrong unlock addresses,
  343. flash_detect_legacy would have to try all of them before we even
  344. get here. */
  345. switch(info->chipwidth) {
  346. case FLASH_CFI_8BIT:
  347. uaddr_idx = jedec_entry->uaddr[0];
  348. break;
  349. case FLASH_CFI_16BIT:
  350. uaddr_idx = jedec_entry->uaddr[1];
  351. break;
  352. case FLASH_CFI_32BIT:
  353. uaddr_idx = jedec_entry->uaddr[2];
  354. break;
  355. default:
  356. uaddr_idx = MTD_UADDR_NOT_SUPPORTED;
  357. break;
  358. }
  359. debug("unlock address index %d\n", uaddr_idx);
  360. info->addr_unlock1 = unlock_addrs[uaddr_idx].addr1;
  361. info->addr_unlock2 = unlock_addrs[uaddr_idx].addr2;
  362. debug("unlock addresses are 0x%x/0x%x\n", info->addr_unlock1, info->addr_unlock2);
  363. sect_cnt = 0;
  364. total_size = 0;
  365. for (i = 0; i < jedec_entry->NumEraseRegions; i++) {
  366. ulong erase_region_size = jedec_entry->regions[i] >> 8;
  367. ulong erase_region_count = (jedec_entry->regions[i] & 0xff) + 1;
  368. total_size += erase_region_size * erase_region_count;
  369. debug ("erase_region_count = %d erase_region_size = %d\n",
  370. erase_region_count, erase_region_size);
  371. for (j = 0; j < erase_region_count; j++) {
  372. if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
  373. printf("ERROR: too many flash sectors\n");
  374. break;
  375. }
  376. info->start[sect_cnt] = base;
  377. base += (erase_region_size * size_ratio);
  378. sect_cnt++;
  379. }
  380. }
  381. info->sector_count = sect_cnt;
  382. info->size = total_size * size_ratio;
  383. }
  384. /*-----------------------------------------------------------------------
  385. * match jedec ids against table. If a match is found, fill flash_info entry
  386. */
  387. int jedec_flash_match(flash_info_t *info, ulong base)
  388. {
  389. int ret = 0;
  390. int i;
  391. ulong mask = 0xFFFF;
  392. if (info->chipwidth == 1)
  393. mask = 0xFF;
  394. for (i = 0; i < ARRAY_SIZE(jedec_table); i++) {
  395. if ((jedec_table[i].mfr_id & mask) == (info->manufacturer_id & mask) &&
  396. (jedec_table[i].dev_id & mask) == (info->device_id & mask)) {
  397. fill_info(info, &jedec_table[i], base);
  398. ret = 1;
  399. break;
  400. }
  401. }
  402. return ret;
  403. }