|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Bang & Olufsen A/S, Denmark |
| 3 | + * Copyright (c) 2025 Linumiz GmbH |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#define DT_DRV_COMPAT ti_mspm0_spi |
| 9 | + |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/drivers/clock_control.h> |
| 12 | +#include <zephyr/drivers/clock_control/mspm0_clock_control.h> |
| 13 | +#include <zephyr/drivers/pinctrl.h> |
| 14 | +#include <zephyr/drivers/spi.h> |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | + |
| 17 | +/* TI DriverLib includes */ |
| 18 | +#include <driverlib/dl_spi.h> |
| 19 | + |
| 20 | +LOG_MODULE_REGISTER(spi_mspm0, CONFIG_SPI_LOG_LEVEL); |
| 21 | + |
| 22 | +/* This must be included after log module registration */ |
| 23 | +#include "spi_context.h" |
| 24 | + |
| 25 | +/* Data Frame Size (DFS) */ |
| 26 | +#define SPI_DFS_8BIT 1 |
| 27 | +#define SPI_DFS_16BIT 2 |
| 28 | + |
| 29 | +/* Range for SPI Serial Clock Rate (SCR) */ |
| 30 | +#define MSPM0_SPI_SCR_MIN 0 |
| 31 | +#define MSPM0_SPI_SCR_MAX 1023 |
| 32 | + |
| 33 | +/* Delay after enabling power for SPI module */ |
| 34 | +#define POWER_STARTUP_DELAY 16 |
| 35 | + |
| 36 | +#define SPI_DT_CLK_DIV(inst) \ |
| 37 | + DT_INST_PROP(inst, clk_div) |
| 38 | + |
| 39 | +#define SPI_DT_CLK_DIV_ENUM(inst) \ |
| 40 | + _CONCAT(DL_SPI_CLOCK_DIVIDE_RATIO_, SPI_DT_CLK_DIV(inst)) |
| 41 | + |
| 42 | +#define SPI_MODE(operation) \ |
| 43 | + (operation & BIT(0) ? DL_SPI_MODE_PERIPHERAL : DL_SPI_MODE_CONTROLLER) |
| 44 | + |
| 45 | +#define BIT_ORDER_MODE(operation) \ |
| 46 | + (operation & BIT(4) ? DL_SPI_BIT_ORDER_LSB_FIRST : DL_SPI_BIT_ORDER_MSB_FIRST) |
| 47 | + |
| 48 | +/* MSPM0 DSS field expects word size - 1 */ |
| 49 | +#define DATA_SIZE_MODE(operation) \ |
| 50 | + (SPI_WORD_SIZE_GET(operation) - 1) |
| 51 | + |
| 52 | +#define POLARITY_MODE(operation) \ |
| 53 | + (SPI_MODE_GET(operation) & SPI_MODE_CPOL ? SPI_CTL0_SPO_HIGH : SPI_CTL0_SPO_LOW) |
| 54 | + |
| 55 | +#define PHASE_MODE(operation) \ |
| 56 | + (SPI_MODE_GET(operation) & SPI_MODE_CPHA ? SPI_CTL0_SPH_SECOND : SPI_CTL0_SPH_FIRST) |
| 57 | + |
| 58 | +#define DUPLEX_MODE(operation) \ |
| 59 | + (operation & BIT(11) ? SPI_CTL0_FRF_MOTOROLA_3WIRE : SPI_CTL0_FRF_MOTOROLA_4WIRE) |
| 60 | + |
| 61 | +/* TI uses fixed frame format; Motorola combines duplex, polarity, phase */ |
| 62 | +#define FRAME_FORMAT_MODE(operation) \ |
| 63 | + (operation & SPI_FRAME_FORMAT_TI \ |
| 64 | + ? SPI_CTL0_FRF_TI_SYNC \ |
| 65 | + : DUPLEX_MODE(operation) | POLARITY_MODE(operation) | PHASE_MODE(operation)) |
| 66 | + |
| 67 | +/* Computes the minimum number of bytes per frame using word_size. Utilizes ceil |
| 68 | + * division, to ensure sufficient byte allocation for non-multiples of 8 bits |
| 69 | + */ |
| 70 | +#define BYTES_PER_FRAME(word_size) (((word_size) + 7) / 8) |
| 71 | + |
| 72 | +struct spi_mspm0_config { |
| 73 | + SPI_Regs *base; |
| 74 | + uint32_t clock_frequency; |
| 75 | + const struct pinctrl_dev_config *pinctrl; |
| 76 | + |
| 77 | + const DL_SPI_ClockConfig clock_config; |
| 78 | +}; |
| 79 | + |
| 80 | +struct spi_mspm0_data { |
| 81 | + struct spi_context ctx; |
| 82 | +}; |
| 83 | + |
| 84 | +static int spi_mspm0_configure(const struct device *dev, const struct spi_config *spi_cfg, |
| 85 | + uint8_t dfs) |
| 86 | +{ |
| 87 | + const struct spi_mspm0_config *const cfg = dev->config; |
| 88 | + struct spi_mspm0_data *const data = dev->data; |
| 89 | + uint16_t clock_scr; |
| 90 | + |
| 91 | + if (spi_context_configured(&data->ctx, spi_cfg)) { |
| 92 | + /* This configuration is already in use */ |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + /* Only master mode is supported */ |
| 97 | + if (SPI_OP_MODE_GET(spi_cfg->operation) != SPI_OP_MODE_MASTER) { |
| 98 | + return -ENOTSUP; |
| 99 | + } |
| 100 | + |
| 101 | + if (spi_cfg->frequency > (cfg->clock_frequency / 2)) { |
| 102 | + return -EINVAL; |
| 103 | + } |
| 104 | + |
| 105 | + /* See DL_SPI_setBitRateSerialClockDivider for details */ |
| 106 | + clock_scr = (cfg->clock_frequency / (2 * spi_cfg->frequency)) - 1; |
| 107 | + if (!IN_RANGE(clock_scr, MSPM0_SPI_SCR_MIN, MSPM0_SPI_SCR_MAX)) { |
| 108 | + return -EINVAL; |
| 109 | + } |
| 110 | + |
| 111 | + const DL_SPI_Config dl_cfg = { |
| 112 | + .parity = DL_SPI_PARITY_NONE, /* Currently unused in zephyr */ |
| 113 | + .chipSelectPin = DL_SPI_CHIP_SELECT_NONE, /* spi_context controls the CS */ |
| 114 | + .mode = SPI_MODE(spi_cfg->operation), |
| 115 | + .bitOrder = BIT_ORDER_MODE(spi_cfg->operation), |
| 116 | + .dataSize = DATA_SIZE_MODE(spi_cfg->operation), |
| 117 | + .frameFormat = FRAME_FORMAT_MODE(spi_cfg->operation), |
| 118 | + }; |
| 119 | + |
| 120 | + /* Peripheral should be disabled before applying a new configuration */ |
| 121 | + DL_SPI_disable(cfg->base); |
| 122 | + |
| 123 | + DL_SPI_init(cfg->base, (DL_SPI_Config *)&dl_cfg); |
| 124 | + DL_SPI_setBitRateSerialClockDivider(cfg->base, (uint32_t)clock_scr); |
| 125 | + |
| 126 | + if (dfs > SPI_DFS_16BIT) { |
| 127 | + DL_SPI_enablePacking(cfg->base); |
| 128 | + } else { |
| 129 | + DL_SPI_disablePacking(cfg->base); |
| 130 | + } |
| 131 | + |
| 132 | + if (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_LOOP) { |
| 133 | + DL_SPI_enableLoopbackMode(cfg->base); |
| 134 | + } else { |
| 135 | + DL_SPI_disableLoopbackMode(cfg->base); |
| 136 | + } |
| 137 | + |
| 138 | + DL_SPI_enable(cfg->base); |
| 139 | + |
| 140 | + /* Cache SPI config for reuse, required by spi_context owner */ |
| 141 | + data->ctx.config = spi_cfg; |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static void spi_mspm0_frame_tx(const struct device *dev, uint8_t dfs) |
| 147 | +{ |
| 148 | + const struct spi_mspm0_config *cfg = dev->config; |
| 149 | + struct spi_mspm0_data *data = dev->data; |
| 150 | + |
| 151 | + /* Transmit dummy frame when no TX data is provided */ |
| 152 | + uint32_t tx_frame = 0; |
| 153 | + |
| 154 | + if (spi_context_tx_buf_on(&data->ctx)) { |
| 155 | + if (dfs == SPI_DFS_8BIT) { |
| 156 | + tx_frame = UNALIGNED_GET((uint8_t *)(data->ctx.tx_buf)); |
| 157 | + } else if (dfs == SPI_DFS_16BIT) { |
| 158 | + tx_frame = UNALIGNED_GET((uint16_t *)(data->ctx.tx_buf)); |
| 159 | + } else { |
| 160 | + tx_frame = UNALIGNED_GET((uint32_t *)(data->ctx.tx_buf)); |
| 161 | + } |
| 162 | + } |
| 163 | + DL_SPI_transmitDataCheck32(cfg->base, tx_frame); |
| 164 | + |
| 165 | + while (DL_SPI_isBusy(cfg->base)) { |
| 166 | + /* Wait for tx fifo to be sent */ |
| 167 | + } |
| 168 | + |
| 169 | + spi_context_update_tx(&data->ctx, dfs, 1); |
| 170 | +} |
| 171 | + |
| 172 | +static void spi_mspm0_frame_rx(const struct device *dev, uint8_t dfs) |
| 173 | +{ |
| 174 | + const struct spi_mspm0_config *cfg = dev->config; |
| 175 | + struct spi_mspm0_data *data = dev->data; |
| 176 | + uint32_t rx_val = 0; |
| 177 | + |
| 178 | + DL_SPI_receiveDataCheck32(cfg->base, &rx_val); |
| 179 | + |
| 180 | + if (!spi_context_rx_buf_on(&data->ctx)) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (dfs == SPI_DFS_8BIT) { |
| 185 | + UNALIGNED_PUT((uint8_t)rx_val, (uint8_t *)data->ctx.rx_buf); |
| 186 | + } else if (dfs == SPI_DFS_16BIT) { |
| 187 | + UNALIGNED_PUT((uint16_t)rx_val, (uint16_t *)data->ctx.rx_buf); |
| 188 | + } else { |
| 189 | + UNALIGNED_PUT(rx_val, (uint32_t *)data->ctx.rx_buf); |
| 190 | + } |
| 191 | + |
| 192 | + spi_context_update_rx(&data->ctx, dfs, 1); |
| 193 | +} |
| 194 | + |
| 195 | +static void spi_mspm0_start_transfer(const struct device *dev, const struct spi_config *spi_cfg, |
| 196 | + uint8_t dfs) |
| 197 | +{ |
| 198 | + struct spi_mspm0_data *data = dev->data; |
| 199 | + |
| 200 | + spi_context_cs_control(&data->ctx, true); |
| 201 | + |
| 202 | + while (spi_context_tx_on(&data->ctx) || spi_context_rx_on(&data->ctx)) { |
| 203 | + spi_mspm0_frame_tx(dev, dfs); |
| 204 | + spi_mspm0_frame_rx(dev, dfs); |
| 205 | + } |
| 206 | + |
| 207 | + spi_context_cs_control(&data->ctx, false); |
| 208 | + spi_context_complete(&data->ctx, dev, 0); |
| 209 | +} |
| 210 | + |
| 211 | +static int spi_mspm0_transceive(const struct device *dev, |
| 212 | + const struct spi_config *spi_cfg, |
| 213 | + const struct spi_buf_set *tx_bufs, |
| 214 | + const struct spi_buf_set *rx_bufs) |
| 215 | +{ |
| 216 | + struct spi_mspm0_data *data = dev->data; |
| 217 | + int ret; |
| 218 | + uint8_t dfs; |
| 219 | + |
| 220 | + if (!tx_bufs && !rx_bufs) { |
| 221 | + return 0; |
| 222 | + } |
| 223 | + |
| 224 | + spi_context_lock(&data->ctx, false, NULL, NULL, spi_cfg); |
| 225 | + |
| 226 | + dfs = BYTES_PER_FRAME(SPI_WORD_SIZE_GET(spi_cfg->operation)); |
| 227 | + |
| 228 | + ret = spi_mspm0_configure(dev, spi_cfg, dfs); |
| 229 | + if (ret != 0) { |
| 230 | + spi_context_release(&data->ctx, ret); |
| 231 | + return ret; |
| 232 | + } |
| 233 | + |
| 234 | + spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, dfs); |
| 235 | + |
| 236 | + spi_mspm0_start_transfer(dev, spi_cfg, dfs); |
| 237 | + |
| 238 | + ret = spi_context_wait_for_completion(&data->ctx); |
| 239 | + spi_context_release(&data->ctx, ret); |
| 240 | + |
| 241 | + return ret; |
| 242 | +} |
| 243 | + |
| 244 | +static int spi_mspm0_release(const struct device *dev, const struct spi_config *config) |
| 245 | +{ |
| 246 | + const struct spi_mspm0_config *cfg = dev->config; |
| 247 | + struct spi_mspm0_data *data = dev->data; |
| 248 | + |
| 249 | + if (!spi_context_configured(&data->ctx, config)) { |
| 250 | + return -EINVAL; |
| 251 | + } |
| 252 | + |
| 253 | + if (DL_SPI_isBusy(cfg->base)) { |
| 254 | + return -EBUSY; |
| 255 | + } |
| 256 | + |
| 257 | + spi_context_unlock_unconditionally(&data->ctx); |
| 258 | + return 0; |
| 259 | +} |
| 260 | + |
| 261 | +static const struct spi_driver_api spi_mspm0_api = { |
| 262 | + .transceive = spi_mspm0_transceive, |
| 263 | + .release = spi_mspm0_release, |
| 264 | +}; |
| 265 | + |
| 266 | +static int spi_mspm0_init(const struct device *dev) |
| 267 | +{ |
| 268 | + const struct spi_mspm0_config *cfg = dev->config; |
| 269 | + struct spi_mspm0_data *data = dev->data; |
| 270 | + int ret; |
| 271 | + |
| 272 | + DL_SPI_enablePower(cfg->base); |
| 273 | + delay_cycles(POWER_STARTUP_DELAY); |
| 274 | + |
| 275 | + ret = pinctrl_apply_state(cfg->pinctrl, PINCTRL_STATE_DEFAULT); |
| 276 | + if (ret < 0) { |
| 277 | + return ret; |
| 278 | + } |
| 279 | + |
| 280 | + ret = spi_context_cs_configure_all(&data->ctx); |
| 281 | + if (ret < 0) { |
| 282 | + return ret; |
| 283 | + } |
| 284 | + |
| 285 | + DL_SPI_setClockConfig(cfg->base, (DL_SPI_ClockConfig *)&cfg->clock_config); |
| 286 | + DL_SPI_enable(cfg->base); |
| 287 | + |
| 288 | + spi_context_unlock_unconditionally(&data->ctx); |
| 289 | + |
| 290 | + return ret; |
| 291 | +} |
| 292 | + |
| 293 | +#define MSPM0_SPI_INIT(inst) \ |
| 294 | + PINCTRL_DT_INST_DEFINE(inst); \ |
| 295 | + \ |
| 296 | + static struct spi_mspm0_config spi_mspm0_##inst##_cfg = { \ |
| 297 | + .base = (SPI_Regs *)DT_INST_REG_ADDR(inst), \ |
| 298 | + .pinctrl = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ |
| 299 | + .clock_config = {.clockSel = \ |
| 300 | + MSPM0_CLOCK_PERIPH_REG_MASK(DT_INST_CLOCKS_CELL(inst, clk)), \ |
| 301 | + .divideRatio = SPI_DT_CLK_DIV_ENUM(inst)}, \ |
| 302 | + .clock_frequency = DT_PROP(DT_INST_CLOCKS_CTLR(inst), clock_frequency), \ |
| 303 | + }; \ |
| 304 | + \ |
| 305 | + static struct spi_mspm0_data spi_mspm0_##inst##_data = { \ |
| 306 | + SPI_CONTEXT_INIT_LOCK(spi_mspm0_##inst##_data, ctx), \ |
| 307 | + SPI_CONTEXT_INIT_SYNC(spi_mspm0_##inst##_data, ctx), \ |
| 308 | + SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(inst), ctx)}; \ |
| 309 | + \ |
| 310 | + DEVICE_DT_INST_DEFINE(inst, spi_mspm0_init, NULL, &spi_mspm0_##inst##_data, \ |
| 311 | + &spi_mspm0_##inst##_cfg, POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \ |
| 312 | + &spi_mspm0_api); |
| 313 | + |
| 314 | +DT_INST_FOREACH_STATUS_OKAY(MSPM0_SPI_INIT) |
0 commit comments