]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/sdhc/fsl_sdhc_hal.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / sdhc / fsl_sdhc_hal.c
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "fsl_sdhc_hal.h"
31
32 #ifndef MBED_NO_SDHC
33
34 /*FUNCTION****************************************************************
35  *
36  * Function Name: SDHC_HAL_Init
37  * Description: Initialize sdhc hal
38  *
39  *END*********************************************************************/
40 void SDHC_HAL_Init(uint32_t baseAddr)
41 {
42     SDHC_HAL_SetSdClock(baseAddr, false);
43     SDHC_HAL_SetExternalDmaRequest(baseAddr, false);
44     SDHC_HAL_SetIntState(baseAddr, false, (uint32_t)-1);
45     SDHC_HAL_SetIntSignal(baseAddr, false, (uint32_t)-1);
46 }
47
48 /*FUNCTION****************************************************************
49  *
50  * Function Name: SDHC_HAL_SetIntSignal
51  * Description: Enable specified interrupts
52  *
53  *END*********************************************************************/
54 void SDHC_HAL_SetIntSignal(uint32_t baseAddr, bool enable, uint32_t mask)
55 {
56     if (enable)
57     {
58         HW_SDHC_IRQSIGEN_SET(baseAddr, mask);
59     }
60     else
61     {
62         HW_SDHC_IRQSIGEN_CLR(baseAddr, mask);
63     }
64 }
65
66 /*FUNCTION****************************************************************
67  *
68  * Function Name: SDHC_HAL_SetIntState
69  * Description: Enable specified interrupts' state
70  *
71  *END*********************************************************************/
72 void SDHC_HAL_SetIntState(uint32_t baseAddr, bool enable, uint32_t mask)
73 {
74     if (enable)
75     {
76         HW_SDHC_IRQSTATEN_SET(baseAddr, mask);
77     }
78     else
79     {
80         HW_SDHC_IRQSTATEN_CLR(baseAddr, mask);
81     }
82 }
83
84 /*FUNCTION****************************************************************
85  *
86  * Function Name: SDHC_HAL_GetResponse
87  * Description: get command response
88  *
89  *END*********************************************************************/
90 uint32_t SDHC_HAL_GetResponse(uint32_t baseAddr, uint32_t index)
91 {
92     uint32_t ret = 0;
93
94     assert(index < 4);
95
96     switch(index)
97     {
98         case 0:
99             ret = BR_SDHC_CMDRSP0_CMDRSP0(baseAddr);
100             break;
101         case 1:
102             ret = BR_SDHC_CMDRSP1_CMDRSP1(baseAddr);
103             break;
104         case 2:
105             ret = BR_SDHC_CMDRSP2_CMDRSP2(baseAddr);
106             break;
107         case 3:
108             ret = BR_SDHC_CMDRSP3_CMDRSP3(baseAddr);
109             break;
110         default:
111             break;
112     }
113
114     return ret;
115 }
116
117 /*FUNCTION****************************************************************
118  *
119  * Function Name: SDHC_HAL_InitCard
120  * Description: Initialize card by sending 80 clocks to card
121  *
122  *END*********************************************************************/
123 uint32_t SDHC_HAL_InitCard(uint32_t baseAddr, uint32_t timeout)
124 {
125     assert(timeout);
126     BW_SDHC_SYSCTL_INITA(baseAddr, 1);
127     while((!BR_SDHC_SYSCTL_INITA(baseAddr)))
128     {
129         if (!timeout)
130         {
131             break;
132         }
133         timeout--;
134     }
135     return (!timeout);
136 }
137
138 /*FUNCTION****************************************************************
139  *
140  * Function Name: SDHC_HAL_Reset
141  * Description: Perform different kinds of reset
142  *
143  *END*********************************************************************/
144 uint32_t SDHC_HAL_Reset(uint32_t baseAddr, uint32_t type, uint32_t timeout)
145 {
146     uint32_t mask;
147     assert(timeout);
148     mask = type & (BM_SDHC_SYSCTL_RSTA
149                  | BM_SDHC_SYSCTL_RSTC
150                  | BM_SDHC_SYSCTL_RSTD);
151     HW_SDHC_SYSCTL_SET(baseAddr, mask);
152     while (!(HW_SDHC_SYSCTL_RD(baseAddr) & mask))
153     {
154         if (!timeout)
155         {
156             break;
157         }
158         timeout--;
159     }
160     return (!timeout);
161 }
162
163 #endif /* MBED_NO_SDHC */
164
165 /*************************************************************************************************
166  * EOF
167  ************************************************************************************************/