Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / db_xcvr2450.c
1 /*
2  * Copyright 2009 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <memory_map.h>
19 #include <db_base.h>
20 #include <stdio.h>
21 #include <spi.h>
22 #include <hal_io.h>
23 #include <clocks.h>
24 #include <mdelay.h>
25
26 void set_atr_regs(int bank, struct db_base *db); //FIXME I need to be in a header
27
28 // RX IO Pins
29 #define LOCKDET (1 << 15)           // This is an INPUT!!!
30 #define EN      (1 << 14)
31 #define RX_EN   (1 << 13)           // 1 = RX on, 0 = RX off
32 #define RX_HP   (1 << 12)
33 #define B1      (1 << 11)
34 #define B2      (1 << 10)
35 #define B3      (1 << 9)
36 #define B4      (1 << 8)
37 #define B5      (1 << 7)
38 #define B6      (1 << 6)
39 #define B7      (1 << 5)
40 #define RX_OE_MASK EN|RX_EN|RX_HP|B1|B2|B3|B4|B5|B6|B7
41 #define RX_SAFE_IO EN
42 #define RX_ATR_MASK EN|RX_EN|RX_HP
43
44 // TX IO Pins
45 #define HB_PA_OFF      (1 << 15)    // 5GHz PA, 1 = off, 0 = on
46 #define LB_PA_OFF      (1 << 14)    // 2.4GHz PA, 1 = off, 0 = on
47 #define ANTSEL_TX1_RX2 (1 << 13)    // 1 = Ant 1 to TX, Ant 2 to RX
48 #define ANTSEL_TX2_RX1 (1 << 12)    // 1 = Ant 2 to TX, Ant 1 to RX
49 #define TX_EN          (1 << 11)    // 1 = TX on, 0 = TX off
50 #define AD9515DIV      (1 << 4)     // 1 = Div  by 3, 0 = Div by 2
51 #define TX_OE_MASK HB_PA_OFF|LB_PA_OFF|ANTSEL_TX1_RX2|ANTSEL_TX2_RX1|TX_EN|AD9515DIV
52 #define TX_SAFE_IO HB_PA_OFF|LB_PA_OFF|ANTSEL_TX1_RX2|AD9515DIV
53 #define TX_ATR_MASK HB_PA_OFF|LB_PA_OFF|ANTSEL_TX1_RX2|ANTSEL_TX2_RX1|TX_EN|AD9515DIV
54
55 #define LB_FREQ_MIN U2_DOUBLE_TO_FXPT_FREQ(2.3e9)
56 #define LB_FREQ_MAX U2_DOUBLE_TO_FXPT_FREQ(2.6e9)
57 #define HB_FREQ_MIN U2_DOUBLE_TO_FXPT_FREQ(4.8e9)
58 #define HB_FREQ_MAX U2_DOUBLE_TO_FXPT_FREQ(6.1e9)
59 #define MASTER_REF_CLK_DIV 1
60 #define N_DIV_MIN_Q16 (131 << 16)
61
62 bool xcvr2450_init(struct db_base *db);
63 bool xcvr2450_set_freq(struct db_base *db, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc);
64 bool xcvr2450_set_gain_rx(struct db_base *db, u2_fxpt_gain_t gain);
65 bool xcvr2450_set_gain_tx(struct db_base *db, u2_fxpt_gain_t gain);
66 bool xcvr2450_set_tx_enable(struct db_base *db, bool on);
67
68 struct db_xcvr2450_common {
69   int d_mimo, d_int_div, d_frac_div, d_highband, d_five_gig;
70   int d_cp_current, d_ref_div, d_rssi_hbw;
71   int d_txlpf_bw, d_rxlpf_bw, d_rxlpf_fine, d_rxvga_ser;
72   int d_rssi_range, d_rssi_mode, d_rssi_mux;
73   int d_rx_hp_pin, d_rx_hpf, d_rx_ant;
74   int d_tx_ant, d_txvga_ser, d_tx_driver_lin;
75   int d_tx_vga_lin, d_tx_upconv_lin, d_tx_bb_gain;
76   int d_pabias_delay, d_pabias;
77   int d_rx_rf_gain, d_rx_bb_gain, d_txgain;
78   int d_ad9515_div;
79   int d_tx_enb;
80 };
81
82 struct db_xcvr2450_dummy {
83   struct db_base base;
84   struct db_xcvr2450_common *common;
85 };
86
87 struct db_xcvr2450_rx {
88   struct db_base base;
89   struct db_xcvr2450_common *common;
90 };
91
92 struct db_xcvr2450_tx {
93   struct db_base base;
94   struct db_xcvr2450_common *common;
95 };
96
97 /*
98  * shared common between rx and tx db
99  */
100 struct db_xcvr2450_common db_xcvr2450_common = {
101   /* set sane defaults */
102   .d_mimo = 1,          // 0 = OFF, 1 = ON
103   .d_int_div = 192,     // 128 = min, 255 = max
104   .d_frac_div = 0,      // 0 = min, 65535 = max
105   .d_highband = 0,      // 0 = freq <= 5.4e9, 1 = freq > 5.4e9
106   .d_five_gig = 0,      // 0 = freq <= 3.e9, 1 = freq > 3e9
107   .d_cp_current = 0,    // 0 = 2mA, 1 = 4mA
108   .d_ref_div = 1,       // 1 to 7
109   .d_rssi_hbw = 0,      // 0 = 2 MHz, 1 = 6 MHz
110   .d_txlpf_bw = 1,      // 1 = 12 MHz, 2 = 18 MHz, 3 = 24 MHz
111   .d_rxlpf_bw = 1,      // 0 = 7.5 MHz, 1 = 9.5 MHz, 2 = 14 MHz, 3 = 18 MHz
112   .d_rxlpf_fine = 2,    // 0 = 90%, 1 = 95%, 2 = 100%, 3 = 105%, 4 = 110%
113   .d_rxvga_ser = 1,     // 0 = RXVGA controlled by B7:1, 1=controlled serially
114   .d_rssi_range = 1,    // 0 = low range (datasheet typo), 1=high range (0.5V - 2.0V)
115   .d_rssi_mode = 1,     // 0 = enable follows RXHP, 1 = enabled
116   .d_rssi_mux = 0,      // 0 = RSSI, 1 = TEMP
117   .d_rx_hp_pin = 0,     // 0 = Fc set by rx_hpf, 1 = 600 KHz
118   .d_rx_hpf = 0,        // 0 = 100Hz, 1 = 30KHz
119   .d_rx_ant = 0,        // 0 = Ant. #1, 1 = Ant. #2
120   .d_tx_ant = 0,        // 0 = Ant. #1, 1 = Ant. #2
121   .d_txvga_ser = 1,     // 0 = TXVGA controlled by B6:1, 1=controlled serially
122   .d_tx_driver_lin = 2, // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
123   .d_tx_vga_lin = 2,    // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
124   .d_tx_upconv_lin = 2, // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
125   .d_tx_bb_gain = 3,    // 0=maxgain-5dB, 1=max-3dB, 2=max-1.5dB, 3=max
126   .d_pabias_delay = 15, // 0 = 0, 15 = 7uS
127   .d_pabias = 0,        // 0 = 0 uA, 63 = 315uA
128   .d_rx_rf_gain = 0,    // 0 = 0dB, 1 = 0dB, 2 = 15dB, 3 = 30dB
129   .d_rx_bb_gain = 16,   // 0 = min, 31 = max (0 - 62 dB)
130   .d_txgain = 63,       // 0 = min, 63 = max
131   .d_tx_enb = 1,        // 0 = disabled, 1 = enabled
132 };
133
134 /*
135  * The class instances
136  */
137 struct db_xcvr2450_rx db_xcvr2450_rx = {
138   .base.dbid = 0x0061,
139   .base.is_tx = false,
140   .base.output_enables = RX_OE_MASK,
141   .base.used_pins = 0xFFFF,
142   .base.freq_min = LB_FREQ_MIN,
143   .base.freq_max = HB_FREQ_MAX,
144   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
145   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(92),
146   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
147   .base.is_quadrature = true,
148   .base.i_and_q_swapped = false,
149   .base.spectrum_inverted = false,
150   .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
151   .base.init = xcvr2450_init,
152   .base.set_freq = xcvr2450_set_freq,
153   .base.set_gain = xcvr2450_set_gain_rx,
154   .base.atr_mask = RX_ATR_MASK,
155   .base.atr_txval = 0x0,
156   .base.atr_rxval = 0x0,
157   .common = &db_xcvr2450_common,
158 };
159
160 struct db_xcvr2450_tx db_xcvr2450_tx = {
161   .base.dbid = 0x0060,
162   .base.is_tx = true,
163   .base.output_enables = TX_OE_MASK,
164   .base.used_pins = 0xFFFF,
165   .base.freq_min = LB_FREQ_MIN,
166   .base.freq_max = HB_FREQ_MAX,
167   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
168   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(30),
169   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(30.0/63.0),
170   .base.is_quadrature = true,
171   .base.i_and_q_swapped = true,
172   .base.spectrum_inverted = false,
173   .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
174   .base.init = xcvr2450_init,
175   .base.set_freq = xcvr2450_set_freq,
176   .base.set_gain = xcvr2450_set_gain_tx,
177   .base.set_tx_enable = xcvr2450_set_tx_enable,
178   .base.atr_mask = TX_ATR_MASK,
179   .base.atr_txval = 0x0,
180   .base.atr_rxval = 0x0,
181   .common = &db_xcvr2450_common,
182 };
183
184 /**************************************************
185  * Set Registers
186  **************************************************/
187 static void
188 send_reg(int v){
189   // Send 24 bits, it keeps last 18 clocked in
190   spi_transact(SPI_TXONLY,SPI_SS_RX_DB,v,24,SPIF_PUSH_FALL);
191   //printf("xcvr2450: Setting reg %d to %x\n", (v&15), v);
192 }
193
194 static void
195 set_reg_standby(struct db_xcvr2450_dummy *db){
196   int reg_standby = (
197     (db->common->d_mimo<<17) |
198     (1<<16)                 |
199     (1<<6)                  |
200     (1<<5)                  |
201     (1<<4)                  | 2);
202   send_reg(reg_standby);
203 }
204
205 static void
206 set_reg_int_divider(struct db_xcvr2450_dummy *db){
207   int reg_int_divider = ((
208     (db->common->d_frac_div & 0x03)<<16) |
209     (db->common->d_int_div<<4)           | 3);
210   send_reg(reg_int_divider);
211 }
212
213 static void
214 set_reg_frac_divider(struct db_xcvr2450_dummy *db){
215   int reg_frac_divider = ((db->common->d_frac_div & 0xfffc)<<2) | 4;
216   send_reg(reg_frac_divider);
217 }
218
219 static void
220 set_reg_bandselpll(struct db_xcvr2450_dummy *db){
221   int reg_bandselpll = ((db->common->d_mimo<<17) |
222     (1<<16) |
223     (1<<15) |
224     (0<<11) | //this bit gets toggled
225     (db->common->d_highband<<10)  |
226     (db->common->d_cp_current<<9) |
227     (db->common->d_ref_div<<5)    |
228     (db->common->d_five_gig<<4)   | 5);
229   send_reg(reg_bandselpll);
230   reg_bandselpll = ((db->common->d_mimo<<17) |
231     (1<<16) |
232     (1<<15) |
233     (1<<11) |
234     (db->common->d_highband<<10)  |
235     (db->common->d_cp_current<<9) |
236     (db->common->d_ref_div<<5)    |
237     (db->common->d_five_gig<<4)   | 5);
238   send_reg(reg_bandselpll);
239 }
240
241 static void
242 set_reg_cal(struct db_xcvr2450_dummy *db){
243   // FIXME do calibration
244   int reg_cal = (
245     (1<<14) | 6);
246   send_reg(reg_cal);
247 }
248
249 static void
250 set_reg_lpf(struct db_xcvr2450_dummy *db){
251   int reg_lpf = (
252     (db->common->d_rssi_hbw<<15)  |
253     (db->common->d_txlpf_bw<<10)  |
254     (db->common->d_rxlpf_bw<<9)   |
255     (db->common->d_rxlpf_fine<<4) | 7);
256   send_reg(reg_lpf);
257 }
258
259 static void
260 set_reg_rxrssi_ctrl(struct db_xcvr2450_dummy *db){
261   int reg_rxrssi_ctrl = (
262        (db->common->d_rxvga_ser<<16)  |
263        (db->common->d_rssi_range<<15) |
264        (db->common->d_rssi_mode<<14)  |
265        (db->common->d_rssi_mux<<12)   |
266        (1<<9)                        |
267        (db->common->d_rx_hpf<<6)      |
268        (1<<4)                        | 8);
269   send_reg(reg_rxrssi_ctrl);
270 }
271
272 static void
273 set_reg_txlin_gain(struct db_xcvr2450_dummy *db){
274   int reg_txlin_gain = (
275       (db->common->d_txvga_ser<<14)     |
276       (db->common->d_tx_driver_lin<<12) |
277       (db->common->d_tx_vga_lin<<10)    |
278       (db->common->d_tx_upconv_lin<<6)  |
279       (db->common->d_tx_bb_gain<<4)     | 9);
280   send_reg(reg_txlin_gain);
281 }
282
283 static void
284 set_reg_pabias(struct db_xcvr2450_dummy *db){
285   int reg_pabias = (
286       (db->common->d_pabias_delay<<10) |
287       (db->common->d_pabias<<4)        | 10);
288   send_reg(reg_pabias);
289 }
290
291 static void
292 set_reg_rxgain(struct db_xcvr2450_dummy *db){
293   int reg_rxgain = (
294     (db->common->d_rx_rf_gain<<9) |
295     (db->common->d_rx_bb_gain<<4) | 11);
296   send_reg(reg_rxgain);
297 }
298
299 static void
300 set_reg_txgain(struct db_xcvr2450_dummy *db){
301   int reg_txgain = (
302     (db->common->d_txgain<<4) | 12);
303   send_reg(reg_txgain);
304 }
305
306 /**************************************************
307  * GPIO
308  **************************************************/
309 static void
310 set_gpio(struct db_xcvr2450_dummy *db){
311   //set tx/rx gpio pins for auto tr
312   int tx_enb_sel = (db->common->d_tx_enb)? TX_EN:0;
313   int ad9515_sel = (db->common->d_ad9515_div == 3)? AD9515DIV:0;
314   int rx_hp = (db->common->d_rx_hp_pin)? RX_HP:0;
315   int tx_antsel = (db->common->d_tx_ant)? ANTSEL_TX2_RX1:ANTSEL_TX1_RX2;
316   int rx_antsel = (db->common->d_rx_ant)? ANTSEL_TX2_RX1:ANTSEL_TX1_RX2;
317   int tx_pa_sel = (db->common->d_five_gig)? LB_PA_OFF:HB_PA_OFF;
318
319   /* FIXME better way to set rx and tx val for RX and TX banks */
320   /* set rx bank */
321   db->base.atr_rxval = EN|rx_hp|RX_EN;
322   db->base.atr_txval = EN|rx_hp;
323   set_atr_regs(GPIO_RX_BANK, (struct db_base *)db);
324   /* set tx bank */
325   db->base.atr_rxval = HB_PA_OFF|LB_PA_OFF|rx_antsel|ad9515_sel;
326   db->base.atr_txval = tx_pa_sel|tx_antsel|tx_enb_sel|ad9515_sel;
327   set_atr_regs(GPIO_TX_BANK, (struct db_base *)db);
328 }
329
330 /**************************************************
331  * Init for TX and RX
332  **************************************************/
333 bool
334 xcvr2450_init(struct db_base *dbb){
335   struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
336   /* Initialize chipset */
337   clocks_enable_tx_dboard(true, MASTER_REF_CLK_DIV);
338   set_gpio(db);
339   set_reg_standby(db);
340   set_reg_bandselpll(db);
341   set_reg_cal(db);
342   set_reg_lpf(db);
343   set_reg_rxrssi_ctrl(db);
344   set_reg_txlin_gain(db);
345   set_reg_pabias(db);
346   set_reg_rxgain(db);
347   set_reg_txgain(db);
348   //u2_fxpt_freq_t dc;
349   //db->base.set_freq(dbb, U2_DOUBLE_TO_FXPT_FREQ(2.434e9), &dc);
350   return true;
351 }
352
353 /**************************************************
354  * Lock detect
355  **************************************************/
356 static bool
357 lock_detect(){
358   //true when the VCO/PLL lock detect bit is set.
359   if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET) {
360     return true;
361   }
362   else {      // Give it a second chance
363     mdelay(1);
364     if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET)
365       return true;
366     else
367       return false;
368   }
369 }
370
371 /**************************************************
372  * Set the freq
373  **************************************************/
374 bool
375 xcvr2450_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc){
376   unsigned int scaler, div_factor, actual_div_q16;
377   struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
378   /* determine if the freq range is in low or high band */
379   if (freq >= LB_FREQ_MIN && freq <= LB_FREQ_MAX) {
380     db->common->d_five_gig = 0;
381     scaler = 3;
382     //printf("2.4-GHZ\n");
383   } else if (freq >= HB_FREQ_MIN && freq <= HB_FREQ_MAX) {
384     db->common->d_five_gig = 1;
385     scaler = 5;
386     //printf("5-GHZ\n");
387   } else {
388     printf("Out of range\n");
389     return false;
390   }
391   /* set the highband bit */
392   if(freq > U2_DOUBLE_TO_FXPT_FREQ(5.408e9)) {
393     db->common->d_highband = 1;
394     //printf("5-HB\n");
395   }
396   else {
397     db->common->d_highband = 0;
398     //printf("5-LB\n");
399   }
400   unsigned int loop_iter = 0;
401   do { /* set the dividers so that the n divider is above the practical minimum */
402     switch(loop_iter){
403       case 0:
404         db->common->d_ad9515_div = 3;
405         db->common->d_ref_div = 1;
406         break;
407       case 1:
408         db->common->d_ad9515_div = 2;
409         db->common->d_ref_div = 2;
410         break;
411       default:
412         db->common->d_ad9515_div = 3;
413         db->common->d_ref_div = loop_iter;
414     }
415     loop_iter++;
416     div_factor = db->common->d_ref_div*db->common->d_ad9515_div*4*MASTER_REF_CLK_DIV;
417     actual_div_q16 = ((freq*div_factor)/(scaler*MASTER_CLK_RATE)) >> (U2_FPF_RP-16);
418   } while (actual_div_q16 < N_DIV_MIN_Q16);
419   /* calculate the divisors */
420   db->common->d_int_div = actual_div_q16 >> 16;
421   db->common->d_frac_div = actual_div_q16 & 0xffff; //isolate lower 16 bits
422   /* calculate the dc freq */
423   *dc = ((((u2_fxpt_freq_t)MASTER_CLK_RATE)*actual_div_q16*scaler) / div_factor) << (U2_FPF_RP-16);
424   /*printf("scaler %d, div(int) %u, div_factor %d, ad9515_div %u, ref_div %u\n",
425     scaler, db->common->d_int_div, div_factor, db->common->d_ad9515_div, db->common->d_ref_div);
426   printf("actual div %u, Target Freq %uKHz, Actual Freq %uKHz\n",
427     actual_div_q16, u2_fxpt_freq_round_to_int(freq/1000), u2_fxpt_freq_round_to_int(*dc/1000));
428   */
429   set_gpio(db);
430   set_reg_int_divider(db);
431   set_reg_frac_divider(db);
432   set_reg_bandselpll(db);
433
434   bool ok = lock_detect();
435   if(!ok){
436     printf("Fail lock detect %uKHz\n", u2_fxpt_freq_round_to_int(freq/1000));
437   }
438   return ok;
439 }
440
441 /**************************************************
442  * Set RX Gain
443  **************************************************/
444 bool
445 xcvr2450_set_gain_rx(struct db_base *dbb, u2_fxpt_gain_t gain){
446   struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
447   //ensure gain is within range
448   if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
449     return false;
450   }
451   // Split the gain between RF and baseband
452   // This is experimental, not prescribed
453   if(gain < U2_DOUBLE_TO_FXPT_GAIN(30.0)) {
454     db->common->d_rx_rf_gain = 0; // 0 dB RF gain
455     db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int(gain/2);
456   }
457   else if(gain >= U2_DOUBLE_TO_FXPT_GAIN(30.0) && gain < U2_DOUBLE_TO_FXPT_GAIN(61.0)) {
458     db->common->d_rx_rf_gain = 2; // 15 dB RF gain
459     db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int((gain-U2_DOUBLE_TO_FXPT_GAIN(15.0))/2);
460   }
461   else if(gain >= U2_DOUBLE_TO_FXPT_GAIN(61.0)) {
462     db->common->d_rx_rf_gain = 3; // 30.5 dB RF gain
463     db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int((gain-U2_DOUBLE_TO_FXPT_GAIN(30.5))/2);
464   }
465   //printf("RX RF Gain %u, RX BB Gain %u\n", db->common->d_rx_rf_gain, db->common->d_rx_bb_gain);
466   set_reg_rxgain(db);
467   return true;
468 }
469
470 /**************************************************
471  * Set TX Gain
472  **************************************************/
473 bool
474 xcvr2450_set_gain_tx(struct db_base *dbb, u2_fxpt_gain_t gain){
475   struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
476   //ensure gain in within range
477   if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
478     return false;
479   }
480   //scale for register and set
481   db->common->d_txgain = (gain*63)/db->base.gain_max;
482   //printf("TX Gain %u, TX Reg %u\n", u2_fxpt_gain_round_to_int(gain), db->common->d_txgain);
483   set_reg_txgain(db);
484   return true;
485 }
486
487 /**************************************************
488  * Set TX Enable
489  **************************************************/
490 bool
491 xcvr2450_set_tx_enable(struct db_base *dbb, bool on){
492   struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
493   db->common->d_tx_enb = on;
494   set_gpio(db);
495   return true;
496 }