imx233: rework i2c driver to fix dma issues
[maemo-rb.git] / firmware / target / arm / imx233 / sd-imx233.c
blob121b5c17aa27dfdfec37c6bd9773768a0857ed61
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 "sdmmc.h"
25 #include "ssp-imx233.h"
26 #include "pinctrl-imx233.h"
27 #include "partitions-imx233.h"
28 #include "button-target.h"
29 #include "fat.h"
30 #include "disk.h"
31 #include "usb.h"
32 #include "debug.h"
34 struct sd_config_t
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 */
43 /* flags */
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[] =
57 #ifdef SANSA_FUZEPLUS
58 /* The Fuze+ uses pin #B0P8 for power */
60 .name = "microSD",
61 .flags = POWER_PIN | POWER_INVERTED | REMOVABLE,
62 .power_pin = PIN(0, 8),
63 .ssp = 1
65 #elif defined(CREATIVE_ZENXFI2)
66 /* The Zen X-Fi2 uses pin B1P29 for power*/
68 .name = "microSD",
69 .flags = POWER_PIN | REMOVABLE | DETECT_INVERTED,
70 .power_pin = PIN(1, 29),
71 .ssp = 1,
73 #elif defined(CREATIVE_ZENXFI3)
75 .name = "internal/SD",
76 .flags = WINDOW,
77 .ssp = 2
79 /* The Zen X-Fi3 uses pin #B0P07 for power*/
81 .name = "microSD",
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? */
85 .ssp = 1
87 #else
88 #error You need to write the sd config!
89 #endif
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);
116 else
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);
134 else
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);
143 else
144 imx233_ssp_setup_ssp2_sd_mmc_pins(on, 4, PINCTRL_DRIVE_4mA);
147 void sd_enable(bool on)
149 (void) 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))
162 return false;
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];
172 resp[0] = resp[3];
173 resp[3] = tmp;
174 tmp = resp[1];
175 resp[1] = resp[2];
176 resp[2] = tmp;
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;
185 int cmd_retry = 10;
187 while (1)
189 while(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP, &response) && cmd_retry > 0)
190 cmd_retry--;
192 if(cmd_retry <= 0)
193 return -1;
195 if(((response >> 9) & 0xf) == SD_TRAN)
196 return 0;
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);
216 /* SSPCLK @ 96MHz
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;
225 bool sd_v2 = false;
226 uint32_t resp;
227 long init_timeout;
228 /* go to idle state */
229 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
230 return -1;
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)
235 sd_v2 = true;
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))
242 return -2;
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))
247 return -100;
248 } while(!(card_info[drive].ocr & (1<<31)));
250 /* CMD2 send CID */
251 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info[drive].cid))
252 return -3;
254 /* CMD3 send RCA */
255 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
256 return -4;
258 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
259 if(sd_v2)
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))
263 return -5;
265 if(sd_wait_for_tran_state(drive))
266 return -6;
268 /* CMD6 */
269 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
270 return -7;
271 sleep(HZ/10);
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))
276 return -8;
279 /* CMD9 send CSD */
280 if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca, MCI_RESP|MCI_LONG_RESP, card_info[drive].csd))
281 return -9;
283 sd_parse_csd(&card_info[drive]);
285 /* SSPCLK @ 96MHz
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))
291 return -12;
292 if(sd_wait_for_tran_state(drive) < 0)
293 return -13;
295 /* ACMD6: set bus width to 4-bit */
296 if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP|MCI_ACMD, &resp))
297 return -15;
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))
300 return -17;
302 /* Switch to 4-bit */
303 imx233_ssp_set_bus_width(ssp, 4);
305 card_info[drive].initialized = 1;
307 /* compute window */
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 */
313 uint8_t mbr[512];
314 int ret = sd_read_sectors(IF_MD2(drive,) 0, 1, mbr);
315 if(ret)
316 panicf("Cannot read MBR: %d", ret);
317 ret = imx233_partitions_compute_window(mbr, &sd_window_start[drive],
318 &sd_window_end[drive]);
319 if(ret)
320 panicf("cannot compute partitions window: %d", ret);
321 card_info[drive].numblocks = sd_window_end[drive] - sd_window_start[drive];
324 return 0;
327 static void sd_thread(void) NORETURN_ATTR;
328 static void sd_thread(void)
330 struct queue_event ev;
332 while (1)
334 queue_wait_w_tmo(&sd_queue, &ev, HZ);
336 switch(ev.id)
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
345 * another thread */
346 fat_lock();
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))
359 continue;
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
377 if(microsd_init)
378 queue_broadcast(SYS_FS_CHANGED, 0);
380 /* Access is now safe */
381 mutex_unlock(&sd_mutex);
382 fat_unlock();
383 break;
385 case SYS_TIMEOUT:
386 if(!TIME_BEFORE(current_tick, last_disk_activity +3 * HZ))
387 sd_enable(false);
388 break;
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);
393 break;
398 int sd_init(void)
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);
412 return 0;
415 static int transfer_sectors(IF_MD2(int drive,) unsigned long start, int count, void *buf, bool read)
417 int ret = 0;
418 uint32_t resp;
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)
428 goto Lend;
431 /* check window */
432 start += sd_window_start[drive];
433 if((start + count) >= sd_window_end[drive])
435 ret = -201;
436 goto Lend;
439 if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
441 ret = -20;
442 goto Lend;
444 ret = sd_wait_for_tran_state(drive);
445 if(ret < 0)
446 goto Ldeselect;
447 while(count != 0)
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)
458 break;
459 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP|MCI_BUSY, &resp))
461 ret = -15;
462 break;
464 count -= this_count;
465 start += this_count;
466 buf += this_count * 512;
469 Ldeselect:
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))
472 ret = -23;
473 Lend:
474 mutex_unlock(&sd_mutex);
475 return ret;
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));
503 else
504 return true;
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;