flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * (C) Copyright 2001, 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Flash Routines for AMD devices on the TQM8260 board
  6. *
  7. *--------------------------------------------------------------------
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <mpc8xx.h>
  28. #define V_ULONG(a) (*(volatile unsigned long *)( a ))
  29. #define V_BYTE(a) (*(volatile unsigned char *)( a ))
  30. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  31. /*-----------------------------------------------------------------------
  32. */
  33. void flash_reset (void)
  34. {
  35. if (flash_info[0].flash_id != FLASH_UNKNOWN) {
  36. V_ULONG (flash_info[0].start[0]) = 0x00F000F0;
  37. V_ULONG (flash_info[0].start[0] + 4) = 0x00F000F0;
  38. }
  39. }
  40. /*-----------------------------------------------------------------------
  41. */
  42. ulong flash_get_size (ulong baseaddr, flash_info_t * info)
  43. {
  44. short i;
  45. unsigned long flashtest_h, flashtest_l;
  46. /* Write auto select command sequence and test FLASH answer */
  47. V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
  48. V_ULONG (baseaddr + ((ulong) 0x02AA << 3)) = 0x00550055;
  49. V_ULONG (baseaddr + ((ulong) 0x0555 << 3)) = 0x00900090;
  50. V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00AA00AA;
  51. V_ULONG (baseaddr + 4 + ((ulong) 0x02AA << 3)) = 0x00550055;
  52. V_ULONG (baseaddr + 4 + ((ulong) 0x0555 << 3)) = 0x00900090;
  53. flashtest_h = V_ULONG (baseaddr); /* manufacturer ID */
  54. flashtest_l = V_ULONG (baseaddr + 4);
  55. switch ((int) flashtest_h) {
  56. case AMD_MANUFACT:
  57. info->flash_id = FLASH_MAN_AMD;
  58. break;
  59. case FUJ_MANUFACT:
  60. info->flash_id = FLASH_MAN_FUJ;
  61. break;
  62. default:
  63. info->flash_id = FLASH_UNKNOWN;
  64. info->sector_count = 0;
  65. info->size = 0;
  66. return (0); /* no or unknown flash */
  67. }
  68. flashtest_h = V_ULONG (baseaddr + 8); /* device ID */
  69. flashtest_l = V_ULONG (baseaddr + 12);
  70. if (flashtest_h != flashtest_l) {
  71. info->flash_id = FLASH_UNKNOWN;
  72. } else {
  73. switch (flashtest_h) {
  74. case AMD_ID_LV800T:
  75. info->flash_id += FLASH_AM800T;
  76. info->sector_count = 19;
  77. info->size = 0x00400000;
  78. break; /* 4 * 1 MB = 4 MB */
  79. case AMD_ID_LV800B:
  80. info->flash_id += FLASH_AM800B;
  81. info->sector_count = 19;
  82. info->size = 0x00400000;
  83. break; /* 4 * 1 MB = 4 MB */
  84. case AMD_ID_LV160T:
  85. info->flash_id += FLASH_AM160T;
  86. info->sector_count = 35;
  87. info->size = 0x00800000;
  88. break; /* 4 * 2 MB = 8 MB */
  89. case AMD_ID_LV160B:
  90. info->flash_id += FLASH_AM160B;
  91. info->sector_count = 35;
  92. info->size = 0x00800000;
  93. break; /* 4 * 2 MB = 8 MB */
  94. case AMD_ID_DL322T:
  95. info->flash_id += FLASH_AMDL322T;
  96. info->sector_count = 71;
  97. info->size = 0x01000000;
  98. break; /* 4 * 4 MB = 16 MB */
  99. case AMD_ID_DL322B:
  100. info->flash_id += FLASH_AMDL322B;
  101. info->sector_count = 71;
  102. info->size = 0x01000000;
  103. break; /* 4 * 4 MB = 16 MB */
  104. case AMD_ID_DL323T:
  105. info->flash_id += FLASH_AMDL323T;
  106. info->sector_count = 71;
  107. info->size = 0x01000000;
  108. break; /* 4 * 4 MB = 16 MB */
  109. case AMD_ID_DL323B:
  110. info->flash_id += FLASH_AMDL323B;
  111. info->sector_count = 71;
  112. info->size = 0x01000000;
  113. break; /* 4 * 4 MB = 16 MB */
  114. case AMD_ID_LV640U:
  115. info->flash_id += FLASH_AM640U;
  116. info->sector_count = 128;
  117. info->size = 0x02000000;
  118. break; /* 4 * 8 MB = 32 MB */
  119. default:
  120. info->flash_id = FLASH_UNKNOWN;
  121. return (0); /* no or unknown flash */
  122. }
  123. }
  124. if (flashtest_h == AMD_ID_LV640U) {
  125. /* set up sector start adress table (uniform sector type) */
  126. for (i = 0; i < info->sector_count; i++)
  127. info->start[i] = baseaddr + (i * 0x00040000);
  128. } else if (info->flash_id & FLASH_BTYPE) {
  129. /* set up sector start adress table (bottom sector type) */
  130. info->start[0] = baseaddr + 0x00000000;
  131. info->start[1] = baseaddr + 0x00010000;
  132. info->start[2] = baseaddr + 0x00018000;
  133. info->start[3] = baseaddr + 0x00020000;
  134. for (i = 4; i < info->sector_count; i++) {
  135. info->start[i] = baseaddr + (i * 0x00040000) - 0x000C0000;
  136. }
  137. } else {
  138. /* set up sector start adress table (top sector type) */
  139. i = info->sector_count - 1;
  140. info->start[i--] = baseaddr + info->size - 0x00010000;
  141. info->start[i--] = baseaddr + info->size - 0x00018000;
  142. info->start[i--] = baseaddr + info->size - 0x00020000;
  143. for (; i >= 0; i--) {
  144. info->start[i] = baseaddr + i * 0x00040000;
  145. }
  146. }
  147. /* check for protected sectors */
  148. for (i = 0; i < info->sector_count; i++) {
  149. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  150. if ((V_ULONG (info->start[i] + 16) & 0x00010001) ||
  151. (V_ULONG (info->start[i] + 20) & 0x00010001)) {
  152. info->protect[i] = 1; /* D0 = 1 if protected */
  153. } else {
  154. info->protect[i] = 0;
  155. }
  156. }
  157. flash_reset ();
  158. return (info->size);
  159. }
  160. /*-----------------------------------------------------------------------
  161. */
  162. unsigned long flash_init (void)
  163. {
  164. unsigned long size_b0 = 0;
  165. int i;
  166. /* Init: no FLASHes known */
  167. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  168. flash_info[i].flash_id = FLASH_UNKNOWN;
  169. }
  170. /* Static FLASH Bank configuration here (only one bank) */
  171. size_b0 = flash_get_size (CFG_FLASH0_BASE, &flash_info[0]);
  172. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  173. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  174. size_b0, size_b0 >> 20);
  175. }
  176. /*
  177. * protect monitor and environment sectors
  178. */
  179. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  180. flash_protect (FLAG_PROTECT_SET,
  181. CFG_MONITOR_BASE,
  182. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
  183. #endif
  184. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  185. # ifndef CFG_ENV_SIZE
  186. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  187. # endif
  188. flash_protect (FLAG_PROTECT_SET,
  189. CFG_ENV_ADDR,
  190. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  191. #endif
  192. return (size_b0);
  193. }
  194. /*-----------------------------------------------------------------------
  195. */
  196. void flash_print_info (flash_info_t * info)
  197. {
  198. int i;
  199. if (info->flash_id == FLASH_UNKNOWN) {
  200. printf ("missing or unknown FLASH type\n");
  201. return;
  202. }
  203. switch (info->flash_id & FLASH_VENDMASK) {
  204. case FLASH_MAN_AMD:
  205. printf ("AMD ");
  206. break;
  207. case FLASH_MAN_FUJ:
  208. printf ("FUJITSU ");
  209. break;
  210. default:
  211. printf ("Unknown Vendor ");
  212. break;
  213. }
  214. switch (info->flash_id & FLASH_TYPEMASK) {
  215. case FLASH_AM800T:
  216. printf ("29LV800T (8 M, top sector)\n");
  217. break;
  218. case FLASH_AM800B:
  219. printf ("29LV800T (8 M, bottom sector)\n");
  220. break;
  221. case FLASH_AM160T:
  222. printf ("29LV160T (16 M, top sector)\n");
  223. break;
  224. case FLASH_AM160B:
  225. printf ("29LV160B (16 M, bottom sector)\n");
  226. break;
  227. case FLASH_AMDL322T:
  228. printf ("29DL322T (32 M, top sector)\n");
  229. break;
  230. case FLASH_AMDL322B:
  231. printf ("29DL322B (32 M, bottom sector)\n");
  232. break;
  233. case FLASH_AMDL323T:
  234. printf ("29DL323T (32 M, top sector)\n");
  235. break;
  236. case FLASH_AMDL323B:
  237. printf ("29DL323B (32 M, bottom sector)\n");
  238. break;
  239. case FLASH_AM640U:
  240. printf ("29LV640D (64 M, uniform sector)\n");
  241. break;
  242. default:
  243. printf ("Unknown Chip Type\n");
  244. break;
  245. }
  246. printf (" Size: %ld MB in %d Sectors\n",
  247. info->size >> 20, info->sector_count);
  248. printf (" Sector Start Addresses:");
  249. for (i = 0; i < info->sector_count; ++i) {
  250. if ((i % 5) == 0)
  251. printf ("\n ");
  252. printf (" %08lX%s",
  253. info->start[i],
  254. info->protect[i] ? " (RO)" : " "
  255. );
  256. }
  257. printf ("\n");
  258. return;
  259. }
  260. /*-----------------------------------------------------------------------
  261. */
  262. int flash_erase (flash_info_t * info, int s_first, int s_last)
  263. {
  264. int flag, prot, sect, l_sect;
  265. ulong start, now, last;
  266. if ((s_first < 0) || (s_first > s_last)) {
  267. if (info->flash_id == FLASH_UNKNOWN) {
  268. printf ("- missing\n");
  269. } else {
  270. printf ("- no sectors to erase\n");
  271. }
  272. return 1;
  273. }
  274. prot = 0;
  275. for (sect = s_first; sect <= s_last; sect++) {
  276. if (info->protect[sect])
  277. prot++;
  278. }
  279. if (prot) {
  280. printf ("- Warning: %d protected sectors will not be erased!\n",
  281. prot);
  282. } else {
  283. printf ("\n");
  284. }
  285. l_sect = -1;
  286. /* Disable interrupts which might cause a timeout here */
  287. flag = disable_interrupts ();
  288. V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
  289. V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
  290. V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00800080;
  291. V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
  292. V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
  293. V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
  294. V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
  295. V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00800080;
  296. V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
  297. V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
  298. udelay (1000);
  299. /* Start erase on unprotected sectors */
  300. for (sect = s_first; sect <= s_last; sect++) {
  301. if (info->protect[sect] == 0) { /* not protected */
  302. V_ULONG (info->start[sect]) = 0x00300030;
  303. V_ULONG (info->start[sect] + 4) = 0x00300030;
  304. l_sect = sect;
  305. }
  306. }
  307. /* re-enable interrupts if necessary */
  308. if (flag)
  309. enable_interrupts ();
  310. /* wait at least 80us - let's wait 1 ms */
  311. udelay (1000);
  312. /*
  313. * We wait for the last triggered sector
  314. */
  315. if (l_sect < 0)
  316. goto DONE;
  317. start = get_timer (0);
  318. last = start;
  319. while ((V_ULONG (info->start[l_sect]) & 0x00800080) != 0x00800080 ||
  320. (V_ULONG (info->start[l_sect] + 4) & 0x00800080) != 0x00800080)
  321. {
  322. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  323. printf ("Timeout\n");
  324. return 1;
  325. }
  326. /* show that we're waiting */
  327. if ((now - last) > 1000) { /* every second */
  328. serial_putc ('.');
  329. last = now;
  330. }
  331. }
  332. DONE:
  333. /* reset to read mode */
  334. flash_reset ();
  335. printf (" done\n");
  336. return 0;
  337. }
  338. static int write_dword (flash_info_t *, ulong, unsigned char *);
  339. /*-----------------------------------------------------------------------
  340. * Copy memory to flash, returns:
  341. * 0 - OK
  342. * 1 - write timeout
  343. * 2 - Flash not erased
  344. */
  345. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  346. {
  347. ulong dp;
  348. static unsigned char bb[8];
  349. int i, l, rc, cc = cnt;
  350. dp = (addr & ~7); /* get lower dword aligned address */
  351. /*
  352. * handle unaligned start bytes
  353. */
  354. if ((l = addr - dp) != 0) {
  355. for (i = 0; i < 8; i++)
  356. bb[i] = (i < l || (i - l) >= cc) ? V_BYTE (dp + i) : *src++;
  357. if ((rc = write_dword (info, dp, bb)) != 0) {
  358. return (rc);
  359. }
  360. dp += 8;
  361. cc -= 8 - l;
  362. }
  363. /*
  364. * handle word aligned part
  365. */
  366. while (cc >= 8) {
  367. if ((rc = write_dword (info, dp, src)) != 0) {
  368. return (rc);
  369. }
  370. dp += 8;
  371. src += 8;
  372. cc -= 8;
  373. }
  374. if (cc <= 0) {
  375. return (0);
  376. }
  377. /*
  378. * handle unaligned tail bytes
  379. */
  380. for (i = 0; i < 8; i++) {
  381. bb[i] = (i < cc) ? *src++ : V_BYTE (dp + i);
  382. }
  383. return (write_dword (info, dp, bb));
  384. }
  385. /*-----------------------------------------------------------------------
  386. * Write a dword to Flash, returns:
  387. * 0 - OK
  388. * 1 - write timeout
  389. * 2 - Flash not erased
  390. */
  391. static int write_dword (flash_info_t * info, ulong dest, unsigned char *pdata)
  392. {
  393. ulong start, cl, ch;
  394. int flag, i;
  395. for (ch = 0, i = 0; i < 4; i++)
  396. ch = (ch << 8) + *pdata++; /* high word */
  397. for (cl = 0, i = 0; i < 4; i++)
  398. cl = (cl << 8) + *pdata++; /* low word */
  399. /* Check if Flash is (sufficiently) erased */
  400. if ((*((vu_long *) dest) & ch) != ch
  401. || (*((vu_long *) (dest + 4)) & cl) != cl) {
  402. return (2);
  403. }
  404. /* Disable interrupts which might cause a timeout here */
  405. flag = disable_interrupts ();
  406. V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00AA00AA;
  407. V_ULONG (info->start[0] + (0x02AA << 3)) = 0x00550055;
  408. V_ULONG (info->start[0] + (0x0555 << 3)) = 0x00A000A0;
  409. V_ULONG (dest) = ch;
  410. V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00AA00AA;
  411. V_ULONG (info->start[0] + 4 + (0x02AA << 3)) = 0x00550055;
  412. V_ULONG (info->start[0] + 4 + (0x0555 << 3)) = 0x00A000A0;
  413. V_ULONG (dest + 4) = cl;
  414. /* re-enable interrupts if necessary */
  415. if (flag)
  416. enable_interrupts ();
  417. /* data polling for D7 */
  418. start = get_timer (0);
  419. while (((V_ULONG (dest) & 0x00800080) != (ch & 0x00800080)) ||
  420. ((V_ULONG (dest + 4) & 0x00800080) != (cl & 0x00800080))) {
  421. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  422. return (1);
  423. }
  424. }
  425. return (0);
  426. }