/********************************************************************** * slc_ctrl register definitions
**********************************************************************/ #define SLCCTRL_SW_RESET (1 << 2) /* Reset the NAND controller bit */ #define SLCCTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */ #define SLCCTRL_DMA_START (1 << 0) /* Start DMA channel bit */
/********************************************************************** * slc_cfg register definitions
**********************************************************************/ #define SLCCFG_CE_LOW (1 << 5) /* Force CE low bit */ #define SLCCFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */ #define SLCCFG_ECC_EN (1 << 3) /* ECC enable bit */ #define SLCCFG_DMA_BURST (1 << 2) /* DMA burst bit */ #define SLCCFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */ #define SLCCFG_WIDTH (1 << 0) /* External device width, 0=8bit */
/********************************************************************** * slc_stat register definitions
**********************************************************************/ #define SLCSTAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */ #define SLCSTAT_SLC_FIFO (1 << 1) /* SLC FIFO has data bit */ #define SLCSTAT_NAND_READY (1 << 0) /* NAND device is ready bit */
/********************************************************************** * slc_int_stat, slc_ien, slc_isr, and slc_icr register definitions
**********************************************************************/ #define SLCSTAT_INT_TC (1 << 1) /* Transfer count bit */ #define SLCSTAT_INT_RDY_EN (1 << 0) /* Ready interrupt bit */
/********************************************************************** * slc_tac register definitions
**********************************************************************/ /* Computation of clock cycles on basis of controller and device clock rates */ #define SLCTAC_CLOCKS(c, n, s) (min_t(u32, DIV_ROUND_UP(c, n) - 1, 0xF) << s)
/* Clock setting for RDY write sample wait time in 2*n clocks */ #define SLCTAC_WDR(n) (((n) & 0xF) << 28) /* Write pulse width in clock cycles, 1 to 16 clocks */ #define SLCTAC_WWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 24)) /* Write hold time of control and data signals, 1 to 16 clocks */ #define SLCTAC_WHOLD(c, n) (SLCTAC_CLOCKS(c, n, 20)) /* Write setup time of control and data signals, 1 to 16 clocks */ #define SLCTAC_WSETUP(c, n) (SLCTAC_CLOCKS(c, n, 16)) /* Clock setting for RDY read sample wait time in 2*n clocks */ #define SLCTAC_RDR(n) (((n) & 0xF) << 12) /* Read pulse width in clock cycles, 1 to 16 clocks */ #define SLCTAC_RWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 8)) /* Read hold time of control and data signals, 1 to 16 clocks */ #define SLCTAC_RHOLD(c, n) (SLCTAC_CLOCKS(c, n, 4)) /* Read setup time of control and data signals, 1 to 16 clocks */ #define SLCTAC_RSETUP(c, n) (SLCTAC_CLOCKS(c, n, 0))
/* * DMA requires storage space for the DMA local buffer and the hardware ECC * storage area. The DMA local buffer is only used if DMA mapping fails * during runtime.
*/ #define LPC32XX_DMA_DATA_SIZE 4096 #define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4)
/* Number of bytes used for ECC stored in NAND per 256 bytes */ #define LPC32XX_SLC_DEV_ECC_BYTES 3
/* * If the NAND base clock frequency can't be fetched, this frequency will be * used instead as the base. This rate is used to setup the timing registers * used for NAND accesses.
*/ #define LPC32XX_DEF_BUS_RATE 133250000
/* * NAND ECC Layout for small page NAND devices * Note: For large and huge page devices, the default layouts are used
*/ staticint lpc32xx_ooblayout_ecc(struct mtd_info *mtd, int section, struct mtd_oob_region *oobregion)
{ if (section) return -ERANGE;
oobregion->length = 6;
oobregion->offset = 10;
return 0;
}
staticint lpc32xx_ooblayout_free(struct mtd_info *mtd, int section, struct mtd_oob_region *oobregion)
{ if (section > 1) return -ERANGE;
/* * Hardware specific access to control lines
*/ staticvoid lpc32xx_nand_cmd_ctrl(struct nand_chip *chip, int cmd, unsignedint ctrl)
{
uint32_t tmp; struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
/* Does CE state need to be changed? */
tmp = readl(SLC_CFG(host->io_base)); if (ctrl & NAND_NCE)
tmp |= SLCCFG_CE_LOW; else
tmp &= ~SLCCFG_CE_LOW;
writel(tmp, SLC_CFG(host->io_base));
if (cmd != NAND_CMD_NONE) { if (ctrl & NAND_CLE)
writel(cmd, SLC_CMD(host->io_base)); else
writel(cmd, SLC_ADDR(host->io_base));
}
}
/* * Prepares SLC for transfers with H/W ECC enabled
*/ staticvoid lpc32xx_nand_ecc_enable(struct nand_chip *chip, int mode)
{ /* Hardware ECC is enabled automatically in hardware as needed */
}
/* * Calculates the ECC for the data
*/ staticint lpc32xx_nand_ecc_calculate(struct nand_chip *chip, constunsignedchar *buf, unsignedchar *code)
{ /* * ECC is calculated automatically in hardware during syndrome read * and write operations, so it doesn't need to be calculated here.
*/ return 0;
}
/* * Read a single byte from NAND device
*/ static uint8_t lpc32xx_nand_read_byte(struct nand_chip *chip)
{ struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
/* Direct device write with no ECC */ while (len-- > 0)
writel((uint32_t)*buf++, SLC_DATA(host->io_base));
}
/* * Read the OOB data from the device without ECC using FIFO method
*/ staticint lpc32xx_nand_read_oob_syndrome(struct nand_chip *chip, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip);
/* * Write the OOB data to the device without ECC using FIFO method
*/ staticint lpc32xx_nand_write_oob_syndrome(struct nand_chip *chip, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip);
/* * Fills in the ECC fields in the OOB buffer with the hardware generated ECC
*/ staticvoid lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count)
{ int i;
for (i = 0; i < (count * 3); i += 3) {
uint32_t ce = ecc[i / 3];
ce = ~(ce << 2) & 0xFFFFFF;
spare[i + 2] = (uint8_t)(ce & 0xFF);
ce >>= 8;
spare[i + 1] = (uint8_t)(ce & 0xFF);
ce >>= 8;
spare[i] = (uint8_t)(ce & 0xFF);
}
}
/* Transfer size is data area only */
writel(mtd->writesize, SLC_TC(host->io_base));
/* Start transfer in the NAND controller */
writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START,
SLC_CTRL(host->io_base));
for (i = 0; i < chip->ecc.steps; i++) { /* Data */
res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma),
dma_buf + i * chip->ecc.size,
mtd->writesize / chip->ecc.steps, dir); if (res) return res;
/* Always _read_ ECC */ if (i == chip->ecc.steps - 1) break; if (!read) /* ECC availability delayed on write */
udelay(10);
res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma),
&host->ecc_buf[i], 4, DMA_DEV_TO_MEM); if (res) return res;
}
/* * According to NXP, the DMA can be finished here, but the NAND * controller may still have buffered data. After porting to using the * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty) * appears to be always true, according to tests. Keeping the check for * safety reasons for now.
*/ if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) {
dev_warn(mtd->dev.parent, "FIFO not empty!\n");
timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT); while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) &&
time_before(jiffies, timeout))
cpu_relax(); if (!time_before(jiffies, timeout)) {
dev_err(mtd->dev.parent, "FIFO held data too long\n");
status = -EIO;
}
}
/* Read last calculated ECC value */ if (!read)
udelay(10);
host->ecc_buf[chip->ecc.steps - 1] =
readl(SLC_ECC(host->io_base));
if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO ||
readl(SLC_TC(host->io_base))) { /* Something is left in the FIFO, something is wrong */
dev_err(mtd->dev.parent, "DMA FIFO failure\n");
status = -EIO;
}
if (!dma_mapped && read)
memcpy(buf, host->data_buf, mtd->writesize);
return status;
}
/* * Read the data and OOB data from the device, use ECC correction with the * data, disable ECC for the OOB data
*/ staticint lpc32xx_nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip); struct lpc32xx_nand_host *host = nand_get_controller_data(chip); struct mtd_oob_region oobregion = { }; int stat, i, status, error;
uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE];
/* * Read the data and OOB data from the device, no ECC correction with the * data or OOB data
*/ staticint lpc32xx_nand_read_page_raw_syndrome(struct nand_chip *chip,
uint8_t *buf, int oob_required, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip);
/* Raw reads can just use the FIFO interface */
chip->legacy.read_buf(chip, buf, chip->ecc.size * chip->ecc.steps);
chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
return 0;
}
/* * Write the data and OOB data to the device, use ECC with the data, * disable ECC for the OOB data
*/ staticint lpc32xx_nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip); struct lpc32xx_nand_host *host = nand_get_controller_data(chip); struct mtd_oob_region oobregion = { };
uint8_t *pb; int error;
nand_prog_page_begin_op(chip, page, 0, NULL, 0);
/* Write data, calculate ECC on outbound data */
error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0); if (error) return error;
/* * The calculated ECC needs some manual work done to it before * committing it to NAND. Process the calculated ECC and place
* the resultant values directly into the OOB buffer. */
error = mtd_ooblayout_ecc(mtd, 0, &oobregion); if (error) return error;
/* Write ECC data to device */
chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
return nand_prog_page_end_op(chip);
}
/* * Write the data and OOB data to the device, no ECC correction with the * data or OOB data
*/ staticint lpc32xx_nand_write_page_raw_syndrome(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page)
{ struct mtd_info *mtd = nand_to_mtd(chip);
/* Raw writes can just use the FIFO interface */
nand_prog_page_begin_op(chip, page, 0, buf,
chip->ecc.size * chip->ecc.steps);
chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
host->dma_chan = dma_request_chan(mtd->dev.parent, "rx-tx"); if (IS_ERR(host->dma_chan)) { /* fallback to request using platform data */ if (!host->pdata || !host->pdata->dma_filter) {
dev_err(mtd->dev.parent, "no DMA platform data\n"); return -ENOENT;
}
if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST) return 0;
/* OOB and ECC CPU and DMA work areas */
host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE);
/* * Small page FLASH has a unique OOB layout, but large and huge * page FLASH use the standard layout. Small page FLASH uses a * custom BBT marker layout.
*/ if (mtd->writesize <= 512)
mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops);
/* * Use a custom BBT marker setup for small page FLASH that * won't interfere with the ECC layout. Large and huge page * FLASH use the standard layout.
*/ if ((chip->bbt_options & NAND_BBT_USE_FLASH) &&
mtd->writesize <= 512) {
chip->bbt_td = &bbt_smallpage_main_descr;
chip->bbt_md = &bbt_smallpage_mirror_descr;
}
/* Allocate memory for the device structure (and zero it) */
host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); if (!host) return -ENOMEM;
host->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &rc); if (IS_ERR(host->io_base)) return PTR_ERR(host->io_base);
host->io_base_dma = rc->start; if (pdev->dev.of_node)
host->ncfg = lpc32xx_parse_dt(&pdev->dev); if (!host->ncfg) {
dev_err(&pdev->dev, "Missing or bad NAND config from device tree\n"); return -ENOENT;
}
/* Start with WP disabled, if available */
host->wp_gpio = gpiod_get_optional(&pdev->dev, NULL, GPIOD_OUT_LOW);
res = PTR_ERR_OR_ZERO(host->wp_gpio); if (res) { if (res != -EPROBE_DEFER)
dev_err(&pdev->dev, "WP GPIO is not available: %d\n",
res); return res;
}
/* * Allocate a large enough buffer for a single huge page plus * extra space for the spare area and ECC storage area
*/
host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE;
host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len,
GFP_KERNEL); if (host->data_buf == NULL) {
res = -ENOMEM; goto enable_wp;
}
res = lpc32xx_nand_dma_setup(host); if (res) {
res = -EIO; goto enable_wp;
}
/* Find NAND device */
chip->legacy.dummy_controller.ops = &lpc32xx_nand_controller_ops;
res = nand_scan(chip, 1); if (res) goto release_dma;
mtd->name = "nxp_lpc3220_slc";
res = mtd_device_register(mtd, host->ncfg->parts,
host->ncfg->num_parts); if (res) goto cleanup_nand;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.