flash.c 7.9 KB

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