1 /*
2  * Copyright (c) 2016-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*!***************************************************************************
34  *  @file       SDHostCC32XX.h
35  *  @brief      SDHost driver implementation for CC32XX devices.
36  *
37  *  The SDHost header file should be included in an application as follows:
38  *  @code
39  *  #include <ti/drivers/SD.h>
40  *  #include <ti/drivers/sd/SDHostCC32XX.h>
41  *  @endcode
42  *
43  *  Refer to @ref SD.h for a complete description of APIs & example of use.
44  *
45  *  This SDHost driver implementation is designed to operate on a CC32XX
46  *  SD Host controller using a micro DMA controller.
47  *
48  *  Note: The driver API's are not thread safe and must not be accessed through
49  *      multiple threads without the use of mutexes.
50  *
51  *  ## DMA buffer alignment #
52  *
53  *  When performing disk operations with a word aligned buffer the driver will
54  *  make transfers using the DMA controller. Alternatively, if the buffer is
55  *  not aligned, the data will be copied to the internal SD Host controller
56  *  buffer using a polling method.
57  *
58  *  ## DMA Interrupts #
59  *
60  *  When DMA is used, the micro DMA controller generates and IRQ on the
61  *  perpheral's interrupt vector. This implementation automatically installs
62  *  a DMA interrupt to service the assigned micro DMA channels.
63  *
64  *  ## DMA accessible memory #
65  *
66  *  When DMA is used, it is the responsibility of the application to ensure
67  *  that read/write buffers reside in memory that is accessible by the DMA.
68  *
69  *  <hr>
70  */
71 
72 #ifndef ti_drivers_sd_SDHostCC32XX__include
73 #define ti_drivers_sd_SDHostCC32XX__include
74 
75 #include <stdint.h>
76 #include <ti/drivers/SD.h>
77 
78 #include <ti/drivers/dpl/HwiP.h>
79 #include <ti/drivers/dpl/SemaphoreP.h>
80 #include <ti/drivers/Power.h>
81 #include <ti/drivers/power/PowerCC32XX.h>
82 #include <ti/drivers/dma/UDMACC32XX.h>
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 #define SDHostCC32XX_PIN_06_SDCARD_DATA  0x0805
89 #define SDHostCC32XX_PIN_07_SDCARD_CLK   0x0806
90 #define SDHostCC32XX_PIN_08_SDCARD_CMD   0x0807
91 #define SDHostCC32XX_PIN_01_SDCARD_CLK   0x0600
92 #define SDHostCC32XX_PIN_02_SDCARD_CMD   0x0601
93 #define SDHostCC32XX_PIN_64_SDCARD_DATA  0x063f
94 
95 /* SDHost function table */
96 extern const SD_FxnTable sdHostCC32XX_fxnTable;
97 
98 /*!
99  *  @brief  SDHostCC32XX Hardware attributes
100  *
101  *  The SDHostCC32XX configuration structure is passed to the SDHostCC32XX
102  *  driver implementation with hardware specifics regarding GPIO Pins and Ports
103  *  to be used.
104  *
105  *  The SDHostCC32XX driver uses this information to:
106  *  - Configure and reconfigure specific ports/pins to initialize the SD Card
107  *    for SD mode
108  *  - Identify which GPIO port and pin is used for the SDHost clock, data and
109  *    command lines
110  *
111  *  These fields are used by driverlib APIs and therefore must be populated by
112  *  driverlib macro definitions. For CC32xxWare these definitions are found in:
113  *      - inc/hw_memmap.h
114  *      - driverlib/pin.h
115  *
116  *  @struct SDHostCC32XX_HWAttrs
117  *  An example configuration structure could look as the following:
118  *  @code
119  *  const SDHostCC32XX_HWAttrsV1 sdhostCC32XXHWattrs[] = {
120  *      {
121  *          .clkRate = 8000000,
122  *          .intPriority = ~0,
123  *          .baseAddr = SDHOST_BASE,
124  *          .rxChIdx = UDMA_CH23_SDHOST_RX,
125  *          .txChIdx = UDMA_CH24_SDHOST_TX,
126  *          .dataPin = SDHostCC32XX_PIN_06_SDCARD_DATA,
127  *          .cmdPin = SDHostCC32XX_PIN_08_SDCARD_CMD,
128  *          .clkPin = SDHostCC32XX_PIN_07_SDCARD_CLK,
129  *      }
130  *  };
131  *  @endcode
132  */
133 typedef struct {
134     /*! SD interface clock rate */
135     uint_fast32_t clkRate;
136 
137     /*! Internal SDHost ISR command/transfer priorty */
138     int_fast32_t intPriority;
139 
140     /*! SDHost Peripheral base address */
141     uint_fast32_t baseAddr;
142 
143     /*! uDMA controlTable receive channel index */
144     unsigned long rxChIdx;
145 
146     /*! uDMA controlTable transmit channel index */
147     unsigned long txChIdx;
148 
149     /*! SD Host Data pin */
150     uint32_t dataPin;
151 
152     /*! SD Host CMD pin */
153     uint32_t cmdPin;
154 
155     /*! SD Host CLK pin */
156     uint32_t clkPin;
157 } SDHostCC32XX_HWAttrsV1;
158 
159 /*!
160  *  @brief  SDHostCC32XX Object
161  *
162  *  The application must not access any member variables of this structure!
163  */
164 typedef struct {
165     /* Relative Card Address */
166     volatile uint32_t     rca;
167     /* Number of sectors written */
168     volatile uint32_t     sectorCount;
169     /* Read data pointer */
170     volatile uint32_t     *buffer;
171     /*
172      *  Semaphore to suspend thread execution when waiting for SD Commands
173      *  or data transfers to complete.
174      */
175     SemaphoreP_Handle      cmdSem;
176     /* SD Card interrupt handle. */
177     HwiP_Handle            hwiHandle;
178     /* UDMA Handle */
179     UDMACC32XX_Handle      dmaHandle;
180     /* Determined from base address */
181     unsigned int           powerMgrId;
182     /* LPDS wake-up notify object */
183     Power_NotifyObj        postNotify;
184     /* Previous park state SDCARD_CLK pin */
185     PowerCC32XX_ParkState  prevParkCLK;
186     /* SDCARD_CLK pin */
187     uint16_t               clkPin;
188     /* SD Card command state */
189     volatile int_fast8_t   stat;
190     /* State of the driver (open or closed) */
191     bool                   isOpen;
192     /* Indicates if a DMA transfer is in a ping or pong state */
193     bool                    ping;
194     /* Indicates if a semaphore has been posted for a DMA event */
195     bool                    dmaPosted;
196     /* SDCard Card Command Class (CCC) */
197     SD_CardType            cardType;
198 } SDHostCC32XX_Object;
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif /* ti_drivers_sd_SDHostCC32XX__include */
205