as3525v2: Use CD Interrupt vs. CD polling in send_cmd()
[kugel-rb.git] / firmware / target / arm / as3525 / sd-as3525v2.c
blobfe4d60925a01d92eefe00c1e2441d9826fa188c6
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é
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "config.h" /* for HAVE_MULTIVOLUME */
24 #include "fat.h"
25 #include "thread.h"
26 #include "led.h"
27 #include "hotswap.h"
28 #include "system.h"
29 #include "kernel.h"
30 #include "cpu.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "as3525v2.h"
35 #include "pl081.h" /* DMA controller */
36 #include "dma-target.h" /* DMA request lines */
37 #include "clock-target.h"
38 #include "panic.h"
39 #include "stdbool.h"
40 #include "ata_idle_notify.h"
41 #include "sd.h"
42 #include "usb.h"
44 #ifdef HAVE_HOTSWAP
45 #include "disk.h"
46 #endif
48 #include "lcd.h"
49 #include <stdarg.h>
50 #include "sysfont.h"
52 #define INTERNAL_AS3525 0 /* embedded SD card */
53 #define SD_SLOT_AS3525 1 /* SD slot if present */
55 /* command flags */
56 #define MCI_NO_RESP (0<<0)
57 #define MCI_RESP (1<<0)
58 #define MCI_LONG_RESP (1<<1)
60 /* controller registers */
61 #define SD_BASE 0xC6070000
63 #define SD_REG(x) (*(volatile unsigned long *) (SD_BASE+x))
65 #define MCI_CTRL SD_REG(0x00)
67 /* control bits */
68 #define CTRL_RESET (1<<0)
69 #define FIFO_RESET (1<<1)
70 #define DMA_RESET (1<<2)
71 #define INT_ENABLE (1<<4)
72 #define DMA_ENABLE (1<<5)
73 #define READ_WAIT (1<<6)
74 #define SEND_IRQ_RESP (1<<7)
75 #define ABRT_READ_DATA (1<<8)
76 #define SEND_CCSD (1<<9)
77 #define SEND_AS_CCSD (1<<10)
78 #define EN_OD_PULLUP (1<<24)
81 #define MCI_PWREN SD_REG(0x04) /* power enable */
83 #define PWR_CRD_0 (1<<0)
84 #define PWR_CRD_1 (1<<1)
85 #define PWR_CRD_2 (1<<2)
86 #define PWR_CRD_3 (1<<3)
88 #define MCI_CLKDIV SD_REG(0x08) /* clock divider */
89 /* CLK_DIV_0 : bits 7:0
90 * CLK_DIV_1 : bits 15:8
91 * CLK_DIV_2 : bits 23:16
92 * CLK_DIV_3 : bits 31:24
95 #define MCI_CLKSRC SD_REG(0x0C) /* clock source */
96 /* CLK_SRC_CRD0: bits 1:0
97 * CLK_SRC_CRD1: bits 3:2
98 * CLK_SRC_CRD2: bits 5:4
99 * CLK_SRC_CRD3: bits 7:6
102 #define MCI_CLKENA SD_REG(0x10) /* clock enable */
104 #define CCLK_ENA_CRD0 (1<<0)
105 #define CCLK_ENA_CRD1 (1<<1)
106 #define CCLK_ENA_CRD2 (1<<2)
107 #define CCLK_ENA_CRD3 (1<<3)
108 #define CCLK_LP_CRD0 (1<<16) /* LP --> Low Power Mode? */
109 #define CCLK_LP_CRD1 (1<<17)
110 #define CCLK_LP_CRD2 (1<<18)
111 #define CCLK_LP_CRD3 (1<<19)
113 #define MCI_TMOUT SD_REG(0x14) /* timeout */
114 /* response timeout bits 0:7
115 * data timeout bits 8:31
118 #define MCI_CTYPE SD_REG(0x18) /* card type */
119 /* 1 bit per card, set = wide bus */
120 #define WIDTH4_CRD0 (1<<0)
121 #define WIDTH4_CRD1 (1<<1)
122 #define WIDTH4_CRD2 (1<<2)
123 #define WIDTH4_CRD3 (1<<3)
125 #define MCI_BLKSIZ SD_REG(0x1C) /* block size bits 0:15*/
126 #define MCI_BYTCNT SD_REG(0x20) /* byte count bits 0:31*/
127 #define MCI_MASK SD_REG(0x24) /* interrupt mask */
131 #define MCI_ARGUMENT SD_REG(0x28)
132 #define MCI_COMMAND SD_REG(0x2C)
134 /* command bits (bits 5:0 are the command index) */
135 #define CMD_RESP_EXP_BIT (1<<6)
136 #define CMD_RESP_LENGTH_BIT (1<<7)
137 #define CMD_CHECK_CRC_BIT (1<<8)
138 #define CMD_DATA_EXP_BIT (1<<9)
139 #define CMD_RW_BIT (1<<10)
140 #define CMD_TRANSMODE_BIT (1<<11)
141 #define CMD_SENT_AUTO_STOP_BIT (1<<12)
142 #define CMD_WAIT_PRV_DAT_BIT (1<<13)
143 #define CMD_ABRT_CMD_BIT (1<<14)
144 #define CMD_SEND_INIT_BIT (1<<15)
145 #define CMD_CARD_NO(x) ((x)<<16) /* 5 bits wide */
146 #define CMD_SEND_CLK_ONLY (1<<21)
147 #define CMD_READ_CEATA (1<<22)
148 #define CMD_CCS_EXPECTED (1<<23)
149 #define CMD_DONE_BIT (1<<31)
152 #define MCI_RESP0 SD_REG(0x30)
153 #define MCI_RESP1 SD_REG(0x34)
154 #define MCI_RESP2 SD_REG(0x38)
155 #define MCI_RESP3 SD_REG(0x3C)
157 #define MCI_MASK_STATUS SD_REG(0x40) /* masked interrupt status */
158 #define MCI_RAW_STATUS SD_REG(0x44) /* raw interrupt status, also used as
159 * status clear */
161 /* interrupt bits */ /* C D E (Cmd) (Data) (End) */
162 #define MCI_INT_CRDDET (1<<0) /* card detect */
163 #define MCI_INT_RE (1<<1) /* x response error */
164 #define MCI_INT_CD (1<<2) /* x command done */
165 #define MCI_INT_DTO (1<<3) /* x data transfer over */
166 #define MCI_INT_TXDR (1<<4) /* tx fifo data request */
167 #define MCI_INT_RXDR (1<<5) /* rx fifo data request */
168 #define MCI_INT_RCRC (1<<6) /* x response crc error */
169 #define MCI_INT_DCRC (1<<7) /* x data crc error */
170 #define MCI_INT_RTO (1<<8) /* x response timeout */
171 #define MCI_INT_DRTO (1<<9) /* x data read timeout */
172 #define MCI_INT_HTO (1<<10) /* x data starv timeout */
173 #define MCI_INT_FRUN (1<<11) /* x fifo over/underrun */
174 #define MCI_INT_HLE (1<<12) /* x x hw locked while error */
175 #define MCI_INT_SBE (1<<13) /* x start bit error */
176 #define MCI_INT_ACD (1<<14) /* auto command done */
177 #define MCI_INT_EBE (1<<15) /* x end bit error */
178 #define MCI_INT_SDIO (0xf<<16)
181 * STATUS register
182 * & 0xBA80 = MCI_INT_DCRC | MCI_INT_DRTO | MCI_INT_FRUN | \
183 * MCI_INT_HLE | MCI_INT_SBE | MCI_INT_EBE
184 * & 8 = MCI_INT_DTO
185 * & 0x428 = MCI_INT_DTO | MCI_INT_RXDR | MCI_INT_HTO
186 * & 0x418 = MCI_INT_DTO | MCI_INT_TXDR | MCI_INT_HTO
189 #define MCI_CMD_ERROR \
190 (MCI_INT_RE | \
191 MCI_INT_RCRC | \
192 MCI_INT_RTO | \
193 MCI_INT_HLE)
195 #define MCI_DATA_ERROR \
196 ( MCI_INT_DCRC | \
197 MCI_INT_DRTO | \
198 MCI_INT_HTO | \
199 MCI_INT_FRUN | \
200 MCI_INT_HLE | \
201 MCI_INT_SBE | \
202 MCI_INT_EBE)
204 #define MCI_STATUS SD_REG(0x48)
206 #define FIFO_RX_WM (1<<0)
207 #define FIFO_TX_WM (1<<1)
208 #define FIFO_EMPTY (1<<2)
209 #define FIFO_FULL (1<<3)
210 #define CMD_FSM_STATE_B0 (1<<4)
211 #define CMD_FSM_STATE_B1 (1<<5)
212 #define CMD_FSM_STATE_B2 (1<<6)
213 #define CMD_FSM_STATE_B3 (1<<7)
214 #define DATA_3_STAT (1<<8)
215 #define DATA_BUSY (1<<9)
216 #define DATA_STAT_MC_BUSY (1<<10)
217 #define RESP_IDX_B0 (1<<11)
218 #define RESP_IDX_B1 (1<<12)
219 #define RESP_IDX_B2 (1<<13)
220 #define RESP_IDX_B3 (1<<14)
221 #define RESP_IDX_B4 (1<<15)
222 #define RESP_IDX_B5 (1<<16)
223 #define FIFO_CNT_B00 (1<<17)
224 #define FIFO_CNT_B01 (1<<18)
225 #define FIFO_CNT_B02 (1<<19)
226 #define FIFO_CNT_B03 (1<<20)
227 #define FIFO_CNT_B04 (1<<21)
228 #define FIFO_CNT_B05 (1<<22)
229 #define FIFO_CNT_B06 (1<<23)
230 #define FIFO_CNT_B07 (1<<24)
231 #define FIFO_CNT_B08 (1<<25)
232 #define FIFO_CNT_B09 (1<<26)
233 #define FIFO_CNT_B10 (1<<27)
234 #define FIFO_CNT_B11 (1<<28)
235 #define FIFO_CNT_B12 (1<<29)
236 #define DMA_ACK (1<<30)
237 #define START_CMD (1<<31)
239 #define MCI_FIFOTH SD_REG(0x4C) /* FIFO threshold */
240 /* TX watermark : bits 11:0
241 * RX watermark : bits 27:16
242 * DMA MTRANS SIZE : bits 30:28
243 * bits 31, 15:12 : unused
245 #define MCI_FIFOTH_MASK 0x8000f000
247 #define MCI_CDETECT SD_REG(0x50) /* card detect */
249 #define CDETECT_CRD_0 (1<<0)
250 #define CDETECT_CRD_1 (1<<1)
251 #define CDETECT_CRD_2 (1<<2)
252 #define CDETECT_CRD_3 (1<<3)
254 #define MCI_WRTPRT SD_REG(0x54) /* write protect */
255 #define MCI_GPIO SD_REG(0x58)
256 #define MCI_TCBCNT SD_REG(0x5C) /* transferred CIU byte count (card)*/
257 #define MCI_TBBCNT SD_REG(0x60) /* transferred host/DMA to/from bytes (FIFO)*/
258 #define MCI_DEBNCE SD_REG(0x64) /* card detect debounce bits 23:0*/
259 #define MCI_USRID SD_REG(0x68) /* user id */
260 #define MCI_VERID SD_REG(0x6C) /* version id */
262 #define MCI_HCON SD_REG(0x70) /* hardware config */
263 /* bit 0 : card type
264 * bits 5:1 : maximum card index
265 * bit 6 : BUS TYPE
266 * bits 9:7 : DATA WIDTH
267 * bits 15:10 : ADDR WIDTH
268 * bits 17:16 : DMA IF
269 * bits 20:18 : DMA WIDTH
270 * bit 21 : FIFO RAM INSIDE
271 * bit 22 : IMPL HOLD REG
272 * bit 23 : SET CLK FALSE
273 * bits 25:24 : MAX CLK DIV IDX
274 * bit 26 : AREA OPTIM
277 #define MCI_BMOD SD_REG(0x80) /* bus mode */
278 /* bit 0 : SWR
279 * bit 1 : FB
280 * bits 6:2 : DSL
281 * bit 7 : DE
282 * bit 10:8 : PBL
285 #define MCI_PLDMND SD_REG(0x84) /* poll demand */
286 #define MCI_DBADDR SD_REG(0x88) /* descriptor base address */
287 #define MCI_IDSTS SD_REG(0x8C) /* internal DMAC status */
288 /* bit 0 : TI
289 * bit 1 : RI
290 * bit 2 : FBE
291 * bit 3 : unused
292 * bit 4 : DU
293 * bit 5 : CES
294 * bits 7:6 : unused
295 * bits 8 : NIS
296 * bit 9 : AIS
297 * bits 12:10 : EB
298 * bits 16:13 : FSM
301 #define MCI_IDINTEN SD_REG(0x90) /* internal DMAC interrupt enable */
302 /* bit 0 : TI
303 * bit 1 : RI
304 * bit 2 : FBE
305 * bit 3 : unused
306 * bit 4 : DU
307 * bit 5 : CES
308 * bits 7:6 : unused
309 * bits 8 : NI
310 * bit 9 : AI
312 #define MCI_DSCADDR SD_REG(0x94) /* current host descriptor address */
313 #define MCI_BUFADDR SD_REG(0x98) /* current host buffer address */
315 #define MCI_FIFO ((unsigned long *) (SD_BASE+0x100))
317 #define UNALIGNED_NUM_SECTORS 10
318 static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS* SD_BLOCK_SIZE] __attribute__((aligned(32))); /* align on cache line size */
319 static unsigned char *uncached_buffer = UNCACHED_ADDR(&aligned_buffer[0]);
321 static void init_controller(void);
322 static int sd_wait_for_state(const int drive, unsigned int state);
324 static tCardInfo card_info[NUM_DRIVES];
326 /* for compatibility */
327 static long last_disk_activity = -1;
329 #define MIN_YIELD_PERIOD 5 /* ticks */
330 static long next_yield = 0;
332 static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
333 static const char sd_thread_name[] = "ata/sd";
334 static struct mutex sd_mtx SHAREDBSS_ATTR;
335 static struct event_queue sd_queue;
336 #ifndef BOOTLOADER
337 bool sd_enabled = false;
338 #endif
340 static struct wakeup transfer_completion_signal;
341 static struct wakeup command_completion_signal;
342 static volatile bool retry;
343 static volatile int cmd_error;
345 #if defined(HAVE_MULTIDRIVE)
346 int active_card = 0;
347 #define EXT_SD_BITS (1<<2)
348 #endif
350 static inline void mci_delay(void) { udelay(1000); }
352 void INT_NAND(void)
354 MCI_CTRL &= ~INT_ENABLE;
355 /* use raw status here as we need to check some Ints that are masked */
356 const int status = MCI_RAW_STATUS;
358 MCI_RAW_STATUS = status; /* clear status */
360 if(status & MCI_DATA_ERROR)
361 retry = true;
363 if( status & (MCI_INT_DTO|MCI_DATA_ERROR))
364 wakeup_signal(&transfer_completion_signal);
366 cmd_error = status & MCI_CMD_ERROR;
368 if(status & MCI_INT_CD)
369 wakeup_signal(&command_completion_signal);
371 MCI_CTRL |= INT_ENABLE;
374 static inline bool card_detect_target(void)
376 #if defined(HAVE_MULTIDRIVE)
377 #if defined(SANSA_FUZEV2)
378 return GPIOA_PIN(2);
379 #elif defined(SANSA_CLIPPLUS)
380 return !(GPIOA_PIN(2));
381 #else
382 #error "microSD pin not defined for your target"
383 #endif
384 #else
385 return false;
386 #endif
389 static bool send_cmd(const int drive, const int cmd, const int arg, const int flags,
390 unsigned long *response)
392 #if defined(HAVE_MULTIDRIVE)
393 /* Check to see if we need to switch cards */
394 if(sd_present(SD_SLOT_AS3525))
395 if(active_card != drive)
397 GPIOB_PIN(5) = (1-drive) << 5;
398 active_card = drive;
400 #endif
402 #define TRANSFER_CMD (cmd == SD_READ_MULTIPLE_BLOCK || \
403 cmd == SD_WRITE_MULTIPLE_BLOCK)
405 /* RCRC & RTO interrupts should be set together with the CD interrupt but
406 * in practice sometimes incorrectly precede the CD interrupt. If we leave
407 * them masked for now we can check them in the isr by reading raw status when
408 * the CD int is triggered.
410 MCI_MASK |= MCI_INT_CD;
411 MCI_ARGUMENT = arg;
413 /* Construct MCI_COMMAND */
414 MCI_COMMAND =
415 /*b5:0*/ cmd
416 /*b6 */ | ((flags & MCI_RESP) ? CMD_RESP_EXP_BIT: 0)
417 /*b7 */ | ((flags & MCI_LONG_RESP) ? CMD_RESP_LENGTH_BIT: 0)
418 /*b8 | CMD_CHECK_CRC_BIT unused */
419 /*b9 */ | (TRANSFER_CMD ? CMD_DATA_EXP_BIT: 0)
420 /*b10 */ | ((cmd == SD_WRITE_MULTIPLE_BLOCK) ? CMD_RW_BIT: 0)
421 /*b11 | CMD_TRANSMODE_BIT unused */
422 /*b12 | CMD_SENT_AUTO_STOP_BIT unused */
423 /*b13 */ | (TRANSFER_CMD ? CMD_WAIT_PRV_DAT_BIT: 0)
424 /*b14 | CMD_ABRT_CMD_BIT unused */
425 /*b15 | CMD_SEND_INIT_BIT unused */
426 /*b20:16 */ | CMD_CARD_NO(drive)
427 /*b21 | CMD_SEND_CLK_ONLY unused */
428 /*b22 | CMD_READ_CEATA unused */
429 /*b23 | CMD_CCS_EXPECTED unused */
430 /*b31 */ | CMD_DONE_BIT;
432 wakeup_wait(&command_completion_signal, TIMEOUT_BLOCK);
434 MCI_MASK &= ~MCI_INT_CD;
436 /* Handle command responses */
438 /* TODO Check crc values to determine if the response was valid */
439 if(flags & MCI_RESP)
441 if(cmd_error & MCI_INT_RCRC) /* skipping timeout for now */
442 return false;
444 if(flags & MCI_LONG_RESP)
446 response[0] = MCI_RESP3;
447 response[1] = MCI_RESP2;
448 response[2] = MCI_RESP1;
449 response[3] = MCI_RESP0;
451 else
452 response[0] = MCI_RESP0;
454 return true;
457 static int sd_init_card(const int drive)
459 unsigned long response;
460 long init_timeout;
461 bool sd_v2 = false;
463 /* assume 24 MHz clock / 60 = 400 kHz */
464 MCI_CLKDIV = (MCI_CLKDIV & ~(0xFF)) | 0x3C; /* CLK_DIV_0 : bits 7:0 */
466 /* 100 - 400kHz clock required for Identification Mode */
467 /* Start of Card Identification Mode ************************************/
469 /* CMD0 Go Idle */
470 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
471 return -1;
472 mci_delay();
474 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
475 Non v2 cards will not respond to this command*/
476 if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &response))
477 if((response & 0xFFF) == 0x1AA)
478 sd_v2 = true;
480 /* timeout for initialization is 1sec, from SD Specification 2.00 */
481 init_timeout = current_tick + HZ;
483 do {
484 /* this timeout is the only valid error for this loop*/
485 if(TIME_AFTER(current_tick, init_timeout))
486 return -2;
488 /* app_cmd */
489 send_cmd(drive, SD_APP_CMD, 0, MCI_RESP, &response);
491 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
492 if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
493 MCI_RESP, &card_info[drive].ocr))
494 return -3;
495 } while(!(card_info[drive].ocr & (1<<31)) );
497 /* CMD2 send CID */
498 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info[drive].cid))
499 return -4;
501 /* CMD3 send RCA */
502 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
503 return -5;
505 #ifdef HAVE_MULTIDRIVE
506 /* Make sure we have 2 unique rca numbers */
507 if(card_info[INTERNAL_AS3525].rca == card_info[SD_SLOT_AS3525].rca)
508 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
509 return -6;
510 #endif
511 /* End of Card Identification Mode ************************************/
513 /* Card back to full speed */
514 MCI_CLKDIV &= ~(0xFF); /* CLK_DIV_0 : bits 7:0 = 0x00 */
516 /* Attempt to switch cards to HS timings, non HS cards just ignore this */
517 /* CMD7 w/rca: Select card to put it in TRAN state */
518 if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_RESP, &response))
519 return -7;
521 if(sd_wait_for_state(drive, SD_TRAN))
522 return -8;
524 /* CMD6 */
525 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
526 return -9;
528 /* We need to go back to STBY state now so we can read csd */
529 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
530 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_RESP, &response))
531 return -10;
533 /* CMD9 send CSD */
534 if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca,
535 MCI_RESP|MCI_LONG_RESP, card_info[drive].csd))
536 return -11;
538 sd_parse_csd(&card_info[drive]);
541 #ifndef HAVE_MULTIDRIVE
542 /* CMD7 w/rca: Select card to put it in TRAN state */
543 if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
544 return -12;
545 #endif
547 card_info[drive].initialized = 1;
549 return 0;
552 static void sd_thread(void) __attribute__((noreturn));
553 static void sd_thread(void)
555 struct queue_event ev;
556 bool idle_notified = false;
558 while (1)
560 queue_wait_w_tmo(&sd_queue, &ev, HZ);
562 switch ( ev.id )
564 #ifdef HAVE_HOTSWAP
565 case SYS_HOTSWAP_INSERTED:
566 case SYS_HOTSWAP_EXTRACTED:
568 int microsd_init = 1;
569 fat_lock(); /* lock-out FAT activity first -
570 prevent deadlocking via disk_mount that
571 would cause a reverse-order attempt with
572 another thread */
573 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
574 into driver that bypass the fat cache */
576 /* We now have exclusive control of fat cache and ata */
578 disk_unmount(SD_SLOT_AS3525); /* release "by force", ensure file
579 descriptors aren't leaked and any busy
580 ones are invalid if mounting */
581 /* Force card init for new card, re-init for re-inserted one or
582 * clear if the last attempt to init failed with an error. */
583 card_info[SD_SLOT_AS3525].initialized = 0;
585 if (ev.id == SYS_HOTSWAP_INSERTED)
587 sd_enable(true);
588 microsd_init = sd_init_card(SD_SLOT_AS3525);
589 if (microsd_init < 0) /* initialisation failed */
590 panicf("microSD init failed : %d", microsd_init);
592 microsd_init = disk_mount(SD_SLOT_AS3525); /* 0 if fail */
596 * Mount succeeded, or this was an EXTRACTED event,
597 * in both cases notify the system about the changed filesystems
599 if (microsd_init)
600 queue_broadcast(SYS_FS_CHANGED, 0);
601 /* Access is now safe */
602 mutex_unlock(&sd_mtx);
603 fat_unlock();
604 sd_enable(false);
606 break;
607 #endif
608 case SYS_TIMEOUT:
609 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
611 idle_notified = false;
613 else
615 /* never let a timer wrap confuse us */
616 next_yield = current_tick;
618 if (!idle_notified)
620 call_storage_idle_notifys(false);
621 idle_notified = true;
624 break;
626 case SYS_USB_CONNECTED:
627 usb_acknowledge(SYS_USB_CONNECTED_ACK);
628 /* Wait until the USB cable is extracted again */
629 usb_wait_for_disconnect(&sd_queue);
631 break;
632 case SYS_USB_DISCONNECTED:
633 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
634 break;
639 static void init_controller(void)
641 int hcon_numcards = ((MCI_HCON>>1) & 0x1F) + 1;
642 int card_mask = (1 << hcon_numcards) - 1;
644 MCI_PWREN &= ~card_mask; /* power off all cards */
646 MCI_CLKSRC = 0x00; /* All CLK_SRC_CRD set to 0*/
647 MCI_CLKDIV = 0x00; /* CLK_DIV_0 : bits 7:0 */
649 MCI_PWREN |= card_mask; /* power up cards */
650 mci_delay();
652 MCI_CTRL |= CTRL_RESET;
653 while(MCI_CTRL & CTRL_RESET)
656 MCI_RAW_STATUS = 0xffffffff; /* Clear all MCI Interrupts */
658 MCI_TMOUT = 0xffffffff; /* data b31:8, response b7:0 */
660 MCI_CTYPE = 0x0; /* all cards 1 bit bus for now */
662 MCI_CLKENA = card_mask; /* Enables card clocks */
664 MCI_ARGUMENT = 0;
665 MCI_COMMAND = CMD_DONE_BIT|CMD_SEND_CLK_ONLY|CMD_WAIT_PRV_DAT_BIT;
666 while(MCI_COMMAND & CMD_DONE_BIT)
669 MCI_DEBNCE = 0xfffff; /* default value */
671 /* Rx watermark = 63(sd reads) Tx watermark = 128 (sd writes) */
672 MCI_FIFOTH = (MCI_FIFOTH & MCI_FIFOTH_MASK) | 0x503f0080;
674 GPIOB_DIR |= (1<<5); /* Set pin B5 to output */
676 /* Mask all MCI Interrupts initially */
677 MCI_MASK = 0;
679 MCI_CTRL |= INT_ENABLE;
682 int sd_init(void)
684 int ret;
686 CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
688 CGU_IDE = (1<<7) /* AHB interface enable */
689 | (AS3525_IDE_DIV << 2)
690 | 1; /* clock source = PLLA */
692 CGU_MEMSTICK = (1<<7) /* interface enable */
693 | (AS3525_MS_DIV << 2)
694 | 1; /* clock source = PLLA */
696 CGU_SDSLOT = (1<<7) /* interface enable */
697 | (AS3525_SDSLOT_DIV << 2)
698 | 1; /* clock source = PLLA */
700 wakeup_init(&transfer_completion_signal);
701 wakeup_init(&command_completion_signal);
702 #ifdef HAVE_MULTIDRIVE
703 /* clear previous irq */
704 GPIOA_IC = EXT_SD_BITS;
705 /* enable edge detecting */
706 GPIOA_IS &= ~EXT_SD_BITS;
707 /* detect both raising and falling edges */
708 GPIOA_IBE |= EXT_SD_BITS;
709 /* Configure XPD for SD-MCI interface */
710 CCU_IO |= (1<<2);
711 #endif
713 VIC_INT_ENABLE = INTERRUPT_NAND;
715 init_controller();
716 ret = sd_init_card(INTERNAL_AS3525);
717 if(ret < 0)
718 return ret;
720 /* init mutex */
721 mutex_init(&sd_mtx);
723 queue_init(&sd_queue, true);
724 create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
725 sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
727 #ifndef BOOTLOADER
728 sd_enabled = true;
729 sd_enable(false);
730 #endif
731 return 0;
734 static int sd_wait_for_state(const int drive, unsigned int state)
736 unsigned long response;
737 unsigned int timeout = 100; /* ticks */
738 long t = current_tick;
740 while (1)
742 long tick;
744 while(!(send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP, &response)));
746 if (((response >> 9) & 0xf) == state)
747 return 0;
749 if(TIME_AFTER(current_tick, t + timeout))
750 return -10 * ((response >> 9) & 0xf);
752 if (TIME_AFTER((tick = current_tick), next_yield))
754 yield();
755 timeout += current_tick - tick;
756 next_yield = tick + MIN_YIELD_PERIOD;
761 static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
762 int count, void* buf, bool write)
764 int ret = 0;
765 #ifndef HAVE_MULTIDRIVE
766 const int drive = 0;
767 #endif
769 /* skip SanDisk OF */
770 if (drive == INTERNAL_AS3525)
771 start += 0xf000;
773 mutex_lock(&sd_mtx);
774 #ifndef BOOTLOADER
775 sd_enable(true);
776 led(true);
777 #endif
779 if (card_info[drive].initialized <= 0)
781 ret = sd_init_card(drive);
782 if (!(card_info[drive].initialized))
784 panicf("card not initialised (%d)", ret);
785 goto sd_transfer_error;
789 #ifdef HAVE_MULTIDRIVE
790 /* CMD7 w/rca: Select card to put it in TRAN state */
791 if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
792 return -6;
793 #endif
795 last_disk_activity = current_tick;
796 dma_retain();
798 const int cmd = write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
802 void *dma_buf = aligned_buffer;
803 unsigned int transfer = count;
804 if(transfer > UNALIGNED_NUM_SECTORS)
805 transfer = UNALIGNED_NUM_SECTORS;
807 if(write)
808 memcpy(uncached_buffer, buf, transfer * SD_BLOCK_SIZE);
810 /* Interrupt handler might set this to true during transfer */
811 retry = false;
813 MCI_BLKSIZ = SD_BLOCK_SIZE;
814 MCI_BYTCNT = transfer * SD_BLOCK_SIZE;
816 ret = sd_wait_for_state(drive, SD_TRAN);
817 if (ret < 0)
819 static const char *st[9] = {
820 "IDLE", "RDY", "IDENT", "STBY", "TRAN", "DATA", "RCV",
821 "PRG", "DIS"};
822 if(ret <= -10)
823 panicf("wait for TRAN state failed (%s) %d",
824 st[(-ret / 10) % 9], drive);
825 else
826 panicf("wait for state failed");
827 goto sd_transfer_error;
830 MCI_MASK |= (MCI_DATA_ERROR | MCI_INT_DTO);
832 int arg = start;
833 if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
834 arg *= SD_BLOCK_SIZE;
836 if(write)
837 dma_enable_channel(0, dma_buf, MCI_FIFO, DMA_PERI_SD,
838 DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8, NULL);
839 else
840 dma_enable_channel(0, MCI_FIFO, dma_buf, DMA_PERI_SD,
841 DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8, NULL);
843 MCI_CTRL |= DMA_ENABLE;
845 unsigned long dummy; /* if we don't ask for a response, writing fails */
846 if(!send_cmd(drive, cmd, arg, MCI_RESP, &dummy))
847 panicf("%s multiple blocks failed", write ? "write" : "read");
849 wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
851 MCI_MASK &= ~(MCI_DATA_ERROR | MCI_INT_DTO);
853 last_disk_activity = current_tick;
855 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_NO_RESP, NULL))
857 ret = -666;
858 panicf("STOP TRANSMISSION failed");
859 goto sd_transfer_error;
862 if(!retry)
864 if(!write)
865 memcpy(buf, uncached_buffer, transfer * SD_BLOCK_SIZE);
866 buf += transfer * SD_BLOCK_SIZE;
867 start += transfer;
868 count -= transfer;
870 else /* reset controller if we had an error */
872 MCI_CTRL |= (FIFO_RESET|DMA_RESET);
873 while(MCI_CTRL & (FIFO_RESET|DMA_RESET))
877 } while(retry || count);
879 dma_release();
881 #ifdef HAVE_MULTIDRIVE
882 /* CMD lines are separate, not common, so we need to actively deselect */
883 /* CMD7 w/rca =0 : deselects card & puts it in STBY state */
884 if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
885 return -6;
886 #endif
888 #ifndef BOOTLOADER
889 sd_enable(false);
890 led(false);
891 #endif
892 mutex_unlock(&sd_mtx);
893 return 0;
895 sd_transfer_error:
896 panicf("transfer error : %d",ret);
897 card_info[drive].initialized = 0;
898 return ret;
901 int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
902 void* buf)
904 return sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
907 int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
908 const void* buf)
910 #if defined(BOOTLOADER) /* we don't need write support in bootloader */
911 #ifdef HAVE_MULTIDRIVE
912 (void) drive;
913 #endif
914 (void) start;
915 (void) count;
916 (void) buf;
917 return -1;
918 #else
919 return sd_transfer_sectors(IF_MD2(drive,) start, count, (void*)buf, true);
920 #endif /* defined(BOOTLOADER) */
923 #ifndef BOOTLOADER
924 long sd_last_disk_activity(void)
926 return last_disk_activity;
929 void sd_enable(bool on)
931 /* TODO */
932 (void) on;
935 tCardInfo *card_get_info_target(int card_no)
937 return &card_info[card_no];
939 #endif /* BOOTLOADER */
941 #ifdef HAVE_HOTSWAP
942 bool sd_removable(IF_MD_NONVOID(int drive))
944 return (drive==1);
947 bool sd_present(IF_MD_NONVOID(int drive))
949 return (drive == 0) ? true : card_detect_target();
952 static int sd1_oneshot_callback(struct timeout *tmo)
954 (void)tmo;
956 /* This is called only if the state was stable for 300ms - check state
957 * and post appropriate event. */
958 if (card_detect_target())
960 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
962 else
963 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
965 return 0;
968 void sd_gpioa_isr(void)
970 static struct timeout sd1_oneshot;
971 if (GPIOA_MIS & EXT_SD_BITS)
972 timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
973 /* acknowledge interrupt */
974 GPIOA_IC = EXT_SD_BITS;
977 void card_enable_monitoring_target(bool on)
979 if (on) /* enable interrupt */
980 GPIOA_IE |= EXT_SD_BITS;
981 else /* disable interrupt */
982 GPIOA_IE &= ~EXT_SD_BITS;
984 #endif /* HAVE_HOTSWAP */
986 #ifdef CONFIG_STORAGE_MULTI
987 int sd_num_drives(int first_drive)
989 /* We don't care which logical drive number(s) we have been assigned */
990 (void)first_drive;
992 return NUM_DRIVES;
994 #endif /* CONFIG_STORAGE_MULTI */