flash.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. CFG_ENV_ADDR,
  67. CFG_ENV_ADDR + CFG_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. Done:
  106. }
  107. /*
  108. * * Loop until both write state machines complete.
  109. * */
  110. static unsigned short flash_status_wait (unsigned long addr,
  111. unsigned short value)
  112. {
  113. unsigned short status;
  114. long timeout = FLASH_TIMEOUT;
  115. while (((status = (FL_WORD (addr))) != value) && timeout > 0) {
  116. timeout--;
  117. }
  118. return status;
  119. }
  120. /*
  121. * Loop until the Write State machine is ready, then do a full error
  122. * check. Clear status and leave the flash in Read Array mode; return
  123. * 0 for no error, -1 for error.
  124. */
  125. static int flash_status_full_check (unsigned long addr, unsigned short value1,
  126. unsigned short value2)
  127. {
  128. unsigned short status1, status2;
  129. status1 = flash_status_wait (addr, value1);
  130. status2 = flash_status_wait (addr + 2, value2);
  131. return (status1 != value1 || status2 != value2) ? -1 : 0;
  132. }
  133. /*-----------------------------------------------------------------------
  134. */
  135. int flash_erase (flash_info_t * info, int s_first, int s_last)
  136. {
  137. int flag, prot, sect;
  138. int rc = ERR_OK;
  139. unsigned long base;
  140. unsigned long addr;
  141. if ((info->flash_id & FLASH_VENDMASK) !=
  142. (FUJ_MANUFACT & FLASH_VENDMASK)) {
  143. return ERR_UNKNOWN_FLASH_VENDOR;
  144. }
  145. prot = 0;
  146. for (sect = s_first; sect <= s_last; ++sect) {
  147. if (info->protect[sect]) {
  148. prot++;
  149. }
  150. }
  151. if (prot)
  152. return ERR_PROTECTED;
  153. /*
  154. * Disable interrupts which might cause a timeout
  155. * here. Remember that our exception vectors are
  156. * at address 0 in the flash, and we don't want a
  157. * (ticker) exception to happen while the flash
  158. * chip is in programming mode.
  159. */
  160. flag = disable_interrupts ();
  161. printf ("Erasing %d sectors starting at sector %2d.\n"
  162. "This make take some time ... ",
  163. s_last - s_first, sect);
  164. /* Start erase on unprotected sectors */
  165. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  166. /* ARM simple, non interrupt dependent timer */
  167. reset_timer_masked ();
  168. if (info->protect[sect] == 0) { /* not protected */
  169. addr = sect * MAIN_SECT_SIZE;
  170. addr &= ~(unsigned long) UNALIGNED_MASK; /* word align */
  171. base = addr & 0xF0000000;
  172. FL_WORD (base + (0x555 << 1)) = 0xAA;
  173. FL_WORD (base + (0x2AA << 1)) = 0x55;
  174. FL_WORD (base + (0x555 << 1)) = 0x80;
  175. FL_WORD (base + (0x555 << 1)) = 0xAA;
  176. FL_WORD (base + (0x2AA << 1)) = 0x55;
  177. FL_WORD (addr) = 0x30;
  178. if (flash_status_full_check (addr, 0xFFFF, 0xFFFF))
  179. return ERR_PROTECTED;
  180. }
  181. }
  182. printf ("\nDone.\n");
  183. if (ctrlc ())
  184. printf ("User Interrupt!\n");
  185. /* allow flash to settle - wait 10 ms */
  186. udelay_masked (10000);
  187. if (flag)
  188. enable_interrupts ();
  189. return rc;
  190. }
  191. /*-----------------------------------------------------------------------
  192. * Copy memory to flash
  193. */
  194. static int write_word (flash_info_t * info, ulong dest, ushort data)
  195. {
  196. int flag;
  197. unsigned long base;
  198. /* Check if Flash is (sufficiently) erased
  199. */
  200. if ((FL_WORD (dest) & data) != data)
  201. return ERR_NOT_ERASED;
  202. /*if(dest & UNALIGNED_MASK) return ERR_ALIGN; */
  203. /*
  204. * Disable interrupts which might cause a timeout
  205. * here. Remember that our exception vectors are
  206. * at address 0 in the flash, and we don't want a
  207. * (ticker) exception to happen while the flash
  208. * chip is in programming mode.
  209. */
  210. flag = disable_interrupts ();
  211. /* arm simple, non interrupt dependent timer */
  212. reset_timer_masked ();
  213. base = dest & 0xF0000000;
  214. FL_WORD (base + (0x555 << 1)) = 0xAA;
  215. FL_WORD (base + (0x2AA << 1)) = 0x55;
  216. FL_WORD (base + (0x555 << 1)) = 0xA0;
  217. FL_WORD (dest) = data;
  218. /*printf("writing 0x%p = 0x%x\n",dest,data); */
  219. if (flash_status_wait (dest, data) != data)
  220. return ERR_PROG_ERROR;
  221. if (flag)
  222. enable_interrupts ();
  223. return ERR_OK;
  224. }
  225. /*-----------------------------------------------------------------------
  226. * Copy memory to flash.
  227. */
  228. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  229. {
  230. ulong cp, wp;
  231. ushort data;
  232. int l;
  233. int i, rc;
  234. wp = (addr & ~1); /* get lower word aligned address */
  235. printf ("Writing %d short data to 0x%p from 0x%p.\n ", cnt, wp, src);
  236. /*
  237. * handle unaligned start bytes
  238. */
  239. if ((l = addr - wp) != 0) {
  240. data = 0;
  241. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  242. data = (data >> 8) | (*(uchar *) cp << 8);
  243. }
  244. for (; i < 2 && cnt > 0; ++i) {
  245. data = (data >> 8) | (*src++ << 8);
  246. --cnt;
  247. ++cp;
  248. }
  249. for (; cnt == 0 && i < 2; ++i, ++cp) {
  250. data = (data >> 8) | (*(uchar *) cp << 8);
  251. }
  252. if ((rc = write_word (info, wp, data)) != 0) {
  253. return (rc);
  254. }
  255. wp += 2;
  256. }
  257. /*
  258. * handle word aligned part
  259. */
  260. while (cnt >= 2) {
  261. data = *((vu_short *) src);
  262. if ((rc = write_word (info, wp, data)) != 0) {
  263. return (rc);
  264. }
  265. src += 2;
  266. wp += 2;
  267. cnt -= 2;
  268. }
  269. if (cnt == 0) {
  270. printf ("\nDone.\n");
  271. return ERR_OK;
  272. }
  273. /*
  274. * handle unaligned tail bytes
  275. */
  276. data = 0;
  277. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  278. data = (data >> 8) | (*src++ << 8);
  279. --cnt;
  280. }
  281. for (; i < 2; ++i, ++cp) {
  282. data = (data >> 8) | (*(uchar *) cp << 8);
  283. }
  284. return write_word (info, wp, data);
  285. }