flash.c 11 KB

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