| 1 | /* -*- c++ -*- |
|---|
| 2 | * |
|---|
| 3 | * SOCLIB_LGPL_HEADER_BEGIN |
|---|
| 4 | * |
|---|
| 5 | * This file is part of SoCLib, GNU LGPLv2.1. |
|---|
| 6 | * |
|---|
| 7 | * SoCLib is free software; you can redistribute it and/or modify it |
|---|
| 8 | * under the terms of the GNU Lesser General Public License as published |
|---|
| 9 | * by the Free Software Foundation; version 2.1 of the License. |
|---|
| 10 | * |
|---|
| 11 | * SoCLib is distributed in the hope that it will be useful, but |
|---|
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 14 | * Lesser General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
|---|
| 17 | * License along with SoCLib; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|---|
| 19 | * 02110-1301 USA |
|---|
| 20 | * |
|---|
| 21 | * SOCLIB_LGPL_HEADER_END |
|---|
| 22 | * |
|---|
| 23 | * Copyright (c) UPMC, Lip6, SoC |
|---|
| 24 | * manuel.bouyer@lip6.fr october 2013 |
|---|
| 25 | * |
|---|
| 26 | * Maintainers: bouyer |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include <stdint.h> |
|---|
| 30 | #include <iostream> |
|---|
| 31 | #include <arithmetics.h> |
|---|
| 32 | #include <fcntl.h> |
|---|
| 33 | #include "vci_spi.h" |
|---|
| 34 | #include "vcispi.h" |
|---|
| 35 | |
|---|
| 36 | namespace soclib { namespace caba { |
|---|
| 37 | |
|---|
| 38 | #define tmpl(t) template<typename vci_param> t VciSpi<vci_param> |
|---|
| 39 | |
|---|
| 40 | using namespace soclib::caba; |
|---|
| 41 | using namespace soclib::common; |
|---|
| 42 | |
|---|
| 43 | //////////////////////// |
|---|
| 44 | tmpl(void)::transition() |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | bool s_dma_bsy = (r_initiator_fsm != M_IDLE); |
|---|
| 48 | if(p_resetn.read() == false) |
|---|
| 49 | { |
|---|
| 50 | r_initiator_fsm = M_IDLE; |
|---|
| 51 | r_target_fsm = T_IDLE; |
|---|
| 52 | r_spi_fsm = S_IDLE; |
|---|
| 53 | r_ss = 0; |
|---|
| 54 | r_divider = 0xffff; |
|---|
| 55 | r_ctrl_char_len = 0; |
|---|
| 56 | r_ctrl_ie = false; |
|---|
| 57 | r_ctrl_cpol = false; |
|---|
| 58 | r_ctrl_cpha = false; |
|---|
| 59 | r_spi_bsy = false; |
|---|
| 60 | r_dma_count = 0; |
|---|
| 61 | r_spi_clk_counter = 0xffff; |
|---|
| 62 | r_spi_clk = 0; |
|---|
| 63 | r_spi_done = false; |
|---|
| 64 | |
|---|
| 65 | r_irq = false; |
|---|
| 66 | r_read = false; |
|---|
| 67 | |
|---|
| 68 | r_dma_fifo_read.init(); |
|---|
| 69 | r_dma_fifo_write.init(); |
|---|
| 70 | |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 75 | // The Target FSM controls the following registers: |
|---|
| 76 | // r_target_fsm, r_irq_enable, r_nblocks, r_buf adress, r_lba, r_go, r_read |
|---|
| 77 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 78 | |
|---|
| 79 | if (r_spi_done) |
|---|
| 80 | r_spi_bsy = false; |
|---|
| 81 | |
|---|
| 82 | switch(r_target_fsm) { |
|---|
| 83 | //////////// |
|---|
| 84 | case T_IDLE: |
|---|
| 85 | { |
|---|
| 86 | if ( p_vci_target.cmdval.read() ) |
|---|
| 87 | { |
|---|
| 88 | r_srcid = p_vci_target.srcid.read(); |
|---|
| 89 | r_trdid = p_vci_target.trdid.read(); |
|---|
| 90 | r_pktid = p_vci_target.pktid.read(); |
|---|
| 91 | uint32_t wdata = p_vci_target.wdata.read(); |
|---|
| 92 | sc_dt::sc_uint<vci_param::N> address = p_vci_target.address.read(); |
|---|
| 93 | |
|---|
| 94 | bool found = false; |
|---|
| 95 | std::list<soclib::common::Segment>::iterator seg; |
|---|
| 96 | for ( seg = m_seglist.begin() ; seg != m_seglist.end() ; seg++ ) |
|---|
| 97 | { |
|---|
| 98 | if ( seg->contains(address) ) found = true; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | if (not found) { |
|---|
| 103 | if (p_vci_target.cmd.read() == vci_param::CMD_WRITE) |
|---|
| 104 | r_target_fsm = T_ERROR_WRITE; |
|---|
| 105 | else |
|---|
| 106 | r_target_fsm = T_ERROR_READ; |
|---|
| 107 | } else if (p_vci_target.cmd.read() != vci_param::CMD_READ && |
|---|
| 108 | p_vci_target.cmd.read() != vci_param::CMD_WRITE) { |
|---|
| 109 | r_target_fsm = T_ERROR_READ; |
|---|
| 110 | } else { |
|---|
| 111 | bool write = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) & !r_spi_bsy &!s_dma_bsy; |
|---|
| 112 | uint32_t cell = (uint32_t)((address & 0x3F)>>2); |
|---|
| 113 | switch(cell) { |
|---|
| 114 | case SPI_DATA_TXRX0: |
|---|
| 115 | r_rdata = r_txrx[0] & (uint64_t)0x00000000ffffffffULL; |
|---|
| 116 | if (write) { |
|---|
| 117 | r_txrx[0] = |
|---|
| 118 | (r_txrx[0] & (uint64_t)0xffffffff00000000ULL) | |
|---|
| 119 | ((uint64_t)wdata); |
|---|
| 120 | } |
|---|
| 121 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 122 | break; |
|---|
| 123 | case SPI_DATA_TXRX1: |
|---|
| 124 | r_rdata = r_txrx[0] >> 32; |
|---|
| 125 | if (write) { |
|---|
| 126 | r_txrx[0] = |
|---|
| 127 | (r_txrx[0] & (uint64_t)0x00000000ffffffffULL) | |
|---|
| 128 | ((uint64_t)wdata << 32); |
|---|
| 129 | } |
|---|
| 130 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 131 | break; |
|---|
| 132 | case SPI_DATA_TXRX2: |
|---|
| 133 | r_rdata = r_txrx[1] & (uint64_t)0x00000000ffffffffULL; |
|---|
| 134 | if (write) { |
|---|
| 135 | r_txrx[1] = |
|---|
| 136 | (r_txrx[1] & (uint64_t)0xffffffff00000000ULL) | |
|---|
| 137 | ((uint64_t)wdata); |
|---|
| 138 | } |
|---|
| 139 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 140 | break; |
|---|
| 141 | case SPI_DATA_TXRX3: |
|---|
| 142 | r_rdata = r_txrx[1] >> 32; |
|---|
| 143 | if (write) { |
|---|
| 144 | r_txrx[1] = |
|---|
| 145 | (r_txrx[1] & (uint64_t)0x00000000ffffffffULL) | |
|---|
| 146 | ((uint64_t)wdata << 32); |
|---|
| 147 | } |
|---|
| 148 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 149 | break; |
|---|
| 150 | case SPI_CTRL: |
|---|
| 151 | { |
|---|
| 152 | uint32_t data = 0; |
|---|
| 153 | if (r_ctrl_cpol.read()) |
|---|
| 154 | data |= SPI_CTRL_CPOL; |
|---|
| 155 | if (r_ctrl_cpha.read()) |
|---|
| 156 | data |= SPI_CTRL_CPHA; |
|---|
| 157 | if (r_ctrl_ie.read()) |
|---|
| 158 | data |= SPI_CTRL_IE_EN; |
|---|
| 159 | if (r_spi_bsy.read()) |
|---|
| 160 | data |= SPI_CTRL_GO_BSY; |
|---|
| 161 | if (s_dma_bsy) |
|---|
| 162 | data |= SPI_CTRL_DMA_BSY; |
|---|
| 163 | if (r_dma_error) |
|---|
| 164 | data |= SPI_CTRL_DMA_ERR; |
|---|
| 165 | data |= (uint32_t)r_ctrl_char_len.read(); |
|---|
| 166 | r_rdata = data; |
|---|
| 167 | if (write) { |
|---|
| 168 | r_ctrl_cpol = ((wdata & SPI_CTRL_CPOL) != 0); |
|---|
| 169 | r_ctrl_cpha = ((wdata & SPI_CTRL_CPHA) != 0); |
|---|
| 170 | r_ctrl_ie = ((wdata & SPI_CTRL_IE_EN) != 0); |
|---|
| 171 | if (wdata & SPI_CTRL_GO_BSY) |
|---|
| 172 | r_spi_bsy = true; |
|---|
| 173 | r_ctrl_char_len = (wdata & SPI_CTRL_CHAR_LEN_MASK); |
|---|
| 174 | #ifdef SOCLIB_MODULE_DEBUG |
|---|
| 175 | if ((wdata & SPI_CTRL_GO_BSY) != 0) { |
|---|
| 176 | std::cout << name() << " start xfer " << std::dec << (int)r_ctrl_char_len.read() << " data " << std::hex << r_txrx[1] << " " << r_txrx[0] << std::endl; |
|---|
| 177 | } |
|---|
| 178 | #endif |
|---|
| 179 | } else { |
|---|
| 180 | r_irq = r_irq & r_spi_bsy; |
|---|
| 181 | } |
|---|
| 182 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 183 | break; |
|---|
| 184 | } |
|---|
| 185 | case SPI_DIVIDER: |
|---|
| 186 | r_rdata = r_divider.read(); |
|---|
| 187 | if (write) { |
|---|
| 188 | #ifdef SOCLIB_MODULE_DEBUG |
|---|
| 189 | std::cout << name() << " divider set to " << std::dec << wdata << std::endl; |
|---|
| 190 | #endif |
|---|
| 191 | r_divider = wdata; |
|---|
| 192 | } |
|---|
| 193 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 194 | break; |
|---|
| 195 | case SPI_SS: |
|---|
| 196 | r_rdata = r_ss.read(); |
|---|
| 197 | if (write) { |
|---|
| 198 | r_ss = wdata; |
|---|
| 199 | } |
|---|
| 200 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 201 | break; |
|---|
| 202 | case SPI_DMA_BASE: |
|---|
| 203 | r_rdata = r_buf_address.read(); |
|---|
| 204 | if (write) { |
|---|
| 205 | r_buf_address = (r_buf_address & (uint64_t)0xffffffff00000000) | wdata; |
|---|
| 206 | } |
|---|
| 207 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 208 | break; |
|---|
| 209 | case SPI_DMA_BASEH: |
|---|
| 210 | r_rdata = r_buf_address >> 32; |
|---|
| 211 | if (write) { |
|---|
| 212 | r_buf_address = (r_buf_address & (uint64_t)0x00000000ffffffff) | ((uint64_t)wdata << 32); |
|---|
| 213 | } |
|---|
| 214 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 215 | break; |
|---|
| 216 | case SPI_DMA_COUNT: |
|---|
| 217 | r_rdata = (r_dma_count.read() << m_byte2burst_shift) | |
|---|
| 218 | r_read; |
|---|
| 219 | if (write) { |
|---|
| 220 | r_read = (wdata & 0x1); |
|---|
| 221 | r_dma_count = wdata >> m_byte2burst_shift; |
|---|
| 222 | r_ctrl_char_len = vci_param::B * 8; |
|---|
| 223 | } |
|---|
| 224 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_RSP_WRITE : T_RSP_READ; |
|---|
| 225 | break; |
|---|
| 226 | default: |
|---|
| 227 | r_target_fsm = (p_vci_target.cmd.read() == vci_param::CMD_WRITE) ? T_ERROR_WRITE : T_ERROR_READ; |
|---|
| 228 | break; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | break; |
|---|
| 233 | } |
|---|
| 234 | //////////////////// |
|---|
| 235 | case T_RSP_READ: |
|---|
| 236 | case T_RSP_WRITE: |
|---|
| 237 | case T_ERROR_READ: |
|---|
| 238 | case T_ERROR_WRITE: |
|---|
| 239 | if (p_vci_target.rspack.read() ) { |
|---|
| 240 | r_target_fsm = T_IDLE; |
|---|
| 241 | } |
|---|
| 242 | break; |
|---|
| 243 | } // end switch target fsm |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 249 | // the SPI FSM controls SPI signals |
|---|
| 250 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 251 | if (r_spi_bsy == false) |
|---|
| 252 | r_spi_done = false; |
|---|
| 253 | switch (r_spi_fsm) { |
|---|
| 254 | case S_IDLE: |
|---|
| 255 | r_spi_clk_counter = r_divider.read(); |
|---|
| 256 | r_spi_clk = 0; |
|---|
| 257 | r_spi_clk_previous = r_ctrl_cpha; |
|---|
| 258 | r_spi_clk_ignore = r_ctrl_cpha; |
|---|
| 259 | r_spi_bit_count = r_ctrl_char_len; |
|---|
| 260 | if (r_dma_count != 0) { |
|---|
| 261 | if (r_read.read()) |
|---|
| 262 | r_spi_fsm = S_DMA_SEND_START; |
|---|
| 263 | else |
|---|
| 264 | r_spi_fsm = S_DMA_RECEIVE; |
|---|
| 265 | } else if (r_spi_bsy.read() && !r_spi_done.read()) { |
|---|
| 266 | r_spi_fsm = S_XMIT; |
|---|
| 267 | r_spi_out = (r_txrx[(r_ctrl_char_len -1)/ 64] >> ((r_ctrl_char_len - 1) % 64)) & (uint64_t)0x0000000000000001ULL; |
|---|
| 268 | } |
|---|
| 269 | break; |
|---|
| 270 | case S_DMA_RECEIVE: |
|---|
| 271 | { |
|---|
| 272 | r_spi_clk_counter = r_divider.read(); |
|---|
| 273 | r_spi_clk = 0; |
|---|
| 274 | r_spi_clk_previous = r_ctrl_cpha; |
|---|
| 275 | r_spi_clk_ignore = r_ctrl_cpha; |
|---|
| 276 | r_spi_bit_count = r_ctrl_char_len; |
|---|
| 277 | if (r_initiator_fsm != M_WRITE_RSP || !p_vci_initiator.rspval.read()) { |
|---|
| 278 | if (r_dma_fifo_write.rok()) { |
|---|
| 279 | typename vci_param::data_t v = r_dma_fifo_write.read(); |
|---|
| 280 | r_dma_fifo_write.simple_get(); |
|---|
| 281 | r_txrx[0] = v; |
|---|
| 282 | r_spi_out = (v >> ((vci_param::B * 8) - 1)) & 0x1; |
|---|
| 283 | r_spi_fsm = S_XMIT; |
|---|
| 284 | } else if (r_initiator_fsm == M_WRITE_END) { |
|---|
| 285 | r_spi_fsm = S_IDLE; |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | break; |
|---|
| 289 | } |
|---|
| 290 | case S_DMA_SEND_START: |
|---|
| 291 | r_spi_word_count = (r_dma_count << (m_byte2burst_shift - 2)) - 1; |
|---|
| 292 | r_spi_out = 1; |
|---|
| 293 | r_txrx[0] = 0xffffffff; |
|---|
| 294 | r_spi_fsm = S_XMIT; |
|---|
| 295 | break; |
|---|
| 296 | case S_DMA_SEND: |
|---|
| 297 | r_spi_out = 1; |
|---|
| 298 | r_spi_clk_counter = r_divider.read(); |
|---|
| 299 | r_spi_clk = 0; |
|---|
| 300 | r_spi_clk_previous = r_ctrl_cpha; |
|---|
| 301 | r_spi_clk_ignore = r_ctrl_cpha; |
|---|
| 302 | r_spi_bit_count = r_ctrl_char_len; |
|---|
| 303 | if (r_initiator_fsm != M_READ_CMD) { |
|---|
| 304 | if (r_dma_fifo_read.wok()) { |
|---|
| 305 | r_dma_fifo_read.simple_put( |
|---|
| 306 | (typename vci_param::data_t)r_txrx[0]); |
|---|
| 307 | r_spi_word_count = r_spi_word_count - 1; |
|---|
| 308 | r_txrx[0] = 0xffffffff; |
|---|
| 309 | if ( r_spi_word_count == 0 ) { |
|---|
| 310 | r_spi_fsm = S_DMA_SEND_END; |
|---|
| 311 | } else { |
|---|
| 312 | r_spi_fsm = S_XMIT; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | break; |
|---|
| 317 | case S_DMA_SEND_END: |
|---|
| 318 | if (r_initiator_fsm == M_IDLE) |
|---|
| 319 | r_spi_fsm = S_IDLE; |
|---|
| 320 | break; |
|---|
| 321 | case S_XMIT: |
|---|
| 322 | { |
|---|
| 323 | bool s_clk_sample; |
|---|
| 324 | // on clock transition, sample input line, and shift data |
|---|
| 325 | s_clk_sample = r_spi_clk ^ r_ctrl_cpha; |
|---|
| 326 | if (!r_spi_clk_ignore) { |
|---|
| 327 | if (r_spi_clk_previous == 0 && s_clk_sample == 1) { |
|---|
| 328 | // low to high transition: shift and sample |
|---|
| 329 | r_txrx[1] = (r_txrx[1] << 1) | (r_txrx[0] >> 63); |
|---|
| 330 | r_txrx[0] = (r_txrx[0] << 1) | p_spi_miso; |
|---|
| 331 | r_spi_bit_count = r_spi_bit_count - 1; |
|---|
| 332 | } else if (r_spi_clk_previous == 1 && s_clk_sample == 0) { |
|---|
| 333 | // high to low transition: change output, or stop |
|---|
| 334 | if (r_spi_bit_count == 0) { |
|---|
| 335 | if (r_initiator_fsm != M_IDLE) { |
|---|
| 336 | if (r_read) |
|---|
| 337 | r_spi_fsm = S_DMA_SEND; |
|---|
| 338 | else |
|---|
| 339 | r_spi_fsm = S_DMA_RECEIVE; |
|---|
| 340 | } else { |
|---|
| 341 | r_spi_fsm = S_IDLE; |
|---|
| 342 | r_irq = r_ctrl_ie; |
|---|
| 343 | r_spi_done = true; |
|---|
| 344 | } |
|---|
| 345 | #ifdef SOCLIB_MODULE_DEBUG0 |
|---|
| 346 | std::cout << name() << " end xfer " << std::dec << (int)r_ctrl_char_len.read() << " data " << std::hex << r_txrx[1] << " " << r_txrx[0] << std::endl; |
|---|
| 347 | #endif |
|---|
| 348 | } else { |
|---|
| 349 | r_spi_out = (r_txrx[(r_ctrl_char_len -1)/ 64] >> ((r_ctrl_char_len - 1) % 64)) & (uint64_t)0x0000000000000001ULL; |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | r_spi_clk_previous = s_clk_sample; |
|---|
| 354 | // generate the SPI clock |
|---|
| 355 | if (r_spi_clk_counter.read() == 0) { |
|---|
| 356 | r_spi_clk_counter = r_divider.read(); |
|---|
| 357 | r_spi_clk = !r_spi_clk.read(); |
|---|
| 358 | r_spi_clk_ignore = false; |
|---|
| 359 | } else { |
|---|
| 360 | r_spi_clk_counter = r_spi_clk_counter.read() - 1; |
|---|
| 361 | } |
|---|
| 362 | break; |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 366 | // The initiator FSM executes a loop, transfering one burst per iteration. |
|---|
| 367 | // data comes from or goes to fifos, the other end of the fifos is |
|---|
| 368 | // feed by or eaten by the SPI fsm. |
|---|
| 369 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 370 | |
|---|
| 371 | switch( r_initiator_fsm.read() ) { |
|---|
| 372 | //////////// |
|---|
| 373 | case M_IDLE: // check buffer alignment to compute the number of bursts |
|---|
| 374 | { |
|---|
| 375 | if ( r_dma_count != 0 ) |
|---|
| 376 | { |
|---|
| 377 | // start transfer |
|---|
| 378 | if ( r_read.read() ) r_initiator_fsm = M_READ_WAIT; |
|---|
| 379 | else r_initiator_fsm = M_WRITE_WAIT; |
|---|
| 380 | } |
|---|
| 381 | break; |
|---|
| 382 | } |
|---|
| 383 | case M_READ_WAIT: // wait for the FIFO to be full |
|---|
| 384 | if (!r_dma_fifo_read.wok()) { |
|---|
| 385 | r_burst_word = m_words_per_burst - 1; |
|---|
| 386 | r_initiator_fsm = M_READ_CMD; |
|---|
| 387 | } |
|---|
| 388 | break; |
|---|
| 389 | //////////////// |
|---|
| 390 | case M_READ_CMD: // Send a multi-flits VCI WRITE command |
|---|
| 391 | { |
|---|
| 392 | if ( p_vci_initiator.cmdack.read() ) |
|---|
| 393 | { |
|---|
| 394 | if ( r_burst_word == 0 ) // last flit |
|---|
| 395 | { |
|---|
| 396 | r_initiator_fsm = M_READ_RSP; |
|---|
| 397 | } |
|---|
| 398 | else // not the last flit |
|---|
| 399 | { |
|---|
| 400 | r_burst_word = r_burst_word.read() - 1; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | r_dma_fifo_read.simple_get(); // consume one fifo word |
|---|
| 404 | // compute next word address |
|---|
| 405 | r_buf_address = r_buf_address.read() + vci_param::B; |
|---|
| 406 | } |
|---|
| 407 | break; |
|---|
| 408 | } |
|---|
| 409 | //////////////// |
|---|
| 410 | case M_READ_RSP: // Wait a single flit VCI WRITE response |
|---|
| 411 | { |
|---|
| 412 | if ( p_vci_initiator.rspval.read() ) |
|---|
| 413 | { |
|---|
| 414 | if ( (p_vci_initiator.rerror.read()&0x1) != 0 ) |
|---|
| 415 | { |
|---|
| 416 | r_burst_word = 0; |
|---|
| 417 | r_dma_count = 0; |
|---|
| 418 | r_dma_error = true; |
|---|
| 419 | r_initiator_fsm = M_READ_END; |
|---|
| 420 | #ifdef SOCLIB_MODULE_DEBUG |
|---|
| 421 | std::cout << "vci_bd M_READ_ERROR" << std::endl; |
|---|
| 422 | #endif |
|---|
| 423 | } |
|---|
| 424 | else if ( r_spi_fsm == S_DMA_SEND_END ) // last burst |
|---|
| 425 | { |
|---|
| 426 | r_dma_count = 0; |
|---|
| 427 | r_initiator_fsm = M_READ_END; |
|---|
| 428 | r_dma_error = false; |
|---|
| 429 | #ifdef SOCLIB_MODULE_DEBUG |
|---|
| 430 | std::cout << "vci_bd M_READ_SUCCESS" << std::endl; |
|---|
| 431 | #endif |
|---|
| 432 | } |
|---|
| 433 | else // keep on reading |
|---|
| 434 | { |
|---|
| 435 | r_dma_count = r_dma_count - 1; |
|---|
| 436 | r_initiator_fsm = M_READ_WAIT; |
|---|
| 437 | } |
|---|
| 438 | } |
|---|
| 439 | break; |
|---|
| 440 | } |
|---|
| 441 | /////////////////// |
|---|
| 442 | case M_READ_END: |
|---|
| 443 | // wait one cycle because VHDL can't update r_dma_count |
|---|
| 444 | // in the same cycle as going back IDLE |
|---|
| 445 | r_initiator_fsm = M_IDLE; |
|---|
| 446 | break; |
|---|
| 447 | /////////////////// |
|---|
| 448 | case M_WRITE_WAIT: // wait for the FIFO to be empty |
|---|
| 449 | if (!r_dma_fifo_write.rok()) { |
|---|
| 450 | r_burst_word = m_words_per_burst - 1; |
|---|
| 451 | r_dma_count = r_dma_count - 1; |
|---|
| 452 | r_initiator_fsm = M_WRITE_CMD; |
|---|
| 453 | } |
|---|
| 454 | break; |
|---|
| 455 | ///////////////// |
|---|
| 456 | case M_WRITE_CMD: // This is actually a single flit VCI READ command |
|---|
| 457 | { |
|---|
| 458 | if ( p_vci_initiator.cmdack.read() ) r_initiator_fsm = M_WRITE_RSP; |
|---|
| 459 | break; |
|---|
| 460 | } |
|---|
| 461 | ///////////////// |
|---|
| 462 | case M_WRITE_RSP: // This is actually a multi-words VCI READ response |
|---|
| 463 | { |
|---|
| 464 | if ( p_vci_initiator.rspval.read() ) |
|---|
| 465 | { |
|---|
| 466 | typename vci_param::data_t v = p_vci_initiator.rdata.read(); |
|---|
| 467 | typename vci_param::data_t f = 0; |
|---|
| 468 | // byte-swap |
|---|
| 469 | for (int i = 0; i < (vci_param::B * 8); i += 8) { |
|---|
| 470 | f |= ((v >> i) & 0xff) << ((vci_param::B * 8) - 8 - i); |
|---|
| 471 | } |
|---|
| 472 | r_dma_fifo_write.simple_put(f); |
|---|
| 473 | r_burst_word = r_burst_word.read() - 1; |
|---|
| 474 | if ( p_vci_initiator.reop.read() ) // last flit of the burst |
|---|
| 475 | { |
|---|
| 476 | r_buf_address = r_buf_address.read() + m_burst_size; |
|---|
| 477 | |
|---|
| 478 | if( (p_vci_initiator.rerror.read()&0x1) != 0 ) |
|---|
| 479 | { |
|---|
| 480 | r_dma_count = 0; |
|---|
| 481 | r_dma_error = 1; |
|---|
| 482 | r_initiator_fsm = M_WRITE_END; |
|---|
| 483 | #ifdef SOCLIB_MODULE_DEBUG |
|---|
| 484 | std::cout << "vci_bd M_WRITE_ERROR" << std::endl; |
|---|
| 485 | #endif |
|---|
| 486 | } |
|---|
| 487 | else if ( r_dma_count.read() == 0) // last burst |
|---|
| 488 | { |
|---|
| 489 | r_dma_error = 0; |
|---|
| 490 | r_initiator_fsm = M_WRITE_END; |
|---|
| 491 | } |
|---|
| 492 | else // not the last burst |
|---|
| 493 | { |
|---|
| 494 | r_initiator_fsm = M_WRITE_WAIT; |
|---|
| 495 | } |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | break; |
|---|
| 499 | } |
|---|
| 500 | ///////////////// |
|---|
| 501 | case M_WRITE_END: // wait for the write to be complete |
|---|
| 502 | { |
|---|
| 503 | if (r_spi_fsm == S_IDLE) { // write complete |
|---|
| 504 | r_initiator_fsm = M_IDLE; |
|---|
| 505 | } |
|---|
| 506 | break; |
|---|
| 507 | } |
|---|
| 508 | } // end switch r_initiator_fsm |
|---|
| 509 | } // end transition |
|---|
| 510 | |
|---|
| 511 | ////////////////////// |
|---|
| 512 | tmpl(void)::genMoore() |
|---|
| 513 | { |
|---|
| 514 | // p_vci_target port |
|---|
| 515 | p_vci_target.rsrcid = (sc_dt::sc_uint<vci_param::S>)r_srcid.read(); |
|---|
| 516 | p_vci_target.rtrdid = (sc_dt::sc_uint<vci_param::T>)r_trdid.read(); |
|---|
| 517 | p_vci_target.rpktid = (sc_dt::sc_uint<vci_param::P>)r_pktid.read(); |
|---|
| 518 | p_vci_target.reop = true; |
|---|
| 519 | |
|---|
| 520 | switch(r_target_fsm) { |
|---|
| 521 | case T_IDLE: |
|---|
| 522 | p_vci_target.cmdack = true; |
|---|
| 523 | p_vci_target.rspval = false; |
|---|
| 524 | p_vci_target.rdata = 0; |
|---|
| 525 | break; |
|---|
| 526 | case T_RSP_READ: |
|---|
| 527 | p_vci_target.cmdack = false; |
|---|
| 528 | p_vci_target.rspval = true; |
|---|
| 529 | p_vci_target.rdata = r_rdata; |
|---|
| 530 | p_vci_target.rerror = VCI_READ_OK; |
|---|
| 531 | break; |
|---|
| 532 | case T_RSP_WRITE: |
|---|
| 533 | p_vci_target.cmdack = false; |
|---|
| 534 | p_vci_target.rspval = true; |
|---|
| 535 | p_vci_target.rdata = 0; |
|---|
| 536 | p_vci_target.rerror = VCI_WRITE_OK; |
|---|
| 537 | break; |
|---|
| 538 | case T_ERROR_READ: |
|---|
| 539 | p_vci_target.cmdack = false; |
|---|
| 540 | p_vci_target.rspval = true; |
|---|
| 541 | p_vci_target.rdata = 0; |
|---|
| 542 | p_vci_target.rerror = VCI_READ_ERROR; |
|---|
| 543 | break; |
|---|
| 544 | case T_ERROR_WRITE: |
|---|
| 545 | p_vci_target.cmdack = false; |
|---|
| 546 | p_vci_target.rspval = true; |
|---|
| 547 | p_vci_target.rdata = 0; |
|---|
| 548 | p_vci_target.rerror = VCI_WRITE_ERROR; |
|---|
| 549 | break; |
|---|
| 550 | } // end switch target fsm |
|---|
| 551 | |
|---|
| 552 | // p_vci_initiator port |
|---|
| 553 | p_vci_initiator.srcid = (sc_dt::sc_uint<vci_param::S>)m_srcid; |
|---|
| 554 | p_vci_initiator.trdid = 0; |
|---|
| 555 | p_vci_initiator.contig = true; |
|---|
| 556 | p_vci_initiator.cons = false; |
|---|
| 557 | p_vci_initiator.wrap = false; |
|---|
| 558 | p_vci_initiator.cfixed = false; |
|---|
| 559 | p_vci_initiator.clen = 0; |
|---|
| 560 | |
|---|
| 561 | switch (r_initiator_fsm) { |
|---|
| 562 | case M_WRITE_CMD: // It is actually a single flit VCI read command |
|---|
| 563 | p_vci_initiator.rspack = false; |
|---|
| 564 | p_vci_initiator.cmdval = true; |
|---|
| 565 | p_vci_initiator.address = (sc_dt::sc_uint<vci_param::N>)r_buf_address.read(); |
|---|
| 566 | p_vci_initiator.cmd = vci_param::CMD_READ; |
|---|
| 567 | p_vci_initiator.pktid = TYPE_READ_DATA_UNC; |
|---|
| 568 | p_vci_initiator.wdata = 0; |
|---|
| 569 | p_vci_initiator.be = 0; |
|---|
| 570 | p_vci_initiator.plen = (sc_dt::sc_uint<vci_param::K>)(m_burst_size); |
|---|
| 571 | p_vci_initiator.eop = true; |
|---|
| 572 | break; |
|---|
| 573 | case M_READ_CMD: // It is actually a multi-words VCI WRITE command |
|---|
| 574 | { |
|---|
| 575 | typename vci_param::data_t v = 0; |
|---|
| 576 | typename vci_param::data_t f; |
|---|
| 577 | p_vci_initiator.rspack = false; |
|---|
| 578 | p_vci_initiator.cmdval = true; |
|---|
| 579 | p_vci_initiator.address = (sc_dt::sc_uint<vci_param::N>)r_buf_address.read(); |
|---|
| 580 | p_vci_initiator.cmd = vci_param::CMD_WRITE; |
|---|
| 581 | p_vci_initiator.pktid = TYPE_WRITE; |
|---|
| 582 | p_vci_initiator.plen = (sc_dt::sc_uint<vci_param::K>)(m_burst_size); |
|---|
| 583 | f = r_dma_fifo_read.read(); |
|---|
| 584 | // byte-swap |
|---|
| 585 | for (int i = 0; i < (vci_param::B * 8); i += 8) { |
|---|
| 586 | v |= ((f >> i) & 0xff) << ((vci_param::B * 8) - 8 - i); |
|---|
| 587 | } |
|---|
| 588 | p_vci_initiator.wdata = v; |
|---|
| 589 | p_vci_initiator.eop = ( r_burst_word.read() == 0); |
|---|
| 590 | if (vci_param::B == 8) |
|---|
| 591 | { |
|---|
| 592 | p_vci_initiator.be = 0xFF; |
|---|
| 593 | } |
|---|
| 594 | else |
|---|
| 595 | { |
|---|
| 596 | p_vci_initiator.be = 0xF; |
|---|
| 597 | } |
|---|
| 598 | break; |
|---|
| 599 | } |
|---|
| 600 | case M_READ_RSP: |
|---|
| 601 | case M_WRITE_RSP: |
|---|
| 602 | p_vci_initiator.rspack = true; |
|---|
| 603 | p_vci_initiator.cmdval = false; |
|---|
| 604 | break; |
|---|
| 605 | default: |
|---|
| 606 | p_vci_initiator.rspack = false; |
|---|
| 607 | p_vci_initiator.cmdval = false; |
|---|
| 608 | break; |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | // SPI signals |
|---|
| 612 | p_spi_ss = ((r_ss & 0x1) == 0); |
|---|
| 613 | switch(r_spi_fsm) { |
|---|
| 614 | default: |
|---|
| 615 | p_spi_mosi = r_spi_out; |
|---|
| 616 | p_spi_clk = 0; |
|---|
| 617 | break; |
|---|
| 618 | case S_XMIT: |
|---|
| 619 | { |
|---|
| 620 | bool s_clk_sample = r_spi_clk ^ r_ctrl_cpha; |
|---|
| 621 | p_spi_clk = r_spi_clk ^ r_ctrl_cpol; |
|---|
| 622 | if (s_clk_sample == 0) { |
|---|
| 623 | // clock low: get data directly from shift register |
|---|
| 624 | // as r_spi_out may be delayed by one clock cycle |
|---|
| 625 | p_spi_mosi = (r_txrx[(r_ctrl_char_len -1)/ 64] >> ((r_ctrl_char_len - 1) % 64)) & (uint64_t)0x0000000000000001ULL; |
|---|
| 626 | } else { |
|---|
| 627 | // clock high: get data from saved value, as the shift register |
|---|
| 628 | // may have changed |
|---|
| 629 | p_spi_mosi = r_spi_out; |
|---|
| 630 | } |
|---|
| 631 | break; |
|---|
| 632 | } |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | // IRQ signal |
|---|
| 636 | p_irq = r_irq; |
|---|
| 637 | } // end GenMoore() |
|---|
| 638 | |
|---|
| 639 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 640 | tmpl(/**/)::VciSpi( sc_core::sc_module_name name, |
|---|
| 641 | const soclib::common::MappingTable &mt, |
|---|
| 642 | const soclib::common::IntTab &srcid, |
|---|
| 643 | const soclib::common::IntTab &tgtid, |
|---|
| 644 | const uint32_t burst_size) |
|---|
| 645 | |
|---|
| 646 | : caba::BaseModule(name), |
|---|
| 647 | m_seglist(mt.getSegmentList(tgtid)), |
|---|
| 648 | m_srcid(mt.indexForId(srcid)), |
|---|
| 649 | m_burst_size(burst_size), |
|---|
| 650 | m_words_per_burst(burst_size / vci_param::B), |
|---|
| 651 | m_byte2burst_shift(soclib::common::uint32_log2(burst_size)), |
|---|
| 652 | p_clk("p_clk"), |
|---|
| 653 | p_resetn("p_resetn"), |
|---|
| 654 | p_vci_initiator("p_vci_initiator"), |
|---|
| 655 | p_vci_target("p_vci_target"), |
|---|
| 656 | p_irq("p_irq"), |
|---|
| 657 | p_spi_ss("p_spi_ss"), |
|---|
| 658 | p_spi_clk("p_spi_clk"), |
|---|
| 659 | p_spi_mosi("p_spi_mosi"), |
|---|
| 660 | p_spi_miso("p_spi_miso"), |
|---|
| 661 | |
|---|
| 662 | r_dma_fifo_read("r_dma_fifo_read", burst_size / vci_param::B), // one cache line |
|---|
| 663 | r_dma_fifo_write("r_dma_fifo_read", burst_size / vci_param::B) // one cache line |
|---|
| 664 | { |
|---|
| 665 | std::cout << " - Building VciSpi " << name << std::endl; |
|---|
| 666 | |
|---|
| 667 | SC_METHOD(transition); |
|---|
| 668 | dont_initialize(); |
|---|
| 669 | sensitive << p_clk.pos(); |
|---|
| 670 | |
|---|
| 671 | SC_METHOD(genMoore); |
|---|
| 672 | dont_initialize(); |
|---|
| 673 | sensitive << p_clk.neg(); |
|---|
| 674 | |
|---|
| 675 | size_t nbsegs = 0; |
|---|
| 676 | std::list<soclib::common::Segment>::iterator seg; |
|---|
| 677 | for ( seg = m_seglist.begin() ; seg != m_seglist.end() ; seg++ ) |
|---|
| 678 | { |
|---|
| 679 | nbsegs++; |
|---|
| 680 | |
|---|
| 681 | if ( (seg->baseAddress() & 0x0000003F) != 0 ) |
|---|
| 682 | { |
|---|
| 683 | std::cout << "Error in component VciSpi : " << name |
|---|
| 684 | << "The base address of segment " << seg->name() |
|---|
| 685 | << " must be multiple of 64 bytes" << std::endl; |
|---|
| 686 | exit(1); |
|---|
| 687 | } |
|---|
| 688 | if ( seg->size() < 64 ) |
|---|
| 689 | { |
|---|
| 690 | std::cout << "Error in component VciSpi : " << name |
|---|
| 691 | << "The size of segment " << seg->name() |
|---|
| 692 | << " cannot be smaller than 64 bytes" << std::endl; |
|---|
| 693 | exit(1); |
|---|
| 694 | } |
|---|
| 695 | std::cout << " => segment " << seg->name() |
|---|
| 696 | << " / base = " << std::hex << seg->baseAddress() |
|---|
| 697 | << " / size = " << seg->size() << std::endl; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | if( nbsegs == 0 ) |
|---|
| 701 | { |
|---|
| 702 | std::cout << "Error in component VciSpi : " << name |
|---|
| 703 | << " No segment allocated" << std::endl; |
|---|
| 704 | exit(1); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | if( (burst_size != 8 ) && |
|---|
| 708 | (burst_size != 16) && |
|---|
| 709 | (burst_size != 32) && |
|---|
| 710 | (burst_size != 64) ) |
|---|
| 711 | { |
|---|
| 712 | std::cout << "Error in component VciSpi : " << name |
|---|
| 713 | << " The burst size must be 8, 16, 32 or 64 bytes" << std::endl; |
|---|
| 714 | exit(1); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | if ( (vci_param::B != 4) and (vci_param::B != 8) ) |
|---|
| 718 | { |
|---|
| 719 | std::cout << "Error in component VciSpi : " << name |
|---|
| 720 | << " The VCI data fields must have 32 bits or 64 bits" << std::endl; |
|---|
| 721 | exit(1); |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | } // end constructor |
|---|
| 725 | |
|---|
| 726 | tmpl(/**/)::~VciSpi() |
|---|
| 727 | { |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | ////////////////////////// |
|---|
| 732 | tmpl(void)::print_trace() |
|---|
| 733 | { |
|---|
| 734 | const char* initiator_str[] = |
|---|
| 735 | { |
|---|
| 736 | "M_IDLE", |
|---|
| 737 | |
|---|
| 738 | "M_READ_WAIT", |
|---|
| 739 | "M_READ_CMD", |
|---|
| 740 | "M_READ_RSP", |
|---|
| 741 | "M_READ_END", |
|---|
| 742 | |
|---|
| 743 | "M_WRITE_WAIT", |
|---|
| 744 | "M_WRITE_CMD", |
|---|
| 745 | "M_WRITE_RSP", |
|---|
| 746 | "M_WRITE_END", |
|---|
| 747 | }; |
|---|
| 748 | const char* target_str[] = |
|---|
| 749 | { |
|---|
| 750 | "T_IDLE", |
|---|
| 751 | "T_RSP_READ", |
|---|
| 752 | "T_RSP_WRITE", |
|---|
| 753 | "T_ERROR_READ", |
|---|
| 754 | "T_ERROR_WRITE", |
|---|
| 755 | }; |
|---|
| 756 | const char* spi_str[] = |
|---|
| 757 | { |
|---|
| 758 | "S_IDLE", |
|---|
| 759 | "S_DMA_RECEIVE", |
|---|
| 760 | "S_DMA_SEND_START", |
|---|
| 761 | "S_DMA_SEND", |
|---|
| 762 | "S_DMA_SEND_END", |
|---|
| 763 | "S_XMIT", |
|---|
| 764 | }; |
|---|
| 765 | |
|---|
| 766 | std::cout << name() << " _TGT : " << target_str[r_target_fsm.read()] |
|---|
| 767 | << std::endl; |
|---|
| 768 | std::cout << name() << " _SPI : " << spi_str[r_spi_fsm.read()] |
|---|
| 769 | << " clk_counter " << r_spi_clk_counter.read() |
|---|
| 770 | << " r_spi_bit_count " << r_spi_bit_count.read() |
|---|
| 771 | << " r_spi_bsy " << (int)r_spi_bsy.read() << std::endl; |
|---|
| 772 | std::cout << name() << " _SPI : " |
|---|
| 773 | << " r_spi_clk " << r_spi_clk.read() |
|---|
| 774 | << " cpol " << r_ctrl_cpol.read() |
|---|
| 775 | << " cpha " << r_ctrl_cpha.read() |
|---|
| 776 | << " r_spi_clk_ignore " << r_spi_clk_ignore.read() |
|---|
| 777 | << " r_txrx 0x" << std::hex |
|---|
| 778 | << r_txrx[1].read() << " " << r_txrx[0].read() |
|---|
| 779 | |
|---|
| 780 | << std::endl; |
|---|
| 781 | std::cout << name() << " _INI : " << initiator_str[r_initiator_fsm.read()] |
|---|
| 782 | << " buf = " << std::hex << r_buf_address.read() |
|---|
| 783 | << " burst = " << r_burst_word.read() |
|---|
| 784 | << " count = " << r_dma_count.read() |
|---|
| 785 | << " spi_count = " << r_spi_word_count.read() |
|---|
| 786 | <<std::endl; |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | }} // end namespace |
|---|
| 790 | |
|---|
| 791 | // Local Variables: |
|---|
| 792 | // tab-width: 4 |
|---|
| 793 | // c-basic-offset: 4 |
|---|
| 794 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 795 | // indent-tabs-mode: nil |
|---|
| 796 | // End: |
|---|
| 797 | |
|---|
| 798 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 799 | |
|---|