30 parameter READ_REG_LATENCY =
2,
// 0 if reg_rdata is available with reg_re/reg_addr, 2 with re/regen 31 // parameter READ_CT_LATENCY = 1, // 0 if reg_rdata is available with reg_re/reg_addr, 2 with re/regen 32 parameter READ_CT_LATENCY =
2,
// 0 if reg_rdata is available with reg_re/reg_addr, 2 with re/regen 33 parameter ADDRESS_BITS =
10 // number of memory address bits - now fixed. Low half - RO/RW/RWC,RW1 (2-cycle write), 2-nd just RW (single-cycle) 36 input hba_rst,
// @posedge mclk - when port is reset (even COMINIT)? 37 input mclk,
// for command/status 39 // Command pulses to execute states 40 input fetch_cmd,
// Enter p:FetchCmd, fetch command header (from the register memory, prefetch command FIS) 41 // wait for either fetch_cmd_busy == 0 or pCmdToIssue ==1 after fetch_cmd 42 input cfis_xmit,
// transmit command (wait for dma_ct_busy == 0) 43 input dx_xmit,
// send FIS header DWORD, (just 0x46), then forward DMA data 44 // transmit until error, 2048DWords or pDmaXferCnt 48 output reg done,
// for fetch_cmd - dma_start, for *_xmit - xmit_ok, xmit_err, syncesc_recv or xrdy_collision 53 // output dmaCntrZero, // DMA counter is zero - would be a duplicate to the one in receive module and dwords_sent output 54 // output reg fetch_cmd_busy, // does not include prefetching CT - now just use busy/done 56 // Should wait for xmit_ok? Timeout? Timeout will be handled by software, so just wait for OK or some error 57 input xmit_ok,
// FIS transmission acknowledged OK 61 output [
2:
0]
dx_err,
// bit 0 - syncesc_recv, 1 - R_ERR (was xmit_err), 2 - collision (valid @ xmit_err and later, reset by new command) 63 output [
15:
0]
ch_prdtl,
// Physical region descriptor table length (in entries, 0 is 0) 64 output ch_c,
// Clear busy upon R_OK for this FIS 65 output ch_b,
// Built-in self test command 66 output ch_r,
// reset - may need to send SYNC escape before this command 67 output ch_p,
// prefetchable - only used with non-zero PRDTL or ATAPI bit set 68 output ch_w,
// Write: system memory -> device 69 output ch_a,
// ATAPI: 1 means device should send PIO setup FIS for ATAPI command 70 output [
4:
0]
ch_cfl,
// length of the command FIS in DW, 0 means none. 0 and 1 - illegal, 71 // maximal is 16 (0x10) 72 output reg [
11:
0]
dwords_sent,
// number of DWORDs transmitted (up to 2048) 74 // register memory interface 79 // ahci_fis_receive interface 80 input [
31:
2]
xfer_cntr,
// transfer counter in words for both DMA (31 bit) and PIO (lower 15 bits), updated after decr_dwc 85 output dma_start,
// start processing command table, reset prdbc (next cycle after dma_ctba_ld, bits prdtl valid) 87 input dma_ct_busy,
// dma module is busy reading command table from the system memory 88 // issue dma_prd_start same time as dma_start if prefetch enabled, otherwise with cfis_xmit 89 output reg dma_prd_start,
// at or after cmd_start - enable reading PRD/data (if any) ch_prdtl should be valid, twice - OK 92 // reading out command table data from DMA module 93 output reg [
4:
0]
ct_addr,
// DWORD address 94 output [
1:
0]
ct_re,
// [0] - re, [1] - regen 97 // DMA (memory -> device) interface 98 input [
31:
0]
dma_out,
// 32-bit data from the DMA module, HBA -> device port 99 input dma_dav,
// at least one dword is ready to be read from DMA module 100 output dma_re,
// read dword from DMA module to the output register 103 // Data System memory or FIS -> device 104 output reg [
31:
0]
todev_data,
// 32-bit data from the system memory to HBA (dma data) 105 output reg [
1:
0]
todev_type,
// 0 - data, 1 - FIS head, 2 - FIS LAST) 111 // Add a possiblity to flush any data to FIFO if error was detected after data went there? 113 localparam CLB_OFFS32 =
'h200;
// # In the second half of the register space (0x800..0xbff - 1KB) 122 // for fis_data_valid - longer latency 123 // wire fis_out_w = !dma_en_r && fis_data_valid && todev_ready; 125 /// wire dma_re_w = dma_en_r && dma_dav && todev_ready && (!todev_full_r || !watch_prd_end_w); 135 reg [
4:
0]
cfis_acmd_left_r;
// number of DWORDS in CFIS or ACMD area of the command table left to be fetched from ahci_dma module BRAM 136 // For CFIS this register is set from ch_cmd_len_r, for ACMD - from the xfer_cntr input 137 // (stored in the ahci_fis_receive module) 140 // reg [31:7] ch_ctba_r; 142 wire reg_re_w;
// combined conditions to read register memory 143 /// wire reg_stb = reg_re_r[READ_REG_LATENCY]; 144 /// wire reg_stb = reg_re_r[READ_REG_LATENCY-1]; 156 // reg anc_fis_r; // This is ATAPI FIS, not Command FIS 176 // wire done_w = dx_dma_last_w || ((|dx_err_r) && dx_busy_r) || chead_done_w || acfis_xmit_end || dma_start; // done on last transmit or error 177 // dma_start ends 'fetch_cmd' 182 // now ahci_dma watches for the last data DWORD and generates last_h2d_data, so transmission will end if either of xfer counter or DMA data (defined by total prd size) 183 // if xfer_cntr wazs 0, it will never be decremented and never equal to 1, will not generate last) 184 // reg xfer_cntr_is_set; 185 // reg watch_prd_end; 186 // wire masked_last_h2d_data = xfer_cntr_not_set && last_h2d_data; // otherwise use xfer counter to find FIS end 187 // wire watch_prd_end_w = masked_last_h2d_data || watch_prd_end; // Maybe not needed - just use watch_prd_end 189 // reg [1:0] was_dma_re; // previous values of dma_re 190 // reg [2:0] was_dma_ndav; // inverted/masked previous values of dma_dav 191 // wire send_last_w = was_dma_ndav[2]; 193 // assign todev_valid = todev_full_r && (!watch_prd_end_w || dma_dav || send_last_w); 204 // assign ch_cfl = cfis_acmd_left_r; 210 // assign dmaCntrZero = dmaCntrZero_r; 212 /// assign fis_data_valid = ct_stb; // no wait write to output register 'todev_data', ct_re_r[0] is throttled according to FIFO room availability 213 // What else to wait for when 216 /// assign ct_re_w = todev_ready && ((cfis_acmd_left_r[4:1] != 0) || (cfis_acmd_left_r[0] && !ct_re_r[0])); // Later add more sources 227 /// assign write_or_w = (dma_en_r?(dma_dav && todev_ready ):fis_data_valid); // do not fill the buffer if FIFO is not ready for DMA, 228 /// assign write_or_w = (dma_en_r?(dma_dav && todev_ready && (!todev_full_r || !watch_prd_end_w)):fis_data_valid); // do not fill the buffer if FIFO is not ready for DMA, 231 // When watching for FIS end, do not fill/use output register in the same cycle 239 // Mutliplex between DMA and FIS output to the output routed to transmit FIFO 240 // Count bypassing DMA dwords to generate FIS_last condition? 247 if (
hba_rst)
todev_type <=
3;
// invalid? - no, now first and last word in command FIS (impossible?) 249 // Read 3 DWORDs from the command header 271 // save command header data to registers 279 // ch_a_r <= reg_rdata[ 5]; 296 // fetch and send command/atapi FIS 310 // Counting CFIS/ATAPI FIS dwords sent to TL 319 // else if (cfis_acmd_left_r[0]) ct_addr <= ct_addr + 1; 322 // first/last dword in FIS 326 //TODO: update xfer length, prdtl (only after R_OK) - yes, do it outside 341 // Abort on transmit errors 376 // assign debug_01 = {acfis_xmit_start_w, acfis_xmit_pend_r, dma_ct_busy, fetch_cmd_busy_r, ct_re_w, dbg_was_cfis_acmd_left_r}; // 1,2,5 377 // wire acfis_xmit_start_w = (cfis_xmit || atapi_xmit || acfis_xmit_pend_r) && !dma_ct_busy && !fetch_cmd_busy_r; // dma_ct_busy no gaps with fetch_cmd_busy
13370fetch_chead_rreg[3:0]
reg [11:0] 13324dwords_sent
13395dbg_was_ct_re_rreg[3:0]
13379acfis_xmit_start_wwire
reg [31:0] 13343todev_data
13376acfis_xmit_pend_rreg
13378acfis_xmit_busy_rreg
13354fis_data_outwire[31:0]
reg [ADDRESS_BITS-1:0] 13325reg_addr
reg [ 1:0] 13344todev_type
13367reg_re_rreg[READ_REG_LATENCY:0]
13371fetch_chead_stb_rreg[3:0]
13353fis_data_typewire[1:0]
13382ct_re_rreg[READ_CT_LATENCY:0]
13366cfis_acmd_left_out_rreg[4:0]
13364ch_cmd_len_rreg[4:0]
13386dx_dwords_leftreg[11:0]
13396dbg_was_cfis_acmd_left_rreg[4:0]
13377acfis_xmit_start_rreg
13365cfis_acmd_left_rreg[4:0]