Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Platform.c
3 * @brief Implementation of Platform Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014(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 "util.h"
45 #include "adc_core.h"
46 #include "dac_core.h"
47 #include "parameters.h"
48 #include "platform.h"
49 #include "socal/socal.h"
50 #include "alt_spi.h"
51 #include "alt_address_space.h"
52 #include "alt_bridge_manager.h"
53
54 /* Altera Avalon SPI Registers Definition */
55
56 #define ALT_AVL_SPI_RXDATA_REG 0x0000
57 #define ALT_AVL_SPI_TXDATA_REG 0x0004
58 #define ALT_AVL_SPI_STATUS_REG 0x0008
59 #define ALT_AVL_SPI_CONTROL_REG 0x000C
60 #define ALT_AVL_SPI_CONTROL_SSO_MSK (1 << 10)
61 #define ALT_AVL_SPI_SLAVE_SEL_REG 0x0014
62 #define ALT_AVL_SPI_STATUS_TMT_MSK (1 << 5)
63 #define ALT_AVL_SPI_STATUS_TRDY_MSK (1 << 6)
64 #define ALT_AVL_SPI_STATUS_RRDY_MSK (1 << 7)
65
66 /* Altera Avalon GPIO Registers Definition */
67
68 #define ALT_AVL_PIO_DATA_REG 0x0000
69 #define ALT_AVL_PIO_DIRECTION_REG 0x0004
70
71 /***************************************************************************//**
72 * @brief usleep
73 *******************************************************************************/
74 static inline void usleep(unsigned long usleep)
75 {
76 unsigned long delay = 0;
77
78 for(delay = 0; delay < usleep * 10; delay++);
79 }
80
81 /***************************************************************************//**
82 * @brief altera_bridge_init
83 *******************************************************************************/
84 int32_t altera_bridge_init(void)
85 {
86 int32_t status = 0;
87
88 status = alt_bridge_init(ALT_BRIDGE_LWH2F, NULL, NULL);
89
90 if (status == 0)
91 {
92 status = alt_addr_space_remap(ALT_ADDR_SPACE_MPU_ZERO_AT_BOOTROM,
93 ALT_ADDR_SPACE_NONMPU_ZERO_AT_OCRAM,
94 ALT_ADDR_SPACE_H2F_ACCESSIBLE,
95 ALT_ADDR_SPACE_LWH2F_ACCESSIBLE);
96 }
97
98 return status;
99 }
100
101 /***************************************************************************//**
102 * @brief altera_bridge_uninit
103 *******************************************************************************/
104 int32_t altera_bridge_uninit(void)
105 {
106 int32_t status = 0;
107
108 status = alt_bridge_uninit(ALT_BRIDGE_LWH2F, NULL, NULL);
109
110 return status;
111 }
112
113 /***************************************************************************//**
114 * @brief alt_avl_spi_read
115 *******************************************************************************/
116 uint32_t alt_avl_spi_read(uint32_t reg_addr)
117 {
118 uint32_t reg_data;
119
120 reg_data = alt_read_word(SPI_BASEADDR + reg_addr);
121
122 return reg_data;
123 }
124
125 /***************************************************************************//**
126 * @brief alt_avl_spi_write
127 *******************************************************************************/
128 void alt_avl_spi_write(uint32_t reg_addr, uint32_t reg_data)
129 {
130 alt_write_word(SPI_BASEADDR + reg_addr, reg_data);
131 }
132
133 /***************************************************************************//**
134 * @brief spi_init
135 *******************************************************************************/
136 int32_t spi_init(uint32_t device_id,
137 uint8_t clk_pha,
138 uint8_t clk_pol)
139 {
140 return 0;
141 }
142
143 /***************************************************************************//**
144 * @brief spi_read
145 *******************************************************************************/
146 int32_t spi_read(uint8_t *data,
147 uint8_t bytes_number)
148 {
149 uint32_t cnt = 0;
150
151 /* Enable Slave Select mask. */
152 alt_avl_spi_write(ALT_AVL_SPI_SLAVE_SEL_REG, 1);
153 /* Set the SSO bit (force chip select). */
154 alt_avl_spi_write(ALT_AVL_SPI_CONTROL_REG, ALT_AVL_SPI_CONTROL_SSO_MSK);
155 /* Discard any stale data, in case previous communication was interrupted. */
156 alt_avl_spi_read(ALT_AVL_SPI_RXDATA_REG);
157
158 while(cnt < bytes_number)
159 {
160 /* Wait until txdata register is empty. */
161 while((alt_avl_spi_read(ALT_AVL_SPI_STATUS_REG) &
162 ALT_AVL_SPI_STATUS_TRDY_MSK) == 0);
163 /* Write data to txdata register. */
164 alt_avl_spi_write(ALT_AVL_SPI_TXDATA_REG, data[cnt]);
165 /* Wait until rxdata register is full. */
166 while ((alt_avl_spi_read(ALT_AVL_SPI_STATUS_REG)
167 & ALT_AVL_SPI_STATUS_RRDY_MSK) == 0);
168 /* Read data from rxdata register. */
169 data[cnt] = alt_avl_spi_read(ALT_AVL_SPI_RXDATA_REG);
170 cnt++;
171 }
172
173 /* Wait until the interface has finished transmitting. */
174 while((alt_avl_spi_read(ALT_AVL_SPI_STATUS_REG) &
175 ALT_AVL_SPI_STATUS_TMT_MSK) == 0);
176 /* Clear the SSO bit (release chip select). */
177 alt_avl_spi_write(ALT_AVL_SPI_CONTROL_REG, 0);
178
179 return 0;
180 }
181
182 /***************************************************************************//**
183 * @brief spi_write_then_read
184 *******************************************************************************/
185 int spi_write_then_read(struct spi_device *spi,
186 const unsigned char *txbuf, unsigned n_tx,
187 unsigned char *rxbuf, unsigned n_rx)
188 {
189 uint8_t buffer[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00};
192 uint8_t byte;
193
194 for(byte = 0; byte < n_tx; byte++)
195 {
196 buffer[byte] = (unsigned char)txbuf[byte];
197 }
198 spi_read(buffer, n_tx + n_rx);
199 for(byte = n_tx; byte < n_tx + n_rx; byte++)
200 {
201 rxbuf[byte - n_tx] = buffer[byte];
202 }
203
204 return SUCCESS;
205 }
206
207 /***************************************************************************//**
208 * @brief alt_avl_gpio_read
209 *******************************************************************************/
210 uint32_t alt_avl_gpio_read(uint32_t reg_addr)
211 {
212 uint32_t reg_data;
213
214 reg_data = alt_read_word(GPIO_BASEADDR + reg_addr);
215
216 return reg_data;
217 }
218
219 /***************************************************************************//**
220 * @brief alt_avl_gpio_write
221 *******************************************************************************/
222 void alt_avl_gpio_write(uint32_t reg_addr, uint32_t reg_data)
223 {
224 alt_write_word(GPIO_BASEADDR + reg_addr, reg_data);
225 }
226
227 /***************************************************************************//**
228 * @brief gpio_init
229 *******************************************************************************/
230 void gpio_init(uint32_t device_id)
231 {
232
233 }
234
235 /***************************************************************************//**
236 * @brief gpio_direction
237 *******************************************************************************/
238 void gpio_direction(uint8_t pin, uint8_t direction)
239 {
240 uint32_t reg_val;
241
242 reg_val = alt_avl_gpio_read(ALT_AVL_PIO_DIRECTION_REG);
243 if (direction)
244 reg_val |= (1 << pin);
245 else
246 reg_val &= ~(1 << pin);
247 alt_avl_gpio_write(ALT_AVL_PIO_DIRECTION_REG, reg_val);
248 }
249
250 /***************************************************************************//**
251 * @brief gpio_is_valid
252 *******************************************************************************/
253 bool gpio_is_valid(int number)
254 {
255 if(number >= 0)
256 return 1;
257 else
258 return 0;
259 }
260
261 /***************************************************************************//**
262 * @brief gpio_data
263 *******************************************************************************/
264 void gpio_data(uint8_t pin, uint8_t data)
265 {
266
267 }
268
269 /***************************************************************************//**
270 * @brief gpio_set_value
271 *******************************************************************************/
272 void gpio_set_value(unsigned gpio, int value)
273 {
274 uint32_t reg_val;
275
276 reg_val = alt_avl_gpio_read(ALT_AVL_PIO_DATA_REG);
277 if (value)
278 reg_val |= (1 << gpio);
279 else
280 reg_val &= ~(1 << gpio);
281 alt_avl_gpio_write(ALT_AVL_PIO_DATA_REG, reg_val);
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();
316 dac_init(phy, DATA_SEL_DDS);
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(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(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 }