flash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Alex Zuepke <azu@sysgo.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. ulong myflush(void);
  26. #define FLASH_BANK_SIZE 0x400000 /* 4 MB */
  27. #define MAIN_SECT_SIZE 0x20000 /* 128 KB */
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  29. #define CMD_READ_ARRAY 0x00F000F0
  30. #define CMD_UNLOCK1 0x00AA00AA
  31. #define CMD_UNLOCK2 0x00550055
  32. #define CMD_ERASE_SETUP 0x00800080
  33. #define CMD_ERASE_CONFIRM 0x00300030
  34. #define CMD_PROGRAM 0x00A000A0
  35. #define CMD_UNLOCK_BYPASS 0x00200020
  36. #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
  37. #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
  38. #define BIT_ERASE_DONE 0x00800080
  39. #define BIT_RDY_MASK 0x00800080
  40. #define BIT_PROGRAM_ERROR 0x00200020
  41. #define BIT_TIMEOUT 0x80000000 /* our flag */
  42. #define READY 1
  43. #define ERR 2
  44. #define TMO 4
  45. /*-----------------------------------------------------------------------
  46. */
  47. ulong flash_init(void)
  48. {
  49. int i, j;
  50. ulong size = 0;
  51. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
  52. {
  53. ulong flashbase = 0;
  54. flash_info[i].flash_id =
  55. (AMD_MANUFACT & FLASH_VENDMASK) |
  56. (AMD_ID_LV160B & FLASH_TYPEMASK);
  57. flash_info[i].size = FLASH_BANK_SIZE;
  58. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  59. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  60. if (i == 0)
  61. flashbase = PHYS_FLASH_1;
  62. else
  63. panic("configured to many flash banks!\n");
  64. for (j = 0; j < flash_info[i].sector_count; j++)
  65. {
  66. if (j <= 3)
  67. {
  68. /* 1st one is 32 KB */
  69. if (j == 0)
  70. {
  71. flash_info[i].start[j] = flashbase + 0;
  72. }
  73. /* 2nd and 3rd are both 16 KB */
  74. if ((j == 1) || (j == 2))
  75. {
  76. flash_info[i].start[j] = flashbase + 0x8000 + (j-1)*0x4000;
  77. }
  78. /* 4th 64 KB */
  79. if (j == 3)
  80. {
  81. flash_info[i].start[j] = flashbase + 0x10000;
  82. }
  83. }
  84. else
  85. {
  86. flash_info[i].start[j] = flashbase + (j - 3)*MAIN_SECT_SIZE;
  87. }
  88. }
  89. size += flash_info[i].size;
  90. }
  91. /*
  92. * Protect monitor and environment sectors
  93. */
  94. flash_protect(FLAG_PROTECT_SET,
  95. i386boot_start-CFG_FLASH_BASE,
  96. i386boot_end-CFG_FLASH_BASE,
  97. &flash_info[0]);
  98. flash_protect(FLAG_PROTECT_SET,
  99. CFG_ENV_ADDR,
  100. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  101. &flash_info[0]);
  102. return size;
  103. }
  104. /*-----------------------------------------------------------------------
  105. */
  106. void flash_print_info (flash_info_t *info)
  107. {
  108. int i;
  109. switch (info->flash_id & FLASH_VENDMASK)
  110. {
  111. case (AMD_MANUFACT & FLASH_VENDMASK):
  112. printf("AMD: ");
  113. break;
  114. default:
  115. printf("Unknown Vendor ");
  116. break;
  117. }
  118. switch (info->flash_id & FLASH_TYPEMASK)
  119. {
  120. case (AMD_ID_LV160B & FLASH_TYPEMASK):
  121. printf("2x Amd29F160BB (16Mbit)\n");
  122. break;
  123. default:
  124. printf("Unknown Chip Type\n");
  125. goto Done;
  126. break;
  127. }
  128. printf(" Size: %ld MB in %d Sectors\n",
  129. info->size >> 20, info->sector_count);
  130. printf(" Sector Start Addresses:");
  131. for (i = 0; i < info->sector_count; i++)
  132. {
  133. if ((i % 5) == 0)
  134. {
  135. printf ("\n ");
  136. }
  137. printf (" %08lX%s", info->start[i],
  138. info->protect[i] ? " (RO)" : " ");
  139. }
  140. printf ("\n");
  141. Done:
  142. }
  143. /*-----------------------------------------------------------------------
  144. */
  145. int flash_erase (flash_info_t *info, int s_first, int s_last)
  146. {
  147. ulong result;
  148. int iflag, cflag, prot, sect;
  149. int rc = ERR_OK;
  150. int chip1, chip2;
  151. /* first look for protection bits */
  152. if (info->flash_id == FLASH_UNKNOWN)
  153. return ERR_UNKNOWN_FLASH_TYPE;
  154. if ((s_first < 0) || (s_first > s_last)) {
  155. return ERR_INVAL;
  156. }
  157. if ((info->flash_id & FLASH_VENDMASK) !=
  158. (AMD_MANUFACT & FLASH_VENDMASK)) {
  159. return ERR_UNKNOWN_FLASH_VENDOR;
  160. }
  161. prot = 0;
  162. for (sect=s_first; sect<=s_last; ++sect) {
  163. if (info->protect[sect]) {
  164. prot++;
  165. }
  166. }
  167. if (prot)
  168. return ERR_PROTECTED;
  169. /*
  170. * Disable interrupts which might cause a timeout
  171. * here. Remember that our exception vectors are
  172. * at address 0 in the flash, and we don't want a
  173. * (ticker) exception to happen while the flash
  174. * chip is in programming mode.
  175. */
  176. iflag = disable_interrupts();
  177. /* Start erase on unprotected sectors */
  178. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  179. {
  180. printf("Erasing sector %2d ... ", sect);
  181. /* arm simple, non interrupt dependent timer */
  182. reset_timer();
  183. if (info->protect[sect] == 0)
  184. { /* not protected */
  185. vu_long *addr = (vu_long *)(info->start[sect]);
  186. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  187. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  188. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  189. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  190. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  191. *addr = CMD_ERASE_CONFIRM;
  192. /* wait until flash is ready */
  193. chip1 = chip2 = 0;
  194. do
  195. {
  196. result = *addr;
  197. /* check timeout */
  198. if (get_timer(0) > CFG_FLASH_ERASE_TOUT)
  199. {
  200. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  201. chip1 = TMO;
  202. break;
  203. }
  204. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  205. chip1 = READY;
  206. if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  207. chip1 = ERR;
  208. if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
  209. chip2 = READY;
  210. if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
  211. chip2 = ERR;
  212. } while (!chip1 || !chip2);
  213. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  214. if (chip1 == ERR || chip2 == ERR)
  215. {
  216. rc = ERR_PROG_ERROR;
  217. goto outahere;
  218. }
  219. if (chip1 == TMO)
  220. {
  221. rc = ERR_TIMOUT;
  222. goto outahere;
  223. }
  224. printf("ok.\n");
  225. }
  226. else /* it was protected */
  227. {
  228. printf("protected!\n");
  229. }
  230. }
  231. if (ctrlc())
  232. printf("User Interrupt!\n");
  233. outahere:
  234. /* allow flash to settle - wait 10 ms */
  235. udelay(10000);
  236. if (iflag)
  237. enable_interrupts();
  238. return rc;
  239. }
  240. /*-----------------------------------------------------------------------
  241. * Copy memory to flash
  242. */
  243. volatile static int write_word (flash_info_t *info, ulong dest, ulong data)
  244. {
  245. vu_long *addr = (vu_long *)dest;
  246. ulong result;
  247. int rc = ERR_OK;
  248. int iflag;
  249. int chip1, chip2;
  250. /*
  251. * Check if Flash is (sufficiently) erased
  252. */
  253. result = *addr;
  254. if ((result & data) != data)
  255. return ERR_NOT_ERASED;
  256. /*
  257. * Disable interrupts which might cause a timeout
  258. * here. Remember that our exception vectors are
  259. * at address 0 in the flash, and we don't want a
  260. * (ticker) exception to happen while the flash
  261. * chip is in programming mode.
  262. */
  263. iflag = disable_interrupts();
  264. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  265. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  266. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  267. *addr = CMD_PROGRAM;
  268. *addr = data;
  269. /* arm simple, non interrupt dependent timer */
  270. reset_timer();
  271. /* wait until flash is ready */
  272. chip1 = chip2 = 0;
  273. do
  274. {
  275. result = *addr;
  276. /* check timeout */
  277. if (get_timer(0) > CFG_FLASH_ERASE_TOUT)
  278. {
  279. chip1 = ERR | TMO;
  280. break;
  281. }
  282. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  283. chip1 = READY;
  284. if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
  285. {
  286. result = *addr;
  287. if ((result & 0x80) == (data & 0x80))
  288. chip1 = READY;
  289. else
  290. chip1 = ERR;
  291. }
  292. if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
  293. chip2 = READY;
  294. if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR))
  295. {
  296. result = *addr;
  297. if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
  298. chip2 = READY;
  299. else
  300. chip2 = ERR;
  301. }
  302. } while (!chip1 || !chip2);
  303. *addr = CMD_READ_ARRAY;
  304. if (chip1 == ERR || chip2 == ERR || *addr != data)
  305. rc = ERR_PROG_ERROR;
  306. if (iflag)
  307. enable_interrupts();
  308. return rc;
  309. }
  310. /*-----------------------------------------------------------------------
  311. * Copy memory to flash.
  312. */
  313. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  314. {
  315. ulong cp, wp, data;
  316. int l;
  317. int i, rc;
  318. wp = (addr & ~3); /* get lower word aligned address */
  319. /*
  320. * handle unaligned start bytes
  321. */
  322. if ((l = addr - wp) != 0) {
  323. data = 0;
  324. for (i=0, cp=wp; i<l; ++i, ++cp) {
  325. data = (data >> 8) | (*(uchar *)cp << 24);
  326. }
  327. for (; i<4 && cnt>0; ++i) {
  328. data = (data >> 8) | (*src++ << 24);
  329. --cnt;
  330. ++cp;
  331. }
  332. for (; cnt==0 && i<4; ++i, ++cp) {
  333. data = (data >> 8) | (*(uchar *)cp << 24);
  334. }
  335. if ((rc = write_word(info, wp, data)) != 0) {
  336. return (rc);
  337. }
  338. wp += 4;
  339. }
  340. /*
  341. * handle word aligned part
  342. */
  343. while (cnt >= 4) {
  344. data = *((vu_long*)src);
  345. if ((rc = write_word(info, wp, data)) != 0) {
  346. return (rc);
  347. }
  348. src += 4;
  349. wp += 4;
  350. cnt -= 4;
  351. }
  352. if (cnt == 0) {
  353. return ERR_OK;
  354. }
  355. /*
  356. * handle unaligned tail bytes
  357. */
  358. data = 0;
  359. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  360. data = (data >> 8) | (*src++ << 24);
  361. --cnt;
  362. }
  363. for (; i<4; ++i, ++cp) {
  364. data = (data >> 8) | (*(uchar *)cp << 24);
  365. }
  366. return write_word(info, wp, data);
  367. }