syncflash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * board/mx1ads/syncflash.c
  3. *
  4. * (c) Copyright 2004
  5. * Techware Information Technology, Inc.
  6. * http://www.techware.com.tw/
  7. *
  8. * Ming-Len Wu <minglen_wu@techware.com.tw>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <mc9328.h>
  27. typedef unsigned long * p_u32;
  28. /* 4Mx16x2 IAM=0 CSD1 */
  29. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  30. /* Following Setting is for CSD1 */
  31. #define SFCTL 0x00221004
  32. #define reg_SFCTL __REG(SFCTL)
  33. #define SYNCFLASH_A10 (0x00100000)
  34. #define CMD_NORMAL (0x81020300) /* Normal Mode */
  35. #define CMD_PREC (CMD_NORMAL + 0x10000000) /* Precharge Command */
  36. #define CMD_AUTO (CMD_NORMAL + 0x20000000) /* Auto Refresh Command */
  37. #define CMD_LMR (CMD_NORMAL + 0x30000000) /* Load Mode Register Command */
  38. #define CMD_LCR (CMD_NORMAL + 0x60000000) /* LCR Command */
  39. #define CMD_PROGRAM (CMD_NORMAL + 0x70000000)
  40. #define MODE_REG_VAL (CFG_FLASH_BASE+0x0008CC00) /* Cas Latency 3 */
  41. /* LCR Command */
  42. #define LCR_READSTATUS (0x0001C000) /* 0x70 */
  43. #define LCR_ERASE_CONFIRM (0x00008000) /* 0x20 */
  44. #define LCR_ERASE_NVMODE (0x0000C000) /* 0x30 */
  45. #define LCR_PROG_NVMODE (0x00028000) /* 0xA0 */
  46. #define LCR_SR_CLEAR (0x00014000) /* 0x50 */
  47. /* Get Status register */
  48. u32 SF_SR(void) {
  49. u32 tmp,tmp1;
  50. reg_SFCTL = CMD_PROGRAM;
  51. tmp = __REG(CFG_FLASH_BASE);
  52. reg_SFCTL = CMD_NORMAL;
  53. reg_SFCTL = CMD_LCR; /* Activate LCR Mode */
  54. tmp1 = __REG(CFG_FLASH_BASE + LCR_SR_CLEAR);
  55. return tmp;
  56. }
  57. /* check if SyncFlash is ready */
  58. u8 SF_Ready(void) {
  59. u32 tmp;
  60. tmp = SF_SR();
  61. if ((tmp & 0x00800000) && (tmp & 0x001C0000)) {
  62. printf ("SyncFlash Error code %08x\n",tmp);
  63. };
  64. if ((tmp & 0x00000080) && (tmp & 0x0000001C)) {
  65. printf ("SyncFlash Error code %08x\n",tmp);
  66. };
  67. if (tmp == 0x00800080) /* Test Bit 7 of SR */
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. /* Issue the precharge all command */
  73. void SF_PrechargeAll(void) {
  74. u32 tmp;
  75. reg_SFCTL = CMD_PREC; /* Set Precharge Command */
  76. tmp = __REG(CFG_FLASH_BASE + SYNCFLASH_A10); /* Issue Precharge All Command */
  77. }
  78. /* set SyncFlash to normal mode */
  79. void SF_Normal(void) {
  80. SF_PrechargeAll();
  81. reg_SFCTL = CMD_NORMAL;
  82. }
  83. /* Erase SyncFlash */
  84. void SF_Erase(u32 RowAddress) {
  85. u32 tmp;
  86. reg_SFCTL = CMD_NORMAL;
  87. tmp = __REG(RowAddress);
  88. reg_SFCTL = CMD_PREC;
  89. tmp = __REG(RowAddress);
  90. reg_SFCTL = CMD_LCR; /* Set LCR mode */
  91. __REG(RowAddress + LCR_ERASE_CONFIRM) = 0; /* Issue Erase Setup Command */
  92. reg_SFCTL = CMD_NORMAL; /* return to Normal mode */
  93. __REG(RowAddress) = 0xD0D0D0D0; /* Confirm */
  94. while(!SF_Ready());
  95. }
  96. void SF_NvmodeErase(void) {
  97. SF_PrechargeAll();
  98. reg_SFCTL = CMD_LCR; /* Set to LCR mode */
  99. __REG(CFG_FLASH_BASE + LCR_ERASE_NVMODE) = 0; /* Issue Erase Nvmode Reg Command */
  100. reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
  101. __REG(CFG_FLASH_BASE + LCR_ERASE_NVMODE) = 0xC0C0C0C0; /* Confirm */
  102. while(!SF_Ready());
  103. }
  104. void SF_NvmodeWrite(void) {
  105. SF_PrechargeAll();
  106. reg_SFCTL = CMD_LCR; /* Set to LCR mode */
  107. __REG(CFG_FLASH_BASE+LCR_PROG_NVMODE) = 0; /* Issue Program Nvmode reg command */
  108. reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
  109. __REG(CFG_FLASH_BASE+LCR_PROG_NVMODE) = 0xC0C0C0C0; /* Confirm not needed */
  110. }
  111. /****************************************************************************************/
  112. ulong flash_init(void) {
  113. int i, j;
  114. u32 tmp;
  115. /* Turn on CSD1 for negating RESETSF of SyncFLash */
  116. reg_SFCTL |= 0x80000000; /* enable CSD1 for SyncFlash */
  117. udelay(200);
  118. reg_SFCTL = CMD_LMR; /* Set Load Mode Register Command */
  119. tmp = __REG(MODE_REG_VAL); /* Issue Load Mode Register Command */
  120. SF_Normal();
  121. i = 0;
  122. flash_info[i].flash_id = FLASH_MAN_MT | FLASH_MT28S4M16LC;
  123. flash_info[i].size = FLASH_BANK_SIZE;
  124. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  125. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  126. for (j = 0; j < flash_info[i].sector_count; j++) {
  127. flash_info[i].start[j] = CFG_FLASH_BASE + j * 0x00100000;
  128. }
  129. flash_protect(FLAG_PROTECT_SET,
  130. CFG_FLASH_BASE,
  131. CFG_FLASH_BASE + monitor_flash_len - 1,
  132. &flash_info[0]);
  133. flash_protect(FLAG_PROTECT_SET,
  134. CFG_ENV_ADDR,
  135. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  136. &flash_info[0]);
  137. return FLASH_BANK_SIZE;
  138. }
  139. void flash_print_info (flash_info_t *info) {
  140. int i;
  141. switch (info->flash_id & FLASH_VENDMASK) {
  142. case (FLASH_MAN_MT & FLASH_VENDMASK):
  143. printf("Micron: ");
  144. break;
  145. default:
  146. printf("Unknown Vendor ");
  147. break;
  148. }
  149. switch (info->flash_id & FLASH_TYPEMASK) {
  150. case (FLASH_MT28S4M16LC & FLASH_TYPEMASK):
  151. printf("2x FLASH_MT28S4M16LC (16MB Total)\n");
  152. break;
  153. default:
  154. printf("Unknown Chip Type\n");
  155. return;
  156. break;
  157. }
  158. printf(" Size: %ld MB in %d Sectors\n",
  159. info->size >> 20, info->sector_count);
  160. printf(" Sector Start Addresses: ");
  161. for (i = 0; i < info->sector_count; i++) {
  162. if ((i % 5) == 0)
  163. printf ("\n ");
  164. printf (" %08lX%s", info->start[i],
  165. info->protect[i] ? " (RO)" : " ");
  166. }
  167. printf ("\n");
  168. }
  169. /*-----------------------------------------------------------------------*/
  170. int flash_erase (flash_info_t *info, int s_first, int s_last) {
  171. int iflag, cflag, prot, sect;
  172. int rc = ERR_OK;
  173. /* first look for protection bits */
  174. if (info->flash_id == FLASH_UNKNOWN)
  175. return ERR_UNKNOWN_FLASH_TYPE;
  176. if ((s_first < 0) || (s_first > s_last))
  177. return ERR_INVAL;
  178. if ((info->flash_id & FLASH_VENDMASK) != (FLASH_MAN_MT & FLASH_VENDMASK))
  179. return ERR_UNKNOWN_FLASH_VENDOR;
  180. prot = 0;
  181. for (sect = s_first; sect <= s_last; ++sect) {
  182. if (info->protect[sect])
  183. prot++;
  184. }
  185. if (prot) {
  186. printf("protected!\n");
  187. return ERR_PROTECTED;
  188. }
  189. /*
  190. * Disable interrupts which might cause a timeout
  191. * here. Remember that our exception vectors are
  192. * at address 0 in the flash, and we don't want a
  193. * (ticker) exception to happen while the flash
  194. * chip is in programming mode.
  195. */
  196. cflag = icache_status();
  197. icache_disable();
  198. iflag = disable_interrupts();
  199. /* Start erase on unprotected sectors */
  200. for (sect = s_first; sect <= s_last && !ctrlc(); sect++) {
  201. printf("Erasing sector %2d ... ", sect);
  202. /* arm simple, non interrupt dependent timer */
  203. reset_timer_masked();
  204. SF_NvmodeErase();
  205. SF_NvmodeWrite();
  206. SF_Erase(CFG_FLASH_BASE + (0x0100000 * sect));
  207. SF_Normal();
  208. printf("ok.\n");
  209. }
  210. if (ctrlc())
  211. printf("User Interrupt!\n");
  212. if (iflag)
  213. enable_interrupts();
  214. if (cflag)
  215. icache_enable();
  216. return rc;
  217. }
  218. /*-----------------------------------------------------------------------
  219. * Copy memory to flash.
  220. */
  221. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) {
  222. int i;
  223. for(i = 0; i < cnt; i += 4) {
  224. SF_PrechargeAll();
  225. reg_SFCTL = CMD_PROGRAM; /* Enter SyncFlash Program mode */
  226. __REG(addr + i) = __REG((u32)src + i);
  227. while(!SF_Ready());
  228. }
  229. SF_Normal();
  230. return ERR_OK;
  231. }