blob: 1b56d5bc6a6886b2e09401a5b30374a2cbd37025 [file] [log] [blame]
Wolfgang Denk6cb142f2006-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
29#include "flash-defines.h"
30
31void flash_reset(void)
32{
33 reset_flash();
34}
35
Aubrey.Li3f0606a2007-03-09 13:38:44 +080036unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010037{
38 int id = 0, i = 0;
39 static int FlagDev = 1;
40
41 id = get_codes();
Aubrey.Li3f0606a2007-03-09 13:38:44 +080042 if (FlagDev) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010043#ifdef DEBUG
44 printf("Device ID of the Flash is %x\n", id);
45#endif
46 FlagDev = 0;
47 }
48 info->flash_id = id;
49
50 switch (bank_flag) {
51 case 0:
52 for (i = PriFlashABegin; i < SecFlashABegin; i++)
53 info->start[i] = (baseaddr + (i * AFP_SectorSize1));
54 info->size = 0x200000;
55 info->sector_count = 32;
56 break;
57 case 1:
58 info->start[0] = baseaddr + SecFlashASec1Off;
59 info->start[1] = baseaddr + SecFlashASec2Off;
60 info->start[2] = baseaddr + SecFlashASec3Off;
61 info->start[3] = baseaddr + SecFlashASec4Off;
62 info->size = 0x10000;
63 info->sector_count = 4;
64 break;
65 case 2:
66 info->start[0] = baseaddr + SecFlashBSec1Off;
67 info->start[1] = baseaddr + SecFlashBSec2Off;
68 info->start[2] = baseaddr + SecFlashBSec3Off;
69 info->start[3] = baseaddr + SecFlashBSec4Off;
70 info->size = 0x10000;
71 info->sector_count = 4;
72 break;
73 }
74 return (info->size);
75}
76
77unsigned long flash_init(void)
78{
79 unsigned long size_b0, size_b1, size_b2;
80 int i;
81
82 size_b0 = size_b1 = size_b2 = 0;
83#ifdef DEBUG
84 printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
85 printf("Memory Map for the Flash\n");
86 printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
87 printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
88 printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
89 printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
90 printf("Please type command flinfo for information on Sectors \n");
91#endif
92 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
93 flash_info[i].flash_id = FLASH_UNKNOWN;
94 }
95
96 size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
97 size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
98 size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
99
100 if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
101 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800102 size_b0, size_b0 >> 20);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100103 }
104
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800105 (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
106 (flash_info[0].start[2] - 1), &flash_info[0]);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100107
108 return (size_b0 + size_b1 + size_b2);
109}
110
111void flash_print_info(flash_info_t * info)
112{
113 int i;
114
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf("missing or unknown FLASH type\n");
117 return;
118 }
119
120 switch (info->flash_id) {
121 case FLASH_PSD4256GV:
122 printf("ST Microelectronics ");
123 break;
124 default:
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800125 printf("Unknown Vendor: (0x%08X) ", info->flash_id);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100126 break;
127 }
128 for (i = 0; i < info->sector_count; ++i) {
129 if ((i % 5) == 0)
130 printf("\n ");
131 printf(" %08lX%s",
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800132 info->start[i], info->protect[i] ? " (RO)" : " ");
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100133 }
134 printf("\n");
135 return;
136}
137
138int flash_erase(flash_info_t * info, int s_first, int s_last)
139{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800140 int cnt = 0, i;
141 int prot, sect;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100142
143 prot = 0;
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100144 for (sect = s_first; sect <= s_last; ++sect) {
145 if (info->protect[sect])
146 prot++;
147 }
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100148
149 if (prot)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800150 printf("- Warning: %d protected sectors will not be erased!\n",
151 prot);
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100152 else
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800153 printf("\n");
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100154
155 cnt = s_last - s_first + 1;
156
157 if (cnt == FLASH_TOT_SECT) {
158 printf("Erasing flash, Please Wait \n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800159 if (erase_flash() < 0) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100160 printf("Erasing flash failed \n");
161 return FLASH_FAIL;
162 }
163 } else {
164 printf("Erasing Flash locations, Please Wait\n");
165 for (i = s_first; i <= s_last; i++) {
166 if (info->protect[i] == 0) { /* not protected */
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800167 if (erase_block_flash(i, info->start[i]) < 0) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100168 printf("Error Sector erasing \n");
169 return FLASH_FAIL;
170 }
171 }
172 }
173 }
174 return FLASH_SUCCESS;
175}
176
177int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
178{
179 int ret;
180
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800181 ret = write_data(addr, cnt, 1, (int *)src);
182 if (ret == FLASH_FAIL)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100183 return ERR_NOT_ERASED;
184 return FLASH_SUCCESS;
185}
186
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100187int write_data(long lStart, long lCount, long lStride, int *pnData)
188{
189 long i = 0;
190 int j = 0;
191 unsigned long ulOffset = lStart - CFG_FLASH_BASE;
192 int d;
193 int iShift = 0;
194 int iNumWords = 2;
195 int nLeftover = lCount % 4;
196 int nSector = 0;
197
198 for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
199 for (iShift = 0, j = 0; (j < iNumWords);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800200 j++, ulOffset += (lStride * 2)) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100201 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800202 && (ulOffset < INVALIDLOCNEND)) {
203 printf
204 ("Invalid locations, Try writing to another location \n");
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100205 return FLASH_FAIL;
206 }
207 get_sector_number(ulOffset, &nSector);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800208 read_flash(ulOffset, &d);
209 if (d != 0xffff) {
210 printf
211 ("Flash not erased at offset 0x%x Please erase to reprogram \n",
212 ulOffset);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100213 return FLASH_FAIL;
214 }
215 unlock_flash(ulOffset);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800216 if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100217 printf("Error programming the flash \n");
218 return FLASH_FAIL;
219 }
220 iShift += 16;
221 }
222 }
223 if (nLeftover > 0) {
224 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800225 && (ulOffset < INVALIDLOCNEND))
226 return FLASH_FAIL;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100227 get_sector_number(ulOffset, &nSector);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800228 read_flash(ulOffset, &d);
229 if (d != 0xffff) {
230 printf
231 ("Flash already programmed. Please erase to reprogram \n");
232 printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100233 return FLASH_FAIL;
234 }
235 unlock_flash(ulOffset);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800236 if (write_flash(ulOffset, pnData[i]) < 0) {
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100237 printf("Error programming the flash \n");
238 return FLASH_FAIL;
239 }
240 }
241 return FLASH_SUCCESS;
242}
243
244int read_data(long ulStart, long lCount, long lStride, int *pnData)
245{
246 long i = 0;
247 int j = 0;
248 long ulOffset = ulStart;
249 int iShift = 0;
250 int iNumWords = 2;
251 int nLeftover = lCount % 4;
252 int nHi, nLow;
253 int nSector = 0;
254
255 for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
256 for (iShift = 0, j = 0; j < iNumWords; j += 2) {
257 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800258 && (ulOffset < INVALIDLOCNEND))
259 return FLASH_FAIL;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100260
261 get_sector_number(ulOffset, &nSector);
262 read_flash(ulOffset, &nLow);
263 ulOffset += (lStride * 2);
264 read_flash(ulOffset, &nHi);
265 ulOffset += (lStride * 2);
266 pnData[i] = (nHi << 16) | nLow;
267 }
268 }
269 if (nLeftover > 0) {
270 if ((ulOffset >= INVALIDLOCNSTART)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800271 && (ulOffset < INVALIDLOCNEND))
272 return FLASH_FAIL;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100273
274 get_sector_number(ulOffset, &nSector);
275 read_flash(ulOffset, &pnData[i]);
276 }
277 return FLASH_SUCCESS;
278}
279
280int write_flash(long nOffset, int nValue)
281{
282 long addr;
283
284 addr = (CFG_FLASH_BASE + nOffset);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800285 __builtin_bfin_ssync();
286 *(unsigned volatile short *)addr = nValue;
287 __builtin_bfin_ssync();
288 if (poll_toggle_bit(nOffset) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100289 return FLASH_FAIL;
290 return FLASH_SUCCESS;
291}
292
293int read_flash(long nOffset, int *pnValue)
294{
295 int nValue = 0x0;
296 long addr = (CFG_FLASH_BASE + nOffset);
297
298 if (nOffset != 0x2)
299 reset_flash();
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800300 __builtin_bfin_ssync();
301 nValue = *(volatile unsigned short *)addr;
302 __builtin_bfin_ssync();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100303 *pnValue = nValue;
304 return TRUE;
305}
306
307int poll_toggle_bit(long lOffset)
308{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800309 unsigned int u1, u2;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100310 unsigned long timeout = 0xFFFFFFFF;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800311 volatile unsigned long *FB =
312 (volatile unsigned long *)(0x20000000 + lOffset);
313 while (1) {
314 if (timeout < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100315 break;
316 u1 = *(volatile unsigned short *)FB;
317 u2 = *(volatile unsigned short *)FB;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800318 if ((u1 & 0x0040) == (u2 & 0x0040))
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100319 return FLASH_SUCCESS;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800320 if ((u2 & 0x0020) == 0x0000)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100321 continue;
322 u1 = *(volatile unsigned short *)FB;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800323 if ((u2 & 0x0040) == (u1 & 0x0040))
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100324 return FLASH_SUCCESS;
325 else {
326 reset_flash();
327 return FLASH_FAIL;
328 }
329 timeout--;
330 }
331 printf("Time out occured \n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800332 if (timeout < 0)
333 return FLASH_FAIL;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100334}
335
336void reset_flash(void)
337{
338 write_flash(WRITESEQ1, RESET_VAL);
339 /* Wait for 10 micro seconds */
340 udelay(10);
341}
342
343int erase_flash(void)
344{
345 write_flash(WRITESEQ1, WRITEDATA1);
346 write_flash(WRITESEQ2, WRITEDATA2);
347 write_flash(WRITESEQ3, WRITEDATA3);
348 write_flash(WRITESEQ4, WRITEDATA4);
349 write_flash(WRITESEQ5, WRITEDATA5);
350 write_flash(WRITESEQ6, WRITEDATA6);
351
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800352 if (poll_toggle_bit(0x0000) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100353 return FLASH_FAIL;
354
355 write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
356 write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
357 write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
358 write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
359 write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
360 write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
361
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800362 if (poll_toggle_bit(SecFlashASec1Off) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100363 return FLASH_FAIL;
364
365 write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
366 write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
367 write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
368 write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
369 write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
370 write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
371
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800372 if (poll_toggle_bit(PriFlashBOff) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100373 return FLASH_FAIL;
374
375 write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
376 write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
377 write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
378 write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
379 write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
380 write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
381
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800382 if (poll_toggle_bit(SecFlashBOff) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100383 return FLASH_FAIL;
384
385 return FLASH_SUCCESS;
386}
387
388int erase_block_flash(int nBlock, unsigned long address)
389{
390 long ulSectorOff = 0x0;
391
392 if ((nBlock < 0) || (nBlock > AFP_NumSectors))
393 return FALSE;
394
395 ulSectorOff = (address - CFG_FLASH_BASE);
396
397 write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
398 write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
399 write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
400 write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
401 write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
402
403 write_flash(ulSectorOff, BlockEraseVal);
404
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800405 if (poll_toggle_bit(ulSectorOff) < 0)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100406 return FLASH_FAIL;
407
408 return FLASH_SUCCESS;
409}
410
411void unlock_flash(long ulOffset)
412{
413 unsigned long ulOffsetAddr = ulOffset;
414 ulOffsetAddr &= 0xFFFF0000;
415
416 write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
417 write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
418 write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
419}
420
421int get_codes()
422{
423 int dev_id = 0;
424
425 write_flash(WRITESEQ1, GETCODEDATA1);
426 write_flash(WRITESEQ2, GETCODEDATA2);
427 write_flash(WRITESEQ3, GETCODEDATA3);
428
429 read_flash(0x0002, &dev_id);
430 dev_id &= 0x00FF;
431
432 reset_flash();
433
434 return dev_id;
435}
436
437void get_sector_number(long ulOffset, int *pnSector)
438{
439 int nSector = 0;
440
441 if (ulOffset >= SecFlashAOff) {
442 if ((ulOffset < SecFlashASec1Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800443 && (ulOffset < SecFlashASec2Off)) {
444 nSector = SECT32;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100445 } else if ((ulOffset >= SecFlashASec2Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800446 && (ulOffset < SecFlashASec3Off)) {
447 nSector = SECT33;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100448 } else if ((ulOffset >= SecFlashASec3Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800449 && (ulOffset < SecFlashASec4Off)) {
450 nSector = SECT34;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100451 } else if ((ulOffset >= SecFlashASec4Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800452 && (ulOffset < SecFlashAEndOff)) {
453 nSector = SECT35;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100454 }
455 } else if (ulOffset >= SecFlashBOff) {
456 if ((ulOffset < SecFlashBSec1Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800457 && (ulOffset < SecFlashBSec2Off)) {
458 nSector = SECT36;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100459 }
460 if ((ulOffset < SecFlashBSec2Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800461 && (ulOffset < SecFlashBSec3Off)) {
462 nSector = SECT37;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100463 }
464 if ((ulOffset < SecFlashBSec3Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800465 && (ulOffset < SecFlashBSec4Off)) {
466 nSector = SECT38;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100467 }
468 if ((ulOffset < SecFlashBSec4Off)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800469 && (ulOffset < SecFlashBEndOff)) {
470 nSector = SECT39;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100471 }
472 } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
473 nSector = ulOffset & 0xffff0000;
474 nSector = ulOffset >> 16;
475 nSector = nSector & 0x000ff;
476 }
477
478 if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
479 *pnSector = nSector;
480 }
481}