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"
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
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) */
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)
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
[] =
84 /* The Fuze+ uses pin #B0P8 for power */
87 .flags
= POWER_PIN
| POWER_INVERTED
| REMOVABLE
,
88 .power_pin
= PIN(0, 8),
92 /* The Fuze+ uses pin #B1P29 for power */
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? */
101 #elif defined(CREATIVE_ZENXFI2)
102 /* The Zen X-Fi2 uses pin B1P29 for power*/
105 .flags
= POWER_PIN
| REMOVABLE
| DETECT_INVERTED
,
106 .power_pin
= PIN(1, 29),
110 #elif defined(CREATIVE_ZENXFI3)
112 .name
= "internal/SD",
117 /* The Zen X-Fi3 uses pin #B0P07 for power*/
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? */
127 #error You need to write the sd/mmc config!
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
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 ! */
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
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 */
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 */
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);
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
);
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);
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
))
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];
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
;
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)
263 if(((response
>> 9) & 0xf) == state
)
266 if(TIME_AFTER(current_tick
, timeout
))
267 return -10 * ((response
>> 9) & 0xf);
269 if(TIME_AFTER(current_tick
, next_yield
))
272 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
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
);
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;
300 /* go to idle state */
301 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
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)
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
))
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
))
320 } while(!(SDMMC_INFO(drive
).ocr
& (1<<31)));
323 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, SDMMC_INFO(drive
).cid
))
327 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &SDMMC_INFO(drive
).rca
))
330 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
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
))
337 if(wait_for_state(drive
, SD_TRAN
))
341 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_NO_RESP
, NULL
))
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
))
352 if(!send_cmd(drive
, SD_SEND_CSD
, SDMMC_RCA(drive
), MCI_RESP
|MCI_LONG_RESP
,
353 SDMMC_INFO(drive
).csd
))
356 sd_parse_csd(&SDMMC_INFO(drive
));
357 window_start
[drive
] = 0;
358 window_end
[drive
] = SDMMC_INFO(drive
).numblocks
;
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
))
367 if(wait_for_state(drive
, SD_TRAN
))
370 /* ACMD6: set bus width to 4-bit */
371 if(!send_cmd(drive
, SD_SET_BUS_WIDTH
, 2, MCI_RESP
|MCI_ACMD
, &resp
))
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
))
377 /* Switch to 4-bit */
378 imx233_ssp_set_bus_width(ssp
, 4);
380 SDMMC_INFO(drive
).initialized
= 1;
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
);
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
))
408 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
409 unsigned timeout
= current_tick
+ HZ
;
414 ret
= send_cmd(drive
, MMC_SEND_OP_COND
, 0x40ff8000, MCI_RESP
, &ocr
);
415 if(ret
&& ocr
& (1 << 31))
417 }while(!TIME_AFTER(current_tick
, timeout
));
423 if(!send_cmd(drive
, MMC_ALL_SEND_CID
, 0, MCI_LONG_RESP
, cid
))
427 if(!send_cmd(drive
, MMC_SET_RELATIVE_ADDR
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
430 if(!send_cmd(drive
, MMC_SELECT_CARD
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
432 /* Check TRAN state */
433 if(wait_for_state(drive
, MMC_TRAN
))
435 /* Switch to 8-bit bus */
436 if(!send_cmd(drive
, MMC_SWITCH
, 0x3b70200, MCI_RESP
|MCI_BUSY
, &status
))
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
))
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
))
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
;
463 if(!send_cmd(drive
, MMC_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
470 // low-level function, don't call directly!
471 static int __xfer_sectors(int drive
, unsigned long start
, int count
, void *buf
, bool read
)
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
;
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
)
491 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
492 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
|MCI_BUSY
, &resp
))
499 buf
+= this_count
* 512;
504 static int transfer_sectors(int drive
, unsigned long start
, int count
, void *buf
, bool read
)
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)
525 start
+= window_start
[drive
];
526 if((start
+ count
) > window_end
[drive
])
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
))
538 /* wait for TRAN state */
539 /* NOTE: rely on SD_TRAN=MMC_TRAN */
540 ret
= wait_for_state(drive
, SD_TRAN
);
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.
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.
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.
564 void *ptr
= CACHEALIGN_UP(buf
);
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));
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);
579 ret
= __xfer_sectors(drive
, start
, count
, buf
, read
);
583 void *ptr
= CACHEALIGN_UP(buf
);
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
);
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
);
597 ret
= __xfer_sectors(drive
, start
, count
, buf
, read
);
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
))
607 /* release per-drive mutex */
608 mutex_unlock(&mutex
[drive
]);
612 static int init_drive(int drive
)
615 switch(SDMMC_MODE(drive
))
617 #if CONFIG_STORAGE & STORAGE_SD
618 case SD_MODE
: ret
= init_sd_card(drive
); break;
620 #if CONFIG_STORAGE & STORAGE_MMC
621 case MMC_MODE
: ret
= init_mmc_drive(drive
); break;
629 if((SDMMC_FLAGS(drive
) & WINDOW
) && imx233_partitions_is_window_enabled())
632 int ret
= transfer_sectors(drive
, 0, 1, mbr
, true);
634 panicf("Cannot read MBR: %d", ret
);
635 ret
= imx233_partitions_compute_window(mbr
, &window_start
[drive
],
638 panicf("cannot compute partitions window: %d", ret
);
639 SDMMC_INFO(drive
).numblocks
= window_end
[drive
] - window_start
[drive
];
645 static void sdmmc_thread(void) NORETURN_ATTR
;
646 static void sdmmc_thread(void)
648 struct queue_event ev
;
649 bool idle_notified
= false;
654 queue_wait_w_tmo(&sdmmc_queue
, &ev
, HZ
);
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
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
))
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
700 queue_broadcast(SYS_FS_CHANGED
, 0);
702 mutex_unlock(&mutex
[drive
]);
704 /* Access is now safe */
710 #if CONFIG_STORAGE & STORAGE_SD
711 timeout
= MAX(timeout
, sd_last_disk_activity()+(3*HZ
));
713 #if CONFIG_STORAGE & STORAGE_MMC
714 timeout
= MAX(timeout
, mmc_last_disk_activity()+(3*HZ
));
716 if(TIME_BEFORE(current_tick
, timeout
))
718 idle_notified
= false;
722 next_yield
= current_tick
;
726 call_storage_idle_notifys(false);
727 idle_notified
= true;
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
);
741 static int sdmmc_init(void)
743 static int is_initialized
= false;
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
);
764 static int sdmmc_present(int drive
)
766 if(SDMMC_FLAGS(drive
) & REMOVABLE
)
767 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive
));
772 static inline int sdmmc_removable(int drive
)
774 return SDMMC_FLAGS(drive
) & REMOVABLE
;
777 #if CONFIG_STORAGE & STORAGE_SD
780 int ret
= sdmmc_init();
781 if(ret
< 0) return ret
;
784 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
785 if(SDMMC_MODE(drive
) == SD_MODE
)
786 sd_map
[_sd_num_drives
++] = drive
;
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)
814 for(unsigned i
= 0; i
< _sd_num_drives
; i
++)
815 last
= MAX(last
, disk_last_activity
[sd_map
[i
]]);
819 void sd_enable(bool 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);
835 #if CONFIG_STORAGE & STORAGE_MMC
838 int ret
= sdmmc_init();
839 if(ret
< 0) return ret
;
842 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
843 if(SDMMC_MODE(drive
) == MMC_MODE
)
845 mmc_map
[_mmc_num_drives
++] = drive
;
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)
880 for(unsigned i
= 0; i
< _mmc_num_drives
; i
++)
881 last
= MAX(last
, disk_last_activity
[mmc_map
[i
]]);
885 void mmc_enable(bool on
)
894 void mmc_sleepnow(void)
898 bool mmc_disk_is_active(void)
903 bool mmc_usb_active(void)
905 return mmc_disk_is_active();
908 int mmc_soft_reset(void)
922 void mmc_spindown(int seconds
)
927 int mmc_spinup_time(void)
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);