/** * spinand_upd_cfg() - Update the configuration register * @spinand: the spinand device * @mask: the mask encoding the bits to update in the config reg * @val: the new value to apply * * Update the configuration register. * * Return: 0 on success, a negative error code otherwise.
*/ int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
{ int ret;
u8 cfg;
ret = spinand_get_cfg(spinand, &cfg); if (ret) return ret;
cfg &= ~mask;
cfg |= val;
return spinand_set_cfg(spinand, cfg);
}
/** * spinand_select_target() - Select a specific NAND target/die * @spinand: the spinand device * @target: the target/die to select * * Select a new target/die. If chip only has one die, this function is a NOOP. * * Return: 0 on success, a negative error code otherwise.
*/ int spinand_select_target(struct spinand_device *spinand, unsignedint target)
{ struct nand_device *nand = spinand_to_nand(spinand); int ret;
if (WARN_ON(target >= nand->memorg.ntargets)) return -EINVAL;
for (target = 0; target < nand->memorg.ntargets; target++) {
ret = spinand_select_target(spinand, target); if (ret) return ret;
/* * We use spinand_read_reg_op() instead of spinand_get_cfg() * here to bypass the config cache.
*/
ret = spinand_read_reg_op(spinand, REG_CFG,
&spinand->cfg_cache[target]); if (ret) return ret;
}
if (spinand->eccinfo.get_status) return spinand->eccinfo.get_status(spinand, status);
switch (status & STATUS_ECC_MASK) { case STATUS_ECC_NO_BITFLIPS: return 0;
case STATUS_ECC_HAS_BITFLIPS: /* * We have no way to know exactly how many bitflips have been * fixed, so let's return the maximum possible value so that * wear-leveling layers move the data immediately.
*/ return nanddev_get_ecc_conf(nand)->strength;
/* Nothing to do when finishing a page write */ if (req->type == NAND_PAGE_WRITE) return 0;
/* Finish a page read: check the status, report errors/bitflips */
ret = spinand_check_ecc_status(spinand, engine_conf->status); if (ret == -EBADMSG) {
mtd->ecc_stats.failed++;
} elseif (ret > 0) { unsignedint pages;
/* * Continuous reads don't allow us to get the detail, * so we may exagerate the actual number of corrected bitflips.
*/ if (!req->continuous)
pages = 1; else
pages = req->datalen / nanddev_page_size(nand);
if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT)
column |= req->pos.plane << fls(nanddev_page_size(nand));
while (nbytes) {
ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf); if (ret < 0) return ret;
if (!ret || ret > nbytes) return -EIO;
nbytes -= ret;
column += ret;
buf += ret;
/* * Dirmap accesses are allowed to toggle the CS. * Toggling the CS during a continuous read is forbidden.
*/ if (nbytes && req->continuous) return -EIO;
}
if (req->datalen)
memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
req->datalen);
if (req->ooblen) { if (req->mode == MTD_OPS_AUTO_OOB)
mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
spinand->oobbuf,
req->ooboffs,
req->ooblen); else
memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
req->ooblen);
}
/* * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset * the cache content to 0xFF (depends on vendor implementation), so we * must fill the page cache entirely even if we only want to program * the data portion of the page, otherwise we might corrupt the BBM or * user data previously programmed in OOB area. * * Only reset the data buffer manually, the OOB buffer is prepared by * ECC engines ->prepare_io_req() callback.
*/
nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
memset(spinand->databuf, 0xff, nanddev_page_size(nand));
if (req->datalen)
memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
req->datalen);
if (req->ooblen) { if (req->mode == MTD_OPS_AUTO_OOB)
mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
spinand->oobbuf,
req->ooboffs,
req->ooblen); else
memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
req->ooblen);
}
/** * spinand_wait() - Poll memory device status * @spinand: the spinand device * @initial_delay_us: delay in us before starting to poll * @poll_delay_us: time to sleep between reads in us * @s: the pointer to variable to store the value of REG_STATUS * * This function polls a status register (REG_STATUS) and returns when * the STATUS_READY bit is 0 or when the timeout has expired. * * Return: 0 on success, a negative error code otherwise.
*/ int spinand_wait(struct spinand_device *spinand, unsignedlong initial_delay_us, unsignedlong poll_delay_us, u8 *s)
{ struct spi_mem_op op = SPINAND_GET_FEATURE_1S_1S_1S_OP(REG_STATUS,
spinand->scratchbuf);
u8 status; int ret;
ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0,
initial_delay_us,
poll_delay_us,
SPINAND_WAITRDY_TIMEOUT_MS); if (ret) return ret;
status = *spinand->scratchbuf; if (!(status & STATUS_BUSY)) goto out;
/* * Extra read, just in case the STATUS_READY bit has changed * since our last check
*/
ret = spinand_read_status(spinand, &status); if (ret) return ret;
/** * spinand_read_page() - Read a page * @spinand: the spinand device * @req: the I/O request * * Return: 0 or a positive number of bitflips corrected on success. * A negative error code otherwise.
*/ int spinand_read_page(struct spinand_device *spinand, conststruct nand_page_io_req *req)
{ struct nand_device *nand = spinand_to_nand(spinand);
u8 status; int ret;
ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); if (ret) return ret;
ret = spinand_load_page_op(spinand, req); if (ret) return ret;
ret = spinand_wait(spinand,
SPINAND_READ_INITIAL_DELAY_US,
SPINAND_READ_POLL_DELAY_US,
&status); if (ret < 0) return ret;
spinand_ondie_ecc_save_status(nand, status);
ret = spinand_read_from_cache_op(spinand, req); if (ret) return ret;
/** * spinand_write_page() - Write a page * @spinand: the spinand device * @req: the I/O request * * Return: 0 or a positive number of bitflips corrected on success. * A negative error code otherwise.
*/ int spinand_write_page(struct spinand_device *spinand, conststruct nand_page_io_req *req)
{ struct nand_device *nand = spinand_to_nand(spinand);
u8 status; int ret;
ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); if (ret) return ret;
ret = spinand_write_enable_op(spinand); if (ret) return ret;
ret = spinand_write_to_cache_op(spinand, req); if (ret) return ret;
ret = spinand_program_op(spinand, req); if (ret) return ret;
ret = spinand_wait(spinand,
SPINAND_WRITE_INITIAL_DELAY_US,
SPINAND_WRITE_POLL_DELAY_US,
&status); if (ret) return ret;
ret = spinand_cont_read_enable(spinand, true); if (ret) return ret;
/* * The cache is divided into two halves. While one half of the cache has * the requested data, the other half is loaded with the next chunk of data. * Therefore, the host can read out the data continuously from page to page. * Each data read must be a multiple of 4-bytes and full pages should be read; * otherwise, the data output might get out of sequence from one read command * to another.
*/
nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) {
ret = spinand_select_target(spinand, iter.req.pos.target); if (ret) goto end_cont_read;
ret = nand_ecc_prepare_io_req(nand, &iter.req); if (ret) goto end_cont_read;
ret = spinand_load_page_op(spinand, &iter.req); if (ret) goto end_cont_read;
ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US,
SPINAND_READ_POLL_DELAY_US, NULL); if (ret < 0) goto end_cont_read;
ret = spinand_read_from_cache_op(spinand, &iter.req); if (ret) goto end_cont_read;
ops->retlen += iter.req.datalen;
ret = spinand_read_status(spinand, &status); if (ret) goto end_cont_read;
spinand_ondie_ecc_save_status(nand, status);
ret = nand_ecc_finish_io_req(nand, &iter.req); if (ret < 0) goto end_cont_read;
*max_bitflips = max_t(unsignedint, *max_bitflips, ret);
ret = 0;
}
end_cont_read: /* * Once all the data has been read out, the host can either pull CS# * high and wait for tRST or manually clear the bit in the configuration * register to terminate the continuous read operation. We have no * guarantee the SPI controller drivers will effectively deassert the CS * when we expect them to, so take the register based approach.
*/
spinand_cont_read_enable(spinand, false);
/* OOBs won't be retrieved */ if (ops->ooblen || ops->oobbuf) returnfalse;
nanddev_offs_to_pos(nand, from, &start_pos);
nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos);
/* * Continuous reads never cross LUN boundaries. Some devices don't * support crossing planes boundaries. Some devices don't even support * crossing blocks boundaries. The common case being to read through UBI, * we will very rarely read two consequent blocks or more, so it is safer * and easier (can be improved) to only enable continuous reads when * reading within the same erase block.
*/ if (start_pos.target != end_pos.target ||
start_pos.plane != end_pos.plane ||
start_pos.eraseblock != end_pos.eraseblock) returnfalse;
ret = spinand_read_page(spinand, &req); if (ret == -EOPNOTSUPP) { /* Retry with ECC in case raw access is not supported */
req.mode = MTD_OPS_PLACE_OOB;
spinand_read_page(spinand, &req);
}
if (marker[0] != 0xff || marker[1] != 0xff) returntrue;
ret = spinand_select_target(spinand, pos->target); if (ret) return ret;
ret = spinand_write_page(spinand, &req); if (ret == -EOPNOTSUPP) { /* Retry with ECC in case raw access is not supported */
req.mode = MTD_OPS_PLACE_OOB;
ret = spinand_write_page(spinand, &req);
}
ret = spinand_read_id_op(spinand, 0, 0, id); if (ret) return ret;
ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); if (!ret) return 0;
ret = spinand_read_id_op(spinand, 1, 0, id); if (ret) return ret;
ret = spinand_manufacturer_match(spinand,
SPINAND_READID_METHOD_OPCODE_ADDR); if (!ret) return 0;
ret = spinand_read_id_op(spinand, 0, 1, id); if (ret) return ret;
ret = spinand_manufacturer_match(spinand,
SPINAND_READID_METHOD_OPCODE_DUMMY);
return ret;
}
staticint spinand_manufacturer_init(struct spinand_device *spinand)
{ int ret;
if (spinand->manufacturer->ops->init) {
ret = spinand->manufacturer->ops->init(spinand); if (ret) return ret;
}
if (spinand->configure_chip) {
ret = spinand->configure_chip(spinand); if (ret) return ret;
}
return 0;
}
staticvoid spinand_manufacturer_cleanup(struct spinand_device *spinand)
{ /* Release manufacturer private data */ if (spinand->manufacturer->ops->cleanup) return spinand->manufacturer->ops->cleanup(spinand);
}
/** * spinand_match_and_init() - Try to find a match between a device ID and an * entry in a spinand_info table * @spinand: SPI NAND object * @table: SPI NAND device description table * @table_size: size of the device description table * @rdid_method: read id method to match * * Match between a device ID retrieved through the READ_ID command and an * entry in the SPI NAND description table. If a match is found, the spinand * object will be initialized with information provided by the matching * spinand_info entry. * * Return: 0 on success, a negative error code otherwise.
*/ int spinand_match_and_init(struct spinand_device *spinand, conststruct spinand_info *table, unsignedint table_size, enum spinand_readid_method rdid_method)
{
u8 *id = spinand->id.data; struct nand_device *nand = spinand_to_nand(spinand); unsignedint i;
for (i = 0; i < table_size; i++) { conststruct spinand_info *info = &table[i]; conststruct spi_mem_op *op;
if (rdid_method != info->devid.method) continue;
if (memcmp(id + 1, info->devid.id, info->devid.len)) continue;
ret = spinand_reset_op(spinand); if (ret) return ret;
ret = spinand_id_detect(spinand); if (ret) {
dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
spinand->id.data); return ret;
}
if (nand->memorg.ntargets > 1 && !spinand->select_target) {
dev_err(dev, "SPI NANDs with more than one die must implement ->select_target()\n"); return -EINVAL;
}
ret = spinand_read_cfg(spinand); if (ret) return ret;
ret = spinand_init_quad_enable(spinand); if (ret) return ret;
ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); if (ret) return ret;
ret = spinand_manufacturer_init(spinand); if (ret) {
dev_err(dev, "Failed to initialize the SPI NAND chip (err = %d)\n",
ret); return ret;
}
/* After power up, all blocks are locked, so unlock them here. */ for (i = 0; i < nand->memorg.ntargets; i++) {
ret = spinand_select_target(spinand, i); if (ret) break;
ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); if (ret) break;
}
/* * We need a scratch buffer because the spi_mem interface requires that * buf passed in spi_mem_op->data.buf be DMA-able.
*/
spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); if (!spinand->scratchbuf) return -ENOMEM;
ret = spinand_detect(spinand); if (ret) goto err_free_bufs;
/* * Use kzalloc() instead of devm_kzalloc() here, because some drivers * may use this buffer for DMA access. * Memory allocated by devm_ does not guarantee DMA-safe alignment.
*/
spinand->databuf = kzalloc(nanddev_eraseblock_size(nand),
GFP_KERNEL); if (!spinand->databuf) {
ret = -ENOMEM; goto err_free_bufs;
}
spinand_ecc_enable(spinand, false);
ret = nanddev_ecc_engine_init(nand); if (ret) goto err_cleanup_nanddev;
/* * Continuous read can only be enabled with an on-die ECC engine, so the * ECC initialization must have happened previously.
*/
spinand_cont_read_init(spinand);
if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) {
ret = spinand_set_mtd_otp_ops(spinand); if (ret) goto err_cleanup_ecc_engine;
}
if (nand->ecc.engine) {
ret = mtd_ooblayout_count_freebytes(mtd); if (ret < 0) goto err_cleanup_ecc_engine;
}
mtd->oobavail = ret;
/* Propagate ECC information to mtd_info */
mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength;
mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size;
mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
ret = spinand_create_dirmaps(spinand); if (ret) {
dev_err(dev, "Failed to create direct mappings for read/write operations (err = %d)\n",
ret); goto err_cleanup_ecc_engine;
}
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.