blob: 5d685338fb85d832c9bfdefb60e15121cbdd7a61 [file] [log] [blame]
wdenk2d24a3a2004-06-09 21:50:45 +00001/*
2 * board/mx1ads/syncflash.c
wdenk49822e22004-06-19 21:19:10 +00003 *
wdenk2d24a3a2004-06-09 21:50:45 +00004 * (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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenk2d24a3a2004-06-09 21:50:45 +000011 */
12
13#include <common.h>
wdenk281e00a2004-08-01 22:48:16 +000014/*#include <mc9328.h>*/
15#include <asm/arch/imx-regs.h>
wdenk2d24a3a2004-06-09 21:50:45 +000016
17typedef unsigned long * p_u32;
18
19/* 4Mx16x2 IAM=0 CSD1 */
20
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk2d24a3a2004-06-09 21:50:45 +000022
23/* Following Setting is for CSD1 */
wdenk281e00a2004-08-01 22:48:16 +000024#define SFCTL 0x00221004
25#define reg_SFCTL __REG(SFCTL)
wdenk2d24a3a2004-06-09 21:50:45 +000026
wdenk281e00a2004-08-01 22:48:16 +000027#define SYNCFLASH_A10 (0x00100000)
wdenk2d24a3a2004-06-09 21:50:45 +000028
wdenk281e00a2004-08-01 22:48:16 +000029#define CMD_NORMAL (0x81020300) /* Normal Mode */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020030#define CMD_PREC (CMD_NORMAL + 0x10000000) /* Precharge Command */
31#define CMD_AUTO (CMD_NORMAL + 0x20000000) /* Auto Refresh Command */
32#define CMD_LMR (CMD_NORMAL + 0x30000000) /* Load Mode Register Command */
33#define CMD_LCR (CMD_NORMAL + 0x60000000) /* LCR Command */
wdenk281e00a2004-08-01 22:48:16 +000034#define CMD_PROGRAM (CMD_NORMAL + 0x70000000)
wdenk2d24a3a2004-06-09 21:50:45 +000035
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020036#define MODE_REG_VAL (CONFIG_SYS_FLASH_BASE+0x0008CC00) /* Cas Latency 3 */
wdenk2d24a3a2004-06-09 21:50:45 +000037
38/* LCR Command */
wdenk281e00a2004-08-01 22:48:16 +000039#define LCR_READSTATUS (0x0001C000) /* 0x70 */
40#define LCR_ERASE_CONFIRM (0x00008000) /* 0x20 */
41#define LCR_ERASE_NVMODE (0x0000C000) /* 0x30 */
42#define LCR_PROG_NVMODE (0x00028000) /* 0xA0 */
43#define LCR_SR_CLEAR (0x00014000) /* 0x50 */
wdenk2d24a3a2004-06-09 21:50:45 +000044
Wolfgang Denk53677ef2008-05-20 16:00:29 +020045/* Get Status register */
wdenk2d24a3a2004-06-09 21:50:45 +000046u32 SF_SR(void) {
Anatolij Gustschin6859ea72011-11-19 13:12:16 +000047 u32 tmp;
wdenk2d24a3a2004-06-09 21:50:45 +000048
49 reg_SFCTL = CMD_PROGRAM;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 tmp = __REG(CONFIG_SYS_FLASH_BASE);
wdenk49822e22004-06-19 21:19:10 +000051
wdenk2d24a3a2004-06-09 21:50:45 +000052 reg_SFCTL = CMD_NORMAL;
53
Wolfgang Denk53677ef2008-05-20 16:00:29 +020054 reg_SFCTL = CMD_LCR; /* Activate LCR Mode */
Anatolij Gustschin6859ea72011-11-19 13:12:16 +000055 __REG(CONFIG_SYS_FLASH_BASE + LCR_SR_CLEAR);
wdenk2d24a3a2004-06-09 21:50:45 +000056
57 return tmp;
58}
59
Wolfgang Denk53677ef2008-05-20 16:00:29 +020060/* check if SyncFlash is ready */
wdenk2d24a3a2004-06-09 21:50:45 +000061u8 SF_Ready(void) {
62 u32 tmp;
63
64 tmp = SF_SR();
65
66 if ((tmp & 0x00800000) && (tmp & 0x001C0000)) {
67 printf ("SyncFlash Error code %08x\n",tmp);
68 };
69
70 if ((tmp & 0x00000080) && (tmp & 0x0000001C)) {
71 printf ("SyncFlash Error code %08x\n",tmp);
wdenk2d24a3a2004-06-09 21:50:45 +000072 };
73
Wolfgang Denk53677ef2008-05-20 16:00:29 +020074 if (tmp == 0x00800080) /* Test Bit 7 of SR */
wdenk2d24a3a2004-06-09 21:50:45 +000075 return 1;
76 else
77 return 0;
78}
79
Wolfgang Denk53677ef2008-05-20 16:00:29 +020080/* Issue the precharge all command */
wdenk2d24a3a2004-06-09 21:50:45 +000081void SF_PrechargeAll(void) {
82
Anatolij Gustschin6859ea72011-11-19 13:12:16 +000083 /* Set Precharge Command */
84 reg_SFCTL = CMD_PREC;
85 /* Issue Precharge All Command */
86 __REG(CONFIG_SYS_FLASH_BASE + SYNCFLASH_A10);
wdenk2d24a3a2004-06-09 21:50:45 +000087}
88
89/* set SyncFlash to normal mode */
90void SF_Normal(void) {
91
92 SF_PrechargeAll();
wdenk49822e22004-06-19 21:19:10 +000093
wdenk2d24a3a2004-06-09 21:50:45 +000094 reg_SFCTL = CMD_NORMAL;
95}
96
Wolfgang Denk53677ef2008-05-20 16:00:29 +020097/* Erase SyncFlash */
wdenk2d24a3a2004-06-09 21:50:45 +000098void SF_Erase(u32 RowAddress) {
wdenk2d24a3a2004-06-09 21:50:45 +000099
100 reg_SFCTL = CMD_NORMAL;
Anatolij Gustschin6859ea72011-11-19 13:12:16 +0000101 __REG(RowAddress);
wdenk2d24a3a2004-06-09 21:50:45 +0000102
103 reg_SFCTL = CMD_PREC;
Anatolij Gustschin6859ea72011-11-19 13:12:16 +0000104 __REG(RowAddress);
wdenk49822e22004-06-19 21:19:10 +0000105
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200106 reg_SFCTL = CMD_LCR; /* Set LCR mode */
107 __REG(RowAddress + LCR_ERASE_CONFIRM) = 0; /* Issue Erase Setup Command */
wdenk49822e22004-06-19 21:19:10 +0000108
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200109 reg_SFCTL = CMD_NORMAL; /* return to Normal mode */
110 __REG(RowAddress) = 0xD0D0D0D0; /* Confirm */
wdenk2d24a3a2004-06-09 21:50:45 +0000111
112 while(!SF_Ready());
113}
114
wdenk2d24a3a2004-06-09 21:50:45 +0000115void SF_NvmodeErase(void) {
116 SF_PrechargeAll();
117
118 reg_SFCTL = CMD_LCR; /* Set to LCR mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119 __REG(CONFIG_SYS_FLASH_BASE + LCR_ERASE_NVMODE) = 0; /* Issue Erase Nvmode Reg Command */
wdenk49822e22004-06-19 21:19:10 +0000120
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200121 reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 __REG(CONFIG_SYS_FLASH_BASE + LCR_ERASE_NVMODE) = 0xC0C0C0C0; /* Confirm */
wdenk2d24a3a2004-06-09 21:50:45 +0000123
124 while(!SF_Ready());
125}
126
127void SF_NvmodeWrite(void) {
128 SF_PrechargeAll();
129
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200130 reg_SFCTL = CMD_LCR; /* Set to LCR mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200131 __REG(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0; /* Issue Program Nvmode reg command */
wdenk49822e22004-06-19 21:19:10 +0000132
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200133 reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200134 __REG(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0xC0C0C0C0; /* Confirm not needed */
wdenk2d24a3a2004-06-09 21:50:45 +0000135}
136
wdenk2d24a3a2004-06-09 21:50:45 +0000137/****************************************************************************************/
138
139ulong flash_init(void) {
140 int i, j;
wdenk2d24a3a2004-06-09 21:50:45 +0000141
142/* Turn on CSD1 for negating RESETSF of SyncFLash */
143
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200144 reg_SFCTL |= 0x80000000; /* enable CSD1 for SyncFlash */
wdenk2d24a3a2004-06-09 21:50:45 +0000145 udelay(200);
146
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200147 reg_SFCTL = CMD_LMR; /* Set Load Mode Register Command */
Anatolij Gustschin6859ea72011-11-19 13:12:16 +0000148 __REG(MODE_REG_VAL); /* Issue Load Mode Register Command */
wdenk2d24a3a2004-06-09 21:50:45 +0000149
150 SF_Normal();
wdenk49822e22004-06-19 21:19:10 +0000151
wdenk2d24a3a2004-06-09 21:50:45 +0000152 i = 0;
153
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200154 flash_info[i].flash_id = FLASH_MAN_MT | FLASH_MT28S4M16LC;
wdenk49822e22004-06-19 21:19:10 +0000155
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200156 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200157 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk2d24a3a2004-06-09 21:50:45 +0000158
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200159 memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenk2d24a3a2004-06-09 21:50:45 +0000160
161 for (j = 0; j < flash_info[i].sector_count; j++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162 flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE + j * 0x00100000;
wdenk2d24a3a2004-06-09 21:50:45 +0000163 }
wdenk49822e22004-06-19 21:19:10 +0000164
wdenk2d24a3a2004-06-09 21:50:45 +0000165 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166 CONFIG_SYS_FLASH_BASE,
167 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
wdenk2d24a3a2004-06-09 21:50:45 +0000168 &flash_info[0]);
169
170 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200171 CONFIG_ENV_ADDR,
172 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
wdenk2d24a3a2004-06-09 21:50:45 +0000173 &flash_info[0]);
174
175 return FLASH_BANK_SIZE;
176}
177
wdenk2d24a3a2004-06-09 21:50:45 +0000178void flash_print_info (flash_info_t *info) {
179
180 int i;
181
182 switch (info->flash_id & FLASH_VENDMASK) {
183 case (FLASH_MAN_MT & FLASH_VENDMASK):
184 printf("Micron: ");
185 break;
186 default:
187 printf("Unknown Vendor ");
188 break;
189 }
wdenk49822e22004-06-19 21:19:10 +0000190
wdenk2d24a3a2004-06-09 21:50:45 +0000191 switch (info->flash_id & FLASH_TYPEMASK) {
192 case (FLASH_MT28S4M16LC & FLASH_TYPEMASK):
193 printf("2x FLASH_MT28S4M16LC (16MB Total)\n");
194 break;
195 default:
196 printf("Unknown Chip Type\n");
197 return;
198 break;
199 }
200
201 printf(" Size: %ld MB in %d Sectors\n",
202 info->size >> 20, info->sector_count);
203
204 printf(" Sector Start Addresses: ");
205
206 for (i = 0; i < info->sector_count; i++) {
wdenk49822e22004-06-19 21:19:10 +0000207 if ((i % 5) == 0)
wdenk2d24a3a2004-06-09 21:50:45 +0000208 printf ("\n ");
209
210 printf (" %08lX%s", info->start[i],
211 info->protect[i] ? " (RO)" : " ");
212 }
wdenk49822e22004-06-19 21:19:10 +0000213
wdenk2d24a3a2004-06-09 21:50:45 +0000214 printf ("\n");
215}
216
wdenk2d24a3a2004-06-09 21:50:45 +0000217/*-----------------------------------------------------------------------*/
218
219int flash_erase (flash_info_t *info, int s_first, int s_last) {
220 int iflag, cflag, prot, sect;
221 int rc = ERR_OK;
222
223/* first look for protection bits */
224
225 if (info->flash_id == FLASH_UNKNOWN)
226 return ERR_UNKNOWN_FLASH_TYPE;
227
wdenk49822e22004-06-19 21:19:10 +0000228 if ((s_first < 0) || (s_first > s_last))
wdenk2d24a3a2004-06-09 21:50:45 +0000229 return ERR_INVAL;
230
wdenk49822e22004-06-19 21:19:10 +0000231 if ((info->flash_id & FLASH_VENDMASK) != (FLASH_MAN_MT & FLASH_VENDMASK))
wdenk2d24a3a2004-06-09 21:50:45 +0000232 return ERR_UNKNOWN_FLASH_VENDOR;
233
234 prot = 0;
235
236 for (sect = s_first; sect <= s_last; ++sect) {
wdenk49822e22004-06-19 21:19:10 +0000237 if (info->protect[sect])
wdenk2d24a3a2004-06-09 21:50:45 +0000238 prot++;
239 }
wdenk49822e22004-06-19 21:19:10 +0000240
wdenk2d24a3a2004-06-09 21:50:45 +0000241 if (prot) {
242 printf("protected!\n");
243 return ERR_PROTECTED;
244 }
245/*
246 * Disable interrupts which might cause a timeout
247 * here. Remember that our exception vectors are
248 * at address 0 in the flash, and we don't want a
249 * (ticker) exception to happen while the flash
250 * chip is in programming mode.
251 */
252
253 cflag = icache_status();
254 icache_disable();
255 iflag = disable_interrupts();
256
257/* Start erase on unprotected sectors */
258 for (sect = s_first; sect <= s_last && !ctrlc(); sect++) {
wdenk49822e22004-06-19 21:19:10 +0000259
wdenk2d24a3a2004-06-09 21:50:45 +0000260 printf("Erasing sector %2d ... ", sect);
261
262/* arm simple, non interrupt dependent timer */
263
Graeme Russa60d1e52011-07-15 23:31:37 +0000264 get_timer(0);
wdenk2d24a3a2004-06-09 21:50:45 +0000265
266 SF_NvmodeErase();
267 SF_NvmodeWrite();
268
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200269 SF_Erase(CONFIG_SYS_FLASH_BASE + (0x0100000 * sect));
wdenk2d24a3a2004-06-09 21:50:45 +0000270 SF_Normal();
271
272 printf("ok.\n");
273 }
274
275 if (ctrlc())
276 printf("User Interrupt!\n");
277
278 if (iflag)
279 enable_interrupts();
280
281 if (cflag)
282 icache_enable();
283
284 return rc;
285}
286
wdenk2d24a3a2004-06-09 21:50:45 +0000287/*-----------------------------------------------------------------------
288 * Copy memory to flash.
289 */
290
291int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) {
292 int i;
293
wdenk49822e22004-06-19 21:19:10 +0000294 for(i = 0; i < cnt; i += 4) {
wdenk2d24a3a2004-06-09 21:50:45 +0000295
296 SF_PrechargeAll();
297
298 reg_SFCTL = CMD_PROGRAM; /* Enter SyncFlash Program mode */
299 __REG(addr + i) = __REG((u32)src + i);
300
301 while(!SF_Ready());
302 }
303
304 SF_Normal();
wdenk49822e22004-06-19 21:19:10 +0000305
wdenk2d24a3a2004-06-09 21:50:45 +0000306 return ERR_OK;
307}