Re: [PATCH v7 2/3] mtd: rawnand: Add Cortina CS7542 controller support
From: Miquel Raynal
Date: Thu Dec 03 2026 - 05:28:53 EST
Hi,
> + if (p.command == (NAND_CMD_RNDOUT | (NAND_CMD_RNDOUTSTART << 8))) {
> + col = p.address;
> + if (h->read_mode != NAND_CMD_PARAM && h->read_mode != NAND_CMD_READ0)
> + return -EINVAL;
> + if (col < h->cache_column)
> + return -EINVAL;
> + col -= h->cache_column;
> + if (col > h->buf_data_len)
> + return -EINVAL;
> + if (requested > h->buf_data_len - col)
> + return -EOPNOTSUPP;
> + h->buf_offs = col;
> + goto cached;
> + }
> + h->buf_offs = 0;
> + h->buf_data_len = 0;
> + h->read_mode = p.command & 0xff;
> + h->cache_column = 0;
> + len = p.data && p.data->type == NAND_OP_DATA_IN_INSTR ? requested : 0;
> + switch (p.command) {
> + case NAND_CMD_READID:
> + len = max(requested, 8U);
> + break;
> + case NAND_CMD_PARAM:
> + /* Keep all standard ONFI copies plus room for extended parameters. */
> + len = sizeof(h->own_buf);
> + break;
> + case NAND_CMD_STATUS:
> + len = 1;
> + break;
> + case NAND_CMD_READ0 | (NAND_CMD_READSTART << 8):
> + col = p.address & 0xffff;
> + if (col >= mtd->writesize + mtd->oobsize)
> + return -EINVAL;
> + h->cache_column = col;
> + len = mtd->writesize + mtd->oobsize - col;
> + break;
> + default:
> + break;
Certainly not!
I don't really get why this driver is implemented like that. I believe
you are trying to use the implementation for the legacy API with the
exec_op() API. You driver does not seem to have its own bitfiels for
defining the commands. Therefore there are likely no reason to have a
big switch case over the command opcodes and you should rather just
fill the registers with whatever commands the core gives you. There are
parsers available to help you in filtering what you controller
supports/does not support.
Furthermore, the core shouuld have a way to know whether a command is
supported before executing it. This is clearly missing. Most of the
checks should be handled early. Have a look at the recent controller
drivers.
> + }
> + if (len > sizeof(h->own_buf) || (requested > len &&
> + (!p.data || p.data->type == NAND_OP_DATA_IN_INSTR)))
> + return -EOPNOTSUPP;
> + if (p.data && p.data->type == NAND_OP_DATA_OUT_INSTR)
> + return cs752x_nand_pio(h, &p, (u8 *)p.data->ctx.data.buf.out,
> + requested, true);
> +
> + ret = cs752x_nand_pio(h, &p, h->own_buf, len,
> + p.command == NAND_CMD_RESET);
> + if (ret)
> + return ret;
> + h->buf_data_len = len;
> +cached:
> + if (!requested)
> + return 0;
> + if (h->buf_offs > h->buf_data_len ||
> + requested > h->buf_data_len - h->buf_offs)
> + return -EOPNOTSUPP;
> + memcpy(p.data->ctx.data.buf.in, h->own_buf + h->buf_offs, requested);
> + h->buf_offs += requested;
> + return 0;
> +}
> +
> +static int cs752x_nand_read_page_hwecc(struct nand_chip *chip, u8 *buf,
> + int oob_required, int page)
> +{
> + struct cs752x_nand_host *h = to_host(chip);
> + struct mtd_info *mtd = nand_to_mtd(chip);
> + int ret;
> +
> + h->use_hwecc = true;
> + ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
> + h->use_hwecc = false;
> + if (ret)
> + return ret;
> + return hamming_correct(h, buf, page);
> +}
> +
> +static int cs752x_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf,
> + int oob_required, int page)
> +{
> + struct cs752x_nand_host *h = to_host(chip);
> + struct mtd_info *mtd = nand_to_mtd(chip);
> + int ret;
> +
> + h->use_hwecc = true;
> + ret = nand_prog_page_op(chip, page, 0, buf, mtd->writesize);
> + h->use_hwecc = false;
> + return ret;
> +}
> +
> +static int cs752x_nand_attach_chip(struct nand_chip *chip)
> +{
> + struct mtd_info *mtd = nand_to_mtd(chip);
> + struct cs752x_nand_host *h = to_host(chip);
> + int ecc_total;
> +
> + /* Byte-bus transfer handling and the staging buffer are driver limits. */
> + if (chip->options & NAND_BUSWIDTH_16 ||
> + mtd->writesize + mtd->oobsize > sizeof(h->own_buf) ||
> + mtd->writesize % sizeof(u32) || mtd->oobsize % sizeof(u32))
> + return -EOPNOTSUPP;
Why those limits? Can't you just resize the buffers?
> + chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
> + chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
> + /* The core overrides raw callbacks for NONE with split program ops. */
> + if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT)
> + return 0;
Shouldn't you make sure the ECC controller is correctly disabled? (same
in the SW scenario).
> + if ((mtd->writesize != SZ_2K && mtd->writesize != SZ_4K) ||
> + (mtd->oobsize != 64 && mtd->oobsize != 128))
> + return -EOPNOTSUPP;
-EINVAL would probably be more appropriate.
> + if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST ||
SW ECC support is missing, it should basically be the same as
NONE. Maybe you need to place the monolithic helpers, though (and test).
> + (chip->ecc.placement != NAND_ECC_PLACEMENT_UNKNOWN &&
> + chip->ecc.placement != NAND_ECC_PLACEMENT_OOB) ||
> + (chip->ecc.algo != NAND_ECC_ALGO_UNKNOWN &&
> + chip->ecc.algo != NAND_ECC_ALGO_HAMMING) ||
> + (chip->ecc.size && chip->ecc.size != 256) ||
ECC size can only be 256? Really? That's pretty small nowadays.
> + (chip->ecc.strength && chip->ecc.strength != 1))
I still don't get your driver, there are references to BCH registers,
but you are oterhwise mostly using Hamming. Does the controller support
both? The binding should reflect all the hardware capabilities, even
though the driver might not support everything.
> + return -EOPNOTSUPP;
> +
> + ecc_total = mtd->writesize / 256 * 3;
> + /* The standard Hamming layout reserves the last 24 or 48 OOB bytes. */
> + if (ecc_total > (mtd->oobsize == 64 ? 24 : 48))
> + return -EINVAL;
> + chip->ecc.placement = NAND_ECC_PLACEMENT_OOB;
> + chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
> + chip->ecc.size = 256;
> + chip->ecc.bytes = 3;
> + chip->ecc.strength = 1;
> +
> + h->ecc_calc = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
> + h->ecc_code = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
> + if (!h->ecc_calc || !h->ecc_code)
> + return -ENOMEM;
> +
> + mtd_set_ooblayout(mtd, nand_get_large_page_hamming_ooblayout());
> +
> + chip->ecc.read_page = cs752x_nand_read_page_hwecc;
> + chip->ecc.write_page = cs752x_nand_write_page_hwecc;
> +
> + dev_info(h->dev, "HW Hamming ECC, %d bytes per %d-byte step, page %u, oob %u\n",
> + chip->ecc.bytes, chip->ecc.size, mtd->writesize, mtd->oobsize);
> + return 0;
> +}
> +
> +static const struct nand_controller_ops cs752x_nand_controller_ops = {
> + .attach_chip = cs752x_nand_attach_chip,
> + .exec_op = cs752x_nand_exec_op,
> +};
> +
> +static int cs752x_nand_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct cs752x_nand_host *h;
> + struct nand_chip *chip;
> + struct mtd_info *mtd;
> + struct device_node *flash;
> + struct reset_control *reset;
> + struct clk *clk;
> + u32 cs;
> + int err;
> +
> + h = devm_kzalloc(dev, sizeof(*h), GFP_KERNEL);
> + if (!h)
> + return -ENOMEM;
> + h->dev = dev;
> +
> + clk = devm_clk_get_enabled(dev, NULL);
> + if (IS_ERR(clk))
> + return dev_err_probe(dev, PTR_ERR(clk), "AXI clock\n");
> + err = devm_clk_rate_exclusive_get(dev, clk);
> + if (err)
> + return dev_err_probe(dev, err, "retain firmware bus timing\n");
> + reset = devm_reset_control_get_optional_exclusive(dev, NULL);
> + if (IS_ERR(reset))
> + return dev_err_probe(dev, PTR_ERR(reset), "flash
> reset\n");
Please add a bit of spacing here and there, it's too packed and barely
readable.
> + /* Preserve the bootloader timing and data: never pulse flash
> reset. */
Quite the opposite actually, one should not depend on the bootloader.
> + err = reset_control_deassert(reset);
> + if (err)
> + return dev_err_probe(dev, err, "release flash reset\n");
> +
> + h->iobase_fl = devm_platform_ioremap_resource(pdev, 0);
> + if (IS_ERR(h->iobase_fl))
> + return PTR_ERR(h->iobase_fl);
> +
> + nand_controller_init(&h->controller);
> + h->controller.ops = &cs752x_nand_controller_ops;
> +
> + chip = &h->chip;
> + mtd = nand_to_mtd(chip);
> + chip->controller = &h->controller;
> + if (of_get_available_child_count(dev->of_node) != 1)
> + return dev_err_probe(dev, -EOPNOTSUPP,
> + "one connected NAND target is supported\n");
> + flash = of_get_next_available_child(dev->of_node, NULL);
> + if (!flash || !of_node_name_eq(flash, "nand")) {
> + of_node_put(flash);
> + return dev_err_probe(dev, -ENODEV, "missing NAND chip node\n");
> + }
> + err = of_property_read_u32(flash, "reg", &cs);
> + if (err || cs > 1) {
> + of_node_put(flash);
> + return dev_err_probe(dev, -EINVAL, "invalid chip select\n");
> + }
> + h->cs = cs;
> + nand_set_flash_node(chip, flash);
> + of_node_put(flash);
> + nand_set_controller_data(chip, h);
> + mtd->dev.parent = dev;
> + if (!mtd->name)
> + mtd->name = "cs752x_nand_flash";
There is a label for that if you *really* need. Since you probably do
not, please drop this.
> + chip->options |= NAND_NO_SUBPAGE_WRITE;
Subpage writes are really useful for nands. Disabling it shall only be
done with controllers not supporting this feature. I doubt your
controller does not support it?
> + dev_info(dev, "flash type 0x%08x\n", fl_readl(h, FLASH_TYPE));
No info log during probe for no reason. Either a _dbg log or drop it.
> + err = nand_scan(chip, 1);
> + if (err)
> + return dev_err_probe(dev, err, "NAND scan failed\n");
> +
> + err = mtd_device_register(mtd, NULL, 0);
> + if (err) {
> + nand_cleanup(chip);
> + return err;
> + }
> +
> + platform_set_drvdata(pdev, h);
> + return 0;
> +}
...
> +static struct platform_driver cs752x_nand_driver = {
> + .driver = {
> + .name = "cs752x_nand",
> + .of_match_table = cs752x_nand_dt_ids,
> + /* Fixed flash has no safe manual detach with active MTD users. */
> + .suppress_bind_attrs = true,
What is the rationale? We usually don't set this flag.
> + },
> + .probe = cs752x_nand_probe,
> + .remove = cs752x_nand_remove,
> +};
> +module_platform_driver(cs752x_nand_driver);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_DESCRIPTION("NAND flash driver for Cortina CS7542");
> +MODULE_ALIAS("platform:cs752x_nand");