All of lore.kernel.org
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Fil Dunsky via B4 Relay <devnull+filipp.dunsky.gmail.com@kernel.org>
Cc: Richard Weinberger <richard@nod.at>,
	 Vignesh Raghavendra <vigneshr@ti.com>,
	 Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	filipp.dunsky@gmail.com,  linux-mtd@lists.infradead.org,
	devicetree@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/3] mtd: rawnand: Add Cortina CS7542 controller support
Date: Thu, 26 Nov 2026 11:57:31 +0100	[thread overview]
Message-ID: <87jylzyi5g.fsf@bootlin.com> (raw)
In-Reply-To: <20261123-b4-cs7542-nand-v2-2-9d9f08608534@gmail.com> (Fil Dunsky via's message of "Mon, 23 Nov 2026 23:27:07 +0300")

Hi Fil,

> +static void reset_ecc_bch_registers(struct cs752x_nand_host *h)

So you have BCH support not only Hamming. The binding is wrong.

> +static void cs752x_setup_page_regs(struct cs752x_nand_host *h, int page,
> +				   bool write, bool oob_required)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u32 cmd, addr1, addr2, cnt;
> +
> +	cnt = NF_CNT_DATA(mtd->writesize - 1);
> +	cnt |= NF_CNT_OOB(oob_required ? mtd->oobsize - 1 : NCNT_EMPTY_OOB);
> +
> +	cnt |= NF_CNT_CMD(NCNT_CMD_2);
> +	cmd = write ? mk_nf_command(NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 0) :
> +		      mk_nf_command(NAND_CMD_READ0, NAND_CMD_READSTART,
> 0);

You should not need to hardcode the opcodes.

> +	cnt |= NF_CNT_ADDR(mk_nf_addr(chip, page, &addr1, &addr2, 0));
> +
> +	fl_writel(h, FLASH_NF_COUNT, cnt);
> +	fl_writel(h, FLASH_NF_COMMAND, cmd);
> +	fl_writel(h, FLASH_NF_ADDRESS_1, addr1);
> +	fl_writel(h, FLASH_NF_ADDRESS_2, addr2);
> +	fl_writel(h, FLASH_NF_ACCESS,
> +		  NF_ACCESS_WIDTH32 |
> +		  NF_ACCESS_EXTADDR(((u64)page << chip->page_shift) >> 27));
> +}
> +
> +static int cs752x_page_op_begin(struct cs752x_nand_host *h)
> +{
> +	int ret = check_flash_ctrl_status(h);
> +
> +	if (ret) {
> +		fl_writel(h, FLASH_NF_ECC_RESET,
> +			  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +		return ret;
> +	}
> +	reset_ecc_bch_registers(h);
> +	return 0;
> +}
> +
> +static int cs752x_nand_words(struct cs752x_nand_host *h, u8 *buf,
> +			     unsigned int len, bool write)
> +{
> +	u32 value;
> +	unsigned int i;
> +	int ret;
> +
> +	if (len % sizeof(u32))
> +		return -EINVAL;
> +	for (i = 0; i < len; i += sizeof(u32)) {
> +		if (write)
> +			fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
> +		fl_writel(h, FLASH_FLASH_ACCESS_START,
> +			  FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
> +		ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
> +						value, !(value & FLASH_START_REGREQ), 1, 10000);
> +		if (ret) {
> +			fl_writel(h, FLASH_NF_ECC_RESET,
> +				  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +			return ret;
> +		}
> +		if (!write)
> +			put_unaligned_le32(fl_readl(h, FLASH_NF_DATA), buf + i);
> +	}
> +	return 0;
> +}
> +
> +static int cs752x_nand_read_page_pio(struct cs752x_nand_host *h, u8 *buf,
> +				     bool oob_required, int page)
> +{
> +	struct mtd_info *mtd = nand_to_mtd(&h->chip);
> +	int ret;
> +
> +	cs752x_setup_page_regs(h, page, false, oob_required);
> +	ret = cs752x_nand_words(h, buf, mtd->writesize, false);
> +	if (ret || !oob_required)
> +		return ret;
> +	return cs752x_nand_words(h, h->chip.oob_poi, mtd->oobsize,
> false);

The spacing is broken everywhere. Please run checkpatch and /kreview
using Chris Mason's prompts.

> +static int cs752x_nand_write_page_pio(struct cs752x_nand_host *h, const u8 *buf,
> +				      bool oob_required, int page,
> +				    int (*fill_oob)(struct cs752x_nand_host *))
> +{
> +	struct mtd_info *mtd = nand_to_mtd(&h->chip);
> +	int ret;
> +
> +	cs752x_setup_page_regs(h, page, true, oob_required);
> +	ret = cs752x_nand_words(h, (u8 *)buf, mtd->writesize, true);
> +	if (ret || !oob_required)
> +		return ret;
> +	if (fill_oob) {
> +		ret = fill_oob(h);
> +		if (ret) {
> +			fl_writel(h, FLASH_NF_ECC_RESET,
> +				  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +			return ret;
> +		}
> +	}
> +	return cs752x_nand_words(h, h->chip.oob_poi, mtd->oobsize, true);
> +}
> +
> +static int fill_hamming_oob_data(struct cs752x_nand_host *h)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u8 *ecc_calc = h->ecc_calc;
> +	int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
> +	int i, j, ret;
> +
> +	ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
> +	if (ret)
> +		return ret;
> +	fl_writel(h, FLASH_NF_ECC_CONTROL,
> +		  fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
> +
> +	for (i = 0, j = 0; eccsteps; eccsteps--, i++, j += eccbytes) {
> +		u32 v = fl_readl(h, FLASH_NF_ECC_GEN0 + 4 * i);
> +
> +		ecc_calc[j]     = v & 0xff;
> +		ecc_calc[j + 1] = (v >> 8) & 0xff;
> +		ecc_calc[j + 2] = (v >> 16) & 0xff;
> +	}
> +	return mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
> +					  chip->ecc.total);
> +}
> +
> +static int hamming_correct(struct cs752x_nand_host *h, u8 *buf, int page)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u8 *ecc_code = h->ecc_code;
> +	int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
> +	int eccsize = chip->ecc.size;
> +	int i, max_bitflips = 0, ret;
> +	u8 *p = buf;
> +
> +	ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
> +	if (ret)
> +		return ret;
> +	fl_writel(h, FLASH_NF_ECC_CONTROL,
> +		  fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
> +
> +	ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, chip->ecc.total);
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
> +		u32 sts, ctl;
> +		int step = i / eccbytes;
> +
> +		fl_writel(h, FLASH_NF_ECC_OOB,
> +			  ecc_code[i] | (ecc_code[i + 1] << 8) | (ecc_code[i + 2] << 16));
> +		ctl = fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_CODESEL(0xf);
> +		fl_writel(h, FLASH_NF_ECC_CONTROL, ctl | ECC_CTL_CODESEL(step));
> +
> +		sts = fl_readl(h, FLASH_NF_ECC_STATUS);
> +		switch (ECC_STS_STATUS(sts)) {
> +		case ECC_NO_ERR:
> +			break;
> +		case ECC_1BIT_DATA_ERR:
> +			if (ECC_STS_ERRBYTE(sts) >= eccsize || ECC_STS_ERRBIT(sts) >= 8) {
> +				mtd->ecc_stats.failed++;
> +				break;
> +			}
> +			p[ECC_STS_ERRBYTE(sts)] ^= BIT(ECC_STS_ERRBIT(sts));
> +			mtd->ecc_stats.corrected++;
> +			max_bitflips = max(max_bitflips, 1);
> +			dev_dbg(h->dev, "page %d step %d: 1-bit data error corrected\n",
> +				page, step);
> +			break;
> +		case ECC_1BIT_ECC_ERR:
> +			mtd->ecc_stats.corrected++;
> +			max_bitflips = max(max_bitflips, 1);
> +			dev_dbg(h->dev, "page %d step %d: 1-bit ecc error\n", page, step);
> +			break;
> +		case ECC_UNCORRECTABLE:
> +			mtd->ecc_stats.failed++;
> +			dev_dbg(h->dev, "page %d step %d: uncorrectable\n", page, step);
> +			break;
> +		}
> +	}
> +	return max_bitflips;
> +}
> +
> +/* The controller sequences complete transactions; do not split CMD/ADDR. */
> +struct cs752x_nand_op {
> +	u32 command;
> +	u64 address;
> +	unsigned int ncmds;
> +	unsigned int naddrs;
> +	const struct nand_op_instr *data;
> +	unsigned int timeout_ms;
> +	unsigned int delay_ns;
> +};
> +
> +static int cs752x_nand_decode(const struct nand_operation *op,
> +			      struct cs752x_nand_op *p)
> +{
> +	unsigned int i, state = 0, nsig = 0, nwait = 0;
> +	char sig[7] = {};
> +
> +	memset(p, 0, sizeof(*p));
> +	p->timeout_ms = 100;
> +	if (op->cs || !op->ninstrs || op->ninstrs > 6)
> +		return -EOPNOTSUPP;
> +	for (i = 0; i < op->ninstrs; i++) {
> +		const struct nand_op_instr *in = &op->instrs[i];
> +		unsigned int j;
> +
> +		p->delay_ns = max(p->delay_ns, in->delay_ns);
> +		if (in->type != NAND_OP_WAITRDY_INSTR) {
> +			if (in->type > NAND_OP_DATA_OUT_INSTR)
> +				return -EOPNOTSUPP;
> +			sig[nsig++] = "CAIO"[in->type];
> +		} else if (++nwait > 1 || !i ||
> +			   (op->instrs[i - 1].type != NAND_OP_CMD_INSTR &&
> +			    op->instrs[i - 1].type != NAND_OP_ADDR_INSTR)) {
> +			return -EOPNOTSUPP;
> +		}
> +		switch (in->type) {
> +		case NAND_OP_CMD_INSTR:
> +			if (p->ncmds == 2 || state == 4)
> +				return -EOPNOTSUPP;
> +			p->command |= (u32)in->ctx.cmd.opcode << (p->ncmds++ * 8);
> +			state = 1;
> +			break;
> +		case NAND_OP_ADDR_INSTR:
> +			if (state != 1 || p->naddrs || !in->ctx.addr.naddrs ||
> +			    in->ctx.addr.naddrs > 5)
> +				return -EOPNOTSUPP;
> +			p->naddrs = in->ctx.addr.naddrs;
> +			for (j = 0; j < p->naddrs; j++)
> +				p->address |= (u64)in->ctx.addr.addrs[j] << (j * 8);
> +			state = 2;
> +			break;
> +		case NAND_OP_DATA_IN_INSTR:
> +		case NAND_OP_DATA_OUT_INSTR:
> +			if (p->data || !in->ctx.data.len ||
> +			    in->ctx.data.len > CS75XX_OWN_BUF_SIZE)
> +				return -EOPNOTSUPP;
> +			p->data = in;
> +			state = in->type == NAND_OP_DATA_IN_INSTR ? 4 : 3;
> +			break;
> +		case NAND_OP_WAITRDY_INSTR:
> +			if (!in->ctx.waitrdy.timeout_ms)
> +				return -EOPNOTSUPP;
> +			p->timeout_ms = in->ctx.waitrdy.timeout_ms;
> +			break;
> +		default:
> +			return -EOPNOTSUPP;
> +		}
> +	}
> +
> +	if (!p->ncmds)
> +		return p->data && p->data->type == NAND_OP_DATA_IN_INSTR &&
> +		       op->ninstrs == 1 ? 0 : -EOPNOTSUPP;
> +	/* Validate the whole sequence, not just the available register fields. */
> +	if (p->command == NAND_CMD_RESET) {
> +		if (strcmp(sig, "C"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == NAND_CMD_STATUS) {
> +		if (strcmp(sig, "C") && strcmp(sig, "CI"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == NAND_CMD_READID || p->command == NAND_CMD_PARAM) {
> +		if (strcmp(sig, "CA") && strcmp(sig, "CAI"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == (NAND_CMD_SEQIN | (NAND_CMD_PAGEPROG << 8))) {
> +		if (strcmp(sig, "CAOC"))
> +			return -EOPNOTSUPP;
> +	} else if (strcmp(sig, "CAC") && strcmp(sig, "CACI")) {
> +		return -EOPNOTSUPP;
> +	}
> +	switch (p->command) {
> +	case NAND_CMD_RESET:
> +		return !p->naddrs && !p->data ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_STATUS:
> +		return !p->naddrs && (!p->data ||
> +		       (p->data->type == NAND_OP_DATA_IN_INSTR &&
> +			p->data->ctx.data.len == 1)) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_READID:
> +	case NAND_CMD_PARAM:
> +		return p->naddrs == 1 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_READ0 | (NAND_CMD_READSTART << 8):
> +		return p->naddrs >= 4 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_RNDOUT | (NAND_CMD_RNDOUTSTART << 8):
> +		return p->naddrs == 2 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_SEQIN | (NAND_CMD_PAGEPROG << 8):
> +		return p->naddrs >= 4 && p->data &&
> +		       p->data->type == NAND_OP_DATA_OUT_INSTR &&
> +		       !(p->data->ctx.data.len % sizeof(u32)) &&
> +		       !(p->address & 3) && state == 1 ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_ERASE1 | (NAND_CMD_ERASE2 << 8):
> +		return p->naddrs >= 2 && p->naddrs <= 3 && !p->data ? 0 : -EOPNOTSUPP;
> +	default:
> +		return -EOPNOTSUPP;
> +	}

Is the controller only capable to send those opcodes because it is super
smart? Or is it just sanity checks?

> +}
> +
> +static int cs752x_nand_wait_ready(struct cs752x_nand_host *h,
> +				  unsigned int timeout_ms)
> +{
> +	u32 value;
> +
> +	/* nflash_rbIn_d is the sampled NAND R/B input, not the FSM state. */
> +	return readl_poll_timeout(h->iobase_fl + FLASH_STATUS, value,
> +				 value & BIT(26), 1, (u64)timeout_ms * 1000);
> +}
> +
> +static int cs752x_nand_pio(struct cs752x_nand_host *h,
> +			   const struct cs752x_nand_op *p, u8 *buf,
> +			  unsigned int len, bool write)
> +{
> +	u32 value;
> +	unsigned int i, step = write && len ? sizeof(u32) : 1;
> +	int ret;
> +
> +	ret = check_flash_ctrl_status(h);
> +	if (ret)
> +		return ret;
> +	reset_ecc_bch_registers(h);
> +	fl_writel(h, FLASH_NF_COUNT,
> +		  NF_CNT_OOB(NCNT_EMPTY_OOB) |
> +		  NF_CNT_DATA(len ? len - 1 : NCNT_EMPTY_DATA) |
> +		  NF_CNT_ADDR(p->naddrs ? p->naddrs - 1 : NCNT_EMPTY_ADDR) |
> +		  NF_CNT_CMD(p->ncmds - 1));
> +	fl_writel(h, FLASH_NF_COMMAND, p->command);
> +	fl_writel(h, FLASH_NF_ADDRESS_1, lower_32_bits(p->address));
> +	fl_writel(h, FLASH_NF_ADDRESS_2, upper_32_bits(p->address));
> +	fl_writel(h, FLASH_NF_ACCESS, write && len ? NF_ACCESS_WIDTH32 : NF_ACCESS_WIDTH8);
> +
> +	for (i = 0; i < max(len, 1U); i += step) {
> +		if (write && len)
> +			fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
> +		fl_writel(h, FLASH_FLASH_ACCESS_START,
> +			  FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
> +		/* Byte PIO completes in microseconds, below scheduler granularity. */
> +		ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
> +						value, !(value & FLASH_START_REGREQ), 1,
> +					(u64)p->timeout_ms * 1000);
> +		if (ret)
> +			goto out;
> +		if (!write && len)
> +			buf[i] = fl_readl(h, FLASH_NF_DATA) >> ((i % 4) * 8);
> +	}
> +	ret = p->command == NAND_CMD_STATUS ? 0 :
> +	      cs752x_nand_wait_ready(h, p->timeout_ms);
> +	if (p->delay_ns)
> +		ndelay(p->delay_ns);
> +out:
> +	fl_writel(h, FLASH_NF_ECC_RESET,
> +		  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +	return ret;
> +}
> +
> +static int cs752x_nand_exec_op(struct nand_chip *chip,
> +			       const struct nand_operation *op, bool check_only)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	struct cs752x_nand_op p;
> +	unsigned int len = 0, requested, col;
> +	int ret;
> +
> +	ret = cs752x_nand_decode(op, &p);
> +	if (ret || check_only)
> +		return ret;
> +	requested = p.data ? p.data->ctx.data.len : 0;
> +	if (!p.ncmds) {
> +		if (h->read_mode != NAND_CMD_STATUS)
> +			goto cached;
> +		if (requested != 1)
> +			return -EOPNOTSUPP;
> +		p.command = NAND_CMD_STATUS;
> +		p.ncmds = 1;
> +	}
> +	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 || col - h->cache_column > h->buf_data_len)
> +			return -EINVAL;
> +		h->buf_offs = col - h->cache_column;
> +		goto cached;
> +	}
> +	h->buf_offs = 0;
> +	h->buf_data_len = 0;
> +	h->read_mode = p.command & 0xff;
> +	h->cache_column = 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;
> +	}
> +	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_program_status(struct nand_chip *chip)
> +{
> +	u8 status;
> +	int ret;
> +
> +	ret = cs752x_nand_wait_ready(to_host(chip), 1000);
> +	if (ret)
> +		return ret;
> +	ret = nand_status_op(chip, &status);
> +	if (ret)
> +		return ret;
> +	return status & NAND_STATUS_FAIL ? -EIO : 0;
> +}
> +
> +static int cs752x_nand_read_page_raw(struct nand_chip *chip, u8 *buf,
> +				     int oob_required, int page)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_read_page_pio(h, buf, oob_required, page);
> +}
> +
> +static int cs752x_nand_write_page_raw(struct nand_chip *chip, const u8 *buf,
> +				      int oob_required, int page)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	ret = cs752x_nand_write_page_pio(h, buf, oob_required, page, NULL);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_program_status(chip);
> +}
> +
> +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);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	configure_hwecc_reg(h, false);
> +
> +	ret = cs752x_nand_read_page_pio(h, buf, true, page);
> +	if (ret)
> +		return ret;
> +
> +	ret = hamming_correct(h, buf, page);
> +	return ret;
> +}
> +
> +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);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	configure_hwecc_reg(h, true);
> +
> +	ret = cs752x_nand_write_page_pio(h, buf, true, page, fill_hamming_oob_data);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_program_status(chip);
> +}
> +
> +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;
> +
> +	if (chip->options & NAND_BUSWIDTH_16 ||
> +	    (mtd->writesize != SZ_2K && mtd->writesize != SZ_4K) ||
> +	    (mtd->oobsize != 64 && mtd->oobsize != 128))

Is your controller really so constrained?

> +		return -EOPNOTSUPP;
> +	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST ||
> +	    (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) ||
> +	    (chip->ecc.strength && chip->ecc.strength != 1))
> +		return -EOPNOTSUPP;

Why refusing to use SW operations?

> +
> +	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;
> +	chip->ecc.read_page_raw	 = cs752x_nand_read_page_raw;
> +	chip->ecc.write_page_raw = cs752x_nand_write_page_raw;
> +
> +	dev_info(h->dev, "HW ECC %s, %d bytes per %d-byte step, page %u, oob %u\n",
> +		 chip->ecc.algo == NAND_ECC_ALGO_BCH ? "BCH" : "Hamming",
> +		 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,

Maybe a supports_op() where you make your checks would be relevant to
remove the churn from exec_op()?

> +};
> +

Thanks,
Miquèl

WARNING: multiple messages have this Message-ID (diff)
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Fil Dunsky via B4 Relay <devnull+filipp.dunsky.gmail.com@kernel.org>
Cc: Richard Weinberger <richard@nod.at>,
	 Vignesh Raghavendra <vigneshr@ti.com>,
	 Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	filipp.dunsky@gmail.com,  linux-mtd@lists.infradead.org,
	devicetree@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/3] mtd: rawnand: Add Cortina CS7542 controller support
Date: Thu, 26 Nov 2026 11:57:31 +0100	[thread overview]
Message-ID: <87jylzyi5g.fsf@bootlin.com> (raw)
In-Reply-To: <20261123-b4-cs7542-nand-v2-2-9d9f08608534@gmail.com> (Fil Dunsky via's message of "Mon, 23 Nov 2026 23:27:07 +0300")

Hi Fil,

> +static void reset_ecc_bch_registers(struct cs752x_nand_host *h)

So you have BCH support not only Hamming. The binding is wrong.

> +static void cs752x_setup_page_regs(struct cs752x_nand_host *h, int page,
> +				   bool write, bool oob_required)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u32 cmd, addr1, addr2, cnt;
> +
> +	cnt = NF_CNT_DATA(mtd->writesize - 1);
> +	cnt |= NF_CNT_OOB(oob_required ? mtd->oobsize - 1 : NCNT_EMPTY_OOB);
> +
> +	cnt |= NF_CNT_CMD(NCNT_CMD_2);
> +	cmd = write ? mk_nf_command(NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 0) :
> +		      mk_nf_command(NAND_CMD_READ0, NAND_CMD_READSTART,
> 0);

You should not need to hardcode the opcodes.

> +	cnt |= NF_CNT_ADDR(mk_nf_addr(chip, page, &addr1, &addr2, 0));
> +
> +	fl_writel(h, FLASH_NF_COUNT, cnt);
> +	fl_writel(h, FLASH_NF_COMMAND, cmd);
> +	fl_writel(h, FLASH_NF_ADDRESS_1, addr1);
> +	fl_writel(h, FLASH_NF_ADDRESS_2, addr2);
> +	fl_writel(h, FLASH_NF_ACCESS,
> +		  NF_ACCESS_WIDTH32 |
> +		  NF_ACCESS_EXTADDR(((u64)page << chip->page_shift) >> 27));
> +}
> +
> +static int cs752x_page_op_begin(struct cs752x_nand_host *h)
> +{
> +	int ret = check_flash_ctrl_status(h);
> +
> +	if (ret) {
> +		fl_writel(h, FLASH_NF_ECC_RESET,
> +			  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +		return ret;
> +	}
> +	reset_ecc_bch_registers(h);
> +	return 0;
> +}
> +
> +static int cs752x_nand_words(struct cs752x_nand_host *h, u8 *buf,
> +			     unsigned int len, bool write)
> +{
> +	u32 value;
> +	unsigned int i;
> +	int ret;
> +
> +	if (len % sizeof(u32))
> +		return -EINVAL;
> +	for (i = 0; i < len; i += sizeof(u32)) {
> +		if (write)
> +			fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
> +		fl_writel(h, FLASH_FLASH_ACCESS_START,
> +			  FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
> +		ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
> +						value, !(value & FLASH_START_REGREQ), 1, 10000);
> +		if (ret) {
> +			fl_writel(h, FLASH_NF_ECC_RESET,
> +				  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +			return ret;
> +		}
> +		if (!write)
> +			put_unaligned_le32(fl_readl(h, FLASH_NF_DATA), buf + i);
> +	}
> +	return 0;
> +}
> +
> +static int cs752x_nand_read_page_pio(struct cs752x_nand_host *h, u8 *buf,
> +				     bool oob_required, int page)
> +{
> +	struct mtd_info *mtd = nand_to_mtd(&h->chip);
> +	int ret;
> +
> +	cs752x_setup_page_regs(h, page, false, oob_required);
> +	ret = cs752x_nand_words(h, buf, mtd->writesize, false);
> +	if (ret || !oob_required)
> +		return ret;
> +	return cs752x_nand_words(h, h->chip.oob_poi, mtd->oobsize,
> false);

The spacing is broken everywhere. Please run checkpatch and /kreview
using Chris Mason's prompts.

> +static int cs752x_nand_write_page_pio(struct cs752x_nand_host *h, const u8 *buf,
> +				      bool oob_required, int page,
> +				    int (*fill_oob)(struct cs752x_nand_host *))
> +{
> +	struct mtd_info *mtd = nand_to_mtd(&h->chip);
> +	int ret;
> +
> +	cs752x_setup_page_regs(h, page, true, oob_required);
> +	ret = cs752x_nand_words(h, (u8 *)buf, mtd->writesize, true);
> +	if (ret || !oob_required)
> +		return ret;
> +	if (fill_oob) {
> +		ret = fill_oob(h);
> +		if (ret) {
> +			fl_writel(h, FLASH_NF_ECC_RESET,
> +				  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +			return ret;
> +		}
> +	}
> +	return cs752x_nand_words(h, h->chip.oob_poi, mtd->oobsize, true);
> +}
> +
> +static int fill_hamming_oob_data(struct cs752x_nand_host *h)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u8 *ecc_calc = h->ecc_calc;
> +	int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
> +	int i, j, ret;
> +
> +	ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
> +	if (ret)
> +		return ret;
> +	fl_writel(h, FLASH_NF_ECC_CONTROL,
> +		  fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
> +
> +	for (i = 0, j = 0; eccsteps; eccsteps--, i++, j += eccbytes) {
> +		u32 v = fl_readl(h, FLASH_NF_ECC_GEN0 + 4 * i);
> +
> +		ecc_calc[j]     = v & 0xff;
> +		ecc_calc[j + 1] = (v >> 8) & 0xff;
> +		ecc_calc[j + 2] = (v >> 16) & 0xff;
> +	}
> +	return mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
> +					  chip->ecc.total);
> +}
> +
> +static int hamming_correct(struct cs752x_nand_host *h, u8 *buf, int page)
> +{
> +	struct nand_chip *chip = &h->chip;
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	u8 *ecc_code = h->ecc_code;
> +	int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
> +	int eccsize = chip->ecc.size;
> +	int i, max_bitflips = 0, ret;
> +	u8 *p = buf;
> +
> +	ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
> +	if (ret)
> +		return ret;
> +	fl_writel(h, FLASH_NF_ECC_CONTROL,
> +		  fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
> +
> +	ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, chip->ecc.total);
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
> +		u32 sts, ctl;
> +		int step = i / eccbytes;
> +
> +		fl_writel(h, FLASH_NF_ECC_OOB,
> +			  ecc_code[i] | (ecc_code[i + 1] << 8) | (ecc_code[i + 2] << 16));
> +		ctl = fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_CODESEL(0xf);
> +		fl_writel(h, FLASH_NF_ECC_CONTROL, ctl | ECC_CTL_CODESEL(step));
> +
> +		sts = fl_readl(h, FLASH_NF_ECC_STATUS);
> +		switch (ECC_STS_STATUS(sts)) {
> +		case ECC_NO_ERR:
> +			break;
> +		case ECC_1BIT_DATA_ERR:
> +			if (ECC_STS_ERRBYTE(sts) >= eccsize || ECC_STS_ERRBIT(sts) >= 8) {
> +				mtd->ecc_stats.failed++;
> +				break;
> +			}
> +			p[ECC_STS_ERRBYTE(sts)] ^= BIT(ECC_STS_ERRBIT(sts));
> +			mtd->ecc_stats.corrected++;
> +			max_bitflips = max(max_bitflips, 1);
> +			dev_dbg(h->dev, "page %d step %d: 1-bit data error corrected\n",
> +				page, step);
> +			break;
> +		case ECC_1BIT_ECC_ERR:
> +			mtd->ecc_stats.corrected++;
> +			max_bitflips = max(max_bitflips, 1);
> +			dev_dbg(h->dev, "page %d step %d: 1-bit ecc error\n", page, step);
> +			break;
> +		case ECC_UNCORRECTABLE:
> +			mtd->ecc_stats.failed++;
> +			dev_dbg(h->dev, "page %d step %d: uncorrectable\n", page, step);
> +			break;
> +		}
> +	}
> +	return max_bitflips;
> +}
> +
> +/* The controller sequences complete transactions; do not split CMD/ADDR. */
> +struct cs752x_nand_op {
> +	u32 command;
> +	u64 address;
> +	unsigned int ncmds;
> +	unsigned int naddrs;
> +	const struct nand_op_instr *data;
> +	unsigned int timeout_ms;
> +	unsigned int delay_ns;
> +};
> +
> +static int cs752x_nand_decode(const struct nand_operation *op,
> +			      struct cs752x_nand_op *p)
> +{
> +	unsigned int i, state = 0, nsig = 0, nwait = 0;
> +	char sig[7] = {};
> +
> +	memset(p, 0, sizeof(*p));
> +	p->timeout_ms = 100;
> +	if (op->cs || !op->ninstrs || op->ninstrs > 6)
> +		return -EOPNOTSUPP;
> +	for (i = 0; i < op->ninstrs; i++) {
> +		const struct nand_op_instr *in = &op->instrs[i];
> +		unsigned int j;
> +
> +		p->delay_ns = max(p->delay_ns, in->delay_ns);
> +		if (in->type != NAND_OP_WAITRDY_INSTR) {
> +			if (in->type > NAND_OP_DATA_OUT_INSTR)
> +				return -EOPNOTSUPP;
> +			sig[nsig++] = "CAIO"[in->type];
> +		} else if (++nwait > 1 || !i ||
> +			   (op->instrs[i - 1].type != NAND_OP_CMD_INSTR &&
> +			    op->instrs[i - 1].type != NAND_OP_ADDR_INSTR)) {
> +			return -EOPNOTSUPP;
> +		}
> +		switch (in->type) {
> +		case NAND_OP_CMD_INSTR:
> +			if (p->ncmds == 2 || state == 4)
> +				return -EOPNOTSUPP;
> +			p->command |= (u32)in->ctx.cmd.opcode << (p->ncmds++ * 8);
> +			state = 1;
> +			break;
> +		case NAND_OP_ADDR_INSTR:
> +			if (state != 1 || p->naddrs || !in->ctx.addr.naddrs ||
> +			    in->ctx.addr.naddrs > 5)
> +				return -EOPNOTSUPP;
> +			p->naddrs = in->ctx.addr.naddrs;
> +			for (j = 0; j < p->naddrs; j++)
> +				p->address |= (u64)in->ctx.addr.addrs[j] << (j * 8);
> +			state = 2;
> +			break;
> +		case NAND_OP_DATA_IN_INSTR:
> +		case NAND_OP_DATA_OUT_INSTR:
> +			if (p->data || !in->ctx.data.len ||
> +			    in->ctx.data.len > CS75XX_OWN_BUF_SIZE)
> +				return -EOPNOTSUPP;
> +			p->data = in;
> +			state = in->type == NAND_OP_DATA_IN_INSTR ? 4 : 3;
> +			break;
> +		case NAND_OP_WAITRDY_INSTR:
> +			if (!in->ctx.waitrdy.timeout_ms)
> +				return -EOPNOTSUPP;
> +			p->timeout_ms = in->ctx.waitrdy.timeout_ms;
> +			break;
> +		default:
> +			return -EOPNOTSUPP;
> +		}
> +	}
> +
> +	if (!p->ncmds)
> +		return p->data && p->data->type == NAND_OP_DATA_IN_INSTR &&
> +		       op->ninstrs == 1 ? 0 : -EOPNOTSUPP;
> +	/* Validate the whole sequence, not just the available register fields. */
> +	if (p->command == NAND_CMD_RESET) {
> +		if (strcmp(sig, "C"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == NAND_CMD_STATUS) {
> +		if (strcmp(sig, "C") && strcmp(sig, "CI"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == NAND_CMD_READID || p->command == NAND_CMD_PARAM) {
> +		if (strcmp(sig, "CA") && strcmp(sig, "CAI"))
> +			return -EOPNOTSUPP;
> +	} else if (p->command == (NAND_CMD_SEQIN | (NAND_CMD_PAGEPROG << 8))) {
> +		if (strcmp(sig, "CAOC"))
> +			return -EOPNOTSUPP;
> +	} else if (strcmp(sig, "CAC") && strcmp(sig, "CACI")) {
> +		return -EOPNOTSUPP;
> +	}
> +	switch (p->command) {
> +	case NAND_CMD_RESET:
> +		return !p->naddrs && !p->data ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_STATUS:
> +		return !p->naddrs && (!p->data ||
> +		       (p->data->type == NAND_OP_DATA_IN_INSTR &&
> +			p->data->ctx.data.len == 1)) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_READID:
> +	case NAND_CMD_PARAM:
> +		return p->naddrs == 1 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_READ0 | (NAND_CMD_READSTART << 8):
> +		return p->naddrs >= 4 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_RNDOUT | (NAND_CMD_RNDOUTSTART << 8):
> +		return p->naddrs == 2 && (!p->data ||
> +		       p->data->type == NAND_OP_DATA_IN_INSTR) ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_SEQIN | (NAND_CMD_PAGEPROG << 8):
> +		return p->naddrs >= 4 && p->data &&
> +		       p->data->type == NAND_OP_DATA_OUT_INSTR &&
> +		       !(p->data->ctx.data.len % sizeof(u32)) &&
> +		       !(p->address & 3) && state == 1 ? 0 : -EOPNOTSUPP;
> +	case NAND_CMD_ERASE1 | (NAND_CMD_ERASE2 << 8):
> +		return p->naddrs >= 2 && p->naddrs <= 3 && !p->data ? 0 : -EOPNOTSUPP;
> +	default:
> +		return -EOPNOTSUPP;
> +	}

Is the controller only capable to send those opcodes because it is super
smart? Or is it just sanity checks?

> +}
> +
> +static int cs752x_nand_wait_ready(struct cs752x_nand_host *h,
> +				  unsigned int timeout_ms)
> +{
> +	u32 value;
> +
> +	/* nflash_rbIn_d is the sampled NAND R/B input, not the FSM state. */
> +	return readl_poll_timeout(h->iobase_fl + FLASH_STATUS, value,
> +				 value & BIT(26), 1, (u64)timeout_ms * 1000);
> +}
> +
> +static int cs752x_nand_pio(struct cs752x_nand_host *h,
> +			   const struct cs752x_nand_op *p, u8 *buf,
> +			  unsigned int len, bool write)
> +{
> +	u32 value;
> +	unsigned int i, step = write && len ? sizeof(u32) : 1;
> +	int ret;
> +
> +	ret = check_flash_ctrl_status(h);
> +	if (ret)
> +		return ret;
> +	reset_ecc_bch_registers(h);
> +	fl_writel(h, FLASH_NF_COUNT,
> +		  NF_CNT_OOB(NCNT_EMPTY_OOB) |
> +		  NF_CNT_DATA(len ? len - 1 : NCNT_EMPTY_DATA) |
> +		  NF_CNT_ADDR(p->naddrs ? p->naddrs - 1 : NCNT_EMPTY_ADDR) |
> +		  NF_CNT_CMD(p->ncmds - 1));
> +	fl_writel(h, FLASH_NF_COMMAND, p->command);
> +	fl_writel(h, FLASH_NF_ADDRESS_1, lower_32_bits(p->address));
> +	fl_writel(h, FLASH_NF_ADDRESS_2, upper_32_bits(p->address));
> +	fl_writel(h, FLASH_NF_ACCESS, write && len ? NF_ACCESS_WIDTH32 : NF_ACCESS_WIDTH8);
> +
> +	for (i = 0; i < max(len, 1U); i += step) {
> +		if (write && len)
> +			fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
> +		fl_writel(h, FLASH_FLASH_ACCESS_START,
> +			  FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
> +		/* Byte PIO completes in microseconds, below scheduler granularity. */
> +		ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
> +						value, !(value & FLASH_START_REGREQ), 1,
> +					(u64)p->timeout_ms * 1000);
> +		if (ret)
> +			goto out;
> +		if (!write && len)
> +			buf[i] = fl_readl(h, FLASH_NF_DATA) >> ((i % 4) * 8);
> +	}
> +	ret = p->command == NAND_CMD_STATUS ? 0 :
> +	      cs752x_nand_wait_ready(h, p->timeout_ms);
> +	if (p->delay_ns)
> +		ndelay(p->delay_ns);
> +out:
> +	fl_writel(h, FLASH_NF_ECC_RESET,
> +		  ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
> +	return ret;
> +}
> +
> +static int cs752x_nand_exec_op(struct nand_chip *chip,
> +			       const struct nand_operation *op, bool check_only)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	struct mtd_info *mtd = nand_to_mtd(chip);
> +	struct cs752x_nand_op p;
> +	unsigned int len = 0, requested, col;
> +	int ret;
> +
> +	ret = cs752x_nand_decode(op, &p);
> +	if (ret || check_only)
> +		return ret;
> +	requested = p.data ? p.data->ctx.data.len : 0;
> +	if (!p.ncmds) {
> +		if (h->read_mode != NAND_CMD_STATUS)
> +			goto cached;
> +		if (requested != 1)
> +			return -EOPNOTSUPP;
> +		p.command = NAND_CMD_STATUS;
> +		p.ncmds = 1;
> +	}
> +	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 || col - h->cache_column > h->buf_data_len)
> +			return -EINVAL;
> +		h->buf_offs = col - h->cache_column;
> +		goto cached;
> +	}
> +	h->buf_offs = 0;
> +	h->buf_data_len = 0;
> +	h->read_mode = p.command & 0xff;
> +	h->cache_column = 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;
> +	}
> +	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_program_status(struct nand_chip *chip)
> +{
> +	u8 status;
> +	int ret;
> +
> +	ret = cs752x_nand_wait_ready(to_host(chip), 1000);
> +	if (ret)
> +		return ret;
> +	ret = nand_status_op(chip, &status);
> +	if (ret)
> +		return ret;
> +	return status & NAND_STATUS_FAIL ? -EIO : 0;
> +}
> +
> +static int cs752x_nand_read_page_raw(struct nand_chip *chip, u8 *buf,
> +				     int oob_required, int page)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_read_page_pio(h, buf, oob_required, page);
> +}
> +
> +static int cs752x_nand_write_page_raw(struct nand_chip *chip, const u8 *buf,
> +				      int oob_required, int page)
> +{
> +	struct cs752x_nand_host *h = to_host(chip);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	ret = cs752x_nand_write_page_pio(h, buf, oob_required, page, NULL);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_program_status(chip);
> +}
> +
> +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);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	configure_hwecc_reg(h, false);
> +
> +	ret = cs752x_nand_read_page_pio(h, buf, true, page);
> +	if (ret)
> +		return ret;
> +
> +	ret = hamming_correct(h, buf, page);
> +	return ret;
> +}
> +
> +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);
> +	int ret;
> +
> +	ret = cs752x_page_op_begin(h);
> +	if (ret)
> +		return ret;
> +	configure_hwecc_reg(h, true);
> +
> +	ret = cs752x_nand_write_page_pio(h, buf, true, page, fill_hamming_oob_data);
> +	if (ret)
> +		return ret;
> +	return cs752x_nand_program_status(chip);
> +}
> +
> +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;
> +
> +	if (chip->options & NAND_BUSWIDTH_16 ||
> +	    (mtd->writesize != SZ_2K && mtd->writesize != SZ_4K) ||
> +	    (mtd->oobsize != 64 && mtd->oobsize != 128))

Is your controller really so constrained?

> +		return -EOPNOTSUPP;
> +	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST ||
> +	    (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) ||
> +	    (chip->ecc.strength && chip->ecc.strength != 1))
> +		return -EOPNOTSUPP;

Why refusing to use SW operations?

> +
> +	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;
> +	chip->ecc.read_page_raw	 = cs752x_nand_read_page_raw;
> +	chip->ecc.write_page_raw = cs752x_nand_write_page_raw;
> +
> +	dev_info(h->dev, "HW ECC %s, %d bytes per %d-byte step, page %u, oob %u\n",
> +		 chip->ecc.algo == NAND_ECC_ALGO_BCH ? "BCH" : "Hamming",
> +		 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,

Maybe a supports_op() where you make your checks would be relevant to
remove the churn from exec_op()?

> +};
> +

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  parent reply	other threads:[~2026-11-26 10:57 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:27 [PATCH v2 0/3] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
2026-11-23 20:27 ` Fil Dunsky
2026-11-23 20:27 ` Fil Dunsky via B4 Relay
2026-11-23 20:27 ` [PATCH v2 1/3] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
2026-11-23 20:27   ` Fil Dunsky
2026-11-23 20:27   ` Fil Dunsky via B4 Relay
2026-11-26 10:49   ` Miquel Raynal
2026-11-26 10:49     ` Miquel Raynal
2026-11-23 20:27 ` [PATCH v2 2/3] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
2026-11-23 20:27   ` Fil Dunsky
2026-11-23 20:27   ` Fil Dunsky via B4 Relay
2026-11-23 20:39   ` sashiko-bot
2026-11-23 20:39     ` sashiko-bot
2026-11-23 21:26     ` Fil Dunsky
2026-11-23 21:26       ` Fil Dunsky
2026-11-26 10:57   ` Miquel Raynal [this message]
2026-11-26 10:57     ` Miquel Raynal
2026-11-26 11:37     ` Fil Dunsky
2026-11-26 11:37       ` Fil Dunsky
2026-11-23 20:27 ` [PATCH v2 3/3] MAINTAINERS: Add Cortina CS7542 NAND controller entry Fil Dunsky via B4 Relay
2026-11-23 20:27   ` Fil Dunsky
2026-11-23 20:27   ` Fil Dunsky via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87jylzyi5g.fsf@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=devnull+filipp.dunsky.gmail.com@kernel.org \
    --cc=filipp.dunsky@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    --cc=robh@kernel.org \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.