mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/9] mci: add common PBL helper for chainloading after BootROM initialization
Date: Tue, 22 Apr 2025 07:26:30 +0200	[thread overview]
Message-ID: <20250422052635.3423961-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250422052635.3423961-1-a.fatoum@pengutronix.de>

We have a number of PBL drivers that communicate directly with a
SD-Card or eMMC already initialized by the BootROM and put into
transfer state. This lets us skip having a stripped down MMC framework
in the prebootloader.

Instead of duplicating this code across drivers, let's put the duplicate
code into a common location.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/mci/Makefile  |   1 +
 drivers/mci/mci-pbl.c | 110 ++++++++++++++++++++++++++++++++++++++++++
 include/pbl/mci.h     |  37 ++++++++++++++
 3 files changed, 148 insertions(+)
 create mode 100644 drivers/mci/mci-pbl.c
 create mode 100644 include/pbl/mci.h

diff --git a/drivers/mci/Makefile b/drivers/mci/Makefile
index d3df4c1bb650..8f54107e46c1 100644
--- a/drivers/mci/Makefile
+++ b/drivers/mci/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_MCI)		+= mci-core.o
+pbl-$(CONFIG_MCI)		+= mci-pbl.o
 obj-$(CONFIG_MCI_MMC_RPMB)	+= rpmb.o
 obj-$(CONFIG_MCI_AM654)		+= am654-sdhci.o
 obj-$(CONFIG_MCI_ARASAN)	+= arasan-sdhci.o
diff --git a/drivers/mci/mci-pbl.c b/drivers/mci/mci-pbl.c
new file mode 100644
index 000000000000..b58cd8dafe84
--- /dev/null
+++ b/drivers/mci/mci-pbl.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * This code assumes that the SD/eMMC is already in transfer mode,
+ * because it card initialization has been done by the BootROM.
+ *
+ * In interest of reducing complexity, code size and boot time, we
+ * don't want to reset the card, but reuse it as-is.
+ *
+ * Full reinitialization of the card has to wait until we are
+ * in barebox proper (see mci-core.c).
+ */
+
+#define pr_fmt(fmt) "mci-pbl: " fmt
+
+#include <mci.h>
+#include <pbl/mci.h>
+#include <pbl/bio.h>
+#include <linux/minmax.h>
+#include <linux/bug.h>
+
+#define MCI_PBL_BLOCK_LEN		512
+
+static int pbl_mci_read_blocks(struct pbl_mci *mci, void *dst,
+			       off_t start, unsigned int nblocks)
+{
+	struct mci_cmd cmd = {};
+	struct mci_data data;
+	int ret;
+
+	if (mci->capacity == PBL_MCI_STANDARD_CAPACITY)
+		start *= MCI_PBL_BLOCK_LEN;
+
+	mci_setup_cmd(&cmd, MMC_CMD_READ_SINGLE_BLOCK,
+		      start, MMC_RSP_R1);
+	if (nblocks > 1)
+		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
+
+	data.dest = dst;
+	data.flags = MMC_DATA_READ;
+	data.blocksize = MCI_PBL_BLOCK_LEN;
+	data.blocks = nblocks;
+
+	ret = pbl_mci_send_cmd(mci, &cmd, &data);
+	if (ret || nblocks > 1) {
+		mci_setup_cmd(&cmd, MMC_CMD_STOP_TRANSMISSION, 0,
+			      MMC_RSP_R1b);
+		pbl_mci_send_cmd(mci, &cmd, NULL);
+	}
+
+	return ret;
+}
+
+static int pbl_bio_mci_read(struct pbl_bio *bio, off_t start,
+			    void *buf, unsigned int nblocks)
+{
+	struct pbl_mci *mci = bio->priv;
+	unsigned int count = 0;
+	unsigned int block_len = MCI_PBL_BLOCK_LEN;
+	int ret;
+
+	while (count < nblocks) {
+		unsigned n = nblocks - count;
+
+		if (mci->max_blocks_per_read)
+			n = min(n, mci->max_blocks_per_read);
+
+		ret = pbl_mci_read_blocks(mci, buf, start, n);
+		if (ret < 0)
+			return ret;
+
+		count += n;
+		start += n;
+		buf += n *block_len;
+	}
+
+	return count;
+}
+
+static const char *capacity_tostr(enum pbl_mci_capacity capacity)
+{
+	switch (capacity) {
+	case PBL_MCI_STANDARD_CAPACITY:
+		return "standard";
+	case PBL_MCI_HIGH_CAPACITY:
+		return "high/extended";
+	case PBL_MCI_ULTRA_CAPACITY:
+		return "ultra";
+	case PBL_MCI_RESERVED_CAPACITY:
+		return "reserved";
+	case PBL_MCI_UNKNOWN_CAPACITY:
+		break;
+	}
+
+	return "unknown";
+}
+
+int pbl_mci_bio_init(struct pbl_mci *mci, struct pbl_bio *bio)
+{
+
+	if (mci->capacity == PBL_MCI_UNKNOWN_CAPACITY)
+		BUG();
+	else
+		pr_debug("assuming %s capacity card\n",
+			 capacity_tostr(mci->capacity));
+
+	bio->priv = mci;
+	bio->read = pbl_bio_mci_read;
+
+	return 0;
+}
diff --git a/include/pbl/mci.h b/include/pbl/mci.h
new file mode 100644
index 000000000000..dd4fcac5412b
--- /dev/null
+++ b/include/pbl/mci.h
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __PBL_MCI_H__
+#define __PBL_MCI_H__
+
+#include <linux/types.h>
+#include <mci.h>
+
+struct mci_cmd;
+struct mci_data;
+struct pbl_bio;
+
+enum pbl_mci_capacity {
+	PBL_MCI_UNKNOWN_CAPACITY,
+	PBL_MCI_STANDARD_CAPACITY,
+	PBL_MCI_HIGH_CAPACITY, /* and extended */
+	PBL_MCI_ULTRA_CAPACITY,
+	PBL_MCI_RESERVED_CAPACITY,
+};
+
+struct pbl_mci {
+	void *priv;
+	enum pbl_mci_capacity capacity;
+	unsigned max_blocks_per_read;
+	int (*send_cmd)(struct pbl_mci *mci, struct mci_cmd *cmd,
+			struct mci_data *data);
+};
+
+static inline int pbl_mci_send_cmd(struct pbl_mci *mci,
+				   struct mci_cmd *cmd,
+				   struct mci_data *data)
+{
+	return mci->send_cmd(mci, cmd, data);
+}
+
+int pbl_mci_bio_init(struct pbl_mci *mci, struct pbl_bio *bio);
+
+#endif /* __PBL_MCI_H__ */
-- 
2.39.5




  parent reply	other threads:[~2025-04-22  5:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22  5:26 [PATCH 0/9] ARM: OMAP: beaglebone: add PBL SD xload support Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 1/9] clocksource: make available in PBL Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 2/9] clocksource: ti-dm: " Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 3/9] mci: move mci_setup_cmd definition into header Ahmad Fatoum
2025-04-22  5:26 ` Ahmad Fatoum [this message]
2025-04-22  5:26 ` [PATCH 5/9] mci: pbl: add autodetection of BootROM-initialized standard capacity cards Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 6/9] mci: omap_hsmmc: split out common code Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 7/9] ARM: OMAP: add am33xx_hsmmc_start_image for PBL Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 8/9] mci: omap_hsmmc: add xload implementation " Ahmad Fatoum
2025-04-22  5:26 ` [PATCH 9/9] ARM: OMAP: beaglebone: add PBL SD xload support Ahmad Fatoum
2025-04-22 10:55 ` [PATCH 0/9] " Sascha Hauer

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=20250422052635.3423961-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox