2014-12-14 04:45:26 -05:00
|
|
|
from lib.sata.common import *
|
2014-12-03 10:32:55 -05:00
|
|
|
|
2014-12-15 07:38:50 -05:00
|
|
|
def _get_item(obj, name, width):
|
|
|
|
if "_lsb" in name:
|
|
|
|
item = getattr(obj, name.replace("_lsb", ""))[:width]
|
|
|
|
elif "_msb" in name:
|
|
|
|
item = getattr(obj, name.replace("_msb", ""))[width:2*width]
|
|
|
|
else:
|
|
|
|
item = getattr(obj, name)
|
|
|
|
return item
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def _encode_cmd(obj, description, signal):
|
2014-12-11 14:19:14 -05:00
|
|
|
r = []
|
2014-12-14 04:52:56 -05:00
|
|
|
for k, v in sorted(description.items()):
|
2014-12-11 18:56:29 -05:00
|
|
|
start = v.dword*32 + v.offset
|
2014-12-11 14:19:14 -05:00
|
|
|
end = start + v.width
|
2014-12-15 07:38:50 -05:00
|
|
|
item = _get_item(obj, k, v.width)
|
2014-12-11 18:56:29 -05:00
|
|
|
r.append(signal[start:end].eq(item))
|
2014-12-11 14:19:14 -05:00
|
|
|
return r
|
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATATransportTX(Module):
|
2014-12-11 14:19:14 -05:00
|
|
|
def __init__(self, link):
|
2014-12-14 04:52:56 -05:00
|
|
|
self.sink = sink = Sink(transport_tx_description(32))
|
2014-12-11 14:19:14 -05:00
|
|
|
|
|
|
|
###
|
|
|
|
|
2014-12-12 12:16:30 -05:00
|
|
|
cmd_ndwords = max(fis_reg_h2d_cmd_len, fis_data_cmd_len)
|
2014-12-11 14:19:14 -05:00
|
|
|
encoded_cmd = Signal(cmd_ndwords*32)
|
|
|
|
|
2014-12-15 13:33:38 -05:00
|
|
|
counter = Counter(max=cmd_ndwords+1)
|
|
|
|
self.submodules += counter
|
2014-12-11 14:19:14 -05:00
|
|
|
|
2014-12-15 13:33:38 -05:00
|
|
|
cmd_len = Signal(counter.width)
|
2014-12-11 14:19:14 -05:00
|
|
|
cmd_with_data = Signal()
|
|
|
|
|
|
|
|
cmd_send = Signal()
|
|
|
|
data_send = Signal()
|
|
|
|
cmd_done = Signal()
|
|
|
|
|
2014-12-11 18:56:29 -05:00
|
|
|
def test_type(name):
|
2014-12-12 08:31:00 -05:00
|
|
|
return sink.type == fis_types[name]
|
2014-12-11 18:56:29 -05:00
|
|
|
|
2014-12-11 14:19:14 -05:00
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
|
|
|
|
fsm.act("IDLE",
|
2014-12-15 13:33:38 -05:00
|
|
|
counter.reset.eq(1),
|
2014-12-12 08:31:00 -05:00
|
|
|
If(sink.stb & sink.sop,
|
2014-12-11 18:56:29 -05:00
|
|
|
If(test_type("REG_H2D"),
|
2014-12-11 14:19:14 -05:00
|
|
|
NextState("SEND_REG_H2D_CMD")
|
2014-12-11 18:56:29 -05:00
|
|
|
).Elif(test_type("DATA"),
|
|
|
|
NextState("SEND_DATA_CMD")
|
2014-12-11 14:19:14 -05:00
|
|
|
).Else(
|
2014-12-12 08:31:00 -05:00
|
|
|
sink.ack.eq(1)
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
2014-12-12 08:31:00 -05:00
|
|
|
).Else(
|
2014-12-15 07:26:53 -05:00
|
|
|
sink.ack.eq(1)
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_REG_H2D_CMD",
|
2014-12-12 08:31:00 -05:00
|
|
|
_encode_cmd(sink, fis_reg_h2d_layout, encoded_cmd),
|
2014-12-12 19:18:08 -05:00
|
|
|
cmd_len.eq(fis_reg_h2d_cmd_len-1),
|
2014-12-14 15:03:14 -05:00
|
|
|
cmd_send.eq(1),
|
2014-12-11 18:56:29 -05:00
|
|
|
If(cmd_done,
|
2014-12-12 08:31:00 -05:00
|
|
|
sink.ack.eq(1),
|
|
|
|
NextState("IDLE")
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_DATA_CMD",
|
2014-12-12 08:31:00 -05:00
|
|
|
_encode_cmd(sink, fis_data_layout, encoded_cmd),
|
2014-12-12 19:18:08 -05:00
|
|
|
cmd_len.eq(fis_data_cmd_len-1),
|
2014-12-11 18:56:29 -05:00
|
|
|
cmd_with_data.eq(1),
|
2014-12-14 15:03:14 -05:00
|
|
|
cmd_send.eq(1),
|
2014-12-11 14:19:14 -05:00
|
|
|
If(cmd_done,
|
2014-12-11 18:56:29 -05:00
|
|
|
NextState("SEND_DATA")
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_DATA",
|
|
|
|
data_send.eq(1),
|
2014-12-12 08:31:00 -05:00
|
|
|
sink.ack.eq(link.sink.ack),
|
|
|
|
If(sink.stb & sink.eop & sink.ack,
|
|
|
|
NextState("IDLE")
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
|
|
|
)
|
2014-12-12 08:31:00 -05:00
|
|
|
|
2014-12-11 14:19:14 -05:00
|
|
|
cmd_cases = {}
|
|
|
|
for i in range(cmd_ndwords):
|
|
|
|
cmd_cases[i] = [link.sink.d.eq(encoded_cmd[32*i:32*(i+1)])]
|
|
|
|
|
|
|
|
self.comb += \
|
|
|
|
If(cmd_send,
|
2014-12-14 15:03:14 -05:00
|
|
|
link.sink.stb.eq(sink.stb),
|
2014-12-15 13:33:38 -05:00
|
|
|
link.sink.sop.eq(counter.value == 0),
|
|
|
|
link.sink.eop.eq((counter.value == cmd_len) & ~cmd_with_data),
|
|
|
|
Case(counter.value, cmd_cases),
|
|
|
|
counter.ce.eq(sink.stb & link.sink.ack),
|
|
|
|
cmd_done.eq((counter.value == cmd_len) & link.sink.stb & link.sink.ack)
|
2014-12-11 14:19:14 -05:00
|
|
|
).Elif(data_send,
|
2014-12-12 08:31:00 -05:00
|
|
|
link.sink.stb.eq(sink.stb),
|
2014-12-11 14:19:14 -05:00
|
|
|
link.sink.sop.eq(0),
|
2014-12-12 08:31:00 -05:00
|
|
|
link.sink.eop.eq(sink.eop),
|
|
|
|
link.sink.d.eq(sink.data),
|
2014-12-11 14:19:14 -05:00
|
|
|
)
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def _decode_cmd(signal, description, obj):
|
2014-12-11 15:56:56 -05:00
|
|
|
r = []
|
2014-12-14 04:52:56 -05:00
|
|
|
for k, v in sorted(description.items()):
|
2014-12-11 18:56:29 -05:00
|
|
|
start = v.dword*32+v.offset
|
2014-12-11 15:56:56 -05:00
|
|
|
end = start+v.width
|
2014-12-15 07:38:50 -05:00
|
|
|
item = _get_item(obj, k, v.width)
|
2014-12-11 18:56:29 -05:00
|
|
|
r.append(item.eq(signal[start:end]))
|
2014-12-11 15:56:56 -05:00
|
|
|
return r
|
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATATransportRX(Module):
|
2014-12-11 14:19:14 -05:00
|
|
|
def __init__(self, link):
|
2014-12-14 04:52:56 -05:00
|
|
|
self.source = source = Source(transport_rx_description(32))
|
2014-12-03 10:32:55 -05:00
|
|
|
|
2014-12-11 15:56:56 -05:00
|
|
|
###
|
|
|
|
|
2014-12-12 12:16:30 -05:00
|
|
|
cmd_ndwords = max(fis_reg_d2h_cmd_len, fis_dma_activate_d2h_cmd_len, fis_data_cmd_len)
|
2014-12-11 15:56:56 -05:00
|
|
|
encoded_cmd = Signal(cmd_ndwords*32)
|
|
|
|
|
2014-12-15 13:33:38 -05:00
|
|
|
counter = Counter(max=cmd_ndwords+1)
|
|
|
|
self.submodules += counter
|
2014-12-11 15:56:56 -05:00
|
|
|
|
2014-12-15 13:33:38 -05:00
|
|
|
cmd_len = Signal(counter.width)
|
2014-12-11 15:56:56 -05:00
|
|
|
|
|
|
|
cmd_receive = Signal()
|
|
|
|
data_receive = Signal()
|
|
|
|
cmd_done = Signal()
|
|
|
|
data_done = Signal()
|
|
|
|
|
|
|
|
def test_type(name):
|
|
|
|
return link.source.d[:8] == fis_types[name]
|
|
|
|
|
2014-12-11 18:56:29 -05:00
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
|
2014-12-14 06:59:02 -05:00
|
|
|
data_sop = Signal()
|
|
|
|
|
2014-12-11 15:56:56 -05:00
|
|
|
fsm.act("IDLE",
|
2014-12-15 13:33:38 -05:00
|
|
|
counter.reset.eq(1),
|
2014-12-11 15:56:56 -05:00
|
|
|
If(link.source.stb & link.source.sop,
|
2014-12-11 18:56:29 -05:00
|
|
|
If(test_type("REG_D2H"),
|
2014-12-11 15:56:56 -05:00
|
|
|
NextState("RECEIVE_REG_D2H_CMD")
|
|
|
|
).Elif(test_type("DMA_ACTIVATE_D2H"),
|
|
|
|
NextState("RECEIVE_DMA_ACTIVATE_D2H_CMD")
|
|
|
|
).Elif(test_type("DATA"),
|
|
|
|
NextState("RECEIVE_DATA_CMD"),
|
|
|
|
).Else(
|
|
|
|
link.source.ack.eq(1)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
link.source.ack.eq(1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_REG_D2H_CMD",
|
2014-12-12 19:18:08 -05:00
|
|
|
cmd_len.eq(fis_reg_d2h_cmd_len-1),
|
2014-12-11 15:56:56 -05:00
|
|
|
cmd_receive.eq(1),
|
|
|
|
If(cmd_done,
|
|
|
|
NextState("PRESENT_REG_D2H_CMD")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("PRESENT_REG_D2H_CMD",
|
2014-12-12 08:31:00 -05:00
|
|
|
source.stb.eq(1),
|
2014-12-14 06:59:02 -05:00
|
|
|
source.sop.eq(1),
|
|
|
|
source.eop.eq(1),
|
2014-12-12 08:31:00 -05:00
|
|
|
_decode_cmd(encoded_cmd, fis_reg_d2h_layout, source),
|
2014-12-15 07:26:53 -05:00
|
|
|
If(source.stb & source.ack,
|
2014-12-11 15:56:56 -05:00
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_DMA_ACTIVATE_D2H_CMD",
|
2014-12-12 19:18:08 -05:00
|
|
|
cmd_len.eq(fis_dma_activate_d2h_cmd_len-1),
|
2014-12-11 15:56:56 -05:00
|
|
|
cmd_receive.eq(1),
|
|
|
|
If(cmd_done,
|
|
|
|
NextState("PRESENT_DMA_ACTIVATE_D2H_CMD")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("PRESENT_DMA_ACTIVATE_D2H_CMD",
|
2014-12-12 08:31:00 -05:00
|
|
|
source.stb.eq(1),
|
2014-12-14 06:59:02 -05:00
|
|
|
source.sop.eq(1),
|
|
|
|
source.eop.eq(1),
|
2014-12-12 08:31:00 -05:00
|
|
|
_decode_cmd(encoded_cmd, fis_dma_activate_d2h_layout, source),
|
2014-12-15 07:26:53 -05:00
|
|
|
If(source.stb & source.ack,
|
2014-12-11 15:56:56 -05:00
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_DATA_CMD",
|
2014-12-12 19:18:08 -05:00
|
|
|
cmd_len.eq(fis_data_cmd_len-1),
|
2014-12-11 15:56:56 -05:00
|
|
|
cmd_receive.eq(1),
|
|
|
|
If(cmd_done,
|
2014-12-12 08:31:00 -05:00
|
|
|
NextState("PRESENT_DATA")
|
2014-12-11 15:56:56 -05:00
|
|
|
)
|
|
|
|
)
|
2014-12-12 08:31:00 -05:00
|
|
|
fsm.act("PRESENT_DATA",
|
2014-12-11 15:56:56 -05:00
|
|
|
data_receive.eq(1),
|
2014-12-12 08:31:00 -05:00
|
|
|
source.stb.eq(link.source.stb),
|
|
|
|
_decode_cmd(encoded_cmd, fis_data_layout, source),
|
2014-12-14 06:59:02 -05:00
|
|
|
source.sop.eq(data_sop),
|
2014-12-12 08:31:00 -05:00
|
|
|
source.eop.eq(link.source.eop),
|
2014-12-12 12:16:30 -05:00
|
|
|
source.data.eq(link.source.d),
|
2014-12-12 08:31:00 -05:00
|
|
|
If(source.stb & source.eop & source.ack,
|
2014-12-11 15:56:56 -05:00
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-14 06:59:02 -05:00
|
|
|
self.sync += \
|
|
|
|
If(fsm.ongoing("RECEIVE_DATA_CMD"),
|
|
|
|
data_sop.eq(1)
|
|
|
|
).Elif(fsm.ongoing("PRESENT_DATA"),
|
|
|
|
If(source.stb & source.ack,
|
|
|
|
data_sop.eq(0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-11 15:56:56 -05:00
|
|
|
cmd_cases = {}
|
|
|
|
for i in range(cmd_ndwords):
|
2014-12-11 18:56:29 -05:00
|
|
|
cmd_cases[i] = [encoded_cmd[32*i:32*(i+1)].eq(link.source.d)]
|
2014-12-11 15:56:56 -05:00
|
|
|
|
2014-12-12 16:26:04 -05:00
|
|
|
self.comb += \
|
2014-12-15 13:33:38 -05:00
|
|
|
If(cmd_receive & link.source.stb,
|
|
|
|
counter.ce.eq(1)
|
2014-12-11 15:56:56 -05:00
|
|
|
)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.sync += \
|
|
|
|
If(cmd_receive,
|
2014-12-15 13:33:38 -05:00
|
|
|
Case(counter.value, cmd_cases),
|
2014-12-12 16:26:04 -05:00
|
|
|
)
|
2014-12-15 13:33:38 -05:00
|
|
|
self.comb += cmd_done.eq((counter.value == cmd_len) & link.source.ack)
|
2014-12-12 08:31:00 -05:00
|
|
|
self.comb += link.source.ack.eq(cmd_receive | (data_receive & source.ack))
|
2014-12-11 15:56:56 -05:00
|
|
|
|
2014-12-12 08:35:48 -05:00
|
|
|
class SATATransport(Module):
|
2014-12-11 14:19:14 -05:00
|
|
|
def __init__(self, link):
|
2014-12-18 19:35:18 -05:00
|
|
|
self.tx = SATATransportTX(link)
|
|
|
|
self.rx = SATATransportRX(link)
|
2014-12-12 08:31:00 -05:00
|
|
|
self.sink, self.source = self.tx.sink, self.rx.source
|