flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
  6. * Add support the Sharp chips on the mpc8260ads.
  7. * I started with board/ip860/flash.c and made changes I found in
  8. * the MTD project by David Schleef.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  14. #if defined(CONFIG_ENV_IS_IN_FLASH)
  15. # ifndef CONFIG_ENV_ADDR
  16. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  17. # endif
  18. # ifndef CONFIG_ENV_SIZE
  19. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  20. # endif
  21. # ifndef CONFIG_ENV_SECT_SIZE
  22. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  23. # endif
  24. #endif
  25. /*-----------------------------------------------------------------------
  26. * Functions
  27. */
  28. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  29. static int write_word (flash_info_t *info, ulong dest, ulong data);
  30. static int clear_block_lock_bit(vu_long * addr);
  31. /*-----------------------------------------------------------------------
  32. */
  33. unsigned long flash_init (void)
  34. {
  35. #ifndef CONFIG_MPC8266ADS
  36. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  37. volatile memctl8xx_t *memctl = &immap->im_memctl;
  38. volatile ip860_bcsr_t *bcsr = (ip860_bcsr_t *)BCSR_BASE;
  39. #endif
  40. unsigned long size;
  41. int i;
  42. /* Init: enable write,
  43. * or we cannot even write flash commands
  44. */
  45. #ifndef CONFIG_MPC8266ADS
  46. bcsr->bd_ctrl |= BD_CTRL_FLWE;
  47. #endif
  48. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  49. flash_info[i].flash_id = FLASH_UNKNOWN;
  50. /* set the default sector offset */
  51. }
  52. /* Static FLASH Bank configuration here - FIXME XXX */
  53. size = flash_get_size((vu_long *)FLASH_BASE, &flash_info[0]);
  54. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  55. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  56. size, size<<20);
  57. }
  58. #ifndef CONFIG_MPC8266ADS
  59. /* Remap FLASH according to real size */
  60. memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
  61. memctl->memc_br1 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
  62. (memctl->memc_br1 & ~(BR_BA_MSK));
  63. #endif
  64. /* Re-do sizing to get full correct info */
  65. size = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  66. flash_info[0].size = size;
  67. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  68. /* monitor protection ON by default */
  69. flash_protect(FLAG_PROTECT_SET,
  70. CONFIG_SYS_MONITOR_BASE,
  71. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  72. &flash_info[0]);
  73. #endif
  74. #ifdef CONFIG_ENV_IS_IN_FLASH
  75. /* ENV protection ON by default */
  76. flash_protect(FLAG_PROTECT_SET,
  77. CONFIG_ENV_ADDR,
  78. CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
  79. &flash_info[0]);
  80. #endif
  81. return (size);
  82. }
  83. /*-----------------------------------------------------------------------
  84. */
  85. void flash_print_info (flash_info_t *info)
  86. {
  87. int i;
  88. if (info->flash_id == FLASH_UNKNOWN) {
  89. printf ("missing or unknown FLASH type\n");
  90. return;
  91. }
  92. switch (info->flash_id & FLASH_VENDMASK) {
  93. case FLASH_MAN_INTEL: printf ("Intel "); break;
  94. case FLASH_MAN_SHARP: printf ("Sharp "); break;
  95. default: printf ("Unknown Vendor "); break;
  96. }
  97. switch (info->flash_id & FLASH_TYPEMASK) {
  98. case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
  99. break;
  100. case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
  101. break;
  102. case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
  103. break;
  104. case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
  105. break;
  106. default: printf ("Unknown Chip Type\n");
  107. break;
  108. }
  109. printf (" Size: %ld MB in %d Sectors\n",
  110. info->size >> 20, info->sector_count);
  111. printf (" Sector Start Addresses:");
  112. for (i=0; i<info->sector_count; ++i) {
  113. if ((i % 5) == 0)
  114. printf ("\n ");
  115. printf (" %08lX%s",
  116. info->start[i],
  117. info->protect[i] ? " (RO)" : " "
  118. );
  119. }
  120. printf ("\n");
  121. }
  122. /*-----------------------------------------------------------------------
  123. */
  124. /*-----------------------------------------------------------------------
  125. */
  126. /*
  127. * The following code cannot be run from FLASH!
  128. */
  129. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  130. {
  131. short i;
  132. ulong value;
  133. ulong base = (ulong)addr;
  134. ulong sector_offset;
  135. /* Write "Intelligent Identifier" command: read Manufacturer ID */
  136. *addr = 0x90909090;
  137. value = addr[0] & 0x00FF00FF;
  138. switch (value) {
  139. case MT_MANUFACT: /* SHARP, MT or => Intel */
  140. case INTEL_ALT_MANU:
  141. info->flash_id = FLASH_MAN_INTEL;
  142. break;
  143. default:
  144. printf("unknown manufacturer: %x\n", (unsigned int)value);
  145. info->flash_id = FLASH_UNKNOWN;
  146. info->sector_count = 0;
  147. info->size = 0;
  148. return (0); /* no or unknown flash */
  149. }
  150. value = addr[1]; /* device ID */
  151. switch (value) {
  152. case (INTEL_ID_28F016S):
  153. info->flash_id += FLASH_28F016SV;
  154. info->sector_count = 32;
  155. info->size = 0x00400000;
  156. sector_offset = 0x20000;
  157. break; /* => 2x2 MB */
  158. case (INTEL_ID_28F160S3):
  159. info->flash_id += FLASH_28F160S3;
  160. info->sector_count = 32;
  161. info->size = 0x00400000;
  162. sector_offset = 0x20000;
  163. break; /* => 2x2 MB */
  164. case (INTEL_ID_28F320S3):
  165. info->flash_id += FLASH_28F320S3;
  166. info->sector_count = 64;
  167. info->size = 0x00800000;
  168. sector_offset = 0x20000;
  169. break; /* => 2x4 MB */
  170. case SHARP_ID_28F016SCL:
  171. case SHARP_ID_28F016SCZ:
  172. info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
  173. info->sector_count = 32;
  174. info->size = 0x00800000;
  175. sector_offset = 0x40000;
  176. break; /* => 4x2 MB */
  177. default:
  178. info->flash_id = FLASH_UNKNOWN;
  179. return (0); /* => no or unknown flash */
  180. }
  181. /* set up sector start address table */
  182. for (i = 0; i < info->sector_count; i++) {
  183. info->start[i] = base;
  184. base += sector_offset;
  185. /* don't know how to check sector protection */
  186. info->protect[i] = 0;
  187. }
  188. /*
  189. * Prevent writes to uninitialized FLASH.
  190. */
  191. if (info->flash_id != FLASH_UNKNOWN) {
  192. addr = (vu_long *)info->start[0];
  193. *addr = 0xFFFFFF; /* reset bank to read array mode */
  194. }
  195. return (info->size);
  196. }
  197. /*-----------------------------------------------------------------------
  198. */
  199. int flash_erase (flash_info_t *info, int s_first, int s_last)
  200. {
  201. int flag, prot, sect;
  202. ulong start, now, last;
  203. if ((s_first < 0) || (s_first > s_last)) {
  204. if (info->flash_id == FLASH_UNKNOWN) {
  205. printf ("- missing\n");
  206. } else {
  207. printf ("- no sectors to erase\n");
  208. }
  209. return 1;
  210. }
  211. if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
  212. && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
  213. printf ("Can't erase unknown flash type %08lx - aborted\n",
  214. info->flash_id);
  215. return 1;
  216. }
  217. prot = 0;
  218. for (sect=s_first; sect<=s_last; ++sect) {
  219. if (info->protect[sect]) {
  220. prot++;
  221. }
  222. }
  223. if (prot) {
  224. printf ("- Warning: %d protected sectors will not be erased!\n",
  225. prot);
  226. } else {
  227. printf ("\n");
  228. }
  229. /* Make Sure Block Lock Bit is not set. */
  230. if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
  231. return 1;
  232. }
  233. /* Start erase on unprotected sectors */
  234. for (sect = s_first; sect<=s_last; sect++) {
  235. if (info->protect[sect] == 0) { /* not protected */
  236. vu_long *addr = (vu_long *)(info->start[sect]);
  237. last = start = get_timer (0);
  238. /* Disable interrupts which might cause a timeout here */
  239. flag = disable_interrupts();
  240. /* Reset Array */
  241. *addr = 0xffffffff;
  242. /* Clear Status Register */
  243. *addr = 0x50505050;
  244. /* Single Block Erase Command */
  245. *addr = 0x20202020;
  246. /* Confirm */
  247. *addr = 0xD0D0D0D0;
  248. if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
  249. /* Resume Command, as per errata update */
  250. *addr = 0xD0D0D0D0;
  251. }
  252. /* re-enable interrupts if necessary */
  253. if (flag)
  254. enable_interrupts();
  255. /* wait at least 80us - let's wait 1 ms */
  256. udelay (1000);
  257. while ((*addr & 0x80808080) != 0x80808080) {
  258. if(*addr & 0x20202020){
  259. printf("Error in Block Erase - Lock Bit may be set!\n");
  260. printf("Status Register = 0x%X\n", (uint)*addr);
  261. *addr = 0xFFFFFFFF; /* reset bank */
  262. return 1;
  263. }
  264. if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  265. printf ("Timeout\n");
  266. *addr = 0xFFFFFFFF; /* reset bank */
  267. return 1;
  268. }
  269. /* show that we're waiting */
  270. if ((now - last) > 1000) { /* every second */
  271. putc ('.');
  272. last = now;
  273. }
  274. }
  275. /* reset to read mode */
  276. *addr = 0xFFFFFFFF;
  277. }
  278. }
  279. printf (" done\n");
  280. return 0;
  281. }
  282. /*-----------------------------------------------------------------------
  283. * Copy memory to flash, returns:
  284. * 0 - OK
  285. * 1 - write timeout
  286. * 2 - Flash not erased
  287. */
  288. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  289. {
  290. ulong cp, wp, data;
  291. int i, l, rc;
  292. wp = (addr & ~3); /* get lower word aligned address */
  293. /*
  294. * handle unaligned start bytes
  295. */
  296. if ((l = addr - wp) != 0) {
  297. data = 0;
  298. for (i=0, cp=wp; i<l; ++i, ++cp) {
  299. data = (data << 8) | (*(uchar *)cp);
  300. }
  301. for (; i<4 && cnt>0; ++i) {
  302. data = (data << 8) | *src++;
  303. --cnt;
  304. ++cp;
  305. }
  306. for (; cnt==0 && i<4; ++i, ++cp) {
  307. data = (data << 8) | (*(uchar *)cp);
  308. }
  309. if ((rc = write_word(info, wp, data)) != 0) {
  310. return (rc);
  311. }
  312. wp += 4;
  313. }
  314. /*
  315. * handle word aligned part
  316. */
  317. while (cnt >= 4) {
  318. data = 0;
  319. for (i=0; i<4; ++i) {
  320. data = (data << 8) | *src++;
  321. }
  322. if ((rc = write_word(info, wp, data)) != 0) {
  323. return (rc);
  324. }
  325. wp += 4;
  326. cnt -= 4;
  327. }
  328. if (cnt == 0) {
  329. return (0);
  330. }
  331. /*
  332. * handle unaligned tail bytes
  333. */
  334. data = 0;
  335. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  336. data = (data << 8) | *src++;
  337. --cnt;
  338. }
  339. for (; i<4; ++i, ++cp) {
  340. data = (data << 8) | (*(uchar *)cp);
  341. }
  342. return (write_word(info, wp, data));
  343. }
  344. /*-----------------------------------------------------------------------
  345. * Write a word to Flash, returns:
  346. * 0 - OK
  347. * 1 - write timeout
  348. * 2 - Flash not erased
  349. */
  350. static int write_word (flash_info_t *info, ulong dest, ulong data)
  351. {
  352. vu_long *addr = (vu_long *)dest;
  353. ulong start, csr;
  354. int flag;
  355. /* Check if Flash is (sufficiently) erased */
  356. if ((*addr & data) != data) {
  357. return (2);
  358. }
  359. /* Disable interrupts which might cause a timeout here */
  360. flag = disable_interrupts();
  361. /* Write Command */
  362. *addr = 0x10101010;
  363. /* Write Data */
  364. *addr = data;
  365. /* re-enable interrupts if necessary */
  366. if (flag)
  367. enable_interrupts();
  368. /* data polling for D7 */
  369. start = get_timer (0);
  370. flag = 0;
  371. while (((csr = *addr) & 0x80808080) != 0x80808080) {
  372. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  373. flag = 1;
  374. break;
  375. }
  376. }
  377. if (csr & 0x40404040) {
  378. printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
  379. flag = 1;
  380. }
  381. /* Clear Status Registers Command */
  382. *addr = 0x50505050;
  383. /* Reset to read array mode */
  384. *addr = 0xFFFFFFFF;
  385. return (flag);
  386. }
  387. /*-----------------------------------------------------------------------
  388. * Clear Block Lock Bit, returns:
  389. * 0 - OK
  390. * 1 - Timeout
  391. */
  392. static int clear_block_lock_bit(vu_long * addr)
  393. {
  394. ulong start, now;
  395. /* Reset Array */
  396. *addr = 0xffffffff;
  397. /* Clear Status Register */
  398. *addr = 0x50505050;
  399. *addr = 0x60606060;
  400. *addr = 0xd0d0d0d0;
  401. start = get_timer (0);
  402. while(*addr != 0x80808080){
  403. if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  404. printf ("Timeout on clearing Block Lock Bit\n");
  405. *addr = 0xFFFFFFFF; /* reset bank */
  406. return 1;
  407. }
  408. }
  409. return 0;
  410. }