imx233: fix sd windowed drive
[maemo-rb.git] / firmware / target / arm / imx233 / sdmmc-imx233.c
blob3bdc302ada0f3316077c3ea69c71db330b85990d
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"
35 #include "ata_idle_notify.h"
36 #include "led.h"
38 /** NOTE For convenience, this drivers relies on the many similar commands
39 * between SD and MMC. The following assumptions are made:
40 * - SD_SEND_STATUS = MMC_SEND_STATUS
41 * - SD_SELECT_CARD = MMC_SELECT_CARD
42 * - SD_TRAN = MMC_TRAN
43 * - MMC_WRITE_MULTIPLE_BLOCK = SD_WRITE_MULTIPLE_BLOCK
44 * - MMC_READ_MULTIPLE_BLOCK = SD_READ_MULTIPLE_BLOCK
45 * - SD_STOP_TRANSMISSION = MMC_STOP_TRANSMISSION
46 * - SD_DESELECT_CARD = MMC_DESELECT_CARD
48 #if SD_SEND_STATUS != MMC_SEND_STATUS || SD_SELECT_CARD != MMC_SELECT_CARD || \
49 SD_TRAN != MMC_TRAN || MMC_WRITE_MULTIPLE_BLOCK != SD_WRITE_MULTIPLE_BLOCK || \
50 MMC_READ_MULTIPLE_BLOCK != SD_READ_MULTIPLE_BLOCK || \
51 SD_STOP_TRANSMISSION != MMC_STOP_TRANSMISSION || \
52 SD_DESELECT_CARD != MMC_DESELECT_CARD
53 #error SD/MMC mismatch
54 #endif
56 struct sdmmc_config_t
58 const char *name; /* name(for debug) */
59 int flags; /* flags */
60 int power_pin; /* power pin */
61 int power_delay; /* extra power up delay */
62 int ssp; /* associated ssp block */
63 int mode; /* mode (SD vs MMC) */
66 /* flags */
67 #define POWER_PIN (1 << 0)
68 #define POWER_INVERTED (1 << 1)
69 #define REMOVABLE (1 << 2)
70 #define DETECT_INVERTED (1 << 3)
71 #define POWER_DELAY (1 << 4)
72 #define WINDOW (1 << 5)
74 /* modes */
75 #define SD_MODE 0
76 #define MMC_MODE 1
78 #define PIN(bank,pin) ((bank) << 5 | (pin))
79 #define PIN2BANK(v) ((v) >> 5)
80 #define PIN2PIN(v) ((v) & 0x1f)
82 struct sdmmc_config_t sdmmc_config[] =
84 #ifdef SANSA_FUZEPLUS
85 /* The Fuze+ uses pin #B0P8 for power */
87 .name = "microSD",
88 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE,
89 .power_pin = PIN(0, 8),
90 .ssp = 1,
91 .mode = SD_MODE,
93 /* The Fuze+ uses pin #B1P29 for power */
95 .name = "eMMC",
96 .flags = POWER_PIN | POWER_INVERTED | WINDOW | POWER_DELAY,
97 .power_pin = PIN(1, 29),
98 .power_delay = HZ / 5, /* extra delay, to ramp up voltage? */
99 .ssp = 2,
100 .mode = MMC_MODE,
102 #elif defined(CREATIVE_ZENXFI2)
103 /* The Zen X-Fi2 uses pin B1P29 for power*/
105 .name = "microSD",
106 .flags = POWER_PIN | REMOVABLE | DETECT_INVERTED,
107 .power_pin = PIN(1, 29),
108 .ssp = 1,
109 .mode = SD_MODE,
111 #elif defined(CREATIVE_ZENXFI3)
113 .name = "internal/SD",
114 .flags = WINDOW,
115 .ssp = 2,
116 .mode = SD_MODE,
118 /* The Zen X-Fi3 uses pin #B0P07 for power*/
120 .name = "microSD",
121 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE | POWER_DELAY,
122 .power_pin = PIN(0, 7),
123 .power_delay = HZ / 10, /* extra delay, to ramp up voltage? */
124 .ssp = 1,
125 .mode = SD_MODE,
127 #else
128 #error You need to write the sd/mmc config!
129 #endif
132 #define SDMMC_NUM_DRIVES (sizeof(sdmmc_config) / sizeof(sdmmc_config[0]))
134 #define SDMMC_CONF(drive) sdmmc_config[drive]
135 #define SDMMC_FLAGS(drive) SDMMC_CONF(drive).flags
136 #define SDMMC_SSP(drive) SDMMC_CONF(drive).ssp
137 #define SDMMC_MODE(drive) SDMMC_CONF(drive).mode
139 /** WARNING
140 * to be consistent with all our SD drivers, the .rca field of sdmmc_card_info
141 * in reality holds (rca << 16) because all command arguments actually require
142 * the RCA is the 16-bit msb. Be careful that this is not the actuall RCA ! */
144 /* common */
145 static unsigned window_start[SDMMC_NUM_DRIVES];
146 static unsigned window_end[SDMMC_NUM_DRIVES];
147 static uint8_t aligned_buffer[SDMMC_NUM_DRIVES][512] CACHEALIGN_ATTR;
148 static tCardInfo sdmmc_card_info[SDMMC_NUM_DRIVES];
149 static struct mutex mutex[SDMMC_NUM_DRIVES];
150 static int disk_last_activity[SDMMC_NUM_DRIVES];
151 #define MIN_YIELD_PERIOD 5 /* ticks */
152 static int next_yield = 0;
154 #define SDMMC_INFO(drive) sdmmc_card_info[drive]
155 #define SDMMC_RCA(drive) SDMMC_INFO(drive).rca
157 /* sd only */
158 static long sdmmc_stack[(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
159 static const char sdmmc_thread_name[] = "sdmmc";
160 static struct event_queue sdmmc_queue;
161 #if CONFIG_STORAGE & STORAGE_SD
162 static int sd_first_drive;
163 static unsigned _sd_num_drives;
164 static int sd_map[SDMMC_NUM_DRIVES]; /* sd->sdmmc map */
165 #endif
166 /* mmc only */
167 #if CONFIG_STORAGE & STORAGE_MMC
168 static int mmc_first_drive;
169 static unsigned _mmc_num_drives;
170 static int mmc_map[SDMMC_NUM_DRIVES]; /* mmc->sdmmc map */
171 #endif
173 static int init_drive(int drive);
175 /* WARNING NOTE BUG FIXME
176 * There are three numbering schemes involved in the driver:
177 * - the sdmmc indexes into sdmmc_config[]
178 * - the sd drive indexes
179 * - the mmc drive indexes
180 * By convention, [drive] refers to a sdmmc index whereas sd_drive/mmc_drive
181 * refer to sd/mmc drive indexes. We keep two maps sd->sdmmc and mmc->sdmmc
182 * to find the sdmmc index from the sd or mmc one */
184 static void sdmmc_detect_callback(int ssp)
186 /* This is called only if the state was stable for 300ms - check state
187 * and post appropriate event. */
188 if(imx233_ssp_sdmmc_detect(ssp))
189 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
190 else
191 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
192 imx233_ssp_sdmmc_setup_detect(ssp, true, sdmmc_detect_callback, false,
193 imx233_ssp_sdmmc_is_detect_inverted(ssp));
196 static void sdmmc_power(int drive, bool on)
198 /* power chip if needed */
199 if(SDMMC_FLAGS(drive) & POWER_PIN)
201 int bank = PIN2BANK(SDMMC_CONF(drive).power_pin);
202 int pin = PIN2PIN(SDMMC_CONF(drive).power_pin);
203 imx233_pinctrl_acquire_pin(bank, pin, "sd/mmc power");
204 imx233_set_pin_function(bank, pin, PINCTRL_FUNCTION_GPIO);
205 imx233_enable_gpio_output(bank, pin, true);
206 if(SDMMC_FLAGS(drive) & POWER_INVERTED)
207 imx233_set_gpio_output(bank, pin, !on);
208 else
209 imx233_set_gpio_output(bank, pin, on);
211 if(SDMMC_FLAGS(drive) & POWER_DELAY)
212 sleep(SDMMC_CONF(drive).power_delay);
213 /* setup pins, never use alternatives pin on SSP1 because no device use it
214 * but this could be made a flag */
215 int bus_width = SDMMC_MODE(drive) == MMC_MODE ? 8 : 4;
216 if(SDMMC_SSP(drive) == 1)
217 imx233_ssp_setup_ssp1_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA, false);
218 else
219 imx233_ssp_setup_ssp2_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA);
222 #define MCI_NO_RESP 0
223 #define MCI_RESP (1<<0)
224 #define MCI_LONG_RESP (1<<1)
225 #define MCI_ACMD (1<<2) /* sd only */
226 #define MCI_NOCRC (1<<3)
227 #define MCI_BUSY (1<<4)
229 static bool send_cmd(int drive, uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp)
231 if((flags & MCI_ACMD) && !send_cmd(drive, SD_APP_CMD, SDMMC_RCA(drive), MCI_RESP, resp))
232 return false;
234 enum imx233_ssp_resp_t resp_type = (flags & MCI_LONG_RESP) ? SSP_LONG_RESP :
235 (flags & MCI_RESP) ? SSP_SHORT_RESP : SSP_NO_RESP;
236 enum imx233_ssp_error_t ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive), cmd,
237 arg, resp_type, NULL, 0, !!(flags & MCI_BUSY), false, resp);
238 if(resp_type == SSP_LONG_RESP)
240 /* Our SD codes assume most significant word first, so reverse resp */
241 uint32_t tmp = resp[0];
242 resp[0] = resp[3];
243 resp[3] = tmp;
244 tmp = resp[1];
245 resp[1] = resp[2];
246 resp[2] = tmp;
248 return ret == SSP_SUCCESS;
251 static int wait_for_state(int drive, unsigned state)
253 unsigned long response;
254 unsigned int timeout = current_tick + 5*HZ;
255 int cmd_retry = 10;
257 while (1)
259 /* NOTE: rely on SD_SEND_STATUS=MMC_SEND_STATUS */
260 while(!send_cmd(drive, SD_SEND_STATUS, SDMMC_RCA(drive), MCI_RESP, &response) && cmd_retry > 0)
261 cmd_retry--;
263 if(cmd_retry <= 0)
264 return -1;
266 if(((response >> 9) & 0xf) == state)
267 return 0;
269 if(TIME_AFTER(current_tick, timeout))
270 return -10 * ((response >> 9) & 0xf);
272 if(TIME_AFTER(current_tick, next_yield))
274 yield();
275 next_yield = current_tick + MIN_YIELD_PERIOD;
279 return 0;
282 #if CONFIG_STORAGE & STORAGE_SD
283 static int init_sd_card(int drive)
285 int ssp = SDMMC_SSP(drive);
286 sdmmc_power(drive, false);
287 sdmmc_power(drive, true);
288 imx233_ssp_start(ssp);
289 imx233_ssp_softreset(ssp);
290 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
291 /* SSPCLK @ 96MHz
292 * gives bitrate of 96000 / 240 / 1 = 400kHz */
293 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
295 imx233_ssp_sd_mmc_power_up_sequence(ssp);
296 imx233_ssp_set_bus_width(ssp, 1);
297 imx233_ssp_set_block_size(ssp, 9);
299 SDMMC_RCA(drive) = 0;
300 bool sd_v2 = false;
301 uint32_t resp;
302 long init_timeout;
303 /* go to idle state */
304 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
305 return -1;
306 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
307 Non v2 cards will not respond to this command */
308 if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &resp))
309 if((resp & 0xFFF) == 0x1AA)
310 sd_v2 = true;
311 /* timeout for initialization is 1sec, from SD Specification 2.00 */
312 init_timeout = current_tick + HZ;
315 /* this timeout is the only valid error for this loop*/
316 if(TIME_AFTER(current_tick, init_timeout))
317 return -2;
319 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
320 if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
321 MCI_ACMD|MCI_NOCRC|MCI_RESP, &SDMMC_INFO(drive).ocr))
322 return -100;
323 } while(!(SDMMC_INFO(drive).ocr & (1<<31)));
325 /* CMD2 send CID */
326 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, SDMMC_INFO(drive).cid))
327 return -3;
329 /* CMD3 send RCA */
330 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &SDMMC_INFO(drive).rca))
331 return -4;
333 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
334 if(sd_v2)
336 /* CMD7 w/rca: Select card to put it in TRAN state */
337 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, NULL))
338 return -5;
340 if(wait_for_state(drive, SD_TRAN))
341 return -6;
343 /* CMD6 */
344 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
345 return -7;
346 sleep(HZ/10);
348 /* go back to STBY state so we can read csd */
349 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
350 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
351 return -8;
354 /* CMD9 send CSD */
355 if(!send_cmd(drive, SD_SEND_CSD, SDMMC_RCA(drive), MCI_RESP|MCI_LONG_RESP,
356 SDMMC_INFO(drive).csd))
357 return -9;
359 sd_parse_csd(&SDMMC_INFO(drive));
360 window_start[drive] = 0;
361 window_end[drive] = SDMMC_INFO(drive).numblocks;
363 /* SSPCLK @ 96MHz
364 * gives bitrate of 96 / 4 / 1 = 24MHz */
365 imx233_ssp_set_timings(ssp, 4, 0, 0xffff);
367 /* CMD7 w/rca: Select card to put it in TRAN state */
368 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &resp))
369 return -12;
370 if(wait_for_state(drive, SD_TRAN))
371 return -13;
373 /* ACMD6: set bus width to 4-bit */
374 if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp))
375 return -15;
376 /* ACMD42: disconnect the pull-up resistor on CD/DAT3 */
377 if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP|MCI_ACMD, &resp))
378 return -17;
380 /* Switch to 4-bit */
381 imx233_ssp_set_bus_width(ssp, 4);
383 SDMMC_INFO(drive).initialized = 1;
385 return 0;
387 #endif
389 #if CONFIG_STORAGE & STORAGE_MMC
390 static int init_mmc_drive(int drive)
392 int ssp = SDMMC_SSP(drive);
393 /* we can choose the RCA of mmc cards: pick drive. Following our convention,
394 * .rca is actually RCA << 16 */
395 SDMMC_RCA(drive) = drive << 16;
397 sdmmc_power(drive, false);
398 sdmmc_power(drive, true);
399 imx233_ssp_start(ssp);
400 imx233_ssp_softreset(ssp);
401 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
402 /* SSPCLK @ 96MHz
403 * gives bitrate of 96000 / 240 / 1 = 400kHz */
404 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
405 imx233_ssp_sd_mmc_power_up_sequence(ssp);
406 imx233_ssp_set_bus_width(ssp, 1);
407 imx233_ssp_set_block_size(ssp, 9);
408 /* go to idle state */
409 if(!send_cmd(drive, MMC_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
410 return -1;
411 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
412 unsigned timeout = current_tick + HZ;
413 bool ret = false;
416 uint32_t ocr;
417 ret = send_cmd(drive, MMC_SEND_OP_COND, 0x40ff8000, MCI_RESP, &ocr);
418 if(ret && ocr & (1 << 31))
419 break;
420 }while(!TIME_AFTER(current_tick, timeout));
422 if(!ret)
423 return -2;
424 /* get CID */
425 uint32_t cid[4];
426 if(!send_cmd(drive, MMC_ALL_SEND_CID, 0, MCI_LONG_RESP, cid))
427 return -3;
428 /* Set RCA */
429 uint32_t status;
430 if(!send_cmd(drive, MMC_SET_RELATIVE_ADDR, SDMMC_RCA(drive), MCI_RESP, &status))
431 return -4;
432 /* Select card */
433 if(!send_cmd(drive, MMC_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &status))
434 return -5;
435 /* Check TRAN state */
436 if(wait_for_state(drive, MMC_TRAN))
437 return -6;
438 /* Switch to 8-bit bus */
439 if(!send_cmd(drive, MMC_SWITCH, 0x3b70200, MCI_RESP|MCI_BUSY, &status))
440 return -8;
441 /* switch error ? */
442 if(status & 0x80)
443 return -9;
444 imx233_ssp_set_bus_width(ssp, 8);
445 /* Switch to high speed mode */
446 if(!send_cmd(drive, MMC_SWITCH, 0x3b90100, MCI_RESP|MCI_BUSY, &status))
447 return -10;
448 /* switch error ?*/
449 if(status & 0x80)
450 return -11;
451 /* SSPCLK @ 96MHz
452 * gives bitrate of 96 / 2 / 1 = 48MHz */
453 imx233_ssp_set_timings(ssp, 2, 0, 0xffff);
455 /* read extended CSD */
457 uint8_t ext_csd[512];
458 if(imx233_ssp_sd_mmc_transfer(ssp, 8, 0, SSP_SHORT_RESP, aligned_buffer[drive], 1, true, true, &status))
459 return -12;
460 memcpy(ext_csd, aligned_buffer[drive], 512);
461 uint32_t *sec_count = (void *)&ext_csd[212];
462 window_start[drive] = 0;
463 window_end[drive] = *sec_count;
465 /* deselect card */
466 if(!send_cmd(drive, MMC_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
467 return -13;
469 return 0;
471 #endif
473 // low-level function, don't call directly!
474 static int __xfer_sectors(int drive, unsigned long start, int count, void *buf, bool read)
476 uint32_t resp;
477 int ret = 0;
478 while(count != 0)
480 int this_count = MIN(count, IMX233_MAX_SINGLE_DMA_XFER_SIZE / 512);
481 /* Set bank_start to the correct unit (blocks or bytes).
482 * MMC drives use block addressing, SD cards bytes or blocks */
483 int bank_start = start;
484 if(SDMMC_MODE(drive) == SD_MODE && !(SDMMC_INFO(drive).ocr & (1<<30))) /* not SDHC */
485 bank_start *= SD_BLOCK_SIZE;
486 /* issue read/write
487 * NOTE: rely on SD_{READ,WRITE}_MULTIPLE_BLOCK=MMC_{READ,WRITE}_MULTIPLE_BLOCK */
488 ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive),
489 read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK,
490 bank_start, SSP_SHORT_RESP, buf, this_count, false, read, &resp);
491 if(ret != SSP_SUCCESS)
492 break;
493 /* stop transmission
494 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
495 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp))
497 ret = -15;
498 break;
500 count -= this_count;
501 start += this_count;
502 buf += this_count * 512;
504 return ret;
507 static int transfer_sectors(int drive, unsigned long start, int count, void *buf, bool read)
509 int ret = 0;
511 /* update disk activity */
512 disk_last_activity[drive] = current_tick;
514 /* lock per-drive mutex */
515 mutex_lock(&mutex[drive]);
517 /* update led status */
518 led(true);
520 /* for SD cards, init if necessary */
521 #if CONFIG_STORAGE & STORAGE_SD
522 if(SDMMC_MODE(drive) == SD_MODE && SDMMC_INFO(drive).initialized <= 0)
524 ret = init_drive(drive);
525 if(SDMMC_INFO(drive).initialized <= 0)
526 goto Lend;
528 #endif
530 /* check window */
531 start += window_start[drive];
532 if((start + count) > window_end[drive])
534 ret = -201;
535 goto Lend;
537 /* select card.
538 * NOTE: rely on SD_SELECT_CARD=MMC_SELECT_CARD */
539 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_NO_RESP, NULL))
541 ret = -20;
542 goto Lend;
544 /* wait for TRAN state */
545 /* NOTE: rely on SD_TRAN=MMC_TRAN */
546 ret = wait_for_state(drive, SD_TRAN);
547 if(ret < 0)
548 goto Ldeselect;
551 * NOTE: we need to make sure dma transfers are aligned. This is handled
552 * differently for read and write transfers. We do not repeat it each
553 * time but it should be noted that all transfers are limited by
554 * IMX233_MAX_SINGLE_DMA_XFER_SIZE and thus need to be split if needed.
556 * Read transfers:
557 * If the buffer is already aligned, transfer everything at once.
558 * Otherwise, transfer all sectors but one to the sub-buffer starting
559 * on the next cache ligned and then move the data. Then transfer the
560 * last sector to the aligned_buffer and then copy to the buffer.
562 * Write transfers:
563 * If the buffer is already aligned, transfer everything at once.
564 * Otherwise, copy the first sector to the aligned_buffer and transfer.
565 * Then move all other sectors within the buffer to make it cache
566 * aligned and transfer it.
568 if(read)
570 void *ptr = CACHEALIGN_UP(buf);
571 if(buf != ptr)
573 // copy count-1 sector and then move within the buffer
574 ret = __xfer_sectors(drive, start, count - 1, ptr, read);
575 memmove(buf, ptr, 512 * (count - 1));
576 if(ret >= 0)
578 // transfer the last sector the aligned_buffer and copy
579 ret = __xfer_sectors(drive, start + count - 1, 1,
580 aligned_buffer[drive], read);
581 memcpy(buf + 512 * (count - 1), aligned_buffer[drive], 512);
584 else
585 ret = __xfer_sectors(drive, start, count, buf, read);
587 else
589 void *ptr = CACHEALIGN_UP(buf);
590 if(buf != ptr)
592 // transfer the first sector to aligned_buffer and copy
593 memcpy(aligned_buffer[drive], buf, 512);
594 ret = __xfer_sectors(drive, start, 1, aligned_buffer[drive], read);
595 if(ret >= 0)
597 // move within the buffer and transfer
598 memmove(ptr, buf + 512, 512 * (count - 1));
599 ret = __xfer_sectors(drive, start + 1, count - 1, ptr, read);
602 else
603 ret = __xfer_sectors(drive, start, count, buf, read);
606 /* deselect card */
607 Ldeselect:
608 /* CMD7 w/rca =0 : deselects card & puts it in STBY state
609 * NOTE: rely on SD_DESELECT_CARD=MMC_DESELECT_CARD */
610 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
611 ret = -23;
612 Lend:
613 /* update led status */
614 led(false);
615 /* release per-drive mutex */
616 mutex_unlock(&mutex[drive]);
617 return ret;
620 static int init_drive(int drive)
622 int ret;
623 switch(SDMMC_MODE(drive))
625 #if CONFIG_STORAGE & STORAGE_SD
626 case SD_MODE: ret = init_sd_card(drive); break;
627 #endif
628 #if CONFIG_STORAGE & STORAGE_MMC
629 case MMC_MODE: ret = init_mmc_drive(drive); break;
630 #endif
631 default: ret = 0;
633 if(ret < 0)
634 return ret;
636 /* compute window */
637 if((SDMMC_FLAGS(drive) & WINDOW) && imx233_partitions_is_window_enabled())
639 uint8_t mbr[512];
640 int ret = transfer_sectors(drive, 0, 1, mbr, true);
641 if(ret)
642 panicf("Cannot read MBR: %d", ret);
643 ret = imx233_partitions_compute_window(mbr, &window_start[drive],
644 &window_end[drive]);
645 if(ret)
646 panicf("cannot compute partitions window: %d", ret);
647 SDMMC_INFO(drive).numblocks = window_end[drive] - window_start[drive];
650 return 0;
653 static void sdmmc_thread(void) NORETURN_ATTR;
654 static void sdmmc_thread(void)
656 struct queue_event ev;
657 bool idle_notified = false;
658 int timeout = 0;
660 while (1)
662 queue_wait_w_tmo(&sdmmc_queue, &ev, HZ);
664 switch(ev.id)
666 #if CONFIG_STORAGE & STORAGE_SD
667 case SYS_HOTSWAP_INSERTED:
668 case SYS_HOTSWAP_EXTRACTED:
670 int microsd_init = 1;
671 /* lock-out FAT activity first -
672 * prevent deadlocking via disk_mount that
673 * would cause a reverse-order attempt with
674 * another thread */
675 fat_lock();
677 /* We now have exclusive control of fat cache and sd.
678 * Release "by force", ensure file
679 * descriptors aren't leaked and any busy
680 * ones are invalid if mounting. */
681 for(unsigned sd_drive = 0; sd_drive < _sd_num_drives; sd_drive++)
683 int drive = sd_map[sd_drive];
684 /* Skip non-removable drivers */
685 if(!sd_removable(sd_drive))
686 continue;
687 /* lock-out card activity - direct calls
688 * into driver that bypass the fat cache */
689 mutex_lock(&mutex[drive]);
690 disk_unmount(sd_first_drive + sd_drive);
691 /* Force card init for new card, re-init for re-inserted one or
692 * clear if the last attempt to init failed with an error. */
693 SDMMC_INFO(sd_map[sd_drive]).initialized = 0;
695 if(ev.id == SYS_HOTSWAP_INSERTED)
697 microsd_init = init_drive(drive);
698 if(microsd_init < 0) /* initialisation failed */
699 panicf("%s init failed : %d", SDMMC_CONF(sd_map[sd_drive]).name, microsd_init);
701 microsd_init = disk_mount(sd_first_drive + sd_drive); /* 0 if fail */
704 * Mount succeeded, or this was an EXTRACTED event,
705 * in both cases notify the system about the changed filesystems
707 if(microsd_init)
708 queue_broadcast(SYS_FS_CHANGED, 0);
709 /* unlock card */
710 mutex_unlock(&mutex[drive]);
712 /* Access is now safe */
713 fat_unlock();
714 break;
716 #endif
717 case SYS_TIMEOUT:
718 #if CONFIG_STORAGE & STORAGE_SD
719 timeout = MAX(timeout, sd_last_disk_activity()+(3*HZ));
720 #endif
721 #if CONFIG_STORAGE & STORAGE_MMC
722 timeout = MAX(timeout, mmc_last_disk_activity()+(3*HZ));
723 #endif
724 if(TIME_BEFORE(current_tick, timeout))
726 idle_notified = false;
728 else
730 next_yield = current_tick;
732 if(!idle_notified)
734 call_storage_idle_notifys(false);
735 idle_notified = true;
738 break;
739 break;
740 case SYS_USB_CONNECTED:
741 usb_acknowledge(SYS_USB_CONNECTED_ACK);
742 /* Wait until the USB cable is extracted again */
743 usb_wait_for_disconnect(&sdmmc_queue);
744 break;
749 static int sdmmc_init(void)
751 static int is_initialized = false;
752 if(is_initialized)
753 return 0;
754 is_initialized = true;
755 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
756 mutex_init(&mutex[drive]);
758 queue_init(&sdmmc_queue, true);
759 create_thread(sdmmc_thread, sdmmc_stack, sizeof(sdmmc_stack), 0,
760 sdmmc_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
762 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
764 if(SDMMC_FLAGS(drive) & REMOVABLE)
765 imx233_ssp_sdmmc_setup_detect(SDMMC_SSP(drive), true, sdmmc_detect_callback,
766 false, SDMMC_FLAGS(drive) & DETECT_INVERTED);
769 return 0;
772 static int sdmmc_present(int drive)
774 if(SDMMC_FLAGS(drive) & REMOVABLE)
775 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive));
776 else
777 return true;
780 static inline int sdmmc_removable(int drive)
782 return SDMMC_FLAGS(drive) & REMOVABLE;
785 #if CONFIG_STORAGE & STORAGE_SD
786 int sd_init(void)
788 int ret = sdmmc_init();
789 if(ret < 0) return ret;
791 _sd_num_drives = 0;
792 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
793 if(SDMMC_MODE(drive) == SD_MODE)
794 sd_map[_sd_num_drives++] = drive;
795 return 0;
798 tCardInfo *card_get_info_target(int sd_card_no)
800 return &SDMMC_INFO(sd_map[sd_card_no]);
803 int sd_num_drives(int first_drive)
805 sd_first_drive = first_drive;
806 return _sd_num_drives;
809 bool sd_present(IF_MV_NONVOID(int sd_drive))
811 return sdmmc_present(sd_map[sd_drive]);
814 bool sd_removable(IF_MV_NONVOID(int sd_drive))
816 return sdmmc_removable(sd_map[sd_drive]);
819 long sd_last_disk_activity(void)
821 long last = 0;
822 for(unsigned i = 0; i < _sd_num_drives; i++)
823 last = MAX(last, disk_last_activity[sd_map[i]]);
824 return last;
827 void sd_enable(bool on)
829 (void) on;
832 int sd_read_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, void *buf)
834 return transfer_sectors(sd_map[sd_drive], start, count, buf, true);
837 int sd_write_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, const void* buf)
839 return transfer_sectors(sd_map[sd_drive], start, count, (void *)buf, false);
841 #endif
843 #if CONFIG_STORAGE & STORAGE_MMC
844 int mmc_init(void)
846 int ret = sdmmc_init();
847 if(ret < 0) return ret;
849 _sd_num_drives = 0;
850 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
851 if(SDMMC_MODE(drive) == MMC_MODE)
853 mmc_map[_mmc_num_drives++] = drive;
854 init_drive(drive);
856 return 0;
859 void mmc_get_info(IF_MD2(int mmc_drive,) struct storage_info *info)
861 int drive = mmc_map[mmc_drive];
862 info->sector_size = 512;
863 info->num_sectors = window_end[drive] - window_start[drive];
864 info->vendor = "Rockbox";
865 info->product = "Internal Storage";
866 info->revision = "0.00";
869 int mmc_num_drives(int first_drive)
871 mmc_first_drive = first_drive;
872 return _mmc_num_drives;
875 bool mmc_present(IF_MV_NONVOID(int mmc_drive))
877 return sdmmc_present(mmc_map[mmc_drive]);
880 bool mmc_removable(IF_MV_NONVOID(int mmc_drive))
882 return sdmmc_removable(mmc_map[mmc_drive]);
885 long mmc_last_disk_activity(void)
887 long last = 0;
888 for(unsigned i = 0; i < _mmc_num_drives; i++)
889 last = MAX(last, disk_last_activity[mmc_map[i]]);
890 return last;
893 void mmc_enable(bool on)
895 (void) on;
898 void mmc_sleep(void)
902 void mmc_sleepnow(void)
906 bool mmc_disk_is_active(void)
908 return false;
911 bool mmc_usb_active(void)
913 return mmc_disk_is_active();
916 int mmc_soft_reset(void)
918 return 0;
921 int mmc_flush(void)
923 return 0;
926 void mmc_spin(void)
930 void mmc_spindown(int seconds)
932 (void) seconds;
935 int mmc_spinup_time(void)
937 return 0;
940 int mmc_read_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, void *buf)
942 return transfer_sectors(mmc_map[mmc_drive], start, count, buf, true);
945 int mmc_write_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, const void* buf)
947 return transfer_sectors(mmc_map[mmc_drive], start, count, (void *)buf, false);
950 #endif