flash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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[CONFIG_SYS_MAX_FLASH_BANKS];
  36. /*-----------------------------------------------------------------------
  37. */
  38. ulong flash_init (void)
  39. {
  40. int i, j;
  41. ulong size = 0;
  42. for (i = 0; i < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  48. memset (flash_info[i].protect, 0, CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  63. CONFIG_SYS_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. ulong start;
  144. if ((info->flash_id & FLASH_VENDMASK) !=
  145. (FUJ_MANUFACT & FLASH_VENDMASK)) {
  146. return ERR_UNKNOWN_FLASH_VENDOR;
  147. }
  148. prot = 0;
  149. for (sect = s_first; sect <= s_last; ++sect) {
  150. if (info->protect[sect]) {
  151. prot++;
  152. }
  153. }
  154. if (prot)
  155. return ERR_PROTECTED;
  156. /*
  157. * Disable interrupts which might cause a timeout
  158. * here. Remember that our exception vectors are
  159. * at address 0 in the flash, and we don't want a
  160. * (ticker) exception to happen while the flash
  161. * chip is in programming mode.
  162. */
  163. flag = disable_interrupts ();
  164. printf ("Erasing %d sectors starting at sector %2d.\n"
  165. "This make take some time ... ",
  166. s_last - s_first, sect);
  167. /* Start erase on unprotected sectors */
  168. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  169. /* ARM simple, non interrupt dependent timer */
  170. start = get_timer(0);
  171. if (info->protect[sect] == 0) { /* not protected */
  172. addr = sect * MAIN_SECT_SIZE;
  173. addr &= ~(unsigned long) UNALIGNED_MASK; /* word align */
  174. base = addr & 0xF0000000;
  175. FL_WORD (base + (0x555 << 1)) = 0xAA;
  176. FL_WORD (base + (0x2AA << 1)) = 0x55;
  177. FL_WORD (base + (0x555 << 1)) = 0x80;
  178. FL_WORD (base + (0x555 << 1)) = 0xAA;
  179. FL_WORD (base + (0x2AA << 1)) = 0x55;
  180. FL_WORD (addr) = 0x30;
  181. if (flash_status_full_check (addr, 0xFFFF, 0xFFFF))
  182. return ERR_PROTECTED;
  183. }
  184. }
  185. printf ("\nDone.\n");
  186. if (ctrlc ())
  187. printf ("User Interrupt!\n");
  188. /* allow flash to settle - wait 10 ms */
  189. udelay_masked (10000);
  190. if (flag)
  191. enable_interrupts ();
  192. return rc;
  193. }
  194. /*-----------------------------------------------------------------------
  195. * Copy memory to flash
  196. */
  197. static int write_word (flash_info_t * info, ulong dest, ushort data)
  198. {
  199. int flag;
  200. unsigned long base;
  201. ulong start;
  202. /* Check if Flash is (sufficiently) erased
  203. */
  204. if ((FL_WORD (dest) & data) != data)
  205. return ERR_NOT_ERASED;
  206. /*if(dest & UNALIGNED_MASK) return ERR_ALIGN; */
  207. /*
  208. * Disable interrupts which might cause a timeout
  209. * here. Remember that our exception vectors are
  210. * at address 0 in the flash, and we don't want a
  211. * (ticker) exception to happen while the flash
  212. * chip is in programming mode.
  213. */
  214. flag = disable_interrupts ();
  215. /* arm simple, non interrupt dependent timer */
  216. start = get_timer(0);
  217. base = dest & 0xF0000000;
  218. FL_WORD (base + (0x555 << 1)) = 0xAA;
  219. FL_WORD (base + (0x2AA << 1)) = 0x55;
  220. FL_WORD (base + (0x555 << 1)) = 0xA0;
  221. FL_WORD (dest) = data;
  222. /*printf("writing 0x%p = 0x%x\n",dest,data); */
  223. if (flash_status_wait (dest, data) != data)
  224. return ERR_PROG_ERROR;
  225. if (flag)
  226. enable_interrupts ();
  227. return ERR_OK;
  228. }
  229. /*-----------------------------------------------------------------------
  230. * Copy memory to flash.
  231. */
  232. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  233. {
  234. ulong cp, wp;
  235. ushort data;
  236. int l;
  237. int i, rc;
  238. wp = (addr & ~1); /* get lower word aligned address */
  239. printf ("Writing %lu short data to 0x%lx from 0x%p.\n ", cnt, wp, src);
  240. /*
  241. * handle unaligned start bytes
  242. */
  243. if ((l = addr - wp) != 0) {
  244. data = 0;
  245. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  246. data = (data >> 8) | (*(uchar *) cp << 8);
  247. }
  248. for (; i < 2 && cnt > 0; ++i) {
  249. data = (data >> 8) | (*src++ << 8);
  250. --cnt;
  251. ++cp;
  252. }
  253. for (; cnt == 0 && i < 2; ++i, ++cp) {
  254. data = (data >> 8) | (*(uchar *) cp << 8);
  255. }
  256. if ((rc = write_word (info, wp, data)) != 0) {
  257. return (rc);
  258. }
  259. wp += 2;
  260. }
  261. /*
  262. * handle word aligned part
  263. */
  264. while (cnt >= 2) {
  265. data = *((vu_short *) src);
  266. if ((rc = write_word (info, wp, data)) != 0) {
  267. return (rc);
  268. }
  269. src += 2;
  270. wp += 2;
  271. cnt -= 2;
  272. }
  273. if (cnt == 0) {
  274. printf ("\nDone.\n");
  275. return ERR_OK;
  276. }
  277. /*
  278. * handle unaligned tail bytes
  279. */
  280. data = 0;
  281. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  282. data = (data >> 8) | (*src++ << 8);
  283. --cnt;
  284. }
  285. for (; i < 2; ++i, ++cp) {
  286. data = (data >> 8) | (*(uchar *) cp << 8);
  287. }
  288. return write_word (info, wp, data);
  289. }