1 /*
2 * Copyright (c) 2024 Vogl Electronic GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/spi.h>
9 #include <zephyr/drivers/spi/rtio.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/sys_clock.h>
12
13 #include "spi_context.h"
14 #include <soc.h>
15
get_dfs_value(const struct spi_config * config)16 static inline uint8_t get_dfs_value(const struct spi_config *config)
17 {
18 switch (SPI_WORD_SIZE_GET(config->operation)) {
19 case 8:
20 return 1;
21 case 16:
22 return 2;
23 case 24:
24 return 3;
25 case 32:
26 return 4;
27 default:
28 return 1;
29 }
30 }
31
litex_spi_tx_put(uint8_t len,uint32_t * txd,const uint8_t * tx_buf)32 static inline void litex_spi_tx_put(uint8_t len, uint32_t *txd, const uint8_t *tx_buf)
33 {
34 switch (len) {
35 case 4:
36 *txd = sys_get_be32(tx_buf);
37 break;
38 case 3:
39 *txd = sys_get_be24(tx_buf);
40 break;
41 case 2:
42 *txd = sys_get_be16(tx_buf);
43 break;
44 default:
45 *txd = *tx_buf;
46 break;
47 }
48 }
49
litex_spi_rx_put(uint8_t len,uint32_t * rxd,uint8_t * rx_buf)50 static inline void litex_spi_rx_put(uint8_t len, uint32_t *rxd, uint8_t *rx_buf)
51 {
52 switch (len) {
53 case 4:
54 sys_put_be32(*rxd, rx_buf);
55 break;
56 case 3:
57 sys_put_be24(*rxd, rx_buf);
58 break;
59 case 2:
60 sys_put_be16(*rxd, rx_buf);
61 break;
62 default:
63 *rx_buf = *rxd;
64 break;
65 }
66 }
67