blob: c64f7dc358d3a6e153f750b0cc14a8362aa1f09c [file] [log] [blame]
wdenk4a551702003-10-08 23:26:14 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24
25#include <common.h>
26#include <nios.h>
27
28/*---------------------------------------------------------------------*/
29#define BANKSZ (8 * 1024 * 1024)
30#define SECTSZ (64 * 1024)
31#define USERFLASH (2 * 1024 * 1024) /* bottom 2 MB for user */
32
33flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
34
35#define FLASH_WORD_SIZE unsigned char
36
37/*---------------------------------------------------------------------*/
38
39unsigned long flash_init (void)
40{
41 int i;
42 unsigned long addr;
43 flash_info_t *fli = &flash_info[0];
44
45 fli->size = BANKSZ;
46 fli->sector_count = CFG_MAX_FLASH_SECT;
47 fli->flash_id = FLASH_MAN_AMD;
48
49 addr = CFG_FLASH_BASE;
50 for (i = 0; i < fli->sector_count; ++i) {
51 fli->start[i] = addr;
52 addr += SECTSZ;
53
54 /* Protect all but 2 MByte user area */
55 if (addr < (CFG_FLASH_BASE + USERFLASH))
56 fli->protect[i] = 0;
57 else
58 fli->protect[i] = 1;
59 }
60
61 return (BANKSZ);
62}
63
64/*--------------------------------------------------------------------*/
65void flash_print_info (flash_info_t * info)
66{
67 int i, k;
68 unsigned long size;
69 int erased;
70 volatile unsigned char *flash;
71
72 printf (" Size: %ld KB in %d Sectors\n",
73 info->size >> 10, info->sector_count);
74 printf (" Sector Start Addresses:");
75 for (i = 0; i < info->sector_count; ++i) {
76
77 /* Check if whole sector is erased */
78 if (i != (info->sector_count - 1))
79 size = info->start[i + 1] - info->start[i];
80 else
81 size = info->start[0] + info->size - info->start[i];
82 erased = 1;
83 flash = (volatile unsigned char *) info->start[i];
84 for (k = 0; k < size; k++) {
85 if (*flash++ != 0xff) {
86 erased = 0;
87 break;
88 }
89 }
90
91 /* Print the info */
92 if ((i % 5) == 0)
93 printf ("\n ");
94 printf (" %08lX%s%s", info->start[i], erased ? " E" : " ",
95 info->protect[i] ? "RO " : " ");
96 }
97 printf ("\n");
98}
99
100/*-------------------------------------------------------------------*/
101
102
103int flash_erase (flash_info_t * info, int s_first, int s_last)
104{
105 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
106 volatile FLASH_WORD_SIZE *addr2;
107 int prot, sect;
108 int any = 0;
109 unsigned oldpri;
110 ulong start;
111
112 /* Some sanity checking */
113 if ((s_first < 0) || (s_first > s_last)) {
114 printf ("- no sectors to erase\n");
115 return 1;
116 }
117
118 prot = 0;
119 for (sect = s_first; sect <= s_last; ++sect) {
120 if (info->protect[sect]) {
121 prot++;
122 }
123 }
124 if (prot) {
125 printf ("- Warning: %d protected sectors will not be erased!\n",
126 prot);
127 } else {
128 printf ("\n");
129 }
130
131 /* NOTE: disabling interrupts on Nios can be very bad since it
132 * also disables the LO_LIMIT exception. It's better here to
133 * set the interrupt priority to 3 & restore it when we're done.
134 */
135 oldpri = ipri (3);
136
137 /* It's ok to erase multiple sectors provided we don't delay more
138 * than 50 usec between cmds ... at which point the erase time-out
139 * occurs. So don't go and put printf() calls in the loop ... it
140 * won't be very helpful ;-)
141 */
142 for (sect = s_first; sect <= s_last; sect++) {
143 if (info->protect[sect] == 0) { /* not protected */
144 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
145 *addr = 0xaa;
146 *addr = 0x55;
147 *addr = 0x80;
148 *addr = 0xaa;
149 *addr = 0x55;
150 *addr2 = 0x30;
151 any = 1;
152 }
153 }
154
155 /* Now just wait for 0xff & provide some user feedback while
156 * we wait.
157 */
158 if (any) {
159 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
160 start = get_timer (0);
161 while (*addr2 != 0xff) {
162 udelay (1000 * 1000);
163 putc ('.');
164 if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
165 printf ("timeout\n");
166 return 1;
167 }
168 }
169 printf ("\n");
170 }
171
172 /* Restore interrupt priority */
173 ipri (oldpri);
174
175 return 0;
176}
177
178/*-----------------------------------------------------------------------
179 * Copy memory to flash, returns:
180 * 0 - OK
181 * 1 - write timeout
182 * 2 - Flash not erased
183 */
184
185int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
186{
187
188 vu_char *cmd = (vu_char *) info->start[0];
189 vu_char *dst = (vu_char *) addr;
190 unsigned char b;
191 unsigned oldpri;
192 ulong start;
193
194 while (cnt) {
195 /* Check for sufficient erase */
196 b = *src;
197 if ((*dst & b) != b) {
198 printf ("%02x : %02x\n", *dst, b);
199 return (2);
200 }
201
202 /* Disable interrupts other than window underflow
203 * (interrupt priority 2)
204 */
205 oldpri = ipri (3);
206 *cmd = 0xaa;
207 *cmd = 0x55;
208 *cmd = 0xa0;
209 *dst = b;
210
211 /* Verify write */
212 start = get_timer (0);
213 while (*dst != b) {
214 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
215 ipri (oldpri);
216 return 1;
217 }
218 }
219 dst++;
220 src++;
221 cnt--;
222 ipri (oldpri);
223 }
224
225 return (0);
226}