syncflash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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[CONFIG_SYS_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 (CONFIG_SYS_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;
  51. reg_SFCTL = CMD_PROGRAM;
  52. tmp = __REG(CONFIG_SYS_FLASH_BASE);
  53. reg_SFCTL = CMD_NORMAL;
  54. reg_SFCTL = CMD_LCR; /* Activate LCR Mode */
  55. __REG(CONFIG_SYS_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. /* Set Precharge Command */
  76. reg_SFCTL = CMD_PREC;
  77. /* Issue Precharge All Command */
  78. __REG(CONFIG_SYS_FLASH_BASE + SYNCFLASH_A10);
  79. }
  80. /* set SyncFlash to normal mode */
  81. void SF_Normal(void) {
  82. SF_PrechargeAll();
  83. reg_SFCTL = CMD_NORMAL;
  84. }
  85. /* Erase SyncFlash */
  86. void SF_Erase(u32 RowAddress) {
  87. reg_SFCTL = CMD_NORMAL;
  88. __REG(RowAddress);
  89. reg_SFCTL = CMD_PREC;
  90. __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(CONFIG_SYS_FLASH_BASE + LCR_ERASE_NVMODE) = 0; /* Issue Erase Nvmode Reg Command */
  101. reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
  102. __REG(CONFIG_SYS_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(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0; /* Issue Program Nvmode reg command */
  109. reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
  110. __REG(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0xC0C0C0C0; /* Confirm not needed */
  111. }
  112. /****************************************************************************************/
  113. ulong flash_init(void) {
  114. int i, j;
  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. __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 = CONFIG_SYS_MAX_FLASH_SECT;
  125. memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  126. for (j = 0; j < flash_info[i].sector_count; j++) {
  127. flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE + j * 0x00100000;
  128. }
  129. flash_protect(FLAG_PROTECT_SET,
  130. CONFIG_SYS_FLASH_BASE,
  131. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  132. &flash_info[0]);
  133. flash_protect(FLAG_PROTECT_SET,
  134. CONFIG_ENV_ADDR,
  135. CONFIG_ENV_ADDR + CONFIG_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. get_timer(0);
  204. SF_NvmodeErase();
  205. SF_NvmodeWrite();
  206. SF_Erase(CONFIG_SYS_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. }