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 ****************************************************************************/
25 #include "ssp-imx233.h"
26 #include "pinctrl-imx233.h"
27 #include "partitions-imx233.h"
28 #include "button-target.h"
36 const char *name
; /* name(for debug) */
37 int flags
; /* flags */
38 int power_pin
; /* power pin */
39 int power_delay
; /* extra power up delay */
40 int ssp
; /* associated ssp block */
44 #define POWER_PIN (1 << 0)
45 #define POWER_INVERTED (1 << 1)
46 #define REMOVABLE (1 << 2)
47 #define DETECT_INVERTED (1 << 3)
48 #define POWER_DELAY (1 << 4)
49 #define WINDOW (1 << 5)
51 #define PIN(bank,pin) ((bank) << 5 | (pin))
52 #define PIN2BANK(v) ((v) >> 5)
53 #define PIN2PIN(v) ((v) & 0x1f)
55 struct sd_config_t sd_config
[] =
58 /* The Fuze+ uses pin #B0P8 for power */
61 .flags
= POWER_PIN
| POWER_INVERTED
| REMOVABLE
,
62 .power_pin
= PIN(0, 8),
65 #elif defined(CREATIVE_ZENXFI2)
66 /* The Zen X-Fi2 uses pin B1P29 for power*/
69 .flags
= POWER_PIN
| REMOVABLE
| DETECT_INVERTED
,
70 .power_pin
= PIN(1, 29),
73 #elif defined(CREATIVE_ZENXFI3)
75 .name
= "internal/SD",
79 /* The Zen X-Fi3 uses pin #B0P07 for power*/
82 .flags
= POWER_PIN
| POWER_INVERTED
| REMOVABLE
| POWER_DELAY
,
83 .power_pin
= PIN(0, 7),
84 .power_delay
= HZ
/ 10, /* extra delay, to ramp up voltage? */
88 #error You need to write the sd config!
92 #define SD_NUM_DRIVES (sizeof(sd_config) / sizeof(sd_config[0]))
94 #define SD_CONF(drive) sd_config[drive]
95 #define SD_FLAGS(drive) SD_CONF(drive).flags
96 #define SD_SSP(drive) SD_CONF(drive).ssp
97 #define IF_FIRST_DRIVE(drive) if((drive) == 0)
98 #define IF_SECOND_DRIVE(drive) if((drive) == 1)
100 static tCardInfo card_info
[SD_NUM_DRIVES
];
101 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
102 static struct mutex sd_mutex
;
103 static const char sd_thread_name
[] = "sd";
104 static struct event_queue sd_queue
;
105 static int sd_first_drive
;
106 static int last_disk_activity
;
107 static unsigned sd_window_start
[SD_NUM_DRIVES
];
108 static unsigned sd_window_end
[SD_NUM_DRIVES
];
110 static void sd_detect_callback(int ssp
)
112 /* This is called only if the state was stable for 300ms - check state
113 * and post appropriate event. */
114 if(imx233_ssp_sdmmc_detect(ssp
))
115 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
117 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
118 imx233_ssp_sdmmc_setup_detect(ssp
, true, sd_detect_callback
, false,
119 imx233_ssp_sdmmc_is_detect_inverted(ssp
));
122 void sd_power(int drive
, bool on
)
124 /* power chip if needed */
125 if(SD_FLAGS(drive
) & POWER_PIN
)
127 int bank
= PIN2BANK(SD_CONF(drive
).power_pin
);
128 int pin
= PIN2PIN(SD_CONF(drive
).power_pin
);
129 imx233_pinctrl_acquire_pin(bank
, pin
, "sd power");
130 imx233_set_pin_function(bank
, pin
, PINCTRL_FUNCTION_GPIO
);
131 imx233_enable_gpio_output(bank
, pin
, true);
132 if(SD_FLAGS(drive
) & POWER_INVERTED
)
133 imx233_set_gpio_output(bank
, pin
, !on
);
135 imx233_set_gpio_output(bank
, pin
, on
);
137 if(SD_FLAGS(drive
) & POWER_DELAY
)
138 sleep(SD_CONF(drive
).power_delay
);
139 /* setup pins, never use alternatives pin on SSP1 because these are force
140 * bus width >= 4 and SD cannot use more than 4 data lines. */
141 if(SD_SSP(drive
) == 1)
142 imx233_ssp_setup_ssp1_sd_mmc_pins(on
, 4, PINCTRL_DRIVE_4mA
, false);
144 imx233_ssp_setup_ssp2_sd_mmc_pins(on
, 4, PINCTRL_DRIVE_4mA
);
147 void sd_enable(bool on
)
152 #define MCI_NO_RESP 0
153 #define MCI_RESP (1<<0)
154 #define MCI_LONG_RESP (1<<1)
155 #define MCI_ACMD (1<<2)
156 #define MCI_NOCRC (1<<3)
157 #define MCI_BUSY (1<<4)
159 static bool send_cmd(int drive
, uint8_t cmd
, uint32_t arg
, uint32_t flags
, uint32_t *resp
)
161 if((flags
& MCI_ACMD
) && !send_cmd(drive
, SD_APP_CMD
, card_info
[drive
].rca
, MCI_RESP
, resp
))
164 enum imx233_ssp_resp_t resp_type
= (flags
& MCI_LONG_RESP
) ? SSP_LONG_RESP
:
165 (flags
& MCI_RESP
) ? SSP_SHORT_RESP
: SSP_NO_RESP
;
166 enum imx233_ssp_error_t ret
= imx233_ssp_sd_mmc_transfer(SD_SSP(drive
), cmd
,
167 arg
, resp_type
, NULL
, 0, !!(flags
& MCI_BUSY
), false, resp
);
168 if(resp_type
== SSP_LONG_RESP
)
170 /* Our SD codes assume most significant word first, so reverse resp */
171 uint32_t tmp
= resp
[0];
178 return ret
== SSP_SUCCESS
;
181 static int sd_wait_for_tran_state(int drive
)
183 unsigned long response
;
184 unsigned int timeout
= current_tick
+ 5*HZ
;
189 while(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
, MCI_RESP
, &response
) && cmd_retry
> 0)
195 if(((response
>> 9) & 0xf) == SD_TRAN
)
198 if(TIME_AFTER(current_tick
, timeout
))
199 return -10 * ((response
>> 9) & 0xf);
201 last_disk_activity
= current_tick
;
205 static int sd_init_card(int drive
)
207 /* sanity check against bad configuration of SD_NUM_DRIVES/NUM_DRIVES */
208 if((unsigned)drive
>= SD_NUM_DRIVES
)
209 panicf("drive >= SD_NUM_DRIVES in sd_init_card!");
210 int ssp
= SD_SSP(drive
);
211 sd_power(drive
, false);
212 sd_power(drive
, true);
213 imx233_ssp_start(ssp
);
214 imx233_ssp_softreset(ssp
);
215 imx233_ssp_set_mode(ssp
, HW_SSP_CTRL1__SSP_MODE__SD_MMC
);
217 * gives bitrate of 96000 / 240 / 1 = 400kHz */
218 imx233_ssp_set_timings(ssp
, 240, 0, 0xffff);
220 imx233_ssp_sd_mmc_power_up_sequence(ssp
);
221 imx233_ssp_set_bus_width(ssp
, 1);
222 imx233_ssp_set_block_size(ssp
, 9);
224 card_info
[drive
].rca
= 0;
228 /* go to idle state */
229 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
231 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
232 Non v2 cards will not respond to this command */
233 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
, &resp
))
234 if((resp
& 0xFFF) == 0x1AA)
236 /* timeout for initialization is 1sec, from SD Specification 2.00 */
237 init_timeout
= current_tick
+ HZ
;
240 /* this timeout is the only valid error for this loop*/
241 if(TIME_AFTER(current_tick
, init_timeout
))
244 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
245 if(!send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
246 MCI_ACMD
|MCI_NOCRC
|MCI_RESP
, &card_info
[drive
].ocr
))
248 } while(!(card_info
[drive
].ocr
& (1<<31)));
251 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
, card_info
[drive
].cid
))
255 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
, &card_info
[drive
].rca
))
258 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
261 /* CMD7 w/rca: Select card to put it in TRAN state */
262 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_RESP
, NULL
))
265 if(sd_wait_for_tran_state(drive
))
269 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_NO_RESP
, NULL
))
273 /* go back to STBY state so we can read csd */
274 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
275 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
280 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
, MCI_RESP
|MCI_LONG_RESP
, card_info
[drive
].csd
))
283 sd_parse_csd(&card_info
[drive
]);
286 * gives bitrate of 96 / 4 / 1 = 24MHz */
287 imx233_ssp_set_timings(ssp
, 4, 0, 0xffff);
289 /* CMD7 w/rca: Select card to put it in TRAN state */
290 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_RESP
, &resp
))
292 if(sd_wait_for_tran_state(drive
) < 0)
295 /* ACMD6: set bus width to 4-bit */
296 if(!send_cmd(drive
, SD_SET_BUS_WIDTH
, 2, MCI_RESP
|MCI_ACMD
, &resp
))
298 /* ACMD42: disconnect the pull-up resistor on CD/DAT3 */
299 if(!send_cmd(drive
, SD_SET_CLR_CARD_DETECT
, 0, MCI_RESP
|MCI_ACMD
, &resp
))
302 /* Switch to 4-bit */
303 imx233_ssp_set_bus_width(ssp
, 4);
305 card_info
[drive
].initialized
= 1;
308 sd_window_start
[drive
] = 0;
309 sd_window_end
[drive
] = card_info
[drive
].numblocks
;
310 if((SD_FLAGS(drive
) & WINDOW
) && imx233_partitions_is_window_enabled())
312 /* WARNING: sd_first_drive is not set at this point */
314 int ret
= sd_read_sectors(IF_MD2(drive
,) 0, 1, mbr
);
316 panicf("Cannot read MBR: %d", ret
);
317 ret
= imx233_partitions_compute_window(mbr
, &sd_window_start
[drive
],
318 &sd_window_end
[drive
]);
320 panicf("cannot compute partitions window: %d", ret
);
321 card_info
[drive
].numblocks
= sd_window_end
[drive
] - sd_window_start
[drive
];
327 static void sd_thread(void) NORETURN_ATTR
;
328 static void sd_thread(void)
330 struct queue_event ev
;
334 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
338 case SYS_HOTSWAP_INSERTED
:
339 case SYS_HOTSWAP_EXTRACTED
:
341 int microsd_init
= 1;
342 /* lock-out FAT activity first -
343 * prevent deadlocking via disk_mount that
344 * would cause a reverse-order attempt with
347 /* lock-out card activity - direct calls
348 * into driver that bypass the fat cache */
349 mutex_lock(&sd_mutex
);
351 /* We now have exclusive control of fat cache and sd.
352 * Release "by force", ensure file
353 * descriptors aren't leaked and any busy
354 * ones are invalid if mounting. */
355 for(unsigned drive
= 0; drive
< SD_NUM_DRIVES
; drive
++)
357 /* Skip non-removable drivers */
358 if(!sd_removable(drive
))
360 disk_unmount(sd_first_drive
+ drive
);
361 /* Force card init for new card, re-init for re-inserted one or
362 * clear if the last attempt to init failed with an error. */
363 card_info
[drive
].initialized
= 0;
365 if(ev
.id
== SYS_HOTSWAP_INSERTED
)
367 microsd_init
= sd_init_card(drive
);
368 if(microsd_init
< 0) /* initialisation failed */
369 panicf("%s init failed : %d", SD_CONF(drive
).name
, microsd_init
);
371 microsd_init
= disk_mount(sd_first_drive
+ drive
); /* 0 if fail */
374 * Mount succeeded, or this was an EXTRACTED event,
375 * in both cases notify the system about the changed filesystems
378 queue_broadcast(SYS_FS_CHANGED
, 0);
380 /* Access is now safe */
381 mutex_unlock(&sd_mutex
);
386 if(!TIME_BEFORE(current_tick
, last_disk_activity
+3 * HZ
))
389 case SYS_USB_CONNECTED
:
390 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
391 /* Wait until the USB cable is extracted again */
392 usb_wait_for_disconnect(&sd_queue
);
400 mutex_init(&sd_mutex
);
401 queue_init(&sd_queue
, true);
402 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
403 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
405 for(unsigned drive
= 0; drive
< SD_NUM_DRIVES
; drive
++)
407 if(SD_FLAGS(drive
) & REMOVABLE
)
408 imx233_ssp_sdmmc_setup_detect(SD_SSP(drive
), true, sd_detect_callback
,
409 false, SD_FLAGS(drive
) & DETECT_INVERTED
);
415 static int transfer_sectors(IF_MD2(int drive
,) unsigned long start
, int count
, void *buf
, bool read
)
420 last_disk_activity
= current_tick
;
422 mutex_lock(&sd_mutex
);
424 if(card_info
[drive
].initialized
<= 0)
426 ret
= sd_init_card(drive
);
427 if(card_info
[drive
].initialized
<= 0)
432 start
+= sd_window_start
[drive
];
433 if((start
+ count
) >= sd_window_end
[drive
])
439 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_NO_RESP
, NULL
))
444 ret
= sd_wait_for_tran_state(drive
);
449 int this_count
= MIN(count
, IMX233_MAX_SSP_XFER_SIZE
/ 512);
450 /* Set bank_start to the correct unit (blocks or bytes) */
451 int bank_start
= start
;
452 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
453 bank_start
*= SD_BLOCK_SIZE
;
454 ret
= imx233_ssp_sd_mmc_transfer(SD_SSP(drive
),
455 read
? SD_READ_MULTIPLE_BLOCK
: SD_WRITE_MULTIPLE_BLOCK
,
456 bank_start
, SSP_SHORT_RESP
, buf
, this_count
, false, read
, &resp
);
457 if(ret
!= SSP_SUCCESS
)
459 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
|MCI_BUSY
, &resp
))
466 buf
+= this_count
* 512;
470 /* CMD7 w/rca =0 : deselects card & puts it in STBY state */
471 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
474 mutex_unlock(&sd_mutex
);
478 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
, void* buf
)
480 return transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, true);
483 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
, const void* buf
)
485 return transfer_sectors(IF_MD2(drive
,) start
, count
, (void *)buf
, false);
488 tCardInfo
*card_get_info_target(int card_no
)
490 return &card_info
[card_no
];
493 int sd_num_drives(int first_drive
)
495 sd_first_drive
= first_drive
;
496 return SD_NUM_DRIVES
;
499 bool sd_present(IF_MV_NONVOID(int drive
))
501 if(SD_FLAGS(drive
) & REMOVABLE
)
502 return imx233_ssp_sdmmc_detect(SD_SSP(drive
));
507 bool sd_removable(IF_MV_NONVOID(int drive
))
509 return SD_FLAGS(drive
) & REMOVABLE
;
512 long sd_last_disk_activity(void)
514 return last_disk_activity
;