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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
|
/*
* SD/MMC Greybus driver.
*
* Copyright 2014-2015 Google Inc.
* Copyright 2014-2015 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#include <linux/kernel.h>
#include <linux/mmc/core.h>
#include <linux/mmc/host.h>
#include <linux/mmc/mmc.h>
#include <linux/scatterlist.h>
#include <linux/workqueue.h>
#include "greybus.h"
#include "gbphy.h"
struct gb_sdio_host {
struct gb_connection *connection;
struct gbphy_device *gbphy_dev;
struct mmc_host *mmc;
struct mmc_request *mrq;
struct mutex lock; /* lock for this host */
size_t data_max;
spinlock_t xfer; /* lock to cancel ongoing transfer */
bool xfer_stop;
struct workqueue_struct *mrq_workqueue;
struct work_struct mrqwork;
u8 queued_events;
bool removed;
bool card_present;
bool read_only;
};
#define GB_SDIO_RSP_R1_R5_R6_R7 (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
GB_SDIO_RSP_OPCODE)
#define GB_SDIO_RSP_R3_R4 (GB_SDIO_RSP_PRESENT)
#define GB_SDIO_RSP_R2 (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
GB_SDIO_RSP_136)
#define GB_SDIO_RSP_R1B (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
GB_SDIO_RSP_OPCODE | GB_SDIO_RSP_BUSY)
static inline bool single_op(struct mmc_command *cmd)
{
uint32_t opcode = cmd->opcode;
return opcode == MMC_WRITE_BLOCK ||
opcode == MMC_READ_SINGLE_BLOCK;
}
static void _gb_sdio_set_host_caps(struct gb_sdio_host *host, u32 r)
{
u32 caps = 0;
u32 caps2 = 0;
caps = ((r & GB_SDIO_CAP_NONREMOVABLE) ? MMC_CAP_NONREMOVABLE : 0) |
((r & GB_SDIO_CAP_4_BIT_DATA) ? MMC_CAP_4_BIT_DATA : 0) |
((r & GB_SDIO_CAP_8_BIT_DATA) ? MMC_CAP_8_BIT_DATA : 0) |
((r & GB_SDIO_CAP_MMC_HS) ? MMC_CAP_MMC_HIGHSPEED : 0) |
((r & GB_SDIO_CAP_SD_HS) ? MMC_CAP_SD_HIGHSPEED : 0) |
((r & GB_SDIO_CAP_ERASE) ? MMC_CAP_ERASE : 0) |
((r & GB_SDIO_CAP_1_2V_DDR) ? MMC_CAP_1_2V_DDR : 0) |
((r & GB_SDIO_CAP_1_8V_DDR) ? MMC_CAP_1_8V_DDR : 0) |
((r & GB_SDIO_CAP_POWER_OFF_CARD) ? MMC_CAP_POWER_OFF_CARD : 0) |
((r & GB_SDIO_CAP_UHS_SDR12) ? MMC_CAP_UHS_SDR12 : 0) |
((r & GB_SDIO_CAP_UHS_SDR25) ? MMC_CAP_UHS_SDR25 : 0) |
((r & GB_SDIO_CAP_UHS_SDR50) ? MMC_CAP_UHS_SDR50 : 0) |
((r & GB_SDIO_CAP_UHS_SDR104) ? MMC_CAP_UHS_SDR104 : 0) |
((r & GB_SDIO_CAP_UHS_DDR50) ? MMC_CAP_UHS_DDR50 : 0) |
((r & GB_SDIO_CAP_DRIVER_TYPE_A) ? MMC_CAP_DRIVER_TYPE_A : 0) |
((r & GB_SDIO_CAP_DRIVER_TYPE_C) ? MMC_CAP_DRIVER_TYPE_C : 0) |
((r & GB_SDIO_CAP_DRIVER_TYPE_D) ? MMC_CAP_DRIVER_TYPE_D : 0);
caps2 = ((r & GB_SDIO_CAP_HS200_1_2V) ? MMC_CAP2_HS200_1_2V_SDR : 0) |
#ifdef MMC_HS400_SUPPORTED
((r & GB_SDIO_CAP_HS400_1_2V) ? MMC_CAP2_HS400_1_2V : 0) |
((r & GB_SDIO_CAP_HS400_1_8V) ? MMC_CAP2_HS400_1_8V : 0) |
#endif
((r & GB_SDIO_CAP_HS200_1_8V) ? MMC_CAP2_HS200_1_8V_SDR : 0);
host->mmc->caps = caps | MMC_CAP_NEEDS_POLL;
host->mmc->caps2 = caps2;
if (caps & MMC_CAP_NONREMOVABLE)
host->card_present = true;
}
static u32 _gb_sdio_get_host_ocr(u32 ocr)
{
return (((ocr & GB_SDIO_VDD_165_195) ? MMC_VDD_165_195 : 0) |
((ocr & GB_SDIO_VDD_20_21) ? MMC_VDD_20_21 : 0) |
((ocr & GB_SDIO_VDD_21_22) ? MMC_VDD_21_22 : 0) |
((ocr & GB_SDIO_VDD_22_23) ? MMC_VDD_22_23 : 0) |
((ocr & GB_SDIO_VDD_23_24) ? MMC_VDD_23_24 : 0) |
((ocr & GB_SDIO_VDD_24_25) ? MMC_VDD_24_25 : 0) |
((ocr & GB_SDIO_VDD_25_26) ? MMC_VDD_25_26 : 0) |
((ocr & GB_SDIO_VDD_26_27) ? MMC_VDD_26_27 : 0) |
((ocr & GB_SDIO_VDD_27_28) ? MMC_VDD_27_28 : 0) |
((ocr & GB_SDIO_VDD_28_29) ? MMC_VDD_28_29 : 0) |
((ocr & GB_SDIO_VDD_29_30) ? MMC_VDD_29_30 : 0) |
((ocr & GB_SDIO_VDD_30_31) ? MMC_VDD_30_31 : 0) |
((ocr & GB_SDIO_VDD_31_32) ? MMC_VDD_31_32 : 0) |
((ocr & GB_SDIO_VDD_32_33) ? MMC_VDD_32_33 : 0) |
((ocr & GB_SDIO_VDD_33_34) ? MMC_VDD_33_34 : 0) |
((ocr & GB_SDIO_VDD_34_35) ? MMC_VDD_34_35 : 0) |
((ocr & GB_SDIO_VDD_35_36) ? MMC_VDD_35_36 : 0)
);
}
static int gb_sdio_get_caps(struct gb_sdio_host *host)
{
struct gb_sdio_get_caps_response response;
struct mmc_host *mmc = host->mmc;
u16 data_max;
u32 blksz;
u32 ocr;
u32 r;
int ret;
ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_GET_CAPABILITIES,
NULL, 0, &response, sizeof(response));
if (ret < 0)
return ret;
r = le32_to_cpu(response.caps);
_gb_sdio_set_host_caps(host, r);
/* get the max block size that could fit our payload */
data_max = gb_operation_get_payload_size_max(host->connection);
data_max = min(data_max - sizeof(struct gb_sdio_transfer_request),
data_max - sizeof(struct gb_sdio_transfer_response));
blksz = min(le16_to_cpu(response.max_blk_size), data_max);
blksz = max_t(u32, 512, blksz);
mmc->max_blk_size = rounddown_pow_of_two(blksz);
mmc->max_blk_count = le16_to_cpu(response.max_blk_count);
host->data_max = data_max;
/* get ocr supported values */
ocr = _gb_sdio_get_host_ocr(le32_to_cpu(response.ocr));
mmc->ocr_avail = ocr;
mmc->ocr_avail_sdio = mmc->ocr_avail;
mmc->ocr_avail_sd = mmc->ocr_avail;
mmc->ocr_avail_mmc = mmc->ocr_avail;
/* get frequency range values */
mmc->f_min = le32_to_cpu(response.f_min);
mmc->f_max = le32_to_cpu(response.f_max);
return 0;
}
static void _gb_queue_event(struct gb_sdio_host *host, u8 event)
{
if (event & GB_SDIO_CARD_INSERTED)
host->queued_events &= ~GB_SDIO_CARD_REMOVED;
else if (event & GB_SDIO_CARD_REMOVED)
host->queued_events &= ~GB_SDIO_CARD_INSERTED;
host->queued_events |= event;
}
static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
{
u8 state_changed = 0;
if (event & GB_SDIO_CARD_INSERTED) {
if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
return 0;
if (host->card_present)
return 0;
host->card_present = true;
state_changed = 1;
}
if (event & GB_SDIO_CARD_REMOVED) {
if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
return 0;
if (!(host->card_present))
return 0;
host->card_present = false;
state_changed = 1;
}
if (event & GB_SDIO_WP) {
host->read_only = true;
}
if (state_changed) {
dev_info(mmc_dev(host->mmc), "card %s now event\n",
(host->card_present ? "inserted" : "removed"));
mmc_detect_change(host->mmc, 0);
}
return 0;
}
static int gb_sdio_request_handler(struct gb_operation *op)
{
struct gb_sdio_host *host = gb_connection_get_data(op->connection);
struct gb_message *request;
struct gb_sdio_event_request *payload;
u8 type = op->type;
int ret = 0;
u8 event;
if (type != GB_SDIO_TYPE_EVENT) {
dev_err(mmc_dev(host->mmc),
"unsupported unsolicited event: %u\n", type);
return -EINVAL;
}
request = op->request;
if (request->payload_size < sizeof(*payload)) {
dev_err(mmc_dev(host->mmc), "wrong event size received (%zu < %zu)\n",
request->payload_size, sizeof(*payload));
return -EINVAL;
}
payload = request->payload;
event = payload->event;
if (host->removed)
_gb_queue_event(host, event);
else
ret = _gb_sdio_process_events(host, event);
return ret;
}
static int gb_sdio_set_ios(struct gb_sdio_host *host,
struct gb_sdio_set_ios_request *request)
{
return gb_operation_sync(host->connection, GB_SDIO_TYPE_SET_IOS,
request, sizeof(*request), NULL, 0);
}
static int _gb_sdio_send(struct gb_sdio_host *host, struct mmc_data *data,
size_t len, u16 nblocks, off_t skip)
{
struct gb_sdio_transfer_request *request;
struct gb_sdio_transfer_response *response;
struct gb_operation *operation;
struct scatterlist *sg = data->sg;
unsigned int sg_len = data->sg_len;
size_t copied;
u16 send_blksz;
u16 send_blocks;
int ret;
WARN_ON(len > host->data_max);
operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
len + sizeof(*request),
sizeof(*response), GFP_KERNEL);
if (!operation)
return -ENOMEM;
request = operation->request->payload;
request->data_flags = (data->flags >> 8);
request->data_blocks = cpu_to_le16(nblocks);
request->data_blksz = cpu_to_le16(data->blksz);
copied = sg_pcopy_to_buffer(sg, sg_len, &request->data[0], len, skip);
if (copied != len) {
ret = -EINVAL;
goto err_put_operation;
}
ret = gb_operation_request_send_sync(operation);
if (ret < 0)
goto err_put_operation;
response = operation->response->payload;
send_blocks = le16_to_cpu(response->data_blocks);
send_blksz = le16_to_cpu(response->data_blksz);
if (len != send_blksz * send_blocks) {
dev_err(mmc_dev(host->mmc), "send: size received: %zu != %d\n",
len, send_blksz * send_blocks);
ret = -EINVAL;
}
err_put_operation:
gb_operation_put(operation);
return ret;
}
static int _gb_sdio_recv(struct gb_sdio_host *host, struct mmc_data *data,
size_t len, u16 nblocks, off_t skip)
{
struct gb_sdio_transfer_request *request;
struct gb_sdio_transfer_response *response;
struct gb_operation *operation;
struct scatterlist *sg = data->sg;
unsigned int sg_len = data->sg_len;
size_t copied;
u16 recv_blksz;
u16 recv_blocks;
int ret;
WARN_ON(len > host->data_max);
operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
sizeof(*request),
len + sizeof(*response), GFP_KERNEL);
if (!operation)
return -ENOMEM;
request = operation->request->payload;
request->data_flags = (data->flags >> 8);
request->data_blocks = cpu_to_le16(nblocks);
request->data_blksz = cpu_to_le16(data->blksz);
ret = gb_operation_request_send_sync(operation);
if (ret < 0)
goto err_put_operation;
response = operation->response->payload;
recv_blocks = le16_to_cpu(response->data_blocks);
recv_blksz = le16_to_cpu(response->data_blksz);
if (len != recv_blksz * recv_blocks) {
dev_err(mmc_dev(host->mmc), "recv: size received: %d != %zu\n",
recv_blksz * recv_blocks, len);
ret = -EINVAL;
goto err_put_operation;
}
copied = sg_pcopy_from_buffer(sg, sg_len, &response->data[0], len,
skip);
if (copied != len)
ret = -EINVAL;
err_put_operation:
gb_operation_put(operation);
return ret;
}
static int gb_sdio_transfer(struct gb_sdio_host *host, struct mmc_data *data)
{
size_t left, len;
off_t skip = 0;
int ret = 0;
u16 nblocks;
if (single_op(data->mrq->cmd) && data->blocks > 1) {
ret = -ETIMEDOUT;
goto out;
}
left = data->blksz * data->blocks;
while (left) {
/* check is a stop transmission is pending */
spin_lock(&host->xfer);
if (host->xfer_stop) {
host->xfer_stop = false;
spin_unlock(&host->xfer);
ret = -EINTR;
goto out;
}
spin_unlock(&host->xfer);
len = min(left, host->data_max);
nblocks = len / data->blksz;
len = nblocks * data->blksz;
if (data->flags & MMC_DATA_READ) {
ret = _gb_sdio_recv(host, data, len, nblocks, skip);
if (ret < 0)
goto out;
} else {
ret = _gb_sdio_send(host, data, len, nblocks, skip);
if (ret < 0)
goto out;
}
data->bytes_xfered += len;
left -= len;
skip += len;
}
out:
data->error = ret;
return ret;
}
static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
{
struct gb_sdio_command_request request = {0};
struct gb_sdio_command_response response;
struct mmc_data *data = host->mrq->data;
u8 cmd_flags;
u8 cmd_type;
int i;
int ret;
switch (mmc_resp_type(cmd)) {
case MMC_RSP_NONE:
cmd_flags = GB_SDIO_RSP_NONE;
break;
case MMC_RSP_R1:
cmd_flags = GB_SDIO_RSP_R1_R5_R6_R7;
break;
case MMC_RSP_R1B:
cmd_flags = GB_SDIO_RSP_R1B;
break;
case MMC_RSP_R2:
cmd_flags = GB_SDIO_RSP_R2;
break;
case MMC_RSP_R3:
cmd_flags = GB_SDIO_RSP_R3_R4;
break;
default:
dev_err(mmc_dev(host->mmc), "cmd flag invalid 0x%04x\n",
mmc_resp_type(cmd));
ret = -EINVAL;
goto out;
}
switch (mmc_cmd_type(cmd)) {
case MMC_CMD_BC:
cmd_type = GB_SDIO_CMD_BC;
break;
case MMC_CMD_BCR:
cmd_type = GB_SDIO_CMD_BCR;
break;
case MMC_CMD_AC:
cmd_type = GB_SDIO_CMD_AC;
break;
case MMC_CMD_ADTC:
cmd_type = GB_SDIO_CMD_ADTC;
break;
default:
dev_err(mmc_dev(host->mmc), "cmd type invalid 0x%04x\n",
mmc_cmd_type(cmd));
ret = -EINVAL;
goto out;
}
request.cmd = cmd->opcode;
request.cmd_flags = cmd_flags;
request.cmd_type = cmd_type;
request.cmd_arg = cpu_to_le32(cmd->arg);
/* some controllers need to know at command time data details */
if (data) {
request.data_blocks = cpu_to_le16(data->blocks);
request.data_blksz = cpu_to_le16(data->blksz);
}
ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_COMMAND,
&request, sizeof(request), &response,
sizeof(response));
if (ret < 0)
goto out;
/* no response expected */
if (cmd_flags & GB_SDIO_RSP_NONE)
goto out;
/* long response expected */
if (cmd_flags & GB_SDIO_RSP_R2)
for (i = 0; i < 4; i++)
cmd->resp[i] = le32_to_cpu(response.resp[i]);
else
cmd->resp[0] = le32_to_cpu(response.resp[0]);
out:
cmd->error = ret;
return ret;
}
static void gb_sdio_mrq_work(struct work_struct *work)
{
struct gb_sdio_host *host;
struct mmc_request *mrq;
int ret;
host = container_of(work, struct gb_sdio_host, mrqwork);
mutex_lock(&host->lock);
mrq = host->mrq;
if (!mrq) {
mutex_unlock(&host->lock);
dev_err(mmc_dev(host->mmc), "mmc request is NULL");
return;
}
if (host->removed) {
mrq->cmd->error = -ESHUTDOWN;
goto done;
}
if (mrq->sbc) {
ret = gb_sdio_command(host, mrq->sbc);
if (ret < 0)
goto done;
}
ret = gb_sdio_command(host, mrq->cmd);
if (ret < 0)
goto done;
if (mrq->data) {
ret = gb_sdio_transfer(host, mrq->data);
if (ret < 0)
goto done;
}
if (mrq->stop) {
ret = gb_sdio_command(host, mrq->stop);
if (ret < 0)
goto done;
}
done:
host->mrq = NULL;
mutex_unlock(&host->lock);
mmc_request_done(host->mmc, mrq);
}
static void gb_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
struct gb_sdio_host *host = mmc_priv(mmc);
struct mmc_command *cmd = mrq->cmd;
/* Check if it is a cancel to ongoing transfer */
if (cmd->opcode == MMC_STOP_TRANSMISSION) {
spin_lock(&host->xfer);
host->xfer_stop = true;
spin_unlock(&host->xfer);
}
mutex_lock(&host->lock);
WARN_ON(host->mrq);
host->mrq = mrq;
if (host->removed) {
mrq->cmd->error = -ESHUTDOWN;
goto out;
}
if (!host->card_present) {
mrq->cmd->error = -ENOMEDIUM;
goto out;
}
queue_work(host->mrq_workqueue, &host->mrqwork);
mutex_unlock(&host->lock);
return;
out:
host->mrq = NULL;
mutex_unlock(&host->lock);
mmc_request_done(mmc, mrq);
}
static void gb_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
struct gb_sdio_host *host = mmc_priv(mmc);
struct gb_sdio_set_ios_request request;
int ret;
u8 power_mode;
u8 bus_width;
u8 timing;
u8 signal_voltage;
u8 drv_type;
mutex_lock(&host->lock);
request.clock = cpu_to_le32(ios->clock);
request.vdd = cpu_to_le32(1 << ios->vdd);
request.bus_mode = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN ?
GB_SDIO_BUSMODE_OPENDRAIN :
GB_SDIO_BUSMODE_PUSHPULL);
switch (ios->power_mode) {
case MMC_POWER_OFF:
default:
power_mode = GB_SDIO_POWER_OFF;
break;
case MMC_POWER_UP:
power_mode = GB_SDIO_POWER_UP;
break;
case MMC_POWER_ON:
power_mode = GB_SDIO_POWER_ON;
break;
#ifdef MMC_POWER_UNDEFINED_SUPPORTED
case MMC_POWER_UNDEFINED:
power_mode = GB_SDIO_POWER_UNDEFINED;
break;
#endif
}
request.power_mode = power_mode;
switch (ios->bus_width) {
case MMC_BUS_WIDTH_1:
bus_width = GB_SDIO_BUS_WIDTH_1;
break;
case MMC_BUS_WIDTH_4:
default:
bus_width = GB_SDIO_BUS_WIDTH_4;
break;
case MMC_BUS_WIDTH_8:
bus_width = GB_SDIO_BUS_WIDTH_8;
break;
}
request.bus_width = bus_width;
switch (ios->timing) {
case MMC_TIMING_LEGACY:
default:
timing = GB_SDIO_TIMING_LEGACY;
break;
case MMC_TIMING_MMC_HS:
timing = GB_SDIO_TIMING_MMC_HS;
break;
case MMC_TIMING_SD_HS:
timing = GB_SDIO_TIMING_SD_HS;
break;
case MMC_TIMING_UHS_SDR12:
timing = GB_SDIO_TIMING_UHS_SDR12;
break;
case MMC_TIMING_UHS_SDR25:
timing = GB_SDIO_TIMING_UHS_SDR25;
break;
case MMC_TIMING_UHS_SDR50:
timing = GB_SDIO_TIMING_UHS_SDR50;
break;
case MMC_TIMING_UHS_SDR104:
timing = GB_SDIO_TIMING_UHS_SDR104;
break;
case MMC_TIMING_UHS_DDR50:
timing = GB_SDIO_TIMING_UHS_DDR50;
break;
#ifdef MMC_DDR52_DEFINED
case MMC_TIMING_MMC_DDR52:
timing = GB_SDIO_TIMING_MMC_DDR52;
break;
#endif
case MMC_TIMING_MMC_HS200:
timing = GB_SDIO_TIMING_MMC_HS200;
break;
#ifdef MMC_HS400_SUPPORTED
case MMC_TIMING_MMC_HS400:
timing = GB_SDIO_TIMING_MMC_HS400;
break;
#endif
}
request.timing = timing;
switch (ios->signal_voltage) {
case MMC_SIGNAL_VOLTAGE_330:
signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_330;
break;
case MMC_SIGNAL_VOLTAGE_180:
default:
signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_180;
break;
case MMC_SIGNAL_VOLTAGE_120:
signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_120;
break;
}
request.signal_voltage = signal_voltage;
switch (ios->drv_type) {
case MMC_SET_DRIVER_TYPE_A:
drv_type = GB_SDIO_SET_DRIVER_TYPE_A;
break;
case MMC_SET_DRIVER_TYPE_C:
drv_type = GB_SDIO_SET_DRIVER_TYPE_C;
break;
case MMC_SET_DRIVER_TYPE_D:
drv_type = GB_SDIO_SET_DRIVER_TYPE_D;
break;
case MMC_SET_DRIVER_TYPE_B:
default:
drv_type = GB_SDIO_SET_DRIVER_TYPE_B;
break;
}
request.drv_type = drv_type;
ret = gb_sdio_set_ios(host, &request);
if (ret < 0)
goto out;
memcpy(&mmc->ios, ios, sizeof(mmc->ios));
out:
mutex_unlock(&host->lock);
}
static int gb_mmc_get_ro(struct mmc_host *mmc)
{
struct gb_sdio_host *host = mmc_priv(mmc);
mutex_lock(&host->lock);
if (host->removed) {
mutex_unlock(&host->lock);
return -ESHUTDOWN;
}
mutex_unlock(&host->lock);
return host->read_only;
}
static int gb_mmc_get_cd(struct mmc_host *mmc)
{
struct gb_sdio_host *host = mmc_priv(mmc);
mutex_lock(&host->lock);
if (host->removed) {
mutex_unlock(&host->lock);
return -ESHUTDOWN;
}
mutex_unlock(&host->lock);
return host->card_present;
}
static const struct mmc_host_ops gb_sdio_ops = {
.request = gb_mmc_request,
.set_ios = gb_mmc_set_ios,
.get_ro = gb_mmc_get_ro,
.get_cd = gb_mmc_get_cd,
};
static int gb_sdio_probe(struct gbphy_device *gbphy_dev,
const struct gbphy_device_id *id)
{
struct gb_connection *connection;
struct mmc_host *mmc;
struct gb_sdio_host *host;
int ret = 0;
mmc = mmc_alloc_host(sizeof(*host), &gbphy_dev->dev);
if (!mmc)
return -ENOMEM;
connection = gb_connection_create(gbphy_dev->bundle,
le16_to_cpu(gbphy_dev->cport_desc->id),
gb_sdio_request_handler);
if (IS_ERR(connection)) {
ret = PTR_ERR(connection);
goto exit_mmc_free;
}
host = mmc_priv(mmc);
host->mmc = mmc;
host->removed = true;
host->connection = connection;
gb_connection_set_data(connection, host);
host->gbphy_dev = gbphy_dev;
gb_gbphy_set_data(gbphy_dev, host);
ret = gb_connection_enable_tx(connection);
if (ret)
goto exit_connection_destroy;
ret = gb_sdio_get_caps(host);
if (ret < 0)
goto exit_connection_disable;
mmc->ops = &gb_sdio_ops;
/* for now we just make a map 1:1 between max blocks and segments */
mmc->max_segs = host->mmc->max_blk_count;
mmc->max_seg_size = host->mmc->max_blk_size;
mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
mutex_init(&host->lock);
spin_lock_init(&host->xfer);
host->mrq_workqueue = alloc_workqueue("mmc-%s", 0, 1,
dev_name(&gbphy_dev->dev));
if (!host->mrq_workqueue) {
ret = -ENOMEM;
goto exit_connection_disable;
}
INIT_WORK(&host->mrqwork, gb_sdio_mrq_work);
ret = gb_connection_enable(connection);
if (ret)
goto exit_wq_destroy;
ret = mmc_add_host(mmc);
if (ret < 0)
goto exit_wq_destroy;
host->removed = false;
ret = _gb_sdio_process_events(host, host->queued_events);
host->queued_events = 0;
return ret;
exit_wq_destroy:
destroy_workqueue(host->mrq_workqueue);
exit_connection_disable:
gb_connection_disable(connection);
exit_connection_destroy:
gb_connection_destroy(connection);
exit_mmc_free:
mmc_free_host(mmc);
return ret;
}
static void gb_sdio_remove(struct gbphy_device *gbphy_dev)
{
struct gb_sdio_host *host = gb_gbphy_get_data(gbphy_dev);
struct gb_connection *connection = host->connection;
struct mmc_host *mmc;
mutex_lock(&host->lock);
host->removed = true;
mmc = host->mmc;
gb_connection_set_data(connection, NULL);
mutex_unlock(&host->lock);
flush_workqueue(host->mrq_workqueue);
destroy_workqueue(host->mrq_workqueue);
gb_connection_disable_rx(connection);
mmc_remove_host(mmc);
gb_connection_disable(connection);
gb_connection_destroy(connection);
mmc_free_host(mmc);
}
static const struct gbphy_device_id gb_sdio_id_table[] = {
{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_SDIO) },
{ },
};
MODULE_DEVICE_TABLE(gbphy, gb_sdio_id_table);
static struct gbphy_driver sdio_driver = {
.name = "sdio",
.probe = gb_sdio_probe,
.remove = gb_sdio_remove,
.id_table = gb_sdio_id_table,
};
module_gbphy_driver(sdio_driver);
MODULE_LICENSE("GPL v2");
|