-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
mspi_ambiq.c
253 lines (200 loc) · 6.89 KB
/
mspi_ambiq.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
/*
* Copyright (c) 2023 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ambiq_mspi
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mspi_ambiq);
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/kernel.h>
#include "spi_context.h"
#include <am_mcu_apollo.h>
#define SPI_WORD_SIZE 8
#define MSPI_MAX_FREQ 96000000
#define MSPI_TIMEOUT_US 1000000
#define PWRCTRL_MAX_WAIT_US 5
#define MSPI_BUSY BIT(2)
typedef int (*ambiq_mspi_pwr_func_t)(void);
struct mspi_ambiq_config {
uint32_t base;
int size;
uint32_t clock_freq;
const struct pinctrl_dev_config *pcfg;
ambiq_mspi_pwr_func_t pwr_func;
};
struct mspi_ambiq_data {
struct spi_context ctx;
void *mspiHandle;
am_hal_mspi_dev_config_t mspicfg;
};
static int mspi_set_freq(uint32_t freq)
{
uint32_t d = MSPI_MAX_FREQ / freq;
switch (d) {
case AM_HAL_MSPI_CLK_96MHZ:
case AM_HAL_MSPI_CLK_48MHZ:
case AM_HAL_MSPI_CLK_32MHZ:
case AM_HAL_MSPI_CLK_24MHZ:
case AM_HAL_MSPI_CLK_16MHZ:
case AM_HAL_MSPI_CLK_12MHZ:
case AM_HAL_MSPI_CLK_8MHZ:
case AM_HAL_MSPI_CLK_6MHZ:
case AM_HAL_MSPI_CLK_4MHZ:
case AM_HAL_MSPI_CLK_3MHZ:
break;
default:
LOG_ERR("Frequency not supported!");
d = AM_HAL_MSPI_CLK_INVALID;
break;
}
return d;
}
static int mspi_config(const struct device *dev, const struct spi_config *config)
{
struct mspi_ambiq_data *data = dev->data;
int ret;
am_hal_mspi_dev_config_t mspicfg = {0};
if (config->operation & SPI_HALF_DUPLEX) {
LOG_ERR("Half-duplex not supported");
return -ENOTSUP;
}
if (SPI_WORD_SIZE_GET(config->operation) != 8) {
LOG_ERR("Word size must be %d", SPI_WORD_SIZE);
return -ENOTSUP;
}
if ((config->operation & SPI_LINES_MASK) != SPI_LINES_SINGLE) {
LOG_ERR("Only single mode is currently supported");
return -ENOTSUP;
}
if (config->operation & SPI_LOCK_ON) {
LOG_ERR("Lock On not supported");
return -ENOTSUP;
}
if (config->operation & SPI_TRANSFER_LSB) {
LOG_ERR("LSB first not supported");
return -ENOTSUP;
}
if (config->operation & (SPI_MODE_CPOL | SPI_MODE_CPHA)) {
if (config->operation & (SPI_MODE_CPOL && SPI_MODE_CPHA)) {
mspicfg.eSpiMode = AM_HAL_MSPI_SPI_MODE_3;
} else if (config->operation & SPI_MODE_CPOL) {
mspicfg.eSpiMode = AM_HAL_MSPI_SPI_MODE_2;
} else if (config->operation & SPI_MODE_CPHA) {
mspicfg.eSpiMode = AM_HAL_MSPI_SPI_MODE_1;
} else {
mspicfg.eSpiMode = AM_HAL_MSPI_SPI_MODE_0;
}
}
mspicfg.eClockFreq = mspi_set_freq(config->frequency);
if (mspicfg.eClockFreq == AM_HAL_MSPI_CLK_INVALID) {
return -ENOTSUP;
}
mspicfg.eDeviceConfig = AM_HAL_MSPI_FLASH_SERIAL_CE0;
ret = am_hal_mspi_disable(data->mspiHandle);
if (ret) {
return ret;
}
ret = am_hal_mspi_device_configure(data->mspiHandle, &mspicfg);
if (ret) {
return ret;
}
ret = am_hal_mspi_enable(data->mspiHandle);
return ret;
}
static int mspi_ambiq_xfer(const struct device *dev, const struct spi_config *config)
{
struct mspi_ambiq_data *data = dev->data;
struct spi_context *ctx = &data->ctx;
int ret;
am_hal_mspi_pio_transfer_t trans = {0};
trans.bSendAddr = true;
trans.bSendInstr = true;
trans.ui16DeviceInstr = *ctx->tx_buf;
spi_context_update_tx(ctx, 1, 1);
trans.ui32DeviceAddr = *ctx->tx_buf;
if (ctx->rx_buf != NULL) {
spi_context_update_rx(ctx, 1, ctx->rx_len);
trans.eDirection = AM_HAL_MSPI_RX;
trans.pui32Buffer = (uint32_t *)ctx->rx_buf;
trans.ui32NumBytes = ctx->rx_len;
} else if (ctx->tx_buf != NULL) {
spi_context_update_tx(ctx, 1, 1);
trans.eDirection = AM_HAL_MSPI_TX;
trans.pui32Buffer = (uint32_t *)ctx->tx_buf;
trans.ui32NumBytes = ctx->tx_len;
}
ret = am_hal_mspi_blocking_transfer(data->mspiHandle, &trans, MSPI_TIMEOUT_US);
spi_context_complete(ctx, dev, 0);
return ret;
}
static int mspi_ambiq_transceive(const struct device *dev, const struct spi_config *config,
const struct spi_buf_set *tx_bufs,
const struct spi_buf_set *rx_bufs)
{
struct mspi_ambiq_data *data = dev->data;
int ret = mspi_config(dev, config);
if (ret) {
return ret;
}
if (!tx_bufs && !rx_bufs) {
return 0;
}
spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, 1);
ret = mspi_ambiq_xfer(dev, config);
return ret;
}
static int mspi_ambiq_release(const struct device *dev, const struct spi_config *config)
{
const struct mspi_ambiq_config *cfg = dev->config;
if (sys_read32(cfg->base) & MSPI_BUSY) {
return -EBUSY;
}
return 0;
}
static const struct spi_driver_api mspi_ambiq_driver_api = {
.transceive = mspi_ambiq_transceive,
.release = mspi_ambiq_release,
};
static int mspi_ambiq_init(const struct device *dev)
{
struct mspi_ambiq_data *data = dev->data;
const struct mspi_ambiq_config *cfg = dev->config;
am_hal_mspi_config_t mspiCfg = {0};
mspiCfg.pTCB = NULL;
int ret = am_hal_mspi_initialize((cfg->base - REG_MSPI_BASEADDR) / (cfg->size * 4),
&data->mspiHandle);
if (ret) {
return ret;
}
ret = cfg->pwr_func();
ret = am_hal_mspi_configure(data->mspiHandle, &mspiCfg);
if (ret) {
return ret;
}
ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
return ret;
}
#define AMBIQ_MSPI_DEFINE(n) \
PINCTRL_DT_INST_DEFINE(n); \
static int pwr_on_ambiq_mspi_##n(void) \
{ \
uint32_t addr = DT_REG_ADDR(DT_INST_PHANDLE(n, ambiq_pwrcfg)) + \
DT_INST_PHA(n, ambiq_pwrcfg, offset); \
sys_write32((sys_read32(addr) | DT_INST_PHA(n, ambiq_pwrcfg, mask)), addr); \
k_busy_wait(PWRCTRL_MAX_WAIT_US); \
return 0; \
} \
static struct mspi_ambiq_data mspi_ambiq_data##n = { \
SPI_CONTEXT_INIT_SYNC(mspi_ambiq_data##n, ctx)}; \
static const struct mspi_ambiq_config mspi_ambiq_config##n = { \
.base = DT_INST_REG_ADDR(n), \
.size = DT_INST_REG_SIZE(n), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
.pwr_func = pwr_on_ambiq_mspi_##n, \
}; \
DEVICE_DT_INST_DEFINE(n, mspi_ambiq_init, NULL, &mspi_ambiq_data##n, \
&mspi_ambiq_config##n, POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
&mspi_ambiq_driver_api);
DT_INST_FOREACH_STATUS_OKAY(AMBIQ_MSPI_DEFINE)