summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx233/mmc-imx233.c
blob: b2b18c871e343d5fe0aabc5db6b0096d69d765e5 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 by Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "system.h"
#include "mmc.h"
#include "sdmmc.h"
#include "storage.h"
#include "ssp-imx233.h"
#include "pinctrl-imx233.h"
#include "partitions-imx233.h"

/**
 * This code assumes a single eMMC internal flash
 */

#ifdef SANSA_FUZEPLUS
#define MMC_SSP     2
#else
#error You need to configure the ssp to use
#endif

#define MMC_RCA     1

/** When set, this values restrict the windows of the read and writes */
static unsigned mmc_window_start;
static unsigned mmc_window_end;
static bool mmc_window_enable = true;
static long mmc_last_activity = -1;
static bool mmc_is_active = false;
static unsigned mmc_size = 0;
static int mmc_first_drive = 0;

static struct mutex mmc_mutex;

void imx233_mmc_disable_window(void)
{
    mmc_window_enable = false;
}

int mmc_init(void)
{
    mutex_init(&mmc_mutex);
    
    imx233_ssp_start(MMC_SSP);
    imx233_ssp_softreset(MMC_SSP);
    imx233_ssp_set_mode(MMC_SSP, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
    #ifdef SANSA_FUZEPLUS
    /** Sansa Fuze+ has an internal eMMC 8-bit wide flash, power gate is pin PWM3
     * and power up time is 20ms */
    imx233_pinctrl_acquire_pin(1, 29, "emmc power");
    imx233_set_pin_function(1, 29, PINCTRL_FUNCTION_GPIO);
    imx233_enable_gpio_output(1, 29, true);
    imx233_set_gpio_output(1, 29, false);
    sleep(HZ / 5);

    imx233_ssp_setup_ssp2_sd_mmc_pins(true, 8, PINCTRL_DRIVE_8mA);
    #endif
    /* SSPCLK @ 96MHz
     * gives bitrate of 96000 / 240 / 1 = 400kHz */
    imx233_ssp_set_timings(MMC_SSP, 240, 0, 0xffff);
    imx233_ssp_sd_mmc_power_up_sequence(MMC_SSP);
    imx233_ssp_set_bus_width(MMC_SSP, 1);
    imx233_ssp_set_block_size(MMC_SSP, 9);
    /* go to idle state */
    int ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 0, 0, SSP_NO_RESP, NULL, 0, false, false, NULL);
    if(ret != 0)
        return -1;
    /* send op cond until the card respond with busy bit set; it must complete within 1sec */
    unsigned timeout = current_tick + HZ;
    do
    {
        uint32_t ocr;
        ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 1, 0x40ff8000, SSP_SHORT_RESP, NULL, 0, false, false, &ocr);
        if(ret == 0 && ocr & (1 << 31))
            break;
    }while(!TIME_AFTER(current_tick, timeout));

    if(ret != 0)
        return -2;
    /* get CID */
    uint32_t cid[4];
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 2, 0, SSP_LONG_RESP, NULL, 0, false, false, cid);
    if(ret != 0)
        return -3;
    /* Set RCA */
    uint32_t status;
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 3, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
    if(ret != 0)
        return -4;
    /* Select card */
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 7, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
    if(ret != 0)
        return -5;
    /* Check TRAN state */
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 13, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
    if(ret != 0)
        return -6;
    if(((status >> 9) & 0xf) != 4)
        return -7;
    /* Switch to 8-bit bus */
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b70200, SSP_SHORT_RESP, NULL, 0, true, false, &status);
    if(ret != 0)
        return -8;
    /* switch error ? */
    if(status & 0x80)
        return -9;
    imx233_ssp_set_bus_width(MMC_SSP, 8);
    /* Switch to high speed mode */
    ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b90100, SSP_SHORT_RESP, NULL, 0, true, false, &status);
    if(ret != 0)
        return -10;
    /* switch error ?*/
    if(status & 0x80)
        return -11;
    /* SSPCLK @ 96MHz
     * gives bitrate of 96 / 2 / 1 = 48MHz */
    imx233_ssp_set_timings(MMC_SSP, 2, 0, 0xffff);

    /* read extended CSD */
    {
        uint8_t ext_csd[512];
        ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 8, 0, SSP_SHORT_RESP, ext_csd, 1, true, true, &status);
        if(ret != 0)
            return -12;
        uint32_t *sec_count = (void *)&ext_csd[212];
        mmc_size = *sec_count;
    }

    mmc_window_start = 0;
    mmc_window_end = INT_MAX;
    #ifdef SANSA_FUZEPLUS
    if(imx233_partitions_is_window_enabled())
    {
        /* WARNING: mmc_first_drive is not set yet at this point */
        uint8_t mbr[512];
        ret = mmc_read_sectors(IF_MD2(0,) 0, 1, mbr);
        if(ret)
            panicf("cannot read MBR: %d", ret);
        ret = imx233_partitions_compute_window(mbr, &mmc_window_start, &mmc_window_end);
        if(ret)
            panicf("cannot compute partitions window: %d", ret);
        mmc_size = mmc_window_end - mmc_window_start;
    }
    #endif

    return 0;
}

int mmc_num_drives(int first_drive)
{
    mmc_first_drive = first_drive;
    return 1;
}

#ifdef STORAGE_GET_INFO
void mmc_get_info(IF_MD2(int drive,) struct storage_info *info)
{
#ifdef HAVE_MULTIDRIVE
    (void) drive;
#endif
    info->sector_size = 512;
    info->num_sectors = mmc_size;
    info->vendor = "Rockbox";
    info->product = "Internal Storage";
    info->revision = "0.00";
}
#endif

static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf, bool read)
{
    IF_MD((void) drive);
    /* check window */
    start += mmc_window_start;
    if((start + count) > mmc_window_end)
        return -201;
    /* get mutex (needed because we do multiple commands for count > 0) */
    mutex_lock(&mmc_mutex);
    int ret = 0;
    uint32_t resp;

    mmc_last_activity = current_tick;
    mmc_is_active = true;

    do
    {
        int this_count = MIN(count, IMX233_MAX_SSP_XFER_SIZE / 512);
        if(this_count == 1)
        {
            ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 17 : 24, start,
                SSP_SHORT_RESP, buf, this_count, false, read, &resp);
        }
        else
        {
            ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 23, this_count, SSP_SHORT_RESP, NULL,
                0, false, false, &resp);
            if(ret == 0)
                ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 18 : 25, start,
                    SSP_SHORT_RESP, buf, this_count, false, read, &resp);
        }
        count -= this_count;
        start += this_count;
        buf += this_count * 512;
    }while(count != 0 && ret == SSP_SUCCESS);

    mmc_is_active = false;

    mutex_unlock(&mmc_mutex);

    return ret;
}

int mmc_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf)
{
    return transfer_sectors(IF_MD2(drive,) start, count, buf, true);
}

int mmc_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf)
{
    return transfer_sectors(IF_MD2(drive,) start, count, (void *)buf, false);
}

bool mmc_present(IF_MD(int drive))
{
    IF_MD((void) drive);
    return true;
}

bool mmc_removable(IF_MD(int drive))
{
    IF_MD((void) drive);
    return false;
}

void mmc_sleep(void)
{
}

void mmc_sleepnow(void)
{
}

bool mmc_disk_is_active(void)
{
    return mmc_is_active;
}

bool mmc_usb_active(void)
{
    return mmc_disk_is_active();
}

int mmc_soft_reset(void)
{
    return 0;
}

int mmc_flush(void)
{
    return 0;
}

void mmc_spin(void)
{
}

void mmc_spindown(int seconds)
{
    (void) seconds;
}

long mmc_last_disk_activity(void)
{
    return mmc_last_activity;
}

int mmc_spinup_time(void)
{
    return 0;
}

void mmc_enable(bool enable)
{
    (void) enable;
}