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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
/* $OpenBSD: dwmmc.c,v 1.8 2018/04/07 15:05:25 kettenis Exp $ */
/*
* Copyright (c) 2017 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <machine/intr.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/fdt.h>
#include <dev/sdmmc/sdmmcvar.h>
#define SDMMC_CTRL 0x0000
#define SDMMC_CTRL_USE_INTERNAL_DMAC (1 << 25)
#define SDMMC_CTRL_DMA_RESET (1 << 2)
#define SDMMC_CTRL_FIFO_RESET (1 << 1)
#define SDMMC_CTRL_CONTROLLER_RESET (1 << 0)
#define SDMMC_CTRL_ALL_RESET (SDMMC_CTRL_CONTROLLER_RESET | \
SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET)
#define SDMMC_PWREN 0x0004
#define SDMMC_CLKDIV 0x0008
#define SDMMC_CLKSRC 0x000c
#define SDMMC_CLKENA 0x0010
#define SDMMC_CLKENA_CCLK_LOW_POWER (1 << 16)
#define SDMMC_CLKENA_CCLK_ENABLE (1 << 0)
#define SDMMC_TMOUT 0x0014
#define SDMMC_CTYPE 0x0018
#define SDMMC_CTYPE_8BIT (1 << 16)
#define SDMMC_CTYPE_4BIT (1 << 0)
#define SDMMC_BLKSIZ 0x001c
#define SDMMC_BYTCNT 0x0020
#define SDMMC_INTMASK 0x0024
#define SDMMC_CMDARG 0x0028
#define SDMMC_CMD 0x002c
#define SDMMC_CMD_START_CMD (1U << 31)
#define SDMMC_CMD_USE_HOLD_REG (1 << 29)
#define SDMMC_CMD_UPDATE_CLOCK_REGISTERS_ONLY (1 << 21)
#define SDMMC_CMD_SEND_INITIALIZATION (1 << 15)
#define SDMMC_CMD_STOP_ABORT_CMD (1 << 14)
#define SDMMC_CMD_WAIT_PRVDATA_COMPLETE (1 << 13)
#define SDMMC_CMD_SEND_AUTO_STOP (1 << 12)
#define SDMMC_CMD_WR (1 << 10)
#define SDMMC_CMD_DATA_EXPECTED (1 << 9)
#define SDMMC_CMD_CHECK_REPONSE_CRC (1 << 8)
#define SDMMC_CMD_RESPONSE_LENGTH (1 << 7)
#define SDMMC_CMD_RESPONSE_EXPECT (1 << 6)
#define SDMMC_RESP0 0x0030
#define SDMMC_RESP1 0x0034
#define SDMMC_RESP2 0x0038
#define SDMMC_RESP3 0x003c
#define SDMMC_MINTSTS 0x0040
#define SDMMC_RINTSTS 0x0044
#define SDMMC_RINTSTS_EBE (1 << 15)
#define SDMMC_RINTSTS_ACD (1 << 14)
#define SDMMC_RINTSTS_SBE (1 << 13)
#define SDMMC_RINTSTS_HLE (1 << 12)
#define SDMMC_RINTSTS_FRUN (1 << 11)
#define SDMMC_RINTSTS_HTO (1 << 10)
#define SDMMC_RINTSTS_DRTO (1 << 9)
#define SDMMC_RINTSTS_RTO (1 << 8)
#define SDMMC_RINTSTS_DCRC (1 << 7)
#define SDMMC_RINTSTS_RCRC (1 << 6)
#define SDMMC_RINTSTS_RXDR (1 << 5)
#define SDMMC_RINTSTS_TXDR (1 << 4)
#define SDMMC_RINTSTS_DTO (1 << 3)
#define SDMMC_RINTSTS_CD (1 << 2)
#define SDMMC_RINTSTS_RE (1 << 1)
#define SDMMC_RINTSTS_CDT (1 << 0)
#define SDMMC_RINTSTS_DATA_ERR (SDMMC_RINTSTS_EBE | SDMMC_RINTSTS_SBE | \
SDMMC_RINTSTS_HLE | SDMMC_RINTSTS_FRUN | SDMMC_RINTSTS_DCRC)
#define SDMMC_RINTSTS_DATA_TO (SDMMC_RINTSTS_HTO | SDMMC_RINTSTS_DRTO)
#define SDMMC_STATUS 0x0048
#define SDMMC_STATUS_FIFO_COUNT(x) (((x) >> 17) & 0x1fff)
#define SDMMC_STATUS_DATA_BUSY (1 << 9)
#define SDMMC_FIFOTH 0x004c
#define SDMMC_CDETECT 0x0050
#define SDMMC_CDETECT_CARD_DETECT_0 (1 << 0)
#define SDMMC_WRTPRT 0x0054
#define SDMMC_TCBCNT 0x005c
#define SDMMC_TBBCNT 0x0060
#define SDMMC_DEBNCE 0x0064
#define SDMMC_USRID 0x0068
#define SDMMC_VERID 0x006c
#define SDMMC_HCON 0x0070
#define SDMMC_HCON_DATA_WIDTH(x) (((x) >> 7) & 0x7)
#define SDMMC_UHS_REG 0x0074
#define SDMMC_RST_n 0x0078
#define SDMMC_BMOD 0x0080
#define SDMMC_PLDMND 0x0084
#define SDMMC_DBADDR 0x0088
#define SDMMC_IDSTS 0x008c
#define SDMMC_IDINTEN 0x0090
#define SDMMC_DSCADDR 0x0094
#define SDMMC_BUFADDR 0x0098
#define SDMMC_CLKSEL 0x009c
#define SDMMC_CARDTHRCTL 0x0100
#define SDMMC_CARDTHRCTL_RDTHR_SHIFT 16
#define SDMMC_CARDTHRCTL_RDTHREN (1 << 0)
#define SDMMC_BACK_END_POWER 0x0104
#define SDMMC_EMMC_DDR_REG 0x0108
#define SDMMC_FIFO_BASE 0x0200
#define HREAD4(sc, reg) \
(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val) \
bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
struct dwmmc_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
bus_size_t sc_size;
int sc_node;
void *sc_ih;
uint32_t sc_clkbase;
uint32_t sc_fifo_depth;
uint32_t sc_fifo_width;
void (*sc_read_data)(struct dwmmc_softc *, u_char *, int);
void (*sc_write_data)(struct dwmmc_softc *, u_char *, int);
uint32_t sc_gpio[4];
struct device *sc_sdmmc;
};
int dwmmc_match(struct device *, void *, void *);
void dwmmc_attach(struct device *, struct device *, void *);
struct cfattach dwmmc_ca = {
sizeof(struct dwmmc_softc), dwmmc_match, dwmmc_attach
};
struct cfdriver dwmmc_cd = {
NULL, "dwmmc", DV_DULL
};
int dwmmc_intr(void *);
int dwmmc_host_reset(sdmmc_chipset_handle_t);
uint32_t dwmmc_host_ocr(sdmmc_chipset_handle_t);
int dwmmc_host_maxblklen(sdmmc_chipset_handle_t);
int dwmmc_card_detect(sdmmc_chipset_handle_t);
int dwmmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
int dwmmc_bus_clock(sdmmc_chipset_handle_t, int, int);
int dwmmc_bus_width(sdmmc_chipset_handle_t, int);
void dwmmc_exec_command(sdmmc_chipset_handle_t, struct sdmmc_command *);
struct sdmmc_chip_functions dwmmc_chip_functions = {
.host_reset = dwmmc_host_reset,
.host_ocr = dwmmc_host_ocr,
.host_maxblklen = dwmmc_host_maxblklen,
.card_detect = dwmmc_card_detect,
.bus_power = dwmmc_bus_power,
.bus_clock = dwmmc_bus_clock,
.bus_width = dwmmc_bus_width,
.exec_command = dwmmc_exec_command,
};
void dwmmc_transfer_data(struct dwmmc_softc *, struct sdmmc_command *);
void dwmmc_read_data32(struct dwmmc_softc *, u_char *, int);
void dwmmc_write_data32(struct dwmmc_softc *, u_char *, int);
void dwmmc_read_data64(struct dwmmc_softc *, u_char *, int);
void dwmmc_write_data64(struct dwmmc_softc *, u_char *, int);
int
dwmmc_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return (OF_is_compatible(faa->fa_node, "rockchip,rk3288-dw-mshc") ||
OF_is_compatible(faa->fa_node, "samsung,exynos5420-dw-mshc"));
}
void
dwmmc_attach(struct device *parent, struct device *self, void *aux)
{
struct dwmmc_softc *sc = (struct dwmmc_softc *)self;
struct fdt_attach_args *faa = aux;
struct sdmmcbus_attach_args saa;
uint32_t hcon, ciu_div, width;
int timeout;
if (faa->fa_nreg < 1) {
printf(": no registers\n");
return;
}
sc->sc_node = faa->fa_node;
sc->sc_iot = faa->fa_iot;
sc->sc_size = faa->fa_reg[0].size;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
printf(": can't map registers\n");
return;
}
pinctrl_byname(faa->fa_node, "default");
clock_enable_all(faa->fa_node);
reset_deassert_all(faa->fa_node);
/*
* Determine FIFO width from hardware configuration register.
* We only support 32-bit and 64-bit FIFOs.
*/
hcon = HREAD4(sc, SDMMC_HCON);
switch (SDMMC_HCON_DATA_WIDTH(hcon)) {
case 1:
sc->sc_fifo_width = 4;
sc->sc_read_data = dwmmc_read_data32;
sc->sc_write_data = dwmmc_write_data32;
break;
case 2:
sc->sc_fifo_width = 8;
sc->sc_read_data = dwmmc_read_data64;
sc->sc_write_data = dwmmc_write_data64;
break;
default:
printf(": unsupported FIFO width\n");
return;
}
sc->sc_fifo_depth = OF_getpropint(faa->fa_node, "fifo-depth", 0);
if (sc->sc_fifo_depth == 0) {
printf(": unsupported FIFO depth\n");
return;
}
sc->sc_clkbase = clock_get_frequency(faa->fa_node, "ciu");
ciu_div = OF_getpropint(faa->fa_node, "samsung,dw-mshc-ciu-div", 0);
sc->sc_clkbase /= (ciu_div + 1);
sc->sc_ih = arm_intr_establish_fdt(faa->fa_node, IPL_BIO,
dwmmc_intr, sc, sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": can't establish interrupt\n");
goto unmap;
}
OF_getpropintarray(faa->fa_node, "cd-gpios", sc->sc_gpio,
sizeof(sc->sc_gpio));
if (sc->sc_gpio[0])
gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_INPUT);
printf(": %d MHz base clock\n", sc->sc_clkbase / 1000000);
HSET4(sc, SDMMC_CTRL, SDMMC_CTRL_ALL_RESET);
for (timeout = 5000; timeout > 0; timeout--) {
if ((HREAD4(sc, SDMMC_CTRL) & SDMMC_CTRL_ALL_RESET) == 0)
break;
delay(100);
}
if (timeout == 0)
printf("%s: reset failed\n", sc->sc_dev.dv_xname);
/* We don't do DMA yet. */
HCLR4(sc, SDMMC_CTRL, SDMMC_CTRL_USE_INTERNAL_DMAC);
/* Set card read threshold to the size of a block. */
HWRITE4(sc, SDMMC_CARDTHRCTL,
512 << SDMMC_CARDTHRCTL_RDTHR_SHIFT | SDMMC_CARDTHRCTL_RDTHREN);
dwmmc_bus_width(sc, 1);
memset(&saa, 0, sizeof(saa));
saa.saa_busname = "sdmmc";
saa.sct = &dwmmc_chip_functions;
saa.sch = sc;
width = OF_getpropint(faa->fa_node, "bus-width", 1);
if (width >= 8)
saa.caps |= SMC_CAPS_8BIT_MODE;
if (width >= 4)
saa.caps |= SMC_CAPS_4BIT_MODE;
sc->sc_sdmmc = config_found(self, &saa, NULL);
return;
unmap:
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
}
int
dwmmc_intr(void *arg)
{
return 1;
}
int
dwmmc_host_reset(sdmmc_chipset_handle_t sch)
{
printf("%s\n", __func__);
return 0;
}
uint32_t
dwmmc_host_ocr(sdmmc_chipset_handle_t sch)
{
return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
}
int
dwmmc_host_maxblklen(sdmmc_chipset_handle_t sch)
{
return 512;
}
int
dwmmc_card_detect(sdmmc_chipset_handle_t sch)
{
struct dwmmc_softc *sc = sch;
uint32_t cdetect;
if (OF_getproplen(sc->sc_node, "non-removable") == 0)
return 1;
if (sc->sc_gpio[0]) {
int inverted, val;
val = gpio_controller_get_pin(sc->sc_gpio);
inverted = (OF_getproplen(sc->sc_node, "cd-inverted") == 0);
return inverted ? !val : val;
}
cdetect = HREAD4(sc, SDMMC_CDETECT);
return !(cdetect & SDMMC_CDETECT_CARD_DETECT_0);
}
int
dwmmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
{
struct dwmmc_softc *sc = sch;
if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
HSET4(sc, SDMMC_PWREN, 1);
else
HCLR4(sc, SDMMC_PWREN, 0);
return 0;
}
int
dwmmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, int timing)
{
struct dwmmc_softc *sc = sch;
int div = 0, timeout;
HWRITE4(sc, SDMMC_CLKENA, 0);
HWRITE4(sc, SDMMC_CLKSRC, 0);
if (freq == 0)
return 0;
if (sc->sc_clkbase / 1000 > freq) {
for (div = 1; div < 256; div++)
if (sc->sc_clkbase / (2 * 1000 * div) <= freq)
break;
}
HWRITE4(sc, SDMMC_CLKDIV, div);
/* Update clock. */
HWRITE4(sc, SDMMC_CMD, SDMMC_CMD_START_CMD |
SDMMC_CMD_WAIT_PRVDATA_COMPLETE |
SDMMC_CMD_UPDATE_CLOCK_REGISTERS_ONLY);
for (timeout = 1000; timeout > 0; timeout--) {
if ((HREAD4(sc, SDMMC_CMD) & SDMMC_CMD_START_CMD) == 0)
break;
}
if (timeout == 0) {
printf("%s: timeout\n", __func__);
return ETIMEDOUT;
}
/* Enable clock in low power mode. */
HWRITE4(sc, SDMMC_CLKENA,
SDMMC_CLKENA_CCLK_ENABLE | SDMMC_CLKENA_CCLK_LOW_POWER);
/* Update clock again. */
HWRITE4(sc, SDMMC_CMD, SDMMC_CMD_START_CMD |
SDMMC_CMD_WAIT_PRVDATA_COMPLETE |
SDMMC_CMD_UPDATE_CLOCK_REGISTERS_ONLY);
for (timeout = 1000; timeout > 0; timeout--) {
if ((HREAD4(sc, SDMMC_CMD) & SDMMC_CMD_START_CMD) == 0)
break;
}
if (timeout == 0) {
printf("%s: timeout\n", __func__);
return ETIMEDOUT;
}
delay(1000000);
return 0;
}
int
dwmmc_bus_width(sdmmc_chipset_handle_t sch, int width)
{
struct dwmmc_softc *sc = sch;
switch (width) {
case 1:
HCLR4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT|SDMMC_CTYPE_4BIT);
break;
case 4:
HSET4(sc, SDMMC_CTYPE, SDMMC_CTYPE_4BIT);
HCLR4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT);
break;
case 8:
HSET4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT);
break;
default:
return EINVAL;
}
return 0;
}
void
dwmmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
{
struct dwmmc_softc *sc = sch;
uint32_t cmdval = SDMMC_CMD_START_CMD | SDMMC_CMD_USE_HOLD_REG;
uint32_t status;
int timeout;
int s;
#if 0
printf("%s: cmd %d arg 0x%x flags 0x%x data %p datalen %d blklen %d\n",
sc->sc_dev.dv_xname, cmd->c_opcode, cmd->c_arg, cmd->c_flags,
cmd->c_data, cmd->c_datalen, cmd->c_blklen);
#endif
s = splbio();
for (timeout = 10000; timeout > 0; timeout--) {
status = HREAD4(sc, SDMMC_STATUS);
if ((status & SDMMC_STATUS_DATA_BUSY) == 0)
break;
delay(100);
}
if (timeout == 0) {
printf("%s: timeout on data busy\n", sc->sc_dev.dv_xname);
goto done;
}
if (cmd->c_opcode == MMC_STOP_TRANSMISSION)
cmdval |= SDMMC_CMD_STOP_ABORT_CMD;
else if (cmd->c_opcode != MMC_SEND_STATUS)
cmdval |= SDMMC_CMD_WAIT_PRVDATA_COMPLETE;
if (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE ||
cmd->c_opcode == MMC_WRITE_BLOCK_MULTIPLE)
cmdval |= SDMMC_CMD_SEND_AUTO_STOP;
if (cmd->c_opcode == 0)
cmdval |= SDMMC_CMD_SEND_INITIALIZATION;
if (cmd->c_flags & SCF_RSP_PRESENT)
cmdval |= SDMMC_CMD_RESPONSE_EXPECT;
if (cmd->c_flags & SCF_RSP_136)
cmdval |= SDMMC_CMD_RESPONSE_LENGTH;
if (cmd->c_flags & SCF_RSP_CRC)
cmdval |= SDMMC_CMD_CHECK_REPONSE_CRC;
if (cmd->c_datalen > 0) {
HWRITE4(sc, SDMMC_TMOUT, 0xffffffff);
HWRITE4(sc, SDMMC_BYTCNT, cmd->c_datalen);
HWRITE4(sc, SDMMC_BLKSIZ, cmd->c_blklen);
HSET4(sc, SDMMC_CTRL, SDMMC_CTRL_FIFO_RESET);
for (timeout = 1000; timeout > 0; timeout--) {
if ((HREAD4(sc, SDMMC_CTRL) &
SDMMC_CTRL_FIFO_RESET) == 0)
break;
delay(100);
}
if (timeout == 0)
printf("%s: FIFO reset failed\n", sc->sc_dev.dv_xname);
cmdval |= SDMMC_CMD_DATA_EXPECTED;
if (!ISSET(cmd->c_flags, SCF_CMD_READ))
cmdval |= SDMMC_CMD_WR;
}
HWRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
HWRITE4(sc, SDMMC_CMDARG, cmd->c_arg);
HWRITE4(sc, SDMMC_CMD, cmdval | cmd->c_opcode);
for (timeout = 1000; timeout > 0; timeout--) {
status = HREAD4(sc, SDMMC_RINTSTS);
if (status & SDMMC_RINTSTS_CD)
break;
delay(100);
}
if (timeout == 0 || status & SDMMC_RINTSTS_RTO) {
cmd->c_error = ETIMEDOUT;
goto done;
}
if (cmd->c_flags & SCF_RSP_PRESENT) {
if (cmd->c_flags & SCF_RSP_136) {
cmd->c_resp[0] = HREAD4(sc, SDMMC_RESP0);
cmd->c_resp[1] = HREAD4(sc, SDMMC_RESP1);
cmd->c_resp[2] = HREAD4(sc, SDMMC_RESP2);
cmd->c_resp[3] = HREAD4(sc, SDMMC_RESP3);
if (cmd->c_flags & SCF_RSP_CRC) {
cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
(cmd->c_resp[1] << 24);
cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
(cmd->c_resp[2] << 24);
cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
(cmd->c_resp[3] << 24);
cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
}
} else {
cmd->c_resp[0] = HREAD4(sc, SDMMC_RESP0);
}
}
if (cmd->c_datalen > 0)
dwmmc_transfer_data(sc, cmd);
if (cmdval & SDMMC_CMD_SEND_AUTO_STOP) {
for (timeout = 10000; timeout > 0; timeout--) {
status = HREAD4(sc, SDMMC_RINTSTS);
if (status & SDMMC_RINTSTS_ACD)
break;
delay(10);
}
if (timeout == 0) {
cmd->c_error = ETIMEDOUT;
goto done;
}
}
done:
cmd->c_flags |= SCF_ITSDONE;
#if 0
printf("%s: err %d rintsts 0x%x\n", sc->sc_dev.dv_xname, cmd->c_error,
HREAD4(sc, SDMMC_RINTSTS));
#endif
splx(s);
}
void
dwmmc_transfer_data(struct dwmmc_softc *sc, struct sdmmc_command *cmd)
{
int datalen = cmd->c_datalen;
u_char *datap = cmd->c_data;
uint32_t status;
int count, timeout;
int fifodr = SDMMC_RINTSTS_DTO | SDMMC_RINTSTS_HTO;
if (ISSET(cmd->c_flags, SCF_CMD_READ))
fifodr |= SDMMC_RINTSTS_RXDR;
else
fifodr |= SDMMC_RINTSTS_TXDR;
while (datalen > 0) {
status = HREAD4(sc, SDMMC_RINTSTS);
if (status & SDMMC_RINTSTS_DATA_ERR) {
cmd->c_error = EIO;
return;
}
if (status & SDMMC_RINTSTS_DRTO) {
cmd->c_error = ETIMEDOUT;
return;
}
for (timeout = 10000; timeout > 0; timeout--) {
status = HREAD4(sc, SDMMC_RINTSTS);
if (status & fifodr)
break;
delay(100);
}
if (timeout == 0) {
cmd->c_error = ETIMEDOUT;
return;
}
count = SDMMC_STATUS_FIFO_COUNT(HREAD4(sc, SDMMC_STATUS));
if (!ISSET(cmd->c_flags, SCF_CMD_READ))
count = sc->sc_fifo_depth - count;
count = MIN(datalen, count * sc->sc_fifo_width);
if (ISSET(cmd->c_flags, SCF_CMD_READ))
sc->sc_read_data(sc, datap, count);
else
sc->sc_write_data(sc, datap, count);
datap += count;
datalen -= count;
}
for (timeout = 10000; timeout > 0; timeout--) {
status = HREAD4(sc, SDMMC_RINTSTS);
if (status & SDMMC_RINTSTS_DTO)
break;
delay(100);
}
if (timeout == 0)
cmd->c_error = ETIMEDOUT;
}
void
dwmmc_read_data32(struct dwmmc_softc *sc, u_char *datap, int datalen)
{
while (datalen > 3) {
*(uint32_t *)datap = HREAD4(sc, SDMMC_FIFO_BASE);
datap += 4;
datalen -= 4;
}
if (datalen > 0) {
uint32_t rv = HREAD4(sc, SDMMC_FIFO_BASE);
do {
*datap++ = rv & 0xff;
rv = rv >> 8;
} while (--datalen > 0);
}
HWRITE4(sc, SDMMC_RINTSTS, SDMMC_RINTSTS_RXDR);
}
void
dwmmc_write_data32(struct dwmmc_softc *sc, u_char *datap, int datalen)
{
while (datalen > 3) {
HWRITE4(sc, SDMMC_FIFO_BASE, *((uint32_t *)datap));
datap += 4;
datalen -= 4;
}
if (datalen > 0) {
uint32_t rv = *datap++;
if (datalen > 1)
rv |= *datap++ << 8;
if (datalen > 2)
rv |= *datap++ << 16;
HWRITE4(sc, SDMMC_FIFO_BASE, rv);
}
HWRITE4(sc, SDMMC_RINTSTS, SDMMC_RINTSTS_TXDR);
}
void
dwmmc_read_data64(struct dwmmc_softc *sc, u_char *datap, int datalen)
{
while (datalen > 7) {
*(uint32_t *)datap = HREAD4(sc, SDMMC_FIFO_BASE);
datap += 4;
datalen -= 4;
*(uint32_t *)datap = HREAD4(sc, SDMMC_FIFO_BASE + 4);
datap += 4;
datalen -= 4;
}
if (datalen > 0) {
uint64_t rv = HREAD4(sc, SDMMC_FIFO_BASE) |
((uint64_t)HREAD4(sc, SDMMC_FIFO_BASE + 4) << 32);
do {
*datap++ = rv & 0xff;
rv = rv >> 8;
} while (--datalen > 0);
}
HWRITE4(sc, SDMMC_RINTSTS, SDMMC_RINTSTS_RXDR);
}
void
dwmmc_write_data64(struct dwmmc_softc *sc, u_char *datap, int datalen)
{
while (datalen > 7) {
HWRITE4(sc, SDMMC_FIFO_BASE, *((uint32_t *)datap));
datap += 4;
datalen -= 4;
HWRITE4(sc, SDMMC_FIFO_BASE + 4, *((uint32_t *)datap));
datap += 4;
datalen -= 4;
}
if (datalen > 0) {
uint32_t rv = *datap++;
if (datalen > 1)
rv |= *datap++ << 8;
if (datalen > 2)
rv |= *datap++ << 16;
if (datalen > 3)
rv |= *datap++ << 24;
HWRITE4(sc, SDMMC_FIFO_BASE, rv);
if (datalen > 4)
rv = *datap++;
if (datalen > 5)
rv |= *datap++ << 8;
if (datalen > 6)
rv |= *datap++ << 16;
HWRITE4(sc, SDMMC_FIFO_BASE + 4, rv);
}
HWRITE4(sc, SDMMC_RINTSTS, SDMMC_RINTSTS_TXDR);
}
|