1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 #include "ssp-imx233.h"
27 #include "pinctrl-imx233.h"
28 #include "partitions-imx233.h"
29 #include "button-target.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
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) */
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)
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
[] =
83 /* The Fuze+ uses pin #B0P8 for power */
86 .flags
= POWER_PIN
| POWER_INVERTED
| REMOVABLE
,
87 .power_pin
= PIN(0, 8),
91 /* The Fuze+ uses pin #B1P29 for power */
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? */
100 #elif defined(CREATIVE_ZENXFI2)
101 /* The Zen X-Fi2 uses pin B1P29 for power*/
104 .flags
= POWER_PIN
| REMOVABLE
| DETECT_INVERTED
,
105 .power_pin
= PIN(1, 29),
109 #elif defined(CREATIVE_ZENXFI3)
111 .name
= "internal/SD",
116 /* The Zen X-Fi3 uses pin #B0P07 for power*/
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? */
126 #error You need to write the sd/mmc config!
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
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 ! */
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
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 */
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 */
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);
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
);
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);
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
))
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];
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
;
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)
260 if(((response
>> 9) & 0xf) == state
)
263 if(TIME_AFTER(current_tick
, timeout
))
264 return -10 * ((response
>> 9) & 0xf);
266 disk_last_activity
[drive
] = current_tick
;
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
);
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;
293 /* go to idle state */
294 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
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)
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
))
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
))
313 } while(!(SDMMC_INFO(drive
).ocr
& (1<<31)));
316 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, SDMMC_INFO(drive
).cid
))
320 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &SDMMC_INFO(drive
).rca
))
323 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
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
))
330 if(wait_for_state(drive
, SD_TRAN
))
334 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_NO_RESP
, NULL
))
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
))
345 if(!send_cmd(drive
, SD_SEND_CSD
, SDMMC_RCA(drive
), MCI_RESP
|MCI_LONG_RESP
,
346 SDMMC_INFO(drive
).csd
))
349 sd_parse_csd(&SDMMC_INFO(drive
));
350 window_start
[drive
] = 0;
351 window_end
[drive
] = SDMMC_INFO(drive
).numblocks
;
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
))
360 if(wait_for_state(drive
, SD_TRAN
))
363 /* ACMD6: set bus width to 4-bit */
364 if(!send_cmd(drive
, SD_SET_BUS_WIDTH
, 2, MCI_RESP
|MCI_ACMD
, &resp
))
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
))
370 /* Switch to 4-bit */
371 imx233_ssp_set_bus_width(ssp
, 4);
373 SDMMC_INFO(drive
).initialized
= 1;
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
);
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
))
401 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
402 unsigned timeout
= current_tick
+ HZ
;
407 ret
= send_cmd(drive
, MMC_SEND_OP_COND
, 0x40ff8000, MCI_RESP
, &ocr
);
408 if(ret
&& ocr
& (1 << 31))
410 }while(!TIME_AFTER(current_tick
, timeout
));
416 if(!send_cmd(drive
, MMC_ALL_SEND_CID
, 0, MCI_LONG_RESP
, cid
))
420 if(!send_cmd(drive
, MMC_SET_RELATIVE_ADDR
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
423 if(!send_cmd(drive
, MMC_SELECT_CARD
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
425 /* Check TRAN state */
426 if(wait_for_state(drive
, MMC_TRAN
))
428 /* Switch to 8-bit bus */
429 if(!send_cmd(drive
, MMC_SWITCH
, 0x3b70200, MCI_RESP
|MCI_BUSY
, &status
))
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
))
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
))
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
;
456 if(!send_cmd(drive
, MMC_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
463 static int transfer_sectors(int drive
, unsigned long start
, int count
, void *buf
, bool read
)
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)
485 start
+= window_start
[drive
];
486 if((start
+ count
) > window_end
[drive
])
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
))
499 /* wait for TRAN state */
500 /* NOTE: rely on SD_TRAN=MMC_TRAN */
501 ret
= wait_for_state(drive
, SD_TRAN
);
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 */
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 */
517 memcpy(aligned_buffer
[drive
], buf
, 512);
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
)
526 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
527 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
|MCI_BUSY
, &resp
))
532 /* on read transfers, copy the data back to the user buffer */
534 memcpy(buf
, aligned_buffer
[drive
], 512);
537 buf
+= this_count
* 512;
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
))
546 /* release per-drive mutex */
547 mutex_unlock(&mutex
[drive
]);
551 static int init_drive(int drive
)
554 switch(SDMMC_MODE(drive
))
556 #if CONFIG_STORAGE & STORAGE_SD
557 case SD_MODE
: ret
= init_sd_card(drive
); break;
559 #if CONFIG_STORAGE & STORAGE_MMC
560 case MMC_MODE
: ret
= init_mmc_drive(drive
); break;
565 panicf("die %d", ret
);
570 if((SDMMC_FLAGS(drive
) & WINDOW
) && imx233_partitions_is_window_enabled())
573 int ret
= transfer_sectors(drive
, 0, 1, mbr
, true);
575 panicf("Cannot read MBR: %d", ret
);
576 ret
= imx233_partitions_compute_window(mbr
, &window_start
[drive
],
579 panicf("cannot compute partitions window: %d", ret
);
580 SDMMC_INFO(drive
).numblocks
= window_end
[drive
] - window_start
[drive
];
586 static void sd_thread(void) NORETURN_ATTR
;
587 static void sd_thread(void)
589 struct queue_event ev
;
593 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
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
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
))
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
638 queue_broadcast(SYS_FS_CHANGED
, 0);
640 mutex_unlock(&mutex
[drive
]);
642 /* Access is now safe */
647 if(!TIME_BEFORE(current_tick
, sd_last_disk_activity() + 3 * HZ
))
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
);
659 static int sdmmc_init(void)
661 static int is_initialized
= false;
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
));
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
);
684 static int sdmmc_present(int drive
)
686 if(SDMMC_FLAGS(drive
) & REMOVABLE
)
687 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive
));
692 static inline int sdmmc_removable(int drive
)
694 return SDMMC_FLAGS(drive
) & REMOVABLE
;
697 #if CONFIG_STORAGE & STORAGE_SD
700 int ret
= sdmmc_init();
701 if(ret
< 0) return ret
;
704 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
705 if(SDMMC_MODE(drive
) == SD_MODE
)
706 sd_map
[_sd_num_drives
++] = drive
;
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)
734 for(unsigned i
= 0; i
< _sd_num_drives
; i
++)
735 last
= MAX(last
, disk_last_activity
[sd_map
[i
]]);
739 void sd_enable(bool 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);
755 #if CONFIG_STORAGE & STORAGE_MMC
758 int ret
= sdmmc_init();
759 if(ret
< 0) return ret
;
762 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
763 if(SDMMC_MODE(drive
) == MMC_MODE
)
765 mmc_map
[_mmc_num_drives
++] = drive
;
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)
800 for(unsigned i
= 0; i
< _mmc_num_drives
; i
++)
801 last
= MAX(last
, disk_last_activity
[mmc_map
[i
]]);
805 void mmc_enable(bool on
)
814 void mmc_sleepnow(void)
818 bool mmc_disk_is_active(void)
823 bool mmc_usb_active(void)
825 return mmc_disk_is_active();
828 int mmc_soft_reset(void)
842 void mmc_spindown(int seconds
)
847 int mmc_spinup_time(void)
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);