flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. **=====================================================================
  3. **
  4. ** Copyright (C) 2000, 2001, 2002, 2003
  5. ** The LEOX team <team@leox.org>, http://www.leox.org
  6. **
  7. ** LEOX.org is about the development of free hardware and software resources
  8. ** for system on chip.
  9. **
  10. ** Description: U-Boot port on the LEOX's ELPT860 CPU board
  11. ** ~~~~~~~~~~~
  12. **
  13. **=====================================================================
  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. **=====================================================================
  31. */
  32. /*
  33. ** Note 1: In this file, you have to provide the following variable:
  34. ** ------
  35. ** flash_info_t flash_info[CFG_MAX_FLASH_BANKS]
  36. ** 'flash_info_t' structure is defined into 'include/flash.h'
  37. ** and defined as extern into 'common/cmd_flash.c'
  38. **
  39. ** Note 2: In this file, you have to provide the following functions:
  40. ** ------
  41. ** unsigned long flash_init(void)
  42. ** called from 'board_init_r()' into 'common/board.c'
  43. **
  44. ** void flash_print_info(flash_info_t *info)
  45. ** called from 'do_flinfo()' into 'common/cmd_flash.c'
  46. **
  47. ** int flash_erase(flash_info_t *info,
  48. ** int s_first,
  49. ** int s_last)
  50. ** called from 'do_flerase()' & 'flash_sect_erase()' into 'common/cmd_flash.c'
  51. **
  52. ** int write_buff (flash_info_t *info,
  53. ** uchar *src,
  54. ** ulong addr,
  55. ** ulong cnt)
  56. ** called from 'flash_write()' into 'common/cmd_flash.c'
  57. */
  58. #include <common.h>
  59. #include <mpc8xx.h>
  60. #ifndef CFG_ENV_ADDR
  61. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  62. #endif
  63. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  64. /*-----------------------------------------------------------------------
  65. * Internal Functions
  66. */
  67. static void flash_get_offsets (ulong base, flash_info_t *info);
  68. static ulong flash_get_size (volatile unsigned char *addr, flash_info_t *info);
  69. static int write_word (flash_info_t *info, ulong dest, ulong data);
  70. static int write_byte (flash_info_t *info, ulong dest, uchar data);
  71. /*-----------------------------------------------------------------------
  72. */
  73. unsigned long
  74. flash_init (void)
  75. {
  76. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  77. volatile memctl8xx_t *memctl = &immap->im_memctl;
  78. unsigned long size_b0;
  79. int i;
  80. /* Init: no FLASHes known */
  81. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
  82. {
  83. flash_info[i].flash_id = FLASH_UNKNOWN;
  84. }
  85. /* Static FLASH Bank configuration here - FIXME XXX */
  86. size_b0 = flash_get_size ((volatile unsigned char *)FLASH_BASE0_PRELIM,
  87. &flash_info[0]);
  88. if ( flash_info[0].flash_id == FLASH_UNKNOWN )
  89. {
  90. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  91. size_b0, size_b0<<20);
  92. }
  93. /* Remap FLASH according to real size */
  94. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
  95. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_PS_8 | BR_V;
  96. /* Re-do sizing to get full correct info */
  97. size_b0 = flash_get_size ((volatile unsigned char *)CFG_FLASH_BASE,
  98. &flash_info[0]);
  99. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  100. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  101. /* monitor protection ON by default */
  102. flash_protect (FLAG_PROTECT_SET,
  103. CFG_MONITOR_BASE,
  104. CFG_MONITOR_BASE + monitor_flash_len-1,
  105. &flash_info[0]);
  106. #endif
  107. #ifdef CFG_ENV_IS_IN_FLASH
  108. /* ENV protection ON by default */
  109. flash_protect(FLAG_PROTECT_SET,
  110. CFG_ENV_ADDR,
  111. CFG_ENV_ADDR + CFG_ENV_SIZE-1,
  112. &flash_info[0]);
  113. #endif
  114. flash_info[0].size = size_b0;
  115. return (size_b0);
  116. }
  117. /*-----------------------------------------------------------------------
  118. */
  119. static void
  120. flash_get_offsets (ulong base,
  121. flash_info_t *info)
  122. {
  123. int i;
  124. #define SECTOR_64KB 0x00010000
  125. /* set up sector start adress table */
  126. for (i = 0; i < info->sector_count; i++)
  127. {
  128. info->start[i] = base + (i * SECTOR_64KB);
  129. }
  130. }
  131. /*-----------------------------------------------------------------------
  132. */
  133. void
  134. flash_print_info (flash_info_t *info)
  135. {
  136. int i;
  137. if ( info->flash_id == FLASH_UNKNOWN )
  138. {
  139. printf ("missing or unknown FLASH type\n");
  140. return;
  141. }
  142. switch ( info->flash_id & FLASH_VENDMASK )
  143. {
  144. case FLASH_MAN_AMD: printf ("AMD "); break;
  145. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  146. case FLASH_MAN_STM: printf ("STM (Thomson) "); break;
  147. default: printf ("Unknown Vendor "); break;
  148. }
  149. switch ( info->flash_id & FLASH_TYPEMASK )
  150. {
  151. case FLASH_AM040: printf ("AM29F040 (4 Mbits)\n");
  152. break;
  153. default: printf ("Unknown Chip Type\n");
  154. break;
  155. }
  156. printf (" Size: %ld KB in %d Sectors\n",
  157. info->size >> 10, info->sector_count);
  158. printf (" Sector Start Addresses:");
  159. for (i=0; i<info->sector_count; ++i)
  160. {
  161. if ((i % 5) == 0)
  162. printf ("\n ");
  163. printf (" %08lX%s",
  164. info->start[i],
  165. info->protect[i] ? " (RO)" : " "
  166. );
  167. }
  168. printf ("\n");
  169. return;
  170. }
  171. /*-----------------------------------------------------------------------
  172. */
  173. /*-----------------------------------------------------------------------
  174. */
  175. /*
  176. * The following code cannot be run from FLASH!
  177. */
  178. static ulong
  179. flash_get_size (volatile unsigned char *addr,
  180. flash_info_t *info)
  181. {
  182. short i;
  183. uchar value;
  184. ulong base = (ulong)addr;
  185. /* Write auto select command: read Manufacturer ID */
  186. addr[0x0555] = 0xAA;
  187. addr[0x02AA] = 0x55;
  188. addr[0x0555] = 0x90;
  189. value = addr[0];
  190. switch ( value )
  191. {
  192. /* case AMD_MANUFACT: */
  193. case 0x01:
  194. info->flash_id = FLASH_MAN_AMD;
  195. break;
  196. /* case FUJ_MANUFACT: */
  197. case 0x04:
  198. info->flash_id = FLASH_MAN_FUJ;
  199. break;
  200. /* case STM_MANUFACT: */
  201. case 0x20:
  202. info->flash_id = FLASH_MAN_STM;
  203. break;
  204. default:
  205. info->flash_id = FLASH_UNKNOWN;
  206. info->sector_count = 0;
  207. info->size = 0;
  208. return (0); /* no or unknown flash */
  209. }
  210. value = addr[1]; /* device ID */
  211. switch ( value )
  212. {
  213. case STM_ID_F040B:
  214. case AMD_ID_F040B:
  215. info->flash_id += FLASH_AM040; /* 4 Mbits = 512k * 8 */
  216. info->sector_count = 8;
  217. info->size = 0x00080000;
  218. break;
  219. default:
  220. info->flash_id = FLASH_UNKNOWN;
  221. return (0); /* => no or unknown flash */
  222. }
  223. /* set up sector start adress table */
  224. for (i = 0; i < info->sector_count; i++)
  225. {
  226. info->start[i] = base + (i * 0x00010000);
  227. }
  228. /* check for protected sectors */
  229. for (i = 0; i < info->sector_count; i++)
  230. {
  231. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  232. /* D0 = 1 if protected */
  233. addr = (volatile unsigned char *)(info->start[i]);
  234. info->protect[i] = addr[2] & 1;
  235. }
  236. /*
  237. * Prevent writes to uninitialized FLASH.
  238. */
  239. if ( info->flash_id != FLASH_UNKNOWN )
  240. {
  241. addr = (volatile unsigned char *)info->start[0];
  242. *addr = 0xF0; /* reset bank */
  243. }
  244. return (info->size);
  245. }
  246. /*-----------------------------------------------------------------------
  247. */
  248. int
  249. flash_erase (flash_info_t *info,
  250. int s_first,
  251. int s_last)
  252. {
  253. volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
  254. int flag, prot, sect, l_sect;
  255. ulong start, now, last;
  256. if ( (s_first < 0) || (s_first > s_last) )
  257. {
  258. if ( info->flash_id == FLASH_UNKNOWN )
  259. {
  260. printf ("- missing\n");
  261. }
  262. else
  263. {
  264. printf ("- no sectors to erase\n");
  265. }
  266. return ( 1 );
  267. }
  268. if ( (info->flash_id == FLASH_UNKNOWN) ||
  269. (info->flash_id > FLASH_AMD_COMP) )
  270. {
  271. printf ("Can't erase unknown flash type %08lx - aborted\n",
  272. info->flash_id);
  273. return ( 1 );
  274. }
  275. prot = 0;
  276. for (sect=s_first; sect<=s_last; ++sect)
  277. {
  278. if ( info->protect[sect] )
  279. {
  280. prot++;
  281. }
  282. }
  283. if ( prot )
  284. {
  285. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  286. }
  287. else
  288. {
  289. printf ("\n");
  290. }
  291. l_sect = -1;
  292. /* Disable interrupts which might cause a timeout here */
  293. flag = disable_interrupts();
  294. addr[0x0555] = 0xAA;
  295. addr[0x02AA] = 0x55;
  296. addr[0x0555] = 0x80;
  297. addr[0x0555] = 0xAA;
  298. addr[0x02AA] = 0x55;
  299. /* Start erase on unprotected sectors */
  300. for (sect = s_first; sect<=s_last; sect++)
  301. {
  302. if (info->protect[sect] == 0) /* not protected */
  303. {
  304. addr = (volatile unsigned char *)(info->start[sect]);
  305. addr[0] = 0x30;
  306. l_sect = sect;
  307. }
  308. }
  309. /* re-enable interrupts if necessary */
  310. if ( flag )
  311. enable_interrupts();
  312. /* wait at least 80us - let's wait 1 ms */
  313. udelay (1000);
  314. /*
  315. * We wait for the last triggered sector
  316. */
  317. if ( l_sect < 0 )
  318. goto DONE;
  319. start = get_timer (0);
  320. last = start;
  321. addr = (volatile unsigned char *)(info->start[l_sect]);
  322. while ( (addr[0] & 0x80) != 0x80 )
  323. {
  324. if ( (now = get_timer(start)) > CFG_FLASH_ERASE_TOUT )
  325. {
  326. printf ("Timeout\n");
  327. return ( 1 );
  328. }
  329. /* show that we're waiting */
  330. if ( (now - last) > 1000 ) /* every second */
  331. {
  332. putc ('.');
  333. last = now;
  334. }
  335. }
  336. DONE:
  337. /* reset to read mode */
  338. addr = (volatile unsigned char *)info->start[0];
  339. addr[0] = 0xF0; /* reset bank */
  340. printf (" done\n");
  341. return ( 0 );
  342. }
  343. /*-----------------------------------------------------------------------
  344. * Copy memory to flash, returns:
  345. * 0 - OK
  346. * 1 - write timeout
  347. * 2 - Flash not erased
  348. */
  349. int
  350. write_buff (flash_info_t *info,
  351. uchar *src,
  352. ulong addr,
  353. ulong cnt)
  354. {
  355. ulong cp, wp, data;
  356. uchar bdata;
  357. int i, l, rc;
  358. if ( (info->flash_id & FLASH_TYPEMASK) == FLASH_AM040 )
  359. {
  360. /* Width of the data bus: 8 bits */
  361. wp = addr;
  362. while ( cnt )
  363. {
  364. bdata = *src++;
  365. if ( (rc = write_byte(info, wp, bdata)) != 0 )
  366. {
  367. return (rc);
  368. }
  369. ++wp;
  370. --cnt;
  371. }
  372. return ( 0 );
  373. }
  374. else
  375. {
  376. /* Width of the data bus: 32 bits */
  377. wp = (addr & ~3); /* get lower word aligned address */
  378. /*
  379. * handle unaligned start bytes
  380. */
  381. if ( (l = addr - wp) != 0 )
  382. {
  383. data = 0;
  384. for (i=0, cp=wp; i<l; ++i, ++cp)
  385. {
  386. data = (data << 8) | (*(uchar *)cp);
  387. }
  388. for (; i<4 && cnt>0; ++i)
  389. {
  390. data = (data << 8) | *src++;
  391. --cnt;
  392. ++cp;
  393. }
  394. for (; cnt==0 && i<4; ++i, ++cp)
  395. {
  396. data = (data << 8) | (*(uchar *)cp);
  397. }
  398. if ( (rc = write_word(info, wp, data)) != 0 )
  399. {
  400. return (rc);
  401. }
  402. wp += 4;
  403. }
  404. /*
  405. * handle word aligned part
  406. */
  407. while ( cnt >= 4 )
  408. {
  409. data = 0;
  410. for (i=0; i<4; ++i)
  411. {
  412. data = (data << 8) | *src++;
  413. }
  414. if ( (rc = write_word(info, wp, data)) != 0 )
  415. {
  416. return (rc);
  417. }
  418. wp += 4;
  419. cnt -= 4;
  420. }
  421. if ( cnt == 0 )
  422. {
  423. return (0);
  424. }
  425. /*
  426. * handle unaligned tail bytes
  427. */
  428. data = 0;
  429. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp)
  430. {
  431. data = (data << 8) | *src++;
  432. --cnt;
  433. }
  434. for (; i<4; ++i, ++cp)
  435. {
  436. data = (data << 8) | (*(uchar *)cp);
  437. }
  438. return (write_word(info, wp, data));
  439. }
  440. }
  441. /*-----------------------------------------------------------------------
  442. * Write a word to Flash, returns:
  443. * 0 - OK
  444. * 1 - write timeout
  445. * 2 - Flash not erased
  446. */
  447. static int
  448. write_word (flash_info_t *info,
  449. ulong dest,
  450. ulong data)
  451. {
  452. vu_long *addr = (vu_long*)(info->start[0]);
  453. ulong start;
  454. int flag;
  455. /* Check if Flash is (sufficiently) erased */
  456. if ( (*((vu_long *)dest) & data) != data )
  457. {
  458. return (2);
  459. }
  460. /* Disable interrupts which might cause a timeout here */
  461. flag = disable_interrupts();
  462. addr[0x0555] = 0x00AA00AA;
  463. addr[0x02AA] = 0x00550055;
  464. addr[0x0555] = 0x00A000A0;
  465. *((vu_long *)dest) = data;
  466. /* re-enable interrupts if necessary */
  467. if ( flag )
  468. enable_interrupts();
  469. /* data polling for D7 */
  470. start = get_timer (0);
  471. while ( (*((vu_long *)dest) & 0x00800080) != (data & 0x00800080) )
  472. {
  473. if ( get_timer(start) > CFG_FLASH_WRITE_TOUT )
  474. {
  475. return (1);
  476. }
  477. }
  478. return (0);
  479. }
  480. /*-----------------------------------------------------------------------
  481. * Write a byte to Flash, returns:
  482. * 0 - OK
  483. * 1 - write timeout
  484. * 2 - Flash not erased
  485. */
  486. static int
  487. write_byte (flash_info_t *info,
  488. ulong dest,
  489. uchar data)
  490. {
  491. volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
  492. ulong start;
  493. int flag;
  494. /* Check if Flash is (sufficiently) erased */
  495. if ( (*((volatile unsigned char *)dest) & data) != data )
  496. {
  497. return (2);
  498. }
  499. /* Disable interrupts which might cause a timeout here */
  500. flag = disable_interrupts();
  501. addr[0x0555] = 0xAA;
  502. addr[0x02AA] = 0x55;
  503. addr[0x0555] = 0xA0;
  504. *((volatile unsigned char *)dest) = data;
  505. /* re-enable interrupts if necessary */
  506. if ( flag )
  507. enable_interrupts();
  508. /* data polling for D7 */
  509. start = get_timer (0);
  510. while ( (*((volatile unsigned char *)dest) & 0x80) != (data & 0x80) )
  511. {
  512. if ( get_timer(start) > CFG_FLASH_WRITE_TOUT )
  513. {
  514. return (1);
  515. }
  516. }
  517. return (0);
  518. }
  519. /*-----------------------------------------------------------------------
  520. */