flash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@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. #define FLASH_BANK_SIZE 0x800000
  26. #define MAIN_SECT_SIZE 0x20000
  27. #define PARAM_SECT_SIZE 0x4000
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  29. /*-----------------------------------------------------------------------
  30. */
  31. ulong flash_init(void)
  32. {
  33. int i, j;
  34. ulong size = 0;
  35. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  36. {
  37. ulong flashbase = 0;
  38. flash_info[i].flash_id =
  39. (INTEL_MANUFACT & FLASH_VENDMASK) |
  40. (INTEL_ID_28F320B3T & FLASH_TYPEMASK);
  41. flash_info[i].size = FLASH_BANK_SIZE;
  42. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  43. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  44. if (i == 0)
  45. flashbase = PHYS_FLASH_1;
  46. else if (i == 1)
  47. flashbase = PHYS_FLASH_2;
  48. else
  49. panic("configured to many flash banks!\n");
  50. for (j = 0; j < flash_info[i].sector_count; j++)
  51. {
  52. if (j <= 7)
  53. {
  54. flash_info[i].start[j] = flashbase + j * PARAM_SECT_SIZE;
  55. }
  56. else
  57. {
  58. flash_info[i].start[j] = flashbase + (j - 7)*MAIN_SECT_SIZE;
  59. }
  60. }
  61. size += flash_info[i].size;
  62. }
  63. /* Protect monitor and environment sectors
  64. */
  65. flash_protect(FLAG_PROTECT_SET,
  66. CFG_FLASH_BASE,
  67. CFG_FLASH_BASE + monitor_flash_len - 1,
  68. &flash_info[0]);
  69. flash_protect(FLAG_PROTECT_SET,
  70. CFG_ENV_ADDR,
  71. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  72. &flash_info[0]);
  73. return size;
  74. }
  75. /*-----------------------------------------------------------------------
  76. */
  77. void flash_print_info (flash_info_t *info)
  78. {
  79. int i;
  80. switch (info->flash_id & FLASH_VENDMASK)
  81. {
  82. case (INTEL_MANUFACT & FLASH_VENDMASK):
  83. printf("Intel: ");
  84. break;
  85. default:
  86. printf("Unknown Vendor ");
  87. break;
  88. }
  89. switch (info->flash_id & FLASH_TYPEMASK)
  90. {
  91. case (INTEL_ID_28F320B3T & FLASH_TYPEMASK):
  92. printf("28F320F3B (16Mbit)\n");
  93. break;
  94. default:
  95. printf("Unknown Chip Type\n");
  96. goto Done;
  97. break;
  98. }
  99. printf(" Size: %ld MB in %d Sectors\n",
  100. info->size >> 20, info->sector_count);
  101. printf(" Sector Start Addresses:");
  102. for (i = 0; i < info->sector_count; i++)
  103. {
  104. if ((i % 5) == 0)
  105. {
  106. printf ("\n ");
  107. }
  108. printf (" %08lX%s", info->start[i],
  109. info->protect[i] ? " (RO)" : " ");
  110. }
  111. printf ("\n");
  112. Done:
  113. }
  114. /*-----------------------------------------------------------------------
  115. */
  116. int flash_erase (flash_info_t *info, int s_first, int s_last)
  117. {
  118. int flag, prot, sect;
  119. int rc = ERR_OK;
  120. if (info->flash_id == FLASH_UNKNOWN)
  121. return ERR_UNKNOWN_FLASH_TYPE;
  122. if ((s_first < 0) || (s_first > s_last)) {
  123. return ERR_INVAL;
  124. }
  125. if ((info->flash_id & FLASH_VENDMASK) !=
  126. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  127. return ERR_UNKNOWN_FLASH_VENDOR;
  128. }
  129. prot = 0;
  130. for (sect=s_first; sect<=s_last; ++sect) {
  131. if (info->protect[sect]) {
  132. prot++;
  133. }
  134. }
  135. if (prot)
  136. return ERR_PROTECTED;
  137. /*
  138. * Disable interrupts which might cause a timeout
  139. * here. Remember that our exception vectors are
  140. * at address 0 in the flash, and we don't want a
  141. * (ticker) exception to happen while the flash
  142. * chip is in programming mode.
  143. */
  144. flag = disable_interrupts();
  145. /* Start erase on unprotected sectors */
  146. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  147. printf("Erasing sector %2d ... ", sect);
  148. /* arm simple, non interrupt dependent timer */
  149. reset_timer_masked();
  150. if (info->protect[sect] == 0) { /* not protected */
  151. vu_long *addr = (vu_long *)(info->start[sect]);
  152. *addr = 0x00200020; /* erase setup */
  153. *addr = 0x00D000D0; /* erase confirm */
  154. while ((*addr & 0x00800080) != 0x00800080) {
  155. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  156. *addr = 0x00B000B0; /* suspend erase */
  157. *addr = 0x00FF00FF; /* reset to read mode */
  158. rc = ERR_TIMOUT;
  159. goto outahere;
  160. }
  161. }
  162. *addr = 0x00FF00FF; /* reset to read mode */
  163. }
  164. printf("ok.\n");
  165. }
  166. if (ctrlc())
  167. printf("User Interrupt!\n");
  168. outahere:
  169. /* allow flash to settle - wait 10 ms */
  170. udelay_masked(10000);
  171. if (flag)
  172. enable_interrupts();
  173. return rc;
  174. }
  175. /*-----------------------------------------------------------------------
  176. * Copy memory to flash
  177. */
  178. static int write_word (flash_info_t *info, ulong dest, ulong data)
  179. {
  180. vu_long *addr = (vu_long *)dest;
  181. ulong barf;
  182. int rc = ERR_OK;
  183. int flag;
  184. /* Check if Flash is (sufficiently) erased
  185. */
  186. if ((*addr & data) != data)
  187. return ERR_NOT_ERASED;
  188. /*
  189. * Disable interrupts which might cause a timeout
  190. * here. Remember that our exception vectors are
  191. * at address 0 in the flash, and we don't want a
  192. * (ticker) exception to happen while the flash
  193. * chip is in programming mode.
  194. */
  195. flag = disable_interrupts();
  196. /* clear status register command */
  197. *addr = 0x00500050;
  198. /* program set-up command */
  199. *addr = 0x00400040;
  200. /* latch address/data */
  201. *addr = data;
  202. /* arm simple, non interrupt dependent timer */
  203. reset_timer_masked();
  204. /* read status register command */
  205. *addr = 0x00700070;
  206. /* wait while polling the status register */
  207. while((*addr & 0x00800080) != 0x00800080)
  208. {
  209. if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
  210. rc = ERR_TIMOUT;
  211. /* suspend program command */
  212. *addr = 0x00B000B0;
  213. goto outahere;
  214. }
  215. if( *addr & 0x003A003A) { /* check for error */
  216. barf = *addr;
  217. if( barf & 0x003A0000) {
  218. barf >>=16;
  219. } else {
  220. barf &= 0x0000003A;
  221. }
  222. printf("\nFlash write error %02lx at address %08lx\n",
  223. barf, (unsigned long)dest);
  224. if(barf & 0x0002) {
  225. printf("Block locked, not erased.\n");
  226. rc = ERR_NOT_ERASED;
  227. goto outahere;
  228. }
  229. if(barf & 0x0010) {
  230. printf("Programming error.\n");
  231. rc = ERR_PROG_ERROR;
  232. goto outahere;
  233. }
  234. if(barf & 0x0008) {
  235. printf("Vpp Low error.\n");
  236. rc = ERR_PROG_ERROR;
  237. goto outahere;
  238. }
  239. rc = ERR_PROG_ERROR;
  240. goto outahere;
  241. }
  242. }
  243. outahere:
  244. /* read array command */
  245. *addr = 0x00FF00FF;
  246. if (flag)
  247. enable_interrupts();
  248. return rc;
  249. }
  250. /*-----------------------------------------------------------------------
  251. * Copy memory to flash.
  252. */
  253. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  254. {
  255. ulong cp, wp, data;
  256. int l;
  257. int i, rc;
  258. wp = (addr & ~3); /* get lower word aligned address */
  259. /*
  260. * handle unaligned start bytes
  261. */
  262. if ((l = addr - wp) != 0) {
  263. data = 0;
  264. for (i=0, cp=wp; i<l; ++i, ++cp) {
  265. data = (data >> 8) | (*(uchar *)cp << 24);
  266. }
  267. for (; i<4 && cnt>0; ++i) {
  268. data = (data >> 8) | (*src++ << 24);
  269. --cnt;
  270. ++cp;
  271. }
  272. for (; cnt==0 && i<4; ++i, ++cp) {
  273. data = (data >> 8) | (*(uchar *)cp << 24);
  274. }
  275. if ((rc = write_word(info, wp, data)) != 0) {
  276. return (rc);
  277. }
  278. wp += 4;
  279. }
  280. /*
  281. * handle word aligned part
  282. */
  283. while (cnt >= 4) {
  284. data = *((vu_long*)src);
  285. if ((rc = write_word(info, wp, data)) != 0) {
  286. return (rc);
  287. }
  288. src += 4;
  289. wp += 4;
  290. cnt -= 4;
  291. }
  292. if (cnt == 0) {
  293. return ERR_OK;
  294. }
  295. /*
  296. * handle unaligned tail bytes
  297. */
  298. data = 0;
  299. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  300. data = (data >> 8) | (*src++ << 24);
  301. --cnt;
  302. }
  303. for (; i<4; ++i, ++cp) {
  304. data = (data >> 8) | (*(uchar *)cp << 24);
  305. }
  306. return write_word(info, wp, data);
  307. }