imx233: simplify sd/mmc driver
[maemo-rb.git] / firmware / target / arm / imx233 / sdmmc-imx233.c
blob5309f844f2ea7a15d5812457e0cb29062af171ad
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 "sd.h"
24 #include "mmc.h"
25 #include "sdmmc.h"
26 #include "ssp-imx233.h"
27 #include "pinctrl-imx233.h"
28 #include "partitions-imx233.h"
29 #include "button-target.h"
30 #include "fat.h"
31 #include "disk.h"
32 #include "usb.h"
33 #include "debug.h"
34 #include "string.h"
36 /** NOTE For convenience, this drivers relies on the many similar commands
37 * between SD and MMC. The following assumptions are made:
38 * - SD_SEND_STATUS = MMC_SEND_STATUS
39 * - SD_SELECT_CARD = MMC_SELECT_CARD
40 * - SD_TRAN = MMC_TRAN
41 * - MMC_WRITE_MULTIPLE_BLOCK = SD_WRITE_MULTIPLE_BLOCK
42 * - MMC_READ_MULTIPLE_BLOCK = SD_READ_MULTIPLE_BLOCK
43 * - SD_STOP_TRANSMISSION = MMC_STOP_TRANSMISSION
44 * - SD_DESELECT_CARD = MMC_DESELECT_CARD
46 #if SD_SEND_STATUS != MMC_SEND_STATUS || SD_SELECT_CARD != MMC_SELECT_CARD || \
47 SD_TRAN != MMC_TRAN || MMC_WRITE_MULTIPLE_BLOCK != SD_WRITE_MULTIPLE_BLOCK || \
48 MMC_READ_MULTIPLE_BLOCK != SD_READ_MULTIPLE_BLOCK || \
49 SD_STOP_TRANSMISSION != MMC_STOP_TRANSMISSION || \
50 SD_DESELECT_CARD != MMC_DESELECT_CARD
51 #error SD/MMC mismatch
52 #endif
54 struct sdmmc_config_t
56 const char *name; /* name(for debug) */
57 int flags; /* flags */
58 int power_pin; /* power pin */
59 int power_delay; /* extra power up delay */
60 int ssp; /* associated ssp block */
61 int mode; /* mode (SD vs MMC) */
64 /* flags */
65 #define POWER_PIN (1 << 0)
66 #define POWER_INVERTED (1 << 1)
67 #define REMOVABLE (1 << 2)
68 #define DETECT_INVERTED (1 << 3)
69 #define POWER_DELAY (1 << 4)
70 #define WINDOW (1 << 5)
72 /* modes */
73 #define SD_MODE 0
74 #define MMC_MODE 1
76 #define PIN(bank,pin) ((bank) << 5 | (pin))
77 #define PIN2BANK(v) ((v) >> 5)
78 #define PIN2PIN(v) ((v) & 0x1f)
80 struct sdmmc_config_t sdmmc_config[] =
82 #ifdef SANSA_FUZEPLUS
83 /* The Fuze+ uses pin #B0P8 for power */
85 .name = "microSD",
86 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE,
87 .power_pin = PIN(0, 8),
88 .ssp = 1,
89 .mode = SD_MODE,
91 /* The Fuze+ uses pin #B1P29 for power */
93 .name = "eMMC",
94 .flags = POWER_PIN | POWER_INVERTED | WINDOW | POWER_DELAY,
95 .power_pin = PIN(1, 29),
96 .power_delay = HZ / 5, /* extra delay, to ramp up voltage? */
97 .ssp = 2,
98 .mode = MMC_MODE,
100 #elif defined(CREATIVE_ZENXFI2)
101 /* The Zen X-Fi2 uses pin B1P29 for power*/
103 .name = "microSD",
104 .flags = POWER_PIN | REMOVABLE | DETECT_INVERTED,
105 .power_pin = PIN(1, 29),
106 .ssp = 1,
107 .mode = SD_MODE,
109 #elif defined(CREATIVE_ZENXFI3)
111 .name = "internal/SD",
112 .flags = WINDOW,
113 .ssp = 2,
114 .mode = SD_MODE,
116 /* The Zen X-Fi3 uses pin #B0P07 for power*/
118 .name = "microSD",
119 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE | POWER_DELAY,
120 .power_pin = PIN(0, 7),
121 .power_delay = HZ / 10, /* extra delay, to ramp up voltage? */
122 .ssp = 1,
123 .mode = SD_MODE,
125 #else
126 #error You need to write the sd/mmc config!
127 #endif
130 #define SDMMC_NUM_DRIVES (sizeof(sdmmc_config) / sizeof(sdmmc_config[0]))
132 #define SDMMC_CONF(drive) sdmmc_config[drive]
133 #define SDMMC_FLAGS(drive) SDMMC_CONF(drive).flags
134 #define SDMMC_SSP(drive) SDMMC_CONF(drive).ssp
135 #define SDMMC_MODE(drive) SDMMC_CONF(drive).mode
137 /** WARNING
138 * to be consistent with all our SD drivers, the .rca field of sdmmc_card_info
139 * in reality holds (rca << 16) because all command arguments actually require
140 * the RCA is the 16-bit msb. Be careful that this is not the actuall RCA ! */
142 /* common */
143 static unsigned window_start[SDMMC_NUM_DRIVES];
144 static unsigned window_end[SDMMC_NUM_DRIVES];
145 static uint8_t aligned_buffer[SDMMC_NUM_DRIVES][512] CACHEALIGN_ATTR;
146 static tCardInfo sdmmc_card_info[SDMMC_NUM_DRIVES];
147 static struct mutex mutex[SDMMC_NUM_DRIVES];
148 static int disk_last_activity[SDMMC_NUM_DRIVES];
150 #define SDMMC_INFO(drive) sdmmc_card_info[drive]
151 #define SDMMC_RCA(drive) SDMMC_INFO(drive).rca
153 /* sd only */
154 #if CONFIG_STORAGE & STORAGE_SD
155 static long sd_stack[(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
156 static const char sd_thread_name[] = "sd";
157 static struct event_queue sd_queue;
158 static int sd_first_drive;
159 static unsigned _sd_num_drives;
160 static int sd_map[SDMMC_NUM_DRIVES]; /* sd->sdmmc map */
161 #endif
162 /* mmc only */
163 #if CONFIG_STORAGE & STORAGE_MMC
164 static int mmc_first_drive;
165 static unsigned _mmc_num_drives;
166 static int mmc_map[SDMMC_NUM_DRIVES]; /* mmc->sdmmc map */
167 #endif
169 /* WARNING NOTE BUG FIXME
170 * There are three numbering schemes involved in the driver:
171 * - the sdmmc indexes into sdmmc_config[]
172 * - the sd drive indexes
173 * - the mmc drive indexes
174 * By convention, [drive] refers to a sdmmc index whereas sd_drive/mmc_drive
175 * refer to sd/mmc drive indexes. We keep two maps sd->sdmmc and mmc->sdmmc
176 * to find the sdmmc index from the sd or mmc one */
178 static void sdmmc_detect_callback(int ssp)
180 /* This is called only if the state was stable for 300ms - check state
181 * and post appropriate event. */
182 if(imx233_ssp_sdmmc_detect(ssp))
183 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
184 else
185 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
186 imx233_ssp_sdmmc_setup_detect(ssp, true, sdmmc_detect_callback, false,
187 imx233_ssp_sdmmc_is_detect_inverted(ssp));
190 static void sdmmc_power(int drive, bool on)
192 /* power chip if needed */
193 if(SDMMC_FLAGS(drive) & POWER_PIN)
195 int bank = PIN2BANK(SDMMC_CONF(drive).power_pin);
196 int pin = PIN2PIN(SDMMC_CONF(drive).power_pin);
197 imx233_pinctrl_acquire_pin(bank, pin, "sd/mmc power");
198 imx233_set_pin_function(bank, pin, PINCTRL_FUNCTION_GPIO);
199 imx233_enable_gpio_output(bank, pin, true);
200 if(SDMMC_FLAGS(drive) & POWER_INVERTED)
201 imx233_set_gpio_output(bank, pin, !on);
202 else
203 imx233_set_gpio_output(bank, pin, on);
205 if(SDMMC_FLAGS(drive) & POWER_DELAY)
206 sleep(SDMMC_CONF(drive).power_delay);
207 /* setup pins, never use alternatives pin on SSP1 because no device use it
208 * but this could be made a flag */
209 int bus_width = SDMMC_MODE(drive) == MMC_MODE ? 8 : 4;
210 if(SDMMC_SSP(drive) == 1)
211 imx233_ssp_setup_ssp1_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA, false);
212 else
213 imx233_ssp_setup_ssp2_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA);
216 #define MCI_NO_RESP 0
217 #define MCI_RESP (1<<0)
218 #define MCI_LONG_RESP (1<<1)
219 #define MCI_ACMD (1<<2) /* sd only */
220 #define MCI_NOCRC (1<<3)
221 #define MCI_BUSY (1<<4)
223 static bool send_cmd(int drive, uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp)
225 if((flags & MCI_ACMD) && !send_cmd(drive, SD_APP_CMD, SDMMC_RCA(drive), MCI_RESP, resp))
226 return false;
228 enum imx233_ssp_resp_t resp_type = (flags & MCI_LONG_RESP) ? SSP_LONG_RESP :
229 (flags & MCI_RESP) ? SSP_SHORT_RESP : SSP_NO_RESP;
230 enum imx233_ssp_error_t ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive), cmd,
231 arg, resp_type, NULL, 0, !!(flags & MCI_BUSY), false, resp);
232 if(resp_type == SSP_LONG_RESP)
234 /* Our SD codes assume most significant word first, so reverse resp */
235 uint32_t tmp = resp[0];
236 resp[0] = resp[3];
237 resp[3] = tmp;
238 tmp = resp[1];
239 resp[1] = resp[2];
240 resp[2] = tmp;
242 return ret == SSP_SUCCESS;
245 static int wait_for_state(int drive, unsigned state)
247 unsigned long response;
248 unsigned int timeout = current_tick + 5*HZ;
249 int cmd_retry = 10;
251 while (1)
253 /* NOTE: rely on SD_SEND_STATUS=MMC_SEND_STATUS */
254 while(!send_cmd(drive, SD_SEND_STATUS, SDMMC_RCA(drive), MCI_RESP, &response) && cmd_retry > 0)
255 cmd_retry--;
257 if(cmd_retry <= 0)
258 return -1;
260 if(((response >> 9) & 0xf) == state)
261 return 0;
263 if(TIME_AFTER(current_tick, timeout))
264 return -10 * ((response >> 9) & 0xf);
266 disk_last_activity[drive] = current_tick;
269 return 0;
272 #if CONFIG_STORAGE & STORAGE_SD
273 static int init_sd_card(int drive)
275 int ssp = SDMMC_SSP(drive);
276 sdmmc_power(drive, false);
277 sdmmc_power(drive, true);
278 imx233_ssp_start(ssp);
279 imx233_ssp_softreset(ssp);
280 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
281 /* SSPCLK @ 96MHz
282 * gives bitrate of 96000 / 240 / 1 = 400kHz */
283 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
285 imx233_ssp_sd_mmc_power_up_sequence(ssp);
286 imx233_ssp_set_bus_width(ssp, 1);
287 imx233_ssp_set_block_size(ssp, 9);
289 SDMMC_RCA(drive) = 0;
290 bool sd_v2 = false;
291 uint32_t resp;
292 long init_timeout;
293 /* go to idle state */
294 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
295 return -1;
296 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
297 Non v2 cards will not respond to this command */
298 if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &resp))
299 if((resp & 0xFFF) == 0x1AA)
300 sd_v2 = true;
301 /* timeout for initialization is 1sec, from SD Specification 2.00 */
302 init_timeout = current_tick + HZ;
305 /* this timeout is the only valid error for this loop*/
306 if(TIME_AFTER(current_tick, init_timeout))
307 return -2;
309 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
310 if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
311 MCI_ACMD|MCI_NOCRC|MCI_RESP, &SDMMC_INFO(drive).ocr))
312 return -100;
313 } while(!(SDMMC_INFO(drive).ocr & (1<<31)));
315 /* CMD2 send CID */
316 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, SDMMC_INFO(drive).cid))
317 return -3;
319 /* CMD3 send RCA */
320 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &SDMMC_INFO(drive).rca))
321 return -4;
323 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
324 if(sd_v2)
326 /* CMD7 w/rca: Select card to put it in TRAN state */
327 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, NULL))
328 return -5;
330 if(wait_for_state(drive, SD_TRAN))
331 return -6;
333 /* CMD6 */
334 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
335 return -7;
336 sleep(HZ/10);
338 /* go back to STBY state so we can read csd */
339 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
340 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
341 return -8;
344 /* CMD9 send CSD */
345 if(!send_cmd(drive, SD_SEND_CSD, SDMMC_RCA(drive), MCI_RESP|MCI_LONG_RESP,
346 SDMMC_INFO(drive).csd))
347 return -9;
349 sd_parse_csd(&SDMMC_INFO(drive));
350 window_start[drive] = 0;
351 window_end[drive] = SDMMC_INFO(drive).numblocks;
353 /* SSPCLK @ 96MHz
354 * gives bitrate of 96 / 4 / 1 = 24MHz */
355 imx233_ssp_set_timings(ssp, 4, 0, 0xffff);
357 /* CMD7 w/rca: Select card to put it in TRAN state */
358 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &resp))
359 return -12;
360 if(wait_for_state(drive, SD_TRAN))
361 return -13;
363 /* ACMD6: set bus width to 4-bit */
364 if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp))
365 return -15;
366 /* ACMD42: disconnect the pull-up resistor on CD/DAT3 */
367 if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP|MCI_ACMD, &resp))
368 return -17;
370 /* Switch to 4-bit */
371 imx233_ssp_set_bus_width(ssp, 4);
373 SDMMC_INFO(drive).initialized = 1;
375 return 0;
377 #endif
379 #if CONFIG_STORAGE & STORAGE_MMC
380 static int init_mmc_drive(int drive)
382 int ssp = SDMMC_SSP(drive);
383 /* we can choose the RCA of mmc cards: pick drive. Following our convention,
384 * .rca is actually RCA << 16 */
385 SDMMC_RCA(drive) = drive << 16;
387 sdmmc_power(drive, false);
388 sdmmc_power(drive, true);
389 imx233_ssp_start(ssp);
390 imx233_ssp_softreset(ssp);
391 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
392 /* SSPCLK @ 96MHz
393 * gives bitrate of 96000 / 240 / 1 = 400kHz */
394 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
395 imx233_ssp_sd_mmc_power_up_sequence(ssp);
396 imx233_ssp_set_bus_width(ssp, 1);
397 imx233_ssp_set_block_size(ssp, 9);
398 /* go to idle state */
399 if(!send_cmd(drive, MMC_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
400 return -1;
401 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
402 unsigned timeout = current_tick + HZ;
403 bool ret = false;
406 uint32_t ocr;
407 ret = send_cmd(drive, MMC_SEND_OP_COND, 0x40ff8000, MCI_RESP, &ocr);
408 if(ret && ocr & (1 << 31))
409 break;
410 }while(!TIME_AFTER(current_tick, timeout));
412 if(!ret)
413 return -2;
414 /* get CID */
415 uint32_t cid[4];
416 if(!send_cmd(drive, MMC_ALL_SEND_CID, 0, MCI_LONG_RESP, cid))
417 return -3;
418 /* Set RCA */
419 uint32_t status;
420 if(!send_cmd(drive, MMC_SET_RELATIVE_ADDR, SDMMC_RCA(drive), MCI_RESP, &status))
421 return -4;
422 /* Select card */
423 if(!send_cmd(drive, MMC_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &status))
424 return -5;
425 /* Check TRAN state */
426 if(wait_for_state(drive, MMC_TRAN))
427 return -6;
428 /* Switch to 8-bit bus */
429 if(!send_cmd(drive, MMC_SWITCH, 0x3b70200, MCI_RESP|MCI_BUSY, &status))
430 return -8;
431 /* switch error ? */
432 if(status & 0x80)
433 return -9;
434 imx233_ssp_set_bus_width(ssp, 8);
435 /* Switch to high speed mode */
436 if(!send_cmd(drive, MMC_SWITCH, 0x3b90100, MCI_RESP|MCI_BUSY, &status))
437 return -10;
438 /* switch error ?*/
439 if(status & 0x80)
440 return -11;
441 /* SSPCLK @ 96MHz
442 * gives bitrate of 96 / 2 / 1 = 48MHz */
443 imx233_ssp_set_timings(ssp, 2, 0, 0xffff);
445 /* read extended CSD */
447 uint8_t ext_csd[512];
448 if(imx233_ssp_sd_mmc_transfer(ssp, 8, 0, SSP_SHORT_RESP, aligned_buffer[drive], 1, true, true, &status))
449 return -12;
450 memcpy(ext_csd, aligned_buffer[drive], 512);
451 uint32_t *sec_count = (void *)&ext_csd[212];
452 window_start[drive] = 0;
453 window_end[drive] = *sec_count;
455 /* deselect card */
456 if(!send_cmd(drive, MMC_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
457 return -13;
459 return 0;
461 #endif
463 static int transfer_sectors(int drive, unsigned long start, int count, void *buf, bool read)
465 int ret = 0;
466 uint32_t resp;
468 /* update disk activity */
469 disk_last_activity[drive] = current_tick;
471 /* lock per-drive mutex */
472 mutex_lock(&mutex[drive]);
474 /* for SD cards, init if necessary */
475 #if CONFIG_STORAGE & STORAGE_SD
476 if(SDMMC_MODE(drive) == SD_MODE && SDMMC_INFO(drive).initialized <= 0)
478 ret = init_sd_card(drive);
479 if(SDMMC_INFO(drive).initialized <= 0)
480 goto Lend;
482 #endif
484 /* check window */
485 start += window_start[drive];
486 if((start + count) > window_end[drive])
488 ret = -201;
489 goto Lend;
492 /* select card.
493 * NOTE: rely on SD_SELECT_CARD=MMC_SELECT_CARD */
494 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_NO_RESP, NULL))
496 ret = -20;
497 goto Lend;
499 /* wait for TRAN state */
500 /* NOTE: rely on SD_TRAN=MMC_TRAN */
501 ret = wait_for_state(drive, SD_TRAN);
502 if(ret < 0)
503 goto Ldeselect;
504 while(count != 0)
506 /* FIXME implement this_count > 1 by using a sub-buffer of [sub] that is
507 * cache-aligned and then moving the data when possible. This way we could
508 * transfer much greater amount of data at once */
509 int this_count = 1;
510 /* Set bank_start to the correct unit (blocks or bytes).
511 * MMC drives use block addressing, SD cards bytes or blocks */
512 int bank_start = start;
513 if(SDMMC_MODE(drive) == SD_MODE && !(SDMMC_INFO(drive).ocr & (1<<30))) /* not SDHC */
514 bank_start *= SD_BLOCK_SIZE;
515 /* on write transfers, copy data to the aligned buffer */
516 if(!read)
517 memcpy(aligned_buffer[drive], buf, 512);
518 /* issue read/write
519 * NOTE: rely on SD_{READ,WRITE}_MULTIPLE_BLOCK=MMC_{READ,WRITE}_MULTIPLE_BLOCK */
520 ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive),
521 read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK,
522 bank_start, SSP_SHORT_RESP, aligned_buffer[drive], this_count, false, read, &resp);
523 if(ret != SSP_SUCCESS)
524 break;
525 /* stop transmission
526 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
527 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp))
529 ret = -15;
530 break;
532 /* on read transfers, copy the data back to the user buffer */
533 if(read)
534 memcpy(buf, aligned_buffer[drive], 512);
535 count -= this_count;
536 start += this_count;
537 buf += this_count * 512;
539 /* deselect card */
540 Ldeselect:
541 /* CMD7 w/rca =0 : deselects card & puts it in STBY state
542 * NOTE: rely on SD_DESELECT_CARD=MMC_DESELECT_CARD */
543 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
544 ret = -23;
545 Lend:
546 /* release per-drive mutex */
547 mutex_unlock(&mutex[drive]);
548 return ret;
551 static int init_drive(int drive)
553 int ret;
554 switch(SDMMC_MODE(drive))
556 #if CONFIG_STORAGE & STORAGE_SD
557 case SD_MODE: ret = init_sd_card(drive); break;
558 #endif
559 #if CONFIG_STORAGE & STORAGE_MMC
560 case MMC_MODE: ret = init_mmc_drive(drive); break;
561 #endif
562 default: ret = 0;
564 if(ret < 0)
565 panicf("die %d", ret);
566 if(ret < 0)
567 return ret;
569 /* compute window */
570 if((SDMMC_FLAGS(drive) & WINDOW) && imx233_partitions_is_window_enabled())
572 uint8_t mbr[512];
573 int ret = transfer_sectors(drive, 0, 1, mbr, true);
574 if(ret)
575 panicf("Cannot read MBR: %d", ret);
576 ret = imx233_partitions_compute_window(mbr, &window_start[drive],
577 &window_end[drive]);
578 if(ret)
579 panicf("cannot compute partitions window: %d", ret);
580 SDMMC_INFO(drive).numblocks = window_end[drive] - window_start[drive];
583 return 0;
586 static void sd_thread(void) NORETURN_ATTR;
587 static void sd_thread(void)
589 struct queue_event ev;
591 while (1)
593 queue_wait_w_tmo(&sd_queue, &ev, HZ);
595 switch(ev.id)
597 case SYS_HOTSWAP_INSERTED:
598 case SYS_HOTSWAP_EXTRACTED:
600 int microsd_init = 1;
601 /* lock-out FAT activity first -
602 * prevent deadlocking via disk_mount that
603 * would cause a reverse-order attempt with
604 * another thread */
605 fat_lock();
607 /* We now have exclusive control of fat cache and sd.
608 * Release "by force", ensure file
609 * descriptors aren't leaked and any busy
610 * ones are invalid if mounting. */
611 for(unsigned sd_drive = 0; sd_drive < _sd_num_drives; sd_drive++)
613 int drive = sd_map[sd_drive];
614 /* Skip non-removable drivers */
615 if(!sd_removable(sd_drive))
616 continue;
617 /* lock-out card activity - direct calls
618 * into driver that bypass the fat cache */
619 mutex_lock(&mutex[drive]);
620 disk_unmount(sd_first_drive + sd_drive);
621 /* Force card init for new card, re-init for re-inserted one or
622 * clear if the last attempt to init failed with an error. */
623 SDMMC_INFO(sd_map[sd_drive]).initialized = 0;
625 if(ev.id == SYS_HOTSWAP_INSERTED)
627 microsd_init = init_drive(drive);
628 if(microsd_init < 0) /* initialisation failed */
629 panicf("%s init failed : %d", SDMMC_CONF(sd_map[sd_drive]).name, microsd_init);
631 microsd_init = disk_mount(sd_first_drive + sd_drive); /* 0 if fail */
634 * Mount succeeded, or this was an EXTRACTED event,
635 * in both cases notify the system about the changed filesystems
637 if(microsd_init)
638 queue_broadcast(SYS_FS_CHANGED, 0);
639 /* unlock card */
640 mutex_unlock(&mutex[drive]);
642 /* Access is now safe */
643 fat_unlock();
644 break;
646 case SYS_TIMEOUT:
647 if(!TIME_BEFORE(current_tick, sd_last_disk_activity() + 3 * HZ))
648 sd_enable(false);
649 break;
650 case SYS_USB_CONNECTED:
651 usb_acknowledge(SYS_USB_CONNECTED_ACK);
652 /* Wait until the USB cable is extracted again */
653 usb_wait_for_disconnect(&sd_queue);
654 break;
659 static int sdmmc_init(void)
661 static int is_initialized = false;
662 if(is_initialized)
663 return 0;
664 is_initialized = true;
665 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
666 mutex_init(&mutex[drive]);
668 #if CONFIG_STORAGE & STORAGE_SD
669 queue_init(&sd_queue, true);
670 create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
671 sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
672 #endif
674 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
676 if(SDMMC_FLAGS(drive) & REMOVABLE)
677 imx233_ssp_sdmmc_setup_detect(SDMMC_SSP(drive), true, sdmmc_detect_callback,
678 false, SDMMC_FLAGS(drive) & DETECT_INVERTED);
681 return 0;
684 static int sdmmc_present(int drive)
686 if(SDMMC_FLAGS(drive) & REMOVABLE)
687 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive));
688 else
689 return true;
692 static inline int sdmmc_removable(int drive)
694 return SDMMC_FLAGS(drive) & REMOVABLE;
697 #if CONFIG_STORAGE & STORAGE_SD
698 int sd_init(void)
700 int ret = sdmmc_init();
701 if(ret < 0) return ret;
703 _sd_num_drives = 0;
704 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
705 if(SDMMC_MODE(drive) == SD_MODE)
706 sd_map[_sd_num_drives++] = drive;
707 return 0;
710 tCardInfo *card_get_info_target(int sd_card_no)
712 return &SDMMC_INFO(sd_map[sd_card_no]);
715 int sd_num_drives(int first_drive)
717 sd_first_drive = first_drive;
718 return _sd_num_drives;
721 bool sd_present(IF_MV_NONVOID(int sd_drive))
723 return sdmmc_present(sd_map[sd_drive]);
726 bool sd_removable(IF_MV_NONVOID(int sd_drive))
728 return sdmmc_removable(sd_map[sd_drive]);
731 long sd_last_disk_activity(void)
733 long last = 0;
734 for(unsigned i = 0; i < _sd_num_drives; i++)
735 last = MAX(last, disk_last_activity[sd_map[i]]);
736 return last;
739 void sd_enable(bool on)
741 (void) on;
744 int sd_read_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, void *buf)
746 return transfer_sectors(sd_map[sd_drive], start, count, buf, true);
749 int sd_write_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, const void* buf)
751 return transfer_sectors(sd_map[sd_drive], start, count, (void *)buf, false);
753 #endif
755 #if CONFIG_STORAGE & STORAGE_MMC
756 int mmc_init(void)
758 int ret = sdmmc_init();
759 if(ret < 0) return ret;
761 _sd_num_drives = 0;
762 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
763 if(SDMMC_MODE(drive) == MMC_MODE)
765 mmc_map[_mmc_num_drives++] = drive;
766 init_drive(drive);
768 return 0;
771 void mmc_get_info(IF_MD2(int mmc_drive,) struct storage_info *info)
773 int drive = mmc_map[mmc_drive];
774 info->sector_size = 512;
775 info->num_sectors = window_end[drive] - window_start[drive];
776 info->vendor = "Rockbox";
777 info->product = "Internal Storage";
778 info->revision = "0.00";
781 int mmc_num_drives(int first_drive)
783 mmc_first_drive = first_drive;
784 return _mmc_num_drives;
787 bool mmc_present(IF_MV_NONVOID(int mmc_drive))
789 return sdmmc_present(mmc_map[mmc_drive]);
792 bool mmc_removable(IF_MV_NONVOID(int mmc_drive))
794 return sdmmc_removable(mmc_map[mmc_drive]);
797 long mmc_last_disk_activity(void)
799 long last = 0;
800 for(unsigned i = 0; i < _mmc_num_drives; i++)
801 last = MAX(last, disk_last_activity[mmc_map[i]]);
802 return last;
805 void mmc_enable(bool on)
807 (void) on;
810 void mmc_sleep(void)
814 void mmc_sleepnow(void)
818 bool mmc_disk_is_active(void)
820 return false;
823 bool mmc_usb_active(void)
825 return mmc_disk_is_active();
828 int mmc_soft_reset(void)
830 return 0;
833 int mmc_flush(void)
835 return 0;
838 void mmc_spin(void)
842 void mmc_spindown(int seconds)
844 (void) seconds;
847 int mmc_spinup_time(void)
849 return 0;
852 int mmc_read_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, void *buf)
854 return transfer_sectors(mmc_map[mmc_drive], start, count, buf, true);
857 int mmc_write_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, const void* buf)
859 return transfer_sectors(mmc_map[mmc_drive], start, count, (void *)buf, false);
862 #endif