flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * (C) Copyright 2005
  3. * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
  4. *
  5. * (C) Copyright 2001
  6. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  7. *
  8. * (C) Copyright 2001-2005
  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. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  32. #if defined(CONFIG_ENV_IS_IN_FLASH)
  33. # ifndef CONFIG_ENV_ADDR
  34. # define CONFIG_ENV_ADDR (CFG_FLASH_BASE + CONFIG_ENV_OFFSET)
  35. # endif
  36. # ifndef CONFIG_ENV_SIZE
  37. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  38. # endif
  39. # ifndef CONFIG_ENV_SECT_SIZE
  40. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  41. # endif
  42. #endif
  43. /*-----------------------------------------------------------------------
  44. * Protection Flags:
  45. */
  46. #define FLAG_PROTECT_SET 0x01
  47. #define FLAG_PROTECT_CLEAR 0x02
  48. /* Board support for 1 or 2 flash devices */
  49. #undef FLASH_PORT_WIDTH32
  50. #undef FLASH_PORT_WIDTH16
  51. #define FLASH_PORT_WIDTH8
  52. #ifdef FLASH_PORT_WIDTH16
  53. #define FLASH_PORT_WIDTH ushort
  54. #define FLASH_PORT_WIDTHV vu_short
  55. #elif FLASH_PORT_WIDTH32
  56. #define FLASH_PORT_WIDTH ulong
  57. #define FLASH_PORT_WIDTHV vu_long
  58. #else /* FLASH_PORT_WIDTH8 */
  59. #define FLASH_PORT_WIDTH uchar
  60. #define FLASH_PORT_WIDTHV vu_char
  61. #endif
  62. #define FPW FLASH_PORT_WIDTH
  63. #define FPWV FLASH_PORT_WIDTHV
  64. /*-----------------------------------------------------------------------
  65. * Functions
  66. */
  67. static ulong flash_get_size (FPWV * addr, flash_info_t * info);
  68. static int write_data (flash_info_t * info, ulong dest, FPW data);
  69. static void flash_get_offsets (ulong base, flash_info_t * info);
  70. /*-----------------------------------------------------------------------
  71. */
  72. unsigned long flash_init (void)
  73. {
  74. unsigned long size_b0;
  75. int i;
  76. volatile immap_t * immr = (immap_t *)CFG_IMMR;
  77. volatile memctl8260_t *memctl = &immr->im_memctl;
  78. /* Init: no FLASHes known */
  79. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  80. flash_info[i].flash_id = FLASH_UNKNOWN;
  81. }
  82. /* Static FLASH Bank configuration here - FIXME XXX */
  83. size_b0 = flash_get_size ((FPW *) CFG_FLASH0_BASE, &flash_info[0]);
  84. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  85. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  86. size_b0, size_b0 << 20);
  87. }
  88. memctl->memc_or0 = 0xff800060;
  89. memctl->memc_br0 = 0xff800801;
  90. flash_get_offsets (0xff800000, &flash_info[0]);
  91. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  92. /* monitor protection ON by default */
  93. (void) flash_protect (FLAG_PROTECT_SET,
  94. CFG_MONITOR_BASE,
  95. CFG_MONITOR_BASE + monitor_flash_len - 1,
  96. &flash_info[0]);
  97. #endif
  98. #ifdef CONFIG_ENV_IS_IN_FLASH
  99. /* ENV protection ON by default */
  100. flash_protect (FLAG_PROTECT_SET,
  101. CONFIG_ENV_ADDR,
  102. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  103. &flash_info[0]);
  104. #endif
  105. flash_info[0].size = size_b0;
  106. return (size_b0);
  107. }
  108. /*-----------------------------------------------------------------------
  109. */
  110. static void flash_get_offsets (ulong base, flash_info_t * info)
  111. {
  112. int i;
  113. if (info->flash_id == FLASH_UNKNOWN) {
  114. return;
  115. }
  116. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  117. for (i = 0; i < info->sector_count; i++) {
  118. info->start[i] = base + (i * 0x00020000);
  119. }
  120. }
  121. }
  122. /*-----------------------------------------------------------------------
  123. */
  124. void flash_print_info (flash_info_t * info)
  125. {
  126. int i;
  127. if (info->flash_id == FLASH_UNKNOWN) {
  128. printf ("missing or unknown FLASH type\n");
  129. return;
  130. }
  131. switch (info->flash_id & FLASH_VENDMASK) {
  132. case FLASH_MAN_INTEL:
  133. printf ("INTEL ");
  134. break;
  135. default:
  136. printf ("Unknown Vendor ");
  137. break;
  138. }
  139. switch (info->flash_id & FLASH_TYPEMASK) {
  140. case FLASH_28F320J3A:
  141. printf ("28F320J3A\n");
  142. break;
  143. case FLASH_28F640J3A:
  144. printf ("28F640J3A\n");
  145. break;
  146. case FLASH_28F128J3A:
  147. printf ("28F128J3A\n");
  148. break;
  149. default:
  150. printf ("Unknown Chip Type\n");
  151. break;
  152. }
  153. printf (" Size: %ld MB in %d Sectors\n",
  154. info->size >> 20, info->sector_count);
  155. printf (" Sector Start Addresses:");
  156. for (i = 0; i < info->sector_count; ++i) {
  157. if ((i % 5) == 0)
  158. printf ("\n ");
  159. printf (" %08lX%s",
  160. info->start[i],
  161. info->protect[i] ? " (RO)" : " ");
  162. }
  163. printf ("\n");
  164. return;
  165. }
  166. /*-----------------------------------------------------------------------
  167. */
  168. /*-----------------------------------------------------------------------
  169. */
  170. /*
  171. * The following code cannot be run from FLASH!
  172. */
  173. static ulong flash_get_size (FPWV * addr, flash_info_t * info)
  174. {
  175. FPW value;
  176. addr[0] = (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. #ifdef FLASH_PORT_WIDTH8
  191. value = addr[2]; /* device ID */
  192. #else
  193. value = addr[1]; /* device ID */
  194. #endif
  195. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  196. switch (value) {
  197. case (FPW) INTEL_ID_28F320J3A:
  198. info->flash_id += FLASH_28F320J3A;
  199. info->sector_count = 32;
  200. info->size = 0x00400000;
  201. break; /* => 4 MB */
  202. case (FPW) INTEL_ID_28F640J3A:
  203. info->flash_id += FLASH_28F640J3A;
  204. info->sector_count = 64;
  205. info->size = 0x00800000;
  206. break; /* => 8 MB */
  207. case (FPW) INTEL_ID_28F128J3A:
  208. info->flash_id += FLASH_28F128J3A;
  209. info->sector_count = 128;
  210. info->size = 0x01000000;
  211. break; /* => 16 MB */
  212. default:
  213. info->flash_id = FLASH_UNKNOWN;
  214. break;
  215. }
  216. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  217. printf ("** ERROR: sector count %d > max (%d) **\n",
  218. info->sector_count, CFG_MAX_FLASH_SECT);
  219. info->sector_count = CFG_MAX_FLASH_SECT;
  220. }
  221. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  222. return (info->size);
  223. }
  224. /*-----------------------------------------------------------------------
  225. */
  226. int flash_erase (flash_info_t * info, int s_first, int s_last)
  227. {
  228. int flag, prot, sect;
  229. ulong type, start, now, last;
  230. int rcode = 0;
  231. if ((s_first < 0) || (s_first > s_last)) {
  232. if (info->flash_id == FLASH_UNKNOWN) {
  233. printf ("- missing\n");
  234. } else {
  235. printf ("- no sectors to erase\n");
  236. }
  237. return 1;
  238. }
  239. type = (info->flash_id & FLASH_VENDMASK);
  240. if ((type != FLASH_MAN_INTEL)) {
  241. printf ("Can't erase unknown flash type %08lx - aborted\n",
  242. info->flash_id);
  243. return 1;
  244. }
  245. prot = 0;
  246. for (sect = s_first; sect <= s_last; ++sect) {
  247. if (info->protect[sect]) {
  248. prot++;
  249. }
  250. }
  251. if (prot) {
  252. printf ("- Warning: %d protected sectors will not be erased!\n",
  253. prot);
  254. } else {
  255. printf ("\n");
  256. }
  257. start = get_timer (0);
  258. last = start;
  259. /* Start erase on unprotected sectors */
  260. for (sect = s_first; sect <= s_last; sect++) {
  261. if (info->protect[sect] == 0) { /* not protected */
  262. FPWV *addr = (FPWV *) (info->start[sect]);
  263. FPW status;
  264. /* Disable interrupts which might cause a timeout here */
  265. flag = disable_interrupts ();
  266. *addr = (FPW) 0x00500050; /* clear status register */
  267. *addr = (FPW) 0x00200020; /* erase setup */
  268. *addr = (FPW) 0x00D000D0; /* erase confirm */
  269. /* re-enable interrupts if necessary */
  270. if (flag)
  271. enable_interrupts ();
  272. /* wait at least 80us - let's wait 1 ms */
  273. udelay (1000);
  274. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  275. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  276. printf ("Timeout\n");
  277. *addr = (FPW) 0x00B000B0; /* suspend erase */
  278. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  279. rcode = 1;
  280. break;
  281. }
  282. /* show that we're waiting */
  283. if ((now - last) > 1000) { /* every second */
  284. putc ('.');
  285. last = now;
  286. }
  287. }
  288. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  289. }
  290. }
  291. printf (" done\n");
  292. return rcode;
  293. }
  294. /*-----------------------------------------------------------------------
  295. * Copy memory to flash, returns:
  296. * 0 - OK
  297. * 1 - write timeout
  298. * 2 - Flash not erased
  299. * 4 - Flash not identified
  300. */
  301. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  302. {
  303. ulong cp, wp;
  304. FPW data;
  305. int i, l, rc, port_width;
  306. if (info->flash_id == FLASH_UNKNOWN) {
  307. return 4;
  308. }
  309. /* get lower word aligned address */
  310. #ifdef FLASH_PORT_WIDTH16
  311. wp = (addr & ~1);
  312. port_width = 2;
  313. #elif defined(FLASH_PORT_WIDTH32)
  314. wp = (addr & ~3);
  315. port_width = 4;
  316. #else
  317. wp = addr;
  318. port_width = 1;
  319. #endif
  320. /*
  321. * handle unaligned start bytes
  322. */
  323. if ((l = addr - wp) != 0) {
  324. data = 0;
  325. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  326. data = (data << 8) | (*(uchar *) cp);
  327. }
  328. for (; i < port_width && cnt > 0; ++i) {
  329. data = (data << 8) | *src++;
  330. --cnt;
  331. ++cp;
  332. }
  333. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  334. data = (data << 8) | (*(uchar *) cp);
  335. }
  336. if ((rc = write_data (info, wp, data)) != 0) {
  337. return (rc);
  338. }
  339. wp += port_width;
  340. }
  341. /*
  342. * handle word aligned part
  343. */
  344. while (cnt >= port_width) {
  345. data = 0;
  346. for (i = 0; i < port_width; ++i) {
  347. data = (data << 8) | *src++;
  348. }
  349. if ((rc = write_data (info, wp, data)) != 0) {
  350. return (rc);
  351. }
  352. wp += port_width;
  353. cnt -= port_width;
  354. }
  355. if (cnt == 0) {
  356. return (0);
  357. }
  358. /*
  359. * handle unaligned tail bytes
  360. */
  361. data = 0;
  362. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  363. data = (data << 8) | *src++;
  364. --cnt;
  365. }
  366. for (; i < port_width; ++i, ++cp) {
  367. data = (data << 8) | (*(uchar *) cp);
  368. }
  369. return (write_data (info, wp, data));
  370. }
  371. /*-----------------------------------------------------------------------
  372. * Write a word or halfword to Flash, returns:
  373. * 0 - OK
  374. * 1 - write timeout
  375. * 2 - Flash not erased
  376. */
  377. static int write_data (flash_info_t * info, ulong dest, FPW data)
  378. {
  379. FPWV *addr = (FPWV *) dest;
  380. ulong status;
  381. ulong start;
  382. int flag;
  383. /* Check if Flash is (sufficiently) erased */
  384. if ((*addr & data) != data) {
  385. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  386. return (2);
  387. }
  388. /* Disable interrupts which might cause a timeout here */
  389. flag = disable_interrupts ();
  390. *addr = (FPW) 0x00400040; /* write setup */
  391. *addr = data;
  392. /* re-enable interrupts if necessary */
  393. if (flag)
  394. enable_interrupts ();
  395. start = get_timer (0);
  396. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  397. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  398. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  399. return (1);
  400. }
  401. }
  402. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  403. return (0);
  404. }