// Get chip select mask for command register
#define MT29F_CSID(chip) (((chip)->chip_select << NFC_CMD_CSID_SHIFT) & NFC_CMD_CSID_MASK)
+// Get block from page
+#define PAGE(blk) ((blk) * MT29F_PAGES_PER_BLOCK)
+
+// Page from block and page in block
+#define BLOCK(page) ((uint16_t)((page) / MT29F_PAGES_PER_BLOCK))
+#define PAGE_IN_BLOCK(page) ((uint16_t)((page) % MT29F_PAGES_PER_BLOCK))
+
/*
* Remap info written in the first page of each block
uint32_t cycle0;
uint32_t cycle1234;
- getAddrCycles(block * MT29F_PAGES_PER_BLOCK, 0, &cycle0, &cycle1234);
+ uint16_t remapped_block = chip->block_map[block];
+ if (block != remapped_block)
+ {
+ LOG_INFO("mt29f_blockErase: remapped block: blk %d->%d\n", block, remapped_block);
+ block = remapped_block;
+ }
+
+ getAddrCycles(PAGE(block), 0, &cycle0, &cycle1234);
sendCommand(MT29F_CSID(chip) |
NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
}
-static bool checkEcc(void)
+static bool checkEcc(Mt29f *chip)
{
- uint32_t sr1 = SMC_ECC_SR1;
+ struct RemapInfo *remap_info = (struct RemapInfo *)(NFC_SRAM_BASE_ADDR + MT29F_REMAP_TAG_OFFSET);
- if (sr1)
+ /*
+ * Check for ECC hardware status only if a valid RemapInfo structure is found.
+ * That guarantees we wrote the block and a valid ECC is present.
+ */
+ if (remap_info->tag == MT29F_REMAP_TAG)
{
- LOG_INFO("ECC error, ECC_SR1=0x%lx\n", sr1);
- return false;
+ uint32_t sr1 = SMC_ECC_SR1;
+ if (sr1)
+ {
+ LOG_INFO("ECC error, ECC_SR1=0x%lx\n", sr1);
+ chip->status |= MT29F_ERR_ECC;
+ return false;
+ }
}
- else
- return true;
+
+ return true;
}
* Read page data and ECC, checking for errors.
* TODO: fix errors with ECC when possible.
*/
-bool mt29f_read(Mt29f *chip, uint32_t page, void *buf, uint16_t size)
+static bool mt29f_read(Mt29f *chip, uint32_t page, void *buf, uint16_t size)
{
- ASSERT(size <= MT29F_DATA_SIZE);
+ uint32_t remapped_page = PAGE(chip->block_map[BLOCK(page)]) + PAGE_IN_BLOCK(page);
+
+ if (page != remapped_page)
+ {
+ LOG_INFO("mt29f_read: remapped block: blk %d->%d, pg %ld->%ld\n",
+ BLOCK(page), chip->block_map[BLOCK(page)], page, remapped_page);
+ page = remapped_page;
+ }
if (!mt29f_readPage(chip, page, 0))
return false;
memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
- return checkEcc();
+ return checkEcc(chip);
}
uint32_t cycle0;
uint32_t cycle1234;
- LOG_INFO("mt29f_writePage: page 0x%lx off 0x%x\n", page, offset);
+ //LOG_INFO("mt29f_writePage: page 0x%lx off 0x%x\n", page, offset);
getAddrCycles(page, offset, &cycle0, &cycle1234);
/*
* Write the spare area in a page: ECC and remap block index.
+ * \param page the page to be written
+ * \parma original_page if different from page, it's the page that's being remapped
*
* ECC data are extracted from ECC_PRx registers and written
* in the page's spare area.
* For 2048 bytes pages and 1 ECC word each 256 bytes,
* 24 bytes of ECC data are stored.
*/
-static bool mt29f_writePageSpare(Mt29f *chip, uint32_t page)
+static bool mt29f_writePageSpare(Mt29f *chip, uint32_t page, uint32_t original_page)
{
int i;
uint32_t *buf = (uint32_t *)NFC_SRAM_BASE_ADDR;
- uint16_t blk = page / MT29F_PAGES_PER_BLOCK;
- uint16_t page_in_blk = page % MT29F_PAGES_PER_BLOCK;
struct RemapInfo *remap_info = (struct RemapInfo *)(NFC_SRAM_BASE_ADDR + MT29F_REMAP_TAG_OFFSET);
memset((void *)NFC_SRAM_BASE_ADDR, 0xff, MT29F_SPARE_SIZE);
for (i = 0; i < MT29F_ECC_NWORDS; i++)
buf[i] = *((reg32_t *)(SMC_BASE + SMC_ECC_PR0_OFF) + i);
- // Check for remapped block
- if (chip->block_map[blk] != blk)
- page = chip->block_map[blk] * MT29F_PAGES_PER_BLOCK + page_in_blk;
-
- // Write remap tag in first page in block
- if (page_in_blk == 0)
- {
- remap_info->tag = MT29F_REMAP_TAG;
- remap_info->mapped_blk = blk;
- }
+ // Write remap tag
+ remap_info->tag = MT29F_REMAP_TAG;
+ remap_info->mapped_blk = BLOCK(original_page);
return mt29f_writePage(chip, page, MT29F_DATA_SIZE);
}
-bool mt29f_write(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
+static bool mt29f_write(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
{
- return
- mt29f_writePageData(chip, page, buf, size) &&
- mt29f_writePageSpare(chip, page);
-}
+ uint32_t remapped_page = PAGE(chip->block_map[BLOCK(page)]) + PAGE_IN_BLOCK(page);
+ if (page != remapped_page)
+ LOG_INFO("mt29f_write: remapped block: blk %d->%d, pg %ld->%ld\n",
+ BLOCK(page), chip->block_map[BLOCK(page)], page, remapped_page);
-int mt29f_error(Mt29f *chip)
-{
- return chip->status;
-}
-
-
-void mt29f_clearError(Mt29f *chip)
-{
- chip->status = 0;
+ return
+ mt29f_writePageData(chip, remapped_page, buf, size) &&
+ mt29f_writePageSpare(chip, remapped_page, page);
}
bool good;
// Check first byte in spare area of first page in block
- mt29f_readPage(chip, blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE);
- good = *first_byte == 0xFF;
+ mt29f_readPage(chip, PAGE(blk), MT29F_DATA_SIZE);
+ good = *first_byte != 0;
if (!good)
LOG_INFO("mt29f: bad block %d\n", blk);
{
struct RemapInfo *remap_info = (struct RemapInfo *)NFC_SRAM_BASE_ADDR;
- if (!mt29f_readPage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
+ if (!mt29f_readPage(chip, PAGE(dest_blk), MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
return -1;
if (remap_info->tag == MT29F_REMAP_TAG)
LOG_INFO("mt29f, setMapping(): src=%ld dst=%ld\n", src_blk, dest_blk);
- if (!mt29f_readPage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
+ if (!mt29f_readPage(chip, PAGE(dest_blk), MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
return false;
remap_info->tag = MT29F_REMAP_TAG;
remap_info->mapped_blk = src_blk;
- return mt29f_writePage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET);
+ return mt29f_writePage(chip, PAGE(dest_blk), MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET);
}
*/
static void initBlockMap(Mt29f *chip)
{
- unsigned b, last;
+ int b, last;
// Default is for each block to not be remapped
- for (b = 0; b < MT29F_NUM_USER_BLOCKS; b++)
+ for (b = 0; b < MT29F_NUM_BLOCKS; b++)
chip->block_map[b] = b;
chip->remap_start = MT29F_NUM_USER_BLOCKS;
// Scan for assigned blocks in remap area
for (b = last = MT29F_NUM_USER_BLOCKS; b < MT29F_NUM_BLOCKS; b++)
{
- if (blockIsGood(chip, b))
+ int remapped_blk = getBadBlockFromRemapBlock(chip, b);
+ if (remapped_blk != -1 && remapped_blk != b)
{
- int remapped_blk = getBadBlockFromRemapBlock(chip, b);
- if (remapped_blk != -1 && remapped_blk != MT29F_NULL_REMAP)
- {
- LOG_INFO("mt29f: found remapped block %d->%d\n", remapped_blk, b);
- chip->block_map[remapped_blk] = b;
- last = b + 1;
- }
+ LOG_INFO("mt29f: found remapped block %d->%d\n", remapped_blk, b);
+ chip->block_map[remapped_blk] = b;
+ last = b + 1;
}
}
chip->remap_start = last;
*/
if (!remapped_anything)
{
- setMapping(chip, MT29F_NULL_REMAP, MT29F_NUM_USER_BLOCKS);
+ setMapping(chip, MT29F_NUM_USER_BLOCKS, MT29F_NUM_USER_BLOCKS);
LOG_INFO("mt29f: no bad block founds, marked NAND\n");
}
}
}
-#ifdef _DEBUG
-
-/*
- * Erase all blocks.
- * DON'T USE on production chips: this function will try to erase
- * factory marked bad blocks too.
+/**
+ * Reset bad blocks map and erase all blocks.
+ *
+ * \note DON'T USE on production chips: this function will try to erase
+ * factory marked bad blocks too.
*/
-static void mt29f_wipe(Mt29f *chip)
+void mt29f_format(Mt29f *chip)
{
int b;
+
for (b = 0; b < MT29F_NUM_BLOCKS; b++)
{
LOG_INFO("mt29f: erasing block %d\n", b);
+ chip->block_map[b] = b;
mt29f_blockErase(chip, b);
}
+ chip->remap_start = MT29F_NUM_USER_BLOCKS;
}
+#ifdef _DEBUG
+
/*
* Create some bad blocks, erasing them and writing the bad block mark.
*/
-static void mt29f_ruinSomeBlocks(Mt29f *chip)
+void mt29f_ruinSomeBlocks(Mt29f *chip)
{
int bads[] = { 7, 99, 555, 1003, 1004, 1432 };
unsigned i;
LOG_INFO("mt29f: erasing block %d\n", bads[i]);
mt29f_blockErase(chip, bads[i]);
- LOG_INFO("mt29f: marking page %d as bad\n", bads[i] * MT29F_PAGES_PER_BLOCK);
+ LOG_INFO("mt29f: marking page %d as bad\n", PAGE(bads[i]));
memset((void *)NFC_SRAM_BASE_ADDR, 0, MT29F_SPARE_SIZE);
- mt29f_writePage(chip, bads[i] * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE);
+ mt29f_writePage(chip, PAGE(bads[i]), MT29F_DATA_SIZE);
}
}
}
-bool mt29f_init(Mt29f *chip, struct Heap *heap, uint8_t chip_select)
+static bool commonInit(Mt29f *chip, struct Heap *heap, unsigned chip_select)
{
memset(chip, 0, sizeof(Mt29f));
+ DB(chip->fd.priv.type = KBT_NAND);
+ chip->fd.blk_size = MT29F_BLOCK_SIZE;
+ chip->fd.blk_cnt = MT29F_NUM_USER_BLOCKS;
+
chip->chip_select = chip_select;
- chip->block_map = heap_allocmem(heap, MT29F_NUM_USER_BLOCKS * sizeof(*chip->block_map));
+ chip->block_map = heap_allocmem(heap, MT29F_NUM_BLOCKS * sizeof(*chip->block_map));
if (!chip->block_map)
{
LOG_ERR("mt29f: error allocating block map\n");
return true;
}
+
+/**************** Kblock interface ****************/
+
+
+static size_t mt29f_writeDirect(struct KBlock *kblk, block_idx_t idx, const void *buf, size_t offset, size_t size)
+{
+ ASSERT(offset <= MT29F_BLOCK_SIZE);
+ ASSERT(offset % MT29F_DATA_SIZE == 0);
+ ASSERT(size <= MT29F_BLOCK_SIZE);
+ ASSERT(size % MT29F_DATA_SIZE == 0);
+
+ LOG_INFO("mt29f_writeDirect: blk=%ld\n", idx);
+
+ mt29f_blockErase(MT29F_CAST(kblk), idx);
+
+ while (offset < size)
+ {
+ uint32_t page = PAGE(idx) + (offset / MT29F_DATA_SIZE);
+
+ if (!mt29f_write(MT29F_CAST(kblk), page, buf, MT29F_DATA_SIZE))
+ break;
+
+ offset += MT29F_DATA_SIZE;
+ buf = (const char *)buf + MT29F_DATA_SIZE;
+ }
+
+ return offset;
+}
+
+
+static size_t mt29f_readDirect(struct KBlock *kblk, block_idx_t idx, void *buf, size_t offset, size_t size)
+{
+ ASSERT(offset <= MT29F_BLOCK_SIZE);
+ ASSERT(offset % MT29F_DATA_SIZE == 0);
+ ASSERT(size <= MT29F_BLOCK_SIZE);
+ ASSERT(size % MT29F_DATA_SIZE == 0);
+
+ LOG_INFO("mt29f_readDirect: blk=%ld\n", idx);
+
+ while (offset < size)
+ {
+ uint32_t page = PAGE(idx) + (offset / MT29F_DATA_SIZE);
+
+ if (!mt29f_read(MT29F_CAST(kblk), page, buf, MT29F_DATA_SIZE))
+ break;
+
+ offset += MT29F_DATA_SIZE;
+ buf = (char *)buf + MT29F_DATA_SIZE;
+ }
+
+ return offset;
+}
+
+
+static int mt29f_error(struct KBlock *kblk)
+{
+ Mt29f *chip = MT29F_CAST(kblk);
+ return chip->status;
+}
+
+
+static void mt29f_clearError(struct KBlock *kblk)
+{
+ Mt29f *chip = MT29F_CAST(kblk);
+ chip->status = 0;
+}
+
+
+static const KBlockVTable mt29f_buffered_vt =
+{
+ .readDirect = mt29f_readDirect,
+ .writeDirect = mt29f_writeDirect,
+
+ .readBuf = kblock_swReadBuf,
+ .writeBuf = kblock_swWriteBuf,
+ .load = kblock_swLoad,
+ .store = kblock_swStore,
+
+ .error = mt29f_error,
+ .clearerr = mt29f_clearError,
+};
+
+static const KBlockVTable mt29f_unbuffered_vt =
+{
+ .readDirect = mt29f_readDirect,
+ .writeDirect = mt29f_writeDirect,
+
+ .error = mt29f_error,
+ .clearerr = mt29f_clearError,
+};
+
+
+bool mt29f_init(Mt29f *chip, struct Heap *heap, unsigned chip_select)
+{
+ if (!commonInit(chip, heap, chip_select))
+ return false;
+
+ chip->fd.priv.vt = &mt29f_buffered_vt;
+ chip->fd.priv.flags |= KB_BUFFERED;
+
+ chip->fd.priv.buf = heap_allocmem(heap, MT29F_BLOCK_SIZE);
+ if (!chip->fd.priv.buf)
+ {
+ LOG_ERR("mt29f: error allocating block buffer\n");
+ return false;
+ }
+
+ // Load the first block in the cache
+ return mt29f_readDirect(&chip->fd, 0, chip->fd.priv.buf, 0, MT29F_DATA_SIZE);
+}
+
+
+bool mt29f_initUnbuffered(Mt29f *chip, struct Heap *heap, unsigned chip_select)
+{
+ if (!commonInit(chip, heap, chip_select))
+ return false;
+
+ chip->fd.priv.vt = &mt29f_unbuffered_vt;
+ return true;
+}
+
+