flash.c 13 KB

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