flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc8xx.h>
  25. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  30. static int write_byte (flash_info_t *info, ulong dest, uchar data);
  31. static void flash_get_offsets (ulong base, flash_info_t *info);
  32. /*-----------------------------------------------------------------------
  33. */
  34. unsigned long flash_init (void)
  35. {
  36. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  37. volatile memctl8xx_t *memctl = &immap->im_memctl;
  38. unsigned long size;
  39. int i;
  40. /* Init: no FLASHes known */
  41. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  42. flash_info[i].flash_id = FLASH_UNKNOWN;
  43. }
  44. /* Static FLASH Bank configuration here - FIXME XXX */
  45. size = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  46. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  47. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  48. size, size<<20);
  49. }
  50. /* Remap FLASH according to real size */
  51. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
  52. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) |
  53. (memctl->memc_br0 & ~(BR_BA_MSK));
  54. /* Re-do sizing to get full correct info */
  55. size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  56. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  57. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  58. /* monitor protection ON by default */
  59. flash_protect(FLAG_PROTECT_SET,
  60. CFG_MONITOR_BASE,
  61. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  62. &flash_info[0]);
  63. #endif
  64. flash_info[0].size = size;
  65. return (size);
  66. }
  67. /*-----------------------------------------------------------------------
  68. */
  69. static void flash_get_offsets (ulong base, flash_info_t *info)
  70. {
  71. int i;
  72. /* set up sector start address table */
  73. if (info->flash_id & FLASH_BTYPE) {
  74. /* set sector offsets for bottom boot block type */
  75. info->start[0] = base + 0x00000000;
  76. info->start[1] = base + 0x00004000;
  77. info->start[2] = base + 0x00006000;
  78. info->start[3] = base + 0x00008000;
  79. for (i = 4; i < info->sector_count; i++) {
  80. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  81. }
  82. } else {
  83. /* set sector offsets for top boot block type */
  84. i = info->sector_count - 1;
  85. info->start[i--] = base + info->size - 0x00004000;
  86. info->start[i--] = base + info->size - 0x00006000;
  87. info->start[i--] = base + info->size - 0x00008000;
  88. for (; i >= 0; i--) {
  89. info->start[i] = base + i * 0x00010000;
  90. }
  91. }
  92. }
  93. /*-----------------------------------------------------------------------
  94. */
  95. void flash_print_info (flash_info_t *info)
  96. {
  97. int i;
  98. if (info->flash_id == FLASH_UNKNOWN) {
  99. printf ("missing or unknown FLASH type\n");
  100. return;
  101. }
  102. switch (info->flash_id & FLASH_VENDMASK) {
  103. case FLASH_MAN_AMD: printf ("AMD "); break;
  104. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  105. default: printf ("Unknown Vendor "); break;
  106. }
  107. switch (info->flash_id & FLASH_TYPEMASK) {
  108. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  109. break;
  110. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  111. break;
  112. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  113. break;
  114. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  115. break;
  116. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  117. break;
  118. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  119. break;
  120. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  121. break;
  122. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  123. break;
  124. default: printf ("Unknown Chip Type\n");
  125. break;
  126. }
  127. printf (" Size: %ld MB in %d Sectors\n",
  128. info->size >> 20, info->sector_count);
  129. printf (" Sector Start Addresses:");
  130. for (i=0; i<info->sector_count; ++i) {
  131. if ((i % 5) == 0)
  132. printf ("\n ");
  133. printf (" %08lX%s",
  134. info->start[i],
  135. info->protect[i] ? " (RO)" : " "
  136. );
  137. }
  138. printf ("\n");
  139. return;
  140. }
  141. /*-----------------------------------------------------------------------
  142. */
  143. /*-----------------------------------------------------------------------
  144. */
  145. /*
  146. * The following code cannot be run from FLASH!
  147. */
  148. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  149. {
  150. short i;
  151. uchar value;
  152. vu_char *caddr = (vu_char *)addr;
  153. ulong base = (ulong)addr;
  154. /* Write auto select command: read Manufacturer ID */
  155. caddr[0x0AAA] = 0xAA;
  156. caddr[0x0555] = 0x55;
  157. caddr[0x0AAA] = 0x90;
  158. value = caddr[0];
  159. switch (value) {
  160. case (AMD_MANUFACT & 0xFF):
  161. info->flash_id = FLASH_MAN_AMD;
  162. break;
  163. case (FUJ_MANUFACT & 0xFF):
  164. info->flash_id = FLASH_MAN_FUJ;
  165. break;
  166. default:
  167. info->flash_id = FLASH_UNKNOWN;
  168. info->sector_count = 0;
  169. info->size = 0;
  170. return (0); /* no or unknown flash */
  171. }
  172. value = caddr[2]; /* device ID */
  173. switch (value) {
  174. case (AMD_ID_LV400T & 0xFF):
  175. info->flash_id += FLASH_AM400T;
  176. info->sector_count = 11;
  177. info->size = 0x00080000;
  178. break; /* => 512 kB */
  179. case (AMD_ID_LV400B & 0xFF):
  180. info->flash_id += FLASH_AM400B;
  181. info->sector_count = 11;
  182. info->size = 0x00080000;
  183. break; /* => 512 kB */
  184. case (AMD_ID_LV800T & 0xFF):
  185. info->flash_id += FLASH_AM800T;
  186. info->sector_count = 19;
  187. info->size = 0x00100000;
  188. break; /* => 1 MB */
  189. case (AMD_ID_LV800B & 0xFF):
  190. info->flash_id += FLASH_AM800B;
  191. info->sector_count = 19;
  192. info->size = 0x00100000;
  193. break; /* => 1 MB */
  194. case (AMD_ID_LV160T & 0xFF):
  195. info->flash_id += FLASH_AM160T;
  196. info->sector_count = 35;
  197. info->size = 0x00200000;
  198. break; /* => 2 MB */
  199. case (AMD_ID_LV160B & 0xFF):
  200. info->flash_id += FLASH_AM160B;
  201. info->sector_count = 35;
  202. info->size = 0x00200000;
  203. break; /* => 2 MB */
  204. #if 0 /* enable when device IDs are available */
  205. case (AMD_ID_LV320T & 0xFF):
  206. info->flash_id += FLASH_AM320T;
  207. info->sector_count = 67;
  208. info->size = 0x00400000;
  209. break; /* => 4 MB */
  210. case (AMD_ID_LV320B & 0xFF):
  211. info->flash_id += FLASH_AM320B;
  212. info->sector_count = 67;
  213. info->size = 0x00400000;
  214. break; /* => 4 MB */
  215. #endif
  216. default:
  217. info->flash_id = FLASH_UNKNOWN;
  218. return (0); /* => no or unknown flash */
  219. }
  220. /* set up sector start address table */
  221. if (info->flash_id & FLASH_BTYPE) {
  222. /* set sector offsets for bottom boot block type */
  223. info->start[0] = base + 0x00000000;
  224. info->start[1] = base + 0x00004000;
  225. info->start[2] = base + 0x00006000;
  226. info->start[3] = base + 0x00008000;
  227. for (i = 4; i < info->sector_count; i++) {
  228. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  229. }
  230. } else {
  231. /* set sector offsets for top boot block type */
  232. i = info->sector_count - 1;
  233. info->start[i--] = base + info->size - 0x00004000;
  234. info->start[i--] = base + info->size - 0x00006000;
  235. info->start[i--] = base + info->size - 0x00008000;
  236. for (; i >= 0; i--) {
  237. info->start[i] = base + i * 0x00010000;
  238. }
  239. }
  240. /* check for protected sectors */
  241. for (i = 0; i < info->sector_count; i++) {
  242. /* read sector protection: D0 = 1 if protected */
  243. caddr = (volatile unsigned char *)(info->start[i]);
  244. info->protect[i] = caddr[4] & 1;
  245. }
  246. /*
  247. * Prevent writes to uninitialized FLASH.
  248. */
  249. if (info->flash_id != FLASH_UNKNOWN) {
  250. caddr = (vu_char *)info->start[0];
  251. *caddr = 0xF0; /* reset bank */
  252. }
  253. return (info->size);
  254. }
  255. /*-----------------------------------------------------------------------
  256. */
  257. int flash_erase (flash_info_t *info, int s_first, int s_last)
  258. {
  259. vu_char *addr = (vu_char*)(info->start[0]);
  260. int flag, prot, sect, l_sect;
  261. ulong start, now, last;
  262. if ((s_first < 0) || (s_first > s_last)) {
  263. if (info->flash_id == FLASH_UNKNOWN) {
  264. printf ("- missing\n");
  265. } else {
  266. printf ("- no sectors to erase\n");
  267. }
  268. return 1;
  269. }
  270. if ((info->flash_id == FLASH_UNKNOWN) ||
  271. (info->flash_id > FLASH_AMD_COMP)) {
  272. printf ("Can't erase unknown flash type %08lx - aborted\n",
  273. info->flash_id);
  274. return 1;
  275. }
  276. prot = 0;
  277. for (sect=s_first; sect<=s_last; ++sect) {
  278. if (info->protect[sect]) {
  279. prot++;
  280. }
  281. }
  282. if (prot) {
  283. printf ("- Warning: %d protected sectors will not be erased!\n",
  284. prot);
  285. } else {
  286. printf ("\n");
  287. }
  288. l_sect = -1;
  289. /* Disable interrupts which might cause a timeout here */
  290. flag = disable_interrupts();
  291. addr[0x0AAA] = 0xAA;
  292. addr[0x0555] = 0x55;
  293. addr[0x0AAA] = 0x80;
  294. addr[0x0AAA] = 0xAA;
  295. addr[0x0555] = 0x55;
  296. /* Start erase on unprotected sectors */
  297. for (sect = s_first; sect<=s_last; sect++) {
  298. if (info->protect[sect] == 0) { /* not protected */
  299. addr = (vu_char*)(info->start[sect]);
  300. addr[0] = 0x30;
  301. l_sect = sect;
  302. }
  303. }
  304. /* re-enable interrupts if necessary */
  305. if (flag)
  306. enable_interrupts();
  307. /* wait at least 80us - let's wait 1 ms */
  308. udelay (1000);
  309. /*
  310. * We wait for the last triggered sector
  311. */
  312. if (l_sect < 0)
  313. goto DONE;
  314. start = get_timer (0);
  315. last = start;
  316. addr = (vu_char*)(info->start[l_sect]);
  317. while ((addr[0] & 0x80) != 0x80) {
  318. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  319. printf ("Timeout\n");
  320. return 1;
  321. }
  322. /* show that we're waiting */
  323. if ((now - last) > 1000) { /* every second */
  324. putc ('.');
  325. last = now;
  326. }
  327. }
  328. DONE:
  329. /* reset to read mode */
  330. addr = (vu_char *)info->start[0];
  331. addr[0] = 0xF0; /* reset bank */
  332. printf (" done\n");
  333. return 0;
  334. }
  335. /*-----------------------------------------------------------------------
  336. * Copy memory to flash, returns:
  337. * 0 - OK
  338. * 1 - write timeout
  339. * 2 - Flash not erased
  340. */
  341. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  342. {
  343. int rc;
  344. while (cnt > 0) {
  345. if ((rc = write_byte(info, addr++, *src++)) != 0) {
  346. return (rc);
  347. }
  348. --cnt;
  349. }
  350. return (0);
  351. }
  352. /*-----------------------------------------------------------------------
  353. * Write a word to Flash, returns:
  354. * 0 - OK
  355. * 1 - write timeout
  356. * 2 - Flash not erased
  357. */
  358. static int write_byte (flash_info_t *info, ulong dest, uchar data)
  359. {
  360. vu_char *addr = (vu_char*)(info->start[0]);
  361. ulong start;
  362. int flag;
  363. /* Check if Flash is (sufficiently) erased */
  364. if ((*((vu_char *)dest) & data) != data) {
  365. return (2);
  366. }
  367. /* Disable interrupts which might cause a timeout here */
  368. flag = disable_interrupts();
  369. addr[0x0AAA] = 0xAA;
  370. addr[0x0555] = 0x55;
  371. addr[0x0AAA] = 0xA0;
  372. *((vu_char *)dest) = data;
  373. /* re-enable interrupts if necessary */
  374. if (flag)
  375. enable_interrupts();
  376. /* data polling for D7 */
  377. start = get_timer (0);
  378. while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
  379. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  380. return (1);
  381. }
  382. }
  383. return (0);
  384. }
  385. /*-----------------------------------------------------------------------
  386. */