|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 MASSDRIVER EI (massdriver.space) |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#define DT_DRV_COMPAT wch_spi |
| 7 | + |
| 8 | +#define LOG_LEVEL CONFIG_SPI_LOG_LEVEL |
| 9 | +#include <zephyr/logging/log.h> |
| 10 | +LOG_MODULE_REGISTER(spi_wch); |
| 11 | + |
| 12 | +#include "spi_context.h" |
| 13 | +#include <errno.h> |
| 14 | +#include <zephyr/device.h> |
| 15 | +#include <zephyr/drivers/spi.h> |
| 16 | +#include <zephyr/drivers/spi/rtio.h> |
| 17 | +#include <zephyr/drivers/pinctrl.h> |
| 18 | +#include <zephyr/drivers/clock_control.h> |
| 19 | + |
| 20 | +#include <hal_ch32fun.h> |
| 21 | + |
| 22 | +#define SPI_CTLR1_LSBFIRST BIT(7) |
| 23 | +#define SPI_CTLR1_BR_POS 3 |
| 24 | + |
| 25 | +struct spi_wch_config { |
| 26 | + SPI_TypeDef *regs; |
| 27 | + const struct pinctrl_dev_config *pin_cfg; |
| 28 | + const struct device *clk_dev; |
| 29 | + uint8_t clock_id; |
| 30 | +}; |
| 31 | + |
| 32 | +struct spi_wch_data { |
| 33 | + struct spi_context ctx; |
| 34 | +}; |
| 35 | + |
| 36 | +static uint8_t spi_wch_get_br(uint32_t target_clock_ratio) |
| 37 | +{ |
| 38 | + uint8_t prescaler; |
| 39 | + int prescaler_val = 2; |
| 40 | + |
| 41 | + for (prescaler = 0; prescaler < 7; prescaler++) { |
| 42 | + if (prescaler_val > target_clock_ratio) { |
| 43 | + break; |
| 44 | + } |
| 45 | + prescaler_val *= 2; |
| 46 | + } |
| 47 | + |
| 48 | + return prescaler; |
| 49 | +} |
| 50 | + |
| 51 | +static int spi_wch_configure(const struct device *dev, const struct spi_config *config) |
| 52 | +{ |
| 53 | + const struct spi_wch_config *cfg = dev->config; |
| 54 | + struct spi_wch_data *data = dev->data; |
| 55 | + SPI_TypeDef *regs = cfg->regs; |
| 56 | + int err; |
| 57 | + uint32_t clock_rate; |
| 58 | + clock_control_subsys_t clk_sys; |
| 59 | + int8_t prescaler; |
| 60 | + |
| 61 | + if (spi_context_configured(&data->ctx, config)) { |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + if ((config->operation & SPI_HALF_DUPLEX) != 0U) { |
| 66 | + LOG_ERR("Half-duplex not supported"); |
| 67 | + return -ENOTSUP; |
| 68 | + } |
| 69 | + |
| 70 | + if (SPI_OP_MODE_GET(config->operation) != SPI_OP_MODE_MASTER) { |
| 71 | + LOG_ERR("Slave mode not supported"); |
| 72 | + return -ENOTSUP; |
| 73 | + } |
| 74 | + |
| 75 | + if ((config->operation & SPI_MODE_LOOP) != 0U) { |
| 76 | + LOG_ERR("Loop mode not supported"); |
| 77 | + return -ENOTSUP; |
| 78 | + } |
| 79 | + |
| 80 | + if (SPI_WORD_SIZE_GET(config->operation) != 8) { |
| 81 | + LOG_ERR("Frame size != 8 bits not supported"); |
| 82 | + return -ENOTSUP; |
| 83 | + } |
| 84 | + |
| 85 | + regs->CTLR1 = 0; |
| 86 | + regs->CTLR2 = 0; |
| 87 | + regs->STATR = 0; |
| 88 | + |
| 89 | + if (spi_cs_is_gpio(config)) { |
| 90 | + /* When using soft NSS, SSI must be set high */ |
| 91 | + regs->CTLR1 |= SPI_CTLR1_SSM | SPI_CTLR1_SSI; |
| 92 | + } else { |
| 93 | + regs->CTLR2 |= SPI_CTLR2_SSOE; |
| 94 | + } |
| 95 | + |
| 96 | + regs->CTLR1 |= SPI_CTLR1_MSTR; |
| 97 | + |
| 98 | + if ((config->operation & SPI_TRANSFER_LSB) != 0U) { |
| 99 | + regs->CTLR1 |= SPI_CTLR1_LSBFIRST; |
| 100 | + } |
| 101 | + |
| 102 | + if ((config->operation & SPI_MODE_CPOL) != 0U) { |
| 103 | + regs->CTLR1 |= SPI_CTLR1_CPOL; |
| 104 | + } |
| 105 | + |
| 106 | + if ((config->operation & SPI_MODE_CPHA) != 0U) { |
| 107 | + regs->CTLR1 |= SPI_CTLR1_CPHA; |
| 108 | + } |
| 109 | + |
| 110 | + clk_sys = (clock_control_subsys_t)(uintptr_t)cfg->clock_id; |
| 111 | + err = clock_control_get_rate(cfg->clk_dev, clk_sys, &clock_rate); |
| 112 | + if (err != 0) { |
| 113 | + return err; |
| 114 | + } |
| 115 | + |
| 116 | + /* Approximate clock rate given ratios available */ |
| 117 | + prescaler = spi_wch_get_br(clock_rate / config->frequency); |
| 118 | +#if CONFIG_SPI_LOG_LEVEL >= LOG_LEVEL_INF |
| 119 | + uint32_t j = 2; |
| 120 | + |
| 121 | + for (int i = 0; i < prescaler; i++) { |
| 122 | + j = j * 2; |
| 123 | + } |
| 124 | + LOG_INF("Selected divider %d, value %d, results in %d frequency", j, prescaler, |
| 125 | + clock_rate / j); |
| 126 | +#endif |
| 127 | + regs->CTLR1 |= prescaler << SPI_CTLR1_BR_POS; |
| 128 | + |
| 129 | + data->ctx.config = config; |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +static int spi_wch_transceive(const struct device *dev, const struct spi_config *config, |
| 135 | + const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs) |
| 136 | +{ |
| 137 | + const struct spi_wch_config *cfg = dev->config; |
| 138 | + struct spi_wch_data *data = dev->data; |
| 139 | + SPI_TypeDef *regs = cfg->regs; |
| 140 | + int err; |
| 141 | + uint8_t rx; |
| 142 | + |
| 143 | + spi_context_lock(&data->ctx, false, NULL, NULL, config); |
| 144 | + |
| 145 | + err = spi_wch_configure(dev, config); |
| 146 | + if (err != 0) { |
| 147 | + goto done; |
| 148 | + } |
| 149 | + |
| 150 | + spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, 1); |
| 151 | + |
| 152 | + spi_context_cs_control(&data->ctx, true); |
| 153 | + |
| 154 | + /* Start SPI *AFTER* setting CS */ |
| 155 | + regs->CTLR1 |= SPI_CTLR1_SPE; |
| 156 | + |
| 157 | + while (spi_context_tx_on(&data->ctx) || spi_context_rx_on(&data->ctx)) { |
| 158 | + if (spi_context_tx_buf_on(&data->ctx)) { |
| 159 | + while ((regs->STATR & SPI_STATR_TXE) == 0U) { |
| 160 | + } |
| 161 | + regs->DATAR = *(uint8_t *)(data->ctx.tx_buf); |
| 162 | + } else { |
| 163 | + while ((regs->STATR & SPI_STATR_TXE) == 0U) { |
| 164 | + } |
| 165 | + regs->DATAR = 0; |
| 166 | + } |
| 167 | + spi_context_update_tx(&data->ctx, 1, 1); |
| 168 | + while ((regs->STATR & SPI_STATR_RXNE) == 0U) { |
| 169 | + } |
| 170 | + rx = regs->DATAR; |
| 171 | + if (spi_context_rx_buf_on(&data->ctx)) { |
| 172 | + *data->ctx.rx_buf = rx; |
| 173 | + } |
| 174 | + spi_context_update_rx(&data->ctx, 1, 1); |
| 175 | + } |
| 176 | + |
| 177 | +done: |
| 178 | + regs->CTLR1 &= ~(SPI_CTLR1_SPE); |
| 179 | + spi_context_cs_control(&data->ctx, false); |
| 180 | + spi_context_release(&data->ctx, err); |
| 181 | + return err; |
| 182 | +} |
| 183 | + |
| 184 | +static int spi_wch_transceive_sync(const struct device *dev, const struct spi_config *config, |
| 185 | + const struct spi_buf_set *tx_bufs, |
| 186 | + const struct spi_buf_set *rx_bufs) |
| 187 | +{ |
| 188 | + return spi_wch_transceive(dev, config, tx_bufs, rx_bufs); |
| 189 | +} |
| 190 | + |
| 191 | +static int spi_wch_release(const struct device *dev, const struct spi_config *config) |
| 192 | +{ |
| 193 | + struct spi_wch_data *data = dev->data; |
| 194 | + |
| 195 | + spi_context_unlock_unconditionally(&data->ctx); |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +static int spi_wch_init(const struct device *dev) |
| 201 | +{ |
| 202 | + int err; |
| 203 | + const struct spi_wch_config *cfg = dev->config; |
| 204 | + struct spi_wch_data *data = dev->data; |
| 205 | + clock_control_subsys_t clk_sys; |
| 206 | + |
| 207 | + clk_sys = (clock_control_subsys_t)(uintptr_t)cfg->clock_id; |
| 208 | + |
| 209 | + err = clock_control_on(cfg->clk_dev, clk_sys); |
| 210 | + if (err < 0) { |
| 211 | + return err; |
| 212 | + } |
| 213 | + |
| 214 | + err = pinctrl_apply_state(cfg->pin_cfg, PINCTRL_STATE_DEFAULT); |
| 215 | + if (err < 0) { |
| 216 | + return err; |
| 217 | + } |
| 218 | + |
| 219 | + err = spi_context_cs_configure_all(&data->ctx); |
| 220 | + if (err < 0) { |
| 221 | + return err; |
| 222 | + } |
| 223 | + |
| 224 | + spi_context_unlock_unconditionally(&data->ctx); |
| 225 | + |
| 226 | + return 0; |
| 227 | +} |
| 228 | + |
| 229 | +static DEVICE_API(spi, spi_wch_driver_api) = { |
| 230 | + .transceive = spi_wch_transceive_sync, |
| 231 | +#ifdef CONFIG_SPI_RTIO |
| 232 | + .iodev_submit = spi_rtio_iodev_default_submit, |
| 233 | +#endif |
| 234 | + .release = spi_wch_release, |
| 235 | +}; |
| 236 | + |
| 237 | +#define SPI_WCH_DEVICE_INIT(n) \ |
| 238 | + PINCTRL_DT_INST_DEFINE(n); \ |
| 239 | + static const struct spi_wch_config spi_wch_config_##n = { \ |
| 240 | + .regs = (SPI_TypeDef *)DT_INST_REG_ADDR(n), \ |
| 241 | + .clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \ |
| 242 | + .pin_cfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \ |
| 243 | + .clock_id = DT_INST_CLOCKS_CELL(n, id)}; \ |
| 244 | + static struct spi_wch_data spi_wch_dev_data_##n = { \ |
| 245 | + SPI_CONTEXT_INIT_LOCK(spi_wch_dev_data_##n, ctx), \ |
| 246 | + SPI_CONTEXT_INIT_SYNC(spi_wch_dev_data_##n, ctx), \ |
| 247 | + SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx)}; \ |
| 248 | + SPI_DEVICE_DT_INST_DEFINE(n, spi_wch_init, NULL, &spi_wch_dev_data_##n, \ |
| 249 | + &spi_wch_config_##n, POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \ |
| 250 | + &spi_wch_driver_api); |
| 251 | + |
| 252 | +DT_INST_FOREACH_STATUS_OKAY(SPI_WCH_DEVICE_INIT) |
0 commit comments