1/*-
2 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * This software is derived from software provide by Kwikbyte who specifically
25 * disclaimed copyright on the code.
26 *
27 * $FreeBSD: releng/11.0/sys/boot/arm/at91/libat91/sd-card.c 297793 2016-04-10 23:07:00Z pfg $
28 */
29
30//*----------------------------------------------------------------------------
31//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
32//*----------------------------------------------------------------------------
33//* The software is delivered "AS IS" without warranty or condition of any
34//* kind, either express, implied or statutory. This includes without
35//* limitation any warranty or condition with respect to merchantability or
36//* fitness for any particular purpose, or against the infringements of
37//* intellectual property rights of others.
38//*----------------------------------------------------------------------------
39//* File Name           : main.c
40//* Object              : main application written in C
41//* Creation            : FB   21/11/2002
42//*
43//*----------------------------------------------------------------------------
44#include "at91rm9200.h"
45#include "lib_AT91RM9200.h"
46#include "mci_device.h"
47#include "lib.h"
48#include "sd-card.h"
49
50#define AT91C_MCI_TIMEOUT       1000000   /* For AT91F_MCIDeviceWaitReady */
51#define SD_BLOCK_SIZE           512
52
53//* Global Variables
54static AT91S_MciDevice          MCI_Device;
55
56/******************************************************************************
57**Error return codes
58******************************************************************************/
59#define MCI_UNSUPP_SIZE_ERROR   5
60#define MCI_UNSUPP_OFFSET_ERROR 6
61
62//*----------------------------------------------------------------------------
63//* \fn    MCIDeviceWaitReady
64//* \brief Wait for MCI Device ready
65//*----------------------------------------------------------------------------
66static unsigned int
67MCIDeviceWaitReady(unsigned int timeout)
68{
69	volatile unsigned int status;
70	int waitfor;
71
72	if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK)
73		waitfor = AT91C_MCI_RXBUFF;
74	else
75		waitfor = AT91C_MCI_NOTBUSY;
76	do
77	{
78		status = AT91C_BASE_MCI->MCI_SR;
79		timeout--;
80	}
81	while( !(status & waitfor) && (timeout>0) );
82
83	status = AT91C_BASE_MCI->MCI_SR;
84
85	// If End of Tx Buffer Empty interrupt occurred
86	if (MCI_Device.state == AT91C_MCI_TX_SINGLE_BLOCK && status & AT91C_MCI_TXBUFE) {
87		AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
88 		AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
89		MCI_Device.state = AT91C_MCI_IDLE;
90	}	// End of if AT91C_MCI_TXBUFF
91
92	// If End of Rx Buffer Full interrupt occurred
93	if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK && status & AT91C_MCI_RXBUFF) {
94		AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
95 		AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
96		MCI_Device.state = AT91C_MCI_IDLE;
97	}	// End of if AT91C_MCI_RXBUFF
98
99	//printf("WaitReady returning status %x\n", status);
100
101	return status;
102}
103
104static inline unsigned int
105swap(unsigned int v)
106{
107	unsigned int t1;
108
109	__asm __volatile("eor %1, %0, %0, ror #16\n"
110	    		"bic %1, %1, #0x00ff0000\n"
111			"mov %0, %0, ror #8\n"
112			"eor %0, %0, %1, lsr #8\n"
113			 : "+r" (v), "=r" (t1));
114
115	return (v);
116}
117
118inline static unsigned int
119wait_ready()
120{
121	int status;
122	int timeout = AT91C_MCI_TIMEOUT;
123
124	// wait for CMDRDY Status flag to read the response
125	do
126	{
127		status = AT91C_BASE_MCI->MCI_SR;
128	} while( !(status & AT91C_MCI_CMDRDY) && (--timeout > 0)  );
129
130	return status;
131}
132
133//*----------------------------------------------------------------------------
134//* \fn    MCI_SendCommand
135//* \brief Generic function to send a command to the MMC or SDCard
136//*----------------------------------------------------------------------------
137static int
138MCI_SendCommand(
139	unsigned int Cmd,
140	unsigned int Arg)
141{
142	unsigned int error;
143	unsigned int errorMask = AT91C_MCI_SR_ERROR;
144	unsigned int opcode = Cmd & 0x3F;
145
146	//printf("SendCmd %d (%x) arg %x\n", opcode, Cmd, Arg);
147
148	// Don't check response CRC on ACMD41 (R3 response type).
149
150	if (opcode == 41)
151		errorMask &= ~AT91C_MCI_RCRCE;
152
153	AT91C_BASE_MCI->MCI_ARGR = Arg;
154	AT91C_BASE_MCI->MCI_CMDR = Cmd;
155
156	error = wait_ready();
157
158	if ((error & errorMask) != 0) {
159		return (1);
160	}
161	return 0;
162}
163
164//*----------------------------------------------------------------------------
165//* \fn    MCI_GetStatus
166//* \brief Addressed card sends its status register
167//*----------------------------------------------------------------------------
168static unsigned int
169MCI_GetStatus()
170{
171	if (MCI_SendCommand(SEND_STATUS_CMD, MCI_Device.RCA << 16))
172		return 0;
173	return (AT91C_BASE_MCI->MCI_RSPR[0]);
174
175}
176
177//*----------------------------------------------------------------------------
178//* \fn    MCI_ReadBlock
179//* \brief Start the read for a single 512-byte block
180//*----------------------------------------------------------------------------
181static int
182MCI_StartReadBlock(unsigned blknum, void *dataBuffer)
183{
184        // Init Mode Register
185	AT91C_BASE_MCI->MCI_MR |= ((SD_BLOCK_SIZE << 16) | AT91C_MCI_PDCMODE);
186
187	// (PDC) Receiver Transfer Enable
188	AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
189	AT91C_BASE_PDC_MCI->PDC_RPR  = (unsigned int)dataBuffer;
190	AT91C_BASE_PDC_MCI->PDC_RCR  = SD_BLOCK_SIZE / 4;
191	AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTEN;
192
193	// SDHC wants block offset, non-HC wants byte offset.
194	if (!MCI_Device.IsSDHC)
195		blknum *= SD_BLOCK_SIZE;
196
197	// Send the Read single block command
198	if (MCI_SendCommand(READ_SINGLE_BLOCK_CMD, blknum)) {
199		return AT91C_READ_ERROR;
200	}
201	MCI_Device.state = AT91C_MCI_RX_SINGLE_BLOCK;
202
203	return 0;
204}
205
206//*----------------------------------------------------------------------------
207//* \fn    MCI_readblocks
208//* \brief Read one or more blocks
209//*----------------------------------------------------------------------------
210int
211MCI_readblocks(char* dest, unsigned blknum, unsigned blkcount)
212{
213	unsigned int status;
214	unsigned int *walker;
215
216	if (MCI_Device.state != AT91C_MCI_IDLE) {
217		return 1;
218	}
219
220	if ((MCI_GetStatus() & AT91C_SR_READY_FOR_DATA) == 0) {
221		return 1;
222	}
223
224	// As long as there is data to read
225	while (blkcount)
226	{
227		//Do the reading
228		if (MCI_StartReadBlock(blknum, dest))
229			return -1;
230
231		// Wait MCI Device Ready
232		status = MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
233		if (status & AT91C_MCI_SR_ERROR)
234			return 1;
235
236		// Fix erratum in MCI part - endian-swap all data.
237		for (walker = (unsigned int *)dest;
238		     walker < (unsigned int *)(dest + SD_BLOCK_SIZE); walker++)
239		    *walker = swap(*walker);
240
241		// Update counters & pointers
242		++blknum;
243		--blkcount;
244		dest += SD_BLOCK_SIZE;
245	}
246
247
248	return 0;
249}
250
251//*----------------------------------------------------------------------------
252//* \fn    MCI_read
253//* \brief Legacy read function, takes byte offset and length but was always
254//*  used to read full blocks; interface preserved for existing boot code.
255//*----------------------------------------------------------------------------
256int
257MCI_read(char* dest, unsigned byteoffset, unsigned length)
258{
259	return MCI_readblocks(dest,
260	    byteoffset/SD_BLOCK_SIZE, length/SD_BLOCK_SIZE);
261}
262
263//*----------------------------------------------------------------------------
264//* \fn    MCI_SDCard_SendAppCommand
265//* \brief Specific function to send a specific command to the SDCard
266//*----------------------------------------------------------------------------
267static int
268MCI_SDCard_SendAppCommand(
269	unsigned int Cmd_App,
270	unsigned int Arg)
271{
272	int status;
273
274	if ((status = MCI_SendCommand(APP_CMD, (MCI_Device.RCA << 16))) == 0)
275		status = MCI_SendCommand(Cmd_App,Arg);
276	return status;
277}
278
279//*----------------------------------------------------------------------------
280//* \fn    MCI_GetCSD
281//* \brief Asks to the specified card to send its CSD
282//*----------------------------------------------------------------------------
283static int
284MCI_GetCSD(unsigned int rca, unsigned int *response)
285{
286	if (MCI_SendCommand(SEND_CSD_CMD, (rca << 16)))
287		return 1;
288
289	response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
290	response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
291	response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
292	response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
293
294	return 0;
295}
296
297//*----------------------------------------------------------------------------
298//* \fn    MCI_SDCard_GetOCR
299//* \brief Wait for card to power up and determine whether it's SDHC or not.
300//*----------------------------------------------------------------------------
301static int
302MCI_SDCard_GetOCR()
303{
304	unsigned int response;
305	unsigned int arg = AT91C_MMC_HOST_VOLTAGE_RANGE;
306	int          timeout = AT91C_MCI_TIMEOUT;
307
308	// Force card to idle state.
309
310	MCI_SendCommand(GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
311
312	// Begin probe for SDHC by sending CMD8; only v2.0 cards respond to it.
313	//
314	// Arg is vvpp where vv is voltage range and pp is an arbitrary bit
315	// pattern that gets echoed back in the response. The only voltage
316	// ranges defined are:
317	//   0x01 = 2.7 - 3.6
318	//   0x02 = "reserved for low voltage" whatever that means.
319	//
320	// If the card fails to respond then it's not v2.0. If it responds by
321	// echoing back exactly the arg we sent, then it's a v2.0 card and can
322	// run at our voltage.  That means that when we send the ACMD41 (in
323	// MCI_SDCard_GetOCR) we can include the HCS bit to inquire about SDHC.
324
325	if (MCI_SendCommand(SD_SEND_IF_COND_CMD, 0x01AA) == 0) {
326		MCI_Device.IsSDv2 = (AT91C_BASE_MCI->MCI_RSPR[0] == 0x01AA);
327	}
328
329	// If we've determined the card supports v2.0 functionality, set the
330	// HCS/CCS bit to indicate that we support SDHC.  This will cause a
331	// v2.0 card to report whether it is SDHC in the ACMD41 response.
332
333	if (MCI_Device.IsSDv2) {
334		arg |= AT91C_CCS;
335	}
336
337	// The RCA to be used for CMD55 in Idle state shall be the card's
338	// default RCA=0x0000.
339
340	MCI_Device.RCA = 0x0;
341
342	// Repeat ACMD41 until the card comes out of power-up-busy state.
343
344	do {
345		if (MCI_SDCard_SendAppCommand(SDCARD_APP_OP_COND_CMD, arg)) {
346			return 1;
347		}
348		response = AT91C_BASE_MCI->MCI_RSPR[0];
349	} while (!(response & AT91C_CARD_POWER_UP_DONE) && (--timeout > 0));
350
351	// A v2.0 card sets CCS (card capacity status) in the response if it's SDHC.
352
353	if (MCI_Device.IsSDv2) {
354		MCI_Device.IsSDHC = ((response & AT91C_CCS) == AT91C_CCS);
355	}
356
357	return (0);
358}
359
360//*----------------------------------------------------------------------------
361//* \fn    MCI_SDCard_GetCID
362//* \brief Asks to the SDCard on the chosen slot to send its CID
363//*----------------------------------------------------------------------------
364static int
365MCI_SDCard_GetCID(unsigned int *response)
366{
367	if (MCI_SendCommand(ALL_SEND_CID_CMD, AT91C_NO_ARGUMENT))
368		return 1;
369
370	response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
371	response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
372	response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
373	response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
374
375	return 0;
376}
377
378//*----------------------------------------------------------------------------
379//* \fn    sdcard_4wire
380//* \brief  Set bus width to 1-bit or 4-bit according to the parm.
381//*
382//* Unlike most functions in this file, the return value from this one is
383//* bool-ish; returns 0 on failure, 1 on success.
384//*----------------------------------------------------------------------------
385int
386sdcard_use4wire(int use4wire)
387{
388	volatile int	ret_value;
389
390	do {
391		ret_value=MCI_GetStatus();
392	}
393	while((ret_value > 0) && ((ret_value & AT91C_SR_READY_FOR_DATA) == 0));
394
395	// If going to 4-wire mode, ask the card to turn off the DAT3 card detect
396	// pullup resistor, if going to 1-wire ask it to turn it back on.
397
398	ret_value = MCI_SDCard_SendAppCommand(SDCARD_SET_CLR_CARD_DETECT_CMD,
399					      use4wire ? 0 : 1);
400	if (ret_value != AT91C_CMD_SEND_OK)
401		return 0;
402
403	// Ask the card to go into the requested mode.
404
405	ret_value = MCI_SDCard_SendAppCommand(SDCARD_SET_BUS_WIDTH_CMD,
406					      use4wire ? AT91C_BUS_WIDTH_4BITS :
407					                 AT91C_BUS_WIDTH_1BIT);
408	if (ret_value != AT91C_CMD_SEND_OK)
409		return 0;
410
411	// Set the MCI device to match the mode we set in the card.
412
413	if (use4wire) {
414		MCI_Device.SDCard_bus_width = AT91C_BUS_WIDTH_4BITS;
415		AT91C_BASE_MCI->MCI_SDCR |= AT91C_MCI_SCDBUS;
416	} else {
417		MCI_Device.SDCard_bus_width = AT91C_BUS_WIDTH_1BIT;
418		AT91C_BASE_MCI->MCI_SDCR &= ~AT91C_MCI_SCDBUS;
419	}
420
421	return 1;
422}
423
424//*----------------------------------------------------------------------------
425//* \fn    sdcard_init
426//* \brief get the mci device ready to read from an SD or SDHC card.
427//*
428//* Unlike most functions in this file, the return value from this one is
429//* bool-ish; returns 0 on failure, 1 on success.
430//*----------------------------------------------------------------------------
431int
432sdcard_init(void)
433{
434	unsigned int	tab_response[4];
435	int i;
436
437	// Init MCI for MMC and SDCard interface
438	AT91F_MCI_CfgPIO();
439	AT91F_MCI_CfgPMC();
440	AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
441
442	// Init Device Structure
443	MCI_Device.state		= AT91C_MCI_IDLE;
444	MCI_Device.SDCard_bus_width	= 0;
445	MCI_Device.IsSDv2		= 0;
446	MCI_Device.IsSDHC		= 0;
447
448	// Reset the MCI and set the bus speed.
449	// Using MCK/230 gives a legal (under 400khz) bus speed for the card id
450	// sequence for all reasonable master clock speeds.
451
452	AT91C_BASE_MCI->MCI_CR = AT91C_MCI_MCIDIS | 0x80;
453	AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
454	AT91C_BASE_MCI->MCI_DTOR = AT91C_MCI_DTOR_1MEGA_CYCLES;
455	AT91C_BASE_MCI->MCI_MR = AT91C_MCI_PDCMODE | 114; /* clkdiv 114 = MCK/230 */
456	AT91C_BASE_MCI->MCI_SDCR = AT91C_MCI_MMC_SLOTA;
457	AT91C_BASE_MCI->MCI_CR = AT91C_MCI_MCIEN|AT91C_MCI_PWSEN;
458
459	// Wait for the card to come out of power-up-busy state by repeatedly
460	// sending ACMD41.  This also probes for SDHC versus standard cards.
461
462	for (i = 0; i < 100; i++) {
463		if (MCI_SDCard_GetOCR() == 0)
464			break;
465		if ((i & 0x01) == 0) {
466			printf(".");
467		}
468	}
469	if (i >= 100)
470		return 0;
471
472	if (MCI_SDCard_GetCID(tab_response))
473		return 0;
474
475	// Tell the card to set its address, and remember the result.
476
477	if (MCI_SendCommand(SET_RELATIVE_ADDR_CMD, 0))
478		return 0;
479	MCI_Device.RCA = (AT91C_BASE_MCI->MCI_RSPR[0] >> 16);
480
481	// After sending CMD3 (set addr) we can increase the clock to full speed.
482	// Using MCK/4 gives a legal (under 25mhz) bus speed for all reasonable
483	// master clock speeds.
484
485	AT91C_BASE_MCI->MCI_MR = AT91C_MCI_PDCMODE | 1; /* clkdiv 1 = MCK/4 */
486
487	if (MCI_GetCSD(MCI_Device.RCA,tab_response))
488		return 0;
489	MCI_Device.READ_BL_LEN = (tab_response[1] >> CSD_1_RD_B_LEN_S) &
490	    CSD_1_RD_B_LEN_M;
491
492#ifdef REPORT_SIZE
493	{
494		unsigned int	mult,blocknr;
495		// compute MULT
496		mult = 1 << ( ((tab_response[2] >> CSD_2_C_SIZE_M_S) &
497		    CSD_2_C_SIZE_M_M) + 2 );
498		// compute MSB of C_SIZE
499		blocknr = ((tab_response[1] >> CSD_1_CSIZE_H_S) &
500		    CSD_1_CSIZE_H_M) << 2;
501		// compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
502		blocknr = mult * ((blocknr + ((tab_response[2] >> CSD_2_CSIZE_L_S) &
503		    CSD_2_CSIZE_L_M)) + 1);
504		MCI_Device.Memory_Capacity = (1 << MCI_Device.READ_BL_LEN) * blocknr;
505		printf("Found SD card %u bytes\n", MCI_Device.Memory_Capacity);
506	}
507#endif
508
509	// Select card and set block length for following transfers.
510
511	if (MCI_SendCommand(SEL_DESEL_CARD_CMD, (MCI_Device.RCA)<<16))
512		return 0;
513	if (MCI_SendCommand(SET_BLOCKLEN_CMD, SD_BLOCK_SIZE))
514		return 0;
515
516	return 1;
517}
518