flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <configs/sacsng.h>
  9. #undef DEBUG
  10. #ifndef CONFIG_ENV_ADDR
  11. #define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  12. #endif
  13. #ifndef CONFIG_ENV_SIZE
  14. #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  15. #endif
  16. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  17. /*-----------------------------------------------------------------------
  18. * Functions
  19. */
  20. static ulong flash_get_size (vu_short *addr, flash_info_t *info);
  21. static int write_word (flash_info_t *info, ulong dest, ulong data);
  22. /*-----------------------------------------------------------------------
  23. */
  24. unsigned long flash_init (void)
  25. {
  26. unsigned long size_b0, size_b1;
  27. int i;
  28. /* Init: no FLASHes known */
  29. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  30. flash_info[i].flash_id = FLASH_UNKNOWN;
  31. }
  32. size_b0 = flash_get_size((vu_short *)CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
  33. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  34. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  35. size_b0, size_b0<<20);
  36. }
  37. size_b1 = flash_get_size((vu_short *)CONFIG_SYS_FLASH1_BASE, &flash_info[1]);
  38. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  39. /* monitor protection ON by default */
  40. flash_protect(FLAG_PROTECT_SET,
  41. CONFIG_SYS_MONITOR_BASE,
  42. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  43. &flash_info[0]);
  44. #endif
  45. #ifdef CONFIG_ENV_IS_IN_FLASH
  46. /* ENV protection ON by default */
  47. flash_protect(FLAG_PROTECT_SET,
  48. CONFIG_ENV_ADDR,
  49. CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
  50. &flash_info[0]);
  51. #endif
  52. if (size_b1) {
  53. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  54. /* monitor protection ON by default */
  55. flash_protect(FLAG_PROTECT_SET,
  56. CONFIG_SYS_MONITOR_BASE,
  57. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  58. &flash_info[1]);
  59. #endif
  60. #ifdef CONFIG_ENV_IS_IN_FLASH
  61. /* ENV protection ON by default */
  62. flash_protect(FLAG_PROTECT_SET,
  63. CONFIG_ENV_ADDR,
  64. CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
  65. &flash_info[1]);
  66. #endif
  67. } else {
  68. flash_info[1].flash_id = FLASH_UNKNOWN;
  69. flash_info[1].sector_count = -1;
  70. }
  71. flash_info[0].size = size_b0;
  72. flash_info[1].size = size_b1;
  73. /*
  74. * We only report the primary flash for U-Boot's use.
  75. */
  76. return (size_b0);
  77. }
  78. /*-----------------------------------------------------------------------
  79. */
  80. void flash_print_info (flash_info_t *info)
  81. {
  82. int i;
  83. if (info->flash_id == FLASH_UNKNOWN) {
  84. printf ("missing or unknown FLASH type\n");
  85. return;
  86. }
  87. switch (info->flash_id & FLASH_VENDMASK) {
  88. case FLASH_MAN_AMD: printf ("AMD "); break;
  89. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  90. default: printf ("Unknown Vendor "); break;
  91. }
  92. switch (info->flash_id & FLASH_TYPEMASK) {
  93. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  94. break;
  95. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  96. break;
  97. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  98. break;
  99. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  100. break;
  101. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  102. break;
  103. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  104. break;
  105. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  106. break;
  107. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  108. break;
  109. default: printf ("Unknown Chip Type\n");
  110. break;
  111. }
  112. printf (" Size: %ld MB in %d Sectors\n",
  113. info->size >> 20, info->sector_count);
  114. printf (" Sector Start Addresses:");
  115. for (i=0; i<info->sector_count; ++i) {
  116. if ((i % 5) == 0)
  117. printf ("\n ");
  118. printf (" %08lX%s",
  119. info->start[i],
  120. info->protect[i] ? " (RO)" : " "
  121. );
  122. }
  123. printf ("\n");
  124. return;
  125. }
  126. /*-----------------------------------------------------------------------
  127. */
  128. /*-----------------------------------------------------------------------
  129. */
  130. /*
  131. * The following code cannot be run from FLASH!
  132. */
  133. static ulong flash_get_size (vu_short *addr, flash_info_t *info)
  134. {
  135. short i;
  136. ushort value;
  137. ulong base = (ulong)addr;
  138. /* Write auto select command: read Manufacturer ID */
  139. addr[0x0555] = 0xAAAA;
  140. addr[0x02AA] = 0x5555;
  141. addr[0x0555] = 0x9090;
  142. __asm__ __volatile__(" sync\n ");
  143. value = addr[0];
  144. #ifdef DEBUG
  145. printf("Flash manufacturer 0x%04X\n", value);
  146. #endif
  147. if(value == (ushort)AMD_MANUFACT) {
  148. info->flash_id = FLASH_MAN_AMD;
  149. } else if (value == (ushort)FUJ_MANUFACT) {
  150. info->flash_id = FLASH_MAN_FUJ;
  151. } else {
  152. #ifdef DEBUG
  153. printf("Unknown flash manufacturer 0x%04X\n", value);
  154. #endif
  155. info->flash_id = FLASH_UNKNOWN;
  156. info->sector_count = 0;
  157. info->size = 0;
  158. return (0); /* no or unknown flash */
  159. }
  160. value = addr[1]; /* device ID */
  161. #ifdef DEBUG
  162. printf("Flash type 0x%04X\n", value);
  163. #endif
  164. if(value == (ushort)AMD_ID_LV400T) {
  165. info->flash_id += FLASH_AM400T;
  166. info->sector_count = 11;
  167. info->size = 0x00080000; /* => 0.5 MB */
  168. } else if(value == (ushort)AMD_ID_LV400B) {
  169. info->flash_id += FLASH_AM400B;
  170. info->sector_count = 11;
  171. info->size = 0x00080000; /* => 0.5 MB */
  172. } else if(value == (ushort)AMD_ID_LV800T) {
  173. info->flash_id += FLASH_AM800T;
  174. info->sector_count = 19;
  175. info->size = 0x00100000; /* => 1 MB */
  176. } else if(value == (ushort)AMD_ID_LV800B) {
  177. info->flash_id += FLASH_AM800B;
  178. info->sector_count = 19;
  179. info->size = 0x00100000; /* => 1 MB */
  180. } else if(value == (ushort)AMD_ID_LV160T) {
  181. info->flash_id += FLASH_AM160T;
  182. info->sector_count = 35;
  183. info->size = 0x00200000; /* => 2 MB */
  184. } else if(value == (ushort)AMD_ID_LV160B) {
  185. info->flash_id += FLASH_AM160B;
  186. info->sector_count = 35;
  187. info->size = 0x00200000; /* => 2 MB */
  188. } else if(value == (ushort)AMD_ID_LV320T) {
  189. info->flash_id += FLASH_AM320T;
  190. info->sector_count = 67;
  191. info->size = 0x00400000; /* => 4 MB */
  192. } else if(value == (ushort)AMD_ID_LV320B) {
  193. info->flash_id += FLASH_AM320B;
  194. info->sector_count = 67;
  195. info->size = 0x00400000; /* => 4 MB */
  196. } else {
  197. #ifdef DEBUG
  198. printf("Unknown flash type 0x%04X\n", value);
  199. info->size = CONFIG_SYS_FLASH_SIZE;
  200. #else
  201. info->flash_id = FLASH_UNKNOWN;
  202. return (0); /* => no or unknown flash */
  203. #endif
  204. }
  205. /* set up sector start address table */
  206. if (info->flash_id & FLASH_BTYPE) {
  207. /* set sector offsets for bottom boot block type */
  208. info->start[0] = base + 0x00000000;
  209. info->start[1] = base + 0x00004000;
  210. info->start[2] = base + 0x00006000;
  211. info->start[3] = base + 0x00008000;
  212. for (i = 4; i < info->sector_count; i++) {
  213. info->start[i] = base + ((i - 3) * 0x00010000);
  214. }
  215. } else {
  216. /* set sector offsets for top boot block type */
  217. i = info->sector_count - 1;
  218. info->start[i--] = base + info->size - 0x00004000;
  219. info->start[i--] = base + info->size - 0x00006000;
  220. info->start[i--] = base + info->size - 0x00008000;
  221. for (; i >= 0; i--) {
  222. info->start[i] = base + (i * 0x00010000);
  223. }
  224. }
  225. /* check for protected sectors */
  226. for (i = 0; i < info->sector_count; i++) {
  227. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  228. /* D0 = 1 if protected */
  229. addr = (volatile unsigned short *)(info->start[i]);
  230. info->protect[i] = addr[2] & 1;
  231. }
  232. /*
  233. * Prevent writes to uninitialized FLASH.
  234. */
  235. if (info->flash_id != FLASH_UNKNOWN) {
  236. addr = (volatile unsigned short *)info->start[0];
  237. }
  238. addr[0] = 0xF0F0; /* reset bank */
  239. __asm__ __volatile__(" sync\n ");
  240. return (info->size);
  241. }
  242. /*-----------------------------------------------------------------------
  243. */
  244. int flash_erase (flash_info_t *info, int s_first, int s_last)
  245. {
  246. vu_short *addr = (vu_short*)(info->start[0]);
  247. int flag, prot, sect, l_sect;
  248. ulong start, now, last;
  249. if ((s_first < 0) || (s_first > s_last)) {
  250. if (info->flash_id == FLASH_UNKNOWN) {
  251. printf ("- missing\n");
  252. } else {
  253. printf ("- no sectors to erase\n");
  254. }
  255. return 1;
  256. }
  257. if ((info->flash_id == FLASH_UNKNOWN) ||
  258. (info->flash_id > FLASH_AMD_COMP)) {
  259. printf ("Can't erase unknown flash type %08lx - aborted\n",
  260. info->flash_id);
  261. return 1;
  262. }
  263. prot = 0;
  264. for (sect=s_first; sect<=s_last; ++sect) {
  265. if (info->protect[sect]) {
  266. prot++;
  267. }
  268. }
  269. if (prot) {
  270. printf ("- Warning: %d protected sectors will not be erased!\n",
  271. prot);
  272. } else {
  273. printf ("\n");
  274. }
  275. l_sect = -1;
  276. /* Disable interrupts which might cause a timeout here */
  277. flag = disable_interrupts();
  278. addr[0x0555] = 0xAAAA;
  279. addr[0x02AA] = 0x5555;
  280. addr[0x0555] = 0x8080;
  281. addr[0x0555] = 0xAAAA;
  282. addr[0x02AA] = 0x5555;
  283. __asm__ __volatile__(" sync\n ");
  284. /* Start erase on unprotected sectors */
  285. for (sect = s_first; sect<=s_last; sect++) {
  286. if (info->protect[sect] == 0) { /* not protected */
  287. addr = (vu_short*)(info->start[sect]);
  288. addr[0] = 0x3030;
  289. l_sect = sect;
  290. }
  291. }
  292. /* re-enable interrupts if necessary */
  293. if (flag)
  294. enable_interrupts();
  295. /* wait at least 80us - let's wait 1 ms */
  296. udelay (1000);
  297. /*
  298. * We wait for the last triggered sector
  299. */
  300. if (l_sect < 0)
  301. goto DONE;
  302. start = get_timer (0);
  303. last = start;
  304. addr = (vu_short*)(info->start[l_sect]);
  305. while ((addr[0] & 0x0080) != 0x0080) {
  306. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  307. printf ("Timeout\n");
  308. addr[0] = 0xF0F0; /* reset bank */
  309. __asm__ __volatile__(" sync\n ");
  310. return 1;
  311. }
  312. /* show that we're waiting */
  313. if ((now - last) > 1000) { /* every second */
  314. putc ('.');
  315. last = now;
  316. }
  317. }
  318. DONE:
  319. /* reset to read mode */
  320. addr = (vu_short*)info->start[0];
  321. addr[0] = 0xF0F0; /* reset bank */
  322. __asm__ __volatile__(" sync\n ");
  323. printf (" done\n");
  324. return 0;
  325. }
  326. /*-----------------------------------------------------------------------
  327. * Copy memory to flash, returns:
  328. * 0 - OK
  329. * 1 - write timeout
  330. * 2 - Flash not erased
  331. */
  332. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  333. {
  334. ulong cp, wp, data;
  335. int i, l, rc;
  336. wp = (addr & ~3); /* get lower word aligned address */
  337. /*
  338. * handle unaligned start bytes
  339. */
  340. if ((l = addr - wp) != 0) {
  341. data = 0;
  342. for (i=0, cp=wp; i<l; ++i, ++cp) {
  343. data = (data << 8) | (*(uchar *)cp);
  344. }
  345. for (; i<4 && cnt>0; ++i) {
  346. data = (data << 8) | *src++;
  347. --cnt;
  348. ++cp;
  349. }
  350. for (; cnt==0 && i<4; ++i, ++cp) {
  351. data = (data << 8) | (*(uchar *)cp);
  352. }
  353. if ((rc = write_word(info, wp, data)) != 0) {
  354. return (rc);
  355. }
  356. wp += 4;
  357. }
  358. /*
  359. * handle word aligned part
  360. */
  361. while (cnt >= 4) {
  362. data = 0;
  363. for (i=0; i<4; ++i) {
  364. data = (data << 8) | *src++;
  365. }
  366. if ((rc = write_word(info, wp, data)) != 0) {
  367. return (rc);
  368. }
  369. wp += 4;
  370. cnt -= 4;
  371. }
  372. if (cnt == 0) {
  373. return (0);
  374. }
  375. /*
  376. * handle unaligned tail bytes
  377. */
  378. data = 0;
  379. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  380. data = (data << 8) | *src++;
  381. --cnt;
  382. }
  383. for (; i<4; ++i, ++cp) {
  384. data = (data << 8) | (*(uchar *)cp);
  385. }
  386. return (write_word(info, wp, data));
  387. }
  388. /*-----------------------------------------------------------------------
  389. * Write a word to Flash, returns:
  390. * 0 - OK
  391. * 1 - write timeout
  392. * 2 - Flash not erased
  393. */
  394. static int write_word (flash_info_t *info, ulong dest, ulong data)
  395. {
  396. vu_short *addr = (vu_short*)(info->start[0]);
  397. ulong start;
  398. int flag;
  399. int j;
  400. /* Check if Flash is (sufficiently) erased */
  401. if (((*(vu_long *)dest) & data) != data) {
  402. return (2);
  403. }
  404. /* Disable interrupts which might cause a timeout here */
  405. flag = disable_interrupts();
  406. /* The original routine was designed to write 32 bit words to
  407. * 32 bit wide memory. We have 16 bit wide memory so we do
  408. * two writes. We write the LSB first at dest+2 and then the
  409. * MSB at dest (lousy big endian).
  410. */
  411. dest += 2;
  412. for(j = 0; j < 2; j++) {
  413. addr[0x0555] = 0xAAAA;
  414. addr[0x02AA] = 0x5555;
  415. addr[0x0555] = 0xA0A0;
  416. __asm__ __volatile__(" sync\n ");
  417. *((vu_short *)dest) = (ushort)data;
  418. /* re-enable interrupts if necessary */
  419. if (flag)
  420. enable_interrupts();
  421. /* data polling for D7 */
  422. start = get_timer (0);
  423. while (*(vu_short *)dest != (ushort)data) {
  424. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  425. return (1);
  426. }
  427. }
  428. dest -= 2;
  429. data >>= 16;
  430. }
  431. return (0);
  432. }
  433. /*-----------------------------------------------------------------------
  434. */