Skip to content

Commit

Permalink
lk2nd: hw: bdev: Add mmc_sdhci bdev driver
Browse files Browse the repository at this point in the history
  • Loading branch information
TravMurav committed Sep 27, 2023
1 parent 143327b commit 2af4382
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions lk2nd/hw/bdev/bdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
void lk2nd_bdev_init(void)
{
lk2nd_wrapper_bio_register();
lk2nd_mmc_sdhci_bio_register();

lk2nd_bdev_dump_devices();
}
1 change: 1 addition & 0 deletions lk2nd/hw/bdev/bdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
void lk2nd_bdev_dump_devices(void);

void lk2nd_wrapper_bio_register(void);
void lk2nd_mmc_sdhci_bio_register(void);

#endif
78 changes: 78 additions & 0 deletions lk2nd/hw/bdev/mmc_sdhci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright (c) 2023 Nikita Travkin <[email protected]> */

#include <err.h>
#include <stdlib.h>
#include <target.h>
#include <debug.h>
#include <partition_parser.h>
#include <lib/bio.h>
#include <lib/partition.h>
#include <mmc_sdhci.h>

#include <lk2nd/init.h>
#include <lk2nd/util/container_of.h>

#include "bdev.h"

struct mmc_bdev {
struct bdev dev;
struct mmc_device *mmc;
};

static ssize_t lk2nd_mmc_sdhci_bdev_read_block(struct bdev *bdev, void *buf, bnum_t block, uint count)
{
struct mmc_bdev *dev = container_of(bdev, struct mmc_bdev, dev);
uint32_t block_size = dev->dev.block_size;
uint32_t read_size = SDHCI_ADMA_MAX_TRANS_SZ;
uint32_t data_len = count * block_size;
uint64_t data_addr = block * block_size;
uint8_t *sptr = (uint8_t *)buf;
uint32_t ret = 0;

arch_clean_invalidate_cache_range((addr_t)(buf), data_len);

while (data_len > read_size) {
ret = mmc_sdhci_read(dev->mmc, (void *)sptr, (data_addr / block_size), (read_size / block_size));
if (ret)
return ERR_IO;

sptr += read_size;
data_addr += read_size;
data_len -= read_size;
}

if (data_len) {
ret = mmc_sdhci_read(dev->mmc, (void *)sptr, (data_addr / block_size), (data_len / block_size));
if (ret)
return ERR_IO;
}

return count * block_size;
}

void lk2nd_mmc_sdhci_bio_register(void)
{
struct mmc_bdev *bdev = malloc(sizeof(*bdev));
struct mmc_device *mmc;
char name[32];

dprintf(INFO, "Registering mmc_sdhci bio devices...\n");
mmc = target_get_second_mmc();

if (!mmc) {
dprintf(INFO, "Secondary MMC is unavailable.\n");
free(bdev);
return;
}

snprintf(name, sizeof(name), "mmc%d", mmc->config.slot);
bio_initialize_bdev(&bdev->dev, name, mmc->card.block_size, mmc->card.capacity / mmc->card.block_size);

bdev->mmc = mmc;
bdev->dev.read_block = lk2nd_mmc_sdhci_bdev_read_block;

bio_register_device(&bdev->dev);
partition_publish(name, 0);
}

1 change: 1 addition & 0 deletions lk2nd/hw/bdev/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ OBJS += \
$(LOCAL_DIR)/bdev.o \
$(LOCAL_DIR)/util.o \
$(LOCAL_DIR)/wrapper.o \
$(LOCAL_DIR)/mmc_sdhci.o \

0 comments on commit 2af4382

Please sign in to comment.