flash.c 12 KB

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