flash.c 12 KB

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