flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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(CFG_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 + CFG_MONITOR_LEN - 1,
  93. &flash_info[0]);
  94. #endif
  95. #ifdef CFG_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. /* Write auto select command: read Manufacturer ID */
  174. addr[0x5555] = (FPW) 0x00AA00AA;
  175. addr[0x2AAA] = (FPW) 0x00550055;
  176. addr[0x5555] = (FPW) 0x00900090;
  177. value = addr[0];
  178. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  179. switch (value) {
  180. case (FPW) INTEL_MANUFACT:
  181. info->flash_id = FLASH_MAN_INTEL;
  182. break;
  183. default:
  184. info->flash_id = FLASH_UNKNOWN;
  185. info->sector_count = 0;
  186. info->size = 0;
  187. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  188. return (0); /* no or unknown flash */
  189. }
  190. value = addr[1]; /* device ID */
  191. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  192. switch (value) {
  193. case (FPW) INTEL_ID_28F320J3A:
  194. info->flash_id += FLASH_28F320J3A;
  195. info->sector_count = 32;
  196. info->size = 0x00400000;
  197. break; /* => 4 MB */
  198. case (FPW) INTEL_ID_28F640J3A:
  199. info->flash_id += FLASH_28F640J3A;
  200. info->sector_count = 64;
  201. info->size = 0x00800000;
  202. break; /* => 8 MB */
  203. case (FPW) INTEL_ID_28F128J3A:
  204. info->flash_id += FLASH_28F128J3A;
  205. info->sector_count = 128;
  206. info->size = 0x01000000;
  207. break; /* => 16 MB */
  208. default:
  209. info->flash_id = FLASH_UNKNOWN;
  210. break;
  211. }
  212. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  213. printf ("** ERROR: sector count %d > max (%d) **\n",
  214. info->sector_count, CFG_MAX_FLASH_SECT);
  215. info->sector_count = CFG_MAX_FLASH_SECT;
  216. }
  217. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  218. return (info->size);
  219. }
  220. /*-----------------------------------------------------------------------
  221. */
  222. int flash_erase (flash_info_t * info, int s_first, int s_last)
  223. {
  224. int flag, prot, sect;
  225. ulong type, start, now, last;
  226. int rcode = 0;
  227. if ((s_first < 0) || (s_first > s_last)) {
  228. if (info->flash_id == FLASH_UNKNOWN) {
  229. printf ("- missing\n");
  230. } else {
  231. printf ("- no sectors to erase\n");
  232. }
  233. return 1;
  234. }
  235. type = (info->flash_id & FLASH_VENDMASK);
  236. if ((type != FLASH_MAN_INTEL)) {
  237. printf ("Can't erase unknown flash type %08lx - aborted\n",
  238. info->flash_id);
  239. return 1;
  240. }
  241. prot = 0;
  242. for (sect = s_first; sect <= s_last; ++sect) {
  243. if (info->protect[sect]) {
  244. prot++;
  245. }
  246. }
  247. if (prot) {
  248. printf ("- Warning: %d protected sectors will not be erased!\n",
  249. prot);
  250. } else {
  251. printf ("\n");
  252. }
  253. start = get_timer (0);
  254. last = start;
  255. /* Start erase on unprotected sectors */
  256. for (sect = s_first; sect <= s_last; sect++) {
  257. if (info->protect[sect] == 0) { /* not protected */
  258. FPWV *addr = (FPWV *) (info->start[sect]);
  259. FPW status;
  260. /* Disable interrupts which might cause a timeout here */
  261. flag = disable_interrupts ();
  262. *addr = (FPW) 0x00500050; /* clear status register */
  263. *addr = (FPW) 0x00200020; /* erase setup */
  264. *addr = (FPW) 0x00D000D0; /* erase confirm */
  265. /* re-enable interrupts if necessary */
  266. if (flag)
  267. enable_interrupts ();
  268. /* wait at least 80us - let's wait 1 ms */
  269. udelay (1000);
  270. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  271. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  272. printf ("Timeout\n");
  273. *addr = (FPW) 0x00B000B0; /* suspend erase */
  274. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  275. rcode = 1;
  276. break;
  277. }
  278. /* show that we're waiting */
  279. if ((now - last) > 1000) { /* every second */
  280. putc ('.');
  281. last = now;
  282. }
  283. }
  284. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  285. }
  286. }
  287. printf (" done\n");
  288. return rcode;
  289. }
  290. /*-----------------------------------------------------------------------
  291. * Copy memory to flash, returns:
  292. * 0 - OK
  293. * 1 - write timeout
  294. * 2 - Flash not erased
  295. * 4 - Flash not identified
  296. */
  297. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  298. {
  299. ulong cp, wp;
  300. FPW data;
  301. int i, l, rc, port_width;
  302. if (info->flash_id == FLASH_UNKNOWN) {
  303. return 4;
  304. }
  305. /* get lower word aligned address */
  306. #ifdef FLASH_PORT_WIDTH16
  307. wp = (addr & ~1);
  308. port_width = 2;
  309. #else
  310. wp = (addr & ~3);
  311. port_width = 4;
  312. #endif
  313. /*
  314. * handle unaligned start bytes
  315. */
  316. if ((l = addr - wp) != 0) {
  317. data = 0;
  318. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  319. data = (data << 8) | (*(uchar *) cp);
  320. }
  321. for (; i < port_width && cnt > 0; ++i) {
  322. data = (data << 8) | *src++;
  323. --cnt;
  324. ++cp;
  325. }
  326. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  327. data = (data << 8) | (*(uchar *) cp);
  328. }
  329. if ((rc = write_data (info, wp, data)) != 0) {
  330. return (rc);
  331. }
  332. wp += port_width;
  333. }
  334. /*
  335. * handle word aligned part
  336. */
  337. while (cnt >= port_width) {
  338. data = 0;
  339. for (i = 0; i < port_width; ++i) {
  340. data = (data << 8) | *src++;
  341. }
  342. if ((rc = write_data (info, wp, data)) != 0) {
  343. return (rc);
  344. }
  345. wp += port_width;
  346. cnt -= port_width;
  347. }
  348. if (cnt == 0) {
  349. return (0);
  350. }
  351. /*
  352. * handle unaligned tail bytes
  353. */
  354. data = 0;
  355. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  356. data = (data << 8) | *src++;
  357. --cnt;
  358. }
  359. for (; i < port_width; ++i, ++cp) {
  360. data = (data << 8) | (*(uchar *) cp);
  361. }
  362. return (write_data (info, wp, data));
  363. }
  364. /*-----------------------------------------------------------------------
  365. * Write a word or halfword to Flash, returns:
  366. * 0 - OK
  367. * 1 - write timeout
  368. * 2 - Flash not erased
  369. */
  370. static int write_data (flash_info_t * info, ulong dest, FPW data)
  371. {
  372. FPWV *addr = (FPWV *) dest;
  373. ulong status;
  374. ulong start;
  375. int flag;
  376. /* Check if Flash is (sufficiently) erased */
  377. if ((*addr & data) != data) {
  378. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  379. return (2);
  380. }
  381. /* Disable interrupts which might cause a timeout here */
  382. flag = disable_interrupts ();
  383. *addr = (FPW) 0x00400040; /* write setup */
  384. *addr = data;
  385. /* re-enable interrupts if necessary */
  386. if (flag)
  387. enable_interrupts ();
  388. start = get_timer (0);
  389. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  390. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  391. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  392. return (1);
  393. }
  394. }
  395. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  396. return (0);
  397. }