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.fat...@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