2 * U-boot - flash.c Flash driver for PSD4256GV
4 * Copyright (c) 2005 blackfin.uclinux.org
5 * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
7 * (C) Copyright 2000-2004
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include "flash-defines.h"
31 void flash_reset(void)
36 unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
39 static int FlagDev = 1;
44 printf("Device ID of the Flash is %x\n", id);
52 for (i = PriFlashABegin; i < SecFlashABegin; i++)
53 info->start[i] = (baseaddr + (i * AFP_SectorSize1));
54 info->size = 0x200000;
55 info->sector_count = 32;
58 info->start[0] = baseaddr + SecFlashASec1Off;
59 info->start[1] = baseaddr + SecFlashASec2Off;
60 info->start[2] = baseaddr + SecFlashASec3Off;
61 info->start[3] = baseaddr + SecFlashASec4Off;
63 info->sector_count = 4;
66 info->start[0] = baseaddr + SecFlashBSec1Off;
67 info->start[1] = baseaddr + SecFlashBSec2Off;
68 info->start[2] = baseaddr + SecFlashBSec3Off;
69 info->start[3] = baseaddr + SecFlashBSec4Off;
71 info->sector_count = 4;
77 unsigned long flash_init(void)
79 unsigned long size_b0, size_b1, size_b2;
82 size_b0 = size_b1 = size_b2 = 0;
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");
92 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
93 flash_info[i].flash_id = FLASH_UNKNOWN;
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);
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",
102 size_b0, size_b0 >> 20);
105 (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
106 (flash_info[0].start[2] - 1), &flash_info[0]);
108 return (size_b0 + size_b1 + size_b2);
111 void flash_print_info(flash_info_t * info)
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf("missing or unknown FLASH type\n");
120 switch (info->flash_id) {
121 case FLASH_PSD4256GV:
122 printf("ST Microelectronics ");
125 printf("Unknown Vendor: (0x%08X) ", info->flash_id);
128 for (i = 0; i < info->sector_count; ++i) {
132 info->start[i], info->protect[i] ? " (RO)" : " ");
138 int flash_erase(flash_info_t * info, int s_first, int s_last)
144 for (sect = s_first; sect <= s_last; ++sect) {
145 if (info->protect[sect])
150 printf("- Warning: %d protected sectors will not be erased!\n",
155 cnt = s_last - s_first + 1;
157 if (cnt == FLASH_TOT_SECT) {
158 printf("Erasing flash, Please Wait \n");
159 if (erase_flash() < 0) {
160 printf("Erasing flash failed \n");
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 */
167 if (erase_block_flash(i, info->start[i]) < 0) {
168 printf("Error Sector erasing \n");
174 return FLASH_SUCCESS;
177 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
181 ret = write_data(addr, cnt, 1, (int *)src);
182 if (ret == FLASH_FAIL)
183 return ERR_NOT_ERASED;
184 return FLASH_SUCCESS;
187 int write_data(long lStart, long lCount, long lStride, int *pnData)
191 unsigned long ulOffset = lStart - CFG_FLASH_BASE;
195 int nLeftover = lCount % 4;
198 for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
199 for (iShift = 0, j = 0; (j < iNumWords);
200 j++, ulOffset += (lStride * 2)) {
201 if ((ulOffset >= INVALIDLOCNSTART)
202 && (ulOffset < INVALIDLOCNEND)) {
204 ("Invalid locations, Try writing to another location \n");
207 get_sector_number(ulOffset, &nSector);
208 read_flash(ulOffset, &d);
211 ("Flash not erased at offset 0x%x Please erase to reprogram \n",
215 unlock_flash(ulOffset);
216 if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
217 printf("Error programming the flash \n");
224 if ((ulOffset >= INVALIDLOCNSTART)
225 && (ulOffset < INVALIDLOCNEND))
227 get_sector_number(ulOffset, &nSector);
228 read_flash(ulOffset, &d);
231 ("Flash already programmed. Please erase to reprogram \n");
232 printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
235 unlock_flash(ulOffset);
236 if (write_flash(ulOffset, pnData[i]) < 0) {
237 printf("Error programming the flash \n");
241 return FLASH_SUCCESS;
244 int read_data(long ulStart, long lCount, long lStride, int *pnData)
248 long ulOffset = ulStart;
251 int nLeftover = lCount % 4;
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)
258 && (ulOffset < INVALIDLOCNEND))
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;
270 if ((ulOffset >= INVALIDLOCNSTART)
271 && (ulOffset < INVALIDLOCNEND))
274 get_sector_number(ulOffset, &nSector);
275 read_flash(ulOffset, &pnData[i]);
277 return FLASH_SUCCESS;
280 int write_flash(long nOffset, int nValue)
284 addr = (CFG_FLASH_BASE + nOffset);
285 __builtin_bfin_ssync();
286 *(unsigned volatile short *)addr = nValue;
287 __builtin_bfin_ssync();
288 if (poll_toggle_bit(nOffset) < 0)
290 return FLASH_SUCCESS;
293 int read_flash(long nOffset, int *pnValue)
296 long addr = (CFG_FLASH_BASE + nOffset);
300 __builtin_bfin_ssync();
301 nValue = *(volatile unsigned short *)addr;
302 __builtin_bfin_ssync();
307 int poll_toggle_bit(long lOffset)
310 unsigned long timeout = 0xFFFFFFFF;
311 volatile unsigned long *FB =
312 (volatile unsigned long *)(0x20000000 + lOffset);
316 u1 = *(volatile unsigned short *)FB;
317 u2 = *(volatile unsigned short *)FB;
318 if ((u1 & 0x0040) == (u2 & 0x0040))
319 return FLASH_SUCCESS;
320 if ((u2 & 0x0020) == 0x0000)
322 u1 = *(volatile unsigned short *)FB;
323 if ((u2 & 0x0040) == (u1 & 0x0040))
324 return FLASH_SUCCESS;
331 printf("Time out occured \n");
336 void reset_flash(void)
338 write_flash(WRITESEQ1, RESET_VAL);
339 /* Wait for 10 micro seconds */
343 int erase_flash(void)
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);
352 if (poll_toggle_bit(0x0000) < 0)
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);
362 if (poll_toggle_bit(SecFlashASec1Off) < 0)
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);
372 if (poll_toggle_bit(PriFlashBOff) < 0)
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);
382 if (poll_toggle_bit(SecFlashBOff) < 0)
385 return FLASH_SUCCESS;
388 int erase_block_flash(int nBlock, unsigned long address)
390 long ulSectorOff = 0x0;
392 if ((nBlock < 0) || (nBlock > AFP_NumSectors))
395 ulSectorOff = (address - CFG_FLASH_BASE);
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);
403 write_flash(ulSectorOff, BlockEraseVal);
405 if (poll_toggle_bit(ulSectorOff) < 0)
408 return FLASH_SUCCESS;
411 void unlock_flash(long ulOffset)
413 unsigned long ulOffsetAddr = ulOffset;
414 ulOffsetAddr &= 0xFFFF0000;
416 write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
417 write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
418 write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
425 write_flash(WRITESEQ1, GETCODEDATA1);
426 write_flash(WRITESEQ2, GETCODEDATA2);
427 write_flash(WRITESEQ3, GETCODEDATA3);
429 read_flash(0x0002, &dev_id);
437 void get_sector_number(long ulOffset, int *pnSector)
441 if (ulOffset >= SecFlashAOff) {
442 if ((ulOffset < SecFlashASec1Off)
443 && (ulOffset < SecFlashASec2Off)) {
445 } else if ((ulOffset >= SecFlashASec2Off)
446 && (ulOffset < SecFlashASec3Off)) {
448 } else if ((ulOffset >= SecFlashASec3Off)
449 && (ulOffset < SecFlashASec4Off)) {
451 } else if ((ulOffset >= SecFlashASec4Off)
452 && (ulOffset < SecFlashAEndOff)) {
455 } else if (ulOffset >= SecFlashBOff) {
456 if ((ulOffset < SecFlashBSec1Off)
457 && (ulOffset < SecFlashBSec2Off)) {
460 if ((ulOffset < SecFlashBSec2Off)
461 && (ulOffset < SecFlashBSec3Off)) {
464 if ((ulOffset < SecFlashBSec3Off)
465 && (ulOffset < SecFlashBSec4Off)) {
468 if ((ulOffset < SecFlashBSec4Off)
469 && (ulOffset < SecFlashBEndOff)) {
472 } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
473 nSector = ulOffset & 0xffff0000;
474 nSector = ulOffset >> 16;
475 nSector = nSector & 0x000ff;
478 if ((nSector >= 0) && (nSector < AFP_NumSectors)) {