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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Secure Digital (SD) Host Controller interface DMA support code
*/
#include <commonlib/sdhci.h>
#include <commonlib/storage.h>
#include <console/console.h>
#include <delay.h>
#include <endian.h>
#include <stdlib.h>
#include <string.h>
#include "sdhci.h"
#include "sd_mmc.h"
#include "storage.h"
static void sdhci_alloc_adma_descs(struct sdhci_ctrlr *sdhci_ctrlr,
u32 need_descriptors)
{
if (sdhci_ctrlr->adma_descs) {
if (sdhci_ctrlr->adma_desc_count < need_descriptors) {
/* Previously allocated array is too small */
free(sdhci_ctrlr->adma_descs);
sdhci_ctrlr->adma_desc_count = 0;
sdhci_ctrlr->adma_descs = NULL;
}
}
/* use dma_malloc() to make sure we get the coherent/uncached memory */
if (!sdhci_ctrlr->adma_descs) {
sdhci_ctrlr->adma_descs = malloc(need_descriptors
* sizeof(*sdhci_ctrlr->adma_descs));
if (sdhci_ctrlr->adma_descs == NULL)
die("fail to malloc adma_descs\n");
sdhci_ctrlr->adma_desc_count = need_descriptors;
}
memset(sdhci_ctrlr->adma_descs, 0, sizeof(*sdhci_ctrlr->adma_descs)
* need_descriptors);
}
static void sdhci_alloc_adma64_descs(struct sdhci_ctrlr *sdhci_ctrlr,
u32 need_descriptors)
{
if (sdhci_ctrlr->adma64_descs) {
if (sdhci_ctrlr->adma_desc_count < need_descriptors) {
/* Previously allocated array is too small */
free(sdhci_ctrlr->adma64_descs);
sdhci_ctrlr->adma_desc_count = 0;
sdhci_ctrlr->adma64_descs = NULL;
}
}
/* use dma_malloc() to make sure we get the coherent/uncached memory */
if (!sdhci_ctrlr->adma64_descs) {
sdhci_ctrlr->adma64_descs = malloc(need_descriptors
* sizeof(*sdhci_ctrlr->adma64_descs));
if (sdhci_ctrlr->adma64_descs == NULL)
die("fail to malloc adma64_descs\n");
sdhci_ctrlr->adma_desc_count = need_descriptors;
}
memset(sdhci_ctrlr->adma64_descs, 0, sizeof(*sdhci_ctrlr->adma64_descs)
* need_descriptors);
}
int sdhci_setup_adma(struct sdhci_ctrlr *sdhci_ctrlr, struct mmc_data *data)
{
int i, togo, need_descriptors;
int dma64;
char *buffer_data;
u16 attributes;
togo = data->blocks * data->blocksize;
if (!togo) {
sdhc_error("%s: MmcData corrupted: %d blocks of %d bytes\n",
__func__, data->blocks, data->blocksize);
return -1;
}
need_descriptors = 1 + togo / SDHCI_MAX_PER_DESCRIPTOR;
dma64 = sdhci_ctrlr->sd_mmc_ctrlr.caps & DRVR_CAP_DMA_64BIT;
if (dma64)
sdhci_alloc_adma64_descs(sdhci_ctrlr, need_descriptors);
else
sdhci_alloc_adma_descs(sdhci_ctrlr, need_descriptors);
buffer_data = data->dest;
/* Now set up the descriptor chain. */
for (i = 0; togo; i++) {
unsigned int desc_length;
if (togo < SDHCI_MAX_PER_DESCRIPTOR)
desc_length = togo;
else
desc_length = SDHCI_MAX_PER_DESCRIPTOR;
togo -= desc_length;
attributes = SDHCI_ADMA_VALID | SDHCI_ACT_TRAN;
if (togo == 0)
attributes |= SDHCI_ADMA_END;
if (dma64) {
sdhci_ctrlr->adma64_descs[i].addr =
(uintptr_t)buffer_data;
sdhci_ctrlr->adma64_descs[i].addr_hi = 0;
sdhci_ctrlr->adma64_descs[i].length = desc_length;
sdhci_ctrlr->adma64_descs[i].attributes = attributes;
} else {
sdhci_ctrlr->adma_descs[i].addr =
(uintptr_t)buffer_data;
sdhci_ctrlr->adma_descs[i].length = desc_length;
sdhci_ctrlr->adma_descs[i].attributes = attributes;
}
buffer_data += desc_length;
}
if (dma64)
sdhci_writel(sdhci_ctrlr, (uintptr_t)sdhci_ctrlr->adma64_descs,
SDHCI_ADMA_ADDRESS);
else
sdhci_writel(sdhci_ctrlr, (uintptr_t)sdhci_ctrlr->adma_descs,
SDHCI_ADMA_ADDRESS);
return 0;
}
int sdhci_complete_adma(struct sdhci_ctrlr *sdhci_ctrlr,
struct mmc_command *cmd)
{
int retry;
u32 stat = 0, mask;
mask = SDHCI_INT_RESPONSE | SDHCI_INT_ERROR;
retry = 10000; /* Command should be done in way less than 10 ms. */
while (--retry) {
stat = sdhci_readl(sdhci_ctrlr, SDHCI_INT_STATUS);
if (stat & mask)
break;
udelay(1);
}
sdhci_writel(sdhci_ctrlr, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
if (retry && !(stat & SDHCI_INT_ERROR)) {
/* Command OK, let's wait for data transfer completion. */
mask = SDHCI_INT_DATA_END |
SDHCI_INT_ERROR | SDHCI_INT_ADMA_ERROR;
/* Transfer should take 10 seconds tops. */
retry = 10 * 1000 * 1000;
while (--retry) {
stat = sdhci_readl(sdhci_ctrlr, SDHCI_INT_STATUS);
if (stat & mask)
break;
udelay(1);
}
sdhci_writel(sdhci_ctrlr, stat, SDHCI_INT_STATUS);
if (retry && !(stat & SDHCI_INT_ERROR)) {
sdhci_cmd_done(sdhci_ctrlr, cmd);
return 0;
}
}
sdhc_error("%s: transfer error, stat %#x, adma error %#x, retry %d\n",
__func__, stat, sdhci_readl(sdhci_ctrlr, SDHCI_ADMA_ERROR),
retry);
sdhci_reset(sdhci_ctrlr, SDHCI_RESET_CMD);
sdhci_reset(sdhci_ctrlr, SDHCI_RESET_DATA);
if (stat & SDHCI_INT_TIMEOUT)
return CARD_TIMEOUT;
return CARD_COMM_ERR;
}
|