3 * Marius Groeger <mgroeger@sysgo.de>
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * (C) Copyright 2000, 2001
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * Advent Networks, Inc. <http://www.adventnetworks.com>
11 * Oliver Brown <oliverb@alumni.utexas.net>
13 * SPDX-License-Identifier: GPL-2.0+
16 /*********************************************************************/
18 * This file contains the flash routines for the GW8260 board.
26 * RESTRICTIONS/LIMITATIONS:
28 * Only supports the following flash devices:
32 * Copyright (c) 2001, Advent Networks, Inc.
35 /*********************************************************************/
40 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
42 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
43 static int write_word (flash_info_t *info, ulong dest, ulong data);
45 /*********************************************************************/
47 /*********************************************************************/
50 * NAME: flash_init() - initializes flash banks
53 * This function initializes the flash bank(s).
56 * The size in bytes of the flash
58 * RESTRICTIONS/LIMITATIONS:
62 unsigned long flash_init(void)
66 /* Init: no FLASHes known */
67 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
68 flash_info[i].flash_id = FLASH_UNKNOWN;
70 /* for now, only support the 4 MB Flash SIMM */
71 (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
74 * protect monitor and environment sectors
76 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
77 flash_protect(FLAG_PROTECT_SET,
78 CONFIG_SYS_MONITOR_BASE,
79 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
83 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
84 #ifndef CONFIG_ENV_SIZE
85 #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
87 flash_protect(FLAG_PROTECT_SET,
89 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
92 return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024; /*size */
95 /*********************************************************************/
96 /* NAME: flash_print_info() - prints flash imformation */
99 /* This function prints the flash information. */
102 /* flash_info_t *info - flash information structure */
105 /* Displays flash information to console */
110 /* RESTRICTIONS/LIMITATIONS: */
113 /*********************************************************************/
114 void flash_print_info (flash_info_t *info)
118 if (info->flash_id == FLASH_UNKNOWN) {
119 printf ("missing or unknown FLASH type\n");
123 switch ((info->flash_id >> 16) & 0xff) {
128 printf ("Unknown Vendor ");
132 switch (info->flash_id & FLASH_TYPEMASK) {
134 printf ("AM29F040B (4 Mbit)\n");
137 printf ("AM29F080B (8 Mbit)\n");
140 printf ("AM29F016D (16 Mbit)\n");
143 printf ("Unknown Chip Type\n");
147 printf (" Size: %ld MB in %d Sectors\n",
148 info->size >> 20, info->sector_count);
150 printf (" Sector Start Addresses:");
151 for (i=0; i<info->sector_count; ++i) {
156 info->protect[i] ? " (RO)" : " "
163 /*********************************************************************/
164 /* The following code cannot be run from FLASH! */
165 /*********************************************************************/
167 /*********************************************************************/
168 /* NAME: flash_get_size() - detects the flash size */
171 /* 1) Reads vendor ID and devices ID from the flash devices. */
172 /* 2) Initializes flash info struct. */
173 /* 3) Return the flash size */
176 /* vu_long *addr - pointer to start of flash */
177 /* flash_info_t *info - flash information structure */
183 /* Size of the flash in bytes, or 0 if device id is unknown. */
185 /* RESTRICTIONS/LIMITATIONS: */
186 /* Only supports the following devices: */
190 /*********************************************************************/
191 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
194 vu_long vendor, devid;
195 ulong base = (ulong)addr;
197 /*printf("addr = %08lx\n", (unsigned long)addr); */
199 /* Reset and Write auto select command: read Manufacturer ID */
200 addr[0x0000] = 0xf0f0f0f0;
201 addr[0x0555] = 0xAAAAAAAA;
202 addr[0x02AA] = 0x55555555;
203 addr[0x0555] = 0x90909090;
207 /*printf("vendor = %08lx\n", vendor); */
208 if (vendor != 0x01010101) {
214 /*printf("devid = %08lx\n", devid); */
216 if ((devid & 0xff) == AMD_ID_F080B) {
217 info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
218 /* we have 16 sectors with 64KB each x 4 */
219 info->sector_count = 16;
220 info->size = 4 * info->sector_count * 64*1024;
221 } else if ((devid & 0xff) == AMD_ID_F016D){
222 info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F016D;
223 /* we have 32 sectors with 64KB each x 4 */
224 info->sector_count = 32;
225 info->size = 4 * info->sector_count * 64*1024;
230 /*printf("sector count = %08x\n", info->sector_count); */
231 /* check for protected sectors */
232 for (i = 0; i < info->sector_count; i++) {
233 /* sector base address */
234 info->start[i] = base + i * (info->size / info->sector_count);
235 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
236 /* D0 = 1 if protected */
237 addr = (volatile unsigned long *)(info->start[i]);
238 info->protect[i] = addr[2] & 1;
242 addr = (vu_long *)info->start[0];
245 addr[0] = 0xf0f0f0f0;
247 /*printf("size = %08x\n", info->size); */
251 /*********************************************************************/
252 /* NAME: flash_erase() - erases flash by sector */
255 /* This function erases flash sectors starting for s_first to */
259 /* flash_info_t *info - flash information structure */
260 /* int s_first - first sector to erase */
261 /* int s_last - last sector to erase */
267 /* Returns 0 for success, 1 for failure. */
269 /* RESTRICTIONS/LIMITATIONS: */
271 /*********************************************************************/
272 int flash_erase (flash_info_t *info, int s_first, int s_last)
274 vu_long *addr = (vu_long*)(info->start[0]);
275 int flag, prot, sect, l_sect;
276 ulong start, now, last;
278 if ((s_first < 0) || (s_first > s_last)) {
279 if (info->flash_id == FLASH_UNKNOWN) {
280 printf ("- missing\n");
282 printf ("- no sectors to erase\n");
288 for (sect = s_first; sect <= s_last; sect++) {
289 if (info->protect[sect]) {
295 printf ("- Warning: %d protected sectors will not be erased!\n",
303 /* Disable interrupts which might cause a timeout here */
304 flag = disable_interrupts();
306 addr[0x0555] = 0xAAAAAAAA;
307 addr[0x02AA] = 0x55555555;
308 addr[0x0555] = 0x80808080;
309 addr[0x0555] = 0xAAAAAAAA;
310 addr[0x02AA] = 0x55555555;
313 /* Start erase on unprotected sectors */
314 for (sect = s_first; sect <= s_last; sect++) {
315 if (info->protect[sect] == 0) { /* not protected */
316 addr = (vu_long*)(info->start[sect]);
317 addr[0] = 0x30303030;
322 /* re-enable interrupts if necessary */
326 /* wait at least 80us - let's wait 1 ms */
330 * We wait for the last triggered sector
335 start = get_timer (0);
337 addr = (vu_long*)(info->start[l_sect]);
338 while ((addr[0] & 0x80808080) != 0x80808080) {
339 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
340 printf ("Timeout\n");
343 /* show that we're waiting */
344 if ((now - last) > 1000) { /* every second */
351 /* reset to read mode */
352 addr = (volatile unsigned long *)info->start[0];
353 addr[0] = 0xF0F0F0F0; /* reset bank */
359 /*********************************************************************/
360 /* NAME: write_buff() - writes a buffer to flash */
363 /* This function copies a buffer, *src, to flash. */
366 /* flash_info_t *info - flash information structure */
367 /* uchar *src - pointer to buffer to write to flash */
368 /* ulong addr - address to start write at */
369 /* ulong cnt - number of bytes to write to flash */
376 /* 1 - write timeout */
377 /* 2 - Flash not erased */
379 /* RESTRICTIONS/LIMITATIONS: */
381 /*********************************************************************/
382 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
387 wp = (addr & ~3); /* get lower word aligned address */
390 * handle unaligned start bytes
392 if ((l = addr - wp) != 0) {
394 for (i = 0, cp = wp; i < l; ++i, ++cp) {
395 data = (data << 8) | (*(uchar *)cp);
397 for (; (i < 4) && (cnt > 0); ++i) {
398 data = (data << 8) | *src++;
402 for (; (cnt == 0) && (i < 4); ++i, ++cp) {
403 data = (data << 8) | (*(uchar *)cp);
406 if ((rc = write_word(info, wp, data)) != 0) {
413 * handle word aligned part
417 for (i = 0; i < 4; ++i) {
418 data = (data << 8) | *src++;
420 if ((rc = write_word(info, wp, data)) != 0) {
432 * handle unaligned tail bytes
435 for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
436 data = (data << 8) | *src++;
439 for (; (i < 4); ++i, ++cp) {
440 data = (data << 8) | (*(uchar *)cp);
443 return (write_word(info, wp, data));
446 /*********************************************************************/
447 /* NAME: write_word() - writes a word to flash */
450 /* This writes a single word to flash. */
453 /* flash_info_t *info - flash information structure */
454 /* ulong dest - address to write */
455 /* ulong data - data to write */
462 /* 1 - write timeout */
463 /* 2 - Flash not erased */
465 /* RESTRICTIONS/LIMITATIONS: */
467 /*********************************************************************/
468 static int write_word (flash_info_t *info, ulong dest, ulong data)
470 vu_long *addr = (vu_long*)(info->start[0]);
474 /* Check if Flash is (sufficiently) erased */
475 if ((*((vu_long *)dest) & data) != data) {
478 /* Disable interrupts which might cause a timeout here */
479 flag = disable_interrupts();
481 addr[0x0555] = 0xAAAAAAAA;
482 addr[0x02AA] = 0x55555555;
483 addr[0x0555] = 0xA0A0A0A0;
485 *((vu_long *)dest) = data;
487 /* re-enable interrupts if necessary */
491 /* data polling for D7 */
492 start = get_timer (0);
493 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
494 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
500 /*********************************************************************/
502 /*********************************************************************/