2015-04-12 10:49:39 -04:00
|
|
|
from migen.genlib.io import DDROutput
|
|
|
|
from migen.flow.plumbing import Multiplexer, Demultiplexer
|
2015-04-26 08:13:09 -04:00
|
|
|
from migen.genlib.cdc import PulseSynchronizer
|
2015-04-12 10:49:39 -04:00
|
|
|
|
|
|
|
from misoclib.com.liteeth.common import *
|
|
|
|
|
2015-04-12 11:33:38 -04:00
|
|
|
from misoclib.com.liteeth.phy.gmii import LiteEthPHYGMIICRG
|
2015-04-12 10:49:39 -04:00
|
|
|
from misoclib.com.liteeth.phy.mii import LiteEthPHYMIITX, LiteEthPHYMIIRX
|
|
|
|
from misoclib.com.liteeth.phy.gmii import LiteEthPHYGMIITX, LiteEthPHYGMIIRX
|
|
|
|
|
|
|
|
modes = {
|
2015-04-13 04:39:46 -04:00
|
|
|
"GMII": 0,
|
|
|
|
"MII": 1
|
2015-04-12 10:49:39 -04:00
|
|
|
}
|
|
|
|
|
2015-04-12 14:19:32 -04:00
|
|
|
tx_pads_layout = [("tx_er", 1), ("tx_en", 1), ("tx_data", 8)]
|
|
|
|
rx_pads_layout = [("rx_er", 1), ("dv", 1), ("rx_data", 8)]
|
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-12 10:49:39 -04:00
|
|
|
class LiteEthPHYGMIIMIITX(Module):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self, pads, mode):
|
|
|
|
self.sink = sink = Sink(eth_phy_description(8))
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
gmii_tx_pads = Record(tx_pads_layout)
|
|
|
|
gmii_tx = LiteEthPHYGMIITX(gmii_tx_pads, pads_register=False)
|
|
|
|
self.submodules += gmii_tx
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
mii_tx_pads = Record(tx_pads_layout)
|
|
|
|
mii_tx = LiteEthPHYMIITX(mii_tx_pads, pads_register=False)
|
|
|
|
self.submodules += mii_tx
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
demux = Demultiplexer(eth_phy_description(8), 2)
|
|
|
|
self.submodules += demux
|
|
|
|
self.comb += [
|
2015-04-13 04:56:18 -04:00
|
|
|
demux.sel.eq(mode == modes["MII"]),
|
2015-04-13 03:53:43 -04:00
|
|
|
Record.connect(sink, demux.sink),
|
|
|
|
Record.connect(demux.source0, gmii_tx.sink),
|
|
|
|
Record.connect(demux.source1, mii_tx.sink),
|
|
|
|
]
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
if hasattr(pads, "tx_er"):
|
|
|
|
self.comb += pads.tx_er.eq(0)
|
|
|
|
self.sync += [
|
2015-04-13 04:56:18 -04:00
|
|
|
If(mode == modes["MII"],
|
2015-04-13 03:53:43 -04:00
|
|
|
pads.tx_en.eq(mii_tx_pads.tx_en),
|
|
|
|
pads.tx_data.eq(mii_tx_pads.tx_data),
|
|
|
|
).Else(
|
|
|
|
pads.tx_en.eq(gmii_tx_pads.tx_en),
|
|
|
|
pads.tx_data.eq(gmii_tx_pads.tx_data),
|
|
|
|
)
|
|
|
|
]
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-12 10:49:39 -04:00
|
|
|
class LiteEthPHYGMIIMIIRX(Module):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self, pads, mode):
|
|
|
|
self.source = source = Source(eth_phy_description(8))
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
pads_d = Record(rx_pads_layout)
|
|
|
|
self.sync += [
|
|
|
|
pads_d.dv.eq(pads.dv),
|
|
|
|
pads_d.rx_data.eq(pads.rx_data)
|
|
|
|
]
|
2015-04-12 14:19:32 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
gmii_rx = LiteEthPHYGMIIRX(pads_d)
|
|
|
|
self.submodules += gmii_rx
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
mii_rx = LiteEthPHYMIIRX(pads_d)
|
|
|
|
self.submodules += mii_rx
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
mux = Multiplexer(eth_phy_description(8), 2)
|
|
|
|
self.submodules += mux
|
|
|
|
self.comb += [
|
2015-04-13 04:56:18 -04:00
|
|
|
mux.sel.eq(mode == modes["MII"]),
|
2015-04-13 03:53:43 -04:00
|
|
|
Record.connect(gmii_rx.source, mux.sink0),
|
|
|
|
Record.connect(mii_rx.source, mux.sink1),
|
|
|
|
Record.connect(mux.source, source)
|
|
|
|
]
|
2015-04-12 10:49:39 -04:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-26 08:13:09 -04:00
|
|
|
class LiteEthGMIIMIIModeDetection(Module, AutoCSR):
|
2015-04-26 11:32:25 -04:00
|
|
|
def __init__(self, clk_freq):
|
2015-04-26 08:13:09 -04:00
|
|
|
self.mode = Signal()
|
2015-04-26 11:32:25 -04:00
|
|
|
self._mode = CSRStatus()
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-26 11:32:25 -04:00
|
|
|
mode = Signal()
|
|
|
|
update_mode = Signal()
|
|
|
|
self.sync += \
|
|
|
|
If(update_mode,
|
|
|
|
self.mode.eq(mode)
|
|
|
|
)
|
|
|
|
self.comb += self._mode.status.eq(self.mode)
|
2015-04-26 08:13:09 -04:00
|
|
|
|
|
|
|
# Principle:
|
|
|
|
# sys_clk >= 125MHz
|
|
|
|
# eth_rx <= 125Mhz
|
2015-04-26 11:32:25 -04:00
|
|
|
# We generate ticks every 1024 clock cycles in eth_rx domain
|
|
|
|
# and measure ticks period in sys_clk domain.
|
2015-04-26 08:13:09 -04:00
|
|
|
|
2015-04-26 11:32:25 -04:00
|
|
|
# Generate a tick every 1024 clock cycles (eth_rx clock domain)
|
|
|
|
eth_tick = Signal()
|
|
|
|
eth_counter = Signal(10)
|
2015-04-26 08:13:09 -04:00
|
|
|
self.sync.eth_rx += eth_counter.eq(eth_counter + 1)
|
2015-04-26 11:32:25 -04:00
|
|
|
self.comb += eth_tick.eq(eth_counter == 0)
|
2015-04-26 08:13:09 -04:00
|
|
|
|
2015-04-26 11:32:25 -04:00
|
|
|
# Synchronize tick (sys clock domain)
|
|
|
|
sys_tick = Signal()
|
2015-04-26 08:13:09 -04:00
|
|
|
eth_ps = PulseSynchronizer("eth_rx", "sys")
|
|
|
|
self.comb += [
|
2015-04-26 11:32:25 -04:00
|
|
|
eth_ps.i.eq(eth_tick),
|
|
|
|
sys_tick.eq(eth_ps.o)
|
2015-04-26 08:13:09 -04:00
|
|
|
]
|
|
|
|
self.submodules += eth_ps
|
|
|
|
|
2015-04-26 11:32:25 -04:00
|
|
|
# sys_clk domain counter
|
|
|
|
sys_counter = Counter(24)
|
|
|
|
self.submodules += sys_counter
|
|
|
|
|
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
2015-04-26 08:13:09 -04:00
|
|
|
|
2015-04-26 11:32:25 -04:00
|
|
|
fsm.act("IDLE",
|
|
|
|
sys_counter.reset.eq(1),
|
|
|
|
If(sys_tick,
|
|
|
|
NextState("COUNT")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("COUNT",
|
|
|
|
sys_counter.ce.eq(1),
|
|
|
|
If(sys_tick,
|
|
|
|
NextState("DETECTION")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("DETECTION",
|
|
|
|
update_mode.eq(1),
|
|
|
|
# if freq < 125MHz-5% use MII mode
|
|
|
|
If(sys_counter.value > int((clk_freq/125000000)*1024*1.05),
|
|
|
|
mode.eq(1)
|
|
|
|
# if freq >= 125MHz-5% use GMII mode
|
|
|
|
).Else(
|
|
|
|
mode.eq(0)
|
|
|
|
),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
2015-04-12 11:33:38 -04:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-12 10:49:39 -04:00
|
|
|
class LiteEthPHYGMIIMII(Module, AutoCSR):
|
2015-04-26 11:32:25 -04:00
|
|
|
def __init__(self, clock_pads, pads, clk_freq, with_hw_init_reset=True):
|
2015-04-13 03:53:43 -04:00
|
|
|
self.dw = 8
|
|
|
|
# Note: we can use GMII CRG since it also handles tx clock pad used for MII
|
2015-04-26 11:32:25 -04:00
|
|
|
self.submodules.mode_detection = LiteEthGMIIMIIModeDetection(clk_freq)
|
2015-04-26 08:13:09 -04:00
|
|
|
mode = self.mode_detection.mode
|
2015-04-13 04:56:18 -04:00
|
|
|
self.submodules.crg = LiteEthPHYGMIICRG(clock_pads, pads, with_hw_init_reset, mode == modes["MII"])
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.tx = RenameClockDomains(LiteEthPHYGMIIMIITX(pads, mode), "eth_tx")
|
|
|
|
self.submodules.rx = RenameClockDomains(LiteEthPHYGMIIMIIRX(pads, mode), "eth_rx")
|
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|