summaryrefslogtreecommitdiff
path: root/chip/stm32/spi_master-stm32h7.c
blob: 4195dc595ab871f4ef06b5350bb3487969d649de (plain)
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
/*
 * Copyright 2017 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * SPI master driver.
 */

#include "common.h"
#include "dma.h"
#include "gpio.h"
#include "shared_mem.h"
#include "spi.h"
#include "stm32-dma.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* SPI ports are used as master */
static stm32_spi_regs_t *SPI_REGS[] = {
#ifdef CONFIG_STM32_SPI1_CONTROLLER
	STM32_SPI1_REGS,
#endif
	STM32_SPI2_REGS,
	STM32_SPI3_REGS,
	STM32_SPI4_REGS,
};

/* DMA request mapping on channels */
static uint8_t dma_req_tx[ARRAY_SIZE(SPI_REGS)] = {
#ifdef CONFIG_STM32_SPI1_CONTROLLER
	DMAMUX1_REQ_SPI1_TX,
#endif
	DMAMUX1_REQ_SPI2_TX,
	DMAMUX1_REQ_SPI3_TX,
	DMAMUX1_REQ_SPI4_TX,
};
static uint8_t dma_req_rx[ARRAY_SIZE(SPI_REGS)] = {
#ifdef CONFIG_STM32_SPI1_CONTROLLER
	DMAMUX1_REQ_SPI1_RX,
#endif
	DMAMUX1_REQ_SPI2_RX,
	DMAMUX1_REQ_SPI3_RX,
	DMAMUX1_REQ_SPI4_RX,
};

static struct mutex spi_mutex[ARRAY_SIZE(SPI_REGS)];

#define SPI_TRANSACTION_TIMEOUT_USEC (800 * MSEC)

static const struct dma_option dma_tx_option[] = {
#ifdef CONFIG_STM32_SPI1_CONTROLLER
	{
		STM32_DMAC_SPI1_TX, (void *)&STM32_SPI1_REGS->txdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
#endif
	{
		STM32_DMAC_SPI2_TX, (void *)&STM32_SPI2_REGS->txdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
	{
		STM32_DMAC_SPI3_TX, (void *)&STM32_SPI3_REGS->txdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
	{
		STM32_DMAC_SPI4_TX, (void *)&STM32_SPI4_REGS->txdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
};

static const struct dma_option dma_rx_option[] = {
#ifdef CONFIG_STM32_SPI1_CONTROLLER
	{
		STM32_DMAC_SPI1_RX, (void *)&STM32_SPI1_REGS->rxdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
#endif
	{
		STM32_DMAC_SPI2_RX, (void *)&STM32_SPI2_REGS->rxdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
	{
		STM32_DMAC_SPI3_RX, (void *)&STM32_SPI3_REGS->rxdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
	{
		STM32_DMAC_SPI4_RX, (void *)&STM32_SPI4_REGS->rxdr,
		STM32_DMA_CCR_MSIZE_8_BIT | STM32_DMA_CCR_PSIZE_8_BIT
	},
};

static uint8_t spi_enabled[ARRAY_SIZE(SPI_REGS)];

/**
 * Initialize SPI module, registers, and clocks
 * @param spi_device  device to initialize.
 */
static void spi_master_config(const struct spi_device_t *spi_device)
{
	int port = spi_device->port;

	stm32_spi_regs_t *spi = SPI_REGS[port];

	/*
	 * Set SPI master, baud rate, and software slave control.
	 */
	spi->cr1 = STM32_SPI_CR1_SSI;
	spi->cfg2 = STM32_SPI_CFG2_MSTR | STM32_SPI_CFG2_SSM |
		    STM32_SPI_CFG2_AFCNTR;
	spi->cfg1 = STM32_SPI_CFG1_DATASIZE(8) | STM32_SPI_CFG1_FTHLV(4) |
		    STM32_SPI_CFG1_CRCSIZE(8) |
		    STM32_SPI_CR1_DIV(spi_device->div);

	dma_select_channel(dma_tx_option[port].channel, dma_req_tx[port]);
	dma_select_channel(dma_rx_option[port].channel, dma_req_rx[port]);
}

static int spi_master_initialize(const struct spi_device_t *spi_device)
{
	spi_master_config(spi_device);

	gpio_set_level(spi_device->gpio_cs, 1);

	/* Set flag */
	spi_enabled[spi_device->port] = 1;

	return EC_SUCCESS;
}

/**
 * Shutdown SPI module
 */
static int spi_master_shutdown(const struct spi_device_t *spi_device)
{
	int rv = EC_SUCCESS;
	int port = spi_device->port;
	stm32_spi_regs_t *spi = SPI_REGS[port];

	/* Set flag */
	spi_enabled[port] = 0;

	/* Disable DMA streams */
	dma_disable(dma_tx_option[port].channel);
	dma_disable(dma_rx_option[port].channel);

	/* Disable SPI */
	spi->cr1 &= ~STM32_SPI_CR1_SPE;

	/* Disable DMA buffers */
	spi->cfg1 &= ~(STM32_SPI_CFG1_TXDMAEN | STM32_SPI_CFG1_RXDMAEN);

	return rv;
}

int spi_enable(const struct spi_device_t *spi_device, int enable)
{
	int port = spi_device->port;
	if (enable == spi_enabled[port])
		return EC_SUCCESS;
	if (enable)
		return spi_master_initialize(spi_device);
	else
		return spi_master_shutdown(spi_device);
}

static int spi_dma_start(const struct spi_device_t *spi_device,
			 const uint8_t *txdata, uint8_t *rxdata, int len)
{
	dma_chan_t *txdma;
	int port = spi_device->port;
	stm32_spi_regs_t *spi = SPI_REGS[port];

	/*
	 * Workaround for STM32H7 errata: without resetting the SPI controller,
	 * the RX DMA requests will happen too early on the 2nd transfer.
	 */
	STM32_RCC_APB2RSTR = STM32_RCC_PB2_SPI4;
	STM32_RCC_APB2RSTR = 0;
	dma_clear_isr(dma_tx_option[port].channel);
	dma_clear_isr(dma_rx_option[port].channel);
	/* restore proper SPI configuration registers. */
	spi_master_config(spi_device);

	spi->cr2 = len;
	spi->cfg1 |= STM32_SPI_CFG1_RXDMAEN;
	/* Set up RX DMA */
	if (rxdata)
		dma_start_rx(&dma_rx_option[port], len, rxdata);

	/* Set up TX DMA */
	if (txdata) {
		txdma = dma_get_channel(dma_tx_option[port].channel);
		dma_prepare_tx(&dma_tx_option[port], len, txdata);
		dma_go(txdma);
	}

	spi->cfg1 |= STM32_SPI_CFG1_TXDMAEN;
	spi->cr1 |= STM32_SPI_CR1_SPE;
	spi->cr1 |= STM32_SPI_CR1_CSTART;

	return EC_SUCCESS;
}

static inline bool dma_is_enabled_(const struct dma_option *option)
{
	return dma_is_enabled(dma_get_channel(option->channel));
}

static int spi_dma_wait(int port)
{
	timestamp_t timeout;
	stm32_spi_regs_t *spi = SPI_REGS[port];
	int rv = EC_SUCCESS;

	/* Wait for DMA transmission to complete */
	if (dma_is_enabled_(&dma_tx_option[port])) {
		rv = dma_wait(dma_tx_option[port].channel);
		if (rv)
			return rv;

		timeout.val = get_time().val + SPI_TRANSACTION_TIMEOUT_USEC;
		/* Wait for FIFO empty and BSY bit clear */
		while (!(spi->sr & (STM32_SPI_SR_TXC)))
			if (get_time().val > timeout.val)
				return EC_ERROR_TIMEOUT;

		/* Disable TX DMA */
		dma_disable(dma_tx_option[port].channel);
	}

	/* Wait for DMA reception to complete */
	if (dma_is_enabled_(&dma_rx_option[port])) {
		rv = dma_wait(dma_rx_option[port].channel);
		if (rv)
			return rv;

		timeout.val = get_time().val + SPI_TRANSACTION_TIMEOUT_USEC;
		/* Wait for FRLVL[1:0] to indicate FIFO empty */
		while (spi->sr & (STM32_SPI_SR_FRLVL | STM32_SPI_SR_RXNE))
			if (get_time().val > timeout.val)
				return EC_ERROR_TIMEOUT;

		/* Disable RX DMA */
		dma_disable(dma_rx_option[port].channel);
	}

	spi->cr1 &= ~STM32_SPI_CR1_SPE;
	spi->cfg1 &= ~(STM32_SPI_CFG1_TXDMAEN | STM32_SPI_CFG1_RXDMAEN);

	return rv;
}

int spi_transaction_async(const struct spi_device_t *spi_device,
			  const uint8_t *txdata, int txlen,
			  uint8_t *rxdata, int rxlen)
{
	int rv = EC_SUCCESS;
	int port = spi_device->port;
	int full_readback = 0;

	char *buf = NULL;

#ifndef CONFIG_SPI_HALFDUPLEX
	if (rxlen == SPI_READBACK_ALL) {
		buf = rxdata;
		full_readback = 1;
	} else {
		rv = shared_mem_acquire(MAX(txlen, rxlen), &buf);
		if (rv != EC_SUCCESS)
			return rv;
	}
#endif

	/* Drive SS low */
	gpio_set_level(spi_device->gpio_cs, 0);

	rv = spi_dma_start(spi_device, txdata, buf, txlen);
	if (rv != EC_SUCCESS)
		goto err_free;

	if (full_readback)
		return EC_SUCCESS;

	if (rxlen) {
		rv = spi_dma_wait(port);
		if (rv != EC_SUCCESS)
			goto err_free;

		rv = spi_dma_start(spi_device, buf, rxdata, rxlen);
		if (rv != EC_SUCCESS)
			goto err_free;
	}

err_free:
	if (!full_readback)
		shared_mem_release(buf);
	return rv;
}

int spi_transaction_flush(const struct spi_device_t *spi_device)
{
	int rv = spi_dma_wait(spi_device->port);

	/* Drive SS high */
	gpio_set_level(spi_device->gpio_cs, 1);

	return rv;
}

int spi_transaction_wait(const struct spi_device_t *spi_device)
{
	return spi_dma_wait(spi_device->port);
}

int spi_transaction(const struct spi_device_t *spi_device,
		    const uint8_t *txdata, int txlen,
		    uint8_t *rxdata, int rxlen)
{
	int rv;
	int port = spi_device->port;

	mutex_lock(spi_mutex + port);
	rv = spi_transaction_async(spi_device, txdata, txlen, rxdata, rxlen);
	rv |= spi_transaction_flush(spi_device);
	mutex_unlock(spi_mutex + port);

	return rv;
}