flash.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * (C) Copyright 2005 Rowel Atienza <rowel@diwalabs.com>
  7. * Flash driver for armadillo board HT1070
  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. /*value used by hermit is 0x200*/
  30. /*document says sector size is either 64k in low mem reg and 8k in high mem reg*/
  31. #define MAIN_SECT_SIZE 0x10000
  32. #define UNALIGNED_MASK (3)
  33. #define FL_WORD(addr) (*(volatile unsigned short*)(addr))
  34. #define FLASH_TIMEOUT 20000000
  35. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  36. /*-----------------------------------------------------------------------
  37. */
  38. ulong flash_init (void)
  39. {
  40. int i, j;
  41. ulong size = 0;
  42. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  43. ulong flashbase = 0;
  44. flash_info[i].flash_id = (FUJ_MANUFACT & FLASH_VENDMASK);
  45. /*(INTEL_ID_28F128J3 & FLASH_TYPEMASK); */
  46. flash_info[i].size = FLASH_BANK_SIZE;
  47. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  48. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  49. if (i == 0)
  50. flashbase = PHYS_FLASH_1;
  51. else
  52. panic ("configured too many flash banks!\n");
  53. for (j = 0; j < flash_info[i].sector_count; j++) {
  54. flash_info[i].start[j] =
  55. flashbase + j * MAIN_SECT_SIZE;
  56. }
  57. size += flash_info[i].size;
  58. }
  59. /* Protect monitor and environment sectors
  60. */
  61. flash_protect (FLAG_PROTECT_SET,
  62. CFG_FLASH_BASE,
  63. CFG_FLASH_BASE + monitor_flash_len - 1,
  64. &flash_info[0]);
  65. flash_protect (FLAG_PROTECT_SET,
  66. CONFIG_ENV_ADDR,
  67. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  68. return size;
  69. }
  70. /*-----------------------------------------------------------------------
  71. */
  72. void flash_print_info (flash_info_t * info)
  73. {
  74. int i;
  75. switch (info->flash_id & FLASH_VENDMASK) {
  76. case (FUJ_MANUFACT & FLASH_VENDMASK):
  77. printf ("Fujitsu: ");
  78. break;
  79. default:
  80. printf ("Unknown Vendor ");
  81. break;
  82. }
  83. /*
  84. switch (info->flash_id & FLASH_TYPEMASK) {
  85. case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
  86. printf ("28F128J3 (128Mbit)\n");
  87. break;
  88. default:
  89. printf ("Unknown Chip Type\n");
  90. goto Done;
  91. break;
  92. }
  93. */
  94. printf (" Size: %ld MB in %d Sectors\n",
  95. info->size >> 20, info->sector_count);
  96. printf (" Sector Start Addresses:");
  97. for (i = 0; i < info->sector_count; i++) {
  98. if ((i % 5) == 0) {
  99. printf ("\n ");
  100. }
  101. printf (" %08lX%s", info->start[i],
  102. info->protect[i] ? " (RO)" : " ");
  103. }
  104. printf ("\n");
  105. /*
  106. Done: ;
  107. */
  108. }
  109. /*
  110. * * Loop until both write state machines complete.
  111. * */
  112. static unsigned short flash_status_wait (unsigned long addr,
  113. unsigned short value)
  114. {
  115. unsigned short status;
  116. long timeout = FLASH_TIMEOUT;
  117. while (((status = (FL_WORD (addr))) != value) && timeout > 0) {
  118. timeout--;
  119. }
  120. return status;
  121. }
  122. /*
  123. * Loop until the Write State machine is ready, then do a full error
  124. * check. Clear status and leave the flash in Read Array mode; return
  125. * 0 for no error, -1 for error.
  126. */
  127. static int flash_status_full_check (unsigned long addr, unsigned short value1,
  128. unsigned short value2)
  129. {
  130. unsigned short status1, status2;
  131. status1 = flash_status_wait (addr, value1);
  132. status2 = flash_status_wait (addr + 2, value2);
  133. return (status1 != value1 || status2 != value2) ? -1 : 0;
  134. }
  135. /*-----------------------------------------------------------------------
  136. */
  137. int flash_erase (flash_info_t * info, int s_first, int s_last)
  138. {
  139. int flag, prot, sect;
  140. int rc = ERR_OK;
  141. unsigned long base;
  142. unsigned long addr;
  143. if ((info->flash_id & FLASH_VENDMASK) !=
  144. (FUJ_MANUFACT & FLASH_VENDMASK)) {
  145. return ERR_UNKNOWN_FLASH_VENDOR;
  146. }
  147. prot = 0;
  148. for (sect = s_first; sect <= s_last; ++sect) {
  149. if (info->protect[sect]) {
  150. prot++;
  151. }
  152. }
  153. if (prot)
  154. return ERR_PROTECTED;
  155. /*
  156. * Disable interrupts which might cause a timeout
  157. * here. Remember that our exception vectors are
  158. * at address 0 in the flash, and we don't want a
  159. * (ticker) exception to happen while the flash
  160. * chip is in programming mode.
  161. */
  162. flag = disable_interrupts ();
  163. printf ("Erasing %d sectors starting at sector %2d.\n"
  164. "This make take some time ... ",
  165. s_last - s_first, sect);
  166. /* Start erase on unprotected sectors */
  167. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  168. /* ARM simple, non interrupt dependent timer */
  169. reset_timer_masked ();
  170. if (info->protect[sect] == 0) { /* not protected */
  171. addr = sect * MAIN_SECT_SIZE;
  172. addr &= ~(unsigned long) UNALIGNED_MASK; /* word align */
  173. base = addr & 0xF0000000;
  174. FL_WORD (base + (0x555 << 1)) = 0xAA;
  175. FL_WORD (base + (0x2AA << 1)) = 0x55;
  176. FL_WORD (base + (0x555 << 1)) = 0x80;
  177. FL_WORD (base + (0x555 << 1)) = 0xAA;
  178. FL_WORD (base + (0x2AA << 1)) = 0x55;
  179. FL_WORD (addr) = 0x30;
  180. if (flash_status_full_check (addr, 0xFFFF, 0xFFFF))
  181. return ERR_PROTECTED;
  182. }
  183. }
  184. printf ("\nDone.\n");
  185. if (ctrlc ())
  186. printf ("User Interrupt!\n");
  187. /* allow flash to settle - wait 10 ms */
  188. udelay_masked (10000);
  189. if (flag)
  190. enable_interrupts ();
  191. return rc;
  192. }
  193. /*-----------------------------------------------------------------------
  194. * Copy memory to flash
  195. */
  196. static int write_word (flash_info_t * info, ulong dest, ushort data)
  197. {
  198. int flag;
  199. unsigned long base;
  200. /* Check if Flash is (sufficiently) erased
  201. */
  202. if ((FL_WORD (dest) & data) != data)
  203. return ERR_NOT_ERASED;
  204. /*if(dest & UNALIGNED_MASK) return ERR_ALIGN; */
  205. /*
  206. * Disable interrupts which might cause a timeout
  207. * here. Remember that our exception vectors are
  208. * at address 0 in the flash, and we don't want a
  209. * (ticker) exception to happen while the flash
  210. * chip is in programming mode.
  211. */
  212. flag = disable_interrupts ();
  213. /* arm simple, non interrupt dependent timer */
  214. reset_timer_masked ();
  215. base = dest & 0xF0000000;
  216. FL_WORD (base + (0x555 << 1)) = 0xAA;
  217. FL_WORD (base + (0x2AA << 1)) = 0x55;
  218. FL_WORD (base + (0x555 << 1)) = 0xA0;
  219. FL_WORD (dest) = data;
  220. /*printf("writing 0x%p = 0x%x\n",dest,data); */
  221. if (flash_status_wait (dest, data) != data)
  222. return ERR_PROG_ERROR;
  223. if (flag)
  224. enable_interrupts ();
  225. return ERR_OK;
  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. ushort data;
  234. int l;
  235. int i, rc;
  236. wp = (addr & ~1); /* get lower word aligned address */
  237. printf ("Writing %lu short data to 0x%lx from 0x%p.\n ", cnt, wp, src);
  238. /*
  239. * handle unaligned start bytes
  240. */
  241. if ((l = addr - wp) != 0) {
  242. data = 0;
  243. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  244. data = (data >> 8) | (*(uchar *) cp << 8);
  245. }
  246. for (; i < 2 && cnt > 0; ++i) {
  247. data = (data >> 8) | (*src++ << 8);
  248. --cnt;
  249. ++cp;
  250. }
  251. for (; cnt == 0 && i < 2; ++i, ++cp) {
  252. data = (data >> 8) | (*(uchar *) cp << 8);
  253. }
  254. if ((rc = write_word (info, wp, data)) != 0) {
  255. return (rc);
  256. }
  257. wp += 2;
  258. }
  259. /*
  260. * handle word aligned part
  261. */
  262. while (cnt >= 2) {
  263. data = *((vu_short *) src);
  264. if ((rc = write_word (info, wp, data)) != 0) {
  265. return (rc);
  266. }
  267. src += 2;
  268. wp += 2;
  269. cnt -= 2;
  270. }
  271. if (cnt == 0) {
  272. printf ("\nDone.\n");
  273. return ERR_OK;
  274. }
  275. /*
  276. * handle unaligned tail bytes
  277. */
  278. data = 0;
  279. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  280. data = (data >> 8) | (*src++ << 8);
  281. --cnt;
  282. }
  283. for (; i < 2; ++i, ++cp) {
  284. data = (data >> 8) | (*(uchar *) cp << 8);
  285. }
  286. return write_word (info, wp, data);
  287. }