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"
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
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) */
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)
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
[] =
85 /* The Fuze+ uses pin #B0P8 for power */
88 .flags
= POWER_PIN
| POWER_INVERTED
| REMOVABLE
,
89 .power_pin
= PIN(0, 8),
93 /* The Fuze+ uses pin #B1P29 for power */
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? */
102 #elif defined(CREATIVE_ZENXFI2)
103 /* The Zen X-Fi2 uses pin B1P29 for power*/
106 .flags
= POWER_PIN
| REMOVABLE
| DETECT_INVERTED
,
107 .power_pin
= PIN(1, 29),
111 #elif defined(CREATIVE_ZENXFI3)
113 .name
= "internal/SD",
118 /* The Zen X-Fi3 uses pin #B0P07 for power*/
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? */
128 #error You need to write the sd/mmc config!
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
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 ! */
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
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 */
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 */
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);
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
);
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);
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
))
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];
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
;
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)
266 if(((response
>> 9) & 0xf) == state
)
269 if(TIME_AFTER(current_tick
, timeout
))
270 return -10 * ((response
>> 9) & 0xf);
272 if(TIME_AFTER(current_tick
, next_yield
))
275 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
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
);
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;
303 /* go to idle state */
304 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
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)
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
))
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
))
323 } while(!(SDMMC_INFO(drive
).ocr
& (1<<31)));
326 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, SDMMC_INFO(drive
).cid
))
330 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &SDMMC_INFO(drive
).rca
))
333 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
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
))
340 if(wait_for_state(drive
, SD_TRAN
))
344 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_NO_RESP
, NULL
))
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
))
355 if(!send_cmd(drive
, SD_SEND_CSD
, SDMMC_RCA(drive
), MCI_RESP
|MCI_LONG_RESP
,
356 SDMMC_INFO(drive
).csd
))
359 sd_parse_csd(&SDMMC_INFO(drive
));
360 window_start
[drive
] = 0;
361 window_end
[drive
] = SDMMC_INFO(drive
).numblocks
;
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
))
370 if(wait_for_state(drive
, SD_TRAN
))
373 /* ACMD6: set bus width to 4-bit */
374 if(!send_cmd(drive
, SD_SET_BUS_WIDTH
, 2, MCI_RESP
|MCI_ACMD
, &resp
))
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
))
380 /* Switch to 4-bit */
381 imx233_ssp_set_bus_width(ssp
, 4);
383 SDMMC_INFO(drive
).initialized
= 1;
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
);
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
))
411 /* send op cond until the card respond with busy bit set; it must complete within 1sec */
412 unsigned timeout
= current_tick
+ HZ
;
417 ret
= send_cmd(drive
, MMC_SEND_OP_COND
, 0x40ff8000, MCI_RESP
, &ocr
);
418 if(ret
&& ocr
& (1 << 31))
420 }while(!TIME_AFTER(current_tick
, timeout
));
426 if(!send_cmd(drive
, MMC_ALL_SEND_CID
, 0, MCI_LONG_RESP
, cid
))
430 if(!send_cmd(drive
, MMC_SET_RELATIVE_ADDR
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
433 if(!send_cmd(drive
, MMC_SELECT_CARD
, SDMMC_RCA(drive
), MCI_RESP
, &status
))
435 /* Check TRAN state */
436 if(wait_for_state(drive
, MMC_TRAN
))
438 /* Switch to 8-bit bus */
439 if(!send_cmd(drive
, MMC_SWITCH
, 0x3b70200, MCI_RESP
|MCI_BUSY
, &status
))
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
))
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
))
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
;
466 if(!send_cmd(drive
, MMC_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
473 // low-level function, don't call directly!
474 static int __xfer_sectors(int drive
, unsigned long start
, int count
, void *buf
, bool read
)
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
;
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
)
494 * NOTE: rely on SD_STOP_TRANSMISSION=MMC_STOP_TRANSMISSION */
495 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
|MCI_BUSY
, &resp
))
502 buf
+= this_count
* 512;
507 static int transfer_sectors(int drive
, unsigned long start
, int count
, void *buf
, bool read
)
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 */
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)
531 start
+= window_start
[drive
];
532 if((start
+ count
) > window_end
[drive
])
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
))
544 /* wait for TRAN state */
545 /* NOTE: rely on SD_TRAN=MMC_TRAN */
546 ret
= wait_for_state(drive
, SD_TRAN
);
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.
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.
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.
570 void *ptr
= CACHEALIGN_UP(buf
);
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));
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);
585 ret
= __xfer_sectors(drive
, start
, count
, buf
, read
);
589 void *ptr
= CACHEALIGN_UP(buf
);
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
);
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
);
603 ret
= __xfer_sectors(drive
, start
, count
, buf
, read
);
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
))
613 /* update led status */
615 /* release per-drive mutex */
616 mutex_unlock(&mutex
[drive
]);
620 static int init_drive(int drive
)
623 switch(SDMMC_MODE(drive
))
625 #if CONFIG_STORAGE & STORAGE_SD
626 case SD_MODE
: ret
= init_sd_card(drive
); break;
628 #if CONFIG_STORAGE & STORAGE_MMC
629 case MMC_MODE
: ret
= init_mmc_drive(drive
); break;
637 if((SDMMC_FLAGS(drive
) & WINDOW
) && imx233_partitions_is_window_enabled())
640 int ret
= transfer_sectors(drive
, 0, 1, mbr
, true);
642 panicf("Cannot read MBR: %d", ret
);
643 ret
= imx233_partitions_compute_window(mbr
, &window_start
[drive
],
646 panicf("cannot compute partitions window: %d", ret
);
647 SDMMC_INFO(drive
).numblocks
= window_end
[drive
] - window_start
[drive
];
653 static void sdmmc_thread(void) NORETURN_ATTR
;
654 static void sdmmc_thread(void)
656 struct queue_event ev
;
657 bool idle_notified
= false;
662 queue_wait_w_tmo(&sdmmc_queue
, &ev
, HZ
);
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
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
))
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
708 queue_broadcast(SYS_FS_CHANGED
, 0);
710 mutex_unlock(&mutex
[drive
]);
712 /* Access is now safe */
718 #if CONFIG_STORAGE & STORAGE_SD
719 timeout
= MAX(timeout
, sd_last_disk_activity()+(3*HZ
));
721 #if CONFIG_STORAGE & STORAGE_MMC
722 timeout
= MAX(timeout
, mmc_last_disk_activity()+(3*HZ
));
724 if(TIME_BEFORE(current_tick
, timeout
))
726 idle_notified
= false;
730 next_yield
= current_tick
;
734 call_storage_idle_notifys(false);
735 idle_notified
= true;
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
);
749 static int sdmmc_init(void)
751 static int is_initialized
= false;
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
);
772 static int sdmmc_present(int drive
)
774 if(SDMMC_FLAGS(drive
) & REMOVABLE
)
775 return imx233_ssp_sdmmc_detect(SDMMC_SSP(drive
));
780 static inline int sdmmc_removable(int drive
)
782 return SDMMC_FLAGS(drive
) & REMOVABLE
;
785 #if CONFIG_STORAGE & STORAGE_SD
788 int ret
= sdmmc_init();
789 if(ret
< 0) return ret
;
792 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
793 if(SDMMC_MODE(drive
) == SD_MODE
)
794 sd_map
[_sd_num_drives
++] = drive
;
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)
822 for(unsigned i
= 0; i
< _sd_num_drives
; i
++)
823 last
= MAX(last
, disk_last_activity
[sd_map
[i
]]);
827 void sd_enable(bool 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);
843 #if CONFIG_STORAGE & STORAGE_MMC
846 int ret
= sdmmc_init();
847 if(ret
< 0) return ret
;
850 for(unsigned drive
= 0; drive
< SDMMC_NUM_DRIVES
; drive
++)
851 if(SDMMC_MODE(drive
) == MMC_MODE
)
853 mmc_map
[_mmc_num_drives
++] = drive
;
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)
888 for(unsigned i
= 0; i
< _mmc_num_drives
; i
++)
889 last
= MAX(last
, disk_last_activity
[mmc_map
[i
]]);
893 void mmc_enable(bool on
)
902 void mmc_sleepnow(void)
906 bool mmc_disk_is_active(void)
911 bool mmc_usb_active(void)
913 return mmc_disk_is_active();
916 int mmc_soft_reset(void)
930 void mmc_spindown(int seconds
)
935 int mmc_spinup_time(void)
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);