2015-01-06 10:48:19 -05:00
|
|
|
import math
|
|
|
|
|
2014-11-03 12:54:41 -05:00
|
|
|
from migen.fhdl.std import *
|
2015-01-22 10:02:41 -05:00
|
|
|
from migen.fhdl.decorators import ModuleDecorator
|
2014-12-18 12:02:35 -05:00
|
|
|
from migen.genlib.resetsync import *
|
|
|
|
from migen.genlib.fsm import *
|
2014-11-11 04:19:24 -05:00
|
|
|
from migen.genlib.record import *
|
2015-03-01 10:45:50 -05:00
|
|
|
from migen.genlib.misc import chooser, optree, Counter, Timeout
|
2015-01-16 17:52:41 -05:00
|
|
|
from migen.genlib.cdc import *
|
2014-12-03 10:32:55 -05:00
|
|
|
from migen.flow.actor import *
|
2015-01-16 17:52:41 -05:00
|
|
|
from migen.flow.plumbing import Multiplexer, Demultiplexer
|
2015-01-06 11:03:27 -05:00
|
|
|
from migen.flow.plumbing import Buffer
|
2014-12-18 12:02:35 -05:00
|
|
|
from migen.actorlib.fifo import *
|
2015-01-16 17:52:41 -05:00
|
|
|
from migen.actorlib.structuring import Pipeline, Converter
|
2014-11-03 12:54:41 -05:00
|
|
|
|
2015-01-22 11:15:12 -05:00
|
|
|
bitrates = {
|
|
|
|
"sata_gen3" : 6.0,
|
|
|
|
"sata_gen2" : 3.0,
|
|
|
|
"sata_gen1" : 1.5,
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:52:05 -05:00
|
|
|
frequencies = {
|
2015-01-22 11:15:12 -05:00
|
|
|
"sata_gen3" : 150.0,
|
|
|
|
"sata_gen2" : 75.0,
|
|
|
|
"sata_gen1" : 37.5,
|
2015-01-19 11:52:05 -05:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:15:12 -05:00
|
|
|
# PHY / Link Layers
|
2014-11-11 04:19:24 -05:00
|
|
|
primitives = {
|
|
|
|
"ALIGN" : 0x7B4A4ABC,
|
2014-12-03 05:12:26 -05:00
|
|
|
"CONT" : 0X9999AA7C,
|
2014-11-11 04:19:24 -05:00
|
|
|
"SYNC" : 0xB5B5957C,
|
|
|
|
"R_RDY" : 0x4A4A957C,
|
|
|
|
"R_OK" : 0x3535B57C,
|
|
|
|
"R_ERR" : 0x5656B57C,
|
|
|
|
"R_IP" : 0X5555B57C,
|
|
|
|
"X_RDY" : 0x5757B57C,
|
|
|
|
"CONT" : 0x9999AA7C,
|
|
|
|
"WTRM" : 0x5858B57C,
|
|
|
|
"SOF" : 0x3737B57C,
|
|
|
|
"EOF" : 0xD5D5B57C,
|
|
|
|
"HOLD" : 0xD5D5AA7C,
|
2014-12-02 15:34:16 -05:00
|
|
|
"HOLDA" : 0X9595AA7C
|
2014-11-11 04:19:24 -05:00
|
|
|
}
|
|
|
|
|
2014-12-03 05:12:26 -05:00
|
|
|
def is_primitive(dword):
|
|
|
|
for k, v in primitives.items():
|
|
|
|
if dword == v:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def decode_primitive(dword):
|
|
|
|
for k, v in primitives.items():
|
|
|
|
if dword == v:
|
|
|
|
return k
|
|
|
|
return ""
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def phy_description(dw):
|
2014-11-03 12:54:41 -05:00
|
|
|
layout = [
|
2014-11-11 08:54:54 -05:00
|
|
|
("data", dw),
|
|
|
|
("charisk", dw//8),
|
2014-11-03 12:54:41 -05:00
|
|
|
]
|
2014-12-02 13:24:46 -05:00
|
|
|
return EndpointDescription(layout, packetized=False)
|
2014-11-04 11:35:46 -05:00
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def link_description(dw):
|
2014-11-04 11:35:46 -05:00
|
|
|
layout = [
|
2014-11-11 08:54:54 -05:00
|
|
|
("d", dw),
|
|
|
|
("error", 1)
|
2014-11-04 11:35:46 -05:00
|
|
|
]
|
2014-12-02 13:24:46 -05:00
|
|
|
return EndpointDescription(layout, packetized=True)
|
2014-12-03 10:32:55 -05:00
|
|
|
|
2014-12-14 04:45:26 -05:00
|
|
|
# Transport Layer
|
2015-01-06 10:48:19 -05:00
|
|
|
fis_max_dwords = 2048
|
|
|
|
|
2014-12-14 04:45:26 -05:00
|
|
|
fis_types = {
|
|
|
|
"REG_H2D": 0x27,
|
|
|
|
"REG_D2H": 0x34,
|
|
|
|
"DMA_ACTIVATE_D2H": 0x39,
|
2015-01-16 16:49:34 -05:00
|
|
|
"PIO_SETUP_D2H": 0x5F,
|
2014-12-14 04:45:26 -05:00
|
|
|
"DATA": 0x46
|
|
|
|
}
|
|
|
|
|
|
|
|
class FISField():
|
|
|
|
def __init__(self, dword, offset, width):
|
|
|
|
self.dword = dword
|
|
|
|
self.offset = offset
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
fis_reg_h2d_cmd_len = 5
|
|
|
|
fis_reg_h2d_layout = {
|
|
|
|
"type": FISField(0, 0, 8),
|
|
|
|
"pm_port": FISField(0, 8, 4),
|
|
|
|
"c": FISField(0, 15, 1),
|
|
|
|
"command": FISField(0, 16, 8),
|
|
|
|
"features_lsb": FISField(0, 24, 8),
|
|
|
|
|
|
|
|
"lba_lsb": FISField(1, 0, 24),
|
|
|
|
"device": FISField(1, 24, 8),
|
|
|
|
|
|
|
|
"lba_msb": FISField(2, 0, 24),
|
|
|
|
"features_msb": FISField(2, 24, 8),
|
|
|
|
|
|
|
|
"count": FISField(3, 0, 16),
|
|
|
|
"icc": FISField(3, 16, 8),
|
|
|
|
"control": FISField(3, 24, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
fis_reg_d2h_cmd_len = 5
|
|
|
|
fis_reg_d2h_layout = {
|
|
|
|
"type": FISField(0, 0, 8),
|
|
|
|
"pm_port": FISField(0, 8, 4),
|
|
|
|
"i": FISField(0, 14, 1),
|
|
|
|
"status": FISField(0, 16, 8),
|
|
|
|
"error": FISField(0, 24, 8),
|
|
|
|
|
|
|
|
"lba_lsb": FISField(1, 0, 24),
|
|
|
|
"device": FISField(1, 24, 8),
|
|
|
|
|
|
|
|
"lba_msb": FISField(2, 0, 24),
|
|
|
|
|
|
|
|
"count": FISField(3, 0, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
fis_dma_activate_d2h_cmd_len = 1
|
|
|
|
fis_dma_activate_d2h_layout = {
|
|
|
|
"type": FISField(0, 0, 8),
|
|
|
|
"pm_port": FISField(0, 8, 4)
|
|
|
|
}
|
|
|
|
|
2015-01-16 16:49:34 -05:00
|
|
|
fis_pio_setup_d2h_cmd_len = 5
|
|
|
|
fis_pio_setup_d2h_layout = {
|
|
|
|
"type": FISField(0, 0, 8),
|
|
|
|
"pm_port": FISField(0, 8, 4),
|
|
|
|
"d": FISField(0, 13, 1),
|
|
|
|
"i": FISField(0, 14, 1),
|
|
|
|
"status": FISField(0, 16, 8),
|
|
|
|
"error": FISField(0, 24, 8),
|
|
|
|
|
|
|
|
"lba_lsb": FISField(1, 0, 24),
|
|
|
|
|
|
|
|
"lba_msb": FISField(2, 0, 24),
|
|
|
|
|
|
|
|
"count": FISField(3, 0, 16),
|
|
|
|
|
|
|
|
"transfer_count": FISField(4, 0, 16),
|
|
|
|
}
|
|
|
|
|
2014-12-14 04:45:26 -05:00
|
|
|
fis_data_cmd_len = 1
|
|
|
|
fis_data_layout = {
|
|
|
|
"type": FISField(0, 0, 8)
|
|
|
|
}
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def transport_tx_description(dw):
|
2014-12-03 10:32:55 -05:00
|
|
|
layout = [
|
2014-12-11 14:19:14 -05:00
|
|
|
("type", 8),
|
|
|
|
("pm_port", 4),
|
|
|
|
("c", 1),
|
|
|
|
("command", 8),
|
|
|
|
("features", 16),
|
|
|
|
("lba", 48),
|
|
|
|
("device", 8),
|
|
|
|
("count", 16),
|
|
|
|
("icc", 8),
|
|
|
|
("control", 8),
|
2014-12-12 08:31:00 -05:00
|
|
|
("data", dw)
|
2014-12-11 14:19:14 -05:00
|
|
|
]
|
2014-12-12 08:31:00 -05:00
|
|
|
return EndpointDescription(layout, packetized=True)
|
2014-12-11 14:19:14 -05:00
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def transport_rx_description(dw):
|
2014-12-11 14:19:14 -05:00
|
|
|
layout = [
|
|
|
|
("type", 8),
|
|
|
|
("pm_port", 4),
|
2015-01-16 16:49:34 -05:00
|
|
|
("r", 1),
|
|
|
|
("d", 1),
|
2014-12-11 14:19:14 -05:00
|
|
|
("i", 1),
|
|
|
|
("status", 8),
|
2015-02-12 14:45:15 -05:00
|
|
|
("errors", 8),
|
2014-12-11 14:19:14 -05:00
|
|
|
("lba", 48),
|
|
|
|
("device", 8),
|
|
|
|
("count", 16),
|
2015-01-16 16:49:34 -05:00
|
|
|
("transfer_count", 16),
|
2014-12-25 07:11:22 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", 1)
|
2014-12-12 12:16:30 -05:00
|
|
|
]
|
|
|
|
return EndpointDescription(layout, packetized=True)
|
|
|
|
|
2014-12-14 04:57:16 -05:00
|
|
|
# Command Layer
|
2014-12-14 04:45:26 -05:00
|
|
|
regs = {
|
|
|
|
"WRITE_DMA_EXT" : 0x35,
|
2015-01-16 16:49:34 -05:00
|
|
|
"READ_DMA_EXT" : 0x25,
|
|
|
|
"IDENTIFY_DEVICE" : 0xEC
|
2014-12-14 04:45:26 -05:00
|
|
|
}
|
|
|
|
|
2015-01-20 11:14:01 -05:00
|
|
|
reg_d2h_status = {
|
|
|
|
"bsy" : 7,
|
|
|
|
"drdy" : 6,
|
|
|
|
"df" : 5,
|
|
|
|
"se" : 5,
|
|
|
|
"dwe" : 4,
|
|
|
|
"drq" : 3,
|
|
|
|
"ae" : 2,
|
|
|
|
"sns" : 1,
|
|
|
|
"cc" : 0,
|
|
|
|
"err" : 0
|
|
|
|
}
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def command_tx_description(dw):
|
2014-12-12 12:16:30 -05:00
|
|
|
layout = [
|
|
|
|
("write", 1),
|
|
|
|
("read", 1),
|
2015-01-16 16:49:34 -05:00
|
|
|
("identify", 1),
|
2014-12-15 07:26:53 -05:00
|
|
|
("sector", 48),
|
2015-01-06 10:48:19 -05:00
|
|
|
("count", 16),
|
2014-12-12 12:16:30 -05:00
|
|
|
("data", dw)
|
|
|
|
]
|
|
|
|
return EndpointDescription(layout, packetized=True)
|
|
|
|
|
2014-12-14 04:52:56 -05:00
|
|
|
def command_rx_description(dw):
|
2014-12-12 12:16:30 -05:00
|
|
|
layout = [
|
|
|
|
("write", 1),
|
|
|
|
("read", 1),
|
2015-01-16 16:49:34 -05:00
|
|
|
("identify", 1),
|
2015-01-06 10:48:19 -05:00
|
|
|
("last", 1),
|
2014-12-12 12:16:30 -05:00
|
|
|
("failed", 1),
|
2014-12-12 08:31:00 -05:00
|
|
|
("data", dw)
|
2014-12-03 10:32:55 -05:00
|
|
|
]
|
|
|
|
return EndpointDescription(layout, packetized=True)
|
2014-12-15 09:31:08 -05:00
|
|
|
|
|
|
|
def command_rx_cmd_description(dw):
|
|
|
|
layout = [
|
|
|
|
("write", 1),
|
|
|
|
("read", 1),
|
2015-01-16 16:49:34 -05:00
|
|
|
("identify", 1),
|
2015-01-06 10:48:19 -05:00
|
|
|
("last", 1),
|
2015-01-16 16:49:34 -05:00
|
|
|
("failed", 1)
|
2014-12-15 09:31:08 -05:00
|
|
|
]
|
|
|
|
return EndpointDescription(layout, packetized=False)
|
|
|
|
|
|
|
|
def command_rx_data_description(dw):
|
|
|
|
layout = [
|
|
|
|
("data", dw)
|
|
|
|
]
|
2014-12-15 13:33:38 -05:00
|
|
|
return EndpointDescription(layout, packetized=True)
|
|
|
|
|
2015-01-06 10:48:19 -05:00
|
|
|
# HDD
|
|
|
|
logical_sector_size = 512 # constant since all HDDs use this
|
|
|
|
|
|
|
|
def dwords2sectors(n):
|
|
|
|
return math.ceil(n*4/logical_sector_size)
|
|
|
|
|
|
|
|
def sectors2dwords(n):
|
|
|
|
return n*logical_sector_size//4
|
|
|
|
|
|
|
|
# Generic modules
|
2015-01-22 10:02:41 -05:00
|
|
|
class BufferizeEndpoints(ModuleDecorator):
|
2015-01-21 06:01:28 -05:00
|
|
|
def __init__(self, submodule, *args):
|
2015-01-22 10:02:41 -05:00
|
|
|
ModuleDecorator.__init__(self, submodule)
|
2015-01-08 16:59:31 -05:00
|
|
|
|
2015-01-21 06:01:28 -05:00
|
|
|
endpoints = get_endpoints(submodule)
|
2015-01-08 16:59:31 -05:00
|
|
|
sinks = {}
|
|
|
|
sources = {}
|
|
|
|
for name, endpoint in endpoints.items():
|
|
|
|
if name in args or len(args) == 0:
|
|
|
|
if isinstance(endpoint, Sink):
|
|
|
|
sinks.update({name : endpoint})
|
|
|
|
elif isinstance(endpoint, Source):
|
|
|
|
sources.update({name : endpoint})
|
|
|
|
|
|
|
|
# add buffer on sinks
|
|
|
|
for name, sink in sinks.items():
|
|
|
|
buf = Buffer(sink.description)
|
|
|
|
self.submodules += buf
|
|
|
|
setattr(self, name, buf.d)
|
|
|
|
self.comb += Record.connect(buf.q, sink)
|
|
|
|
|
|
|
|
# add buffer on sources
|
|
|
|
for name, source in sources.items():
|
|
|
|
buf = Buffer(source.description)
|
|
|
|
self.submodules += buf
|
|
|
|
self.comb += Record.connect(source, buf.d)
|
|
|
|
setattr(self, name, buf.q)
|
|
|
|
|
2015-01-21 17:11:38 -05:00
|
|
|
class EndpointPacketStatus(Module):
|
|
|
|
def __init__(self, endpoint):
|
|
|
|
self.start = Signal()
|
|
|
|
self.done = Signal()
|
|
|
|
self.ongoing = Signal()
|
|
|
|
|
|
|
|
ongoing = Signal()
|
|
|
|
self.comb += [
|
|
|
|
self.start.eq(endpoint.stb & endpoint.sop & endpoint.ack),
|
|
|
|
self.done.eq(endpoint.stb & endpoint.eop & endpoint.ack)
|
|
|
|
]
|
|
|
|
self.sync += \
|
|
|
|
If(self.start,
|
|
|
|
ongoing.eq(1)
|
|
|
|
).Elif(self.done,
|
|
|
|
ongoing.eq(0)
|
|
|
|
)
|
|
|
|
self.comb += self.ongoing.eq((self.start | ongoing) & ~self.done)
|
|
|
|
|
|
|
|
class PacketBuffer(Module):
|
|
|
|
def __init__(self, description, data_depth, cmd_depth=4, almost_full=None):
|
|
|
|
self.sink = sink = Sink(description)
|
|
|
|
self.source = source = Source(description)
|
|
|
|
|
|
|
|
###
|
|
|
|
sink_status = EndpointPacketStatus(self.sink)
|
|
|
|
source_status = EndpointPacketStatus(self.source)
|
|
|
|
self.submodules += sink_status, source_status
|
|
|
|
|
|
|
|
# store incoming packets
|
|
|
|
# cmds
|
|
|
|
def cmd_description():
|
|
|
|
layout = [("error", 1)]
|
|
|
|
return EndpointDescription(layout)
|
2015-01-22 10:02:41 -05:00
|
|
|
cmd_fifo = SyncFIFO(cmd_description(), cmd_depth)
|
|
|
|
self.submodules += cmd_fifo
|
2015-01-21 17:11:38 -05:00
|
|
|
self.comb += [
|
|
|
|
cmd_fifo.sink.stb.eq(sink_status.done),
|
|
|
|
cmd_fifo.sink.error.eq(sink.error)
|
|
|
|
]
|
|
|
|
|
|
|
|
# data
|
2015-01-22 10:02:41 -05:00
|
|
|
data_fifo = SyncFIFO(description, data_depth, buffered=True)
|
|
|
|
self.submodules += data_fifo
|
2015-01-21 17:11:38 -05:00
|
|
|
self.comb += [
|
|
|
|
Record.connect(self.sink, data_fifo.sink),
|
|
|
|
data_fifo.sink.stb.eq(self.sink.stb & cmd_fifo.sink.ack),
|
|
|
|
self.sink.ack.eq(data_fifo.sink.ack & cmd_fifo.sink.ack),
|
|
|
|
]
|
|
|
|
|
|
|
|
# output packets
|
|
|
|
self.fsm = fsm = FSM(reset_state="IDLE")
|
2015-01-22 10:02:41 -05:00
|
|
|
self.submodules += fsm
|
2015-01-21 17:11:38 -05:00
|
|
|
fsm.act("IDLE",
|
|
|
|
If(cmd_fifo.source.stb,
|
|
|
|
NextState("SEEK_SOP")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEEK_SOP",
|
|
|
|
If(~data_fifo.source.sop,
|
|
|
|
data_fifo.source.ack.eq(1)
|
|
|
|
).Else(
|
|
|
|
NextState("OUTPUT")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("OUTPUT",
|
|
|
|
Record.connect(data_fifo.source, self.source),
|
|
|
|
self.source.error.eq(cmd_fifo.source.error),
|
|
|
|
If(source_status.done,
|
|
|
|
cmd_fifo.source.ack.eq(1),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# compute almost full
|
|
|
|
if almost_full is not None:
|
|
|
|
self.almost_full = Signal()
|
|
|
|
self.comb += self.almost_full.eq(data_fifo.fifo.level > almost_full)
|