Rockchip rk27xx port initial commit. This is still work in progress.
[kugel-rb.git] / firmware / target / arm / rk27xx / sd-rk27xx.c
blobc5a23ad00d82a9a91080f8da22910ac1e6dc872c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Daniel Ankers
11 * Copyright © 2008-2009 Rafaël Carré
12 * Copyright (C) 2011 Marcin Bukat
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "config.h" /* for HAVE_MULTIVOLUME */
25 #include "fat.h"
26 #include "thread.h"
27 #include "gcc_extensions.h"
28 #include "led.h"
29 #include "sdmmc.h"
30 #include "system.h"
31 #include "kernel.h"
32 #include "cpu.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "panic.h"
37 #include "stdbool.h"
38 #include "ata_idle_notify.h"
39 #include "sd.h"
40 #include "usb.h"
42 #ifdef HAVE_HOTSWAP
43 #include "disk.h"
44 #endif
46 #include "lcd.h"
47 #include <stdarg.h>
48 #include "sysfont.h"
50 #define RES_NO (-1)
52 static tCardInfo card_info;
54 /* for compatibility */
55 static long last_disk_activity = -1;
57 static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
58 static const char sd_thread_name[] = "ata/sd";
59 static struct mutex sd_mtx SHAREDBSS_ATTR;
60 static struct event_queue sd_queue;
61 #ifndef BOOTLOADER
62 bool sd_enabled = false;
63 #endif
65 static struct semaphore transfer_completion_signal;
66 static struct semaphore command_completion_signal;
67 static volatile bool retry;
68 static volatile int cmd_error;
70 /* interrupt handler for SD */
71 void INT_SD(void)
73 const int status = SD_INT;
75 SD_INT = 0; /* disable sd interrupts, clear pending interrupts */
77 /* cmd and response status pending */
78 if(status & CMD_RES_STAT)
80 /* get the status */
81 cmd_error = SD_CMDRES;
82 semaphore_release(&command_completion_signal);
85 /* data transfer status pending */
86 if(status & DATA_XFER_STAT)
88 cmd_error = SD_DATAT;
89 if (cmd_error & DATA_XFER_ERR)
90 retry = true;
92 semaphore_release(&transfer_completion_signal);
95 SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
98 /* Exchange buffers - the one where SD module puts into/reads from
99 * data and the one controlled by MCU. This allows some overlap
100 * in transfer operations and should increase throuput.
102 static void mmu_switch_buff(void)
104 static unsigned int i = 0;
106 if (i++ & 0x01)
108 MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
109 MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;
111 else
113 MMU_CTRL = MMU_MMU0_BUFI | MMU_CPU_BUFII | MMU_BUFII_RESET |
114 MMU_BUFII_WORD | MMU_BUFI_RESET | MMU_BUFI_BYTE;
118 /* Reset internal pointers of the MMU submodule */
119 static void mmu_buff_reset(void)
121 MMU_CTRL |= MMU_BUFII_RESET | MMU_BUFI_RESET;
124 /* My generic device uses PC7 pin, active low */
125 static inline bool card_detect_target(void)
127 return !(GPIO_PCDR & 0x80);
130 /* Send command to the SD card. Command finish is signaled in ISR */
131 static bool send_cmd(const int cmd, const int arg, const int res,
132 unsigned long *response)
134 SD_CMD = arg;
136 if (res > 0)
137 SD_CMDREST = CMD_XFER_START | RES_XFER_START | res | cmd;
138 else
139 SD_CMDREST = CMD_XFER_START | RES_XFER_END | RES_R1 | cmd;
141 semaphore_wait(&command_completion_signal, TIMEOUT_BLOCK);
143 /* Handle command responses & errors */
144 if(res != RES_NO)
146 if(cmd_error & STAT_CMD_RES_ERR)
147 return false;
149 if(res == RES_R2)
151 response[0] = SD_RES3;
152 response[1] = SD_RES2;
153 response[2] = SD_RES1;
154 response[3] = SD_RES0;
156 else
157 response[0] = SD_RES3;
159 return true;
162 #if 0
163 /* for some misterious reason the card does not report itself as being in TRAN
164 * but transfers are successful. Rockchip OF does not check the card state
165 * after SELECT. I checked two different cards.
167 static void print_card_status(void)
169 unsigned long response;
170 send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
171 &response);
173 printf("card status: 0x%0x, state: 0x%0x", response, (response>>9)&0xf);
176 static int sd_wait_for_tran_state(void)
178 unsigned long response;
179 unsigned int timeout = current_tick + 5*HZ;
180 int cmd_retry = 10;
182 while (1)
184 while (!send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
185 &response) && cmd_retry > 0)
187 cmd_retry--;
190 if (cmd_retry <= 0)
192 return -1;
195 if (((response >> 9) & 0xf) == SD_TRAN)
197 return 0;
200 if(TIME_AFTER(current_tick, timeout))
202 return -10 * ((response >> 9) & 0xf);
205 last_disk_activity = current_tick;
208 #endif
210 static bool sd_wait_card_busy(void)
212 unsigned int timeout = current_tick + 5*HZ;
214 while (!(SD_CARD & SD_CARD_BSY))
216 if(TIME_AFTER(current_tick, timeout))
217 return false;
220 return true;
223 static int sd_init_card(void)
225 unsigned long response;
226 long init_timeout;
227 bool sd_v2 = false;
229 card_info.rca = 0;
231 /* assume 50 MHz APB freq / 125 = 400 kHz */
232 SD_CTRL = (SD_CTRL & ~(0x7FF)) | 0x7D;
234 /* 100 - 400kHz clock required for Identification Mode */
235 /* Start of Card Identification Mode ************************************/
237 /* CMD0 Go Idle */
238 if(!send_cmd(SD_GO_IDLE_STATE, 0, RES_NO, NULL))
239 return -1;
241 sleep(1);
243 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
244 Non v2 cards will not respond to this command*/
245 if(send_cmd(SD_SEND_IF_COND, 0x1AA, RES_R6, &response))
246 if((response & 0xFFF) == 0x1AA)
247 sd_v2 = true;
249 /* timeout for initialization is 1sec, from SD Specification 2.00 */
250 init_timeout = current_tick + HZ;
252 do {
253 /* this timeout is the only valid error for this loop*/
254 if(TIME_AFTER(current_tick, init_timeout))
255 return -2;
257 if(!send_cmd(SD_APP_CMD, card_info.rca, RES_R1, &response))
258 return -3;
260 sleep(1); /* bus conflict otherwise */
262 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
263 if(!send_cmd(SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
264 RES_R3, &card_info.ocr))
265 return -4;
266 } while(!(card_info.ocr & (1<<31)) );
268 /* CMD2 send CID */
269 if(!send_cmd(SD_ALL_SEND_CID, 0, RES_R2, card_info.cid))
270 return -5;
272 /* CMD3 send RCA */
273 if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, RES_R6, &card_info.rca))
274 return -6;
276 /* End of Card Identification Mode ************************************/
278 /* Card back to full speed 25MHz*/
279 SD_CTRL = (SD_CTRL & ~0x7FF) | 1; /* FIXME check this divider - OF uses 0 here*/
281 /* CMD9 send CSD */
282 if(!send_cmd(SD_SEND_CSD, card_info.rca, RES_R2, card_info.csd))
283 return -11;
285 sd_parse_csd(&card_info);
287 if(!send_cmd(SD_SELECT_CARD, card_info.rca, RES_R1b, &response))
288 return -20;
290 if (!sd_wait_card_busy())
291 return -21;
293 card_info.initialized = 1;
295 return 0;
298 static void sd_thread(void) NORETURN_ATTR;
299 static void sd_thread(void)
301 struct queue_event ev;
302 bool idle_notified = false;
304 while (1)
306 queue_wait_w_tmo(&sd_queue, &ev, HZ);
308 switch ( ev.id )
310 #ifdef HAVE_HOTSWAP
311 case SYS_HOTSWAP_INSERTED:
312 case SYS_HOTSWAP_EXTRACTED:
314 int microsd_init = 1;
315 fat_lock(); /* lock-out FAT activity first -
316 prevent deadlocking via disk_mount that
317 would cause a reverse-order attempt with
318 another thread */
319 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
320 into driver that bypass the fat cache */
322 /* We now have exclusive control of fat cache and ata */
324 disk_unmount(sd_first_drive); /* release "by force", ensure file
325 descriptors aren't leaked and any busy
326 ones are invalid if mounting */
327 /* Force card init for new card, re-init for re-inserted one or
328 * clear if the last attempt to init failed with an error. */
329 card_info.initialized = 0;
331 if (ev.id == SYS_HOTSWAP_INSERTED)
333 sd_enable(true);
334 microsd_init = sd_init_card(sd_first_drive);
335 if (microsd_init < 0) /* initialisation failed */
336 panicf("microSD init failed : %d", microsd_init);
338 microsd_init = disk_mount(sd_first_drive); /* 0 if fail */
342 * Mount succeeded, or this was an EXTRACTED event,
343 * in both cases notify the system about the changed filesystems
345 if (microsd_init)
346 queue_broadcast(SYS_FS_CHANGED, 0);
348 sd_enable(false);
350 /* Access is now safe */
351 mutex_unlock(&sd_mtx);
352 fat_unlock();
354 break;
355 #endif
356 case SYS_TIMEOUT:
357 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
359 idle_notified = false;
361 else if (!idle_notified)
363 call_storage_idle_notifys(false);
364 idle_notified = true;
366 break;
368 case SYS_USB_CONNECTED:
369 usb_acknowledge(SYS_USB_CONNECTED_ACK);
370 /* Wait until the USB cable is extracted again */
371 usb_wait_for_disconnect(&sd_queue);
373 break;
378 static void init_controller(void)
380 /* reset SD module */
381 SCU_RSTCFG |= (1<<9);
382 sleep(1);
383 SCU_RSTCFG &= ~(1<<9);
385 /* set pins functions as SD signals */
386 SCU_IOMUXA_CON |= IOMUX_SD;
388 /* enable and unmask SD interrupts in interrupt controller */
389 INTC_IMR |= (1<<10);
390 INTC_IECR |= (1<<10);
392 SD_CTRL = SD_PWR_CPU | SD_DETECT_MECH | SD_CLOCK_EN | 0x7D;
393 SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
394 SD_CARD = SD_CARD_SELECT | SD_CARD_PWR_EN;
396 /* setup mmu buffers */
397 MMU_PNRI = 0x1ff;
398 MMU_PNRII = 0x1ff;
399 MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
400 MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;
404 int sd_init(void)
406 int ret;
408 semaphore_init(&transfer_completion_signal, 1, 0);
409 semaphore_init(&command_completion_signal, 1, 0);
411 init_controller();
413 ret = sd_init_card();
414 if(ret < 0)
415 return ret;
417 /* init mutex */
418 mutex_init(&sd_mtx);
420 queue_init(&sd_queue, true);
421 create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
422 sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
424 return 0;
427 int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
428 void* buf)
430 #ifdef HAVE_MULTIDRIVE
431 (void)drive;
432 #endif
433 unsigned long response;
434 unsigned int retry_cnt = 0;
435 int cnt, ret = 0;
436 unsigned char *dst;
438 mutex_lock(&sd_mtx);
439 sd_enable(true);
441 if (count <= 0 || start + count > card_info.numblocks)
442 return -1;
444 if(!(card_info.ocr & (1<<30)))
445 start <<= 9; /* not SDHC */
447 /* setup A2A DMA CH0 */
448 A2A_ISRC0 = (unsigned long)(&MMU_DATA);
449 A2A_ICNT0 = 512;
450 A2A_LCNT0 = 1;
451 A2A_DOMAIN = 0;
453 while (retry_cnt++ < 20)
455 cnt = count;
456 dst = (unsigned char *)buf;
458 ret = 0;
459 retry = false; /* reset retry flag */
460 mmu_buff_reset(); /* reset recive buff state */
462 /* issue read command to the card */
463 if (!send_cmd(SD_READ_MULTIPLE_BLOCK, start, RES_R1, &response))
465 ret = -4;
466 continue;
469 while (cnt > 0)
471 if (cnt == 1)
473 /* last block to tranfer */
474 SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
475 DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
476 DATA_XFER_SINGLE;
478 else
480 /* more than one block to transfer */
481 SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
482 DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
483 DATA_XFER_MULTI;
486 /* wait for transfer completion */
487 semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
489 if (retry)
491 /* data transfer error */
492 ret = -5;
493 break;
496 /* exchange buffers */
497 mmu_switch_buff();
499 last_disk_activity = current_tick;
501 /* transfer data from receive buffer to the dest
502 * for (i=0; i<(512/4); i++)
503 * *dst++ = MMU_DATA;
505 * below is DMA version in software mode.
506 * SD module provides DMAreq signals and all this
507 * can be done in hardware in theory but I can't
508 * figure this out. OF doesn't use DMA at all.
510 A2A_IDST0 = (unsigned long)dst;
511 A2A_CON0 = (3<<9) | (1<<6) | (1<<3) | (2<<1) | (1<<0);
513 /* wait for DMA engine to finish transfer */
514 while (A2A_DMA_STS & 1);
516 dst += 512;
517 cnt--;
518 } /* while (cnt > 0) */
520 if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
521 ret = -6;
523 /* transfer successfull - leave retry loop */
524 if (ret == 0)
525 break;
528 sd_enable(false);
529 mutex_unlock(&sd_mtx);
531 return ret;
534 /* Not tested */
535 int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
536 const void* buf)
538 #ifdef HAVE_MULTIDRIVE
539 (void) drive;
540 #endif
541 #if defined(BOOTLOADER) /* we don't need write support in bootloader */
542 (void) start;
543 (void) count;
544 (void) buf;
545 return -1;
546 #else
547 unsigned long response;
548 unsigned int retry_cnt = 0;
549 int cnt, ret = 0;
550 unsigned char *src;
551 bool card_selected = false;
553 mutex_lock(&sd_mtx);
554 sd_enable(true);
556 if (count <= 0 || start + count > card_info.numblocks)
557 return -1;
559 if(!(card_info.ocr & (1<<30)))
560 start <<= 9; /* not SDHC */
562 /* setup A2A DMA CH0 */
563 A2A_IDST0 = (unsigned long)(&MMU_DATA);
564 A2A_ICNT0 = 512;
565 A2A_LCNT0 = 1;
566 A2A_DOMAIN = 0;
568 while (retry_cnt++ < 20)
570 cnt = count;
571 src = (unsigned char *)buf;
573 ret = 0;
574 retry = false; /* reset retry flag */
575 mmu_buff_reset(); /* reset recive buff state */
577 if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK, start, RES_R1, &response))
579 ret = -3;
580 continue;
583 while (cnt > 0)
585 /* transfer data from receive buffer to the dest
586 * for (i=0; i<(512/4); i++)
587 * MMU_DATA = *src++;
589 * Below is DMA version in software mode.
592 A2A_ISRC0 = (unsigned long)src;
593 A2A_CON0 = (3<<9) | (1<<5) | (1<<3) | (2<<1) | (1<<0);
595 while (A2A_DMA_STS & 1);
597 src += 512;
599 /* exchange buffers */
600 mmu_switch_buff();
602 if (cnt == 1)
604 /* last block to tranfer */
605 SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
606 DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
607 DATA_XFER_SINGLE;
610 else
612 /* more than one block to transfer */
613 SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
614 DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
615 DATA_XFER_MULTI;
619 /* wait for transfer completion */
620 semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
622 if (retry)
624 /* data transfer error */
625 ret = -3;
626 break;
629 cnt--;
630 } /* while (cnt > 0) */
632 if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
633 ret = -4;
635 if (!sd_wait_card_busy())
636 ret = -5;
638 /* transfer successfull - leave retry loop */
639 if (ret == 0)
640 break;
643 sd_enable(false);
644 mutex_unlock(&sd_mtx);
646 return ret;
648 #endif /* defined(BOOTLOADER) */
651 void sd_enable(bool on)
653 /* enable or disable clock signal for SD module */
654 if (on)
656 SCU_CLKCFG &= ~(1<<22);
658 else
660 SCU_CLKCFG |= (1<<22);
664 #ifndef BOOTLOADER
665 long sd_last_disk_activity(void)
667 return last_disk_activity;
670 tCardInfo *card_get_info_target(int card_no)
672 (void)card_no;
673 return &card_info;
675 #endif /* BOOTLOADER */
677 #ifdef HAVE_HOTSWAP
678 /* Not complete and disabled in config */
679 bool sd_removable(IF_MD_NONVOID(int drive))
681 (void)drive;
682 return true;
685 bool sd_present(IF_MD_NONVOID(int drive))
687 (void)drive;
688 return card_detect_target();
691 static int sd_oneshot_callback(struct timeout *tmo)
693 (void)tmo;
695 /* This is called only if the state was stable for 300ms - check state
696 * and post appropriate event. */
697 if (card_detect_target())
699 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
701 else
702 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
704 return 0;
707 /* interrupt handler for SD detect */
709 #endif /* HAVE_HOTSWAP */
711 #ifdef CONFIG_STORAGE_MULTI
712 int sd_num_drives(int first_drive)
714 (void)first_drive;
716 /* we have only one SD drive */
717 return 1;
719 #endif /* CONFIG_STORAGE_MULTI */