blob: 409a7a82fb49c3dc05509515caa2897ecb8addbe [file] [log] [blame]
wdenk5c952cf2004-10-10 21:27:30 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk5c952cf2004-10-10 21:27:30 +00006 */
7
8
9#include <common.h>
10#if defined(CONFIG_NIOS)
11#include <nios.h>
12#else
Scott McNutt3d22d0b2006-06-08 12:03:21 -040013#include <asm/io.h>
wdenk5c952cf2004-10-10 21:27:30 +000014#endif
15
16#define SECTSZ (64 * 1024)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020017flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk5c952cf2004-10-10 21:27:30 +000018
19/*----------------------------------------------------------------------*/
20unsigned long flash_init (void)
21{
22 int i;
23 unsigned long addr;
24 flash_info_t *fli = &flash_info[0];
25
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026 fli->size = CONFIG_SYS_FLASH_SIZE;
27 fli->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk5c952cf2004-10-10 21:27:30 +000028 fli->flash_id = FLASH_MAN_AMD + FLASH_AMDLV065D;
29
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030 addr = CONFIG_SYS_FLASH_BASE;
wdenk5c952cf2004-10-10 21:27:30 +000031 for (i = 0; i < fli->sector_count; ++i) {
32 fli->start[i] = addr;
33 addr += SECTSZ;
34 fli->protect[i] = 1;
35 }
36
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020037 return (CONFIG_SYS_FLASH_SIZE);
wdenk5c952cf2004-10-10 21:27:30 +000038}
39/*--------------------------------------------------------------------*/
40void flash_print_info (flash_info_t * info)
41{
42 int i, k;
wdenk5c952cf2004-10-10 21:27:30 +000043 int erased;
Scott McNutt3d22d0b2006-06-08 12:03:21 -040044 unsigned long *addr;
wdenk5c952cf2004-10-10 21:27:30 +000045
46 printf (" Size: %ld KB in %d Sectors\n",
47 info->size >> 10, info->sector_count);
48 printf (" Sector Start Addresses:");
49 for (i = 0; i < info->sector_count; ++i) {
50
51 /* Check if whole sector is erased */
wdenk5c952cf2004-10-10 21:27:30 +000052 erased = 1;
Scott McNutt3d22d0b2006-06-08 12:03:21 -040053 addr = (unsigned long *) info->start[i];
54 for (k = 0; k < SECTSZ/sizeof(unsigned long); k++) {
55 if ( readl(addr++) != (unsigned long)-1) {
wdenk5c952cf2004-10-10 21:27:30 +000056 erased = 0;
57 break;
58 }
59 }
60
61 /* Print the info */
62 if ((i % 5) == 0)
63 printf ("\n ");
64 printf (" %08lX%s%s",
Scott McNutt3d22d0b2006-06-08 12:03:21 -040065 info->start[i],
wdenk5c952cf2004-10-10 21:27:30 +000066 erased ? " E" : " ",
67 info->protect[i] ? "RO " : " ");
68 }
69 printf ("\n");
70}
71
72/*-------------------------------------------------------------------*/
73
74
75int flash_erase (flash_info_t * info, int s_first, int s_last)
76{
Scott McNutt3d22d0b2006-06-08 12:03:21 -040077 unsigned char *addr = (unsigned char *) info->start[0];
78 unsigned char *addr2;
wdenk5c952cf2004-10-10 21:27:30 +000079 int prot, sect;
80 ulong start;
81
82 /* Some sanity checking */
83 if ((s_first < 0) || (s_first > s_last)) {
84 printf ("- no sectors to erase\n");
85 return 1;
86 }
87
88 prot = 0;
89 for (sect = s_first; sect <= s_last; ++sect) {
90 if (info->protect[sect]) {
91 prot++;
92 }
93 }
94 if (prot) {
95 printf ("- Warning: %d protected sectors will not be erased!\n",
96 prot);
97 } else {
98 printf ("\n");
99 }
100
101 /* It's ok to erase multiple sectors provided we don't delay more
102 * than 50 usec between cmds ... at which point the erase time-out
103 * occurs. So don't go and put printf() calls in the loop ... it
104 * won't be very helpful ;-)
105 */
106 for (sect = s_first; sect <= s_last; sect++) {
107 if (info->protect[sect] == 0) { /* not protected */
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400108 addr2 = (unsigned char *) info->start[sect];
Scott McNutt3ea00372010-03-21 21:24:43 -0400109 writeb (0xaa, addr);
110 writeb (0x55, addr);
111 writeb (0x80, addr);
112 writeb (0xaa, addr);
113 writeb (0x55, addr);
114 writeb (0x30, addr2);
wdenk5c952cf2004-10-10 21:27:30 +0000115 /* Now just wait for 0xff & provide some user
116 * feedback while we wait.
117 */
118 start = get_timer (0);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400119 while ( readb (addr2) != 0xff) {
wdenk5c952cf2004-10-10 21:27:30 +0000120 udelay (1000 * 1000);
121 putc ('.');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk5c952cf2004-10-10 21:27:30 +0000123 printf ("timeout\n");
124 return 1;
125 }
126 }
127 }
128 }
129 printf ("\n");
130 return 0;
131}
132
133/*-----------------------------------------------------------------------
134 * Copy memory to flash, returns:
135 * 0 - OK
136 * 1 - write timeout
137 * 2 - Flash not erased
138 */
139
140int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
141{
142
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400143 vu_char *cmd = (vu_char *) info->start[0];
144 vu_char *dst = (vu_char *) addr;
wdenk5c952cf2004-10-10 21:27:30 +0000145 unsigned char b;
146 ulong start;
147
148 while (cnt) {
149 /* Check for sufficient erase */
150 b = *src;
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400151 if ((readb (dst) & b) != b) {
152 printf ("%02x : %02x\n", readb (dst), b);
wdenk5c952cf2004-10-10 21:27:30 +0000153 return (2);
154 }
155
Scott McNutt3ea00372010-03-21 21:24:43 -0400156 writeb (0xaa, cmd);
157 writeb (0x55, cmd);
158 writeb (0xa0, cmd);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400159 writeb (dst, b);
wdenk5c952cf2004-10-10 21:27:30 +0000160
161 /* Verify write */
162 start = get_timer (0);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400163 while (readb (dst) != b) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200164 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk5c952cf2004-10-10 21:27:30 +0000165 return 1;
166 }
167 }
168 dst++;
169 src++;
170 cnt--;
171 }
172
173 return (0);
174}