diff options
author | Jes B. Klinke <jbk@chromium.org> | 2022-12-02 21:29:24 -0800 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-12-09 07:00:10 +0000 |
commit | 96747f3dc04518ce0d06c429fa5009f96fbe6294 (patch) | |
tree | bc0e15ab1b40800ee0e6abe3b97ab892da3ec3b5 | |
parent | fb763e90aff3168e92fb3fde481f44e4bf80267e (diff) | |
download | chrome-ec-96747f3dc04518ce0d06c429fa5009f96fbe6294.tar.gz |
chips/stm32/usb_spi: Forwarding to multiple spi devices
For devices similar to Servo Micro, in case of multiple entries in
spi_devices, this change allows the USB host to request which device its
transaction requests are to be forwarded to.
BUG=b:192262089
TEST=HyperDebug board forwards to two SPI busses
Change-Id: Ic94e66a8d7f5502c3016a26a1beb24236b3c4893
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4077428
Commit-Queue: Jes Klinke <jbk@chromium.org>
Tested-by: Jes Klinke <jbk@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r-- | board/c2d2/board.c | 2 | ||||
-rw-r--r-- | board/discovery-stm32f072/board.c | 2 | ||||
-rw-r--r-- | board/hammer/board.c | 3 | ||||
-rw-r--r-- | board/hyperdebug/board.c | 2 | ||||
-rw-r--r-- | board/servo_micro/board.c | 2 | ||||
-rw-r--r-- | chip/stm32/usb_spi.c | 18 | ||||
-rw-r--r-- | chip/stm32/usb_spi.h | 20 | ||||
-rw-r--r-- | include/spi.h | 7 |
8 files changed, 43 insertions, 13 deletions
diff --git a/board/c2d2/board.c b/board/c2d2/board.c index 9c131e17d5..02fce6143e 100644 --- a/board/c2d2/board.c +++ b/board/c2d2/board.c @@ -293,7 +293,7 @@ USB_STREAM_CONFIG_USART_IFACE(usart4_usb, USB_IFACE_USART4_STREAM, /* SPI devices */ const struct spi_device_t spi_devices[] = { - { CONFIG_SPI_FLASH_PORT, 1, GPIO_SPI_CSN }, + { CONFIG_SPI_FLASH_PORT, 1, GPIO_SPI_CSN, USB_SPI_ENABLED }, }; const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices); diff --git a/board/discovery-stm32f072/board.c b/board/discovery-stm32f072/board.c index 6d8d9820ac..fb61ea1232 100644 --- a/board/discovery-stm32f072/board.c +++ b/board/discovery-stm32f072/board.c @@ -128,7 +128,7 @@ BUILD_ASSERT(ARRAY_SIZE(usb_strings) == USB_STR_COUNT); /* SPI devices */ const struct spi_device_t spi_devices[] = { - { CONFIG_SPI_FLASH_PORT, 0, GPIO_SPI_CS }, + { CONFIG_SPI_FLASH_PORT, 0, GPIO_SPI_CS, USB_SPI_ENABLED }, }; const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices); diff --git a/board/hammer/board.c b/board/hammer/board.c index 496555149d..85be6a0449 100644 --- a/board/hammer/board.c +++ b/board/hammer/board.c @@ -79,7 +79,8 @@ BUILD_ASSERT(ARRAY_SIZE(usb_strings) == USB_STR_COUNT); #ifdef HAS_SPI_TOUCHPAD /* SPI devices */ const struct spi_device_t spi_devices[] = { - [SPI_ST_TP_DEVICE_ID] = { CONFIG_SPI_TOUCHPAD_PORT, 2, GPIO_SPI1_NSS }, + [SPI_ST_TP_DEVICE_ID] = { CONFIG_SPI_TOUCHPAD_PORT, 2, GPIO_SPI1_NSS, + USB_SPI_ENABLED }, }; const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices); diff --git a/board/hyperdebug/board.c b/board/hyperdebug/board.c index b98cbc8d4c..63ce786e47 100644 --- a/board/hyperdebug/board.c +++ b/board/hyperdebug/board.c @@ -128,7 +128,7 @@ USB_STREAM_CONFIG(usart5_usb, USB_IFACE_USART5_STREAM, /* SPI devices */ const struct spi_device_t spi_devices[] = { - { 1 /* SPI2 */, 7, GPIO_SPI2_CS }, + { 1 /* SPI2 */, 7, GPIO_SPI2_CS, USB_SPI_ENABLED }, }; const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices); diff --git a/board/servo_micro/board.c b/board/servo_micro/board.c index 5f0786cf45..9fbc4be635 100644 --- a/board/servo_micro/board.c +++ b/board/servo_micro/board.c @@ -298,7 +298,7 @@ BUILD_ASSERT(ARRAY_SIZE(usb_strings) == USB_STR_COUNT); /* SPI devices */ const struct spi_device_t spi_devices[] = { - { CONFIG_SPI_FLASH_PORT, 1, GPIO_SPI_CS }, + { CONFIG_SPI_FLASH_PORT, 1, GPIO_SPI_CS, USB_SPI_ENABLED }, }; const unsigned int spi_devices_used = ARRAY_SIZE(spi_devices); diff --git a/chip/stm32/usb_spi.c b/chip/stm32/usb_spi.c index 5ea813a86c..35bd07d103 100644 --- a/chip/stm32/usb_spi.c +++ b/chip/stm32/usb_spi.c @@ -346,13 +346,15 @@ static void usb_spi_process_rx_packet(struct usb_spi_config const *config, * asserted or deasserted. */ uint16_t flags = packet->cmd_cs.flags; + const struct spi_device_t *current_device = + &spi_devices[config->state->current_spi_device_idx]; if (flags & USB_SPI_CHIP_SELECT) { /* Set chip select low (asserted). */ - gpio_set_level(SPI_FLASH_DEVICE->gpio_cs, 0); + gpio_set_level(current_device->gpio_cs, 0); } else { /* Set chip select high (adesserted). */ - gpio_set_level(SPI_FLASH_DEVICE->gpio_cs, 1); + gpio_set_level(current_device->gpio_cs, 1); } config->state->mode = USB_SPI_MODE_SEND_CHIP_SELECT_RESPONSE; break; @@ -422,6 +424,8 @@ void usb_spi_deferred(struct usb_spi_config const *config) /* Start a new SPI transfer. */ if (config->state->mode == USB_SPI_MODE_START_SPI) { + const struct spi_device_t *current_device = + &spi_devices[config->state->current_spi_device_idx]; uint16_t status_code; int read_count = config->state->spi_read_ctx.transfer_size; #ifndef CONFIG_SPI_HALFDUPLEX @@ -436,7 +440,7 @@ void usb_spi_deferred(struct usb_spi_config const *config) } #endif status_code = spi_transaction( - SPI_FLASH_DEVICE, config->state->spi_write_ctx.buffer, + current_device, config->state->spi_write_ctx.buffer, config->state->spi_write_ctx.transfer_size, config->state->spi_read_ctx.buffer, read_count); @@ -624,10 +628,14 @@ int usb_spi_interface(struct usb_spi_config const *config, usb_uint *rx_buf, (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE)) return 1; - if (setup.wValue != 0 || setup.wIndex != config->interface || - setup.wLength != 0) + if (setup.wValue >= spi_devices_used || + !(spi_devices[setup.wValue].usb_flags & USB_SPI_ENABLED) || + setup.wIndex != config->interface || setup.wLength != 0) return 1; + /* Record which SPI device the host wished to manipulate. */ + config->state->current_spi_device_idx = setup.wValue; + switch (setup.bRequest) { case USB_SPI_REQ_ENABLE: config->state->enabled_host = 1; diff --git a/chip/stm32/usb_spi.h b/chip/stm32/usb_spi.h index 3ff582dd14..3f73efe9bc 100644 --- a/chip/stm32/usb_spi.h +++ b/chip/stm32/usb_spi.h @@ -280,6 +280,13 @@ #define USB_SPI_MIN_PACKET_SIZE (2) +/* + * Values used in spi_device_t.usb_flags + */ + +/* Is the USB host allowed to operate on SPI device. */ +#define USB_SPI_ENABLED (BIT(0)) + enum packet_id_type { /* Request USB SPI configuration data from device. */ USB_SPI_PKT_ID_CMD_GET_USB_SPI_CONFIG = 0, @@ -457,8 +464,8 @@ struct usb_spi_state { * control endpoint. The enabled_device flag is set by calling * usb_spi_enable. */ - int enabled_host; - int enabled_device; + uint8_t enabled_host; + uint8_t enabled_device; /* * The current enabled state. This is only updated in the deferred @@ -470,7 +477,13 @@ struct usb_spi_state { * specific state update routines are only called from the deferred * callback. */ - int enabled; + uint8_t enabled; + + /* + * The index of the SPI port currently receiving forwarded transactions, + * default is zero. + */ + uint8_t current_spi_device_idx; /* Mark the current operating mode. */ enum usb_spi_mode mode; @@ -554,6 +567,7 @@ struct usb_spi_config { .enabled_host = 0, \ .enabled_device = 0, \ .enabled = 0, \ + .current_spi_device_idx = 0, \ .spi_write_ctx.buffer = (uint8_t *)CONCAT2(NAME, _buffer_), \ .spi_read_ctx.buffer = (uint8_t *)CONCAT2(NAME, _buffer_), \ }; \ diff --git a/include/spi.h b/include/spi.h index 965f941fa3..b6251b74b2 100644 --- a/include/spi.h +++ b/include/spi.h @@ -45,6 +45,13 @@ struct spi_device_t { /* gpio used for chip selection. */ enum gpio_signal gpio_cs; + +#ifdef CONFIG_USB_SPI + /* + * Flags used by usb_spi.c + */ + uint8_t usb_flags; +#endif }; extern |