flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. * Inferno is complicated, it's hardware locked
  94. */
  95. #ifdef CONFIG_INFERNO
  96. /* first one, 0x00000 to 0x07fff */
  97. flash_protect(FLAG_PROTECT_SET,
  98. CFG_FLASH_BASE + 0x00000,
  99. CFG_FLASH_BASE + 0x08000 - 1,
  100. &flash_info[0]);
  101. /* third to 10th, 0x0c000 - 0xdffff */
  102. flash_protect(FLAG_PROTECT_SET,
  103. CFG_FLASH_BASE + 0x0c000,
  104. CFG_FLASH_BASE + 0xe0000 - 1,
  105. &flash_info[0]);
  106. #else
  107. flash_protect(FLAG_PROTECT_SET,
  108. CFG_FLASH_BASE,
  109. CFG_FLASH_BASE + monitor_flash_len - 1,
  110. &flash_info[0]);
  111. flash_protect(FLAG_PROTECT_SET,
  112. CFG_ENV_ADDR,
  113. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  114. &flash_info[0]);
  115. #endif
  116. return size;
  117. }
  118. /*-----------------------------------------------------------------------
  119. */
  120. void flash_print_info (flash_info_t *info)
  121. {
  122. int i;
  123. switch (info->flash_id & FLASH_VENDMASK)
  124. {
  125. case (AMD_MANUFACT & FLASH_VENDMASK):
  126. printf("AMD: ");
  127. break;
  128. default:
  129. printf("Unknown Vendor ");
  130. break;
  131. }
  132. switch (info->flash_id & FLASH_TYPEMASK)
  133. {
  134. case (AMD_ID_LV160B & FLASH_TYPEMASK):
  135. printf("2x Amd29F160BB (16Mbit)\n");
  136. break;
  137. default:
  138. printf("Unknown Chip Type\n");
  139. goto Done;
  140. break;
  141. }
  142. printf(" Size: %ld MB in %d Sectors\n",
  143. info->size >> 20, info->sector_count);
  144. printf(" Sector Start Addresses:");
  145. for (i = 0; i < info->sector_count; i++)
  146. {
  147. if ((i % 5) == 0)
  148. {
  149. printf ("\n ");
  150. }
  151. printf (" %08lX%s", info->start[i],
  152. info->protect[i] ? " (RO)" : " ");
  153. }
  154. printf ("\n");
  155. Done:
  156. }
  157. /*-----------------------------------------------------------------------
  158. */
  159. int flash_erase (flash_info_t *info, int s_first, int s_last)
  160. {
  161. ulong result;
  162. int iflag, cflag, prot, sect;
  163. int rc = ERR_OK;
  164. int chip1, chip2;
  165. /* first look for protection bits */
  166. if (info->flash_id == FLASH_UNKNOWN)
  167. return ERR_UNKNOWN_FLASH_TYPE;
  168. if ((s_first < 0) || (s_first > s_last)) {
  169. return ERR_INVAL;
  170. }
  171. if ((info->flash_id & FLASH_VENDMASK) !=
  172. (AMD_MANUFACT & FLASH_VENDMASK)) {
  173. return ERR_UNKNOWN_FLASH_VENDOR;
  174. }
  175. prot = 0;
  176. for (sect=s_first; sect<=s_last; ++sect) {
  177. if (info->protect[sect]) {
  178. prot++;
  179. }
  180. }
  181. if (prot)
  182. return ERR_PROTECTED;
  183. /*
  184. * Disable interrupts which might cause a timeout
  185. * here. Remember that our exception vectors are
  186. * at address 0 in the flash, and we don't want a
  187. * (ticker) exception to happen while the flash
  188. * chip is in programming mode.
  189. */
  190. cflag = icache_status();
  191. icache_disable();
  192. iflag = disable_interrupts();
  193. /* Start erase on unprotected sectors */
  194. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  195. {
  196. printf("Erasing sector %2d ... ", sect);
  197. /* arm simple, non interrupt dependent timer */
  198. reset_timer_masked();
  199. if (info->protect[sect] == 0)
  200. { /* not protected */
  201. vu_long *addr = (vu_long *)(info->start[sect]);
  202. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  203. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  204. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  205. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  206. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  207. *addr = CMD_ERASE_CONFIRM;
  208. /* wait until flash is ready */
  209. chip1 = chip2 = 0;
  210. do
  211. {
  212. result = *addr;
  213. /* check timeout */
  214. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  215. {
  216. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  217. chip1 = TMO;
  218. break;
  219. }
  220. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  221. chip1 = READY;
  222. if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  223. chip1 = ERR;
  224. if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
  225. chip2 = READY;
  226. if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
  227. chip2 = ERR;
  228. } while (!chip1 || !chip2);
  229. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  230. if (chip1 == ERR || chip2 == ERR)
  231. {
  232. rc = ERR_PROG_ERROR;
  233. goto outahere;
  234. }
  235. if (chip1 == TMO)
  236. {
  237. rc = ERR_TIMOUT;
  238. goto outahere;
  239. }
  240. printf("ok.\n");
  241. }
  242. else /* it was protected */
  243. {
  244. printf("protected!\n");
  245. }
  246. }
  247. if (ctrlc())
  248. printf("User Interrupt!\n");
  249. outahere:
  250. /* allow flash to settle - wait 10 ms */
  251. udelay_masked(10000);
  252. if (iflag)
  253. enable_interrupts();
  254. if (cflag)
  255. icache_enable();
  256. return rc;
  257. }
  258. /*-----------------------------------------------------------------------
  259. * Copy memory to flash
  260. */
  261. volatile static int write_word (flash_info_t *info, ulong dest, ulong data)
  262. {
  263. vu_long *addr = (vu_long *)dest;
  264. ulong result;
  265. int rc = ERR_OK;
  266. int cflag, iflag;
  267. int chip1, chip2;
  268. /*
  269. * Check if Flash is (sufficiently) erased
  270. */
  271. result = *addr;
  272. if ((result & data) != data)
  273. return ERR_NOT_ERASED;
  274. /*
  275. * Disable interrupts which might cause a timeout
  276. * here. Remember that our exception vectors are
  277. * at address 0 in the flash, and we don't want a
  278. * (ticker) exception to happen while the flash
  279. * chip is in programming mode.
  280. */
  281. cflag = icache_status();
  282. icache_disable();
  283. iflag = disable_interrupts();
  284. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  285. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  286. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  287. *addr = CMD_PROGRAM;
  288. *addr = data;
  289. /* arm simple, non interrupt dependent timer */
  290. reset_timer_masked();
  291. /* wait until flash is ready */
  292. chip1 = chip2 = 0;
  293. do
  294. {
  295. result = *addr;
  296. /* check timeout */
  297. if (get_timer_masked() > CFG_FLASH_ERASE_TOUT)
  298. {
  299. chip1 = ERR | TMO;
  300. break;
  301. }
  302. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  303. chip1 = READY;
  304. if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
  305. {
  306. result = *addr;
  307. if ((result & 0x80) == (data & 0x80))
  308. chip1 = READY;
  309. else
  310. chip1 = ERR;
  311. }
  312. if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
  313. chip2 = READY;
  314. if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR))
  315. {
  316. result = *addr;
  317. if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
  318. chip2 = READY;
  319. else
  320. chip2 = ERR;
  321. }
  322. } while (!chip1 || !chip2);
  323. *addr = CMD_READ_ARRAY;
  324. if (chip1 == ERR || chip2 == ERR || *addr != data)
  325. rc = ERR_PROG_ERROR;
  326. if (iflag)
  327. enable_interrupts();
  328. if (cflag)
  329. icache_enable();
  330. return rc;
  331. }
  332. /*-----------------------------------------------------------------------
  333. * Copy memory to flash.
  334. */
  335. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  336. {
  337. ulong cp, wp, data;
  338. int l;
  339. int i, rc;
  340. wp = (addr & ~3); /* get lower word aligned address */
  341. /*
  342. * handle unaligned start bytes
  343. */
  344. if ((l = addr - wp) != 0) {
  345. data = 0;
  346. for (i=0, cp=wp; i<l; ++i, ++cp) {
  347. data = (data >> 8) | (*(uchar *)cp << 24);
  348. }
  349. for (; i<4 && cnt>0; ++i) {
  350. data = (data >> 8) | (*src++ << 24);
  351. --cnt;
  352. ++cp;
  353. }
  354. for (; cnt==0 && i<4; ++i, ++cp) {
  355. data = (data >> 8) | (*(uchar *)cp << 24);
  356. }
  357. if ((rc = write_word(info, wp, data)) != 0) {
  358. return (rc);
  359. }
  360. wp += 4;
  361. }
  362. /*
  363. * handle word aligned part
  364. */
  365. while (cnt >= 4) {
  366. data = *((vu_long*)src);
  367. if ((rc = write_word(info, wp, data)) != 0) {
  368. return (rc);
  369. }
  370. src += 4;
  371. wp += 4;
  372. cnt -= 4;
  373. }
  374. if (cnt == 0) {
  375. return ERR_OK;
  376. }
  377. /*
  378. * handle unaligned tail bytes
  379. */
  380. data = 0;
  381. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  382. data = (data >> 8) | (*src++ << 24);
  383. --cnt;
  384. }
  385. for (; i<4; ++i, ++cp) {
  386. data = (data >> 8) | (*(uchar *)cp << 24);
  387. }
  388. return write_word(info, wp, data);
  389. }