syncflash.c 7.4 KB

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