]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/eltec/mhpc/flash.c
rename CFG_ macros to CONFIG_SYS
[people/ms/u-boot.git] / board / eltec / mhpc / flash.c
1 /*
2 * (C) Copyright 2001
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 #include <common.h>
25 #include <mpc8xx.h>
26 #include <linux/byteorder/swab.h>
27
28 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
29
30 /*-----------------------------------------------------------------------
31 * Protection Flags:
32 */
33 #define FLAG_PROTECT_SET 0x01
34 #define FLAG_PROTECT_CLEAR 0x02
35
36 /* Board support for 1 or 2 flash devices */
37 #undef FLASH_PORT_WIDTH32
38 #define FLASH_PORT_WIDTH16
39
40 #ifdef FLASH_PORT_WIDTH16
41 #define FLASH_PORT_WIDTH ushort
42 #define FLASH_PORT_WIDTHV vu_short
43 #define SWAP(x) __swab16(x)
44 #else
45 #define FLASH_PORT_WIDTH ulong
46 #define FLASH_PORT_WIDTHV vu_long
47 #define SWAP(x) __swab32(x)
48 #endif
49
50 #define FPW FLASH_PORT_WIDTH
51 #define FPWV FLASH_PORT_WIDTHV
52
53 /*-----------------------------------------------------------------------
54 * Functions
55 */
56 static ulong flash_get_size (FPW *addr, flash_info_t *info);
57 static int write_data (flash_info_t *info, ulong dest, FPW data);
58 static void flash_get_offsets (ulong base, flash_info_t *info);
59
60 /*-----------------------------------------------------------------------
61 */
62
63 unsigned long flash_init (void)
64 {
65 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
66 volatile memctl8xx_t *memctl = &immap->im_memctl;
67 unsigned long size_b0;
68 int i;
69
70 /* Init: no FLASHes known */
71 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
72 flash_info[i].flash_id = FLASH_UNKNOWN;
73 }
74
75 /* Static FLASH Bank configuration here - FIXME XXX */
76 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
77
78 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
79 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
80 size_b0, size_b0<<20);
81 }
82
83 /* Remap FLASH according to real size */
84 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
85 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
86
87 /* Re-do sizing to get full correct info */
88 size_b0 = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
89
90 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
91
92 /* monitor protection ON by default */
93 (void)flash_protect(FLAG_PROTECT_SET,
94 CONFIG_SYS_FLASH_BASE,
95 CONFIG_SYS_FLASH_BASE+monitor_flash_len-1,
96 &flash_info[0]);
97
98 flash_info[0].size = size_b0;
99
100 return (size_b0);
101 }
102
103 /*-----------------------------------------------------------------------
104 */
105 static void flash_get_offsets (ulong base, flash_info_t *info)
106 {
107 int i;
108
109 if (info->flash_id == FLASH_UNKNOWN) {
110 return;
111 }
112
113 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
114 for (i = 0; i < info->sector_count; i++) {
115 info->start[i] = base + (i * 0x00020000);
116 }
117 }
118 }
119
120 /*-----------------------------------------------------------------------
121 */
122 void flash_print_info (flash_info_t *info)
123 {
124 int i;
125
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf ("missing or unknown FLASH type\n");
128 return;
129 }
130
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_INTEL: printf ("INTEL "); break;
133 default: printf ("Unknown Vendor "); break;
134 }
135
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F640J5 :
138 printf ("28F640J5 \n"); break;
139 default: printf ("Unknown Chip Type=0x%lXh\n",
140 info->flash_id & FLASH_TYPEMASK); break;
141 }
142
143 printf (" Size: %ld MB in %d Sectors\n",
144 info->size >> 20, info->sector_count);
145
146 printf (" Sector Start Addresses:");
147 for (i=0; i<info->sector_count; ++i) {
148 if ((i % 5) == 0)
149 printf ("\n ");
150 printf (" %08lX%s",
151 info->start[i],
152 info->protect[i] ? " (RO)" : " "
153 );
154 }
155 printf ("\n");
156 }
157
158 /*-----------------------------------------------------------------------
159 */
160
161
162 /*-----------------------------------------------------------------------
163 */
164
165 /*
166 * The following code cannot be run from FLASH!
167 */
168
169 static ulong flash_get_size (FPW *addr, flash_info_t *info)
170 {
171 FPW value;
172
173 /* Write auto select command: read Manufacturer ID */
174 addr[0x5555] = (FPW)0xAA00AA00;
175 addr[0x2AAA] = (FPW)0x55005500;
176 addr[0x5555] = (FPW)0x90009000;
177
178 value = SWAP(addr[0]);
179
180 switch (value) {
181 case (FPW)INTEL_MANUFACT:
182 info->flash_id = FLASH_MAN_INTEL;
183 break;
184 default:
185 info->flash_id = FLASH_UNKNOWN;
186 info->sector_count = 0;
187 info->size = 0;
188 addr[0] = (FPW)0xFF00FF00; /* restore read mode */
189 return (0); /* no or unknown flash */
190 }
191
192 value = SWAP(addr[1]); /* device ID no swap !*/
193
194 switch (value) {
195 case (FPW)INTEL_ID_28F640J5 :
196 info->flash_id += FLASH_28F640J5 ;
197 info->sector_count = 64;
198 info->size = 0x00800000;
199 break; /* => 8 MB */
200
201 default:
202 info->flash_id = FLASH_UNKNOWN;
203 break;
204 }
205
206 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
207 printf ("** ERROR: sector count %d > max (%d) **\n",
208 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
209 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
210 }
211
212 addr[0] = (FPW)0xFF00FF00; /* restore read mode */
213
214 return (info->size);
215 }
216
217
218 /*-----------------------------------------------------------------------
219 */
220
221 int flash_erase (flash_info_t *info, int s_first, int s_last)
222 {
223 int flag, prot, sect;
224 ulong type, start, now, last;
225 int rc = 0;
226
227 if ((s_first < 0) || (s_first > s_last)) {
228 if (info->flash_id == FLASH_UNKNOWN) {
229 printf ("- missing\n");
230 } else {
231 printf ("- no sectors to erase\n");
232 }
233 return 1;
234 }
235
236 type = (info->flash_id & FLASH_VENDMASK);
237 if ((type != FLASH_MAN_INTEL)) {
238 printf ("Can't erase unknown flash type %08lx - aborted\n",
239 info->flash_id);
240 return 1;
241 }
242
243 prot = 0;
244 for (sect=s_first; sect<=s_last; ++sect) {
245 if (info->protect[sect]) {
246 prot++;
247 }
248 }
249
250 if (prot) {
251 printf ("- Warning: %d protected sectors will not be erased!\n",
252 prot);
253 } else {
254 printf ("\n");
255 }
256
257 start = get_timer (0);
258 last = start;
259 /* Start erase on unprotected sectors */
260 for (sect = s_first; sect<=s_last; sect++) {
261 if (info->protect[sect] == 0) { /* not protected */
262 FPWV *addr = (FPWV *)(info->start[sect]);
263 FPW status;
264
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
267
268 *addr = (FPW)0x50005000; /* clear status register */
269 *addr = (FPW)0x20002000; /* erase setup */
270 *addr = (FPW)0xD000D000; /* erase confirm */
271
272 /* re-enable interrupts if necessary */
273 if (flag)
274 enable_interrupts();
275
276 /* wait at least 80us - let's wait 1 ms */
277 udelay (1000);
278
279 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
280 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
281 printf ("Timeout\n");
282 *addr = (FPW)0xB000B000; /* suspend erase */
283 *addr = (FPW)0xFF00FF00; /* reset to read mode */
284 rc = 1;
285 break;
286 }
287
288 /* show that we're waiting */
289 if ((now - last) > 1000) { /* every second */
290 putc ('.');
291 last = now;
292 }
293 }
294
295 *addr = (FPW)0xFF00FF00; /* reset to read mode */
296 printf (" done\n");
297 }
298 }
299 return rc;
300 }
301
302 /*-----------------------------------------------------------------------
303 * Copy memory to flash, returns:
304 * 0 - OK
305 * 1 - write timeout
306 * 2 - Flash not erased
307 * 4 - Flash not identified
308 */
309
310 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
311 {
312 ulong cp, wp;
313 FPW data;
314 int count, i, l, rc, port_width;
315
316 if (info->flash_id == FLASH_UNKNOWN) {
317 return 4;
318 }
319 /* get lower word aligned address */
320 #ifdef FLASH_PORT_WIDTH16
321 wp = (addr & ~1);
322 port_width = 2;
323 #else
324 wp = (addr & ~3);
325 port_width = 4;
326 #endif
327
328 /*
329 * handle unaligned start bytes
330 */
331 if ((l = addr - wp) != 0) {
332 data = 0;
333 for (i=0, cp=wp; i<l; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
335 }
336 for (; i<port_width && cnt>0; ++i) {
337 data = (data << 8) | *src++;
338 --cnt;
339 ++cp;
340 }
341 for (; cnt==0 && i<port_width; ++i, ++cp) {
342 data = (data << 8) | (*(uchar *)cp);
343 }
344
345 if ((rc = write_data(info, wp, data)) != 0) {
346 return (rc);
347 }
348 wp += port_width;
349 }
350
351 /*
352 * handle word aligned part
353 */
354 count = 0;
355 while (cnt >= port_width) {
356 data = 0;
357 for (i=0; i<port_width; ++i) {
358 data = (data << 8) | *src++;
359 }
360 if ((rc = write_data(info, wp, data)) != 0) {
361 return (rc);
362 }
363 wp += port_width;
364 cnt -= port_width;
365 if ((wp & 0xfff) == 0)
366 {
367 printf("%08lX",wp);
368 printf("\x1b[8D");
369 }
370 }
371
372 if (cnt == 0) {
373 return (0);
374 }
375
376 /*
377 * handle unaligned tail bytes
378 */
379 data = 0;
380 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
381 data = (data << 8) | *src++;
382 --cnt;
383 }
384 for (; i<port_width; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
386 }
387
388 return (write_data(info, wp, data));
389 }
390
391 /*-----------------------------------------------------------------------
392 * Write a word or halfword to Flash, returns:
393 * 0 - OK
394 * 1 - write timeout
395 * 2 - Flash not erased
396 */
397 static int write_data (flash_info_t *info, ulong dest, FPW data)
398 {
399 FPWV *addr = (FPWV *)dest;
400 ulong status;
401 ulong start;
402 int flag;
403
404 /* Check if Flash is (sufficiently) erased */
405 if ((*addr & data) != data) {
406 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
407 return (2);
408 }
409 /* Disable interrupts which might cause a timeout here */
410 flag = disable_interrupts();
411
412 *addr = (FPW)0x40004000; /* write setup */
413 *addr = data;
414
415 /* re-enable interrupts if necessary */
416 if (flag)
417 enable_interrupts();
418
419 start = get_timer (0);
420
421 while (((status = SWAP(*addr)) & (FPW)0x00800080) != (FPW)0x00800080) {
422 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
423 *addr = (FPW)0xFF00FF00; /* restore read mode */
424 return (1);
425 }
426 }
427
428 *addr = (FPW)0xFF00FF00; /* restore read mode */
429
430 return (0);
431 }