[552] | 1 | |
---|
| 2 | /* -*- c++ -*- |
---|
| 3 | * |
---|
| 4 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
| 5 | * |
---|
| 6 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
| 7 | * |
---|
| 8 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
| 9 | * under the terms of the GNU Lesser General Public License as published |
---|
| 10 | * by the Free Software Foundation; version 2.1 of the License. |
---|
| 11 | * |
---|
| 12 | * SoCLib is distributed in the hope that it will be useful, but |
---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * Lesser General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU Lesser General Public |
---|
| 18 | * License along with SoCLib; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 20 | * 02110-1301 USA |
---|
| 21 | * |
---|
| 22 | * SOCLIB_LGPL_HEADER_END |
---|
| 23 | * |
---|
| 24 | * Copyright (c) UPMC, Lip6, SoC |
---|
| 25 | * manuel.bouyer@lip6.fr october 2013 |
---|
| 26 | * |
---|
| 27 | * Maintainers: bouyer |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 31 | // This component is a SD/MMC block device. |
---|
| 32 | // |
---|
| 33 | // This component can perform data transfers between one single file belonging |
---|
| 34 | // to the host system and a SPI controller. |
---|
| 35 | // The file name is an argument of the constructor. |
---|
| 36 | // |
---|
| 37 | |
---|
| 38 | #ifndef SOCLIB_SDMMC_H |
---|
| 39 | #define SOCLIB_SDMMC_H |
---|
| 40 | |
---|
| 41 | #include <stdint.h> |
---|
| 42 | #include <systemc> |
---|
| 43 | #include <unistd.h> |
---|
| 44 | #include "caba_base_module.h" |
---|
| 45 | |
---|
| 46 | namespace soclib { |
---|
| 47 | namespace caba { |
---|
| 48 | |
---|
| 49 | using namespace sc_core; |
---|
| 50 | |
---|
| 51 | class SdMMC |
---|
| 52 | : public caba::BaseModule |
---|
| 53 | { |
---|
| 54 | private: |
---|
| 55 | |
---|
| 56 | // Registers |
---|
| 57 | sc_signal<int> r_spi_fsm; // SPI state register |
---|
| 58 | sc_signal<uint8_t> r_spi_shiftreg; // data shift in/out |
---|
| 59 | sc_signal<uint8_t> r_spi_bitcount; |
---|
| 60 | sc_signal<bool> r_spi_clk; |
---|
| 61 | |
---|
| 62 | sc_signal<uint8_t> r_command; |
---|
| 63 | sc_signal<uint32_t> r_args; |
---|
| 64 | sc_signal<uint8_t> r_cmdcrc; |
---|
| 65 | int m_fd; // File descriptor |
---|
| 66 | uint64_t m_device_size; // Total number of blocks |
---|
| 67 | const uint32_t m_latency; // device latency |
---|
| 68 | |
---|
| 69 | uint8_t m_databuf[1 /* reponse */ + 1 /* data tocken */ + 512 /* data block */ + 2 /* CRC */ ]; |
---|
| 70 | uint32_t m_datalen_snd; // data size to be sent to host |
---|
| 71 | uint32_t m_datalen_rcv; // data size expected from host |
---|
| 72 | uint32_t m_data_idx; |
---|
| 73 | bool m_acmd; // next command will be acmd |
---|
| 74 | int m_sdstate; // sdcard internal state |
---|
| 75 | |
---|
| 76 | // sd states |
---|
| 77 | enum { |
---|
| 78 | SD_IDLE = 0, |
---|
| 79 | SD_READY = 1, |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | // methods |
---|
| 83 | void transition(); |
---|
| 84 | void genMoore(); |
---|
| 85 | |
---|
| 86 | void handle_sdmmc_cmd(uint8_t, uint32_t); |
---|
| 87 | void handle_sdmmc_write(); |
---|
| 88 | |
---|
| 89 | // Master FSM states |
---|
| 90 | enum { |
---|
| 91 | S_IDLE = 0, |
---|
| 92 | S_RECEIVE_CMD = 1, |
---|
| 93 | S_RECEIVE_ARGS_START = 2, |
---|
| 94 | S_RECEIVE_ARGS = 3, |
---|
| 95 | S_RECEIVE_CRC = 4, |
---|
| 96 | S_RECEIVE_DATA = 5, |
---|
| 97 | S_RECEIVE_DATA_END = 6, |
---|
| 98 | S_SEND_DATA = 7, |
---|
| 99 | S_NOP = 8, |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | protected: |
---|
| 103 | |
---|
| 104 | SC_HAS_PROCESS(SdMMC); |
---|
| 105 | |
---|
| 106 | public: |
---|
| 107 | |
---|
| 108 | // ports |
---|
| 109 | sc_in<bool> p_clk; |
---|
| 110 | sc_in<bool> p_resetn; |
---|
| 111 | sc_in<bool> p_spi_ss; |
---|
| 112 | sc_in<bool> p_spi_clk; |
---|
| 113 | sc_in<bool> p_spi_mosi; |
---|
| 114 | sc_out<bool> p_spi_miso; |
---|
| 115 | |
---|
| 116 | void print_trace(); |
---|
| 117 | |
---|
| 118 | // Constructor |
---|
| 119 | SdMMC( |
---|
| 120 | sc_module_name name, |
---|
| 121 | const std::string &filename, |
---|
| 122 | const uint32_t latency = 0); |
---|
| 123 | |
---|
| 124 | ~SdMMC(); |
---|
| 125 | |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | }} |
---|
| 129 | |
---|
| 130 | #endif /* SOCLIB_SDMMC_H */ |
---|
| 131 | |
---|
| 132 | // Local Variables: |
---|
| 133 | // tab-width: 4 |
---|
| 134 | // c-basic-offset: 4 |
---|
| 135 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 136 | // indent-tabs-mode: nil |
---|
| 137 | // End: |
---|
| 138 | |
---|
| 139 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|