flash.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * (C) Copyright 2002
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #define FLASH_BANK_SIZE 0x02000000
  29. #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
  30. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  31. /*-----------------------------------------------------------------------
  32. */
  33. ulong flash_init(void)
  34. {
  35. int i, j;
  36. ulong size = 0;
  37. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  38. {
  39. ulong flashbase = 0;
  40. flash_info[i].flash_id =
  41. (INTEL_MANUFACT & FLASH_VENDMASK) |
  42. (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
  43. flash_info[i].size = FLASH_BANK_SIZE;
  44. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  45. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  46. switch (i)
  47. {
  48. case 0:
  49. flashbase = PHYS_FLASH_1;
  50. break;
  51. default:
  52. panic("configured to many flash banks!\n");
  53. break;
  54. }
  55. for (j = 0; j < flash_info[i].sector_count; j++)
  56. {
  57. flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
  58. }
  59. size += flash_info[i].size;
  60. }
  61. /* Protect monitor and environment sectors
  62. */
  63. flash_protect(FLAG_PROTECT_SET,
  64. CFG_FLASH_BASE,
  65. CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
  66. &flash_info[0]);
  67. flash_protect(FLAG_PROTECT_SET,
  68. CFG_ENV_ADDR,
  69. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  70. &flash_info[0]);
  71. return size;
  72. }
  73. /*-----------------------------------------------------------------------
  74. */
  75. void flash_print_info (flash_info_t *info)
  76. {
  77. int i, j;
  78. for (j=0; j<CFG_MAX_FLASH_BANKS; j++)
  79. {
  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_28F128J3 & FLASH_TYPEMASK):
  92. printf("28F128J3 (128Mbit)\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. info++;
  113. }
  114. Done:
  115. }
  116. /*-----------------------------------------------------------------------
  117. */
  118. int flash_erase (flash_info_t *info, int s_first, int s_last)
  119. {
  120. int flag, prot, sect;
  121. int rc = ERR_OK;
  122. if (info->flash_id == FLASH_UNKNOWN)
  123. return ERR_UNKNOWN_FLASH_TYPE;
  124. if ((s_first < 0) || (s_first > s_last)) {
  125. return ERR_INVAL;
  126. }
  127. if ((info->flash_id & FLASH_VENDMASK) !=
  128. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  129. return ERR_UNKNOWN_FLASH_VENDOR;
  130. }
  131. prot = 0;
  132. for (sect=s_first; sect<=s_last; ++sect) {
  133. if (info->protect[sect]) {
  134. prot++;
  135. }
  136. }
  137. if (prot)
  138. return ERR_PROTECTED;
  139. /*
  140. * Disable interrupts which might cause a timeout
  141. * here. Remember that our exception vectors are
  142. * at address 0 in the flash, and we don't want a
  143. * (ticker) exception to happen while the flash
  144. * chip is in programming mode.
  145. */
  146. flag = disable_interrupts();
  147. /* Start erase on unprotected sectors */
  148. for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
  149. printf("Erasing sector %2d ... ", sect);
  150. /* arm simple, non interrupt dependent timer */
  151. reset_timer_masked();
  152. if (info->protect[sect] == 0) { /* not protected */
  153. /* vushort *addr = (vushort *)(info->start[sect]); */
  154. ushort *addr = (ushort *)(info->start[sect]);
  155. *addr = 0x20; /* erase setup */
  156. *addr = 0xD0; /* erase confirm */
  157. while ((*addr & 0x80) != 0x80) {
  158. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
  159. *addr = 0xB0; /* suspend erase */
  160. *addr = 0xFF; /* reset to read mode */
  161. rc = ERR_TIMOUT;
  162. goto outahere;
  163. }
  164. }
  165. /* clear status register command */
  166. *addr = 0x50;
  167. /* reset to read mode */
  168. *addr = 0xFF;
  169. }
  170. printf("ok.\n");
  171. }
  172. if (ctrlc())
  173. printf("User Interrupt!\n");
  174. outahere:
  175. /* allow flash to settle - wait 10 ms */
  176. udelay_masked(10000);
  177. if (flag)
  178. enable_interrupts();
  179. return rc;
  180. }
  181. /*-----------------------------------------------------------------------
  182. * Copy memory to flash
  183. */
  184. static int write_word (flash_info_t *info, ulong dest, ushort data)
  185. {
  186. /* vushort *addr = (vushort *)dest, val; */
  187. ushort *addr = (ushort *)dest, val;
  188. int rc = ERR_OK;
  189. int flag;
  190. /* Check if Flash is (sufficiently) erased
  191. */
  192. if ((*addr & data) != data)
  193. return ERR_NOT_ERASED;
  194. /*
  195. * Disable interrupts which might cause a timeout
  196. * here. Remember that our exception vectors are
  197. * at address 0 in the flash, and we don't want a
  198. * (ticker) exception to happen while the flash
  199. * chip is in programming mode.
  200. */
  201. flag = disable_interrupts();
  202. /* clear status register command */
  203. *addr = 0x50;
  204. /* program set-up command */
  205. *addr = 0x40;
  206. /* latch address/data */
  207. *addr = data;
  208. /* arm simple, non interrupt dependent timer */
  209. reset_timer_masked();
  210. /* wait while polling the status register */
  211. while(((val = *addr) & 0x80) != 0x80)
  212. {
  213. if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
  214. rc = ERR_TIMOUT;
  215. /* suspend program command */
  216. *addr = 0xB0;
  217. goto outahere;
  218. }
  219. }
  220. if(val & 0x1A) { /* check for error */
  221. printf("\nFlash write error %02x at address %08lx\n",
  222. (int)val, (unsigned long)dest);
  223. if(val & (1<<3)) {
  224. printf("Voltage range error.\n");
  225. rc = ERR_PROG_ERROR;
  226. goto outahere;
  227. }
  228. if(val & (1<<1)) {
  229. printf("Device protect error.\n");
  230. rc = ERR_PROTECTED;
  231. goto outahere;
  232. }
  233. if(val & (1<<4)) {
  234. printf("Programming error.\n");
  235. rc = ERR_PROG_ERROR;
  236. goto outahere;
  237. }
  238. rc = ERR_PROG_ERROR;
  239. goto outahere;
  240. }
  241. outahere:
  242. /* read array command */
  243. *addr = 0xFF;
  244. if (flag)
  245. enable_interrupts();
  246. return rc;
  247. }
  248. /*-----------------------------------------------------------------------
  249. * Copy memory to flash.
  250. */
  251. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  252. {
  253. ulong cp, wp;
  254. ushort data;
  255. int l;
  256. int i, rc;
  257. wp = (addr & ~1); /* get lower word aligned address */
  258. /*
  259. * handle unaligned start bytes
  260. */
  261. if ((l = addr - wp) != 0) {
  262. data = 0;
  263. for (i=0, cp=wp; i<l; ++i, ++cp) {
  264. data = (data >> 8) | (*(uchar *)cp << 8);
  265. }
  266. for (; i<2 && cnt>0; ++i) {
  267. data = (data >> 8) | (*src++ << 8);
  268. --cnt;
  269. ++cp;
  270. }
  271. for (; cnt==0 && i<2; ++i, ++cp) {
  272. data = (data >> 8) | (*(uchar *)cp << 8);
  273. }
  274. if ((rc = write_word(info, wp, data)) != 0) {
  275. return (rc);
  276. }
  277. wp += 2;
  278. }
  279. /*
  280. * handle word aligned part
  281. */
  282. while (cnt >= 2) {
  283. /* data = *((vushort*)src); */
  284. data = *((ushort*)src);
  285. if ((rc = write_word(info, wp, data)) != 0) {
  286. return (rc);
  287. }
  288. src += 2;
  289. wp += 2;
  290. cnt -= 2;
  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<2 && cnt>0; ++i, ++cp) {
  300. data = (data >> 8) | (*src++ << 8);
  301. --cnt;
  302. }
  303. for (; i<2; ++i, ++cp) {
  304. data = (data >> 8) | (*(uchar *)cp << 8);
  305. }
  306. return write_word(info, wp, data);
  307. }