2008-02-25 17:41:25 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <stdio.h>
|
2010-02-08 12:52:46 +00:00
|
|
|
#include "cfi_flash.h"
|
2007-07-05 16:01:45 +00:00
|
|
|
|
2010-11-26 16:55:51 +00:00
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
* Reverse the order of the erase regions in the CFI QRY structure.
|
|
|
|
* This is needed for chips that are either a) correctly detected as
|
|
|
|
* top-boot, or b) buggy.
|
|
|
|
*/
|
|
|
|
static void cfi_reverse_geometry(struct cfi_qry *qry)
|
|
|
|
{
|
|
|
|
unsigned int i, j;
|
|
|
|
u32 tmp;
|
|
|
|
|
|
|
|
for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
|
|
|
|
tmp = qry->erase_region_info[i];
|
|
|
|
qry->erase_region_info[i] = qry->erase_region_info[j];
|
|
|
|
qry->erase_region_info[j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 08:08:27 +00:00
|
|
|
static void flash_unlock_seq(struct flash_info *info)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_UNLOCK_START);
|
|
|
|
flash_write_cmd (info, 0, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
|
2007-07-05 16:01:45 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 07:16:07 +00:00
|
|
|
/*
|
2007-07-05 16:01:45 +00:00
|
|
|
* read jedec ids from device and set corresponding fields in info struct
|
|
|
|
*
|
|
|
|
* Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
|
|
|
|
*
|
|
|
|
*/
|
2015-06-22 08:08:27 +00:00
|
|
|
static void amd_read_jedec_ids(struct flash_info *info)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
2010-11-26 16:59:39 +00:00
|
|
|
info->cmd_reset = AMD_CMD_RESET;
|
2007-07-05 16:01:45 +00:00
|
|
|
info->manufacturer_id = 0;
|
|
|
|
info->device_id = 0;
|
|
|
|
info->device_id2 = 0;
|
|
|
|
|
2010-11-26 16:28:39 +00:00
|
|
|
/* calculate command offsets as in the Linux driver */
|
|
|
|
info->addr_unlock1 = 0x555;
|
|
|
|
info->addr_unlock2 = 0x2AA;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modify the unlock address if we are in compatibility mode
|
|
|
|
*/
|
|
|
|
if ( /* x8/x16 in x8 mode */
|
|
|
|
((info->chipwidth == FLASH_CFI_BY8) &&
|
|
|
|
(info->interface == FLASH_CFI_X8X16)) ||
|
|
|
|
/* x16/x32 in x16 mode */
|
|
|
|
((info->chipwidth == FLASH_CFI_BY16) &&
|
|
|
|
(info->interface == FLASH_CFI_X16X32)))
|
|
|
|
{
|
|
|
|
info->addr_unlock1 = 0xaaa;
|
|
|
|
info->addr_unlock2 = 0x555;
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:59:39 +00:00
|
|
|
flash_write_cmd(info, 0, 0, info->cmd_reset);
|
2010-04-19 08:28:18 +00:00
|
|
|
flash_unlock_seq(info);
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
|
2007-07-05 16:01:45 +00:00
|
|
|
udelay(1000); /* some flash are slow to respond */
|
2010-11-26 16:00:38 +00:00
|
|
|
|
|
|
|
info->manufacturer_id = jedec_read_mfr(info);
|
2015-06-22 08:08:27 +00:00
|
|
|
info->device_id = flash_read_uchar(info,
|
2007-07-05 16:01:45 +00:00
|
|
|
FLASH_OFFSET_DEVICE_ID);
|
|
|
|
if (info->device_id == 0x7E) {
|
|
|
|
/* AMD 3-byte (expanded) device ids */
|
2015-06-22 08:08:27 +00:00
|
|
|
info->device_id2 = flash_read_uchar(info,
|
2007-07-05 16:01:45 +00:00
|
|
|
FLASH_OFFSET_DEVICE_ID2);
|
|
|
|
info->device_id2 <<= 8;
|
2015-06-22 08:08:27 +00:00
|
|
|
info->device_id2 |= flash_read_uchar(info,
|
2007-07-05 16:01:45 +00:00
|
|
|
FLASH_OFFSET_DEVICE_ID3);
|
|
|
|
}
|
2010-11-26 16:59:39 +00:00
|
|
|
flash_write_cmd(info, 0, 0, info->cmd_reset);
|
2007-07-05 16:01:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 08:08:27 +00:00
|
|
|
static int flash_toggle(struct flash_info *info, flash_sect_t sect,
|
|
|
|
unsigned int offset, u8 cmd)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
2010-11-26 17:01:41 +00:00
|
|
|
void *addr;
|
2007-07-05 16:01:45 +00:00
|
|
|
cfiword_t cword;
|
|
|
|
int retval;
|
|
|
|
|
2010-11-26 17:01:41 +00:00
|
|
|
addr = flash_make_addr (info, sect, offset);
|
2007-07-05 16:01:45 +00:00
|
|
|
flash_make_cmd (info, cmd, &cword);
|
2015-06-22 08:08:27 +00:00
|
|
|
|
2008-02-26 10:28:55 +00:00
|
|
|
if (bankwidth_is_1(info)) {
|
2010-11-26 17:01:41 +00:00
|
|
|
retval = flash_read8(addr) != flash_read8(addr);
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_2(info)) {
|
2010-11-26 17:01:41 +00:00
|
|
|
retval = flash_read16(addr) != flash_read16(addr);
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_4(info)) {
|
2010-11-26 17:01:41 +00:00
|
|
|
retval = flash_read32(addr) != flash_read32(addr);
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_8(info)) {
|
2010-11-26 17:01:41 +00:00
|
|
|
retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
|
|
|
|
(flash_read32(addr+4) != flash_read32(addr+4)) );
|
2015-06-22 08:08:27 +00:00
|
|
|
} else {
|
2007-07-05 16:01:45 +00:00
|
|
|
retval = 0;
|
2015-06-22 08:08:27 +00:00
|
|
|
}
|
2008-02-26 10:28:55 +00:00
|
|
|
|
2007-07-05 16:01:45 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* flash_is_busy - check to see if the flash is busy
|
|
|
|
* This routine checks the status of the chip and returns true if the chip is busy
|
|
|
|
*/
|
2015-06-22 08:08:27 +00:00
|
|
|
static int amd_flash_is_busy(struct flash_info *info, flash_sect_t sect)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
|
|
|
return flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
|
|
|
|
}
|
|
|
|
|
2015-06-22 08:08:27 +00:00
|
|
|
static int amd_flash_erase_one(struct flash_info *info, long sect)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
2010-04-19 08:28:18 +00:00
|
|
|
flash_unlock_seq(info);
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_ERASE_START);
|
2010-04-19 08:28:18 +00:00
|
|
|
flash_unlock_seq(info);
|
2007-07-05 16:01:45 +00:00
|
|
|
flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
|
|
|
|
|
2010-02-08 11:47:51 +00:00
|
|
|
return flash_status_check(info, sect, info->erase_blk_tout, "erase");
|
2007-07-05 16:01:45 +00:00
|
|
|
}
|
|
|
|
|
2010-06-29 06:40:39 +00:00
|
|
|
static void amd_flash_prepare_write(struct flash_info *info)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
2010-04-19 08:28:18 +00:00
|
|
|
flash_unlock_seq(info);
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
|
2007-07-05 16:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CFI_BUFFER_WRITE
|
2015-06-22 08:08:27 +00:00
|
|
|
static int amd_flash_write_cfibuffer(struct flash_info *info, unsigned long dest,
|
|
|
|
const u8 *cp, int len)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
|
|
|
flash_sect_t sector;
|
|
|
|
int cnt;
|
2015-06-22 08:08:27 +00:00
|
|
|
void *src = (void *)cp;
|
2010-11-26 17:01:41 +00:00
|
|
|
void *dst = (void *)dest;
|
2008-02-26 09:32:25 +00:00
|
|
|
cfiword_t cword;
|
2007-07-05 16:01:45 +00:00
|
|
|
|
|
|
|
sector = find_sector (info, dest);
|
|
|
|
|
2010-04-19 08:28:18 +00:00
|
|
|
flash_unlock_seq(info);
|
2008-02-26 09:32:25 +00:00
|
|
|
flash_make_cmd (info, AMD_CMD_WRITE_TO_BUFFER, &cword);
|
|
|
|
flash_write_word(info, cword, (void *)dest);
|
2007-07-05 16:01:45 +00:00
|
|
|
|
2008-02-26 10:28:55 +00:00
|
|
|
if (bankwidth_is_1(info)) {
|
2007-07-05 16:01:45 +00:00
|
|
|
cnt = len;
|
2011-08-25 11:52:12 +00:00
|
|
|
flash_write_cmd(info, sector, 0, (u32)cnt - 1);
|
2010-11-26 17:01:41 +00:00
|
|
|
while (cnt-- > 0) {
|
|
|
|
flash_write8(flash_read8(src), dst);
|
|
|
|
src += 1, dst += 1;
|
|
|
|
}
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_2(info)) {
|
2007-07-05 16:01:45 +00:00
|
|
|
cnt = len >> 1;
|
2011-08-25 11:52:12 +00:00
|
|
|
flash_write_cmd(info, sector, 0, (u32)cnt - 1);
|
2010-11-26 17:01:41 +00:00
|
|
|
while (cnt-- > 0) {
|
|
|
|
flash_write16(flash_read16(src), dst);
|
|
|
|
src += 2, dst += 2;
|
|
|
|
}
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_4(info)) {
|
2007-07-05 16:01:45 +00:00
|
|
|
cnt = len >> 2;
|
2011-08-25 11:52:12 +00:00
|
|
|
flash_write_cmd(info, sector, 0, (u32)cnt - 1);
|
2010-11-26 17:01:41 +00:00
|
|
|
while (cnt-- > 0) {
|
|
|
|
flash_write32(flash_read32(src), dst);
|
|
|
|
src += 4, dst += 4;
|
|
|
|
}
|
2008-02-26 10:28:55 +00:00
|
|
|
} else if (bankwidth_is_8(info)) {
|
2007-07-05 16:01:45 +00:00
|
|
|
cnt = len >> 3;
|
2011-08-25 11:52:12 +00:00
|
|
|
flash_write_cmd(info, sector, 0, (u32)cnt - 1);
|
2010-11-26 17:01:41 +00:00
|
|
|
while (cnt-- > 0) {
|
|
|
|
flash_write64(flash_read64(src), dst);
|
|
|
|
src += 8, dst += 8;
|
|
|
|
}
|
2007-07-05 16:01:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 08:08:27 +00:00
|
|
|
flash_write_cmd(info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
|
|
|
|
|
|
|
|
return flash_status_check(info, sector, info->buffer_write_tout,
|
2007-07-05 16:01:45 +00:00
|
|
|
"buffer write");
|
|
|
|
}
|
2009-10-03 23:09:19 +00:00
|
|
|
#else
|
|
|
|
#define amd_flash_write_cfibuffer NULL
|
2007-07-05 16:01:45 +00:00
|
|
|
#endif /* CONFIG_CFI_BUFFER_WRITE */
|
|
|
|
|
2015-06-22 08:08:27 +00:00
|
|
|
static int amd_flash_real_protect(struct flash_info *info, long sector, int prot)
|
2010-11-26 16:00:38 +00:00
|
|
|
{
|
2015-06-22 08:08:27 +00:00
|
|
|
if (info->manufacturer_id != (u8)ATM_MANUFACT)
|
2010-11-26 16:00:38 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (prot) {
|
|
|
|
flash_unlock_seq (info);
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd (info, 0, info->addr_unlock1,
|
2010-11-26 16:00:38 +00:00
|
|
|
ATM_CMD_SOFTLOCK_START);
|
|
|
|
flash_unlock_seq (info);
|
|
|
|
flash_write_cmd (info, sector, 0, ATM_CMD_LOCK_SECT);
|
|
|
|
} else {
|
2010-11-26 16:28:39 +00:00
|
|
|
flash_write_cmd (info, 0, info->addr_unlock1,
|
2010-11-26 16:00:38 +00:00
|
|
|
AMD_CMD_UNLOCK_START);
|
|
|
|
if (info->device_id == ATM_ID_BV6416)
|
|
|
|
flash_write_cmd (info, sector, 0,
|
|
|
|
ATM_CMD_UNLOCK_SECT);
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:00:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-26 17:11:41 +00:00
|
|
|
/*
|
|
|
|
* Manufacturer-specific quirks. Add workarounds for geometry
|
|
|
|
* reversal, etc. here.
|
|
|
|
*/
|
2015-06-22 08:08:27 +00:00
|
|
|
static void flash_fixup_amd(struct flash_info *info, struct cfi_qry *qry)
|
2010-11-26 16:55:51 +00:00
|
|
|
{
|
|
|
|
/* check if flash geometry needs reversal */
|
|
|
|
if (qry->num_erase_regions > 1) {
|
|
|
|
/* reverse geometry if top boot part */
|
|
|
|
if (info->cfi_version < 0x3131) {
|
|
|
|
/* CFI < 1.1, try to guess from device id */
|
|
|
|
if ((info->device_id & 0x80) != 0)
|
|
|
|
cfi_reverse_geometry(qry);
|
|
|
|
} else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
|
|
|
|
/* CFI >= 1.1, deduct from top/bottom flag */
|
|
|
|
/* note: ext_addr is valid since cfi_version > 0 */
|
|
|
|
cfi_reverse_geometry(qry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-26 17:11:41 +00:00
|
|
|
static void flash_fixup_atmel(struct flash_info *info, struct cfi_qry *qry)
|
|
|
|
{
|
|
|
|
int reverse_geometry = 0;
|
|
|
|
|
|
|
|
/* Check the "top boot" bit in the PRI */
|
|
|
|
if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
|
|
|
|
reverse_geometry = 1;
|
|
|
|
|
|
|
|
/* AT49BV6416(T) list the erase regions in the wrong order.
|
|
|
|
* However, the device ID is identical with the non-broken
|
|
|
|
* AT49BV642D since u-boot only reads the low byte (they
|
|
|
|
* differ in the high byte.) So leave out this fixup for now.
|
|
|
|
*/
|
|
|
|
if (info->device_id == 0xd6 || info->device_id == 0xd2)
|
|
|
|
reverse_geometry = !reverse_geometry;
|
|
|
|
|
|
|
|
if (reverse_geometry)
|
|
|
|
cfi_reverse_geometry(qry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void amd_flash_fixup(struct flash_info *info, struct cfi_qry *qry)
|
|
|
|
{
|
|
|
|
/* Do manufacturer-specific fixups */
|
|
|
|
switch (info->manufacturer_id) {
|
|
|
|
case 0x0001:
|
|
|
|
flash_fixup_amd(info, qry);
|
|
|
|
break;
|
|
|
|
case 0x001f:
|
|
|
|
flash_fixup_atmel(info, qry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:45 +00:00
|
|
|
struct cfi_cmd_set cfi_cmd_set_amd = {
|
|
|
|
.flash_write_cfibuffer = amd_flash_write_cfibuffer,
|
|
|
|
.flash_erase_one = amd_flash_erase_one,
|
|
|
|
.flash_is_busy = amd_flash_is_busy,
|
|
|
|
.flash_read_jedec_ids = amd_read_jedec_ids,
|
|
|
|
.flash_prepare_write = amd_flash_prepare_write,
|
2008-02-26 10:55:41 +00:00
|
|
|
.flash_status_check = flash_generic_status_check,
|
2010-11-26 16:00:38 +00:00
|
|
|
.flash_real_protect = amd_flash_real_protect,
|
2010-11-26 16:55:51 +00:00
|
|
|
.flash_fixup = amd_flash_fixup,
|
2007-07-05 16:01:45 +00:00
|
|
|
};
|