flash.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Alex Zuepke <azu@sysgo.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. ulong myflush (void);
  26. #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
  27. #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  29. #define CMD_READ_ARRAY 0x000000F0
  30. #define CMD_UNLOCK1 0x000000AA
  31. #define CMD_UNLOCK2 0x00000055
  32. #define CMD_ERASE_SETUP 0x00000080
  33. #define CMD_ERASE_CONFIRM 0x00000030
  34. #define CMD_PROGRAM 0x000000A0
  35. #define CMD_UNLOCK_BYPASS 0x00000020
  36. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
  37. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
  38. #define BIT_ERASE_DONE 0x00000080
  39. #define BIT_RDY_MASK 0x00000080
  40. #define BIT_PROGRAM_ERROR 0x00000020
  41. #define BIT_TIMEOUT 0x80000000 /* our flag */
  42. #define READY 1
  43. #define ERR 2
  44. #define TMO 4
  45. /*-----------------------------------------------------------------------
  46. */
  47. ulong flash_init (void)
  48. {
  49. int i, j;
  50. ulong size = 0;
  51. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  52. ulong flashbase = 0;
  53. flash_info[i].flash_id =
  54. #if defined(CONFIG_AMD_LV400)
  55. (AMD_MANUFACT & FLASH_VENDMASK) |
  56. (AMD_ID_LV400B & FLASH_TYPEMASK);
  57. #elif defined(CONFIG_AMD_LV800)
  58. (AMD_MANUFACT & FLASH_VENDMASK) |
  59. (AMD_ID_LV800B & FLASH_TYPEMASK);
  60. #else
  61. #error "Unknown flash configured"
  62. #endif
  63. flash_info[i].size = FLASH_BANK_SIZE;
  64. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  65. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  66. if (i == 0)
  67. flashbase = PHYS_FLASH_1;
  68. else
  69. panic ("configured too many flash banks!\n");
  70. for (j = 0; j < flash_info[i].sector_count; j++) {
  71. if (j <= 3) {
  72. /* 1st one is 16 KB */
  73. if (j == 0) {
  74. flash_info[i].start[j] =
  75. flashbase + 0;
  76. }
  77. /* 2nd and 3rd are both 8 KB */
  78. if ((j == 1) || (j == 2)) {
  79. flash_info[i].start[j] =
  80. flashbase + 0x4000 + (j -
  81. 1) *
  82. 0x2000;
  83. }
  84. /* 4th 32 KB */
  85. if (j == 3) {
  86. flash_info[i].start[j] =
  87. flashbase + 0x8000;
  88. }
  89. } else {
  90. flash_info[i].start[j] =
  91. flashbase + (j - 3) * MAIN_SECT_SIZE;
  92. }
  93. }
  94. size += flash_info[i].size;
  95. }
  96. flash_protect (FLAG_PROTECT_SET,
  97. CFG_FLASH_BASE,
  98. CFG_FLASH_BASE + monitor_flash_len - 1,
  99. &flash_info[0]);
  100. flash_protect (FLAG_PROTECT_SET,
  101. CFG_ENV_ADDR,
  102. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  103. return size;
  104. }
  105. /*-----------------------------------------------------------------------
  106. */
  107. void flash_print_info (flash_info_t * info)
  108. {
  109. int i;
  110. switch (info->flash_id & FLASH_VENDMASK) {
  111. case (AMD_MANUFACT & FLASH_VENDMASK):
  112. printf ("AMD: ");
  113. break;
  114. default:
  115. printf ("Unknown Vendor ");
  116. break;
  117. }
  118. switch (info->flash_id & FLASH_TYPEMASK) {
  119. case (AMD_ID_LV400B & FLASH_TYPEMASK):
  120. printf ("1x Amd29LV400BB (4Mbit)\n");
  121. break;
  122. case (AMD_ID_LV800B & FLASH_TYPEMASK):
  123. printf ("1x Amd29LV800BB (8Mbit)\n");
  124. break;
  125. default:
  126. printf ("Unknown Chip Type\n");
  127. goto Done;
  128. break;
  129. }
  130. printf (" Size: %ld MB in %d Sectors\n",
  131. info->size >> 20, info->sector_count);
  132. printf (" Sector Start Addresses:");
  133. for (i = 0; i < info->sector_count; i++) {
  134. if ((i % 5) == 0) {
  135. printf ("\n ");
  136. }
  137. printf (" %08lX%s", info->start[i],
  138. info->protect[i] ? " (RO)" : " ");
  139. }
  140. printf ("\n");
  141. Done:;
  142. }
  143. /*-----------------------------------------------------------------------
  144. */
  145. int flash_erase (flash_info_t * info, int s_first, int s_last)
  146. {
  147. ushort result;
  148. int iflag, cflag, prot, sect;
  149. int rc = ERR_OK;
  150. int chip;
  151. /* first look for protection bits */
  152. if (info->flash_id == FLASH_UNKNOWN)
  153. return ERR_UNKNOWN_FLASH_TYPE;
  154. if ((s_first < 0) || (s_first > s_last)) {
  155. return ERR_INVAL;
  156. }
  157. if ((info->flash_id & FLASH_VENDMASK) !=
  158. (AMD_MANUFACT & FLASH_VENDMASK)) {
  159. return ERR_UNKNOWN_FLASH_VENDOR;
  160. }
  161. prot = 0;
  162. for (sect = s_first; sect <= s_last; ++sect) {
  163. if (info->protect[sect]) {
  164. prot++;
  165. }
  166. }
  167. if (prot)
  168. return ERR_PROTECTED;
  169. /*
  170. * Disable interrupts which might cause a timeout
  171. * here. Remember that our exception vectors are
  172. * at address 0 in the flash, and we don't want a
  173. * (ticker) exception to happen while the flash
  174. * chip is in programming mode.
  175. */
  176. cflag = icache_status ();
  177. icache_disable ();
  178. iflag = disable_interrupts ();
  179. /* Start erase on unprotected sectors */
  180. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  181. printf ("Erasing sector %2d ... ", sect);
  182. /* arm simple, non interrupt dependent timer */
  183. reset_timer_masked ();
  184. if (info->protect[sect] == 0) { /* not protected */
  185. vu_short *addr = (vu_short *) (info->start[sect]);
  186. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  187. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  188. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  189. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  190. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  191. *addr = CMD_ERASE_CONFIRM;
  192. /* wait until flash is ready */
  193. chip = 0;
  194. do {
  195. result = *addr;
  196. /* check timeout */
  197. if (get_timer_masked () >
  198. CFG_FLASH_ERASE_TOUT) {
  199. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  200. chip = TMO;
  201. break;
  202. }
  203. if (!chip
  204. && (result & 0xFFFF) & BIT_ERASE_DONE)
  205. chip = READY;
  206. if (!chip
  207. && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  208. chip = ERR;
  209. } while (!chip);
  210. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  211. if (chip == ERR) {
  212. rc = ERR_PROG_ERROR;
  213. goto outahere;
  214. }
  215. if (chip == TMO) {
  216. rc = ERR_TIMOUT;
  217. goto outahere;
  218. }
  219. printf ("ok.\n");
  220. } else { /* it was protected */
  221. printf ("protected!\n");
  222. }
  223. }
  224. if (ctrlc ())
  225. printf ("User Interrupt!\n");
  226. outahere:
  227. /* allow flash to settle - wait 10 ms */
  228. udelay_masked (10000);
  229. if (iflag)
  230. enable_interrupts ();
  231. if (cflag)
  232. icache_enable ();
  233. return rc;
  234. }
  235. /*-----------------------------------------------------------------------
  236. * Copy memory to flash
  237. */
  238. static int write_hword (flash_info_t * info, ulong dest, ushort data)
  239. {
  240. vu_short *addr = (vu_short *) dest;
  241. ushort result;
  242. int rc = ERR_OK;
  243. int cflag, iflag;
  244. int chip;
  245. /*
  246. * Check if Flash is (sufficiently) erased
  247. */
  248. result = *addr;
  249. if ((result & data) != data)
  250. return ERR_NOT_ERASED;
  251. /*
  252. * Disable interrupts which might cause a timeout
  253. * here. Remember that our exception vectors are
  254. * at address 0 in the flash, and we don't want a
  255. * (ticker) exception to happen while the flash
  256. * chip is in programming mode.
  257. */
  258. cflag = icache_status ();
  259. icache_disable ();
  260. iflag = disable_interrupts ();
  261. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  262. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  263. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  264. *addr = CMD_PROGRAM;
  265. *addr = data;
  266. /* arm simple, non interrupt dependent timer */
  267. reset_timer_masked ();
  268. /* wait until flash is ready */
  269. chip = 0;
  270. do {
  271. result = *addr;
  272. /* check timeout */
  273. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  274. chip = ERR | TMO;
  275. break;
  276. }
  277. if (!chip && ((result & 0x80) == (data & 0x80)))
  278. chip = READY;
  279. if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
  280. result = *addr;
  281. if ((result & 0x80) == (data & 0x80))
  282. chip = READY;
  283. else
  284. chip = ERR;
  285. }
  286. } while (!chip);
  287. *addr = CMD_READ_ARRAY;
  288. if (chip == ERR || *addr != data)
  289. rc = ERR_PROG_ERROR;
  290. if (iflag)
  291. enable_interrupts ();
  292. if (cflag)
  293. icache_enable ();
  294. return rc;
  295. }
  296. /*-----------------------------------------------------------------------
  297. * Copy memory to flash.
  298. */
  299. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  300. {
  301. ulong cp, wp;
  302. int l;
  303. int i, rc;
  304. ushort data;
  305. wp = (addr & ~1); /* get lower word aligned address */
  306. /*
  307. * handle unaligned start bytes
  308. */
  309. if ((l = addr - wp) != 0) {
  310. data = 0;
  311. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  312. data = (data >> 8) | (*(uchar *) cp << 8);
  313. }
  314. for (; i < 2 && cnt > 0; ++i) {
  315. data = (data >> 8) | (*src++ << 8);
  316. --cnt;
  317. ++cp;
  318. }
  319. for (; cnt == 0 && i < 2; ++i, ++cp) {
  320. data = (data >> 8) | (*(uchar *) cp << 8);
  321. }
  322. if ((rc = write_hword (info, wp, data)) != 0) {
  323. return (rc);
  324. }
  325. wp += 2;
  326. }
  327. /*
  328. * handle word aligned part
  329. */
  330. while (cnt >= 2) {
  331. data = *((vu_short *) src);
  332. if ((rc = write_hword (info, wp, data)) != 0) {
  333. return (rc);
  334. }
  335. src += 2;
  336. wp += 2;
  337. cnt -= 2;
  338. }
  339. if (cnt == 0) {
  340. return ERR_OK;
  341. }
  342. /*
  343. * handle unaligned tail bytes
  344. */
  345. data = 0;
  346. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  347. data = (data >> 8) | (*src++ << 8);
  348. --cnt;
  349. }
  350. for (; i < 2; ++i, ++cp) {
  351. data = (data >> 8) | (*(uchar *) cp << 8);
  352. }
  353. return write_hword (info, wp, data);
  354. }