Rockbox supports not only 1bpp BMPs
[kugel-rb.git] / firmware / drivers / ata_mmc.c
blob5104f8cf97ff96e8212aa9739ca76e42ae6196d2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Jens Arnold
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 <stdbool.h>
22 #include "mmc.h"
23 #include "ata_mmc.h"
24 #include "ata_idle_notify.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include "led.h"
28 #include "sh7034.h"
29 #include "system.h"
30 #include "debug.h"
31 #include "panic.h"
32 #include "usb.h"
33 #include "power.h"
34 #include "string.h"
35 #include "hwcompat.h"
36 #include "adc.h"
37 #include "bitswap.h"
38 #include "disk.h" /* for mount/unmount */
39 #include "storage.h"
41 #define BLOCK_SIZE 512 /* fixed */
43 /* Command definitions */
44 #define CMD_GO_IDLE_STATE 0x40 /* R1 */
45 #define CMD_SEND_OP_COND 0x41 /* R1 */
46 #define CMD_SEND_CSD 0x49 /* R1 */
47 #define CMD_SEND_CID 0x4a /* R1 */
48 #define CMD_STOP_TRANSMISSION 0x4c /* R1 */
49 #define CMD_SEND_STATUS 0x4d /* R2 */
50 #define CMD_SET_BLOCKLEN 0x50 /* R1 */
51 #define CMD_READ_SINGLE_BLOCK 0x51 /* R1 */
52 #define CMD_READ_MULTIPLE_BLOCK 0x52 /* R1 */
53 #define CMD_WRITE_BLOCK 0x58 /* R1b */
54 #define CMD_WRITE_MULTIPLE_BLOCK 0x59 /* R1b */
55 #define CMD_READ_OCR 0x7a /* R3 */
57 /* Response formats:
58 R1 = single byte, msb=0, various error flags
59 R1b = R1 + busy token(s)
60 R2 = 2 bytes (1st byte identical to R1), additional flags
61 R3 = 5 bytes (R1 + OCR register)
64 #define R1_PARAMETER_ERR 0x40
65 #define R1_ADDRESS_ERR 0x20
66 #define R1_ERASE_SEQ_ERR 0x10
67 #define R1_COM_CRC_ERR 0x08
68 #define R1_ILLEGAL_CMD 0x04
69 #define R1_ERASE_RESET 0x02
70 #define R1_IN_IDLE_STATE 0x01
72 #define R2_OUT_OF_RANGE 0x80
73 #define R2_ERASE_PARAM 0x40
74 #define R2_WP_VIOLATION 0x20
75 #define R2_CARD_ECC_FAIL 0x10
76 #define R2_CC_ERROR 0x08
77 #define R2_ERROR 0x04
78 #define R2_ERASE_SKIP 0x02
79 #define R2_CARD_LOCKED 0x01
81 /* Data start tokens */
83 #define DT_START_BLOCK 0xfe
84 #define DT_START_WRITE_MULTIPLE 0xfc
85 #define DT_STOP_TRAN 0xfd
87 /* for compatibility */
88 static long last_disk_activity = -1;
90 /* private variables */
92 static struct mutex mmc_mutex;
94 #ifdef HAVE_HOTSWAP
95 static bool mmc_monitor_enabled = true;
96 static long mmc_stack[((DEFAULT_STACK_SIZE*2) + 0x800)/sizeof(long)];
97 #else
98 static long mmc_stack[(DEFAULT_STACK_SIZE*2)/sizeof(long)];
99 #endif
100 static const char mmc_thread_name[] = "mmc";
101 static struct event_queue mmc_queue;
102 static bool initialized = false;
103 static bool new_mmc_circuit;
105 static enum {
106 MMC_UNKNOWN,
107 MMC_UNTOUCHED,
108 MMC_TOUCHED
109 } mmc_status = MMC_UNKNOWN;
111 static enum {
112 SER_POLL_WRITE,
113 SER_POLL_READ,
114 SER_DISABLED
115 } serial_mode;
117 static const unsigned char dummy[] = {
118 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
121 /* 2 buffers used alternatively for writing, including start token,
122 * dummy CRC and an extra byte to keep word alignment. */
123 static unsigned char write_buffer[2][BLOCK_SIZE+4];
124 static int current_buffer = 0;
125 static const unsigned char *send_block_addr = NULL;
127 static tCardInfo card_info[2];
128 #ifndef HAVE_MULTIVOLUME
129 static int current_card = 0;
130 #endif
131 static bool last_mmc_status = false;
132 static int countdown = HZ/3; /* for mmc switch debouncing */
133 static bool usb_activity; /* monitoring the USB bridge */
134 static long last_usb_activity;
136 /* private function declarations */
138 static int select_card(int card_no);
139 static void deselect_card(void);
140 static void setup_sci1(int bitrate_register);
141 static void set_sci1_poll_read(void);
142 static void write_transfer(const unsigned char *buf, int len)
143 __attribute__ ((section(".icode")));
144 static void read_transfer(unsigned char *buf, int len)
145 __attribute__ ((section(".icode")));
146 static unsigned char poll_byte(long timeout);
147 static unsigned char poll_busy(long timeout);
148 static unsigned char send_cmd(int cmd, unsigned long parameter, void *data);
149 static int receive_cxd(unsigned char *buf);
150 static int initialize_card(int card_no);
151 static int receive_block(unsigned char *inbuf, long timeout);
152 static void send_block_prepare(void);
153 static int send_block_send(unsigned char start_token, long timeout,
154 bool prepare_next);
155 static void mmc_tick(void);
157 /* implementation */
159 void mmc_enable_int_flash_clock(bool on)
161 /* Internal flash clock is enabled by setting PA12 high with the new
162 * clock circuit, and by setting it low with the old clock circuit */
163 if (on ^ new_mmc_circuit)
164 and_b(~0x10, &PADRH); /* clear clock gate PA12 */
165 else
166 or_b(0x10, &PADRH); /* set clock gate PA12 */
169 static int select_card(int card_no)
171 mutex_lock(&mmc_mutex);
172 led(true);
173 last_disk_activity = current_tick;
175 mmc_enable_int_flash_clock(card_no == 0);
177 if (!card_info[card_no].initialized)
179 setup_sci1(7); /* Initial rate: 375 kbps (need <= 400 per mmc specs) */
180 write_transfer(dummy, 10); /* allow the card to synchronize */
181 while (!(SSR1 & SCI_TEND));
184 if (card_no == 0) /* internal */
185 and_b(~0x04, &PADRH); /* assert CS */
186 else /* external */
187 and_b(~0x02, &PADRH); /* assert CS */
189 if (card_info[card_no].initialized)
191 setup_sci1(card_info[card_no].bitrate_register);
192 return 0;
194 else
196 return initialize_card(card_no);
200 static void deselect_card(void)
202 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
203 or_b(0x06, &PADRH); /* deassert CS (both cards) */
205 led(false);
206 mutex_unlock(&mmc_mutex);
207 last_disk_activity = current_tick;
210 static void setup_sci1(int bitrate_register)
212 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
214 SCR1 = 0; /* disable serial port */
215 SMR1 = SYNC_MODE; /* no prescale */
216 BRR1 = bitrate_register;
217 SSR1 = 0;
219 SCR1 = SCI_TE; /* enable transmitter */
220 serial_mode = SER_POLL_WRITE;
223 static void set_sci1_poll_read(void)
225 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
226 SCR1 = 0; /* disable transmitter (& receiver) */
227 SCR1 = (SCI_TE|SCI_RE); /* re-enable transmitter & receiver */
228 while (!(SSR1 & SCI_TEND)); /* wait for SCI init completion (!) */
229 serial_mode = SER_POLL_READ;
230 TDR1 = 0xFF; /* send do-nothing while reading */
233 static void write_transfer(const unsigned char *buf, int len)
235 const unsigned char *buf_end = buf + len;
236 register unsigned char data;
238 if (serial_mode != SER_POLL_WRITE)
240 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
241 SCR1 = 0; /* disable transmitter & receiver */
242 SSR1 = 0; /* clear all flags */
243 SCR1 = SCI_TE; /* enable transmitter only */
244 serial_mode = SER_POLL_WRITE;
247 while (buf < buf_end)
249 data = fliptable[(signed char)(*buf++)]; /* bitswap */
250 while (!(SSR1 & SCI_TDRE)); /* wait for end of transfer */
251 TDR1 = data; /* write byte */
252 SSR1 = 0; /* start transmitting */
256 /* don't call this with len == 0 */
257 static void read_transfer(unsigned char *buf, int len)
259 unsigned char *buf_end = buf + len - 1;
260 register signed char data;
262 if (serial_mode != SER_POLL_READ)
263 set_sci1_poll_read();
265 SSR1 = 0; /* start receiving first byte */
266 while (buf < buf_end)
268 while (!(SSR1 & SCI_RDRF)); /* wait for data */
269 data = RDR1; /* read byte */
270 SSR1 = 0; /* start receiving */
271 *buf++ = fliptable[data]; /* bitswap */
273 while (!(SSR1 & SCI_RDRF)); /* wait for last byte */
274 *buf = fliptable[(signed char)(RDR1)]; /* read & bitswap */
277 /* returns 0xFF on timeout, timeout is in bytes */
278 static unsigned char poll_byte(long timeout)
280 long i;
281 unsigned char data = 0; /* stop the compiler complaining */
283 if (serial_mode != SER_POLL_READ)
284 set_sci1_poll_read();
286 i = 0;
287 do {
288 SSR1 = 0; /* start receiving */
289 while (!(SSR1 & SCI_RDRF)); /* wait for data */
290 data = RDR1; /* read byte */
291 } while ((data == 0xFF) && (++i < timeout));
293 return fliptable[(signed char)data];
296 /* returns 0 on timeout, timeout is in bytes */
297 static unsigned char poll_busy(long timeout)
299 long i;
300 unsigned char data, dummy;
302 if (serial_mode != SER_POLL_READ)
303 set_sci1_poll_read();
305 /* get data response */
306 SSR1 = 0; /* start receiving */
307 while (!(SSR1 & SCI_RDRF)); /* wait for data */
308 data = fliptable[(signed char)(RDR1)]; /* read byte */
310 /* wait until the card is ready again */
311 i = 0;
312 do {
313 SSR1 = 0; /* start receiving */
314 while (!(SSR1 & SCI_RDRF)); /* wait for data */
315 dummy = RDR1; /* read byte */
316 } while ((dummy != 0xFF) && (++i < timeout));
318 return (dummy == 0xFF) ? data : 0;
321 /* Send MMC command and get response. Returns R1 byte directly.
322 * Returns further R2 or R3 bytes in *data (can be NULL for other commands) */
323 static unsigned char send_cmd(int cmd, unsigned long parameter, void *data)
325 static struct {
326 unsigned char cmd;
327 unsigned long parameter;
328 const unsigned char crc7; /* fixed, valid for CMD0 only */
329 const unsigned char trailer;
330 } __attribute__((packed)) command = {0x40, 0, 0x95, 0xFF};
332 unsigned char ret;
334 command.cmd = cmd;
335 command.parameter = htobe32(parameter);
337 write_transfer((unsigned char *)&command, sizeof(command));
339 ret = poll_byte(20);
341 switch (cmd)
343 case CMD_SEND_CSD: /* R1 response, leave open */
344 case CMD_SEND_CID:
345 case CMD_READ_SINGLE_BLOCK:
346 case CMD_READ_MULTIPLE_BLOCK:
347 return ret;
349 case CMD_SEND_STATUS: /* R2 response, close with dummy */
350 read_transfer(data, 1);
351 break;
353 case CMD_READ_OCR: /* R3 response, close with dummy */
354 read_transfer(data, 4);
355 break;
357 default: /* R1 response, close with dummy */
358 break; /* also catches block writes */
360 write_transfer(dummy, 1);
361 return ret;
364 /* Receive CID/ CSD data (16 bytes) */
365 static int receive_cxd(unsigned char *buf)
367 if (poll_byte(20) != DT_START_BLOCK)
369 write_transfer(dummy, 1);
370 return -1; /* not start of data */
373 read_transfer(buf, 16);
374 write_transfer(dummy, 3); /* 2 bytes dontcare crc + 1 byte trailer */
375 return 0;
379 static int initialize_card(int card_no)
381 int rc, i;
382 int blk_exp, ts_exp, taac_exp;
383 tCardInfo *card = &card_info[card_no];
385 static const char mantissa[] = { /* *10 */
386 0, 10, 12, 13, 15, 20, 25, 30,
387 35, 40, 45, 50, 55, 60, 70, 80
389 static const int exponent[] = { /* use varies */
390 1, 10, 100, 1000, 10000, 100000, 1000000,
391 10000000, 100000000, 1000000000
394 if (card_no == 1)
395 mmc_status = MMC_TOUCHED;
397 /* switch to SPI mode */
398 if (send_cmd(CMD_GO_IDLE_STATE, 0, NULL) != 0x01)
399 return -1; /* error or no response */
401 /* initialize card */
402 for (i = HZ;;) /* try for 1 second*/
404 sleep(1);
405 if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0)
406 break;
407 if (--i <= 0)
408 return -2; /* timeout */
411 /* get OCR register */
412 if (send_cmd(CMD_READ_OCR, 0, &card->ocr))
413 return -3;
414 card->ocr = betoh32(card->ocr); /* no-op on big endian */
416 /* check voltage */
417 if (!(card->ocr & 0x00100000)) /* 3.2 .. 3.3 V */
418 return -4;
420 /* get CSD register */
421 if (send_cmd(CMD_SEND_CSD, 0, NULL))
422 return -5;
423 rc = receive_cxd((unsigned char*)card->csd);
424 if (rc)
425 return rc * 10 - 5;
427 blk_exp = card_extract_bits(card->csd, 44, 4);
428 if (blk_exp < 9) /* block size < 512 bytes not supported */
429 return -6;
431 card->numblocks = (card_extract_bits(card->csd, 54, 12) + 1)
432 << (card_extract_bits(card->csd, 78, 3) + 2 + blk_exp - 9);
433 card->blocksize = BLOCK_SIZE;
435 /* max transmission speed, clock divider */
436 ts_exp = card_extract_bits(card->csd, 29, 3);
437 ts_exp = (ts_exp > 3) ? 3 : ts_exp;
438 card->speed = mantissa[card_extract_bits(card->csd, 25, 4)]
439 * exponent[ts_exp + 4];
440 card->bitrate_register = (FREQ/4-1) / card->speed;
442 /* NSAC, TSAC, read timeout */
443 card->nsac = 100 * card_extract_bits(card->csd, 16, 8);
444 card->tsac = mantissa[card_extract_bits(card->csd, 9, 4)];
445 taac_exp = card_extract_bits(card->csd, 13, 3);
446 card->read_timeout = ((FREQ/4) / (card->bitrate_register + 1)
447 * card->tsac / exponent[9 - taac_exp]
448 + (10 * card->nsac));
449 card->read_timeout /= 8; /* clocks -> bytes */
450 card->tsac = card->tsac * exponent[taac_exp] / 10;
452 /* r2w_factor, write timeout */
453 card->r2w_factor = 1 << card_extract_bits(card->csd, 99, 3);
454 card->write_timeout = card->read_timeout * card->r2w_factor;
456 if (card->r2w_factor > 32) /* Such cards often need extra read delay */
457 card->read_timeout *= 4;
459 /* switch to full speed */
460 setup_sci1(card->bitrate_register);
462 /* always use 512 byte blocks */
463 if (send_cmd(CMD_SET_BLOCKLEN, BLOCK_SIZE, NULL))
464 return -7;
466 /* get CID register */
467 if (send_cmd(CMD_SEND_CID, 0, NULL))
468 return -8;
469 rc = receive_cxd((unsigned char*)card->cid);
470 if (rc)
471 return rc * 10 - 8;
473 card->initialized = true;
474 return 0;
477 tCardInfo *mmc_card_info(int card_no)
479 tCardInfo *card = &card_info[card_no];
481 if (!card->initialized && ((card_no == 0) || mmc_detect()))
483 select_card(card_no);
484 deselect_card();
486 return card;
489 /* Receive one block with DMA and bitswap it (chasing bitswap). */
490 static int receive_block(unsigned char *inbuf, long timeout)
492 unsigned long buf_end;
494 if (poll_byte(timeout) != DT_START_BLOCK)
496 write_transfer(dummy, 1);
497 return -1; /* not start of data */
500 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
502 SCR1 = 0; /* disable serial */
503 SSR1 = 0; /* clear all flags */
505 /* setup DMA channel 0 */
506 CHCR0 = 0; /* disable */
507 SAR0 = RDR1_ADDR;
508 DAR0 = (unsigned long) inbuf;
509 DTCR0 = BLOCK_SIZE;
510 CHCR0 = 0x4601; /* fixed source address, RXI1, enable */
511 DMAOR = 0x0001;
512 SCR1 = (SCI_RE|SCI_RIE); /* kick off DMA */
514 /* DMA receives 2 bytes more than DTCR2, but the last 2 bytes are not
515 * stored. The first extra byte is available from RDR1 after the DMA ends,
516 * the second one is lost because of the SCI overrun. However, this
517 * behaviour conveniently discards the crc. */
519 yield(); /* be nice */
521 /* Bitswap received data, chasing the DMA pointer */
522 buf_end = (unsigned long)inbuf + BLOCK_SIZE;
525 /* Call bitswap whenever (a multiple of) 8 bytes are
526 * available (value optimised by experimentation). */
527 int swap_now = (DAR0 - (unsigned long)inbuf) & ~0x00000007;
528 if (swap_now)
530 bitswap(inbuf, swap_now);
531 inbuf += swap_now;
534 while ((unsigned long)inbuf < buf_end);
536 while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
537 while (!(SSR1 & SCI_ORER)); /* wait for the trailing bytes */
538 SCR1 = 0;
539 serial_mode = SER_DISABLED;
541 write_transfer(dummy, 1); /* send trailer */
542 last_disk_activity = current_tick;
543 return 0;
546 /* Prepare a block for sending by copying it to the next write buffer
547 * and bitswapping it. */
548 static void send_block_prepare(void)
550 unsigned char *dest;
552 current_buffer ^= 1; /* toggle buffer */
553 dest = write_buffer[current_buffer] + 2;
555 memcpy(dest, send_block_addr, BLOCK_SIZE);
556 bitswap(dest, BLOCK_SIZE);
558 send_block_addr += BLOCK_SIZE;
561 /* Send one block with DMA from the current write buffer, possibly preparing
562 * the next block within the next write buffer in the background. */
563 static int send_block_send(unsigned char start_token, long timeout,
564 bool prepare_next)
566 int rc = 0;
567 unsigned char *curbuf = write_buffer[current_buffer];
569 curbuf[1] = fliptable[(signed char)start_token];
570 *(unsigned short *)(curbuf + BLOCK_SIZE + 2) = 0xFFFF;
572 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
574 SCR1 = 0; /* disable serial */
575 SSR1 = 0; /* clear all flags */
577 /* setup DMA channel 0 */
578 CHCR0 = 0; /* disable */
579 SAR0 = (unsigned long)(curbuf + 1);
580 DAR0 = TDR1_ADDR;
581 DTCR0 = BLOCK_SIZE + 3; /* start token + block + dummy crc */
582 CHCR0 = 0x1701; /* fixed dest. address, TXI1, enable */
583 DMAOR = 0x0001;
584 SCR1 = (SCI_TE|SCI_TIE); /* kick off DMA */
586 if (prepare_next)
587 send_block_prepare();
588 yield(); /* be nice */
590 while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
591 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
592 SCR1 = 0;
593 serial_mode = SER_DISABLED;
595 if ((poll_busy(timeout) & 0x1F) != 0x05) /* something went wrong */
596 rc = -1;
598 write_transfer(dummy, 1);
599 last_disk_activity = current_tick;
601 return rc;
604 int mmc_read_sectors(IF_MV2(int drive,)
605 unsigned long start,
606 int incount,
607 void* inbuf)
609 int rc = 0;
610 int lastblock = 0;
611 unsigned long end_block;
612 tCardInfo *card;
613 #ifndef HAVE_MULTIVOLUME
614 int drive = current_card;
615 #endif
617 card = &card_info[drive];
618 rc = select_card(drive);
619 if (rc)
621 rc = rc * 10 - 1;
622 goto error;
625 end_block = start + incount;
626 if (end_block > card->numblocks)
628 rc = -2;
629 goto error;
632 /* Some cards don't like reading the very last block with
633 * CMD_READ_MULTIPLE_BLOCK, so make sure this block is always
634 * read with CMD_READ_SINGLE_BLOCK. */
635 if (end_block == card->numblocks)
636 lastblock = 1;
638 if (incount > 1)
640 /* MMC4.2: make multiplication conditional */
641 if (send_cmd(CMD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE, NULL))
643 rc = -3;
644 goto error;
646 while (--incount >= lastblock)
648 rc = receive_block(inbuf, card->read_timeout);
649 if (rc)
651 /* If an error occurs during multiple block reading, the
652 * host still needs to send CMD_STOP_TRANSMISSION */
653 send_cmd(CMD_STOP_TRANSMISSION, 0, NULL);
654 rc = rc * 10 - 4;
655 goto error;
657 inbuf += BLOCK_SIZE;
658 start++;
659 /* ^^ necessary for the abovementioned last block special case */
661 if (send_cmd(CMD_STOP_TRANSMISSION, 0, NULL))
663 rc = -5;
664 goto error;
667 if (incount > 0)
669 /* MMC4.2: make multiplication conditional */
670 if (send_cmd(CMD_READ_SINGLE_BLOCK, start * BLOCK_SIZE, NULL))
672 rc = -6;
673 goto error;
675 rc = receive_block(inbuf, card->read_timeout);
676 if (rc)
678 rc = rc * 10 - 7;
679 goto error;
683 error:
685 deselect_card();
687 return rc;
690 int mmc_write_sectors(IF_MV2(int drive,)
691 unsigned long start,
692 int count,
693 const void* buf)
695 int rc = 0;
696 int write_cmd;
697 unsigned char start_token;
698 tCardInfo *card;
699 #ifndef HAVE_MULTIVOLUME
700 int drive = current_card;
701 #endif
703 card = &card_info[drive];
704 rc = select_card(drive);
705 if (rc)
707 rc = rc * 10 - 1;
708 goto error;
711 if (start + count > card->numblocks)
712 panicf("Writing past end of card");
714 send_block_addr = buf;
715 send_block_prepare();
717 if (count > 1)
719 write_cmd = CMD_WRITE_MULTIPLE_BLOCK;
720 start_token = DT_START_WRITE_MULTIPLE;
722 else
724 write_cmd = CMD_WRITE_BLOCK;
725 start_token = DT_START_BLOCK;
727 /* MMC4.2: make multiplication conditional */
728 if (send_cmd(write_cmd, start * BLOCK_SIZE, NULL))
730 rc = -2;
731 goto error;
733 while (--count >= 0)
735 rc = send_block_send(start_token, card->write_timeout, count > 0);
736 if (rc)
738 rc = rc * 10 - 3;
739 break;
740 /* If an error occurs during multiple block writing,
741 * the STOP_TRAN token still needs to be sent. */
744 if (write_cmd == CMD_WRITE_MULTIPLE_BLOCK)
746 static const unsigned char stop_tran = DT_STOP_TRAN;
747 write_transfer(&stop_tran, 1);
748 poll_busy(card->write_timeout);
751 error:
753 deselect_card();
755 return rc;
758 bool mmc_disk_is_active(void)
760 /* this is correct unless early return from write gets implemented */
761 return mmc_mutex.locked;
764 static void mmc_thread(void)
766 struct queue_event ev;
767 bool idle_notified = false;
769 while (1) {
770 queue_wait_w_tmo(&mmc_queue, &ev, HZ);
771 switch ( ev.id )
773 case SYS_USB_CONNECTED:
774 usb_acknowledge(SYS_USB_CONNECTED_ACK);
775 /* Wait until the USB cable is extracted again */
776 usb_wait_for_disconnect(&mmc_queue);
777 break;
779 #ifdef HAVE_HOTSWAP
780 case SYS_HOTSWAP_INSERTED:
781 disk_mount(1); /* mount MMC */
782 queue_broadcast(SYS_FS_CHANGED, 0);
783 break;
785 case SYS_HOTSWAP_EXTRACTED:
786 disk_unmount(1); /* release "by force" */
787 queue_broadcast(SYS_FS_CHANGED, 0);
788 break;
789 #endif
791 default:
792 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
794 idle_notified = false;
796 else
798 if (!idle_notified)
800 call_storage_idle_notifys(false);
801 idle_notified = true;
804 break;
809 #ifdef HAVE_HOTSWAP
810 void mmc_enable_monitoring(bool on)
812 mmc_monitor_enabled = on;
814 #endif
816 bool mmc_detect(void)
818 return adc_read(ADC_MMC_SWITCH) < 0x200 ? true : false;
821 bool mmc_touched(void)
823 if (mmc_status == MMC_UNKNOWN) /* try to detect */
825 mutex_lock(&mmc_mutex);
826 setup_sci1(7); /* safe value */
827 and_b(~0x02, &PADRH); /* assert CS */
828 if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0xFF)
829 mmc_status = MMC_UNTOUCHED;
830 else
831 mmc_status = MMC_TOUCHED;
833 deselect_card();
835 return mmc_status == MMC_TOUCHED;
838 bool mmc_usb_active(int delayticks)
840 /* reading "inactive" is delayed by user-supplied monoflop value */
841 return (usb_activity ||
842 TIME_BEFORE(current_tick, last_usb_activity + delayticks));
845 static void mmc_tick(void)
847 bool current_status;
848 #ifndef HAVE_HOTSWAP
849 const bool mmc_monitor_enabled = true;
850 #endif
852 if (new_mmc_circuit)
853 /* USB bridge activity is 0 on idle, ~527 on active */
854 current_status = adc_read(ADC_USB_ACTIVE) > 0x100;
855 else
856 current_status = adc_read(ADC_USB_ACTIVE) < 0x190;
858 if (!current_status && usb_activity)
859 last_usb_activity = current_tick;
860 usb_activity = current_status;
862 if (mmc_monitor_enabled)
864 current_status = mmc_detect();
865 /* Only report when the status has changed */
866 if (current_status != last_mmc_status)
868 last_mmc_status = current_status;
869 countdown = HZ/3;
871 else
873 /* Count down until it gets negative */
874 if (countdown >= 0)
875 countdown--;
877 if (countdown == 0)
879 if (current_status)
881 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
883 else
885 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
886 mmc_status = MMC_UNTOUCHED;
887 card_info[1].initialized = false;
894 void mmc_enable(bool on)
896 PBCR1 &= ~0x0CF0; /* PB13, PB11 and PB10 become GPIO,
897 * if not modified below */
898 if (on)
899 PBCR1 |= 0x08A0; /* as SCK1, TxD1, RxD1 */
901 and_b(~0x80, &PADRL); /* assert flash reset */
902 sleep(HZ/100);
903 or_b(0x80, &PADRL); /* de-assert flash reset */
904 sleep(HZ/100);
905 card_info[0].initialized = false;
906 card_info[1].initialized = false;
909 int mmc_init(void)
911 int rc = 0;
913 if (!initialized)
915 mutex_init(&mmc_mutex);
916 queue_init(&mmc_queue, true);
918 mutex_lock(&mmc_mutex);
919 led(false);
921 last_mmc_status = mmc_detect();
922 #ifndef HAVE_MULTIVOLUME
923 /* Use MMC if inserted, internal flash otherwise */
924 current_card = last_mmc_status ? 1 : 0;
925 #endif
927 if (!initialized)
929 if (!last_mmc_status)
930 mmc_status = MMC_UNTOUCHED;
932 /* Port setup */
933 PACR1 &= ~0x0F3C; /* GPIO function for PA13 (flash busy), PA12
934 * (clk gate), PA10 (flash CS), PA9 (MMC CS) */
935 PACR2 &= ~0x4000; /* GPIO for PA7 (flash reset) */
936 PADR |= 0x0680; /* set all the selects + reset high (=inactive) */
937 PAIOR |= 0x1680; /* make outputs for them and the PA12 clock gate */
939 PBCR1 &= ~0x0CF0; /* GPIO function for PB13, PB11 and PB10 */
940 PBDR |= 0x2C00; /* SCK1, TxD1 and RxD1 high in GPIO */
941 PBIOR |= 0x2000; /* SCK1 output */
942 PBIOR &= ~0x0C00; /* TxD1, RxD1 input */
944 IPRE &= 0x0FFF; /* disable SCI1 interrupts for the CPU */
946 new_mmc_circuit = ((HW_MASK & MMC_CLOCK_POLARITY) != 0);
948 create_thread(mmc_thread, mmc_stack,
949 sizeof(mmc_stack), 0, mmc_thread_name
950 IF_PRIO(, PRIORITY_SYSTEM)
951 IF_COP(, CPU));
952 tick_add_task(mmc_tick);
953 initialized = true;
955 mmc_enable(true);
957 mutex_unlock(&mmc_mutex);
958 return rc;
961 long mmc_last_disk_activity(void)
963 return last_disk_activity;
966 #ifdef STORAGE_GET_INFO
967 void mmc_get_info(IF_MV2(int drive,) struct storage_info *info)
969 #ifndef HAVE_MULTIVOLUME
970 const int drive=0;
971 #endif
972 info->sector_size=card_info[drive].blocksize;
973 info->num_sectors=card_info[drive].numblocks;
974 info->vendor="Rockbox";
975 if(drive==0)
977 info->product="Internal Storage";
979 else
981 info->product="MMC Card Slot";
983 info->revision="0.00";
985 #endif
987 #ifdef HAVE_HOTSWAP
988 bool mmc_removable(IF_MV_NONVOID(int drive))
990 #ifndef HAVE_MULTIVOLUME
991 const int drive=0;
992 #endif
993 return (drive==1);
996 bool mmc_present(IF_MV_NONVOID(int drive))
998 #ifndef HAVE_MULTIVOLUME
999 const int drive=0;
1000 #endif
1001 return (card_info[drive].initialized && card_info[drive].numblocks > 0);
1003 #endif
1006 void mmc_sleep(void)
1010 void mmc_spin(void)
1014 void mmc_spindown(int seconds)
1016 (void)seconds;