flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /********************************************************************
  2. *
  3. * Unless otherwise specified, Copyright (C) 2004-2005 Barco Control Rooms
  4. *
  5. * $Source: /home/services/cvs/firmware/ppc/u-boot-1.1.2/board/barco/flash.c,v $
  6. * $Revision: 1.3 $
  7. * $Author: mleeman $
  8. * $Date: 2005/02/21 12:48:58 $
  9. *
  10. * Last ChangeLog Entry
  11. * $Log: flash.c,v $
  12. * Revision 1.3 2005/02/21 12:48:58 mleeman
  13. * update of copyright years (feedback wd)
  14. *
  15. * Revision 1.2 2005/02/21 11:04:04 mleeman
  16. * remove dead code and Coding style (feedback wd)
  17. *
  18. * Revision 1.1 2005/02/14 09:23:46 mleeman
  19. * - moved 'barcohydra' directory to a more generic barco; since we will be
  20. * supporting and adding multiple boards
  21. *
  22. * Revision 1.2 2005/02/09 12:56:23 mleeman
  23. * add generic header to track changes in sources
  24. *
  25. *
  26. *******************************************************************/
  27. /*
  28. * (C) Copyright 2000
  29. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  30. *
  31. * See file CREDITS for list of people who contributed to this
  32. * project.
  33. *
  34. * This program is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU General Public License as
  36. * published by the Free Software Foundation; either version 2 of
  37. * the License, or (at your option) any later version.
  38. *
  39. * This program is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. * GNU General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU General Public License
  45. * along with this program; if not, write to the Free Software
  46. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  47. * MA 02111-1307 USA
  48. */
  49. #include <common.h>
  50. #include <mpc824x.h>
  51. #include <asm/processor.h>
  52. #include <flash.h>
  53. #define ROM_CS0_START 0xFF800000
  54. #define ROM_CS1_START 0xFF000000
  55. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  56. #if defined(CONFIG_ENV_IS_IN_FLASH)
  57. # ifndef CONFIG_ENV_ADDR
  58. # define CONFIG_ENV_ADDR (CFG_FLASH_BASE + CONFIG_ENV_OFFSET)
  59. # endif
  60. # ifndef CONFIG_ENV_SIZE
  61. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  62. # endif
  63. # ifndef CONFIG_ENV_SECT_SIZE
  64. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  65. # endif
  66. #endif
  67. /*-----------------------------------------------------------------------
  68. * Functions
  69. */
  70. static int write_word (flash_info_t *info, ulong dest, ulong data);
  71. /*flash command address offsets*/
  72. #define ADDR0 (0xAAA)
  73. #define ADDR1 (0x555)
  74. #define ADDR3 (0x001)
  75. #define FLASH_WORD_SIZE unsigned char
  76. /*-----------------------------------------------------------------------
  77. */
  78. static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
  79. typedef struct{
  80. FLASH_WORD_SIZE extval;
  81. unsigned short intval;
  82. } map_entry;
  83. static unsigned long flash_id(unsigned char mfct, unsigned char chip)
  84. {
  85. static const map_entry mfct_map[] = {
  86. {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
  87. {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
  88. {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
  89. {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
  90. {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
  91. {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
  92. };
  93. static const map_entry chip_map[] = {
  94. {AMD_ID_F040B, FLASH_AM040},
  95. {AMD_ID_F033C, FLASH_AM033},
  96. {AMD_ID_F065D, FLASH_AM065},
  97. {ATM_ID_LV040, FLASH_AT040},
  98. {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
  99. };
  100. const map_entry *p;
  101. unsigned long result = FLASH_UNKNOWN;
  102. /* find chip id */
  103. for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++){
  104. if(p->extval == chip){
  105. result = FLASH_VENDMASK | p->intval;
  106. break;
  107. }
  108. }
  109. /* find vendor id */
  110. for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++){
  111. if(p->extval == mfct){
  112. result &= ~FLASH_VENDMASK;
  113. result |= (unsigned long) p->intval << 16;
  114. break;
  115. }
  116. }
  117. return result;
  118. }
  119. unsigned long flash_init(void)
  120. {
  121. unsigned long i;
  122. unsigned char j;
  123. static const ulong flash_banks[] = CFG_FLASH_BANKS;
  124. /* Init: no FLASHes known */
  125. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++){
  126. flash_info_t * const pflinfo = &flash_info[i];
  127. pflinfo->flash_id = FLASH_UNKNOWN;
  128. pflinfo->size = 0;
  129. pflinfo->sector_count = 0;
  130. }
  131. /* Enable writes to Hydra/Argus flash */
  132. {
  133. register unsigned int temp;
  134. CONFIG_READ_WORD(PICR1,temp);
  135. temp |= PICR1_FLASH_WR_EN;
  136. CONFIG_WRITE_WORD(PICR1,temp);
  137. }
  138. for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++){
  139. flash_info_t * const pflinfo = &flash_info[i];
  140. const unsigned long base_address = flash_banks[i];
  141. volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
  142. /* write autoselect sequence */
  143. flash[0x5555] = 0xaa;
  144. flash[0x2aaa] = 0x55;
  145. flash[0x5555] = 0x90;
  146. __asm__ __volatile__("sync");
  147. pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
  148. switch(pflinfo->flash_id & FLASH_TYPEMASK){
  149. case FLASH_AM033:
  150. pflinfo->size = 0x00200000;
  151. pflinfo->sector_count = 64;
  152. for(j = 0; j < 64; j++){
  153. pflinfo->start[j] = base_address + 0x00010000 * j;
  154. pflinfo->protect[j] = flash[(j << 16) | 0x2];
  155. }
  156. break;
  157. case FLASH_AM065:
  158. pflinfo->size = 0x00800000;
  159. pflinfo->sector_count =128;
  160. for(j = 0; j < 128; j++){
  161. pflinfo->start[j] = base_address + 0x00010000 * j;
  162. pflinfo->protect[j] = flash[(j << 16) | 0x2];
  163. }
  164. break;
  165. case FLASH_AT040:
  166. pflinfo->size = 0x00080000;
  167. pflinfo->sector_count = 2;
  168. pflinfo->start[0] = base_address ;
  169. pflinfo->start[1] = base_address + 0x00004000;
  170. pflinfo->protect[0] = ((flash[0x02] & 0X01)==0) ? 0X02 : 0X01;
  171. pflinfo->protect[1] = 0X02;
  172. break;
  173. case FLASH_AM040:
  174. pflinfo->size = 0x00080000;
  175. pflinfo->sector_count = 8;
  176. for(j = 0; j < 8; j++){
  177. pflinfo->start[j] = base_address + 0x00010000 * j;
  178. pflinfo->protect[j] = flash[(j << 16) | 0x2];
  179. }
  180. break;
  181. case FLASH_STM800AB:
  182. pflinfo->size = 0x00100000;
  183. pflinfo->sector_count = 19;
  184. pflinfo->start[0] = base_address;
  185. pflinfo->start[1] = base_address + 0x4000;
  186. pflinfo->start[2] = base_address + 0x6000;
  187. pflinfo->start[3] = base_address + 0x8000;
  188. for(j = 1; j < 16; j++){
  189. pflinfo->start[j+3] = base_address + 0x00010000 * j;
  190. }
  191. break;
  192. }
  193. /* Protect monitor and environment sectors */
  194. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  195. flash_protect(FLAG_PROTECT_SET,
  196. CFG_MONITOR_BASE,
  197. CFG_MONITOR_BASE + monitor_flash_len - 1,
  198. &flash_info[0]);
  199. #endif
  200. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  201. flash_protect(FLAG_PROTECT_SET,
  202. CONFIG_ENV_ADDR,
  203. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  204. &flash_info[0]);
  205. #endif
  206. /* reset device to read mode */
  207. flash[0x0000] = 0xf0;
  208. __asm__ __volatile__("sync");
  209. }
  210. return flash_info[0].size + flash_info[1].size;
  211. }
  212. /*-----------------------------------------------------------------------
  213. */
  214. void flash_print_info(flash_info_t *info)
  215. {
  216. static const char unk[] = "Unknown";
  217. const char *mfct = unk, *type = unk;
  218. unsigned int i;
  219. if(info->flash_id != FLASH_UNKNOWN){
  220. switch(info->flash_id & FLASH_VENDMASK){
  221. case FLASH_MAN_ATM:
  222. mfct = "Atmel";
  223. break;
  224. case FLASH_MAN_AMD:
  225. mfct = "AMD";
  226. break;
  227. case FLASH_MAN_FUJ:
  228. mfct = "FUJITSU";
  229. break;
  230. case FLASH_MAN_STM:
  231. mfct = "STM";
  232. break;
  233. case FLASH_MAN_SST:
  234. mfct = "SST";
  235. break;
  236. case FLASH_MAN_BM:
  237. mfct = "Bright Microelectonics";
  238. break;
  239. case FLASH_MAN_INTEL:
  240. mfct = "Intel";
  241. break;
  242. }
  243. switch(info->flash_id & FLASH_TYPEMASK){
  244. case FLASH_AT040:
  245. type = "AT49LV040 (512K * 8, uniform sector size)";
  246. break;
  247. case FLASH_AM033:
  248. type = "AM29F033C (4 Mbit * 8, uniform sector size)";
  249. break;
  250. case FLASH_AM040:
  251. type = "AM29F040B (512K * 8, uniform sector size)";
  252. break;
  253. case FLASH_AM065:
  254. type = "AM29F0465D ( 8 MBit * 8, uniform sector size) or part of AM29F652D( 16 MB)";
  255. break;
  256. case FLASH_AM400B:
  257. type = "AM29LV400B (4 Mbit, bottom boot sect)";
  258. break;
  259. case FLASH_AM400T:
  260. type = "AM29LV400T (4 Mbit, top boot sector)";
  261. break;
  262. case FLASH_AM800B:
  263. type = "AM29LV800B (8 Mbit, bottom boot sect)";
  264. break;
  265. case FLASH_AM800T:
  266. type = "AM29LV800T (8 Mbit, top boot sector)";
  267. break;
  268. case FLASH_AM160T:
  269. type = "AM29LV160T (16 Mbit, top boot sector)";
  270. break;
  271. case FLASH_AM320B:
  272. type = "AM29LV320B (32 Mbit, bottom boot sect)";
  273. break;
  274. case FLASH_AM320T:
  275. type = "AM29LV320T (32 Mbit, top boot sector)";
  276. break;
  277. case FLASH_STM800AB:
  278. type = "M29W800AB (8 Mbit, bottom boot sect)";
  279. break;
  280. case FLASH_SST800A:
  281. type = "SST39LF/VF800 (8 Mbit, uniform sector size)";
  282. break;
  283. case FLASH_SST160A:
  284. type = "SST39LF/VF160 (16 Mbit, uniform sector size)";
  285. break;
  286. }
  287. }
  288. printf(
  289. "\n Brand: %s Type: %s\n"
  290. " Size: %lu KB in %d Sectors\n",
  291. mfct,
  292. type,
  293. info->size >> 10,
  294. info->sector_count
  295. );
  296. printf (" Sector Start Addresses:");
  297. for (i = 0; i < info->sector_count; i++){
  298. unsigned long size;
  299. unsigned int erased;
  300. unsigned long * flash = (unsigned long *) info->start[i];
  301. /*
  302. * Check if whole sector is erased
  303. */
  304. size =
  305. (i != (info->sector_count - 1)) ?
  306. (info->start[i + 1] - info->start[i]) >> 2 :
  307. (info->start[0] + info->size - info->start[i]) >> 2;
  308. for(
  309. flash = (unsigned long *) info->start[i], erased = 1;
  310. (flash != (unsigned long *) info->start[i] + size) && erased;
  311. flash++
  312. ){
  313. erased = *flash == ~0x0UL;
  314. }
  315. printf(
  316. "%s %08lX %s %s",
  317. (i % 5) ? "" : "\n ",
  318. info->start[i],
  319. erased ? "E" : " ",
  320. info->protect[i] ? "RO" : " "
  321. );
  322. }
  323. puts("\n");
  324. return;
  325. }
  326. int flash_erase(flash_info_t *info, int s_first, int s_last)
  327. {
  328. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  329. int flag, prot, sect, l_sect;
  330. ulong start, now, last;
  331. unsigned char sh8b;
  332. if ((s_first < 0) || (s_first > s_last)) {
  333. if (info->flash_id == FLASH_UNKNOWN) {
  334. printf ("- missing\n");
  335. } else {
  336. printf ("- no sectors to erase\n");
  337. }
  338. return 1;
  339. }
  340. if ((info->flash_id == FLASH_UNKNOWN) ||
  341. (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
  342. printf ("Can't erase unknown flash type - aborted\n");
  343. return 1;
  344. }
  345. prot = 0;
  346. for (sect=s_first; sect<=s_last; ++sect) {
  347. if (info->protect[sect]) {
  348. prot++;
  349. }
  350. }
  351. if (prot) {
  352. printf ("- Warning: %d protected sectors will not be erased!\n",
  353. prot);
  354. } else {
  355. printf ("\n");
  356. }
  357. l_sect = -1;
  358. /* Check the ROM CS */
  359. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START)){
  360. sh8b = 3;
  361. }
  362. else{
  363. sh8b = 0;
  364. }
  365. /* Disable interrupts which might cause a timeout here */
  366. flag = disable_interrupts();
  367. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  368. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  369. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  370. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  371. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  372. /* Start erase on unprotected sectors */
  373. for (sect = s_first; sect<=s_last; sect++) {
  374. if (info->protect[sect] == 0) { /* not protected */
  375. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  376. (info->start[sect] - info->start[0]) << sh8b));
  377. if (info->flash_id & FLASH_MAN_SST){
  378. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  379. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  380. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
  381. addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  382. addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  383. addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  384. udelay(30000); /* wait 30 ms */
  385. }
  386. else
  387. addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  388. l_sect = sect;
  389. }
  390. }
  391. /* re-enable interrupts if necessary */
  392. if (flag){
  393. enable_interrupts();
  394. }
  395. /* wait at least 80us - let's wait 1 ms */
  396. udelay (1000);
  397. /*
  398. * We wait for the last triggered sector
  399. */
  400. if (l_sect < 0){
  401. goto DONE;
  402. }
  403. start = get_timer (0);
  404. last = start;
  405. addr = (FLASH_WORD_SIZE *)(info->start[0] + (
  406. (info->start[l_sect] - info->start[0]) << sh8b));
  407. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  408. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  409. printf ("Timeout\n");
  410. return 1;
  411. }
  412. /* show that we're waiting */
  413. if ((now - last) > 1000) { /* every second */
  414. serial_putc ('.');
  415. last = now;
  416. }
  417. }
  418. DONE:
  419. /* reset to read mode */
  420. addr = (FLASH_WORD_SIZE *)info->start[0];
  421. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  422. printf (" done\n");
  423. return 0;
  424. }
  425. /*-----------------------------------------------------------------------
  426. * Copy memory to flash, returns:
  427. * 0 - OK
  428. * 1 - write timeout
  429. * 2 - Flash not erased
  430. */
  431. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  432. {
  433. ulong cp, wp, data;
  434. int i, l, rc;
  435. wp = (addr & ~3); /* get lower word aligned address */
  436. /*
  437. * handle unaligned start bytes
  438. */
  439. if ((l = addr - wp) != 0) {
  440. data = 0;
  441. for (i=0, cp=wp; i<l; ++i, ++cp) {
  442. data = (data << 8) | (*(uchar *)cp);
  443. }
  444. for (; i<4 && cnt>0; ++i) {
  445. data = (data << 8) | *src++;
  446. --cnt;
  447. ++cp;
  448. }
  449. for (; cnt==0 && i<4; ++i, ++cp) {
  450. data = (data << 8) | (*(uchar *)cp);
  451. }
  452. if ((rc = write_word(info, wp, data)) != 0) {
  453. return (rc);
  454. }
  455. wp += 4;
  456. }
  457. /*
  458. * handle word aligned part
  459. */
  460. while (cnt >= 4) {
  461. data = 0;
  462. for (i=0; i<4; ++i) {
  463. data = (data << 8) | *src++;
  464. }
  465. if ((rc = write_word(info, wp, data)) != 0) {
  466. return (rc);
  467. }
  468. wp += 4;
  469. cnt -= 4;
  470. }
  471. if (cnt == 0) {
  472. return (0);
  473. }
  474. /*
  475. * handle unaligned tail bytes
  476. */
  477. data = 0;
  478. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  479. data = (data << 8) | *src++;
  480. --cnt;
  481. }
  482. for (; i<4; ++i, ++cp) {
  483. data = (data << 8) | (*(uchar *)cp);
  484. }
  485. return (write_word(info, wp, data));
  486. }
  487. /*-----------------------------------------------------------------------
  488. * Write a word to Flash, returns:
  489. * 0 - OK
  490. * 1 - write timeout
  491. * 2 - Flash not erased
  492. */
  493. static int write_word (flash_info_t *info, ulong dest, ulong data)
  494. {
  495. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
  496. volatile FLASH_WORD_SIZE *dest2;
  497. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  498. ulong start;
  499. int flag;
  500. int i;
  501. unsigned char sh8b;
  502. /* Check the ROM CS */
  503. if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START)){
  504. sh8b = 3;
  505. }
  506. else{
  507. sh8b = 0;
  508. }
  509. dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
  510. info->start[0]);
  511. /* Check if Flash is (sufficiently) erased */
  512. if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  513. return (2);
  514. }
  515. /* Disable interrupts which might cause a timeout here */
  516. flag = disable_interrupts();
  517. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++){
  518. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
  519. addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
  520. addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
  521. dest2[i << sh8b] = data2[i];
  522. /* re-enable interrupts if necessary */
  523. if (flag){
  524. enable_interrupts();
  525. }
  526. /* data polling for D7 */
  527. start = get_timer (0);
  528. while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
  529. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  530. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  531. return (1);
  532. }
  533. }
  534. }
  535. return (0);
  536. }
  537. /*----------------------------------------------------------------------- */