flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc8xx.h>
  25. #include <flash.h>
  26. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  27. /*-----------------------------------------------------------------------
  28. * Functions
  29. */
  30. ulong flash_recognize (vu_long *base);
  31. int write_word (flash_info_t *info, ulong dest, ulong data);
  32. void flash_get_geometry (vu_long *base, flash_info_t *info);
  33. void flash_unprotect(flash_info_t *info);
  34. int _flash_real_protect(flash_info_t *info, long idx, int on);
  35. unsigned long flash_init (void)
  36. {
  37. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  38. volatile memctl8xx_t *memctl = &immap->im_memctl;
  39. int i;
  40. int rec;
  41. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  42. flash_info[i].flash_id = FLASH_UNKNOWN;
  43. }
  44. *((vu_short*)CFG_FLASH_BASE) = 0xffff;
  45. flash_get_geometry ((vu_long*)CFG_FLASH_BASE, &flash_info[0]);
  46. /* Remap FLASH according to real size */
  47. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-flash_info[0].size & 0xFFFF8000);
  48. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) |
  49. (memctl->memc_br0 & ~(BR_BA_MSK));
  50. rec = flash_recognize((vu_long*)CFG_FLASH_BASE);
  51. if (rec == FLASH_UNKNOWN) {
  52. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  53. flash_info[0].size, flash_info[0].size<<20);
  54. }
  55. #if CFG_FLASH_PROTECTION
  56. /*Unprotect all the flash memory*/
  57. flash_unprotect(&flash_info[0]);
  58. #endif
  59. *((vu_short*)CFG_FLASH_BASE) = 0xffff;
  60. return (flash_info[0].size);
  61. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  62. /* monitor protection ON by default */
  63. flash_protect(FLAG_PROTECT_SET,
  64. CFG_MONITOR_BASE,
  65. CFG_MONITOR_BASE+monitor_flash_len-1,
  66. &flash_info[0]);
  67. #endif
  68. #ifdef CONFIG_ENV_IS_IN_FLASH
  69. /* ENV protection ON by default */
  70. flash_protect(FLAG_PROTECT_SET,
  71. CFG_ENV_OFFSET,
  72. CFG_ENV_OFFSET+CFG_ENV_SIZE-1,
  73. &flash_info[0]);
  74. #endif
  75. return (flash_info[0].size);
  76. }
  77. int flash_get_protect_status(flash_info_t * info, long idx)
  78. {
  79. vu_short * base;
  80. ushort res;
  81. #ifdef DEBUG
  82. printf("\n Attempting to set protection info with %d sectors\n", info->sector_count);
  83. #endif
  84. base = (vu_short*)info->start[idx];
  85. *(base) = 0xffff;
  86. *(base + 0x55) = 0x0098;
  87. res = base[0x2];
  88. *(base) = 0xffff;
  89. if(res != 0)
  90. res = 1;
  91. else
  92. res = 0;
  93. return res;
  94. }
  95. void flash_get_geometry (vu_long *base, flash_info_t *info)
  96. {
  97. int i,j;
  98. ulong ner = 0;
  99. vu_short * sb = (vu_short*)base;
  100. ulong offset = (ulong)base;
  101. /* Read Device geometry */
  102. *sb = 0xffff;
  103. *sb = 0x0090;
  104. info->flash_id = ((ulong)base[0x0]);
  105. #ifdef DEBUG
  106. printf("Id is %x\n", (uint)(ulong)info->flash_id);
  107. #endif
  108. *sb = 0xffff;
  109. *(sb+0x55) = 0x0098;
  110. info->size = 1 << (sb[0x27]); /* Read flash size */
  111. #ifdef DEBUG
  112. printf("Size is %x\n", (uint)(ulong)info->size);
  113. #endif
  114. *sb = 0xffff;
  115. *(sb + 0x55) = 0x0098;
  116. ner = sb[0x2c] ; /*Number of erase regions*/
  117. #ifdef DEBUG
  118. printf("Number of erase regions %x\n", (uint)ner);
  119. #endif
  120. info->sector_count = 0;
  121. for(i = 0; i < ner; i++)
  122. {
  123. uint s;
  124. uint count;
  125. uint t1,t2,t3,t4;
  126. *sb = 0xffff;
  127. *(sb + 0x55) = 0x0098;
  128. t1 = sb[0x2d + i*4];
  129. t2 = sb[0x2e + i*4];
  130. t3 = sb[0x2f + i*4];
  131. t4 = sb[0x30 + i*4];
  132. count = ((t1 & 0x00ff) | (((t2 & 0x00ff) << 8) & 0xff00) )+ 1; /*sector count*/
  133. s = ((t3 & 0x00ff) | (((t4 & 0x00ff) << 8) & 0xff00)) * 256;; /*Sector size*/
  134. #ifdef DEBUG
  135. printf("count and size %x, %x\n", count, s);
  136. printf("sector count for erase region %d is %d\n", i, count);
  137. #endif
  138. for(j = 0; j < count; j++)
  139. {
  140. #ifdef DEBUG
  141. printf("%x, ", (uint)offset);
  142. #endif
  143. info->start[ info->sector_count + j] = offset;
  144. offset += s;
  145. }
  146. info->sector_count += count;
  147. }
  148. if ((offset - (ulong)base) != info->size)
  149. printf("WARNING reported size %x does not match to calculted size %x.\n"
  150. , (uint)info->size, (uint)(offset - (ulong)base) );
  151. /* Next check if there are any sectors protected.*/
  152. for(i = 0; i < info->sector_count; i++)
  153. info->protect[i] = flash_get_protect_status(info, i);
  154. *sb = 0xffff;
  155. }
  156. /*-----------------------------------------------------------------------
  157. */
  158. void flash_print_info (flash_info_t *info)
  159. {
  160. int i;
  161. if (info->flash_id == FLASH_UNKNOWN) {
  162. printf ("missing or unknown FLASH type\n");
  163. return ;
  164. }
  165. switch (info->flash_id & FLASH_VENDMASK) {
  166. case INTEL_MANUFACT & FLASH_VENDMASK:
  167. printf ("Intel ");
  168. break;
  169. default:
  170. printf ("Unknown Vendor ");
  171. break;
  172. }
  173. switch (info->flash_id & FLASH_TYPEMASK) {
  174. case INTEL_ID_28F320C3B & FLASH_TYPEMASK:
  175. printf ("28F320RC3(4 MB)\n");
  176. break;
  177. case INTEL_ID_28F320J3A:
  178. printf("28F320J3A (4 MB)\n");
  179. break;
  180. default:
  181. printf ("Unknown Chip Type\n");
  182. break;
  183. }
  184. printf (" Size: %ld MB in %d Sectors\n",
  185. info->size >> 20, info->sector_count);
  186. printf (" Sector Start Addresses:");
  187. for (i=0; i<info->sector_count; ++i) {
  188. if ((i % 4) == 0)
  189. printf ("\n ");
  190. printf (" %02d %08lX%s",
  191. i, info->start[i],
  192. info->protect[i]!=0 ? " (RO)" : " "
  193. );
  194. }
  195. printf ("\n");
  196. return ;
  197. }
  198. ulong flash_recognize (vu_long *base)
  199. {
  200. ulong id;
  201. ulong res = FLASH_UNKNOWN;
  202. vu_short * sb = (vu_short*)base;
  203. *sb = 0xffff;
  204. *sb = 0x0090;
  205. id = base[0];
  206. switch (id & 0x00FF0000)
  207. {
  208. case (MT_MANUFACT & 0x00FF0000): /* MT or => Intel */
  209. case (INTEL_ALT_MANU & 0x00FF0000):
  210. res = FLASH_MAN_INTEL;
  211. break;
  212. default:
  213. res = FLASH_UNKNOWN;
  214. }
  215. *sb = 0xffff;
  216. return res;
  217. }
  218. /*-----------------------------------------------------------------------*/
  219. #define INTEL_FLASH_STATUS_BLS 0x02
  220. #define INTEL_FLASH_STATUS_PSS 0x04
  221. #define INTEL_FLASH_STATUS_VPPS 0x08
  222. #define INTEL_FLASH_STATUS_PS 0x10
  223. #define INTEL_FLASH_STATUS_ES 0x20
  224. #define INTEL_FLASH_STATUS_ESS 0x40
  225. #define INTEL_FLASH_STATUS_WSMS 0x80
  226. int flash_decode_status_bits(char status)
  227. {
  228. int err = 0;
  229. if(!(status & INTEL_FLASH_STATUS_WSMS)) {
  230. printf("Busy\n");
  231. err = -1;
  232. }
  233. if(status & INTEL_FLASH_STATUS_ESS) {
  234. printf("Erase suspended\n");
  235. err = -1;
  236. }
  237. if(status & INTEL_FLASH_STATUS_ES) {
  238. printf("Error in block erase\n");
  239. err = -1;
  240. }
  241. if(status & INTEL_FLASH_STATUS_PS) {
  242. printf("Error in programming\n");
  243. err = -1;
  244. }
  245. if(status & INTEL_FLASH_STATUS_VPPS) {
  246. printf("Vpp low, operation aborted\n");
  247. err = -1;
  248. }
  249. if(status & INTEL_FLASH_STATUS_PSS) {
  250. printf("Program is suspended\n");
  251. err = -1;
  252. }
  253. if(status & INTEL_FLASH_STATUS_BLS) {
  254. printf("Attempting to program/erase a locked sector\n");
  255. err = -1;
  256. }
  257. if((status & INTEL_FLASH_STATUS_PS) &&
  258. (status & INTEL_FLASH_STATUS_ES) &&
  259. (status & INTEL_FLASH_STATUS_ESS)) {
  260. printf("A command sequence error\n");
  261. return -1;
  262. }
  263. return err;
  264. }
  265. /*-----------------------------------------------------------------------
  266. */
  267. int flash_erase (flash_info_t *info, int s_first, int s_last)
  268. {
  269. vu_short *addr;
  270. int flag, prot, sect;
  271. ulong start, now;
  272. int rcode = 0;
  273. if ((s_first < 0) || (s_first > s_last)) {
  274. if (info->flash_id == FLASH_UNKNOWN) {
  275. printf ("- missing\n");
  276. } else {
  277. printf ("- no sectors to erase\n");
  278. }
  279. return 1;
  280. }
  281. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
  282. printf ("Can't erase unknown flash type %08lx - aborted\n",
  283. info->flash_id);
  284. return 1;
  285. }
  286. prot = 0;
  287. for (sect=s_first; sect<=s_last; ++sect) {
  288. if (info->protect[sect]) {
  289. prot++;
  290. }
  291. }
  292. if (prot) {
  293. printf ("- Warning: %d protected sectors will not be erased!\n",
  294. prot);
  295. } else {
  296. printf ("\n");
  297. }
  298. start = get_timer (0);
  299. /* Start erase on unprotected sectors */
  300. for (sect = s_first; sect<=s_last; sect++) {
  301. char tmp;
  302. if (info->protect[sect] == 0) { /* not protected */
  303. addr = (vu_short *)(info->start[sect]);
  304. /* Disable interrupts which might cause a timeout here */
  305. flag = disable_interrupts();
  306. /* Single Block Erase Command */
  307. *addr = 0x0020;
  308. /* Confirm */
  309. *addr = 0x00D0;
  310. /* Resume Command, as per errata update */
  311. *addr = 0x00D0;
  312. /* re-enable interrupts if necessary */
  313. if (flag)
  314. enable_interrupts();
  315. *addr = 0x70; /*Read status register command*/
  316. tmp = (short)*addr & 0x00FF; /* Read the status */
  317. while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
  318. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  319. *addr = 0x0050; /* Reset the status register */
  320. *addr = 0xffff;
  321. printf ("Timeout\n");
  322. return 1;
  323. }
  324. /* show that we're waiting */
  325. if ((now - start) > 1000) { /* every second */
  326. putc ('.');
  327. }
  328. udelay(100000); /* 100 ms */
  329. *addr = 0x0070; /*Read status register command*/
  330. tmp = (short)*addr & 0x00FF; /* Read status */
  331. start = get_timer(0);
  332. }
  333. if( tmp & INTEL_FLASH_STATUS_ES )
  334. flash_decode_status_bits(tmp);
  335. *addr = 0x0050; /* Reset the status register */
  336. *addr = 0xffff; /* Reset to read mode */
  337. }
  338. }
  339. printf (" done\n");
  340. return rcode;
  341. }
  342. void flash_unprotect (flash_info_t *info)
  343. {
  344. /*We can only unprotect the whole flash at once*/
  345. /*Therefore we must prevent the _flash_real_protect()*/
  346. /*from re-protecting sectors, that ware protected before */
  347. /*we called flash_real_protect();*/
  348. int i;
  349. for(i = 0; i < info->sector_count; i++)
  350. info->protect[i] = 0;
  351. #ifdef CFG_FLASH_PROTECTION
  352. _flash_real_protect(info, 0, 0);
  353. #endif
  354. }
  355. /*-----------------------------------------------------------------------
  356. * Copy memory to flash, returns:
  357. * 0 - OK
  358. * 1 - write timeout
  359. * 2 - Flash not erased
  360. */
  361. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  362. {
  363. ulong cp, wp, data;
  364. int i, l, rc;
  365. wp = (addr & ~3); /* get lower word aligned address */
  366. /*
  367. * handle unaligned start bytes
  368. */
  369. if ((l = addr - wp) != 0) {
  370. data = 0;
  371. for (i=0, cp=wp; i<l; ++i, ++cp) {
  372. data = (data << 8) | (*(uchar *)cp);
  373. }
  374. for (; i<4 && cnt>0; ++i) {
  375. data = (data << 8) | *src++;
  376. --cnt;
  377. ++cp;
  378. }
  379. for (; cnt==0 && i<4; ++i, ++cp) {
  380. data = (data << 8) | (*(uchar *)cp);
  381. }
  382. if ((rc = write_word(info, wp, data)) != 0) {
  383. return (rc);
  384. }
  385. wp += 4;
  386. }
  387. /*
  388. * handle word aligned part
  389. */
  390. while (cnt >= 4) {
  391. data = 0;
  392. for (i=0; i<4; ++i) {
  393. data = (data << 8) | *src++;
  394. }
  395. if ((rc = write_word(info, wp, data)) != 0) {
  396. return (rc);
  397. }
  398. wp += 4;
  399. cnt -= 4;
  400. }
  401. if (cnt == 0) {
  402. return (0);
  403. }
  404. /*
  405. * handle unaligned tail bytes
  406. */
  407. data = 0;
  408. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  409. data = (data << 8) | *src++;
  410. --cnt;
  411. }
  412. for (; i<4; ++i, ++cp) {
  413. data = (data << 8) | (*(uchar *)cp);
  414. }
  415. return (write_word(info, wp, data));
  416. }
  417. /*-----------------------------------------------------------------------
  418. * Write a word to Flash, returns:
  419. * 0 - OK
  420. * 1 - write timeout
  421. * 2 - Flash not erased
  422. */
  423. int write_word (flash_info_t *info, ulong dest, ulong da)
  424. {
  425. vu_short *addr = (vu_short *)dest;
  426. ulong start;
  427. char csr;
  428. int flag;
  429. ushort * d = (ushort*)&da;
  430. int i;
  431. /* Check if Flash is (sufficiently) erased */
  432. if (((*addr & d[0]) != d[0]) || ((*(addr+1) & d[1]) != d[1])) {
  433. return (2);
  434. }
  435. /* Disable interrupts which might cause a timeout here */
  436. flag = disable_interrupts();
  437. for(i = 0; i < 2; i++)
  438. {
  439. /* Write Command */
  440. *addr = 0x0010;
  441. /* Write Data */
  442. *addr = d[i];
  443. /* re-enable interrupts if necessary */
  444. if (flag)
  445. enable_interrupts();
  446. /* data polling for D7 */
  447. start = get_timer (0);
  448. flag = 0;
  449. *addr = 0x0070; /*Read statusregister command */
  450. while (((csr = *addr) & INTEL_FLASH_STATUS_WSMS)!=INTEL_FLASH_STATUS_WSMS) {
  451. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  452. flag = 1;
  453. break;
  454. }
  455. *addr = 0x0070; /*Read statusregister command */
  456. }
  457. if (csr & INTEL_FLASH_STATUS_PSS) {
  458. printf ("CSR indicates write error (%0x) at %08lx\n",
  459. csr, (ulong)addr);
  460. flag = 1;
  461. }
  462. /* Clear Status Registers Command */
  463. *addr = 0x0050;
  464. /* Reset to read array mode */
  465. *addr = 0xffff;
  466. addr++;
  467. }
  468. return (flag);
  469. }
  470. int flash_real_protect(flash_info_t *info, long offset, int prot)
  471. {
  472. int i, idx;
  473. for(idx = 0; idx < info->sector_count; idx++)
  474. if(info->start[idx] == offset)
  475. break;
  476. if(idx==info->sector_count)
  477. return -1;
  478. if(prot == 0) {
  479. /* Unprotect one sector, which means unprotect all flash
  480. * and reprotect the other protected sectors.
  481. */
  482. _flash_real_protect(info, 0, 0); /* Unprotects the whole flash*/
  483. info->protect[idx] = 0;
  484. for(i = 0; i < info->sector_count; i++)
  485. if(info->protect[i])
  486. _flash_real_protect(info, i, 1);
  487. }
  488. else {
  489. /* We can protect individual sectors */
  490. _flash_real_protect(info, idx, 1);
  491. }
  492. for( i = 0; i < info->sector_count; i++)
  493. info->protect[i] = flash_get_protect_status(info, i);
  494. return 0;
  495. }
  496. int _flash_real_protect(flash_info_t *info, long idx, int prot)
  497. {
  498. vu_short *addr;
  499. int flag;
  500. ushort cmd;
  501. ushort tmp;
  502. ulong now, start;
  503. if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK)) {
  504. printf ("Can't change protection for unknown flash type %08lx - aborted\n",
  505. info->flash_id);
  506. return -1;
  507. }
  508. if(prot == 0) {
  509. /*Unlock the sector*/
  510. cmd = 0x00D0;
  511. }
  512. else {
  513. /*Lock the sector*/
  514. cmd = 0x0001;
  515. }
  516. addr = (vu_short *)(info->start[idx]);
  517. /* If chip is busy, wait for it */
  518. start = get_timer(0);
  519. *addr = 0x0070; /*Read status register command*/
  520. tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
  521. while(!(tmp & INTEL_FLASH_STATUS_WSMS)) {
  522. /*Write State Machine Busy*/
  523. /*Wait untill done or timeout.*/
  524. if ((now=get_timer(start)) > CFG_FLASH_WRITE_TOUT) {
  525. *addr = 0x0050; /* Reset the status register */
  526. *addr = 0xffff; /* Reset the chip */
  527. printf ("TTimeout\n");
  528. return 1;
  529. }
  530. *addr = 0x0070;
  531. tmp = ((ushort)(*addr))&0x00ff; /*Read the status*/
  532. start = get_timer(0);
  533. }
  534. /* Disable interrupts which might cause a timeout here */
  535. flag = disable_interrupts();
  536. /* Unlock block*/
  537. *addr = 0x0060;
  538. *addr = cmd;
  539. /* re-enable interrupts if necessary */
  540. if (flag)
  541. enable_interrupts();
  542. start = get_timer(0);
  543. *addr = 0x0070; /*Read status register command*/
  544. tmp = ((ushort)(*addr)) & 0x00FF; /* Read the status */
  545. while (!(tmp & INTEL_FLASH_STATUS_WSMS)) {
  546. /* Write State Machine Busy */
  547. if ((now=get_timer(start)) > CFG_FLASH_WRITE_TOUT) {
  548. *addr = 0x0050; /* Reset the status register */
  549. *addr = 0xffff;
  550. printf ("Timeout\n");
  551. return 1;
  552. }
  553. /* show that we're waiting */
  554. if ((now - start) > 1000) { /* every second */
  555. putc ('.');
  556. }
  557. udelay(100000); /* 100 ms */
  558. *addr = 0x70; /*Read status register command*/
  559. tmp = (short)*addr & 0x00FF; /* Read status */
  560. start = get_timer(0);
  561. }
  562. if( tmp & INTEL_FLASH_STATUS_PS )
  563. flash_decode_status_bits(tmp);
  564. *addr =0x0050; /*Clear status register*/
  565. /* reset to read mode */
  566. *addr = 0xffff;
  567. return 0;
  568. }