forked from zephyrproject-rtos/zephyr
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi_cc13xx_cc26xx.c
333 lines (278 loc) · 8.37 KB
/
spi_cc13xx_cc26xx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (c) 2019 Brett Witherspoon
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_cc13xx_cc26xx_spi
#define LOG_LEVEL CONFIG_SPI_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(spi_cc13xx_cc26xx);
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/policy.h>
#include <driverlib/prcm.h>
#include <driverlib/ssi.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26X2.h>
#include "spi_context.h"
struct spi_cc13xx_cc26xx_config {
uint32_t base;
const struct pinctrl_dev_config *pcfg;
};
struct spi_cc13xx_cc26xx_data {
struct spi_context ctx;
};
#define CPU_FREQ DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency)
static int spi_cc13xx_cc26xx_configure(const struct device *dev,
const struct spi_config *config)
{
const struct spi_cc13xx_cc26xx_config *cfg = dev->config;
struct spi_cc13xx_cc26xx_data *data = dev->data;
struct spi_context *ctx = &data->ctx;
uint32_t prot;
int ret;
if (spi_context_configured(ctx, config)) {
return 0;
}
if (config->operation & SPI_HALF_DUPLEX) {
LOG_ERR("Half-duplex not supported");
return -ENOTSUP;
}
/* Slave mode has not been implemented */
if (SPI_OP_MODE_GET(config->operation) != SPI_OP_MODE_MASTER) {
LOG_ERR("Slave mode is not supported");
return -ENOTSUP;
}
/* Word sizes other than 8 bits has not been implemented */
if (SPI_WORD_SIZE_GET(config->operation) != 8) {
LOG_ERR("Word sizes other than 8 bits are not supported");
return -ENOTSUP;
}
if (config->operation & SPI_TRANSFER_LSB) {
LOG_ERR("Transfer LSB first mode is not supported");
return -EINVAL;
}
if (IS_ENABLED(CONFIG_SPI_EXTENDED_MODES) &&
(config->operation & SPI_LINES_MASK) != SPI_LINES_SINGLE) {
LOG_ERR("Multiple lines are not supported");
return -EINVAL;
}
if (config->operation & SPI_CS_ACTIVE_HIGH && !spi_cs_is_gpio(config)) {
LOG_ERR("Active high CS requires emulation through a GPIO line.");
return -EINVAL;
}
if (config->frequency < 2000000) {
LOG_ERR("Frequencies lower than 2 MHz are not supported");
return -EINVAL;
}
if (2 * config->frequency > CPU_FREQ) {
LOG_ERR("Frequency greater than supported in master mode");
return -EINVAL;
}
if (SPI_MODE_GET(config->operation) & SPI_MODE_CPOL) {
if (SPI_MODE_GET(config->operation) & SPI_MODE_CPHA) {
prot = SSI_FRF_MOTO_MODE_3;
} else {
prot = SSI_FRF_MOTO_MODE_2;
}
} else {
if (SPI_MODE_GET(config->operation) & SPI_MODE_CPHA) {
prot = SSI_FRF_MOTO_MODE_1;
} else {
prot = SSI_FRF_MOTO_MODE_0;
}
}
ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
if (ret < 0) {
LOG_ERR("applying SPI pinctrl state failed");
return ret;
}
ctx->config = config;
/* Disable SSI before making configuration changes */
SSIDisable(cfg->base);
/* Configure SSI */
SSIConfigSetExpClk(cfg->base, CPU_FREQ, prot,
SSI_MODE_MASTER, config->frequency, 8);
if (SPI_MODE_GET(config->operation) & SPI_MODE_LOOP) {
sys_set_bit(cfg->base + SSI_O_CR1, 0);
}
/* Re-enable SSI after making configuration changes */
SSIEnable(cfg->base);
return 0;
}
static int spi_cc13xx_cc26xx_transceive(const struct device *dev,
const struct spi_config *config,
const struct spi_buf_set *tx_bufs,
const struct spi_buf_set *rx_bufs)
{
const struct spi_cc13xx_cc26xx_config *cfg = dev->config;
struct spi_cc13xx_cc26xx_data *data = dev->data;
struct spi_context *ctx = &data->ctx;
uint32_t txd, rxd;
int err;
spi_context_lock(ctx, false, NULL, NULL, config);
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
err = spi_cc13xx_cc26xx_configure(dev, config);
if (err) {
goto done;
}
spi_context_buffers_setup(ctx, tx_bufs, rx_bufs, 1);
spi_context_cs_control(ctx, true);
do {
if (spi_context_tx_buf_on(ctx)) {
txd = *ctx->tx_buf;
} else {
txd = 0U;
}
SSIDataPut(cfg->base, txd);
spi_context_update_tx(ctx, 1, 1);
SSIDataGet(cfg->base, &rxd);
if (spi_context_rx_buf_on(ctx)) {
*ctx->rx_buf = rxd;
}
spi_context_update_rx(ctx, 1, 1);
} while (spi_context_tx_on(ctx) || spi_context_rx_on(ctx));
spi_context_cs_control(ctx, false);
done:
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
spi_context_release(ctx, err);
return err;
}
static int spi_cc13xx_cc26xx_release(const struct device *dev,
const struct spi_config *config)
{
const struct spi_cc13xx_cc26xx_config *cfg = dev->config;
struct spi_cc13xx_cc26xx_data *data = dev->data;
struct spi_context *ctx = &data->ctx;
if (!spi_context_configured(ctx, config)) {
return -EINVAL;
}
if (SSIBusy(cfg->base)) {
return -EBUSY;
}
spi_context_unlock_unconditionally(ctx);
return 0;
}
#ifdef CONFIG_PM_DEVICE
static int spi_cc13xx_cc26xx_pm_action(const struct device *dev,
enum pm_device_action action)
{
const struct spi_cc13xx_cc26xx_config *config = dev->config;
switch (action) {
case PM_DEVICE_ACTION_RESUME:
if (config->base == DT_INST_REG_ADDR(0)) {
Power_setDependency(PowerCC26XX_PERIPH_SSI0);
} else {
Power_setDependency(PowerCC26XX_PERIPH_SSI1);
}
break;
case PM_DEVICE_ACTION_SUSPEND:
SSIDisable(config->base);
/*
* Release power dependency
*/
if (config->base == DT_INST_REG_ADDR(0)) {
Power_releaseDependency(PowerCC26XX_PERIPH_SSI0);
} else {
Power_releaseDependency(PowerCC26XX_PERIPH_SSI1);
}
break;
default:
return -ENOTSUP;
}
return 0;
}
#endif /* CONFIG_PM_DEVICE */
static const struct spi_driver_api spi_cc13xx_cc26xx_driver_api = {
.transceive = spi_cc13xx_cc26xx_transceive,
.release = spi_cc13xx_cc26xx_release,
};
#ifdef CONFIG_PM
#define SPI_CC13XX_CC26XX_POWER_SPI(n) \
do { \
/* Set Power dependencies & constraints */ \
if (DT_INST_REG_ADDR(n) == 0x40000000) { \
Power_setDependency(PowerCC26XX_PERIPH_SSI0); \
} else { \
Power_setDependency(PowerCC26XX_PERIPH_SSI1); \
} \
} while (false)
#else
#define SPI_CC13XX_CC26XX_POWER_SPI(n) \
do { \
uint32_t domain, periph; \
\
/* Enable UART power domain */ \
if (DT_INST_REG_ADDR(n) == 0x40000000) { \
domain = PRCM_DOMAIN_SERIAL; \
periph = PRCM_PERIPH_SSI0; \
} else { \
domain = PRCM_DOMAIN_PERIPH; \
periph = PRCM_PERIPH_SSI1; \
} \
/* Enable SSI##n power domain */ \
PRCMPowerDomainOn(domain); \
\
/* Enable SSI##n peripherals */ \
PRCMPeripheralRunEnable(periph); \
PRCMPeripheralSleepEnable(periph); \
PRCMPeripheralDeepSleepEnable(periph); \
\
/* Load PRCM settings */ \
PRCMLoadSet(); \
while (!PRCMLoadGet()) { \
continue; \
} \
\
/* SSI should not be accessed until power domain is on. */\
while (PRCMPowerDomainsAllOn(domain) != \
PRCM_DOMAIN_POWER_ON) { \
continue; \
} \
} while (false)
#endif
#define SPI_CC13XX_CC26XX_DEVICE_INIT(n) \
PM_DEVICE_DT_INST_DEFINE(n, spi_cc13xx_cc26xx_pm_action); \
\
DEVICE_DT_INST_DEFINE(n, \
spi_cc13xx_cc26xx_init_##n, \
PM_DEVICE_DT_INST_GET(n), \
&spi_cc13xx_cc26xx_data_##n, &spi_cc13xx_cc26xx_config_##n, \
POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
&spi_cc13xx_cc26xx_driver_api)
#define SPI_CC13XX_CC26XX_INIT_FUNC(n) \
static int spi_cc13xx_cc26xx_init_##n(const struct device *dev) \
{ \
struct spi_cc13xx_cc26xx_data *data = dev->data; \
int err; \
SPI_CC13XX_CC26XX_POWER_SPI(n); \
\
err = spi_context_cs_configure_all(&data->ctx); \
if (err < 0) { \
return err; \
} \
\
spi_context_unlock_unconditionally(&data->ctx); \
\
return 0; \
}
#define SPI_CC13XX_CC26XX_INIT(n) \
PINCTRL_DT_INST_DEFINE(n); \
SPI_CC13XX_CC26XX_INIT_FUNC(n) \
\
static const struct spi_cc13xx_cc26xx_config \
spi_cc13xx_cc26xx_config_##n = { \
.base = DT_INST_REG_ADDR(n), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n) \
}; \
\
static struct spi_cc13xx_cc26xx_data \
spi_cc13xx_cc26xx_data_##n = { \
SPI_CONTEXT_INIT_LOCK(spi_cc13xx_cc26xx_data_##n, ctx), \
SPI_CONTEXT_INIT_SYNC(spi_cc13xx_cc26xx_data_##n, ctx), \
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx) \
}; \
\
SPI_CC13XX_CC26XX_DEVICE_INIT(n);
DT_INST_FOREACH_STATUS_OKAY(SPI_CC13XX_CC26XX_INIT)