flash.c 15 KB

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