-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lk2nd: hw: bdev: Add mmc_sdhci bdev driver
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ OBJS += \ | |
$(LOCAL_DIR)/bdev.o \ | ||
$(LOCAL_DIR)/util.o \ | ||
$(LOCAL_DIR)/wrapper.o \ | ||
$(LOCAL_DIR)/mmc_sdhci.o \ |