2013-11-15 09:30:05 -06:00
|
|
|
/****************************************************************************
|
|
|
|
* arch/arm/src/sama5/sam_nand.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
2013-11-15 11:22:23 -06:00
|
|
|
* References:
|
|
|
|
* SAMA5D3 Series Data Sheet
|
|
|
|
* Atmel NoOS sample code.
|
|
|
|
*
|
|
|
|
* The Atmel sample code has a BSD compatibile license that requires this
|
|
|
|
* copyright notice:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, 2012, Atmel Corporation
|
|
|
|
*
|
2013-11-15 09:30:05 -06:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2013-11-15 11:22:23 -06:00
|
|
|
* 3. Neither the names NuttX nor Atmel nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
2013-11-15 09:30:05 -06:00
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2013-11-16 11:46:35 -06:00
|
|
|
#include <nuttx/mtd/nand_config.h>
|
2013-11-15 09:30:05 -06:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-11-17 08:56:30 -06:00
|
|
|
#include <assert.h>
|
2013-11-15 09:30:05 -06:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-15 11:22:23 -06:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2013-11-16 11:46:35 -06:00
|
|
|
#include <nuttx/mtd/nand.h>
|
2013-11-17 08:56:30 -06:00
|
|
|
#include <nuttx/mtd/nand_raw.h>
|
2013-11-16 18:26:07 -06:00
|
|
|
#include <nuttx/mtd/nand_model.h>
|
2013-11-16 11:46:35 -06:00
|
|
|
|
|
|
|
#include <arch/board/board.h>
|
2013-11-15 09:30:05 -06:00
|
|
|
|
|
|
|
#include "sam_nand.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-16 18:26:07 -06:00
|
|
|
/* This type represents the state of the raw NAND MTD device. The struct
|
2013-11-17 08:56:30 -06:00
|
|
|
* nand_raw_s must appear at the beginning of the definition so that you can
|
2013-11-17 12:22:09 -06:00
|
|
|
* freely cast between pointers to struct nand_raw_s and struct sam_rawnand_s.
|
2013-11-15 09:30:05 -06:00
|
|
|
*/
|
|
|
|
|
2013-11-17 08:56:30 -06:00
|
|
|
struct sam_rawnand_s
|
2013-11-15 09:30:05 -06:00
|
|
|
{
|
2013-11-17 08:56:30 -06:00
|
|
|
struct nand_raw_s raw; /* Externally visible part of the driver */
|
2013-11-16 18:26:07 -06:00
|
|
|
uint8_t cs; /* Chip select number (0..3) */
|
2013-11-15 09:30:05 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2013-11-17 12:22:09 -06:00
|
|
|
static int nand_eraseblock(struct nand_raw_s *raw, off_t block);
|
|
|
|
static int nand_readpage(struct nand_raw_s *raw, off_t block,
|
|
|
|
unsigned int page, void *data, void *spare);
|
|
|
|
static int nand_writepage(struct nand_raw_s *raw, off_t block,
|
|
|
|
unsigned int page, const void *data, const void *spare);
|
2013-11-15 09:30:05 -06:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
/* These pre-allocated structures hold the state of the MTD driver for NAND
|
|
|
|
* on CS0..3 as configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS0_NAND
|
2013-11-17 08:56:30 -06:00
|
|
|
static struct sam_rawnand_s g_cs0nand;
|
2013-11-15 09:30:05 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS1_NAND
|
2013-11-17 08:56:30 -06:00
|
|
|
static struct sam_rawnand_s g_cs1nand;
|
2013-11-15 09:30:05 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS2_NAND
|
2013-11-17 08:56:30 -06:00
|
|
|
static struct sam_rawnand_s g_cs2nand;
|
2013-11-15 09:30:05 -06:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS3_NAND
|
2013-11-17 08:56:30 -06:00
|
|
|
static struct sam_rawnand_s g_cs3nand;
|
2013-11-15 09:30:05 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-11-17 12:22:09 -06:00
|
|
|
* Name: nand_eraseblock
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2013-11-17 12:22:09 -06:00
|
|
|
* Erases the specified block of the device.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* raw - Lower-half, raw NAND FLASH interface
|
|
|
|
* block - Number of the physical block to erase.
|
|
|
|
*
|
|
|
|
* Returned value.
|
|
|
|
* OK is returned in succes; a negated errno value is returned on failure.
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-17 12:22:09 -06:00
|
|
|
static int nand_eraseblock(struct nand_raw_s *raw, off_t block)
|
2013-11-15 09:30:05 -06:00
|
|
|
{
|
2013-11-17 12:22:09 -06:00
|
|
|
struct sam_rawnand_s *priv = (struct sam_rawnand_s *)raw;
|
|
|
|
DEBUGASSERT(raw);
|
2013-11-15 09:30:05 -06:00
|
|
|
#warning Missing logic
|
2013-11-17 12:22:09 -06:00
|
|
|
return -ENOSYS;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-11-17 12:22:09 -06:00
|
|
|
* Name: nand_readpage
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2013-11-17 12:22:09 -06:00
|
|
|
* Reads the data and/or the spare areas of a page of a NAND FLASH into the
|
|
|
|
* provided buffers.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* raw - Lower-half, raw NAND FLASH interface
|
|
|
|
* block - Number of the block where the page to read resides.
|
|
|
|
* page - Number of the page to read inside the given block.
|
|
|
|
* data - Buffer where the data area will be stored.
|
|
|
|
* spare - Buffer where the spare area will be stored.
|
|
|
|
*
|
|
|
|
* Returned value.
|
|
|
|
* OK is returned in succes; a negated errno value is returned on failure.
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-17 12:22:09 -06:00
|
|
|
static int nand_readpage(struct nand_raw_s *raw, off_t block,
|
|
|
|
unsigned int page, void *data, void *spare)
|
2013-11-15 09:30:05 -06:00
|
|
|
{
|
2013-11-17 12:22:09 -06:00
|
|
|
struct sam_rawnand_s *priv = (struct sam_rawnand_s *)raw;
|
|
|
|
DEBUGASSERT(raw);
|
2013-11-15 09:30:05 -06:00
|
|
|
#warning Missing logic
|
2013-11-17 12:22:09 -06:00
|
|
|
return -ENOSYS;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-11-17 12:22:09 -06:00
|
|
|
* Name: nand_writepage
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
* Description:
|
2013-11-17 12:22:09 -06:00
|
|
|
* Writes the data and/or the spare area of a page on a NAND FLASH chip.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* raw - Lower-half, raw NAND FLASH interface
|
|
|
|
* block - Number of the block where the page to write resides.
|
|
|
|
* page - Number of the page to write inside the given block.
|
|
|
|
* data - Buffer containing the data to be writting
|
|
|
|
* spare - Buffer conatining the spare data to be written.
|
|
|
|
*
|
|
|
|
* Returned value.
|
|
|
|
* OK is returned in succes; a negated errno value is returned on failure.
|
2013-11-15 09:30:05 -06:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-17 12:22:09 -06:00
|
|
|
static int nand_writepage(struct nand_raw_s *raw, off_t block,
|
|
|
|
unsigned int page, const void *data,
|
|
|
|
const void *spare)
|
2013-11-15 09:30:05 -06:00
|
|
|
{
|
2013-11-17 12:22:09 -06:00
|
|
|
struct sam_rawnand_s *priv = (struct sam_rawnand_s *)raw;
|
|
|
|
DEBUGASSERT(raw);
|
2013-11-15 09:30:05 -06:00
|
|
|
#warning Missing logic
|
2013-11-17 12:22:09 -06:00
|
|
|
return -ENOSYS;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_nand_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2013-11-17 12:22:09 -06:00
|
|
|
* Create and initialize an raw NAND device instance. This driver
|
|
|
|
* implements the RAW NAND interface: No software ECC or sparing is
|
2013-11-16 13:19:09 -06:00
|
|
|
* performed here. Those necessary NAND features are provided by common,
|
2013-11-17 12:22:09 -06:00
|
|
|
* higher level NAND MTD layers found in drivers/mtd.
|
2013-11-16 13:19:09 -06:00
|
|
|
*
|
2013-11-15 09:30:05 -06:00
|
|
|
* Input parameters:
|
|
|
|
* cs - Chip select number (in the event that multiple NAND devices
|
|
|
|
* are connected on-board).
|
2013-11-16 11:46:35 -06:00
|
|
|
*
|
2013-11-15 09:30:05 -06:00
|
|
|
* Returned value.
|
|
|
|
* On success a non-NULL pointer to an MTD device structure is returned;
|
|
|
|
* NULL is returned on a failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct mtd_dev_s *sam_nand_initialize(int cs)
|
|
|
|
{
|
2013-11-17 08:56:30 -06:00
|
|
|
struct sam_rawnand_s *priv;
|
2013-11-16 18:26:07 -06:00
|
|
|
struct mtd_s *mtd;
|
2013-11-16 11:46:35 -06:00
|
|
|
uintptr_t cmdaddr;
|
|
|
|
uintptr_t addraddr;
|
|
|
|
uintptr_t dataaddr;
|
2013-11-15 09:30:05 -06:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
fvdbg("CS%d\n", cs);
|
|
|
|
|
|
|
|
/* Select the device structure */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS0_NAND
|
|
|
|
if (cs == HSMC_CS0)
|
|
|
|
{
|
2013-11-16 11:46:35 -06:00
|
|
|
/* Refer to the pre-allocated NAND device structure */
|
|
|
|
|
2013-11-15 09:30:05 -06:00
|
|
|
priv = &g_cs0nand;
|
2013-11-16 11:46:35 -06:00
|
|
|
|
|
|
|
/* Set up the NAND addresses. These must be provided in the board.h
|
|
|
|
* header file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmdaddr = BOARD_EBICS0_NAND_CMDADDR;
|
|
|
|
addraddr = BOARD_EBICS0_NAND_ADDRADDR;
|
|
|
|
dataaddr = BOARD_EBICS0_NAND_DATAADDR;
|
2013-11-15 09:30:05 -06:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS1_NAND
|
|
|
|
if (cs == HSMC_CS1)
|
|
|
|
{
|
2013-11-16 11:46:35 -06:00
|
|
|
/* Refer to the pre-allocated NAND device structure */
|
|
|
|
|
2013-11-15 09:30:05 -06:00
|
|
|
priv = &g_cs1nand;
|
2013-11-16 11:46:35 -06:00
|
|
|
|
|
|
|
/* Set up the NAND addresses. These must be provided in the board.h
|
|
|
|
* header file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmdaddr = BOARD_EBICS1_NAND_CMDADDR;
|
|
|
|
addraddr = BOARD_EBICS1_NAND_ADDRADDR;
|
|
|
|
dataaddr = BOARD_EBICS1_NAND_DATAADDR;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS2_NAND
|
|
|
|
if (cs == HSMC_CS2)
|
|
|
|
{
|
2013-11-16 11:46:35 -06:00
|
|
|
/* Refer to the pre-allocated NAND device structure */
|
|
|
|
|
2013-11-15 09:30:05 -06:00
|
|
|
priv = &g_cs2nand;
|
2013-11-16 11:46:35 -06:00
|
|
|
|
|
|
|
/* Set up the NAND addresses. These must be provided in the board.h
|
|
|
|
* header file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmdaddr = BOARD_EBICS2_NAND_CMDADDR;
|
|
|
|
addraddr = BOARD_EBICS2_NAND_ADDRADDR;
|
|
|
|
dataaddr = BOARD_EBICS2_NAND_DATAADDR;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SAMA5_EBICS3_NAND
|
|
|
|
if (cs == HSMC_CS3)
|
|
|
|
{
|
2013-11-16 11:46:35 -06:00
|
|
|
/* Refer to the pre-allocated NAND device structure */
|
|
|
|
|
2013-11-15 09:30:05 -06:00
|
|
|
priv = &g_cs3nand;
|
2013-11-16 11:46:35 -06:00
|
|
|
|
|
|
|
/* Set up the NAND addresses. These must be provided in the board.h
|
|
|
|
* header file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmdaddr = BOARD_EBICS3_NAND_CMDADDR;
|
|
|
|
addraddr = BOARD_EBICS3_NAND_ADDRADDR;
|
|
|
|
dataaddr = BOARD_EBICS3_NAND_DATAADDR;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
fdbg("ERROR: CS%d unsupported or invalid\n", cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the device structure */
|
|
|
|
|
2013-11-17 08:56:30 -06:00
|
|
|
memset(priv, 0, sizeof(struct sam_rawnand_s));
|
|
|
|
priv->raw.cmdaddr = cmdaddr;
|
|
|
|
priv->raw.addraddr = addraddr;
|
|
|
|
priv->raw.dataaddr = dataaddr;
|
2013-11-17 12:22:09 -06:00
|
|
|
priv->raw.eraseblock = nand_eraseblock;
|
|
|
|
priv->raw.readpage = nand_readpage;
|
|
|
|
priv->raw.writepage = nand_writepage;
|
2013-11-17 08:56:30 -06:00
|
|
|
priv->cs = cs;
|
2013-11-15 09:30:05 -06:00
|
|
|
|
|
|
|
/* Initialize the NAND hardware */
|
|
|
|
/* Perform board-specific SMC intialization for this CS */
|
|
|
|
|
|
|
|
ret = board_nandflash_config(cs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-11-16 11:46:35 -06:00
|
|
|
fdbg("ERROR: board_nandflash_config failed for CS%d: %d\n",
|
|
|
|
cs, ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-16 18:26:07 -06:00
|
|
|
/* Probe the NAND part. On success, an MTD interface that wraps
|
|
|
|
* our raw NAND interface is returned.
|
|
|
|
*/
|
2013-11-16 11:46:35 -06:00
|
|
|
|
2013-11-17 08:56:30 -06:00
|
|
|
mtd = nand_initialize(&priv->raw);
|
2013-11-16 18:26:07 -06:00
|
|
|
if (!mtd)
|
2013-11-16 11:46:35 -06:00
|
|
|
{
|
2013-11-17 08:56:30 -06:00
|
|
|
fdbg("ERROR: CS%d nand_initialize failed %d\n", cs);
|
2013-11-15 09:30:05 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#warning Missing logic
|
|
|
|
|
2013-11-16 18:26:07 -06:00
|
|
|
/* Return the MTD wrapper interface as the MTD device */
|
2013-11-15 09:30:05 -06:00
|
|
|
|
2013-11-16 18:26:07 -06:00
|
|
|
return mtd;
|
2013-11-15 09:30:05 -06:00
|
|
|
}
|