flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * (C) Copyright 2003
  3. * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * File: flash.c
  9. *
  10. * Discription: This Driver is for 28F320J3A, 28F640J3A and
  11. * 28F128J3A Intel flashs working in 16 Bit mode.
  12. * They are single bank flashs.
  13. *
  14. * Most of this code is taken from existing u-boot
  15. * source code.
  16. */
  17. #include <common.h>
  18. #include <mpc5xx.h>
  19. #if defined(CONFIG_ENV_IS_IN_FLASH)
  20. # ifndef CONFIG_ENV_ADDR
  21. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  22. # endif
  23. # ifndef CONFIG_ENV_SIZE
  24. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  25. # endif
  26. # ifndef CONFIG_ENV_SECT_SIZE
  27. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  28. # endif
  29. #endif
  30. #define FLASH_ID_MASK 0xFFFF
  31. #define FLASH_BLOCK_SIZE 0x00010000
  32. #define FLASH_CMD_READ_ID 0x0090
  33. #define FLASH_CMD_RESET 0x00ff
  34. #define FLASH_CMD_BLOCK_ERASE 0x0020
  35. #define FLASH_CMD_ERASE_CONFIRM 0x00D0
  36. #define FLASH_CMD_CLEAR_STATUS 0x0050
  37. #define FLASH_CMD_SUSPEND_ERASE 0x00B0
  38. #define FLASH_CMD_WRITE 0x0040
  39. #define FLASH_CMD_PROTECT 0x0060
  40. #define FLASH_CMD_PROTECT_SET 0x0001
  41. #define FLASH_CMD_PROTECT_CLEAR 0x00D0
  42. #define FLASH_STATUS_DONE 0x0080
  43. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  44. /*
  45. * Local function prototypes
  46. */
  47. static ulong flash_get_size (vu_short *addr, flash_info_t *info);
  48. static int write_short (flash_info_t *info, ulong dest, ushort data);
  49. static void flash_get_offsets (ulong base, flash_info_t *info);
  50. /*
  51. * Initialize flash
  52. */
  53. unsigned long flash_init (void)
  54. {
  55. unsigned long size_b0;
  56. int i;
  57. /* Init: no FLASHes known */
  58. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  59. flash_info[i].flash_id = FLASH_UNKNOWN;
  60. }
  61. /* Static FLASH Bank configuration here - FIXME XXX */
  62. #if 1
  63. debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
  64. #endif
  65. size_b0 = flash_get_size((vu_short *)FLASH_BASE0_PRELIM, &flash_info[0]);
  66. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  67. printf ("## Unknown FLASH on Bank 0: "
  68. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  69. flash_info[0].flash_id,
  70. size_b0, size_b0<<20);
  71. }
  72. flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
  73. flash_info[0].size = size_b0;
  74. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  75. /* monitor protection ON by default */
  76. flash_protect(FLAG_PROTECT_SET,
  77. CONFIG_SYS_MONITOR_BASE,
  78. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  79. &flash_info[0]);
  80. #endif
  81. #ifdef CONFIG_ENV_IS_IN_FLASH
  82. /* ENV protection ON by default */
  83. flash_protect(FLAG_PROTECT_SET,
  84. CONFIG_ENV_ADDR,
  85. CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
  86. &flash_info[0]);
  87. #endif
  88. return size_b0;
  89. }
  90. /*
  91. * Compute start adress of each sector (block)
  92. */
  93. static void flash_get_offsets (ulong base, flash_info_t *info)
  94. {
  95. int i;
  96. if (info->flash_id == FLASH_UNKNOWN) {
  97. return;
  98. }
  99. switch (info->flash_id & FLASH_VENDMASK) {
  100. case FLASH_MAN_INTEL:
  101. for (i = 0; i < info->sector_count; i++) {
  102. info->start[i] = base + i * FLASH_BLOCK_SIZE;
  103. }
  104. return;
  105. default:
  106. printf ("Don't know sector offsets for flash type 0x%lx\n",
  107. info->flash_id);
  108. return;
  109. }
  110. }
  111. /*
  112. * Print flash information
  113. */
  114. void flash_print_info (flash_info_t *info)
  115. {
  116. int i;
  117. if (info->flash_id == FLASH_UNKNOWN) {
  118. printf ("missing or unknown FLASH type\n");
  119. return;
  120. }
  121. switch (info->flash_id & FLASH_VENDMASK) {
  122. case FLASH_MAN_AMD: printf ("AMD "); break;
  123. case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
  124. case FLASH_MAN_SST: printf ("SST "); break;
  125. case FLASH_MAN_STM: printf ("STM "); break;
  126. case FLASH_MAN_INTEL: printf ("Intel "); break;
  127. case FLASH_MAN_MT: printf ("MT "); break;
  128. default: printf ("Unknown Vendor "); break;
  129. }
  130. switch (info->flash_id & FLASH_TYPEMASK) {
  131. case FLASH_28F320J3A: printf ("28F320J3A (32Mbit) 16-Bit\n");
  132. break;
  133. case FLASH_28F640J3A: printf ("28F640J3A (64Mbit) 16-Bit\n");
  134. break;
  135. case FLASH_28F128J3A: printf ("28F128J3A (128Mbit) 16-Bit\n");
  136. break;
  137. default: printf ("Unknown Chip Type\n");
  138. break;
  139. }
  140. if (info->size >= (1 << 20)) {
  141. i = 20;
  142. } else {
  143. i = 10;
  144. }
  145. printf (" Size: %ld %cB in %d Sectors\n",
  146. info->size >> i,
  147. (i == 20) ? 'M' : 'k',
  148. info->sector_count);
  149. printf (" Sector Start Addresses:");
  150. for (i=0; i<info->sector_count; ++i) {
  151. if ((i % 5) == 0)
  152. printf ("\n ");
  153. printf (" %08lX%s",
  154. info->start[i],
  155. info->protect[i] ? " (RO)" : " "
  156. );
  157. }
  158. printf ("\n");
  159. return;
  160. }
  161. /*
  162. * Get size of flash in bytes.
  163. * The following code cannot be run from FLASH!
  164. */
  165. static ulong flash_get_size (vu_short *addr, flash_info_t *info)
  166. {
  167. vu_short value;
  168. /* Read Manufacturer ID */
  169. addr[0] = FLASH_CMD_READ_ID;
  170. value = addr[0];
  171. switch (value) {
  172. case (AMD_MANUFACT & FLASH_ID_MASK):
  173. info->flash_id = FLASH_MAN_AMD;
  174. break;
  175. case (FUJ_MANUFACT & FLASH_ID_MASK):
  176. info->flash_id = FLASH_MAN_FUJ;
  177. break;
  178. case (SST_MANUFACT & FLASH_ID_MASK):
  179. info->flash_id = FLASH_MAN_SST;
  180. break;
  181. case (STM_MANUFACT & FLASH_ID_MASK):
  182. info->flash_id = FLASH_MAN_STM;
  183. break;
  184. case (INTEL_MANUFACT & FLASH_ID_MASK):
  185. info->flash_id = FLASH_MAN_INTEL;
  186. break;
  187. default:
  188. info->flash_id = FLASH_UNKNOWN;
  189. info->sector_count = 0;
  190. info->size = 0;
  191. addr[0] = FLASH_CMD_RESET; /* restore read mode */
  192. return (0); /* no or unknown flash */
  193. }
  194. value = addr[1]; /* device ID */
  195. switch (value) {
  196. case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
  197. info->flash_id += FLASH_28F320J3A;
  198. info->sector_count = 32;
  199. info->size = 0x00400000;
  200. break; /* => 32 MBit */
  201. case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
  202. info->flash_id += FLASH_28F640J3A;
  203. info->sector_count = 64;
  204. info->size = 0x00800000;
  205. break; /* => 64 MBit */
  206. case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
  207. info->flash_id += FLASH_28F128J3A;
  208. info->sector_count = 128;
  209. info->size = 0x01000000;
  210. break; /* => 128 MBit */
  211. default:
  212. info->flash_id = FLASH_UNKNOWN;
  213. addr[0] = FLASH_CMD_RESET; /* restore read mode */
  214. return (0); /* => no or unknown flash */
  215. }
  216. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  217. printf ("** ERROR: sector count %d > max (%d) **\n",
  218. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  219. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  220. }
  221. addr[0] = FLASH_CMD_RESET; /* restore read mode */
  222. return (info->size);
  223. }
  224. /*
  225. * Erase unprotected sectors
  226. */
  227. int flash_erase (flash_info_t *info, int s_first, int s_last)
  228. {
  229. int flag, prot, sect;
  230. ulong start, now, last;
  231. if ((s_first < 0) || (s_first > s_last)) {
  232. if (info->flash_id == FLASH_UNKNOWN) {
  233. printf ("- missing\n");
  234. } else {
  235. printf ("- no sectors to erase\n");
  236. }
  237. return 1;
  238. }
  239. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  240. printf ("Can erase only Intel flash types - aborted\n");
  241. return 1;
  242. }
  243. prot = 0;
  244. for (sect=s_first; sect<=s_last; ++sect) {
  245. if (info->protect[sect]) {
  246. prot++;
  247. }
  248. }
  249. if (prot) {
  250. printf ("- Warning: %d protected sectors will not be erased!\n",
  251. prot);
  252. } else {
  253. printf ("\n");
  254. }
  255. start = get_timer (0);
  256. last = start;
  257. /* Start erase on unprotected sectors */
  258. for (sect = s_first; sect<=s_last; sect++) {
  259. if (info->protect[sect] == 0) { /* not protected */
  260. vu_short *addr = (vu_short *)(info->start[sect]);
  261. unsigned long status;
  262. /* Disable interrupts which might cause a timeout here */
  263. flag = disable_interrupts();
  264. #ifdef DEBUG
  265. printf("Erase sector %d at start addr 0x%08X", sect, (unsigned int)info->start[sect]);
  266. #endif
  267. *addr = FLASH_CMD_CLEAR_STATUS;
  268. *addr = FLASH_CMD_BLOCK_ERASE;
  269. *addr = FLASH_CMD_ERASE_CONFIRM;
  270. /* re-enable interrupts if necessary */
  271. if (flag)
  272. enable_interrupts();
  273. /* wait at least 80us - let's wait 1 ms */
  274. udelay (1000);
  275. while (((status = *addr) & FLASH_STATUS_DONE) != FLASH_STATUS_DONE) {
  276. if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  277. printf("Flash erase timeout at address %lx\n", info->start[sect]);
  278. *addr = FLASH_CMD_SUSPEND_ERASE;
  279. *addr = FLASH_CMD_RESET;
  280. return 1;
  281. }
  282. /* show that we're waiting */
  283. if ((now - last) > 1000) { /* every second */
  284. putc ('.');
  285. last = now;
  286. }
  287. }
  288. *addr = FLASH_CMD_RESET;
  289. }
  290. }
  291. printf (" done\n");
  292. return 0;
  293. }
  294. /*
  295. * Copy memory to flash, returns:
  296. * 0 - OK
  297. * 1 - write timeout
  298. * 2 - Flash not erased
  299. * 4 - Flash not identified
  300. */
  301. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  302. {
  303. ulong cp, wp;
  304. ushort data;
  305. int i, rc;
  306. if (info->flash_id == FLASH_UNKNOWN) {
  307. return 4;
  308. }
  309. wp = (addr & ~1); /* get lower word aligned address */
  310. /*
  311. * handle unaligned start byte
  312. */
  313. if (addr - wp) {
  314. data = 0;
  315. data = (data << 8) | *src++;
  316. --cnt;
  317. if ((rc = write_short(info, wp, data)) != 0) {
  318. return (rc);
  319. }
  320. wp += 2;
  321. }
  322. /*
  323. * handle word aligned part
  324. */
  325. while (cnt >= 2) {
  326. data = 0;
  327. for (i=0; i<2; ++i) {
  328. data = (data << 8) | *src++;
  329. }
  330. if ((rc = write_short(info, wp, data)) != 0) {
  331. return (rc);
  332. }
  333. wp += 2;
  334. cnt -= 2;
  335. }
  336. if (cnt == 0) {
  337. return (0);
  338. }
  339. /*
  340. * handle unaligned tail bytes
  341. */
  342. data = 0;
  343. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  344. data = (data << 8) | *src++;
  345. --cnt;
  346. }
  347. for (; i<2; ++i, ++cp) {
  348. data = (data << 8) | (*(uchar *)cp);
  349. }
  350. return (write_short(info, wp, data));
  351. }
  352. /*
  353. * Write 16 bit (short) to flash
  354. */
  355. static int write_short (flash_info_t *info, ulong dest, ushort data)
  356. {
  357. vu_short *addr = (vu_short*)(info->start[0]);
  358. ulong start;
  359. int flag;
  360. /* Check if Flash is (sufficiently) erased */
  361. if ((*((vu_short *)dest) & data) != data) {
  362. return (2);
  363. }
  364. /* Disable interrupts which might cause a timeout here */
  365. flag = disable_interrupts();
  366. if (!(info->flash_id & FLASH_VENDMASK)) {
  367. return 4;
  368. }
  369. *addr = FLASH_CMD_ERASE_CONFIRM;
  370. *addr = FLASH_CMD_WRITE;
  371. *((vu_short *)dest) = data;
  372. /* re-enable interrupts if necessary */
  373. if (flag) {
  374. enable_interrupts();
  375. }
  376. /* data polling for D7 */
  377. start = get_timer (0);
  378. /* wait for error or finish */
  379. while(!(addr[0] & FLASH_STATUS_DONE)){
  380. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  381. addr[0] = FLASH_CMD_RESET;
  382. return (1);
  383. }
  384. }
  385. *addr = FLASH_CMD_RESET;
  386. return (0);
  387. }
  388. /*
  389. * Protects a flash sector
  390. */
  391. int flash_real_protect(flash_info_t *info, long sector, int prot)
  392. {
  393. vu_short *addr = (vu_short*)(info->start[sector]);
  394. ulong start;
  395. *addr = FLASH_CMD_CLEAR_STATUS;
  396. *addr = FLASH_CMD_PROTECT;
  397. if(prot) {
  398. *addr = FLASH_CMD_PROTECT_SET;
  399. } else {
  400. *addr = FLASH_CMD_PROTECT_CLEAR;
  401. }
  402. /* wait for error or finish */
  403. start = get_timer (0);
  404. while(!(addr[0] & FLASH_STATUS_DONE)){
  405. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  406. printf("Flash protect timeout at address %lx\n", info->start[sector]);
  407. addr[0] = FLASH_CMD_RESET;
  408. return (1);
  409. }
  410. }
  411. /* Set software protect flag */
  412. info->protect[sector] = prot;
  413. *addr = FLASH_CMD_RESET;
  414. return (0);
  415. }