flash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  6. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #ifndef CFG_FLASH_CFI
  28. typedef unsigned short FLASH_PORT_WIDTH;
  29. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  30. #define PHYS_FLASH_1 CFG_FLASH_BASE
  31. #define FLASH_BANK_SIZE 0x200000
  32. #define FPW FLASH_PORT_WIDTH
  33. #define FPWV FLASH_PORT_WIDTHV
  34. /* Intel-compatible flash commands */
  35. #define INTEL_PROGRAM 0x00100010
  36. #define INTEL_ERASE 0x00200020
  37. #define INTEL_WRSETUP 0x00400040
  38. #define INTEL_CLEAR 0x00500050
  39. #define INTEL_LOCKBIT 0x00600060
  40. #define INTEL_PROTECT 0x00010001
  41. #define INTEL_STATUS 0x00700070
  42. #define INTEL_READID 0x00900090
  43. #define INTEL_CFIQRY 0x00980098
  44. #define INTEL_SUSERASE 0x00B000B0
  45. #define INTEL_PROTPROG 0x00C000C0
  46. #define INTEL_CONFIRM 0x00D000D0
  47. #define INTEL_RESET 0x00FF00FF
  48. /* Intel-compatible flash status bits */
  49. #define INTEL_FINISHED 0x00800080
  50. #define INTEL_OK 0x00800080
  51. #define INTEL_ERASESUS 0x00600060
  52. #define INTEL_WSM_SUS (INTEL_FINISHED | INTEL_ERASESUS)
  53. /* 28F160C3B CFI Data offset - This could vary */
  54. #define INTEL_CFI_MFG 0x00 /* Manufacturer ID */
  55. #define INTEL_CFI_PART 0x01 /* Product ID */
  56. #define INTEL_CFI_LOCK 0x02 /* */
  57. #define INTEL_CFI_TWPRG 0x1F /* Typical Single Word Program Timeout 2^n us */
  58. #define INTEL_CFI_MBUFW 0x20 /* Typical Max Buffer Write Timeout 2^n us */
  59. #define INTEL_CFI_TERB 0x21 /* Typical Block Erase Timeout 2^n ms */
  60. #define INTEL_CFI_MWPRG 0x23 /* Maximum Word program timeout 2^n us */
  61. #define INTEL_CFI_MERB 0x25 /* Maximum Block Erase Timeout 2^n s */
  62. #define INTEL_CFI_SIZE 0x27 /* Device size 2^n bytes */
  63. #define INTEL_CFI_BANK 0x2C /* Number of Bank */
  64. #define INTEL_CFI_SZ1A 0x2F /* Block Region Size */
  65. #define INTEL_CFI_SZ1B 0x30
  66. #define INTEL_CFI_SZ2A 0x33
  67. #define INTEL_CFI_SZ2B 0x34
  68. #define INTEL_CFI_BLK1 0x2D /* Number of Blocks */
  69. #define INTEL_CFI_BLK2 0x31
  70. #define WR_BLOCK 0x20
  71. #define SYNC __asm__("nop")
  72. /*-----------------------------------------------------------------------
  73. * Functions
  74. */
  75. ulong flash_get_size(FPWV * addr, flash_info_t * info);
  76. int flash_get_offsets(ulong base, flash_info_t * info);
  77. int flash_cmd_rd(FPWV * addr, int index);
  78. int write_data(flash_info_t * info, ulong dest, FPW data);
  79. void flash_sync_real_protect(flash_info_t * info);
  80. uchar intel_sector_protected(flash_info_t * info, ushort sector);
  81. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  82. ulong flash_init(void)
  83. {
  84. FPWV *flash_addr[CFG_MAX_FLASH_BANKS];
  85. ulong size;
  86. int i;
  87. flash_addr[0] = (FPW *) CFG_FLASH0_BASE;
  88. #ifdef CFG_FLASH1_BASE
  89. flash_addr[1] = (FPW *) CFG_FLASH1_BASE;
  90. #endif
  91. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  92. memset(&flash_info[i], 0, sizeof(flash_info_t));
  93. size = flash_get_size(flash_addr[i], &flash_info[i]);
  94. flash_protect(FLAG_PROTECT_CLEAR,
  95. flash_info[i].start[0],
  96. flash_info[i].start[0] + size - 1,
  97. &flash_info[0]);
  98. /* get the h/w and s/w protection status in sync */
  99. flash_sync_real_protect(&flash_info[i]);
  100. }
  101. /* Protect monitor and environment sectors */
  102. flash_protect(FLAG_PROTECT_SET,
  103. CFG_MONITOR_BASE,
  104. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
  105. return size;
  106. }
  107. void flash_print_info(flash_info_t * info)
  108. {
  109. int i;
  110. switch (info->flash_id & FLASH_VENDMASK) {
  111. case FLASH_MAN_INTEL:
  112. printf("INTEL ");
  113. break;
  114. default:
  115. printf("Unknown Vendor ");
  116. break;
  117. }
  118. switch (info->flash_id & FLASH_TYPEMASK) {
  119. case FLASH_28F160C3B:
  120. printf("28F160C3B\n");
  121. break;
  122. case FLASH_28F160C3T:
  123. printf("28F160C3T\n");
  124. break;
  125. case FLASH_28F320C3B:
  126. printf("28F320C3B\n");
  127. break;
  128. case FLASH_28F320C3T:
  129. printf("28F320C3T\n");
  130. break;
  131. case FLASH_28F640C3B:
  132. printf("28F640C3B\n");
  133. break;
  134. case FLASH_28F640C3T:
  135. printf("28F640C3T\n");
  136. break;
  137. default:
  138. printf("Unknown Chip Type\n");
  139. return;
  140. }
  141. if (info->size > 0x100000) {
  142. int remainder;
  143. printf(" Size: %ld", info->size >> 20);
  144. remainder = (info->size % 0x100000);
  145. if (remainder) {
  146. remainder >>= 10;
  147. remainder = (int)((float)
  148. (((float)remainder / (float)1024) *
  149. 10000));
  150. printf(".%d ", remainder);
  151. }
  152. printf("MB in %d Sectors\n", info->sector_count);
  153. } else
  154. printf(" Size: %ld KB in %d Sectors\n",
  155. info->size >> 10, info->sector_count);
  156. printf(" Sector Start Addresses:");
  157. for (i = 0; i < info->sector_count; ++i) {
  158. if ((i % 5) == 0)
  159. printf("\n ");
  160. printf(" %08lX%s",
  161. info->start[i], info->protect[i] ? " (RO)" : " ");
  162. }
  163. printf("\n");
  164. }
  165. /*
  166. * The following code cannot be run from FLASH!
  167. */
  168. ulong flash_get_size(FPWV * addr, flash_info_t * info)
  169. {
  170. int intel = 0;
  171. u16 value;
  172. static int bank = 0;
  173. /* Write auto select command: read Manufacturer ID */
  174. /* Write auto select command sequence and test FLASH answer */
  175. *addr = (FPW) INTEL_RESET; /* restore read mode */
  176. *addr = (FPW) INTEL_READID;
  177. switch (addr[INTEL_CFI_MFG] & 0xff) {
  178. case (ushort) INTEL_MANUFACT:
  179. info->flash_id = FLASH_MAN_INTEL;
  180. value = addr[INTEL_CFI_PART];
  181. intel = 1;
  182. break;
  183. default:
  184. printf("Unknown Flash\n");
  185. info->flash_id = FLASH_UNKNOWN;
  186. info->sector_count = 0;
  187. info->size = 0;
  188. *addr = (FPW) INTEL_RESET; /* restore read mode */
  189. return (0); /* no or unknown flash */
  190. }
  191. switch (value) {
  192. case (u16) INTEL_ID_28F160C3B:
  193. info->flash_id += FLASH_28F160C3B;
  194. break;
  195. case (u16) INTEL_ID_28F160C3T:
  196. info->flash_id += FLASH_28F160C3T;
  197. break;
  198. case (u16) INTEL_ID_28F320C3B:
  199. info->flash_id += FLASH_28F320C3B;
  200. break;
  201. case (u16) INTEL_ID_28F320C3T:
  202. info->flash_id += FLASH_28F320C3T;
  203. break;
  204. case (u16) INTEL_ID_28F640C3B:
  205. info->flash_id += FLASH_28F640C3B;
  206. break;
  207. case (u16) INTEL_ID_28F640C3T:
  208. info->flash_id += FLASH_28F640C3T;
  209. break;
  210. default:
  211. info->flash_id = FLASH_UNKNOWN;
  212. break;
  213. }
  214. if (intel) {
  215. /* Intel spec. under CFI section */
  216. u32 sz, size, offset;
  217. int sec, sectors, bs;
  218. int part, i, j, cnt;
  219. part = flash_cmd_rd(addr, INTEL_CFI_BANK);
  220. /* Geometry y1 = y1 + 1, y2 = y2 + 1, CFI spec.
  221. * To be exact, Z = [0x2f 0x30] (LE) * 256 bytes * [0x2D 0x2E] block count
  222. * Z = [0x33 0x34] (LE) * 256 bytes * [0x31 0x32] block count
  223. */
  224. offset = (u32) addr;
  225. sectors = sec = 0;
  226. size = sz = cnt = 0;
  227. for (i = 0; i < part; i++) {
  228. bs = (((addr[INTEL_CFI_SZ1B + i * 4] << 8) |
  229. addr[INTEL_CFI_SZ1A + i * 4]) * 0x100);
  230. sec = addr[INTEL_CFI_BLK1 + i * 4] + 1;
  231. sz = bs * sec;
  232. for (j = 0; j < sec; j++) {
  233. info->start[cnt++] = offset;
  234. offset += bs;
  235. }
  236. sectors += sec;
  237. size += sz;
  238. }
  239. info->sector_count = sectors;
  240. info->size = size;
  241. }
  242. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  243. printf("** ERROR: sector count %d > max (%d) **\n",
  244. info->sector_count, CFG_MAX_FLASH_SECT);
  245. info->sector_count = CFG_MAX_FLASH_SECT;
  246. }
  247. *addr = (FPW) INTEL_RESET; /* restore read mode */
  248. return (info->size);
  249. }
  250. int flash_cmd_rd(FPWV * addr, int index)
  251. {
  252. return (int)addr[index];
  253. }
  254. /*
  255. * This function gets the u-boot flash sector protection status
  256. * (flash_info_t.protect[]) in sync with the sector protection
  257. * status stored in hardware.
  258. */
  259. void flash_sync_real_protect(flash_info_t * info)
  260. {
  261. int i;
  262. switch (info->flash_id & FLASH_TYPEMASK) {
  263. case FLASH_28F160C3B:
  264. case FLASH_28F160C3T:
  265. case FLASH_28F320C3B:
  266. case FLASH_28F320C3T:
  267. case FLASH_28F640C3B:
  268. case FLASH_28F640C3T:
  269. for (i = 0; i < info->sector_count; ++i) {
  270. info->protect[i] = intel_sector_protected(info, i);
  271. }
  272. break;
  273. default:
  274. /* no h/w protect support */
  275. break;
  276. }
  277. }
  278. /*
  279. * checks if "sector" in bank "info" is protected. Should work on intel
  280. * strata flash chips 28FxxxJ3x in 8-bit mode.
  281. * Returns 1 if sector is protected (or timed-out while trying to read
  282. * protection status), 0 if it is not.
  283. */
  284. uchar intel_sector_protected(flash_info_t * info, ushort sector)
  285. {
  286. FPWV *addr;
  287. FPWV *lock_conf_addr;
  288. ulong start;
  289. unsigned char ret;
  290. /*
  291. * first, wait for the WSM to be finished. The rationale for
  292. * waiting for the WSM to become idle for at most
  293. * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
  294. * because of: (1) erase, (2) program or (3) lock bit
  295. * configuration. So we just wait for the longest timeout of
  296. * the (1)-(3), i.e. the erase timeout.
  297. */
  298. /* wait at least 35ns (W12) before issuing Read Status Register */
  299. /*udelay(1); */
  300. addr = (FPWV *) info->start[sector];
  301. *addr = (FPW) INTEL_STATUS;
  302. start = get_timer(0);
  303. while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
  304. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
  305. *addr = (FPW) INTEL_RESET; /* restore read mode */
  306. printf("WSM busy too long, can't get prot status\n");
  307. return 1;
  308. }
  309. }
  310. /* issue the Read Identifier Codes command */
  311. *addr = (FPW) INTEL_READID;
  312. /* Intel example code uses offset of 4 for 8-bit flash */
  313. lock_conf_addr = (FPWV *) info->start[sector];
  314. ret = (lock_conf_addr[INTEL_CFI_LOCK] & (FPW) INTEL_PROTECT) ? 1 : 0;
  315. /* put flash back in read mode */
  316. *addr = (FPW) INTEL_RESET;
  317. return ret;
  318. }
  319. int flash_erase(flash_info_t * info, int s_first, int s_last)
  320. {
  321. int flag, prot, sect;
  322. ulong type, start, last;
  323. int rcode = 0;
  324. if ((s_first < 0) || (s_first > s_last)) {
  325. if (info->flash_id == FLASH_UNKNOWN)
  326. printf("- missing\n");
  327. else
  328. printf("- no sectors to erase\n");
  329. return 1;
  330. }
  331. type = (info->flash_id & FLASH_VENDMASK);
  332. if ((type != FLASH_MAN_INTEL)) {
  333. type = (info->flash_id & FLASH_VENDMASK);
  334. printf("Can't erase unknown flash type %08lx - aborted\n",
  335. info->flash_id);
  336. return 1;
  337. }
  338. prot = 0;
  339. for (sect = s_first; sect <= s_last; ++sect) {
  340. if (info->protect[sect]) {
  341. prot++;
  342. }
  343. }
  344. if (prot)
  345. printf("- Warning: %d protected sectors will not be erased!\n",
  346. prot);
  347. else
  348. printf("\n");
  349. start = get_timer(0);
  350. last = start;
  351. /* Disable interrupts which might cause a timeout here */
  352. flag = disable_interrupts();
  353. /* Start erase on unprotected sectors */
  354. for (sect = s_first; sect <= s_last; sect++) {
  355. if (info->protect[sect] == 0) { /* not protected */
  356. FPWV *addr = (FPWV *) (info->start[sect]);
  357. int min = 0;
  358. printf("Erasing sector %2d ... ", sect);
  359. /* arm simple, non interrupt dependent timer */
  360. start = get_timer(0);
  361. *addr = (FPW) INTEL_READID;
  362. min = addr[INTEL_CFI_TERB];
  363. min = 1 << min; /* ms */
  364. min = (min / info->sector_count) * 1000;
  365. /* start erase block */
  366. *addr = (FPW) INTEL_CLEAR; /* clear status register */
  367. *addr = (FPW) INTEL_ERASE; /* erase setup */
  368. *addr = (FPW) INTEL_CONFIRM; /* erase confirm */
  369. while ((*addr & (FPW) INTEL_FINISHED) !=
  370. (FPW) INTEL_FINISHED) {
  371. if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
  372. printf("Timeout\n");
  373. *addr = (FPW) INTEL_SUSERASE; /* suspend erase */
  374. *addr = (FPW) INTEL_RESET; /* reset to read mode */
  375. rcode = 1;
  376. break;
  377. }
  378. }
  379. *addr = (FPW) INTEL_RESET; /* resest to read mode */
  380. printf(" done\n");
  381. }
  382. }
  383. return rcode;
  384. }
  385. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  386. {
  387. if (info->flash_id == FLASH_UNKNOWN)
  388. return 4;
  389. switch (info->flash_id & FLASH_VENDMASK) {
  390. case FLASH_MAN_INTEL:
  391. {
  392. ulong cp, wp;
  393. FPW data;
  394. int i, l, rc, port_width;
  395. /* get lower word aligned address */
  396. wp = addr;
  397. port_width = 1;
  398. /*
  399. * handle unaligned start bytes
  400. */
  401. if ((l = addr - wp) != 0) {
  402. data = 0;
  403. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  404. data = (data << 8) | (*(uchar *) cp);
  405. }
  406. for (; i < port_width && cnt > 0; ++i) {
  407. data = (data << 8) | *src++;
  408. --cnt;
  409. ++cp;
  410. }
  411. for (; cnt == 0 && i < port_width; ++i, ++cp)
  412. data = (data << 8) | (*(uchar *) cp);
  413. if ((rc = write_data(info, wp, data)) != 0)
  414. return (rc);
  415. wp += port_width;
  416. }
  417. /* handle word aligned part */
  418. while (cnt >= 2) {
  419. data = *((FPW *) src);
  420. if ((rc =
  421. write_data(info, (ulong) ((FPWV *) wp),
  422. (FPW) data)) != 0) {
  423. return (rc);
  424. }
  425. src += sizeof(FPW);
  426. wp += sizeof(FPW);
  427. cnt -= sizeof(FPW);
  428. }
  429. if (cnt == 0)
  430. return ERR_OK;
  431. /*
  432. * handle unaligned tail bytes
  433. */
  434. data = 0;
  435. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  436. data = (data >> 8) | (*src++ << 8);
  437. --cnt;
  438. }
  439. for (; i < 2; ++i, ++cp) {
  440. data |= (*(uchar *) cp);
  441. }
  442. return write_data(info, (ulong) ((FPWV *) wp),
  443. (FPW) data);
  444. } /* case FLASH_MAN_INTEL */
  445. } /* switch */
  446. return ERR_OK;
  447. }
  448. /*-----------------------------------------------------------------------
  449. * Write a word or halfword to Flash, returns:
  450. * 0 - OK
  451. * 1 - write timeout
  452. * 2 - Flash not erased
  453. */
  454. int write_data(flash_info_t * info, ulong dest, FPW data)
  455. {
  456. FPWV *addr = (FPWV *) dest;
  457. ulong start;
  458. int flag;
  459. /* Check if Flash is (sufficiently) erased */
  460. if ((*addr & data) != data) {
  461. printf("not erased at %08lx (%lx)\n", (ulong) addr,
  462. (ulong) * addr);
  463. return (2);
  464. }
  465. /* Disable interrupts which might cause a timeout here */
  466. flag = (int)disable_interrupts();
  467. *addr = (FPW) INTEL_CLEAR;
  468. *addr = (FPW) INTEL_RESET;
  469. *addr = (FPW) INTEL_WRSETUP; /* write setup */
  470. *addr = data;
  471. /* arm simple, non interrupt dependent timer */
  472. start = get_timer(0);
  473. /* wait while polling the status register */
  474. while ((*addr & (FPW) INTEL_OK) != (FPW) INTEL_OK) {
  475. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  476. *addr = (FPW) INTEL_SUSERASE; /* suspend mode */
  477. *addr = (FPW) INTEL_CLEAR; /* clear status */
  478. *addr = (FPW) INTEL_RESET; /* reset */
  479. return (1);
  480. }
  481. }
  482. *addr = (FPW) INTEL_CLEAR; /* clear status */
  483. *addr = (FPW) INTEL_RESET; /* restore read mode */
  484. return (0);
  485. }
  486. #ifdef CFG_FLASH_PROTECTION
  487. /*-----------------------------------------------------------------------
  488. */
  489. int flash_real_protect(flash_info_t * info, long sector, int prot)
  490. {
  491. int rcode = 0; /* assume success */
  492. FPWV *addr; /* address of sector */
  493. FPW value;
  494. addr = (FPWV *) (info->start[sector]);
  495. switch (info->flash_id & FLASH_TYPEMASK) {
  496. case FLASH_28F160C3B:
  497. case FLASH_28F160C3T:
  498. case FLASH_28F320C3B:
  499. case FLASH_28F320C3T:
  500. case FLASH_28F640C3B:
  501. case FLASH_28F640C3T:
  502. *addr = (FPW) INTEL_RESET; /* make sure in read mode */
  503. *addr = (FPW) INTEL_LOCKBIT; /* lock command setup */
  504. if (prot)
  505. *addr = (FPW) INTEL_PROTECT; /* lock sector */
  506. else
  507. *addr = (FPW) INTEL_CONFIRM; /* unlock sector */
  508. /* now see if it really is locked/unlocked as requested */
  509. *addr = (FPW) INTEL_READID;
  510. /* read sector protection at sector address, (A7 .. A0) = 0x02.
  511. * D0 = 1 for each device if protected.
  512. * If at least one device is protected the sector is marked
  513. * protected, but return failure. Mixed protected and
  514. * unprotected devices within a sector should never happen.
  515. */
  516. value = addr[2] & (FPW) INTEL_PROTECT;
  517. if (value == 0)
  518. info->protect[sector] = 0;
  519. else if (value == (FPW) INTEL_PROTECT)
  520. info->protect[sector] = 1;
  521. else {
  522. /* error, mixed protected and unprotected */
  523. rcode = 1;
  524. info->protect[sector] = 1;
  525. }
  526. if (info->protect[sector] != prot)
  527. rcode = 1; /* failed to protect/unprotect as requested */
  528. /* reload all protection bits from hardware for now */
  529. flash_sync_real_protect(info);
  530. break;
  531. default:
  532. /* no hardware protect that we support */
  533. info->protect[sector] = prot;
  534. break;
  535. }
  536. return rcode;
  537. }
  538. #endif /* CFG_FLASH_PROTECTION */
  539. #endif /* CFG_FLASH_CFI */