3 * Marius Groeger <mgroeger@sysgo.de>
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Flash Routines for AM290[48]0B devices
11 *--------------------------------------------------------------------
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 /* flash hardware ids */
35 #define VENDOR_AMD 0x0001
36 #define AMD_29DL323C_B 0x2253
38 /* Define this to include autoselect sequence in flash_init(). Does NOT
39 * work when executing from flash itself, so this should be turned
40 * on only when debugging the RAM version.
42 #undef WITH_AUTOSELECT
44 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
52 /*-----------------------------------------------------------------------
56 static unsigned char write_ull(flash_info_t *info,
57 unsigned long address,
58 volatile unsigned long long data);
60 /* from flash_asm.S */
61 extern void ull_write(unsigned long long volatile *address,
62 unsigned long long volatile *data);
63 extern void ull_read(unsigned long long volatile *address,
64 unsigned long long volatile *data);
66 /*-----------------------------------------------------------------------
69 unsigned long flash_init (void)
74 #ifdef WITH_AUTOSELECT
76 unsigned long long *f_addr = (unsigned long long *)PHYS_FLASH;
77 unsigned long long f_command, vendor, device;
78 /* Perform Autoselect */
79 f_command = 0x00AA00AA00AA00AAULL;
80 ull_write(&f_addr[0x555], &f_command);
81 f_command = 0x0055005500550055ULL;
82 ull_write(&f_addr[0x2AA], &f_command);
83 f_command = 0x0090009000900090ULL;
84 ull_write(&f_addr[0x555], &f_command);
85 ull_read(&f_addr[0], &vendor);
87 ull_read(&f_addr[1], &device);
89 f_command = 0x00F000F000F000F0ULL;
90 ull_write(&f_addr[0x555], &f_command);
91 if (vendor != VENDOR_AMD || device != AMD_29DL323C_B)
96 /* Init: no FLASHes known */
97 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
98 flash_info[i].flash_id = FLASH_UNKNOWN;
101 /* 1st bank: 8 x 32 KB sectors */
102 flash_info[0].flash_id = VENDOR_AMD << 16 | AMD_29DL323C_B;
103 flash_info[0].sector_count = 8;
104 flash_info[0].size = flash_info[0].sector_count * 32 * 1024;
106 for(i = 0; i < flash_info[0].sector_count; i++) {
107 flash_info[0].start[i] = addr;
108 addr += flash_info[0].size / flash_info[0].sector_count;
110 /* 1st bank: 63 x 256 KB sectors */
111 flash_info[1].flash_id = VENDOR_AMD << 16 | AMD_29DL323C_B;
112 flash_info[1].sector_count = 63;
113 flash_info[1].size = flash_info[1].sector_count * 256 * 1024;
114 for(i = 0; i < flash_info[1].sector_count; i++) {
115 flash_info[1].start[i] = addr;
116 addr += flash_info[1].size / flash_info[1].sector_count;
120 * protect monitor and environment sectors
123 #if CFG_MONITOR_BASE >= PHYS_FLASH
124 flash_protect(FLAG_PROTECT_SET,
126 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
128 flash_protect(FLAG_PROTECT_SET,
130 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
134 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
135 # ifndef CFG_ENV_SIZE
136 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
138 flash_protect(FLAG_PROTECT_SET,
140 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
142 flash_protect(FLAG_PROTECT_SET,
144 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
148 return flash_info[0].size + flash_info[1].size;
151 /*-----------------------------------------------------------------------
153 void flash_print_info (flash_info_t *info)
157 if (info->flash_id == FLASH_UNKNOWN) {
158 printf ("missing or unknown FLASH type\n");
162 switch (info->flash_id >> 16) {
167 printf ("Unknown Vendor ");
171 switch (info->flash_id & FLASH_TYPEMASK) {
173 printf ("AM29DL323CB (32 Mbit)\n");
176 printf ("Unknown Chip Type\n");
180 printf (" Size: %ld MB in %d Sectors\n",
181 info->size >> 20, info->sector_count);
183 printf (" Sector Start Addresses:");
184 for (i=0; i<info->sector_count; ++i) {
189 info->protect[i] ? " (RO)" : " "
196 /*-----------------------------------------------------------------------
199 int flash_erase (flash_info_t *info, int s_first, int s_last)
201 int flag, prot, sect, l_sect;
203 unsigned long long volatile *f_addr;
204 unsigned long long volatile f_command;
206 if ((s_first < 0) || (s_first > s_last)) {
207 if (info->flash_id == FLASH_UNKNOWN) {
208 printf ("- missing\n");
210 printf ("- no sectors to erase\n");
216 for (sect = s_first; sect <= s_last; sect++) {
217 if (info->protect[sect]) {
222 printf ("- Warning: %d protected sectors will not be erased!\n",
228 f_addr = (unsigned long long *)info->start[0];
229 f_command = 0x00AA00AA00AA00AAULL;
230 ull_write(&f_addr[0x555], &f_command);
231 f_command = 0x0055005500550055ULL;
232 ull_write(&f_addr[0x2AA], &f_command);
233 f_command = 0x0080008000800080ULL;
234 ull_write(&f_addr[0x555], &f_command);
235 f_command = 0x00AA00AA00AA00AAULL;
236 ull_write(&f_addr[0x555], &f_command);
237 f_command = 0x0055005500550055ULL;
238 ull_write(&f_addr[0x2AA], &f_command);
240 /* Disable interrupts which might cause a timeout here */
241 flag = disable_interrupts();
243 /* Start erase on unprotected sectors */
244 for (l_sect = -1, sect = s_first; sect<=s_last; sect++) {
245 if (info->protect[sect] == 0) { /* not protected */
248 (unsigned long long *)(info->start[sect]);
249 f_command = 0x0030003000300030ULL;
250 ull_write(f_addr, &f_command);
255 /* re-enable interrupts if necessary */
259 start = get_timer (0);
262 if (get_timer(start) > CFG_FLASH_ERASE_TOUT)
263 { /* write reset command, command address is unimportant */
264 /* this command turns the flash back to read mode */
266 (unsigned long long *)(info->start[l_sect]);
267 f_command = 0x00F000F000F000F0ULL;
268 ull_write(f_addr, &f_command);
269 printf (" timeout\n");
272 } while(*f_addr != 0xFFFFFFFFFFFFFFFFULL);
278 /*-----------------------------------------------------------------------
279 * Copy memory to flash, returns:
282 * 2 - Flash not erased
285 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
287 unsigned long cp, wp;
288 unsigned long long data;
291 wp = (addr & ~7); /* get lower long long aligned address */
294 * handle unaligned start bytes
296 if ((l = addr - wp) != 0) {
298 for (i=0, cp=wp; i<l; ++i, ++cp) {
299 data = (data << 8) | (*(uchar *)cp);
301 for (; i<8 && cnt>0; ++i) {
302 data = (data << 8) | *src++;
306 for (; cnt==0 && i<8; ++i, ++cp) {
307 data = (data << 8) | (*(uchar *)cp);
310 if ((rc = write_ull(info, wp, data)) != 0) {
317 * handle long long aligned part
321 for (i=0; i<8; ++i) {
322 data = (data << 8) | *src++;
324 if ((rc = write_ull(info, wp, data)) != 0) {
336 * handle unaligned tail bytes
339 for (i=0, cp=wp; i<8 && cnt>0; ++i, ++cp) {
340 data = (data << 8) | *src++;
343 for (; i<8; ++i, ++cp) {
344 data = (data << 8) | (*(uchar *)cp);
347 return write_ull(info, wp, data);
350 /*---------------------------------------------------------------------------
352 * FUNCTION NAME: write_ull
354 * DESCRIPTION: writes 8 bytes to flash
356 * EXTERNAL EFFECT: nothing
358 * PARAMETERS: 32 bit long pointer to address, 64 bit long pointer to data
360 * RETURNS: 0 if OK, 1 if timeout, 4 if parameter error
361 *--------------------------------------------------------------------------*/
363 static unsigned char write_ull(flash_info_t *info,
364 unsigned long address,
365 volatile unsigned long long data)
367 static unsigned long long f_command;
368 static unsigned long long *f_addr;
371 /* address muss be 8-aligned! */
375 f_addr = (unsigned long long *)info->start[0];
376 f_command = 0x00AA00AA00AA00AAULL;
377 ull_write(&f_addr[0x555], &f_command);
378 f_command = 0x0055005500550055ULL;
379 ull_write(&f_addr[0x2AA], &f_command);
380 f_command = 0x00A000A000A000A0ULL;
381 ull_write(&f_addr[0x555], &f_command);
383 f_addr = (unsigned long long *)address;
385 ull_write(f_addr, &f_command);
387 start = get_timer (0);
390 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
392 /* write reset command, command address is unimportant */
393 /* this command turns the flash back to read mode */
394 f_addr = (unsigned long long *)info->start[0];
395 f_command = 0x00F000F000F000F0ULL;
396 ull_write(f_addr, &f_command);
399 } while(*((unsigned long long *)address) != data);