flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001
  6. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  7. *
  8. * (C) Copyright 2001
  9. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #undef DEBUG
  30. #include <common.h>
  31. #include <mpc8xx.h>
  32. #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
  33. #define CFG_OR_TIMING_FLASH_AT_50MHZ (OR_ACS_DIV1 | OR_TRLX | OR_CSNT_SAM | \
  34. OR_SCY_2_CLK | OR_EHTR | OR_BI)
  35. #endif
  36. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  37. #if defined(CFG_ENV_IS_IN_FLASH)
  38. # ifndef CFG_ENV_ADDR
  39. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  40. # endif
  41. # ifndef CFG_ENV_SIZE
  42. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  43. # endif
  44. # ifndef CFG_ENV_SECT_SIZE
  45. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  46. # endif
  47. #endif
  48. /*-----------------------------------------------------------------------
  49. * Protection Flags:
  50. */
  51. #define FLAG_PROTECT_SET 0x01
  52. #define FLAG_PROTECT_CLEAR 0x02
  53. /* Board support for 1 or 2 flash devices */
  54. #undef FLASH_PORT_WIDTH32
  55. #undef FLASH_PORT_WIDTH16
  56. #define FLASH_PORT_WIDTH8
  57. #ifdef FLASH_PORT_WIDTH16
  58. #define FLASH_PORT_WIDTH ushort
  59. #define FLASH_PORT_WIDTHV vu_short
  60. #elif FLASH_PORT_WIDTH32
  61. #define FLASH_PORT_WIDTH ulong
  62. #define FLASH_PORT_WIDTHV vu_long
  63. #else /* FLASH_PORT_WIDTH8 */
  64. #define FLASH_PORT_WIDTH uchar
  65. #define FLASH_PORT_WIDTHV vu_char
  66. #endif
  67. #define FPW FLASH_PORT_WIDTH
  68. #define FPWV FLASH_PORT_WIDTHV
  69. /*-----------------------------------------------------------------------
  70. * Functions
  71. */
  72. static ulong flash_get_size (FPWV * addr, flash_info_t * info);
  73. static int write_data (flash_info_t * info, ulong dest, FPW data);
  74. static void flash_get_offsets (ulong base, flash_info_t * info);
  75. /*-----------------------------------------------------------------------
  76. */
  77. unsigned long flash_init (void)
  78. {
  79. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  80. volatile memctl8xx_t *memctl = &immap->im_memctl;
  81. unsigned long size_b0;
  82. int i;
  83. #ifdef CFG_OR_TIMING_FLASH_AT_50MHZ
  84. int scy, trlx, flash_or_timing, clk_diff;
  85. DECLARE_GLOBAL_DATA_PTR;
  86. scy = (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_SCY_MSK) >> 4;
  87. if (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_TRLX) {
  88. trlx = OR_TRLX;
  89. scy *= 2;
  90. } else
  91. trlx = 0;
  92. /* We assume that each 10MHz of bus clock require 1-clk SCY
  93. * adjustment.
  94. */
  95. clk_diff = (gd->bus_clk / 1000000) - 50;
  96. /* We need proper rounding here. This is what the "+5" and "-5"
  97. * are here for.
  98. */
  99. if (clk_diff >= 0)
  100. scy += (clk_diff + 5) / 10;
  101. else
  102. scy += (clk_diff - 5) / 10;
  103. /* For bus frequencies above 50MHz, we want to use relaxed
  104. * timing (OR_TRLX).
  105. */
  106. if (gd->bus_clk >= 50000000)
  107. trlx = OR_TRLX;
  108. else
  109. trlx = 0;
  110. if (trlx)
  111. scy /= 2;
  112. if (scy > 0xf)
  113. scy = 0xf;
  114. if (scy < 1)
  115. scy = 1;
  116. flash_or_timing = (scy << 4) | trlx |
  117. (CFG_OR_TIMING_FLASH_AT_50MHZ & ~(OR_TRLX | OR_SCY_MSK));
  118. #endif
  119. /* Init: no FLASHes known */
  120. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  121. flash_info[i].flash_id = FLASH_UNKNOWN;
  122. }
  123. /* Static FLASH Bank configuration here - FIXME XXX */
  124. size_b0 = flash_get_size ((FPW *) FLASH_BASE0_PRELIM, &flash_info[0]);
  125. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  126. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  127. size_b0, size_b0 << 20);
  128. }
  129. /* Remap FLASH according to real size */
  130. #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
  131. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
  132. #else
  133. memctl->memc_or0 = flash_or_timing | (-size_b0 & OR_AM_MSK);
  134. #endif
  135. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_8 | BR_MS_GPCM | BR_V;
  136. /* Re-do sizing to get full correct info */
  137. size_b0 = flash_get_size ((FPW *) CFG_FLASH_BASE, &flash_info[0]);
  138. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  139. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  140. /* monitor protection ON by default */
  141. (void) flash_protect (FLAG_PROTECT_SET,
  142. CFG_MONITOR_BASE,
  143. CFG_MONITOR_BASE + monitor_flash_len - 1,
  144. &flash_info[0]);
  145. #endif
  146. #ifdef CFG_ENV_IS_IN_FLASH
  147. /* ENV protection ON by default */
  148. flash_protect (FLAG_PROTECT_SET,
  149. CFG_ENV_ADDR,
  150. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  151. &flash_info[0]);
  152. #endif
  153. flash_info[0].size = size_b0;
  154. return (size_b0);
  155. }
  156. /*-----------------------------------------------------------------------
  157. */
  158. static void flash_get_offsets (ulong base, flash_info_t * info)
  159. {
  160. int i;
  161. if (info->flash_id == FLASH_UNKNOWN) {
  162. return;
  163. }
  164. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  165. for (i = 0; i < info->sector_count; i++) {
  166. info->start[i] = base + (i * 0x00020000);
  167. }
  168. }
  169. }
  170. /*-----------------------------------------------------------------------
  171. */
  172. void flash_print_info (flash_info_t * info)
  173. {
  174. int i;
  175. if (info->flash_id == FLASH_UNKNOWN) {
  176. printf ("missing or unknown FLASH type\n");
  177. return;
  178. }
  179. switch (info->flash_id & FLASH_VENDMASK) {
  180. case FLASH_MAN_INTEL:
  181. printf ("INTEL ");
  182. break;
  183. default:
  184. printf ("Unknown Vendor ");
  185. break;
  186. }
  187. switch (info->flash_id & FLASH_TYPEMASK) {
  188. case FLASH_28F320J3A:
  189. printf ("28F320J3A\n");
  190. break;
  191. case FLASH_28F640J3A:
  192. printf ("28F640J3A\n");
  193. break;
  194. case FLASH_28F128J3A:
  195. printf ("28F128J3A\n");
  196. break;
  197. default:
  198. printf ("Unknown Chip Type\n");
  199. break;
  200. }
  201. printf (" Size: %ld MB in %d Sectors\n",
  202. info->size >> 20, info->sector_count);
  203. printf (" Sector Start Addresses:");
  204. for (i = 0; i < info->sector_count; ++i) {
  205. if ((i % 5) == 0)
  206. printf ("\n ");
  207. printf (" %08lX%s",
  208. info->start[i],
  209. info->protect[i] ? " (RO)" : " ");
  210. }
  211. printf ("\n");
  212. return;
  213. }
  214. /*-----------------------------------------------------------------------
  215. */
  216. /*-----------------------------------------------------------------------
  217. */
  218. /*
  219. * The following code cannot be run from FLASH!
  220. */
  221. static ulong flash_get_size (FPWV * addr, flash_info_t * info)
  222. {
  223. FPW value;
  224. addr[0] = (FPW) 0x00900090;
  225. value = addr[0];
  226. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  227. switch (value) {
  228. case (FPW) INTEL_MANUFACT:
  229. info->flash_id = FLASH_MAN_INTEL;
  230. break;
  231. default:
  232. info->flash_id = FLASH_UNKNOWN;
  233. info->sector_count = 0;
  234. info->size = 0;
  235. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  236. return (0); /* no or unknown flash */
  237. }
  238. #ifdef FLASH_PORT_WIDTH8
  239. value = addr[2]; /* device ID */
  240. #else
  241. value = addr[1]; /* device ID */
  242. #endif
  243. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  244. switch (value) {
  245. case (FPW) INTEL_ID_28F320J3A:
  246. info->flash_id += FLASH_28F320J3A;
  247. info->sector_count = 32;
  248. info->size = 0x00400000;
  249. break; /* => 4 MB */
  250. case (FPW) INTEL_ID_28F640J3A:
  251. info->flash_id += FLASH_28F640J3A;
  252. info->sector_count = 64;
  253. info->size = 0x00800000;
  254. break; /* => 8 MB */
  255. case (FPW) INTEL_ID_28F128J3A:
  256. info->flash_id += FLASH_28F128J3A;
  257. info->sector_count = 128;
  258. info->size = 0x01000000;
  259. break; /* => 16 MB */
  260. default:
  261. info->flash_id = FLASH_UNKNOWN;
  262. break;
  263. }
  264. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  265. printf ("** ERROR: sector count %d > max (%d) **\n",
  266. info->sector_count, CFG_MAX_FLASH_SECT);
  267. info->sector_count = CFG_MAX_FLASH_SECT;
  268. }
  269. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  270. return (info->size);
  271. }
  272. /*-----------------------------------------------------------------------
  273. */
  274. int flash_erase (flash_info_t * info, int s_first, int s_last)
  275. {
  276. int flag, prot, sect;
  277. ulong type, start, now, last;
  278. int rcode = 0;
  279. if ((s_first < 0) || (s_first > s_last)) {
  280. if (info->flash_id == FLASH_UNKNOWN) {
  281. printf ("- missing\n");
  282. } else {
  283. printf ("- no sectors to erase\n");
  284. }
  285. return 1;
  286. }
  287. type = (info->flash_id & FLASH_VENDMASK);
  288. if ((type != FLASH_MAN_INTEL)) {
  289. printf ("Can't erase unknown flash type %08lx - aborted\n",
  290. info->flash_id);
  291. return 1;
  292. }
  293. prot = 0;
  294. for (sect = s_first; sect <= s_last; ++sect) {
  295. if (info->protect[sect]) {
  296. prot++;
  297. }
  298. }
  299. if (prot) {
  300. printf ("- Warning: %d protected sectors will not be erased!\n",
  301. prot);
  302. } else {
  303. printf ("\n");
  304. }
  305. start = get_timer (0);
  306. last = start;
  307. /* Start erase on unprotected sectors */
  308. for (sect = s_first; sect <= s_last; sect++) {
  309. if (info->protect[sect] == 0) { /* not protected */
  310. FPWV *addr = (FPWV *) (info->start[sect]);
  311. FPW status;
  312. /* Disable interrupts which might cause a timeout here */
  313. flag = disable_interrupts ();
  314. *addr = (FPW) 0x00500050; /* clear status register */
  315. *addr = (FPW) 0x00200020; /* erase setup */
  316. *addr = (FPW) 0x00D000D0; /* erase confirm */
  317. /* re-enable interrupts if necessary */
  318. if (flag)
  319. enable_interrupts ();
  320. /* wait at least 80us - let's wait 1 ms */
  321. udelay (1000);
  322. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  323. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  324. printf ("Timeout\n");
  325. *addr = (FPW) 0x00B000B0; /* suspend erase */
  326. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  327. rcode = 1;
  328. break;
  329. }
  330. /* show that we're waiting */
  331. if ((now - last) > 1000) { /* every second */
  332. putc ('.');
  333. last = now;
  334. }
  335. }
  336. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  337. }
  338. }
  339. printf (" done\n");
  340. return rcode;
  341. }
  342. /*-----------------------------------------------------------------------
  343. * Copy memory to flash, returns:
  344. * 0 - OK
  345. * 1 - write timeout
  346. * 2 - Flash not erased
  347. * 4 - Flash not identified
  348. */
  349. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  350. {
  351. ulong cp, wp;
  352. FPW data;
  353. int i, l, rc, port_width;
  354. if (info->flash_id == FLASH_UNKNOWN) {
  355. return 4;
  356. }
  357. /* get lower word aligned address */
  358. #ifdef FLASH_PORT_WIDTH16
  359. wp = (addr & ~1);
  360. port_width = 2;
  361. #elif defined(FLASH_PORT_WIDTH32)
  362. wp = (addr & ~3);
  363. port_width = 4;
  364. #else
  365. wp = addr;
  366. port_width = 1;
  367. #endif
  368. /*
  369. * handle unaligned start bytes
  370. */
  371. if ((l = addr - wp) != 0) {
  372. data = 0;
  373. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  374. data = (data << 8) | (*(uchar *) cp);
  375. }
  376. for (; i < port_width && cnt > 0; ++i) {
  377. data = (data << 8) | *src++;
  378. --cnt;
  379. ++cp;
  380. }
  381. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  382. data = (data << 8) | (*(uchar *) cp);
  383. }
  384. if ((rc = write_data (info, wp, data)) != 0) {
  385. return (rc);
  386. }
  387. wp += port_width;
  388. }
  389. /*
  390. * handle word aligned part
  391. */
  392. while (cnt >= port_width) {
  393. data = 0;
  394. for (i = 0; i < port_width; ++i) {
  395. data = (data << 8) | *src++;
  396. }
  397. if ((rc = write_data (info, wp, data)) != 0) {
  398. return (rc);
  399. }
  400. wp += port_width;
  401. cnt -= port_width;
  402. }
  403. if (cnt == 0) {
  404. return (0);
  405. }
  406. /*
  407. * handle unaligned tail bytes
  408. */
  409. data = 0;
  410. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  411. data = (data << 8) | *src++;
  412. --cnt;
  413. }
  414. for (; i < port_width; ++i, ++cp) {
  415. data = (data << 8) | (*(uchar *) cp);
  416. }
  417. return (write_data (info, wp, data));
  418. }
  419. /*-----------------------------------------------------------------------
  420. * Write a word or halfword to Flash, returns:
  421. * 0 - OK
  422. * 1 - write timeout
  423. * 2 - Flash not erased
  424. */
  425. static int write_data (flash_info_t * info, ulong dest, FPW data)
  426. {
  427. FPWV *addr = (FPWV *) dest;
  428. ulong status;
  429. ulong start;
  430. int flag;
  431. /* Check if Flash is (sufficiently) erased */
  432. if ((*addr & data) != data) {
  433. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  434. return (2);
  435. }
  436. /* Disable interrupts which might cause a timeout here */
  437. flag = disable_interrupts ();
  438. *addr = (FPW) 0x00400040; /* write setup */
  439. *addr = data;
  440. /* re-enable interrupts if necessary */
  441. if (flag)
  442. enable_interrupts ();
  443. start = get_timer (0);
  444. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  445. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  446. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  447. return (1);
  448. }
  449. }
  450. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  451. return (0);
  452. }