]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/sdio.c
Merge tag 'renesas-fixes-for-v4.13' of https://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / staging / greybus / sdio.c
1 /*
2  * SD/MMC Greybus driver.
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/mmc/core.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/scatterlist.h>
15 #include <linux/workqueue.h>
16
17 #include "greybus.h"
18 #include "gbphy.h"
19
20 struct gb_sdio_host {
21         struct gb_connection    *connection;
22         struct gbphy_device     *gbphy_dev;
23         struct mmc_host         *mmc;
24         struct mmc_request      *mrq;
25         struct mutex            lock;   /* lock for this host */
26         size_t                  data_max;
27         spinlock_t              xfer;   /* lock to cancel ongoing transfer */
28         bool                    xfer_stop;
29         struct workqueue_struct *mrq_workqueue;
30         struct work_struct      mrqwork;
31         u8                      queued_events;
32         bool                    removed;
33         bool                    card_present;
34         bool                    read_only;
35 };
36
37
38 #define GB_SDIO_RSP_R1_R5_R6_R7 (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
39                                  GB_SDIO_RSP_OPCODE)
40 #define GB_SDIO_RSP_R3_R4       (GB_SDIO_RSP_PRESENT)
41 #define GB_SDIO_RSP_R2          (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
42                                  GB_SDIO_RSP_136)
43 #define GB_SDIO_RSP_R1B         (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
44                                  GB_SDIO_RSP_OPCODE | GB_SDIO_RSP_BUSY)
45
46 /* kernel vdd starts at 0x80 and we need to translate to greybus ones 0x01 */
47 #define GB_SDIO_VDD_SHIFT       8
48
49 #ifndef MMC_CAP2_CORE_RUNTIME_PM
50 #define MMC_CAP2_CORE_RUNTIME_PM        0
51 #endif
52
53 static inline bool single_op(struct mmc_command *cmd)
54 {
55         u32 opcode = cmd->opcode;
56
57         return opcode == MMC_WRITE_BLOCK ||
58                opcode == MMC_READ_SINGLE_BLOCK;
59 }
60
61 static void _gb_sdio_set_host_caps(struct gb_sdio_host *host, u32 r)
62 {
63         u32 caps = 0;
64         u32 caps2 = 0;
65
66         caps = ((r & GB_SDIO_CAP_NONREMOVABLE) ? MMC_CAP_NONREMOVABLE : 0) |
67                 ((r & GB_SDIO_CAP_4_BIT_DATA) ? MMC_CAP_4_BIT_DATA : 0) |
68                 ((r & GB_SDIO_CAP_8_BIT_DATA) ? MMC_CAP_8_BIT_DATA : 0) |
69                 ((r & GB_SDIO_CAP_MMC_HS) ? MMC_CAP_MMC_HIGHSPEED : 0) |
70                 ((r & GB_SDIO_CAP_SD_HS) ? MMC_CAP_SD_HIGHSPEED : 0) |
71                 ((r & GB_SDIO_CAP_ERASE) ? MMC_CAP_ERASE : 0) |
72                 ((r & GB_SDIO_CAP_1_2V_DDR) ? MMC_CAP_1_2V_DDR : 0) |
73                 ((r & GB_SDIO_CAP_1_8V_DDR) ? MMC_CAP_1_8V_DDR : 0) |
74                 ((r & GB_SDIO_CAP_POWER_OFF_CARD) ? MMC_CAP_POWER_OFF_CARD : 0) |
75                 ((r & GB_SDIO_CAP_UHS_SDR12) ? MMC_CAP_UHS_SDR12 : 0) |
76                 ((r & GB_SDIO_CAP_UHS_SDR25) ? MMC_CAP_UHS_SDR25 : 0) |
77                 ((r & GB_SDIO_CAP_UHS_SDR50) ? MMC_CAP_UHS_SDR50 : 0) |
78                 ((r & GB_SDIO_CAP_UHS_SDR104) ? MMC_CAP_UHS_SDR104 : 0) |
79                 ((r & GB_SDIO_CAP_UHS_DDR50) ? MMC_CAP_UHS_DDR50 : 0) |
80                 ((r & GB_SDIO_CAP_DRIVER_TYPE_A) ? MMC_CAP_DRIVER_TYPE_A : 0) |
81                 ((r & GB_SDIO_CAP_DRIVER_TYPE_C) ? MMC_CAP_DRIVER_TYPE_C : 0) |
82                 ((r & GB_SDIO_CAP_DRIVER_TYPE_D) ? MMC_CAP_DRIVER_TYPE_D : 0);
83
84         caps2 = ((r & GB_SDIO_CAP_HS200_1_2V) ? MMC_CAP2_HS200_1_2V_SDR : 0) |
85                 ((r & GB_SDIO_CAP_HS400_1_2V) ? MMC_CAP2_HS400_1_2V : 0) |
86                 ((r & GB_SDIO_CAP_HS400_1_8V) ? MMC_CAP2_HS400_1_8V : 0) |
87                 ((r & GB_SDIO_CAP_HS200_1_8V) ? MMC_CAP2_HS200_1_8V_SDR : 0);
88
89         host->mmc->caps = caps;
90         host->mmc->caps2 = caps2 | MMC_CAP2_CORE_RUNTIME_PM;
91
92         if (caps & MMC_CAP_NONREMOVABLE)
93                 host->card_present = true;
94 }
95
96 static u32 _gb_sdio_get_host_ocr(u32 ocr)
97 {
98         return (((ocr & GB_SDIO_VDD_165_195) ? MMC_VDD_165_195 : 0) |
99                 ((ocr & GB_SDIO_VDD_20_21) ? MMC_VDD_20_21 : 0) |
100                 ((ocr & GB_SDIO_VDD_21_22) ? MMC_VDD_21_22 : 0) |
101                 ((ocr & GB_SDIO_VDD_22_23) ? MMC_VDD_22_23 : 0) |
102                 ((ocr & GB_SDIO_VDD_23_24) ? MMC_VDD_23_24 : 0) |
103                 ((ocr & GB_SDIO_VDD_24_25) ? MMC_VDD_24_25 : 0) |
104                 ((ocr & GB_SDIO_VDD_25_26) ? MMC_VDD_25_26 : 0) |
105                 ((ocr & GB_SDIO_VDD_26_27) ? MMC_VDD_26_27 : 0) |
106                 ((ocr & GB_SDIO_VDD_27_28) ? MMC_VDD_27_28 : 0) |
107                 ((ocr & GB_SDIO_VDD_28_29) ? MMC_VDD_28_29 : 0) |
108                 ((ocr & GB_SDIO_VDD_29_30) ? MMC_VDD_29_30 : 0) |
109                 ((ocr & GB_SDIO_VDD_30_31) ? MMC_VDD_30_31 : 0) |
110                 ((ocr & GB_SDIO_VDD_31_32) ? MMC_VDD_31_32 : 0) |
111                 ((ocr & GB_SDIO_VDD_32_33) ? MMC_VDD_32_33 : 0) |
112                 ((ocr & GB_SDIO_VDD_33_34) ? MMC_VDD_33_34 : 0) |
113                 ((ocr & GB_SDIO_VDD_34_35) ? MMC_VDD_34_35 : 0) |
114                 ((ocr & GB_SDIO_VDD_35_36) ? MMC_VDD_35_36 : 0)
115                 );
116 }
117
118 static int gb_sdio_get_caps(struct gb_sdio_host *host)
119 {
120         struct gb_sdio_get_caps_response response;
121         struct mmc_host *mmc = host->mmc;
122         u16 data_max;
123         u32 blksz;
124         u32 ocr;
125         u32 r;
126         int ret;
127
128         ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_GET_CAPABILITIES,
129                                 NULL, 0, &response, sizeof(response));
130         if (ret < 0)
131                 return ret;
132         r = le32_to_cpu(response.caps);
133
134         _gb_sdio_set_host_caps(host, r);
135
136         /* get the max block size that could fit our payload */
137         data_max = gb_operation_get_payload_size_max(host->connection);
138         data_max = min(data_max - sizeof(struct gb_sdio_transfer_request),
139                        data_max - sizeof(struct gb_sdio_transfer_response));
140
141         blksz = min_t(u16, le16_to_cpu(response.max_blk_size), data_max);
142         blksz = max_t(u32, 512, blksz);
143
144         mmc->max_blk_size = rounddown_pow_of_two(blksz);
145         mmc->max_blk_count = le16_to_cpu(response.max_blk_count);
146         host->data_max = data_max;
147
148         /* get ocr supported values */
149         ocr = _gb_sdio_get_host_ocr(le32_to_cpu(response.ocr));
150         mmc->ocr_avail = ocr;
151         mmc->ocr_avail_sdio = mmc->ocr_avail;
152         mmc->ocr_avail_sd = mmc->ocr_avail;
153         mmc->ocr_avail_mmc = mmc->ocr_avail;
154
155         /* get frequency range values */
156         mmc->f_min = le32_to_cpu(response.f_min);
157         mmc->f_max = le32_to_cpu(response.f_max);
158
159         return 0;
160 }
161
162 static void _gb_queue_event(struct gb_sdio_host *host, u8 event)
163 {
164         if (event & GB_SDIO_CARD_INSERTED)
165                 host->queued_events &= ~GB_SDIO_CARD_REMOVED;
166         else if (event & GB_SDIO_CARD_REMOVED)
167                 host->queued_events &= ~GB_SDIO_CARD_INSERTED;
168
169         host->queued_events |= event;
170 }
171
172 static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
173 {
174         u8 state_changed = 0;
175
176         if (event & GB_SDIO_CARD_INSERTED) {
177                 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
178                         return 0;
179                 if (host->card_present)
180                         return 0;
181                 host->card_present = true;
182                 state_changed = 1;
183         }
184
185         if (event & GB_SDIO_CARD_REMOVED) {
186                 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
187                         return 0;
188                 if (!(host->card_present))
189                         return 0;
190                 host->card_present = false;
191                 state_changed = 1;
192         }
193
194         if (event & GB_SDIO_WP)
195                 host->read_only = true;
196
197         if (state_changed) {
198                 dev_info(mmc_dev(host->mmc), "card %s now event\n",
199                          (host->card_present ?  "inserted" : "removed"));
200                 mmc_detect_change(host->mmc, 0);
201         }
202
203         return 0;
204 }
205
206 static int gb_sdio_request_handler(struct gb_operation *op)
207 {
208         struct gb_sdio_host *host = gb_connection_get_data(op->connection);
209         struct gb_message *request;
210         struct gb_sdio_event_request *payload;
211         u8 type = op->type;
212         int ret =  0;
213         u8 event;
214
215         if (type != GB_SDIO_TYPE_EVENT) {
216                 dev_err(mmc_dev(host->mmc),
217                         "unsupported unsolicited event: %u\n", type);
218                 return -EINVAL;
219         }
220
221         request = op->request;
222
223         if (request->payload_size < sizeof(*payload)) {
224                 dev_err(mmc_dev(host->mmc), "wrong event size received (%zu < %zu)\n",
225                         request->payload_size, sizeof(*payload));
226                 return -EINVAL;
227         }
228
229         payload = request->payload;
230         event = payload->event;
231
232         if (host->removed)
233                 _gb_queue_event(host, event);
234         else
235                 ret = _gb_sdio_process_events(host, event);
236
237         return ret;
238 }
239
240 static int gb_sdio_set_ios(struct gb_sdio_host *host,
241                            struct gb_sdio_set_ios_request *request)
242 {
243         int ret;
244
245         ret = gbphy_runtime_get_sync(host->gbphy_dev);
246         if (ret)
247                 return ret;
248
249         ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_SET_IOS, request,
250                                 sizeof(*request), NULL, 0);
251
252         gbphy_runtime_put_autosuspend(host->gbphy_dev);
253
254         return ret;
255 }
256
257 static int _gb_sdio_send(struct gb_sdio_host *host, struct mmc_data *data,
258                          size_t len, u16 nblocks, off_t skip)
259 {
260         struct gb_sdio_transfer_request *request;
261         struct gb_sdio_transfer_response *response;
262         struct gb_operation *operation;
263         struct scatterlist *sg = data->sg;
264         unsigned int sg_len = data->sg_len;
265         size_t copied;
266         u16 send_blksz;
267         u16 send_blocks;
268         int ret;
269
270         WARN_ON(len > host->data_max);
271
272         operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
273                                         len + sizeof(*request),
274                                         sizeof(*response), GFP_KERNEL);
275         if (!operation)
276                 return -ENOMEM;
277
278         request = operation->request->payload;
279         request->data_flags = (data->flags >> 8);
280         request->data_blocks = cpu_to_le16(nblocks);
281         request->data_blksz = cpu_to_le16(data->blksz);
282
283         copied = sg_pcopy_to_buffer(sg, sg_len, &request->data[0], len, skip);
284
285         if (copied != len) {
286                 ret = -EINVAL;
287                 goto err_put_operation;
288         }
289
290         ret = gb_operation_request_send_sync(operation);
291         if (ret < 0)
292                 goto err_put_operation;
293
294         response = operation->response->payload;
295
296         send_blocks = le16_to_cpu(response->data_blocks);
297         send_blksz = le16_to_cpu(response->data_blksz);
298
299         if (len != send_blksz * send_blocks) {
300                 dev_err(mmc_dev(host->mmc), "send: size received: %zu != %d\n",
301                         len, send_blksz * send_blocks);
302                 ret = -EINVAL;
303         }
304
305 err_put_operation:
306         gb_operation_put(operation);
307
308         return ret;
309 }
310
311 static int _gb_sdio_recv(struct gb_sdio_host *host, struct mmc_data *data,
312                          size_t len, u16 nblocks, off_t skip)
313 {
314         struct gb_sdio_transfer_request *request;
315         struct gb_sdio_transfer_response *response;
316         struct gb_operation *operation;
317         struct scatterlist *sg = data->sg;
318         unsigned int sg_len = data->sg_len;
319         size_t copied;
320         u16 recv_blksz;
321         u16 recv_blocks;
322         int ret;
323
324         WARN_ON(len > host->data_max);
325
326         operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
327                                         sizeof(*request),
328                                         len + sizeof(*response), GFP_KERNEL);
329         if (!operation)
330                 return -ENOMEM;
331
332         request = operation->request->payload;
333         request->data_flags = (data->flags >> 8);
334         request->data_blocks = cpu_to_le16(nblocks);
335         request->data_blksz = cpu_to_le16(data->blksz);
336
337         ret = gb_operation_request_send_sync(operation);
338         if (ret < 0)
339                 goto err_put_operation;
340
341         response = operation->response->payload;
342         recv_blocks = le16_to_cpu(response->data_blocks);
343         recv_blksz = le16_to_cpu(response->data_blksz);
344
345         if (len != recv_blksz * recv_blocks) {
346                 dev_err(mmc_dev(host->mmc), "recv: size received: %d != %zu\n",
347                         recv_blksz * recv_blocks, len);
348                 ret = -EINVAL;
349                 goto err_put_operation;
350         }
351
352         copied = sg_pcopy_from_buffer(sg, sg_len, &response->data[0], len,
353                                       skip);
354         if (copied != len)
355                 ret = -EINVAL;
356
357 err_put_operation:
358         gb_operation_put(operation);
359
360         return ret;
361 }
362
363 static int gb_sdio_transfer(struct gb_sdio_host *host, struct mmc_data *data)
364 {
365         size_t left, len;
366         off_t skip = 0;
367         int ret = 0;
368         u16 nblocks;
369
370         if (single_op(data->mrq->cmd) && data->blocks > 1) {
371                 ret = -ETIMEDOUT;
372                 goto out;
373         }
374
375         left = data->blksz * data->blocks;
376
377         while (left) {
378                 /* check is a stop transmission is pending */
379                 spin_lock(&host->xfer);
380                 if (host->xfer_stop) {
381                         host->xfer_stop = false;
382                         spin_unlock(&host->xfer);
383                         ret = -EINTR;
384                         goto out;
385                 }
386                 spin_unlock(&host->xfer);
387                 len = min(left, host->data_max);
388                 nblocks = len / data->blksz;
389                 len = nblocks * data->blksz;
390
391                 if (data->flags & MMC_DATA_READ) {
392                         ret = _gb_sdio_recv(host, data, len, nblocks, skip);
393                         if (ret < 0)
394                                 goto out;
395                 } else {
396                         ret = _gb_sdio_send(host, data, len, nblocks, skip);
397                         if (ret < 0)
398                                 goto out;
399                 }
400                 data->bytes_xfered += len;
401                 left -= len;
402                 skip += len;
403         }
404
405 out:
406         data->error = ret;
407         return ret;
408 }
409
410 static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
411 {
412         struct gb_sdio_command_request request = {0};
413         struct gb_sdio_command_response response;
414         struct mmc_data *data = host->mrq->data;
415         u8 cmd_flags;
416         u8 cmd_type;
417         int i;
418         int ret;
419
420         switch (mmc_resp_type(cmd)) {
421         case MMC_RSP_NONE:
422                 cmd_flags = GB_SDIO_RSP_NONE;
423                 break;
424         case MMC_RSP_R1:
425                 cmd_flags = GB_SDIO_RSP_R1_R5_R6_R7;
426                 break;
427         case MMC_RSP_R1B:
428                 cmd_flags = GB_SDIO_RSP_R1B;
429                 break;
430         case MMC_RSP_R2:
431                 cmd_flags = GB_SDIO_RSP_R2;
432                 break;
433         case MMC_RSP_R3:
434                 cmd_flags = GB_SDIO_RSP_R3_R4;
435                 break;
436         default:
437                 dev_err(mmc_dev(host->mmc), "cmd flag invalid 0x%04x\n",
438                         mmc_resp_type(cmd));
439                 ret = -EINVAL;
440                 goto out;
441         }
442
443         switch (mmc_cmd_type(cmd)) {
444         case MMC_CMD_BC:
445                 cmd_type = GB_SDIO_CMD_BC;
446                 break;
447         case MMC_CMD_BCR:
448                 cmd_type = GB_SDIO_CMD_BCR;
449                 break;
450         case MMC_CMD_AC:
451                 cmd_type = GB_SDIO_CMD_AC;
452                 break;
453         case MMC_CMD_ADTC:
454                 cmd_type = GB_SDIO_CMD_ADTC;
455                 break;
456         default:
457                 dev_err(mmc_dev(host->mmc), "cmd type invalid 0x%04x\n",
458                         mmc_cmd_type(cmd));
459                 ret = -EINVAL;
460                 goto out;
461         }
462
463         request.cmd = cmd->opcode;
464         request.cmd_flags = cmd_flags;
465         request.cmd_type = cmd_type;
466         request.cmd_arg = cpu_to_le32(cmd->arg);
467         /* some controllers need to know at command time data details */
468         if (data) {
469                 request.data_blocks = cpu_to_le16(data->blocks);
470                 request.data_blksz = cpu_to_le16(data->blksz);
471         }
472
473         ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_COMMAND,
474                                 &request, sizeof(request), &response,
475                                 sizeof(response));
476         if (ret < 0)
477                 goto out;
478
479         /* no response expected */
480         if (cmd_flags == GB_SDIO_RSP_NONE)
481                 goto out;
482
483         /* long response expected */
484         if (cmd_flags & GB_SDIO_RSP_R2)
485                 for (i = 0; i < 4; i++)
486                         cmd->resp[i] = le32_to_cpu(response.resp[i]);
487         else
488                 cmd->resp[0] = le32_to_cpu(response.resp[0]);
489
490 out:
491         cmd->error = ret;
492         return ret;
493 }
494
495 static void gb_sdio_mrq_work(struct work_struct *work)
496 {
497         struct gb_sdio_host *host;
498         struct mmc_request *mrq;
499         int ret;
500
501         host = container_of(work, struct gb_sdio_host, mrqwork);
502
503         ret = gbphy_runtime_get_sync(host->gbphy_dev);
504         if (ret)
505                 return;
506
507         mutex_lock(&host->lock);
508         mrq = host->mrq;
509         if (!mrq) {
510                 mutex_unlock(&host->lock);
511                 gbphy_runtime_put_autosuspend(host->gbphy_dev);
512                 dev_err(mmc_dev(host->mmc), "mmc request is NULL");
513                 return;
514         }
515
516         if (host->removed) {
517                 mrq->cmd->error = -ESHUTDOWN;
518                 goto done;
519         }
520
521         if (mrq->sbc) {
522                 ret = gb_sdio_command(host, mrq->sbc);
523                 if (ret < 0)
524                         goto done;
525         }
526
527         ret = gb_sdio_command(host, mrq->cmd);
528         if (ret < 0)
529                 goto done;
530
531         if (mrq->data) {
532                 ret = gb_sdio_transfer(host, mrq->data);
533                 if (ret < 0)
534                         goto done;
535         }
536
537         if (mrq->stop) {
538                 ret = gb_sdio_command(host, mrq->stop);
539                 if (ret < 0)
540                         goto done;
541         }
542
543 done:
544         host->mrq = NULL;
545         mutex_unlock(&host->lock);
546         mmc_request_done(host->mmc, mrq);
547         gbphy_runtime_put_autosuspend(host->gbphy_dev);
548 }
549
550 static void gb_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
551 {
552         struct gb_sdio_host *host = mmc_priv(mmc);
553         struct mmc_command *cmd = mrq->cmd;
554
555         /* Check if it is a cancel to ongoing transfer */
556         if (cmd->opcode == MMC_STOP_TRANSMISSION) {
557                 spin_lock(&host->xfer);
558                 host->xfer_stop = true;
559                 spin_unlock(&host->xfer);
560         }
561
562         mutex_lock(&host->lock);
563
564         WARN_ON(host->mrq);
565         host->mrq = mrq;
566
567         if (host->removed) {
568                 mrq->cmd->error = -ESHUTDOWN;
569                 goto out;
570         }
571         if (!host->card_present) {
572                 mrq->cmd->error = -ENOMEDIUM;
573                 goto out;
574         }
575
576         queue_work(host->mrq_workqueue, &host->mrqwork);
577
578         mutex_unlock(&host->lock);
579         return;
580
581 out:
582         host->mrq = NULL;
583         mutex_unlock(&host->lock);
584         mmc_request_done(mmc, mrq);
585 }
586
587 static void gb_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
588 {
589         struct gb_sdio_host *host = mmc_priv(mmc);
590         struct gb_sdio_set_ios_request request;
591         int ret;
592         u8 power_mode;
593         u8 bus_width;
594         u8 timing;
595         u8 signal_voltage;
596         u8 drv_type;
597         u32 vdd = 0;
598
599         mutex_lock(&host->lock);
600         request.clock = cpu_to_le32(ios->clock);
601
602         if (ios->vdd)
603                 vdd = 1 << (ios->vdd - GB_SDIO_VDD_SHIFT);
604         request.vdd = cpu_to_le32(vdd);
605
606         request.bus_mode = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN ?
607                             GB_SDIO_BUSMODE_OPENDRAIN :
608                             GB_SDIO_BUSMODE_PUSHPULL);
609
610         switch (ios->power_mode) {
611         case MMC_POWER_OFF:
612         default:
613                 power_mode = GB_SDIO_POWER_OFF;
614                 break;
615         case MMC_POWER_UP:
616                 power_mode = GB_SDIO_POWER_UP;
617                 break;
618         case MMC_POWER_ON:
619                 power_mode = GB_SDIO_POWER_ON;
620                 break;
621         case MMC_POWER_UNDEFINED:
622                 power_mode = GB_SDIO_POWER_UNDEFINED;
623                 break;
624         }
625         request.power_mode = power_mode;
626
627         switch (ios->bus_width) {
628         case MMC_BUS_WIDTH_1:
629                 bus_width = GB_SDIO_BUS_WIDTH_1;
630                 break;
631         case MMC_BUS_WIDTH_4:
632         default:
633                 bus_width = GB_SDIO_BUS_WIDTH_4;
634                 break;
635         case MMC_BUS_WIDTH_8:
636                 bus_width = GB_SDIO_BUS_WIDTH_8;
637                 break;
638         }
639         request.bus_width = bus_width;
640
641         switch (ios->timing) {
642         case MMC_TIMING_LEGACY:
643         default:
644                 timing = GB_SDIO_TIMING_LEGACY;
645                 break;
646         case MMC_TIMING_MMC_HS:
647                 timing = GB_SDIO_TIMING_MMC_HS;
648                 break;
649         case MMC_TIMING_SD_HS:
650                 timing = GB_SDIO_TIMING_SD_HS;
651                 break;
652         case MMC_TIMING_UHS_SDR12:
653                 timing = GB_SDIO_TIMING_UHS_SDR12;
654                 break;
655         case MMC_TIMING_UHS_SDR25:
656                 timing = GB_SDIO_TIMING_UHS_SDR25;
657                 break;
658         case MMC_TIMING_UHS_SDR50:
659                 timing = GB_SDIO_TIMING_UHS_SDR50;
660                 break;
661         case MMC_TIMING_UHS_SDR104:
662                 timing = GB_SDIO_TIMING_UHS_SDR104;
663                 break;
664         case MMC_TIMING_UHS_DDR50:
665                 timing = GB_SDIO_TIMING_UHS_DDR50;
666                 break;
667         case MMC_TIMING_MMC_DDR52:
668                 timing = GB_SDIO_TIMING_MMC_DDR52;
669                 break;
670         case MMC_TIMING_MMC_HS200:
671                 timing = GB_SDIO_TIMING_MMC_HS200;
672                 break;
673         case MMC_TIMING_MMC_HS400:
674                 timing = GB_SDIO_TIMING_MMC_HS400;
675                 break;
676         }
677         request.timing = timing;
678
679         switch (ios->signal_voltage) {
680         case MMC_SIGNAL_VOLTAGE_330:
681                 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_330;
682                 break;
683         case MMC_SIGNAL_VOLTAGE_180:
684         default:
685                 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_180;
686                 break;
687         case MMC_SIGNAL_VOLTAGE_120:
688                 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_120;
689                 break;
690         }
691         request.signal_voltage = signal_voltage;
692
693         switch (ios->drv_type) {
694         case MMC_SET_DRIVER_TYPE_A:
695                 drv_type = GB_SDIO_SET_DRIVER_TYPE_A;
696                 break;
697         case MMC_SET_DRIVER_TYPE_C:
698                 drv_type = GB_SDIO_SET_DRIVER_TYPE_C;
699                 break;
700         case MMC_SET_DRIVER_TYPE_D:
701                 drv_type = GB_SDIO_SET_DRIVER_TYPE_D;
702                 break;
703         case MMC_SET_DRIVER_TYPE_B:
704         default:
705                 drv_type = GB_SDIO_SET_DRIVER_TYPE_B;
706                 break;
707         }
708         request.drv_type = drv_type;
709
710         ret = gb_sdio_set_ios(host, &request);
711         if (ret < 0)
712                 goto out;
713
714         memcpy(&mmc->ios, ios, sizeof(mmc->ios));
715
716 out:
717         mutex_unlock(&host->lock);
718 }
719
720 static int gb_mmc_get_ro(struct mmc_host *mmc)
721 {
722         struct gb_sdio_host *host = mmc_priv(mmc);
723
724         mutex_lock(&host->lock);
725         if (host->removed) {
726                 mutex_unlock(&host->lock);
727                 return -ESHUTDOWN;
728         }
729         mutex_unlock(&host->lock);
730
731         return host->read_only;
732 }
733
734 static int gb_mmc_get_cd(struct mmc_host *mmc)
735 {
736         struct gb_sdio_host *host = mmc_priv(mmc);
737
738         mutex_lock(&host->lock);
739         if (host->removed) {
740                 mutex_unlock(&host->lock);
741                 return -ESHUTDOWN;
742         }
743         mutex_unlock(&host->lock);
744
745         return host->card_present;
746 }
747
748 static int gb_mmc_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
749 {
750         return 0;
751 }
752
753 static const struct mmc_host_ops gb_sdio_ops = {
754         .request        = gb_mmc_request,
755         .set_ios        = gb_mmc_set_ios,
756         .get_ro         = gb_mmc_get_ro,
757         .get_cd         = gb_mmc_get_cd,
758         .start_signal_voltage_switch    = gb_mmc_switch_voltage,
759 };
760
761 static int gb_sdio_probe(struct gbphy_device *gbphy_dev,
762                          const struct gbphy_device_id *id)
763 {
764         struct gb_connection *connection;
765         struct mmc_host *mmc;
766         struct gb_sdio_host *host;
767         int ret = 0;
768
769         mmc = mmc_alloc_host(sizeof(*host), &gbphy_dev->dev);
770         if (!mmc)
771                 return -ENOMEM;
772
773         connection = gb_connection_create(gbphy_dev->bundle,
774                                           le16_to_cpu(gbphy_dev->cport_desc->id),
775                                           gb_sdio_request_handler);
776         if (IS_ERR(connection)) {
777                 ret = PTR_ERR(connection);
778                 goto exit_mmc_free;
779         }
780
781         host = mmc_priv(mmc);
782         host->mmc = mmc;
783         host->removed = true;
784
785         host->connection = connection;
786         gb_connection_set_data(connection, host);
787         host->gbphy_dev = gbphy_dev;
788         gb_gbphy_set_data(gbphy_dev, host);
789
790         ret = gb_connection_enable_tx(connection);
791         if (ret)
792                 goto exit_connection_destroy;
793
794         ret = gb_sdio_get_caps(host);
795         if (ret < 0)
796                 goto exit_connection_disable;
797
798         mmc->ops = &gb_sdio_ops;
799
800         mmc->max_segs = host->mmc->max_blk_count;
801
802         /* for now we make a map 1:1 between max request and segment size */
803         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
804         mmc->max_seg_size = mmc->max_req_size;
805
806         mutex_init(&host->lock);
807         spin_lock_init(&host->xfer);
808         host->mrq_workqueue = alloc_workqueue("mmc-%s", 0, 1,
809                                               dev_name(&gbphy_dev->dev));
810         if (!host->mrq_workqueue) {
811                 ret = -ENOMEM;
812                 goto exit_connection_disable;
813         }
814         INIT_WORK(&host->mrqwork, gb_sdio_mrq_work);
815
816         ret = gb_connection_enable(connection);
817         if (ret)
818                 goto exit_wq_destroy;
819
820         ret = mmc_add_host(mmc);
821         if (ret < 0)
822                 goto exit_wq_destroy;
823         host->removed = false;
824         ret = _gb_sdio_process_events(host, host->queued_events);
825         host->queued_events = 0;
826
827         gbphy_runtime_put_autosuspend(gbphy_dev);
828
829         return ret;
830
831 exit_wq_destroy:
832         destroy_workqueue(host->mrq_workqueue);
833 exit_connection_disable:
834         gb_connection_disable(connection);
835 exit_connection_destroy:
836         gb_connection_destroy(connection);
837 exit_mmc_free:
838         mmc_free_host(mmc);
839
840         return ret;
841 }
842
843 static void gb_sdio_remove(struct gbphy_device *gbphy_dev)
844 {
845         struct gb_sdio_host *host = gb_gbphy_get_data(gbphy_dev);
846         struct gb_connection *connection = host->connection;
847         struct mmc_host *mmc;
848         int ret;
849
850         ret = gbphy_runtime_get_sync(gbphy_dev);
851         if (ret)
852                 gbphy_runtime_get_noresume(gbphy_dev);
853
854         mutex_lock(&host->lock);
855         host->removed = true;
856         mmc = host->mmc;
857         gb_connection_set_data(connection, NULL);
858         mutex_unlock(&host->lock);
859
860         flush_workqueue(host->mrq_workqueue);
861         destroy_workqueue(host->mrq_workqueue);
862         gb_connection_disable_rx(connection);
863         mmc_remove_host(mmc);
864         gb_connection_disable(connection);
865         gb_connection_destroy(connection);
866         mmc_free_host(mmc);
867 }
868
869 static const struct gbphy_device_id gb_sdio_id_table[] = {
870         { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_SDIO) },
871         { },
872 };
873 MODULE_DEVICE_TABLE(gbphy, gb_sdio_id_table);
874
875 static struct gbphy_driver sdio_driver = {
876         .name           = "sdio",
877         .probe          = gb_sdio_probe,
878         .remove         = gb_sdio_remove,
879         .id_table       = gb_sdio_id_table,
880 };
881
882 module_gbphy_driver(sdio_driver);
883 MODULE_LICENSE("GPL v2");