Skip to content

Commit d3ff578

Browse files
dgarskedanielinux
authored andcommitted
Add support for multi-block reads (performance improvements)
1 parent 963cdad commit d3ff578

File tree

5 files changed

+63
-32
lines changed

5 files changed

+63
-32
lines changed

arch.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ ifeq ($(ARCH),RISCV64)
586586
endif
587587

588588
ifneq ($(NO_ASM),1)
589+
CFLAGS+=-DWOLFSSL_RISCV_ASM
589590
MATH_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/riscv/riscv-64-sha256.o \
590591
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/riscv/riscv-64-sha512.o \
591592
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/riscv/riscv-64-sha3.o \

config/examples/polarfire_mpfs250.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ WOLFBOOT_LOAD_ADDRESS?=0x8E000000
4646
WOLFBOOT_NO_PARTITIONS=1
4747
CFLAGS_EXTRA+=-DBOOT_PART_A=1
4848
CFLAGS_EXTRA+=-DBOOT_PART_B=2
49+
# Speed up disk partition read (1MB chunks)
50+
CFLAGS_EXTRA+=-DDISK_BLOCK_SIZE=0x100000
4951

5052
# DTS (Device Tree)
5153
WOLFBOOT_LOAD_DTS_ADDRESS?=0x8A000000

docs/Targets.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,10 @@ See:
936936
Building mchp-base-image Yocto Linux:
937937

938938
```sh
939-
cd yocto-dev-polarfire/
939+
mkdir ../yocto-dev-polarfire
940+
cd ../yocto-dev-polarfire
941+
repo init -u https://github.com/linux4microchip/meta-mchp-manifest.git -b refs/tags/linux4microchip+fpga-2025.10 -m polarfire-soc/default.xml
942+
repo sync
940943
export TEMPLATECONF=${TEMPLATECONF:-../meta-mchp/meta-mchp-polarfire-soc/meta-mchp-polarfire-soc-bsp/conf/templates/default}
941944
source openembedded-core/oe-init-build-env
942945
# A Microchip base image with standard Linux utilities, as well as some Microchip apps and examples
@@ -1077,7 +1080,7 @@ Booting at 80200000
10771080
### PolarFire TODO
10781081

10791082
* Add eMMC/SD features:
1080-
- Multi-block read (CMD18 support)
1083+
- Improve mmc_delay and timeout handling
10811084
- DMA read support
10821085
- Write support
10831086
- eMMC support (not just SD)

hal/mpfs250.c

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ int mmc_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
456456
uint32_t sz)
457457
{
458458
int status;
459-
uint32_t reg;
459+
uint32_t block_count;
460+
uint32_t reg, cmd_reg;
460461

461462
/* wait for idle */
462463
status = mmc_wait_busy(0);
@@ -468,46 +469,66 @@ int mmc_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
468469
/* wait for command and data line busy to clear */
469470
while ((EMMC_SD_SRS09 & (EMMC_SD_SRS09_CICMD | EMMC_SD_SRS09_CIDAT)) != 0);
470471

472+
/* get block count (round up) */
473+
block_count = (sz + (EMMC_SD_BLOCK_SIZE - 1)) / EMMC_SD_BLOCK_SIZE;
471474
/* set transfer block count */
472-
EMMC_SD_SRS01 = (1 << EMMC_SD_SRS01_BCCT_SHIFT) | sz;
475+
EMMC_SD_SRS01 = (block_count << EMMC_SD_SRS01_BCCT_SHIFT) | sz;
476+
477+
cmd_reg = ((cmd_index << EMMC_SD_SRS03_CIDX_SHIFT) |
478+
EMMC_SD_SRS03_DPS | EMMC_SD_SRS03_DTDS |
479+
EMMC_SD_SRS03_BCE | EMMC_SD_SRS03_RECE | EMMC_SD_SRS03_RID |
480+
EMMC_SD_SRS03_RESP_48 | EMMC_SD_SRS03_CRCCE | EMMC_SD_SRS03_CICE);
473481

474482
if (cmd_index == SD_ACMD51_SEND_SCR) {
475-
status = mmc_send_cmd(SD_CMD_16, sz, EMMC_SD_RESP_R1);
483+
status = mmc_send_cmd(SD_CMD16, sz, EMMC_SD_RESP_R1);
476484
if (status == 0) {
477485
status = mmc_send_cmd(SD_CMD55_APP_CMD, (g_rca << SD_RCA_SHIFT),
478486
EMMC_SD_RESP_R1);
479487
}
480488
status = 0; /* ignore error */
481489
}
490+
else if (cmd_index == MMC_CMD18_READ_MULTIPLE) {
491+
cmd_reg |= EMMC_SD_SRS03_MSBS; /* enable multi-block select */
492+
EMMC_SD_SRS01 = (block_count << EMMC_SD_SRS01_BCCT_SHIFT) |
493+
EMMC_SD_BLOCK_SIZE;
494+
}
482495

483496
#ifdef DEBUG_MMC
484-
wolfBoot_printf("mmc_read: cmd_index: %d, block_addr: %08X, dst %p, sz: %d\n",
485-
cmd_index, block_addr, dst, sz);
497+
wolfBoot_printf("mmc_read: cmd_index: %d, block_addr: %08X, dst %p, sz: %d (%d blocks)\n",
498+
cmd_index, block_addr, dst, sz, block_count);
486499
#endif
487500

488501
EMMC_SD_SRS02 = block_addr; /* cmd argument */
489-
/* execute command */
490-
EMMC_SD_SRS03 = ((cmd_index << EMMC_SD_SRS03_CIDX_SHIFT) |
491-
EMMC_SD_SRS03_DPS | EMMC_SD_SRS03_DTDS |
492-
EMMC_SD_SRS03_BCE | EMMC_SD_SRS03_RECE | EMMC_SD_SRS03_RID |
493-
EMMC_SD_SRS03_RESP_48 | EMMC_SD_SRS03_CRCCE | EMMC_SD_SRS03_CICE);
494-
495-
/* wait for buffer read ready */
496-
while (((reg = EMMC_SD_SRS12) & (EMMC_SD_SRS12_BRR | EMMC_SD_SRS12_EINT)) == 0);
497-
498-
/* read in buffer - read 4 bytes at a time */
499-
if (reg & EMMC_SD_SRS12_BRR) {
500-
uint32_t i;
501-
for (i=0; i<sz; i+=4) {
502-
*dst = EMMC_SD_SRS08;
503-
dst++;
502+
EMMC_SD_SRS03 = cmd_reg; /* execute command */
503+
while (sz > 0) {
504+
/* wait for buffer read ready */
505+
while (((reg = EMMC_SD_SRS12) &
506+
(EMMC_SD_SRS12_BRR | EMMC_SD_SRS12_EINT)) == 0);
507+
508+
/* read in buffer - read 4 bytes at a time */
509+
if (reg & EMMC_SD_SRS12_BRR) {
510+
uint32_t i, read_sz = sz;
511+
if (read_sz > EMMC_SD_BLOCK_SIZE) {
512+
read_sz = EMMC_SD_BLOCK_SIZE;
513+
}
514+
for (i=0; i<read_sz; i+=4) {
515+
*dst = EMMC_SD_SRS08;
516+
dst++;
517+
}
518+
sz -= read_sz;
504519
}
505520
}
506521

522+
if (cmd_index == MMC_CMD18_READ_MULTIPLE) {
523+
/* send CMD12 to stop transfer - ignore response */
524+
(void)mmc_send_cmd(MMC_CMD12_STOP_TRANS, (g_rca << SD_RCA_SHIFT),
525+
EMMC_SD_RESP_R1);
526+
}
527+
507528
/* check for any errors and wait for idle */
508529
reg = EMMC_SD_SRS12;
509530
if ((reg & EMMC_SD_SRS12_ERR_STAT) == 0) {
510-
mmc_delay(0xFF);
531+
mmc_delay(0xFFF);
511532
status = mmc_wait_busy(0);
512533
}
513534
else {
@@ -585,7 +606,7 @@ int mmc_send_switch_function(uint32_t mode, uint32_t function_number,
585606
cmd_arg = (function_number << ((group_number - 1) * 4));
586607
do {
587608
/* first run check to see if function is supported */
588-
status = mmc_read(SD_CMD_6_SWITCH_FUNC,
609+
status = mmc_read(SD_CMD6_SWITCH_FUNC,
589610
(mode | cmd_arg),
590611
func_status, sizeof(func_status));
591612
if (status == 0) {
@@ -904,7 +925,7 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
904925
}
905926
if (read_sz < EMMC_SD_BLOCK_SIZE || /* last partial */
906927
start_offset != 0 || /* start not block aligned */
907-
((uintptr_t)buf % 4) != 0) /* buf not 4-byte aligned */
928+
((uintptr_t)buf % 4) != 0) /* buf not 4-byte aligned */
908929
{
909930
/* block read to temporary buffer */
910931
status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr,
@@ -916,9 +937,13 @@ int disk_read(int drv, uint64_t start, uint32_t count, uint8_t *buf)
916937
}
917938
}
918939
else {
919-
/* direct full block read */
920-
status = mmc_read(MMC_CMD17_READ_SINGLE, block_addr,
921-
(uint32_t*)buf, read_sz);
940+
/* direct full block(s) read */
941+
uint32_t blocks = (count / EMMC_SD_BLOCK_SIZE);
942+
read_sz = (blocks * EMMC_SD_BLOCK_SIZE);
943+
status = mmc_read(blocks > 1 ?
944+
MMC_CMD18_READ_MULTIPLE :
945+
MMC_CMD17_READ_SINGLE,
946+
block_addr, (uint32_t*)buf, read_sz);
922947
}
923948
if (status != 0) {
924949
break;

hal/mpfs250.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,17 +837,17 @@
837837
#define MMC_CMD1_SEND_OP_COND 1 /* MMC: Send operating conditions */
838838
#define MMC_CMD2_ALL_SEND_CID 2 /* Get card identification */
839839
#define MMC_CMD3_SET_REL_ADDR 3 /* Set relative address */
840-
#define MMC_CMD_4_SET_DSR 4
841-
#define SD_CMD_6_SWITCH_FUNC 6 /* SD: Switch function */
840+
#define MMC_CMD4_SET_DSR 4
841+
#define SD_CMD6_SWITCH_FUNC 6 /* SD: Switch function */
842842
#define MMC_CMD7_SELECT_CARD 7 /* Select/deselect card */
843843
#define MMC_CMD8_SEND_EXT_CSD 8 /* MMC: Get EXT_CSD */
844844
#define SD_CMD8_SEND_IF_COND 8 /* SD: Send interface condition */
845845
#define MMC_CMD9_SEND_CSD 9 /* Get card-specific data */
846846
#define SD_CMD11_VOLAGE_SWITCH 11 /* R1 Rsp */
847847
#define MMC_CMD12_STOP_TRANS 12 /* Stop transmission */
848848
#define MMC_CMD13_SEND_STATUS 13 /* Get card status */
849-
#define MMC_CMD_15_GOTO_INACT_ST 15
850-
#define SD_CMD_16 16 /* R1 Rsp */
849+
#define MMC_CMD15_GOTO_INACT_ST 15
850+
#define SD_CMD16 16 /* R1 Rsp */
851851
#define MMC_CMD17_READ_SINGLE 17 /* Read single block */
852852
#define MMC_CMD18_READ_MULTIPLE 18 /* Read multiple blocks */
853853
#define MMC_CMD24_WRITE_SINGLE 24 /* Write single block */

0 commit comments

Comments
 (0)