aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/LLD/SDMMCv1/ch_sdmmc_mmc.c
blob: 99ad689620c06e31971bb5d2b819f2d6456b3956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <string.h>
#include "hal.h"

#if (HAL_USE_SDMMC == TRUE)

#include "sama_sdmmc_lld.h"
#include "ch_sdmmc_device.h"
#include "ch_sdmmc_cmds.h"
#include "ch_sdmmc_sd.h"

#ifndef SDMMC_TRIM_MMC

/** Check if MMC Spec version 4 */
#define MMC_IsVer4(pSd)     ( MMC_CSD_SPEC_VERS(pSd->CSD) >= 4 )

/** Check if MMC CSD structure is 1.2 (3.1 or later) */
#define MMC_IsCSDVer1_2(pSd) \
    (  (SD_CSD_STRUCTURE(pSd->CSD)==2) \
     ||(SD_CSD_STRUCTURE(pSd->CSD)>2&&MMC_EXT_CSD_STRUCTURE(pSd->EXT)>=2) )


/** MMC transfer multiplier factor codes (1/10) list */

const uint8_t mmcTransMultipliers[16] = {
	0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
};


static uint8_t mmcSelectBuswidth(SdmmcDriver *driver, uint8_t busWidth, bool ddr)
{
	uint8_t error;
	uint32_t status;
	MmcCmd6Arg cmd6Arg = {
		.access = 0x3,   /* Write byte in the EXT_CSD register */
		.index = MMC_EXT_BUS_WIDTH_I,   /* Target byte in EXT_CSD */
		.value = MMC_EXT_BUS_WIDTH_1BIT,   /* Byte value */
	};

	if (busWidth == 8)
		cmd6Arg.value = MMC_EXT_BUS_WIDTH_8BITS
		    | (ddr ? MMC_EXT_BUS_WIDTH_DDR : 0);
	else if (busWidth == 4)
		cmd6Arg.value = MMC_EXT_BUS_WIDTH_4BITS
		    | (ddr ? MMC_EXT_BUS_WIDTH_DDR : 0);
	else if (busWidth != 1)
		return SDMMC_PARAM;

	error = MmcCmd6(driver, &cmd6Arg, &status);

	if (error)
		return SDMMC_ERR;
	else if (status & STATUS_MMC_SWITCH)
		return SDMMC_NOT_SUPPORTED;

	return SDMMC_OK;
}

static uint8_t mmcDetectBuswidth(SdmmcDriver *driver)
{
	uint8_t error, busWidth, mask = 0xff, i, len;
	sSdCard *pSd =&driver->card;
	//assert(sizeof(pSd->sandbox1) >= 8);
	//assert(sizeof(pSd->sandbox2) >= 8);

	memset(pSd->sandbox1, 0, 8);
	for (busWidth = 8; busWidth != 0; busWidth /= busWidth == 8 ? 2 : 4) {
		error = HwSetBusWidth(driver, busWidth);
		if (error)
			continue;
		switch (busWidth) {
		case 8:
			pSd->sandbox1[0] = 0x55;
			pSd->sandbox1[1] = 0xaa;
			break;
		case 4:
			pSd->sandbox1[0] = 0x5a;
			pSd->sandbox1[1] = 0;
			break;
		case 1:
			pSd->sandbox1[0] = 0x80;
			pSd->sandbox1[1] = 0;
			break;
		}
		len = (uint8_t)max_u32(busWidth, 2);
		error = Cmd19(driver, pSd->sandbox1, len, NULL);

		if (error) {
			/* Devices which do not respond to CMD19 - which results
			 * in the driver returning SDMMC_ERROR_NORESPONSE -
			 * simply do not support the bus test procedure.
			 * When the device responds to CMD19, mind the
			 * difference with other data write commands: further
			 * to host data, the device does not emit the CRC status
			 * token. Typically the peripheral reports the anomaly,
			 * and the driver is likely to return SDMMC_ERR_IO. */
			if (error != SDMMC_ERR_IO)
				return 0;
		}
		error = Cmd14(driver, pSd->sandbox2, busWidth, NULL);

		if (error)
			continue;
		if (busWidth == 1) {
			mask = 0xc0;
			pSd->sandbox2[0] &= mask;
		}
		len = busWidth == 8 ? 2 : 1;
		for (i = 0; i < len; i++) {
			if ((pSd->sandbox1[i] ^ pSd->sandbox2[i]) != mask)
				break;
		}
		if (i == len)
			break;
	}
	return busWidth;
}


uint8_t MmcGetExtInformation(SdmmcDriver *driver)
{
	sSdCard *pSd = &driver->card;
	/* MMC 4.0 Higher version */
	if (SD_CSD_STRUCTURE(pSd->CSD) >= 2 && MMC_IsVer4(pSd))
		return MmcCmd8(driver);
	else
		return SDMMC_NOT_SUPPORTED;
}


uint8_t MmcInit(SdmmcDriver *driver)
{
	MmcCmd6Arg sw_arg = {
		.access = 0x3,   /* Write byte in the EXT_CSD register */
	};
	uint64_t mem_size;
	uint32_t freq, drv_err, status;
	uint8_t error, tim_mode, pwr_class, width;
	bool flag;

	tim_mode = driver->card.bSpeedMode = SDMMC_TIM_MMC_BC;
	/* The host then issues the command ALL_SEND_CID (CMD2) to the card to get
	 * its unique card identification (CID) number.
	 * Card that is unidentified (i.e. which is in Ready State) sends its CID
	 * number as the response (on the CMD line). */
	error = Cmd2(driver);
	if (error)
		return error;

	/* Thereafter, the host issues SET_RELATIVE_ADDR (CMD3) to assign the
	 * device a dedicated relative card address (RCA), which is shorter than
	 * CID and which is used to address the card in the future data transfer
	 * mode. Once the RCA is received the card state changes to the Stand-by
	 * State. */
	error = Cmd3(driver);
	if (error)
		return error;
	//else
		TRACE_1("RCA=%u\n\r", driver->card.wAddress);

	/* SEND_CSD (CMD9) to obtain the Card Specific Data (CSD register),
	 * e.g. block length, card storage capacity, etc... */
	error = Cmd9(driver);
	if (error)
		return error;

	/* Calculate transfer speed */
	freq = SdmmcGetMaxFreq(driver);

	error = HwSetClock(driver, &freq);

	if (error != SDMMC_OK && error != SDMMC_CHANGED)
		return error;

	driver->card.dwCurrSpeed = freq;

	/* Now select the card, to TRAN state */
	error = SdMmcSelect(driver, driver->card.wAddress, 0);
	if (error)
		return error;

	/* If CSD:SPEC_VERS indicates v4.0 or higher, read EXT_CSD */
	error = MmcGetExtInformation(driver);
	/* Consider HS200 timing mode */
	if (error == SDMMC_OK && MMC_EXT_EXT_CSD_REV(driver->card.EXT) >= 6
	    && MMC_IsCSDVer1_2((&driver->card)) && MMC_EXT_CARD_TYPE(driver->card.EXT) & 0x10
	    && HwIsTimingSupported(driver, SDMMC_TIM_MMC_HS200))
		tim_mode = SDMMC_TIM_MMC_HS200;
	/* Consider High Speed DDR timing mode */
	else if (error == SDMMC_OK && MMC_EXT_EXT_CSD_REV(driver->card.EXT) >= 4
	    && MMC_IsCSDVer1_2((&driver->card)) && MMC_EXT_CARD_TYPE(driver->card.EXT) & 0x4
	    && HwIsTimingSupported(driver, SDMMC_TIM_MMC_HS_DDR))
		tim_mode = SDMMC_TIM_MMC_HS_DDR;
	/* Consider High Speed SDR timing mode */
	else if (error == SDMMC_OK
	    && MMC_IsCSDVer1_2((&driver->card)) && MMC_EXT_CARD_TYPE(driver->card.EXT) & 0x1
	    && HwIsTimingSupported(driver, SDMMC_TIM_MMC_HS_SDR))
		tim_mode = SDMMC_TIM_MMC_HS_SDR;
	/* Check power requirements of the device */
	if (error == SDMMC_OK) {
		if (tim_mode == SDMMC_TIM_MMC_HS200)
			pwr_class = MMC_EXT_PWR_CL_200_195(driver->card.EXT);
		else if (tim_mode == SDMMC_TIM_MMC_HS_DDR)
			pwr_class = MMC_EXT_PWR_CL_DDR_52_360(driver->card.EXT);
		else if (tim_mode == SDMMC_TIM_MMC_HS_SDR)
			pwr_class = MMC_EXT_PWR_CL_52_360(driver->card.EXT);
		else
			pwr_class = MMC_EXT_PWR_CL_26_360(driver->card.EXT);

		if (pwr_class != 0) {
			sw_arg.index = MMC_EXT_POWER_CLASS_I;
			sw_arg.value = 0xf;
			error = MmcCmd6(driver, &sw_arg, &status);
			if (error) {
				TRACE_1("Pwr class %s\n\r",
				    SD_StringifyRetCode(error));
		}
		}
	}

	/* Enable High Speed SDR timing mode */
	if (tim_mode == SDMMC_TIM_MMC_HS_SDR || tim_mode == SDMMC_TIM_MMC_HS_DDR) {

		sw_arg.index = MMC_EXT_HS_TIMING_I;
		sw_arg.value = MMC_EXT_HS_TIMING_EN;

		error = MmcCmd6(driver, &sw_arg, &status);

		if (error == SDMMC_OK)
			error = HwSetHsMode(driver, SDMMC_TIM_MMC_HS_SDR);
		if (error == SDMMC_OK)
			error = Cmd13(driver, &status);
		if (error == SDMMC_OK && (status & ~STATUS_STATE
		    & ~STATUS_READY_FOR_DATA
		    || (status & STATUS_STATE) != STATUS_TRAN))
			error = SDMMC_STATE;
		if (error == SDMMC_OK) {
			driver->card.bSpeedMode = SDMMC_TIM_MMC_HS_SDR;
			freq = SdmmcGetMaxFreq(driver);
			error = HwSetClock(driver, &freq);
			driver->card.dwCurrSpeed = freq;
			error = error == SDMMC_CHANGED ? SDMMC_OK : error;
		}
		if (error != SDMMC_OK) {
			TRACE_ERROR_1("HS %s\n\r", SD_StringifyRetCode(error));
			return error;
		}
	}

	/* Consider using the widest supported data bus */
	if (MMC_IsCSDVer1_2((&driver->card)) && MMC_IsVer4((&driver->card))) {

		width = mmcDetectBuswidth(driver);

		if (width > 1) {

			error = mmcSelectBuswidth(driver, width,tim_mode == SDMMC_TIM_MMC_HS_DDR);

			if (error == SDMMC_OK)
				error = HwSetBusWidth(driver, width);

			if (error == SDMMC_OK)
				driver->card.bBusMode = width;

			if (error == SDMMC_OK && tim_mode == SDMMC_TIM_MMC_HS_DDR)
				/* Switch to High Speed DDR timing mode */
				error = HwSetHsMode(driver, tim_mode);
			if (error == SDMMC_OK)
				error = Cmd13(driver, &status);
			if (error == SDMMC_OK && (status & ~STATUS_STATE
			    & ~STATUS_READY_FOR_DATA
			    || (status & STATUS_STATE) != STATUS_TRAN))
				error = SDMMC_STATE;
			if (error == SDMMC_OK
			    && tim_mode == SDMMC_TIM_MMC_HS_DDR)
				driver->card.bSpeedMode = tim_mode;
			else if (error) {
				TRACE_ERROR_1("Width/DDR %s\n\r",
				    SD_StringifyRetCode(error));
				return error;
			}
		}
	}

	/* Enable HS200 timing mode */
	if (tim_mode == SDMMC_TIM_MMC_HS200 && driver->card.bBusMode > 1) {
		sw_arg.index = MMC_EXT_HS_TIMING_I;
		/* Select device output Driver Type-0, i.e. 50 ohm nominal
		 * output impedance.
		 * TODO select the optimal device output Driver Type.
		 * That depends on board design. Use an oscilloscope to observe
		 * signal integrity, and among the driver types that meet rise
		 * and fall time requirements, select the weakest. */
		sw_arg.value = MMC_EXT_HS_TIMING_HS200 | MMC_EXT_HS_TIMING_50R;
		error = MmcCmd6(driver, &sw_arg, &status);
		if (error == SDMMC_OK)
			error = HwSetHsMode(driver, tim_mode);
		if (error == SDMMC_OK) {
			error = Cmd13(driver, &status);
			if (error == SDMMC_OK && (status & ~STATUS_STATE
			    & ~STATUS_READY_FOR_DATA
			    || (status & STATUS_STATE) != STATUS_TRAN))
				error = SDMMC_STATE;
		}
		if (error == SDMMC_OK) {
			driver->card.bSpeedMode = tim_mode;
			freq = SdmmcGetMaxFreq(driver);
			error = HwSetClock(driver, &freq);
			driver->card.dwCurrSpeed = freq;
			error = error == SDMMC_CHANGED ? SDMMC_OK : error;
		}
		if (error != SDMMC_OK) {
			TRACE_ERROR_1("HS200 %s\n\r", SD_StringifyRetCode(error));
			return error;
		}
	}

	/* Update card information since status changed */
	flag = driver->card.bSpeedMode >= SDMMC_TIM_MMC_HS_SDR;
	if (flag || driver->card.bBusMode > 1)
		SdMmcUpdateInformation(driver, flag, true);

	/* MMC devices have the SET_BLOCK_COUNT command part of both the
	 * block-oriented read and the block-oriented write commands,
	 * i.e. class 2 and class 4 commands.
	 * FIXME we should normally check CSD.CCC before issuing any MMC block-
	 * oriented read/write command. */
	driver->card.bSetBlkCnt = 1;
	/* Ask the driver to implicitly send the SET_BLOCK_COUNT command,
	 * immediately before every READ_MULTIPLE_BLOCK and WRITE_MULTIPLE_BLOCK
	 * command. */
	driver->control_param =  driver->card.bSetBlkCnt;
	drv_err = sdmmc_device_control(driver,SDMMC_IOCTL_SET_LENPREFIX);

	/* In case the driver does not support this function, we'll take it in
	 * charge. */
	if (driver->card.bSetBlkCnt && drv_err == SDMMC_OK && driver->control_param)
		driver->card.bSetBlkCnt = 0;

	driver->card.wCurrBlockLen = SDMMC_BLOCK_SIZE;

	if (MMC_IsCSDVer1_2((&driver->card)) && MMC_IsVer4((&driver->card))) {
		/* Get size from EXT_CSD */
		if (MMC_EXT_DATA_SECTOR_SIZE(driver->card.EXT)
		    == MMC_EXT_DATA_SECT_4KIB)
			driver->card.wBlockSize = 4096;
		else
			driver->card.wBlockSize = 512;
		driver->card.dwNbBlocks = MMC_EXT_SEC_COUNT(driver->card.EXT)
		    / (driver->card.wBlockSize / 512UL);
		/* Device density >= 4 GiB does not fit 32-bit dwTotalSize */
		driver->card.dwTotalSize = MMC_EXT_SEC_COUNT(driver->card.EXT);
		if (driver->card.dwTotalSize >= 0x800000)
			driver->card.dwTotalSize = 0xFFFFFFFF;
		else
			driver->card.dwTotalSize *= 512UL;
	}
	else {
		driver->card.wBlockSize = 512;
		mem_size = SD_CSD_TOTAL_SIZE(driver->card.CSD);
		driver->card.dwNbBlocks = (uint32_t)(mem_size >> 9);
		driver->card.dwTotalSize = mem_size >> 32 ? 0xFFFFFFFF
		    : (uint32_t)mem_size;
	}

	/* Check device status and eat past exceptions, which would otherwise
	 * prevent upcoming data transaction routines from reliably checking
	 * fresh exceptions. */
	error = Cmd13(driver, &status);
	if (error)
		return error;
	status = status & ~STATUS_STATE & ~STATUS_READY_FOR_DATA & ~STATUS_APP_CMD;
	if (status) {
		TRACE_WARNING_1("st %lx\n\r", status);
	}

	return SDMMC_OK;
}
#endif
#endif