virtio-spi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/kthread.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/idr.h>
  10. #include <linux/wait.h>
  11. #include <linux/types.h>
  12. #include <linux/virtio.h>
  13. #include <uapi/linux/virtio_ids.h>
  14. #include <linux/virtio_config.h>
  15. #include <linux/spi/spi.h>
  16. /* Virtio ID of SPI: 0xC009 */
  17. #define VIRTIO_ID_SPI 49161
  18. #define VIRTIO_CONFIG_SPI_BUS_NUMBER 0
  19. #define VIRTIO_CONFIG_SPI_CS_MAX_NUMBER 4
  20. #define VIRTIO_SPI_MSG_OK 0
  21. /**
  22. * the structure define the transfer configuration:
  23. *
  24. * mode: how data is clocked out and in
  25. * freq: transfer clock rate
  26. * slave_id: chip select index the transfer used
  27. * bits_per_word: transfer word size
  28. * word_delay_usecs: how long to wait between words within one transfer
  29. * cs_change: deselect device before starting the next transfer
  30. */
  31. struct spi_transfer_head {
  32. u32 mode;
  33. u32 freq;
  34. u8 slave_id;
  35. u8 bits_per_word;
  36. u8 word_delay_usecs;
  37. u8 cs_change;
  38. };
  39. /**
  40. * the structure define the transfer result:
  41. *
  42. * result: return value from backend
  43. */
  44. struct spi_transfer_end {
  45. u8 result;
  46. };
  47. /**
  48. * the structure sent to the backend, including 2 parts:
  49. * -- head: the configuration of the transfer
  50. * -- end: the transfer result set by the backend
  51. *
  52. * note: don't need allocate transfer buffer for the request struct.
  53. * virtio-spi based on spidev driver, when open the spidev device,
  54. * the driver will allocate the tx_buf and rx_buf for spidev device.
  55. * For more details, please refer to the spidev_open function in spidev.c
  56. */
  57. struct virtio_spi_req {
  58. struct spi_transfer_head head;
  59. struct spi_transfer_end end;
  60. };
  61. /**
  62. * struct virtio_spi - virtio spi device
  63. * @spi: spi controller
  64. * @vdev: the virtio device
  65. * @spi_req: description of the fromat of transfer data
  66. * @vq: spi virtqueue
  67. */
  68. struct virtio_spi {
  69. struct spi_master *spi;
  70. struct virtio_device *vdev;
  71. struct virtio_spi_req spi_req;
  72. struct virtqueue *vq;
  73. wait_queue_head_t inq;
  74. };
  75. /* virtqueue incoming data interrupt IRQ */
  76. static void virtspi_vq_isr(struct virtqueue *vq)
  77. {
  78. struct virtio_spi *vspi = vq->vdev->priv;
  79. wake_up(&vspi->inq);
  80. }
  81. static int virtspi_init_vqs(struct virtio_spi *vspi)
  82. {
  83. struct virtqueue *vqs[1];
  84. vq_callback_t *cbs[] = { virtspi_vq_isr };
  85. static const char * const names[] = { "virtspi_vq_isr" };
  86. int err;
  87. err = virtio_find_vqs(vspi->vdev, 1, vqs, cbs, names, NULL);
  88. if (err)
  89. return err;
  90. vspi->vq = vqs[0];
  91. return 0;
  92. }
  93. static void virtspi_del_vqs(struct virtio_spi *vspi)
  94. {
  95. vspi->vdev->config->del_vqs(vspi->vdev);
  96. }
  97. /**
  98. * transfer one message to the backend
  99. */
  100. static int virtio_spi_transfer_one(struct spi_master *spi, struct spi_device *slv,
  101. struct spi_transfer *xfer)
  102. {
  103. struct virtio_spi *vspi = spi_controller_get_devdata(spi);
  104. struct virtio_spi_req *spi_req = &vspi->spi_req;
  105. struct scatterlist outhdr, tx_bufhdr, rx_bufhdr, inhdr, *sgs[4];
  106. unsigned int num_out = 0, num_in = 0, len;
  107. int err = 0;
  108. struct virtqueue *vq = vspi->vq;
  109. if ((xfer->tx_buf == NULL) && (xfer->rx_buf == NULL)) {
  110. dev_err(&vspi->vdev->dev, "Invalid transfer buffer.\n");
  111. return -EINVAL;
  112. }
  113. if (xfer->len < 1) {
  114. dev_err(&vspi->vdev->dev, "Invalid transfer length.\n");
  115. return -EINVAL;
  116. }
  117. spi_req->head.slave_id = slv->chip_select;
  118. spi_req->head.mode = slv->mode;
  119. spi_req->head.freq = xfer->speed_hz;
  120. if (xfer->bits_per_word == 0)
  121. spi_req->head.bits_per_word = slv->bits_per_word;
  122. else
  123. spi_req->head.bits_per_word = xfer->bits_per_word;
  124. spi_req->head.cs_change = xfer->cs_change;
  125. /* init the head queue */
  126. sg_init_one(&outhdr, &spi_req->head, sizeof(spi_req->head));
  127. sgs[num_out++] = &outhdr;
  128. if (xfer->tx_buf != NULL) {
  129. /* init the tx_buf queue */
  130. sg_init_one(&tx_bufhdr, xfer->tx_buf, xfer->len);
  131. sgs[num_out++] = &tx_bufhdr;
  132. }
  133. if (xfer->rx_buf != NULL) {
  134. /* init the rx_buf queue */
  135. sg_init_one(&rx_bufhdr, xfer->rx_buf, xfer->len);
  136. sgs[num_out + num_in++] = &rx_bufhdr;
  137. }
  138. /* init the result queue */
  139. sg_init_one(&inhdr, &spi_req->end, sizeof(spi_req->end));
  140. sgs[num_out + num_in++] = &inhdr;
  141. /* call the virtqueue function */
  142. err = virtqueue_add_sgs(vq, sgs, num_out, num_in, spi_req, GFP_KERNEL);
  143. if (err)
  144. return -EIO;
  145. /* Tell Host to go! */
  146. err = virtqueue_kick(vq);
  147. if (!err)
  148. return -EIO;
  149. wait_event(vspi->inq, virtqueue_get_buf(vq, &len));
  150. if (spi_req->end.result != VIRTIO_SPI_MSG_OK)
  151. err = -EIO;
  152. else
  153. err = 0;
  154. return err;
  155. }
  156. /**
  157. * used to allocate and register the spi controller
  158. */
  159. static int virtspi_init_hw(struct virtio_device *vdev,
  160. struct virtio_spi *vspi)
  161. {
  162. int err;
  163. /* allocate the spi controller */
  164. vspi->spi = __spi_alloc_controller(&vdev->dev, 0, 0);
  165. if (!vspi->spi)
  166. return -ENOMEM;
  167. spi_controller_set_devdata(vspi->spi, vspi);
  168. vspi->spi->dev.of_node = vdev->dev.parent->of_node;
  169. /* read the bus number from the config space with offset 0 */
  170. vspi->spi->bus_num = virtio_cread32(vdev, VIRTIO_CONFIG_SPI_BUS_NUMBER);
  171. /* read the max chip select number from the config space with offset 4 */
  172. vspi->spi->num_chipselect = virtio_cread32(vdev, VIRTIO_CONFIG_SPI_CS_MAX_NUMBER);
  173. vspi->spi->mode_bits = (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH
  174. | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP
  175. | SPI_NO_CS | SPI_READY | SPI_TX_DUAL
  176. | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
  177. vspi->spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  178. vspi->spi->transfer_one = virtio_spi_transfer_one;
  179. vspi->spi->auto_runtime_pm = false;
  180. /* register the spi controller */
  181. err = spi_register_controller(vspi->spi);
  182. if (err)
  183. return err;
  184. return 0;
  185. }
  186. static int virtspi_probe(struct virtio_device *vdev)
  187. {
  188. struct virtio_spi *vspi;
  189. int err = 0;
  190. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  191. return -ENODEV;
  192. vspi = kzalloc(sizeof(*vspi), GFP_KERNEL);
  193. if (!vspi)
  194. return -ENOMEM;
  195. vspi->vdev = vdev;
  196. vdev->priv = vspi;
  197. init_waitqueue_head(&vspi->inq);
  198. err = virtspi_init_vqs(vspi);
  199. if (err)
  200. goto err_init_vq;
  201. virtio_device_ready(vdev);
  202. virtqueue_enable_cb(vspi->vq);
  203. err = virtspi_init_hw(vdev, vspi);
  204. if (err)
  205. goto err_init_hw;
  206. return 0;
  207. err_init_hw:
  208. virtspi_del_vqs(vspi);
  209. err_init_vq:
  210. kfree(vspi);
  211. return err;
  212. }
  213. static void virtspi_remove(struct virtio_device *vdev)
  214. {
  215. struct virtio_spi *vspi = vdev->priv;
  216. spi_unregister_controller(vspi->spi);
  217. vdev->config->reset(vdev);
  218. virtspi_del_vqs(vspi);
  219. kfree(vspi);
  220. }
  221. static unsigned int features[] = {
  222. /* none */
  223. };
  224. static struct virtio_device_id id_table[] = {
  225. { VIRTIO_ID_SPI, VIRTIO_DEV_ANY_ID },
  226. { },
  227. };
  228. MODULE_DEVICE_TABLE(virtio, id_table);
  229. static struct virtio_driver virtio_spi_driver = {
  230. .driver.name = KBUILD_MODNAME,
  231. .driver.owner = THIS_MODULE,
  232. .feature_table = features,
  233. .feature_table_size = ARRAY_SIZE(features),
  234. .id_table = id_table,
  235. .probe = virtspi_probe,
  236. .remove = virtspi_remove,
  237. };
  238. module_virtio_driver(virtio_spi_driver);
  239. MODULE_DESCRIPTION("Virtio spi frontend driver");
  240. MODULE_LICENSE("GPL");