flash.c 11 KB

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