imx233: fix auto slow divisor
[maemo-rb.git] / firmware / target / arm / imx233 / sdmmc-imx233.c
blobe0966f784b5901bfa6d1c33d85ce5ccff34444f7
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"
37 /** NOTE For convenience, this drivers relies on the many similar commands
38 * between SD and MMC. The following assumptions are made:
39 * - SD_SEND_STATUS = MMC_SEND_STATUS
40 * - SD_SELECT_CARD = MMC_SELECT_CARD
41 * - SD_TRAN = MMC_TRAN
42 * - MMC_WRITE_MULTIPLE_BLOCK = SD_WRITE_MULTIPLE_BLOCK
43 * - MMC_READ_MULTIPLE_BLOCK = SD_READ_MULTIPLE_BLOCK
44 * - SD_STOP_TRANSMISSION = MMC_STOP_TRANSMISSION
45 * - SD_DESELECT_CARD = MMC_DESELECT_CARD
47 #if SD_SEND_STATUS != MMC_SEND_STATUS || SD_SELECT_CARD != MMC_SELECT_CARD || \
48 SD_TRAN != MMC_TRAN || MMC_WRITE_MULTIPLE_BLOCK != SD_WRITE_MULTIPLE_BLOCK || \
49 MMC_READ_MULTIPLE_BLOCK != SD_READ_MULTIPLE_BLOCK || \
50 SD_STOP_TRANSMISSION != MMC_STOP_TRANSMISSION || \
51 SD_DESELECT_CARD != MMC_DESELECT_CARD
52 #error SD/MMC mismatch
53 #endif
55 struct sdmmc_config_t
57 const char *name; /* name(for debug) */
58 int flags; /* flags */
59 int power_pin; /* power pin */
60 int power_delay; /* extra power up delay */
61 int ssp; /* associated ssp block */
62 int mode; /* mode (SD vs MMC) */
65 /* flags */
66 #define POWER_PIN (1 << 0)
67 #define POWER_INVERTED (1 << 1)
68 #define REMOVABLE (1 << 2)
69 #define DETECT_INVERTED (1 << 3)
70 #define POWER_DELAY (1 << 4)
71 #define WINDOW (1 << 5)
73 /* modes */
74 #define SD_MODE 0
75 #define MMC_MODE 1
77 #define PIN(bank,pin) ((bank) << 5 | (pin))
78 #define PIN2BANK(v) ((v) >> 5)
79 #define PIN2PIN(v) ((v) & 0x1f)
81 struct sdmmc_config_t sdmmc_config[] =
83 #ifdef SANSA_FUZEPLUS
84 /* The Fuze+ uses pin #B0P8 for power */
86 .name = "microSD",
87 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE,
88 .power_pin = PIN(0, 8),
89 .ssp = 1,
90 .mode = SD_MODE,
92 /* The Fuze+ uses pin #B1P29 for power */
94 .name = "eMMC",
95 .flags = POWER_PIN | POWER_INVERTED | WINDOW | POWER_DELAY,
96 .power_pin = PIN(1, 29),
97 .power_delay = HZ / 5, /* extra delay, to ramp up voltage? */
98 .ssp = 2,
99 .mode = MMC_MODE,
101 #elif defined(CREATIVE_ZENXFI2)
102 /* The Zen X-Fi2 uses pin B1P29 for power*/
104 .name = "microSD",
105 .flags = POWER_PIN | REMOVABLE | DETECT_INVERTED,
106 .power_pin = PIN(1, 29),
107 .ssp = 1,
108 .mode = SD_MODE,
110 #elif defined(CREATIVE_ZENXFI3)
112 .name = "internal/SD",
113 .flags = WINDOW,
114 .ssp = 2,
115 .mode = SD_MODE,
117 /* The Zen X-Fi3 uses pin #B0P07 for power*/
119 .name = "microSD",
120 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE | POWER_DELAY,
121 .power_pin = PIN(0, 7),
122 .power_delay = HZ / 10, /* extra delay, to ramp up voltage? */
123 .ssp = 1,
124 .mode = SD_MODE,
126 #else
127 #error You need to write the sd/mmc config!
128 #endif
131 #define SDMMC_NUM_DRIVES (sizeof(sdmmc_config) / sizeof(sdmmc_config[0]))
133 #define SDMMC_CONF(drive) sdmmc_config[drive]
134 #define SDMMC_FLAGS(drive) SDMMC_CONF(drive).flags
135 #define SDMMC_SSP(drive) SDMMC_CONF(drive).ssp
136 #define SDMMC_MODE(drive) SDMMC_CONF(drive).mode
138 /** WARNING
139 * to be consistent with all our SD drivers, the .rca field of sdmmc_card_info
140 * in reality holds (rca << 16) because all command arguments actually require
141 * the RCA is the 16-bit msb. Be careful that this is not the actuall RCA ! */
143 /* common */
144 static unsigned window_start[SDMMC_NUM_DRIVES];
145 static unsigned window_end[SDMMC_NUM_DRIVES];
146 static uint8_t aligned_buffer[SDMMC_NUM_DRIVES][512] CACHEALIGN_ATTR;
147 static tCardInfo sdmmc_card_info[SDMMC_NUM_DRIVES];
148 static struct mutex mutex[SDMMC_NUM_DRIVES];
149 static int disk_last_activity[SDMMC_NUM_DRIVES];
150 #define MIN_YIELD_PERIOD 5 /* ticks */
151 static int next_yield = 0;
153 #define SDMMC_INFO(drive) sdmmc_card_info[drive]
154 #define SDMMC_RCA(drive) SDMMC_INFO(drive).rca
156 /* sd only */
157 static long sdmmc_stack[(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
158 static const char sdmmc_thread_name[] = "sdmmc";
159 static struct event_queue sdmmc_queue;
160 #if CONFIG_STORAGE & STORAGE_SD
161 static int sd_first_drive;
162 static unsigned _sd_num_drives;
163 static int sd_map[SDMMC_NUM_DRIVES]; /* sd->sdmmc map */
164 #endif
165 /* mmc only */
166 #if CONFIG_STORAGE & STORAGE_MMC
167 static int mmc_first_drive;
168 static unsigned _mmc_num_drives;
169 static int mmc_map[SDMMC_NUM_DRIVES]; /* mmc->sdmmc map */
170 #endif
172 /* WARNING NOTE BUG FIXME
173 * There are three numbering schemes involved in the driver:
174 * - the sdmmc indexes into sdmmc_config[]
175 * - the sd drive indexes
176 * - the mmc drive indexes
177 * By convention, [drive] refers to a sdmmc index whereas sd_drive/mmc_drive
178 * refer to sd/mmc drive indexes. We keep two maps sd->sdmmc and mmc->sdmmc
179 * to find the sdmmc index from the sd or mmc one */
181 static void sdmmc_detect_callback(int ssp)
183 /* This is called only if the state was stable for 300ms - check state
184 * and post appropriate event. */
185 if(imx233_ssp_sdmmc_detect(ssp))
186 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
187 else
188 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
189 imx233_ssp_sdmmc_setup_detect(ssp, true, sdmmc_detect_callback, false,
190 imx233_ssp_sdmmc_is_detect_inverted(ssp));
193 static void sdmmc_power(int drive, bool on)
195 /* power chip if needed */
196 if(SDMMC_FLAGS(drive) & POWER_PIN)
198 int bank = PIN2BANK(SDMMC_CONF(drive).power_pin);
199 int pin = PIN2PIN(SDMMC_CONF(drive).power_pin);
200 imx233_pinctrl_acquire_pin(bank, pin, "sd/mmc power");
201 imx233_set_pin_function(bank, pin, PINCTRL_FUNCTION_GPIO);
202 imx233_enable_gpio_output(bank, pin, true);
203 if(SDMMC_FLAGS(drive) & POWER_INVERTED)
204 imx233_set_gpio_output(bank, pin, !on);
205 else
206 imx233_set_gpio_output(bank, pin, on);
208 if(SDMMC_FLAGS(drive) & POWER_DELAY)
209 sleep(SDMMC_CONF(drive).power_delay);
210 /* setup pins, never use alternatives pin on SSP1 because no device use it
211 * but this could be made a flag */
212 int bus_width = SDMMC_MODE(drive) == MMC_MODE ? 8 : 4;
213 if(SDMMC_SSP(drive) == 1)
214 imx233_ssp_setup_ssp1_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA, false);
215 else
216 imx233_ssp_setup_ssp2_sd_mmc_pins(on, bus_width, PINCTRL_DRIVE_4mA);
219 #define MCI_NO_RESP 0
220 #define MCI_RESP (1<<0)
221 #define MCI_LONG_RESP (1<<1)
222 #define MCI_ACMD (1<<2) /* sd only */
223 #define MCI_NOCRC (1<<3)
224 #define MCI_BUSY (1<<4)
226 static bool send_cmd(int drive, uint8_t cmd, uint32_t arg, uint32_t flags, uint32_t *resp)
228 if((flags & MCI_ACMD) && !send_cmd(drive, SD_APP_CMD, SDMMC_RCA(drive), MCI_RESP, resp))
229 return false;
231 enum imx233_ssp_resp_t resp_type = (flags & MCI_LONG_RESP) ? SSP_LONG_RESP :
232 (flags & MCI_RESP) ? SSP_SHORT_RESP : SSP_NO_RESP;
233 enum imx233_ssp_error_t ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive), cmd,
234 arg, resp_type, NULL, 0, !!(flags & MCI_BUSY), false, resp);
235 if(resp_type == SSP_LONG_RESP)
237 /* Our SD codes assume most significant word first, so reverse resp */
238 uint32_t tmp = resp[0];
239 resp[0] = resp[3];
240 resp[3] = tmp;
241 tmp = resp[1];
242 resp[1] = resp[2];
243 resp[2] = tmp;
245 return ret == SSP_SUCCESS;
248 static int wait_for_state(int drive, unsigned state)
250 unsigned long response;
251 unsigned int timeout = current_tick + 5*HZ;
252 int cmd_retry = 10;
254 while (1)
256 /* NOTE: rely on SD_SEND_STATUS=MMC_SEND_STATUS */
257 while(!send_cmd(drive, SD_SEND_STATUS, SDMMC_RCA(drive), MCI_RESP, &response) && cmd_retry > 0)
258 cmd_retry--;
260 if(cmd_retry <= 0)
261 return -1;
263 if(((response >> 9) & 0xf) == state)
264 return 0;
266 if(TIME_AFTER(current_tick, timeout))
267 return -10 * ((response >> 9) & 0xf);
269 if(TIME_AFTER(current_tick, next_yield))
271 yield();
272 next_yield = current_tick + MIN_YIELD_PERIOD;
276 return 0;
279 #if CONFIG_STORAGE & STORAGE_SD
280 static int init_sd_card(int drive)
282 int ssp = SDMMC_SSP(drive);
283 sdmmc_power(drive, false);
284 sdmmc_power(drive, true);
285 imx233_ssp_start(ssp);
286 imx233_ssp_softreset(ssp);
287 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
288 /* SSPCLK @ 96MHz
289 * gives bitrate of 96000 / 240 / 1 = 400kHz */
290 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
292 imx233_ssp_sd_mmc_power_up_sequence(ssp);
293 imx233_ssp_set_bus_width(ssp, 1);
294 imx233_ssp_set_block_size(ssp, 9);
296 SDMMC_RCA(drive) = 0;
297 bool sd_v2 = false;
298 uint32_t resp;
299 long init_timeout;
300 /* go to idle state */
301 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
302 return -1;
303 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
304 Non v2 cards will not respond to this command */
305 if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &resp))
306 if((resp & 0xFFF) == 0x1AA)
307 sd_v2 = true;
308 /* timeout for initialization is 1sec, from SD Specification 2.00 */
309 init_timeout = current_tick + HZ;
312 /* this timeout is the only valid error for this loop*/
313 if(TIME_AFTER(current_tick, init_timeout))
314 return -2;
316 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
317 if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
318 MCI_ACMD|MCI_NOCRC|MCI_RESP, &SDMMC_INFO(drive).ocr))
319 return -100;
320 } while(!(SDMMC_INFO(drive).ocr & (1<<31)));
322 /* CMD2 send CID */
323 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, SDMMC_INFO(drive).cid))
324 return -3;
326 /* CMD3 send RCA */
327 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &SDMMC_INFO(drive).rca))
328 return -4;
330 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
331 if(sd_v2)
333 /* CMD7 w/rca: Select card to put it in TRAN state */
334 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, NULL))
335 return -5;
337 if(wait_for_state(drive, SD_TRAN))
338 return -6;
340 /* CMD6 */
341 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
342 return -7;
343 sleep(HZ/10);
345 /* go back to STBY state so we can read csd */
346 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
347 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
348 return -8;
351 /* CMD9 send CSD */
352 if(!send_cmd(drive, SD_SEND_CSD, SDMMC_RCA(drive), MCI_RESP|MCI_LONG_RESP,
353 SDMMC_INFO(drive).csd))
354 return -9;
356 sd_parse_csd(&SDMMC_INFO(drive));
357 window_start[drive] = 0;
358 window_end[drive] = SDMMC_INFO(drive).numblocks;
360 /* SSPCLK @ 96MHz
361 * gives bitrate of 96 / 4 / 1 = 24MHz */
362 imx233_ssp_set_timings(ssp, 4, 0, 0xffff);
364 /* CMD7 w/rca: Select card to put it in TRAN state */
365 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &resp))
366 return -12;
367 if(wait_for_state(drive, SD_TRAN))
368 return -13;
370 /* ACMD6: set bus width to 4-bit */
371 if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp))
372 return -15;
373 /* ACMD42: disconnect the pull-up resistor on CD/DAT3 */
374 if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP|MCI_ACMD, &resp))
375 return -17;
377 /* Switch to 4-bit */
378 imx233_ssp_set_bus_width(ssp, 4);
380 SDMMC_INFO(drive).initialized = 1;
382 return 0;
384 #endif
386 #if CONFIG_STORAGE & STORAGE_MMC
387 static int init_mmc_drive(int drive)
389 int ssp = SDMMC_SSP(drive);
390 /* we can choose the RCA of mmc cards: pick drive. Following our convention,
391 * .rca is actually RCA << 16 */
392 SDMMC_RCA(drive) = drive << 16;
394 sdmmc_power(drive, false);
395 sdmmc_power(drive, true);
396 imx233_ssp_start(ssp);
397 imx233_ssp_softreset(ssp);
398 imx233_ssp_set_mode(ssp, HW_SSP_CTRL1__SSP_MODE__SD_MMC);
399 /* SSPCLK @ 96MHz
400 * gives bitrate of 96000 / 240 / 1 = 400kHz */
401 imx233_ssp_set_timings(ssp, 240, 0, 0xffff);
402 imx233_ssp_sd_mmc_power_up_sequence(ssp);
403 imx233_ssp_set_bus_width(ssp, 1);
404 imx233_ssp_set_block_size(ssp, 9);
405 /* go to idle state */
406 if(!send_cmd(drive, MMC_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
407 return -1;
408 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
409 unsigned timeout = current_tick + HZ;
410 bool ret = false;
413 uint32_t ocr;
414 ret = send_cmd(drive, MMC_SEND_OP_COND, 0x40ff8000, MCI_RESP, &ocr);
415 if(ret && ocr & (1 << 31))
416 break;
417 }while(!TIME_AFTER(current_tick, timeout));
419 if(!ret)
420 return -2;
421 /* get CID */
422 uint32_t cid[4];
423 if(!send_cmd(drive, MMC_ALL_SEND_CID, 0, MCI_LONG_RESP, cid))
424 return -3;
425 /* Set RCA */
426 uint32_t status;
427 if(!send_cmd(drive, MMC_SET_RELATIVE_ADDR, SDMMC_RCA(drive), MCI_RESP, &status))
428 return -4;
429 /* Select card */
430 if(!send_cmd(drive, MMC_SELECT_CARD, SDMMC_RCA(drive), MCI_RESP, &status))
431 return -5;
432 /* Check TRAN state */
433 if(wait_for_state(drive, MMC_TRAN))
434 return -6;
435 /* Switch to 8-bit bus */
436 if(!send_cmd(drive, MMC_SWITCH, 0x3b70200, MCI_RESP|MCI_BUSY, &status))
437 return -8;
438 /* switch error ? */
439 if(status & 0x80)
440 return -9;
441 imx233_ssp_set_bus_width(ssp, 8);
442 /* Switch to high speed mode */
443 if(!send_cmd(drive, MMC_SWITCH, 0x3b90100, MCI_RESP|MCI_BUSY, &status))
444 return -10;
445 /* switch error ?*/
446 if(status & 0x80)
447 return -11;
448 /* SSPCLK @ 96MHz
449 * gives bitrate of 96 / 2 / 1 = 48MHz */
450 imx233_ssp_set_timings(ssp, 2, 0, 0xffff);
452 /* read extended CSD */
454 uint8_t ext_csd[512];
455 if(imx233_ssp_sd_mmc_transfer(ssp, 8, 0, SSP_SHORT_RESP, aligned_buffer[drive], 1, true, true, &status))
456 return -12;
457 memcpy(ext_csd, aligned_buffer[drive], 512);
458 uint32_t *sec_count = (void *)&ext_csd[212];
459 window_start[drive] = 0;
460 window_end[drive] = *sec_count;
462 /* deselect card */
463 if(!send_cmd(drive, MMC_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
464 return -13;
466 return 0;
468 #endif
470 // low-level function, don't call directly!
471 static int __xfer_sectors(int drive, unsigned long start, int count, void *buf, bool read)
473 uint32_t resp;
474 int ret = 0;
475 while(count != 0)
477 int this_count = MIN(count, IMX233_MAX_SINGLE_DMA_XFER_SIZE / 512);
478 /* Set bank_start to the correct unit (blocks or bytes).
479 * MMC drives use block addressing, SD cards bytes or blocks */
480 int bank_start = start;
481 if(SDMMC_MODE(drive) == SD_MODE && !(SDMMC_INFO(drive).ocr & (1<<30))) /* not SDHC */
482 bank_start *= SD_BLOCK_SIZE;
483 /* issue read/write
484 * NOTE: rely on SD_{READ,WRITE}_MULTIPLE_BLOCK=MMC_{READ,WRITE}_MULTIPLE_BLOCK */
485 ret = imx233_ssp_sd_mmc_transfer(SDMMC_SSP(drive),
486 read ? SD_READ_MULTIPLE_BLOCK : SD_WRITE_MULTIPLE_BLOCK,
487 bank_start, SSP_SHORT_RESP, buf, this_count, false, read, &resp);
488 if(ret != SSP_SUCCESS)
489 break;
490 /* stop transmission
491 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
492 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp))
494 ret = -15;
495 break;
497 count -= this_count;
498 start += this_count;
499 buf += this_count * 512;
501 return ret;
504 static int transfer_sectors(int drive, unsigned long start, int count, void *buf, bool read)
506 int ret = 0;
508 /* update disk activity */
509 disk_last_activity[drive] = current_tick;
511 /* lock per-drive mutex */
512 mutex_lock(&mutex[drive]);
514 /* for SD cards, init if necessary */
515 #if CONFIG_STORAGE & STORAGE_SD
516 if(SDMMC_MODE(drive) == SD_MODE && SDMMC_INFO(drive).initialized <= 0)
518 ret = init_sd_card(drive);
519 if(SDMMC_INFO(drive).initialized <= 0)
520 goto Lend;
522 #endif
524 /* check window */
525 start += window_start[drive];
526 if((start + count) > window_end[drive])
528 ret = -201;
529 goto Lend;
531 /* select card.
532 * NOTE: rely on SD_SELECT_CARD=MMC_SELECT_CARD */
533 if(!send_cmd(drive, SD_SELECT_CARD, SDMMC_RCA(drive), MCI_NO_RESP, NULL))
535 ret = -20;
536 goto Lend;
538 /* wait for TRAN state */
539 /* NOTE: rely on SD_TRAN=MMC_TRAN */
540 ret = wait_for_state(drive, SD_TRAN);
541 if(ret < 0)
542 goto Ldeselect;
545 * NOTE: we need to make sure dma transfers are aligned. This handled
546 * differently for read and write transfers. We do not repeat it each
547 * time but it should be noted that all transfers are limited by
548 * IMX233_MAX_SINGLE_DMA_XFER_SIZE and thus need to be split if needed.
550 * Read transfers:
551 * If the buffer is already aligned, transfer everything at once.
552 * Otherwise, transfer all sectors but one to the sub-buffer starting
553 * on the next cache ligned and then move the data. Then transfer the
554 * last sector to the aligned_buffer and then copy to the buffer.
556 * Write transfers:
557 * If the buffer is already aligned, transfer everything at once.
558 * Otherwise, copy the first sector to the aligned_buffer and transfer.
559 * Then move all other sectors within the buffer to make it cache
560 * aligned and transfer it.
562 if(read)
564 void *ptr = CACHEALIGN_UP(buf);
565 if(buf != ptr)
567 // copy count-1 sector and then move within the buffer
568 ret = __xfer_sectors(drive, start, count - 1, ptr, read);
569 memmove(buf, ptr, 512 * (count - 1));
570 if(ret >= 0)
572 // transfer the last sector the aligned_buffer and copy
573 ret = __xfer_sectors(drive, start + count - 1, 1,
574 aligned_buffer[drive], read);
575 memcpy(buf + 512 * (count - 1), aligned_buffer[drive], 512);
578 else
579 ret = __xfer_sectors(drive, start, count, buf, read);
581 else
583 void *ptr = CACHEALIGN_UP(buf);
584 if(buf != ptr)
586 // transfer the first sector to aligned_buffer and copy
587 memcpy(aligned_buffer[drive], buf, 512);
588 ret = __xfer_sectors(drive, start, 1, aligned_buffer[drive], read);
589 if(ret >= 0)
591 // move within the buffer and transfer
592 memmove(ptr, buf + 512, 512 * (count - 1));
593 ret = __xfer_sectors(drive, start + 1, count - 1, ptr, read);
596 else
597 ret = __xfer_sectors(drive, start, count, buf, read);
600 /* deselect card */
601 Ldeselect:
602 /* CMD7 w/rca =0 : deselects card & puts it in STBY state
603 * NOTE: rely on SD_DESELECT_CARD=MMC_DESELECT_CARD */
604 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
605 ret = -23;
606 Lend:
607 /* release per-drive mutex */
608 mutex_unlock(&mutex[drive]);
609 return ret;
612 static int init_drive(int drive)
614 int ret;
615 switch(SDMMC_MODE(drive))
617 #if CONFIG_STORAGE & STORAGE_SD
618 case SD_MODE: ret = init_sd_card(drive); break;
619 #endif
620 #if CONFIG_STORAGE & STORAGE_MMC
621 case MMC_MODE: ret = init_mmc_drive(drive); break;
622 #endif
623 default: ret = 0;
625 if(ret < 0)
626 return ret;
628 /* compute window */
629 if((SDMMC_FLAGS(drive) & WINDOW) && imx233_partitions_is_window_enabled())
631 uint8_t mbr[512];
632 int ret = transfer_sectors(drive, 0, 1, mbr, true);
633 if(ret)
634 panicf("Cannot read MBR: %d", ret);
635 ret = imx233_partitions_compute_window(mbr, &window_start[drive],
636 &window_end[drive]);
637 if(ret)
638 panicf("cannot compute partitions window: %d", ret);
639 SDMMC_INFO(drive).numblocks = window_end[drive] - window_start[drive];
642 return 0;
645 static void sdmmc_thread(void) NORETURN_ATTR;
646 static void sdmmc_thread(void)
648 struct queue_event ev;
649 bool idle_notified = false;
650 int timeout = 0;
652 while (1)
654 queue_wait_w_tmo(&sdmmc_queue, &ev, HZ);
656 switch(ev.id)
658 #if CONFIG_STORAGE & STORAGE_SD
659 case SYS_HOTSWAP_INSERTED:
660 case SYS_HOTSWAP_EXTRACTED:
662 int microsd_init = 1;
663 /* lock-out FAT activity first -
664 * prevent deadlocking via disk_mount that
665 * would cause a reverse-order attempt with
666 * another thread */
667 fat_lock();
669 /* We now have exclusive control of fat cache and sd.
670 * Release "by force", ensure file
671 * descriptors aren't leaked and any busy
672 * ones are invalid if mounting. */
673 for(unsigned sd_drive = 0; sd_drive < _sd_num_drives; sd_drive++)
675 int drive = sd_map[sd_drive];
676 /* Skip non-removable drivers */
677 if(!sd_removable(sd_drive))
678 continue;
679 /* lock-out card activity - direct calls
680 * into driver that bypass the fat cache */
681 mutex_lock(&mutex[drive]);
682 disk_unmount(sd_first_drive + sd_drive);
683 /* Force card init for new card, re-init for re-inserted one or
684 * clear if the last attempt to init failed with an error. */
685 SDMMC_INFO(sd_map[sd_drive]).initialized = 0;
687 if(ev.id == SYS_HOTSWAP_INSERTED)
689 microsd_init = init_drive(drive);
690 if(microsd_init < 0) /* initialisation failed */
691 panicf("%s init failed : %d", SDMMC_CONF(sd_map[sd_drive]).name, microsd_init);
693 microsd_init = disk_mount(sd_first_drive + sd_drive); /* 0 if fail */
696 * Mount succeeded, or this was an EXTRACTED event,
697 * in both cases notify the system about the changed filesystems
699 if(microsd_init)
700 queue_broadcast(SYS_FS_CHANGED, 0);
701 /* unlock card */
702 mutex_unlock(&mutex[drive]);
704 /* Access is now safe */
705 fat_unlock();
706 break;
708 #endif
709 case SYS_TIMEOUT:
710 #if CONFIG_STORAGE & STORAGE_SD
711 timeout = MAX(timeout, sd_last_disk_activity()+(3*HZ));
712 #endif
713 #if CONFIG_STORAGE & STORAGE_MMC
714 timeout = MAX(timeout, mmc_last_disk_activity()+(3*HZ));
715 #endif
716 if(TIME_BEFORE(current_tick, timeout))
718 idle_notified = false;
720 else
722 next_yield = current_tick;
724 if(!idle_notified)
726 call_storage_idle_notifys(false);
727 idle_notified = true;
730 break;
731 break;
732 case SYS_USB_CONNECTED:
733 usb_acknowledge(SYS_USB_CONNECTED_ACK);
734 /* Wait until the USB cable is extracted again */
735 usb_wait_for_disconnect(&sdmmc_queue);
736 break;
741 static int sdmmc_init(void)
743 static int is_initialized = false;
744 if(is_initialized)
745 return 0;
746 is_initialized = true;
747 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
748 mutex_init(&mutex[drive]);
750 queue_init(&sdmmc_queue, true);
751 create_thread(sdmmc_thread, sdmmc_stack, sizeof(sdmmc_stack), 0,
752 sdmmc_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
754 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
756 if(SDMMC_FLAGS(drive) & REMOVABLE)
757 imx233_ssp_sdmmc_setup_detect(SDMMC_SSP(drive), true, sdmmc_detect_callback,
758 false, SDMMC_FLAGS(drive) & DETECT_INVERTED);
761 return 0;
764 static int sdmmc_present(int drive)
766 if(SDMMC_FLAGS(drive) & REMOVABLE)
767 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive));
768 else
769 return true;
772 static inline int sdmmc_removable(int drive)
774 return SDMMC_FLAGS(drive) & REMOVABLE;
777 #if CONFIG_STORAGE & STORAGE_SD
778 int sd_init(void)
780 int ret = sdmmc_init();
781 if(ret < 0) return ret;
783 _sd_num_drives = 0;
784 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
785 if(SDMMC_MODE(drive) == SD_MODE)
786 sd_map[_sd_num_drives++] = drive;
787 return 0;
790 tCardInfo *card_get_info_target(int sd_card_no)
792 return &SDMMC_INFO(sd_map[sd_card_no]);
795 int sd_num_drives(int first_drive)
797 sd_first_drive = first_drive;
798 return _sd_num_drives;
801 bool sd_present(IF_MV_NONVOID(int sd_drive))
803 return sdmmc_present(sd_map[sd_drive]);
806 bool sd_removable(IF_MV_NONVOID(int sd_drive))
808 return sdmmc_removable(sd_map[sd_drive]);
811 long sd_last_disk_activity(void)
813 long last = 0;
814 for(unsigned i = 0; i < _sd_num_drives; i++)
815 last = MAX(last, disk_last_activity[sd_map[i]]);
816 return last;
819 void sd_enable(bool on)
821 (void) on;
824 int sd_read_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, void *buf)
826 return transfer_sectors(sd_map[sd_drive], start, count, buf, true);
829 int sd_write_sectors(IF_MD2(int sd_drive,) unsigned long start, int count, const void* buf)
831 return transfer_sectors(sd_map[sd_drive], start, count, (void *)buf, false);
833 #endif
835 #if CONFIG_STORAGE & STORAGE_MMC
836 int mmc_init(void)
838 int ret = sdmmc_init();
839 if(ret < 0) return ret;
841 _sd_num_drives = 0;
842 for(unsigned drive = 0; drive < SDMMC_NUM_DRIVES; drive++)
843 if(SDMMC_MODE(drive) == MMC_MODE)
845 mmc_map[_mmc_num_drives++] = drive;
846 init_drive(drive);
848 return 0;
851 void mmc_get_info(IF_MD2(int mmc_drive,) struct storage_info *info)
853 int drive = mmc_map[mmc_drive];
854 info->sector_size = 512;
855 info->num_sectors = window_end[drive] - window_start[drive];
856 info->vendor = "Rockbox";
857 info->product = "Internal Storage";
858 info->revision = "0.00";
861 int mmc_num_drives(int first_drive)
863 mmc_first_drive = first_drive;
864 return _mmc_num_drives;
867 bool mmc_present(IF_MV_NONVOID(int mmc_drive))
869 return sdmmc_present(mmc_map[mmc_drive]);
872 bool mmc_removable(IF_MV_NONVOID(int mmc_drive))
874 return sdmmc_removable(mmc_map[mmc_drive]);
877 long mmc_last_disk_activity(void)
879 long last = 0;
880 for(unsigned i = 0; i < _mmc_num_drives; i++)
881 last = MAX(last, disk_last_activity[mmc_map[i]]);
882 return last;
885 void mmc_enable(bool on)
887 (void) on;
890 void mmc_sleep(void)
894 void mmc_sleepnow(void)
898 bool mmc_disk_is_active(void)
900 return false;
903 bool mmc_usb_active(void)
905 return mmc_disk_is_active();
908 int mmc_soft_reset(void)
910 return 0;
913 int mmc_flush(void)
915 return 0;
918 void mmc_spin(void)
922 void mmc_spindown(int seconds)
924 (void) seconds;
927 int mmc_spinup_time(void)
929 return 0;
932 int mmc_read_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, void *buf)
934 return transfer_sectors(mmc_map[mmc_drive], start, count, buf, true);
937 int mmc_write_sectors(IF_MD2(int mmc_drive,) unsigned long start, int count, const void* buf)
939 return transfer_sectors(mmc_map[mmc_drive], start, count, (void *)buf, false);
942 #endif