flash.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 PHYS_FLASH_SIZE
  27. #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  29. #define CMD_READ_ARRAY 0x000000F0
  30. #define CMD_UNLOCK1 0x000000AA
  31. #define CMD_UNLOCK2 0x00000055
  32. #define CMD_ERASE_SETUP 0x00000080
  33. #define CMD_ERASE_CONFIRM 0x00000030
  34. #define CMD_PROGRAM 0x000000A0
  35. #define CMD_UNLOCK_BYPASS 0x00000020
  36. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
  37. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
  38. #define BIT_ERASE_DONE 0x00000080
  39. #define BIT_RDY_MASK 0x00000080
  40. #define BIT_PROGRAM_ERROR 0x00000020
  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. #if defined(CONFIG_AMD_LV400)
  56. (AMD_MANUFACT & FLASH_VENDMASK) |
  57. (AMD_ID_LV400B & FLASH_TYPEMASK);
  58. #elif defined(CONFIG_AMD_LV800)
  59. (AMD_MANUFACT & FLASH_VENDMASK) |
  60. (AMD_ID_LV800B & FLASH_TYPEMASK);
  61. #else
  62. #error "Unknown flash configured"
  63. #endif
  64. flash_info[i].size = FLASH_BANK_SIZE;
  65. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  66. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  67. if (i == 0)
  68. flashbase = PHYS_FLASH_1;
  69. else
  70. panic("configured to many flash banks!\n");
  71. for (j = 0; j < flash_info[i].sector_count; j++)
  72. {
  73. if (j <= 3)
  74. {
  75. /* 1st one is 16 KB */
  76. if (j == 0)
  77. {
  78. flash_info[i].start[j] = flashbase + 0;
  79. }
  80. /* 2nd and 3rd are both 8 KB */
  81. if ((j == 1) || (j == 2))
  82. {
  83. flash_info[i].start[j] = flashbase + 0x4000 + (j-1)*0x2000;
  84. }
  85. /* 4th 32 KB */
  86. if (j == 3)
  87. {
  88. flash_info[i].start[j] = flashbase + 0x8000;
  89. }
  90. }
  91. else
  92. {
  93. flash_info[i].start[j] = flashbase + (j - 3)*MAIN_SECT_SIZE;
  94. }
  95. }
  96. size += flash_info[i].size;
  97. }
  98. flash_protect(FLAG_PROTECT_SET,
  99. CFG_FLASH_BASE,
  100. CFG_FLASH_BASE + monitor_flash_len - 1,
  101. &flash_info[0]);
  102. flash_protect(FLAG_PROTECT_SET,
  103. CFG_ENV_ADDR,
  104. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  105. &flash_info[0]);
  106. return size;
  107. }
  108. /*-----------------------------------------------------------------------
  109. */
  110. void flash_print_info (flash_info_t *info)
  111. {
  112. int i;
  113. switch (info->flash_id & FLASH_VENDMASK)
  114. {
  115. case (AMD_MANUFACT & FLASH_VENDMASK):
  116. printf("AMD: ");
  117. break;
  118. default:
  119. printf("Unknown Vendor ");
  120. break;
  121. }
  122. switch (info->flash_id & FLASH_TYPEMASK)
  123. {
  124. case (AMD_ID_LV400B & FLASH_TYPEMASK):
  125. printf("1x Amd29LV400BB (4Mbit)\n");
  126. break;
  127. case (AMD_ID_LV800B & FLASH_TYPEMASK):
  128. printf("1x Amd29LV800BB (8Mbit)\n");
  129. break;
  130. default:
  131. printf("Unknown Chip Type\n");
  132. goto Done;
  133. break;
  134. }
  135. printf(" Size: %ld MB in %d Sectors\n",
  136. info->size >> 20, info->sector_count);
  137. printf(" Sector Start Addresses:");
  138. for (i = 0; i < info->sector_count; i++)
  139. {
  140. if ((i % 5) == 0)
  141. {
  142. printf ("\n ");
  143. }
  144. printf (" %08lX%s", info->start[i],
  145. info->protect[i] ? " (RO)" : " ");
  146. }
  147. printf ("\n");
  148. Done:
  149. }
  150. /*-----------------------------------------------------------------------
  151. */
  152. int flash_erase (flash_info_t *info, int s_first, int s_last)
  153. {
  154. ushort result;
  155. int iflag, cflag, prot, sect;
  156. int rc = ERR_OK;
  157. int chip;
  158. /* first look for protection bits */
  159. if (info->flash_id == FLASH_UNKNOWN)
  160. return ERR_UNKNOWN_FLASH_TYPE;
  161. if ((s_first < 0) || (s_first > s_last)) {
  162. return ERR_INVAL;
  163. }
  164. if ((info->flash_id & FLASH_VENDMASK) !=
  165. (AMD_MANUFACT & FLASH_VENDMASK)) {
  166. return ERR_UNKNOWN_FLASH_VENDOR;
  167. }
  168. prot = 0;
  169. for (sect=s_first; sect<=s_last; ++sect) {
  170. if (info->protect[sect]) {
  171. prot++;
  172. }
  173. }
  174. if (prot)
  175. return ERR_PROTECTED;
  176. /*
  177. * Disable interrupts which might cause a timeout
  178. * here. Remember that our exception vectors are
  179. * at address 0 in the flash, and we don't want a
  180. * (ticker) exception to happen while the flash
  181. * chip is in programming mode.
  182. */
  183. cflag = icache_status();
  184. icache_disable();
  185. iflag = disable_interrupts();
  186. /* Start erase on unprotected sectors */
  187. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  188. {
  189. printf("Erasing sector %2d ... ", sect);
  190. /* arm simple, non interrupt dependent timer */
  191. reset_timer_masked();
  192. if (info->protect[sect] == 0)
  193. { /* not protected */
  194. vu_short *addr = (vu_short *)(info->start[sect]);
  195. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  196. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  197. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  198. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  199. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  200. *addr = CMD_ERASE_CONFIRM;
  201. /* wait until flash is ready */
  202. chip = 0;
  203. do
  204. {
  205. result = *addr;
  206. /* check timeout */
  207. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  208. {
  209. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  210. chip = TMO;
  211. break;
  212. }
  213. if (!chip && (result & 0xFFFF) & BIT_ERASE_DONE)
  214. chip = READY;
  215. if (!chip && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  216. chip = ERR;
  217. } while (!chip);
  218. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  219. if (chip == ERR)
  220. {
  221. rc = ERR_PROG_ERROR;
  222. goto outahere;
  223. }
  224. if (chip == TMO)
  225. {
  226. rc = ERR_TIMOUT;
  227. goto outahere;
  228. }
  229. printf("ok.\n");
  230. }
  231. else /* it was protected */
  232. {
  233. printf("protected!\n");
  234. }
  235. }
  236. if (ctrlc())
  237. printf("User Interrupt!\n");
  238. outahere:
  239. /* allow flash to settle - wait 10 ms */
  240. udelay_masked(10000);
  241. if (iflag)
  242. enable_interrupts();
  243. if (cflag)
  244. icache_enable();
  245. return rc;
  246. }
  247. /*-----------------------------------------------------------------------
  248. * Copy memory to flash
  249. */
  250. volatile static int write_hword (flash_info_t *info, ulong dest, ushort data)
  251. {
  252. vu_short *addr = (vu_short *)dest;
  253. ushort result;
  254. int rc = ERR_OK;
  255. int cflag, iflag;
  256. int chip;
  257. /*
  258. * Check if Flash is (sufficiently) erased
  259. */
  260. result = *addr;
  261. if ((result & data) != data)
  262. return ERR_NOT_ERASED;
  263. /*
  264. * Disable interrupts which might cause a timeout
  265. * here. Remember that our exception vectors are
  266. * at address 0 in the flash, and we don't want a
  267. * (ticker) exception to happen while the flash
  268. * chip is in programming mode.
  269. */
  270. cflag = icache_status();
  271. icache_disable();
  272. iflag = disable_interrupts();
  273. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  274. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  275. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  276. *addr = CMD_PROGRAM;
  277. *addr = data;
  278. /* arm simple, non interrupt dependent timer */
  279. reset_timer_masked();
  280. /* wait until flash is ready */
  281. chip = 0;
  282. do
  283. {
  284. result = *addr;
  285. /* check timeout */
  286. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  287. {
  288. chip = ERR | TMO;
  289. break;
  290. }
  291. if (!chip && ((result & 0x80) == (data & 0x80)))
  292. chip = READY;
  293. if (!chip && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
  294. {
  295. result = *addr;
  296. if ((result & 0x80) == (data & 0x80))
  297. chip = READY;
  298. else
  299. chip = ERR;
  300. }
  301. } while (!chip);
  302. *addr = CMD_READ_ARRAY;
  303. if (chip == ERR || *addr != data)
  304. rc = ERR_PROG_ERROR;
  305. if (iflag)
  306. enable_interrupts();
  307. if (cflag)
  308. icache_enable();
  309. return rc;
  310. }
  311. /*-----------------------------------------------------------------------
  312. * Copy memory to flash.
  313. */
  314. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  315. {
  316. ulong cp, wp;
  317. int l;
  318. int i, rc;
  319. ushort data;
  320. wp = (addr & ~1); /* get lower word aligned address */
  321. /*
  322. * handle unaligned start bytes
  323. */
  324. if ((l = addr - wp) != 0) {
  325. data = 0;
  326. for (i=0, cp=wp; i<l; ++i, ++cp) {
  327. data = (data >> 8) | (*(uchar *)cp << 8);
  328. }
  329. for (; i<2 && cnt>0; ++i) {
  330. data = (data >> 8) | (*src++ << 8);
  331. --cnt;
  332. ++cp;
  333. }
  334. for (; cnt==0 && i<2; ++i, ++cp) {
  335. data = (data >> 8) | (*(uchar *)cp << 8);
  336. }
  337. if ((rc = write_hword(info, wp, data)) != 0) {
  338. return (rc);
  339. }
  340. wp += 2;
  341. }
  342. /*
  343. * handle word aligned part
  344. */
  345. while (cnt >= 2) {
  346. data = *((vu_short*)src);
  347. if ((rc = write_hword(info, wp, data)) != 0) {
  348. return (rc);
  349. }
  350. src += 2;
  351. wp += 2;
  352. cnt -= 2;
  353. }
  354. if (cnt == 0) {
  355. return ERR_OK;
  356. }
  357. /*
  358. * handle unaligned tail bytes
  359. */
  360. data = 0;
  361. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  362. data = (data >> 8) | (*src++ << 8);
  363. --cnt;
  364. }
  365. for (; i<2; ++i, ++cp) {
  366. data = (data >> 8) | (*(uchar *)cp << 8);
  367. }
  368. return write_hword(info, wp, data);
  369. }