2 * Copyright 2009 Free Software Foundation, Inc.
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.
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.
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/>.
18 #include <memory_map.h>
26 void set_atr_regs(int bank, struct db_base *db); //FIXME I need to be in a header
29 #define LOCKDET (1 << 15) // This is an INPUT!!!
31 #define RX_EN (1 << 13) // 1 = RX on, 0 = RX off
32 #define RX_HP (1 << 12)
40 #define RX_OE_MASK EN|RX_EN|RX_HP|B1|B2|B3|B4|B5|B6|B7
42 #define RX_ATR_MASK EN|RX_EN|RX_HP
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
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)
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);
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;
82 struct db_xcvr2450_dummy {
84 struct db_xcvr2450_common *common;
87 struct db_xcvr2450_rx {
89 struct db_xcvr2450_common *common;
92 struct db_xcvr2450_tx {
94 struct db_xcvr2450_common *common;
98 * shared common between rx and tx db
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
135 * The class instances
137 struct db_xcvr2450_rx db_xcvr2450_rx = {
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,
160 struct db_xcvr2450_tx db_xcvr2450_tx = {
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,
184 /**************************************************
186 **************************************************/
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);
195 set_reg_standby(struct db_xcvr2450_dummy *db){
197 (db->common->d_mimo<<17) |
202 send_reg(reg_standby);
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);
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);
220 set_reg_bandselpll(struct db_xcvr2450_dummy *db){
221 int reg_bandselpll = ((db->common->d_mimo<<17) |
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) |
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);
242 set_reg_cal(struct db_xcvr2450_dummy *db){
243 // FIXME do calibration
250 set_reg_lpf(struct db_xcvr2450_dummy *db){
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);
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) |
267 (db->common->d_rx_hpf<<6) |
269 send_reg(reg_rxrssi_ctrl);
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);
284 set_reg_pabias(struct db_xcvr2450_dummy *db){
286 (db->common->d_pabias_delay<<10) |
287 (db->common->d_pabias<<4) | 10);
288 send_reg(reg_pabias);
292 set_reg_rxgain(struct db_xcvr2450_dummy *db){
294 (db->common->d_rx_rf_gain<<9) |
295 (db->common->d_rx_bb_gain<<4) | 11);
296 send_reg(reg_rxgain);
300 set_reg_txgain(struct db_xcvr2450_dummy *db){
302 (db->common->d_txgain<<4) | 12);
303 send_reg(reg_txgain);
306 /**************************************************
308 **************************************************/
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;
319 /* FIXME better way to set rx and tx val for RX and TX banks */
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);
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);
330 /**************************************************
332 **************************************************/
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);
340 set_reg_bandselpll(db);
343 set_reg_rxrssi_ctrl(db);
344 set_reg_txlin_gain(db);
349 //db->base.set_freq(dbb, U2_DOUBLE_TO_FXPT_FREQ(2.434e9), &dc);
353 /**************************************************
355 **************************************************/
358 //true when the VCO/PLL lock detect bit is set.
359 if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET) {
362 else { // Give it a second chance
364 if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET)
371 /**************************************************
373 **************************************************/
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;
382 //printf("2.4-GHZ\n");
383 } else if (freq >= HB_FREQ_MIN && freq <= HB_FREQ_MAX) {
384 db->common->d_five_gig = 1;
388 printf("Out of range\n");
391 /* set the highband bit */
392 if(freq > U2_DOUBLE_TO_FXPT_FREQ(5.408e9)) {
393 db->common->d_highband = 1;
397 db->common->d_highband = 0;
400 unsigned int loop_iter = 0;
401 do { /* set the dividers so that the n divider is above the practical minimum */
404 db->common->d_ad9515_div = 3;
405 db->common->d_ref_div = 1;
408 db->common->d_ad9515_div = 2;
409 db->common->d_ref_div = 2;
412 db->common->d_ad9515_div = 3;
413 db->common->d_ref_div = 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));
430 set_reg_int_divider(db);
431 set_reg_frac_divider(db);
432 set_reg_bandselpll(db);
434 bool ok = lock_detect();
436 printf("Fail lock detect %uKHz\n", u2_fxpt_freq_round_to_int(freq/1000));
441 /**************************************************
443 **************************************************/
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)) {
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);
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);
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);
465 //printf("RX RF Gain %u, RX BB Gain %u\n", db->common->d_rx_rf_gain, db->common->d_rx_bb_gain);
470 /**************************************************
472 **************************************************/
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)) {
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);
487 /**************************************************
489 **************************************************/
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;