summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/sdmmc/hal_sdpin.c
blob: 3d47c2c010530e92acf946be6950000401e412c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
  * @file  hal_sdhost.c
  * @author  ALLWINNERTECH IOT WLAN Team
  */

/*
 * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the
 *       distribution.
 *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
 *       its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, )|hhst->sdio_irq_maskPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <hal_gpio.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef CONFIG_KERNEL_FREERTOS
#include <log.h>
#endif
#include "_sdhost.h"
#include "_sd_define.h"

#ifndef SDC_MAX_PIN_NUM
#define SDC_MAX_PIN_NUM   13
#endif

typedef struct
{
    uint32_t *pin;
    uint8_t pin_num : 3;
    uint8_t pin_mux : 3;
    uint8_t pin_drv : 2;
} sdmmc_pin_t;

static sdmmc_pin_t sunxi_sdmmc_pin[4];


int32_t sdmmc_pinctl_set_from_cfg(struct mmc_host *host, char *sdc_str, uint32_t pin_num)
{
#ifdef HAL_SetPin
    int32_t ret = 0;
    user_gpio_set_t gpiocfg[SDC_MAX_PIN_NUM] = {0};
    int i = 0;
    int gpio_num = 0;
    if (pin_num > SDC_MAX_PIN_NUM) {
        SDC_LOGE("pin num over %d\n", SDC_MAX_PIN_NUM);
        return ret;
    }

    ret = Hal_Cfg_GetGPIOSecData(sdc_str, gpiocfg, pin_num);
    if(ret < 0) {
        SDC_LOGE("%s not has pin setting on sys_config.fex\n", sdc_str);
        return ret;
    }

    for (i = 0;i < pin_num; i++) {
        SDC_LOGD("name %s,port %d,port_num %d,mul_sel %d, pull %d, drv_level %d\n",\
                gpiocfg[i].gpio_name, gpiocfg[i].port, gpiocfg[i].port_num, \
                gpiocfg[i].mul_sel, gpiocfg[i].pull, gpiocfg[i].drv_level);
    }

    for(i = 0; i< pin_num; i++ ) {
        gpio_num = (gpiocfg[i].port - 1) * PINS_PER_BANK + gpiocfg[i].port_num;
        ret = hal_gpio_pinmux_set_function(gpio_num, gpiocfg[i].mul_sel);
        if (ret) {
            SDC_LOGE(
                "[sdmmc %s] PIN%lu set function failed! return %d\n",
                sdc_str, gpio_num, ret);
            return -1;
        }

        ret = hal_gpio_set_driving_level(gpio_num, gpiocfg[i].drv_level);
        if (ret) {
            SDC_LOGE(
                "[sdmmc %s] PIN%lu set driving level failed! return %d\n",
                sdc_str, gpio_num, ret);
            return -1;
        }
        ret = hal_gpio_set_pull(gpio_num, gpiocfg[i].pull);
        if (ret) {
            SDC_LOGE(
                "[sdmmc %s] PIN%lu set driving level failed! return %d\n",
                sdc_str, gpio_num, ret);
            return -1;
        }
    }
    return ret;
#else
    SDC_LOGN("unsupport sys fex %d\n");
    return -1;
#endif
}


uint32_t sdmmc_pinctrl_init(struct mmc_host *host)
{
    uint8_t i;
    uint32_t flags = 0;
    int ret;
    uint32_t host_id = host->sdc_id;

    switch (host_id) {
        case 0:
            ret = sdmmc_pinctl_set_from_cfg(host, "sdc0", SDC0_NUM);
            if(ret < 0) {
                sunxi_sdmmc_pin[host_id].pin_num = SDC0_NUM;
                sunxi_sdmmc_pin[host_id].pin_mux = SDMMC_MUXSEL;
                sunxi_sdmmc_pin[host_id].pin_drv = SDMMC_DRVSEL;
                sunxi_sdmmc_pin[host_id].pin = malloc(sizeof(uint32_t) * SDC0_NUM);
                sunxi_sdmmc_pin[host_id].pin[0] = SDC0_CLK;
                sunxi_sdmmc_pin[host_id].pin[1] = SDC0_CMD;
                sunxi_sdmmc_pin[host_id].pin[2] = SDC0_D0;
                sunxi_sdmmc_pin[host_id].pin[3] = SDC0_D1;
                sunxi_sdmmc_pin[host_id].pin[4] = SDC0_D2;
                sunxi_sdmmc_pin[host_id].pin[5] = SDC0_D3;
                SDC_LOGE("sdmmc%ld use default pin setting\n", host_id);
            } else {
                SDC_LOGD("sdmmc%ld use pin setting on sys_config.fex\n", host_id);
                goto out;
            }
            break;
        case 1:
            if (host->param.pwr_mode == POWER_MODE_330) {
                hal_gpio_sel_vol_mode(SDC1_D0, POWER_MODE_330);
            } else {
                hal_gpio_sel_vol_mode(SDC1_D0, POWER_MODE_180);
            }
            ret = sdmmc_pinctl_set_from_cfg(host, "sdc1", SDC1_NUM);
            if(ret < 0) {
                            sunxi_sdmmc_pin[host_id].pin_num = SDC1_NUM;
                            sunxi_sdmmc_pin[host_id].pin_mux = SDMMC_MUXSEL;
                            sunxi_sdmmc_pin[host_id].pin_drv = SDMMC_DRVSEL;
                            sunxi_sdmmc_pin[host_id].pin = malloc(sizeof(uint32_t) * SDC1_NUM);
                            sunxi_sdmmc_pin[host_id].pin[0] = SDC1_CLK;
                            sunxi_sdmmc_pin[host_id].pin[1] = SDC1_CMD;
                            sunxi_sdmmc_pin[host_id].pin[2] = SDC1_D0;
                            // sunxi_sdmmc_pin[host_id].pin[3] = SDC1_D1;
                            // sunxi_sdmmc_pin[host_id].pin[4] = SDC1_D2;
                            // sunxi_sdmmc_pin[host_id].pin[5] = SDC1_D3;
                SDC_LOGE("sdmmc%ld use default pin setting\n", host_id);
            } else {
                SDC_LOGD("sdmmc%ld use pin setting on sys_config.fex\n", host_id);
                goto out;
            }
            break;
        default:
            SDC_LOGE("sdmmc%ld is invalid\n", host_id);
            return -1;

    }

    for (i = 0; i < sunxi_sdmmc_pin[host_id].pin_num; i++)
    {
        ret = hal_gpio_pinmux_set_function(sunxi_sdmmc_pin[host_id].pin[i], sunxi_sdmmc_pin[host_id].pin_mux);
        if (ret)
        {
            SDC_LOGE(
                "[sdmmc%ld] PIN%lu set function failed! return %d\n",
                host_id, sunxi_sdmmc_pin[host_id].pin[i], ret);
            return -1;
        }
        ret = hal_gpio_set_driving_level(sunxi_sdmmc_pin[host_id].pin[i], sunxi_sdmmc_pin[host_id].pin_drv);
        if (ret)
        {
            SDC_LOGE(
                "[sdmmc%ld] PIN%lu set driving level failed! return %d\n",
                host_id, sunxi_sdmmc_pin[host_id].pin[i], ret);
            return -1;
        }
        ret = hal_gpio_set_pull(sunxi_sdmmc_pin[host_id].pin[i], GPIO_PULL_UP);
        // ret = drv_gpio_set_pull_state(sunxi_sdmmc_pin[host_id].pin[i], DRV_GPIO_PULL_DOWN_DISABLE);
    }
out:
    return 0;
}

int mmc_gpiod_request_cd_irq(struct mmc_host *host)
{
    uint32_t irq;
    int ret = 0;
    gpio_pull_status_t pull_state;
    gpio_direction_t gpio_direction;
    gpio_data_t gpio_data;

    host->cd_gpio_pin = SDC0_DET;
    /*set gpio detect-clk 24M*/
    hal_gpio_set_debounce(host->cd_gpio_pin, 1);
    ret = hal_gpio_to_irq(host->cd_gpio_pin, &irq);
    if (ret < 0)
    {
        SDC_LOGE("gpio to irq error, error num: %d\n", ret);
        return ret;
    }

    /*set pin mux*/
    ret = hal_gpio_pinmux_set_function(host->cd_gpio_pin, 0);
    ret = hal_gpio_set_driving_level(host->cd_gpio_pin, 3);
    ret = hal_gpio_set_pull(host->cd_gpio_pin, 1);
    if (ret < 0)
    {
        SDC_LOGE("set pin mux error!\n");
        return -1;
    }

    host->cd_irq = irq;
    ret = hal_gpio_irq_request(irq, host->cd_gpio_isr, IRQ_TYPE_EDGE_BOTH, host);
    if (ret < 0)
    {
        SDC_LOGE("request irq error, irq num:%lu error num: %d\n", (unsigned long)irq, ret);
        return ret;
    }

#if 0
    ret = drv_gpio_irq_enable(irq);
    if (ret < 0)
    {
        printf("request irq error, error num: %d\n", ret);
        return ret;
    }
    ret = drv_gpio_irq_disable(irq);
    if (ret < 0)
    {
        printf("disable irq error, irq num:%lu,error num: %d\n", irq, ret);
        return ret;
    }

    ret = drv_gpio_irq_free(irq);
    if (ret < 0)
    {
        printf("free irq error, error num: %d\n", ret);
        return ret;
    }
#endif
    return ret;
}