Skip to content

Commit 2e8cd5f

Browse files
committed
Fixes to get uSD driver working
1 parent 7897bd8 commit 2e8cd5f

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

.github/workflows/test-build-riscv.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,13 @@ jobs:
8080
- name: Update repository
8181
run: sudo apt-get update -o Acquire::Retries=3
8282

83-
- name: Download and install RISC-V toolchain
84-
if: ${{ inputs.arch == 'riscv' }}
83+
- name: Download and install RISC-V toolchains
8584
run: |
8685
# Download latest prebuilt RISC-V toolchains
87-
wget -q https://github.com/RISCV-Tools/riscv-gnu-toolchain/releases/latest/download/riscv32-elf-ubuntu-24.04-gcc.tar.xz
88-
tar -xf riscv32-elf-ubuntu-24.04-gcc.tar.xz
89-
echo "$GITHUB_WORKSPACE/riscv/bin" >> $GITHUB_PATH
86+
#wget -q https://github.com/RISCV-Tools/riscv-gnu-toolchain/releases/latest/download/riscv32-elf-ubuntu-24.04-gcc.tar.xz
87+
#tar -xf riscv32-elf-ubuntu-24.04-gcc.tar.xz
88+
#echo "$GITHUB_WORKSPACE/riscv/bin" >> $GITHUB_PATH
9089
91-
- name: Download and install RISC-V toolchain
92-
if: ${{ inputs.arch == 'riscv64' }}
93-
run: |
94-
# Download latest prebuilt RISC-V toolchains
9590
wget -q https://github.com/RISCV-Tools/riscv-gnu-toolchain/releases/latest/download/riscv64-elf-ubuntu-24.04-gcc.tar.xz
9691
tar -xf riscv64-elf-ubuntu-24.04-gcc.tar.xz
9792
echo "$GITHUB_WORKSPACE/riscv/bin" >> $GITHUB_PATH
@@ -111,7 +106,7 @@ jobs:
111106
- name: Build wolfboot (riscv32)
112107
if: ${{ inputs.arch == 'riscv' }}
113108
run: |
114-
# using riscv64 for now since riscv32 is missing "zicsr" extension
109+
# using riscv64 for now since riscv64-unknown-elf- is missing "zicsr" extension
115110
make CROSS_COMPILE=riscv64-unknown-elf- FREEDOM_E_SDK=$GITHUB_WORKSPACE/freedom-e-sdk ${{inputs.make-args}}
116111
117112
- name: Build wolfboot (riscv64))

docs/Targets.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,9 @@ set architecture riscv:rv64
879879
### PolarFire Example Boot Output
880880

881881
```
882+
wolfBoot Version: 2.7.0 (Dec 17 2025 11:59:22)
882883
disk_open: drv = 0
883-
mmc_set_timeout: timeout_us: 500000, tcfclk_khz: -824320848 (mhz: 3470646), timeout_val: 500000, dtcv: 15
884+
mmc_set_timeout: timeout_val 500000 (12)
884885
mmc_set_clock: clock_khz: 400, freq_khz: 400
885886
mmc_send_cmd: cmd_index: 0, cmd_arg: 00000000, resp_type: 0
886887
mmc_send_cmd: cmd_index: 8, cmd_arg: 00000100, resp_type: 9
@@ -895,14 +896,27 @@ ocr_reg: 0x40FF8000
895896
mmc_send_cmd: cmd_index: 55, cmd_arg: 00000000, resp_type: 1
896897
mmc_send_cmd: cmd_index: 41, cmd_arg: 41200000, resp_type: 4
897898
ocr_reg: 0xC1FF8000
898-
mmc_send_cmd: cmd_index: 11, cmd_arg: 00000000, resp_type: 1
899899
mmc_send_cmd: cmd_index: 2, cmd_arg: 00000000, resp_type: 3
900900
mmc_send_cmd: cmd_index: 3, cmd_arg: 00000000, resp_type: 8
901901
mmc_init: rca: 43690
902902
mmc_send_cmd: cmd_index: 9, cmd_arg: AAAA0000, resp_type: 3
903903
mmc_init: sector size: 512
904904
mmc_init: sector count: 62333952
905905
mmc_send_cmd: cmd_index: 7, cmd_arg: AAAA0000, resp_type: 2
906+
mmc_send_cmd: cmd_index: 55, cmd_arg: AAAA0000, resp_type: 1
907+
mmc_send_cmd: cmd_index: 6, cmd_arg: 00000002, resp_type: 1
908+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
909+
mmc_send_cmd: cmd_index: 16, cmd_arg: 00000008, resp_type: 1
910+
mmc_send_cmd: cmd_index: 55, cmd_arg: AAAA0000, resp_type: 1
911+
mmc_block_read: cmd_index: 51, block_addr: 00000000, dst 0x801FFCE0, sz: 8
912+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
913+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
914+
mmc_block_read: cmd_index: 6, block_addr: 00000001, dst 0x801FFC48, sz: 64
915+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
916+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
917+
mmc_block_read: cmd_index: 6, block_addr: 80000001, dst 0x801FFC48, sz: 64
918+
mmc_send_cmd: cmd_index: 13, cmd_arg: AAAA0000, resp_type: 1
919+
mmc_set_clock: clock_khz: 50000, freq_khz: 50000
906920
Checking primary OS image in 0,1...
907921
Checking secondary OS image in 0,2...
908922
No valid OS image found in either partition 1 or 2

hal/mpfs250.c

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
/* Placeholder functions - to be implemented */
4949
void hal_init(void)
5050
{
51+
wolfBoot_printf("wolfBoot Version: %s (%s %s)\n",
52+
LIBWOLFBOOT_VERSION_STRING,__DATE__, __TIME__);
5153

5254
}
5355

@@ -150,7 +152,8 @@ int mmc_set_timeout(uint32_t timeout_us)
150152

151153
/* read capabilities to determine timeout clock frequency and unit (MHz or kHz) */
152154
reg = EMMC_SD_SRS16;
153-
tcfclk_khz = (reg * EMMC_SD_SRS16_TCF_MASK);
155+
tcfclk_khz = (reg & EMMC_SD_SRS16_TCF_MASK) >> EMMC_SD_SRS16_TCF_SHIFT;
156+
/* Default timeout clock frequency should be 50MHz */
154157

155158
if (((reg & EMMC_SD_SRS16_TCU) == 0) && (timeout_us < 1000)) {
156159
/* invalid timeout_us value */
@@ -184,14 +187,14 @@ int mmc_set_timeout(uint32_t timeout_us)
184187
}
185188
dtcv = i;
186189

187-
/* set the data timeout counter value - CHECK: 0xc0207 */
190+
/* set the data timeout counter value */
188191
reg = EMMC_SD_SRS11;
189192
reg &= ~EMMC_SD_SRS11_DTCV_MASK;
190193
reg |= (dtcv << EMMC_SD_SRS11_DTCV_SHIFT) & EMMC_SD_SRS11_DTCV_MASK;
191194
EMMC_SD_SRS11 = reg;
192195

193196
#ifdef DEBUG_MMC
194-
wolfBoot_printf("mmc_set_timeout: dtcv 0x%08X, srs11 0x%08X\n", dtcv, reg);
197+
wolfBoot_printf("mmc_set_timeout: timeout_val %d (%d)\n", timeout_val, dtcv);
195198
#endif
196199

197200
return 0;
@@ -268,9 +271,6 @@ uint32_t mmc_set_clock(uint32_t clock_khz)
268271
}
269272
base_clk_khz *= 1000; /* convert MHz to kHz */
270273

271-
/* select clock frequency */
272-
reg = EMMC_SD_SRS11;
273-
reg &= ~(EMMC_SD_SRS11_SDCFSL_MASK | EMMC_SD_SRS11_SDCFSH_MASK);
274274
/* calculate divider */
275275
for (i=1; i<2046; i++) {
276276
if (((base_clk_khz / i) < clock_khz) ||
@@ -279,8 +279,12 @@ uint32_t mmc_set_clock(uint32_t clock_khz)
279279
}
280280
}
281281
mclk = (i / 2);
282-
reg |= ((mclk << EMMC_SD_SRS11_SDCFSL_SHIFT) & EMMC_SD_SRS11_SDCFSL_MASK) /* lower 8 bits */
283-
| ((mclk << EMMC_SD_SRS11_SDCFSH_SHIFT) & EMMC_SD_SRS11_SDCFSH_SHIFT); /* upper 2 bits */
282+
283+
/* select clock frequency */
284+
reg = EMMC_SD_SRS11;
285+
reg &= ~(EMMC_SD_SRS11_SDCFSL_MASK | EMMC_SD_SRS11_SDCFSH_MASK);
286+
reg |= (((mclk & 0x0FF) << EMMC_SD_SRS11_SDCFSL_SHIFT) & EMMC_SD_SRS11_SDCFSL_MASK); /* lower 8 bits */
287+
reg |= (((mclk & 0x300) << EMMC_SD_SRS11_SDCFSH_SHIFT) & EMMC_SD_SRS11_SDCFSH_SHIFT); /* upper 2 bits */
284288
reg |= EMMC_SD_SRS11_ICE; /* clock enable */
285289
reg &= ~EMMC_SD_SRS11_CGS; /* select clock */
286290
EMMC_SD_SRS11 = reg;
@@ -383,7 +387,7 @@ int mmc_send_cmd(uint32_t cmd_index, uint32_t cmd_arg, uint8_t resp_type)
383387
/* check for device busy */
384388
if (resp_type == EMMC_SD_RESP_R1 || resp_type == EMMC_SD_RESP_R1B) {
385389
uint32_t resp = EMMC_SD_SRS04;
386-
#define CARD_STATUS_READY_FOR_DATA (0x1 << 8)
390+
#define CARD_STATUS_READY_FOR_DATA (1U << 8)
387391
if ((resp & CARD_STATUS_READY_FOR_DATA) == 0) {
388392
status = DEVICE_BUSY; /* card is busy */
389393
}
@@ -469,13 +473,26 @@ int mmc_block_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
469473
/* wait for command and data line busy to clear */
470474
while ((EMMC_SD_SRS09 & (EMMC_SD_SRS09_CICMD | EMMC_SD_SRS09_CIDAT)) != 0);
471475

472-
EMMC_SD_SRS01 = block_addr; /* cmd argument */
476+
if (cmd_index == SD_ACMD51_SEND_SCR) {
477+
status = mmc_send_cmd(SD_CMD_16, sz, EMMC_SD_RESP_R1);
478+
if (status == 0) {
479+
status = mmc_send_cmd(SD_CMD55_APP_CMD, (g_rca << SD_RCA_SHIFT),
480+
EMMC_SD_RESP_R1);
481+
}
482+
status = 0; /* ignore error */
483+
}
484+
485+
#ifdef DEBUG_MMC
486+
wolfBoot_printf("mmc_block_read: cmd_index: %d, block_addr: %08X, dst %p, sz: %d\n",
487+
cmd_index, block_addr, dst, sz);
488+
#endif
489+
490+
EMMC_SD_SRS02 = block_addr; /* cmd argument */
473491
/* execute command */
474492
EMMC_SD_SRS03 = ((cmd_index << EMMC_SD_SRS03_CIDX_SHIFT) |
475493
EMMC_SD_SRS03_DPS | EMMC_SD_SRS03_DTDS |
476494
EMMC_SD_SRS03_BCE | EMMC_SD_SRS03_RECE | EMMC_SD_SRS03_RID |
477-
EMMC_SD_SRS03_RECT | EMMC_SD_SRS03_RESP_48 | EMMC_SD_SRS03_CRCCE |
478-
EMMC_SD_SRS03_CICE);
495+
EMMC_SD_SRS03_RESP_48 | EMMC_SD_SRS03_CRCCE | EMMC_SD_SRS03_CICE);
479496

480497
/* wait for buffer read ready */
481498
while (((reg = EMMC_SD_SRS12) & (EMMC_SD_SRS12_BRR | EMMC_SD_SRS12_EINT)) == 0);
@@ -489,15 +506,17 @@ int mmc_block_read(uint32_t cmd_index, uint32_t block_addr, uint32_t* dst,
489506
}
490507
}
491508

492-
/* check for any errors */
509+
/* check for any errors and wait for idle */
493510
reg = EMMC_SD_SRS12;
494-
if (reg & EMMC_SD_SRS12_ERR_STAT) {
511+
if ((reg & EMMC_SD_SRS12_ERR_STAT) == 0) {
512+
mmc_delay(0xFF);
513+
status = mmc_wait_busy(0);
514+
}
515+
else {
495516
#ifdef DEBUG_MMC
496517
wolfBoot_printf("mmc_block_read: error: 0x%08X\n", reg);
497518
#endif
498-
/* wait for idle */
499-
mmc_delay(0xFF);
500-
status = mmc_wait_busy(0);
519+
status = -1;
501520
}
502521

503522
return status;
@@ -538,7 +557,7 @@ static uint32_t get_srs_bits(int from, int count)
538557
int off, shft;
539558

540559
from -= 8;
541-
mask = (count < 32 ? 1 << count : 0) - 1;
560+
mask = ((count < 32) ? (1U << (uint32_t)count) : 0) - 1;
542561
off = from / 32;
543562
shft = from & 31;
544563
ret = resp[off] >> shft;
@@ -785,13 +804,13 @@ int mmc_init(void)
785804
#define SECT_SIZE_CSD_SHIFT 14
786805
c_size = (EMMC_SD_SRS04 & SECT_SIZE_CSD_MASK) >> SECT_SIZE_CSD_SHIFT;
787806
if (c_size < 32) {
788-
g_sector_size = (1 << c_size);
807+
g_sector_size = (1U << c_size);
789808
#ifdef DEBUG_MMC
790809
wolfBoot_printf("mmc_init: sector size: %d\n", g_sector_size);
791810
#endif
792811
}
793812

794-
csd_struct = get_srs_bits(126,2);
813+
csd_struct = get_srs_bits(126, 2);
795814
switch (csd_struct) {
796815
case 0:
797816
c_size = get_srs_bits(62, 12);
@@ -829,16 +848,8 @@ int mmc_init(void)
829848
if (status == 0) {
830849
/* Get SCR registers - 8 bytes */
831850
uint32_t scr_reg[SCR_REG_DATA_SIZE/sizeof(uint32_t)];
832-
833-
status = mmc_send_cmd(SD_CMD_16, sizeof(scr_reg), EMMC_SD_RESP_R1);
834-
if (status == 0) {
835-
status = mmc_send_cmd(SD_CMD55_APP_CMD, (g_rca << SD_RCA_SHIFT),
836-
EMMC_SD_RESP_R1);
837-
}
838-
if (status == 0) {
839-
status = mmc_block_read(SD_ACMD51_SEND_SCR, 0, scr_reg,
840-
sizeof(scr_reg));
841-
}
851+
status = mmc_block_read(SD_ACMD51_SEND_SCR, 0, scr_reg,
852+
sizeof(scr_reg));
842853
}
843854
if (status == 0) {
844855
/* set UHS mode to SDR25 and driver strength to Type B */

0 commit comments

Comments
 (0)