flash.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Alex Zuepke <azu@sysgo.de>
  5. *
  6. * (C) Copyright 2005
  7. * 2N Telekomunikace, a.s. <www.2n.cz>
  8. * Ladislav Michl <michl@2n.cz>
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. /*#if 0 */
  30. #if (PHYS_SDRAM_1_SIZE != SZ_32M)
  31. #include "crcek.h"
  32. #if (CFG_MAX_FLASH_BANKS > 1)
  33. #error There is always only _one_ flash chip
  34. #endif
  35. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  36. #define CMD_READ_ARRAY 0x000000f0
  37. #define CMD_UNLOCK1 0x000000aa
  38. #define CMD_UNLOCK2 0x00000055
  39. #define CMD_ERASE_SETUP 0x00000080
  40. #define CMD_ERASE_CONFIRM 0x00000030
  41. #define CMD_PROGRAM 0x000000a0
  42. #define CMD_UNLOCK_BYPASS 0x00000020
  43. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
  44. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002aa << 1)))
  45. #define BIT_ERASE_DONE 0x00000080
  46. #define BIT_RDY_MASK 0x00000080
  47. #define BIT_PROGRAM_ERROR 0x00000020
  48. #define BIT_TIMEOUT 0x80000000 /* our flag */
  49. /*-----------------------------------------------------------------------
  50. */
  51. ulong flash_init(void)
  52. {
  53. int i;
  54. flash_info[0].flash_id = (AMD_MANUFACT & FLASH_VENDMASK) |
  55. (AMD_ID_LV800B & FLASH_TYPEMASK);
  56. flash_info[0].size = PHYS_FLASH_1_SIZE;
  57. flash_info[0].sector_count = CFG_MAX_FLASH_SECT;
  58. memset(flash_info[0].protect, 0, CFG_MAX_FLASH_SECT);
  59. for (i = 0; i < flash_info[0].sector_count; i++) {
  60. switch (i) {
  61. case 0: /* 16kB */
  62. flash_info[0].start[0] = CFG_FLASH_BASE;
  63. break;
  64. case 1: /* 8kB */
  65. flash_info[0].start[1] = CFG_FLASH_BASE + 0x4000;
  66. break;
  67. case 2: /* 8kB */
  68. flash_info[0].start[2] = CFG_FLASH_BASE + 0x4000 +
  69. 0x2000;
  70. break;
  71. case 3: /* 32 KB */
  72. flash_info[0].start[3] = CFG_FLASH_BASE + 0x4000 +
  73. 2 * 0x2000;
  74. break;
  75. case 4:
  76. flash_info[0].start[4] = CFG_FLASH_BASE + 0x4000 +
  77. 2 * 0x2000 + 0x8000;
  78. break;
  79. default: /* 64kB */
  80. flash_info[0].start[i] = flash_info[0].start[i-1] +
  81. 0x10000;
  82. break;
  83. }
  84. }
  85. /* U-Boot */
  86. flash_protect(FLAG_PROTECT_SET,
  87. LOADER1_OFFSET,
  88. LOADER1_OFFSET + LOADER_SIZE - 1, flash_info);
  89. /* Protect crcek, env and r_env as well */
  90. flash_protect(FLAG_PROTECT_SET, 0, 0x8000 - 1, flash_info);
  91. return flash_info[0].size;
  92. }
  93. /*-----------------------------------------------------------------------
  94. */
  95. void flash_print_info(flash_info_t *info)
  96. {
  97. int i;
  98. switch (info->flash_id & FLASH_VENDMASK) {
  99. case (AMD_MANUFACT & FLASH_VENDMASK):
  100. puts("AMD: ");
  101. break;
  102. default:
  103. puts("Unknown vendor ");
  104. break;
  105. }
  106. switch (info->flash_id & FLASH_TYPEMASK) {
  107. case (AMD_ID_LV800B & FLASH_TYPEMASK):
  108. puts("AM29LV800BB (8Mb)\n");
  109. break;
  110. default:
  111. puts("Unknown chip type\n");
  112. return;
  113. }
  114. printf(" Size: %ld MB in %d sectors\n",
  115. info->size >> 20, info->sector_count);
  116. puts(" Sector start addresses:");
  117. for (i = 0; i < info->sector_count; i++) {
  118. if ((i % 5) == 0)
  119. puts("\n ");
  120. printf(" %08lX%s", info->start[i],
  121. info->protect[i] ? " (RO)" : " ");
  122. }
  123. puts("\n");
  124. }
  125. /*-----------------------------------------------------------------------
  126. */
  127. int flash_erase(flash_info_t *info, int s_first, int s_last)
  128. {
  129. ushort result;
  130. int prot, sect;
  131. int rc = ERR_OK;
  132. /* first look for protection bits */
  133. if (info->flash_id == FLASH_UNKNOWN)
  134. return ERR_UNKNOWN_FLASH_TYPE;
  135. if ((s_first < 0) || (s_first > s_last))
  136. return ERR_INVAL;
  137. if ((info->flash_id & FLASH_VENDMASK) !=
  138. (AMD_MANUFACT & FLASH_VENDMASK))
  139. return ERR_UNKNOWN_FLASH_VENDOR;
  140. prot = 0;
  141. for (sect = s_first; sect <= s_last; ++sect)
  142. if (info->protect[sect])
  143. prot++;
  144. if (prot)
  145. printf("- Warning: %d protected sectors will not be erased!\n",
  146. prot);
  147. else
  148. putc('\n');
  149. /* Start erase on unprotected sectors */
  150. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  151. if (info->protect[sect] == 0) { /* not protected */
  152. vu_short *addr = (vu_short *) (info->start[sect]);
  153. /* arm simple, non interrupt dependent timer */
  154. reset_timer_masked();
  155. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  156. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  157. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  158. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  159. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  160. *addr = CMD_ERASE_CONFIRM;
  161. /* wait until flash is ready */
  162. while (1) {
  163. result = *addr;
  164. /* check timeout */
  165. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  166. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  167. rc = ERR_TIMOUT;
  168. break;
  169. }
  170. if ((result & 0xfff) & BIT_ERASE_DONE)
  171. break;
  172. if ((result & 0xffff) & BIT_PROGRAM_ERROR) {
  173. rc = ERR_PROG_ERROR;
  174. break;
  175. }
  176. }
  177. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  178. if (rc != ERR_OK)
  179. goto out;
  180. putc('.');
  181. }
  182. }
  183. out:
  184. /* allow flash to settle - wait 10 ms */
  185. udelay_masked(10000);
  186. return rc;
  187. }
  188. /*-----------------------------------------------------------------------
  189. * Copy memory to flash
  190. */
  191. volatile static int write_hword(flash_info_t *info, ulong dest, ushort data)
  192. {
  193. vu_short *addr = (vu_short *) dest;
  194. ushort result;
  195. int rc = ERR_OK;
  196. /* check if flash is (sufficiently) erased */
  197. result = *addr;
  198. if ((result & data) != data)
  199. return ERR_NOT_ERASED;
  200. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  201. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  202. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  203. *addr = data;
  204. /* arm simple, non interrupt dependent timer */
  205. reset_timer_masked();
  206. /* wait until flash is ready */
  207. while (1) {
  208. result = *addr;
  209. /* check timeout */
  210. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  211. rc = ERR_TIMOUT;
  212. break;
  213. }
  214. if ((result & 0x80) == (data & 0x80))
  215. break;
  216. if ((result & 0xffff) & BIT_PROGRAM_ERROR) {
  217. result = *addr;
  218. if ((result & 0x80) != (data & 0x80))
  219. rc = ERR_PROG_ERROR;
  220. }
  221. }
  222. *addr = CMD_READ_ARRAY;
  223. if (*addr != data)
  224. rc = ERR_PROG_ERROR;
  225. return rc;
  226. }
  227. /*-----------------------------------------------------------------------
  228. * Copy memory to flash.
  229. */
  230. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  231. {
  232. ulong cp, wp;
  233. int l;
  234. int i, rc;
  235. ushort data;
  236. wp = (addr & ~1); /* get lower word aligned address */
  237. /*
  238. * handle unaligned start bytes
  239. */
  240. if ((l = addr - wp) != 0) {
  241. data = 0;
  242. for (i = 0, cp = wp; i < l; ++i, ++cp)
  243. data = (data >> 8) | (*(uchar *) cp << 8);
  244. for (; i < 2 && cnt > 0; ++i) {
  245. data = (data >> 8) | (*src++ << 8);
  246. --cnt;
  247. ++cp;
  248. }
  249. for (; cnt == 0 && i < 2; ++i, ++cp)
  250. data = (data >> 8) | (*(uchar *) cp << 8);
  251. if ((rc = write_hword(info, wp, data)) != 0)
  252. return (rc);
  253. wp += 2;
  254. }
  255. /*
  256. * handle word aligned part
  257. */
  258. while (cnt >= 2) {
  259. data = *((vu_short *) src);
  260. if ((rc = write_hword(info, wp, data)) != 0)
  261. return (rc);
  262. src += 2;
  263. wp += 2;
  264. cnt -= 2;
  265. }
  266. if (cnt == 0)
  267. return ERR_OK;
  268. /*
  269. * handle unaligned tail bytes
  270. */
  271. data = 0;
  272. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  273. data = (data >> 8) | (*src++ << 8);
  274. --cnt;
  275. }
  276. for (; i < 2; ++i, ++cp)
  277. data = (data >> 8) | (*(uchar *) cp << 8);
  278. return write_hword(info, wp, data);
  279. }
  280. #endif