flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * (C) Copyright 2004
  3. * Yusdi Santoso, Adaptec Inc., yusdi_santoso@adaptec.com
  4. *
  5. * (C) Copyright 2000-2005
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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 <mpc824x.h>
  28. #include <asm/processor.h>
  29. #include <asm/pci_io.h>
  30. #include <w83c553f.h>
  31. #define ROM_CS0_START 0xFF800000
  32. #define ROM_CS1_START 0xFF000000
  33. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  34. #if defined(CONFIG_ENV_IS_IN_FLASH)
  35. # ifndef CFG_ENV_ADDR
  36. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  37. # endif
  38. # ifndef CFG_ENV_SIZE
  39. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  40. # endif
  41. # ifndef CFG_ENV_SECT_SIZE
  42. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  43. # endif
  44. #endif
  45. /*-----------------------------------------------------------------------
  46. * Functions
  47. */
  48. static int write_word (flash_info_t *info, ulong dest, ulong data);
  49. /*flash command address offsets*/
  50. #define ADDR0 (0xAAA)
  51. #define ADDR1 (0x555)
  52. #define ADDR3 (0x001)
  53. #define FLASH_WORD_SIZE unsigned char
  54. /*-----------------------------------------------------------------------
  55. */
  56. static unsigned long flash_id (unsigned char mfct, unsigned char chip)
  57. __attribute__ ((const));
  58. typedef struct {
  59. FLASH_WORD_SIZE extval;
  60. unsigned short intval;
  61. } map_entry;
  62. static unsigned long flash_id (unsigned char mfct, unsigned char chip)
  63. {
  64. static const map_entry mfct_map[] = {
  65. {(FLASH_WORD_SIZE) AMD_MANUFACT,
  66. (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
  67. {(FLASH_WORD_SIZE) FUJ_MANUFACT,
  68. (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
  69. {(FLASH_WORD_SIZE) STM_MANUFACT,
  70. (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
  71. {(FLASH_WORD_SIZE) MT_MANUFACT,
  72. (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
  73. {(FLASH_WORD_SIZE) INTEL_MANUFACT,
  74. (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
  75. {(FLASH_WORD_SIZE) INTEL_ALT_MANU,
  76. (unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
  77. };
  78. static const map_entry chip_map[] = {
  79. {AMD_ID_F040B, FLASH_AM040},
  80. {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
  81. };
  82. const map_entry *p;
  83. unsigned long result = FLASH_UNKNOWN;
  84. /* find chip id */
  85. for (p = &chip_map[0];
  86. p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
  87. if (p->extval == chip) {
  88. result = FLASH_VENDMASK | p->intval;
  89. break;
  90. }
  91. /* find vendor id */
  92. for (p = &mfct_map[0];
  93. p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
  94. if (p->extval == mfct) {
  95. result &= ~FLASH_VENDMASK;
  96. result |= (unsigned long) p->intval << 16;
  97. break;
  98. }
  99. return result;
  100. }
  101. unsigned long flash_init (void)
  102. {
  103. unsigned long i;
  104. unsigned char j;
  105. static const ulong flash_banks[] = CFG_FLASH_BANKS;
  106. /* Init: no FLASHes known */
  107. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  108. flash_info_t *const pflinfo = &flash_info[i];
  109. pflinfo->flash_id = FLASH_UNKNOWN;
  110. pflinfo->size = 0;
  111. pflinfo->sector_count = 0;
  112. }
  113. /* Enable writes to Hidden Dragon flash */
  114. {
  115. register unsigned char temp;
  116. CONFIG_READ_BYTE (CFG_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR,
  117. temp);
  118. temp &= ~0x20; /* clear BIOSWP bit */
  119. CONFIG_WRITE_BYTE (CFG_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR,
  120. temp);
  121. }
  122. for (i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++) {
  123. flash_info_t *const pflinfo = &flash_info[i];
  124. const unsigned long base_address = flash_banks[i];
  125. volatile FLASH_WORD_SIZE *const flash =
  126. (FLASH_WORD_SIZE *) base_address;
  127. flash[0xAAA << (3 * i)] = 0xaa;
  128. flash[0x555 << (3 * i)] = 0x55;
  129. flash[0xAAA << (3 * i)] = 0x90;
  130. __asm__ __volatile__ ("sync");
  131. pflinfo->flash_id =
  132. flash_id (flash[0x0], flash[0x2 + 14 * i]);
  133. switch (pflinfo->flash_id & FLASH_TYPEMASK) {
  134. case FLASH_AM040:
  135. pflinfo->size = 0x00080000;
  136. pflinfo->sector_count = 8;
  137. for (j = 0; j < 8; j++) {
  138. pflinfo->start[j] =
  139. base_address + 0x00010000 * j;
  140. pflinfo->protect[j] = flash[(j << 16) | 0x2];
  141. }
  142. break;
  143. case FLASH_STM800AB:
  144. pflinfo->size = 0x00100000;
  145. pflinfo->sector_count = 19;
  146. pflinfo->start[0] = base_address;
  147. pflinfo->start[1] = base_address + 0x4000;
  148. pflinfo->start[2] = base_address + 0x6000;
  149. pflinfo->start[3] = base_address + 0x8000;
  150. for (j = 1; j < 16; j++) {
  151. pflinfo->start[j + 3] =
  152. base_address + 0x00010000 * j;
  153. }
  154. break;
  155. default:
  156. /* The chip used is not listed in flash_id
  157. TODO: Change this to explicitly detect the flash type
  158. */
  159. {
  160. int sector_addr = base_address;
  161. pflinfo->size = 0x00200000;
  162. pflinfo->sector_count = 35;
  163. pflinfo->start[0] = sector_addr;
  164. sector_addr += 0x4000; /* 16K */
  165. pflinfo->start[1] = sector_addr;
  166. sector_addr += 0x2000; /* 8K */
  167. pflinfo->start[2] = sector_addr;
  168. sector_addr += 0x2000; /* 8K */
  169. pflinfo->start[3] = sector_addr;
  170. sector_addr += 0x8000; /* 32K */
  171. for (j = 4; j < 35; j++) {
  172. pflinfo->start[j] = sector_addr;
  173. sector_addr += 0x10000; /* 64K */
  174. }
  175. }
  176. break;
  177. }
  178. /* Protect monitor and environment sectors
  179. */
  180. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  181. flash_protect (FLAG_PROTECT_SET,
  182. CFG_MONITOR_BASE,
  183. CFG_MONITOR_BASE + monitor_flash_len - 1,
  184. &flash_info[0]);
  185. #endif
  186. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  187. flash_protect (FLAG_PROTECT_SET,
  188. CFG_ENV_ADDR,
  189. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  190. &flash_info[0]);
  191. #endif
  192. /* reset device to read mode */
  193. flash[0x0000] = 0xf0;
  194. __asm__ __volatile__ ("sync");
  195. }
  196. /* only have 1 bank */
  197. return flash_info[0].size;
  198. }
  199. /*-----------------------------------------------------------------------
  200. */
  201. void flash_print_info (flash_info_t * info)
  202. {
  203. static const char unk[] = "Unknown";
  204. const char *mfct = unk, *type = unk;
  205. unsigned int i;
  206. if (info->flash_id != FLASH_UNKNOWN) {
  207. switch (info->flash_id & FLASH_VENDMASK) {
  208. case FLASH_MAN_AMD:
  209. mfct = "AMD";
  210. break;
  211. case FLASH_MAN_FUJ:
  212. mfct = "FUJITSU";
  213. break;
  214. case FLASH_MAN_STM:
  215. mfct = "STM";
  216. break;
  217. case FLASH_MAN_SST:
  218. mfct = "SST";
  219. break;
  220. case FLASH_MAN_BM:
  221. mfct = "Bright Microelectonics";
  222. break;
  223. case FLASH_MAN_INTEL:
  224. mfct = "Intel";
  225. break;
  226. }
  227. switch (info->flash_id & FLASH_TYPEMASK) {
  228. case FLASH_AM040:
  229. type = "AM29F040B (512K * 8, uniform sector size)";
  230. break;
  231. case FLASH_AM400B:
  232. type = "AM29LV400B (4 Mbit, bottom boot sect)";
  233. break;
  234. case FLASH_AM400T:
  235. type = "AM29LV400T (4 Mbit, top boot sector)";
  236. break;
  237. case FLASH_AM800B:
  238. type = "AM29LV800B (8 Mbit, bottom boot sect)";
  239. break;
  240. case FLASH_AM800T:
  241. type = "AM29LV800T (8 Mbit, top boot sector)";
  242. break;
  243. case FLASH_AM160T:
  244. type = "AM29LV160T (16 Mbit, top boot sector)";
  245. break;
  246. case FLASH_AM320B:
  247. type = "AM29LV320B (32 Mbit, bottom boot sect)";
  248. break;
  249. case FLASH_AM320T:
  250. type = "AM29LV320T (32 Mbit, top boot sector)";
  251. break;
  252. case FLASH_STM800AB:
  253. type = "M29W800AB (8 Mbit, bottom boot sect)";
  254. break;
  255. case FLASH_SST800A:
  256. type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
  257. break;
  258. case FLASH_SST160A:
  259. type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
  260. break;
  261. }
  262. }
  263. printf ("\n Brand: %s Type: %s\n"
  264. " Size: %lu KB in %d Sectors\n",
  265. mfct, type, info->size >> 10, info->sector_count);
  266. printf (" Sector Start Addresses:");
  267. for (i = 0; i < info->sector_count; i++) {
  268. unsigned long size;
  269. unsigned int erased;
  270. unsigned long *flash = (unsigned long *) info->start[i];
  271. /*
  272. * Check if whole sector is erased
  273. */
  274. size = (i != (info->sector_count - 1)) ?
  275. (info->start[i + 1] - info->start[i]) >> 2 :
  276. (info->start[0] + info->size - info->start[i]) >> 2;
  277. for (flash = (unsigned long *) info->start[i], erased = 1;
  278. (flash != (unsigned long *) info->start[i] + size)
  279. && erased; flash++)
  280. erased = *flash == ~0x0UL;
  281. printf ("%s %08lX %s %s",
  282. (i % 5) ? "" : "\n ",
  283. info->start[i],
  284. erased ? "E" : " ", info->protect[i] ? "RO" : " ");
  285. }
  286. puts ("\n");
  287. return;
  288. }
  289. int flash_erase (flash_info_t * info, int s_first, int s_last)
  290. {
  291. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
  292. int flag, prot, sect, l_sect;
  293. ulong start, now, last;
  294. unsigned char sh8b;
  295. if ((s_first < 0) || (s_first > s_last)) {
  296. if (info->flash_id == FLASH_UNKNOWN) {
  297. printf ("- missing\n");
  298. } else {
  299. printf ("- no sectors to erase\n");
  300. }
  301. return 1;
  302. }
  303. if ((info->flash_id == FLASH_UNKNOWN) ||
  304. (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
  305. printf ("Can't erase unknown flash type - aborted\n");
  306. return 1;
  307. }
  308. prot = 0;
  309. for (sect = s_first; sect <= s_last; ++sect) {
  310. if (info->protect[sect]) {
  311. prot++;
  312. }
  313. }
  314. if (prot) {
  315. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  316. } else {
  317. printf ("\n");
  318. }
  319. l_sect = -1;
  320. /* Check the ROM CS */
  321. if ((info->start[0] >= ROM_CS1_START)
  322. && (info->start[0] < ROM_CS0_START))
  323. sh8b = 3;
  324. else
  325. sh8b = 0;
  326. /* Disable interrupts which might cause a timeout here */
  327. flag = disable_interrupts ();
  328. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
  329. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
  330. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080;
  331. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
  332. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
  333. /* Start erase on unprotected sectors */
  334. for (sect = s_first; sect <= s_last; sect++) {
  335. if (info->protect[sect] == 0) { /* not protected */
  336. addr = (FLASH_WORD_SIZE *) (info->start[0] +
  337. ((info->start[sect] -
  338. info->start[0]) << sh8b));
  339. if (info->flash_id & FLASH_MAN_SST) {
  340. addr[ADDR0 << sh8b] =
  341. (FLASH_WORD_SIZE) 0x00AA00AA;
  342. addr[ADDR1 << sh8b] =
  343. (FLASH_WORD_SIZE) 0x00550055;
  344. addr[ADDR0 << sh8b] =
  345. (FLASH_WORD_SIZE) 0x00800080;
  346. addr[ADDR0 << sh8b] =
  347. (FLASH_WORD_SIZE) 0x00AA00AA;
  348. addr[ADDR1 << sh8b] =
  349. (FLASH_WORD_SIZE) 0x00550055;
  350. addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
  351. udelay (30000); /* wait 30 ms */
  352. } else
  353. addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  354. l_sect = sect;
  355. }
  356. }
  357. /* re-enable interrupts if necessary */
  358. if (flag)
  359. enable_interrupts ();
  360. /* wait at least 80us - let's wait 1 ms */
  361. udelay (1000);
  362. /*
  363. * We wait for the last triggered sector
  364. */
  365. if (l_sect < 0)
  366. goto DONE;
  367. start = get_timer (0);
  368. last = start;
  369. addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] -
  370. info->
  371. start[0]) << sh8b));
  372. while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
  373. (FLASH_WORD_SIZE) 0x00800080) {
  374. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  375. printf ("Timeout\n");
  376. return 1;
  377. }
  378. /* show that we're waiting */
  379. if ((now - last) > 1000) { /* every second */
  380. serial_putc ('.');
  381. last = now;
  382. }
  383. }
  384. DONE:
  385. /* reset to read mode */
  386. addr = (FLASH_WORD_SIZE *) info->start[0];
  387. addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  388. printf (" done\n");
  389. return 0;
  390. }
  391. /*-----------------------------------------------------------------------
  392. * Copy memory to flash, returns:
  393. * 0 - OK
  394. * 1 - write timeout
  395. * 2 - Flash not erased
  396. */
  397. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  398. {
  399. ulong cp, wp, data;
  400. int i, l, rc;
  401. wp = (addr & ~3); /* get lower word aligned address */
  402. /*
  403. * handle unaligned start bytes
  404. */
  405. if ((l = addr - wp) != 0) {
  406. data = 0;
  407. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  408. data = (data << 8) | (*(uchar *) cp);
  409. }
  410. for (; i < 4 && cnt > 0; ++i) {
  411. data = (data << 8) | *src++;
  412. --cnt;
  413. ++cp;
  414. }
  415. for (; cnt == 0 && i < 4; ++i, ++cp) {
  416. data = (data << 8) | (*(uchar *) cp);
  417. }
  418. if ((rc = write_word (info, wp, data)) != 0) {
  419. return (rc);
  420. }
  421. wp += 4;
  422. }
  423. /*
  424. * handle word aligned part
  425. */
  426. while (cnt >= 4) {
  427. data = 0;
  428. for (i = 0; i < 4; ++i) {
  429. data = (data << 8) | *src++;
  430. }
  431. if ((rc = write_word (info, wp, data)) != 0) {
  432. return (rc);
  433. }
  434. wp += 4;
  435. cnt -= 4;
  436. }
  437. if (cnt == 0) {
  438. return (0);
  439. }
  440. /*
  441. * handle unaligned tail bytes
  442. */
  443. data = 0;
  444. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  445. data = (data << 8) | *src++;
  446. --cnt;
  447. }
  448. for (; i < 4; ++i, ++cp) {
  449. data = (data << 8) | (*(uchar *) cp);
  450. }
  451. return (write_word (info, wp, data));
  452. }
  453. /*-----------------------------------------------------------------------
  454. * Write a word to Flash, returns:
  455. * 0 - OK
  456. * 1 - write timeout
  457. * 2 - Flash not erased
  458. */
  459. static int write_word (flash_info_t * info, ulong dest, ulong data)
  460. {
  461. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0];
  462. volatile FLASH_WORD_SIZE *dest2;
  463. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  464. ulong start;
  465. int flag;
  466. int i;
  467. unsigned char sh8b;
  468. /* Check the ROM CS */
  469. if ((info->start[0] >= ROM_CS1_START)
  470. && (info->start[0] < ROM_CS0_START))
  471. sh8b = 3;
  472. else
  473. sh8b = 0;
  474. dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) +
  475. info->start[0]);
  476. /* Check if Flash is (sufficiently) erased */
  477. if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  478. return (2);
  479. }
  480. /* Disable interrupts which might cause a timeout here */
  481. flag = disable_interrupts ();
  482. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  483. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA;
  484. addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055;
  485. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0;
  486. dest2[i << sh8b] = data2[i];
  487. /* re-enable interrupts if necessary */
  488. if (flag)
  489. enable_interrupts ();
  490. /* data polling for D7 */
  491. start = get_timer (0);
  492. while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) !=
  493. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  494. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  495. return (1);
  496. }
  497. }
  498. }
  499. return (0);
  500. }