flash.c 11 KB

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