| 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 |     int               spi_fsm;           // SPI state register | 
|---|
| 58 |     int               spi_shiftreg;     // data shift in/out | 
|---|
| 59 |     int               spi_bitcount; | 
|---|
| 60 |     int               spi_clk; | 
|---|
| 61 |     int               spi_mosi_previous; // sampled MOSI value | 
|---|
| 62 |  | 
|---|
| 63 |     uint8_t           command; | 
|---|
| 64 |     uint32_t          args; | 
|---|
| 65 |     uint8_t           cmdcrc; | 
|---|
| 66 |     int               m_fd;              // File descriptor | 
|---|
| 67 |     uint64_t          m_device_size;     // Total number of blocks | 
|---|
| 68 |     const uint32_t    m_latency;         // device latency | 
|---|
| 69 |  | 
|---|
| 70 |     uint8_t           m_databuf[1 /* reponse */ + 1 /* data tocken */ + 512 /* data block */ + 2 /* CRC */ ]; | 
|---|
| 71 |     uint32_t          m_datalen_snd; // data size to be sent to host | 
|---|
| 72 |     uint32_t          m_datalen_rcv; // data size expected from host | 
|---|
| 73 |     uint32_t          m_data_idx; | 
|---|
| 74 |     bool              m_acmd; // next command will be acmd | 
|---|
| 75 |     int               m_sdstate; // sdcard internal state | 
|---|
| 76 |  | 
|---|
| 77 |     // sd states | 
|---|
| 78 |     enum { | 
|---|
| 79 |         SD_IDLE = 0, | 
|---|
| 80 |         SD_READY = 1, | 
|---|
| 81 |     }; | 
|---|
| 82 |  | 
|---|
| 83 |     // methods | 
|---|
| 84 |     void genMealy(); | 
|---|
| 85 |  | 
|---|
| 86 |     void handle_sdmmc_cmd(uint8_t, uint32_t); | 
|---|
| 87 |     void handle_sdmmc_write(uint8_t, uint32_t); | 
|---|
| 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_WAIT  = 5, | 
|---|
| 97 |     S_RECEIVE_DATA       = 6, | 
|---|
| 98 |     S_SEND_DATA          = 7, | 
|---|
| 99 |     }; | 
|---|
| 100 |  | 
|---|
| 101 | protected: | 
|---|
| 102 |  | 
|---|
| 103 |     SC_HAS_PROCESS(SdMMC); | 
|---|
| 104 |  | 
|---|
| 105 | public: | 
|---|
| 106 |  | 
|---|
| 107 |     // ports | 
|---|
| 108 |     sc_in<bool>                                               p_clk; | 
|---|
| 109 |     sc_in<bool>                                               p_resetn; | 
|---|
| 110 |     sc_in<bool>                                               p_spi_ss; | 
|---|
| 111 |     sc_in<bool>                                               p_spi_clk; | 
|---|
| 112 |     sc_in<bool>                                               p_spi_mosi; | 
|---|
| 113 |     sc_out<bool>                                              p_spi_miso; | 
|---|
| 114 |  | 
|---|
| 115 |     void print_trace(); | 
|---|
| 116 |  | 
|---|
| 117 |     // Constructor    | 
|---|
| 118 |     SdMMC( | 
|---|
| 119 |         sc_module_name                      name, | 
|---|
| 120 |         const std::string                   &filename, | 
|---|
| 121 |         const uint32_t                      latency = 0); | 
|---|
| 122 |  | 
|---|
| 123 |     ~SdMMC(); | 
|---|
| 124 |  | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | }} | 
|---|
| 128 |  | 
|---|
| 129 | #endif /* SOCLIB_SDMMC_H */ | 
|---|
| 130 |  | 
|---|
| 131 | // Local Variables: | 
|---|
| 132 | // tab-width: 4 | 
|---|
| 133 | // c-basic-offset: 4 | 
|---|
| 134 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | 
|---|
| 135 | // indent-tabs-mode: nil | 
|---|
| 136 | // End: | 
|---|
| 137 |  | 
|---|
| 138 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 | 
|---|