flash.c 8.9 KB

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