jedec_flash.c 8.7 KB

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