flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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[CONFIG_SYS_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 *)(CONFIG_SYS_FLASH_BASE + (0x00000555 << 2)))
  37. #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CONFIG_SYS_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 < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  59. memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  60. if (i == 0)
  61. flashbase = PHYS_FLASH_1;
  62. else
  63. panic("configured too 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. CONFIG_SYS_FLASH_BASE + 0x00000,
  99. CONFIG_SYS_FLASH_BASE + 0x08000 - 1,
  100. &flash_info[0]);
  101. /* third to 10th, 0x0c000 - 0xdffff */
  102. flash_protect(FLAG_PROTECT_SET,
  103. CONFIG_SYS_FLASH_BASE + 0x0c000,
  104. CONFIG_SYS_FLASH_BASE + 0xe0000 - 1,
  105. &flash_info[0]);
  106. #else
  107. flash_protect(FLAG_PROTECT_SET,
  108. CONFIG_SYS_FLASH_BASE,
  109. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  110. &flash_info[0]);
  111. flash_protect(FLAG_PROTECT_SET,
  112. CONFIG_ENV_ADDR,
  113. CONFIG_ENV_ADDR + CONFIG_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. */
  160. int flash_erase (flash_info_t *info, int s_first, int s_last)
  161. {
  162. ulong result;
  163. int iflag, cflag, prot, sect;
  164. int rc = ERR_OK;
  165. int chip1, chip2;
  166. ulong start;
  167. /* first look for protection bits */
  168. if (info->flash_id == FLASH_UNKNOWN)
  169. return ERR_UNKNOWN_FLASH_TYPE;
  170. if ((s_first < 0) || (s_first > s_last)) {
  171. return ERR_INVAL;
  172. }
  173. if ((info->flash_id & FLASH_VENDMASK) !=
  174. (AMD_MANUFACT & FLASH_VENDMASK)) {
  175. return ERR_UNKNOWN_FLASH_VENDOR;
  176. }
  177. prot = 0;
  178. for (sect=s_first; sect<=s_last; ++sect) {
  179. if (info->protect[sect]) {
  180. prot++;
  181. }
  182. }
  183. if (prot)
  184. return ERR_PROTECTED;
  185. /*
  186. * Disable interrupts which might cause a timeout
  187. * here. Remember that our exception vectors are
  188. * at address 0 in the flash, and we don't want a
  189. * (ticker) exception to happen while the flash
  190. * chip is in programming mode.
  191. */
  192. cflag = icache_status();
  193. icache_disable();
  194. iflag = disable_interrupts();
  195. /* Start erase on unprotected sectors */
  196. for (sect = s_first; sect<=s_last && !ctrlc(); sect++)
  197. {
  198. printf("Erasing sector %2d ... ", sect);
  199. /* arm simple, non interrupt dependent timer */
  200. start = get_timer(0);
  201. if (info->protect[sect] == 0)
  202. { /* not protected */
  203. vu_long *addr = (vu_long *)(info->start[sect]);
  204. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  205. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  206. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  207. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  208. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  209. *addr = CMD_ERASE_CONFIRM;
  210. /* wait until flash is ready */
  211. chip1 = chip2 = 0;
  212. do
  213. {
  214. result = *addr;
  215. /* check timeout */
  216. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT)
  217. {
  218. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  219. chip1 = TMO;
  220. break;
  221. }
  222. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  223. chip1 = READY;
  224. if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  225. chip1 = ERR;
  226. if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
  227. chip2 = READY;
  228. if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
  229. chip2 = ERR;
  230. } while (!chip1 || !chip2);
  231. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  232. if (chip1 == ERR || chip2 == ERR)
  233. {
  234. rc = ERR_PROG_ERROR;
  235. goto outahere;
  236. }
  237. if (chip1 == TMO)
  238. {
  239. rc = ERR_TIMOUT;
  240. goto outahere;
  241. }
  242. printf("ok.\n");
  243. }
  244. else /* it was protected */
  245. {
  246. printf("protected!\n");
  247. }
  248. }
  249. if (ctrlc())
  250. printf("User Interrupt!\n");
  251. outahere:
  252. /* allow flash to settle - wait 10 ms */
  253. udelay_masked(10000);
  254. if (iflag)
  255. enable_interrupts();
  256. if (cflag)
  257. icache_enable();
  258. return rc;
  259. }
  260. /*-----------------------------------------------------------------------
  261. * Copy memory to flash
  262. */
  263. static int write_word (flash_info_t *info, ulong dest, ulong data)
  264. {
  265. vu_long *addr = (vu_long *)dest;
  266. ulong result;
  267. int rc = ERR_OK;
  268. int cflag, iflag;
  269. int chip1, chip2;
  270. ulong start;
  271. /*
  272. * Check if Flash is (sufficiently) erased
  273. */
  274. result = *addr;
  275. if ((result & data) != data)
  276. return ERR_NOT_ERASED;
  277. /*
  278. * Disable interrupts which might cause a timeout
  279. * here. Remember that our exception vectors are
  280. * at address 0 in the flash, and we don't want a
  281. * (ticker) exception to happen while the flash
  282. * chip is in programming mode.
  283. */
  284. cflag = icache_status();
  285. icache_disable();
  286. iflag = disable_interrupts();
  287. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  288. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  289. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  290. *addr = CMD_PROGRAM;
  291. *addr = data;
  292. /* arm simple, non interrupt dependent timer */
  293. start = get_timer(0);
  294. /* wait until flash is ready */
  295. chip1 = chip2 = 0;
  296. do
  297. {
  298. result = *addr;
  299. /* check timeout */
  300. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT)
  301. {
  302. chip1 = ERR | TMO;
  303. break;
  304. }
  305. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  306. chip1 = READY;
  307. if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR))
  308. {
  309. result = *addr;
  310. if ((result & 0x80) == (data & 0x80))
  311. chip1 = READY;
  312. else
  313. chip1 = ERR;
  314. }
  315. if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
  316. chip2 = READY;
  317. if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR))
  318. {
  319. result = *addr;
  320. if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
  321. chip2 = READY;
  322. else
  323. chip2 = ERR;
  324. }
  325. } while (!chip1 || !chip2);
  326. *addr = CMD_READ_ARRAY;
  327. if (chip1 == ERR || chip2 == ERR || *addr != data)
  328. rc = ERR_PROG_ERROR;
  329. if (iflag)
  330. enable_interrupts();
  331. if (cflag)
  332. icache_enable();
  333. return rc;
  334. }
  335. /*-----------------------------------------------------------------------
  336. * Copy memory to flash.
  337. */
  338. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  339. {
  340. ulong cp, wp, data;
  341. int l;
  342. int i, rc;
  343. wp = (addr & ~3); /* get lower word aligned address */
  344. /*
  345. * handle unaligned start bytes
  346. */
  347. if ((l = addr - wp) != 0) {
  348. data = 0;
  349. for (i=0, cp=wp; i<l; ++i, ++cp) {
  350. data = (data >> 8) | (*(uchar *)cp << 24);
  351. }
  352. for (; i<4 && cnt>0; ++i) {
  353. data = (data >> 8) | (*src++ << 24);
  354. --cnt;
  355. ++cp;
  356. }
  357. for (; cnt==0 && i<4; ++i, ++cp) {
  358. data = (data >> 8) | (*(uchar *)cp << 24);
  359. }
  360. if ((rc = write_word(info, wp, data)) != 0) {
  361. return (rc);
  362. }
  363. wp += 4;
  364. }
  365. /*
  366. * handle word aligned part
  367. */
  368. while (cnt >= 4) {
  369. data = *((vu_long*)src);
  370. if ((rc = write_word(info, wp, data)) != 0) {
  371. return (rc);
  372. }
  373. src += 4;
  374. wp += 4;
  375. cnt -= 4;
  376. }
  377. if (cnt == 0) {
  378. return ERR_OK;
  379. }
  380. /*
  381. * handle unaligned tail bytes
  382. */
  383. data = 0;
  384. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  385. data = (data >> 8) | (*src++ << 24);
  386. --cnt;
  387. }
  388. for (; i<4; ++i, ++cp) {
  389. data = (data >> 8) | (*(uchar *)cp << 24);
  390. }
  391. return write_word(info, wp, data);
  392. }