imx233: rework i2c driver to fix dma issues
[maemo-rb.git] / firmware / target / arm / imx233 / mmc-imx233.c
blobb2b18c871e343d5fe0aabc5db6b0096d69d765e5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "system.h"
23 #include "mmc.h"
24 #include "sdmmc.h"
25 #include "storage.h"
26 #include "ssp-imx233.h"
27 #include "pinctrl-imx233.h"
28 #include "partitions-imx233.h"
30 /**
31 * This code assumes a single eMMC internal flash
34 #ifdef SANSA_FUZEPLUS
35 #define MMC_SSP 2
36 #else
37 #error You need to configure the ssp to use
38 #endif
40 #define MMC_RCA 1
42 /** When set, this values restrict the windows of the read and writes */
43 static unsigned mmc_window_start;
44 static unsigned mmc_window_end;
45 static bool mmc_window_enable = true;
46 static long mmc_last_activity = -1;
47 static bool mmc_is_active = false;
48 static unsigned mmc_size = 0;
49 static int mmc_first_drive = 0;
51 static struct mutex mmc_mutex;
53 void imx233_mmc_disable_window(void)
55 mmc_window_enable = false;
58 int mmc_init(void)
60 mutex_init(&mmc_mutex);
62 imx233_ssp_start(MMC_SSP);
63 imx233_ssp_softreset(MMC_SSP);
64 imx233_ssp_set_mode(MMC_SSP, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
65 #ifdef SANSA_FUZEPLUS
66 /** Sansa Fuze+ has an internal eMMC 8-bit wide flash, power gate is pin PWM3
67 * and power up time is 20ms */
68 imx233_pinctrl_acquire_pin(1, 29, "emmc power");
69 imx233_set_pin_function(1, 29, PINCTRL_FUNCTION_GPIO);
70 imx233_enable_gpio_output(1, 29, true);
71 imx233_set_gpio_output(1, 29, false);
72 sleep(HZ / 5);
74 imx233_ssp_setup_ssp2_sd_mmc_pins(true, 8, PINCTRL_DRIVE_8mA);
75 #endif
76 /* SSPCLK @ 96MHz
77 * gives bitrate of 96000 / 240 / 1 = 400kHz */
78 imx233_ssp_set_timings(MMC_SSP, 240, 0, 0xffff);
79 imx233_ssp_sd_mmc_power_up_sequence(MMC_SSP);
80 imx233_ssp_set_bus_width(MMC_SSP, 1);
81 imx233_ssp_set_block_size(MMC_SSP, 9);
82 /* go to idle state */
83 int ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 0, 0, SSP_NO_RESP, NULL, 0, false, false, NULL);
84 if(ret != 0)
85 return -1;
86 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
87 unsigned timeout = current_tick + HZ;
90 uint32_t ocr;
91 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 1, 0x40ff8000, SSP_SHORT_RESP, NULL, 0, false, false, &ocr);
92 if(ret == 0 && ocr & (1 << 31))
93 break;
94 }while(!TIME_AFTER(current_tick, timeout));
96 if(ret != 0)
97 return -2;
98 /* get CID */
99 uint32_t cid[4];
100 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 2, 0, SSP_LONG_RESP, NULL, 0, false, false, cid);
101 if(ret != 0)
102 return -3;
103 /* Set RCA */
104 uint32_t status;
105 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 3, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
106 if(ret != 0)
107 return -4;
108 /* Select card */
109 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 7, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
110 if(ret != 0)
111 return -5;
112 /* Check TRAN state */
113 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 13, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
114 if(ret != 0)
115 return -6;
116 if(((status >> 9) & 0xf) != 4)
117 return -7;
118 /* Switch to 8-bit bus */
119 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b70200, SSP_SHORT_RESP, NULL, 0, true, false, &status);
120 if(ret != 0)
121 return -8;
122 /* switch error ? */
123 if(status & 0x80)
124 return -9;
125 imx233_ssp_set_bus_width(MMC_SSP, 8);
126 /* Switch to high speed mode */
127 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b90100, SSP_SHORT_RESP, NULL, 0, true, false, &status);
128 if(ret != 0)
129 return -10;
130 /* switch error ?*/
131 if(status & 0x80)
132 return -11;
133 /* SSPCLK @ 96MHz
134 * gives bitrate of 96 / 2 / 1 = 48MHz */
135 imx233_ssp_set_timings(MMC_SSP, 2, 0, 0xffff);
137 /* read extended CSD */
139 uint8_t ext_csd[512];
140 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 8, 0, SSP_SHORT_RESP, ext_csd, 1, true, true, &status);
141 if(ret != 0)
142 return -12;
143 uint32_t *sec_count = (void *)&ext_csd[212];
144 mmc_size = *sec_count;
147 mmc_window_start = 0;
148 mmc_window_end = INT_MAX;
149 #ifdef SANSA_FUZEPLUS
150 if(imx233_partitions_is_window_enabled())
152 /* WARNING: mmc_first_drive is not set yet at this point */
153 uint8_t mbr[512];
154 ret = mmc_read_sectors(IF_MD2(0,) 0, 1, mbr);
155 if(ret)
156 panicf("cannot read MBR: %d", ret);
157 ret = imx233_partitions_compute_window(mbr, &mmc_window_start, &mmc_window_end);
158 if(ret)
159 panicf("cannot compute partitions window: %d", ret);
160 mmc_size = mmc_window_end - mmc_window_start;
162 #endif
164 return 0;
167 int mmc_num_drives(int first_drive)
169 mmc_first_drive = first_drive;
170 return 1;
173 #ifdef STORAGE_GET_INFO
174 void mmc_get_info(IF_MD2(int drive,) struct storage_info *info)
176 #ifdef HAVE_MULTIDRIVE
177 (void) drive;
178 #endif
179 info->sector_size = 512;
180 info->num_sectors = mmc_size;
181 info->vendor = "Rockbox";
182 info->product = "Internal Storage";
183 info->revision = "0.00";
185 #endif
187 static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf, bool read)
189 IF_MD((void) drive);
190 /* check window */
191 start += mmc_window_start;
192 if((start + count) > mmc_window_end)
193 return -201;
194 /* get mutex (needed because we do multiple commands for count > 0) */
195 mutex_lock(&mmc_mutex);
196 int ret = 0;
197 uint32_t resp;
199 mmc_last_activity = current_tick;
200 mmc_is_active = true;
204 int this_count = MIN(count, IMX233_MAX_SSP_XFER_SIZE / 512);
205 if(this_count == 1)
207 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 17 : 24, start,
208 SSP_SHORT_RESP, buf, this_count, false, read, &resp);
210 else
212 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 23, this_count, SSP_SHORT_RESP, NULL,
213 0, false, false, &resp);
214 if(ret == 0)
215 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 18 : 25, start,
216 SSP_SHORT_RESP, buf, this_count, false, read, &resp);
218 count -= this_count;
219 start += this_count;
220 buf += this_count * 512;
221 }while(count != 0 && ret == SSP_SUCCESS);
223 mmc_is_active = false;
225 mutex_unlock(&mmc_mutex);
227 return ret;
230 int mmc_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf)
232 return transfer_sectors(IF_MD2(drive,) start, count, buf, true);
235 int mmc_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf)
237 return transfer_sectors(IF_MD2(drive,) start, count, (void *)buf, false);
240 bool mmc_present(IF_MD(int drive))
242 IF_MD((void) drive);
243 return true;
246 bool mmc_removable(IF_MD(int drive))
248 IF_MD((void) drive);
249 return false;
252 void mmc_sleep(void)
256 void mmc_sleepnow(void)
260 bool mmc_disk_is_active(void)
262 return mmc_is_active;
265 bool mmc_usb_active(void)
267 return mmc_disk_is_active();
270 int mmc_soft_reset(void)
272 return 0;
275 int mmc_flush(void)
277 return 0;
280 void mmc_spin(void)
284 void mmc_spindown(int seconds)
286 (void) seconds;
289 long mmc_last_disk_activity(void)
291 return mmc_last_activity;
294 int mmc_spinup_time(void)
296 return 0;
299 void mmc_enable(bool enable)
301 (void) enable;