USRP2 firmware fixes for EDK 10.1 microblaze toolchain. Firmware now builds
[debian/gnuradio] / usrp2 / firmware / lib / db_init.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include <memory_map.h>
21 #include <i2c.h>
22 #include <usrp2_i2c_addr.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <db.h>
26 #include <db_base.h>
27 #include <hal_io.h>
28 #include <nonstdio.h>
29
30
31 struct db_base *tx_dboard;      // the tx daughterboard that's installed
32 struct db_base *rx_dboard;      // the rx daughterboard that's installed
33
34 extern struct db_base db_basic_tx;
35 extern struct db_base db_basic_rx;
36 extern struct db_base db_lf_tx;
37 extern struct db_base db_lf_rx;
38 extern struct db_base db_rfx_400_tx;
39 extern struct db_base db_rfx_400_rx;
40 extern struct db_base db_tvrx1;
41 extern struct db_base db_tvrx2;
42 extern struct db_base db_tvrx3;
43
44 struct db_base *all_dboards[] = {
45   &db_basic_tx,
46   &db_basic_rx,
47   &db_lf_tx,
48   &db_lf_rx,
49   &db_rfx_400_tx,
50   &db_rfx_400_rx,
51   &db_tvrx1,
52   &db_tvrx2,
53   &db_tvrx3,
54   0
55 };
56
57
58 typedef enum { UDBE_OK, UDBE_NO_EEPROM, UDBE_INVALID_EEPROM } usrp_dbeeprom_status_t;
59
60 static usrp_dbeeprom_status_t
61 read_raw_dboard_eeprom (unsigned char *buf, int i2c_addr)
62 {
63   if (!eeprom_read (i2c_addr, 0, buf, DB_EEPROM_CLEN))
64     return UDBE_NO_EEPROM;
65
66   if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE)
67     return UDBE_INVALID_EEPROM;
68
69   int sum = 0;
70   unsigned int i;
71   for (i = 0; i < DB_EEPROM_CLEN; i++)
72     sum += buf[i];
73
74   if ((sum & 0xff) != 0)
75     return UDBE_INVALID_EEPROM;
76
77   return UDBE_OK;
78 }
79
80
81 /*
82  * Return DBID, -1 <none> or -2 <invalid eeprom contents>
83  */
84 static int
85 read_dboard_eeprom(int i2c_addr)
86 {
87   unsigned char buf[DB_EEPROM_CLEN];
88
89   usrp_dbeeprom_status_t s = read_raw_dboard_eeprom (buf, i2c_addr);
90
91   //printf("\nread_raw_dboard_eeprom: %d\n", s);
92
93   switch (s){
94   case UDBE_OK:
95     return (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB];
96
97   case UDBE_NO_EEPROM:
98   default:
99     return -1;
100
101   case UDBE_INVALID_EEPROM:
102     return -2;
103   }
104 }
105
106
107 static struct db_base *
108 lookup_dbid(int dbid)
109 {
110   if (dbid < 0)
111     return 0;
112
113   int i;
114   for (i = 0; all_dboards[i]; i++)
115     if (all_dboards[i]->dbid == dbid)
116       return all_dboards[i];
117
118   return 0;
119 }
120
121 static struct db_base *
122 lookup_dboard(int i2c_addr, struct db_base *default_db, char *msg)
123 {
124   struct db_base *db;
125   int dbid = read_dboard_eeprom(i2c_addr);
126   printf("%s dbid: 0x%x\n", msg, dbid);
127
128   if (dbid < 0){        // there was some kind of problem.  Treat as Basic Tx
129     return default_db;
130   }
131   else if ((db = lookup_dbid(dbid)) == 0){
132     return default_db;
133     printf("No daugherboard code for dbid = 0x%x\n", dbid);
134   }
135   return db;
136 }
137
138 static void
139 set_atr_regs(int bank, struct db_base *db)
140 {
141   uint32_t      val[4];
142   int           shift;
143   int           mask;
144   int           i;
145
146   val[ATR_IDLE] = db->atr_rxval;
147   val[ATR_RX]   = db->atr_rxval;
148   val[ATR_TX]   = db->atr_txval;
149   val[ATR_FULL] = db->atr_txval;
150
151   if (bank == GPIO_TX_BANK){
152     mask = 0xffff0000;
153     shift = 16;
154   }
155   else {
156     mask = 0x0000ffff;
157     shift = 0;
158   }
159
160   for (i = 0; i < 4; i++){
161     int t = (atr_regs->v[i] & ~mask) | ((val[i] << shift) & mask);
162     //printf("atr_regs[%d] = 0x%x\n", i, t);
163     atr_regs->v[i] = t;
164   }
165 }
166
167 static void
168 set_gpio_mode(int bank, struct db_base *db)
169 {
170   int   i;
171
172   hal_gpio_set_ddr(bank, db->output_enables, 0xffff);
173   set_atr_regs(bank, db);
174
175   for (i = 0; i < 16; i++){
176     if (db->used_pins & (1 << i)){
177       // set to either GPIO_SEL_SW or GPIO_SEL_ATR
178       hal_gpio_set_sel(bank, i, (db->atr_mask & (1 << i)) ? 'a' : 's');
179     }
180   }
181 }
182
183 void
184 db_init(void)
185 {
186
187   tx_dboard = lookup_dboard(I2C_ADDR_TX_A, &db_basic_tx, "Tx");
188   //printf("db_init: tx dbid = 0x%x\n", tx_dboard->dbid);
189   set_gpio_mode(GPIO_TX_BANK, tx_dboard);
190   tx_dboard->init(tx_dboard);
191
192   rx_dboard = lookup_dboard(I2C_ADDR_RX_A, &db_basic_rx, "Rx");
193   //printf("db_init: rx dbid = 0x%x\n", rx_dboard->dbid);
194   set_gpio_mode(GPIO_RX_BANK, rx_dboard);
195   rx_dboard->init(rx_dboard);
196 }
197
198 /*!
199  *  Calculate the frequency to use for setting the digital down converter.
200  *
201  *  \param[in] target_freq   desired RF frequency (Hz)
202  *  \param[in] baseband_freq the RF frequency that corresponds to DC in the IF.
203  * 
204  *  \param[out] dxc_freq is the value for the ddc
205  *  \param[out] inverted is true if we're operating in an inverted Nyquist zone.
206 */
207 void
208 calc_dxc_freq(u2_fxpt_freq_t target_freq, u2_fxpt_freq_t baseband_freq,
209               u2_fxpt_freq_t *dxc_freq, bool *inverted)
210 {
211   u2_fxpt_freq_t fs = U2_DOUBLE_TO_FXPT_FREQ(100e6);    // converter sample rate
212   u2_fxpt_freq_t delta = target_freq - baseband_freq;
213
214 #if 0
215   printf("calc_dxc_freq\n");
216   printf("  fs       = "); print_fxpt_freq(fs); newline();
217   printf("  target   = "); print_fxpt_freq(target_freq); newline();
218   printf("  baseband = "); print_fxpt_freq(baseband_freq); newline();
219   printf("  delta    = "); print_fxpt_freq(delta); newline();
220 #endif  
221
222   if (delta >= 0){
223     while (delta > fs)
224       delta -= fs;
225     if (delta <= fs/2){         // non-inverted region
226       *dxc_freq = -delta;
227       *inverted = false;
228     }
229     else {                      // inverted region
230       *dxc_freq = delta - fs;
231       *inverted = true;
232     }
233   }
234   else {
235     while (delta < -fs){
236       delta += fs;
237       if (delta >= -fs/2){      // non-inverted region
238         *dxc_freq = -delta;
239         *inverted = false;
240       }
241       else {                    // inverted region
242         *dxc_freq = delta + fs;
243         *inverted = true;
244       }
245     }
246   }
247 }
248
249
250 bool
251 db_tune(struct db_base *db, u2_fxpt_freq_t target_freq, struct tune_result *result)
252 {
253   memset(result, 0, sizeof(*result));
254   bool inverted = false;
255   u2_fxpt_freq_t dxc_freq;
256   u2_fxpt_freq_t actual_dxc_freq;
257
258   // Ask the d'board to tune as closely as it can to target_freq
259   bool ok = db->set_freq(db, target_freq, &result->baseband_freq);
260
261   // Calculate the DDC setting that will downconvert the baseband from the
262   // daughterboard to our target frequency.
263   calc_dxc_freq(target_freq, result->baseband_freq, &dxc_freq, &inverted);
264
265   // If the spectrum is inverted, and the daughterboard doesn't do
266   // quadrature downconversion, we can fix the inversion by flipping the
267   // sign of the dxc_freq...  (This only happens using the basic_rx board)
268   
269   if (db->spectrum_inverted)
270     inverted = !inverted;
271
272   if (inverted && !db->is_quadrature){
273     dxc_freq = -dxc_freq;
274     inverted = !inverted;
275   }
276
277   if (db->is_tx){
278     dxc_freq = -dxc_freq;       // down conversion versus up conversion
279     ok &= db_set_duc_freq(dxc_freq, &actual_dxc_freq);
280   }
281   else {
282     ok &= db_set_ddc_freq(dxc_freq, &actual_dxc_freq);
283   }
284
285   result->dxc_freq = dxc_freq;
286   result->residual_freq = dxc_freq - actual_dxc_freq;
287   result->inverted = inverted;
288   return ok;
289 }
290
291 static int32_t
292 compute_freq_control_word(u2_fxpt_freq_t target_freq, u2_fxpt_freq_t *actual_freq)
293 {
294   // If we were using floating point, we'd calculate
295   //   master = 100e6;
296   //   v = (int) rint(target_freq / master_freq) * pow(2.0, 32.0);
297
298   //printf("compute_freq_control_word\n");
299   //printf("  target_freq = "); print_fxpt_freq(target_freq); newline();
300
301   int32_t master_freq = 100000000;      // 100M
302
303   int32_t v = ((target_freq << 12)) / master_freq;
304   //printf("  fcw = %d\n", v);
305
306   *actual_freq = (v * (int64_t) master_freq) >> 12;
307
308   //printf("  actual = "); print_fxpt_freq(*actual_freq); newline();
309
310   return v;
311 }
312
313
314 bool
315 db_set_ddc_freq(u2_fxpt_freq_t dxc_freq, u2_fxpt_freq_t *actual_dxc_freq)
316 {
317   int32_t v = compute_freq_control_word(dxc_freq, actual_dxc_freq);
318   dsp_rx_regs->freq = v;
319   return true;
320 }
321
322 bool
323 db_set_duc_freq(u2_fxpt_freq_t dxc_freq, u2_fxpt_freq_t *actual_dxc_freq)
324 {
325   int32_t v = compute_freq_control_word(dxc_freq, actual_dxc_freq);
326   dsp_tx_regs->freq = v;
327   return true;
328 }
329
330 bool
331 db_set_gain(struct db_base *db, u2_fxpt_gain_t gain)
332 {
333   return db->set_gain(db, gain);
334 }