flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * U-boot - flash.c Flash driver for PSD4256GV
  3. *
  4. * Copyright (c) 2005-2007 Analog Devices Inc.
  5. * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
  6. *
  7. * (C) Copyright 2000-2004
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  26. * MA 02110-1301 USA
  27. */
  28. #include <asm/io.h>
  29. #include "flash-defines.h"
  30. int AFP_NumSectors = 40;
  31. long AFP_SectorSize1 = 0x10000;
  32. int AFP_SectorSize2 = 0x4000;
  33. void flash_reset(void)
  34. {
  35. reset_flash();
  36. }
  37. unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
  38. {
  39. int id = 0, i = 0;
  40. static int FlagDev = 1;
  41. id = get_codes();
  42. if (FlagDev) {
  43. #ifdef DEBUG
  44. printf("Device ID of the Flash is %x\n", id);
  45. #endif
  46. FlagDev = 0;
  47. }
  48. info->flash_id = id;
  49. switch (bank_flag) {
  50. case 0:
  51. for (i = PriFlashABegin; i < SecFlashABegin; i++)
  52. info->start[i] = (baseaddr + (i * AFP_SectorSize1));
  53. info->size = 0x200000;
  54. info->sector_count = 32;
  55. break;
  56. case 1:
  57. info->start[0] = baseaddr + SecFlashASec1Off;
  58. info->start[1] = baseaddr + SecFlashASec2Off;
  59. info->start[2] = baseaddr + SecFlashASec3Off;
  60. info->start[3] = baseaddr + SecFlashASec4Off;
  61. info->size = 0x10000;
  62. info->sector_count = 4;
  63. break;
  64. case 2:
  65. info->start[0] = baseaddr + SecFlashBSec1Off;
  66. info->start[1] = baseaddr + SecFlashBSec2Off;
  67. info->start[2] = baseaddr + SecFlashBSec3Off;
  68. info->start[3] = baseaddr + SecFlashBSec4Off;
  69. info->size = 0x10000;
  70. info->sector_count = 4;
  71. break;
  72. }
  73. return (info->size);
  74. }
  75. unsigned long flash_init(void)
  76. {
  77. unsigned long size_b0, size_b1, size_b2;
  78. int i;
  79. size_b0 = size_b1 = size_b2 = 0;
  80. #ifdef DEBUG
  81. printf("Flash Memory Start 0x%x\n", CONFIG_SYS_FLASH_BASE);
  82. printf("Memory Map for the Flash\n");
  83. printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
  84. printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
  85. printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
  86. printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
  87. printf("Please type command flinfo for information on Sectors \n");
  88. #endif
  89. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  90. flash_info[i].flash_id = FLASH_UNKNOWN;
  91. }
  92. size_b0 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[0], 0);
  93. size_b1 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[1], 1);
  94. size_b2 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[2], 2);
  95. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  96. printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  97. size_b0, size_b0 >> 20);
  98. }
  99. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH0_BASE,
  100. (flash_info[0].start[2] - 1), &flash_info[0]);
  101. return (size_b0 + size_b1 + size_b2);
  102. }
  103. void flash_print_info(flash_info_t * info)
  104. {
  105. int i;
  106. if (info->flash_id == FLASH_UNKNOWN) {
  107. printf("missing or unknown FLASH type\n");
  108. return;
  109. }
  110. switch (info->flash_id) {
  111. case FLASH_PSD4256GV:
  112. printf("ST Microelectronics ");
  113. break;
  114. default:
  115. printf("Unknown Vendor: (0x%08lX) ", info->flash_id);
  116. break;
  117. }
  118. for (i = 0; i < info->sector_count; ++i) {
  119. if ((i % 5) == 0)
  120. printf("\n ");
  121. printf(" %08lX%s",
  122. info->start[i], info->protect[i] ? " (RO)" : " ");
  123. }
  124. printf("\n");
  125. return;
  126. }
  127. int flash_erase(flash_info_t * info, int s_first, int s_last)
  128. {
  129. int cnt = 0, i;
  130. int prot, sect;
  131. prot = 0;
  132. for (sect = s_first; sect <= s_last; ++sect) {
  133. if (info->protect[sect])
  134. prot++;
  135. }
  136. if (prot)
  137. printf("- Warning: %d protected sectors will not be erased!\n",
  138. prot);
  139. else
  140. printf("\n");
  141. cnt = s_last - s_first + 1;
  142. if (cnt == FLASH_TOT_SECT) {
  143. printf("Erasing flash, Please Wait \n");
  144. if (erase_flash() < 0) {
  145. printf("Erasing flash failed \n");
  146. return FLASH_FAIL;
  147. }
  148. } else {
  149. printf("Erasing Flash locations, Please Wait\n");
  150. for (i = s_first; i <= s_last; i++) {
  151. if (info->protect[i] == 0) { /* not protected */
  152. if (erase_block_flash(i, info->start[i]) < 0) {
  153. printf("Error Sector erasing \n");
  154. return FLASH_FAIL;
  155. }
  156. }
  157. }
  158. }
  159. return FLASH_SUCCESS;
  160. }
  161. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  162. {
  163. int ret;
  164. int d;
  165. if (addr % 2) {
  166. read_flash(addr - 1 - CONFIG_SYS_FLASH_BASE, &d);
  167. d = (int)((d & 0x00FF) | (*src++ << 8));
  168. ret = write_data(addr - 1, 2, (uchar *) & d);
  169. if (ret == FLASH_FAIL)
  170. return ERR_NOT_ERASED;
  171. ret = write_data(addr + 1, cnt - 1, src);
  172. } else
  173. ret = write_data(addr, cnt, src);
  174. if (ret == FLASH_FAIL)
  175. return ERR_NOT_ERASED;
  176. return FLASH_SUCCESS;
  177. }
  178. int write_data(long lStart, long lCount, uchar * pnData)
  179. {
  180. long i = 0;
  181. unsigned long ulOffset = lStart - CONFIG_SYS_FLASH_BASE;
  182. int d;
  183. int nSector = 0;
  184. int flag = 0;
  185. if (lCount % 2) {
  186. flag = 1;
  187. lCount = lCount - 1;
  188. }
  189. for (i = 0; i < lCount - 1; i += 2, ulOffset += 2) {
  190. get_sector_number(ulOffset, &nSector);
  191. read_flash(ulOffset, &d);
  192. if (d != 0xffff) {
  193. printf
  194. ("Flash not erased at offset 0x%lx Please erase to reprogram\n",
  195. ulOffset);
  196. return FLASH_FAIL;
  197. }
  198. unlock_flash(ulOffset);
  199. d = (int)(pnData[i] | pnData[i + 1] << 8);
  200. write_flash(ulOffset, d);
  201. if (poll_toggle_bit(ulOffset) < 0) {
  202. printf("Error programming the flash \n");
  203. return FLASH_FAIL;
  204. }
  205. if ((i > 0) && (!(i % AFP_SectorSize2)))
  206. printf(".");
  207. }
  208. if (flag) {
  209. get_sector_number(ulOffset, &nSector);
  210. read_flash(ulOffset, &d);
  211. if (d != 0xffff) {
  212. printf
  213. ("Flash not erased at offset 0x%lx Please erase to reprogram\n",
  214. ulOffset);
  215. return FLASH_FAIL;
  216. }
  217. unlock_flash(ulOffset);
  218. d = (int)(pnData[i] | (d & 0xFF00));
  219. write_flash(ulOffset, d);
  220. if (poll_toggle_bit(ulOffset) < 0) {
  221. printf("Error programming the flash \n");
  222. return FLASH_FAIL;
  223. }
  224. }
  225. return FLASH_SUCCESS;
  226. }
  227. int read_data(long ulStart, long lCount, long lStride, int *pnData)
  228. {
  229. long i = 0;
  230. int j = 0;
  231. long ulOffset = ulStart;
  232. int iShift = 0;
  233. int iNumWords = 2;
  234. int nLeftover = lCount % 4;
  235. int nHi, nLow;
  236. int nSector = 0;
  237. for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
  238. for (iShift = 0, j = 0; j < iNumWords; j += 2) {
  239. if ((ulOffset >= INVALIDLOCNSTART)
  240. && (ulOffset < INVALIDLOCNEND))
  241. return FLASH_FAIL;
  242. get_sector_number(ulOffset, &nSector);
  243. read_flash(ulOffset, &nLow);
  244. ulOffset += (lStride * 2);
  245. read_flash(ulOffset, &nHi);
  246. ulOffset += (lStride * 2);
  247. pnData[i] = (nHi << 16) | nLow;
  248. }
  249. }
  250. if (nLeftover > 0) {
  251. if ((ulOffset >= INVALIDLOCNSTART)
  252. && (ulOffset < INVALIDLOCNEND))
  253. return FLASH_FAIL;
  254. get_sector_number(ulOffset, &nSector);
  255. read_flash(ulOffset, &pnData[i]);
  256. }
  257. return FLASH_SUCCESS;
  258. }
  259. int write_flash(long nOffset, int nValue)
  260. {
  261. long addr;
  262. addr = (CONFIG_SYS_FLASH_BASE + nOffset);
  263. SSYNC();
  264. *(unsigned volatile short *)addr = nValue;
  265. SSYNC();
  266. if (poll_toggle_bit(nOffset) < 0)
  267. return FLASH_FAIL;
  268. return FLASH_SUCCESS;
  269. }
  270. int read_flash(long nOffset, int *pnValue)
  271. {
  272. int nValue = 0x0;
  273. long addr = (CONFIG_SYS_FLASH_BASE + nOffset);
  274. if (nOffset != 0x2)
  275. reset_flash();
  276. SSYNC();
  277. nValue = *(volatile unsigned short *)addr;
  278. SSYNC();
  279. *pnValue = nValue;
  280. return TRUE;
  281. }
  282. int poll_toggle_bit(long lOffset)
  283. {
  284. unsigned int u1, u2;
  285. unsigned long timeout = 0xFFFFFFFF;
  286. volatile unsigned long *FB =
  287. (volatile unsigned long *)(0x20000000 + lOffset);
  288. while (1) {
  289. if (timeout < 0)
  290. break;
  291. u1 = *(volatile unsigned short *)FB;
  292. u2 = *(volatile unsigned short *)FB;
  293. if ((u1 & 0x0040) == (u2 & 0x0040))
  294. return FLASH_SUCCESS;
  295. if ((u2 & 0x0020) == 0x0000)
  296. continue;
  297. u1 = *(volatile unsigned short *)FB;
  298. if ((u2 & 0x0040) == (u1 & 0x0040))
  299. return FLASH_SUCCESS;
  300. else {
  301. reset_flash();
  302. return FLASH_FAIL;
  303. }
  304. timeout--;
  305. }
  306. printf("Time out occured \n");
  307. if (timeout < 0)
  308. return FLASH_FAIL;
  309. }
  310. void reset_flash(void)
  311. {
  312. write_flash(WRITESEQ1, RESET_VAL);
  313. /* Wait for 10 micro seconds */
  314. udelay(10);
  315. }
  316. int erase_flash(void)
  317. {
  318. write_flash(WRITESEQ1, WRITEDATA1);
  319. write_flash(WRITESEQ2, WRITEDATA2);
  320. write_flash(WRITESEQ3, WRITEDATA3);
  321. write_flash(WRITESEQ4, WRITEDATA4);
  322. write_flash(WRITESEQ5, WRITEDATA5);
  323. write_flash(WRITESEQ6, WRITEDATA6);
  324. if (poll_toggle_bit(0x0000) < 0)
  325. return FLASH_FAIL;
  326. write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
  327. write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
  328. write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
  329. write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
  330. write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
  331. write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
  332. if (poll_toggle_bit(SecFlashASec1Off) < 0)
  333. return FLASH_FAIL;
  334. write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
  335. write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
  336. write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
  337. write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
  338. write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
  339. write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
  340. if (poll_toggle_bit(PriFlashBOff) < 0)
  341. return FLASH_FAIL;
  342. write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
  343. write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
  344. write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
  345. write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
  346. write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
  347. write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
  348. if (poll_toggle_bit(SecFlashBOff) < 0)
  349. return FLASH_FAIL;
  350. return FLASH_SUCCESS;
  351. }
  352. int erase_block_flash(int nBlock, unsigned long address)
  353. {
  354. long ulSectorOff = 0x0;
  355. if ((nBlock < 0) || (nBlock > AFP_NumSectors))
  356. return FALSE;
  357. ulSectorOff = (address - CONFIG_SYS_FLASH_BASE);
  358. write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
  359. write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
  360. write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
  361. write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
  362. write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
  363. write_flash(ulSectorOff, BlockEraseVal);
  364. if (poll_toggle_bit(ulSectorOff) < 0)
  365. return FLASH_FAIL;
  366. return FLASH_SUCCESS;
  367. }
  368. void unlock_flash(long ulOffset)
  369. {
  370. unsigned long ulOffsetAddr = ulOffset;
  371. ulOffsetAddr &= 0xFFFF0000;
  372. write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
  373. write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
  374. write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
  375. }
  376. int get_codes()
  377. {
  378. int dev_id = 0;
  379. write_flash(WRITESEQ1, GETCODEDATA1);
  380. write_flash(WRITESEQ2, GETCODEDATA2);
  381. write_flash(WRITESEQ3, GETCODEDATA3);
  382. read_flash(0x0002, &dev_id);
  383. dev_id &= 0x00FF;
  384. reset_flash();
  385. return dev_id;
  386. }
  387. void get_sector_number(long ulOffset, int *pnSector)
  388. {
  389. int nSector = 0;
  390. if (ulOffset >= SecFlashAOff) {
  391. if ((ulOffset < SecFlashASec1Off)
  392. && (ulOffset < SecFlashASec2Off)) {
  393. nSector = SECT32;
  394. } else if ((ulOffset >= SecFlashASec2Off)
  395. && (ulOffset < SecFlashASec3Off)) {
  396. nSector = SECT33;
  397. } else if ((ulOffset >= SecFlashASec3Off)
  398. && (ulOffset < SecFlashASec4Off)) {
  399. nSector = SECT34;
  400. } else if ((ulOffset >= SecFlashASec4Off)
  401. && (ulOffset < SecFlashAEndOff)) {
  402. nSector = SECT35;
  403. }
  404. } else if (ulOffset >= SecFlashBOff) {
  405. if ((ulOffset < SecFlashBSec1Off)
  406. && (ulOffset < SecFlashBSec2Off)) {
  407. nSector = SECT36;
  408. }
  409. if ((ulOffset < SecFlashBSec2Off)
  410. && (ulOffset < SecFlashBSec3Off)) {
  411. nSector = SECT37;
  412. }
  413. if ((ulOffset < SecFlashBSec3Off)
  414. && (ulOffset < SecFlashBSec4Off)) {
  415. nSector = SECT38;
  416. }
  417. if ((ulOffset < SecFlashBSec4Off)
  418. && (ulOffset < SecFlashBEndOff)) {
  419. nSector = SECT39;
  420. }
  421. } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
  422. nSector = ulOffset & 0xffff0000;
  423. nSector = ulOffset >> 16;
  424. nSector = nSector & 0x000ff;
  425. }
  426. if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
  427. *pnSector = nSector;
  428. }
  429. }