Skip to content

Commit 60202ae

Browse files
committed
phy: Rework data_i_ce sampling logic (was too complicated and off by one cycle).
1 parent fefee15 commit 60202ae

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

litesdcard/phy.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# SPDX-License-Identifier: BSD-2-Clause
77

88
from migen import *
9+
from migen.genlib.cdc import MultiReg
910
from migen.genlib.resetsync import AsyncResetSynchronizer
1011

1112
from litex.gen import *
@@ -505,17 +506,16 @@ def __init__(self, sys_clk_freq, data_timeout):
505506
# SDCard PHY IO ------------------------------------------------------------------------------------
506507

507508
class SDPHYIO(LiteXModule):
508-
def __init__(self, clocker, sdpads, round_trip_latency=2):
509-
# Generate a data_i_ce pulse round_trip_latency cycles after clocker.clk goes high so that
510-
# the data input effectively get sampled on the first sys_clk after the SDCard clk goes high.
511-
clocker_clk_delay = Signal(round_trip_latency)
512-
self.sync += clocker_clk_delay.eq(Cat(clocker.clk, clocker_clk_delay))
513-
self.sync += sdpads.data_i_ce.eq(clocker_clk_delay[-1] & ~clocker_clk_delay[-2])
514-
509+
def add_data_i_ce(self, clocker, sdpads):
510+
# Sample Data on Sys Clk before SDCard Clk rising edge.
511+
clk_i = Signal()
512+
clk_i_d = Signal()
513+
self.specials += MultiReg(~clocker.clk, clk_i, n=1, odomain="sys") # n = 1 = SDROutput / SDRTristate delay.
514+
self.sync += clk_i_d.eq(clk_i)
515+
self.comb += sdpads.data_i_ce.eq(clk_i & ~clk_i_d) # Rising Edge.
515516

516517
class SDPHYIOGen(SDPHYIO):
517518
def __init__(self, clocker, sdpads, pads):
518-
SDPHYIO.__init__(self, clocker, sdpads, round_trip_latency=2)
519519
# Rst
520520
if hasattr(pads, "rst"):
521521
self.comb += pads.rst.eq(0)
@@ -545,6 +545,7 @@ def __init__(self, clocker, sdpads, pads):
545545
oe = sdpads.data.oe,
546546
i = sdpads.data.i[i],
547547
)
548+
self.add_data_i_ce(clocker, sdpads)
548549

549550
# Direction (optional)
550551
if hasattr(pads, "cmd_dir"):
@@ -570,7 +571,6 @@ def __init__(self, clocker, sdpads, pads):
570571

571572
class SDPHYIOEmulator(SDPHYIO):
572573
def __init__(self, clocker, sdpads, pads):
573-
SDPHYIO.__init__(self, clocker, sdpads, round_trip_latency=2) # FIXME: check round_trip_latency.
574574
# Clk
575575
self.comb += pads.clk.eq(clocker.clk)
576576

@@ -588,6 +588,7 @@ def __init__(self, clocker, sdpads, pads):
588588
If(sdpads.data.oe, pads.dat_i.eq(sdpads.data.o)),
589589
sdpads.data.i.eq(0b1111),
590590
]
591+
self.data_i_ce(clocker, sdpads)
591592
for i in range(4):
592593
self.comb += If(~pads.dat_t[i], sdpads.data.i[i].eq(pads.dat_o[i]))
593594

0 commit comments

Comments
 (0)