2017-08-22 00:21:14 +08:00
|
|
|
from migen import *
|
|
|
|
from migen.genlib.cdc import MultiReg, PulseSynchronizer
|
|
|
|
from migen.genlib.misc import WaitTimer
|
|
|
|
|
|
|
|
from misoc.interconnect.csr import *
|
|
|
|
|
2017-08-30 20:31:44 +08:00
|
|
|
from artiq.gateware.serwb.kusphy import KUSSerdes
|
|
|
|
from artiq.gateware.serwb.s7phy import S7Serdes
|
|
|
|
|
2017-08-22 00:21:14 +08:00
|
|
|
|
|
|
|
# Master <--> Slave synchronization:
|
|
|
|
# 1) Master sends idle pattern (zeroes) to reset Slave.
|
|
|
|
# 2) Master sends K28.5 commas to allow Slave to calibrate, Slave sends idle pattern.
|
|
|
|
# 3) Slave sends K28.5 commas to allow Master to calibrate, Master sends K28.5 commas.
|
|
|
|
# 4) Master stops sending K28.5 commas.
|
|
|
|
# 5) Slave stops sending K25.5 commas.
|
|
|
|
# 6) Link is ready.
|
|
|
|
|
2017-08-30 20:31:44 +08:00
|
|
|
class _SerdesMasterInit(Module):
|
2017-08-30 23:59:10 +08:00
|
|
|
def __init__(self, serdes, taps, timeout=1024):
|
2017-08-22 00:21:14 +08:00
|
|
|
self.reset = Signal()
|
|
|
|
self.ready = Signal()
|
2017-08-30 20:31:44 +08:00
|
|
|
self.error = Signal()
|
2017-08-22 00:21:14 +08:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
self.delay = delay = Signal(max=taps)
|
2017-08-29 19:38:52 +08:00
|
|
|
self.delay_min = delay_min = Signal(max=taps)
|
|
|
|
self.delay_min_found = delay_min_found = Signal()
|
|
|
|
self.delay_max = delay_max = Signal(max=taps)
|
|
|
|
self.delay_max_found = delay_max_found = Signal()
|
2017-08-22 00:21:14 +08:00
|
|
|
self.bitslip = bitslip = Signal(max=40)
|
|
|
|
|
2017-08-30 23:59:10 +08:00
|
|
|
timer = WaitTimer(timeout)
|
2017-08-22 00:21:14 +08:00
|
|
|
self.submodules += timer
|
|
|
|
|
|
|
|
self.submodules.fsm = fsm = ResetInserter()(FSM(reset_state="IDLE"))
|
|
|
|
self.comb += self.fsm.reset.eq(self.reset)
|
|
|
|
|
|
|
|
fsm.act("IDLE",
|
|
|
|
NextValue(delay, 0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(delay_min, 0),
|
|
|
|
NextValue(delay_min_found, 0),
|
|
|
|
NextValue(delay_max, 0),
|
|
|
|
NextValue(delay_max_found, 0),
|
2017-08-22 00:21:14 +08:00
|
|
|
serdes.rx_delay_rst.eq(1),
|
|
|
|
NextValue(bitslip, 0),
|
|
|
|
NextState("RESET_SLAVE"),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("RESET_SLAVE",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
|
|
|
NextState("SEND_PATTERN")
|
|
|
|
),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_PATTERN",
|
|
|
|
If(~serdes.rx_idle,
|
|
|
|
NextState("WAIT_STABLE")
|
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_STABLE",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextState("CHECK_PATTERN")
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("CHECK_PATTERN",
|
|
|
|
If(~delay_min_found,
|
|
|
|
If(serdes.rx_comma,
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
2017-08-30 20:31:44 +08:00
|
|
|
timer.wait.eq(0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(delay_min, delay),
|
|
|
|
NextValue(delay_min_found, 1)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
NextState("INC_DELAY_BITSLIP")
|
|
|
|
),
|
|
|
|
).Else(
|
|
|
|
If(~serdes.rx_comma,
|
|
|
|
NextValue(delay_max, delay),
|
|
|
|
NextValue(delay_max_found, 1),
|
2017-08-30 20:31:44 +08:00
|
|
|
NextState("CHECK_SAMPLING_WINDOW")
|
2017-08-29 19:38:52 +08:00
|
|
|
).Else(
|
|
|
|
NextState("INC_DELAY_BITSLIP")
|
|
|
|
)
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
self.comb += serdes.rx_bitslip_value.eq(bitslip)
|
|
|
|
fsm.act("INC_DELAY_BITSLIP",
|
|
|
|
NextState("WAIT_STABLE"),
|
2017-08-22 00:21:14 +08:00
|
|
|
If(delay == (taps - 1),
|
2017-08-29 19:38:52 +08:00
|
|
|
If(bitslip == (40 - 1),
|
2017-08-30 20:31:44 +08:00
|
|
|
NextState("ERROR")
|
|
|
|
).Else(
|
|
|
|
NextValue(delay_min_found, 0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(bitslip, bitslip + 1)
|
|
|
|
),
|
|
|
|
NextValue(delay, 0),
|
|
|
|
serdes.rx_delay_rst.eq(1)
|
2017-08-22 00:21:14 +08:00
|
|
|
).Else(
|
|
|
|
NextValue(delay, delay + 1),
|
|
|
|
serdes.rx_delay_inc.eq(1),
|
2017-08-29 19:38:52 +08:00
|
|
|
serdes.rx_delay_ce.eq(1)
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
2017-08-30 20:31:44 +08:00
|
|
|
fsm.act("CHECK_SAMPLING_WINDOW",
|
|
|
|
If((delay_min == 0) |
|
|
|
|
(delay_max == (taps - 1)) |
|
|
|
|
((delay_max - delay_min) < taps//16),
|
|
|
|
NextValue(delay_min_found, 0),
|
|
|
|
NextValue(delay_max_found, 0),
|
|
|
|
NextState("WAIT_STABLE")
|
|
|
|
).Else(
|
|
|
|
NextState("RESET_SAMPLING_WINDOW")
|
|
|
|
)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("RESET_SAMPLING_WINDOW",
|
|
|
|
NextValue(delay, 0),
|
|
|
|
serdes.rx_delay_rst.eq(1),
|
|
|
|
NextState("WAIT_SAMPLING_WINDOW"),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("CONFIGURE_SAMPLING_WINDOW",
|
|
|
|
If(delay == (delay_min + (delay_max - delay_min)[1:]),
|
|
|
|
NextState("READY")
|
2017-08-22 00:21:14 +08:00
|
|
|
).Else(
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(delay, delay + 1),
|
|
|
|
serdes.rx_delay_inc.eq(1),
|
|
|
|
serdes.rx_delay_ce.eq(1),
|
|
|
|
NextState("WAIT_SAMPLING_WINDOW")
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("WAIT_SAMPLING_WINDOW",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
|
|
|
NextState("CONFIGURE_SAMPLING_WINDOW")
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("READY",
|
|
|
|
self.ready.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("ERROR",
|
|
|
|
self.error.eq(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-08-30 20:31:44 +08:00
|
|
|
class _SerdesSlaveInit(Module, AutoCSR):
|
2017-08-30 23:59:10 +08:00
|
|
|
def __init__(self, serdes, taps, timeout=1024):
|
2017-08-22 00:21:14 +08:00
|
|
|
self.reset = Signal()
|
|
|
|
self.ready = Signal()
|
|
|
|
self.error = Signal()
|
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
self.delay = delay = Signal(max=taps)
|
2017-08-29 19:38:52 +08:00
|
|
|
self.delay_min = delay_min = Signal(max=taps)
|
|
|
|
self.delay_min_found = delay_min_found = Signal()
|
|
|
|
self.delay_max = delay_max = Signal(max=taps)
|
|
|
|
self.delay_max_found = delay_max_found = Signal()
|
2017-08-22 00:21:14 +08:00
|
|
|
self.bitslip = bitslip = Signal(max=40)
|
|
|
|
|
2017-08-30 23:59:10 +08:00
|
|
|
timer = WaitTimer(timeout)
|
2017-08-22 00:21:14 +08:00
|
|
|
self.submodules += timer
|
|
|
|
|
|
|
|
self.comb += self.reset.eq(serdes.rx_idle)
|
|
|
|
|
|
|
|
self.submodules.fsm = fsm = ResetInserter()(FSM(reset_state="IDLE"))
|
|
|
|
fsm.act("IDLE",
|
|
|
|
NextValue(delay, 0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(delay_min, 0),
|
|
|
|
NextValue(delay_min_found, 0),
|
|
|
|
NextValue(delay_max, 0),
|
|
|
|
NextValue(delay_max_found, 0),
|
2017-08-22 00:21:14 +08:00
|
|
|
serdes.rx_delay_rst.eq(1),
|
|
|
|
NextValue(bitslip, 0),
|
|
|
|
NextState("WAIT_STABLE"),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_STABLE",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextState("CHECK_PATTERN")
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("CHECK_PATTERN",
|
|
|
|
If(~delay_min_found,
|
|
|
|
If(serdes.rx_comma,
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
|
|
|
NextValue(delay_min, delay),
|
|
|
|
NextValue(delay_min_found, 1)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
NextState("INC_DELAY_BITSLIP")
|
|
|
|
),
|
|
|
|
).Else(
|
|
|
|
If(~serdes.rx_comma,
|
|
|
|
NextValue(delay_max, delay),
|
|
|
|
NextValue(delay_max_found, 1),
|
2017-08-30 20:31:44 +08:00
|
|
|
NextState("CHECK_SAMPLING_WINDOW")
|
2017-08-29 19:38:52 +08:00
|
|
|
).Else(
|
|
|
|
NextState("INC_DELAY_BITSLIP")
|
|
|
|
)
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
self.comb += serdes.rx_bitslip_value.eq(bitslip)
|
|
|
|
fsm.act("INC_DELAY_BITSLIP",
|
|
|
|
NextState("WAIT_STABLE"),
|
2017-08-22 00:21:14 +08:00
|
|
|
If(delay == (taps - 1),
|
2017-08-29 19:38:52 +08:00
|
|
|
If(bitslip == (40 - 1),
|
2017-08-30 20:31:44 +08:00
|
|
|
NextState("ERROR")
|
|
|
|
).Else(
|
|
|
|
NextValue(delay_min_found, 0),
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(bitslip, bitslip + 1)
|
|
|
|
),
|
|
|
|
NextValue(delay, 0),
|
|
|
|
serdes.rx_delay_rst.eq(1)
|
2017-08-22 00:21:14 +08:00
|
|
|
).Else(
|
|
|
|
NextValue(delay, delay + 1),
|
|
|
|
serdes.rx_delay_inc.eq(1),
|
2017-08-29 19:38:52 +08:00
|
|
|
serdes.rx_delay_ce.eq(1)
|
2017-08-22 00:21:14 +08:00
|
|
|
),
|
|
|
|
serdes.tx_idle.eq(1)
|
|
|
|
)
|
2017-08-30 20:31:44 +08:00
|
|
|
fsm.act("CHECK_SAMPLING_WINDOW",
|
|
|
|
If((delay_min == 0) |
|
|
|
|
(delay_max == (taps - 1)) |
|
|
|
|
((delay_max - delay_min) < taps//16),
|
|
|
|
NextValue(delay_min_found, 0),
|
|
|
|
NextValue(delay_max_found, 0),
|
|
|
|
NextState("WAIT_STABLE")
|
|
|
|
).Else(
|
|
|
|
NextState("RESET_SAMPLING_WINDOW")
|
|
|
|
)
|
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("RESET_SAMPLING_WINDOW",
|
|
|
|
NextValue(delay, 0),
|
|
|
|
serdes.rx_delay_rst.eq(1),
|
|
|
|
NextState("WAIT_SAMPLING_WINDOW")
|
2017-08-22 00:21:14 +08:00
|
|
|
)
|
2017-08-29 19:38:52 +08:00
|
|
|
fsm.act("CONFIGURE_SAMPLING_WINDOW",
|
|
|
|
If(delay == (delay_min + (delay_max - delay_min)[1:]),
|
|
|
|
NextState("SEND_PATTERN")
|
2017-08-22 00:21:14 +08:00
|
|
|
).Else(
|
2017-08-29 19:38:52 +08:00
|
|
|
NextValue(delay, delay + 1),
|
|
|
|
serdes.rx_delay_inc.eq(1),
|
|
|
|
serdes.rx_delay_ce.eq(1),
|
|
|
|
NextState("WAIT_SAMPLING_WINDOW")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_SAMPLING_WINDOW",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
timer.wait.eq(0),
|
|
|
|
NextState("CONFIGURE_SAMPLING_WINDOW")
|
|
|
|
)
|
2017-08-22 00:21:14 +08:00
|
|
|
)
|
|
|
|
fsm.act("SEND_PATTERN",
|
|
|
|
timer.wait.eq(1),
|
|
|
|
If(timer.done,
|
|
|
|
If(~serdes.rx_comma,
|
|
|
|
NextState("READY")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
serdes.tx_comma.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("READY",
|
|
|
|
self.ready.eq(1)
|
|
|
|
)
|
|
|
|
fsm.act("ERROR",
|
|
|
|
self.error.eq(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-08-30 20:31:44 +08:00
|
|
|
class _SerdesControl(Module, AutoCSR):
|
2017-08-22 00:21:14 +08:00
|
|
|
def __init__(self, init, mode="master"):
|
|
|
|
if mode == "master":
|
|
|
|
self.reset = CSR()
|
|
|
|
self.ready = CSRStatus()
|
|
|
|
self.error = CSRStatus()
|
|
|
|
|
|
|
|
self.delay = CSRStatus(9)
|
2017-08-29 19:38:52 +08:00
|
|
|
self.delay_min_found = CSRStatus()
|
|
|
|
self.delay_min = CSRStatus(9)
|
|
|
|
self.delay_max_found = CSRStatus()
|
|
|
|
self.delay_max = CSRStatus(9)
|
2017-08-22 00:21:14 +08:00
|
|
|
self.bitslip = CSRStatus(6)
|
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
if mode == "master":
|
|
|
|
self.comb += init.reset.eq(self.reset.re)
|
|
|
|
self.comb += [
|
|
|
|
self.ready.status.eq(init.ready),
|
|
|
|
self.error.status.eq(init.error),
|
|
|
|
self.delay.status.eq(init.delay),
|
2017-08-29 19:38:52 +08:00
|
|
|
self.delay_min_found.status.eq(init.delay_min_found),
|
|
|
|
self.delay_min.status.eq(init.delay_min),
|
|
|
|
self.delay_max_found.status.eq(init.delay_max_found),
|
|
|
|
self.delay_max.status.eq(init.delay_max),
|
2017-08-22 00:21:14 +08:00
|
|
|
self.bitslip.status.eq(init.bitslip)
|
|
|
|
]
|
2017-08-30 20:31:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SERWBPLL(Module):
|
|
|
|
def __init__(self, refclk_freq, linerate, vco_div=1):
|
|
|
|
assert refclk_freq == 125e6
|
|
|
|
assert linerate == 1.25e9
|
|
|
|
|
|
|
|
self.lock = Signal()
|
|
|
|
self.refclk = Signal()
|
2017-08-30 21:44:44 +08:00
|
|
|
self.serwb_serdes_clk = Signal()
|
|
|
|
self.serwb_serdes_20x_clk = Signal()
|
|
|
|
self.serwb_serdes_5x_clk = Signal()
|
2017-08-30 20:31:44 +08:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2017-08-30 21:44:44 +08:00
|
|
|
#----------------------------
|
|
|
|
# refclk: 125MHz
|
|
|
|
# vco: 1250MHz
|
|
|
|
#----------------------------
|
|
|
|
# serwb_serdes: 31.25MHz
|
|
|
|
# serwb_serdes_20x: 625MHz
|
|
|
|
# serwb_serdes_5x: 156.25MHz
|
|
|
|
#----------------------------
|
2017-08-30 20:31:44 +08:00
|
|
|
self.linerate = linerate
|
|
|
|
|
|
|
|
pll_locked = Signal()
|
|
|
|
pll_fb = Signal()
|
2017-08-30 21:44:44 +08:00
|
|
|
pll_serwb_serdes_clk = Signal()
|
|
|
|
pll_serwb_serdes_20x_clk = Signal()
|
|
|
|
pll_serwb_serdes_5x_clk = Signal()
|
2017-08-30 20:31:44 +08:00
|
|
|
self.specials += [
|
|
|
|
Instance("PLLE2_BASE",
|
|
|
|
p_STARTUP_WAIT="FALSE", o_LOCKED=pll_locked,
|
|
|
|
|
|
|
|
# VCO @ 1.25GHz / vco_div
|
|
|
|
p_REF_JITTER1=0.01, p_CLKIN1_PERIOD=8.0,
|
|
|
|
p_CLKFBOUT_MULT=10, p_DIVCLK_DIVIDE=vco_div,
|
|
|
|
i_CLKIN1=self.refclk, i_CLKFBIN=pll_fb,
|
|
|
|
o_CLKFBOUT=pll_fb,
|
|
|
|
|
2017-08-30 21:44:44 +08:00
|
|
|
# 31.25MHz: serwb_serdes
|
2017-08-30 20:31:44 +08:00
|
|
|
p_CLKOUT0_DIVIDE=40//vco_div, p_CLKOUT0_PHASE=0.0,
|
2017-08-30 21:44:44 +08:00
|
|
|
o_CLKOUT0=pll_serwb_serdes_clk,
|
2017-08-30 20:31:44 +08:00
|
|
|
|
2017-08-30 21:44:44 +08:00
|
|
|
# 625MHz: serwb_serdes_20x
|
2017-08-30 20:31:44 +08:00
|
|
|
p_CLKOUT1_DIVIDE=2//vco_div, p_CLKOUT1_PHASE=0.0,
|
2017-08-30 21:44:44 +08:00
|
|
|
o_CLKOUT1=pll_serwb_serdes_20x_clk,
|
2017-08-30 20:31:44 +08:00
|
|
|
|
2017-08-30 21:44:44 +08:00
|
|
|
# 156.25MHz: serwb_serdes_5x
|
2017-08-30 20:31:44 +08:00
|
|
|
p_CLKOUT2_DIVIDE=8//vco_div, p_CLKOUT2_PHASE=0.0,
|
2017-08-30 21:44:44 +08:00
|
|
|
o_CLKOUT2=pll_serwb_serdes_5x_clk
|
2017-08-30 20:31:44 +08:00
|
|
|
),
|
2017-08-30 21:44:44 +08:00
|
|
|
Instance("BUFG", i_I=pll_serwb_serdes_clk, o_O=self.serwb_serdes_clk),
|
|
|
|
Instance("BUFG", i_I=pll_serwb_serdes_20x_clk, o_O=self.serwb_serdes_20x_clk),
|
|
|
|
Instance("BUFG", i_I=pll_serwb_serdes_5x_clk, o_O=self.serwb_serdes_5x_clk)
|
2017-08-30 20:31:44 +08:00
|
|
|
]
|
|
|
|
self.specials += MultiReg(pll_locked, self.lock)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SERWBPHY(Module, AutoCSR):
|
|
|
|
def __init__(self, device, pll, pads, mode="master"):
|
|
|
|
assert mode in ["master", "slave"]
|
|
|
|
if device[:4] == "xcku":
|
|
|
|
taps = 512
|
|
|
|
self.submodules.serdes = KUSSerdes(pll, pads, mode)
|
|
|
|
elif device[:4] == "xc7a":
|
|
|
|
taps = 32
|
|
|
|
self.submodules.serdes = S7Serdes(pll, pads, mode)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
if mode == "master":
|
|
|
|
self.submodules.init = _SerdesMasterInit(self.serdes, taps)
|
|
|
|
else:
|
|
|
|
self.submodules.init = _SerdesSlaveInit(self.serdes, taps)
|
|
|
|
self.submodules.control = _SerdesControl(self.init, mode)
|