Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad7616-sdz/spi_engine.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file spi_engine.c
3 * @brief Implementation of SPI Engine Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2016(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include <stdbool.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <sleep.h>
48 #include <xil_io.h>
49 #include <xscugic.h>
50 #include <xparameters.h>
51 #include "spi_engine.h"
52
53 /******************************************************************************/
54 /********************** Macros and Constants Definitions **********************/
55 /******************************************************************************/
56 #define SPI_ENGINE_INT_ID 88
57
58 /******************************************************************************/
59 /************************ Variables Definitions *******************************/
60 /******************************************************************************/
61 spi_engine spi;
62 XScuGic_Config *gic_config;
63 XScuGic gic;
64
65 /***************************************************************************//**
66 * @brief spi_engine_read
67 *******************************************************************************/
68 int32_t spi_engine_read(uint32_t reg_addr,
69 uint32_t *reg_data)
70 {
71 *reg_data = Xil_In32((spi.baseaddr + reg_addr));
72
73 return 0;
74 }
75
76 /***************************************************************************//**
77 * @brief spi_engine_write
78 *******************************************************************************/
79 int32_t spi_engine_write(uint32_t reg_addr,
80 uint32_t reg_data)
81 {
82 Xil_Out32((spi.baseaddr + reg_addr), reg_data);
83
84 return 0;
85 }
86
87 /***************************************************************************//**
88 * @brief spi_engine_program_add_cmd
89 *******************************************************************************/
90 void spi_engine_program_add_cmd(spi_engine_program *p,
91 uint8_t dry,
92 uint16_t cmd)
93 {
94 if (!dry)
95 p->instructions[p->length] = cmd;
96 p->length++;
97 }
98
99 /***************************************************************************//**
100 * @brief spi_engine_compile_message
101 *******************************************************************************/
102 int32_t spi_engine_compile_message(spi_message *msg,
103 uint8_t dry,
104 spi_engine_program *p)
105 {
106 uint32_t clk_div = 0;
107
108 spi_engine_program_add_cmd(p, dry,
109 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CONFIG, msg->spi_config));
110
111 clk_div = (spi.spi_ref_clk / msg->spi_clk) - 1;
112 spi_engine_program_add_cmd(p, dry,
113 SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CLK_DIV, clk_div));
114
115 if (msg->cs_change) {
116 spi_engine_program_add_cmd(p, dry,
117 SPI_ENGINE_CMD_ASSERT(1, (0xff ^ BIT(msg->chip_select))));
118 }
119
120 spi_engine_program_add_cmd(p, dry,
121 SPI_ENGINE_CMD_TRANSFER(msg->tx_length ? 1 : 0,
122 msg->rx_length ? 1 : 0,
123 max(msg->tx_length, msg->rx_length) - 1));
124
125 if (msg->cs_change) {
126 spi_engine_program_add_cmd(p, dry,
127 SPI_ENGINE_CMD_ASSERT(1, 0xff));
128 }
129
130 return 0;
131 }
132
133 /***************************************************************************//**
134 * @brief spi_engine_write_cmd_fifo
135 *******************************************************************************/
136 int32_t spi_engine_write_cmd_fifo(void)
137 {
138 uint32_t n, m, i;
139 const uint16_t *buf;
140
141 spi_engine_read(SPI_ENGINE_REG_CMD_FIFO_ROOM, &n);
142
143 while (n && spi.cmd_length) {
144 m = min(n, spi.cmd_length);
145 buf = spi.cmd_buf;
146 for (i = 0; i < m; i++)
147 spi_engine_write(SPI_ENGINE_REG_CMD_FIFO, buf[i]);
148 spi.cmd_length -= m;
149 n -= m;
150 }
151
152 return spi.cmd_length != 0;
153 }
154
155 /***************************************************************************//**
156 * @brief spi_engine_write_tx_fifo
157 *******************************************************************************/
158 int32_t spi_engine_write_tx_fifo(void)
159 {
160 uint32_t n, m, i;
161 const uint8_t *buf;
162
163 spi_engine_read(SPI_ENGINE_REG_SDO_FIFO_ROOM, &n);
164
165 while (n && spi.msg->tx_length) {
166 m = min(n, spi.msg->tx_length);
167 buf = spi.msg->tx_buf;
168 for (i = 0; i < m; i++)
169 spi_engine_write(SPI_ENGINE_REG_SDO_DATA_FIFO, buf[i]);
170 spi.msg->tx_length -= m;
171 n -= m;
172 }
173
174 return spi.msg->tx_length != 0;
175 }
176
177 /***************************************************************************//**
178 * @brief spi_engine_write_tx_fifo
179 *******************************************************************************/
180 int32_t spi_engine_read_rx_fifo()
181 {
182 uint32_t n, m, i;
183 uint32_t data;
184 uint8_t *buf;
185
186 spi_engine_read(SPI_ENGINE_REG_SDI_FIFO_LEVEL, &n);
187
188 while (n && spi.msg->rx_length) {
189 m = min(n, spi.msg->rx_length);
190 buf = spi.msg->rx_buf;
191 for (i = 0; i < m; i++) {
192 spi_engine_read(SPI_ENGINE_REG_SDI_DATA_FIFO, &data);
193 buf[i] = (uint8_t) data;
194 }
195 spi.msg->rx_buf += m;
196 spi.msg->rx_length -= m;
197 n -= m;
198 }
199
200 return spi.msg->rx_length != 0;
201 }
202
203 /***************************************************************************//**
204 * @brief spi_engine_transfer_one_message
205 *******************************************************************************/
206 int32_t spi_engine_transfer_one_message(spi_message *msg)
207 {
208 spi_engine_program p_dry, *p;
209 size_t size;
210
211 p_dry.length = 0;
212 spi_engine_compile_message(msg, true, &p_dry);
213
214 size = sizeof(*p->instructions) * (p_dry.length + 1);
215 p = malloc(sizeof(*p) + size);
216 if (!p)
217 return -1;
218 p->length = 0;
219 spi_engine_compile_message(msg, false, p);
220
221 spi.sync_id = (spi.sync_id + 1) & 0xff;
222 spi_engine_program_add_cmd(p, false,
223 SPI_ENGINE_CMD_SYNC(spi.sync_id));
224
225 spi.msg = msg;
226 spi.p = p;
227
228 spi.cmd_buf = p->instructions;
229 spi.cmd_length = p->length;
230 if (spi_engine_write_cmd_fifo())
231 spi.int_enable |= SPI_ENGINE_INT_CMD_ALMOST_EMPTY;
232
233 if (spi_engine_write_tx_fifo())
234 spi.int_enable |= SPI_ENGINE_INT_SDO_ALMOST_EMPTY;
235
236 if (spi.msg->rx_length != 0)
237 spi.int_enable |= SPI_ENGINE_INT_SDI_ALMOST_FULL;
238
239 spi.int_enable |= SPI_ENGINE_INT_SYNC;
240
241 spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, spi.int_enable);
242
243 return 0;
244 }
245
246 /***************************************************************************//**
247 * @brief spi_engine_isr
248 *******************************************************************************/
249 void spi_engine_isr(void *instance)
250 {
251 uint32_t pending;
252 uint32_t disable_int = 0;
253
254 spi_engine_read(SPI_ENGINE_REG_INT_PENDING, &pending);
255
256 if (pending & SPI_ENGINE_INT_SYNC) {
257 spi_engine_write(SPI_ENGINE_REG_INT_PENDING, SPI_ENGINE_INT_SYNC);
258 spi_engine_read(SPI_ENGINE_REG_SYNC_ID, &spi.completed_id);
259 }
260
261 if (pending & SPI_ENGINE_INT_CMD_ALMOST_EMPTY) {
262 if (!spi_engine_write_cmd_fifo())
263 disable_int |= SPI_ENGINE_INT_CMD_ALMOST_EMPTY;
264 }
265
266 if (pending & SPI_ENGINE_INT_SDO_ALMOST_EMPTY) {
267 if (!spi_engine_write_tx_fifo())
268 disable_int |= SPI_ENGINE_INT_SDO_ALMOST_EMPTY;
269 }
270
271 if (pending & (SPI_ENGINE_INT_SDI_ALMOST_FULL | SPI_ENGINE_INT_SYNC)) {
272 if (!spi_engine_read_rx_fifo())
273 disable_int |= SPI_ENGINE_INT_SDI_ALMOST_FULL;
274 }
275
276 if (pending & SPI_ENGINE_INT_SYNC) {
277 disable_int |= SPI_ENGINE_INT_SYNC;
278 }
279
280 if (disable_int) {
281 spi.int_enable &= ~disable_int;
282 spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, spi.int_enable);
283 }
284
285 }
286
287 /***************************************************************************//**
288 * @brief spi_engine_offload_enable
289 *******************************************************************************/
290 void spi_engine_offload_enable(uint8_t enable)
291 {
292 uint32_t reg;
293
294 spi_engine_read(SPI_ENGINE_REG_OFFLOAD_CTRL(0), &reg);
295 if (enable)
296 reg |= SPI_ENGINE_OFFLOAD_CTRL_ENABLE;
297 else
298 reg &= ~SPI_ENGINE_OFFLOAD_CTRL_ENABLE;
299 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CTRL(0), reg);
300 }
301
302 /***************************************************************************//**
303 * @brief spi_engine_offload_load_msg
304 *******************************************************************************/
305 int32_t spi_engine_offload_load_msg(spi_message *msg)
306 {
307 spi_engine_program p_dry, *p;
308 size_t size;
309 uint32_t i;
310
311 p_dry.length = 0;
312 spi_engine_compile_message(msg, true, &p_dry);
313
314 size = sizeof(*p->instructions) * (p_dry.length + 2);
315 p = malloc(sizeof(*p) + size);
316 if (!p)
317 return -1;
318 p->length = 0;
319 spi_engine_compile_message(msg, false, p);
320 spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SLEEP(10));
321 spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC(0));
322
323 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_RESET(0), 1);
324 for (i = 0; i < msg->tx_length; i++)
325 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_SDO_MEM(0), msg->tx_buf[i]);
326
327 for (i = 0; i < p->length; i++)
328 spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), p->instructions[i]);
329
330 return 0;
331 }
332
333 /***************************************************************************//**
334 * @brief spi_engine_setup
335 *******************************************************************************/
336 int32_t spi_engine_setup(uint32_t baseaddr)
337 {
338 uint32_t version;
339 int32_t status;
340
341 spi.baseaddr = baseaddr;
342 spi.spi_ref_clk = 100000000;
343 spi.sync_id = 0;
344 spi.int_enable = 0;
345
346 spi_engine_read(SPI_ENGINE_REG_VERSION, &version);
347 if (SPI_ENGINE_VERSION_MAJOR(version) != 1) {
348 printf("Unsupported peripheral version %u.%u.%c\n",
349 (unsigned int)SPI_ENGINE_VERSION_MAJOR(version),
350 (unsigned int)SPI_ENGINE_VERSION_MINOR(version),
351 (int)SPI_ENGINE_VERSION_PATCH(version));
352 return -1;
353 }
354
355 spi_engine_write(SPI_ENGINE_REG_RESET, 0x01);
356 usleep(100000);
357 spi_engine_write(SPI_ENGINE_REG_RESET, 0x00);
358 spi_engine_write(SPI_ENGINE_REG_INT_PENDING, 0xff);
359 spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, 0x00);
360
361 gic_config = XScuGic_LookupConfig(XPAR_PS7_SCUGIC_0_DEVICE_ID);
362 if(gic_config == NULL)
363 printf("Error\n");
364
365 status = XScuGic_CfgInitialize(&gic, gic_config,
366 gic_config->CpuBaseAddress);
367 if(status)
368 printf("Error\n");
369
370 XScuGic_SetPriorityTriggerType(&gic, SPI_ENGINE_INT_ID, 0x0, 0x3);
371 status = XScuGic_Connect(&gic, SPI_ENGINE_INT_ID,
372 (Xil_ExceptionHandler)spi_engine_isr, NULL);
373 if(status)
374 printf("Error\n");
375
376 XScuGic_Enable(&gic, SPI_ENGINE_INT_ID);
377
378 Xil_ExceptionInit();
379
380 Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
381 (Xil_ExceptionHandler)XScuGic_InterruptHandler, (void *)&gic);
382 Xil_ExceptionEnable();
383
384 printf("SPI Engine version %u.%u.%c successfully initialized\n",
385 (unsigned int)SPI_ENGINE_VERSION_MAJOR(version),
386 (unsigned int)SPI_ENGINE_VERSION_MINOR(version),
387 (int)SPI_ENGINE_VERSION_PATCH(version));
388
389 return 0;
390 }
391
392 /***************************************************************************//**
393 * @brief spi_write_and_read
394 *******************************************************************************/
395 int32_t spi_engine_write_and_read(uint8_t ss,
396 uint8_t *data,
397 uint8_t bytes_number)
398 {
399 uint32_t expected_id = 0;
400 uint32_t i;
401 spi_message message;
402 uint8_t tx[8];
403 uint8_t rx[8];
404
405 for (i = 0; i < bytes_number; i++)
406 tx[i] = data[i];
407
408 message.chip_select = ss;
409 message.cs_change = true;
410 message.spi_clk = 1000000;
411 message.spi_config = SPI_ENGINE_CONFIG_CPOL;
412 message.tx_buf = tx;
413 message.rx_buf = rx;
414
415 expected_id = spi.sync_id + 1;
416 spi.completed_id = 0xFFFFFFFF;
417
418 message.tx_length = bytes_number;
419 message.rx_length = bytes_number;
420 message.rx_buf = rx;
421 spi_engine_transfer_one_message(&message);
422
423 while (spi.completed_id != expected_id);
424
425 for (i = 0; i < bytes_number; i++)
426 data[i] = rx[i];
427
428 return 0;
429 }