hw/riscv: sifive_u: Add missing SPI support
Subsystems (3)
v2 cover hw/riscv: sifive_u: Add missing SPI support
From: Bin Meng <[email protected]>
This adds the missing SPI support to the sifive_u machine in the QEMU mainline. With this series, upstream U-Boot for the SiFive HiFive Unleashed board can boot on QEMU sifive_u out of the box. This allows users to develop and test the recommended RISC-V boot flow with a real world use
case: ZSBL (in QEMU) loads U-Boot SPL from SD card or SPI flash to L2LIM, then U-Boot SPL loads the payload from SD card or SPI flash that is a combination of OpenSBI fw_dynamic firmware and U-Boot proper.
The m25p80 model is updated to support ISSI flash series. A bunch of ssi-sd issues are fixed, and writing to SD card in SPI mode is supported.
reST documentation for RISC-V is added. Currently only sifive_u machine is documented, but more to come.
Changes in v2:
- Mention QPI (Quad Peripheral Interface) mode is not supported
- Add a debug printf in the state handling codes
- Fix several typos in the commit message
- new patch: add a state representing Nac
- Make this fix a separate patch from the CMD18 support
- Fix 2 typos in the commit message
- Add a comment block to explain the CMD12 timing
- Catch CMD12 in all of the data read states per the timing requirement
- Move multiple write token definitions out of this patch
- Correct the "coding" typo in the commit message
- Correct the "token" typo in the commit message
- Add 'write_bytes' in vmstate_ssi_sd
- Correct the "token" typo in the commit message
- Introduce multiple write token definitions in this patch
- new patch: bump up version ids of VMStateDescription
- Log guest error when trying to write reserved registers
- Log guest error when trying to access out-of-bounds registers
- log guest error when writing to reserved bits for chip select registers and watermark registers
- Log unimplemented warning when trying to write direct-map flash interface registers
- Add test tx fifo full logic in sifive_spi_read(), hence remove setting the tx fifo full flag in sifive_spi_write().
- Populate register with their default value
- Correct the "connects" typo in the commit message
- Mention in the commit message that <reg> property does not populate the second group which represents the memory mapped address of the SPI flash
- Correct the "connects" typo in the commit message
- Correct several typos in sifive_u.rst
- Update doc to mention U-Boot v2021.01
Bin Meng (25):
hw/block: m25p80: Add ISSI SPI flash support
hw/block: m25p80: Add various ISSI flash information hw/sd: ssi-sd: Fix incorrect card response sequence
hw/sd: sd: Support CMD59 for SPI mode
hw/sd: sd: Drop sd_crc16()
util: Add CRC16 (CCITT) calculation routines
hw/sd: ssi-sd: Suffix a data block with CRC16
hw/sd: ssi-sd: Add a state representing Nac
hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION hw/sd: ssi-sd: Support multiple block read
hw/sd: ssi-sd: Use macros for the dummy value and tokens in the
transfer
hw/sd: sd: Remove duplicated codes in single/multiple block read/write hw/sd: sd: Allow single/multiple block write for SPI mode hw/sd: sd.h: Cosmetic change of using spaces
hw/sd: Introduce receive_ready() callback
hw/sd: ssi-sd: Support single block write
hw/sd: ssi-sd: Support multiple block write
hw/sd: ssi-sd: Bump up version ids of VMStateDescription hw/ssi: Add SiFive SPI controller support
hw/riscv: sifive_u: Add QSPI0 controller and connect a flash hw/riscv: sifive_u: Add QSPI2 controller and connect an SD card hw/riscv: sifive_u: Change SIFIVE_U_GEM_IRQ to decimal value docs/system: Sort targets in alphabetical order docs/system: Add RISC-V documentation
docs/system: riscv: Add documentation for sifive_u machine
docs/system/riscv/sifive_u.rst | 336 ++++++++++++++++++++++++++++++ docs/system/target-riscv.rst | 72 +++++++ docs/system/targets.rst | 20 +- include/hw/riscv/sifive_u.h | 9 +- include/hw/sd/sd.h | 44 ++-- include/hw/ssi/sifive_spi.h | 47 +++++ include/qemu/crc-ccitt.h | 33 +++ hw/block/m25p80.c | 57 ++++- hw/riscv/sifive_u.c | 91 ++++++++ hw/sd/core.c | 13 ++ hw/sd/sd.c | 82 +------- hw/sd/ssi-sd.c | 166 +++++++++++++-- hw/ssi/sifive_spi.c | 367 +++++++++++++++++++++++++++++++++ util/crc-ccitt.c | 127 ++++++++++++ hw/riscv/Kconfig | 3 + hw/ssi/Kconfig | 4 + hw/ssi/meson.build | 1 + util/meson.build | 1 + 18 files changed, 1347 insertions(+), 126 deletions(-) create mode 100644 docs/system/riscv/sifive_u.rst create mode 100644 docs/system/target-riscv.rst create mode 100644 include/hw/ssi/sifive_spi.h create mode 100644 include/qemu/crc-ccitt.h create mode 100644 hw/ssi/sifive_spi.c create mode 100644 util/crc-ccitt.c
Signature · 1 line
2.25.1
PATCH v2 1/25 hw/block: m25p80: Add ISSI SPI flash support
From: Bin Meng <[email protected]>
This adds the ISSI SPI flash support. The number of dummy cycles in fast read, fast read dual output and fast read quad output commands is currently using the default 8. Likewise, the same default value is used for fast read dual/quad I/O command. Per the datasheet [1], the number of dummy cycles is configurable, but this is not modeled at present.
For flash whose size is larger than 16 MiB, the sequence of 3-byte address along with EXTADD bit in the bank address register (BAR) is not supported. We assume that guest software always uses op codes with 4-byte address sequence. Fortunately, this is the case for both U-Boot and Linux spi-nor drivers.
QPI (Quad Peripheral Interface) that supports 2-cycle instruction has different default values for dummy cycles of fast read family commands, and is unsupported at the time being.
[1] http://www.issi.com/WW/pdf/25LP-WP256.pdf
---
Changes in v2:
- Mention QPI (Quad Peripheral Interface) mode is not supported
hw/block/m25p80.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
hw/block/m25p80.c +43 -1
@@ -412,6 +412,7 @@ typedef enum {MAN_NUMONYX ,MAN_WINBOND ,MAN_SST , +MAN_ISSI ,MAN_GENERIC , } Manufacturer;
@@ -487,6 +488,8 @@ static inline Manufacturer get_man(Flash *s)return MAN_MACRONIX ;case 0xBF :return MAN_SST ; +case 0x9D : +return MAN_ISSI ;default :return MAN_GENERIC ; }
@@ -706,6 +709,9 @@ static void complete_collecting_data(Flash *s)case MAN_SPANSION : s->quad_enable = !!(s->data[1 ] &0x02 );break ; +case MAN_ISSI : + s->quad_enable =extract32 (s->data[0 ],6 ,1 ); +break ;case MAN_MACRONIX : s->quad_enable =extract32 (s->data[0 ],6 ,1 );if (s->len >1 ) {
@@ -895,6 +901,19 @@ static void decode_fast_read_cmd(Flash *s)SPANSION_DUMMY_CLK_LEN );break ; +case MAN_ISSI : +/* +* The Fast Read instruction code is followed by address bytes and +* dummy cycles, transmitted via the SI line. +* +* The number of dummy cycles is configurable but this is currently +* unmodeled, hence the default value 8 is used. +* +* QPI (Quad Peripheral Interface) mode has different default value +* of dummy cycles, but this is unsupported at the time being. +*/ + s->needed_bytes +=1 ; +break ;default :break ; }
@@ -934,6 +953,16 @@ static void decode_dio_read_cmd(Flash *s)break ; }break ; +case MAN_ISSI : +/* +* The Fast Read Dual I/O instruction code is followed by address bytes +* and dummy cycles, transmitted via the IO1 and IO0 line. +* +* The number of dummy cycles is configurable but this is currently +* unmodeled, hence the default value 4 is used. +*/ + s->needed_bytes +=1 ; +break ;default :break ; }
@@ -974,6 +1003,19 @@ static void decode_qio_read_cmd(Flash *s)break ; }break ; +case MAN_ISSI : +/* +* The Fast Read Quad I/O instruction code is followed by address bytes +* and dummy cycles, transmitted via the IO3, IO2, IO1 and IO0 line. +* +* The number of dummy cycles is configurable but this is currently +* unmodeled, hence the default value 6 is used. +* +* QPI (Quad Peripheral Interface) mode has different default value +* of dummy cycles, but this is unsupported at the time being. +*/ + s->needed_bytes +=3 ; +break ;default :break ; }
@@ -1132,7 +1174,7 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; -if (get_man (s) ==MAN_MACRONIX ) { +if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ; }if (get_man (s) ==MAN_SST ) {
Signature · 1 line
2.25.1
PATCH v2 2/25 hw/block: m25p80: Add various ISSI flash information
In reply to Bin MengFrom: Bin Meng <[email protected]>
This updates the flash information table to include various ISSI flashes that are supported by upstream U-Boot and Linux kernel.
---
(no changes since v1)
hw/block/m25p80.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
hw/block/m25p80.c +13 -0
@@ -210,6 +210,19 @@ static const FlashPartInfo known_devices[] = { { INFO("640s33b", 0x898913, 0, 64 << 10, 128, 0) }, { INFO("n25q064", 0x20ba17, 0, 64 << 10, 128, 0) }, +/* ISSI */ + { INFO("is25lq040b", 0x9d4013, 0, 64 << 10, 8, ER_4K) }, + { INFO("is25lp080d", 0x9d6014, 0, 64 << 10, 16, ER_4K) }, + { INFO("is25lp016d", 0x9d6015, 0, 64 << 10, 32, ER_4K) }, + { INFO("is25lp032", 0x9d6016, 0, 64 << 10, 64, ER_4K) }, + { INFO("is25lp064", 0x9d6017, 0, 64 << 10, 128, ER_4K) }, + { INFO("is25lp128", 0x9d6018, 0, 64 << 10, 256, ER_4K) }, + { INFO("is25lp256", 0x9d6019, 0, 64 << 10, 512, ER_4K) }, + { INFO("is25wp032", 0x9d7016, 0, 64 << 10, 64, ER_4K) }, + { INFO("is25wp064", 0x9d7017, 0, 64 << 10, 128, ER_4K) }, + { INFO("is25wp128", 0x9d7018, 0, 64 << 10, 256, ER_4K) }, + { INFO("is25wp256", 0x9d7019, 0, 64 << 10, 512, ER_4K) }, +/* Macronix */ { INFO("mx25l2005a", 0xc22012, 0, 64 << 10, 4, ER_4K) }, { INFO("mx25l4005a", 0xc22013, 0, 64 << 10, 8, ER_4K) },
Signature · 1 line
2.25.1
PATCH v2 3/25 hw/sd: ssi-sd: Fix incorrect card response sequence
In reply to Bin MengFrom: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00" chapter 7.5.1, "Command/Response", there is a minimum 8 clock cycles (Ncr) before the card response shows up on the data out line. However current implementation jumps directly to the sending response state after all 6 bytes command is received, which is a spec violation.
Add a new state PREP_RESP in the ssi-sd state machine to handle it.
---
Changes in v2:
- Add a debug printf in the state handling codes
hw/sd/ssi-sd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/sd/ssi-sd.c +6 -1
@@ -36,6 +36,7 @@ do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0) typedef enum { SSI_SD_CMD = 0, SSI_SD_CMDARG, + SSI_SD_PREP_RESP, SSI_SD_RESPONSE, SSI_SD_DATA_START, SSI_SD_DATA_READ,
@@ -163,12 +164,16 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->response[1 ] = status;DPRINTF ("Card status 0x%02x\n" , status); } - s->mode =SSI_SD_RESPONSE ; + s->mode =SSI_SD_PREP_RESP ; s->response_pos =0 ; }else { s->cmdarg[s->arglen++] = val; }return 0xff ; +case SSI_SD_PREP_RESP : +DPRINTF ("Prepare card response (Ncr)\n" ); + s->mode =SSI_SD_RESPONSE ; +return 0xff ;case SSI_SD_RESPONSE :if (s->stopping) { s->stopping =0 ;
Signature · 1 line
2.25.1
Re: [PATCH v2 03/25] hw/sd: ssi-sd: Fix incorrect card response sequence
Quoted 28 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00" chapter 7.5.1, "Command/Response", there is a minimum 8 clock cycles (Ncr) before the card response shows up on the data out line. However current implementation jumps directly to the sending response state after all 6 bytes command is received, which is a spec violation.
Add a new state
PREP_RESPin the ssi-sd state machine to handle it.Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>Reviewed-by Pragnesh Patel <[email protected]>Tested-by Pragnesh Patel <[email protected]>---
Changes in v2:
- Add a debug printf in the state handling codeshw/sd/ssi-sd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/sd/ssi-sd.c +0
-0
hw/sd/ssi-sd.c do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0) · line 39
Quoted 4 lines
hw/sd/ssi-sd.c +1
-0@@ -36,6 +36,7 @@ do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0) typedef enum { SSI_SD_CMD = 0, SSI_SD_CMDARG, + SSI_SD_PREP_RESP,
Another migration change.
Otherwise (trusting you with the spec):
Quoted 22 lines
hw/sd/ssi-sd.c +5
-1SSI_SD_RESPONSE ,SSI_SD_DATA_START , SSI_SD_DATA_READ,@@ -163,12 +164,16 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->response[1 ] = status;DPRINTF ("Card status 0x%02x\n" , status); } - s->mode =SSI_SD_RESPONSE ; + s->mode =SSI_SD_PREP_RESP ; s->response_pos =0 ; }else { s->cmdarg[s->arglen++] = val; }return 0xff ; +case SSI_SD_PREP_RESP : +DPRINTF ("Prepare card response (Ncr)\n" ); + s->mode =SSI_SD_RESPONSE ; +return 0xff ;case SSI_SD_RESPONSE :if (s->stopping) { s->stopping =0 ;
PATCH v2 4/25 hw/sd: sd: Support CMD59 for SPI mode
In reply to Bin MengFrom: Bin Meng <[email protected]>
After the card is put into SPI mode, CRC check for all commands including CMD0 will be done according to CMD59 setting. But this command is currently unimplemented. Simply allow the decoding of CMD59, but the CRC remains unchecked.
---
(no changes since v1)
hw/sd/sd.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
hw/sd/sd.c +1 -7
@@ -1517,18 +1517,12 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)if (!sd->spi) { goto bad_cmd; } - goto unimplemented_spi_cmd; +return sd_r1;default : bad_cmd:qemu_log_mask (LOG_GUEST_ERROR ,"SD: Unknown CMD%i\n" , req.cmd);return sd_illegal; - - unimplemented_spi_cmd: -/* Commands that are recognised but not yet implemented in SPI mode. */ -qemu_log_mask (LOG_UNIMP ,"SD: CMD%i not implemented in SPI mode\n" , - req.cmd); -return sd_illegal; }qemu_log_mask (LOG_GUEST_ERROR ,"SD: CMD%i in a wrong state\n" , req.cmd);
Signature · 1 line
2.25.1
Re: [PATCH v2 04/25] hw/sd: sd: Support CMD59 for SPI mode
Quoted 10 lines · Bin Meng
From: Bin Meng <[email protected]>
After the card is put into SPI mode, CRC check for all commands including CMD0 will be done according to CMD59 setting. But this command is currently unimplemented. Simply allow the decoding of CMD59, but the CRC remains unchecked.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Pragnesh Patel <[email protected]>Tested-by Pragnesh Patel <[email protected]>
Quoted 6 lines
---
(no changes since v1)
hw/sd/sd.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
PATCH v2 5/25 hw/sd: sd: Drop sd_crc16()
In reply to Bin MengFrom: Bin Meng <[email protected]>
commit f6fb1f9b319f ("sdcard: Correct CRC16 offset in sd_function_switch()") changed the 16-bit CRC to be stored at offset 64. In fact, this CRC calculation is completely wrong. From the original codes, it wants to calculate the CRC16 of the first 64 bytes of sd->data[], however passing 64 as the width to sd_crc16() actually counts 256 bytes starting from the message for the CRC16 calculation, which is not what we want.
Besides that, it seems existing sd_crc16() algorithm does not match the SD spec, which says CRC16 is the CCITT one but the calculation does not produce expected result. It turns out the CRC16 was never transferred outside the sd core, as in sd_read_byte() we see:
if (sd->data_offset >= 64)
sd->state = sd_transfer_state;Given above reasons, let's drop it.
---
Changes in v2:
- Fix several typos in the commit message
hw/sd/sd.c | 18 ------------------ 1 file changed, 18 deletions(-)
hw/sd/sd.c +0 -18
@@ -271,23 +271,6 @@ static uint8_t sd_crc7(const void *message, size_t width)return shift_reg; } -static uint16_t sd_crc16 (const void *message,size_t width) -{ -int i, bit; -uint16_t shift_reg =0x0000 ; -const uint16_t *msg = (const uint16_t *)message; - width <<=1 ; - -for (i =0 ; i < width; i ++, msg ++) -for (bit =15 ; bit >=0 ; bit --) { - shift_reg <<=1 ; -if ((shift_reg >>15 ) ^ ((*msg >> bit) &1 )) - shift_reg ^=0x1011 ; - } - -return shift_reg; -} -#define OCR_POWER_DELAY_NS 500000/* 0.5ms */ FIELD (OCR ,VDD_VOLTAGE_WINDOW ,0 ,24 )
@@ -843,7 +826,6 @@ static void sd_function_switch(SDState *sd, uint32_t arg) sd->data[16 - (i >>1 )] |= new_func << ((i %2 ) *4 ); }memset (&sd->data[17 ],0 ,47 ); -stw_be_p (sd->data +64 ,sd_crc16 (sd->data,64 )); }static inline bool sd_wp_addr (SDState *sd,uint64_t addr)
Signature · 1 line
2.25.1
Re: [PATCH v2 05/25] hw/sd: sd: Drop sd_crc16()
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
commit
f6fb1f9b319f("sdcard: Correct CRC16 offset insd_function_switch()") changed the 16-bit CRC to be stored at offset 64. In fact, this CRC calculation is completely wrong. From the original codes, it wants to calculate the CRC16 of the first 64 bytes of sd->data[], however passing 64 as thewidthtosd_crc16()actually counts 256 bytes starting from themessagefor the CRC16 calculation, which is not what we want.Besides that, it seems existing
sd_crc16()algorithm does not match the SD spec, which says CRC16 is the CCITT one but the calculation does not produce expected result. It turns out the CRC16 was never transferred outside the sd core, as insd_read_byte()we see:if (sd->data_offset >= 64) sd->state = sd_transfer_state;Given above reasons, let's drop it.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Pragnesh Patel <[email protected]>Tested-by Pragnesh Patel <[email protected]>
Quoted 7 lines
---
Changes in v2:
- Fix several typos in the commit messagehw/sd/sd.c | 18 ------------------ 1 file changed, 18 deletions(-)
PATCH v2 6/25 util: Add CRC16 (CCITT) calculation routines
In reply to Bin MengFrom: Bin Meng <[email protected]>
Import CRC16 calculation routines from Linux kernel v5.10:
include/linux/crc-ccitt.h
lib/crc-ccitt.c
to QEMU:
include/qemu/crc-ccitt.h
util/crc-ccitt.c
---
(no changes since v1)
include/qemu/crc-ccitt.h | 33 ++++++++++ util/crc-ccitt.c | 127 +++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 161 insertions(+) create mode 100644 include/qemu/crc-ccitt.h create mode 100644 util/crc-ccitt.c
include/qemu/crc-ccitt.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* CRC16 (CCITT) Checksum Algorithm +* +* Copyright (c) 2021 Wind River Systems, Inc. +* +* Author: +* Bin Meng <[email protected]> +* +* From Linux kernel v5.10 include/linux/crc-ccitt.h +* +* SPDX-License-Identifier: GPL-2.0 +*/ + +#ifndef _CRC_CCITT_H +#define _CRC_CCITT_H + +extern uint16_t const crc_ccitt_table[256 ]; +extern uint16_t const crc_ccitt_false_table[256 ]; + +extern uint16_t crc_ccitt (uint16_t crc,const uint8_t *buffer,size_t len); +extern uint16_t crc_ccitt_false (uint16_t crc,const uint8_t *buffer,size_t len); + +static inline uint16_t crc_ccitt_byte (uint16_t crc,const uint8_t c) +{ +return (crc >>8 ) ^ crc_ccitt_table[(crc ^ c) &0xff ]; +} + +static inline uint16_t crc_ccitt_false_byte (uint16_t crc,const uint8_t c) +{ +return (crc <<8 ) ^ crc_ccitt_false_table[(crc >>8 ) ^ c]; +} + +#endif /* _CRC_CCITT_H */
util/crc-ccitt.c new file +127 -0
127 lines not on this page · raw message
util/meson.build +1 -0
@@ -29,6 +29,7 @@ util_ss.add(files('qemu-config.c', 'notify.c')) util_ss.add(files('qemu-option.c', 'qemu-progress.c')) util_ss.add(files('keyval.c')) util_ss.add(files('crc32c.c')) +util_ss.add(files('crc-ccitt.c')) util_ss.add(files('uuid.c')) util_ss.add(files('getauxval.c')) util_ss.add(files('rcu.c'))
Signature · 1 line
2.25.1
Re: [PATCH v2 06/25] util: Add CRC16 (CCITT) calculation routines
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
Import CRC16 calculation routines from Linux kernel v5.10:
include/linux/crc-ccitt.h
lib/crc-ccitt.cto QEMU:
include/qemu/crc-ccitt.h
util/crc-ccitt.cSigned-off-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>---
(no changes since v1)
include/qemu/crc-ccitt.h | 33 ++++++++++ util/crc-ccitt.c | 127 +++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 161 insertions(+) create mode 100644 include/qemu/crc-ccitt.h create mode 100644 util/crc-ccitt.c
...
util/meson.build util_ss.add(files('qemu-config.c', 'notify.c')) · line 32
Quoted 9 lines
util/meson.build +1
-0@@ -29,6 +29,7 @@ util_ss.add(files('qemu-config.c', 'notify.c')) util_ss.add(files('qemu-option.c', 'qemu-progress.c')) util_ss.add(files('keyval.c')) util_ss.add(files('crc32c.c')) +util_ss.add(files('crc-ccitt.c'))
OK but we can restrict this to system-mode emulation, as user-mode and tools don't require it.
Re: [PATCH v2 06/25] util: Add CRC16 (CCITT) calculation routines
Quoted 27 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
Import CRC16 calculation routines from Linux kernel v5.10:
include/linux/crc-ccitt.h
lib/crc-ccitt.cto QEMU:
include/qemu/crc-ccitt.h
util/crc-ccitt.cSigned-off-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>---
(no changes since v1)
include/qemu/crc-ccitt.h | 33 ++++++++++ util/crc-ccitt.c | 127 +++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 161 insertions(+) create mode 100644 include/qemu/crc-ccitt.h create mode 100644 util/crc-ccitt.c...
Quoted 12 lines
util/meson.build util_ss.add(files('qemu-config.c', 'notify.c')) · line 32
Quoted 9 lines
util/meson.build +1
-0@@ -29,6 +29,7 @@ util_ss.add(files('qemu-config.c', 'notify.c')) util_ss.add(files('qemu-option.c', 'qemu-progress.c')) util_ss.add(files('keyval.c')) util_ss.add(files('crc32c.c')) +util_ss.add(files('crc-ccitt.c'))OK but we can restrict this to system-mode emulation, as user-mode and tools don't require it.
Doesn't this get put in libutil, where it is only pulled from the archive when needed? Also, libutil is built once, not per-target.
r~
Re: [PATCH v2 06/25] util: Add CRC16 (CCITT) calculation routines
Quoted 41 lines · Richard Henderson
Quoted 27 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
Import CRC16 calculation routines from Linux kernel v5.10:
include/linux/crc-ccitt.h
lib/crc-ccitt.cto QEMU:
include/qemu/crc-ccitt.h
util/crc-ccitt.cSigned-off-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>---
(no changes since v1)
include/qemu/crc-ccitt.h | 33 ++++++++++ util/crc-ccitt.c | 127 +++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 161 insertions(+) create mode 100644 include/qemu/crc-ccitt.h create mode 100644 util/crc-ccitt.c...
Quoted 12 lines
util/meson.build util_ss.add(files('qemu-config.c', 'notify.c')) · line 32
Quoted 9 lines
util/meson.build +1
-0@@ -29,6 +29,7 @@ util_ss.add(files('qemu-config.c', 'notify.c')) util_ss.add(files('qemu-option.c', 'qemu-progress.c')) util_ss.add(files('keyval.c')) util_ss.add(files('crc32c.c')) +util_ss.add(files('crc-ccitt.c'))OK but we can restrict this to system-mode emulation, as user-mode and tools don't require it.
Quoted 2 lines
Doesn't this get put in libutil, where it is only pulled from the archive when needed? Also, libutil is built once, not per-target.
Hmm I just sent a pull request with this change squashed in. Should I cancel it?
My view is we don't install libqemuutil.a anywhere, so why overload building unused objects when some configuration don't need it?
Some of the configurations I test:
- build tools only
- build linux-user only
dbus.o and yank.o are also restricted to have_system. various objects are restricted to have_block (which is have_system or have_tools).
Re: [PATCH v2 06/25] util: Add CRC16 (CCITT) calculation routines
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Richard Henderson
Doesn't this get put in libutil, where it is only pulled from the archive when needed? Also, libutil is built once, not per-target.
Hmm I just sent a pull request with this change squashed in. Should I cancel it?
I guess not.
Quoted 2 lines
My view is we don't install libqemuutil.a anywhere, so why overload building unused objects when some configuration don't need it?
My view is that util/meson.build should not be overly complicated with conditionals.
If we were building objects per-target, that would be one thing. If we were doing --whole-archive, and including unused code in the executables, that would be another thing. But otherwise...
r~
Re: [PATCH v2 06/25] util: Add CRC16 (CCITT) calculation routines
Quoted 8 lines · Richard Henderson
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Richard Henderson
Doesn't this get put in libutil, where it is only pulled from the archive when needed? Also, libutil is built once, not per-target.
Hmm I just sent a pull request with this change squashed in. Should I cancel it?
Quoted 7 lines
I guess not.
Quoted 2 lines
My view is we don't install libqemuutil.a anywhere, so why overload building unused objects when some configuration don't need it?
My view is that util/meson.build should not be overly complicated with conditionals.
Well I do see an impact when building on slow hosts.
Quoted 3 lines
If we were building objects per-target, that would be one thing. If we were doing --whole-archive, and including unused code in the executables, that would be another thing. But otherwise...
I understand your position :)
Thanks,
Phil.
PATCH v2 7/25 hw/sd: ssi-sd: Suffix a data block with CRC16
In reply to Bin MengFrom: Bin Meng <[email protected]>
Per the SD spec, a valid data block is suffixed with a 16-bit CRC generated by the standard CCITT polynomial x16+x12+x5+1. This part is currently missing in the ssi-sd state machine. Without it, all data block transfer fails in guest software because the expected CRC16 is missing on the data out line.
---
(no changes since v1)
hw/sd/ssi-sd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
hw/sd/ssi-sd.c +17 -0
@@ -17,6 +17,7 @@#include "hw/qdev-properties.h" #include "hw/sd/sd.h" #include "qapi/error.h" +#include "qemu/crc-ccitt.h" #include "qemu/module.h" #include "qom/object.h"
@@ -40,6 +41,7 @@ typedef enum {SSI_SD_RESPONSE ,SSI_SD_DATA_START ,SSI_SD_DATA_READ , +SSI_SD_DATA_CRC16 , } ssi_sd_mode; struct ssi_sd_state {
@@ -48,6 +50,7 @@ struct ssi_sd_state {int cmd;uint8_t cmdarg[4 ];uint8_t response[5 ]; +uint16_t crc16;int32_t arglen;int32_t response_pos;int32_t stopping;
@@ -194,12 +197,24 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)case SSI_SD_DATA_START :DPRINTF ("Start read block\n" ); s->mode =SSI_SD_DATA_READ ; + s->response_pos =0 ;return 0xfe ;case SSI_SD_DATA_READ : val =sdbus_read_byte (&s->sdbus); + s->crc16 =crc_ccitt_false (s->crc16, (uint8_t *)&val,1 );if (!sdbus_data_ready (&s->sdbus)) {DPRINTF ("Data read end\n" ); + s->mode =SSI_SD_DATA_CRC16 ; + } +return val; +case SSI_SD_DATA_CRC16 : + val = (s->crc16 &0xff00 ) >>8 ; + s->crc16 <<=8 ; + s->response_pos++; +if (s->response_pos ==2 ) { +DPRINTF ("CRC16 read end\n" ); s->mode =SSI_SD_CMD ; + s->response_pos =0 ; }return val; }
@@ -237,6 +252,7 @@ static const VMStateDescription vmstate_ssi_sd = {VMSTATE_INT32 (cmd, ssi_sd_state),VMSTATE_UINT8_ARRAY (cmdarg, ssi_sd_state,4 ),VMSTATE_UINT8_ARRAY (response, ssi_sd_state,5 ), +VMSTATE_UINT16 (crc16, ssi_sd_state),VMSTATE_INT32 (arglen, ssi_sd_state),VMSTATE_INT32 (response_pos, ssi_sd_state), VMSTATE_INT32(stopping, ssi_sd_state),
@@ -288,6 +304,7 @@ static void ssi_sd_reset(DeviceState *dev) s->cmd =0 ;memset (s->cmdarg,0 ,sizeof (s->cmdarg));memset (s->response,0 ,sizeof (s->response)); + s->crc16 =0 ; s->arglen =0 ; s->response_pos =0 ; s->stopping =0 ;
Signature · 1 line
2.25.1
Re: [PATCH v2 07/25] hw/sd: ssi-sd: Suffix a data block with CRC16
Quoted 11 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the SD spec, a valid data block is suffixed with a 16-bit CRC generated by the standard CCITT polynomial x16+x12+x5+1. This part is currently missing in the ssi-sd state machine. Without it, all data block transfer fails in guest software because the expected CRC16 is missing on the data out line.
Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>
Quoted 6 lines
---
(no changes since v1)
hw/sd/ssi-sd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
Bumping migration version:
PATCH v2 8/25 hw/sd: ssi-sd: Add a state representing Nac
In reply to Bin MengFrom: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00" chapter 7.5.2, "Data Read", there is a minimum 8 clock cycles (Nac) after the card response and before data block shows up on the data out line. This applies to both single and multiple block read operations.
Current implementation of single block read already satisfies the timing requirement as in the RESPONSE state after all responses are transferred the state remains unchanged. In the next 8 clock cycles it jumps to DATA_START state if data is ready.
However we need an explicit state when expanding our support to multiple block read in the future. Let's add a new state PREP_DATA explicitly in the ssi-sd state machine to represent Nac.
Note we don't change the single block read state machine to let it jump from RESPONSE state to DATA_START state as that effectively generates a 16 clock cycles Nac, which might not be safe. As the spec says the maximum Nac shall be calculated from several fields encoded in the CSD register, we don't want to bother updating CSD to ensure our Nac is within range to complicate things.
---
Changes in v2:
- new patch: add a state representing Nac
hw/sd/ssi-sd.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/sd/ssi-sd.c +5 -0
@@ -39,6 +39,7 @@ typedef enum {SSI_SD_CMDARG ,SSI_SD_PREP_RESP ,SSI_SD_RESPONSE , +SSI_SD_PREP_DATA ,SSI_SD_DATA_START ,SSI_SD_DATA_READ , SSI_SD_DATA_CRC16,
@@ -194,6 +195,10 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->mode =SSI_SD_CMD ; }return 0xff ; +case SSI_SD_PREP_DATA : +DPRINTF ("Prepare data block (Nac)\n" ); + s->mode =SSI_SD_DATA_START ; +return 0xff ;case SSI_SD_DATA_START :DPRINTF ("Start read block\n" ); s->mode =SSI_SD_DATA_READ ;
Signature · 1 line
2.25.1
Re: [PATCH v2 08/25] hw/sd: ssi-sd: Add a state representing Nac
Quoted 16 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00" chapter 7.5.2, "Data Read", there is a minimum 8 clock cycles (Nac) after the card response and before data block shows up on the data out line. This applies to both single and multiple block read operations.
Current implementation of single block read already satisfies the timing requirement as in the RESPONSE state after all responses are transferred the state remains unchanged. In the next 8 clock cycles it jumps to
DATA_STARTstate if data is ready.However we need an explicit state when expanding our support to multiple block read in the future. Let's add a new state
PREP_DATAexplicitly in the ssi-sd state machine to represent Nac.
Quoted 6 lines
Note we don't change the single block read state machine to let it jump from RESPONSE state to
DATA_STARTstate as that effectively generates a 16 clock cycles Nac, which might not be safe. As the spec says the maximum Nac shall be calculated from several fields encoded in the CSD register, we don't want to bother updating CSD to ensure our Nac is within range to complicate things.
As I don't have access to that part of the spec, I'm going to trust you.
Quoted 10 lines
Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- new patch: add a state representing Nachw/sd/ssi-sd.c | 5 +++++ 1 file changed, 5 insertions(+)
Re: [PATCH v2 08/25] hw/sd: ssi-sd: Add a state representing Nac
In reply to Bin MengQuoted 38 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00" chapter 7.5.2, "Data Read", there is a minimum 8 clock cycles (Nac) after the card response and before data block shows up on the data out line. This applies to both single and multiple block read operations.
Current implementation of single block read already satisfies the timing requirement as in the RESPONSE state after all responses are transferred the state remains unchanged. In the next 8 clock cycles it jumps to
DATA_STARTstate if data is ready.However we need an explicit state when expanding our support to multiple block read in the future. Let's add a new state
PREP_DATAexplicitly in the ssi-sd state machine to represent Nac.Note we don't change the single block read state machine to let it jump from RESPONSE state to
DATA_STARTstate as that effectively generates a 16 clock cycles Nac, which might not be safe. As the spec says the maximum Nac shall be calculated from several fields encoded in the CSD register, we don't want to bother updating CSD to ensure our Nac is within range to complicate things.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- new patch: add a state representing Nachw/sd/ssi-sd.c | 5 +++++ 1 file changed, 5 insertions(+)hw/sd/ssi-sd.c +0
-0
hw/sd/ssi-sd.c typedef enum { · line 42
Quoted 4 lines
hw/sd/ssi-sd.c +1
-0@@ -39,6 +39,7 @@ typedef enum {SSI_SD_CMDARG ,SSI_SD_PREP_RESP ,SSI_SD_RESPONSE , + SSI_SD_PREP_DATA,
Hmm yet another change breaking migration.
Quoted 3 lines
hw/sd/ssi-sd.c +0
-0SSI_SD_DATA_START ,SSI_SD_DATA_READ , SSI_SD_DATA_CRC16,
PATCH v2 9/25 hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION
In reply to Bin MengFrom: Bin Meng <[email protected]>
This fixes the wrong command index for STOP_TRANSMISSION, the required command to interrupt the multiple block read command, in the old codes. It should be CMD12 (0x4c), not CMD13 (0x4d).
---
Changes in v2:
- Make this fix a separate patch from the CMD18 support
hw/sd/ssi-sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/ssi-sd.c +1 -1
@@ -83,7 +83,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)ssi_sd_state *s =SSI_SD (dev);/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4d ) { +if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) { s->mode =SSI_SD_CMD ;/* There must be at least one byte delay before the card responds. */ s->stopping =1 ;
Signature · 1 line
2.25.1
Re: [PATCH v2 09/25] hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION
Quoted 22 lines · Bin Meng
From: Bin Meng <[email protected]>
This fixes the wrong command index for
STOP_TRANSMISSION, the required command to interrupt the multiple block read command, in the old codes. It should be CMD12 (0x4c), not CMD13 (0x4d).Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Make this fix a separate patch from the CMD18 supporthw/sd/ssi-sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/sd/ssi-sd.c +0
-0
hw/sd/ssi-sd.c static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) · line 86
Quoted 5 lines
hw/sd/ssi-sd.c +1
-1@@ -83,7 +83,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)ssi_sd_state *s =SSI_SD (dev);/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4d ) { +if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) {
Patch is correct, but I wonder if we couldn't improve using instead:
if (s->mode == SSI_SD_DATA_READ && ((val & 0x3f) == 12)) {Quoted 4 lines
hw/sd/ssi-sd.c +0
-0s->mode =SSI_SD_CMD ;/* There must be at least one byte delay before the card responds. */ s->stopping =1 ;
Philippe Mathieu-Daudé
Reviewed-byRe: [PATCH v2 09/25] hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
This fixes the wrong command index for
STOP_TRANSMISSION, the required command to interrupt the multiple block read command, in the old codes. It should be CMD12 (0x4c), not CMD13 (0x4d).Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Make this fix a separate patch from the CMD18 supporthw/sd/ssi-sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/sd/ssi-sd.c +0
-0@@ -83,7 +83,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)ssi_sd_state *s =SSI_SD (dev);
Quoted 5 lines
hw/sd/ssi-sd.c static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) · line 86
Quoted 3 lines
hw/sd/ssi-sd.c +1
-1/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4d ) { +if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) {Patch is correct,
Forgot to amend:
Quoted 9 lines
but I wonder if we couldn't improve using instead:
if (s->mode == SSI_SD_DATA_READ && ((val & 0x3f) == 12)) {Quoted 4 lines
hw/sd/ssi-sd.c +0
-0s->mode =SSI_SD_CMD ;/* There must be at least one byte delay before the card responds. */ s->stopping =1 ;
Re: [PATCH v2 09/25] hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION
In reply to Philippe Mathieu-DaudéQuoted 26 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Bin Meng
From: Bin Meng <[email protected]>
This fixes the wrong command index for
STOP_TRANSMISSION, the required command to interrupt the multiple block read command, in the old codes. It should be CMD12 (0x4c), not CMD13 (0x4d).Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Make this fix a separate patch from the CMD18 supporthw/sd/ssi-sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/sd/ssi-sd.c +0
-0@@ -83,7 +83,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)ssi_sd_state *s =SSI_SD (dev);
Quoted 8 lines
hw/sd/ssi-sd.c static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) · line 86
Quoted 3 lines
hw/sd/ssi-sd.c +1
-1/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4d ) { +if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) {Patch is correct, but I wonder if we couldn't improve using instead:
if (s->mode == SSI_SD_DATA_READ && ((val & 0x3f) == 12)) {
(val & 0x3f == 12) isn't correct because it can catch values equal to 0x7c or 0xfc.
Regards,
Bin
Re: [PATCH v2 09/25] hw/sd: ssi-sd: Fix the wrong command index for STOP_TRANSMISSION
Quoted 34 lines · Bin Meng
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Bin Meng
From: Bin Meng <[email protected]>
This fixes the wrong command index for
STOP_TRANSMISSION, the required command to interrupt the multiple block read command, in the old codes. It should be CMD12 (0x4c), not CMD13 (0x4d).Fixes 775616c3ae8c ("Partial SD card SPI mode support")Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- Make this fix a separate patch from the CMD18 supporthw/sd/ssi-sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/sd/ssi-sd.c +0
-0hw/sd/ssi-sd.c static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) · line 86
Quoted 5 lines
hw/sd/ssi-sd.c +1
-1@@ -83,7 +83,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)ssi_sd_state *s =SSI_SD (dev);/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4d ) { +if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) {Patch is correct, but I wonder if we couldn't improve using instead:
Quoted 5 lines
Quoted 2 lines
if (s->mode == SSI_SD_DATA_READ && ((val & 0x3f) == 12)) {(val &
0x3f== 12) isn't correct because it can catch values equal to0x7cor0xfc.
Sorry, wrong calculation. It should read:
It can catch values 0x0c, 0x8c, 0xcc.
Regards,
Bin
PATCH v2 10/25 hw/sd: ssi-sd: Support multiple block read
In reply to Bin MengFrom: Bin Meng <[email protected]>
In the case of a multiple block read operation every transferred block has its suffix of CRC16. Update the state machine logic to handle multiple block read.
---
Changes in v2:
- Fix 2 typos in the commit message
- Add a comment block to explain the CMD12 timing
- Catch CMD12 in all of the data read states per the timing requirement
hw/sd/ssi-sd.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)
hw/sd/ssi-sd.c +31 -7
@@ -52,6 +52,7 @@ struct ssi_sd_state {uint8_t cmdarg[4 ];uint8_t response[5 ];uint16_t crc16; +int32_t read_bytes;int32_t arglen;int32_t response_pos;int32_t stopping;
@@ -82,11 +83,26 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) {ssi_sd_state *s =SSI_SD (dev); -/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ -if (s->mode ==SSI_SD_DATA_READ && val ==0x4c ) { - s->mode =SSI_SD_CMD ; -/* There must be at least one byte delay before the card responds. */ - s->stopping =1 ; +/* +* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. +* +* See "Physical Layer Specification Version 8.00" chapter 7.5.2.2, +* to avoid conflict between CMD12 response and next data block, +* timing of CMD12 should be controlled as follows: +* +* - CMD12 issued at the timing that end bit of CMD12 and end bit of +* data block is overlapped +* - CMD12 issued after one clock cycle after host receives a token +* (either Start Block token or Data Error token) +* +* We need to catch CMD12 in all of the data read states. +*/ +if (s->mode >=SSI_SD_PREP_DATA && s->mode <=SSI_SD_DATA_CRC16 ) { +if (val ==0x4c ) { + s->mode =SSI_SD_CMD ; +/* There must be at least one byte delay before the card responds */ + s->stopping =1 ; + } }switch (s->mode) {
@@ -206,8 +222,9 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return 0xfe ;case SSI_SD_DATA_READ : val =sdbus_read_byte (&s->sdbus); + s->read_bytes++; s->crc16 =crc_ccitt_false (s->crc16, (uint8_t *)&val,1 ); -if (!sdbus_data_ready (&s->sdbus)) { +if (!sdbus_data_ready (&s->sdbus) || s->read_bytes ==512 ) {DPRINTF ("Data read end\n" ); s->mode =SSI_SD_DATA_CRC16 ; }
@@ -218,7 +235,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->response_pos++;if (s->response_pos ==2 ) {DPRINTF ("CRC16 read end\n" ); - s->mode =SSI_SD_CMD ; +if (s->read_bytes ==512 && s->cmd !=17 ) { + s->mode =SSI_SD_PREP_DATA ; + }else { + s->mode =SSI_SD_CMD ; + } + s->read_bytes =0 ; s->response_pos =0 ; }return val;
@@ -258,6 +280,7 @@ static const VMStateDescription vmstate_ssi_sd = {VMSTATE_UINT8_ARRAY (cmdarg, ssi_sd_state,4 ),VMSTATE_UINT8_ARRAY (response, ssi_sd_state,5 ),VMSTATE_UINT16 (crc16, ssi_sd_state), +VMSTATE_INT32 (read_bytes, ssi_sd_state),VMSTATE_INT32 (arglen, ssi_sd_state),VMSTATE_INT32 (response_pos, ssi_sd_state), VMSTATE_INT32(stopping, ssi_sd_state),
@@ -310,6 +333,7 @@ static void ssi_sd_reset(DeviceState *dev)memset (s->cmdarg,0 ,sizeof (s->cmdarg));memset (s->response,0 ,sizeof (s->response)); s->crc16 =0 ; + s->read_bytes =0 ; s->arglen =0 ; s->response_pos =0 ; s->stopping =0 ;
Signature · 1 line
2.25.1
PATCH v2 11/25 hw/sd: ssi-sd: Use macros for the dummy value and tokens in the transfer
In reply to Bin MengFrom: Bin Meng <[email protected]>
At present the codes use hardcoded numbers (0xff/0xfe) for the dummy value and block start token. Replace them with macros.
---
Changes in v2:
- Move multiple write token definitions out of this patch
hw/sd/ssi-sd.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
hw/sd/ssi-sd.c +16 -10
@@ -79,6 +79,12 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)#define SSI_SDR_ADDRESS_ERROR 0x2000#define SSI_SDR_PARAMETER_ERROR 0x4000 +/* single block read/write, multiple block read */ +#define SSI_TOKEN_SINGLE 0xfe + +/* dummy value - don't care */ +#define SSI_DUMMY 0xff +static uint32_t ssi_sd_transfer (SSIPeripheral *dev,uint32_t val) {ssi_sd_state *s =SSI_SD (dev);
@@ -107,14 +113,14 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)switch (s->mode) {case SSI_SD_CMD : -if (val ==0xff ) { +if (val ==SSI_DUMMY ) {DPRINTF ("NULL command\n" ); -return 0xff ; +return SSI_DUMMY ; } s->cmd = val &0x3f ; s->mode =SSI_SD_CMDARG ; s->arglen =0 ; -return 0xff ; +return SSI_DUMMY ;case SSI_SD_CMDARG :if (s->arglen ==4 ) {SDRequest request;
@@ -189,15 +195,15 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) }else { s->cmdarg[s->arglen++] = val; } -return 0xff ; +return SSI_DUMMY ;case SSI_SD_PREP_RESP :DPRINTF ("Prepare card response (Ncr)\n" ); s->mode =SSI_SD_RESPONSE ; -return 0xff ; +return SSI_DUMMY ;case SSI_SD_RESPONSE :if (s->stopping) { s->stopping =0 ; -return 0xff ; +return SSI_DUMMY ; }if (s->response_pos < s->arglen) {DPRINTF ("Response 0x%02x\n" , s->response[s->response_pos]);
@@ -210,16 +216,16 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)DPRINTF ("End of command\n" ); s->mode =SSI_SD_CMD ; } -return 0xff ; +return SSI_DUMMY ;case SSI_SD_PREP_DATA :DPRINTF ("Prepare data block (Nac)\n" ); s->mode =SSI_SD_DATA_START ; -return 0xff ; +return SSI_DUMMY ;case SSI_SD_DATA_START :DPRINTF ("Start read block\n" ); s->mode =SSI_SD_DATA_READ ; s->response_pos =0 ; -return 0xfe ; +return SSI_TOKEN_SINGLE ;case SSI_SD_DATA_READ : val =sdbus_read_byte (&s->sdbus); s->read_bytes++;
@@ -246,7 +252,7 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return val; }/* Should never happen. */ -return 0xff ; +return SSI_DUMMY ; }static int ssi_sd_post_load (void *opaque,int version_id)
Signature · 1 line
2.25.1
Re: [PATCH v2 11/25] hw/sd: ssi-sd: Use macros for the dummy value and tokens in the transfer
Quoted 8 lines · Bin Meng
From: Bin Meng <[email protected]>
At present the codes use hardcoded numbers (0xff/0xfe) for the dummy value and block start token. Replace them with macros.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>
Quoted 7 lines
---
Changes in v2:
- Move multiple write token definitions out of this patchhw/sd/ssi-sd.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
Again:
https://www.mail-archive.com/[email protected]/msg78880.html
PATCH v2 12/25 hw/sd: sd: Remove duplicated codes in single/multiple block read/write
In reply to Bin MengFrom: Bin Meng <[email protected]>
The single block read (CMD17) codes are the same as the multiple block read (CMD18). Merge them into one. The same applies to single block write (CMD24) and multiple block write (CMD25).
---
(no changes since v1)
hw/sd/sd.c | 47 ----------------------------------------------- 1 file changed, 47 deletions(-)
hw/sd/sd.c +0 -47
@@ -1181,24 +1181,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)break ;case 17 :/* CMD17: READ_SINGLE_BLOCK */ -switch (sd->state) { -case sd_transfer_state: - -if (addr + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; -return sd_r1; - } - - sd->state = sd_sendingdata_state; - sd->data_start = addr; - sd->data_offset =0 ; -return sd_r1; - -default : -break ; - } -break ; -case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state:
@@ -1245,35 +1227,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)/* Block write commands (Class 4) */ case 24 :/* CMD24: WRITE_SINGLE_BLOCK */ -switch (sd->state) { -case sd_transfer_state: -/* Writing in SPI mode not implemented. */ -if (sd->spi) -break ; - -if (addr + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; -return sd_r1; - } - - sd->state = sd_receivingdata_state; - sd->data_start = addr; - sd->data_offset =0 ; - sd->blk_written =0 ; - -if (sd_wp_addr (sd, sd->data_start)) { - sd->card_status |=WP_VIOLATION ; - } -if (sd->csd[14 ] &0x30 ) { - sd->card_status |=WP_VIOLATION ; - } -return sd_r1; - -default : -break ; - } -break ; -case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state:
Signature · 1 line
2.25.1
PATCH v2 13/25 hw/sd: sd: Allow single/multiple block write for SPI mode
In reply to Bin MengFrom: Bin Meng <[email protected]>
At present the single/multiple block write in SPI mode is blocked by sd_normal_command(). Remove the limitation.
---
(no changes since v1)
hw/sd/sd.c | 3 --- 1 file changed, 3 deletions(-)
hw/sd/sd.c +0 -3
@@ -1230,9 +1230,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state: -/* Writing in SPI mode not implemented. */ -if (sd->spi) -break ;if (addr + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ;
Signature · 1 line
2.25.1
PATCH v2 14/25 hw/sd: sd.h: Cosmetic change of using spaces
In reply to Bin MengFrom: Bin Meng <[email protected]>
QEMU coding convention prefers spaces over tabs.
---
Changes in v2:
- Correct the "coding" typo in the commit message
include/hw/sd/sd.h | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
include/hw/sd/sd.h +21 -21
@@ -33,27 +33,27 @@#include "hw/qdev-core.h" #include "qom/object.h" -#define OUT_OF_RANGE (1 << 31) -#define ADDRESS_ERROR (1 << 30) -#define BLOCK_LEN_ERROR (1 << 29) -#define ERASE_SEQ_ERROR (1 << 28) -#define ERASE_PARAM (1 << 27) -#define WP_VIOLATION (1 << 26) -#define CARD_IS_LOCKED (1 << 25) -#define LOCK_UNLOCK_FAILED (1 << 24) -#define COM_CRC_ERROR (1 << 23) -#define ILLEGAL_COMMAND (1 << 22) -#define CARD_ECC_FAILED (1 << 21) -#define CC_ERROR (1 << 20) -#define SD_ERROR (1 << 19) -#define CID_CSD_OVERWRITE (1 << 16) -#define WP_ERASE_SKIP (1 << 15) -#define CARD_ECC_DISABLED (1 << 14) -#define ERASE_RESET (1 << 13) -#define CURRENT_STATE (7 << 9) -#define READY_FOR_DATA (1 << 8) -#define APP_CMD (1 << 5) -#define AKE_SEQ_ERROR (1 << 3) +#define OUT_OF_RANGE (1 << 31) +#define ADDRESS_ERROR (1 << 30) +#define BLOCK_LEN_ERROR (1 << 29) +#define ERASE_SEQ_ERROR (1 << 28) +#define ERASE_PARAM (1 << 27) +#define WP_VIOLATION (1 << 26) +#define CARD_IS_LOCKED (1 << 25) +#define LOCK_UNLOCK_FAILED (1 << 24) +#define COM_CRC_ERROR (1 << 23) +#define ILLEGAL_COMMAND (1 << 22) +#define CARD_ECC_FAILED (1 << 21) +#define CC_ERROR (1 << 20) +#define SD_ERROR (1 << 19) +#define CID_CSD_OVERWRITE (1 << 16) +#define WP_ERASE_SKIP (1 << 15) +#define CARD_ECC_DISABLED (1 << 14) +#define ERASE_RESET (1 << 13) +#define CURRENT_STATE (7 << 9) +#define READY_FOR_DATA (1 << 8) +#define APP_CMD (1 << 5) +#define AKE_SEQ_ERROR (1 << 3)enum SDPhySpecificationVersion { SD_PHY_SPECv1_10_VERS =1 ,
Signature · 1 line
2.25.1
Re: [PATCH v2 14/25] hw/sd: sd.h: Cosmetic change of using spaces
Quoted 55 lines · Bin Meng
From: Bin Meng <[email protected]>
QEMU coding convention prefers spaces over tabs.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>---
Changes in v2:
- Correct the "coding" typo in the commit messageinclude/hw/sd/sd.h | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)include/hw/sd/sd.h +11
-21@@ -33,27 +33,27 @@#include "hw/qdev-core.h" #include "qom/object.h" -#define OUT_OF_RANGE (1 << 31) -#define ADDRESS_ERROR (1 << 30) -#define BLOCK_LEN_ERROR (1 << 29) -#define ERASE_SEQ_ERROR (1 << 28) -#define ERASE_PARAM (1 << 27) -#define WP_VIOLATION (1 << 26) -#define CARD_IS_LOCKED (1 << 25) -#define LOCK_UNLOCK_FAILED (1 << 24) -#define COM_CRC_ERROR (1 << 23) -#define ILLEGAL_COMMAND (1 << 22) -#define CARD_ECC_FAILED (1 << 21) -#define CC_ERROR (1 << 20) -#define SD_ERROR (1 << 19) -#define CID_CSD_OVERWRITE (1 << 16) -#define WP_ERASE_SKIP (1 << 15) -#define CARD_ECC_DISABLED (1 << 14) -#define ERASE_RESET (1 << 13) -#define CURRENT_STATE (7 << 9) -#define READY_FOR_DATA (1 << 8) -#define APP_CMD (1 << 5) -#define AKE_SEQ_ERROR (1 << 3) +#define OUT_OF_RANGE (1 << 31) +#define ADDRESS_ERROR (1 << 30) +#define BLOCK_LEN_ERROR (1 << 29) +#define ERASE_SEQ_ERROR (1 << 28) +#define ERASE_PARAM (1 << 27) +#define WP_VIOLATION (1 << 26) +#define CARD_IS_LOCKED (1 << 25) +#define LOCK_UNLOCK_FAILED (1 << 24) +#define COM_CRC_ERROR (1 << 23) +#define ILLEGAL_COMMAND (1 << 22) +#define CARD_ECC_FAILED (1 << 21)
include/hw/sd/sd.h line 56
Quoted 10 lines
include/hw/sd/sd.h +10
-0+#define CC_ERROR (1 << 20) +#define SD_ERROR (1 << 19) +#define CID_CSD_OVERWRITE (1 << 16) +#define WP_ERASE_SKIP (1 << 15) +#define CARD_ECC_DISABLED (1 << 14) +#define ERASE_RESET (1 << 13) +#define CURRENT_STATE (7 << 9) +#define READY_FOR_DATA (1 << 8) +#define APP_CMD (1 << 5) +#define AKE_SEQ_ERROR (1 << 3)
The plan was to use the REGISTERFIELD definitions
we already have in sd.c and simply remove these:
FIELD(CSR, AKE_SEQ_ERROR, 3, 1) FIELD(CSR, APP_CMD, 5, 1) FIELD(CSR, FX_EVENT, 6, 1) FIELD(CSR, READY_FOR_DATA, 8, 1) FIELD(CSR, CURRENT_STATE, 9, 4) FIELD(CSR, ERASE_RESET, 13, 1) FIELD(CSR, CARD_ECC_DISABLED, 14, 1) FIELD(CSR, WP_ERASE_SKIP, 15, 1) FIELD(CSR, CSD_OVERWRITE, 16, 1) FIELD(CSR, DEFERRED_RESPONSE, 17, 1) FIELD(CSR, ERROR, 19, 1) FIELD(CSR, CC_ERROR, 20, 1) FIELD(CSR, CARD_ECC_FAILED, 21, 1) FIELD(CSR, ILLEGAL_COMMAND, 22, 1) FIELD(CSR, COM_CRC_ERROR, 23, 1) FIELD(CSR, LOCK_UNLOCK_FAILED, 24, 1) FIELD(CSR, CARD_IS_LOCKED, 25, 1) FIELD(CSR, WP_VIOLATION, 26, 1) FIELD(CSR, ERASE_PARAM, 27, 1) FIELD(CSR, ERASE_SEQ_ERROR, 28, 1) FIELD(CSR, BLOCK_LEN_ERROR, 29, 1) FIELD(CSR, ADDRESS_ERROR, 30, 1) FIELD(CSR, OUT_OF_RANGE, 31, 1)
Anyway, we never got there...
PATCH v2 15/25 hw/sd: Introduce receive_ready() callback
In reply to Bin MengFrom: Bin Meng <[email protected]>
At present there is a data_ready() callback for the SD data read path. Let's add a receive_ready() for the SD data write path.
---
(no changes since v1)
include/hw/sd/sd.h | 2 ++ hw/sd/core.c | 13 +++++++++++++ hw/sd/sd.c | 6 ++++++ 3 files changed, 21 insertions(+)
include/hw/sd/sd.h +2 -0
@@ -116,6 +116,7 @@ struct SDCardClass {* Return: byte value read */ uint8_t (*read_byte)(SDState *sd); +bool (*receive_ready)(SDState *sd);bool (*data_ready)(SDState *sd);void (*set_voltage)(SDState *sd,uint16_t millivolts);uint8_t (*get_dat_lines)(SDState *sd);
@@ -187,6 +188,7 @@ void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length);* Read multiple bytes of data on the data lines of a SD bus. */ void sdbus_read_data (SDBus *sdbus,void *buf,size_t length); +bool sdbus_receive_ready (SDBus *sd);bool sdbus_data_ready (SDBus *sd);bool sdbus_get_inserted (SDBus *sd);bool sdbus_get_readonly (SDBus *sd);
hw/sd/core.c +13 -0
@@ -160,6 +160,19 @@ void sdbus_read_data(SDBus *sdbus, void *buf, size_t length) } } +bool sdbus_receive_ready (SDBus *sdbus) +{ +SDState *card =get_card (sdbus); + +if (card) { +SDCardClass *sc =SD_CARD_GET_CLASS (card); + +return sc->receive_ready (card); + } + +return false; +} +bool sdbus_data_ready (SDBus *sdbus) {SDState *card =get_card (sdbus);
hw/sd/sd.c +6 -0
@@ -2037,6 +2037,11 @@ uint8_t sd_read_byte(SDState *sd)return ret; } +static bool sd_receive_ready (SDState *sd) +{ +return sd->state == sd_receivingdata_state; +} +static bool sd_data_ready (SDState *sd) {return sd->state == sd_sendingdata_state;
@@ -2147,6 +2152,7 @@ static void sd_class_init(ObjectClass *klass, void *data) sc->do_command = sd_do_command; sc->write_byte = sd_write_byte; sc->read_byte = sd_read_byte; + sc->receive_ready = sd_receive_ready; sc->data_ready = sd_data_ready; sc->enable = sd_enable; sc->get_inserted = sd_get_inserted;
Signature · 1 line
2.25.1
PATCH v2 16/25 hw/sd: ssi-sd: Support single block write
In reply to Bin MengFrom: Bin Meng <[email protected]>
Add 2 more states for the block write operation. The SPI host needs to send a data start token to start the transfer, and the data block written to the card will be acknowledged by a data response token.
---
Changes in v2:
- Correct the "token" typo in the commit message
- Add 'write_bytes' in vmstate_ssi_sd
hw/sd/ssi-sd.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
hw/sd/ssi-sd.c +37 -1
@@ -43,6 +43,8 @@ typedef enum {SSI_SD_DATA_START ,SSI_SD_DATA_READ ,SSI_SD_DATA_CRC16 , +SSI_SD_DATA_WRITE , +SSI_SD_SKIP_CRC16 , } ssi_sd_mode; struct ssi_sd_state {
@@ -53,6 +55,7 @@ struct ssi_sd_state {uint8_t response[5 ];uint16_t crc16;int32_t read_bytes; +int32_t write_bytes;int32_t arglen;int32_t response_pos;int32_t stopping;
@@ -85,6 +88,9 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)/* dummy value - don't care */ #define SSI_DUMMY 0xff +/* data accepted */ +#define DATA_RESPONSE_ACCEPTED 0x05 +static uint32_t ssi_sd_transfer (SSIPeripheral *dev,uint32_t val) {ssi_sd_state *s =SSI_SD (dev);
@@ -113,10 +119,17 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)switch (s->mode) {case SSI_SD_CMD : -if (val ==SSI_DUMMY ) { +switch (val) { +case SSI_DUMMY :DPRINTF ("NULL command\n" );return SSI_DUMMY ; +break ; +case SSI_TOKEN_SINGLE : +DPRINTF ("Start write block\n" ); + s->mode =SSI_SD_DATA_WRITE ; +return SSI_DUMMY ; } + s->cmd = val &0x3f ; s->mode =SSI_SD_CMDARG ; s->arglen =0 ;
@@ -250,6 +263,27 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->response_pos =0 ; }return val; +case SSI_SD_DATA_WRITE : +sdbus_write_byte (&s->sdbus, val); + s->write_bytes++; +if (!sdbus_receive_ready (&s->sdbus) || s->write_bytes ==512 ) { +DPRINTF ("Data write end\n" ); + s->mode =SSI_SD_SKIP_CRC16 ; + s->response_pos =0 ; + } +return val; +case SSI_SD_SKIP_CRC16 : +/* we don't verify the crc16 */ + s->response_pos++; +if (s->response_pos ==2 ) { +DPRINTF ("CRC16 receive end\n" ); + s->mode =SSI_SD_RESPONSE ; + s->write_bytes =0 ; + s->arglen =1 ; + s->response[0 ] =DATA_RESPONSE_ACCEPTED ; + s->response_pos =0 ; + } +return SSI_DUMMY ; }/* Should never happen. */ return SSI_DUMMY ;
@@ -287,6 +321,7 @@ static const VMStateDescription vmstate_ssi_sd = {VMSTATE_UINT8_ARRAY (response, ssi_sd_state,5 ),VMSTATE_UINT16 (crc16, ssi_sd_state),VMSTATE_INT32 (read_bytes, ssi_sd_state), +VMSTATE_INT32 (write_bytes, ssi_sd_state),VMSTATE_INT32 (arglen, ssi_sd_state),VMSTATE_INT32 (response_pos, ssi_sd_state), VMSTATE_INT32(stopping, ssi_sd_state),
@@ -340,6 +375,7 @@ static void ssi_sd_reset(DeviceState *dev)memset (s->response,0 ,sizeof (s->response)); s->crc16 =0 ; s->read_bytes =0 ; + s->write_bytes =0 ; s->arglen =0 ; s->response_pos =0 ; s->stopping =0 ;
Signature · 1 line
2.25.1
PATCH v2 17/25 hw/sd: ssi-sd: Support multiple block write
In reply to Bin MengFrom: Bin Meng <[email protected]>
For a multiple block write operation, each block begins with a multi write start token. Unlike the SD mode that the multiple block write ends when receiving a STOP_TRAN command (CMD12), a special stop tran token is used to signal the card.
Emulating this by manually sending a CMD12 to the SD card core, to bring it out of the receiving data state.
---
Changes in v2:
- Correct the "token" typo in the commit message
- Introduce multiple write token definitions in this patch
hw/sd/ssi-sd.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
hw/sd/ssi-sd.c +28 -2
@@ -82,6 +82,10 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)#define SSI_SDR_ADDRESS_ERROR 0x2000#define SSI_SDR_PARAMETER_ERROR 0x4000 +/* multiple block write */ +#define SSI_TOKEN_MULTI_WRITE 0xfc +/* terminate multiple block write */ +#define SSI_TOKEN_STOP_TRAN 0xfd/* single block read/write, multiple block read */ #define SSI_TOKEN_SINGLE 0xfe
@@ -94,6 +98,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)static uint32_t ssi_sd_transfer (SSIPeripheral *dev,uint32_t val) {ssi_sd_state *s =SSI_SD (dev); +SDRequest request; +uint8_t longresp[16 ];/* * Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
@@ -125,9 +131,31 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return SSI_DUMMY ;break ;case SSI_TOKEN_SINGLE : +case SSI_TOKEN_MULTI_WRITE :DPRINTF ("Start write block\n" ); s->mode =SSI_SD_DATA_WRITE ;return SSI_DUMMY ; +case SSI_TOKEN_STOP_TRAN : +DPRINTF ("Stop multiple write\n" ); + +/* manually issue cmd12 to stop the transfer */ + request.cmd =12 ; + request.arg =0 ; + s->arglen =sdbus_do_command (&s->sdbus, &request, longresp); +if (s->arglen <=0 ) { + s->arglen =1 ; +/* a zero value indicates the card is busy */ + s->response[0 ] =0 ; +DPRINTF ("SD card busy\n" ); + }else { + s->arglen =1 ; +/* a non-zero value indicates the card is ready */ + s->response[0 ] =SSI_DUMMY ; + } + + s->mode =SSI_SD_RESPONSE ; + s->response_pos =0 ; +return SSI_DUMMY ; } s->cmd = val &0x3f ;
@@ -136,8 +164,6 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return SSI_DUMMY ;case SSI_SD_CMDARG :if (s->arglen ==4 ) { -SDRequest request; -uint8_t longresp[16 ];/* FIXME: Check CRC. */ request.cmd = s->cmd; request.arg =ldl_be_p (s->cmdarg);
Signature · 1 line
2.25.1
PATCH v2 18/25 hw/sd: ssi-sd: Bump up version ids of VMStateDescription
In reply to Bin MengFrom: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
The s->mode check in the post_load() hook is also updated.
---
Changes in v2:
- new patch: bump up version ids of VMStateDescription
hw/sd/ssi-sd.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
hw/sd/ssi-sd.c +8 -3
@@ -4,6 +4,11 @@* Copyright (c) 2007-2009 CodeSourcery. * Written by Paul Brook * +* Copyright (c) 2021 Wind River Systems, Inc. +* Improved by Bin Meng <[email protected]> +* +* Validated with U-Boot v2021.01 and Linux v5.10 mmc_spi driver +* * This code is licensed under the GNU GPL v2. * * Contributions after 2012-01-13 are licensed under the terms of the
@@ -319,7 +324,7 @@ static int ssi_sd_post_load(void *opaque, int version_id) {ssi_sd_state *s = (ssi_sd_state *)opaque; -if (s->mode >SSI_SD_DATA_READ ) { +if (s->mode >SSI_SD_SKIP_CRC16 ) {return -EINVAL ; } if (s->mode == SSI_SD_CMDARG &&
@@ -337,8 +342,8 @@ static int ssi_sd_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_ssi_sd = { .name ="ssi_sd" , - .version_id =2 , - .minimum_version_id =2 , + .version_id =3 , + .minimum_version_id =3 , .post_load = ssi_sd_post_load, .fields = (VMStateField []) {VMSTATE_UINT32 (mode, ssi_sd_state),
Signature · 1 line
2.25.1
Re: [PATCH v2 18/25] hw/sd: ssi-sd: Bump up version ids of VMStateDescription
Quoted 5 lines · Bin Meng
From: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
Unfortunately this breaks bisectability (think about downstream distributions cherry-picking patches individually).
I don't think there is a problem increasing 2 -> 3 -> 4 -> 5 (Cc'ed David in case). Could you respin increasing the version on each VMState change?
Quoted 50 lines
The
s->modecheck in thepost_load()hook is also updated.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- new patch: bump up version ids of VMStateDescriptionhw/sd/ssi-sd.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)hw/sd/ssi-sd.c +8
-3@@ -4,6 +4,11 @@* Copyright (c) 2007-2009 CodeSourcery. * Written by Paul Brook * +* Copyright (c) 2021 Wind River Systems, Inc. +* Improved by Bin Meng <[email protected]> +* +* Validated with U-Boot v2021.01 and Linux v5.10 mmc_spi driver +* * This code is licensed under the GNU GPL v2. * * Contributions after 2012-01-13 are licensed under the terms of the @@ -319,7 +324,7 @@ static int ssi_sd_post_load(void *opaque, int version_id) {ssi_sd_state *s = (ssi_sd_state *)opaque; -if (s->mode >SSI_SD_DATA_READ ) { +if (s->mode >SSI_SD_SKIP_CRC16 ) {return -EINVAL ; } if (s->mode == SSI_SD_CMDARG &&@@ -337,8 +342,8 @@ static int ssi_sd_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_ssi_sd = { .name ="ssi_sd" , - .version_id =2 , - .minimum_version_id =2 , + .version_id =3 , + .minimum_version_id =3 , .post_load = ssi_sd_post_load, .fields = (VMStateField []) {VMSTATE_UINT32 (mode, ssi_sd_state),
Re: [PATCH v2 18/25] hw/sd: ssi-sd: Bump up version ids of VMStateDescription
Quoted 47 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Bin Meng
From: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
Unfortunately this breaks bisectability (think about downstream distributions cherry-picking patches individually).
I don't think there is a problem increasing 2 -> 3 -> 4 -> 5 (Cc'ed David in case). Could you respin increasing the version on each VMState change?
Quoted 33 lines
The
s->modecheck in thepost_load()hook is also updated.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- new patch: bump up version ids of VMStateDescriptionhw/sd/ssi-sd.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)hw/sd/ssi-sd.c +5
-0@@ -4,6 +4,11 @@* Copyright (c) 2007-2009 CodeSourcery. * Written by Paul Brook * +* Copyright (c) 2021 Wind River Systems, Inc. +* Improved by Bin Meng <[email protected]> +* +* Validated with U-Boot v2021.01 and Linux v5.10 mmc_spi driver +* * This code is licensed under the GNU GPL v2. * * Contributions after 2012-01-13 are licensed under the terms of the @@ -319,7 +324,7 @@ static int ssi_sd_post_load(void *opaque, int version_id) {ssi_sd_state *s = (ssi_sd_state *)opaque;
Quoted 2 lines
hw/sd/ssi-sd.c static int ssi_sd_post_load(void *opaque, int version_id) · line 327
Quoted 2 lines
hw/sd/ssi-sd.c +1
-1-if (s->mode >SSI_SD_DATA_READ ) { +if (s->mode >SSI_SD_SKIP_CRC16 ) {
Doesn't this belong to patch #16 "Support single block write"?
Quoted 16 lines
Quoted 15 lines
hw/sd/ssi-sd.c +2
-2return -EINVAL ; } if (s->mode == SSI_SD_CMDARG &&@@ -337,8 +342,8 @@ static int ssi_sd_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_ssi_sd = { .name ="ssi_sd" , - .version_id =2 , - .minimum_version_id =2 , + .version_id =3 , + .minimum_version_id =3 , .post_load = ssi_sd_post_load, .fields = (VMStateField []) {VMSTATE_UINT32 (mode, ssi_sd_state),
Re: [PATCH v2 18/25] hw/sd: ssi-sd: Bump up version ids of VMStateDescription
In reply to Philippe Mathieu-DaudéQuoted 8 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Bin Meng
From: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
Quoted 7 lines
Unfortunately this breaks bisectability (think about downstream distributions cherry-picking patches individually).
I don't think there is a problem increasing 2 -> 3 -> 4 -> 5 (Cc'ed David in case). Could you respin increasing the version on each VMState change?
I definitely could be wrong, the reason I posted a single patch to upreve the version is that, I was under an impression that in each big release (like here 5.2.0 -> 6.0.0), the incompatibility version id should be bumped up once.
It does not look correct to me that in a big release we bump up the version id for 10 times.
Since this is a series to fix issues in the ssi-sd, I don't think it's practical for downstream to just cherry-pick some commits while leaving some other commits there.
Regards,
Bin
Re: [PATCH v2 18/25] hw/sd: ssi-sd: Bump up version ids of VMStateDescription
Quoted 17 lines · * Bin Meng ([email protected]) wrote:
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Bin Meng
From: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
Quoted 7 lines
Unfortunately this breaks bisectability (think about downstream distributions cherry-picking patches individually).
I don't think there is a problem increasing 2 -> 3 -> 4 -> 5 (Cc'ed David in case). Could you respin increasing the version on each VMState change?
Quoted 6 lines
I definitely could be wrong, the reason I posted a single patch to upreve the version is that, I was under an impression that in each big release (like here 5.2.0 -> 6.0.0), the incompatibility version id should be bumped up once.
It does not look correct to me that in a big release we bump up the version id for 10 times.
I think I agree; I don't think we've ever done it incrementally like that before.
It would only break bisectability if you were cross-version migrating during the bisect which is rare.
Quoted 3 lines
Since this is a series to fix issues in the ssi-sd, I don't think it's practical for downstream to just cherry-pick some commits while leaving some other commits there.
Never underestimate downstream :-)
However, please add a comment when you're doing incrimentals like this - e.g. a TODO or something showing that it's unfinished and you need the remaining patches so people don't do it accidentally.
Dave
Quoted 3 lines
Regards,
Bin
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v2 18/25] hw/sd: ssi-sd: Bump up version ids of VMStateDescription
Quoted 26 lines · Dr. David Alan Gilbert
Quoted 17 lines · * Bin Meng ([email protected]) wrote:
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Bin Meng
From: Bin Meng <[email protected]>
With all these fixes and improvements, there is no way for the VMStateDescription to keep backward compatibility. We will have to bump up version ids.
Quoted 7 lines
Unfortunately this breaks bisectability (think about downstream distributions cherry-picking patches individually).
I don't think there is a problem increasing 2 -> 3 -> 4 -> 5 (Cc'ed David in case). Could you respin increasing the version on each VMState change?
Quoted 6 lines
I definitely could be wrong, the reason I posted a single patch to upreve the version is that, I was under an impression that in each big release (like here 5.2.0 -> 6.0.0), the incompatibility version id should be bumped up once.
It does not look correct to me that in a big release we bump up the version id for 10 times.
Quoted 3 lines
I think I agree; I don't think we've ever done it incrementally like that before.
Thanks David.
Quoted 12 lines
It would only break bisectability if you were cross-version migrating during the bisect which is rare.
Quoted 3 lines
Since this is a series to fix issues in the ssi-sd, I don't think it's practical for downstream to just cherry-pick some commits while leaving some other commits there.
Never underestimate downstream :-)
However, please add a comment when you're doing incrimentals like this - e.g. a TODO or something showing that it's unfinished and you need the remaining patches so people don't do it accidentally.
Sure, next time :)
Philippe, I guess we will need to hold on your PR?
http://patchwork.ozlabs.org/project/qemu-devel/list/?series=226135
Regards,
Bin
PATCH v2 19/25 hw/ssi: Add SiFive SPI controller support
In reply to Bin MengFrom: Bin Meng <[email protected]>
This adds the SiFive SPI controller model for the FU540 SoC. The direct memory-mapped SPI flash mode is unsupported.
---
Changes in v2:
- Log guest error when trying to write reserved registers
- Log guest error when trying to access out-of-bounds registers
- log guest error when writing to reserved bits for chip select registers and watermark registers
- Log unimplemented warning when trying to write direct-map flash interface registers
- Add test tx fifo full logic in sifive_spi_read(), hence remove setting the tx fifo full flag in sifive_spi_write().
- Populate register with their default value
include/hw/ssi/sifive_spi.h | 47 +++++ hw/ssi/sifive_spi.c | 367 ++++++++++++++++++++++++++++++++++++ hw/ssi/Kconfig | 4 + hw/ssi/meson.build | 1 + 4 files changed, 419 insertions(+) create mode 100644 include/hw/ssi/sifive_spi.h create mode 100644 hw/ssi/sifive_spi.c
include/hw/ssi/sifive_spi.h new file +47 -0
@@ -0,0 +1,47 @@ +/* +* QEMU model of the SiFive SPI Controller +* +* Copyright (c) 2021 Wind River Systems, Inc. +* +* Author: +* Bin Meng <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef HW_SIFIVE_SPI_H +#define HW_SIFIVE_SPI_H + +#define SIFIVE_SPI_REG_NUM (0x78 / 4) + +#define TYPE_SIFIVE_SPI "sifive.spi" +#define SIFIVE_SPI (obj) OBJECT_CHECK(SiFiveSPIState, (obj), TYPE_SIFIVE_SPI) + +typedef struct SiFiveSPIState { +SysBusDevice parent_obj; + +MemoryRegion mmio; +qemu_irq irq; + +uint32_t num_cs; +qemu_irq *cs_lines; + +SSIBus *spi; + +Fifo8 tx_fifo; +Fifo8 rx_fifo; + +uint32_t regs[SIFIVE_SPI_REG_NUM ]; +}SiFiveSPIState ; + +#endif /* HW_SIFIVE_SPI_H */
hw/ssi/sifive_spi.c new file +367 -0
367 lines not on this page · raw message
hw/ssi/Kconfig +4 -0
@@ -2,6 +2,10 @@ config PL022 bool select SSI +config SIFIVE_SPI + bool + select SSI + config SSI bool
hw/ssi/meson.build +1 -0
@@ -2,6 +2,7 @@ softmmu_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_smc.c')) softmmu_ss.add(when: 'CONFIG_MSF2', if_true: files('mss-spi.c')) softmmu_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_fiu.c')) softmmu_ss.add(when: 'CONFIG_PL022', if_true: files('pl022.c')) +softmmu_ss.add(when: 'CONFIG_SIFIVE_SPI', if_true: files('sifive_spi.c')) softmmu_ss.add(when: 'CONFIG_SSI', if_true: files('ssi.c')) softmmu_ss.add(when: 'CONFIG_STM32F2XX_SPI', if_true: files('stm32f2xx_spi.c')) softmmu_ss.add(when: 'CONFIG_XILINX_SPI', if_true: files('xilinx_spi.c'))
Signature · 1 line
2.25.1
PATCH v2 20/25 hw/riscv: sifive_u: Add QSPI0 controller and connect a flash
In reply to Bin MengFrom: Bin Meng <[email protected]>
This adds the QSPI0 controller to the SoC, and connects an ISSI 25WP256 flash to it. The generation of corresponding device tree source fragment is also added.
Since the direct memory-mapped mode is not supported by the SiFive SPI model, the <reg> property does not populate the second group which represents the memory mapped address of the SPI flash.
With this commit, upstream U-Boot for the SiFive HiFive Unleashed board can boot on QEMU 'sifive_u' out of the box. This allows users to develop and test the recommended RISC-V boot flow with a real world use case: ZSBL (in QEMU) loads U-Boot SPL from SPI flash to L2LIM, then U-Boot SPL loads the payload from SPI flash that is combined with OpenSBI fw_dynamic firmware and U-Boot proper.
Specify machine property msel to 6 to allow booting from the SPI flash. U-Boot spl is directly loaded via -bios, and subsequent payload is stored in the SPI flash image. Example command line:
$ qemu-system-riscv64 -nographic -M sifive_u,msel=6 -smp 5 -m 8G \
-bios u-boot-spl.bin -drive file=spi-nor.img,if=mtd---
Changes in v2:
- Correct the "connects" typo in the commit message
- Mention in the commit message that <reg> property does not populate the second group which represents the memory mapped address of the SPI flash
include/hw/riscv/sifive_u.h | 4 +++ hw/riscv/sifive_u.c | 52 +++++++++++++++++++++++++++++++++++++ hw/riscv/Kconfig | 2 ++ 3 files changed, 58 insertions(+)
include/hw/riscv/sifive_u.h +4 -0
@@ -26,6 +26,7 @@#include "hw/gpio/sifive_gpio.h" #include "hw/misc/sifive_u_otp.h" #include "hw/misc/sifive_u_prci.h" +#include "hw/ssi/sifive_spi.h" #define TYPE_RISCV_U_SOC "riscv.sifive.u.soc"#define RISCV_U_SOC (obj) \
@@ -45,6 +46,7 @@ typedef struct SiFiveUSoCState {SIFIVEGPIOState gpio;SiFiveUOTPState otp;SiFivePDMAState dma; +SiFiveSPIState spi0;CadenceGEMState gem;uint32_t serial;
@@ -82,6 +84,7 @@ enum {SIFIVE_U_DEV_UART0 ,SIFIVE_U_DEV_UART1 ,SIFIVE_U_DEV_GPIO , +SIFIVE_U_DEV_QSPI0 ,SIFIVE_U_DEV_OTP ,SIFIVE_U_DEV_DMC , SIFIVE_U_DEV_FLASH0,
@@ -120,6 +123,7 @@ enum {SIFIVE_U_PDMA_IRQ5 =28 ,SIFIVE_U_PDMA_IRQ6 =29 ,SIFIVE_U_PDMA_IRQ7 =30 , +SIFIVE_U_QSPI0_IRQ =51 , SIFIVE_U_GEM_IRQ = 0x35 };
hw/riscv/sifive_u.c +52 -0
100 lines not on this page · raw message
hw/riscv/Kconfig +2 -0
@@ -52,9 +52,11 @@ config SIFIVE_U select SIFIVE_GPIO select SIFIVE_PDMA select SIFIVE_PLIC + select SIFIVE_SPI select SIFIVE_UART select SIFIVE_U_OTP select SIFIVE_U_PRCI + select SSI_M25P80 select UNIMP config SPIKE
Signature · 1 line
2.25.1
PATCH v2 21/25 hw/riscv: sifive_u: Add QSPI2 controller and connect an SD card
In reply to Bin MengFrom: Bin Meng <[email protected]>
This adds the QSPI2 controller to the SoC, and connects an SD card to it. The generation of corresponding device tree source fragment is also added.
Specify machine property msel to 11 to boot the same upstream U-Boot SPL and payload image for the SiFive HiFive Unleashed board. Note subsequent payload is stored in the SD card image.
$ qemu-system-riscv64 -nographic -M sifive_u,msel=11 -smp 5 -m 8G \
-bios u-boot-spl.bin -drive file=sdcard.img,if=sd---
Changes in v2:
- Correct the "connects" typo in the commit message
include/hw/riscv/sifive_u.h | 3 +++ hw/riscv/sifive_u.c | 43 +++++++++++++++++++++++++++++++++++-- hw/riscv/Kconfig | 1 + 3 files changed, 45 insertions(+), 2 deletions(-)
include/hw/riscv/sifive_u.h +3 -0
@@ -47,6 +47,7 @@ typedef struct SiFiveUSoCState {SiFiveUOTPState otp;SiFivePDMAState dma;SiFiveSPIState spi0; +SiFiveSPIState spi2;CadenceGEMState gem;uint32_t serial;
@@ -85,6 +86,7 @@ enum {SIFIVE_U_DEV_UART1 ,SIFIVE_U_DEV_GPIO ,SIFIVE_U_DEV_QSPI0 , +SIFIVE_U_DEV_QSPI2 ,SIFIVE_U_DEV_OTP ,SIFIVE_U_DEV_DMC , SIFIVE_U_DEV_FLASH0,
@@ -99,6 +101,7 @@ enum {SIFIVE_U_L2CC_IRQ2 =3 ,SIFIVE_U_UART0_IRQ =4 ,SIFIVE_U_UART1_IRQ =5 , +SIFIVE_U_QSPI2_IRQ =6 ,SIFIVE_U_GPIO_IRQ0 =7 ,SIFIVE_U_GPIO_IRQ1 =8 , SIFIVE_U_GPIO_IRQ2 = 9,
hw/riscv/sifive_u.c +41 -2
85 lines not on this page · raw message
hw/riscv/Kconfig +1 -0
@@ -57,6 +57,7 @@ config SIFIVE_U select SIFIVE_U_OTP select SIFIVE_U_PRCI select SSI_M25P80 + select SSI_SD select UNIMP config SPIKE
Signature · 1 line
2.25.1
PATCH v2 22/25 hw/riscv: sifive_u: Change SIFIVE_U_GEM_IRQ to decimal value
In reply to Bin MengFrom: Bin Meng <[email protected]>
All other peripherals' IRQs are in the format of decimal value. Change SIFIVE_U_GEM_IRQ to be consistent.
---
(no changes since v1)
include/hw/riscv/sifive_u.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/riscv/sifive_u.h +1 -1
@@ -127,7 +127,7 @@ enum {SIFIVE_U_PDMA_IRQ6 =29 ,SIFIVE_U_PDMA_IRQ7 =30 ,SIFIVE_U_QSPI0_IRQ =51 , -SIFIVE_U_GEM_IRQ =0x35 +SIFIVE_U_GEM_IRQ =53 }; enum {
Signature · 1 line
2.25.1
PATCH v2 23/25 docs/system: Sort targets in alphabetical order
In reply to Bin MengFrom: Bin Meng <[email protected]>
---
(no changes since v1)
docs/system/targets.rst | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
docs/system/targets.rst +12 -7
@@ -7,16 +7,21 @@ various targets are mentioned in the following sections. Contents: +.. + This table of contents should be kept sorted alphabetically + by the title text of each file, which isn't the same ordering + as an alphabetical sort by filename. + .. toctree:: - target-i386 + target-arm + target-avr + target-m68k + target-mips target-ppc + target-rx + target-s390x target-sparc target-sparc64 - target-mips - target-arm - target-m68k + target-i386 target-xtensa - target-s390x - target-rx - target-avr
Signature · 1 line
2.25.1
PATCH v2 24/25 docs/system: Add RISC-V documentation
In reply to Bin MengFrom: Bin Meng <[email protected]>
Add RISC-V system emulator documentation for generic information. Board-specific documentation and RISC-V CPU features are only a placeholder and will be added in the future.
---
(no changes since v1)
docs/system/target-riscv.rst | 62 ++++++++++++++++++++++++++++++++++++ docs/system/targets.rst | 1 + 2 files changed, 63 insertions(+) create mode 100644 docs/system/target-riscv.rst
docs/system/target-riscv.rst new file +62 -0
@@ -0,0 +1,62 @@ +.. _RISC-V-System-emulator: + +RISC-V System emulator +====================== + +QEMU can emulate both 32-bit and 64-bit RISC-V CPUs. Use the +``qemu-system-riscv64`` executable to simulate a 64-bit RISC-V machine, +``qemu-system-riscv32`` executable to simulate a 32-bit RISC-V machine. + +QEMU has generally good support for RISC-V guests. It has support for +several different machines. The reason we support so many is that +RISC-V hardware is much more widely varying than x86 hardware. RISC-V +CPUs are generally built into "system-on-chip" (SoC) designs created by +many different companies with different devices, and these SoCs are +then built into machines which can vary still further even if they use +the same SoC. + +For most boards the CPU type is fixed (matching what the hardware has), +so typically you don't need to specify the CPU type by hand, except for +special cases like the ``virt`` board. + +Choosing a board model +---------------------- + +For QEMU's RISC-V system emulation, you must specify which board +model you want to use with the ``-M`` or ``--machine`` option; +there is no default. + +Because RISC-V systems differ so much and in fundamental ways, typically +operating system or firmware images intended to run on one machine +will not run at all on any other. This is often surprising for new +users who are used to the x86 world where every system looks like a +standard PC. (Once the kernel has booted, most user space software +cares much less about the detail of the hardware.) + +If you already have a system image or a kernel that works on hardware +and you want to boot with QEMU, check whether QEMU lists that machine +in its ``-machine help`` output. If it is listed, then you can probably +use that board model. If it is not listed, then unfortunately your image +will almost certainly not boot on QEMU. (You might be able to +extract the file system and use that with a different kernel which +boots on a system that QEMU does emulate.) + +If you don't care about reproducing the idiosyncrasies of a particular +bit of hardware, such as small amount of RAM, no PCI or other hard +disk, etc., and just want to run Linux, the best option is to use the +``virt`` board. This is a platform which doesn't correspond to any +real hardware and is designed for use in virtual machines. You'll +need to compile Linux with a suitable configuration for running on +the ``virt`` board. ``virt`` supports PCI, virtio, recent CPUs and +large amounts of RAM. It also supports 64-bit CPUs. + +Board-specific documentation +---------------------------- + +Unfortunately many of the RISC-V boards QEMU supports are currently +undocumented; you can get a complete list by running +``qemu-system-riscv64 --machine help``, or +``qemu-system-riscv32 --machine help``. + +RISC-V CPU features +-------------------
docs/system/targets.rst +1 -0
@@ -19,6 +19,7 @@ Contents: target-m68k target-mips target-ppc + target-riscv target-rx target-s390x target-sparc
Signature · 1 line
2.25.1
PATCH v2 25/25 docs/system: riscv: Add documentation for sifive_u machine
In reply to Bin MengFrom: Bin Meng <[email protected]>
This adds detailed documentation for RISC-V sifive_u machine, including the following information:
- Supported devices
- Hardware configuration information
- Boot options
- Machine-specific options
- Running Linux kernel
- Running VxWorks kernel
- Running U-Boot, and with an alternate configuration
---
Changes in v2:
- Correct several typos in sifive_u.rst
- Update doc to mention U-Boot v2021.01
docs/system/riscv/sifive_u.rst | 336 +++++++++++++++++++++++++++++++++ docs/system/target-riscv.rst | 10 + 2 files changed, 346 insertions(+) create mode 100644 docs/system/riscv/sifive_u.rst
docs/system/riscv/sifive_u.rst new file +336 -0
@@ -0,0 +1,336 @@ +SiFive HiFive Unleashed (``sifive_u``) +====================================== + +SiFive HiFive Unleashed Development Board is the ultimate RISC‑V development +board featuring the Freedom U540 multi-core RISC‑V processor. + +Supported devices +----------------- + +The ``sifive_u`` machine supports the following devices: + + * 1 E51 / E31 core + * Up to 4 U54 / U34 cores + * Core Level Interruptor (CLINT) + * Platform-Level Interrupt Controller (PLIC) + * Power, Reset, Clock, Interrupt (PRCI) + * L2 Loosely Integrated Memory (L2-LIM) + * DDR memory controller + * 2 UARTs + * 1 GEM Ethernet controller + * 1 GPIO controller + * 1 One-Time Programmable (OTP) memory with stored serial number + * 1 DMA controller + * 2 QSPI controllers + * 1 ISSI 25WP256 flash + * 1 SD card in SPI mode + +Please note the real world HiFive Unleashed board has a fixed configuration of +1 E51 core and 4 U54 core combination and the RISC-V core boots in 64-bit mode. +With QEMU, one can create a machine with 1 E51 core and up to 4 U54 cores. It +is also possible to create a 32-bit variant with the same peripherals except +that the RISC-V cores are replaced by the 32-bit ones (E31 and U34), to help +testing of 32-bit guest software. + +Hardware configuration information +---------------------------------- + +The ``sifive_u`` machine automatically generates a device tree blob ("dtb") +which it passes to the guest. This provides information about the addresses, +interrupt lines and other configuration of the various devices in the system. +Guest software should discover the devices that are present in the generated +DTB instead of using a DTB for the real hardware, as some of the devices are +not modeled by QEMU and trying to access these devices may cause unexpected +behavior. + +Boot options +------------ + +The ``sifive_u`` machine can start using the standard -kernel functionality +for loading a Linux kernel, a VxWorks kernel, a modified U-Boot bootloader +(S-mode) or ELF executable with the default OpenSBI firmware image as the +-bios. It also supports booting the unmodified U-Boot bootloader using the +standard -bios functionality. + +Machine-specific options +------------------------ + +The following machine-specific options are supported: + +- serial=nnn + + The board serial number. When not given, the default serial number 1 is used. + + SiFive reserves the first 1 KiB of the 16 KiB OTP memory for internal use. + The current usage is only used to store the serial number of the board at + offset 0xfc. U-Boot reads the serial number from the OTP memory, and uses + it to generate a unique MAC address to be programmed to the on-chip GEM + Ethernet controller. When multiple QEMU ``sifive_u`` machines are created + and connected to the same subnet, they all have the same MAC address hence + it creates an unusable network. In such scenario, user should give different + values to serial= when creating different ``sifive_u`` machines. + +- start-in-flash + + When given, QEMU's ROM codes jump to QSPI memory-mapped flash directly. + Otherwise QEMU will jump to DRAM or L2LIM depending on the msel= value. + When not given, it defaults to direct DRAM booting. + +- msel=[6|11] + + Mode Select (MSEL[3:0]) pins value, used to control where to boot from. + + The FU540 SoC supports booting from several sources, which are controlled + using the Mode Select pins on the chip. Typically, the boot process runs + through several stages before it begins execution of user-provided programs. + These stages typically include the following: + + 1. Zeroth Stage Boot Loader (ZSBL), which is contained in an on-chip mask + ROM and provided by QEMU. Note QEMU implemented ROM codes are not the + same as what is programmed in the hardware. The QEMU one is a simplified + version, but it provides the same functionality as the hardware. + 2. First Stage Boot Loader (FSBL), which brings up PLLs and DDR memory. + This is U-Boot SPL. + 3. Second Stage Boot Loader (SSBL), which further initializes additional + peripherals as needed. This is U-Boot proper combined with an OpenSBI + fw_dynamic firmware image. + + msel=6 means FSBL and SSBL are both on the QSPI flash. msel=11 means FSBL + and SSBL are both on the SD card. + +Running Linux kernel +-------------------- + +Linux mainline v5.10 release is tested at the time of writing. To build a +Linux mainline kernel that can be booted by the ``sifive_u`` machine in +64-bit mode, simply configure the kernel using the defconfig configuration: + +.. code-block:: bash + + $ export ARCH=riscv + $ export CROSS_COMPILE=riscv64-linux- + $ make defconfig + $ make + +To boot the newly built Linux kernel in QEMU with the ``sifive_u`` machine: + +.. code-block:: bash + + $ qemu-system-riscv64 -M sifive_u -smp 5 -m 2G \ + -display none -serial stdio \ + -kernel arch/riscv/boot/Image \ + -initrd /path/to/rootfs.ext4 \ + -append "root=/dev/ram" + +To build a Linux mainline kernel that can be booted by the ``sifive_u`` machine +in 32-bit mode, use the rv32_defconfig configuration. A patch is required to +fix the 32-bit boot issue for Linux kernel v5.10. + +.. code-block:: bash + + $ export ARCH=riscv + $ export CROSS_COMPILE=riscv64-linux- + $ curl https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/mbox/ > riscv.patch + $ git am riscv.patch + $ make rv32_defconfig + $ make + +Replace ``qemu-system-riscv64`` with ``qemu-system-riscv32`` in the command +line above to boot the 32-bit Linux kernel. A rootfs image containing 32-bit +applications shall be used in order for kernel to boot to user space. + +Running VxWorks kernel +---------------------- + +VxWorks 7 SR0650 release is tested at the time of writing. To build a 64-bit +VxWorks mainline kernel that can be booted by the ``sifive_u`` machine, simply +create a VxWorks source build project based on the sifive_generic BSP, and a +VxWorks image project to generate the bootable VxWorks image, by following the +BSP documentation instructions. + +A pre-built 64-bit VxWorks 7 image for HiFive Unleashed board is available as +part of the VxWorks SDK for testing as well. Instructions to download the SDK: + +.. code-block:: bash + + $ wget https://labs.windriver.com/downloads/wrsdk-vxworks7-sifive-hifive-1.01.tar.bz2 + $ tar xvf wrsdk-vxworks7-sifive-hifive-1.01.tar.bz2 + $ ls bsps/sifive_generic_1_0_0_0/uboot/uVxWorks + +To boot the VxWorks kernel in QEMU with the ``sifive_u`` machine, use: + +.. code-block:: bash + + $ qemu-system-riscv64 -M sifive_u -smp 5 -m 2G \ + -display none -serial stdio \ + -nic tap,ifname=tap0,script=no,downscript=no \ + -kernel /path/to/vxWorks \ + -append "gem(0,0)host:vxWorks h=192.168.200.1 e=192.168.200.2:ffffff00 u=target pw=vxTarget f=0x01" + +It is also possible to test 32-bit VxWorks on the ``sifive_u`` machine. Create +a 32-bit project to build the 32-bit VxWorks image, and use exact the same +command line options with ``qemu-system-riscv32``. + +Running U-Boot +-------------- + +U-Boot mainline v2021.01 release is tested at the time of writing. To build a +U-Boot mainline bootloader that can be booted by the ``sifive_u`` machine, use +the sifive_fu540_defconfig with similar commands as described above for Linux: + +.. code-block:: bash + + $ export CROSS_COMPILE=riscv64-linux- + $ export OPENSBI=/path/to/opensbi-riscv64-generic-fw_dynamic.bin + $ make sifive_fu540_defconfig + +You will get spl/u-boot-spl.bin and u-boot.itb file in the build tree. + +To start U-Boot using the ``sifive_u`` machine, prepare an SPI flash image, or +SD card image that is properly partitioned and populated with correct contents. +genimage_ can be used to generate these images. + +A sample configuration file for a 128 MiB SD card image is: + +.. code-block:: bash + + $ cat genimage_sdcard.cfg + image sdcard.img { + size = 128M + + hdimage { + gpt = true + } + + partition u-boot-spl { + image = "u-boot-spl.bin" + offset = 17K + partition-type-uuid = 5B193300-FC78-40CD-8002-E86C45580B47 + } + + partition u-boot { + image = "u-boot.itb" + offset = 1041K + partition-type-uuid = 2E54B353-1271-4842-806F-E436D6AF6985 + } + } + +SPI flash image has slightly different partition offsets, and the size has to +be 32 MiB to match the ISSI 25WP256 flash on the real board: + +.. code-block:: bash + + $ cat genimage_spi-nor.cfg + image spi-nor.img { + size = 32M + + hdimage { + gpt = true + } + + partition u-boot-spl { + image = "u-boot-spl.bin" + offset = 20K + partition-type-uuid = 5B193300-FC78-40CD-8002-E86C45580B47 + } + + partition u-boot { + image = "u-boot.itb" + offset = 1044K + partition-type-uuid = 2E54B353-1271-4842-806F-E436D6AF6985 + } + } + +Assume U-Boot binaries are put in the same directory as the config file, +we can generate the image by: + +.. code-block:: bash + + $ genimage --config genimage_<boot_src>.cfg --inputpath . + +Boot U-Boot from SD card, by specifying msel=11 and pass the SD card image +to QEMU ``sifive_u`` machine: + +.. code-block:: bash + + $ qemu-system-riscv64 -M sifive_u,msel=11 -smp 5 -m 8G \ + -display none -serial stdio \ + -bios /path/to/u-boot-spl.bin \ + -drive file=/path/to/sdcard.img,if=sd + +Changing msel= value to 6, allows booting U-Boot from the SPI flash: + +.. code-block:: bash + + $ qemu-system-riscv64 -M sifive_u,msel=6 -smp 5 -m 8G \ + -display none -serial stdio \ + -bios /path/to/u-boot-spl.bin \ + -drive file=/path/to/spi-nor.img,if=mtd + +Note when testing U-Boot, QEMU automatically generated device tree blob is +not used because U-Boot itself embeds device tree blobs for U-Boot SPL and +U-Boot proper. Hence the number of cores and size of memory have to match +the real hardware, ie: 5 cores (-smp 5) and 8 GiB memory (-m 8G). + +Above use case is to run upstream U-Boot for the SiFive HiFive Unleashed +board on QEMU ``sifive_u`` machine out of the box. This allows users to +develop and test the recommended RISC-V boot flow with a real world use +case: ZSBL (in QEMU) loads U-Boot SPL from SD card or SPI flash to L2LIM, +then U-Boot SPL loads the combined payload image of OpenSBI fw_dynamic +firmware and U-Boot proper. However sometimes we want to have a quick test +of booting U-Boot on QEMU without the needs of preparing the SPI flash or +SD card images, an alternate way can be used, which is to create a U-Boot +S-mode image by modifying the configuration of U-Boot: + +.. code-block:: bash + + $ make menuconfig + +then manually select the following configuration in U-Boot: + + Device Tree Control > Provider of DTB for DT Control > Prior Stage bootloader DTB + +This lets U-Boot to use the QEMU generated device tree blob. During the build, +a build error will be seen below: + +.. code-block:: none + + MKIMAGE u-boot.img + ./tools/mkimage: Can't open arch/riscv/dts/hifive-unleashed-a00.dtb: No such file or directory + ./tools/mkimage: failed to build FIT + make: *** [Makefile:1440: u-boot.img] Error 1 + +The above errors can be safely ignored as we don't run U-Boot SPL under QEMU +in this alternate configuration. + +Boot the 64-bit U-Boot S-mode image directly: + +.. code-block:: bash + + $ qemu-system-riscv64 -M sifive_u -smp 5 -m 2G \ + -display none -serial stdio \ + -kernel /path/to/u-boot.bin + +It's possible to create a 32-bit U-Boot S-mode image as well. + +.. code-block:: bash + + $ export CROSS_COMPILE=riscv64-linux- + $ make sifive_fu540_defconfig + $ make menuconfig + +then manually update the following configuration in U-Boot: + + Device Tree Control > Provider of DTB for DT Control > Prior Stage bootloader DTB + RISC-V architecture > Base ISA > RV32I + Boot images > Text Base > 0x80400000 + +Use the same command line options to boot the 32-bit U-Boot S-mode image: + +.. code-block:: bash + + $ qemu-system-riscv32 -M sifive_u -smp 5 -m 2G \ + -display none -serial stdio \ + -kernel /path/to/u-boot.bin + +.. _genimage: https://github.com/pengutronix/genimage
docs/system/target-riscv.rst +10 -0
@@ -58,5 +58,15 @@ undocumented; you can get a complete list by running ``qemu-system-riscv64 --machine help``, or ``qemu-system-riscv32 --machine help``. +.. + This table of contents should be kept sorted alphabetically + by the title text of each file, which isn't the same ordering + as an alphabetical sort by filename. + +.. toctree:: + :maxdepth: 1 + + riscv/sifive_u + RISC-V CPU features -------------------
Signature · 1 line
2.25.1
Hi Bin,
Quoted 68 lines · Bin Meng
From: Bin Meng <[email protected]>
This adds the missing SPI support to the
sifive_umachine in the QEMU mainline. With this series, upstream U-Boot for the SiFive HiFive Unleashed board can boot on QEMUsifive_uout of the box. This allows users to develop and test the recommended RISC-V boot flow with a real world use
case: ZSBL (in QEMU) loads U-Boot SPL from SD card or SPI flash to L2LIM, then U-Boot SPL loads the payload from SD card or SPI flash that is a combination of OpenSBIfw_dynamicfirmware and U-Boot proper.The m25p80 model is updated to support ISSI flash series. A bunch of ssi-sd issues are fixed, and writing to SD card in SPI mode is supported.
reST documentation for RISC-V is added. Currently only
sifive_umachine is documented, but more to come.Changes in v2:
- Mention QPI (Quad Peripheral Interface) mode is not supported
- Add a debug printf in the state handling codes
- Fix several typos in the commit message
- new patch: add a state representing Nac
- Make this fix a separate patch from the CMD18 support
- Fix 2 typos in the commit message
- Add a comment block to explain the CMD12 timing
- Catch CMD12 in all of the data read states per the timing requirement
- Move multiple write token definitions out of this patch
- Correct the "coding" typo in the commit message
- Correct the "token" typo in the commit message
- Add 'write_bytes' invmstate_ssi_sd
- Correct the "token" typo in the commit message
- Introduce multiple write token definitions in this patch
- new patch: bump up version ids of VMStateDescription
- Log guest error when trying to write reserved registers
- Log guest error when trying to access out-of-bounds registers
- log guest error when writing to reserved bits for chip select registers and watermark registers
- Log unimplemented warning when trying to write direct-map flash interface registers
- Add test tx fifo full logic insifive_spi_read(), hence remove setting the tx fifo full flag insifive_spi_write().
- Populate register with their default value
- Correct the "connects" typo in the commit message
- Mention in the commit message that <reg> property does not populate the second group which represents the memory mapped address of the SPI flash
- Correct the "connects" typo in the commit message
- Correct several typos insifive_u.rst
- Update doc to mention U-Boot v2021.01Bin Meng (25):
hw/block: m25p80: Add ISSI SPI flash support
hw/block: m25p80: Add various ISSI flash information hw/sd: ssi-sd: Fix incorrect card response sequence
hw/sd: sd: Support CMD59 for SPI mode
hw/sd: sd: Dropsd_crc16()
util: Add CRC16 (CCITT) calculation routines
hw/sd: ssi-sd: Suffix a data block with CRC16
hw/sd: ssi-sd: Add a state representing Nac
hw/sd: ssi-sd: Fix the wrong command index forSTOP_TRANSMISSIONhw/sd: ssi-sd: Support multiple block read
hw/sd: ssi-sd: Use macros for the dummy value and tokens in the
transfer
hw/sd: sd: Remove duplicated codes in single/multiple block read/write hw/sd: sd: Allow single/multiple block write for SPI mode hw/sd:sd.h: Cosmetic change of using spaces
hw/sd: Introducereceive_ready()callback
hw/sd: ssi-sd: Support single block write
hw/sd: ssi-sd: Support multiple block write
Quoted 8 lines
hw/sd: ssi-sd: Bump up version ids of VMStateDescription hw/ssi: Add SiFive SPI controller support
hw/riscv:sifive_u: Add QSPI0 controller and connect a flash hw/riscv:sifive_u: Add QSPI2 controller and connect an SD card hw/riscv:sifive_u: ChangeSIFIVE_U_GEM_IRQto decimal value docs/system: Sort targets in alphabetical order docs/system: Add RISC-V documentation
docs/system: riscv: Add documentation forsifive_umachine
I'm queuing patches 3-9,11,14 to sd-next, and will repost your 10,12,13,15-18 based on it.
Regards,
Phil.