4 * Essentially copied from Minimig
6 * Current implementation is polled & synchronous
15 /* Enable / Disable Debug info */
18 /* MMC, or SD Card Specifification V1, V2, SDHC */
19 #define MMC_CARD_NONE 0x00
20 #define MMC_CARD_MMC 0x01
21 #define MMC_CARD_SD_RAW_SPEC_1 0x02
22 #define MMC_CARD_SD_RAW_SPEC_2 0x04
23 #define MMC_CARD_SD_RAW_SPEC_SDHC 0x08
25 static unsigned char mmc_card;
26 static unsigned char mmc_crc_7;
27 static unsigned int mmc_timeout;
28 static unsigned char mmc_resp[5];
31 #define pr_debug(args...) printf_tiny(args)
33 #define pr_debug(args...)
36 /* Calculate CRC7 checksum */
37 static void mmc_add_crc7(unsigned char c)
41 for (i = 0; i < 8; i++) {
51 /* Send a byte and calculate CRC */
52 static void mmc_spi_crc(unsigned char cmd)
58 /* Send a command to the SDcard */
59 static void mmc_cmd_r0(char cmd, unsigned short addr_h, unsigned short addr_l)
70 mmc_spi_crc(addr_h >> 8);
71 mmc_spi_crc(addr_h & 0xff);
72 mmc_spi_crc(addr_l >> 8);
73 mmc_spi_crc(addr_l & 0xff);
75 /* Cook and send CRC */
81 /* Send a command to the SDcard, a one byte response is expected */
82 static void mmc_cmd_r1(char cmd, unsigned short addr_h, unsigned short addr_l)
84 unsigned char i = 100;
86 mmc_cmd_r0(cmd, addr_h, addr_l);
88 mmc_resp[0] = do_spi(0xff);
89 while (mmc_resp[0] == 0xff && --i);
92 /* Send a command to the SDcard, a two byte response is expected */
93 static void mmc_cmd_r2(char cmd, unsigned short addr_h, unsigned short addr_l)
95 unsigned char i = 100;
97 mmc_cmd_r0(cmd, addr_h, addr_l);
99 mmc_resp[0] = do_spi(0xff);
100 while (mmc_resp[0] == 0xff && --i);
101 mmc_resp[1] = do_spi(0xff);
104 /* Send a command to the SDcard, a five byte response is expected */
105 static void mmc_cmd_r3(char cmd, unsigned short addr_h, unsigned short addr_l)
107 unsigned char i = 100;
109 mmc_cmd_r0(cmd, addr_h, addr_l);
111 mmc_resp[0] = do_spi(0xff);
112 while (mmc_resp[0] == 0xff && --i);
113 mmc_resp[1] = do_spi(0xff);
114 mmc_resp[2] = do_spi(0xff);
115 mmc_resp[3] = do_spi(0xff);
116 mmc_resp[4] = do_spi(0xff);
119 /* Enable the MMC/SD card correctly */
120 unsigned char mmc_init(void)
124 /* Set SPI clock .. Might need to be smarter at changing the
125 * SPI clock based on what device is enabled...
127 SSPCON1 = 0x22; //spiclk = Fosc/64 (init clock 100-400 kHz)
133 _M_CD = 1; /* Enable clock*/
134 _M_CS = 1; /* SDcard Disabled */
136 /* Set SDcard in SPI-Mode, Reset*/
137 /* (10 * 8bits = 80 clockpulses) */
138 for (lp = 0; lp < 10; lp++)
141 /* Delay for a lot of milliseconds (least 16 bus clock cycles) */
142 for (lp=0; lp < 56000; lp++)
143 ; /* PRAY sdcc doesn't optimize that out !
144 Should use timer instead ! */
150 /* No card detected by default */
151 mmc_card = MMC_CARD_NONE;
153 /* CMD0: Reset all cards to IDLE state */
154 mmc_cmd_r1(CMD0, 0, 0);
155 if (mmc_resp[0] != 0x01) {
156 pr_debug("No card detected ! (resp=%x)\n", mmc_resp[0]);
161 mmc_card = MMC_CARD_MMC;
163 /* Check for SD Card V2 */
164 /* (Voltage 2.7V - 3.6V, 0xaa test pattern) */
165 mmc_cmd_r3(CMD8, 0x0000, 0x01aa);
166 if (!(mmc_resp[0] & 0x04)) {
167 if (mmc_resp[3] == 0x01 && mmc_resp[4] == 0xaa) {
168 pr_debug("SD card V2 detected, possible SDHC\n");
169 mmc_card |= MMC_CARD_SD_RAW_SPEC_2;
171 pr_debug("Error detecting SD V2 card\n");
175 /* Check for SD card V1 */
176 mmc_cmd_r1(CMD55, 0, 0);
177 mmc_cmd_r1(CMD41, 0, 0);
178 if (!(mmc_resp[0] & 0x04)) {
179 pr_debug("SD card V1 detected\n");
180 mmc_card |= MMC_CARD_SD_RAW_SPEC_1;
182 pr_debug("MMC-card detected\n");
185 /* Wait for card to get ready */
188 if (mmc_card & (MMC_CARD_SD_RAW_SPEC_1 | MMC_CARD_SD_RAW_SPEC_2)) {
189 /* When SD Card detected */
191 if (mmc_card & MMC_CARD_SD_RAW_SPEC_2)
193 /* Activate SD card init process */
194 mmc_cmd_r1(CMD55, 0, 0);
195 mmc_cmd_r1(CMD41, lp, 0);
197 /* Activate the MMC cards init process */
198 mmc_cmd_r1(CMD1, 0, 0);
201 /* Check if card is idle */
202 if (mmc_resp[0] == 0x00)
205 if (mmc_timeout == 1000) {
206 pr_debug("SD/MMC ACMD41/CMD1 response timeout...\n");
211 /* Check for SDHC card */
212 if (mmc_card & MMC_CARD_SD_RAW_SPEC_2) {
213 /* Get operating conditions */
214 mmc_cmd_r3(CMD58, 0, 0);
216 pr_debug("SD/MMC: Failed to get operating conditions\n");
220 if (mmc_resp[1] & 0x40) {
221 pr_debug("SDHC Card Detected\n");
222 mmc_card |= MMC_CARD_SD_RAW_SPEC_SDHC;
226 /* Set block size to 512 bytes */
229 mmc_cmd_r1(CMD16, 0x0000, 0x0200);
233 if(mmc_timeout == 100) {
234 pr_debug("Set block size to 512 timeout\n");
239 spi_disable_sdcard();
241 /* XXX Stuff below from minimig... it's strange... it sets SSPCON1 to a
242 * different speed from what is done in hardware_init.c for everybody
244 // TODO: Change speed for card access
245 SSPCON1 = 0x20; //spiclk = Fosc/4 (5 MHz)
248 mmc_card = MMC_CARD_NONE;
249 spi_disable_sdcard();
253 /* Read single block (with block-size set by CMD16 to 512 by default) */
254 unsigned char mmc_read(unsigned long lba, unsigned char *out_data)
256 unsigned short upper_lba, lower_lba;
260 /* !SDHC uses byte address instead of LBA */
261 if (!(mmc_card & MMC_CARD_SD_RAW_SPEC_SDHC))
264 pr_debug("SD/MMC: Reading LBA 0x%lx\n", lba);
266 upper_lba = (unsigned short)(lba>>16);
267 lower_lba = (unsigned short)lba;
271 mmc_cmd_r1(CMD17, upper_lba, lower_lba);
273 /* Exit if invalid response*/
275 pr_debug("SD/MMC CMD17: invalid response %x\n", mmc_resp[0]);
279 /* Wait for start of data transfer with timeout */
281 while(do_spi(0xff) != 0xfe) {
282 if (mmc_timeout++ >= 50000) {
283 pr_debug("SD/MMC CMD17: no data token\n");
288 /* Read data and exit OK */
290 for (i = 0; i < 128; i++) {
292 while (!SSPSTATbits.BF)
296 while (!SSPSTATbits.BF)
300 while (!SSPSTATbits.BF)
304 while (!SSPSTATbits.BF)
309 /* XXX Todo: check CRC */
310 do_spi(0xff); //Read CRC lo byte
311 do_spi(0xff); //Read CRC hi byte
313 spi_disable_sdcard();
316 spi_disable_sdcard();
322 /* Write: 512 Byte-Mode, this will not work (read MMC and SD-card specs) with any other
325 unsigned char mmc_write(unsigned long lba, unsigned char *in_data)
327 unsigned short upper_lba, lower_lba;
331 /* !SDHC uses byte address instead of LBA */
332 if (!(mmc_card & MMC_CARD_SD_RAW_SPEC_SDHC))
335 pr_debug("SD/MMC: Writing LBA 0x%lx\n", lba);
337 upper_lba = (unsigned short)(lba>>16);
338 lower_lba = (unsigned short)lba;
342 mmc_cmd_r1(CMD24, upper_lba, lower_lba);
344 /* Exit if invalid response*/
346 pr_debug("SD/MMC CMD24: invalid response %x\n", mmc_resp[0]);
350 do_spi(0xff); //One byte gap
351 do_spi(0xfe); //Send Data token
353 /* Send bytes for sector */
355 for (i = 0; i < 128; i++) {
357 while (!SSPSTATbits.BF)
360 while (!SSPSTATbits.BF)
363 while (!SSPSTATbits.BF)
366 while (!SSPSTATbits.BF)
370 /* XXX Todo: Send proper CRC ? */
371 do_spi(0xff); //Send CRC lo byte
372 do_spi(0xff); //Send CRC hi byte
374 /* Read packet response */
378 * 010 = Data accepted
379 * 101 = Data rejected due to CRC error
380 * 110 = Data rejected due to write error
383 if (i != 0b00000101) {
384 pr_debug("SD/MMC CMD24: write error %x\n", i);
389 /* Wait until the card has finished writing the data */
390 while (do_spi(0xff) == 0x00) {
391 if (mmc_timeout++ >= 50000) {
392 pr_debug("SD/MMC CMD24: busy wait timeout\n");
396 spi_disable_sdcard();
399 spi_disable_sdcard();