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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
* Copyright (C) Cvitek Co., Ltd. 2019-2020. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <rtthread.h>
#include <string.h>
#include <stdio.h>
#include "mmio.h"
#include "dw_spi.h"
#include <rthw.h>
#ifdef SPI_DEBUG
#define SP_DEBUG_LOG(fmt, ...) \
do { \
rt_kprintf(fmt, ##__VA_ARGS__); \
} while(0)
#else
#define SP_DEBUG_LOG(fmt, ...)
#endif
/* Restart the controller, disable all interrupts, clean rx fifo */
void spi_hw_init(struct dw_spi *dws)
{
/*
* Try to detect the FIFO depth if not set by interface driver,
* the depth could be from 2 to 256 from HW spec
*/
if (!dws->fifo_len) {
uint32_t fifo;
for (fifo = 1; fifo < 256; fifo++) {
dw_writel(dws, CVI_DW_SPI_TXFTLR, fifo);
if (fifo != dw_readl(dws, CVI_DW_SPI_TXFTLR))
break;
}
dw_writel(dws, CVI_DW_SPI_TXFTLR, 0);
dws->fifo_len = (fifo == 1) ? 0 : fifo;
SP_DEBUG_LOG("Detected FIFO size: %u bytes\n", dws->fifo_len);
}
}
uint32_t min3(uint32_t a, uint32_t b, uint32_t c)
{
uint32_t tmp;
tmp = (a < b) ? a : b;
return (tmp < c) ? tmp : c;
}
static inline void cpu_relax(void)
{
//asm volatile("" ::: "memory");
}
static inline uint32_t tx_max(struct dw_spi *dws)
{
uint32_t tx_left, tx_room, rxtx_gap, temp;
cpu_relax();
tx_left = dws->tx_len;
tx_room = dws->fifo_len - dw_readl(dws, CVI_DW_SPI_TXFLR);
/*
* Another concern is about the tx/rx mismatch, we
* though to use (dws->fifo_len - rxflr - txflr) as
* one maximum value for tx, but it doesn't cover the
* data which is out of tx/rx fifo and inside the
* shift registers. So a control from sw point of
* view is taken.
*/
SP_DEBUG_LOG("tx left: %#x, tx room: %#x\n", tx_left, tx_room);
if (dws->rx != NULL && dws->tx != NULL) {
cpu_relax();
rxtx_gap = dws->fifo_len - (dws->rx_len - dws->tx_len);
temp = min3(tx_left, tx_room, (uint32_t)(rxtx_gap));
} else {
temp = tx_left < tx_room ? tx_left : tx_room;
}
SP_DEBUG_LOG("temp: %#x\n", temp);
return temp;
}
void dw_writer(struct dw_spi *dws)
{
uint32_t max;
uint16_t txw = 0;
max = tx_max(dws);
SP_DEBUG_LOG("max: %#x \n", max);
while (max--) {
if (dws->tx) {
if (dws->n_bytes == 1)
txw = *(uint8_t *)(dws->tx);
else
txw = *(uint16_t *)(dws->tx);
}
dw_writel(dws, CVI_DW_SPI_DR, txw);
dws->tx += dws->n_bytes;
--dws->tx_len;
}
}
static inline uint32_t rx_max(struct dw_spi *dws)
{
uint32_t temp;
uint32_t rx_left = dws->rx_len;
uint32_t data_in_fifo = dw_readl(dws, CVI_DW_SPI_RXFLR);
temp = (rx_left < data_in_fifo ? rx_left : data_in_fifo);
SP_DEBUG_LOG("data_in_fifo:%u temp: %u\n", data_in_fifo, temp);
return temp;
}
int dw_spi_check_status(struct dw_spi *dws, bool raw)
{
uint32_t irq_status;
int ret = 0;
if (raw)
irq_status = dw_readl(dws, CVI_DW_SPI_RISR);
else
irq_status = dw_readl(dws, CVI_DW_SPI_ISR);
if (irq_status & CVI_SPI_INT_RXOI) {
SP_DEBUG_LOG("RX FIFO overflow detected\n");
ret = -1;
}
if (irq_status & CVI_SPI_INT_RXUI) {
SP_DEBUG_LOG("RX FIFO underflow detected\n");
ret = -1;
}
if (irq_status & CVI_SPI_INT_TXOI) {
SP_DEBUG_LOG("TX FIFO overflow detected\n");
ret = -1;
}
if (ret)
spi_reset_chip(dws);
return ret;
}
void dw_reader(struct dw_spi *dws)
{
uint32_t max;
uint16_t rxw;
max = rx_max(dws);
SP_DEBUG_LOG("max: %#x \n", max);
while (max--) {
rxw = dw_readl(dws, CVI_DW_SPI_DR);
if (dws->rx) {
if (dws->n_bytes == 1)
*(uint8_t *)(dws->rx) = rxw;
else
*(uint16_t *)(dws->rx) = rxw;
dws->rx += dws->n_bytes;
}
--dws->rx_len;
}
}
int spi_delay_to_ns(struct spi_delay *_delay, struct dw_spi *dws)
{
uint32_t delay = _delay->value;
uint32_t unit = _delay->unit;
uint32_t hz;
if (!delay)
return 0;
switch (unit) {
case SPI_DELAY_UNIT_USECS:
delay *= 1000;
break;
case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
break;
case SPI_DELAY_UNIT_SCK:
/* clock cycles need to be obtained from spi_transfer */
if (!dws)
return -1;
/* if there is no effective speed know, then approximate
* by underestimating with half the requested hz
*/
hz = dws->speed_hz / 2;
if (!hz)
return -1;
delay *= DIV_ROUND_UP(1000000000, hz);
break;
default:
return -EINVAL;
}
return delay;
}
static void _spi_transfer_delay_ns(uint32_t ns)
{
if (!ns)
return;
if (ns <= 1000) {
rt_hw_us_delay(1);
} else {
uint32_t us = DIV_ROUND_UP(ns, 1000);
rt_hw_us_delay(us);
}
}
int spi_delay_exec(struct spi_delay *_delay, struct dw_spi *dws)
{
int delay;
if (!_delay)
return -1;
delay = spi_delay_to_ns(_delay, dws);
if (delay < 0)
return delay;
_spi_transfer_delay_ns(delay);
return 0;
}
int poll_transfer(struct dw_spi *dws)
{
struct spi_delay delay;
uint16_t nbits;
delay.unit = SPI_DELAY_UNIT_SCK;
nbits = dws->n_bytes * BITS_PER_BYTE;
int ret = 0;
do
{
dw_writer(dws);
cpu_relax();
delay.value = nbits * (dws->rx_len - dws->tx_len);
spi_delay_exec(&delay, dws);
dw_reader(dws);
cpu_relax();
ret = dw_spi_check_status(dws, true);
if (ret)
return ret;
} while (dws->rx_len && dws->tx_len);
return 0;
}
void set_tran_mode(struct dw_spi *dws)
{
uint32_t reg = dw_readl(dws, CVI_DW_SPI_CTRLR0);
uint8_t tmode;
if (dws->rx && dws->tx) {
tmode = CVI_SPI_TMOD_TR;
} else if (dws->rx) {
tmode = CVI_SPI_TMOD_RO;
} else {
tmode = CVI_SPI_TMOD_TO;
}
reg &= ~CVI_SPI_TMOD_MASK;
reg |= (tmode << CVI_SPI_TMOD_OFFSET);
dw_writel(dws, CVI_DW_SPI_CTRLR0, reg);
}
void dw_spi_set_controller_mode(struct dw_spi *dws, uint8_t enable_master)
{
/* do not support to switch controller mode, it is default master mode */
}
void dw_spi_set_cs(struct dw_spi *dws, bool enable, uint32_t index)
{
uint32_t reg = dw_readl(dws, CVI_DW_SPI_SER);
if (enable)
dw_writel(dws, CVI_DW_SPI_SER, reg | BIT(index));
else
dw_writel(dws, CVI_DW_SPI_SER, reg & ~BIT(index));
}
void dw_spi_set_polarity_and_phase(struct dw_spi *dws, uint8_t format)
{
uint32_t reg = dw_readl(dws, CVI_DW_SPI_CTRLR0);
reg &= ~(0x3 << 6);
switch (format) {
case SPI_FORMAT_CPOL0_CPHA0:
reg |= (SPI_MODE_0 << 6);
break;
case SPI_FORMAT_CPOL0_CPHA1:
reg |= (SPI_MODE_1 << 6);
break;
case SPI_FORMAT_CPOL1_CPHA0:
reg |= (SPI_MODE_2 << 6);
break;
case SPI_FORMAT_CPOL1_CPHA1:
reg |= (SPI_MODE_3 << 6);
break;
default:
reg = dw_readl(dws, CVI_DW_SPI_CTRLR0);
break;
}
SP_DEBUG_LOG("set phase and polarity: %#x\n", reg);
dw_writel(dws, CVI_DW_SPI_CTRLR0, reg);
}
uint32_t dw_spi_set_clock(struct dw_spi *dws, uint32_t clock_in, uint32_t clock_out)
{
uint16_t div;
div = (DIV_ROUND_UP(clock_in, clock_out) + 1) & 0xfffe;
dws->speed_hz = clock_in / div;
SP_DEBUG_LOG("clk div value is: %u, hz:%u\n", div, dws->speed_hz);
spi_set_clk(dws, div);
return dws->speed_hz;
}
int dw_spi_set_data_frame_len(struct dw_spi *dws, uint32_t size)
{
uint32_t temp = dw_readl(dws, CVI_DW_SPI_CTRLR0);
temp &= ~0xf;
if (size == 8) {
dws->n_bytes = 1;
} else if (size == 16) {
dws->n_bytes = 2;
} else {
SP_DEBUG_LOG("do not support %u bit data!\n", size);
return -1;
}
temp |= (size - 1);
dw_writel(dws, CVI_DW_SPI_CTRLR0, temp);
SP_DEBUG_LOG("set data frame len: %#x\n", temp);
return 0;
}
void dw_spi_show_regs(struct dw_spi *dws)
{
SP_DEBUG_LOG("CTRLR0: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_CTRLR0));
SP_DEBUG_LOG("CTRLR1: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_CTRLR1));
SP_DEBUG_LOG("SSIENR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_SSIENR));
SP_DEBUG_LOG("SER: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_SER));
SP_DEBUG_LOG("BAUDR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_BAUDR));
SP_DEBUG_LOG("TXFTLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_TXFTLR));
SP_DEBUG_LOG("RXFTLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_RXFTLR));
SP_DEBUG_LOG("TXFLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_TXFLR));
SP_DEBUG_LOG("RXFLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_RXFLR));
SP_DEBUG_LOG("SR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_SR));
SP_DEBUG_LOG("IMR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_IMR));
SP_DEBUG_LOG("ISR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_ISR));
SP_DEBUG_LOG("DMACR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_DMACR));
SP_DEBUG_LOG("DMATDLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_DMATDLR));
SP_DEBUG_LOG("DMARDLR: \t0x%08x\n", dw_readl(dws, CVI_DW_SPI_DMARDLR));
}
|