2014-03-06 09:03:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Sascha Hauer, Pengutronix
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
#define pr_fmt(fmt) "imx-bbu-nand-fcb: " fmt
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
#include <filetype.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ioctl.h>
|
2015-01-08 07:02:20 +00:00
|
|
|
#include <linux/sizes.h>
|
2014-03-06 09:03:33 +00:00
|
|
|
#include <bbu.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <linux/mtd/mtd-abi.h>
|
2015-01-28 10:11:15 +00:00
|
|
|
#include <linux/mtd/nand_mxs.h>
|
2014-03-06 09:03:33 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/stat.h>
|
2015-06-10 13:13:16 +00:00
|
|
|
#include <io.h>
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
struct dbbt_block {
|
|
|
|
uint32_t Checksum;
|
|
|
|
uint32_t FingerPrint;
|
|
|
|
uint32_t Version;
|
2015-06-10 13:13:16 +00:00
|
|
|
uint32_t numberBB; /* reserved on i.MX6 */
|
2014-03-06 09:03:33 +00:00
|
|
|
uint32_t DBBTNumOfPages;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fcb_block {
|
|
|
|
uint32_t Checksum; /* First fingerprint in first byte */
|
|
|
|
uint32_t FingerPrint; /* 2nd fingerprint at byte 4 */
|
|
|
|
uint32_t Version; /* 3rd fingerprint at byte 8 */
|
|
|
|
uint8_t DataSetup;
|
|
|
|
uint8_t DataHold;
|
|
|
|
uint8_t AddressSetup;
|
|
|
|
uint8_t DSAMPLE_TIME;
|
|
|
|
/* These are for application use only and not for ROM. */
|
|
|
|
uint8_t NandTimingState;
|
|
|
|
uint8_t REA;
|
|
|
|
uint8_t RLOH;
|
|
|
|
uint8_t RHOH;
|
|
|
|
uint32_t PageDataSize; /* 2048 for 2K pages, 4096 for 4K pages */
|
|
|
|
uint32_t TotalPageSize; /* 2112 for 2K pages, 4314 for 4K pages */
|
|
|
|
uint32_t SectorsPerBlock; /* Number of 2K sections per block */
|
|
|
|
uint32_t NumberOfNANDs; /* Total Number of NANDs - not used by ROM */
|
|
|
|
uint32_t TotalInternalDie; /* Number of separate chips in this NAND */
|
|
|
|
uint32_t CellType; /* MLC or SLC */
|
|
|
|
uint32_t EccBlockNEccType; /* Type of ECC, can be one of BCH-0-20 */
|
|
|
|
uint32_t EccBlock0Size; /* Number of bytes for Block0 - BCH */
|
|
|
|
uint32_t EccBlockNSize; /* Block size in bytes for all blocks other than Block0 - BCH */
|
|
|
|
uint32_t EccBlock0EccType; /* Ecc level for Block 0 - BCH */
|
|
|
|
uint32_t MetadataBytes; /* Metadata size - BCH */
|
|
|
|
uint32_t NumEccBlocksPerPage; /* Number of blocks per page for ROM use - BCH */
|
|
|
|
uint32_t EccBlockNEccLevelSDK; /* Type of ECC, can be one of BCH-0-20 */
|
|
|
|
uint32_t EccBlock0SizeSDK; /* Number of bytes for Block0 - BCH */
|
|
|
|
uint32_t EccBlockNSizeSDK; /* Block size in bytes for all blocks other than Block0 - BCH */
|
|
|
|
uint32_t EccBlock0EccLevelSDK; /* Ecc level for Block 0 - BCH */
|
|
|
|
uint32_t NumEccBlocksPerPageSDK;/* Number of blocks per page for SDK use - BCH */
|
|
|
|
uint32_t MetadataBytesSDK; /* Metadata size - BCH */
|
|
|
|
uint32_t EraseThreshold; /* To set into BCH_MODE register */
|
|
|
|
uint32_t BootPatch; /* 0 for normal boot and 1 to load patch starting next to FCB */
|
|
|
|
uint32_t PatchSectors; /* Size of patch in sectors */
|
|
|
|
uint32_t Firmware1_startingPage;/* Firmware image starts on this sector */
|
|
|
|
uint32_t Firmware2_startingPage;/* Secondary FW Image starting Sector */
|
|
|
|
uint32_t PagesInFirmware1; /* Number of sectors in firmware image */
|
|
|
|
uint32_t PagesInFirmware2; /* Number of sector in secondary FW image */
|
|
|
|
uint32_t DBBTSearchAreaStartAddress; /* Page address where dbbt search area begins */
|
|
|
|
uint32_t BadBlockMarkerByte; /* Byte in page data that have manufacturer marked bad block marker, */
|
|
|
|
/* this will be swapped with metadata[0] to complete page data. */
|
|
|
|
uint32_t BadBlockMarkerStartBit;/* For BCH ECC sizes other than 8 and 16 the bad block marker does not */
|
|
|
|
/* start at 0th bit of BadBlockMarkerByte. This field is used to get to */
|
|
|
|
/* the start bit of bad block marker byte with in BadBlockMarkerByte */
|
|
|
|
uint32_t BBMarkerPhysicalOffset;/* FCB value that gives byte offset for bad block marker on physical NAND page */
|
|
|
|
uint32_t BCHType;
|
|
|
|
|
|
|
|
uint32_t TMTiming2_ReadLatency;
|
|
|
|
uint32_t TMTiming2_PreambleDelay;
|
|
|
|
uint32_t TMTiming2_CEDelay;
|
|
|
|
uint32_t TMTiming2_PostambleDelay;
|
|
|
|
uint32_t TMTiming2_CmdAddPause;
|
|
|
|
uint32_t TMTiming2_DataPause;
|
|
|
|
uint32_t TMSpeed;
|
|
|
|
uint32_t TMTiming1_BusyTimeout;
|
|
|
|
|
|
|
|
uint32_t DISBBM; /* the flag to enable (1)/disable(0) bi swap */
|
|
|
|
uint32_t BBMarkerPhysicalOffsetInSpareData; /* The swap position of main area in spare area */
|
|
|
|
};
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
struct imx_nand_fcb_bbu_handler {
|
|
|
|
struct bbu_handler handler;
|
|
|
|
|
|
|
|
void (*fcb_create)(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct fcb_block *fcb, struct mtd_info *mtd);
|
|
|
|
void (*dbbt_create)(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct dbbt_block *dbbt, int num_bad_blocks);
|
|
|
|
enum filetype filetype;
|
|
|
|
};
|
|
|
|
|
2014-03-06 09:03:33 +00:00
|
|
|
#define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
|
|
|
|
#define GETBIT(v,n) (((v) >> (n)) & 0x1)
|
|
|
|
|
|
|
|
static uint8_t calculate_parity_13_8(uint8_t d)
|
|
|
|
{
|
|
|
|
uint8_t p = 0;
|
|
|
|
|
|
|
|
p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
|
|
|
|
p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^ GETBIT(d, 1)) << 1;
|
|
|
|
p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^ GETBIT(d, 0)) << 2;
|
|
|
|
p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
|
|
|
|
p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^ GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t *src = _src;
|
|
|
|
uint8_t *ecc = _ecc;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
ecc[i] = calculate_parity_13_8(src[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t calc_chksum(void *buf, size_t size)
|
|
|
|
{
|
|
|
|
u32 chksum = 0;
|
|
|
|
u8 *bp = buf;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
chksum += bp[i];
|
|
|
|
|
|
|
|
return ~chksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __maybe_unused void dump_fcb(void *buf)
|
|
|
|
{
|
|
|
|
struct fcb_block *fcb = buf;
|
|
|
|
|
|
|
|
pr_debug("Checksum: 0x%08x\n", fcb->Checksum);
|
|
|
|
pr_debug("FingerPrint: 0x%08x\n", fcb->FingerPrint);
|
|
|
|
pr_debug("Version: 0x%08x\n", fcb->Version);
|
|
|
|
pr_debug("DataSetup: 0x%02x\n", fcb->DataSetup);
|
|
|
|
pr_debug("DataHold: 0x%02x\n", fcb->DataHold);
|
|
|
|
pr_debug("AddressSetup: 0x%02x\n", fcb->AddressSetup);
|
|
|
|
pr_debug("DSAMPLE_TIME: 0x%02x\n", fcb->DSAMPLE_TIME);
|
|
|
|
pr_debug("NandTimingState: 0x%02x\n", fcb->NandTimingState);
|
|
|
|
pr_debug("REA: 0x%02x\n", fcb->REA);
|
|
|
|
pr_debug("RLOH: 0x%02x\n", fcb->RLOH);
|
|
|
|
pr_debug("RHOH: 0x%02x\n", fcb->RHOH);
|
|
|
|
pr_debug("PageDataSize: 0x%08x\n", fcb->PageDataSize);
|
|
|
|
pr_debug("TotalPageSize: 0x%08x\n", fcb->TotalPageSize);
|
|
|
|
pr_debug("SectorsPerBlock: 0x%08x\n", fcb->SectorsPerBlock);
|
|
|
|
pr_debug("NumberOfNANDs: 0x%08x\n", fcb->NumberOfNANDs);
|
|
|
|
pr_debug("TotalInternalDie: 0x%08x\n", fcb->TotalInternalDie);
|
|
|
|
pr_debug("CellType: 0x%08x\n", fcb->CellType);
|
|
|
|
pr_debug("EccBlockNEccType: 0x%08x\n", fcb->EccBlockNEccType);
|
|
|
|
pr_debug("EccBlock0Size: 0x%08x\n", fcb->EccBlock0Size);
|
|
|
|
pr_debug("EccBlockNSize: 0x%08x\n", fcb->EccBlockNSize);
|
|
|
|
pr_debug("EccBlock0EccType: 0x%08x\n", fcb->EccBlock0EccType);
|
|
|
|
pr_debug("MetadataBytes: 0x%08x\n", fcb->MetadataBytes);
|
|
|
|
pr_debug("NumEccBlocksPerPage: 0x%08x\n", fcb->NumEccBlocksPerPage);
|
|
|
|
pr_debug("EccBlockNEccLevelSDK: 0x%08x\n", fcb->EccBlockNEccLevelSDK);
|
|
|
|
pr_debug("EccBlock0SizeSDK: 0x%08x\n", fcb->EccBlock0SizeSDK);
|
|
|
|
pr_debug("EccBlockNSizeSDK: 0x%08x\n", fcb->EccBlockNSizeSDK);
|
|
|
|
pr_debug("EccBlock0EccLevelSDK: 0x%08x\n", fcb->EccBlock0EccLevelSDK);
|
|
|
|
pr_debug("NumEccBlocksPerPageSDK: 0x%08x\n", fcb->NumEccBlocksPerPageSDK);
|
|
|
|
pr_debug("MetadataBytesSDK: 0x%08x\n", fcb->MetadataBytesSDK);
|
|
|
|
pr_debug("EraseThreshold: 0x%08x\n", fcb->EraseThreshold);
|
|
|
|
pr_debug("BootPatch: 0x%08x\n", fcb->BootPatch);
|
|
|
|
pr_debug("PatchSectors: 0x%08x\n", fcb->PatchSectors);
|
|
|
|
pr_debug("Firmware1_startingPage: 0x%08x\n", fcb->Firmware1_startingPage);
|
|
|
|
pr_debug("Firmware2_startingPage: 0x%08x\n", fcb->Firmware2_startingPage);
|
|
|
|
pr_debug("PagesInFirmware1: 0x%08x\n", fcb->PagesInFirmware1);
|
|
|
|
pr_debug("PagesInFirmware2: 0x%08x\n", fcb->PagesInFirmware2);
|
|
|
|
pr_debug("DBBTSearchAreaStartAddress: 0x%08x\n", fcb->DBBTSearchAreaStartAddress);
|
|
|
|
pr_debug("BadBlockMarkerByte: 0x%08x\n", fcb->BadBlockMarkerByte);
|
|
|
|
pr_debug("BadBlockMarkerStartBit: 0x%08x\n", fcb->BadBlockMarkerStartBit);
|
|
|
|
pr_debug("BBMarkerPhysicalOffset: 0x%08x\n", fcb->BBMarkerPhysicalOffset);
|
|
|
|
pr_debug("BCHType: 0x%08x\n", fcb->BCHType);
|
|
|
|
pr_debug("TMTiming2_ReadLatency: 0x%08x\n", fcb->TMTiming2_ReadLatency);
|
|
|
|
pr_debug("TMTiming2_PreambleDelay: 0x%08x\n", fcb->TMTiming2_PreambleDelay);
|
|
|
|
pr_debug("TMTiming2_CEDelay: 0x%08x\n", fcb->TMTiming2_CEDelay);
|
|
|
|
pr_debug("TMTiming2_PostambleDelay: 0x%08x\n", fcb->TMTiming2_PostambleDelay);
|
|
|
|
pr_debug("TMTiming2_CmdAddPause: 0x%08x\n", fcb->TMTiming2_CmdAddPause);
|
|
|
|
pr_debug("TMTiming2_DataPause: 0x%08x\n", fcb->TMTiming2_DataPause);
|
|
|
|
pr_debug("TMSpeed: 0x%08x\n", fcb->TMSpeed);
|
|
|
|
pr_debug("TMTiming1_BusyTimeout: 0x%08x\n", fcb->TMTiming1_BusyTimeout);
|
|
|
|
pr_debug("DISBBM: 0x%08x\n", fcb->DISBBM);
|
|
|
|
pr_debug("BBMarkerPhysOfsInSpareData: 0x%08x\n", fcb->BBMarkerPhysicalOffsetInSpareData);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __maybe_unused ssize_t raw_read_page(struct mtd_info *mtd, void *dst, loff_t offset)
|
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ops.mode = MTD_OPS_RAW;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = dst;
|
|
|
|
ops.len = mtd->writesize;
|
|
|
|
ops.oobbuf = dst + mtd->writesize;
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
ret = mtd_read_oob(mtd, offset, &ops);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t raw_write_page(struct mtd_info *mtd, void *buf, loff_t offset)
|
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ops.mode = MTD_OPS_RAW;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = buf;
|
|
|
|
ops.len = mtd->writesize;
|
|
|
|
ops.oobbuf = buf + mtd->writesize;
|
|
|
|
ops.ooblen = mtd->oobsize;
|
|
|
|
ret = mtd_write_oob(mtd, offset, &ops);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
static int fcb_create(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct fcb_block *fcb, struct mtd_info *mtd)
|
2014-03-06 09:03:33 +00:00
|
|
|
{
|
|
|
|
fcb->FingerPrint = 0x20424346;
|
|
|
|
fcb->Version = 0x01000000;
|
|
|
|
fcb->PageDataSize = mtd->writesize;
|
|
|
|
fcb->TotalPageSize = mtd->writesize + mtd->oobsize;
|
|
|
|
fcb->SectorsPerBlock = mtd->erasesize / mtd->writesize;
|
|
|
|
|
2015-01-28 10:11:15 +00:00
|
|
|
/* Divide ECC strength by two and save the value into FCB structure. */
|
|
|
|
fcb->EccBlock0EccType =
|
|
|
|
mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1;
|
2014-03-06 09:03:33 +00:00
|
|
|
fcb->EccBlockNEccType = fcb->EccBlock0EccType;
|
|
|
|
|
|
|
|
fcb->EccBlock0Size = 0x00000200;
|
|
|
|
fcb->EccBlockNSize = 0x00000200;
|
|
|
|
|
|
|
|
fcb->NumEccBlocksPerPage = mtd->writesize / fcb->EccBlock0Size - 1;
|
|
|
|
|
2015-06-12 04:58:24 +00:00
|
|
|
/* DBBT search area starts at second page on first block */
|
|
|
|
fcb->DBBTSearchAreaStartAddress = 1;
|
2014-03-06 09:03:33 +00:00
|
|
|
|
2015-01-28 10:11:15 +00:00
|
|
|
fcb->BadBlockMarkerByte = mxs_nand_mark_byte_offset(mtd);
|
|
|
|
fcb->BadBlockMarkerStartBit = mxs_nand_mark_bit_offset(mtd);
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
fcb->BBMarkerPhysicalOffset = mtd->writesize;
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
imx_handler->fcb_create(imx_handler, fcb, mtd);
|
|
|
|
|
2014-03-06 09:03:33 +00:00
|
|
|
fcb->Checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
static int imx_bbu_erase(struct mtd_info *mtd)
|
2014-03-06 09:03:33 +00:00
|
|
|
{
|
|
|
|
uint64_t offset = 0;
|
|
|
|
struct erase_info erase;
|
|
|
|
int ret;
|
|
|
|
|
2015-06-12 05:03:43 +00:00
|
|
|
while (offset < mtd->size) {
|
2014-03-06 09:03:33 +00:00
|
|
|
pr_debug("erasing at 0x%08llx\n", offset);
|
|
|
|
if (mtd_block_isbad(mtd, offset)) {
|
|
|
|
pr_debug("erase skip block @ 0x%08llx\n", offset);
|
2015-06-12 04:40:52 +00:00
|
|
|
offset += mtd->erasesize;
|
2014-03-06 09:03:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&erase, 0, sizeof(erase));
|
|
|
|
erase.addr = offset;
|
|
|
|
erase.len = mtd->erasesize;
|
|
|
|
|
|
|
|
ret = mtd_erase(mtd, &erase);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
offset += mtd->erasesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
/**
|
|
|
|
* imx_bbu_firmware_max_blocks - get max number of blocks for firmware
|
|
|
|
* @mtd: The mtd device
|
|
|
|
*
|
|
|
|
* We use 4 blocks for FCB/DBBT, the rest of the partition is
|
|
|
|
* divided into two equally sized firmware slots. This function
|
|
|
|
* returns the number of blocks available for one firmware slot.
|
|
|
|
* The actually usable size may be smaller due to bad blocks.
|
|
|
|
*/
|
|
|
|
static int imx_bbu_firmware_max_blocks(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
return (mtd_div_by_eb(mtd->size, mtd) - 4) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* imx_bbu_firmware_start_block - get start block for a firmware slot
|
|
|
|
* @mtd: The mtd device
|
|
|
|
* @num: The slot number (0 or 1)
|
|
|
|
*
|
|
|
|
* We use 4 blocks for FCB/DBBT, the rest of the partition is
|
|
|
|
* divided into two equally sized firmware slots. This function
|
|
|
|
* returns the start block for the given firmware slot.
|
|
|
|
*/
|
|
|
|
static int imx_bbu_firmware_start_block(struct mtd_info *mtd, int num)
|
|
|
|
{
|
|
|
|
return 4 + num * imx_bbu_firmware_max_blocks(mtd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int imx_bbu_write_firmware(struct mtd_info *mtd, unsigned num, void *buf,
|
|
|
|
size_t len)
|
2014-03-06 09:03:33 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t written;
|
2016-03-04 11:04:24 +00:00
|
|
|
int num_blocks = imx_bbu_firmware_max_blocks(mtd);
|
|
|
|
int block = imx_bbu_firmware_start_block(mtd, num);
|
|
|
|
uint64_t offset = block * mtd->erasesize;
|
|
|
|
|
|
|
|
pr_info("writing firmware %d to block %d (ofs 0x%08x)\n",
|
|
|
|
num, block, block * mtd->erasesize);
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
int now = min(len, mtd->erasesize);
|
|
|
|
|
2015-06-12 05:48:51 +00:00
|
|
|
if (!num_blocks)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2014-03-06 09:03:33 +00:00
|
|
|
pr_debug("writing %p at 0x%08llx, left 0x%08x\n",
|
|
|
|
buf, offset, len);
|
|
|
|
|
|
|
|
if (mtd_block_isbad(mtd, offset)) {
|
2015-06-12 04:40:52 +00:00
|
|
|
pr_debug("write skip block @ 0x%08llx\n", offset);
|
2014-03-06 09:03:33 +00:00
|
|
|
offset += mtd->erasesize;
|
|
|
|
block++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mtd_write(mtd, offset, now, &written, buf);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
offset += now;
|
|
|
|
len -= now;
|
|
|
|
buf += now;
|
|
|
|
block++;
|
2015-06-12 05:48:51 +00:00
|
|
|
num_blocks--;
|
2014-03-06 09:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2016-03-01 09:36:39 +00:00
|
|
|
static int dbbt_data_create(struct mtd_info *mtd, void *buf, int num_blocks)
|
2014-03-06 09:03:33 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int n_bad_blocks = 0;
|
|
|
|
uint32_t *bb = buf + 0x8;
|
|
|
|
uint32_t *n_bad_blocksp = buf + 0x4;
|
|
|
|
|
2016-03-01 09:36:39 +00:00
|
|
|
for (n = 0; n < num_blocks; n++) {
|
2014-03-06 09:03:33 +00:00
|
|
|
loff_t offset = n * mtd->erasesize;
|
|
|
|
if (mtd_block_isbad(mtd, offset)) {
|
|
|
|
n_bad_blocks++;
|
|
|
|
*bb = n;
|
|
|
|
bb++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*n_bad_blocksp = n_bad_blocks;
|
|
|
|
|
|
|
|
return n_bad_blocks;
|
|
|
|
}
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
static int imx_bbu_nand_update(struct bbu_handler *handler, struct bbu_data *data)
|
2014-03-06 09:03:33 +00:00
|
|
|
{
|
2015-06-10 13:13:16 +00:00
|
|
|
struct imx_nand_fcb_bbu_handler *imx_handler =
|
|
|
|
container_of(handler, struct imx_nand_fcb_bbu_handler, handler);
|
2014-03-06 09:03:33 +00:00
|
|
|
struct cdev *bcb_cdev;
|
|
|
|
struct mtd_info *mtd;
|
2016-03-04 11:04:24 +00:00
|
|
|
int ret;
|
2014-03-06 09:03:33 +00:00
|
|
|
struct fcb_block *fcb;
|
|
|
|
struct dbbt_block *dbbt;
|
|
|
|
void *fcb_raw_page, *dbbt_page, *dbbt_data_page;
|
|
|
|
void *ecc;
|
|
|
|
int written;
|
|
|
|
void *fw;
|
2015-06-12 05:48:51 +00:00
|
|
|
unsigned fw_size, partition_size;
|
2014-03-06 09:03:33 +00:00
|
|
|
int i;
|
2015-06-10 13:13:16 +00:00
|
|
|
enum filetype filetype;
|
2016-03-04 11:04:24 +00:00
|
|
|
unsigned num_blocks_fw;
|
|
|
|
int pages_per_block;
|
2014-03-06 09:03:33 +00:00
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
filetype = file_detect_type(data->image, data->len);
|
|
|
|
|
|
|
|
if (filetype != imx_handler->filetype &&
|
|
|
|
!bbu_force(data, "Image is not of type %s but of type %s",
|
|
|
|
file_type_to_string(imx_handler->filetype),
|
|
|
|
file_type_to_string(filetype)))
|
2014-03-06 09:03:33 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-06-12 04:23:30 +00:00
|
|
|
bcb_cdev = cdev_by_name(handler->devicefile);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (!bcb_cdev) {
|
|
|
|
pr_err("%s: No FCB device!\n", __func__);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtd = bcb_cdev->mtd;
|
2015-06-12 05:48:51 +00:00
|
|
|
partition_size = mtd->size;
|
2016-03-04 11:04:24 +00:00
|
|
|
pages_per_block = mtd->erasesize / mtd->writesize;
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
fcb_raw_page = xzalloc(mtd->writesize + mtd->oobsize);
|
|
|
|
|
|
|
|
fcb = fcb_raw_page + 12;
|
|
|
|
ecc = fcb_raw_page + 512 + 12;
|
|
|
|
|
|
|
|
dbbt_page = xzalloc(mtd->writesize);
|
|
|
|
dbbt_data_page = xzalloc(mtd->writesize);
|
|
|
|
dbbt = dbbt_page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to write one additional page to make the ROM happy.
|
|
|
|
* Maybe the PagesInFirmwarex fields are really the number of pages - 1.
|
|
|
|
* kobs-ng has the same.
|
|
|
|
*/
|
|
|
|
fw_size = ALIGN(data->len + mtd->writesize, mtd->writesize);
|
|
|
|
fw = xzalloc(fw_size);
|
|
|
|
memcpy(fw, data->image, data->len);
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
num_blocks_fw = imx_bbu_firmware_max_blocks(mtd);
|
2015-06-12 05:48:51 +00:00
|
|
|
|
|
|
|
pr_info("maximum size per firmware: 0x%08x bytes\n",
|
|
|
|
num_blocks_fw * mtd->erasesize);
|
|
|
|
|
|
|
|
if (num_blocks_fw * mtd->erasesize < fw_size)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
ret = bbu_confirm(data);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
2014-03-06 09:03:33 +00:00
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
ret = imx_bbu_erase(mtd);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
ret = imx_bbu_write_firmware(mtd, 0, fw, fw_size);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
ret = imx_bbu_write_firmware(mtd, 1, fw, fw_size);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
fcb->Firmware1_startingPage = imx_bbu_firmware_start_block(mtd, 0) * pages_per_block;
|
|
|
|
fcb->Firmware2_startingPage = imx_bbu_firmware_start_block(mtd, 1) * pages_per_block;
|
2014-03-06 09:03:33 +00:00
|
|
|
fcb->PagesInFirmware1 = ALIGN(data->len, mtd->writesize) / mtd->writesize;
|
|
|
|
fcb->PagesInFirmware2 = fcb->PagesInFirmware1;
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
fcb_create(imx_handler, fcb, mtd);
|
2014-03-06 09:03:33 +00:00
|
|
|
encode_hamming_13_8(fcb, ecc, 512);
|
2015-01-27 13:14:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the first and second byte of OOB data to 0xFF, not 0x00. These
|
|
|
|
* bytes are used as the Manufacturers Bad Block Marker (MBBM). Since
|
|
|
|
* the FCB is mostly written to the first page in a block, a scan for
|
|
|
|
* factory bad blocks will detect these blocks as bad, e.g. when
|
|
|
|
* function nand_scan_bbt() is executed to build a new bad block table.
|
|
|
|
*/
|
|
|
|
memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
|
|
|
|
|
2014-03-06 09:03:33 +00:00
|
|
|
dbbt->Checksum = 0;
|
|
|
|
dbbt->FingerPrint = 0x54424244;
|
|
|
|
dbbt->Version = 0x01000000;
|
|
|
|
|
2016-03-04 11:04:24 +00:00
|
|
|
ret = dbbt_data_create(mtd, dbbt_data_page, partition_size / mtd->erasesize);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
if (ret > 0) {
|
2014-03-06 09:03:33 +00:00
|
|
|
dbbt->DBBTNumOfPages = 1;
|
2015-06-10 13:13:16 +00:00
|
|
|
if (imx_handler->dbbt_create)
|
|
|
|
imx_handler->dbbt_create(imx_handler, dbbt, ret);
|
|
|
|
}
|
2014-03-06 09:03:33 +00:00
|
|
|
|
2015-06-12 04:58:24 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
ret = raw_write_page(mtd, fcb_raw_page, mtd->erasesize * i);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
|
|
|
|
mtd->writesize, &written, dbbt_page);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (dbbt->DBBTNumOfPages > 0) {
|
2015-06-12 04:58:24 +00:00
|
|
|
ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize * 5,
|
|
|
|
mtd->writesize, &written, dbbt_data_page);
|
2014-03-06 09:03:33 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(dbbt_page);
|
|
|
|
free(dbbt_data_page);
|
|
|
|
free(fcb_raw_page);
|
|
|
|
free(fw);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
static void imx6_fcb_create(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct fcb_block *fcb, struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
/* Also hardcoded in kobs-ng */
|
|
|
|
fcb->DataSetup = 80;
|
|
|
|
fcb->DataHold = 60;
|
|
|
|
fcb->AddressSetup = 25;
|
|
|
|
fcb->DSAMPLE_TIME = 6;
|
|
|
|
fcb->MetadataBytes = 10;
|
|
|
|
}
|
|
|
|
|
2014-03-06 09:03:33 +00:00
|
|
|
int imx6_bbu_nand_register_handler(const char *name, unsigned long flags)
|
|
|
|
{
|
2015-06-10 13:13:16 +00:00
|
|
|
struct imx_nand_fcb_bbu_handler *imx_handler;
|
|
|
|
struct bbu_handler *handler;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
imx_handler = xzalloc(sizeof(*imx_handler));
|
|
|
|
imx_handler->fcb_create = imx6_fcb_create;
|
|
|
|
imx_handler->filetype = filetype_arm_barebox;
|
|
|
|
|
|
|
|
handler = &imx_handler->handler;
|
|
|
|
handler->devicefile = "nand0.barebox";
|
|
|
|
handler->name = name;
|
|
|
|
handler->flags = flags;
|
|
|
|
handler->handler = imx_bbu_nand_update;
|
|
|
|
|
|
|
|
ret = bbu_register_handler(handler);
|
|
|
|
if (ret)
|
|
|
|
free(handler);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_IMX28
|
|
|
|
#include <mach/imx28-regs.h>
|
|
|
|
|
|
|
|
#define GPMI_TIMING0 0x00000070
|
|
|
|
#define GPMI_TIMING0_ADDRESS_SETUP_MASK (0xff << 16)
|
|
|
|
#define GPMI_TIMING0_ADDRESS_SETUP_OFFSET 16
|
|
|
|
#define GPMI_TIMING0_DATA_HOLD_MASK (0xff << 8)
|
|
|
|
#define GPMI_TIMING0_DATA_HOLD_OFFSET 8
|
|
|
|
#define GPMI_TIMING0_DATA_SETUP_MASK 0xff
|
|
|
|
#define GPMI_TIMING0_DATA_SETUP_OFFSET 0
|
|
|
|
|
|
|
|
#define GPMI_TIMING1 0x00000080
|
|
|
|
|
|
|
|
#define BCH_MODE 0x00000020
|
|
|
|
|
|
|
|
#define BCH_FLASH0LAYOUT0 0x00000080
|
|
|
|
#define BCH_FLASHLAYOUT0_NBLOCKS_MASK (0xff << 24)
|
|
|
|
#define BCH_FLASHLAYOUT0_NBLOCKS_OFFSET 24
|
|
|
|
#define BCH_FLASHLAYOUT0_META_SIZE_MASK (0xff << 16)
|
|
|
|
#define BCH_FLASHLAYOUT0_META_SIZE_OFFSET 16
|
|
|
|
#define BCH_FLASHLAYOUT0_ECC0_MASK (0xf << 12)
|
|
|
|
#define BCH_FLASHLAYOUT0_ECC0_OFFSET 12
|
|
|
|
#define BCH_FLASHLAYOUT0_DATA0_SIZE_MASK 0xfff
|
|
|
|
#define BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET 0
|
|
|
|
|
|
|
|
#define BCH_FLASH0LAYOUT1 0x00000090
|
|
|
|
#define BCH_FLASHLAYOUT1_PAGE_SIZE_MASK (0xffff << 16)
|
|
|
|
#define BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET 16
|
|
|
|
#define BCH_FLASHLAYOUT1_ECCN_MASK (0xf << 12)
|
|
|
|
#define BCH_FLASHLAYOUT1_ECCN_OFFSET 12
|
|
|
|
#define BCH_FLASHLAYOUT1_DATAN_SIZE_MASK 0xfff
|
|
|
|
#define BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET 0
|
|
|
|
|
|
|
|
static void imx28_fcb_create(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct fcb_block *fcb, struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
u32 fl0, fl1, t0;
|
|
|
|
void __iomem *bch_regs = (void *)MXS_BCH_BASE;
|
|
|
|
void __iomem *gpmi_regs = (void *)MXS_GPMI_BASE;
|
|
|
|
|
|
|
|
fl0 = readl(bch_regs + BCH_FLASH0LAYOUT0);
|
|
|
|
fl1 = readl(bch_regs + BCH_FLASH0LAYOUT1);
|
|
|
|
t0 = readl(gpmi_regs + GPMI_TIMING0);
|
|
|
|
|
|
|
|
fcb->MetadataBytes = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
|
|
|
|
fcb->DataSetup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
|
|
|
|
fcb->DataHold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
|
|
|
|
fcb->AddressSetup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
|
|
|
|
fcb->MetadataBytes = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
|
|
|
|
fcb->NumEccBlocksPerPage = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
|
|
|
|
fcb->EraseThreshold = readl(bch_regs + BCH_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void imx28_dbbt_create(struct imx_nand_fcb_bbu_handler *imx_handler,
|
|
|
|
struct dbbt_block *dbbt, int num_bad_blocks)
|
|
|
|
{
|
|
|
|
uint32_t a = 0;
|
|
|
|
uint8_t *p = (void *)dbbt;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dbbt->numberBB = num_bad_blocks;
|
|
|
|
|
|
|
|
for (i = 4; i < 512; i++)
|
|
|
|
a += p[i];
|
|
|
|
|
|
|
|
a ^= 0xffffffff;
|
|
|
|
|
|
|
|
dbbt->Checksum = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
int imx28_bbu_nand_register_handler(const char *name, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct imx_nand_fcb_bbu_handler *imx_handler;
|
2014-03-06 09:03:33 +00:00
|
|
|
struct bbu_handler *handler;
|
|
|
|
int ret;
|
|
|
|
|
2015-06-10 13:13:16 +00:00
|
|
|
imx_handler = xzalloc(sizeof(*imx_handler));
|
|
|
|
imx_handler->fcb_create = imx28_fcb_create;
|
|
|
|
imx_handler->dbbt_create = imx28_dbbt_create;
|
|
|
|
|
|
|
|
imx_handler->filetype = filetype_mxs_bootstream;
|
|
|
|
|
|
|
|
handler = &imx_handler->handler;
|
2015-06-12 04:23:30 +00:00
|
|
|
handler->devicefile = "nand0.barebox";
|
2014-03-06 09:03:33 +00:00
|
|
|
handler->name = name;
|
|
|
|
handler->flags = flags;
|
2015-06-10 13:13:16 +00:00
|
|
|
handler->handler = imx_bbu_nand_update;
|
2014-03-06 09:03:33 +00:00
|
|
|
|
|
|
|
ret = bbu_register_handler(handler);
|
|
|
|
if (ret)
|
|
|
|
free(handler);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-06-10 13:13:16 +00:00
|
|
|
#endif
|