This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 679f0fc484e42a798b1ff477bf6d8f6e19476617 Author: p-szafonimateusz <[email protected]> AuthorDate: Wed Jun 3 11:06:53 2026 +0200 drivers/virtio: add the VirtIO SPI frontend driver Add a frontend for the VirtIO SPI device (device id 45). Each probed device becomes a struct spi_dev_s; virtio_spi_get_master() returns it by index. An exchange is split into requests the transport can map at once, and the device keeps CS asserted between them. The wire-format definitions are in include/nuttx/virtio/virtio-spi.h, shared with a device-side backend. Signed-off-by: p-szafonimateusz <[email protected]> Signed-off-by: yangsong8 <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- Documentation/os/drivers/special/spi.rst | 16 + drivers/virtio/CMakeLists.txt | 4 + drivers/virtio/Kconfig | 13 + drivers/virtio/Make.defs | 4 + drivers/virtio/virtio-spi.c | 755 +++++++++++++++++++++++++++++++ drivers/virtio/virtio-spi.h | 55 +++ drivers/virtio/virtio.c | 9 + include/nuttx/spi/spi_virtio.h | 66 +++ include/nuttx/virtio/virtio-spi.h | 262 +++++++++++ 9 files changed, 1184 insertions(+) diff --git a/Documentation/os/drivers/special/spi.rst b/Documentation/os/drivers/special/spi.rst index 55560eac6c1..bf876dbb1e6 100644 --- a/Documentation/os/drivers/special/spi.rst +++ b/Documentation/os/drivers/special/spi.rst @@ -23,3 +23,19 @@ SPI Device Drivers - **Examples**: ``drivers/loop.c``, ``drivers/mmcsd/mmcsd_spi.c``, ``drivers/ramdisk.c``, etc. + +================= +Native VirtIO SPI +================= + +An SPI controller can be exposed over a VirtIO transport. +``CONFIG_DRIVERS_VIRTIO_SPI`` enables the frontend (client), which probes a +VirtIO SPI device (device id 45), reads its configuration space and exposes a +``struct spi_dev_s`` retrievable with ``virtio_spi_get_master(index)``. +Board code may then pass it to device drivers or register it with +``spi_register()`` as ``/dev/spiN``. ``CONFIG_SPI_EXCHANGE`` is required. + +The wire format in ``include/nuttx/virtio/virtio-spi.h`` follows the +Linux VirtIO SPI definitions. An ``spi_transfer()`` sequence executes as one +bus-locked run of requests, with CS held between transfers that do not +de-select. The frontend can be validated against a host vhost-user backend. diff --git a/drivers/virtio/CMakeLists.txt b/drivers/virtio/CMakeLists.txt index 7e06087170b..1e6976c82a6 100644 --- a/drivers/virtio/CMakeLists.txt +++ b/drivers/virtio/CMakeLists.txt @@ -73,5 +73,9 @@ if(CONFIG_DRIVERS_VIRTIO_SOUND) list(APPEND SRCS virtio-snd.c) endif() +if(CONFIG_DRIVERS_VIRTIO_SPI) + list(APPEND SRCS virtio-spi.c) +endif() + target_sources(drivers PRIVATE ${SRCS}) target_include_directories(drivers PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 0a6ecaae9e1..bb5e308f2c6 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -162,3 +162,16 @@ config DRIVERS_VIRTIO_SND_BUFFER_COUNT int "Virtio snd driver buffer count" default 6 endif + +config DRIVERS_VIRTIO_SPI + bool "Virtio SPI support" + depends on SPI && SPI_EXCHANGE + default n + select DRIVERS_VIRTIO + ---help--- + Enable the native VirtIO SPI frontend driver. It probes a VirtIO + SPI device (device ID 45), reads the SPI configuration space, + exposes a NuttX SPI lower-half instance through + virtio_spi_get_master(), and submits transfers through the request + virtqueue using the Linux-compatible VirtIO SPI wire format. + diff --git a/drivers/virtio/Make.defs b/drivers/virtio/Make.defs index 30d5dfa5f0c..8b8acccfa85 100644 --- a/drivers/virtio/Make.defs +++ b/drivers/virtio/Make.defs @@ -74,6 +74,10 @@ ifeq ($(CONFIG_DRIVERS_VIRTIO_SOUND),y) CSRCS += virtio-snd.c endif +ifeq ($(CONFIG_DRIVERS_VIRTIO_SPI),y) + CSRCS += virtio-spi.c +endif + # Include build support DEPPATH += --dep-path virtio diff --git a/drivers/virtio/virtio-spi.c b/drivers/virtio/virtio-spi.c new file mode 100644 index 00000000000..80bf1177468 --- /dev/null +++ b/drivers/virtio/virtio-spi.c @@ -0,0 +1,755 @@ +/**************************************************************************** + * drivers/virtio/virtio-spi.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <debug.h> +#include <errno.h> +#include <sys/endian.h> +#include <syslog.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> +#include <nuttx/nuttx.h> +#include <nuttx/queue.h> +#include <nuttx/semaphore.h> +#include <nuttx/spinlock.h> +#include <nuttx/spi/spi.h> +#include <nuttx/spi/spi_virtio.h> +#include <nuttx/virtio/virtio-spi.h> +#include <nuttx/virtio/virtio.h> + +#include "virtio-spi.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct virtio_spi_priv_s +{ + struct spi_dev_s spi; /* Must be the first member */ + FAR struct virtio_device *vdev; + dq_entry_t node; /* Entry in the frontend registry */ + int idx; /* VirtIO SPI bus index */ + mutex_t buslock; /* SPI_LOCK exclusive bus access */ + sem_t done; /* Request completion, one in flight */ + struct virtio_spi_config_s hwcfg; /* Device configuration space */ + + /* Wire-format request header, kept up to date by the SPI lower-half + * ops; cs_change holds the after-transfer CS state and is cleared by + * virtio_spi_exchange for intermediate chunks. + */ + + struct spi_transfer_head_s head; +}; + +/* Registry of probed frontends, published for virtio_spi_get_master() */ + +struct virtio_spi_bus_s +{ + spinlock_t lock; + struct dq_queue_s devices; + int idx; /* Next bus index */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int virtio_spi_lock(FAR struct spi_dev_s *dev, bool lock); +static void virtio_spi_select(FAR struct spi_dev_s *dev, uint32_t devid, + bool selected); +static uint32_t virtio_spi_setfrequency(FAR struct spi_dev_s *dev, + uint32_t frequency); +#ifdef CONFIG_SPI_DELAY_CONTROL +static int virtio_spi_setdelay(FAR struct spi_dev_s *dev, + uint32_t startdelay, uint32_t stopdelay, + uint32_t csdelay, uint32_t ifdelay); +#endif +static void virtio_spi_setmode(FAR struct spi_dev_s *dev, + enum spi_mode_e mode); +static void virtio_spi_setbits(FAR struct spi_dev_s *dev, int nbits); +#ifdef CONFIG_SPI_HWFEATURES +static int virtio_spi_hwfeatures(FAR struct spi_dev_s *dev, + spi_hwfeatures_t features); +#endif +static uint8_t virtio_spi_status(FAR struct spi_dev_s *dev, uint32_t devid); +static uint32_t virtio_spi_send(FAR struct spi_dev_s *dev, uint32_t wd); +static int virtio_spi_exchange(FAR struct spi_dev_s *dev, + FAR const void *txbuffer, + FAR void *rxbuffer, size_t nwords); +#ifdef CONFIG_SPI_TRIGGER +static int virtio_spi_trigger(FAR struct spi_dev_s *dev); +#endif +static int virtio_spi_registercallback(FAR struct spi_dev_s *dev, + spi_mediachange_t callback, + FAR void *arg); + +static int virtio_spi_probe(FAR struct virtio_device *vdev); +static void virtio_spi_remove(FAR struct virtio_device *vdev); +static void virtio_spi_dump(FAR struct virtio_device *vdev); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct virtio_driver g_virtio_spi_driver = +{ + VIRTIO_ID_SPI, /* device id */ + virtio_spi_probe, /* probe */ + virtio_spi_remove, /* remove */ + virtio_spi_dump, /* dump */ +}; + +static struct virtio_spi_bus_s g_virtio_spi_bus = +{ + SP_UNLOCKED, +}; + +static const struct spi_ops_s g_virtio_spi_ops = +{ + virtio_spi_lock, + virtio_spi_select, + virtio_spi_setfrequency, +#ifdef CONFIG_SPI_DELAY_CONTROL + virtio_spi_setdelay, +#endif + virtio_spi_setmode, + virtio_spi_setbits, +#ifdef CONFIG_SPI_HWFEATURES + virtio_spi_hwfeatures, +#endif + virtio_spi_status, +#ifdef CONFIG_SPI_CMDDATA + NULL, +#endif + virtio_spi_send, + virtio_spi_exchange, + NULL, /* exchangeseqs */ + NULL, /* cancelseqs */ +#ifdef CONFIG_SPI_TRIGGER + virtio_spi_trigger, +#endif + virtio_spi_registercallback +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtio_spi_done + * + * Description: + * Request virtqueue completion callback: wake the waiter of every + * finished request. + * + ****************************************************************************/ + +static void virtio_spi_done(FAR struct virtqueue *vq) +{ + FAR sem_t *sem; + + /* One request in flight: never concurrent with the submitter */ + + sem = virtqueue_get_buffer(vq, NULL, NULL); + if (sem != NULL) + { + nxsem_post(sem); + } +} + +/**************************************************************************** + * Name: virtio_spi_transfer + * + * Description: + * Run one SPI transfer as a single VirtIO SPI request. The header is + * the wire-format image cached in the driver state, kept up to date by + * the SPI lower-half ops; the + * result lives on the stack and the transmit and receive buffers are + * the caller's. Each is mapped for the device: on MMIO/PCI the mapping + * is a no-op (zero copy), on a bouncing transport it is bounced through + * shared memory. The descriptor chain is the header, an optional + * transmit buffer, an optional receive buffer, and the result byte. + * + ****************************************************************************/ + +static int virtio_spi_transfer(FAR struct virtio_spi_priv_s *priv, + FAR const void *txbuffer, + FAR void *rxbuffer, size_t txlen, + size_t rxlen) +{ + FAR struct virtio_device *vdev = priv->vdev; + FAR struct virtqueue *vq = vdev->vrings_info[0].vq; + struct spi_transfer_result_s result; + struct virtqueue_buf vb[4]; + FAR void *headmap; + FAR void *txmap; + FAR void *rxmap; + FAR void *resultmap; + int readable = 0; + int writable = 0; + int ret; + + /* Callers hold the bus lock, serializing the single in-flight request + * and the cached header it snapshots. + */ + + result.result = VIRTIO_SPI_TRANS_ERR; + + ret = virtio_map_buf(vdev, &priv->head, sizeof(priv->head), + VIRTIO_DMA_TO_DEVICE, &headmap); + if (ret < 0) + { + return ret; + } + + vb[readable + writable].buf = headmap; + vb[readable + writable].len = sizeof(priv->head); + readable++; + + if (txlen > 0) + { + ret = virtio_map_buf(vdev, (FAR void *)txbuffer, txlen, + VIRTIO_DMA_TO_DEVICE, &txmap); + if (ret < 0) + { + goto out_head; + } + + vb[readable + writable].buf = txmap; + vb[readable + writable].len = txlen; + readable++; + } + + if (rxlen > 0) + { + ret = virtio_map_buf(vdev, rxbuffer, rxlen, VIRTIO_DMA_FROM_DEVICE, + &rxmap); + if (ret < 0) + { + goto out_tx; + } + + vb[readable + writable].buf = rxmap; + vb[readable + writable].len = rxlen; + writable++; + } + + ret = virtio_map_buf(vdev, &result, sizeof(result), + VIRTIO_DMA_FROM_DEVICE, &resultmap); + if (ret < 0) + { + goto out_rx; + } + + vb[readable + writable].buf = resultmap; + vb[readable + writable].len = sizeof(result); + writable++; + + ret = virtqueue_add_buffer(vq, vb, readable, writable, &priv->done); + if (ret >= 0) + { + virtqueue_kick(vq); + nxsem_wait_uninterruptible(&priv->done); + } + + /* unmap copies device-written data (rx, result) back to their buffers; + * only after that are they valid on a bouncing transport. + */ + + virtio_unmap_buf(vdev, &result, sizeof(result), VIRTIO_DMA_FROM_DEVICE, + resultmap); +out_rx: + if (rxlen > 0) + { + virtio_unmap_buf(vdev, rxbuffer, rxlen, VIRTIO_DMA_FROM_DEVICE, rxmap); + } + +out_tx: + if (txlen > 0) + { + virtio_unmap_buf(vdev, (FAR void *)txbuffer, txlen, + VIRTIO_DMA_TO_DEVICE, txmap); + } + +out_head: + virtio_unmap_buf(vdev, &priv->head, sizeof(priv->head), + VIRTIO_DMA_TO_DEVICE, headmap); + + if (ret >= 0) + { + ret = virtio_spi_status_to_errno(result.result); + } + + return ret; +} + +/**************************************************************************** + * Name: virtio_spi_lock + ****************************************************************************/ + +static int virtio_spi_lock(FAR struct spi_dev_s *dev, bool lock) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + + return lock ? nxmutex_lock(&priv->buslock) : + nxmutex_unlock(&priv->buslock); +} + +/**************************************************************************** + * Name: virtio_spi_select + ****************************************************************************/ + +static void virtio_spi_select(FAR struct spi_dev_s *dev, uint32_t devid, + bool selected) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + uint32_t cs = SPIDEVID_INDEX(devid); + + /* CS is driven per request through head.cs_change: just latch the + * device id, ignoring de-selects and ids the device cannot address + * (head.chip_select_id is one byte). + */ + + if (selected && cs <= UINT8_MAX && + (priv->hwcfg.cs_max_number == 0 || cs < priv->hwcfg.cs_max_number)) + { + priv->head.chip_select_id = cs; + } +} + +/**************************************************************************** + * Name: virtio_spi_setfrequency + ****************************************************************************/ + +static uint32_t virtio_spi_setfrequency(FAR struct spi_dev_s *dev, + uint32_t frequency) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + + if (priv->hwcfg.max_freq_hz != 0 && frequency > priv->hwcfg.max_freq_hz) + { + frequency = priv->hwcfg.max_freq_hz; + } + + priv->head.freq = htole32(frequency); + return frequency; +} + +/**************************************************************************** + * Name: virtio_spi_setdelay + ****************************************************************************/ + +#ifdef CONFIG_SPI_DELAY_CONTROL +static int virtio_spi_setdelay(FAR struct spi_dev_s *dev, + uint32_t startdelay, uint32_t stopdelay, + uint32_t csdelay, uint32_t ifdelay) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + + priv->head.cs_setup_ns = htole32(startdelay); + priv->head.cs_delay_hold_ns = htole32(stopdelay); + priv->head.cs_change_delay_inactive_ns = htole32(csdelay); + priv->head.word_delay_ns = htole32(ifdelay); + return OK; +} +#endif + +/**************************************************************************** + * Name: virtio_spi_setmode + ****************************************************************************/ + +static void virtio_spi_setmode(FAR struct spi_dev_s *dev, + enum spi_mode_e mode) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + + if (mode <= SPIDEV_MODE3) + { + uint32_t m = le32toh(priv->head.mode); + + m &= ~(VIRTIO_SPI_CPHA | VIRTIO_SPI_CPOL); + priv->head.mode = htole32(m | virtio_spi_mode(mode)); + } +} + +/**************************************************************************** + * Name: virtio_spi_setbits + ****************************************************************************/ + +static void virtio_spi_setbits(FAR struct spi_dev_s *dev, int nbits) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + + if (nbits > 0 && nbits <= 32 && + (priv->hwcfg.bits_per_word_mask == 0 || + (priv->hwcfg.bits_per_word_mask & (1u << (nbits - 1))) != 0)) + { + priv->head.bits_per_word = nbits; + } +} + +/**************************************************************************** + * Name: virtio_spi_hwfeatures + * + * Description: + * Map hardware features to wire mode flags and cs_change forcing; + * -ENOSYS when a feature has no mapping or the device lacks support. + * + ****************************************************************************/ + +#ifdef CONFIG_SPI_HWFEATURES +static int virtio_spi_hwfeatures(FAR struct spi_dev_s *dev, + spi_hwfeatures_t features) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + uint32_t mf = priv->hwcfg.mode_func_supported; + spi_hwfeatures_t valid = 0; + uint32_t flags = 0; + uint32_t m; + +#ifdef CONFIG_SPI_CS_CONTROL + valid |= HWFEAT_FORCE_CS_INACTIVE_AFTER_TRANSFER | + HWFEAT_FORCE_CS_ACTIVE_AFTER_TRANSFER; + if (mf == 0 || (mf & VIRTIO_SPI_MF_SUPPORT_CS_HIGH) != 0) + { + valid |= HWFEAT_INVERT_CS_LEVEL; + } +#endif + +#ifdef CONFIG_SPI_BITORDER + if (mf == 0 || (mf & VIRTIO_SPI_MF_SUPPORT_LSB_FIRST) != 0) + { + valid |= HWFEAT_LSBFIRST; + } +#endif + + if ((features & ~valid) != 0) + { + return -ENOSYS; + } + +#ifdef CONFIG_SPI_CS_CONTROL + if ((features & HWFEAT_INVERT_CS_LEVEL) != 0) + { + flags |= VIRTIO_SPI_CS_HIGH; + } + + priv->head.cs_change = + (features & HWFEAT_FORCE_CS_ACTIVE_AFTER_TRANSFER) != 0 ? 0 : 1; +#endif + +#ifdef CONFIG_SPI_BITORDER + if ((features & HWFEAT_LSBFIRST) != 0) + { + flags |= VIRTIO_SPI_MODE_LSB_FIRST; + } +#endif + + m = le32toh(priv->head.mode) & + ~(VIRTIO_SPI_CS_HIGH | VIRTIO_SPI_MODE_LSB_FIRST); + priv->head.mode = htole32(m | flags); + return OK; +} +#endif + +/**************************************************************************** + * Name: virtio_spi_status + ****************************************************************************/ + +static uint8_t virtio_spi_status(FAR struct spi_dev_s *dev, uint32_t devid) +{ + return SPI_STATUS_PRESENT; +} + +/**************************************************************************** + * Name: virtio_spi_send + ****************************************************************************/ + +static uint32_t virtio_spi_send(FAR struct spi_dev_s *dev, uint32_t wd) +{ + uint32_t rxword = 0; + int ret; + + /* Exchange the low wordsize bytes of the word, in native byte order. */ + + ret = virtio_spi_exchange(dev, &wd, &rxword, 1); + return ret < 0 ? 0 : rxword; +} + +/**************************************************************************** + * Name: virtio_spi_exchange + ****************************************************************************/ + +static int virtio_spi_exchange(FAR struct spi_dev_s *dev, + FAR const void *txbuffer, + FAR void *rxbuffer, size_t nwords) +{ + FAR struct virtio_spi_priv_s *priv = (FAR struct virtio_spi_priv_s *)dev; + FAR const uint8_t *tx = txbuffer; + FAR uint8_t *rx = rxbuffer; + size_t wordsize = virtio_spi_wordsize(priv->head.bits_per_word); + uint8_t cs_change = priv->head.cs_change; + size_t largest; + size_t maxdata; + size_t offset = 0; + size_t total; + int ndirs; + int ret; + + /* Split into chunks the transport can map at once; cs_change=0 makes the + * device hold CS between them. virtio_free_largest is SIZE_MAX on + * MMIO/PCI, so this runs as a single zero-copy pass there. + */ + + ndirs = (tx != NULL) + (rx != NULL); + if (ndirs == 0) + { + ndirs = 1; + } + + largest = virtio_free_largest(priv->vdev); + if (largest <= VIRTIO_SPI_REQ_SIZE(0, 0)) + { + return -ENOMEM; + } + + maxdata = (largest - VIRTIO_SPI_REQ_SIZE(0, 0)) / ndirs / wordsize * + wordsize; + if (maxdata == 0) + { + return -ENOMEM; + } + + total = nwords * wordsize; + + do + { + size_t chunk = total - offset; + + /* Intermediate chunks keep CS asserted; the last chunk carries the + * configured after-transfer state. + */ + + priv->head.cs_change = 0; + if (chunk > maxdata) + { + chunk = maxdata; + } + else + { + priv->head.cs_change = cs_change; + } + + ret = virtio_spi_transfer(priv, + tx != NULL ? tx + offset : NULL, + rx != NULL ? rx + offset : NULL, + tx != NULL ? chunk : 0, + rx != NULL ? chunk : 0); + if (ret < 0) + { + priv->head.cs_change = cs_change; + return ret; + } + + offset += chunk; + } + while (offset < total); + + return OK; +} + +#ifdef CONFIG_SPI_TRIGGER + +/**************************************************************************** + * Name: virtio_spi_trigger + ****************************************************************************/ + +static int virtio_spi_trigger(FAR struct spi_dev_s *dev) +{ + return -ENOSYS; +} +#endif + +/**************************************************************************** + * Name: virtio_spi_registercallback + ****************************************************************************/ + +static int virtio_spi_registercallback(FAR struct spi_dev_s *dev, + spi_mediachange_t callback, + FAR void *arg) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Name: virtio_spi_probe + ****************************************************************************/ + +static int virtio_spi_probe(FAR struct virtio_device *vdev) +{ + FAR struct virtio_spi_priv_s *priv; + FAR const char *vqnames[1]; + vq_callback callback[1]; + irqstate_t flags; + int ret; + + priv = kmm_zalloc(sizeof(*priv)); + if (priv == NULL) + { + vrterr("No enough memory\n"); + return -ENOMEM; + } + + priv->spi.ops = &g_virtio_spi_ops; + priv->vdev = vdev; + priv->head.bits_per_word = 8; + priv->head.tx_nbits = VIRTIO_SPI_SINGLE; + priv->head.rx_nbits = VIRTIO_SPI_SINGLE; + priv->head.freq = htole32(1000000); + priv->head.cs_change = 1; /* De-select after transfer */ + vdev->priv = priv; + nxmutex_init(&priv->buslock); + nxsem_init(&priv->done, 0, 0); + + /* Initialize the virtio device */ + + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER); + virtio_set_features(vdev, 0); + virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK); + + vqnames[0] = "virtio_spi_req"; + callback[0] = virtio_spi_done; + ret = virtio_create_virtqueues(vdev, 0, 1, vqnames, callback, NULL); + if (ret < 0) + { + vrterr("virtio_create_virtqueues failed, ret=%d\n", ret); + goto err_with_priv; + } + + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK); + virtqueue_enable_cb(vdev->vrings_info[0].vq); + + /* Read the SPI configuration space exposed by the device */ + + virtio_read_config(vdev, 0, &priv->hwcfg, sizeof(priv->hwcfg)); + + /* Publish the lower-half so board/in-kernel code can fetch it by bus index + * with virtio_spi_get_master(). Exposing it as a /dev/spiN character + * device with spi_register() is left to the board, as for any other SPI + * lower-half. + */ + + flags = spin_lock_irqsave(&g_virtio_spi_bus.lock); + priv->idx = g_virtio_spi_bus.idx++; + dq_addlast(&priv->node, &g_virtio_spi_bus.devices); + spin_unlock_irqrestore(&g_virtio_spi_bus.lock, flags); + + return OK; + +err_with_priv: + nxsem_destroy(&priv->done); + nxmutex_destroy(&priv->buslock); + kmm_free(priv); + return ret; +} + +/**************************************************************************** + * Name: virtio_spi_remove + ****************************************************************************/ + +static void virtio_spi_remove(FAR struct virtio_device *vdev) +{ + FAR struct virtio_spi_priv_s *priv = vdev->priv; + irqstate_t flags; + + flags = spin_lock_irqsave(&g_virtio_spi_bus.lock); + dq_rem(&priv->node, &g_virtio_spi_bus.devices); + spin_unlock_irqrestore(&g_virtio_spi_bus.lock, flags); + + virtio_reset_device(vdev); + virtio_delete_virtqueues(vdev); + nxsem_destroy(&priv->done); + nxmutex_destroy(&priv->buslock); + kmm_free(priv); +} + +/**************************************************************************** + * Name: virtio_spi_dump + ****************************************************************************/ + +static void virtio_spi_dump(FAR struct virtio_device *vdev) +{ + FAR struct virtio_spi_priv_s *priv = vdev->priv; + + syslog(LOG_EMERG, " idx=%d\n", priv->idx); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtio_register_spi_driver + ****************************************************************************/ + +int virtio_register_spi_driver(void) +{ + return virtio_register_driver(&g_virtio_spi_driver); +} + +/**************************************************************************** + * Name: virtio_spi_get_master + * + * Description: + * Return the SPI lower-half probed by the VirtIO SPI frontend for bus + * index, or NULL if no such bus has been probed. Board/in-kernel code + * uses this to obtain the instance and may then pass it to device drivers + * or register it with spi_register() as /dev/spiN. + * + ****************************************************************************/ + +FAR struct spi_dev_s *virtio_spi_get_master(int index) +{ + FAR struct virtio_spi_priv_s *priv; + FAR struct spi_dev_s *spi = NULL; + irqstate_t flags; + + flags = spin_lock_irqsave(&g_virtio_spi_bus.lock); + dq_for_every_entry(&g_virtio_spi_bus.devices, priv, + struct virtio_spi_priv_s, node) + { + if (priv->idx == index) + { + spi = &priv->spi; + break; + } + } + + spin_unlock_irqrestore(&g_virtio_spi_bus.lock, flags); + return spi; +} diff --git a/drivers/virtio/virtio-spi.h b/drivers/virtio/virtio-spi.h new file mode 100644 index 00000000000..0bf0c932fdb --- /dev/null +++ b/drivers/virtio/virtio-spi.h @@ -0,0 +1,55 @@ +/**************************************************************************** + * drivers/virtio/virtio-spi.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __DRIVERS_VIRTIO_VIRTIO_SPI_H +#define __DRIVERS_VIRTIO_VIRTIO_SPI_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_DRIVERS_VIRTIO_SPI + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int virtio_register_spi_driver(void); + +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_DRIVERS_VIRTIO_SPI */ + +#endif /* __DRIVERS_VIRTIO_VIRTIO_SPI_H */ diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 84b68253dde..b1c7d325c47 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -47,6 +47,7 @@ #include "virtio-rpmsg.h" #include "virtio-serial.h" #include "virtio-snd.h" +#include "virtio-spi.h" /**************************************************************************** * Pre-processor Definitions @@ -364,6 +365,14 @@ void virtio_register_drivers(void) } #endif +#ifdef CONFIG_DRIVERS_VIRTIO_SPI + ret = virtio_register_spi_driver(); + if (ret < 0) + { + vrterr("virtio_register_spi_driver failed, ret=%d\n", ret); + } +#endif + UNUSED(ret); } diff --git a/include/nuttx/spi/spi_virtio.h b/include/nuttx/spi/spi_virtio.h new file mode 100644 index 00000000000..188af211559 --- /dev/null +++ b/include/nuttx/spi/spi_virtio.h @@ -0,0 +1,66 @@ +/**************************************************************************** + * include/nuttx/spi/spi_virtio.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SPI_SPI_VIRTIO_H +#define __INCLUDE_NUTTX_SPI_SPI_VIRTIO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/spi/spi.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: virtio_spi_get_master + * + * Description: + * Return the SPI lower-half probed by the VirtIO SPI frontend for bus + * index, or NULL if no such bus has been probed. Board/in-kernel code + * uses this to obtain the instance and may then pass it to device drivers + * or register it with spi_register() as /dev/spiN. + * + * Input Parameters: + * index - The VirtIO SPI bus index assigned at probe time. + * + * Returned Value: + * The matching SPI lower-half, or NULL if not found. + * + ****************************************************************************/ + +FAR struct spi_dev_s *virtio_spi_get_master(int index); + +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_NUTTX_SPI_SPI_VIRTIO_H */ diff --git a/include/nuttx/virtio/virtio-spi.h b/include/nuttx/virtio/virtio-spi.h new file mode 100644 index 00000000000..9aaf1b7a29b --- /dev/null +++ b/include/nuttx/virtio/virtio-spi.h @@ -0,0 +1,262 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio-spi.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_VIRTIO_VIRTIO_SPI_H +#define __INCLUDE_NUTTX_VIRTIO_VIRTIO_SPI_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/compiler.h> +#include <nuttx/spi/spi.h> + +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VIRTIO_ID_SPI 45 + +#define VIRTIO_SPI_CPHA (1 << 0) +#define VIRTIO_SPI_CPOL (1 << 1) +#define VIRTIO_SPI_CS_HIGH (1 << 2) +#define VIRTIO_SPI_MODE_LSB_FIRST (1 << 3) +#define VIRTIO_SPI_MODE_LOOP (1 << 4) + +#define VIRTIO_SPI_TRANS_OK 0 +#define VIRTIO_SPI_PARAM_ERR 1 +#define VIRTIO_SPI_TRANS_ERR 2 + +#define VIRTIO_SPI_RX_TX_SUPPORT_DUAL (1 << 0) +#define VIRTIO_SPI_RX_TX_SUPPORT_QUAD (1 << 1) +#define VIRTIO_SPI_RX_TX_SUPPORT_OCTAL (1 << 2) + +#define VIRTIO_SPI_MF_SUPPORT_CPHA_0 (1 << 0) +#define VIRTIO_SPI_MF_SUPPORT_CPHA_1 (1 << 1) +#define VIRTIO_SPI_MF_SUPPORT_CPOL_0 (1 << 2) +#define VIRTIO_SPI_MF_SUPPORT_CPOL_1 (1 << 3) +#define VIRTIO_SPI_MF_SUPPORT_CS_HIGH (1 << 4) +#define VIRTIO_SPI_MF_SUPPORT_LSB_FIRST (1 << 5) +#define VIRTIO_SPI_MF_SUPPORT_LOOPBACK (1 << 6) + +#define VIRTIO_SPI_SINGLE 1 +#define VIRTIO_SPI_DUAL 2 +#define VIRTIO_SPI_QUAD 4 +#define VIRTIO_SPI_OCTAL 8 + +#define VIRTIO_SPI_REQ_SIZE(txlen, rxlen) \ + (sizeof(struct spi_transfer_head_s) + (txlen) + (rxlen) + \ + sizeof(struct spi_transfer_result_s)) + +/* Max length of the local SPI device node path in the device config space */ + +#define VHOST_SPI_PATH_MAX 64 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* 5.21.4 Device configuration layout */ + +begin_packed_struct struct virtio_spi_config_s +{ + uint8_t cs_max_number; + uint8_t cs_change_supported; + uint8_t tx_nbits_supported; + uint8_t rx_nbits_supported; + uint32_t bits_per_word_mask; + uint32_t mode_func_supported; + uint32_t max_freq_hz; + uint32_t max_word_delay_ns; + uint32_t max_cs_setup_ns; + uint32_t max_cs_hold_ns; + uint32_t max_cs_inactive_ns; +} end_packed_struct; + +/* Device-side private config space extension: the spec config followed by + * the path of the local SPI device node (e.g. "/dev/spi0") this device + * forwards to, filled by the resource table author and read back by the + * vhost SPI backend, which opens it via file_open(). + */ + +begin_packed_struct struct vhost_spi_config_s +{ + struct virtio_spi_config_s config; + char path[VHOST_SPI_PATH_MAX]; +} end_packed_struct; + +/* 5.21.6.1 Device Operation: Request Queue */ + +begin_packed_struct struct spi_transfer_head_s +{ + uint8_t chip_select_id; + uint8_t bits_per_word; + uint8_t cs_change; + uint8_t tx_nbits; + uint8_t rx_nbits; + uint8_t reserved[3]; + uint32_t mode; + uint32_t freq; + uint32_t word_delay_ns; + uint32_t cs_setup_ns; + uint32_t cs_delay_hold_ns; + uint32_t cs_change_delay_inactive_ns; +} end_packed_struct; + +begin_packed_struct struct spi_transfer_result_s +{ + uint8_t result; +} end_packed_struct; + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/* Shared request-buffer layout helpers and status/mode conversions used by + * both the VirtIO SPI frontend (drivers/virtio) and the vhost SPI backend + * (drivers/vhost). The encode (frontend) and decode (backend) routines are + * private to each driver and built on these primitives. + */ + +/* Container bytes per word for a bits-per-word value. */ + +static inline size_t virtio_spi_wordsize(uint8_t nbits) +{ + if (nbits > 16) + { + return sizeof(uint32_t); + } + + return nbits > 8 ? sizeof(uint16_t) : sizeof(uint8_t); +} + +/* Return true if nbits is a supported VirtIO SPI bus width value. */ + +static inline bool virtio_spi_bus_width_valid(uint8_t nbits) +{ + return nbits == 0 || nbits == VIRTIO_SPI_SINGLE || + nbits == VIRTIO_SPI_DUAL || nbits == VIRTIO_SPI_QUAD || + nbits == VIRTIO_SPI_OCTAL; +} + +/* Return the transmit buffer inside a VirtIO SPI request buffer. */ + +static inline FAR uint8_t *virtio_spi_txbuf(FAR void *req) +{ + return (FAR uint8_t *)req + sizeof(struct spi_transfer_head_s); +} + +/* Return the receive buffer inside a VirtIO SPI request buffer. */ + +static inline FAR uint8_t *virtio_spi_rxbuf(FAR void *req, size_t txlen) +{ + return virtio_spi_txbuf(req) + txlen; +} + +/* Return the VirtIO SPI result byte inside a request buffer. */ + +static inline FAR struct spi_transfer_result_s * +virtio_spi_result(FAR void *req, size_t txlen, size_t rxlen) +{ + return (FAR struct spi_transfer_result_s *) + (virtio_spi_rxbuf(req, txlen) + rxlen); +} + +/* Convert a NuttX SPI mode to VirtIO SPI mode flags. */ + +static inline uint32_t virtio_spi_mode(enum spi_mode_e mode) +{ + uint32_t virtio_mode = 0; + + if (mode == SPIDEV_MODE1 || mode == SPIDEV_MODE3) + { + virtio_mode |= VIRTIO_SPI_CPHA; + } + + if (mode == SPIDEV_MODE2 || mode == SPIDEV_MODE3) + { + virtio_mode |= VIRTIO_SPI_CPOL; + } + + return virtio_mode; +} + +/* Convert VirtIO SPI mode flags to a NuttX SPI mode. */ + +static inline enum spi_mode_e virtio_spi_nuttx_mode(uint32_t mode) +{ + switch (mode & (VIRTIO_SPI_CPHA | VIRTIO_SPI_CPOL)) + { + case VIRTIO_SPI_CPHA: + return SPIDEV_MODE1; + + case VIRTIO_SPI_CPOL: + return SPIDEV_MODE2; + + case VIRTIO_SPI_CPHA | VIRTIO_SPI_CPOL: + return SPIDEV_MODE3; + + default: + return SPIDEV_MODE0; + } +} + +/* Convert a VirtIO SPI transfer status to a NuttX errno value. */ + +static inline int virtio_spi_status_to_errno(uint8_t status) +{ + if (status == VIRTIO_SPI_TRANS_OK) + { + return OK; + } + + if (status == VIRTIO_SPI_PARAM_ERR) + { + return -EINVAL; + } + + return -EIO; +} + +/* Convert a NuttX transfer result to a VirtIO SPI status value. */ + +static inline uint8_t virtio_spi_errno_to_status(int ret) +{ + if (ret >= 0) + { + return VIRTIO_SPI_TRANS_OK; + } + + if (ret == -EINVAL || ret == -ENOTSUP || ret == -E2BIG) + { + return VIRTIO_SPI_PARAM_ERR; + } + + return VIRTIO_SPI_TRANS_ERR; +} + +#endif /* __INCLUDE_NUTTX_VIRTIO_VIRTIO_SPI_H */
