2020-08-23 10:06:55 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteEth.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2015-2018 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2019-06-24 05:43:10 -04:00
|
|
|
|
2020-01-17 17:19:56 -05:00
|
|
|
from migen import *
|
2018-02-23 07:40:09 -05:00
|
|
|
from migen.genlib.cdc import PulseSynchronizer
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2020-04-10 03:20:41 -04:00
|
|
|
from litex.build.io import DDROutput
|
|
|
|
|
2015-11-11 19:45:37 -05:00
|
|
|
from litex.soc.interconnect.stream import Multiplexer, Demultiplexer
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2020-01-17 17:19:56 -05:00
|
|
|
from liteeth.common import *
|
|
|
|
from liteeth.phy.gmii import LiteEthPHYGMIICRG
|
|
|
|
from liteeth.phy.mii import LiteEthPHYMIITX, LiteEthPHYMIIRX
|
|
|
|
from liteeth.phy.gmii import LiteEthPHYGMIITX, LiteEthPHYGMIIRX
|
2015-12-09 06:33:24 -05:00
|
|
|
from liteeth.phy.common import LiteEthPHYMDIO
|
|
|
|
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
modes = {
|
|
|
|
"GMII": 0,
|
|
|
|
"MII": 1
|
|
|
|
}
|
|
|
|
|
|
|
|
tx_pads_layout = [("tx_er", 1), ("tx_en", 1), ("tx_data", 8)]
|
2018-07-05 04:48:17 -04:00
|
|
|
rx_pads_layout = [("rx_er", 1), ("rx_dv", 1), ("rx_data", 8)]
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
class LiteEthPHYGMIIMIITX(Module):
|
|
|
|
def __init__(self, pads, mode):
|
2016-03-15 10:48:02 -04:00
|
|
|
self.sink = sink = stream.Endpoint(eth_phy_description(8))
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
gmii_tx_pads = Record(tx_pads_layout)
|
2016-03-15 10:33:13 -04:00
|
|
|
gmii_tx = LiteEthPHYGMIITX(gmii_tx_pads)
|
2015-09-07 07:28:02 -04:00
|
|
|
self.submodules += gmii_tx
|
|
|
|
|
|
|
|
mii_tx_pads = Record(tx_pads_layout)
|
2016-03-15 10:33:13 -04:00
|
|
|
mii_tx = LiteEthPHYMIITX(mii_tx_pads)
|
2015-09-07 07:28:02 -04:00
|
|
|
self.submodules += mii_tx
|
|
|
|
|
|
|
|
demux = Demultiplexer(eth_phy_description(8), 2)
|
|
|
|
self.submodules += demux
|
|
|
|
self.comb += [
|
|
|
|
demux.sel.eq(mode == modes["MII"]),
|
2015-12-27 06:26:01 -05:00
|
|
|
sink.connect(demux.sink),
|
|
|
|
demux.source0.connect(gmii_tx.sink),
|
|
|
|
demux.source1.connect(mii_tx.sink),
|
2015-09-07 07:28:02 -04:00
|
|
|
]
|
|
|
|
|
2017-07-01 05:22:26 -04:00
|
|
|
|
2015-09-07 07:28:02 -04:00
|
|
|
if hasattr(pads, "tx_er"):
|
2017-07-01 05:22:26 -04:00
|
|
|
pads.tx_er.reset_less = True
|
2015-09-07 07:28:02 -04:00
|
|
|
self.comb += pads.tx_er.eq(0)
|
2017-07-01 05:22:26 -04:00
|
|
|
pads.tx_en.reset_less = True
|
|
|
|
pads.tx_data.reset_less = True
|
2015-09-07 07:28:02 -04:00
|
|
|
self.sync += [
|
|
|
|
If(mode == modes["MII"],
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class LiteEthPHYGMIIMIIRX(Module):
|
|
|
|
def __init__(self, pads, mode):
|
2016-03-15 10:48:02 -04:00
|
|
|
self.source = source = stream.Endpoint(eth_phy_description(8))
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
pads_d = Record(rx_pads_layout)
|
2018-07-05 04:48:17 -04:00
|
|
|
pads_d.rx_dv.reset_less = True
|
2017-07-01 05:22:26 -04:00
|
|
|
pads_d.rx_data.reset_less = True
|
2015-09-07 07:28:02 -04:00
|
|
|
self.sync += [
|
2018-07-05 04:48:17 -04:00
|
|
|
pads_d.rx_dv.eq(pads.rx_dv),
|
2015-09-07 07:28:02 -04:00
|
|
|
pads_d.rx_data.eq(pads.rx_data)
|
|
|
|
]
|
|
|
|
|
|
|
|
gmii_rx = LiteEthPHYGMIIRX(pads_d)
|
|
|
|
self.submodules += gmii_rx
|
|
|
|
|
|
|
|
mii_rx = LiteEthPHYMIIRX(pads_d)
|
|
|
|
self.submodules += mii_rx
|
|
|
|
|
|
|
|
mux = Multiplexer(eth_phy_description(8), 2)
|
|
|
|
self.submodules += mux
|
|
|
|
self.comb += [
|
|
|
|
mux.sel.eq(mode == modes["MII"]),
|
2015-12-27 06:26:01 -05:00
|
|
|
gmii_rx.source.connect(mux.sink0),
|
|
|
|
mii_rx.source.connect(mux.sink1),
|
|
|
|
mux.source.connect(source)
|
2015-09-07 07:28:02 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class LiteEthGMIIMIIModeDetection(Module, AutoCSR):
|
|
|
|
def __init__(self, clk_freq):
|
|
|
|
self.mode = Signal()
|
|
|
|
self._mode = CSRStatus()
|
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
mode = Signal()
|
|
|
|
update_mode = Signal()
|
|
|
|
self.sync += \
|
|
|
|
If(update_mode,
|
|
|
|
self.mode.eq(mode)
|
|
|
|
)
|
|
|
|
self.comb += self._mode.status.eq(self.mode)
|
|
|
|
|
|
|
|
# Principle:
|
|
|
|
# sys_clk >= 125MHz
|
|
|
|
# eth_rx <= 125Mhz
|
|
|
|
# We generate ticks every 1024 clock cycles in eth_rx domain
|
|
|
|
# and measure ticks period in sys_clk domain.
|
|
|
|
|
|
|
|
# Generate a tick every 1024 clock cycles (eth_rx clock domain)
|
|
|
|
eth_tick = Signal()
|
2017-07-01 05:22:26 -04:00
|
|
|
eth_counter = Signal(10, reset_less=True)
|
2015-09-07 07:28:02 -04:00
|
|
|
self.sync.eth_rx += eth_counter.eq(eth_counter + 1)
|
|
|
|
self.comb += eth_tick.eq(eth_counter == 0)
|
|
|
|
|
|
|
|
# Synchronize tick (sys clock domain)
|
|
|
|
sys_tick = Signal()
|
|
|
|
eth_ps = PulseSynchronizer("eth_rx", "sys")
|
|
|
|
self.comb += [
|
|
|
|
eth_ps.i.eq(eth_tick),
|
|
|
|
sys_tick.eq(eth_ps.o)
|
|
|
|
]
|
|
|
|
self.submodules += eth_ps
|
|
|
|
|
|
|
|
# sys_clk domain counter
|
2017-07-01 05:22:26 -04:00
|
|
|
sys_counter = Signal(24, reset_less=True)
|
2015-11-11 19:45:37 -05:00
|
|
|
sys_counter_reset = Signal()
|
|
|
|
sys_counter_ce = Signal()
|
|
|
|
self.sync += [
|
|
|
|
If(sys_counter_reset,
|
|
|
|
sys_counter.eq(0)
|
|
|
|
).Elif(sys_counter_ce,
|
|
|
|
sys_counter.eq(sys_counter + 1)
|
|
|
|
)
|
|
|
|
]
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
|
|
|
|
fsm.act("IDLE",
|
2015-11-11 19:45:37 -05:00
|
|
|
sys_counter_reset.eq(1),
|
2015-09-07 07:28:02 -04:00
|
|
|
If(sys_tick,
|
|
|
|
NextState("COUNT")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("COUNT",
|
2015-11-11 19:45:37 -05:00
|
|
|
sys_counter_ce.eq(1),
|
2015-09-07 07:28:02 -04:00
|
|
|
If(sys_tick,
|
|
|
|
NextState("DETECTION")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("DETECTION",
|
|
|
|
update_mode.eq(1),
|
|
|
|
# if freq < 125MHz-5% use MII mode
|
2015-11-11 19:45:37 -05:00
|
|
|
If(sys_counter > int((clk_freq/125000000)*1024*1.05),
|
2015-09-07 07:28:02 -04:00
|
|
|
mode.eq(1)
|
|
|
|
# if freq >= 125MHz-5% use GMII mode
|
|
|
|
).Else(
|
|
|
|
mode.eq(0)
|
|
|
|
),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LiteEthPHYGMIIMII(Module, AutoCSR):
|
2020-03-01 13:09:55 -05:00
|
|
|
dw = 8
|
|
|
|
tx_clk_freq = 125e6
|
|
|
|
rx_clk_freq = 125e6
|
2015-09-07 07:28:02 -04:00
|
|
|
def __init__(self, clock_pads, pads, clk_freq, with_hw_init_reset=True):
|
|
|
|
# Note: we can use GMII CRG since it also handles tx clock pad used for MII
|
|
|
|
self.submodules.mode_detection = LiteEthGMIIMIIModeDetection(clk_freq)
|
|
|
|
mode = self.mode_detection.mode
|
|
|
|
self.submodules.crg = LiteEthPHYGMIICRG(clock_pads, pads, with_hw_init_reset, mode == modes["MII"])
|
2015-11-11 19:45:37 -05:00
|
|
|
self.submodules.tx = ClockDomainsRenamer("eth_tx")(LiteEthPHYGMIIMIITX(pads, mode))
|
|
|
|
self.submodules.rx = ClockDomainsRenamer("eth_rx")(LiteEthPHYGMIIMIIRX(pads, mode))
|
2015-09-07 07:28:02 -04:00
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|
2015-12-09 06:33:24 -05:00
|
|
|
|
|
|
|
if hasattr(pads, "mdc"):
|
|
|
|
self.submodules.mdio = LiteEthPHYMDIO(pads)
|