hm60x/hm801: Buttons rework.
[maemo-rb.git] / firmware / target / arm / imx233 / mmc-imx233.c
blob8782e8e8af8dcd6c2d8ae95da324d540cfaadbbc
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"
29 /**
30 * This code assumes a single eMMC internal flash
33 #ifdef SANSA_FUZEPLUS
34 #define MMC_SSP 2
35 #else
36 #error You need to configure the ssp to use
37 #endif
39 #define MMC_RCA 1
41 /** When set, this values restrict the windows of the read and writes */
42 static unsigned mmc_window_start = 0;
43 static unsigned mmc_window_end = INT_MAX;
44 static bool mmc_window_enable = true;
45 static long mmc_last_activity = -1;
46 static bool mmc_is_active = false;
47 static unsigned mmc_size = 0;
49 static struct mutex mmc_mutex;
51 void imx233_mmc_disable_window(void)
53 mmc_window_enable = false;
56 int mmc_init(void)
58 mutex_init(&mmc_mutex);
60 imx233_ssp_start(MMC_SSP);
61 imx233_ssp_softreset(MMC_SSP);
62 imx233_ssp_set_mode(MMC_SSP, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
63 #ifdef SANSA_FUZEPLUS
64 /** Sansa Fuze+ has an internal eMMC 8-bit wide flash, power gate is pin PWM3
65 * and power up time is 20ms */
66 imx233_set_pin_function(1, 29, PINCTRL_FUNCTION_GPIO);
67 imx233_enable_gpio_output(1, 29, true);
68 imx233_set_gpio_output(1, 29, false);
69 sleep(HZ / 5);
71 imx233_ssp_setup_ssp2_sd_mmc_pins(true, 8, PINCTRL_DRIVE_8mA);
72 #endif
73 /* SSPCLK @ 96MHz
74 * gives bitrate of 96000 / 240 / 1 = 400kHz */
75 imx233_ssp_set_timings(MMC_SSP, 240, 0, 0xffff);
76 imx233_ssp_sd_mmc_power_up_sequence(MMC_SSP);
77 imx233_ssp_set_bus_width(MMC_SSP, 1);
78 imx233_ssp_set_block_size(MMC_SSP, 9);
79 /* go to idle state */
80 int ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 0, 0, SSP_NO_RESP, NULL, 0, false, false, NULL);
81 if(ret != 0)
82 return -1;
83 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
84 unsigned timeout = current_tick + HZ;
87 uint32_t ocr;
88 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 1, 0x40ff8000, SSP_SHORT_RESP, NULL, 0, false, false, &ocr);
89 if(ret == 0 && ocr & (1 << 31))
90 break;
91 }while(!TIME_AFTER(current_tick, timeout));
93 if(ret != 0)
94 return -2;
95 /* get CID */
96 uint32_t cid[4];
97 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 2, 0, SSP_LONG_RESP, NULL, 0, false, false, cid);
98 if(ret != 0)
99 return -3;
100 /* Set RCA */
101 uint32_t status;
102 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 3, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
103 if(ret != 0)
104 return -4;
105 /* Select card */
106 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 7, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
107 if(ret != 0)
108 return -5;
109 /* Check TRAN state */
110 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 13, MMC_RCA << 16, SSP_SHORT_RESP, NULL, 0, false, false, &status);
111 if(ret != 0)
112 return -6;
113 if(((status >> 9) & 0xf) != 4)
114 return -7;
115 /* Switch to 8-bit bus */
116 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b70200, SSP_SHORT_RESP, NULL, 0, true, false, &status);
117 if(ret != 0)
118 return -8;
119 /* switch error ? */
120 if(status & 0x80)
121 return -9;
122 imx233_ssp_set_bus_width(MMC_SSP, 8);
123 /* Switch to high speed mode */
124 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 6, 0x3b90100, SSP_SHORT_RESP, NULL, 0, true, false, &status);
125 if(ret != 0)
126 return -10;
127 /* switch error ?*/
128 if(status & 0x80)
129 return -11;
130 /* SSPCLK @ 96MHz
131 * gives bitrate of 96 / 2 / 1 = 48MHz */
132 imx233_ssp_set_timings(MMC_SSP, 2, 0, 0xffff);
134 /* read extended CSD */
136 uint8_t ext_csd[512];
137 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 8, 0, SSP_SHORT_RESP, ext_csd, 1, true, true, &status);
138 if(ret != 0)
139 return -12;
140 uint32_t *sec_count = (void *)&ext_csd[212];
141 mmc_size = *sec_count;
144 #ifdef SANSA_FUZEPLUS
145 if(mmc_window_enable)
148 * The Fuze+ uses a strange layout: is has a first MBR at sector 0 with four entries:
149 * 1) Actual user partition
150 * 2) Sigmatel boot partition
151 * 3)4) Other (certificate related ?) partitions
152 * The partition 1) has type 1 but it's actually a type 5 (logical partition) with
153 * a second partition table with usually one entry which is the FAT32 one.
154 * The first table uses 512-byte sector size and the second one usually uses
155 * 2048-byte logical sector size.
157 * We restrict mmc window to the user partition */
158 uint8_t mbr[512];
159 mmc_window_start = 0;
160 mmc_window_end = INT_MAX;
161 ret = mmc_read_sectors(IF_MD2(0,) 0, 1, mbr);
162 if(ret != 0)
163 return -100;
164 if(mbr[510] != 0x55 || mbr[511] != 0xAA)
165 return -101; /* invalid MBR */
166 /* sanity check that the first partition is greater than 2Gib */
167 uint8_t *ent = &mbr[446];
168 mmc_window_start = ent[8] | ent[9] << 8 | ent[10] << 16 | ent[11] << 24;
169 mmc_window_end = (ent[12] | ent[13] << 8 | ent[14] << 16 | ent[15] << 24) +
170 mmc_window_start;
171 if(ent[4] == 0x53)
172 return -102; /* sigmatel partition */
173 if((mmc_window_end - mmc_window_start) < 4 * 1024 * 1024)
174 return -103; /* partition too small */
175 mmc_size = mmc_window_end - mmc_window_start;
177 #endif
179 return 0;
182 int mmc_num_drives(int first_drive)
184 (void) first_drive;
185 return 1;
188 #ifdef STORAGE_GET_INFO
189 void mmc_get_info(IF_MD2(int drive,) struct storage_info *info)
191 #ifdef HAVE_MULTIDRIVE
192 (void) drive;
193 #endif
194 info->sector_size = 512;
195 info->num_sectors = mmc_size;
196 info->vendor = "Rockbox";
197 info->product = "Internal Storage";
198 info->revision = "0.00";
200 #endif
202 static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf, bool read)
204 IF_MD((void) drive);
205 /* check window */
206 start += mmc_window_start;
207 if((start + count) > mmc_window_end)
208 return -201;
209 /* get mutex (needed because we do multiple commands for count > 0) */
210 mutex_lock(&mmc_mutex);
211 int ret = 0;
212 uint32_t resp;
214 mmc_last_activity = current_tick;
215 mmc_is_active = true;
219 int this_count = MIN(count, IMX233_MAX_SSP_XFER_SIZE / 512);
220 if(this_count == 1)
222 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 17 : 24, start,
223 SSP_SHORT_RESP, buf, this_count, false, read, &resp);
225 else
227 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, 23, this_count, SSP_SHORT_RESP, NULL,
228 0, false, false, &resp);
229 if(ret == 0)
230 ret = imx233_ssp_sd_mmc_transfer(MMC_SSP, read ? 18 : 25, start,
231 SSP_SHORT_RESP, buf, this_count, false, read, &resp);
233 count -= this_count;
234 start += this_count;
235 buf += this_count * 512;
236 }while(count != 0 && ret == SSP_SUCCESS);
238 mmc_is_active = false;
240 mutex_unlock(&mmc_mutex);
242 return ret;
245 int mmc_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf)
247 return transfer_sectors(IF_MD2(drive,) start, count, buf, true);
250 int mmc_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf)
252 return transfer_sectors(IF_MD2(drive,) start, count, (void *)buf, false);
255 bool mmc_present(IF_MD(int drive))
257 IF_MD((void) drive);
258 return true;
261 bool mmc_removable(IF_MD(int drive))
263 IF_MD((void) drive);
264 return false;
267 void mmc_sleep(void)
271 void mmc_sleepnow(void)
275 bool mmc_disk_is_active(void)
277 return mmc_is_active;
280 bool mmc_usb_active(void)
282 return mmc_disk_is_active();
285 int mmc_soft_reset(void)
287 return 0;
290 int mmc_flush(void)
292 return 0;
295 void mmc_spin(void)
299 void mmc_spindown(int seconds)
301 (void) seconds;
304 long mmc_last_disk_activity(void)
306 return mmc_last_activity;
309 int mmc_spinup_time(void)
311 return 0;
314 void mmc_enable(bool enable)
316 (void) enable;