flash.c 10 KB

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