flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. if (flashtest_h == AMD_ID_DL323B) {
  74. info->flash_id += FLASH_AMDL323B;
  75. info->sector_count = 71;
  76. info->size = 0x01000000; /* 4 * 4 MB = 16 MB */
  77. } else {
  78. info->flash_id = FLASH_UNKNOWN;
  79. return(0); /* no or unknown flash */
  80. }
  81. /* set up sector start adress table (bottom sector type) */
  82. for (i = 0; i < 8; i++) {
  83. info->start[i] = baseaddr + (i * 0x00008000);
  84. }
  85. for (i = 8; i < info->sector_count; i++) {
  86. info->start[i] = baseaddr + (i * 0x00040000) - 0x001C0000;
  87. }
  88. /* check for protected sectors */
  89. for (i = 0; i < info->sector_count; i++) {
  90. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  91. if ((V_ULONG( info->start[i] + 16 ) & 0x00010001) ||
  92. (V_ULONG( info->start[i] + 20 ) & 0x00010001)) {
  93. info->protect[i] = 1; /* D0 = 1 if protected */
  94. } else {
  95. info->protect[i] = 0;
  96. }
  97. }
  98. flash_reset();
  99. return(info->size);
  100. }
  101. /*-----------------------------------------------------------------------
  102. */
  103. unsigned long flash_init (void)
  104. {
  105. unsigned long size_b0 = 0;
  106. int i;
  107. /* Init: no FLASHes known */
  108. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  109. flash_info[i].flash_id = FLASH_UNKNOWN;
  110. }
  111. /* Static FLASH Bank configuration here (only one bank) */
  112. size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0]);
  113. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  114. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  115. size_b0, size_b0>>20);
  116. }
  117. /*
  118. * protect monitor and environment sectors
  119. */
  120. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  121. flash_protect(FLAG_PROTECT_SET,
  122. CFG_MONITOR_BASE,
  123. CFG_MONITOR_BASE+monitor_flash_len-1,
  124. &flash_info[0]);
  125. #endif
  126. #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
  127. # ifndef CFG_ENV_SIZE
  128. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  129. # endif
  130. flash_protect(FLAG_PROTECT_SET,
  131. CFG_ENV_ADDR,
  132. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  133. &flash_info[0]);
  134. #endif
  135. return (size_b0);
  136. }
  137. /*-----------------------------------------------------------------------
  138. */
  139. void flash_print_info (flash_info_t *info)
  140. {
  141. int i;
  142. if (info->flash_id == FLASH_UNKNOWN) {
  143. printf ("missing or unknown FLASH type\n");
  144. return;
  145. }
  146. switch ((info->flash_id >> 16) & 0xff) {
  147. case FLASH_MAN_AMD: printf ("AMD "); break;
  148. default: printf ("Unknown Vendor "); break;
  149. }
  150. switch (info->flash_id & FLASH_TYPEMASK) {
  151. case FLASH_AMDL323B: printf ("29DL323B (32 M, bottom sector)\n");
  152. break;
  153. default: printf ("Unknown Chip Type\n");
  154. break;
  155. }
  156. printf (" Size: %ld MB in %d Sectors\n",
  157. info->size >> 20, info->sector_count);
  158. printf (" Sector Start Addresses:");
  159. for (i=0; i<info->sector_count; ++i) {
  160. if ((i % 5) == 0)
  161. printf ("\n ");
  162. printf (" %08lX%s",
  163. info->start[i],
  164. info->protect[i] ? " (RO)" : " "
  165. );
  166. }
  167. printf ("\n");
  168. return;
  169. }
  170. /*-----------------------------------------------------------------------
  171. */
  172. int flash_erase (flash_info_t *info, int s_first, int s_last)
  173. {
  174. int flag, prot, sect, l_sect;
  175. ulong start, now, last;
  176. if ((s_first < 0) || (s_first > s_last)) {
  177. if (info->flash_id == FLASH_UNKNOWN) {
  178. printf ("- missing\n");
  179. } else {
  180. printf ("- no sectors to erase\n");
  181. }
  182. return 1;
  183. }
  184. prot = 0;
  185. for (sect = s_first; sect <= s_last; sect++) {
  186. if (info->protect[sect])
  187. prot++;
  188. }
  189. if (prot) {
  190. printf ("- Warning: %d protected sectors will not be erased!\n",
  191. prot);
  192. } else {
  193. printf ("\n");
  194. }
  195. l_sect = -1;
  196. /* Disable interrupts which might cause a timeout here */
  197. flag = disable_interrupts();
  198. V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA;
  199. V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055;
  200. V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00800080;
  201. V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA;
  202. V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055;
  203. V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA;
  204. V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055;
  205. V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00800080;
  206. V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA;
  207. V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055;
  208. udelay (1000);
  209. /* Start erase on unprotected sectors */
  210. for (sect = s_first; sect<=s_last; sect++) {
  211. if (info->protect[sect] == 0) { /* not protected */
  212. V_ULONG( info->start[sect] ) = 0x00300030;
  213. V_ULONG( info->start[sect] + 4 ) = 0x00300030;
  214. l_sect = sect;
  215. }
  216. }
  217. /* re-enable interrupts if necessary */
  218. if (flag)
  219. enable_interrupts();
  220. /* wait at least 80us - let's wait 1 ms */
  221. udelay (1000);
  222. /*
  223. * We wait for the last triggered sector
  224. */
  225. if (l_sect < 0)
  226. goto DONE;
  227. start = get_timer (0);
  228. last = start;
  229. while ((V_ULONG( info->start[l_sect] ) & 0x00800080) != 0x00800080 ||
  230. (V_ULONG( info->start[l_sect] + 4 ) & 0x00800080) != 0x00800080)
  231. {
  232. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  233. printf ("Timeout\n");
  234. return 1;
  235. }
  236. /* show that we're waiting */
  237. if ((now - last) > 1000) { /* every second */
  238. serial_putc ('.');
  239. last = now;
  240. }
  241. }
  242. DONE:
  243. /* reset to read mode */
  244. flash_reset ();
  245. printf (" done\n");
  246. return 0;
  247. }
  248. static int write_dword (flash_info_t *, ulong, unsigned char *);
  249. /*-----------------------------------------------------------------------
  250. * Copy memory to flash, returns:
  251. * 0 - OK
  252. * 1 - write timeout
  253. * 2 - Flash not erased
  254. */
  255. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  256. {
  257. ulong dp;
  258. static unsigned char bb[8];
  259. int i, l, rc, cc = cnt;
  260. dp = (addr & ~7); /* get lower dword aligned address */
  261. /*
  262. * handle unaligned start bytes
  263. */
  264. if ((l = addr - dp) != 0) {
  265. for (i = 0; i < 8; i++)
  266. bb[i] = (i < l || (i-l) >= cc) ? V_BYTE(dp+i) : *src++;
  267. if ((rc = write_dword(info, dp, bb)) != 0)
  268. {
  269. return (rc);
  270. }
  271. dp += 8;
  272. cc -= 8 - l;
  273. }
  274. /*
  275. * handle word aligned part
  276. */
  277. while (cc >= 8) {
  278. if ((rc = write_dword(info, dp, src)) != 0) {
  279. return (rc);
  280. }
  281. dp += 8;
  282. src += 8;
  283. cc -= 8;
  284. }
  285. if (cc <= 0) {
  286. return (0);
  287. }
  288. /*
  289. * handle unaligned tail bytes
  290. */
  291. for (i = 0; i < 8; i++) {
  292. bb[i] = (i < cc) ? *src++ : V_BYTE(dp+i);
  293. }
  294. return (write_dword(info, dp, bb));
  295. }
  296. /*-----------------------------------------------------------------------
  297. * Write a dword to Flash, returns:
  298. * 0 - OK
  299. * 1 - write timeout
  300. * 2 - Flash not erased
  301. */
  302. static int write_dword (flash_info_t *info, ulong dest, unsigned char * pdata)
  303. {
  304. ulong start;
  305. ulong cl = 0, ch =0;
  306. int flag, i;
  307. for (ch=0, i=0; i < 4; i++)
  308. ch = (ch << 8) + *pdata++; /* high word */
  309. for (cl=0, i=0; i < 4; i++)
  310. cl = (cl << 8) + *pdata++; /* low word */
  311. /* Check if Flash is (sufficiently) erased */
  312. if ((*((vu_long *)dest) & ch) != ch
  313. ||(*((vu_long *)(dest + 4)) & cl) != cl)
  314. {
  315. return (2);
  316. }
  317. /* Disable interrupts which might cause a timeout here */
  318. flag = disable_interrupts();
  319. V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA;
  320. V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055;
  321. V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00A000A0;
  322. V_ULONG( dest ) = ch;
  323. V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA;
  324. V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055;
  325. V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00A000A0;
  326. V_ULONG( dest + 4 ) = cl;
  327. /* re-enable interrupts if necessary */
  328. if (flag)
  329. enable_interrupts();
  330. /* data polling for D7 */
  331. start = get_timer (0);
  332. while (((V_ULONG( dest ) & 0x00800080) != (ch & 0x00800080)) ||
  333. ((V_ULONG( dest + 4 ) & 0x00800080) != (cl & 0x00800080))) {
  334. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  335. return (1);
  336. }
  337. }
  338. return (0);
  339. }