flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * (C) Copyright 2003
  3. * EMK Elektronik GmbH <www.emk-elektronik.de>
  4. * Reinhard Meyer <r.meyer@emk-elektronik.de>
  5. *
  6. * copied from the BMW Port - seems that its similiar enough
  7. * to be easily adaped ;) --- Well, it turned out to become a
  8. * merger between parts of the EMKstax Flash routines and the
  9. * BMW funtion frames...
  10. *
  11. * (C) Copyright 2000
  12. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <mpc8xx.h>
  34. #define FLASH_WORD_SIZE unsigned short
  35. #define FLASH_WORD_WIDTH (sizeof (FLASH_WORD_SIZE))
  36. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  37. /*-----------------------------------------------------------------------
  38. * Functions
  39. */
  40. static int write_word (flash_info_t *info, ulong dest, ulong data);
  41. /*****************************************************************************
  42. * software product ID entry/exit
  43. *****************************************************************************/
  44. static void FlashProductIdMode (
  45. volatile FLASH_WORD_SIZE *b,
  46. int on_off)
  47. {
  48. b[0x5555] = 0xaa;
  49. b[0x2aaa] = 0x55;
  50. b[0x5555] = on_off ? 0x90 : 0xf0;
  51. }
  52. /*****************************************************************************
  53. * sector erase start
  54. *****************************************************************************/
  55. static void FlashSectorErase (
  56. volatile FLASH_WORD_SIZE *b,
  57. volatile FLASH_WORD_SIZE *a)
  58. {
  59. b[0x5555] = 0xaa;
  60. b[0x2aaa] = 0x55;
  61. b[0x5555] = 0x80;
  62. b[0x5555] = 0xaa;
  63. b[0x2aaa] = 0x55;
  64. a[0] = 0x30;
  65. }
  66. /*****************************************************************************
  67. * program a word
  68. *****************************************************************************/
  69. static void FlashProgWord (
  70. volatile FLASH_WORD_SIZE *b,
  71. volatile FLASH_WORD_SIZE *a,
  72. FLASH_WORD_SIZE v)
  73. {
  74. b[0x5555] = 0xaa;
  75. b[0x2aaa] = 0x55;
  76. b[0x5555] = 0xa0;
  77. a[0] = v;
  78. }
  79. /*****************************************************************************
  80. * reset bank, back to read mode
  81. *****************************************************************************/
  82. static void FlashReset (volatile FLASH_WORD_SIZE *b)
  83. {
  84. b[0] = 0xf0;
  85. }
  86. /*****************************************************************************
  87. * identify FLASH chip
  88. * this code is a stripped version of the FlashGetType() function in EMKstax
  89. *****************************************************************************/
  90. unsigned long flash_init (void)
  91. {
  92. volatile FLASH_WORD_SIZE * const flash = (volatile FLASH_WORD_SIZE *) CFG_FLASH_BASE;
  93. FLASH_WORD_SIZE manu, dev;
  94. flash_info_t * const pflinfo = &flash_info[0];
  95. int j;
  96. /* get Id Bytes */
  97. FlashProductIdMode (flash, 1);
  98. manu = flash[0];
  99. dev = flash[1];
  100. FlashProductIdMode (flash, 0);
  101. pflinfo->size = 0;
  102. pflinfo->sector_count = 0;
  103. pflinfo->flash_id = 0xffffffff;
  104. pflinfo->portwidth = FLASH_CFI_16BIT;
  105. pflinfo->chipwidth = FLASH_CFI_BY16;
  106. switch (manu&0xff)
  107. {
  108. case 0x01: /* AMD */
  109. pflinfo->flash_id = FLASH_MAN_AMD;
  110. switch (dev&0xff)
  111. {
  112. case 0x49:
  113. pflinfo->size = 0x00200000;
  114. pflinfo->sector_count = 35;
  115. pflinfo->flash_id |= FLASH_AM160B;
  116. pflinfo->start[0] = CFG_FLASH_BASE;
  117. pflinfo->start[1] = CFG_FLASH_BASE + 0x4000;
  118. pflinfo->start[2] = CFG_FLASH_BASE + 0x6000;
  119. pflinfo->start[3] = CFG_FLASH_BASE + 0x8000;
  120. for (j = 4; j < 35; j++)
  121. {
  122. pflinfo->start[j] = CFG_FLASH_BASE + 0x00010000 * (j-3);
  123. }
  124. break;
  125. case 0xf9:
  126. pflinfo->size = 0x00400000;
  127. pflinfo->sector_count = 71;
  128. pflinfo->flash_id |= FLASH_AM320B;
  129. pflinfo->start[0] = CFG_FLASH_BASE;
  130. pflinfo->start[1] = CFG_FLASH_BASE + 0x4000;
  131. pflinfo->start[2] = CFG_FLASH_BASE + 0x6000;
  132. pflinfo->start[3] = CFG_FLASH_BASE + 0x8000;
  133. for (j = 0; j < 8; j++)
  134. {
  135. pflinfo->start[j] = CFG_FLASH_BASE + 0x00002000 * (j);
  136. }
  137. for (j = 8; j < 71; j++)
  138. {
  139. pflinfo->start[j] = CFG_FLASH_BASE + 0x00010000 * (j-7);
  140. }
  141. break;
  142. default:
  143. printf ("unknown AMD dev=%x ", dev);
  144. pflinfo->flash_id |= FLASH_UNKNOWN;
  145. }
  146. break;
  147. default:
  148. printf ("unknown manu=%x ", manu);
  149. }
  150. return pflinfo->size;
  151. }
  152. /*****************************************************************************
  153. * print info about a FLASH
  154. *****************************************************************************/
  155. void flash_print_info (flash_info_t *info)
  156. {
  157. static const char unk[] = "Unknown";
  158. unsigned int i;
  159. const char *mfct=unk,
  160. *type=unk;
  161. if(info->flash_id != FLASH_UNKNOWN)
  162. {
  163. switch (info->flash_id & FLASH_VENDMASK)
  164. {
  165. case FLASH_MAN_AMD:
  166. mfct = "AMD";
  167. break;
  168. }
  169. switch (info->flash_id & FLASH_TYPEMASK)
  170. {
  171. case FLASH_AM160B:
  172. type = "AM29LV160B (16 Mbit, bottom boot sect)";
  173. break;
  174. case FLASH_AM320B:
  175. type = "AM29LV320B (32 Mbit, bottom boot sect)";
  176. break;
  177. }
  178. }
  179. printf (
  180. "\n Brand: %s Type: %s\n"
  181. " Size: %lu KB in %d Sectors\n",
  182. mfct,
  183. type,
  184. info->size >> 10,
  185. info->sector_count
  186. );
  187. printf (" Sector Start Addresses:");
  188. for (i = 0; i < info->sector_count; i++)
  189. {
  190. unsigned long size;
  191. unsigned int erased;
  192. unsigned long *flash = (unsigned long *) info->start[i];
  193. /*
  194. * Check if whole sector is erased
  195. */
  196. size =
  197. (i != (info->sector_count - 1)) ?
  198. (info->start[i + 1] - info->start[i]) >> 2 :
  199. (info->start[0] + info->size - info->start[i]) >> 2;
  200. for (
  201. flash = (unsigned long *) info->start[i], erased = 1;
  202. (flash != (unsigned long *) info->start[i] + size) && erased;
  203. flash++
  204. )
  205. erased = *flash == ~0x0UL;
  206. printf (
  207. "%s %08lX %s %s",
  208. (i % 5) ? "" : "\n ",
  209. info->start[i],
  210. erased ? "E" : " ",
  211. info->protect[i] ? "RO" : " "
  212. );
  213. }
  214. puts ("\n");
  215. return;
  216. }
  217. /*****************************************************************************
  218. * erase one or more sectors
  219. *****************************************************************************/
  220. int flash_erase (flash_info_t *info, int s_first, int s_last)
  221. {
  222. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  223. int flag,
  224. prot,
  225. sect,
  226. l_sect;
  227. ulong start,
  228. now,
  229. last;
  230. if ((s_first < 0) || (s_first > s_last))
  231. {
  232. if (info->flash_id == FLASH_UNKNOWN)
  233. {
  234. printf ("- missing\n");
  235. }
  236. else
  237. {
  238. printf ("- no sectors to erase\n");
  239. }
  240. return 1;
  241. }
  242. if ((info->flash_id == FLASH_UNKNOWN) ||
  243. (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP)))
  244. {
  245. printf ("Can't erase unknown flash type - aborted\n");
  246. return 1;
  247. }
  248. prot = 0;
  249. for (sect=s_first; sect<=s_last; ++sect)
  250. {
  251. if (info->protect[sect])
  252. {
  253. prot++;
  254. }
  255. }
  256. if (prot)
  257. {
  258. printf ("- Warning: %d protected sectors will not be erased!\n",
  259. prot);
  260. }
  261. else
  262. {
  263. printf ("\n");
  264. }
  265. l_sect = -1;
  266. /* Disable interrupts which might cause a timeout here */
  267. flag = disable_interrupts();
  268. /* Start erase on unprotected sectors */
  269. for (sect = s_first; sect<=s_last; sect++)
  270. {
  271. if (info->protect[sect] == 0)
  272. { /* not protected */
  273. FlashSectorErase ((FLASH_WORD_SIZE *)info->start[0], (FLASH_WORD_SIZE *)info->start[sect]);
  274. l_sect = sect;
  275. }
  276. }
  277. /* re-enable interrupts if necessary */
  278. if (flag)
  279. enable_interrupts();
  280. /* wait at least 80us - let's wait 1 ms */
  281. udelay (1000);
  282. /*
  283. * We wait for the last triggered sector
  284. */
  285. if (l_sect < 0)
  286. goto DONE;
  287. start = get_timer (0);
  288. last = start;
  289. addr = (FLASH_WORD_SIZE *)info->start[l_sect];
  290. while ((addr[0] & 0x0080) != 0x0080)
  291. {
  292. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT)
  293. {
  294. printf ("Timeout\n");
  295. return 1;
  296. }
  297. /* show that we're waiting */
  298. if ((now - last) > 1000)
  299. { /* every second */
  300. serial_putc ('.');
  301. last = now;
  302. }
  303. }
  304. DONE:
  305. /* reset to read mode */
  306. FlashReset ((FLASH_WORD_SIZE *)info->start[0]);
  307. printf (" done\n");
  308. return 0;
  309. }
  310. /*****************************************************************************
  311. * Copy memory to flash, returns:
  312. * 0 - OK
  313. * 1 - write timeout
  314. * 2 - Flash not erased
  315. *****************************************************************************/
  316. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  317. {
  318. ulong cp,
  319. wp,
  320. data;
  321. int i,
  322. l,
  323. rc;
  324. wp = (addr & ~(FLASH_WORD_WIDTH-1)); /* get lower word aligned address */
  325. /*
  326. * handle unaligned start bytes, if there are...
  327. */
  328. if ((l = addr - wp) != 0)
  329. {
  330. data = 0;
  331. /* get the current before the new data into our data word */
  332. for (i=0, cp=wp; i<l; ++i, ++cp)
  333. {
  334. data = (data << 8) | (*(uchar *)cp);
  335. }
  336. /* now merge the to be programmed values */
  337. for (; i<4 && cnt>0; ++i, ++cp, --cnt)
  338. {
  339. data = (data << 8) | *src++;
  340. }
  341. /* get the current after the new data into our data word */
  342. for (; cnt==0 && i<FLASH_WORD_WIDTH; ++i, ++cp)
  343. {
  344. data = (data << 8) | (*(uchar *)cp);
  345. }
  346. /* now write the combined word */
  347. if ((rc = write_word (info, wp, data)) != 0)
  348. {
  349. return (rc);
  350. }
  351. wp += FLASH_WORD_WIDTH;
  352. }
  353. /*
  354. * handle word aligned part
  355. */
  356. while (cnt >= FLASH_WORD_WIDTH)
  357. {
  358. data = 0;
  359. for (i=0; i<FLASH_WORD_WIDTH; ++i)
  360. {
  361. data = (data << 8) | *src++;
  362. }
  363. if ((rc = write_word (info, wp, data)) != 0)
  364. {
  365. return (rc);
  366. }
  367. wp += FLASH_WORD_WIDTH;
  368. cnt -= FLASH_WORD_WIDTH;
  369. }
  370. if (cnt == 0)
  371. {
  372. return (0);
  373. }
  374. /*
  375. * handle unaligned tail bytes, if there are...
  376. */
  377. data = 0;
  378. /* now merge the to be programmed values */
  379. for (i=0, cp=wp; i<FLASH_WORD_WIDTH && cnt>0; ++i, ++cp)
  380. {
  381. data = (data << 8) | *src++;
  382. --cnt;
  383. }
  384. /* get the current after the new data into our data word */
  385. for (; i<FLASH_WORD_WIDTH; ++i, ++cp)
  386. {
  387. data = (data << 8) | (*(uchar *)cp);
  388. }
  389. /* now write the combined word */
  390. return (write_word (info, wp, data));
  391. }
  392. /*****************************************************************************
  393. * Write a word to Flash, returns:
  394. * 0 - OK
  395. * 1 - write timeout
  396. * 2 - Flash not erased
  397. *****************************************************************************/
  398. static int write_word (flash_info_t *info, ulong dest, ulong data)
  399. {
  400. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
  401. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  402. FLASH_WORD_SIZE data2 = data;
  403. ulong start;
  404. int flag;
  405. /* Check if Flash is (sufficiently) erased */
  406. if ((*dest2 & data2) != data2)
  407. {
  408. return (2);
  409. }
  410. /* Disable interrupts which might cause a timeout here */
  411. flag = disable_interrupts ();
  412. FlashProgWord (addr2, dest2, data2);
  413. /* re-enable interrupts if necessary */
  414. if (flag)
  415. enable_interrupts ();
  416. /* data polling for D7 */
  417. start = get_timer (0);
  418. while ((*dest2 & 0x0080) != (data2 & 0x0080))
  419. {
  420. if (get_timer (start) > CFG_FLASH_WRITE_TOUT)
  421. {
  422. return (1);
  423. }
  424. }
  425. return (0);
  426. }
  427. /*-----------------------------------------------------------------------
  428. */