PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/board/netphone/flash.c

https://bitbucket.org/accelecon/u-boot
C | 529 lines | 366 code | 80 blank | 83 comment | 65 complexity | e91aa6b67a133b5c6e1606ea9af0d868 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc8xx.h>
  25. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size(vu_long * addr, flash_info_t * info);
  30. static int write_byte(flash_info_t * info, ulong dest, uchar data);
  31. static void flash_get_offsets(ulong base, flash_info_t * info);
  32. /*-----------------------------------------------------------------------
  33. */
  34. unsigned long flash_init(void)
  35. {
  36. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  37. volatile memctl8xx_t *memctl = &immap->im_memctl;
  38. unsigned long size;
  39. #if CONFIG_NETPHONE_VERSION == 2
  40. unsigned long size1;
  41. #endif
  42. int i;
  43. /* Init: no FLASHes known */
  44. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
  45. flash_info[i].flash_id = FLASH_UNKNOWN;
  46. size = flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
  47. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  48. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  49. size, size << 20);
  50. }
  51. /* Remap FLASH according to real size */
  52. memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size & 0xFFFF8000);
  53. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | (memctl->memc_br0 & ~(BR_BA_MSK));
  54. /* Re-do sizing to get full correct info */
  55. size = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  56. flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  57. /* monitor protection ON by default */
  58. flash_protect(FLAG_PROTECT_SET,
  59. CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  60. &flash_info[0]);
  61. flash_protect ( FLAG_PROTECT_SET,
  62. CONFIG_ENV_ADDR,
  63. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  64. &flash_info[0]);
  65. #ifdef CONFIG_ENV_ADDR_REDUND
  66. flash_protect ( FLAG_PROTECT_SET,
  67. CONFIG_ENV_ADDR_REDUND,
  68. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
  69. &flash_info[0]);
  70. #endif
  71. flash_info[0].size = size;
  72. #if CONFIG_NETPHONE_VERSION == 2
  73. size1 = flash_get_size((vu_long *) FLASH_BASE4_PRELIM, &flash_info[1]);
  74. if (size1 > 0) {
  75. if (flash_info[1].flash_id == FLASH_UNKNOWN)
  76. printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", size1, size1 << 20);
  77. /* Remap FLASH according to real size */
  78. memctl->memc_or4 = CONFIG_SYS_OR_TIMING_FLASH | (-size1 & 0xFFFF8000);
  79. memctl->memc_br4 = (CONFIG_SYS_FLASH_BASE4 & BR_BA_MSK) | (memctl->memc_br4 & ~(BR_BA_MSK));
  80. /* Re-do sizing to get full correct info */
  81. size1 = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE4, &flash_info[1]);
  82. flash_get_offsets(CONFIG_SYS_FLASH_BASE4, &flash_info[1]);
  83. size += size1;
  84. } else
  85. memctl->memc_br4 &= ~BR_V;
  86. #endif
  87. return (size);
  88. }
  89. /*-----------------------------------------------------------------------
  90. */
  91. static void flash_get_offsets(ulong base, flash_info_t * info)
  92. {
  93. int i;
  94. /* set up sector start address table */
  95. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
  96. for (i = 0; i < info->sector_count; i++) {
  97. info->start[i] = base + (i * 0x00010000);
  98. }
  99. } else if (info->flash_id & FLASH_BTYPE) {
  100. /* set sector offsets for bottom boot block type */
  101. info->start[0] = base + 0x00000000;
  102. info->start[1] = base + 0x00004000;
  103. info->start[2] = base + 0x00006000;
  104. info->start[3] = base + 0x00008000;
  105. for (i = 4; i < info->sector_count; i++) {
  106. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  107. }
  108. } else {
  109. /* set sector offsets for top boot block type */
  110. i = info->sector_count - 1;
  111. info->start[i--] = base + info->size - 0x00004000;
  112. info->start[i--] = base + info->size - 0x00006000;
  113. info->start[i--] = base + info->size - 0x00008000;
  114. for (; i >= 0; i--) {
  115. info->start[i] = base + i * 0x00010000;
  116. }
  117. }
  118. }
  119. /*-----------------------------------------------------------------------
  120. */
  121. void flash_print_info(flash_info_t * info)
  122. {
  123. int i;
  124. if (info->flash_id == FLASH_UNKNOWN) {
  125. printf("missing or unknown FLASH type\n");
  126. return;
  127. }
  128. switch (info->flash_id & FLASH_VENDMASK) {
  129. case FLASH_MAN_AMD:
  130. printf("AMD ");
  131. break;
  132. case FLASH_MAN_FUJ:
  133. printf("FUJITSU ");
  134. break;
  135. case FLASH_MAN_MX:
  136. printf("MXIC ");
  137. break;
  138. default:
  139. printf("Unknown Vendor ");
  140. break;
  141. }
  142. switch (info->flash_id & FLASH_TYPEMASK) {
  143. case FLASH_AM040:
  144. printf("AM29LV040B (4 Mbit, bottom boot sect)\n");
  145. break;
  146. case FLASH_AM400B:
  147. printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
  148. break;
  149. case FLASH_AM400T:
  150. printf("AM29LV400T (4 Mbit, top boot sector)\n");
  151. break;
  152. case FLASH_AM800B:
  153. printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
  154. break;
  155. case FLASH_AM800T:
  156. printf("AM29LV800T (8 Mbit, top boot sector)\n");
  157. break;
  158. case FLASH_AM160B:
  159. printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
  160. break;
  161. case FLASH_AM160T:
  162. printf("AM29LV160T (16 Mbit, top boot sector)\n");
  163. break;
  164. case FLASH_AM320B:
  165. printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
  166. break;
  167. case FLASH_AM320T:
  168. printf("AM29LV320T (32 Mbit, top boot sector)\n");
  169. break;
  170. default:
  171. printf("Unknown Chip Type\n");
  172. break;
  173. }
  174. printf(" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
  175. printf(" Sector Start Addresses:");
  176. for (i = 0; i < info->sector_count; ++i) {
  177. if ((i % 5) == 0)
  178. printf("\n ");
  179. printf(" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " ");
  180. }
  181. printf("\n");
  182. }
  183. /*-----------------------------------------------------------------------
  184. */
  185. /*-----------------------------------------------------------------------
  186. */
  187. /*
  188. * The following code cannot be run from FLASH!
  189. */
  190. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  191. {
  192. short i;
  193. uchar mid;
  194. uchar pid;
  195. vu_char *caddr = (vu_char *) addr;
  196. ulong base = (ulong) addr;
  197. /* Write auto select command: read Manufacturer ID */
  198. caddr[0x0555] = 0xAA;
  199. caddr[0x02AA] = 0x55;
  200. caddr[0x0555] = 0x90;
  201. mid = caddr[0];
  202. switch (mid) {
  203. case (AMD_MANUFACT & 0xFF):
  204. info->flash_id = FLASH_MAN_AMD;
  205. break;
  206. case (FUJ_MANUFACT & 0xFF):
  207. info->flash_id = FLASH_MAN_FUJ;
  208. break;
  209. case (MX_MANUFACT & 0xFF):
  210. info->flash_id = FLASH_MAN_MX;
  211. break;
  212. case (STM_MANUFACT & 0xFF):
  213. info->flash_id = FLASH_MAN_STM;
  214. break;
  215. default:
  216. info->flash_id = FLASH_UNKNOWN;
  217. info->sector_count = 0;
  218. info->size = 0;
  219. return (0); /* no or unknown flash */
  220. }
  221. pid = caddr[1]; /* device ID */
  222. switch (pid) {
  223. case (AMD_ID_LV400T & 0xFF):
  224. info->flash_id += FLASH_AM400T;
  225. info->sector_count = 11;
  226. info->size = 0x00080000;
  227. break; /* => 512 kB */
  228. case (AMD_ID_LV400B & 0xFF):
  229. info->flash_id += FLASH_AM400B;
  230. info->sector_count = 11;
  231. info->size = 0x00080000;
  232. break; /* => 512 kB */
  233. case (AMD_ID_LV800T & 0xFF):
  234. info->flash_id += FLASH_AM800T;
  235. info->sector_count = 19;
  236. info->size = 0x00100000;
  237. break; /* => 1 MB */
  238. case (AMD_ID_LV800B & 0xFF):
  239. info->flash_id += FLASH_AM800B;
  240. info->sector_count = 19;
  241. info->size = 0x00100000;
  242. break; /* => 1 MB */
  243. case (AMD_ID_LV160T & 0xFF):
  244. info->flash_id += FLASH_AM160T;
  245. info->sector_count = 35;
  246. info->size = 0x00200000;
  247. break; /* => 2 MB */
  248. case (AMD_ID_LV160B & 0xFF):
  249. info->flash_id += FLASH_AM160B;
  250. info->sector_count = 35;
  251. info->size = 0x00200000;
  252. break; /* => 2 MB */
  253. case (AMD_ID_LV040B & 0xFF):
  254. info->flash_id += FLASH_AM040;
  255. info->sector_count = 8;
  256. info->size = 0x00080000;
  257. break;
  258. case (STM_ID_M29W040B & 0xFF):
  259. info->flash_id += FLASH_AM040;
  260. info->sector_count = 8;
  261. info->size = 0x00080000;
  262. break;
  263. #if 0 /* enable when device IDs are available */
  264. case (AMD_ID_LV320T & 0xFF):
  265. info->flash_id += FLASH_AM320T;
  266. info->sector_count = 67;
  267. info->size = 0x00400000;
  268. break; /* => 4 MB */
  269. case (AMD_ID_LV320B & 0xFF):
  270. info->flash_id += FLASH_AM320B;
  271. info->sector_count = 67;
  272. info->size = 0x00400000;
  273. break; /* => 4 MB */
  274. #endif
  275. default:
  276. info->flash_id = FLASH_UNKNOWN;
  277. return (0); /* => no or unknown flash */
  278. }
  279. printf(" ");
  280. /* set up sector start address table */
  281. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
  282. for (i = 0; i < info->sector_count; i++) {
  283. info->start[i] = base + (i * 0x00010000);
  284. }
  285. } else if (info->flash_id & FLASH_BTYPE) {
  286. /* set sector offsets for bottom boot block type */
  287. info->start[0] = base + 0x00000000;
  288. info->start[1] = base + 0x00004000;
  289. info->start[2] = base + 0x00006000;
  290. info->start[3] = base + 0x00008000;
  291. for (i = 4; i < info->sector_count; i++) {
  292. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  293. }
  294. } else {
  295. /* set sector offsets for top boot block type */
  296. i = info->sector_count - 1;
  297. info->start[i--] = base + info->size - 0x00004000;
  298. info->start[i--] = base + info->size - 0x00006000;
  299. info->start[i--] = base + info->size - 0x00008000;
  300. for (; i >= 0; i--) {
  301. info->start[i] = base + i * 0x00010000;
  302. }
  303. }
  304. /* check for protected sectors */
  305. for (i = 0; i < info->sector_count; i++) {
  306. /* read sector protection: D0 = 1 if protected */
  307. caddr = (volatile unsigned char *)(info->start[i]);
  308. info->protect[i] = caddr[2] & 1;
  309. }
  310. /*
  311. * Prevent writes to uninitialized FLASH.
  312. */
  313. if (info->flash_id != FLASH_UNKNOWN) {
  314. caddr = (vu_char *) info->start[0];
  315. caddr[0x0555] = 0xAA;
  316. caddr[0x02AA] = 0x55;
  317. caddr[0x0555] = 0xF0;
  318. udelay(20000);
  319. }
  320. return (info->size);
  321. }
  322. /*-----------------------------------------------------------------------
  323. */
  324. int flash_erase(flash_info_t * info, int s_first, int s_last)
  325. {
  326. vu_char *addr = (vu_char *) (info->start[0]);
  327. int flag, prot, sect, l_sect;
  328. ulong start, now, last;
  329. if ((s_first < 0) || (s_first > s_last)) {
  330. if (info->flash_id == FLASH_UNKNOWN) {
  331. printf("- missing\n");
  332. } else {
  333. printf("- no sectors to erase\n");
  334. }
  335. return 1;
  336. }
  337. if ((info->flash_id == FLASH_UNKNOWN) ||
  338. (info->flash_id > FLASH_AMD_COMP)) {
  339. printf("Can't erase unknown flash type %08lx - aborted\n", info->flash_id);
  340. return 1;
  341. }
  342. prot = 0;
  343. for (sect = s_first; sect <= s_last; ++sect) {
  344. if (info->protect[sect]) {
  345. prot++;
  346. }
  347. }
  348. if (prot) {
  349. printf("- Warning: %d protected sectors will not be erased!\n", prot);
  350. } else {
  351. printf("\n");
  352. }
  353. l_sect = -1;
  354. /* Disable interrupts which might cause a timeout here */
  355. flag = disable_interrupts();
  356. addr[0x0555] = 0xAA;
  357. addr[0x02AA] = 0x55;
  358. addr[0x0555] = 0x80;
  359. addr[0x0555] = 0xAA;
  360. addr[0x02AA] = 0x55;
  361. /* Start erase on unprotected sectors */
  362. for (sect = s_first; sect <= s_last; sect++) {
  363. if (info->protect[sect] == 0) { /* not protected */
  364. addr = (vu_char *) (info->start[sect]);
  365. addr[0] = 0x30;
  366. l_sect = sect;
  367. }
  368. }
  369. /* re-enable interrupts if necessary */
  370. if (flag)
  371. enable_interrupts();
  372. /* wait at least 80us - let's wait 1 ms */
  373. udelay(1000);
  374. /*
  375. * We wait for the last triggered sector
  376. */
  377. if (l_sect < 0)
  378. goto DONE;
  379. start = get_timer(0);
  380. last = start;
  381. addr = (vu_char *) (info->start[l_sect]);
  382. while ((addr[0] & 0x80) != 0x80) {
  383. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  384. printf("Timeout\n");
  385. return 1;
  386. }
  387. /* show that we're waiting */
  388. if ((now - last) > 1000) { /* every second */
  389. putc('.');
  390. last = now;
  391. }
  392. }
  393. DONE:
  394. /* reset to read mode */
  395. addr = (vu_char *) info->start[0];
  396. addr[0] = 0xF0; /* reset bank */
  397. printf(" done\n");
  398. return 0;
  399. }
  400. /*-----------------------------------------------------------------------
  401. * Copy memory to flash, returns:
  402. * 0 - OK
  403. * 1 - write timeout
  404. * 2 - Flash not erased
  405. */
  406. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  407. {
  408. int rc;
  409. while (cnt > 0) {
  410. if ((rc = write_byte(info, addr++, *src++)) != 0) {
  411. return (rc);
  412. }
  413. --cnt;
  414. }
  415. return (0);
  416. }
  417. /*-----------------------------------------------------------------------
  418. * Write a word to Flash, returns:
  419. * 0 - OK
  420. * 1 - write timeout
  421. * 2 - Flash not erased
  422. */
  423. static int write_byte(flash_info_t * info, ulong dest, uchar data)
  424. {
  425. vu_char *addr = (vu_char *) (info->start[0]);
  426. ulong start;
  427. int flag;
  428. /* Check if Flash is (sufficiently) erased */
  429. if ((*((vu_char *) dest) & data) != data) {
  430. return (2);
  431. }
  432. /* Disable interrupts which might cause a timeout here */
  433. flag = disable_interrupts();
  434. addr[0x0555] = 0xAA;
  435. addr[0x02AA] = 0x55;
  436. addr[0x0555] = 0xA0;
  437. *((vu_char *) dest) = data;
  438. /* re-enable interrupts if necessary */
  439. if (flag)
  440. enable_interrupts();
  441. /* data polling for D7 */
  442. start = get_timer(0);
  443. while ((*((vu_char *) dest) & 0x80) != (data & 0x80)) {
  444. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  445. return (1);
  446. }
  447. }
  448. return (0);
  449. }