flash.c 12 KB

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