1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <config.h>
22#include <common.h>
23#include <command.h>
24#include <mmc.h>
25#include <part.h>
26#include <malloc.h>
27#include <asm/io.h>
28#include <asm/arch/sdmmc_defs.h>
29
30#define DAVINCI_MAX_BLOCKS (32)
31#define WATCHDOG_COUNT (100000)
32
33#define get_val(addr) REG(addr)
34#define set_val(addr, val) REG(addr) = (val)
35#define set_bit(addr, val) set_val((addr), (get_val(addr) | (val)))
36#define clear_bit(addr, val) set_val((addr), (get_val(addr) & ~(val)))
37
38
39static void dmmc_set_clock(struct mmc *mmc, uint clock)
40{
41 struct davinci_mmc *host = mmc->priv;
42 struct davinci_mmc_regs *regs = host->reg_base;
43 uint clkrt, sysclk2, act_clock;
44
45 if (clock < mmc->f_min)
46 clock = mmc->f_min;
47 if (clock > mmc->f_max)
48 clock = mmc->f_max;
49
50 set_val(®s->mmcclk, 0);
51 sysclk2 = host->input_clk;
52 clkrt = (sysclk2 / (2 * clock)) - 1;
53
54
55 act_clock = (sysclk2 / (2 * (clkrt + 1)));
56
57
58 if (act_clock > clock)
59 clkrt++;
60
61
62 if (clkrt > 0xFF)
63 clkrt = 0xFF;
64
65 set_val(®s->mmcclk, (clkrt | MMCCLK_CLKEN));
66}
67
68
69static int
70dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
71{
72 uint wdog = WATCHDOG_COUNT;
73
74 while (--wdog && ((get_val(®s->mmcst1) & status) != status))
75 udelay(10);
76
77 if (!(get_val(®s->mmcctl) & MMCCTL_WIDTH_4_BIT))
78 udelay(100);
79
80 if (wdog == 0)
81 return COMM_ERR;
82
83 return 0;
84}
85
86
87static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
88{
89 uint wdog = WATCHDOG_COUNT;
90
91 while (--wdog && (get_val(®s->mmcst1) & MMCST1_BUSY))
92 udelay(10);
93
94 if (wdog == 0)
95 return COMM_ERR;
96
97 return 0;
98}
99
100
101static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
102 uint *cur_st, uint st_ready, uint st_error)
103{
104 uint wdog = WATCHDOG_COUNT;
105 uint mmcstatus = *cur_st;
106
107 while (wdog--) {
108 if (mmcstatus & st_ready) {
109 *cur_st = mmcstatus;
110 mmcstatus = get_val(®s->mmcst1);
111 return 0;
112 } else if (mmcstatus & st_error) {
113 if (mmcstatus & MMCST0_TOUTRS)
114 return TIMEOUT;
115 printf("[ ST0 ERROR %x]\n", mmcstatus);
116
117
118
119
120 if (mmcstatus & MMCST0_CRCRS)
121 return 0;
122 return COMM_ERR;
123 }
124 udelay(10);
125
126 mmcstatus = get_val(®s->mmcst0);
127 }
128
129 printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
130 get_val(®s->mmcst1));
131 return COMM_ERR;
132}
133
134
135
136
137
138static int
139dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
140{
141 struct davinci_mmc *host = mmc->priv;
142 volatile struct davinci_mmc_regs *regs = host->reg_base;
143 uint mmcstatus, status_rdy, status_err;
144 uint i, cmddata, bytes_left = 0;
145 int fifo_words, fifo_bytes, err;
146 char *data_buf = NULL;
147
148
149 mmcstatus = get_val(®s->mmcst0);
150 fifo_words = (host->version == MMC_CTLR_VERSION_2) ? 16 : 8;
151 fifo_bytes = fifo_words << 2;
152
153
154 dmmc_busy_wait(regs);
155
156 cmddata = cmd->cmdidx;
157 cmddata |= MMCCMD_PPLEN;
158
159
160 if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
161 cmddata |= MMCCMD_INITCK;
162
163 switch (cmd->resp_type) {
164 case MMC_RSP_R1b:
165 cmddata |= MMCCMD_BSYEXP;
166
167 case MMC_RSP_R1:
168 cmddata |= MMCCMD_RSPFMT_R1567;
169 break;
170 case MMC_RSP_R2:
171 cmddata |= MMCCMD_RSPFMT_R2;
172 break;
173 case MMC_RSP_R3:
174 cmddata |= MMCCMD_RSPFMT_R3;
175 break;
176 }
177
178 set_val(®s->mmcim, 0);
179
180 if (data) {
181
182 bytes_left = (data->blocksize * data->blocks);
183
184
185 set_val(®s->mmcfifoctl,
186 (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
187
188 if (host->version == MMC_CTLR_VERSION_2)
189 cmddata |= MMCCMD_DMATRIG;
190
191 cmddata |= MMCCMD_WDATX;
192 if (data->flags == MMC_DATA_READ) {
193 set_val(®s->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
194 } else if (data->flags == MMC_DATA_WRITE) {
195 set_val(®s->mmcfifoctl,
196 (MMCFIFOCTL_FIFOLEV |
197 MMCFIFOCTL_FIFODIR));
198 cmddata |= MMCCMD_DTRW;
199 }
200
201 set_val(®s->mmctod, 0xFFFF);
202 set_val(®s->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
203 set_val(®s->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
204
205 if (data->flags == MMC_DATA_WRITE) {
206 uint val;
207 data_buf = (char *)data->src;
208
209 for (i = 0; (i < fifo_words) && bytes_left; i++) {
210 memcpy((char *)&val, data_buf, 4);
211 set_val(®s->mmcdxr, val);
212 data_buf += 4;
213 bytes_left -= 4;
214 }
215 }
216 } else {
217 set_val(®s->mmcblen, 0);
218 set_val(®s->mmcnblk, 0);
219 }
220
221 set_val(®s->mmctor, 0x1FFF);
222
223
224 set_val(®s->mmcarghl, cmd->cmdarg);
225 set_val(®s->mmccmd, cmddata);
226
227 status_rdy = MMCST0_RSPDNE;
228 status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
229 MMCST0_CRCWR | MMCST0_CRCRD);
230 if (cmd->resp_type & MMC_RSP_CRC)
231 status_err |= MMCST0_CRCRS;
232
233 mmcstatus = get_val(®s->mmcst0);
234 err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
235 if (err)
236 return err;
237
238
239 if (cmd->resp_type == MMC_RSP_R1b)
240 dmmc_busy_wait(regs);
241
242
243 if (mmcstatus & MMCST0_RSPDNE) {
244
245 if (cmd->resp_type & MMC_RSP_136) {
246 cmd->response[0] = get_val(®s->mmcrsp67);
247 cmd->response[1] = get_val(®s->mmcrsp45);
248 cmd->response[2] = get_val(®s->mmcrsp23);
249 cmd->response[3] = get_val(®s->mmcrsp01);
250 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
251 cmd->response[0] = get_val(®s->mmcrsp67);
252 }
253 }
254
255 if (data == NULL)
256 return 0;
257
258 if (data->flags == MMC_DATA_READ) {
259
260
261
262
263 status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
264 status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
265 data_buf = data->dest;
266 } else {
267 status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
268 status_err = MMCST0_CRCWR;
269 }
270
271
272 while (bytes_left) {
273 err = dmmc_check_status(regs, &mmcstatus, status_rdy,
274 status_err);
275 if (err)
276 return err;
277
278 if (data->flags == MMC_DATA_READ) {
279
280
281
282
283
284
285
286 if (bytes_left > fifo_bytes)
287 dmmc_wait_fifo_status(regs, 0x4a);
288 else if (bytes_left == fifo_bytes)
289 dmmc_wait_fifo_status(regs, 0x40);
290
291 for (i = 0; bytes_left && (i < fifo_words); i++) {
292 cmddata = get_val(®s->mmcdrr);
293 memcpy(data_buf, (char *)&cmddata, 4);
294 data_buf += 4;
295 bytes_left -= 4;
296 }
297 } else {
298
299
300
301
302
303
304
305 dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
306 for (i = 0; bytes_left && (i < fifo_words); i++) {
307 memcpy((char *)&cmddata, data_buf, 4);
308 set_val(®s->mmcdxr, cmddata);
309 data_buf += 4;
310 bytes_left -= 4;
311 }
312 dmmc_busy_wait(regs);
313 }
314 }
315
316 err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
317 if (err)
318 return err;
319
320 return 0;
321}
322
323
324static int dmmc_init(struct mmc *mmc)
325{
326 struct davinci_mmc *host = mmc->priv;
327 struct davinci_mmc_regs *regs = host->reg_base;
328
329
330
331
332
333 get_val(®s->mmcst0);
334 get_val(®s->mmcst1);
335
336
337 set_bit(®s->mmcctl, MMCCTL_DATRST);
338 set_bit(®s->mmcctl, MMCCTL_CMDRST);
339 udelay(10);
340
341 set_val(®s->mmcclk, 0x0);
342 set_val(®s->mmctor, 0x1FFF);
343 set_val(®s->mmctod, 0xFFFF);
344
345
346 clear_bit(®s->mmcctl, MMCCTL_DATRST);
347 clear_bit(®s->mmcctl, MMCCTL_CMDRST);
348
349 udelay(10);
350
351
352 set_val(®s->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
353 set_val(®s->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
354
355 return 0;
356}
357
358
359static void dmmc_set_ios(struct mmc *mmc)
360{
361 struct davinci_mmc *host = mmc->priv;
362 struct davinci_mmc_regs *regs = host->reg_base;
363
364
365 if (mmc->bus_width == 4)
366 set_bit(®s->mmcctl, MMCCTL_WIDTH_4_BIT);
367 else
368 clear_bit(®s->mmcctl, MMCCTL_WIDTH_4_BIT);
369
370
371 if (mmc->clock)
372 dmmc_set_clock(mmc, mmc->clock);
373}
374
375
376
377
378int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
379{
380 struct mmc *mmc;
381
382 mmc = malloc(sizeof(struct mmc));
383 memset(mmc, 0, sizeof(struct mmc));
384
385 sprintf(mmc->name, "davinci");
386 mmc->priv = host;
387 mmc->send_cmd = dmmc_send_cmd;
388 mmc->set_ios = dmmc_set_ios;
389 mmc->init = dmmc_init;
390 mmc->getcd = NULL;
391
392 mmc->f_min = 200000;
393 mmc->f_max = 25000000;
394 mmc->voltages = host->voltages;
395 mmc->host_caps = host->host_caps;
396
397 mmc->b_max = DAVINCI_MAX_BLOCKS;
398
399 mmc_register(mmc);
400
401 return 0;
402}
403