blob: 067a2609065a3865b14be06e15cb44ab5bd7ab4b [file] [log] [blame]
Wolfgang Denk97caf672006-03-12 02:12:27 +01001/*
2 * U-boot - flash.c Flash driver for PSD4256GV
3 *
4 * Copyright (c) 2005 blackfin.uclinux.org
5 * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
6 *
7 * (C) Copyright 2000-2004
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
Aubrey Li3c569952007-03-12 00:25:14 +080029#include <asm/io.h>
Wolfgang Denk97caf672006-03-12 02:12:27 +010030#include "flash-defines.h"
31
32void flash_reset(void)
33{
34 reset_flash();
35}
36
Aubrey.Li9da597f2007-03-09 13:38:44 +080037unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
Wolfgang Denk97caf672006-03-12 02:12:27 +010038{
39 int id = 0, i = 0;
40 static int FlagDev = 1;
41
42 id = get_codes();
Aubrey.Li9da597f2007-03-09 13:38:44 +080043 if (FlagDev) {
Wolfgang Denk97caf672006-03-12 02:12:27 +010044#ifdef DEBUG
45 printf("Device ID of the Flash is %x\n", id);
46#endif
47 FlagDev = 0;
48 }
49 info->flash_id = id;
50
51 switch (bank_flag) {
52 case 0:
53 for (i = PriFlashABegin; i < SecFlashABegin; i++)
54 info->start[i] = (baseaddr + (i * AFP_SectorSize1));
55 info->size = 0x200000;
56 info->sector_count = 32;
57 break;
58 case 1:
59 info->start[0] = baseaddr + SecFlashASec1Off;
60 info->start[1] = baseaddr + SecFlashASec2Off;
61 info->start[2] = baseaddr + SecFlashASec3Off;
62 info->start[3] = baseaddr + SecFlashASec4Off;
63 info->size = 0x10000;
64 info->sector_count = 4;
65 break;
66 case 2:
67 info->start[0] = baseaddr + SecFlashBSec1Off;
68 info->start[1] = baseaddr + SecFlashBSec2Off;
69 info->start[2] = baseaddr + SecFlashBSec3Off;
70 info->start[3] = baseaddr + SecFlashBSec4Off;
71 info->size = 0x10000;
72 info->sector_count = 4;
73 break;
74 }
75 return (info->size);
76}
77
78unsigned long flash_init(void)
79{
80 unsigned long size_b0, size_b1, size_b2;
81 int i;
82
83 size_b0 = size_b1 = size_b2 = 0;
84#ifdef DEBUG
85 printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
86 printf("Memory Map for the Flash\n");
87 printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
88 printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
89 printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
90 printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
91 printf("Please type command flinfo for information on Sectors \n");
92#endif
93 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
94 flash_info[i].flash_id = FLASH_UNKNOWN;
95 }
96
97 size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
98 size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
99 size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
100
101 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
102 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
Aubrey.Li9da597f2007-03-09 13:38:44 +0800103 size_b0, size_b0 >> 20);
Wolfgang Denk97caf672006-03-12 02:12:27 +0100104 }
105
Aubrey.Li9da597f2007-03-09 13:38:44 +0800106 (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
107 (flash_info[0].start[2] - 1), &flash_info[0]);
Wolfgang Denk97caf672006-03-12 02:12:27 +0100108
109 return (size_b0 + size_b1 + size_b2);
110}
111
112void flash_print_info(flash_info_t * info)
113{
114 int i;
115
116 if (info->flash_id == FLASH_UNKNOWN) {
117 printf("missing or unknown FLASH type\n");
118 return;
119 }
120
121 switch (info->flash_id) {
122 case FLASH_PSD4256GV:
123 printf("ST Microelectronics ");
124 break;
125 default:
Aubrey.Li9da597f2007-03-09 13:38:44 +0800126 printf("Unknown Vendor: (0x%08X) ", info->flash_id);
Wolfgang Denk97caf672006-03-12 02:12:27 +0100127 break;
128 }
129 for (i = 0; i < info->sector_count; ++i) {
130 if ((i % 5) == 0)
131 printf("\n ");
132 printf(" %08lX%s",
Aubrey.Li9da597f2007-03-09 13:38:44 +0800133 info->start[i], info->protect[i] ? " (RO)" : " ");
Wolfgang Denk97caf672006-03-12 02:12:27 +0100134 }
135 printf("\n");
136 return;
137}
138
139int flash_erase(flash_info_t * info, int s_first, int s_last)
140{
Aubrey.Li9da597f2007-03-09 13:38:44 +0800141 int cnt = 0, i;
142 int prot, sect;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100143
144 prot = 0;
Wolfgang Denk2bad8682006-03-12 02:55:22 +0100145 for (sect = s_first; sect <= s_last; ++sect) {
146 if (info->protect[sect])
147 prot++;
148 }
Wolfgang Denk97caf672006-03-12 02:12:27 +0100149
150 if (prot)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800151 printf("- Warning: %d protected sectors will not be erased!\n",
152 prot);
Wolfgang Denk2bad8682006-03-12 02:55:22 +0100153 else
Aubrey.Li9da597f2007-03-09 13:38:44 +0800154 printf("\n");
Wolfgang Denk97caf672006-03-12 02:12:27 +0100155
156 cnt = s_last - s_first + 1;
157
158 if (cnt == FLASH_TOT_SECT) {
159 printf("Erasing flash, Please Wait \n");
Aubrey.Li9da597f2007-03-09 13:38:44 +0800160 if (erase_flash() < 0) {
Wolfgang Denk97caf672006-03-12 02:12:27 +0100161 printf("Erasing flash failed \n");
162 return FLASH_FAIL;
163 }
164 } else {
165 printf("Erasing Flash locations, Please Wait\n");
166 for (i = s_first; i <= s_last; i++) {
167 if (info->protect[i] == 0) { /* not protected */
Aubrey.Li9da597f2007-03-09 13:38:44 +0800168 if (erase_block_flash(i, info->start[i]) < 0) {
Wolfgang Denk97caf672006-03-12 02:12:27 +0100169 printf("Error Sector erasing \n");
170 return FLASH_FAIL;
171 }
172 }
173 }
174 }
175 return FLASH_SUCCESS;
176}
177
178int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
179{
180 int ret;
181
Aubrey.Li9da597f2007-03-09 13:38:44 +0800182 ret = write_data(addr, cnt, 1, (int *)src);
183 if (ret == FLASH_FAIL)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100184 return ERR_NOT_ERASED;
185 return FLASH_SUCCESS;
186}
187
Wolfgang Denk97caf672006-03-12 02:12:27 +0100188int write_data(long lStart, long lCount, long lStride, int *pnData)
189{
190 long i = 0;
191 int j = 0;
192 unsigned long ulOffset = lStart - CFG_FLASH_BASE;
193 int d;
194 int iShift = 0;
195 int iNumWords = 2;
196 int nLeftover = lCount % 4;
197 int nSector = 0;
198
199 for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
200 for (iShift = 0, j = 0; (j < iNumWords);
Aubrey.Li9da597f2007-03-09 13:38:44 +0800201 j++, ulOffset += (lStride * 2)) {
Wolfgang Denk97caf672006-03-12 02:12:27 +0100202 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800203 && (ulOffset < INVALIDLOCNEND)) {
204 printf
205 ("Invalid locations, Try writing to another location \n");
Wolfgang Denk97caf672006-03-12 02:12:27 +0100206 return FLASH_FAIL;
207 }
208 get_sector_number(ulOffset, &nSector);
Aubrey.Li9da597f2007-03-09 13:38:44 +0800209 read_flash(ulOffset, &d);
210 if (d != 0xffff) {
211 printf
212 ("Flash not erased at offset 0x%x Please erase to reprogram \n",
213 ulOffset);
Wolfgang Denk97caf672006-03-12 02:12:27 +0100214 return FLASH_FAIL;
215 }
216 unlock_flash(ulOffset);
Aubrey.Li9da597f2007-03-09 13:38:44 +0800217 if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
Wolfgang Denk97caf672006-03-12 02:12:27 +0100218 printf("Error programming the flash \n");
219 return FLASH_FAIL;
220 }
221 iShift += 16;
222 }
223 }
224 if (nLeftover > 0) {
225 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800226 && (ulOffset < INVALIDLOCNEND))
227 return FLASH_FAIL;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100228 get_sector_number(ulOffset, &nSector);
Aubrey.Li9da597f2007-03-09 13:38:44 +0800229 read_flash(ulOffset, &d);
230 if (d != 0xffff) {
231 printf
232 ("Flash already programmed. Please erase to reprogram \n");
233 printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
Wolfgang Denk97caf672006-03-12 02:12:27 +0100234 return FLASH_FAIL;
235 }
236 unlock_flash(ulOffset);
Aubrey.Li9da597f2007-03-09 13:38:44 +0800237 if (write_flash(ulOffset, pnData[i]) < 0) {
Wolfgang Denk97caf672006-03-12 02:12:27 +0100238 printf("Error programming the flash \n");
239 return FLASH_FAIL;
240 }
241 }
242 return FLASH_SUCCESS;
243}
244
245int read_data(long ulStart, long lCount, long lStride, int *pnData)
246{
247 long i = 0;
248 int j = 0;
249 long ulOffset = ulStart;
250 int iShift = 0;
251 int iNumWords = 2;
252 int nLeftover = lCount % 4;
253 int nHi, nLow;
254 int nSector = 0;
255
256 for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
257 for (iShift = 0, j = 0; j < iNumWords; j += 2) {
258 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800259 && (ulOffset < INVALIDLOCNEND))
260 return FLASH_FAIL;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100261
262 get_sector_number(ulOffset, &nSector);
263 read_flash(ulOffset, &nLow);
264 ulOffset += (lStride * 2);
265 read_flash(ulOffset, &nHi);
266 ulOffset += (lStride * 2);
267 pnData[i] = (nHi << 16) | nLow;
268 }
269 }
270 if (nLeftover > 0) {
271 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800272 && (ulOffset < INVALIDLOCNEND))
273 return FLASH_FAIL;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100274
275 get_sector_number(ulOffset, &nSector);
276 read_flash(ulOffset, &pnData[i]);
277 }
278 return FLASH_SUCCESS;
279}
280
281int write_flash(long nOffset, int nValue)
282{
283 long addr;
284
285 addr = (CFG_FLASH_BASE + nOffset);
Aubrey Li3c569952007-03-12 00:25:14 +0800286 sync();
Aubrey.Li9da597f2007-03-09 13:38:44 +0800287 *(unsigned volatile short *)addr = nValue;
Aubrey Li3c569952007-03-12 00:25:14 +0800288 sync();
Aubrey.Li9da597f2007-03-09 13:38:44 +0800289 if (poll_toggle_bit(nOffset) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100290 return FLASH_FAIL;
291 return FLASH_SUCCESS;
292}
293
294int read_flash(long nOffset, int *pnValue)
295{
296 int nValue = 0x0;
297 long addr = (CFG_FLASH_BASE + nOffset);
298
299 if (nOffset != 0x2)
300 reset_flash();
Aubrey Li3c569952007-03-12 00:25:14 +0800301 sync();
Aubrey.Li9da597f2007-03-09 13:38:44 +0800302 nValue = *(volatile unsigned short *)addr;
Aubrey Li3c569952007-03-12 00:25:14 +0800303 sync();
Wolfgang Denk97caf672006-03-12 02:12:27 +0100304 *pnValue = nValue;
305 return TRUE;
306}
307
308int poll_toggle_bit(long lOffset)
309{
Aubrey.Li9da597f2007-03-09 13:38:44 +0800310 unsigned int u1, u2;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100311 unsigned long timeout = 0xFFFFFFFF;
Aubrey.Li9da597f2007-03-09 13:38:44 +0800312 volatile unsigned long *FB =
313 (volatile unsigned long *)(0x20000000 + lOffset);
314 while (1) {
315 if (timeout < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100316 break;
317 u1 = *(volatile unsigned short *)FB;
318 u2 = *(volatile unsigned short *)FB;
Aubrey.Li9da597f2007-03-09 13:38:44 +0800319 if ((u1 & 0x0040) == (u2 & 0x0040))
Wolfgang Denk97caf672006-03-12 02:12:27 +0100320 return FLASH_SUCCESS;
Aubrey.Li9da597f2007-03-09 13:38:44 +0800321 if ((u2 & 0x0020) == 0x0000)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100322 continue;
323 u1 = *(volatile unsigned short *)FB;
Aubrey.Li9da597f2007-03-09 13:38:44 +0800324 if ((u2 & 0x0040) == (u1 & 0x0040))
Wolfgang Denk97caf672006-03-12 02:12:27 +0100325 return FLASH_SUCCESS;
326 else {
327 reset_flash();
328 return FLASH_FAIL;
329 }
330 timeout--;
331 }
332 printf("Time out occured \n");
Aubrey.Li9da597f2007-03-09 13:38:44 +0800333 if (timeout < 0)
334 return FLASH_FAIL;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100335}
336
337void reset_flash(void)
338{
339 write_flash(WRITESEQ1, RESET_VAL);
340 /* Wait for 10 micro seconds */
341 udelay(10);
342}
343
344int erase_flash(void)
345{
346 write_flash(WRITESEQ1, WRITEDATA1);
347 write_flash(WRITESEQ2, WRITEDATA2);
348 write_flash(WRITESEQ3, WRITEDATA3);
349 write_flash(WRITESEQ4, WRITEDATA4);
350 write_flash(WRITESEQ5, WRITEDATA5);
351 write_flash(WRITESEQ6, WRITEDATA6);
352
Aubrey.Li9da597f2007-03-09 13:38:44 +0800353 if (poll_toggle_bit(0x0000) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100354 return FLASH_FAIL;
355
356 write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
357 write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
358 write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
359 write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
360 write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
361 write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
362
Aubrey.Li9da597f2007-03-09 13:38:44 +0800363 if (poll_toggle_bit(SecFlashASec1Off) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100364 return FLASH_FAIL;
365
366 write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
367 write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
368 write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
369 write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
370 write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
371 write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
372
Aubrey.Li9da597f2007-03-09 13:38:44 +0800373 if (poll_toggle_bit(PriFlashBOff) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100374 return FLASH_FAIL;
375
376 write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
377 write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
378 write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
379 write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
380 write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
381 write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
382
Aubrey.Li9da597f2007-03-09 13:38:44 +0800383 if (poll_toggle_bit(SecFlashBOff) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100384 return FLASH_FAIL;
385
386 return FLASH_SUCCESS;
387}
388
389int erase_block_flash(int nBlock, unsigned long address)
390{
391 long ulSectorOff = 0x0;
392
393 if ((nBlock < 0) || (nBlock > AFP_NumSectors))
394 return FALSE;
395
396 ulSectorOff = (address - CFG_FLASH_BASE);
397
398 write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
399 write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
400 write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
401 write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
402 write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
403
404 write_flash(ulSectorOff, BlockEraseVal);
405
Aubrey.Li9da597f2007-03-09 13:38:44 +0800406 if (poll_toggle_bit(ulSectorOff) < 0)
Wolfgang Denk97caf672006-03-12 02:12:27 +0100407 return FLASH_FAIL;
408
409 return FLASH_SUCCESS;
410}
411
412void unlock_flash(long ulOffset)
413{
414 unsigned long ulOffsetAddr = ulOffset;
415 ulOffsetAddr &= 0xFFFF0000;
416
417 write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
418 write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
419 write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
420}
421
422int get_codes()
423{
424 int dev_id = 0;
425
426 write_flash(WRITESEQ1, GETCODEDATA1);
427 write_flash(WRITESEQ2, GETCODEDATA2);
428 write_flash(WRITESEQ3, GETCODEDATA3);
429
430 read_flash(0x0002, &dev_id);
431 dev_id &= 0x00FF;
432
433 reset_flash();
434
435 return dev_id;
436}
437
438void get_sector_number(long ulOffset, int *pnSector)
439{
440 int nSector = 0;
441
442 if (ulOffset >= SecFlashAOff) {
443 if ((ulOffset < SecFlashASec1Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800444 && (ulOffset < SecFlashASec2Off)) {
445 nSector = SECT32;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100446 } else if ((ulOffset >= SecFlashASec2Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800447 && (ulOffset < SecFlashASec3Off)) {
448 nSector = SECT33;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100449 } else if ((ulOffset >= SecFlashASec3Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800450 && (ulOffset < SecFlashASec4Off)) {
451 nSector = SECT34;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100452 } else if ((ulOffset >= SecFlashASec4Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800453 && (ulOffset < SecFlashAEndOff)) {
454 nSector = SECT35;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100455 }
456 } else if (ulOffset >= SecFlashBOff) {
457 if ((ulOffset < SecFlashBSec1Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800458 && (ulOffset < SecFlashBSec2Off)) {
459 nSector = SECT36;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100460 }
461 if ((ulOffset < SecFlashBSec2Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800462 && (ulOffset < SecFlashBSec3Off)) {
463 nSector = SECT37;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100464 }
465 if ((ulOffset < SecFlashBSec3Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800466 && (ulOffset < SecFlashBSec4Off)) {
467 nSector = SECT38;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100468 }
469 if ((ulOffset < SecFlashBSec4Off)
Aubrey.Li9da597f2007-03-09 13:38:44 +0800470 && (ulOffset < SecFlashBEndOff)) {
471 nSector = SECT39;
Wolfgang Denk97caf672006-03-12 02:12:27 +0100472 }
473 } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
474 nSector = ulOffset & 0xffff0000;
475 nSector = ulOffset >> 16;
476 nSector = nSector & 0x000ff;
477 }
478
479 if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
480 *pnSector = nSector;
481 }
482}