jedec_flash.c 9.9 KB

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