flash.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * (C) Copyright 2000
  3. * Marius Groeger <mgroeger@sysgo.de>
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * Flash Routines for AMD 29F080B devices
  10. *
  11. *--------------------------------------------------------------------
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. */
  30. #include <common.h>
  31. #include <mpc8xx.h>
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  33. /*-----------------------------------------------------------------------
  34. * Functions
  35. */
  36. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  37. static int write_word (flash_info_t *info, ulong dest, ulong data);
  38. /*-----------------------------------------------------------------------
  39. */
  40. unsigned long flash_init (void)
  41. {
  42. unsigned long size;
  43. int i;
  44. /* Init: no FLASHes known */
  45. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  46. flash_info[i].flash_id = FLASH_UNKNOWN;
  47. }
  48. /* for now, only support the 4 MB Flash SIMM */
  49. size = flash_get_size((vu_long *)CFG_FLASH0_BASE, &flash_info[0]);
  50. /*
  51. * protect monitor and environment sectors
  52. */
  53. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  54. flash_protect(FLAG_PROTECT_SET,
  55. CFG_MONITOR_BASE,
  56. CFG_MONITOR_BASE+monitor_flash_len-1,
  57. &flash_info[0]);
  58. #endif
  59. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  60. # ifndef CFG_ENV_SIZE
  61. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  62. # endif
  63. flash_protect(FLAG_PROTECT_SET,
  64. CFG_ENV_ADDR,
  65. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  66. &flash_info[0]);
  67. #endif
  68. return /*size*/ (CFG_FLASH0_SIZE * 1024 * 1024);
  69. }
  70. /*-----------------------------------------------------------------------
  71. */
  72. void flash_print_info (flash_info_t *info)
  73. {
  74. int i;
  75. if (info->flash_id == FLASH_UNKNOWN) {
  76. printf ("missing or unknown FLASH type\n");
  77. return;
  78. }
  79. switch ((info->flash_id >> 16) & 0xff) {
  80. case 0x1:
  81. printf ("AMD ");
  82. break;
  83. default:
  84. printf ("Unknown Vendor ");
  85. break;
  86. }
  87. switch (info->flash_id & FLASH_TYPEMASK) {
  88. case AMD_ID_F040B:
  89. printf ("AM29F040B (4 Mbit)\n");
  90. break;
  91. case AMD_ID_F080B:
  92. printf ("AM29F080B (8 Mbit)\n");
  93. break;
  94. default:
  95. printf ("Unknown Chip Type\n");
  96. break;
  97. }
  98. printf (" Size: %ld MB in %d Sectors\n",
  99. info->size >> 20, info->sector_count);
  100. printf (" Sector Start Addresses:");
  101. for (i=0; i<info->sector_count; ++i) {
  102. if ((i % 5) == 0)
  103. printf ("\n ");
  104. printf (" %08lX%s",
  105. info->start[i],
  106. info->protect[i] ? " (RO)" : " "
  107. );
  108. }
  109. printf ("\n");
  110. return;
  111. }
  112. /*
  113. * The following code cannot be run from FLASH!
  114. */
  115. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  116. {
  117. short i;
  118. vu_long vendor, devid;
  119. ulong base = (ulong)addr;
  120. /* printf("addr = %08lx\n", (unsigned long)addr); */
  121. /* Reset and Write auto select command: read Manufacturer ID */
  122. addr[0] = 0xf0f0f0f0;
  123. addr[0x0555] = 0xAAAAAAAA;
  124. addr[0x02AA] = 0x55555555;
  125. addr[0x0555] = 0x90909090;
  126. udelay (1000);
  127. vendor = addr[0];
  128. /* printf("vendor = %08lx\n", vendor); */
  129. if (vendor != 0x01010101) {
  130. info->size = 0;
  131. goto out;
  132. }
  133. devid = addr[1];
  134. /* printf("devid = %08lx\n", devid); */
  135. if ((devid & 0xff) == AMD_ID_F080B) {
  136. info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
  137. /* we have 16 sectors with 64KB each x 4 */
  138. info->sector_count = 16;
  139. info->size = 4 * info->sector_count * 64*1024;
  140. }
  141. else {
  142. info->size = 0;
  143. goto out;
  144. }
  145. /* check for protected sectors */
  146. for (i = 0; i < info->sector_count; i++) {
  147. /* sector base address */
  148. info->start[i] = base + i * (info->size / info->sector_count);
  149. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  150. /* D0 = 1 if protected */
  151. addr = (volatile unsigned long *)(info->start[i]);
  152. info->protect[i] = addr[2] & 1;
  153. }
  154. /* reset command */
  155. addr = (vu_long *)info->start[0];
  156. out:
  157. addr[0] = 0xf0f0f0f0;
  158. return info->size;
  159. }
  160. /*-----------------------------------------------------------------------
  161. */
  162. int flash_erase (flash_info_t *info, int s_first, int s_last)
  163. {
  164. vu_long *addr = (vu_long*)(info->start[0]);
  165. int flag, prot, sect, l_sect;
  166. ulong start, now, last;
  167. if ((s_first < 0) || (s_first > s_last)) {
  168. if (info->flash_id == FLASH_UNKNOWN) {
  169. printf ("- missing\n");
  170. } else {
  171. printf ("- no sectors to erase\n");
  172. }
  173. return 1;
  174. }
  175. prot = 0;
  176. for (sect = s_first; sect <= s_last; sect++) {
  177. if (info->protect[sect]) {
  178. prot++;
  179. }
  180. }
  181. if (prot) {
  182. printf ("- Warning: %d protected sectors will not be erased!\n",
  183. prot);
  184. } else {
  185. printf ("\n");
  186. }
  187. l_sect = -1;
  188. /* Disable interrupts which might cause a timeout here */
  189. flag = disable_interrupts();
  190. addr[0x0555] = 0xAAAAAAAA;
  191. addr[0x02AA] = 0x55555555;
  192. addr[0x0555] = 0x80808080;
  193. addr[0x0555] = 0xAAAAAAAA;
  194. addr[0x02AA] = 0x55555555;
  195. udelay (100);
  196. /* Start erase on unprotected sectors */
  197. for (sect = s_first; sect<=s_last; sect++) {
  198. if (info->protect[sect] == 0) { /* not protected */
  199. addr = (vu_long*)(info->start[sect]);
  200. addr[0] = 0x30303030;
  201. l_sect = sect;
  202. }
  203. }
  204. /* re-enable interrupts if necessary */
  205. if (flag)
  206. enable_interrupts();
  207. /* wait at least 80us - let's wait 1 ms */
  208. udelay (1000);
  209. /*
  210. * We wait for the last triggered sector
  211. */
  212. if (l_sect < 0)
  213. goto DONE;
  214. start = get_timer (0);
  215. last = start;
  216. addr = (vu_long*)(info->start[l_sect]);
  217. while ((addr[0] & 0x80808080) != 0x80808080) {
  218. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  219. printf ("Timeout\n");
  220. return 1;
  221. }
  222. /* show that we're waiting */
  223. if ((now - last) > 1000) { /* every second */
  224. serial_putc ('.');
  225. last = now;
  226. }
  227. }
  228. DONE:
  229. /* reset to read mode */
  230. addr = (volatile unsigned long *)info->start[0];
  231. addr[0] = 0xF0F0F0F0; /* reset bank */
  232. printf (" done\n");
  233. return 0;
  234. }
  235. /*-----------------------------------------------------------------------
  236. * Copy memory to flash, returns:
  237. * 0 - OK
  238. * 1 - write timeout
  239. * 2 - Flash not erased
  240. */
  241. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  242. {
  243. ulong cp, wp, data;
  244. int i, l, rc;
  245. wp = (addr & ~3); /* get lower word aligned address */
  246. /*
  247. * handle unaligned start bytes
  248. */
  249. if ((l = addr - wp) != 0) {
  250. data = 0;
  251. for (i=0, cp=wp; i<l; ++i, ++cp) {
  252. data = (data << 8) | (*(uchar *)cp);
  253. }
  254. for (; i<4 && cnt>0; ++i) {
  255. data = (data << 8) | *src++;
  256. --cnt;
  257. ++cp;
  258. }
  259. for (; cnt==0 && i<4; ++i, ++cp) {
  260. data = (data << 8) | (*(uchar *)cp);
  261. }
  262. if ((rc = write_word(info, wp, data)) != 0) {
  263. return (rc);
  264. }
  265. wp += 4;
  266. }
  267. /*
  268. * handle word aligned part
  269. */
  270. while (cnt >= 4) {
  271. data = 0;
  272. for (i=0; i<4; ++i) {
  273. data = (data << 8) | *src++;
  274. }
  275. if ((rc = write_word(info, wp, data)) != 0) {
  276. return (rc);
  277. }
  278. wp += 4;
  279. cnt -= 4;
  280. }
  281. if (cnt == 0) {
  282. return (0);
  283. }
  284. /*
  285. * handle unaligned tail bytes
  286. */
  287. data = 0;
  288. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  289. data = (data << 8) | *src++;
  290. --cnt;
  291. }
  292. for (; i<4; ++i, ++cp) {
  293. data = (data << 8) | (*(uchar *)cp);
  294. }
  295. return (write_word(info, wp, data));
  296. }
  297. /*-----------------------------------------------------------------------
  298. * Write a word to Flash, returns:
  299. * 0 - OK
  300. * 1 - write timeout
  301. * 2 - Flash not erased
  302. */
  303. static int write_word (flash_info_t *info, ulong dest, ulong data)
  304. {
  305. vu_long *addr = (vu_long*)(info->start[0]);
  306. ulong start;
  307. int flag;
  308. /* Check if Flash is (sufficiently) erased */
  309. if ((*((vu_long *)dest) & data) != data) {
  310. return (2);
  311. }
  312. /* Disable interrupts which might cause a timeout here */
  313. flag = disable_interrupts();
  314. addr[0x0555] = 0xAAAAAAAA;
  315. addr[0x02AA] = 0x55555555;
  316. addr[0x0555] = 0xA0A0A0A0;
  317. *((vu_long *)dest) = data;
  318. /* re-enable interrupts if necessary */
  319. if (flag)
  320. enable_interrupts();
  321. /* data polling for D7 */
  322. start = get_timer (0);
  323. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  324. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  325. return (1);
  326. }
  327. }
  328. return (0);
  329. }
  330. /*-----------------------------------------------------------------------
  331. */