flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * Keith Outwater, keith_outwater@mvsi.com
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <mpc8xx.h>
  10. #if defined(CONFIG_ENV_IS_IN_FLASH)
  11. # ifndef CONFIG_ENV_ADDR
  12. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  13. # endif
  14. # ifndef CONFIG_ENV_SIZE
  15. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  16. # endif
  17. # ifndef CONFIG_ENV_SECT_SIZE
  18. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  19. # endif
  20. #endif
  21. /*
  22. * Use buffered writes to flash by default - they are about 32x faster than
  23. * single byte writes.
  24. */
  25. #ifndef CONFIG_SYS_GEN860T_FLASH_USE_WRITE_BUFFER
  26. #define CONFIG_SYS_GEN860T_FLASH_USE_WRITE_BUFFER
  27. #endif
  28. /*
  29. * Max time to wait (in mS) for flash device to allocate a write buffer.
  30. */
  31. #ifndef CONFIG_SYS_FLASH_ALLOC_BUFFER_TOUT
  32. #define CONFIG_SYS_FLASH_ALLOC_BUFFER_TOUT 100
  33. #endif
  34. /*
  35. * These functions support a single Intel StrataFlash device (28F128J3A)
  36. * in byte mode only!. The flash routines are very basic and simple
  37. * since there isn't really any remapping necessary.
  38. */
  39. /*
  40. * Intel SCS (Scalable Command Set) command definitions
  41. * (taken from 28F128J3A datasheet)
  42. */
  43. #define SCS_READ_CMD 0xff
  44. #define SCS_READ_ID_CMD 0x90
  45. #define SCS_QUERY_CMD 0x98
  46. #define SCS_READ_STATUS_CMD 0x70
  47. #define SCS_CLEAR_STATUS_CMD 0x50
  48. #define SCS_WRITE_BUF_CMD 0xe8
  49. #define SCS_PROGRAM_CMD 0x40
  50. #define SCS_BLOCK_ERASE_CMD 0x20
  51. #define SCS_BLOCK_ERASE_RESUME_CMD 0xd0
  52. #define SCS_PROGRAM_RESUME_CMD 0xd0
  53. #define SCS_BLOCK_ERASE_SUSPEND_CMD 0xb0
  54. #define SCS_SET_BLOCK_LOCK_CMD 0x60
  55. #define SCS_CLR_BLOCK_LOCK_CMD 0x60
  56. /*
  57. * SCS status/extended status register bit definitions
  58. */
  59. #define SCS_SR7 0x80
  60. #define SCS_XSR7 0x80
  61. /*---------------------------------------------------------------------*/
  62. #if 0
  63. #define DEBUG_FLASH
  64. #endif
  65. #ifdef DEBUG_FLASH
  66. #define PRINTF(fmt,args...) printf(fmt ,##args)
  67. #else
  68. #define PRINTF(fmt,args...)
  69. #endif
  70. /*---------------------------------------------------------------------*/
  71. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  72. /*-----------------------------------------------------------------------
  73. * Functions
  74. */
  75. static ulong flash_get_size (vu_char *addr, flash_info_t *info);
  76. static int write_data8 (flash_info_t *info, ulong dest, uchar data);
  77. static void flash_get_offsets (ulong base, flash_info_t *info);
  78. /*-----------------------------------------------------------------------
  79. * Initialize the flash memory.
  80. */
  81. unsigned long
  82. flash_init (void)
  83. {
  84. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  85. volatile memctl8xx_t *memctl = &immap->im_memctl;
  86. unsigned long size_b0;
  87. int i;
  88. for (i= 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  89. flash_info[i].flash_id = FLASH_UNKNOWN;
  90. }
  91. /*
  92. * The gen860t board only has one FLASH memory device, so the
  93. * FLASH Bank configuration is done statically.
  94. */
  95. PRINTF("\n## Get flash bank 1 size @ 0x%08x\n", FLASH_BASE0_PRELIM);
  96. size_b0 = flash_get_size((vu_char *)FLASH_BASE0_PRELIM, &flash_info[0]);
  97. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  98. printf ("## Unknown FLASH on Bank 0: "
  99. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  100. flash_info[0].flash_id,size_b0, size_b0 << 20);
  101. }
  102. PRINTF("## Before remap:\n"
  103. " BR0: 0x%08x OR0: 0x%08x\n BR1: 0x%08x OR1: 0x%08x\n",
  104. memctl->memc_br0, memctl->memc_or0,
  105. memctl->memc_br1, memctl->memc_or1);
  106. /*
  107. * Remap FLASH according to real size
  108. */
  109. memctl->memc_or0 |= (-size_b0 & 0xFFFF8000);
  110. memctl->memc_br0 |= (CONFIG_SYS_FLASH_BASE & BR_BA_MSK);
  111. PRINTF("## After remap:\n"
  112. " BR0: 0x%08x OR0: 0x%08x\n", memctl->memc_br0, memctl->memc_or0);
  113. /*
  114. * Re-do sizing to get full correct info
  115. */
  116. size_b0 = flash_get_size ((vu_char *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  117. flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  118. flash_info[0].size = size_b0;
  119. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  120. /*
  121. * Monitor protection is ON by default
  122. */
  123. flash_protect(FLAG_PROTECT_SET,
  124. CONFIG_SYS_MONITOR_BASE,
  125. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  126. &flash_info[0]);
  127. #endif
  128. #ifdef CONFIG_ENV_IS_IN_FLASH
  129. /*
  130. * Environment protection ON by default
  131. */
  132. flash_protect(FLAG_PROTECT_SET,
  133. CONFIG_ENV_ADDR,
  134. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  135. &flash_info[0]);
  136. #endif
  137. PRINTF("## Final Flash bank size: 0x%08lx\n",size_b0);
  138. return (size_b0);
  139. }
  140. /*-----------------------------------------------------------------------
  141. * Fill in the FLASH offset table
  142. */
  143. static void
  144. flash_get_offsets (ulong base, flash_info_t *info)
  145. {
  146. int i;
  147. if (info->flash_id == FLASH_UNKNOWN) {
  148. return;
  149. }
  150. switch (info->flash_id & FLASH_VENDMASK) {
  151. case FLASH_MAN_INTEL:
  152. for (i = 0; i < info->sector_count; i++) {
  153. info->start[i] = base;
  154. base += 1024 * 128;
  155. }
  156. return;
  157. default:
  158. printf ("Don't know sector offsets for FLASH"
  159. " type 0x%lx\n", info->flash_id);
  160. return;
  161. }
  162. }
  163. /*-----------------------------------------------------------------------
  164. * Display FLASH device info
  165. */
  166. void
  167. flash_print_info (flash_info_t *info)
  168. {
  169. int i;
  170. if (info->flash_id == FLASH_UNKNOWN) {
  171. printf ("Missing or unknown FLASH type\n");
  172. return;
  173. }
  174. switch (info->flash_id & FLASH_VENDMASK) {
  175. case FLASH_MAN_INTEL:
  176. printf ("Intel ");
  177. break;
  178. default:
  179. printf ("Unknown Vendor ");
  180. break;
  181. }
  182. switch (info->flash_id & FLASH_TYPEMASK) {
  183. case FLASH_28F128J3A:
  184. printf ("28F128J3A (128Mbit = 128K x 128)\n");
  185. break;
  186. default:
  187. printf ("Unknown Chip Type\n");
  188. break;
  189. }
  190. if (info->size >= (1024 * 1024)) {
  191. i = 20;
  192. } else {
  193. i = 10;
  194. }
  195. printf (" Size: %ld %cB in %d Sectors\n",
  196. info->size >> i,
  197. (i == 20) ? 'M' : 'k',
  198. info->sector_count);
  199. printf (" Sector Start Addresses:");
  200. for (i=0; i<info->sector_count; ++i) {
  201. if ((i % 5) == 0)
  202. printf ("\n ");
  203. printf (" %08lX%s",
  204. info->start[i],
  205. info->protect[i] ? " (RO)" : " "
  206. );
  207. }
  208. printf ("\n");
  209. return;
  210. }
  211. /*-----------------------------------------------------------------------
  212. * Get size and other information for a FLASH device.
  213. * NOTE: The following code cannot be run from FLASH!
  214. */
  215. static
  216. ulong flash_get_size (vu_char *addr, flash_info_t *info)
  217. {
  218. #define NO_FLASH 0
  219. vu_char value[2];
  220. /*
  221. * Try to read the manufacturer ID
  222. */
  223. addr[0] = SCS_READ_CMD;
  224. addr[0] = SCS_READ_ID_CMD;
  225. value[0] = addr[0];
  226. value[1] = addr[2];
  227. addr[0] = SCS_READ_CMD;
  228. PRINTF("Manuf. ID @ 0x%08lx: 0x%02x\n", (ulong)addr, value[0]);
  229. switch (value[0]) {
  230. case (INTEL_MANUFACT & 0xff):
  231. info->flash_id = FLASH_MAN_INTEL;
  232. break;
  233. default:
  234. info->flash_id = FLASH_UNKNOWN;
  235. info->sector_count = 0;
  236. info->size = 0;
  237. return (NO_FLASH);
  238. }
  239. /*
  240. * Read the device ID
  241. */
  242. PRINTF("Device ID @ 0x%08lx: 0x%02x\n", (ulong)(&addr[2]), value[1]);
  243. switch (value[1]) {
  244. case (INTEL_ID_28F128J3A & 0xff):
  245. info->flash_id += FLASH_28F128J3A;
  246. info->sector_count = 128;
  247. info->size = 16 * 1024 * 1024;
  248. break;
  249. default:
  250. info->flash_id = FLASH_UNKNOWN;
  251. return (NO_FLASH);
  252. }
  253. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  254. printf ("** ERROR: sector count %d > max (%d) **\n",
  255. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  256. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  257. }
  258. return (info->size);
  259. }
  260. /*-----------------------------------------------------------------------
  261. * Erase the specified sectors in the specified FLASH device
  262. */
  263. int
  264. flash_erase(flash_info_t *info, int s_first, int s_last)
  265. {
  266. int flag, prot, sect;
  267. ulong start, now, last;
  268. if ((s_first < 0) || (s_first > s_last)) {
  269. if (info->flash_id == FLASH_UNKNOWN) {
  270. printf ("- missing\n");
  271. } else {
  272. printf ("- no sectors to erase\n");
  273. }
  274. return 1;
  275. }
  276. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  277. printf ("Can erase only Intel flash types - aborted\n");
  278. return 1;
  279. }
  280. prot = 0;
  281. for (sect=s_first; sect<=s_last; ++sect) {
  282. if (info->protect[sect]) {
  283. prot++;
  284. }
  285. }
  286. if (prot) {
  287. printf ("- Warning: %d protected sectors will not be erased!\n",
  288. prot);
  289. } else {
  290. printf ("\n");
  291. }
  292. start = get_timer (0);
  293. last = start;
  294. /*
  295. * Start erase on unprotected sectors
  296. */
  297. for (sect = s_first; sect<=s_last; sect++) {
  298. if (info->protect[sect] == 0) { /* not protected */
  299. vu_char *addr = (uchar *)(info->start[sect]);
  300. vu_char status;
  301. /*
  302. * Disable interrupts which might cause a timeout
  303. */
  304. flag = disable_interrupts();
  305. *addr = SCS_CLEAR_STATUS_CMD;
  306. *addr = SCS_BLOCK_ERASE_CMD;
  307. *addr = SCS_BLOCK_ERASE_RESUME_CMD;
  308. /*
  309. * Re-enable interrupts if necessary
  310. */
  311. if (flag)
  312. enable_interrupts();
  313. /*
  314. * Wait at least 80us - let's wait 1 ms
  315. */
  316. udelay (1000);
  317. while (((status = *addr) & SCS_SR7) != SCS_SR7) {
  318. if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  319. printf ("Timeout\n");
  320. *addr = SCS_BLOCK_ERASE_SUSPEND_CMD;
  321. *addr = SCS_READ_CMD;
  322. return 1;
  323. }
  324. /*
  325. * Show that we're waiting
  326. */
  327. if ((now - last) > 1000) { /* 1 second */
  328. putc ('.');
  329. last = now;
  330. }
  331. }
  332. *addr = SCS_READ_CMD;
  333. }
  334. }
  335. printf (" done\n");
  336. return 0;
  337. }
  338. #ifdef CONFIG_SYS_GEN860T_FLASH_USE_WRITE_BUFFER
  339. /*
  340. * Allocate a flash buffer, fill it with data and write it to the flash.
  341. * 0 - OK
  342. * 1 - Timeout on buffer request
  343. *
  344. * NOTE: After the last call to this function, WSM status needs to be checked!
  345. */
  346. static int
  347. write_flash_buffer8(flash_info_t *info_p, vu_char *src_p, vu_char *dest_p,
  348. uint count)
  349. {
  350. vu_char *block_addr_p = NULL;
  351. vu_char *start_addr_p = NULL;
  352. ulong blocksize = info_p->size / (ulong)info_p->sector_count;
  353. int i;
  354. uint time = get_timer(0);
  355. PRINTF("%s:%d: src: 0x%p dest: 0x%p count: %d\n",
  356. __FUNCTION__, __LINE__, src_p, dest_p, count);
  357. /*
  358. * What block are we in? We already know that the source address is
  359. * in the flash address range, but we also can't cross a block boundary.
  360. * We assume that the block does not cross a boundary (we'll check before
  361. * calling this function).
  362. */
  363. for (i = 0; i < info_p->sector_count; ++i) {
  364. if ( ((ulong)dest_p >= info_p->start[i]) &&
  365. ((ulong)dest_p < (info_p->start[i] + blocksize)) ) {
  366. PRINTF("%s:%d: Dest addr 0x%p is in block %d @ 0x%.8lx\n",
  367. __FUNCTION__, __LINE__, dest_p, i, info_p->start[i]);
  368. block_addr_p = (vu_char *)info_p->start[i];
  369. break;
  370. }
  371. }
  372. /*
  373. * Request a buffer
  374. */
  375. *block_addr_p = SCS_WRITE_BUF_CMD;
  376. while ((*block_addr_p & SCS_XSR7) != SCS_XSR7) {
  377. if (get_timer(time) > CONFIG_SYS_FLASH_ALLOC_BUFFER_TOUT) {
  378. PRINTF("%s:%d: Buffer allocation timeout @ 0x%p (waited %d mS)\n",
  379. __FUNCTION__, __LINE__, block_addr_p,
  380. CONFIG_SYS_FLASH_ALLOC_BUFFER_TOUT);
  381. return 1;
  382. }
  383. *block_addr_p = SCS_WRITE_BUF_CMD;
  384. }
  385. /*
  386. * Fill the buffer with data
  387. */
  388. start_addr_p = dest_p;
  389. *block_addr_p = count - 1; /* flash device wants count - 1 */
  390. PRINTF("%s:%d: Fill buffer at block addr 0x%p\n",
  391. __FUNCTION__, __LINE__, block_addr_p);
  392. for (i = 0; i < count; i++) {
  393. *start_addr_p++ = *src_p++;
  394. }
  395. /*
  396. * Flush buffer to flash
  397. */
  398. *block_addr_p = SCS_PROGRAM_RESUME_CMD;
  399. #if 1
  400. time = get_timer(0);
  401. while ((*block_addr_p & SCS_SR7) != SCS_SR7) {
  402. if (get_timer(time) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  403. PRINTF("%s:%d: Write timeout @ 0x%p (waited %d mS)\n",
  404. __FUNCTION__, __LINE__, block_addr_p, CONFIG_SYS_FLASH_WRITE_TOUT);
  405. return 1;
  406. }
  407. }
  408. #endif
  409. return 0;
  410. }
  411. #endif
  412. /*-----------------------------------------------------------------------
  413. * Copy memory to flash, returns:
  414. * 0 - OK
  415. * 1 - write timeout
  416. * 2 - Flash not erased
  417. * 4 - Flash not identified
  418. */
  419. int
  420. write_buff(flash_info_t *info_p, uchar *src_p, ulong addr, ulong count)
  421. {
  422. int rc = 0;
  423. #ifdef CONFIG_SYS_GEN860T_FLASH_USE_WRITE_BUFFER
  424. #define FLASH_WRITE_BUF_SIZE 0x00000020 /* 32 bytes */
  425. int i;
  426. uint bufs;
  427. ulong buf_count;
  428. vu_char *sp;
  429. vu_char *dp;
  430. #else
  431. ulong wp;
  432. #endif
  433. PRINTF("\n%s:%d: src: 0x%.8lx dest: 0x%.8lx size: %d (0x%.8lx)\n",
  434. __FUNCTION__, __LINE__, (ulong)src_p, addr, (uint)count, count);
  435. if (info_p->flash_id == FLASH_UNKNOWN) {
  436. return 4;
  437. }
  438. #ifdef CONFIG_SYS_GEN860T_FLASH_USE_WRITE_BUFFER
  439. sp = src_p;
  440. dp = (uchar *)addr;
  441. /*
  442. * For maximum performance, we want to align the start address to
  443. * the beginning of a write buffer boundary (i.e. A4-A0 of the
  444. * start address = 0). See how many bytes are required to get to a
  445. * write-buffer-aligned address. If that number is non-zero, do
  446. * non buffered writes of the non-aligned data. By doing non-buffered
  447. * writes, we avoid the problem of crossing a block (sector) boundary
  448. * with buffered writes.
  449. */
  450. buf_count = FLASH_WRITE_BUF_SIZE - (addr & (FLASH_WRITE_BUF_SIZE - 1));
  451. if (buf_count == FLASH_WRITE_BUF_SIZE) { /* already on a boundary */
  452. buf_count = 0;
  453. }
  454. if (buf_count > count) { /* not a full buffers worth of data to write */
  455. buf_count = count;
  456. }
  457. count -= buf_count;
  458. PRINTF("%s:%d: Write buffer alignment count = %ld\n",
  459. __FUNCTION__, __LINE__, buf_count);
  460. while (buf_count-- >= 1) {
  461. if ((rc = write_data8(info_p, (ulong)dp++, *sp++)) != 0) {
  462. return (rc);
  463. }
  464. }
  465. PRINTF("%s:%d: count = %ld\n", __FUNCTION__, __LINE__, count);
  466. if (count == 0) { /* all done */
  467. PRINTF("%s:%d: Less than 1 buffer (%d) worth of bytes\n",
  468. __FUNCTION__, __LINE__, FLASH_WRITE_BUF_SIZE);
  469. return (rc);
  470. }
  471. /*
  472. * Now that we are write buffer aligned, write full or partial buffers.
  473. * The fact that we are write buffer aligned automatically avoids
  474. * crossing a block address during a write buffer operation.
  475. */
  476. bufs = count / FLASH_WRITE_BUF_SIZE;
  477. PRINTF("%s:%d: %d (0x%x) buffers to write\n", __FUNCTION__, __LINE__,
  478. bufs, bufs);
  479. while (bufs >= 1) {
  480. rc = write_flash_buffer8(info_p, sp, dp, FLASH_WRITE_BUF_SIZE);
  481. if (rc != 0) {
  482. PRINTF("%s:%d: ** Error writing buf %d\n",
  483. __FUNCTION__, __LINE__, bufs);
  484. return (rc);
  485. }
  486. bufs--;
  487. sp += FLASH_WRITE_BUF_SIZE;
  488. dp += FLASH_WRITE_BUF_SIZE;
  489. }
  490. /*
  491. * Do the leftovers
  492. */
  493. i = count % FLASH_WRITE_BUF_SIZE;
  494. PRINTF("%s:%d: %d (0x%x) leftover bytes\n", __FUNCTION__, __LINE__, i, i);
  495. if (i > 0) {
  496. rc = write_flash_buffer8(info_p, sp, dp, i);
  497. }
  498. sp = (vu_char*)info_p->start[0];
  499. *sp = SCS_READ_CMD;
  500. return (rc);
  501. #else
  502. wp = addr;
  503. while (count-- >= 1) {
  504. if((rc = write_data8(info_p, wp++, *src_p++)) != 0)
  505. return (rc);
  506. }
  507. return 0;
  508. #endif
  509. }
  510. /*-----------------------------------------------------------------------
  511. * Write a byte to Flash, returns:
  512. * 0 - OK
  513. * 1 - write timeout
  514. * 2 - Flash not erased
  515. */
  516. static int
  517. write_data8 (flash_info_t *info, ulong dest, uchar data)
  518. {
  519. vu_char *addr = (vu_char *)dest;
  520. vu_char status;
  521. ulong start;
  522. int flag;
  523. /* Check if Flash is (sufficiently) erased */
  524. if ((*addr & data) != data) {
  525. return (2);
  526. }
  527. /* Disable interrupts which might cause a timeout here */
  528. flag = disable_interrupts();
  529. *addr = SCS_PROGRAM_CMD;
  530. *addr = data;
  531. /* re-enable interrupts if necessary */
  532. if (flag)
  533. enable_interrupts();
  534. start = get_timer (0);
  535. while (((status = *addr) & SCS_SR7) != SCS_SR7) {
  536. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  537. *addr = SCS_READ_CMD;
  538. return (1);
  539. }
  540. }
  541. *addr = SCS_READ_CMD;
  542. return (0);
  543. }
  544. /* vim: set ts=4 sw=4 tw=78: */