2014-11-04 11:35:46 -05:00
|
|
|
from migen.fhdl.std import *
|
2014-11-11 12:47:34 -05:00
|
|
|
from migen.genlib.fsm import FSM, NextState
|
2014-12-05 15:27:26 -05:00
|
|
|
from migen.actorlib.fifo import SyncFIFO
|
2014-11-04 11:35:46 -05:00
|
|
|
|
|
|
|
from lib.sata.std import *
|
2014-11-11 12:47:34 -05:00
|
|
|
from lib.sata.link.crc import SATACRCInserter, SATACRCChecker
|
|
|
|
from lib.sata.link.scrambler import SATAScrambler
|
2014-12-03 05:50:31 -05:00
|
|
|
from lib.sata.link.cont import SATACONTInserter, SATACONTRemover
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
from_rx = [
|
|
|
|
("idle", 1),
|
|
|
|
("insert", 32),
|
|
|
|
("det", 32)
|
|
|
|
]
|
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATALinkTX(Module):
|
2014-11-11 12:47:34 -05:00
|
|
|
def __init__(self, phy):
|
|
|
|
self.sink = Sink(link_layout(32))
|
2014-12-03 09:29:01 -05:00
|
|
|
self.from_rx = Sink(from_rx)
|
|
|
|
|
|
|
|
###
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# insert CRC
|
2014-12-03 09:29:01 -05:00
|
|
|
crc = SATACRCInserter(link_layout(32))
|
|
|
|
self.submodules += crc
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# scramble
|
2014-12-03 09:29:01 -05:00
|
|
|
scrambler = SATAScrambler(link_layout(32))
|
|
|
|
self.submodules += scrambler
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
# connect CRC / scrambler
|
2014-11-11 06:26:32 -05:00
|
|
|
self.comb += [
|
2014-12-03 09:29:01 -05:00
|
|
|
Record.connect(self.sink, crc.sink),
|
|
|
|
Record.connect(crc.source, scrambler.sink)
|
2014-11-11 06:26:32 -05:00
|
|
|
]
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-03 05:12:26 -05:00
|
|
|
# inserter CONT and scrambled data between
|
|
|
|
# CONT and next primitive
|
2014-12-03 09:29:01 -05:00
|
|
|
cont = SATACONTInserter(phy_layout(32))
|
|
|
|
self.submodules += cont
|
2014-12-03 05:12:26 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# datas / primitives mux
|
2014-12-03 09:29:01 -05:00
|
|
|
insert = Signal(32)
|
2014-11-11 06:26:32 -05:00
|
|
|
self.comb += [
|
2014-12-03 09:29:01 -05:00
|
|
|
If(self.from_rx.insert,
|
|
|
|
cont.sink.stb.eq(1),
|
|
|
|
cont.sink.data.eq(self.from_rx.insert),
|
|
|
|
cont.sink.charisk.eq(0x0001),
|
|
|
|
).
|
|
|
|
Elif(insert,
|
|
|
|
cont.sink.stb.eq(1),
|
|
|
|
cont.sink.data.eq(insert),
|
|
|
|
cont.sink.charisk.eq(0x0001),
|
|
|
|
).Elif(fsm.ongoing("COPY"),
|
|
|
|
cont.sink.stb.eq(scrambler.source.stb),
|
|
|
|
cont.sink.data.eq(scrambler.source.d),
|
|
|
|
scrambler.source.ack.eq(cont.sink.ack),
|
|
|
|
cont.sink.charisk.eq(0)
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
|
|
|
]
|
2014-12-03 09:29:01 -05:00
|
|
|
self.comb += Record.connect(cont.source, phy.sink)
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
# FSM
|
|
|
|
fsm.act("IDLE",
|
2014-12-05 14:26:09 -05:00
|
|
|
scrambler.reset.eq(1),
|
|
|
|
If(self.from_rx.idle,
|
|
|
|
insert.eq(primitives["SYNC"]),
|
|
|
|
If(scrambler.source.stb & scrambler.source.sop,
|
|
|
|
NextState("RDY"),
|
2014-12-03 09:29:01 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RDY",
|
|
|
|
insert.eq(primitives["X_RDY"]),
|
2014-12-05 14:26:09 -05:00
|
|
|
If(~self.from_rx.idle,
|
|
|
|
NextState("IDLE")
|
|
|
|
).Elif(self.from_rx.det == primitives["R_RDY"],
|
2014-12-03 09:29:01 -05:00
|
|
|
NextState("SOF")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SOF",
|
|
|
|
insert.eq(primitives["SOF"]),
|
|
|
|
If(phy.sink.ack,
|
|
|
|
NextState("COPY")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("COPY",
|
|
|
|
If(self.from_rx.det == primitives["HOLD"],
|
|
|
|
insert.eq(primitives["HOLDA"]),
|
|
|
|
).Elif(~scrambler.source.stb,
|
|
|
|
insert.eq(primitives["HOLD"]),
|
|
|
|
).Elif(scrambler.source.stb & scrambler.source.eop & scrambler.source.ack,
|
|
|
|
NextState("EOF")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("EOF",
|
|
|
|
insert.eq(primitives["EOF"]),
|
|
|
|
If(phy.sink.ack,
|
|
|
|
NextState("WTRM")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WTRM",
|
|
|
|
insert.eq(primitives["WTRM"]),
|
|
|
|
If(self.from_rx.det == primitives["R_OK"],
|
|
|
|
NextState("IDLE")
|
|
|
|
).Elif(self.from_rx.det == primitives["R_ERR"],
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
2014-12-03 05:12:26 -05:00
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATALinkRX(Module):
|
2014-12-03 09:29:01 -05:00
|
|
|
def __init__(self, phy):
|
|
|
|
self.source = Source(link_layout(32))
|
|
|
|
self.to_tx = Source(from_rx)
|
2014-12-03 05:50:31 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
###
|
2014-12-03 05:50:31 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
|
|
|
|
# CONT remover
|
|
|
|
cont = SATACONTRemover(phy_layout(32))
|
|
|
|
self.submodules += cont
|
|
|
|
self.comb += Record.connect(phy.source, cont.sink)
|
2014-12-03 05:50:31 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# datas / primitives detection
|
2014-12-03 09:29:01 -05:00
|
|
|
insert = Signal(32)
|
|
|
|
det = Signal(32)
|
2014-11-11 06:26:32 -05:00
|
|
|
self.comb += \
|
2014-12-03 09:29:01 -05:00
|
|
|
If(cont.source.stb & (cont.source.charisk == 0b0001),
|
|
|
|
det.eq(cont.source.data)
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# descrambler
|
2014-12-03 09:29:01 -05:00
|
|
|
scrambler = SATAScrambler(link_layout(32))
|
|
|
|
self.submodules += scrambler
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# check CRC
|
2014-12-03 09:29:01 -05:00
|
|
|
crc = SATACRCChecker(link_layout(32))
|
|
|
|
self.submodules += crc
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-05 14:26:09 -05:00
|
|
|
sop = Signal()
|
|
|
|
self.sync += \
|
|
|
|
If(fsm.ongoing("RDY"),
|
|
|
|
sop.eq(1)
|
|
|
|
).Elif(scrambler.sink.stb & scrambler.sink.ack,
|
|
|
|
sop.eq(0)
|
|
|
|
)
|
|
|
|
|
2014-12-05 15:27:26 -05:00
|
|
|
# small fifo to manage HOLD
|
|
|
|
self.submodules.fifo = SyncFIFO(link_layout(32), 32)
|
|
|
|
|
2014-11-11 06:26:32 -05:00
|
|
|
# graph
|
2014-12-05 14:26:09 -05:00
|
|
|
self.sync += \
|
2014-12-03 09:29:01 -05:00
|
|
|
If(fsm.ongoing("COPY") & (det == 0),
|
|
|
|
scrambler.sink.stb.eq(cont.source.stb & (cont.source.charisk == 0)),
|
|
|
|
scrambler.sink.d.eq(cont.source.data),
|
2014-12-05 14:26:09 -05:00
|
|
|
).Else(
|
|
|
|
scrambler.sink.stb.eq(0)
|
|
|
|
)
|
|
|
|
self.comb += [
|
|
|
|
scrambler.sink.sop.eq(sop),
|
|
|
|
scrambler.sink.eop.eq(det == primitives["EOF"]),
|
2014-12-03 09:29:01 -05:00
|
|
|
cont.source.ack.eq(1),
|
|
|
|
Record.connect(scrambler.source, crc.sink),
|
2014-12-05 15:27:26 -05:00
|
|
|
Record.connect(crc.source, self.fifo.sink),
|
|
|
|
Record.connect(self.fifo.source, self.source)
|
2014-11-11 06:26:32 -05:00
|
|
|
]
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
# FSM
|
2014-11-04 11:35:46 -05:00
|
|
|
fsm.act("IDLE",
|
2014-12-05 14:26:09 -05:00
|
|
|
scrambler.reset.eq(1),
|
2014-12-03 09:29:01 -05:00
|
|
|
If(det == primitives["X_RDY"],
|
|
|
|
NextState("RDY")
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
2014-11-04 11:35:46 -05:00
|
|
|
)
|
2014-12-03 09:29:01 -05:00
|
|
|
fsm.act("RDY",
|
|
|
|
insert.eq(primitives["R_RDY"]),
|
|
|
|
If(det == primitives["SOF"],
|
|
|
|
NextState("COPY")
|
2014-11-11 12:47:34 -05:00
|
|
|
)
|
2014-11-04 11:35:46 -05:00
|
|
|
)
|
2014-12-03 09:29:01 -05:00
|
|
|
fsm.act("COPY",
|
2014-12-05 14:26:09 -05:00
|
|
|
insert.eq(primitives["R_IP"]),
|
2014-12-03 09:29:01 -05:00
|
|
|
If(det == primitives["HOLD"],
|
|
|
|
insert.eq(primitives["HOLDA"])
|
|
|
|
).Elif(det == primitives["EOF"],
|
|
|
|
NextState("WTRM")
|
2014-12-05 15:27:26 -05:00
|
|
|
).Elif(self.fifo.fifo.level > 8,
|
|
|
|
insert.eq(primitives["HOLD"])
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
2014-11-04 11:35:46 -05:00
|
|
|
)
|
2014-12-03 09:29:01 -05:00
|
|
|
fsm.act("EOF",
|
|
|
|
If(det == primitives["WTRM"],
|
|
|
|
NextState("WTRM")
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
2014-11-04 11:35:46 -05:00
|
|
|
)
|
2014-12-03 09:29:01 -05:00
|
|
|
fsm.act("WTRM",
|
|
|
|
insert.eq(primitives["R_OK"]),
|
|
|
|
If(det == primitives["SYNC"],
|
2014-11-04 11:35:46 -05:00
|
|
|
NextState("IDLE")
|
2014-11-11 06:26:32 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-03 09:29:01 -05:00
|
|
|
# to TX
|
|
|
|
self.comb += [
|
|
|
|
self.to_tx.idle.eq(fsm.ongoing("IDLE")),
|
|
|
|
self.to_tx.insert.eq(insert),
|
|
|
|
self.to_tx.det.eq(det)
|
|
|
|
]
|
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATALink(Module):
|
2014-12-03 09:29:01 -05:00
|
|
|
def __init__(self, phy):
|
2014-12-12 08:35:48 -05:00
|
|
|
self.submodules.tx = SATALinkTX(phy)
|
|
|
|
self.submodules.rx = SATALinkRX(phy)
|
2014-12-03 09:29:01 -05:00
|
|
|
self.comb += Record.connect(self.rx.to_tx, self.tx.from_rx)
|
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|