blob: 61a882e00b20c2b37ff3e8a302abd8015b2095ed [file] [log] [blame]
wdenk9f664dd2004-06-09 21:50:45 +00001/*
2 * board/mx1ads/syncflash.c
wdenk9e930b62004-06-19 21:19:10 +00003 *
wdenk9f664dd2004-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 *
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
26#include <common.h>
wdenk7ac16102004-08-01 22:48:16 +000027/*#include <mc9328.h>*/
28#include <asm/arch/imx-regs.h>
wdenk9f664dd2004-06-09 21:50:45 +000029
30typedef unsigned long * p_u32;
31
32/* 4Mx16x2 IAM=0 CSD1 */
33
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020034flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
wdenk9f664dd2004-06-09 21:50:45 +000035
36/* Following Setting is for CSD1 */
wdenk7ac16102004-08-01 22:48:16 +000037#define SFCTL 0x00221004
38#define reg_SFCTL __REG(SFCTL)
wdenk9f664dd2004-06-09 21:50:45 +000039
wdenk7ac16102004-08-01 22:48:16 +000040#define SYNCFLASH_A10 (0x00100000)
wdenk9f664dd2004-06-09 21:50:45 +000041
wdenk7ac16102004-08-01 22:48:16 +000042#define CMD_NORMAL (0x81020300) /* Normal Mode */
Wolfgang Denka1be4762008-05-20 16:00:29 +020043#define CMD_PREC (CMD_NORMAL + 0x10000000) /* Precharge Command */
44#define CMD_AUTO (CMD_NORMAL + 0x20000000) /* Auto Refresh Command */
45#define CMD_LMR (CMD_NORMAL + 0x30000000) /* Load Mode Register Command */
46#define CMD_LCR (CMD_NORMAL + 0x60000000) /* LCR Command */
wdenk7ac16102004-08-01 22:48:16 +000047#define CMD_PROGRAM (CMD_NORMAL + 0x70000000)
wdenk9f664dd2004-06-09 21:50:45 +000048
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049#define MODE_REG_VAL (CONFIG_SYS_FLASH_BASE+0x0008CC00) /* Cas Latency 3 */
wdenk9f664dd2004-06-09 21:50:45 +000050
51/* LCR Command */
wdenk7ac16102004-08-01 22:48:16 +000052#define LCR_READSTATUS (0x0001C000) /* 0x70 */
53#define LCR_ERASE_CONFIRM (0x00008000) /* 0x20 */
54#define LCR_ERASE_NVMODE (0x0000C000) /* 0x30 */
55#define LCR_PROG_NVMODE (0x00028000) /* 0xA0 */
56#define LCR_SR_CLEAR (0x00014000) /* 0x50 */
wdenk9f664dd2004-06-09 21:50:45 +000057
Wolfgang Denka1be4762008-05-20 16:00:29 +020058/* Get Status register */
wdenk9f664dd2004-06-09 21:50:45 +000059u32 SF_SR(void) {
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +000060 u32 tmp;
wdenk9f664dd2004-06-09 21:50:45 +000061
62 reg_SFCTL = CMD_PROGRAM;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020063 tmp = __REG(CONFIG_SYS_FLASH_BASE);
wdenk9e930b62004-06-19 21:19:10 +000064
wdenk9f664dd2004-06-09 21:50:45 +000065 reg_SFCTL = CMD_NORMAL;
66
Wolfgang Denka1be4762008-05-20 16:00:29 +020067 reg_SFCTL = CMD_LCR; /* Activate LCR Mode */
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +000068 __REG(CONFIG_SYS_FLASH_BASE + LCR_SR_CLEAR);
wdenk9f664dd2004-06-09 21:50:45 +000069
70 return tmp;
71}
72
Wolfgang Denka1be4762008-05-20 16:00:29 +020073/* check if SyncFlash is ready */
wdenk9f664dd2004-06-09 21:50:45 +000074u8 SF_Ready(void) {
75 u32 tmp;
76
77 tmp = SF_SR();
78
79 if ((tmp & 0x00800000) && (tmp & 0x001C0000)) {
80 printf ("SyncFlash Error code %08x\n",tmp);
81 };
82
83 if ((tmp & 0x00000080) && (tmp & 0x0000001C)) {
84 printf ("SyncFlash Error code %08x\n",tmp);
wdenk9f664dd2004-06-09 21:50:45 +000085 };
86
Wolfgang Denka1be4762008-05-20 16:00:29 +020087 if (tmp == 0x00800080) /* Test Bit 7 of SR */
wdenk9f664dd2004-06-09 21:50:45 +000088 return 1;
89 else
90 return 0;
91}
92
Wolfgang Denka1be4762008-05-20 16:00:29 +020093/* Issue the precharge all command */
wdenk9f664dd2004-06-09 21:50:45 +000094void SF_PrechargeAll(void) {
95
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +000096 /* Set Precharge Command */
97 reg_SFCTL = CMD_PREC;
98 /* Issue Precharge All Command */
99 __REG(CONFIG_SYS_FLASH_BASE + SYNCFLASH_A10);
wdenk9f664dd2004-06-09 21:50:45 +0000100}
101
102/* set SyncFlash to normal mode */
103void SF_Normal(void) {
104
105 SF_PrechargeAll();
wdenk9e930b62004-06-19 21:19:10 +0000106
wdenk9f664dd2004-06-09 21:50:45 +0000107 reg_SFCTL = CMD_NORMAL;
108}
109
Wolfgang Denka1be4762008-05-20 16:00:29 +0200110/* Erase SyncFlash */
wdenk9f664dd2004-06-09 21:50:45 +0000111void SF_Erase(u32 RowAddress) {
wdenk9f664dd2004-06-09 21:50:45 +0000112
113 reg_SFCTL = CMD_NORMAL;
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +0000114 __REG(RowAddress);
wdenk9f664dd2004-06-09 21:50:45 +0000115
116 reg_SFCTL = CMD_PREC;
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +0000117 __REG(RowAddress);
wdenk9e930b62004-06-19 21:19:10 +0000118
Wolfgang Denka1be4762008-05-20 16:00:29 +0200119 reg_SFCTL = CMD_LCR; /* Set LCR mode */
120 __REG(RowAddress + LCR_ERASE_CONFIRM) = 0; /* Issue Erase Setup Command */
wdenk9e930b62004-06-19 21:19:10 +0000121
Wolfgang Denka1be4762008-05-20 16:00:29 +0200122 reg_SFCTL = CMD_NORMAL; /* return to Normal mode */
123 __REG(RowAddress) = 0xD0D0D0D0; /* Confirm */
wdenk9f664dd2004-06-09 21:50:45 +0000124
125 while(!SF_Ready());
126}
127
wdenk9f664dd2004-06-09 21:50:45 +0000128void SF_NvmodeErase(void) {
129 SF_PrechargeAll();
130
131 reg_SFCTL = CMD_LCR; /* Set to LCR mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200132 __REG(CONFIG_SYS_FLASH_BASE + LCR_ERASE_NVMODE) = 0; /* Issue Erase Nvmode Reg Command */
wdenk9e930b62004-06-19 21:19:10 +0000133
Wolfgang Denka1be4762008-05-20 16:00:29 +0200134 reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200135 __REG(CONFIG_SYS_FLASH_BASE + LCR_ERASE_NVMODE) = 0xC0C0C0C0; /* Confirm */
wdenk9f664dd2004-06-09 21:50:45 +0000136
137 while(!SF_Ready());
138}
139
140void SF_NvmodeWrite(void) {
141 SF_PrechargeAll();
142
Wolfgang Denka1be4762008-05-20 16:00:29 +0200143 reg_SFCTL = CMD_LCR; /* Set to LCR mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200144 __REG(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0; /* Issue Program Nvmode reg command */
wdenk9e930b62004-06-19 21:19:10 +0000145
Wolfgang Denka1be4762008-05-20 16:00:29 +0200146 reg_SFCTL = CMD_NORMAL; /* Return to Normal mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200147 __REG(CONFIG_SYS_FLASH_BASE+LCR_PROG_NVMODE) = 0xC0C0C0C0; /* Confirm not needed */
wdenk9f664dd2004-06-09 21:50:45 +0000148}
149
wdenk9f664dd2004-06-09 21:50:45 +0000150/****************************************************************************************/
151
152ulong flash_init(void) {
153 int i, j;
wdenk9f664dd2004-06-09 21:50:45 +0000154
155/* Turn on CSD1 for negating RESETSF of SyncFLash */
156
Wolfgang Denka1be4762008-05-20 16:00:29 +0200157 reg_SFCTL |= 0x80000000; /* enable CSD1 for SyncFlash */
wdenk9f664dd2004-06-09 21:50:45 +0000158 udelay(200);
159
Wolfgang Denka1be4762008-05-20 16:00:29 +0200160 reg_SFCTL = CMD_LMR; /* Set Load Mode Register Command */
Anatolij Gustschinf0e1cd42011-11-19 13:12:16 +0000161 __REG(MODE_REG_VAL); /* Issue Load Mode Register Command */
wdenk9f664dd2004-06-09 21:50:45 +0000162
163 SF_Normal();
wdenk9e930b62004-06-19 21:19:10 +0000164
wdenk9f664dd2004-06-09 21:50:45 +0000165 i = 0;
166
Wolfgang Denka1be4762008-05-20 16:00:29 +0200167 flash_info[i].flash_id = FLASH_MAN_MT | FLASH_MT28S4M16LC;
wdenk9e930b62004-06-19 21:19:10 +0000168
Wolfgang Denka1be4762008-05-20 16:00:29 +0200169 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200170 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk9f664dd2004-06-09 21:50:45 +0000171
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200172 memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenk9f664dd2004-06-09 21:50:45 +0000173
174 for (j = 0; j < flash_info[i].sector_count; j++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200175 flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE + j * 0x00100000;
wdenk9f664dd2004-06-09 21:50:45 +0000176 }
wdenk9e930b62004-06-19 21:19:10 +0000177
wdenk9f664dd2004-06-09 21:50:45 +0000178 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200179 CONFIG_SYS_FLASH_BASE,
180 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
wdenk9f664dd2004-06-09 21:50:45 +0000181 &flash_info[0]);
182
183 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200184 CONFIG_ENV_ADDR,
185 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
wdenk9f664dd2004-06-09 21:50:45 +0000186 &flash_info[0]);
187
188 return FLASH_BANK_SIZE;
189}
190
wdenk9f664dd2004-06-09 21:50:45 +0000191void flash_print_info (flash_info_t *info) {
192
193 int i;
194
195 switch (info->flash_id & FLASH_VENDMASK) {
196 case (FLASH_MAN_MT & FLASH_VENDMASK):
197 printf("Micron: ");
198 break;
199 default:
200 printf("Unknown Vendor ");
201 break;
202 }
wdenk9e930b62004-06-19 21:19:10 +0000203
wdenk9f664dd2004-06-09 21:50:45 +0000204 switch (info->flash_id & FLASH_TYPEMASK) {
205 case (FLASH_MT28S4M16LC & FLASH_TYPEMASK):
206 printf("2x FLASH_MT28S4M16LC (16MB Total)\n");
207 break;
208 default:
209 printf("Unknown Chip Type\n");
210 return;
211 break;
212 }
213
214 printf(" Size: %ld MB in %d Sectors\n",
215 info->size >> 20, info->sector_count);
216
217 printf(" Sector Start Addresses: ");
218
219 for (i = 0; i < info->sector_count; i++) {
wdenk9e930b62004-06-19 21:19:10 +0000220 if ((i % 5) == 0)
wdenk9f664dd2004-06-09 21:50:45 +0000221 printf ("\n ");
222
223 printf (" %08lX%s", info->start[i],
224 info->protect[i] ? " (RO)" : " ");
225 }
wdenk9e930b62004-06-19 21:19:10 +0000226
wdenk9f664dd2004-06-09 21:50:45 +0000227 printf ("\n");
228}
229
wdenk9f664dd2004-06-09 21:50:45 +0000230/*-----------------------------------------------------------------------*/
231
232int flash_erase (flash_info_t *info, int s_first, int s_last) {
233 int iflag, cflag, prot, sect;
234 int rc = ERR_OK;
235
236/* first look for protection bits */
237
238 if (info->flash_id == FLASH_UNKNOWN)
239 return ERR_UNKNOWN_FLASH_TYPE;
240
wdenk9e930b62004-06-19 21:19:10 +0000241 if ((s_first < 0) || (s_first > s_last))
wdenk9f664dd2004-06-09 21:50:45 +0000242 return ERR_INVAL;
243
wdenk9e930b62004-06-19 21:19:10 +0000244 if ((info->flash_id & FLASH_VENDMASK) != (FLASH_MAN_MT & FLASH_VENDMASK))
wdenk9f664dd2004-06-09 21:50:45 +0000245 return ERR_UNKNOWN_FLASH_VENDOR;
246
247 prot = 0;
248
249 for (sect = s_first; sect <= s_last; ++sect) {
wdenk9e930b62004-06-19 21:19:10 +0000250 if (info->protect[sect])
wdenk9f664dd2004-06-09 21:50:45 +0000251 prot++;
252 }
wdenk9e930b62004-06-19 21:19:10 +0000253
wdenk9f664dd2004-06-09 21:50:45 +0000254 if (prot) {
255 printf("protected!\n");
256 return ERR_PROTECTED;
257 }
258/*
259 * Disable interrupts which might cause a timeout
260 * here. Remember that our exception vectors are
261 * at address 0 in the flash, and we don't want a
262 * (ticker) exception to happen while the flash
263 * chip is in programming mode.
264 */
265
266 cflag = icache_status();
267 icache_disable();
268 iflag = disable_interrupts();
269
270/* Start erase on unprotected sectors */
271 for (sect = s_first; sect <= s_last && !ctrlc(); sect++) {
wdenk9e930b62004-06-19 21:19:10 +0000272
wdenk9f664dd2004-06-09 21:50:45 +0000273 printf("Erasing sector %2d ... ", sect);
274
275/* arm simple, non interrupt dependent timer */
276
Graeme Russf8b82ee2011-07-15 23:31:37 +0000277 get_timer(0);
wdenk9f664dd2004-06-09 21:50:45 +0000278
279 SF_NvmodeErase();
280 SF_NvmodeWrite();
281
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200282 SF_Erase(CONFIG_SYS_FLASH_BASE + (0x0100000 * sect));
wdenk9f664dd2004-06-09 21:50:45 +0000283 SF_Normal();
284
285 printf("ok.\n");
286 }
287
288 if (ctrlc())
289 printf("User Interrupt!\n");
290
291 if (iflag)
292 enable_interrupts();
293
294 if (cflag)
295 icache_enable();
296
297 return rc;
298}
299
wdenk9f664dd2004-06-09 21:50:45 +0000300/*-----------------------------------------------------------------------
301 * Copy memory to flash.
302 */
303
304int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) {
305 int i;
306
wdenk9e930b62004-06-19 21:19:10 +0000307 for(i = 0; i < cnt; i += 4) {
wdenk9f664dd2004-06-09 21:50:45 +0000308
309 SF_PrechargeAll();
310
311 reg_SFCTL = CMD_PROGRAM; /* Enter SyncFlash Program mode */
312 __REG(addr + i) = __REG((u32)src + i);
313
314 while(!SF_Ready());
315 }
316
317 SF_Normal();
wdenk9e930b62004-06-19 21:19:10 +0000318
wdenk9f664dd2004-06-09 21:50:45 +0000319 return ERR_OK;
320}