From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.84.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796293730; b=Yc7v3gaATjDLerReX2YrmsTNWmX8Vl02o0LRA0vf3Yw+spNw+tdEDYNql1W7/V5IKEEx UXYCAbc8+/wpkNZM5HxixTljoZkBQSqfBftlU8QGbB69mb5iipanD0DbiRQKFJ7v5OTYz PuVEOl9KP6qky/skuEqqGbuPzxhRg/KGD68j0W54AEyQE6i0sUlGld6frxO3T6IoPfAcP YbMs6Iaxee4T+Y3T3zBbEm6/UE18OAvMDBmUCI7AR+G8SuQW15QlOUXnQex8PB2XXQ+0I VcazRhjb1F6wQlFC+zukua8b0rTaqdzjgZ0E+tgfCksi8DPAP0rXp0sUvpAx5vtC+Cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796293730; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:To:Cc: Subject:In-Reply-To:References:User-Agent:Date:Message-ID: MIME-Version:Content-Type:X-Last-TLS-Session-Version; bh=kioKhVec7tYZ0gtqDPT1u/ILMa/u6g1r3lXjk2g+fic=; b=imRGno5nn0/BToZK4FTvm7JP2aNDa6LVRJFylGyqS4hpSswd60MAEYnLnCfw9XIsmFsQ /faXZSh4NsNs4vH+DOMk1NS4TieNVNqcgXRtnhhbM/J5W1QlJ+rJurK0LvJHjSfl2Dldr zRaPiuyIEmMICREPg+2s10+ZKXDhcHH3mpD3lllme6NYUjlcUowBRWjjZutj3YzCwtbnB AkRmQ3HUpiT4yFQ36MrUxbzaebp+EWMYOR1vbbAHM5Jjm/BIQ4ahEEaEPpMuXvpG1h7gW 9PT6Iv0gLxaOE7ZvSyJ0KMZkcy4JZ2Pu5tXP7M+Trt9a/rjlFZi4/o5TKPo+kNHVX9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Qo+uHQY+; arc=none smtp.remote-ip=185.246.84.56 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60648606A322; Thu, 3 Dec 2026 10:28:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 60648606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Qo+uHQY+ Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id AF7BB1A185A; Thu, 3 Dec 2026 10:28:47 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 80953604ED; Thu, 3 Dec 2026 10:28:47 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 906A911EA0757; Thu, 03 Dec 2026 11:28:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796293726; h=from:subject:date:message-id:to:cc:mime-version:content-type: in-reply-to:references; bh=kioKhVec7tYZ0gtqDPT1u/ILMa/u6g1r3lXjk2g+fic=; b=Qo+uHQY+IDIOzeuexcuyxiAPwFIrS+5xsQhYccbht18s45OjqoL4J/a98mGruuuuFrlBBB y4CfXMnsjmULMAD/U94K4fDTC+5ZArQk4dCx633JMtL6QdUHw9Rk4QFWUVRnTHuy4Pt18i +PxD/rhn9P0GCmv40urUJOc0HJevQO5RPKGP02n8h7qN/77dt5zNIz2brorKIkP2i+BLay qo44Eb4qq25M/b9ZV57jvuZCjxTU0tb2krjSo6TX+/21A4yOgDByEsaZR8xzqyEqIS0K84 llFfTXk1o+77Nb+PHKa26tNQ/nzkzwSVGB5okqpG6RD5HUhHil6DN5ogWONwpQ== From: Miquel Raynal To: Fil Dunsky via B4 Relay Cc: Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Fil Dunsky , Philipp Zabel , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 2/3] mtd: rawnand: Add Cortina CS7542 controller support In-Reply-To: <20261202-b4-cs7542-nand-v7-2-81d3ed341c77@gmail.com> (Fil Dunsky via's message of "Wed, 02 Dec 2026 09:50:03 +0300") References: <20261202-b4-cs7542-nand-v7-0-81d3ed341c77@gmail.com> <20261202-b4-cs7542-nand-v7-2-81d3ed341c77@gmail.com> User-Agent: mu4e 1.12.12; emacs 30.2 Date: Thu, 03 Dec 2026 11:28:42 +0100 Message-ID: <87mrqmk691.fsf@bootlin.com> X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain X-Last-TLS-Session-Version: TLSv1.3 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");