flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * (C) Copyright 2001-2003
  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 <mpc824x.h>
  25. #include <asm/processor.h>
  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. #define FLASH_BANK_SIZE 0x800000
  38. #define MAIN_SECT_SIZE 0x40000
  39. #define PARAM_SECT_SIZE 0x8000
  40. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  41. static int write_data (flash_info_t * info, ulong dest, ulong * data);
  42. static void write_via_fpu (vu_long * addr, ulong * data);
  43. static __inline__ unsigned long get_msr (void);
  44. static __inline__ void set_msr (unsigned long msr);
  45. /*---------------------------------------------------------------------*/
  46. #undef DEBUG_FLASH
  47. /*---------------------------------------------------------------------*/
  48. #ifdef DEBUG_FLASH
  49. #define DEBUGF(fmt,args...) printf(fmt ,##args)
  50. #else
  51. #define DEBUGF(fmt,args...)
  52. #endif
  53. /*---------------------------------------------------------------------*/
  54. /*-----------------------------------------------------------------------
  55. */
  56. unsigned long flash_init (void)
  57. {
  58. int i, j;
  59. ulong size = 0;
  60. uchar tempChar;
  61. vu_long *tmpaddr;
  62. /* Enable flash writes on CPC45 */
  63. tempChar = BOARD_CTRL;
  64. tempChar |= (B_CTRL_FWPT_1 | B_CTRL_FWRE_1);
  65. tempChar &= ~(B_CTRL_FWPT_0 | B_CTRL_FWRE_0);
  66. BOARD_CTRL = tempChar;
  67. __asm__ volatile ("sync\n eieio");
  68. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  69. vu_long *addr = (vu_long *) (CFG_FLASH_BASE + i * FLASH_BANK_SIZE);
  70. addr[0] = 0x00900090;
  71. __asm__ volatile ("sync\n eieio");
  72. udelay (100);
  73. DEBUGF ("Flash bank # %d:\n"
  74. "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
  75. "\tDevice ID @ 0x%08lX: 0x%08lX\n",
  76. i,
  77. (ulong) (&addr[0]), addr[0],
  78. (ulong) (&addr[2]), addr[2]);
  79. if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) &&
  80. (addr[2] == addr[3]) && (addr[2] == INTEL_ID_28F160F3T)) {
  81. flash_info[i].flash_id =
  82. (FLASH_MAN_INTEL & FLASH_VENDMASK) |
  83. (INTEL_ID_28F160F3T & FLASH_TYPEMASK);
  84. } else if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT)
  85. && (addr[2] == addr[3])
  86. && (addr[2] == INTEL_ID_28F160C3T)) {
  87. flash_info[i].flash_id =
  88. (FLASH_MAN_INTEL & FLASH_VENDMASK) |
  89. (INTEL_ID_28F160C3T & FLASH_TYPEMASK);
  90. } else {
  91. flash_info[i].flash_id = FLASH_UNKNOWN;
  92. addr[0] = 0xFFFFFFFF;
  93. goto Done;
  94. }
  95. DEBUGF ("flash_id = 0x%08lX\n", flash_info[i].flash_id);
  96. addr[0] = 0xFFFFFFFF;
  97. flash_info[i].size = FLASH_BANK_SIZE;
  98. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  99. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  100. for (j = 0; j < flash_info[i].sector_count; j++) {
  101. if (j > 30) {
  102. flash_info[i].start[j] = CFG_FLASH_BASE +
  103. i * FLASH_BANK_SIZE +
  104. (MAIN_SECT_SIZE * 31) + (j -
  105. 31) *
  106. PARAM_SECT_SIZE;
  107. } else {
  108. flash_info[i].start[j] = CFG_FLASH_BASE +
  109. i * FLASH_BANK_SIZE +
  110. j * MAIN_SECT_SIZE;
  111. }
  112. }
  113. /* unlock sectors, if 160C3T */
  114. for (j = 0; j < flash_info[i].sector_count; j++) {
  115. tmpaddr = (vu_long *) flash_info[i].start[j];
  116. if ((flash_info[i].flash_id & FLASH_TYPEMASK) ==
  117. (INTEL_ID_28F160C3T & FLASH_TYPEMASK)) {
  118. tmpaddr[0] = 0x00600060;
  119. tmpaddr[0] = 0x00D000D0;
  120. tmpaddr[1] = 0x00600060;
  121. tmpaddr[1] = 0x00D000D0;
  122. }
  123. }
  124. size += flash_info[i].size;
  125. addr[0] = 0x00FF00FF;
  126. addr[1] = 0x00FF00FF;
  127. }
  128. /* Protect monitor and environment sectors
  129. */
  130. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE + FLASH_BANK_SIZE
  131. flash_protect (FLAG_PROTECT_SET,
  132. CFG_MONITOR_BASE,
  133. CFG_MONITOR_BASE + monitor_flash_len - 1,
  134. &flash_info[1]);
  135. #else
  136. flash_protect (FLAG_PROTECT_SET,
  137. CFG_MONITOR_BASE,
  138. CFG_MONITOR_BASE + monitor_flash_len - 1,
  139. &flash_info[0]);
  140. #endif
  141. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  142. #if CFG_ENV_ADDR >= CFG_FLASH_BASE + FLASH_BANK_SIZE
  143. flash_protect (FLAG_PROTECT_SET,
  144. CFG_ENV_ADDR,
  145. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[1]);
  146. #else
  147. flash_protect (FLAG_PROTECT_SET,
  148. CFG_ENV_ADDR,
  149. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  150. #endif
  151. #endif
  152. Done:
  153. return size;
  154. }
  155. /*-----------------------------------------------------------------------
  156. */
  157. void flash_print_info (flash_info_t * info)
  158. {
  159. int i;
  160. switch ((i = info->flash_id & FLASH_VENDMASK)) {
  161. case (FLASH_MAN_INTEL & FLASH_VENDMASK):
  162. printf ("Intel: ");
  163. break;
  164. default:
  165. printf ("Unknown Vendor 0x%04x ", i);
  166. break;
  167. }
  168. switch ((i = info->flash_id & FLASH_TYPEMASK)) {
  169. case (INTEL_ID_28F160F3T & FLASH_TYPEMASK):
  170. printf ("28F160F3T (16Mbit)\n");
  171. break;
  172. case (INTEL_ID_28F160C3T & FLASH_TYPEMASK):
  173. printf ("28F160C3T (16Mbit)\n");
  174. break;
  175. default:
  176. printf ("Unknown Chip Type 0x%04x\n", i);
  177. goto Done;
  178. break;
  179. }
  180. printf (" Size: %ld MB in %d Sectors\n",
  181. info->size >> 20, info->sector_count);
  182. printf (" Sector Start Addresses:");
  183. for (i = 0; i < info->sector_count; i++) {
  184. if ((i % 5) == 0) {
  185. printf ("\n ");
  186. }
  187. printf (" %08lX%s", info->start[i],
  188. info->protect[i] ? " (RO)" : " ");
  189. }
  190. printf ("\n");
  191. Done:
  192. return;
  193. }
  194. /*-----------------------------------------------------------------------
  195. */
  196. int flash_erase (flash_info_t * info, int s_first, int s_last)
  197. {
  198. int flag, prot, sect;
  199. ulong start, now, last;
  200. DEBUGF ("Erase flash bank %d sect %d ... %d\n",
  201. info - &flash_info[0], s_first, s_last);
  202. if ((s_first < 0) || (s_first > s_last)) {
  203. if (info->flash_id == FLASH_UNKNOWN) {
  204. printf ("- missing\n");
  205. } else {
  206. printf ("- no sectors to erase\n");
  207. }
  208. return 1;
  209. }
  210. if ((info->flash_id & FLASH_VENDMASK) !=
  211. (FLASH_MAN_INTEL & FLASH_VENDMASK)) {
  212. printf ("Can erase only Intel flash types - aborted\n");
  213. return 1;
  214. }
  215. prot = 0;
  216. for (sect = s_first; sect <= s_last; ++sect) {
  217. if (info->protect[sect]) {
  218. prot++;
  219. }
  220. }
  221. if (prot) {
  222. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  223. } else {
  224. printf ("\n");
  225. }
  226. start = get_timer (0);
  227. last = start;
  228. /* Start erase on unprotected sectors */
  229. for (sect = s_first; sect <= s_last; sect++) {
  230. if (info->protect[sect] == 0) { /* not protected */
  231. vu_long *addr = (vu_long *) (info->start[sect]);
  232. DEBUGF ("Erase sect %d @ 0x%08lX\n",
  233. sect, (ulong) addr);
  234. /* Disable interrupts which might cause a timeout
  235. * here.
  236. */
  237. flag = disable_interrupts ();
  238. addr[0] = 0x00500050; /* clear status register */
  239. addr[0] = 0x00200020; /* erase setup */
  240. addr[0] = 0x00D000D0; /* erase confirm */
  241. addr[1] = 0x00500050; /* clear status register */
  242. addr[1] = 0x00200020; /* erase setup */
  243. addr[1] = 0x00D000D0; /* erase confirm */
  244. /* re-enable interrupts if necessary */
  245. if (flag)
  246. enable_interrupts ();
  247. /* wait at least 80us - let's wait 1 ms */
  248. udelay (1000);
  249. while (((addr[0] & 0x00800080) != 0x00800080) ||
  250. ((addr[1] & 0x00800080) != 0x00800080)) {
  251. if ((now = get_timer (start)) >
  252. CFG_FLASH_ERASE_TOUT) {
  253. printf ("Timeout\n");
  254. addr[0] = 0x00B000B0; /* suspend erase */
  255. addr[0] = 0x00FF00FF; /* to read mode */
  256. return 1;
  257. }
  258. /* show that we're waiting */
  259. if ((now - last) > 1000) { /* every second */
  260. putc ('.');
  261. last = now;
  262. }
  263. }
  264. addr[0] = 0x00FF00FF;
  265. }
  266. }
  267. printf (" done\n");
  268. return 0;
  269. }
  270. /*-----------------------------------------------------------------------
  271. * Copy memory to flash, returns:
  272. * 0 - OK
  273. * 1 - write timeout
  274. * 2 - Flash not erased
  275. * 4 - Flash not identified
  276. */
  277. #define FLASH_WIDTH 8 /* flash bus width in bytes */
  278. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  279. {
  280. ulong wp, cp, msr;
  281. int l, rc, i;
  282. ulong data[2];
  283. ulong *datah = &data[0];
  284. ulong *datal = &data[1];
  285. DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
  286. addr, (ulong) src, cnt);
  287. if (info->flash_id == FLASH_UNKNOWN) {
  288. return 4;
  289. }
  290. msr = get_msr ();
  291. set_msr (msr | MSR_FP);
  292. wp = (addr & ~(FLASH_WIDTH - 1)); /* get lower aligned address */
  293. /*
  294. * handle unaligned start bytes
  295. */
  296. if ((l = addr - wp) != 0) {
  297. *datah = *datal = 0;
  298. for (i = 0, cp = wp; i < l; i++, cp++) {
  299. if (i >= 4) {
  300. *datah = (*datah << 8) |
  301. ((*datal & 0xFF000000) >> 24);
  302. }
  303. *datal = (*datal << 8) | (*(uchar *) cp);
  304. }
  305. for (; i < FLASH_WIDTH && cnt > 0; ++i) {
  306. char tmp = *src++;
  307. if (i >= 4) {
  308. *datah = (*datah << 8) |
  309. ((*datal & 0xFF000000) >> 24);
  310. }
  311. *datal = (*datal << 8) | tmp;
  312. --cnt;
  313. ++cp;
  314. }
  315. for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) {
  316. if (i >= 4) {
  317. *datah = (*datah << 8) |
  318. ((*datal & 0xFF000000) >> 24);
  319. }
  320. *datal = (*datah << 8) | (*(uchar *) cp);
  321. }
  322. if ((rc = write_data (info, wp, data)) != 0) {
  323. set_msr (msr);
  324. return (rc);
  325. }
  326. wp += FLASH_WIDTH;
  327. }
  328. /*
  329. * handle FLASH_WIDTH aligned part
  330. */
  331. while (cnt >= FLASH_WIDTH) {
  332. *datah = *(ulong *) src;
  333. *datal = *(ulong *) (src + 4);
  334. if ((rc = write_data (info, wp, data)) != 0) {
  335. set_msr (msr);
  336. return (rc);
  337. }
  338. wp += FLASH_WIDTH;
  339. cnt -= FLASH_WIDTH;
  340. src += FLASH_WIDTH;
  341. }
  342. if (cnt == 0) {
  343. set_msr (msr);
  344. return (0);
  345. }
  346. /*
  347. * handle unaligned tail bytes
  348. */
  349. *datah = *datal = 0;
  350. for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) {
  351. char tmp = *src++;
  352. if (i >= 4) {
  353. *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
  354. 24);
  355. }
  356. *datal = (*datal << 8) | tmp;
  357. --cnt;
  358. }
  359. for (; i < FLASH_WIDTH; ++i, ++cp) {
  360. if (i >= 4) {
  361. *datah = (*datah << 8) | ((*datal & 0xFF000000) >>
  362. 24);
  363. }
  364. *datal = (*datal << 8) | (*(uchar *) cp);
  365. }
  366. rc = write_data (info, wp, data);
  367. set_msr (msr);
  368. return (rc);
  369. }
  370. /*-----------------------------------------------------------------------
  371. * Write a word to Flash, returns:
  372. * 0 - OK
  373. * 1 - write timeout
  374. * 2 - Flash not erased
  375. */
  376. static int write_data (flash_info_t * info, ulong dest, ulong * data)
  377. {
  378. vu_long *addr = (vu_long *) dest;
  379. ulong start;
  380. int flag;
  381. /* Check if Flash is (sufficiently) erased */
  382. if (((addr[0] & data[0]) != data[0]) ||
  383. ((addr[1] & data[1]) != data[1])) {
  384. return (2);
  385. }
  386. /* Disable interrupts which might cause a timeout here */
  387. flag = disable_interrupts ();
  388. addr[0] = 0x00400040; /* write setup */
  389. write_via_fpu (addr, data);
  390. /* re-enable interrupts if necessary */
  391. if (flag)
  392. enable_interrupts ();
  393. start = get_timer (0);
  394. while (((addr[0] & 0x00800080) != 0x00800080) ||
  395. ((addr[1] & 0x00800080) != 0x00800080)) {
  396. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  397. addr[0] = 0x00FF00FF; /* restore read mode */
  398. return (1);
  399. }
  400. }
  401. addr[0] = 0x00FF00FF; /* restore read mode */
  402. return (0);
  403. }
  404. /*-----------------------------------------------------------------------
  405. */
  406. static void write_via_fpu (vu_long * addr, ulong * data)
  407. {
  408. __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data));
  409. __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr));
  410. }
  411. /*-----------------------------------------------------------------------
  412. */
  413. static __inline__ unsigned long get_msr (void)
  414. {
  415. unsigned long msr;
  416. __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
  417. return msr;
  418. }
  419. static __inline__ void set_msr (unsigned long msr)
  420. {
  421. __asm__ __volatile__ ("mtmsr %0"::"r" (msr));
  422. }