2014-09-27 07:33:43 -04:00
|
|
|
from math import ceil
|
|
|
|
|
2014-09-23 17:03:32 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
from migen.genlib.fsm import FSM, NextState
|
|
|
|
|
2014-09-24 08:28:52 -04:00
|
|
|
from lib.sata.k7sataphy.std import *
|
|
|
|
|
2014-09-23 17:03:32 -04:00
|
|
|
class K7SATAPHYReconfig(Module):
|
|
|
|
def __init__(self, channel_drp, mmcm_drp):
|
|
|
|
self.speed = Signal(3)
|
|
|
|
###
|
|
|
|
speed_r = Signal(3)
|
|
|
|
speed_change = Signal()
|
2014-09-24 08:28:52 -04:00
|
|
|
self.sync += speed_r.eq(self.speed)
|
|
|
|
self.comb += speed_change.eq(self.speed != speed_r)
|
2014-09-23 17:03:32 -04:00
|
|
|
|
|
|
|
drp_sel = Signal()
|
|
|
|
drp = DRPBus()
|
|
|
|
self.comb += \
|
2014-09-24 08:28:52 -04:00
|
|
|
If(drp_sel,
|
2014-09-25 08:14:11 -04:00
|
|
|
drp.connect(mmcm_drp)
|
2014-09-23 17:03:32 -04:00
|
|
|
).Else(
|
2014-09-25 08:14:11 -04:00
|
|
|
drp.connect(channel_drp)
|
2014-09-23 17:03:32 -04:00
|
|
|
)
|
|
|
|
|
2014-09-27 09:34:28 -04:00
|
|
|
class K7SATAPHYCRG(Module):
|
2014-09-27 10:22:40 -04:00
|
|
|
def __init__(self, pads, gtx, clk_freq, default_speed):
|
2014-09-23 17:03:32 -04:00
|
|
|
self.reset = Signal()
|
2014-09-27 10:10:39 -04:00
|
|
|
self.ready = Signal()
|
2014-09-23 17:03:32 -04:00
|
|
|
|
2014-09-24 08:28:52 -04:00
|
|
|
self.clock_domains.cd_sata_tx = ClockDomain()
|
|
|
|
self.clock_domains.cd_sata_rx = ClockDomain()
|
2014-09-23 17:03:32 -04:00
|
|
|
|
2014-09-27 07:33:43 -04:00
|
|
|
# CPLL
|
|
|
|
# (SATA3) 150MHz / VCO @ 3GHz / Line rate @ 6Gbps
|
|
|
|
# (SATA2 & SATA1) VCO still @ 3 GHz, Line rate is decreased with output divivers.
|
|
|
|
# When changing rate, reconfiguration of the CPLL over DRP is needed to:
|
|
|
|
# - update the output divider
|
|
|
|
# - update the equalizer configuration (specific for each line rate).
|
2014-09-23 17:03:32 -04:00
|
|
|
refclk = Signal()
|
|
|
|
self.specials += Instance("IBUFDS_GTE2",
|
2014-09-26 05:36:28 -04:00
|
|
|
i_CEB=0,
|
2014-09-23 17:03:32 -04:00
|
|
|
i_I=pads.refclk_p,
|
|
|
|
i_IB=pads.refclk_n,
|
|
|
|
o_O=refclk
|
|
|
|
)
|
2014-09-26 05:36:28 -04:00
|
|
|
self.comb += gtx.gtrefclk0.eq(refclk)
|
|
|
|
|
2014-09-27 07:33:43 -04:00
|
|
|
# TX clocking
|
2014-09-27 11:25:49 -04:00
|
|
|
# (SATA3) 150MHz from CPLL TXOUTCLK, sata_tx clk @ 300MHz (16-bits)
|
|
|
|
# (SATA2) 150MHz from CPLL TXOUTCLK, sata_tx clk @ 150MHz (16-bits)
|
|
|
|
# (SATA1) 150MHz from CPLL TXOUTCLK, sata_tx clk @ 75MHz (16-bits)
|
2014-09-27 07:33:43 -04:00
|
|
|
# When changing rate, reconfiguration of the MMCM is needed to update the output divider.
|
2014-09-23 17:03:32 -04:00
|
|
|
mmcm_reset = Signal()
|
|
|
|
mmcm_locked = Signal()
|
2014-09-24 08:28:52 -04:00
|
|
|
mmcm_drp = DRPBus()
|
2014-09-23 17:03:32 -04:00
|
|
|
mmcm_fb = Signal()
|
|
|
|
mmcm_clk_i = Signal()
|
2014-09-24 07:55:06 -04:00
|
|
|
mmcm_clk0_o = Signal()
|
2014-09-24 08:28:52 -04:00
|
|
|
mmcm_clk1_o = Signal()
|
2014-09-27 10:22:40 -04:00
|
|
|
mmcm_div_config = {
|
|
|
|
"SATA1" : 16,
|
|
|
|
"SATA2" : 8,
|
|
|
|
"SATA3" : 4
|
|
|
|
}
|
|
|
|
mmcm_div = mmcm_div_config[default_speed]
|
2014-09-23 17:03:32 -04:00
|
|
|
self.specials += [
|
2014-09-27 07:33:43 -04:00
|
|
|
Instance("BUFG", i_I=gtx.txoutclk, o_O=mmcm_clk_i),
|
2014-09-23 17:03:32 -04:00
|
|
|
Instance("MMCME2_ADV",
|
|
|
|
p_BANDWIDTH="HIGH", p_COMPENSATION="ZHOLD", i_RST=mmcm_reset, o_LOCKED=mmcm_locked,
|
|
|
|
|
|
|
|
# DRP
|
2014-09-24 08:28:52 -04:00
|
|
|
i_DCLK=mmcm_drp.clk, i_DEN=mmcm_drp.en, o_DRDY=mmcm_drp.rdy, i_DWE=mmcm_drp.we,
|
2014-09-26 05:36:28 -04:00
|
|
|
i_DADDR=mmcm_drp.addr, i_DI=mmcm_drp.di, o_DO=mmcm_drp.do,
|
2014-09-23 17:03:32 -04:00
|
|
|
|
|
|
|
# VCO
|
2014-09-26 05:36:28 -04:00
|
|
|
p_REF_JITTER1=0.01, p_CLKIN1_PERIOD=6.666,
|
2014-09-27 07:33:43 -04:00
|
|
|
p_CLKFBOUT_MULT_F=8.000, p_CLKFBOUT_PHASE=0.000, p_DIVCLK_DIVIDE=1,
|
2014-09-23 17:03:32 -04:00
|
|
|
i_CLKIN1=mmcm_clk_i, i_CLKFBIN=mmcm_fb, o_CLKFBOUT=mmcm_fb,
|
|
|
|
|
|
|
|
# CLK0
|
2014-09-27 10:22:40 -04:00
|
|
|
p_CLKOUT0_DIVIDE_F=mmcm_div, p_CLKOUT0_PHASE=0.000, o_CLKOUT0=mmcm_clk0_o,
|
2014-09-24 07:55:06 -04:00
|
|
|
|
|
|
|
# CLK1
|
2014-09-27 10:22:40 -04:00
|
|
|
p_CLKOUT1_DIVIDE=mmcm_div*2, p_CLKOUT1_PHASE=0.000, o_CLKOUT1=mmcm_clk1_o,
|
2014-09-23 17:03:32 -04:00
|
|
|
),
|
2014-09-24 07:55:06 -04:00
|
|
|
Instance("BUFG", i_I=mmcm_clk0_o, o_O=self.cd_sata_tx.clk),
|
2014-09-23 17:03:32 -04:00
|
|
|
]
|
2014-09-26 15:52:32 -04:00
|
|
|
self.comb += [
|
|
|
|
gtx.txusrclk.eq(self.cd_sata_tx.clk),
|
|
|
|
gtx.txusrclk2.eq(self.cd_sata_tx.clk)
|
|
|
|
]
|
2014-09-23 17:03:32 -04:00
|
|
|
|
|
|
|
# RX clocking
|
2014-09-27 07:33:43 -04:00
|
|
|
# (SATA3) sata_rx recovered clk @ 300MHz from CPLL RXOUTCLK
|
|
|
|
# (SATA2) sata_rx recovered clk @ 150MHz from CPLL RXOUTCLK
|
|
|
|
# (SATA1) sata_rx recovered clk @ 150MHz from CPLL RXOUTCLK
|
2014-09-23 17:03:32 -04:00
|
|
|
self.specials += [
|
2014-09-27 07:33:43 -04:00
|
|
|
Instance("BUFG", i_I=gtx.rxoutclk, o_O=self.cd_sata_rx.clk),
|
2014-09-23 17:03:32 -04:00
|
|
|
]
|
|
|
|
self.comb += [
|
|
|
|
gtx.rxusrclk.eq(self.cd_sata_rx.clk),
|
|
|
|
gtx.rxusrclk2.eq(self.cd_sata_rx.clk)
|
|
|
|
]
|
|
|
|
|
2014-09-27 10:10:39 -04:00
|
|
|
# Bypass TX buffer
|
2014-09-23 17:03:32 -04:00
|
|
|
self.comb += [
|
2014-09-24 08:28:52 -04:00
|
|
|
gtx.txphdlyreset.eq(0),
|
|
|
|
gtx.txphalignen.eq(0),
|
|
|
|
gtx.txdlyen.eq(0),
|
|
|
|
gtx.txphalign.eq(0),
|
|
|
|
gtx.txphinit.eq(0)
|
2014-09-23 17:03:32 -04:00
|
|
|
]
|
|
|
|
|
2014-09-27 10:10:39 -04:00
|
|
|
# Bypass RX buffer
|
2014-09-27 07:33:43 -04:00
|
|
|
self.comb += [
|
|
|
|
gtx.rxphdlyreset.eq(0),
|
|
|
|
gtx.rxdlyen.eq(0),
|
|
|
|
gtx.rxphalign.eq(0),
|
|
|
|
gtx.rxphalignen.eq(0),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Configuration Reset
|
2014-09-27 09:34:28 -04:00
|
|
|
# After configuration, GTX resets have to stay low for at least 500ns
|
2014-09-27 07:33:43 -04:00
|
|
|
# See AR43482
|
|
|
|
reset_en = Signal()
|
|
|
|
clk_period_ns = 1000000000/clk_freq
|
|
|
|
reset_en_cnt_max = ceil(500/clk_period_ns)
|
|
|
|
reset_en_cnt = Signal(max=reset_en_cnt_max, reset=reset_en_cnt_max-1)
|
|
|
|
self.sync += If(~reset_en, reset_en_cnt.eq(reset_en_cnt-1))
|
|
|
|
self.comb += reset_en.eq(reset_en_cnt == 0)
|
|
|
|
|
2014-09-27 10:10:39 -04:00
|
|
|
# TX Reset FSM
|
|
|
|
tx_reset_fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += tx_reset_fsm
|
|
|
|
tx_reset_fsm.act("IDLE",
|
|
|
|
gtx.txuserrdy.eq(0),
|
|
|
|
gtx.gttxreset.eq(0),
|
|
|
|
gtx.txdlysreset.eq(0),
|
|
|
|
If(reset_en,
|
|
|
|
NextState("RESET_ALL"),
|
2014-09-23 17:03:32 -04:00
|
|
|
)
|
2014-09-27 10:10:39 -04:00
|
|
|
)
|
|
|
|
tx_reset_fsm.act("RESET_ALL",
|
|
|
|
gtx.txuserrdy.eq(0),
|
|
|
|
gtx.gttxreset.eq(1),
|
|
|
|
gtx.txdlysreset.eq(1),
|
|
|
|
If(gtx.cplllock & mmcm_locked,
|
|
|
|
NextState("RELEASE_GTXRESET")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
tx_reset_fsm.act("RELEASE_GTXRESET",
|
|
|
|
gtx.txuserrdy.eq(1),
|
|
|
|
gtx.gttxreset.eq(0),
|
|
|
|
gtx.txdlysreset.eq(1),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
|
|
|
).Elif(gtx.txresetdone,
|
|
|
|
NextState("RELEASE_DLYRESET")
|
2014-09-23 17:03:32 -04:00
|
|
|
)
|
2014-09-27 10:10:39 -04:00
|
|
|
)
|
|
|
|
tx_reset_fsm.act("RELEASE_DLYRESET",
|
|
|
|
gtx.txuserrdy.eq(1),
|
|
|
|
gtx.gttxreset.eq(0),
|
|
|
|
gtx.txdlysreset.eq(0),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
|
|
|
).Elif(gtx.txdlysresetdone,
|
|
|
|
NextState("READY")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
tx_reset_fsm.act("READY",
|
|
|
|
gtx.txuserrdy.eq(1),
|
|
|
|
gtx.gttxreset.eq(0),
|
|
|
|
gtx.txdlysreset.eq(0),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
|
|
|
)
|
|
|
|
)
|
2014-09-23 17:03:32 -04:00
|
|
|
|
2014-09-27 10:10:39 -04:00
|
|
|
# RX Reset FSM
|
|
|
|
rx_reset_fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += rx_reset_fsm
|
|
|
|
rx_reset_fsm.act("IDLE",
|
|
|
|
gtx.rxuserrdy.eq(0),
|
|
|
|
gtx.gtrxreset.eq(0),
|
|
|
|
gtx.rxdlysreset.eq(0),
|
|
|
|
If(reset_en,
|
|
|
|
NextState("RESET_ALL"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rx_reset_fsm.act("RESET_ALL",
|
|
|
|
gtx.rxuserrdy.eq(0),
|
|
|
|
gtx.gtrxreset.eq(1),
|
|
|
|
gtx.rxdlysreset.eq(1),
|
|
|
|
If(gtx.cplllock & mmcm_locked,
|
|
|
|
NextState("RELEASE_GTXRESET")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rx_reset_fsm.act("RELEASE_GTXRESET",
|
|
|
|
gtx.rxuserrdy.eq(1),
|
|
|
|
gtx.gtrxreset.eq(0),
|
|
|
|
gtx.rxdlysreset.eq(1),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
|
|
|
).Elif(gtx.rxresetdone,
|
|
|
|
NextState("RELEASE_DLYRESET")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rx_reset_fsm.act("RELEASE_DLYRESET",
|
|
|
|
gtx.rxuserrdy.eq(1),
|
|
|
|
gtx.gtrxreset.eq(0),
|
|
|
|
gtx.rxdlysreset.eq(0),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
|
|
|
).Elif(gtx.rxdlysresetdone,
|
|
|
|
NextState("READY")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rx_reset_fsm.act("READY",
|
|
|
|
gtx.rxuserrdy.eq(1),
|
|
|
|
gtx.gtrxreset.eq(0),
|
|
|
|
gtx.rxdlysreset.eq(0),
|
|
|
|
If(self.reset,
|
|
|
|
NextState("RESET_ALL")
|
2014-09-26 05:36:28 -04:00
|
|
|
)
|
2014-09-27 10:10:39 -04:00
|
|
|
)
|
2014-09-26 05:36:28 -04:00
|
|
|
|
2014-09-27 10:10:39 -04:00
|
|
|
self.comb += self.ready.eq(tx_reset_fsm.ongoing("READY") & rx_reset_fsm.ongoing("READY"))
|
|
|
|
|
|
|
|
# Reset PLL
|
|
|
|
self.comb += gtx.cpllreset.eq(self.reset | ~reset_en)
|
|
|
|
|
|
|
|
# Reset for SATA TX/RX clock domains
|
2014-09-23 17:03:32 -04:00
|
|
|
self.specials += [
|
2014-09-27 10:10:39 -04:00
|
|
|
AsyncResetSynchronizer(self.cd_sata_tx, ~self.ready),
|
|
|
|
AsyncResetSynchronizer(self.cd_sata_rx, ~self.ready),
|
2014-09-23 17:03:32 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
# Dynamic Reconfiguration
|
|
|
|
self.submodules.reconfig = K7SATAPHYReconfig(mmcm_drp, gtx.drp)
|