"hdlmake/dep_solver.py" did not exist on "834096414ac7c9923fc8b4c3cab3c8f486af56f1"
Newer
Older
/*
* This work is part of the White Rabbit project
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* Released according to the GNU GPL, version 2 or any later version.
*/
#include <stdio.h>
#include <inttypes.h>
#include <stdarg.h>
#include <wrc.h>
#include "board.h"
#include "trace.h"
#include "syscon.h"
#include "endpoint.h"
#include "softpll_ng.h"
Grzegorz Daniluk
committed
#include "wrc_ptp.h"
#include "eeprom.h"
Grzegorz Daniluk
committed
#include "ptpd_netif.h"
/* New calibrator for the transition phase value. A major pain in the ass for
the folks who frequently rebuild their gatewares. The idea is described
below:
- lock the PLL to the master
- scan the whole phase shifter range
- at each scanning step, generate a fake RX timestamp.
- check if the rising edge counter is ahead of the falling edge counter
(added a special bit for it in the TSU).
- determine phases at which positive/negative transitions occur
- transition phase value is in the middle between the rising and falling
edges.
This calibration procedure is fast enough to be run on slave nodes whenever
the link goes up. For master mode, the core must be run at least once as a
slave to calibrate itself and store the current transition phase value in
the EEPROM.
*/
/* how finely we scan the phase shift range to determine where we have the bit
* flip */
#define CAL_SCAN_STEP 100
/* deglitcher threshold (to remove 1->0->1 flip bit glitches that might occur
due to jitter) */
#define CAL_DEGLITCH_THRESHOLD 5
/* we scan at least one clock period to look for rising->falling edge transition
plus some headroom */
#define CAL_SCAN_RANGE (REF_CLOCK_PERIOD_PS + \
(3 * CAL_DEGLITCH_THRESHOLD * CAL_SCAN_STEP))
#define TD_WAIT_INACTIVE 0
#define TD_GOT_TRANSITION 1
#define TD_DONE 2
/* state of transition detector */
struct trans_detect_state {
int prev_val;
int sample_count;
int state;
int trans_phase;
};
/* finds the transition in the value of flip_bit and returns phase associated
with it. If no transition phase has been found yet, returns 0. Non-zero
polarity means we are looking for positive transitions, 0 - negative
transitions */
static int lookup_transition(struct trans_detect_state *state, int flip_bit,
int phase, int polarity)
{
if (polarity)
polarity = 1;
switch (state->state) {
case TD_WAIT_INACTIVE:
/* first, wait until we have at least CAL_DEGLITCH_THRESHOLD of
inactive state samples */
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
if (flip_bit != polarity)
state->sample_count++;
else
state->sample_count = 0;
if (state->sample_count >= CAL_DEGLITCH_THRESHOLD) {
state->state = TD_GOT_TRANSITION;
state->sample_count = 0;
}
break;
case TD_GOT_TRANSITION:
if (flip_bit != polarity)
state->sample_count = 0;
else {
state->sample_count++;
if (state->sample_count >= CAL_DEGLITCH_THRESHOLD) {
state->state = TD_DONE;
state->trans_phase =
phase -
CAL_DEGLITCH_THRESHOLD * CAL_SCAN_STEP;
}
}
break;
case TD_DONE:
return 1;
break;
}
return 0;
}
static struct trans_detect_state det_rising, det_falling;
static int cal_cur_phase;
/* Starts RX timestamper calibration process state machine. Invoked by
ptpnetif's check lock function when the PLL has already locked, to avoid
complicating the API of ptp-noposix/ppsi. */
void rxts_calibration_start()
{
cal_cur_phase = 0;
det_rising.prev_val = det_falling.prev_val = -1;
det_rising.state = det_falling.state = TD_WAIT_INACTIVE;
spll_set_phase_shift(0, 0);
}
/* Updates RX timestamper state machine. Non-zero return value means that
calibration is done. */
int rxts_calibration_update(uint32_t *t24p_value)
{
if (spll_shifter_busy(0))
return 0;
/* generate a fake RX timestamp and check if falling edge counter is
ahead of rising edge counter */
int flip = ep_timestamper_cal_pulse();
/* look for transitions (with deglitching) */
lookup_transition(&det_rising, flip, cal_cur_phase, 1);
lookup_transition(&det_falling, flip, cal_cur_phase, 0);
if (cal_cur_phase >= CAL_SCAN_RANGE) {
if (det_rising.state != TD_DONE || det_falling.state != TD_DONE)
{
TRACE_DEV("RXTS calibration error.\n");
return -1;
}
/* normalize */
Wesley W. Terpstra
committed
while (det_falling.trans_phase >= REF_CLOCK_PERIOD_PS)
det_falling.trans_phase -= REF_CLOCK_PERIOD_PS;
while (det_rising.trans_phase >= REF_CLOCK_PERIOD_PS)
det_rising.trans_phase -= REF_CLOCK_PERIOD_PS;
/* Use falling edge as second sample of rising edge */
uint32_t ttrans = det_falling.trans_phase;
ttrans += REF_CLOCK_PERIOD_PS/2;
if (ttrans >= REF_CLOCK_PERIOD_PS)
ttrans -= REF_CLOCK_PERIOD_PS;
Wesley W. Terpstra
committed
ttrans += det_rising.trans_phase;
ttrans /= 2;
TRACE_DEV("RXTS calibration: R@%dps, F@%dps, transition@%dps\n",
det_rising.trans_phase, det_falling.trans_phase,
ttrans);
Wesley W. Terpstra
committed
*t24p_value = ttrans;
return 1;
}
cal_cur_phase += CAL_SCAN_STEP;
spll_set_phase_shift(0, cal_cur_phase);
return 0;
}
/* legacy function for 'calibration force' command */
int measure_t24p(uint32_t *value)
{
int rv;
pp_printf("Waiting for link...\n");
while (!ep_link_up(NULL))
timer_delay(100);
spll_init(SPLL_MODE_SLAVE, 0, 1);
while (!spll_check_lock(0))
timer_delay(100);
pp_printf("Calibrating RX timestamper...\n");
rxts_calibration_start();
while (!(rv = rxts_calibration_update(value))) ;
return rv;
}
Grzegorz Daniluk
committed
/*SoftPLL must be locked prior calling this function*/
static int calib_t24p_slave(uint32_t *value)
Grzegorz Daniluk
committed
{
int rv;
rxts_calibration_start();
while (!(rv = rxts_calibration_update(value))) ;
if (rv < 0) {
pp_printf("Could not calibrate t24p, trying to read from EEPROM\n");
Grzegorz Daniluk
committed
if(eeprom_phtrans(WRPC_FMC_I2C, FMC_EEPROM_ADR, value, 0) < 0) {
pp_printf("Something went wrong while writing EEPROM\n");
Grzegorz Daniluk
committed
return -1;
}
}
else {
pp_printf("t24p value is %d ps, storing to EEPROM\n", *value);
Grzegorz Daniluk
committed
if(eeprom_phtrans(WRPC_FMC_I2C, FMC_EEPROM_ADR, value, 1) < 0) {
pp_printf("Something went wrong while writing EEPROM\n");
Grzegorz Daniluk
committed
return -1;
}
}
return 0;
}
static int calib_t24p_master(uint32_t *value)
Grzegorz Daniluk
committed
{
int rv;
rv = eeprom_phtrans(WRPC_FMC_I2C, FMC_EEPROM_ADR, value, 0);
pp_printf("Something went wrong while reading from EEPROM: %d\n", rv);
pp_printf("t24p read from EEPROM: %d ps\n", *value);
Grzegorz Daniluk
committed
return rv;
}
int calib_t24p(int mode, uint32_t *value)
Grzegorz Daniluk
committed
{
Grzegorz Daniluk
committed
int ret;
Grzegorz Daniluk
committed
if (mode == WRC_MODE_SLAVE)
Grzegorz Daniluk
committed
ret = calib_t24p_slave(value);
Grzegorz Daniluk
committed
else
Grzegorz Daniluk
committed
ret = calib_t24p_master(value);
//update phtrans value in socket struct
ptpd_netif_set_phase_transition(*value);
return ret;