upstream u-boot with additional patches for our devices/boards:
https://lists.denx.de/pipermail/u-boot/2017-March/282789.html (AXP crashes) ;
Gbit ethernet patch for some LIME2 revisions ;
with SPI flash support
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
265 lines
6.7 KiB
265 lines
6.7 KiB
7 years ago
|
// SPDX-License-Identifier: GPL-2.0+
|
||
11 years ago
|
/*
|
||
|
* Copyright (C) 2014 Gateworks Corporation
|
||
|
* Author: Tim Harvey <tharvey@gateworks.com>
|
||
|
*/
|
||
|
#include <common.h>
|
||
|
#include <nand.h>
|
||
|
#include <malloc.h>
|
||
7 years ago
|
#include "mxs_nand.h"
|
||
11 years ago
|
|
||
9 years ago
|
static struct mtd_info *mtd;
|
||
11 years ago
|
static struct nand_chip nand_chip;
|
||
|
|
||
|
static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
|
||
|
int column, int page_addr)
|
||
|
{
|
||
9 years ago
|
register struct nand_chip *chip = mtd_to_nand(mtd);
|
||
11 years ago
|
u32 timeo, time_start;
|
||
|
|
||
|
/* write out the command to the device */
|
||
|
chip->cmd_ctrl(mtd, command, NAND_CLE);
|
||
|
|
||
|
/* Serially input address */
|
||
|
if (column != -1) {
|
||
|
chip->cmd_ctrl(mtd, column, NAND_ALE);
|
||
|
chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
|
||
|
}
|
||
|
if (page_addr != -1) {
|
||
|
chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
|
||
|
chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
|
||
|
/* One more address cycle for devices > 128MiB */
|
||
|
if (chip->chipsize > (128 << 20))
|
||
|
chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
|
||
|
}
|
||
|
chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
|
||
|
|
||
|
if (command == NAND_CMD_READ0) {
|
||
|
chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
|
||
|
chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
|
||
|
}
|
||
|
|
||
|
/* wait for nand ready */
|
||
|
ndelay(100);
|
||
|
timeo = (CONFIG_SYS_HZ * 20) / 1000;
|
||
|
time_start = get_timer(0);
|
||
|
while (get_timer(time_start) < timeo) {
|
||
|
if (chip->dev_ready(mtd))
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
#if defined (CONFIG_SPL_NAND_IDENT)
|
||
|
|
||
|
/* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
|
||
|
static int mxs_flash_full_ident(struct mtd_info *mtd)
|
||
|
{
|
||
|
int nand_maf_id, nand_dev_id;
|
||
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
||
|
struct nand_flash_dev *type;
|
||
|
|
||
|
type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL);
|
||
|
|
||
|
if (IS_ERR(type)) {
|
||
|
chip->select_chip(mtd, -1);
|
||
|
return PTR_ERR(type);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
/* Trying to detect the NAND flash using ONFi only */
|
||
7 years ago
|
static int mxs_flash_onfi_ident(struct mtd_info *mtd)
|
||
11 years ago
|
{
|
||
9 years ago
|
register struct nand_chip *chip = mtd_to_nand(mtd);
|
||
11 years ago
|
int i;
|
||
|
u8 mfg_id, dev_id;
|
||
|
u8 id_data[8];
|
||
|
struct nand_onfi_params *p = &chip->onfi_params;
|
||
|
|
||
|
/* Reset the chip */
|
||
|
chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
|
||
|
|
||
|
/* Send the command for reading device ID */
|
||
|
chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
|
||
|
|
||
|
/* Read manufacturer and device IDs */
|
||
|
mfg_id = chip->read_byte(mtd);
|
||
|
dev_id = chip->read_byte(mtd);
|
||
|
|
||
|
/* Try again to make sure */
|
||
|
chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
|
||
|
for (i = 0; i < 8; i++)
|
||
|
id_data[i] = chip->read_byte(mtd);
|
||
|
if (id_data[0] != mfg_id || id_data[1] != dev_id) {
|
||
|
printf("second ID read did not match");
|
||
|
return -1;
|
||
|
}
|
||
|
debug("0x%02x:0x%02x ", mfg_id, dev_id);
|
||
|
|
||
|
/* read ONFI */
|
||
|
chip->onfi_version = 0;
|
||
|
chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
|
||
|
if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
|
||
|
chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
|
||
|
return -2;
|
||
|
}
|
||
|
|
||
|
/* we have ONFI, probe it */
|
||
|
chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
|
||
|
chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
|
||
|
mtd->name = p->model;
|
||
|
mtd->writesize = le32_to_cpu(p->byte_per_page);
|
||
|
mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
|
||
|
mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
|
||
|
chip->chipsize = le32_to_cpu(p->blocks_per_lun);
|
||
|
chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
|
||
|
/* Calculate the address shift from the page size */
|
||
|
chip->page_shift = ffs(mtd->writesize) - 1;
|
||
|
chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
|
||
|
/* Convert chipsize to number of pages per chip -1 */
|
||
|
chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
|
||
|
chip->badblockbits = 8;
|
||
|
|
||
|
debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
|
||
|
debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
|
||
|
debug("oobsize=%d\n", mtd->oobsize);
|
||
|
debug("chipsize=%lld\n", chip->chipsize);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
#endif /* CONFIG_SPL_NAND_IDENT */
|
||
|
|
||
7 years ago
|
static int mxs_flash_ident(struct mtd_info *mtd)
|
||
|
{
|
||
|
int ret;
|
||
7 years ago
|
#if defined (CONFIG_SPL_NAND_IDENT)
|
||
|
ret = mxs_flash_full_ident(mtd);
|
||
|
#else
|
||
7 years ago
|
ret = mxs_flash_onfi_ident(mtd);
|
||
7 years ago
|
#endif
|
||
7 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
|
||
|
{
|
||
9 years ago
|
register struct nand_chip *chip = mtd_to_nand(mtd);
|
||
11 years ago
|
int ret;
|
||
|
|
||
|
chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
|
||
|
ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
|
||
|
if (ret < 0) {
|
||
|
printf("read_page failed %d\n", ret);
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
|
||
|
{
|
||
9 years ago
|
register struct nand_chip *chip = mtd_to_nand(mtd);
|
||
11 years ago
|
unsigned int block = offs >> chip->phys_erase_shift;
|
||
|
unsigned int page = offs >> chip->page_shift;
|
||
|
|
||
|
debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
|
||
|
page);
|
||
|
chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
|
||
|
memset(chip->oob_poi, 0, mtd->oobsize);
|
||
|
chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
|
||
|
|
||
|
return chip->oob_poi[0] != 0xff;
|
||
|
}
|
||
|
|
||
|
/* setup mtd and nand structs and init mxs_nand driver */
|
||
|
static int mxs_nand_init(void)
|
||
|
{
|
||
|
/* return if already initalized */
|
||
|
if (nand_chip.numchips)
|
||
|
return 0;
|
||
|
|
||
|
/* init mxs nand driver */
|
||
7 years ago
|
mxs_nand_init_spl(&nand_chip);
|
||
9 years ago
|
mtd = nand_to_mtd(&nand_chip);
|
||
11 years ago
|
/* set mtd functions */
|
||
|
nand_chip.cmdfunc = mxs_nand_command;
|
||
|
nand_chip.numchips = 1;
|
||
|
|
||
|
/* identify flash device */
|
||
9 years ago
|
if (mxs_flash_ident(mtd)) {
|
||
11 years ago
|
printf("Failed to identify\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
/* allocate and initialize buffers */
|
||
|
nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
|
||
|
sizeof(*nand_chip.buffers));
|
||
9 years ago
|
nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
|
||
11 years ago
|
/* setup flash layout (does not scan as we override that) */
|
||
9 years ago
|
mtd->size = nand_chip.chipsize;
|
||
|
nand_chip.scan_bbt(mtd);
|
||
11 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
|
||
|
{
|
||
|
struct nand_chip *chip;
|
||
|
unsigned int page;
|
||
|
unsigned int nand_page_per_block;
|
||
|
unsigned int sz = 0;
|
||
|
|
||
|
if (mxs_nand_init())
|
||
|
return -ENODEV;
|
||
9 years ago
|
chip = mtd_to_nand(mtd);
|
||
11 years ago
|
page = offs >> chip->page_shift;
|
||
9 years ago
|
nand_page_per_block = mtd->erasesize / mtd->writesize;
|
||
11 years ago
|
|
||
|
debug("%s offset:0x%08x len:%d page:%d\n", __func__, offs, size, page);
|
||
|
|
||
9 years ago
|
size = roundup(size, mtd->writesize);
|
||
11 years ago
|
while (sz < size) {
|
||
9 years ago
|
if (mxs_read_page_ecc(mtd, buf, page) < 0)
|
||
11 years ago
|
return -1;
|
||
9 years ago
|
sz += mtd->writesize;
|
||
|
offs += mtd->writesize;
|
||
11 years ago
|
page++;
|
||
9 years ago
|
buf += mtd->writesize;
|
||
11 years ago
|
|
||
|
/*
|
||
|
* Check if we have crossed a block boundary, and if so
|
||
|
* check for bad block.
|
||
|
*/
|
||
|
if (!(page % nand_page_per_block)) {
|
||
|
/*
|
||
|
* Yes, new block. See if this block is good. If not,
|
||
|
* loop until we find a good block.
|
||
|
*/
|
||
9 years ago
|
while (is_badblock(mtd, offs, 1)) {
|
||
11 years ago
|
page = page + nand_page_per_block;
|
||
|
/* Check i we've reached the end of flash. */
|
||
9 years ago
|
if (page >= mtd->size >> chip->page_shift)
|
||
11 years ago
|
return -ENOMEM;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int nand_default_bbt(struct mtd_info *mtd)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void nand_init(void)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void nand_deselect(void)
|
||
|
{
|
||
|
}
|
||
|
|