rolo: make PP code pp6100-compatible
[kugel-rb.git] / firmware / drivers / ata_mmc.c
blob16d06584c50bbd559e0132f4dcd0a8de67f86df6
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 "hotswap.h"
25 #include "ata_idle_notify.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "led.h"
29 #include "sh7034.h"
30 #include "system.h"
31 #include "debug.h"
32 #include "panic.h"
33 #include "usb.h"
34 #include "power.h"
35 #include "string.h"
36 #include "hwcompat.h"
37 #include "adc.h"
38 #include "bitswap.h"
39 #include "disk.h" /* for mount/unmount */
40 #include "storage.h"
42 #define BLOCK_SIZE 512 /* fixed */
44 /* Command definitions */
45 #define CMD_GO_IDLE_STATE 0x40 /* R1 */
46 #define CMD_SEND_OP_COND 0x41 /* R1 */
47 #define CMD_SEND_CSD 0x49 /* R1 */
48 #define CMD_SEND_CID 0x4a /* R1 */
49 #define CMD_STOP_TRANSMISSION 0x4c /* R1 */
50 #define CMD_SEND_STATUS 0x4d /* R2 */
51 #define CMD_SET_BLOCKLEN 0x50 /* R1 */
52 #define CMD_READ_SINGLE_BLOCK 0x51 /* R1 */
53 #define CMD_READ_MULTIPLE_BLOCK 0x52 /* R1 */
54 #define CMD_WRITE_BLOCK 0x58 /* R1b */
55 #define CMD_WRITE_MULTIPLE_BLOCK 0x59 /* R1b */
56 #define CMD_READ_OCR 0x7a /* R3 */
58 /* Response formats:
59 R1 = single byte, msb=0, various error flags
60 R1b = R1 + busy token(s)
61 R2 = 2 bytes (1st byte identical to R1), additional flags
62 R3 = 5 bytes (R1 + OCR register)
65 #define R1_PARAMETER_ERR 0x40
66 #define R1_ADDRESS_ERR 0x20
67 #define R1_ERASE_SEQ_ERR 0x10
68 #define R1_COM_CRC_ERR 0x08
69 #define R1_ILLEGAL_CMD 0x04
70 #define R1_ERASE_RESET 0x02
71 #define R1_IN_IDLE_STATE 0x01
73 #define R2_OUT_OF_RANGE 0x80
74 #define R2_ERASE_PARAM 0x40
75 #define R2_WP_VIOLATION 0x20
76 #define R2_CARD_ECC_FAIL 0x10
77 #define R2_CC_ERROR 0x08
78 #define R2_ERROR 0x04
79 #define R2_ERASE_SKIP 0x02
80 #define R2_CARD_LOCKED 0x01
82 /* Data start tokens */
84 #define DT_START_BLOCK 0xfe
85 #define DT_START_WRITE_MULTIPLE 0xfc
86 #define DT_STOP_TRAN 0xfd
88 /* for compatibility */
89 static long last_disk_activity = -1;
91 /* private variables */
93 static struct mutex mmc_mutex;
95 #ifdef HAVE_HOTSWAP
96 static bool mmc_monitor_enabled = true;
97 static long mmc_stack[((DEFAULT_STACK_SIZE*2) + 0x800)/sizeof(long)];
98 #else
99 static long mmc_stack[(DEFAULT_STACK_SIZE*2)/sizeof(long)];
100 #endif
101 static const char mmc_thread_name[] = "mmc";
102 static struct event_queue mmc_queue;
103 static bool initialized = false;
104 static bool new_mmc_circuit;
106 static enum {
107 MMC_UNKNOWN,
108 MMC_UNTOUCHED,
109 MMC_TOUCHED
110 } mmc_status = MMC_UNKNOWN;
112 static enum {
113 SER_POLL_WRITE,
114 SER_POLL_READ,
115 SER_DISABLED
116 } serial_mode;
118 static const unsigned char dummy[] = {
119 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
122 /* 2 buffers used alternatively for writing, including start token,
123 * dummy CRC and an extra byte to keep word alignment. */
124 static unsigned char write_buffer[2][BLOCK_SIZE+4];
125 static int current_buffer = 0;
126 static const unsigned char *send_block_addr = NULL;
128 static tCardInfo card_info[2];
129 #ifndef HAVE_MULTIDRIVE
130 static int current_card = 0;
131 #endif
132 static bool last_mmc_status = false;
133 static int countdown = HZ/3; /* for mmc switch debouncing */
134 static bool usb_activity; /* monitoring the USB bridge */
135 static long last_usb_activity;
137 /* private function declarations */
139 static int select_card(int card_no);
140 static void deselect_card(void);
141 static void setup_sci1(int bitrate_register);
142 static void set_sci1_poll_read(void);
143 static void write_transfer(const unsigned char *buf, int len)
144 __attribute__ ((section(".icode")));
145 static void read_transfer(unsigned char *buf, int len)
146 __attribute__ ((section(".icode")));
147 static unsigned char poll_byte(long timeout);
148 static unsigned char poll_busy(long timeout);
149 static unsigned char send_cmd(int cmd, unsigned long parameter, void *data);
150 static int receive_cxd(unsigned char *buf);
151 static int initialize_card(int card_no);
152 static int receive_block(unsigned char *inbuf, long timeout);
153 static void send_block_prepare(void);
154 static int send_block_send(unsigned char start_token, long timeout,
155 bool prepare_next);
156 static void mmc_tick(void);
158 /* implementation */
160 void mmc_enable_int_flash_clock(bool on)
162 /* Internal flash clock is enabled by setting PA12 high with the new
163 * clock circuit, and by setting it low with the old clock circuit */
164 if (on ^ new_mmc_circuit)
165 and_b(~0x10, &PADRH); /* clear clock gate PA12 */
166 else
167 or_b(0x10, &PADRH); /* set clock gate PA12 */
170 static int select_card(int card_no)
172 mutex_lock(&mmc_mutex);
173 led(true);
174 last_disk_activity = current_tick;
176 mmc_enable_int_flash_clock(card_no == 0);
178 if (!card_info[card_no].initialized)
180 setup_sci1(7); /* Initial rate: 375 kbps (need <= 400 per mmc specs) */
181 write_transfer(dummy, 10); /* allow the card to synchronize */
182 while (!(SSR1 & SCI_TEND));
185 if (card_no == 0) /* internal */
186 and_b(~0x04, &PADRH); /* assert CS */
187 else /* external */
188 and_b(~0x02, &PADRH); /* assert CS */
190 if (card_info[card_no].initialized)
192 setup_sci1(card_info[card_no].bitrate_register);
193 return 0;
195 else
197 return initialize_card(card_no);
201 static void deselect_card(void)
203 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
204 or_b(0x06, &PADRH); /* deassert CS (both cards) */
206 led(false);
207 mutex_unlock(&mmc_mutex);
208 last_disk_activity = current_tick;
211 static void setup_sci1(int bitrate_register)
213 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
215 SCR1 = 0; /* disable serial port */
216 SMR1 = SYNC_MODE; /* no prescale */
217 BRR1 = bitrate_register;
218 SSR1 = 0;
220 SCR1 = SCI_TE; /* enable transmitter */
221 serial_mode = SER_POLL_WRITE;
224 static void set_sci1_poll_read(void)
226 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
227 SCR1 = 0; /* disable transmitter (& receiver) */
228 SCR1 = (SCI_TE|SCI_RE); /* re-enable transmitter & receiver */
229 while (!(SSR1 & SCI_TEND)); /* wait for SCI init completion (!) */
230 serial_mode = SER_POLL_READ;
231 TDR1 = 0xFF; /* send do-nothing while reading */
234 static void write_transfer(const unsigned char *buf, int len)
236 const unsigned char *buf_end = buf + len;
237 register unsigned char data;
239 if (serial_mode != SER_POLL_WRITE)
241 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
242 SCR1 = 0; /* disable transmitter & receiver */
243 SSR1 = 0; /* clear all flags */
244 SCR1 = SCI_TE; /* enable transmitter only */
245 serial_mode = SER_POLL_WRITE;
248 while (buf < buf_end)
250 data = fliptable[(signed char)(*buf++)]; /* bitswap */
251 while (!(SSR1 & SCI_TDRE)); /* wait for end of transfer */
252 TDR1 = data; /* write byte */
253 SSR1 = 0; /* start transmitting */
257 /* don't call this with len == 0 */
258 static void read_transfer(unsigned char *buf, int len)
260 unsigned char *buf_end = buf + len - 1;
261 register signed char data;
263 if (serial_mode != SER_POLL_READ)
264 set_sci1_poll_read();
266 SSR1 = 0; /* start receiving first byte */
267 while (buf < buf_end)
269 while (!(SSR1 & SCI_RDRF)); /* wait for data */
270 data = RDR1; /* read byte */
271 SSR1 = 0; /* start receiving */
272 *buf++ = fliptable[data]; /* bitswap */
274 while (!(SSR1 & SCI_RDRF)); /* wait for last byte */
275 *buf = fliptable[(signed char)(RDR1)]; /* read & bitswap */
278 /* returns 0xFF on timeout, timeout is in bytes */
279 static unsigned char poll_byte(long timeout)
281 long i;
282 unsigned char data = 0; /* stop the compiler complaining */
284 if (serial_mode != SER_POLL_READ)
285 set_sci1_poll_read();
287 i = 0;
288 do {
289 SSR1 = 0; /* start receiving */
290 while (!(SSR1 & SCI_RDRF)); /* wait for data */
291 data = RDR1; /* read byte */
292 } while ((data == 0xFF) && (++i < timeout));
294 return fliptable[(signed char)data];
297 /* returns 0 on timeout, timeout is in bytes */
298 static unsigned char poll_busy(long timeout)
300 long i;
301 unsigned char data, dummy;
303 if (serial_mode != SER_POLL_READ)
304 set_sci1_poll_read();
306 /* get data response */
307 SSR1 = 0; /* start receiving */
308 while (!(SSR1 & SCI_RDRF)); /* wait for data */
309 data = fliptable[(signed char)(RDR1)]; /* read byte */
311 /* wait until the card is ready again */
312 i = 0;
313 do {
314 SSR1 = 0; /* start receiving */
315 while (!(SSR1 & SCI_RDRF)); /* wait for data */
316 dummy = RDR1; /* read byte */
317 } while ((dummy != 0xFF) && (++i < timeout));
319 return (dummy == 0xFF) ? data : 0;
322 /* Send MMC command and get response. Returns R1 byte directly.
323 * Returns further R2 or R3 bytes in *data (can be NULL for other commands) */
324 static unsigned char send_cmd(int cmd, unsigned long parameter, void *data)
326 static struct {
327 unsigned char cmd;
328 unsigned long parameter;
329 const unsigned char crc7; /* fixed, valid for CMD0 only */
330 const unsigned char trailer;
331 } __attribute__((packed)) command = {0x40, 0, 0x95, 0xFF};
333 unsigned char ret;
335 command.cmd = cmd;
336 command.parameter = htobe32(parameter);
338 write_transfer((unsigned char *)&command, sizeof(command));
340 ret = poll_byte(20);
342 switch (cmd)
344 case CMD_SEND_CSD: /* R1 response, leave open */
345 case CMD_SEND_CID:
346 case CMD_READ_SINGLE_BLOCK:
347 case CMD_READ_MULTIPLE_BLOCK:
348 return ret;
350 case CMD_SEND_STATUS: /* R2 response, close with dummy */
351 read_transfer(data, 1);
352 break;
354 case CMD_READ_OCR: /* R3 response, close with dummy */
355 read_transfer(data, 4);
356 break;
358 default: /* R1 response, close with dummy */
359 break; /* also catches block writes */
361 write_transfer(dummy, 1);
362 return ret;
365 /* Receive CID/ CSD data (16 bytes) */
366 static int receive_cxd(unsigned char *buf)
368 if (poll_byte(20) != DT_START_BLOCK)
370 write_transfer(dummy, 1);
371 return -1; /* not start of data */
374 read_transfer(buf, 16);
375 write_transfer(dummy, 3); /* 2 bytes dontcare crc + 1 byte trailer */
376 return 0;
380 static int initialize_card(int card_no)
382 int rc, i;
383 int blk_exp, ts_exp, taac_exp;
384 tCardInfo *card = &card_info[card_no];
386 static const char mantissa[] = { /* *10 */
387 0, 10, 12, 13, 15, 20, 25, 30,
388 35, 40, 45, 50, 55, 60, 70, 80
390 static const int exponent[] = { /* use varies */
391 1, 10, 100, 1000, 10000, 100000, 1000000,
392 10000000, 100000000, 1000000000
395 if (card_no == 1)
396 mmc_status = MMC_TOUCHED;
398 /* switch to SPI mode */
399 if (send_cmd(CMD_GO_IDLE_STATE, 0, NULL) != 0x01)
400 return -1; /* error or no response */
402 /* initialize card */
403 for (i = HZ;;) /* try for 1 second*/
405 sleep(1);
406 if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0)
407 break;
408 if (--i <= 0)
409 return -2; /* timeout */
412 /* get OCR register */
413 if (send_cmd(CMD_READ_OCR, 0, &card->ocr))
414 return -3;
415 card->ocr = betoh32(card->ocr); /* no-op on big endian */
417 /* check voltage */
418 if (!(card->ocr & 0x00100000)) /* 3.2 .. 3.3 V */
419 return -4;
421 /* get CSD register */
422 if (send_cmd(CMD_SEND_CSD, 0, NULL))
423 return -5;
424 rc = receive_cxd((unsigned char*)card->csd);
425 if (rc)
426 return rc * 10 - 5;
428 blk_exp = card_extract_bits(card->csd, 83, 4);
429 if (blk_exp < 9) /* block size < 512 bytes not supported */
430 return -6;
432 card->numblocks = (card_extract_bits(card->csd, 73, 12) + 1)
433 << (card_extract_bits(card->csd, 49, 3) + 2 + blk_exp - 9);
434 card->blocksize = BLOCK_SIZE;
436 /* max transmission speed, clock divider */
437 ts_exp = card_extract_bits(card->csd, 98, 3);
438 ts_exp = (ts_exp > 3) ? 3 : ts_exp;
439 card->speed = mantissa[card_extract_bits(card->csd, 102, 4)]
440 * exponent[ts_exp + 4];
441 card->bitrate_register = (FREQ/4-1) / card->speed;
443 /* NSAC, TAAC, read timeout */
444 card->nsac = 100 * card_extract_bits(card->csd, 111, 8);
445 card->taac = mantissa[card_extract_bits(card->csd, 118, 4)];
446 taac_exp = card_extract_bits(card->csd, 114, 3);
447 card->read_timeout = ((FREQ/4) / (card->bitrate_register + 1)
448 * card->taac / exponent[9 - taac_exp]
449 + (10 * card->nsac));
450 card->read_timeout /= 8; /* clocks -> bytes */
451 card->taac = card->taac * exponent[taac_exp] / 10;
453 /* r2w_factor, write timeout */
454 card->r2w_factor = BIT_N(card_extract_bits(card->csd, 28, 3));
455 card->write_timeout = card->read_timeout * card->r2w_factor;
457 if (card->r2w_factor > 32) /* Such cards often need extra read delay */
458 card->read_timeout *= 4;
460 /* switch to full speed */
461 setup_sci1(card->bitrate_register);
463 /* always use 512 byte blocks */
464 if (send_cmd(CMD_SET_BLOCKLEN, BLOCK_SIZE, NULL))
465 return -7;
467 /* get CID register */
468 if (send_cmd(CMD_SEND_CID, 0, NULL))
469 return -8;
470 rc = receive_cxd((unsigned char*)card->cid);
471 if (rc)
472 return rc * 10 - 8;
474 card->initialized = true;
475 return 0;
478 tCardInfo *mmc_card_info(int card_no)
480 tCardInfo *card = &card_info[card_no];
482 if (!card->initialized && ((card_no == 0) || mmc_detect()))
484 select_card(card_no);
485 deselect_card();
487 return card;
490 /* Receive one block with DMA and bitswap it (chasing bitswap). */
491 static int receive_block(unsigned char *inbuf, long timeout)
493 unsigned long buf_end;
495 if (poll_byte(timeout) != DT_START_BLOCK)
497 write_transfer(dummy, 1);
498 return -1; /* not start of data */
501 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
503 SCR1 = 0; /* disable serial */
504 SSR1 = 0; /* clear all flags */
506 /* setup DMA channel 0 */
507 CHCR0 = 0; /* disable */
508 SAR0 = RDR1_ADDR;
509 DAR0 = (unsigned long) inbuf;
510 DTCR0 = BLOCK_SIZE;
511 CHCR0 = 0x4601; /* fixed source address, RXI1, enable */
512 DMAOR = 0x0001;
513 SCR1 = (SCI_RE|SCI_RIE); /* kick off DMA */
515 /* DMA receives 2 bytes more than DTCR2, but the last 2 bytes are not
516 * stored. The first extra byte is available from RDR1 after the DMA ends,
517 * the second one is lost because of the SCI overrun. However, this
518 * behaviour conveniently discards the crc. */
520 yield(); /* be nice */
522 /* Bitswap received data, chasing the DMA pointer */
523 buf_end = (unsigned long)inbuf + BLOCK_SIZE;
526 /* Call bitswap whenever (a multiple of) 8 bytes are
527 * available (value optimised by experimentation). */
528 int swap_now = (DAR0 - (unsigned long)inbuf) & ~0x00000007;
529 if (swap_now)
531 bitswap(inbuf, swap_now);
532 inbuf += swap_now;
535 while ((unsigned long)inbuf < buf_end);
537 while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
538 while (!(SSR1 & SCI_ORER)); /* wait for the trailing bytes */
539 SCR1 = 0;
540 serial_mode = SER_DISABLED;
542 write_transfer(dummy, 1); /* send trailer */
543 last_disk_activity = current_tick;
544 return 0;
547 /* Prepare a block for sending by copying it to the next write buffer
548 * and bitswapping it. */
549 static void send_block_prepare(void)
551 unsigned char *dest;
553 current_buffer ^= 1; /* toggle buffer */
554 dest = write_buffer[current_buffer] + 2;
556 memcpy(dest, send_block_addr, BLOCK_SIZE);
557 bitswap(dest, BLOCK_SIZE);
559 send_block_addr += BLOCK_SIZE;
562 /* Send one block with DMA from the current write buffer, possibly preparing
563 * the next block within the next write buffer in the background. */
564 static int send_block_send(unsigned char start_token, long timeout,
565 bool prepare_next)
567 int rc = 0;
568 unsigned char *curbuf = write_buffer[current_buffer];
570 curbuf[1] = fliptable[(signed char)start_token];
571 *(unsigned short *)(curbuf + BLOCK_SIZE + 2) = 0xFFFF;
573 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
575 SCR1 = 0; /* disable serial */
576 SSR1 = 0; /* clear all flags */
578 /* setup DMA channel 0 */
579 CHCR0 = 0; /* disable */
580 SAR0 = (unsigned long)(curbuf + 1);
581 DAR0 = TDR1_ADDR;
582 DTCR0 = BLOCK_SIZE + 3; /* start token + block + dummy crc */
583 CHCR0 = 0x1701; /* fixed dest. address, TXI1, enable */
584 DMAOR = 0x0001;
585 SCR1 = (SCI_TE|SCI_TIE); /* kick off DMA */
587 if (prepare_next)
588 send_block_prepare();
589 yield(); /* be nice */
591 while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
592 while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
593 SCR1 = 0;
594 serial_mode = SER_DISABLED;
596 if ((poll_busy(timeout) & 0x1F) != 0x05) /* something went wrong */
597 rc = -1;
599 write_transfer(dummy, 1);
600 last_disk_activity = current_tick;
602 return rc;
605 int mmc_read_sectors(IF_MD2(int drive,)
606 unsigned long start,
607 int incount,
608 void* inbuf)
610 int rc = 0;
611 int lastblock = 0;
612 unsigned long end_block;
613 tCardInfo *card;
614 #ifndef HAVE_MULTIDRIVE
615 int drive = current_card;
616 #endif
618 card = &card_info[drive];
619 rc = select_card(drive);
620 if (rc)
622 rc = rc * 10 - 1;
623 goto error;
626 end_block = start + incount;
627 if (end_block > card->numblocks)
629 rc = -2;
630 goto error;
633 /* Some cards don't like reading the very last block with
634 * CMD_READ_MULTIPLE_BLOCK, so make sure this block is always
635 * read with CMD_READ_SINGLE_BLOCK. */
636 if (end_block == card->numblocks)
637 lastblock = 1;
639 if (incount > 1)
641 /* MMC4.2: make multiplication conditional */
642 if (send_cmd(CMD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE, NULL))
644 rc = -3;
645 goto error;
647 while (--incount >= lastblock)
649 rc = receive_block(inbuf, card->read_timeout);
650 if (rc)
652 /* If an error occurs during multiple block reading, the
653 * host still needs to send CMD_STOP_TRANSMISSION */
654 send_cmd(CMD_STOP_TRANSMISSION, 0, NULL);
655 rc = rc * 10 - 4;
656 goto error;
658 inbuf += BLOCK_SIZE;
659 start++;
660 /* ^^ necessary for the abovementioned last block special case */
662 if (send_cmd(CMD_STOP_TRANSMISSION, 0, NULL))
664 rc = -5;
665 goto error;
668 if (incount > 0)
670 /* MMC4.2: make multiplication conditional */
671 if (send_cmd(CMD_READ_SINGLE_BLOCK, start * BLOCK_SIZE, NULL))
673 rc = -6;
674 goto error;
676 rc = receive_block(inbuf, card->read_timeout);
677 if (rc)
679 rc = rc * 10 - 7;
680 goto error;
684 error:
686 deselect_card();
688 return rc;
691 int mmc_write_sectors(IF_MD2(int drive,)
692 unsigned long start,
693 int count,
694 const void* buf)
696 int rc = 0;
697 int write_cmd;
698 unsigned char start_token;
699 tCardInfo *card;
700 #ifndef HAVE_MULTIDRIVE
701 int drive = current_card;
702 #endif
704 card = &card_info[drive];
705 rc = select_card(drive);
706 if (rc)
708 rc = rc * 10 - 1;
709 goto error;
712 if (start + count > card->numblocks)
713 panicf("Writing past end of card");
715 send_block_addr = buf;
716 send_block_prepare();
718 if (count > 1)
720 write_cmd = CMD_WRITE_MULTIPLE_BLOCK;
721 start_token = DT_START_WRITE_MULTIPLE;
723 else
725 write_cmd = CMD_WRITE_BLOCK;
726 start_token = DT_START_BLOCK;
728 /* MMC4.2: make multiplication conditional */
729 if (send_cmd(write_cmd, start * BLOCK_SIZE, NULL))
731 rc = -2;
732 goto error;
734 while (--count >= 0)
736 rc = send_block_send(start_token, card->write_timeout, count > 0);
737 if (rc)
739 rc = rc * 10 - 3;
740 break;
741 /* If an error occurs during multiple block writing,
742 * the STOP_TRAN token still needs to be sent. */
745 if (write_cmd == CMD_WRITE_MULTIPLE_BLOCK)
747 static const unsigned char stop_tran = DT_STOP_TRAN;
748 write_transfer(&stop_tran, 1);
749 poll_busy(card->write_timeout);
752 error:
754 deselect_card();
756 return rc;
759 bool mmc_disk_is_active(void)
761 /* this is correct unless early return from write gets implemented */
762 return mmc_mutex.locked;
765 static void mmc_thread(void)
767 struct queue_event ev;
768 bool idle_notified = false;
770 while (1) {
771 queue_wait_w_tmo(&mmc_queue, &ev, HZ);
772 switch ( ev.id )
774 case SYS_USB_CONNECTED:
775 usb_acknowledge(SYS_USB_CONNECTED_ACK);
776 /* Wait until the USB cable is extracted again */
777 usb_wait_for_disconnect(&mmc_queue);
778 break;
780 #ifdef HAVE_HOTSWAP
781 case SYS_HOTSWAP_INSERTED:
782 disk_mount(1); /* mount MMC */
783 queue_broadcast(SYS_FS_CHANGED, 0);
784 break;
786 case SYS_HOTSWAP_EXTRACTED:
787 disk_unmount(1); /* release "by force" */
788 queue_broadcast(SYS_FS_CHANGED, 0);
789 break;
790 #endif
792 default:
793 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
795 idle_notified = false;
797 else
799 if (!idle_notified)
801 call_storage_idle_notifys(false);
802 idle_notified = true;
805 break;
810 #ifdef HAVE_HOTSWAP
811 void mmc_enable_monitoring(bool on)
813 mmc_monitor_enabled = on;
815 #endif
817 bool mmc_detect(void)
819 return (adc_read(ADC_MMC_SWITCH) < 0x200);
822 bool mmc_touched(void)
824 if (mmc_status == MMC_UNKNOWN) /* try to detect */
826 mutex_lock(&mmc_mutex);
827 setup_sci1(7); /* safe value */
828 and_b(~0x02, &PADRH); /* assert CS */
829 if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0xFF)
830 mmc_status = MMC_UNTOUCHED;
831 else
832 mmc_status = MMC_TOUCHED;
834 deselect_card();
836 return mmc_status == MMC_TOUCHED;
839 bool mmc_usb_active(int delayticks)
841 /* reading "inactive" is delayed by user-supplied monoflop value */
842 return (usb_activity ||
843 TIME_BEFORE(current_tick, last_usb_activity + delayticks));
846 static void mmc_tick(void)
848 bool current_status;
849 #ifndef HAVE_HOTSWAP
850 const bool mmc_monitor_enabled = true;
851 #endif
853 if (new_mmc_circuit)
854 /* USB bridge activity is 0 on idle, ~527 on active */
855 current_status = adc_read(ADC_USB_ACTIVE) > 0x100;
856 else
857 current_status = adc_read(ADC_USB_ACTIVE) < 0x190;
859 if (!current_status && usb_activity)
860 last_usb_activity = current_tick;
861 usb_activity = current_status;
863 if (mmc_monitor_enabled)
865 current_status = mmc_detect();
866 /* Only report when the status has changed */
867 if (current_status != last_mmc_status)
869 last_mmc_status = current_status;
870 countdown = HZ/3;
872 else
874 /* Count down until it gets negative */
875 if (countdown >= 0)
876 countdown--;
878 if (countdown == 0)
880 if (current_status)
882 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
884 else
886 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
887 mmc_status = MMC_UNTOUCHED;
888 card_info[1].initialized = false;
895 void mmc_enable(bool on)
897 PBCR1 &= ~0x0CF0; /* PB13, PB11 and PB10 become GPIO,
898 * if not modified below */
899 if (on)
900 PBCR1 |= 0x08A0; /* as SCK1, TxD1, RxD1 */
902 and_b(~0x80, &PADRL); /* assert flash reset */
903 sleep(HZ/100);
904 or_b(0x80, &PADRL); /* de-assert flash reset */
905 sleep(HZ/100);
906 card_info[0].initialized = false;
907 card_info[1].initialized = false;
910 int mmc_init(void)
912 int rc = 0;
914 if (!initialized)
916 mutex_init(&mmc_mutex);
917 queue_init(&mmc_queue, true);
919 mutex_lock(&mmc_mutex);
920 led(false);
922 last_mmc_status = mmc_detect();
923 #ifndef HAVE_MULTIDRIVE
924 /* Use MMC if inserted, internal flash otherwise */
925 current_card = last_mmc_status ? 1 : 0;
926 #endif
928 if (!initialized)
930 if (!last_mmc_status)
931 mmc_status = MMC_UNTOUCHED;
933 /* Port setup */
934 PACR1 &= ~0x0F3C; /* GPIO function for PA13 (flash busy), PA12
935 * (clk gate), PA10 (flash CS), PA9 (MMC CS) */
936 PACR2 &= ~0x4000; /* GPIO for PA7 (flash reset) */
937 PADR |= 0x0680; /* set all the selects + reset high (=inactive) */
938 PAIOR |= 0x1680; /* make outputs for them and the PA12 clock gate */
940 PBCR1 &= ~0x0CF0; /* GPIO function for PB13, PB11 and PB10 */
941 PBDR |= 0x2C00; /* SCK1, TxD1 and RxD1 high in GPIO */
942 PBIOR |= 0x2000; /* SCK1 output */
943 PBIOR &= ~0x0C00; /* TxD1, RxD1 input */
945 IPRE &= 0x0FFF; /* disable SCI1 interrupts for the CPU */
947 new_mmc_circuit = ((HW_MASK & MMC_CLOCK_POLARITY) != 0);
949 create_thread(mmc_thread, mmc_stack,
950 sizeof(mmc_stack), 0, mmc_thread_name
951 IF_PRIO(, PRIORITY_SYSTEM)
952 IF_COP(, CPU));
953 tick_add_task(mmc_tick);
954 initialized = true;
956 mmc_enable(true);
958 mutex_unlock(&mmc_mutex);
959 return rc;
962 long mmc_last_disk_activity(void)
964 return last_disk_activity;
967 #ifdef STORAGE_GET_INFO
968 void mmc_get_info(IF_MD2(int drive,) struct storage_info *info)
970 #ifndef HAVE_MULTIDRIVE
971 const int drive=0;
972 #endif
973 info->sector_size=card_info[drive].blocksize;
974 info->num_sectors=card_info[drive].numblocks;
975 info->vendor="Rockbox";
976 if(drive==0)
978 info->product="Internal Storage";
980 else
982 info->product="MMC Card Slot";
984 info->revision="0.00";
986 #endif
988 #ifdef HAVE_HOTSWAP
989 bool mmc_removable(IF_MD_NONVOID(int drive))
991 #ifndef HAVE_MULTIDRIVE
992 const int drive=0;
993 #endif
994 return (drive==1);
997 bool mmc_present(IF_MD_NONVOID(int drive))
999 #ifndef HAVE_MULTIDRIVE
1000 const int drive=0;
1001 #endif
1002 if(drive==0)
1004 return true;
1006 else
1008 return mmc_detect();
1011 #endif
1014 void mmc_sleep(void)
1018 void mmc_spin(void)
1022 void mmc_spindown(int seconds)
1024 (void)seconds;
1027 #ifdef CONFIG_STORAGE_MULTI
1028 int mmc_num_drives(int first_drive)
1030 /* We don't care which logical drive number(s) we have been assigned */
1031 (void)first_drive;
1033 #ifdef HAVE_MULTIDRIVE
1034 return 2;
1035 #else
1036 return 1;
1037 #endif
1039 #endif