Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad9361/sw/platform_xilinx/platform.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Platform.c
3 * @brief Implementation of Platform Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(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 <stdint.h>
44 #include <xparameters.h>
45 #ifdef _XPARAMETERS_PS_H_
46 #include <xgpiops.h>
47 #include <xspips.h>
48 #else
49 #include <xgpio.h>
50 #include <xgpio_l.h>
51 #include <xspi.h>
52 #endif
53 #include "util.h"
54 #include "adc_core.h"
55 #include "dac_core.h"
56 #include "platform.h"
57 #ifdef _XPARAMETERS_PS_H_
58 #include <sleep.h>
59 #else
60 static inline void usleep(unsigned long usleep)
61 {
62 unsigned long delay = 0;
63
64 for(delay = 0; delay < usleep * 10; delay++);
65 }
66 #endif
67
68 /******************************************************************************/
69 /************************ Variables Definitions *******************************/
70 /******************************************************************************/
71 #ifdef _XPARAMETERS_PS_H_
72 XSpiPs_Config *spi_config;
73 XSpiPs spi_instance;
74 XGpioPs_Config *gpio_config;
75 XGpioPs gpio_instance;
76 #else
77 XSpi_Config *spi_config;
78 XSpi spi_instance;
79 XGpio_Config *gpio_config;
80 #endif
81
82 /***************************************************************************//**
83 * @brief spi_init
84 *******************************************************************************/
85 int32_t spi_init(uint32_t device_id,
86 uint8_t clk_pha,
87 uint8_t clk_pol)
88 {
89
90 uint32_t base_addr = 0;
91 uint32_t spi_options = 0;
92 #ifdef _XPARAMETERS_PS_H_
93
94 spi_config = XSpiPs_LookupConfig(device_id);
95 base_addr = spi_config->BaseAddress;
96
97 XSpiPs_CfgInitialize(&spi_instance, spi_config, base_addr);
98
99 spi_options = XSPIPS_MASTER_OPTION |
100 (clk_pol ? XSPIPS_CLK_ACTIVE_LOW_OPTION : 0) |
101 (clk_pha ? XSPIPS_CLK_PHASE_1_OPTION : 0) |
102 XSPIPS_FORCE_SSELECT_OPTION;
103
104 XSpiPs_SetOptions(&spi_instance, spi_options);
105
106 XSpiPs_SetClkPrescaler(&spi_instance, XSPIPS_CLK_PRESCALE_256);
107 #else
108 XSpi_Initialize(&spi_instance, device_id);
109 XSpi_Stop(&spi_instance);
110 spi_config = XSpi_LookupConfig(device_id);
111 base_addr = spi_config->BaseAddress;
112 XSpi_CfgInitialize(&spi_instance, spi_config, base_addr);
113 spi_options = XSP_MASTER_OPTION |
114 XSP_CLK_PHASE_1_OPTION |
115 XSP_MANUAL_SSELECT_OPTION;
116 XSpi_SetOptions(&spi_instance, spi_options);
117 XSpi_Start(&spi_instance);
118 XSpi_IntrGlobalDisable(&spi_instance);
119 XSpi_SetSlaveSelect(&spi_instance, 1);
120 #endif
121 return SUCCESS;
122 }
123
124 /***************************************************************************//**
125 * @brief spi_read
126 *******************************************************************************/
127 int32_t spi_read(struct spi_device *spi,
128 uint8_t *data,
129 uint8_t bytes_number)
130 {
131 #ifdef _XPARAMETERS_PS_H_
132 XSpiPs_SetSlaveSelect(&spi_instance, (spi->id_no == 0 ? 0 : 1));
133
134 XSpiPs_PolledTransfer(&spi_instance, data, data, bytes_number);
135 #else
136 uint32_t cnt = 0;
137 #ifdef XPAR_AXI_SPI_0_DEVICE_ID
138 uint8_t send_buffer[20];
139
140 for(cnt = 0; cnt < bytes_number; cnt++)
141 {
142 send_buffer[cnt] = data[cnt];
143 }
144
145 XSpi_Transfer(&spi_instance, send_buffer, data, bytes_number);
146 #else
147 Xil_Out32((spi_instance.BaseAddr + 0x60), 0x1e6);
148 Xil_Out32((spi_instance.BaseAddr + 0x70), 0x000);
149 while(cnt < bytes_number)
150 {
151 Xil_Out32((spi_instance.BaseAddr + 0x68), data[cnt]);
152 Xil_Out32((spi_instance.BaseAddr + 0x60), 0x096);
153 do {usleep(100);}
154 while ((Xil_In32((spi_instance.BaseAddr + 0x64)) & 0x4) == 0x0);
155 Xil_Out32((spi_instance.BaseAddr + 0x60), 0x186);
156 data[cnt] = Xil_In32(spi_instance.BaseAddr + 0x6c);
157 cnt++;
158 }
159 Xil_Out32((spi_instance.BaseAddr + 0x70), 0x001);
160 Xil_Out32((spi_instance.BaseAddr + 0x60), 0x180);
161 #endif
162 #endif
163 return SUCCESS;
164 }
165
166 /***************************************************************************//**
167 * @brief spi_write_then_read
168 *******************************************************************************/
169 int spi_write_then_read(struct spi_device *spi,
170 const unsigned char *txbuf, unsigned n_tx,
171 unsigned char *rxbuf, unsigned n_rx)
172 {
173 uint8_t buffer[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175 0x00, 0x00, 0x00, 0x00};
176 uint8_t byte;
177
178 for(byte = 0; byte < n_tx; byte++)
179 {
180 buffer[byte] = (unsigned char)txbuf[byte];
181 }
182 spi_read(spi, buffer, n_tx + n_rx);
183 for(byte = n_tx; byte < n_tx + n_rx; byte++)
184 {
185 rxbuf[byte - n_tx] = buffer[byte];
186 }
187
188 return SUCCESS;
189 }
190
191 /***************************************************************************//**
192 * @brief gpio_init
193 *******************************************************************************/
194 void gpio_init(uint32_t device_id)
195 {
196 #ifdef _XPARAMETERS_PS_H_
197 gpio_config = XGpioPs_LookupConfig(device_id);
198 XGpioPs_CfgInitialize(&gpio_instance, gpio_config, gpio_config->BaseAddr);
199 #else
200 gpio_config = XGpio_LookupConfig(device_id);
201 #endif
202 }
203
204 /***************************************************************************//**
205 * @brief gpio_direction
206 *******************************************************************************/
207 void gpio_direction(uint8_t pin, uint8_t direction)
208 {
209 #ifdef _XPARAMETERS_PS_H_
210 XGpioPs_SetDirectionPin(&gpio_instance, pin, direction);
211 XGpioPs_SetOutputEnablePin(&gpio_instance, pin, 1);
212 #else
213 uint32_t config = 0;
214 uint32_t tri_reg_addr;
215
216 if (pin >= 32) {
217 tri_reg_addr = XGPIO_TRI2_OFFSET;
218 pin -= 32;
219 } else
220 tri_reg_addr = XGPIO_TRI_OFFSET;
221
222 config = Xil_In32((gpio_config->BaseAddress + tri_reg_addr));
223 if(direction)
224 {
225 config &= ~(1 << pin);
226 }
227 else
228 {
229 config |= (1 << pin);
230 }
231 Xil_Out32((gpio_config->BaseAddress + tri_reg_addr), config);
232 #endif
233 }
234
235 /***************************************************************************//**
236 * @brief gpio_is_valid
237 *******************************************************************************/
238 bool gpio_is_valid(int number)
239 {
240 if(number >= 0)
241 return 1;
242 else
243 return 0;
244 }
245
246 /***************************************************************************//**
247 * @brief gpio_data
248 *******************************************************************************/
249 void gpio_data(uint8_t pin, uint8_t data)
250 {
251 #ifdef _XPARAMETERS_PS_H_
252 XGpioPs_WritePin(&gpio_instance, pin, data);
253 #else
254 uint32_t config = 0;
255 uint32_t data_reg_addr;
256
257 if (pin >= 32) {
258 data_reg_addr = XGPIO_DATA2_OFFSET;
259 pin -= 32;
260 } else
261 data_reg_addr = XGPIO_DATA_OFFSET;
262
263 config = Xil_In32((gpio_config->BaseAddress + data_reg_addr));
264 if(data)
265 {
266 config |= (1 << pin);
267 }
268 else
269 {
270 config &= ~(1 << pin);
271 }
272 Xil_Out32((gpio_config->BaseAddress + data_reg_addr), config);
273 #endif
274 }
275
276 /***************************************************************************//**
277 * @brief gpio_set_value
278 *******************************************************************************/
279 void gpio_set_value(unsigned gpio, int value)
280 {
281 gpio_data(gpio, value);
282 }
283
284 /***************************************************************************//**
285 * @brief udelay
286 *******************************************************************************/
287 void udelay(unsigned long usecs)
288 {
289 usleep(usecs);
290 }
291
292 /***************************************************************************//**
293 * @brief mdelay
294 *******************************************************************************/
295 void mdelay(unsigned long msecs)
296 {
297 usleep(msecs * 1000);
298 }
299
300 /***************************************************************************//**
301 * @brief msleep_interruptible
302 *******************************************************************************/
303 unsigned long msleep_interruptible(unsigned int msecs)
304 {
305 mdelay(msecs);
306
307 return 0;
308 }
309
310 /***************************************************************************//**
311 * @brief axiadc_init
312 *******************************************************************************/
313 void axiadc_init(struct ad9361_rf_phy *phy)
314 {
315 adc_init(phy);
316 dac_init(phy, DATA_SEL_DDS, 0);
317 }
318
319 /***************************************************************************//**
320 * @brief axiadc_post_setup
321 *******************************************************************************/
322 int axiadc_post_setup(struct ad9361_rf_phy *phy)
323 {
324 return ad9361_post_setup(phy);
325 }
326
327 /***************************************************************************//**
328 * @brief axiadc_read
329 *******************************************************************************/
330 unsigned int axiadc_read(struct axiadc_state *st, unsigned long reg)
331 {
332 uint32_t val;
333
334 adc_read(st->phy, reg, &val);
335
336 return val;
337 }
338
339 /***************************************************************************//**
340 * @brief axiadc_write
341 *******************************************************************************/
342 void axiadc_write(struct axiadc_state *st, unsigned reg, unsigned val)
343 {
344 adc_write(st->phy, reg, val);
345 }
346
347 /***************************************************************************//**
348 * @brief axiadc_set_pnsel
349 *******************************************************************************/
350 int axiadc_set_pnsel(struct axiadc_state *st, int channel, enum adc_pn_sel sel)
351 {
352 unsigned reg;
353
354 uint32_t version = axiadc_read(st, 0x4000);
355
356 if (PCORE_VERSION_MAJOR(version) > 7) {
357 reg = axiadc_read(st, ADI_REG_CHAN_CNTRL_3(channel));
358 reg &= ~ADI_ADC_PN_SEL(~0);
359 reg |= ADI_ADC_PN_SEL(sel);
360 axiadc_write(st, ADI_REG_CHAN_CNTRL_3(channel), reg);
361 } else {
362 reg = axiadc_read(st, ADI_REG_CHAN_CNTRL(channel));
363
364 if (sel == ADC_PN_CUSTOM) {
365 reg |= ADI_PN_SEL;
366 } else if (sel == ADC_PN9) {
367 reg &= ~ADI_PN23_TYPE;
368 reg &= ~ADI_PN_SEL;
369 } else {
370 reg |= ADI_PN23_TYPE;
371 reg &= ~ADI_PN_SEL;
372 }
373
374 axiadc_write(st, ADI_REG_CHAN_CNTRL(channel), reg);
375 }
376
377 return 0;
378 }
379
380 /***************************************************************************//**
381 * @brief axiadc_idelay_set
382 *******************************************************************************/
383 void axiadc_idelay_set(struct axiadc_state *st,
384 unsigned lane, unsigned val)
385 {
386 if (PCORE_VERSION_MAJOR(st->pcore_version) > 8) {
387 axiadc_write(st, ADI_REG_DELAY(lane), val);
388 } else {
389 axiadc_write(st, ADI_REG_DELAY_CNTRL, 0);
390 axiadc_write(st, ADI_REG_DELAY_CNTRL,
391 ADI_DELAY_ADDRESS(lane)
392 | ADI_DELAY_WDATA(val)
393 | ADI_DELAY_SEL);
394 }
395 }