summaryrefslogtreecommitdiff
path: root/chip/mchp/gpspi.c
blob: 5234db82603208e9f4fad0081cef67f121d55511 (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
/* 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.
 */

/* General Purpose SPI master module for MCHP MEC */

#include "common.h"
#include "console.h"
#include "dma.h"
#include "gpio.h"
#include "registers.h"
#include "spi.h"
#include "timer.h"
#include "util.h"
#include "hooks.h"
#include "task.h"
#include "spi_chip.h"
#include "gpspi_chip.h"
#include "tfdp_chip.h"

#define CPUTS(outstr) cputs(CC_SPI, outstr)
#define CPRINTS(format, args...) cprints(CC_SPI, format, ## args)

#define SPI_BYTE_TRANSFER_TIMEOUT_US (3 * MSEC)
/* One byte at 12 MHz full duplex = 0.67 us */
#define SPI_BYTE_TRANSFER_POLL_INTERVAL_US 20

/*
 * GP-SPI
 */

/**
 * Return zero based GPSPI controller index given hardware port.
 * @param hw_port b[7:4]==1 (GPSPI), b[3:0]=0(GPSPI0), 1(GPSPI1)
 * @return 0(GPSPI0) or 1(GPSPI1)
 */
static uint8_t gpspi_port_to_ctrl_id(uint8_t hw_port)
{
	return (hw_port & 0x01);
}

static int gpspi_wait_byte(const int ctrl)
{
	timestamp_t deadline;

	deadline.val = get_time().val + SPI_BYTE_TRANSFER_TIMEOUT_US;
	while ((MCHP_SPI_SR(ctrl) & 0x3) != 0x3) {
		if (timestamp_expired(deadline, NULL))
			return EC_ERROR_TIMEOUT;
		usleep(SPI_BYTE_TRANSFER_POLL_INTERVAL_US);
	}
	return EC_SUCCESS;
}

/* NOTE: auto-read must be disabled before calling this routine! */
static void gpspi_rx_fifo_clean(const int ctrl)
{
	uint8_t unused = 0;

	/* If ACTIVE and/or RXFF then clean it */
	if ((MCHP_SPI_SR(ctrl) & 0x4) == 0x4)
		unused += MCHP_SPI_RD(ctrl);

	if ((MCHP_SPI_SR(ctrl) & 0x2) == 0x2)
		unused += MCHP_SPI_RD(ctrl);
}
/*
 * NOTE: auto-read must be disabled before calling this routine!
 */
#ifndef CONFIG_MCHP_GPSPI_TX_DMA
static int gpspi_tx(const int ctrl, const uint8_t *txdata, int txlen)
{
	int i;
	int ret;
	uint8_t unused = 0;

	gpspi_rx_fifo_clean(ctrl);

	ret = EC_SUCCESS;
	for (i = 0; i < txlen; ++i) {
		MCHP_SPI_TD(ctrl) = txdata[i];
		ret = gpspi_wait_byte(ctrl);
		if (ret != EC_SUCCESS)
			break;
		unused += MCHP_SPI_RD(ctrl);
	}

	return ret;
}
#endif

int gpspi_transaction_async(const struct spi_device_t *spi_device,
				const uint8_t *txdata, int txlen,
				uint8_t *rxdata, int rxlen)
{
	int hw_port, ctrl;
	int ret = EC_SUCCESS;
	int cs_asserted = 0;
	const struct dma_option *opdma;
#ifdef CONFIG_MCHP_GPSPI_TX_DMA
	dma_chan_t *chan;
#endif
	if (spi_device == NULL)
		return EC_ERROR_PARAM1;

	hw_port = spi_device->port;

	ctrl = gpspi_port_to_ctrl_id(hw_port);

	/* Disable auto read */
	MCHP_SPI_CR(ctrl) &= ~BIT(5);

	if ((txdata != NULL) && (txdata != 0)) {
#ifdef CONFIG_MCHP_GPSPI_TX_DMA
		opdma = spi_dma_option(spi_device, SPI_DMA_OPTION_WR);
		if (opdma == NULL)
			return EC_ERROR_INVAL;

		gpspi_rx_fifo_clean(ctrl);

		dma_prepare_tx(opdma, txlen, txdata);

		chan = dma_get_channel(opdma->channel);

		gpio_set_level(spi_device->gpio_cs, 0);
		cs_asserted = 1;

		dma_go(chan);
		ret = dma_wait(opdma->channel);
		if (ret == EC_SUCCESS)
			ret = gpspi_wait_byte(ctrl);

		dma_disable(opdma->channel);
		dma_clear_isr(opdma->channel);

		gpspi_rx_fifo_clean(ctrl);
#else
		gpio_set_level(spi_device->gpio_cs, 0);
		cs_asserted = 1;

		ret = gpspi_tx(ctrl, txdata, txlen);
#endif
	}

	if (ret == EC_SUCCESS)
		if ((rxlen != 0) && (rxdata != NULL)) {
			ret = EC_ERROR_INVAL;
			opdma = spi_dma_option(spi_device, SPI_DMA_OPTION_RD);
			if (opdma != NULL) {
				if (!cs_asserted)
					gpio_set_level(spi_device->gpio_cs, 0);
				/* Enable auto read */
				MCHP_SPI_CR(ctrl) |= BIT(5);
				dma_start_rx(opdma, rxlen, rxdata);
				MCHP_SPI_TD(ctrl) = 0;
				ret = EC_SUCCESS;
			}
		}

	return ret;
}

int gpspi_transaction_flush(const struct spi_device_t *spi_device)
{
	int ctrl, hw_port;
	int ret;
	enum dma_channel chan;
	const struct dma_option *opdma;
	timestamp_t deadline;

	if (spi_device == NULL)
		return EC_ERROR_PARAM1;

	hw_port = spi_device->port;
	ctrl = gpspi_port_to_ctrl_id(hw_port);
	opdma = spi_dma_option(spi_device, SPI_DMA_OPTION_RD);
	chan = opdma->channel;

	ret = dma_wait(chan);

	/* Disable auto read */
	MCHP_SPI_CR(ctrl) &= ~BIT(5);

	deadline.val = get_time().val + SPI_BYTE_TRANSFER_TIMEOUT_US;
	/* Wait for FIFO empty SPISR_TXBE */
	while ((MCHP_SPI_SR(ctrl) & 0x01) != 0x1) {
		if (timestamp_expired(deadline, NULL)) {
			ret = EC_ERROR_TIMEOUT;
			break;
		}
		usleep(SPI_BYTE_TRANSFER_POLL_INTERVAL_US);
	}

	dma_disable(chan);
	dma_clear_isr(chan);
	if (MCHP_SPI_SR(ctrl) & 0x2)
		hw_port = MCHP_SPI_RD(ctrl);

	gpio_set_level(spi_device->gpio_cs, 1);

	return ret;
}

int gpspi_transaction_wait(const struct spi_device_t *spi_device)
{
	const struct dma_option *opdma;

	opdma = spi_dma_option(spi_device, SPI_DMA_OPTION_RD);

	return dma_wait(opdma->channel);
}

/**
 * Enable GPSPI controller and MODULE_SPI_CONTROLLER pins
 *
 * @param hw_port b[7:4]=1 b[3:0]=0(GPSPI0), 1(GPSPI1)
 * @param enable
 * @return EC_SUCCESS or EC_ERROR_INVAL if port is unrecognized
 * @note called from mec1701/spi.c
 *
 */
int gpspi_enable(int hw_port, int enable)
{
	uint32_t ctrl;

	if ((hw_port != GPSPI0_PORT) && (hw_port != GPSPI1_PORT))
		return EC_ERROR_INVAL;

	gpio_config_module(MODULE_SPI_CONTROLLER, (enable > 0));

	ctrl = (uint32_t)hw_port & 0x0f;

	if (enable) {

		if (ctrl)
			MCHP_PCR_SLP_DIS_DEV(MCHP_PCR_GPSPI1);
		else
			MCHP_PCR_SLP_DIS_DEV(MCHP_PCR_GPSPI0);

		/* Set enable bit in SPI_AR */
		MCHP_SPI_AR(ctrl) |= 0x1;

		/* Set SPDIN to 0 -> Full duplex */
		MCHP_SPI_CR(ctrl) &= ~(0x3 << 2);

		/* Set CLKPOL, TCLKPH, RCLKPH to 0 */
		MCHP_SPI_CC(ctrl) &= ~0x7;

		/* Set LSBF to 0 -> MSB first */
		MCHP_SPI_CR(ctrl) &= ~0x1;
	} else {
		/* soft reset */
		MCHP_SPI_CR(ctrl) |= (1u << 4);

		/* Clear enable bit in SPI_AR */
		MCHP_SPI_AR(ctrl) &= ~0x1;

		if (ctrl)
			MCHP_PCR_SLP_EN_DEV(MCHP_PCR_GPSPI1);
		else
			MCHP_PCR_SLP_EN_DEV(MCHP_PCR_GPSPI0);
	}

	return EC_SUCCESS;
}