Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ad9144/ad9144.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9144.c
3 * @brief Implementation of AD9144 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014-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 "ad9144.h"
44
45 /***************************************************************************//**
46 * @brief ad9144_spi_read
47 *******************************************************************************/
48 int32_t ad9144_spi_read(spi_device *dev,
49 uint16_t reg_addr,
50 uint8_t *reg_data)
51 {
52 uint8_t buf[3];
53 int32_t ret;
54
55 buf[0] = 0x80 | (reg_addr >> 8);
56 buf[1] = reg_addr & 0xFF;
57 buf[2] = 0x00;
58
59 ret = ad_spi_xfer(dev, buf, 3);
60 *reg_data = buf[2];
61
62 return ret;
63 }
64
65 /***************************************************************************//**
66 * @brief ad9144_spi_write
67 *******************************************************************************/
68 int32_t ad9144_spi_write(spi_device *dev,
69 uint16_t reg_addr,
70 uint8_t reg_data)
71 {
72 uint8_t buf[3];
73 int32_t ret;
74
75 buf[0] = reg_addr >> 8;
76 buf[1] = reg_addr & 0xFF;
77 buf[2] = reg_data;
78
79 ret = ad_spi_xfer(dev, buf, 3);
80
81 return ret;
82 }
83
84 /***************************************************************************//**
85 * @brief ad9144_setup
86 *******************************************************************************/
87 int32_t ad9144_setup(spi_device *dev,
88 ad9144_init_param init_param)
89 {
90 uint8_t chip_id;
91 uint8_t scratchpad;
92 uint8_t pll_stat;
93 uint8_t cal_stat;
94 int32_t ret;
95
96 ad9144_spi_read(dev, REG_SPI_PRODIDL, &chip_id);
97 if(chip_id != AD9144_CHIP_ID)
98 {
99 ad_printf("%s : Invalid CHIP ID (0x%x).\n", __func__, chip_id);
100 return -1;
101 }
102
103 ad9144_spi_write(dev, REG_SPI_SCRATCHPAD, 0xAD);
104 ad9144_spi_read(dev, REG_SPI_SCRATCHPAD, &scratchpad);
105 if(scratchpad != 0xAD)
106 {
107 ad_printf("%s : scratchpad read-write failed (0x%x)!\n", __func__, scratchpad);
108 return -1;
109 }
110
111 // power-up and dac initialization
112
113 ad9144_spi_write(dev, REG_SPI_INTFCONFA, SOFTRESET_M | SOFTRESET); // reset
114 ad9144_spi_write(dev, REG_SPI_INTFCONFA, 0x00); // reset
115 mdelay(1);
116
117 ad9144_spi_write(dev, 0x011, 0x00); // dacs - power up everything
118 ad9144_spi_write(dev, 0x080, 0x00); // clocks - power up everything
119 ad9144_spi_write(dev, 0x081, 0x00); // sysref - power up/falling edge
120
121 // required device configurations
122
123 ad9144_spi_write(dev, 0x12d, 0x8b); // data-path
124 ad9144_spi_write(dev, 0x146, 0x01); // data-path
125 ad9144_spi_write(dev, 0x2a4, 0xff); // clock
126 ad9144_spi_write(dev, 0x1c4, 0x73); // dac-pll
127 ad9144_spi_write(dev, 0x291, 0x49); // serde-pll
128 ad9144_spi_write(dev, 0x29c, 0x24); // serde-pll
129 ad9144_spi_write(dev, 0x29f, 0x73); // serde-pll
130 ad9144_spi_write(dev, 0x232, 0xff); // jesd
131 ad9144_spi_write(dev, 0x333, 0x01); // jesd
132
133 // digital data path
134
135 ad9144_spi_write(dev, 0x112, 0x00); // interpolation (bypass)
136 ad9144_spi_write(dev, 0x110, 0x00); // 2's complement
137
138 // transport layer
139
140 ad9144_spi_write(dev, 0x200, 0x00); // phy - power up
141 ad9144_spi_write(dev, 0x201, 0x00); // phy - power up
142 ad9144_spi_write(dev, 0x300, 0x01); // single link - link 0
143 ad9144_spi_write(dev, 0x450, 0x00); // device id (0x400)
144 ad9144_spi_write(dev, 0x451, 0x00); // bank id (0x401)
145 ad9144_spi_write(dev, 0x452, 0x04); // lane-id (0x402)
146 ad9144_spi_write(dev, 0x453, 0x83); // descrambling, 4 lanes
147 ad9144_spi_write(dev, 0x454, 0x00); // octects per frame per lane (1)
148 ad9144_spi_write(dev, 0x455, 0x1f); // mult-frame - framecount (32)
149 ad9144_spi_write(dev, 0x456, 0x01); // no-of-converters (2)
150 ad9144_spi_write(dev, 0x457, 0x0f); // no CS bits, 16bit dac
151 ad9144_spi_write(dev, 0x458, 0x2f); // subclass 1, 16bits per sample
152 ad9144_spi_write(dev, 0x459, 0x20); // jesd204b, 1 samples per converter per device
153 ad9144_spi_write(dev, 0x45a, 0x80); // HD mode, no CS bits
154 ad9144_spi_write(dev, 0x45d, 0x49); // check-sum of 0x450 to 0x45c
155 ad9144_spi_write(dev, 0x46c, 0x0f); // enable deskew for all lanes
156 ad9144_spi_write(dev, 0x476, 0x01); // frame - bytecount (1)
157 ad9144_spi_write(dev, 0x47d, 0x0f); // enable all lanes
158
159 // physical layer
160
161 ad9144_spi_write(dev, 0x2aa, 0xb7); // jesd termination
162 ad9144_spi_write(dev, 0x2ab, 0x87); // jesd termination
163 ad9144_spi_write(dev, 0x2b1, 0xb7); // jesd termination
164 ad9144_spi_write(dev, 0x2b2, 0x87); // jesd termination
165 ad9144_spi_write(dev, 0x2a7, 0x01); // input termination calibration
166 ad9144_spi_write(dev, 0x2ae, 0x01); // input termination calibration
167 ad9144_spi_write(dev, 0x314, 0x01); // pclk == qbd master clock
168 if (init_param.lane_rate_kbps < 2880000)
169 ad9144_spi_write(dev, 0x230, 0x2A); // CDR_OVERSAMP
170 else
171 if (init_param.lane_rate_kbps > 5520000)
172 ad9144_spi_write(dev, 0x230, 0x28); // ENHALFRATE
173 else
174 ad9144_spi_write(dev, 0x230, 0x08);
175 ad9144_spi_write(dev, 0x206, 0x00); // cdr reset
176 ad9144_spi_write(dev, 0x206, 0x01); // cdr reset
177 if (init_param.lane_rate_kbps < 2880000)
178 ad9144_spi_write(dev, 0x289, 0x06); // data-rate < 2.88 Gbps
179 else
180 if (init_param.lane_rate_kbps > 5520000)
181 ad9144_spi_write(dev, 0x289, 0x04); // data-rate > 5.52 Gbps
182 else
183 ad9144_spi_write(dev, 0x289, 0x05);
184 ad9144_spi_write(dev, 0x280, 0x01); // enable serdes pll
185 ad9144_spi_write(dev, 0x280, 0x05); // enable serdes calibration
186 mdelay(20);
187
188 ad9144_spi_read(dev, 0x281, &pll_stat);
189 if ((pll_stat & 0x01) != 0x01)
190 ad_printf("%s : PLL NOT locked!.\n", __func__);
191
192 ad9144_spi_write(dev, 0x268, 0x62); // equalizer
193
194 // data link layer
195
196 ad9144_spi_write(dev, 0x301, 0x01); // subclass-1
197 ad9144_spi_write(dev, 0x304, 0x00); // lmfc delay
198 ad9144_spi_write(dev, 0x305, 0x00); // lmfc delay
199 ad9144_spi_write(dev, 0x306, 0x0a); // receive buffer delay
200 ad9144_spi_write(dev, 0x307, 0x0a); // receive buffer delay
201 ad9144_spi_write(dev, 0x03a, 0x01); // sync-oneshot mode
202 ad9144_spi_write(dev, 0x03a, 0x81); // sync-enable
203 ad9144_spi_write(dev, 0x03a, 0xc1); // sysref-armed
204 ad9144_spi_write(dev, 0x300, 0x01); // enable link
205
206 // dac calibration
207
208 ad9144_spi_write(dev, 0x0e7, 0x38); // set calibration clock to 1m
209 ad9144_spi_write(dev, 0x0ed, 0xa6); // use isb reference of 38 to set cal
210 ad9144_spi_write(dev, 0x0e8, 0x03); // cal 2 dacs at once
211 ad9144_spi_write(dev, 0x0e9, 0x01); // single cal enable
212 ad9144_spi_write(dev, 0x0e9, 0x03); // single cal start
213 mdelay(10);
214
215 ad9144_spi_write(dev, 0x0e8, 0x01); // read dac-0
216 ad9144_spi_read(dev, 0x0e9, &cal_stat);
217 if ((cal_stat & 0xc0) != 0x80)
218 ad_printf("%s : dac-0 calibration failed!\n", __func__);
219
220 ad9144_spi_write(dev, 0x0e8, 0x02); // read dac-1
221 ad9144_spi_read(dev, 0x0e9, &cal_stat);
222 if ((cal_stat & 0xc0) != 0x80)
223 ad_printf("%s : dac-1 calibration failed!\n", __func__);
224
225 ad9144_spi_write(dev, 0x0e7, 0x30); // turn off cal clock
226
227 return ret;
228 }
229
230 /***************************************************************************//**
231 * @brief ad9144_status - return the status of the JESD interface
232 *******************************************************************************/
233 int32_t ad9144_status(spi_device *dev) {
234
235 uint8_t status = 0;
236 int32_t ret = 0;
237
238 // check for jesd status on all lanes
239 // failures on top are 100% guaranteed to make subsequent status checks fail
240
241 ad9144_spi_read(dev, REG_CODEGRPSYNCFLG, &status);
242 if (status != 0x0f)
243 {
244 ad_printf("%s : CGS NOT received (%x)!.\n", __func__, status);
245 ret = -1;
246 }
247 ad9144_spi_read(dev, REG_INITLANESYNCFLG, &status);
248 if (status != 0x0f)
249 {
250 ad_printf("%s : ILAS NOT received (%x)!.\n", __func__, status);
251 ret = -1;
252 }
253 ad9144_spi_read(dev, REG_FRAMESYNCFLG, &status);
254 if (status != 0x0f)
255 {
256 ad_printf("%s : framer OUT OF SYNC (%x)!.\n", __func__, status);
257 ret = -1;
258 }
259 ad9144_spi_read(dev, REG_GOODCHKSUMFLG, &status);
260 if (status != 0x0f)
261 {
262 ad_printf("%s : check-sum MISMATCH (%x)!.\n", __func__, status);
263 ret = -1;
264 }
265
266 return ret;
267 }
268
269 /***************************************************************************//**
270 * @brief ad9144_short_pattern_test
271 *******************************************************************************/
272 int32_t ad9144_short_pattern_test(spi_device *dev, ad9144_init_param init_param) {
273
274 uint32_t dac = 0;
275 uint32_t sample = 0;
276 uint8_t status = 0;
277
278 for (dac = 0; dac < init_param.active_converters; dac++) {
279 for (sample = 0; sample < 4; sample++) {
280 ad9144_spi_write(dev, 0x32c, ((sample << 4) | (dac << 2) | 0x00));
281 ad9144_spi_write(dev, 0x32e, (init_param.stpl_samples[dac][sample]>>8));
282 ad9144_spi_write(dev, 0x32d, (init_param.stpl_samples[dac][sample]>>0));
283 ad9144_spi_write(dev, 0x32c, ((sample << 4) | (dac << 2) | 0x01));
284 ad9144_spi_write(dev, 0x32c, ((sample << 4) | (dac << 2) | 0x03));
285 ad9144_spi_write(dev, 0x32c, ((sample << 4) | (dac << 2) | 0x01));
286 mdelay(1);
287
288 ad9144_spi_read(dev, 0x32f, &status);
289 if ((status & 0x1) == 0x1)
290 ad_printf("%s : short-pattern-test mismatch (0x%x, 0x%x 0x%x, 0x%x)!.\n",
291 __func__, dac, sample, init_param.stpl_samples[dac][sample], status);
292 }
293 }
294 return 0;
295 }
296
297 /***************************************************************************//**
298 * @brief ad9144_datapath_prbs_test
299 *******************************************************************************/
300 int32_t ad9144_datapath_prbs_test(spi_device *dev, ad9144_init_param init_param) {
301
302 uint8_t status = 0;
303 int32_t ret = 0;
304
305
306 ad9144_spi_write(dev, REG_PRBS, ((init_param.prbs_type << 2) | 0x03));
307 ad9144_spi_write(dev, REG_PRBS, ((init_param.prbs_type << 2) | 0x01));
308 mdelay(500);
309
310 ad9144_spi_write(dev, REG_SPI_PAGEINDX, 0x01);
311 ad9144_spi_read(dev, REG_PRBS, &status);
312 if ((status & 0xc0) != 0xc0)
313 {
314 ad_printf("%s : PRBS OUT OF SYNC (%x)!.\n", __func__, status);
315 ret = -1;
316 }
317 ad9144_spi_read(dev, REG_PRBS_ERROR_I, &status);
318 if (status != 0x00)
319 {
320 ad_printf("%s : PRBS I channel ERRORS (%x)!.\n", __func__, status);
321 ret = -1;
322 }
323 ad9144_spi_read(dev, REG_PRBS_ERROR_Q, &status);
324 if (status != 0x00)
325 {
326 ad_printf("%s : PRBS Q channel ERRORS (%x)!.\n", __func__, status);
327 ret = -1;
328 }
329
330 return ret;
331 }
332