flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002
  6. * Gregory E. Allen, gallen@arlut.utexas.edu
  7. * Matthew E. Karger, karger@arlut.utexas.edu
  8. * Applied Research Laboratories, The University of Texas at Austin
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <mpc824x.h>
  30. #include <asm/processor.h>
  31. #define ROM_CS0_START 0xFF800000
  32. #define ROM_CS1_START 0xFF000000
  33. #if defined(CFG_ENV_IS_IN_FLASH)
  34. # ifndef CFG_ENV_ADDR
  35. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  36. # endif
  37. # ifndef CFG_ENV_SIZE
  38. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  39. # endif
  40. # ifndef CFG_ENV_SECT_SIZE
  41. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  42. # endif
  43. #endif
  44. #define FLASH_BANK_SIZE 0x200000
  45. #define MAIN_SECT_SIZE 0x10000
  46. #define SECT_SIZE_32KB 0x8000
  47. #define SECT_SIZE_8KB 0x2000
  48. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  49. static int write_word (flash_info_t *info, ulong dest, ulong data);
  50. static __inline__ unsigned long get_msr(void)
  51. { unsigned long msr;
  52. __asm__ __volatile__ ("mfmsr %0" : "=r" (msr) :);
  53. return msr;
  54. }
  55. static __inline__ void set_msr(unsigned long msr)
  56. {
  57. __asm__ __volatile__ ("mtmsr %0" : : "r" (msr));
  58. }
  59. /*flash command address offsets*/
  60. #define ADDR0 (0x555)
  61. #define ADDR1 (0xAAA)
  62. #define ADDR3 (0x001)
  63. #define FLASH_WORD_SIZE unsigned char
  64. /*---------------------------------------------------------------------*/
  65. /*#define DEBUG_FLASH 1 */
  66. /*---------------------------------------------------------------------*/
  67. unsigned long flash_init(void)
  68. {
  69. int i, j;
  70. ulong size = 0;
  71. unsigned char manuf_id, device_id;
  72. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  73. {
  74. vu_char *addr = (vu_char *)(CFG_FLASH_BASE + i * FLASH_BANK_SIZE);
  75. addr[0x555] = 0xAA; /* 3 cycles to read device info. See */
  76. addr[0x2AA] = 0x55; /* AM29LV116D datasheet for list of */
  77. addr[0x555] = 0x90; /* available commands. */
  78. manuf_id = addr[0];
  79. device_id = addr[1];
  80. #if defined DEBUG_FLASH
  81. printf("manuf_id = %x, device_id = %x\n", manuf_id, device_id);
  82. #endif
  83. if ( (manuf_id == (uchar)(AMD_MANUFACT)) &&
  84. ( device_id == AMD_ID_LV116DT))
  85. {
  86. flash_info[i].flash_id = ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) |
  87. (AMD_ID_LV116DT & FLASH_TYPEMASK);
  88. } else {
  89. flash_info[i].flash_id = FLASH_UNKNOWN;
  90. addr[0] = (long)0xFFFFFFFF;
  91. goto Done;
  92. }
  93. #if defined DEBUG_FLASH
  94. printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
  95. #endif
  96. addr[0] = (long)0xFFFFFFFF;
  97. flash_info[i].size = FLASH_BANK_SIZE;
  98. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  99. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  100. for (j = 0; j < flash_info[i].sector_count; j++)
  101. {
  102. if (j < (CFG_MAX_FLASH_SECT - 3) )
  103. flash_info[i].start[j] = CFG_FLASH_BASE + i * FLASH_BANK_SIZE +
  104. j * MAIN_SECT_SIZE;
  105. else if (j == (CFG_MAX_FLASH_SECT - 3) )
  106. flash_info[i].start[j] = flash_info[i].start[j-1] + SECT_SIZE_32KB;
  107. else
  108. flash_info[i].start[j] = flash_info[i].start[j-1] + SECT_SIZE_8KB;
  109. }
  110. size += flash_info[i].size;
  111. }
  112. /* Protect monitor and environment sectors
  113. */
  114. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  115. flash_protect(FLAG_PROTECT_SET, CFG_MONITOR_BASE,
  116. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
  117. #endif
  118. #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
  119. flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR,
  120. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  121. #endif
  122. Done:
  123. return size;
  124. }
  125. /*-----------------------------------------------------------------------
  126. */
  127. void flash_print_info(flash_info_t *info)
  128. {
  129. static const char unk[] = "Unknown";
  130. const char *mfct = unk, *type = unk;
  131. unsigned int i;
  132. if(info->flash_id != FLASH_UNKNOWN)
  133. {
  134. switch(info->flash_id & FLASH_VENDMASK)
  135. {
  136. case FLASH_MAN_AMD: mfct = "AMD"; break;
  137. case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
  138. case FLASH_MAN_STM: mfct = "STM"; break;
  139. case FLASH_MAN_SST: mfct = "SST"; break;
  140. case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
  141. case FLASH_MAN_INTEL: mfct = "Intel"; break;
  142. }
  143. switch(info->flash_id & FLASH_TYPEMASK)
  144. {
  145. case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
  146. case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
  147. case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
  148. case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
  149. case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
  150. case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
  151. case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
  152. case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
  153. case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
  154. case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
  155. case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
  156. }
  157. }
  158. printf(
  159. "\n Brand: %s Type: %s\n"
  160. " Size: %lu KB in %d Sectors\n",
  161. mfct,
  162. type,
  163. info->size >> 10,
  164. info->sector_count
  165. );
  166. printf (" Sector Start Addresses:");
  167. for (i = 0; i < info->sector_count; i++)
  168. {
  169. unsigned long size;
  170. unsigned int erased;
  171. unsigned long * flash = (unsigned long *) info->start[i];
  172. /*
  173. * Check if whole sector is erased
  174. */
  175. size =
  176. (i != (info->sector_count - 1)) ?
  177. (info->start[i + 1] - info->start[i]) >> 2 :
  178. (info->start[0] + info->size - info->start[i]) >> 2;
  179. for(
  180. flash = (unsigned long *) info->start[i], erased = 1;
  181. (flash != (unsigned long *) info->start[i] + size) && erased;
  182. flash++
  183. )
  184. erased = *flash == ~0x0UL;
  185. printf(
  186. "%s %08lX %s %s",
  187. (i % 5) ? "" : "\n ",
  188. info->start[i],
  189. erased ? "E" : " ",
  190. info->protect[i] ? "RO" : " "
  191. );
  192. }
  193. puts("\n");
  194. return;
  195. }
  196. /*-----------------------------------------------------------------------
  197. */
  198. int flash_erase (flash_info_t *info, int s_first, int s_last)
  199. {
  200. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  201. int flag, prot, sect, l_sect;
  202. ulong start, now, last;
  203. unsigned char sh8b;
  204. if ((s_first < 0) || (s_first > s_last)) {
  205. if (info->flash_id == FLASH_UNKNOWN) {
  206. printf ("- missing\n");
  207. } else {
  208. printf ("- no sectors to erase\n");
  209. }
  210. return 1;
  211. }
  212. if ((info->flash_id == FLASH_UNKNOWN) ||
  213. (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
  214. printf ("Can't erase unknown flash type - aborted\n");
  215. return 1;
  216. }
  217. prot = 0;
  218. for (sect=s_first; sect<=s_last; ++sect) {
  219. if (info->protect[sect]) {
  220. prot++;
  221. }
  222. }
  223. if (prot) {
  224. printf ("- Warning: %d protected sectors will not be erased!\n",
  225. prot);
  226. } else {
  227. printf ("\n");
  228. }
  229. l_sect = -1;
  230. /* Check the ROM CS */
  231. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
  232. sh8b = 3;
  233. else
  234. sh8b = 0;
  235. /* Disable interrupts which might cause a timeout here */
  236. flag = disable_interrupts();
  237. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  238. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  239. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  240. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  241. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  242. /* Start erase on unprotected sectors */
  243. for (sect = s_first; sect<=s_last; sect++)
  244. {
  245. if (info->protect[sect] == 0)
  246. { /* not protected */
  247. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  248. (info->start[sect] - info->start[0]) << sh8b));
  249. if (info->flash_id & FLASH_MAN_SST)
  250. {
  251. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  252. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  253. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  254. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  255. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  256. addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  257. udelay(30000); /* wait 30 ms */
  258. }
  259. else
  260. addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  261. l_sect = sect;
  262. }
  263. }
  264. /* re-enable interrupts if necessary */
  265. if (flag)
  266. enable_interrupts();
  267. /* wait at least 80us - let's wait 1 ms */
  268. udelay (1000);
  269. /*
  270. * We wait for the last triggered sector
  271. */
  272. if (l_sect < 0)
  273. goto DONE;
  274. start = get_timer (0);
  275. last = start;
  276. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  277. (info->start[l_sect] - info->start[0]) << sh8b));
  278. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  279. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  280. printf ("Timeout\n");
  281. return 1;
  282. }
  283. /* show that we're waiting */
  284. if ((now - last) > 1000) { /* every second */
  285. serial_putc ('.');
  286. last = now;
  287. }
  288. }
  289. DONE:
  290. /* reset to read mode */
  291. addr = (FLASH_WORD_SIZE *)info->start[0];
  292. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  293. printf (" done\n");
  294. return 0;
  295. }
  296. /*-----------------------------------------------------------------------
  297. * Copy memory to flash, returns:
  298. * 0 - OK
  299. * 1 - write timeout
  300. * 2 - Flash not erased
  301. */
  302. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  303. {
  304. ulong cp, wp, data;
  305. int i, l, rc;
  306. wp = (addr & ~3); /* get lower word aligned address */
  307. /*
  308. * handle unaligned start bytes
  309. */
  310. if ((l = addr - wp) != 0) {
  311. data = 0;
  312. for (i=0, cp=wp; i<l; ++i, ++cp) {
  313. data = (data << 8) | (*(uchar *)cp);
  314. }
  315. for (; i<4 && cnt>0; ++i) {
  316. data = (data << 8) | *src++;
  317. --cnt;
  318. ++cp;
  319. }
  320. for (; cnt==0 && i<4; ++i, ++cp) {
  321. data = (data << 8) | (*(uchar *)cp);
  322. }
  323. if ((rc = write_word(info, wp, data)) != 0) {
  324. return (rc);
  325. }
  326. wp += 4;
  327. }
  328. /*
  329. * handle word aligned part
  330. */
  331. while (cnt >= 4) {
  332. data = 0;
  333. for (i=0; i<4; ++i) {
  334. data = (data << 8) | *src++;
  335. }
  336. if ((rc = write_word(info, wp, data)) != 0) {
  337. return (rc);
  338. }
  339. wp += 4;
  340. cnt -= 4;
  341. }
  342. if (cnt == 0) {
  343. return (0);
  344. }
  345. /*
  346. * handle unaligned tail bytes
  347. */
  348. data = 0;
  349. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  350. data = (data << 8) | *src++;
  351. --cnt;
  352. }
  353. for (; i<4; ++i, ++cp) {
  354. data = (data << 8) | (*(uchar *)cp);
  355. }
  356. return (write_word(info, wp, data));
  357. }
  358. /*-----------------------------------------------------------------------
  359. * Write a word to Flash, returns:
  360. * 0 - OK
  361. * 1 - write timeout
  362. * 2 - Flash not erased
  363. */
  364. static int write_word (flash_info_t *info, ulong dest, ulong data)
  365. {
  366. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
  367. volatile FLASH_WORD_SIZE *dest2;
  368. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  369. ulong start;
  370. int flag;
  371. int i;
  372. unsigned char sh8b;
  373. /* Check the ROM CS */
  374. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
  375. sh8b = 3;
  376. else
  377. sh8b = 0;
  378. dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
  379. info->start[0]);
  380. /* Check if Flash is (sufficiently) erased */
  381. if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  382. return (2);
  383. }
  384. /* Disable interrupts which might cause a timeout here */
  385. flag = disable_interrupts();
  386. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  387. {
  388. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  389. addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  390. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
  391. dest2[i << sh8b] = data2[i];
  392. /* re-enable interrupts if necessary */
  393. if (flag)
  394. enable_interrupts();
  395. /* data polling for D7 */
  396. start = get_timer (0);
  397. while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
  398. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  399. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  400. return (1);
  401. }
  402. }
  403. }
  404. return (0);
  405. }
  406. /*-----------------------------------------------------------------------
  407. */