3 * Copyright 2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
23 #include "mimo_app_common_v2.h"
24 #include "buffer_pool.h"
25 #include "memcpy_wa.h"
28 #include "print_rmon_regs.h"
35 volatile bool link_is_up = false; // eth handler sets this
36 int cpu_tx_buf_dest_port = PORT_ETH;
38 // If this is non-zero, this dbsm could be writing to the ethernet
39 dbsm_t *ac_could_be_sending_to_eth;
41 static unsigned char exp_seqno __attribute__((unused)) = 0;
46 burn_mac_addr(const op_burn_mac_addr_t *p)
48 return ethernet_set_mac_addr(&p->addr);
52 sync_to_pps(const op_generic_t *p)
54 timesync_regs->sync_on_next_pps = 1;
55 putstr("SYNC to PPS\n");
60 config_mimo_cmd(const op_config_mimo_t *p)
62 clocks_mimo_config(p->flags);
67 set_reply_hdr(u2_eth_packet_t *reply_pkt, u2_eth_packet_t const *cmd_pkt)
69 reply_pkt->ehdr.dst = cmd_pkt->ehdr.src;
70 reply_pkt->ehdr.src = *ethernet_mac_addr();
71 reply_pkt->ehdr.ethertype = U2_ETHERTYPE;
72 reply_pkt->thdr.flags = 0;
73 reply_pkt->thdr.fifo_status = 0; // written by protocol engine
74 reply_pkt->thdr.seqno = 0; // written by protocol engine
75 reply_pkt->thdr.ack = 0; // written by protocol engine
76 u2p_set_word0(&reply_pkt->fixed, 0, CONTROL_CHAN);
77 reply_pkt->fixed.timestamp = timer_regs->time;
81 send_reply(unsigned char *reply, size_t reply_len)
86 // wait for buffer to become idle
87 hal_set_leds(0x4, 0x4);
88 while((buffer_pool_status->status & BPS_IDLE(CPU_TX_BUF)) == 0)
90 hal_set_leds(0x0, 0x4);
92 // copy reply into CPU_TX_BUF
93 memcpy_wa(buffer_ram(CPU_TX_BUF), reply, reply_len);
95 // wait until nobody else is sending to the ethernet
96 if (ac_could_be_sending_to_eth){
97 hal_set_leds(0x8, 0x8);
98 dbsm_wait_for_opening(ac_could_be_sending_to_eth);
99 hal_set_leds(0x0, 0x8);
103 printf("sending_reply to port %d, len = %d\n", cpu_tx_buf_dest_port, (int)reply_len);
104 print_buffer(buffer_ram(CPU_TX_BUF), reply_len/4);
108 bp_send_from_buf(CPU_TX_BUF, cpu_tx_buf_dest_port, 1, 0, reply_len/4);
110 // wait for it to complete (not long, it's a small pkt)
111 while((buffer_pool_status->status & (BPS_DONE(CPU_TX_BUF) | BPS_ERROR(CPU_TX_BUF))) == 0)
114 bp_clear_buf(CPU_TX_BUF);
119 op_id_cmd(const op_generic_t *p,
120 void *reply_payload, size_t reply_payload_space)
122 op_id_reply_t *r = (op_id_reply_t *) reply_payload;
123 if (reply_payload_space < sizeof(*r)) // no room
126 // Build reply subpacket
128 r->opcode = OP_ID_REPLY;
129 r->len = sizeof(op_id_reply_t);
131 r->addr = *ethernet_mac_addr();
132 r->hw_rev = (u2_hw_rev_major << 8) | u2_hw_rev_minor;
133 // r->fpga_md5sum = ; // FIXME
134 // r->sw_md5sum = ; // FIXME
141 config_tx_v2_cmd(const op_config_tx_v2_t *p,
142 void *reply_payload, size_t reply_payload_space)
144 op_config_tx_reply_v2_t *r = (op_config_tx_reply_v2_t *) reply_payload;
145 if (reply_payload_space < sizeof(*r))
148 struct tune_result tune_result;
149 memset(&tune_result, 0, sizeof(tune_result));
154 if (p->valid & CFGV_GAIN){
155 ok &= db_set_gain(tx_dboard, p->gain);
158 if (p->valid & CFGV_FREQ){
159 bool was_streaming = is_streaming();
163 u2_fxpt_freq_t f = u2_fxpt_freq_from_hilo(p->freq_hi, p->freq_lo);
164 bool tune_ok = db_tune(tx_dboard, f, &tune_result);
166 print_tune_result("Tx", tune_ok, f, &tune_result);
172 if (p->valid & CFGV_INTERP_DECIM){
173 int interp = p->interp;
179 interp = interp >> 1;
184 interp = interp >> 1;
187 if (interp < MIN_CIC_INTERP || interp > MAX_CIC_INTERP)
190 dsp_tx_regs->interp_rate = (hb1<<9) | (hb2<<8) | interp;
191 // printf("Interp: %d, register %d\n", p->interp, (hb1<<9) | (hb2<<8) | interp);
195 if (p->valid & CFGV_SCALE_IQ){
196 dsp_tx_regs->scale_iq = p->scale_iq;
200 // Build reply subpacket
202 r->opcode = OP_CONFIG_TX_REPLY_V2;
206 r->inverted = tune_result.inverted;
207 r->baseband_freq_hi = u2_fxpt_freq_hi(tune_result.baseband_freq);
208 r->baseband_freq_lo = u2_fxpt_freq_lo(tune_result.baseband_freq);
209 r->duc_freq_hi = u2_fxpt_freq_hi(tune_result.dxc_freq);
210 r->duc_freq_lo = u2_fxpt_freq_lo(tune_result.dxc_freq);
211 r->residual_freq_hi = u2_fxpt_freq_hi(tune_result.residual_freq);
212 r->residual_freq_lo = u2_fxpt_freq_lo(tune_result.residual_freq);
217 config_rx_v2_cmd(const op_config_rx_v2_t *p,
218 void *reply_payload, size_t reply_payload_space)
220 op_config_rx_reply_v2_t *r = (op_config_rx_reply_v2_t *) reply_payload;
221 if (reply_payload_space < sizeof(*r))
224 struct tune_result tune_result;
225 memset(&tune_result, 0, sizeof(tune_result));
229 if (p->valid & CFGV_GAIN){
230 ok &= db_set_gain(rx_dboard, p->gain);
233 if (p->valid & CFGV_FREQ){
234 bool was_streaming = is_streaming();
238 u2_fxpt_freq_t f = u2_fxpt_freq_from_hilo(p->freq_hi, p->freq_lo);
239 bool tune_ok = db_tune(rx_dboard, f, &tune_result);
241 print_tune_result("Rx", tune_ok, f, &tune_result);
247 if (p->valid & CFGV_INTERP_DECIM){
248 int decim = p->decim;
262 if (decim < MIN_CIC_DECIM || decim > MAX_CIC_DECIM)
265 dsp_rx_regs->decim_rate = (hb1<<9) | (hb2<<8) | decim;
266 // printf("Decim: %d, register %d\n", p->decim, (hb1<<9) | (hb2<<8) | decim);
270 if (p->valid & CFGV_SCALE_IQ){
271 dsp_rx_regs->scale_iq = p->scale_iq;
274 // Build reply subpacket
276 r->opcode = OP_CONFIG_RX_REPLY_V2;
280 r->inverted = tune_result.inverted;
281 r->baseband_freq_hi = u2_fxpt_freq_hi(tune_result.baseband_freq);
282 r->baseband_freq_lo = u2_fxpt_freq_lo(tune_result.baseband_freq);
283 r->ddc_freq_hi = u2_fxpt_freq_hi(tune_result.dxc_freq);
284 r->ddc_freq_lo = u2_fxpt_freq_lo(tune_result.dxc_freq);
285 r->residual_freq_hi = u2_fxpt_freq_hi(tune_result.residual_freq);
286 r->residual_freq_lo = u2_fxpt_freq_lo(tune_result.residual_freq);
292 read_time_cmd(const op_generic_t *p,
293 void *reply_payload, size_t reply_payload_space)
295 op_read_time_reply_t *r = (op_read_time_reply_t *) reply_payload;
296 if (reply_payload_space < sizeof(*r))
299 r->opcode = OP_READ_TIME_REPLY;
302 r->time = timer_regs->time;
308 fill_db_info(u2_db_info_t *p, const struct db_base *db)
311 p->freq_min_hi = u2_fxpt_freq_hi(db->freq_min);
312 p->freq_min_lo = u2_fxpt_freq_lo(db->freq_min);
313 p->freq_max_hi = u2_fxpt_freq_hi(db->freq_max);
314 p->freq_max_lo = u2_fxpt_freq_lo(db->freq_max);
315 p->gain_min = db->gain_min;
316 p->gain_max = db->gain_max;
317 p->gain_step_size = db->gain_step_size;
321 dboard_info_cmd(const op_generic_t *p,
322 void *reply_payload, size_t reply_payload_space)
324 op_dboard_info_reply_t *r = (op_dboard_info_reply_t *) reply_payload;
325 if (reply_payload_space < sizeof(*r))
328 r->opcode = OP_DBOARD_INFO_REPLY;
333 fill_db_info(&r->tx_db_info, tx_dboard);
334 fill_db_info(&r->rx_db_info, rx_dboard);
340 peek_cmd(const op_peek_t *p,
341 void *reply_payload, size_t reply_payload_space)
343 op_generic_t *r = (op_generic_t *) reply_payload;
345 putstr("peek: addr="); puthex32(p->addr);
346 printf(" bytes=%u\n", p->bytes);
348 if ((reply_payload_space < (sizeof(*r) + p->bytes)) ||
349 p->bytes > MAX_SUBPKT_LEN - sizeof(op_generic_t)) {
350 putstr("peek: insufficient reply packet space\n");
351 return 0; // FIXME do partial read?
354 r->opcode = OP_PEEK_REPLY;
355 r->len = sizeof(*r)+p->bytes;
359 memcpy_wa(reply_payload+sizeof(*r), (void *)p->addr, p->bytes);
365 poke_cmd(const op_poke_t *p)
367 int bytes = p->len - sizeof(*p);
368 putstr("poke: addr="); puthex32(p->addr);
369 printf(" bytes=%u\n", bytes);
371 uint8_t *src = (uint8_t *)p + sizeof(*p);
372 memcpy_wa((void *)p->addr, src, bytes);
378 generic_reply(const op_generic_t *p,
379 void *reply_payload, size_t reply_payload_space,
382 op_generic_t *r = (op_generic_t *) reply_payload;
383 if (reply_payload_space < sizeof(*r))
386 r->opcode = p->opcode | OP_REPLY_BIT;
395 add_eop(void *reply_payload, size_t reply_payload_space)
397 op_generic_t *r = (op_generic_t *) reply_payload;
398 if (reply_payload_space < sizeof(*r))
410 handle_control_chan_frame(u2_eth_packet_t *pkt, size_t len)
412 unsigned char reply[sizeof(u2_eth_packet_t) + 4 * sizeof(u2_subpkt_t)] _AL4;
413 unsigned char *reply_payload = &reply[sizeof(u2_eth_packet_t)];
414 int reply_payload_space = sizeof(reply) - sizeof(u2_eth_packet_t);
417 memset(reply, 0, sizeof(reply));
418 set_reply_hdr((u2_eth_packet_t *) reply, pkt);
420 // point to beginning of payload (subpackets)
421 unsigned char *payload = ((unsigned char *) pkt) + sizeof(u2_eth_packet_t);
422 int payload_len = len - sizeof(u2_eth_packet_t);
424 size_t subpktlen = 0;
426 while (payload_len >= sizeof(op_generic_t)){
427 const op_generic_t *gp = (const op_generic_t *) payload;
430 // printf("\nopcode = %d\n", gp->opcode);
433 case OP_EOP: // end of subpackets
434 goto end_of_subpackets;
437 subpktlen = op_id_cmd(gp, reply_payload, reply_payload_space);
440 case OP_CONFIG_TX_V2:
441 subpktlen = config_tx_v2_cmd((op_config_tx_v2_t *) payload,
442 reply_payload, reply_payload_space);
445 case OP_CONFIG_RX_V2:
446 subpktlen = config_rx_v2_cmd((op_config_rx_v2_t *) payload,
447 reply_payload, reply_payload_space);
450 case OP_START_RX_STREAMING:
451 start_rx_streaming_cmd(&pkt->ehdr.src, (op_start_rx_streaming_t *) payload);
452 subpktlen = generic_reply(gp, reply_payload, reply_payload_space, true);
457 subpktlen = generic_reply(gp, reply_payload, reply_payload_space, true);
460 case OP_BURN_MAC_ADDR:
461 subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
462 burn_mac_addr((op_burn_mac_addr_t *) payload));
466 subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
467 config_mimo_cmd((op_config_mimo_t *) payload));
471 subpktlen = read_time_cmd(gp, reply_payload, reply_payload_space);
475 subpktlen = dboard_info_cmd(gp, reply_payload, reply_payload_space);
479 subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
480 sync_to_pps((op_generic_t *) payload));
484 subpktlen = peek_cmd((op_peek_t *)payload, reply_payload, reply_payload_space);
488 subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
489 poke_cmd((op_poke_t *)payload));
493 printf("app_common_v2: unhandled opcode = %d\n", gp->opcode);
497 int t = (gp->len + 3) & ~3; // bump to a multiple of 4
501 subpktlen = (subpktlen + 3) & ~3; // bump to a multiple of 4
502 reply_payload += subpktlen;
503 reply_payload_space -= subpktlen;
508 // add the EOP marker
509 subpktlen = add_eop(reply_payload, reply_payload_space);
510 subpktlen = (subpktlen + 3) & ~3; // bump to a multiple of 4
511 reply_payload += subpktlen;
512 reply_payload_space -= subpktlen;
514 send_reply(reply, reply_payload - reply);
519 * Called when an ethernet packet is received.
520 * Return true if we handled it here, otherwise
521 * it'll be passed on to the DSP Tx pipe
524 eth_pkt_inspector(bsm12_t *sm, int bufno)
526 u2_eth_packet_t *pkt = (u2_eth_packet_t *) buffer_ram(bufno);
527 size_t byte_len = (buffer_pool_status->last_line[bufno] - 3) * 4;
529 //static size_t last_len = 0;
531 // hal_toggle_leds(0x1);
533 // inspect rcvd frame and figure out what do do.
535 if (pkt->ehdr.ethertype != U2_ETHERTYPE)
536 return true; // ignore, probably bogus PAUSE frame from MAC
538 int chan = u2p_chan(&pkt->fixed);
542 handle_control_chan_frame(pkt, byte_len);
547 return 0; // pass it off to DSP TX
550 return 1; // pass it off to SERDES TX
559 * Called when eth phy state changes (w/ interrupts disabled)
562 link_changed_callback(int speed)
564 link_is_up = speed != 0;
565 hal_set_leds(link_is_up ? LED_RJ45 : 0x0, LED_RJ45);
566 printf("\neth link changed: speed = %d\n", speed);
571 print_tune_result(char *msg, bool tune_ok,
572 u2_fxpt_freq_t target_freq, struct tune_result *r)
575 printf("db_tune %s %s\n", msg, tune_ok ? "true" : "false");
576 putstr(" target_freq "); print_fxpt_freq(target_freq); newline();
577 putstr(" baseband_freq "); print_fxpt_freq(r->baseband_freq); newline();
578 putstr(" dxc_freq "); print_fxpt_freq(r->dxc_freq); newline();
579 putstr(" residual_freq "); print_fxpt_freq(r->residual_freq); newline();
580 printf(" inverted %s\n", r->inverted ? "true" : "false");