2014-12-15 10:44:12 -05:00
|
|
|
from lib.sata.common import *
|
2014-12-15 13:04:45 -05:00
|
|
|
from lib.sata.link.scrambler import Scrambler
|
2014-12-23 12:26:07 -05:00
|
|
|
from migen.bank.description import *
|
2014-12-15 13:04:45 -05:00
|
|
|
|
2014-12-23 12:26:07 -05:00
|
|
|
class SATABISTUnit(Module):
|
|
|
|
def __init__(self, sata_con):
|
|
|
|
sink = sata_con.source
|
|
|
|
source = sata_con.sink
|
2014-12-15 10:44:12 -05:00
|
|
|
|
|
|
|
self.start = Signal()
|
2014-12-23 13:44:39 -05:00
|
|
|
self.write_only = Signal()
|
|
|
|
self.read_only = Signal()
|
2014-12-15 10:44:12 -05:00
|
|
|
self.sector = Signal(48)
|
2014-12-15 13:48:22 -05:00
|
|
|
self.count = Signal(4)
|
2014-12-15 10:44:12 -05:00
|
|
|
self.done = Signal()
|
2014-12-15 13:04:45 -05:00
|
|
|
self.ctrl_errors = Signal(32)
|
|
|
|
self.data_errors = Signal(32)
|
2014-12-15 10:44:12 -05:00
|
|
|
|
2014-12-19 16:50:35 -05:00
|
|
|
self.counter = counter = Counter(bits_sign=32)
|
|
|
|
self.ctrl_error_counter = Counter(self.ctrl_errors, bits_sign=32)
|
|
|
|
self.data_error_counter = Counter(self.data_errors, bits_sign=32)
|
2014-12-15 13:04:45 -05:00
|
|
|
|
2014-12-19 16:50:35 -05:00
|
|
|
self.scrambler = scrambler = InsertReset(Scrambler())
|
2014-12-15 13:04:45 -05:00
|
|
|
self.comb += [
|
|
|
|
scrambler.reset.eq(counter.reset),
|
|
|
|
scrambler.ce.eq(counter.ce)
|
|
|
|
]
|
2014-12-15 10:44:12 -05:00
|
|
|
|
2014-12-19 16:50:35 -05:00
|
|
|
self.fsm = fsm = FSM(reset_state="IDLE")
|
2014-12-15 10:44:12 -05:00
|
|
|
fsm.act("IDLE",
|
|
|
|
self.done.eq(1),
|
2014-12-15 13:04:45 -05:00
|
|
|
counter.reset.eq(1),
|
2014-12-15 10:44:12 -05:00
|
|
|
If(self.start,
|
2014-12-22 19:39:41 -05:00
|
|
|
self.ctrl_error_counter.reset.eq(1),
|
|
|
|
self.data_error_counter.reset.eq(1),
|
2014-12-23 13:44:39 -05:00
|
|
|
If(self.read_only,
|
|
|
|
NextState("SEND_READ_CMD")
|
|
|
|
).Else(
|
|
|
|
NextState("SEND_WRITE_CMD_AND_DATA")
|
|
|
|
)
|
2014-12-15 10:44:12 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_WRITE_CMD_AND_DATA",
|
|
|
|
source.stb.eq(1),
|
2014-12-15 13:04:45 -05:00
|
|
|
source.sop.eq((counter.value == 0)),
|
2014-12-23 12:26:07 -05:00
|
|
|
source.eop.eq((counter.value == (sata_con.sector_size//4*self.count)-1)),
|
2014-12-15 10:44:12 -05:00
|
|
|
source.write.eq(1),
|
|
|
|
source.sector.eq(self.sector),
|
2014-12-15 13:48:22 -05:00
|
|
|
source.count.eq(self.count),
|
2014-12-15 13:04:45 -05:00
|
|
|
source.data.eq(scrambler.value),
|
|
|
|
counter.ce.eq(source.ack),
|
2014-12-15 10:44:12 -05:00
|
|
|
If(source.stb & source.eop & source.ack,
|
|
|
|
NextState("WAIT_WRITE_ACK")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_WRITE_ACK",
|
|
|
|
sink.ack.eq(1),
|
2014-12-15 13:04:45 -05:00
|
|
|
If(sink.stb,
|
|
|
|
If(~sink.write | ~sink.success | sink.failed,
|
2014-12-19 16:50:35 -05:00
|
|
|
self.ctrl_error_counter.ce.eq(1)
|
2014-12-15 13:04:45 -05:00
|
|
|
),
|
2014-12-23 13:44:39 -05:00
|
|
|
If(self.write_only,
|
|
|
|
NextState("IDLE")
|
|
|
|
).Else(
|
|
|
|
NextState("SEND_READ_CMD")
|
|
|
|
)
|
2014-12-15 10:44:12 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_READ_CMD",
|
|
|
|
source.stb.eq(1),
|
|
|
|
source.sop.eq(1),
|
|
|
|
source.eop.eq(1),
|
|
|
|
source.read.eq(1),
|
|
|
|
source.sector.eq(self.sector),
|
2014-12-15 13:48:22 -05:00
|
|
|
source.count.eq(self.count),
|
2014-12-15 10:44:12 -05:00
|
|
|
If(source.ack,
|
|
|
|
NextState("WAIT_READ_ACK")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_READ_ACK",
|
2014-12-15 13:04:45 -05:00
|
|
|
counter.reset.eq(1),
|
2014-12-15 10:44:12 -05:00
|
|
|
If(sink.stb & sink.read,
|
2014-12-15 13:04:45 -05:00
|
|
|
If(~sink.read | ~sink.success | sink.failed,
|
2014-12-19 16:50:35 -05:00
|
|
|
self.ctrl_error_counter.ce.eq(1)
|
2014-12-15 13:04:45 -05:00
|
|
|
),
|
2014-12-15 10:44:12 -05:00
|
|
|
NextState("RECEIVE_READ_DATA")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_READ_DATA",
|
|
|
|
sink.ack.eq(1),
|
2014-12-15 13:04:45 -05:00
|
|
|
If(sink.stb,
|
|
|
|
counter.ce.eq(1),
|
|
|
|
If(sink.data != scrambler.value,
|
2014-12-19 16:50:35 -05:00
|
|
|
self.data_error_counter.ce.eq(1)
|
2014-12-15 13:04:45 -05:00
|
|
|
),
|
|
|
|
If(sink.eop,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
2014-12-15 10:44:12 -05:00
|
|
|
)
|
|
|
|
)
|
2014-12-23 12:26:07 -05:00
|
|
|
|
|
|
|
class SATABIST(Module, AutoCSR):
|
|
|
|
def __init__(self, sata_con):
|
|
|
|
self._start = CSR()
|
|
|
|
self._start_sector = CSRStorage(48)
|
|
|
|
self._count = CSRStorage(4)
|
2014-12-23 13:44:39 -05:00
|
|
|
self._write_only = CSRStorage()
|
|
|
|
self._read_only = CSRStorage()
|
|
|
|
|
2014-12-23 12:26:07 -05:00
|
|
|
self._stop = CSRStorage()
|
|
|
|
|
|
|
|
self._sector = CSRStatus(48)
|
|
|
|
self._errors = CSRStatus(32)
|
|
|
|
|
|
|
|
start = self._start.r & self._start.re
|
|
|
|
start_sector = self._start_sector.storage
|
|
|
|
count = self._count.storage
|
|
|
|
stop = self._stop.storage
|
|
|
|
|
2014-12-23 13:44:39 -05:00
|
|
|
compute = Signal()
|
2014-12-23 12:26:07 -05:00
|
|
|
|
2014-12-23 13:44:39 -05:00
|
|
|
write_only = self._write_only.storage
|
|
|
|
read_only = self._read_only.storage
|
2014-12-23 12:26:07 -05:00
|
|
|
sector = self._sector.status
|
|
|
|
errors = self._errors.status
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
self.unit = SATABISTUnit(sata_con)
|
|
|
|
self.comb += [
|
2014-12-23 13:44:39 -05:00
|
|
|
self.unit.write_only.eq(write_only),
|
|
|
|
self.unit.read_only.eq(read_only),
|
2014-12-23 12:26:07 -05:00
|
|
|
self.unit.sector.eq(sector),
|
|
|
|
self.unit.count.eq(count)
|
|
|
|
]
|
|
|
|
|
|
|
|
self.fsm = fsm = FSM(reset_state="IDLE")
|
|
|
|
|
|
|
|
# FSM
|
|
|
|
fsm.act("IDLE",
|
|
|
|
If(start,
|
|
|
|
NextState("START")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("START",
|
|
|
|
self.unit.start.eq(1),
|
|
|
|
NextState("WAIT_DONE")
|
|
|
|
)
|
|
|
|
fsm.act("WAIT_DONE",
|
|
|
|
If(self.unit.done,
|
2014-12-23 13:44:39 -05:00
|
|
|
NextState("COMPUTE")
|
2014-12-23 12:26:07 -05:00
|
|
|
).Elif(stop,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
2014-12-23 13:44:39 -05:00
|
|
|
fsm.act("COMPUTE",
|
|
|
|
compute.eq(1),
|
2014-12-23 12:26:07 -05:00
|
|
|
NextState("START")
|
|
|
|
)
|
|
|
|
|
|
|
|
self.sync += [
|
|
|
|
If(start,
|
|
|
|
errors.eq(0),
|
|
|
|
sector.eq(start_sector)
|
2014-12-23 13:44:39 -05:00
|
|
|
).Elif(compute,
|
2014-12-23 12:26:07 -05:00
|
|
|
errors.eq(errors + self.unit.data_errors),
|
|
|
|
sector.eq(sector + count)
|
|
|
|
)
|
|
|
|
]
|