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);
67 bool xcvr2450_set_rx_antenna(struct db_base *db, int ant);
68 bool xcvr2450_set_tx_antenna(struct db_base *db, int ant);
70 struct db_xcvr2450_common {
71 int d_mimo, d_int_div, d_frac_div, d_highband, d_five_gig;
72 int d_cp_current, d_ref_div, d_rssi_hbw;
73 int d_txlpf_bw, d_rxlpf_bw, d_rxlpf_fine, d_rxvga_ser;
74 int d_rssi_range, d_rssi_mode, d_rssi_mux;
75 int d_rx_hp_pin, d_rx_hpf, d_rx_ant;
76 int d_tx_ant, d_txvga_ser, d_tx_driver_lin;
77 int d_tx_vga_lin, d_tx_upconv_lin, d_tx_bb_gain;
78 int d_pabias_delay, d_pabias;
79 int d_rx_rf_gain, d_rx_bb_gain, d_txgain;
84 struct db_xcvr2450_dummy {
86 struct db_xcvr2450_common *common;
89 struct db_xcvr2450_rx {
91 struct db_xcvr2450_common *common;
94 struct db_xcvr2450_tx {
96 struct db_xcvr2450_common *common;
100 * shared common between rx and tx db
102 struct db_xcvr2450_common db_xcvr2450_common = {
103 /* set sane defaults */
104 .d_mimo = 1, // 0 = OFF, 1 = ON
105 .d_int_div = 192, // 128 = min, 255 = max
106 .d_frac_div = 0, // 0 = min, 65535 = max
107 .d_highband = 0, // 0 = freq <= 5.4e9, 1 = freq > 5.4e9
108 .d_five_gig = 0, // 0 = freq <= 3.e9, 1 = freq > 3e9
109 .d_cp_current = 0, // 0 = 2mA, 1 = 4mA
110 .d_ref_div = 1, // 1 to 7
111 .d_rssi_hbw = 0, // 0 = 2 MHz, 1 = 6 MHz
112 .d_txlpf_bw = 1, // 1 = 12 MHz, 2 = 18 MHz, 3 = 24 MHz
113 .d_rxlpf_bw = 1, // 0 = 7.5 MHz, 1 = 9.5 MHz, 2 = 14 MHz, 3 = 18 MHz
114 .d_rxlpf_fine = 2, // 0 = 90%, 1 = 95%, 2 = 100%, 3 = 105%, 4 = 110%
115 .d_rxvga_ser = 1, // 0 = RXVGA controlled by B7:1, 1=controlled serially
116 .d_rssi_range = 1, // 0 = low range (datasheet typo), 1=high range (0.5V - 2.0V)
117 .d_rssi_mode = 1, // 0 = enable follows RXHP, 1 = enabled
118 .d_rssi_mux = 0, // 0 = RSSI, 1 = TEMP
119 .d_rx_hp_pin = 0, // 0 = Fc set by rx_hpf, 1 = 600 KHz
120 .d_rx_hpf = 0, // 0 = 100Hz, 1 = 30KHz
121 .d_rx_ant = 0, // 0 = Ant. #1, 1 = Ant. #2
122 .d_tx_ant = 0, // 0 = Ant. #1, 1 = Ant. #2
123 .d_txvga_ser = 1, // 0 = TXVGA controlled by B6:1, 1=controlled serially
124 .d_tx_driver_lin = 2, // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
125 .d_tx_vga_lin = 2, // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
126 .d_tx_upconv_lin = 2, // 0=50% (worst linearity), 1=63%, 2=78%, 3=100% (best lin)
127 .d_tx_bb_gain = 3, // 0=maxgain-5dB, 1=max-3dB, 2=max-1.5dB, 3=max
128 .d_pabias_delay = 15, // 0 = 0, 15 = 7uS
129 .d_pabias = 0, // 0 = 0 uA, 63 = 315uA
130 .d_rx_rf_gain = 0, // 0 = 0dB, 1 = 0dB, 2 = 15dB, 3 = 30dB
131 .d_rx_bb_gain = 16, // 0 = min, 31 = max (0 - 62 dB)
132 .d_txgain = 63, // 0 = min, 63 = max
133 .d_tx_enb = 1, // 0 = disabled, 1 = enabled
137 * The class instances
139 struct db_xcvr2450_rx db_xcvr2450_rx = {
142 .base.output_enables = RX_OE_MASK,
143 .base.used_pins = 0xFFFF,
144 .base.freq_min = LB_FREQ_MIN,
145 .base.freq_max = HB_FREQ_MAX,
146 .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
147 .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(92),
148 .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
149 .base.is_quadrature = true,
150 .base.i_and_q_swapped = false,
151 .base.spectrum_inverted = false,
152 .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
153 .base.init = xcvr2450_init,
154 .base.set_freq = xcvr2450_set_freq,
155 .base.set_gain = xcvr2450_set_gain_rx,
156 .base.atr_mask = RX_ATR_MASK,
157 .base.atr_txval = 0x0,
158 .base.atr_rxval = 0x0,
159 .base.set_antenna = xcvr2450_set_rx_antenna,
160 .common = &db_xcvr2450_common,
163 struct db_xcvr2450_tx db_xcvr2450_tx = {
166 .base.output_enables = TX_OE_MASK,
167 .base.used_pins = 0xFFFF,
168 .base.freq_min = LB_FREQ_MIN,
169 .base.freq_max = HB_FREQ_MAX,
170 .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
171 .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(30),
172 .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(30.0/63.0),
173 .base.is_quadrature = true,
174 .base.i_and_q_swapped = true,
175 .base.spectrum_inverted = false,
176 .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
177 .base.init = xcvr2450_init,
178 .base.set_freq = xcvr2450_set_freq,
179 .base.set_gain = xcvr2450_set_gain_tx,
180 .base.set_tx_enable = xcvr2450_set_tx_enable,
181 .base.atr_mask = TX_ATR_MASK,
182 .base.atr_txval = 0x0,
183 .base.atr_rxval = 0x0,
184 .base.set_antenna = xcvr2450_set_tx_antenna,
185 .common = &db_xcvr2450_common,
188 /**************************************************
190 **************************************************/
193 // Send 24 bits, it keeps last 18 clocked in
194 spi_transact(SPI_TXONLY,SPI_SS_RX_DB,v,24,SPIF_PUSH_FALL);
195 //printf("xcvr2450: Setting reg %d to %x\n", (v&15), v);
199 set_reg_standby(struct db_xcvr2450_dummy *db){
201 (db->common->d_mimo<<17) |
206 send_reg(reg_standby);
210 set_reg_int_divider(struct db_xcvr2450_dummy *db){
211 int reg_int_divider = ((
212 (db->common->d_frac_div & 0x03)<<16) |
213 (db->common->d_int_div<<4) | 3);
214 send_reg(reg_int_divider);
218 set_reg_frac_divider(struct db_xcvr2450_dummy *db){
219 int reg_frac_divider = ((db->common->d_frac_div & 0xfffc)<<2) | 4;
220 send_reg(reg_frac_divider);
224 set_reg_bandselpll(struct db_xcvr2450_dummy *db){
225 int reg_bandselpll = ((db->common->d_mimo<<17) |
228 (0<<11) | //this bit gets toggled
229 (db->common->d_highband<<10) |
230 (db->common->d_cp_current<<9) |
231 (db->common->d_ref_div<<5) |
232 (db->common->d_five_gig<<4) | 5);
233 send_reg(reg_bandselpll);
234 reg_bandselpll = ((db->common->d_mimo<<17) |
238 (db->common->d_highband<<10) |
239 (db->common->d_cp_current<<9) |
240 (db->common->d_ref_div<<5) |
241 (db->common->d_five_gig<<4) | 5);
242 send_reg(reg_bandselpll);
246 set_reg_cal(struct db_xcvr2450_dummy *db){
247 // FIXME do calibration
254 set_reg_lpf(struct db_xcvr2450_dummy *db){
256 (db->common->d_rssi_hbw<<15) |
257 (db->common->d_txlpf_bw<<9) |
258 (db->common->d_rxlpf_bw<<7) |
259 (db->common->d_rxlpf_fine<<4) | 7);
264 set_reg_rxrssi_ctrl(struct db_xcvr2450_dummy *db){
265 int reg_rxrssi_ctrl = (
266 (db->common->d_rxvga_ser<<16) |
267 (db->common->d_rssi_range<<15) |
268 (db->common->d_rssi_mode<<14) |
269 (db->common->d_rssi_mux<<12) |
271 (db->common->d_rx_hpf<<6) |
273 send_reg(reg_rxrssi_ctrl);
277 set_reg_txlin_gain(struct db_xcvr2450_dummy *db){
278 int reg_txlin_gain = (
279 (db->common->d_txvga_ser<<14) |
280 (db->common->d_tx_driver_lin<<12) |
281 (db->common->d_tx_vga_lin<<10) |
282 (db->common->d_tx_upconv_lin<<6) |
283 (db->common->d_tx_bb_gain<<4) | 9);
284 send_reg(reg_txlin_gain);
288 set_reg_pabias(struct db_xcvr2450_dummy *db){
290 (db->common->d_pabias_delay<<10) |
291 (db->common->d_pabias<<4) | 10);
292 send_reg(reg_pabias);
296 set_reg_rxgain(struct db_xcvr2450_dummy *db){
298 (db->common->d_rx_rf_gain<<9) |
299 (db->common->d_rx_bb_gain<<4) | 11);
300 send_reg(reg_rxgain);
304 set_reg_txgain(struct db_xcvr2450_dummy *db){
306 (db->common->d_txgain<<4) | 12);
307 send_reg(reg_txgain);
310 /**************************************************
312 **************************************************/
314 set_gpio(struct db_xcvr2450_dummy *db){
315 //set tx/rx gpio pins for auto tr
316 int tx_enb_sel = (db->common->d_tx_enb)? TX_EN:0;
317 int ad9515_sel = (db->common->d_ad9515_div == 3)? AD9515DIV:0;
318 int rx_hp = (db->common->d_rx_hp_pin)? RX_HP:0;
319 int tx_antsel = (db->common->d_tx_ant)? ANTSEL_TX2_RX1:ANTSEL_TX1_RX2;
320 int rx_antsel = (db->common->d_rx_ant)? ANTSEL_TX2_RX1:ANTSEL_TX1_RX2;
321 int tx_pa_sel = (db->common->d_five_gig)? LB_PA_OFF:HB_PA_OFF;
323 /* FIXME better way to set rx and tx val for RX and TX banks */
325 db->base.atr_rxval = EN|rx_hp|RX_EN;
326 db->base.atr_txval = EN|rx_hp;
327 set_atr_regs(GPIO_RX_BANK, (struct db_base *)db);
329 db->base.atr_rxval = HB_PA_OFF|LB_PA_OFF|rx_antsel|ad9515_sel;
330 db->base.atr_txval = tx_pa_sel|tx_antsel|tx_enb_sel|ad9515_sel;
331 set_atr_regs(GPIO_TX_BANK, (struct db_base *)db);
334 /**************************************************
336 **************************************************/
338 xcvr2450_init(struct db_base *dbb){
339 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
340 /* Initialize chipset */
341 clocks_enable_tx_dboard(true, MASTER_REF_CLK_DIV);
344 set_reg_bandselpll(db);
347 set_reg_rxrssi_ctrl(db);
348 set_reg_txlin_gain(db);
353 //db->base.set_freq(dbb, U2_DOUBLE_TO_FXPT_FREQ(2.434e9), &dc);
357 /**************************************************
359 **************************************************/
362 //true when the VCO/PLL lock detect bit is set.
363 if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET) {
366 else { // Give it a second chance
368 if(hal_gpio_read(GPIO_RX_BANK) & LOCKDET)
375 /**************************************************
377 **************************************************/
379 xcvr2450_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc){
380 unsigned int scaler, div_factor, actual_div_q16;
381 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
382 /* determine if the freq range is in low or high band */
383 if (freq >= LB_FREQ_MIN && freq <= LB_FREQ_MAX) {
384 db->common->d_five_gig = 0;
386 //printf("2.4-GHZ\n");
387 } else if (freq >= HB_FREQ_MIN && freq <= HB_FREQ_MAX) {
388 db->common->d_five_gig = 1;
392 printf("Out of range\n");
395 /* set the highband bit */
396 if(freq > U2_DOUBLE_TO_FXPT_FREQ(5.408e9)) {
397 db->common->d_highband = 1;
401 db->common->d_highband = 0;
404 unsigned int loop_iter = 0;
405 do { /* set the dividers so that the n divider is above the practical minimum */
408 db->common->d_ad9515_div = 3;
409 db->common->d_ref_div = 1;
412 db->common->d_ad9515_div = 2;
413 db->common->d_ref_div = 2;
416 db->common->d_ad9515_div = 3;
417 db->common->d_ref_div = loop_iter;
420 div_factor = db->common->d_ref_div*db->common->d_ad9515_div*4*MASTER_REF_CLK_DIV;
421 actual_div_q16 = ((freq*div_factor)/(scaler*MASTER_CLK_RATE)) >> (U2_FPF_RP-16);
422 } while (actual_div_q16 < N_DIV_MIN_Q16);
423 /* calculate the divisors */
424 db->common->d_int_div = actual_div_q16 >> 16;
425 db->common->d_frac_div = actual_div_q16 & 0xffff; //isolate lower 16 bits
426 /* calculate the dc freq */
427 *dc = ((((u2_fxpt_freq_t)MASTER_CLK_RATE)*actual_div_q16*scaler) / div_factor) << (U2_FPF_RP-16);
428 /*printf("scaler %d, div(int) %u, div_factor %d, ad9515_div %u, ref_div %u\n",
429 scaler, db->common->d_int_div, div_factor, db->common->d_ad9515_div, db->common->d_ref_div);
430 printf("actual div %u, Target Freq %uKHz, Actual Freq %uKHz\n",
431 actual_div_q16, u2_fxpt_freq_round_to_int(freq/1000), u2_fxpt_freq_round_to_int(*dc/1000));
434 set_reg_int_divider(db);
435 set_reg_frac_divider(db);
436 set_reg_bandselpll(db);
438 bool ok = lock_detect();
440 printf("Fail lock detect %uKHz\n", u2_fxpt_freq_round_to_int(freq/1000));
445 /**************************************************
447 **************************************************/
449 xcvr2450_set_gain_rx(struct db_base *dbb, u2_fxpt_gain_t gain){
450 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
451 //ensure gain is within range
452 if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
455 // Split the gain between RF and baseband
456 // This is experimental, not prescribed
457 if(gain < U2_DOUBLE_TO_FXPT_GAIN(30.0)) {
458 db->common->d_rx_rf_gain = 0; // 0 dB RF gain
459 db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int(gain/2);
461 else if(gain >= U2_DOUBLE_TO_FXPT_GAIN(30.0) && gain < U2_DOUBLE_TO_FXPT_GAIN(61.0)) {
462 db->common->d_rx_rf_gain = 2; // 15 dB RF gain
463 db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int((gain-U2_DOUBLE_TO_FXPT_GAIN(15.0))/2);
465 else if(gain >= U2_DOUBLE_TO_FXPT_GAIN(61.0)) {
466 db->common->d_rx_rf_gain = 3; // 30.5 dB RF gain
467 db->common->d_rx_bb_gain = u2_fxpt_gain_round_to_int((gain-U2_DOUBLE_TO_FXPT_GAIN(30.5))/2);
469 //printf("RX RF Gain %u, RX BB Gain %u\n", db->common->d_rx_rf_gain, db->common->d_rx_bb_gain);
474 /**************************************************
476 **************************************************/
478 xcvr2450_set_gain_tx(struct db_base *dbb, u2_fxpt_gain_t gain){
479 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
480 //ensure gain in within range
481 if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
484 //scale for register and set
485 db->common->d_txgain = (gain*63)/db->base.gain_max;
486 //printf("TX Gain %u, TX Reg %u\n", u2_fxpt_gain_round_to_int(gain), db->common->d_txgain);
491 /**************************************************
493 **************************************************/
495 xcvr2450_set_tx_enable(struct db_base *dbb, bool on){
496 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
497 db->common->d_tx_enb = on;
502 /**************************************************
504 **************************************************/
505 bool xcvr2450_set_rx_antenna(struct db_base *dbb, int ant){
506 printf("xcvr set rx ant %d\n", ant);
507 if (ant > 1) return false;
508 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
509 db->common->d_rx_ant = ant;
514 bool xcvr2450_set_tx_antenna(struct db_base *dbb, int ant){
515 printf("xcvr set tx ant %d\n", ant);
516 if (ant > 1) return false;
517 struct db_xcvr2450_dummy *db = (struct db_xcvr2450_dummy *) dbb;
518 db->common->d_tx_ant = ant;