2008-04-24 06:09:28 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Pavel Chromy *
|
|
|
|
* chromy@asix.cz *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2018-06-11 14:47:52 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
2008-04-24 06:09:28 -05:00
|
|
|
***************************************************************************/
|
|
|
|
#include "samflash.h"
|
|
|
|
|
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
unsigned int flash_page_count = 1024;
|
|
|
|
unsigned int flash_page_size = 256;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* pages per lock bit */
|
2009-06-23 17:42:54 -05:00
|
|
|
unsigned int flash_lock_pages = 1024/16;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* detect chip and set loader parameters */
|
|
|
|
int flash_init(void)
|
|
|
|
{
|
|
|
|
unsigned int nvpsiz;
|
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
nvpsiz = (inr(DBGU_CIDR) >> 8)&0xf;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
switch (nvpsiz) {
|
|
|
|
case 3:
|
|
|
|
/* AT91SAM7x32 */
|
2009-06-23 17:42:54 -05:00
|
|
|
flash_page_count = 256;
|
|
|
|
flash_page_size = 128;
|
|
|
|
flash_lock_pages = 256/8;
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
/* AT91SAM7x64 */
|
2009-06-23 17:42:54 -05:00
|
|
|
flash_page_count = 512;
|
|
|
|
flash_page_size = 128;
|
|
|
|
flash_lock_pages = 512/16;
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
/* AT91SAM7x128*/
|
2009-06-23 17:42:54 -05:00
|
|
|
flash_page_count = 512;
|
|
|
|
flash_page_size = 256;
|
|
|
|
flash_lock_pages = 512/8;
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
/* AT91SAM7x256 */
|
2009-06-23 17:42:54 -05:00
|
|
|
flash_page_count = 1024;
|
|
|
|
flash_page_size = 256;
|
|
|
|
flash_lock_pages = 1024/16;
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
/* AT91SAM7x512 */
|
2009-06-23 17:42:54 -05:00
|
|
|
flash_page_count = 2048;
|
|
|
|
flash_page_size = 256;
|
|
|
|
flash_lock_pages = 2048/32;
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FLASH_STAT_INITE;
|
|
|
|
}
|
|
|
|
return FLASH_STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* program single flash page */
|
|
|
|
int flash_page_program(uint32 *data, int page_num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int efc_ofs;
|
|
|
|
|
|
|
|
uint32 *flash_ptr;
|
|
|
|
uint32 *data_ptr;
|
|
|
|
|
|
|
|
/* select proper controller */
|
2009-06-23 17:42:54 -05:00
|
|
|
if (page_num >= 1024) efc_ofs = 0x10;
|
|
|
|
else efc_ofs = 0;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* wait until FLASH is ready, just for sure */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* calculate page address, only lower 8 bits are used to address the latch,
|
|
|
|
but the upper part of address is needed for writing to proper EFC */
|
2009-06-23 17:44:17 -05:00
|
|
|
flash_ptr = (uint32 *)(FLASH_AREA_ADDR + (page_num*flash_page_size));
|
2009-06-23 17:42:54 -05:00
|
|
|
data_ptr = data;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* copy data to latch */
|
2009-06-23 17:42:54 -05:00
|
|
|
for (i = flash_page_size/4; i; i--) {
|
2008-04-24 06:09:28 -05:00
|
|
|
/* we do not use memcpy to be sure that only 32 bit access is used */
|
|
|
|
*(flash_ptr++)=*(data_ptr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* page number and page write command to FCR */
|
2009-06-23 17:44:17 -05:00
|
|
|
outr(MC_FCR + efc_ofs, ((page_num&0x3ff) << 8) | MC_KEY | MC_FCMD_WP);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* wait until it's done */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* check for errors */
|
2009-06-23 17:44:17 -05:00
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_PROGE)) return FLASH_STAT_PROGE;
|
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_LOCKE)) return FLASH_STAT_LOCKE;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* verify written data */
|
2009-06-23 17:44:17 -05:00
|
|
|
flash_ptr = (uint32 *)(FLASH_AREA_ADDR + (page_num*flash_page_size));
|
2009-06-23 17:42:54 -05:00
|
|
|
data_ptr = data;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
for (i = flash_page_size/4; i; i--) {
|
2008-04-24 06:09:28 -05:00
|
|
|
if (*(flash_ptr++)!=*(data_ptr++)) return FLASH_STAT_VERIFE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return FLASH_STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int flash_erase_plane(int efc_ofs)
|
|
|
|
{
|
|
|
|
unsigned int lockbits;
|
|
|
|
int page_num;
|
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
page_num = 0;
|
2009-06-23 17:44:17 -05:00
|
|
|
lockbits = inr(MC_FSR + efc_ofs) >> 16;
|
2008-04-24 06:09:28 -05:00
|
|
|
while (lockbits) {
|
|
|
|
if (lockbits&1) {
|
|
|
|
|
|
|
|
/* wait until FLASH is ready, just for sure */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:44:17 -05:00
|
|
|
outr(MC_FCR + efc_ofs, ((page_num&0x3ff) << 8) | 0x5a000004);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* wait until it's done */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* check for errors */
|
2009-06-23 17:44:17 -05:00
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_PROGE)) return FLASH_STAT_PROGE;
|
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_LOCKE)) return FLASH_STAT_LOCKE;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
}
|
2009-06-23 17:45:47 -05:00
|
|
|
if ((page_num += flash_lock_pages) > flash_page_count) break;
|
2008-04-24 06:09:28 -05:00
|
|
|
lockbits>>=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until FLASH is ready, just for sure */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* erase all command to FCR */
|
2009-06-23 17:44:17 -05:00
|
|
|
outr(MC_FCR + efc_ofs, 0x5a000008);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* wait until it's done */
|
2009-06-23 17:44:17 -05:00
|
|
|
while ((inr(MC_FSR + efc_ofs)&MC_FRDY) == 0);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* check for errors */
|
2009-06-23 17:44:17 -05:00
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_PROGE)) return FLASH_STAT_PROGE;
|
|
|
|
if ((inr(MC_FSR + efc_ofs)&MC_LOCKE)) return FLASH_STAT_LOCKE;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* set no erase before programming */
|
2009-06-23 17:45:15 -05:00
|
|
|
outr(MC_FMR + efc_ofs, inr(MC_FMR + efc_ofs) | 0x80);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
return FLASH_STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* erase whole chip */
|
|
|
|
int flash_erase_all(void)
|
|
|
|
{
|
|
|
|
int result;
|
2009-06-23 17:49:23 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
if ((result = flash_erase_plane(0)) != FLASH_STAT_OK) return result;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
/* the second flash controller, if any */
|
2009-06-23 17:45:47 -05:00
|
|
|
if (flash_page_count > 1024) result = flash_erase_plane(0x10);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int flash_verify(uint32 adr, unsigned int len, uint8 *src)
|
|
|
|
{
|
|
|
|
unsigned char *flash_ptr;
|
|
|
|
|
2009-06-23 17:44:17 -05:00
|
|
|
flash_ptr = (uint8 *)FLASH_AREA_ADDR + adr;
|
2009-06-23 17:46:23 -05:00
|
|
|
for (;len; len--) {
|
2008-04-24 06:09:28 -05:00
|
|
|
if (*(flash_ptr++)!=*(src++)) return FLASH_STAT_VERIFE;
|
|
|
|
}
|
|
|
|
return FLASH_STAT_OK;
|
|
|
|
}
|