0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/mmc/core.h>
0011 #include <linux/mmc/host.h>
0012 #include <linux/mmc/mmc.h>
0013 #include <linux/scatterlist.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/greybus.h>
0016
0017 #include "gbphy.h"
0018
0019 struct gb_sdio_host {
0020 struct gb_connection *connection;
0021 struct gbphy_device *gbphy_dev;
0022 struct mmc_host *mmc;
0023 struct mmc_request *mrq;
0024 struct mutex lock;
0025 size_t data_max;
0026 spinlock_t xfer;
0027 bool xfer_stop;
0028 struct workqueue_struct *mrq_workqueue;
0029 struct work_struct mrqwork;
0030 u8 queued_events;
0031 bool removed;
0032 bool card_present;
0033 bool read_only;
0034 };
0035
0036 #define GB_SDIO_RSP_R1_R5_R6_R7 (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
0037 GB_SDIO_RSP_OPCODE)
0038 #define GB_SDIO_RSP_R3_R4 (GB_SDIO_RSP_PRESENT)
0039 #define GB_SDIO_RSP_R2 (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
0040 GB_SDIO_RSP_136)
0041 #define GB_SDIO_RSP_R1B (GB_SDIO_RSP_PRESENT | GB_SDIO_RSP_CRC | \
0042 GB_SDIO_RSP_OPCODE | GB_SDIO_RSP_BUSY)
0043
0044
0045 #define GB_SDIO_VDD_SHIFT 8
0046
0047 #ifndef MMC_CAP2_CORE_RUNTIME_PM
0048 #define MMC_CAP2_CORE_RUNTIME_PM 0
0049 #endif
0050
0051 static inline bool single_op(struct mmc_command *cmd)
0052 {
0053 u32 opcode = cmd->opcode;
0054
0055 return opcode == MMC_WRITE_BLOCK ||
0056 opcode == MMC_READ_SINGLE_BLOCK;
0057 }
0058
0059 static void _gb_sdio_set_host_caps(struct gb_sdio_host *host, u32 r)
0060 {
0061 u32 caps = 0;
0062 u32 caps2 = 0;
0063
0064 caps = ((r & GB_SDIO_CAP_NONREMOVABLE) ? MMC_CAP_NONREMOVABLE : 0) |
0065 ((r & GB_SDIO_CAP_4_BIT_DATA) ? MMC_CAP_4_BIT_DATA : 0) |
0066 ((r & GB_SDIO_CAP_8_BIT_DATA) ? MMC_CAP_8_BIT_DATA : 0) |
0067 ((r & GB_SDIO_CAP_MMC_HS) ? MMC_CAP_MMC_HIGHSPEED : 0) |
0068 ((r & GB_SDIO_CAP_SD_HS) ? MMC_CAP_SD_HIGHSPEED : 0) |
0069 ((r & GB_SDIO_CAP_1_2V_DDR) ? MMC_CAP_1_2V_DDR : 0) |
0070 ((r & GB_SDIO_CAP_1_8V_DDR) ? MMC_CAP_1_8V_DDR : 0) |
0071 ((r & GB_SDIO_CAP_POWER_OFF_CARD) ? MMC_CAP_POWER_OFF_CARD : 0) |
0072 ((r & GB_SDIO_CAP_UHS_SDR12) ? MMC_CAP_UHS_SDR12 : 0) |
0073 ((r & GB_SDIO_CAP_UHS_SDR25) ? MMC_CAP_UHS_SDR25 : 0) |
0074 ((r & GB_SDIO_CAP_UHS_SDR50) ? MMC_CAP_UHS_SDR50 : 0) |
0075 ((r & GB_SDIO_CAP_UHS_SDR104) ? MMC_CAP_UHS_SDR104 : 0) |
0076 ((r & GB_SDIO_CAP_UHS_DDR50) ? MMC_CAP_UHS_DDR50 : 0) |
0077 ((r & GB_SDIO_CAP_DRIVER_TYPE_A) ? MMC_CAP_DRIVER_TYPE_A : 0) |
0078 ((r & GB_SDIO_CAP_DRIVER_TYPE_C) ? MMC_CAP_DRIVER_TYPE_C : 0) |
0079 ((r & GB_SDIO_CAP_DRIVER_TYPE_D) ? MMC_CAP_DRIVER_TYPE_D : 0);
0080
0081 caps2 = ((r & GB_SDIO_CAP_HS200_1_2V) ? MMC_CAP2_HS200_1_2V_SDR : 0) |
0082 ((r & GB_SDIO_CAP_HS400_1_2V) ? MMC_CAP2_HS400_1_2V : 0) |
0083 ((r & GB_SDIO_CAP_HS400_1_8V) ? MMC_CAP2_HS400_1_8V : 0) |
0084 ((r & GB_SDIO_CAP_HS200_1_8V) ? MMC_CAP2_HS200_1_8V_SDR : 0);
0085
0086 host->mmc->caps = caps;
0087 host->mmc->caps2 = caps2 | MMC_CAP2_CORE_RUNTIME_PM;
0088
0089 if (caps & MMC_CAP_NONREMOVABLE)
0090 host->card_present = true;
0091 }
0092
0093 static u32 _gb_sdio_get_host_ocr(u32 ocr)
0094 {
0095 return (((ocr & GB_SDIO_VDD_165_195) ? MMC_VDD_165_195 : 0) |
0096 ((ocr & GB_SDIO_VDD_20_21) ? MMC_VDD_20_21 : 0) |
0097 ((ocr & GB_SDIO_VDD_21_22) ? MMC_VDD_21_22 : 0) |
0098 ((ocr & GB_SDIO_VDD_22_23) ? MMC_VDD_22_23 : 0) |
0099 ((ocr & GB_SDIO_VDD_23_24) ? MMC_VDD_23_24 : 0) |
0100 ((ocr & GB_SDIO_VDD_24_25) ? MMC_VDD_24_25 : 0) |
0101 ((ocr & GB_SDIO_VDD_25_26) ? MMC_VDD_25_26 : 0) |
0102 ((ocr & GB_SDIO_VDD_26_27) ? MMC_VDD_26_27 : 0) |
0103 ((ocr & GB_SDIO_VDD_27_28) ? MMC_VDD_27_28 : 0) |
0104 ((ocr & GB_SDIO_VDD_28_29) ? MMC_VDD_28_29 : 0) |
0105 ((ocr & GB_SDIO_VDD_29_30) ? MMC_VDD_29_30 : 0) |
0106 ((ocr & GB_SDIO_VDD_30_31) ? MMC_VDD_30_31 : 0) |
0107 ((ocr & GB_SDIO_VDD_31_32) ? MMC_VDD_31_32 : 0) |
0108 ((ocr & GB_SDIO_VDD_32_33) ? MMC_VDD_32_33 : 0) |
0109 ((ocr & GB_SDIO_VDD_33_34) ? MMC_VDD_33_34 : 0) |
0110 ((ocr & GB_SDIO_VDD_34_35) ? MMC_VDD_34_35 : 0) |
0111 ((ocr & GB_SDIO_VDD_35_36) ? MMC_VDD_35_36 : 0)
0112 );
0113 }
0114
0115 static int gb_sdio_get_caps(struct gb_sdio_host *host)
0116 {
0117 struct gb_sdio_get_caps_response response;
0118 struct mmc_host *mmc = host->mmc;
0119 u16 data_max;
0120 u32 blksz;
0121 u32 ocr;
0122 u32 r;
0123 int ret;
0124
0125 ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_GET_CAPABILITIES,
0126 NULL, 0, &response, sizeof(response));
0127 if (ret < 0)
0128 return ret;
0129 r = le32_to_cpu(response.caps);
0130
0131 _gb_sdio_set_host_caps(host, r);
0132
0133
0134 data_max = gb_operation_get_payload_size_max(host->connection);
0135 data_max = min(data_max - sizeof(struct gb_sdio_transfer_request),
0136 data_max - sizeof(struct gb_sdio_transfer_response));
0137
0138 blksz = min_t(u16, le16_to_cpu(response.max_blk_size), data_max);
0139 blksz = max_t(u32, 512, blksz);
0140
0141 mmc->max_blk_size = rounddown_pow_of_two(blksz);
0142 mmc->max_blk_count = le16_to_cpu(response.max_blk_count);
0143 host->data_max = data_max;
0144
0145
0146 ocr = _gb_sdio_get_host_ocr(le32_to_cpu(response.ocr));
0147 mmc->ocr_avail = ocr;
0148 mmc->ocr_avail_sdio = mmc->ocr_avail;
0149 mmc->ocr_avail_sd = mmc->ocr_avail;
0150 mmc->ocr_avail_mmc = mmc->ocr_avail;
0151
0152
0153 mmc->f_min = le32_to_cpu(response.f_min);
0154 mmc->f_max = le32_to_cpu(response.f_max);
0155
0156 return 0;
0157 }
0158
0159 static void _gb_queue_event(struct gb_sdio_host *host, u8 event)
0160 {
0161 if (event & GB_SDIO_CARD_INSERTED)
0162 host->queued_events &= ~GB_SDIO_CARD_REMOVED;
0163 else if (event & GB_SDIO_CARD_REMOVED)
0164 host->queued_events &= ~GB_SDIO_CARD_INSERTED;
0165
0166 host->queued_events |= event;
0167 }
0168
0169 static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
0170 {
0171 u8 state_changed = 0;
0172
0173 if (event & GB_SDIO_CARD_INSERTED) {
0174 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
0175 return 0;
0176 if (host->card_present)
0177 return 0;
0178 host->card_present = true;
0179 state_changed = 1;
0180 }
0181
0182 if (event & GB_SDIO_CARD_REMOVED) {
0183 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
0184 return 0;
0185 if (!(host->card_present))
0186 return 0;
0187 host->card_present = false;
0188 state_changed = 1;
0189 }
0190
0191 if (event & GB_SDIO_WP)
0192 host->read_only = true;
0193
0194 if (state_changed) {
0195 dev_info(mmc_dev(host->mmc), "card %s now event\n",
0196 (host->card_present ? "inserted" : "removed"));
0197 mmc_detect_change(host->mmc, 0);
0198 }
0199
0200 return 0;
0201 }
0202
0203 static int gb_sdio_request_handler(struct gb_operation *op)
0204 {
0205 struct gb_sdio_host *host = gb_connection_get_data(op->connection);
0206 struct gb_message *request;
0207 struct gb_sdio_event_request *payload;
0208 u8 type = op->type;
0209 int ret = 0;
0210 u8 event;
0211
0212 if (type != GB_SDIO_TYPE_EVENT) {
0213 dev_err(mmc_dev(host->mmc),
0214 "unsupported unsolicited event: %u\n", type);
0215 return -EINVAL;
0216 }
0217
0218 request = op->request;
0219
0220 if (request->payload_size < sizeof(*payload)) {
0221 dev_err(mmc_dev(host->mmc), "wrong event size received (%zu < %zu)\n",
0222 request->payload_size, sizeof(*payload));
0223 return -EINVAL;
0224 }
0225
0226 payload = request->payload;
0227 event = payload->event;
0228
0229 if (host->removed)
0230 _gb_queue_event(host, event);
0231 else
0232 ret = _gb_sdio_process_events(host, event);
0233
0234 return ret;
0235 }
0236
0237 static int gb_sdio_set_ios(struct gb_sdio_host *host,
0238 struct gb_sdio_set_ios_request *request)
0239 {
0240 int ret;
0241
0242 ret = gbphy_runtime_get_sync(host->gbphy_dev);
0243 if (ret)
0244 return ret;
0245
0246 ret = gb_operation_sync(host->connection, GB_SDIO_TYPE_SET_IOS, request,
0247 sizeof(*request), NULL, 0);
0248
0249 gbphy_runtime_put_autosuspend(host->gbphy_dev);
0250
0251 return ret;
0252 }
0253
0254 static int _gb_sdio_send(struct gb_sdio_host *host, struct mmc_data *data,
0255 size_t len, u16 nblocks, off_t skip)
0256 {
0257 struct gb_sdio_transfer_request *request;
0258 struct gb_sdio_transfer_response *response;
0259 struct gb_operation *operation;
0260 struct scatterlist *sg = data->sg;
0261 unsigned int sg_len = data->sg_len;
0262 size_t copied;
0263 u16 send_blksz;
0264 u16 send_blocks;
0265 int ret;
0266
0267 WARN_ON(len > host->data_max);
0268
0269 operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
0270 len + sizeof(*request),
0271 sizeof(*response), GFP_KERNEL);
0272 if (!operation)
0273 return -ENOMEM;
0274
0275 request = operation->request->payload;
0276 request->data_flags = data->flags >> 8;
0277 request->data_blocks = cpu_to_le16(nblocks);
0278 request->data_blksz = cpu_to_le16(data->blksz);
0279
0280 copied = sg_pcopy_to_buffer(sg, sg_len, &request->data[0], len, skip);
0281
0282 if (copied != len) {
0283 ret = -EINVAL;
0284 goto err_put_operation;
0285 }
0286
0287 ret = gb_operation_request_send_sync(operation);
0288 if (ret < 0)
0289 goto err_put_operation;
0290
0291 response = operation->response->payload;
0292
0293 send_blocks = le16_to_cpu(response->data_blocks);
0294 send_blksz = le16_to_cpu(response->data_blksz);
0295
0296 if (len != send_blksz * send_blocks) {
0297 dev_err(mmc_dev(host->mmc), "send: size received: %zu != %d\n",
0298 len, send_blksz * send_blocks);
0299 ret = -EINVAL;
0300 }
0301
0302 err_put_operation:
0303 gb_operation_put(operation);
0304
0305 return ret;
0306 }
0307
0308 static int _gb_sdio_recv(struct gb_sdio_host *host, struct mmc_data *data,
0309 size_t len, u16 nblocks, off_t skip)
0310 {
0311 struct gb_sdio_transfer_request *request;
0312 struct gb_sdio_transfer_response *response;
0313 struct gb_operation *operation;
0314 struct scatterlist *sg = data->sg;
0315 unsigned int sg_len = data->sg_len;
0316 size_t copied;
0317 u16 recv_blksz;
0318 u16 recv_blocks;
0319 int ret;
0320
0321 WARN_ON(len > host->data_max);
0322
0323 operation = gb_operation_create(host->connection, GB_SDIO_TYPE_TRANSFER,
0324 sizeof(*request),
0325 len + sizeof(*response), GFP_KERNEL);
0326 if (!operation)
0327 return -ENOMEM;
0328
0329 request = operation->request->payload;
0330 request->data_flags = data->flags >> 8;
0331 request->data_blocks = cpu_to_le16(nblocks);
0332 request->data_blksz = cpu_to_le16(data->blksz);
0333
0334 ret = gb_operation_request_send_sync(operation);
0335 if (ret < 0)
0336 goto err_put_operation;
0337
0338 response = operation->response->payload;
0339 recv_blocks = le16_to_cpu(response->data_blocks);
0340 recv_blksz = le16_to_cpu(response->data_blksz);
0341
0342 if (len != recv_blksz * recv_blocks) {
0343 dev_err(mmc_dev(host->mmc), "recv: size received: %d != %zu\n",
0344 recv_blksz * recv_blocks, len);
0345 ret = -EINVAL;
0346 goto err_put_operation;
0347 }
0348
0349 copied = sg_pcopy_from_buffer(sg, sg_len, &response->data[0], len,
0350 skip);
0351 if (copied != len)
0352 ret = -EINVAL;
0353
0354 err_put_operation:
0355 gb_operation_put(operation);
0356
0357 return ret;
0358 }
0359
0360 static int gb_sdio_transfer(struct gb_sdio_host *host, struct mmc_data *data)
0361 {
0362 size_t left, len;
0363 off_t skip = 0;
0364 int ret = 0;
0365 u16 nblocks;
0366
0367 if (single_op(data->mrq->cmd) && data->blocks > 1) {
0368 ret = -ETIMEDOUT;
0369 goto out;
0370 }
0371
0372 left = data->blksz * data->blocks;
0373
0374 while (left) {
0375
0376 spin_lock(&host->xfer);
0377 if (host->xfer_stop) {
0378 host->xfer_stop = false;
0379 spin_unlock(&host->xfer);
0380 ret = -EINTR;
0381 goto out;
0382 }
0383 spin_unlock(&host->xfer);
0384 len = min(left, host->data_max);
0385 nblocks = len / data->blksz;
0386 len = nblocks * data->blksz;
0387
0388 if (data->flags & MMC_DATA_READ) {
0389 ret = _gb_sdio_recv(host, data, len, nblocks, skip);
0390 if (ret < 0)
0391 goto out;
0392 } else {
0393 ret = _gb_sdio_send(host, data, len, nblocks, skip);
0394 if (ret < 0)
0395 goto out;
0396 }
0397 data->bytes_xfered += len;
0398 left -= len;
0399 skip += len;
0400 }
0401
0402 out:
0403 data->error = ret;
0404 return ret;
0405 }
0406
0407 static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
0408 {
0409 struct gb_sdio_command_request request = {0};
0410 struct gb_sdio_command_response response;
0411 struct mmc_data *data = host->mrq->data;
0412 unsigned int timeout_ms;
0413 u8 cmd_flags;
0414 u8 cmd_type;
0415 int i;
0416 int ret;
0417
0418 switch (mmc_resp_type(cmd)) {
0419 case MMC_RSP_NONE:
0420 cmd_flags = GB_SDIO_RSP_NONE;
0421 break;
0422 case MMC_RSP_R1:
0423 cmd_flags = GB_SDIO_RSP_R1_R5_R6_R7;
0424 break;
0425 case MMC_RSP_R1B:
0426 cmd_flags = GB_SDIO_RSP_R1B;
0427 break;
0428 case MMC_RSP_R2:
0429 cmd_flags = GB_SDIO_RSP_R2;
0430 break;
0431 case MMC_RSP_R3:
0432 cmd_flags = GB_SDIO_RSP_R3_R4;
0433 break;
0434 default:
0435 dev_err(mmc_dev(host->mmc), "cmd flag invalid 0x%04x\n",
0436 mmc_resp_type(cmd));
0437 ret = -EINVAL;
0438 goto out;
0439 }
0440
0441 switch (mmc_cmd_type(cmd)) {
0442 case MMC_CMD_BC:
0443 cmd_type = GB_SDIO_CMD_BC;
0444 break;
0445 case MMC_CMD_BCR:
0446 cmd_type = GB_SDIO_CMD_BCR;
0447 break;
0448 case MMC_CMD_AC:
0449 cmd_type = GB_SDIO_CMD_AC;
0450 break;
0451 case MMC_CMD_ADTC:
0452 cmd_type = GB_SDIO_CMD_ADTC;
0453 break;
0454 default:
0455 dev_err(mmc_dev(host->mmc), "cmd type invalid 0x%04x\n",
0456 mmc_cmd_type(cmd));
0457 ret = -EINVAL;
0458 goto out;
0459 }
0460
0461 request.cmd = cmd->opcode;
0462 request.cmd_flags = cmd_flags;
0463 request.cmd_type = cmd_type;
0464 request.cmd_arg = cpu_to_le32(cmd->arg);
0465
0466 if (data) {
0467 request.data_blocks = cpu_to_le16(data->blocks);
0468 request.data_blksz = cpu_to_le16(data->blksz);
0469 }
0470
0471 timeout_ms = cmd->busy_timeout ? cmd->busy_timeout :
0472 GB_OPERATION_TIMEOUT_DEFAULT;
0473
0474 ret = gb_operation_sync_timeout(host->connection, GB_SDIO_TYPE_COMMAND,
0475 &request, sizeof(request), &response,
0476 sizeof(response), timeout_ms);
0477 if (ret < 0)
0478 goto out;
0479
0480
0481 if (cmd_flags == GB_SDIO_RSP_NONE)
0482 goto out;
0483
0484
0485 if (cmd_flags & GB_SDIO_RSP_R2)
0486 for (i = 0; i < 4; i++)
0487 cmd->resp[i] = le32_to_cpu(response.resp[i]);
0488 else
0489 cmd->resp[0] = le32_to_cpu(response.resp[0]);
0490
0491 out:
0492 cmd->error = ret;
0493 return ret;
0494 }
0495
0496 static void gb_sdio_mrq_work(struct work_struct *work)
0497 {
0498 struct gb_sdio_host *host;
0499 struct mmc_request *mrq;
0500 int ret;
0501
0502 host = container_of(work, struct gb_sdio_host, mrqwork);
0503
0504 ret = gbphy_runtime_get_sync(host->gbphy_dev);
0505 if (ret)
0506 return;
0507
0508 mutex_lock(&host->lock);
0509 mrq = host->mrq;
0510 if (!mrq) {
0511 mutex_unlock(&host->lock);
0512 gbphy_runtime_put_autosuspend(host->gbphy_dev);
0513 dev_err(mmc_dev(host->mmc), "mmc request is NULL");
0514 return;
0515 }
0516
0517 if (host->removed) {
0518 mrq->cmd->error = -ESHUTDOWN;
0519 goto done;
0520 }
0521
0522 if (mrq->sbc) {
0523 ret = gb_sdio_command(host, mrq->sbc);
0524 if (ret < 0)
0525 goto done;
0526 }
0527
0528 ret = gb_sdio_command(host, mrq->cmd);
0529 if (ret < 0)
0530 goto done;
0531
0532 if (mrq->data) {
0533 ret = gb_sdio_transfer(host, mrq->data);
0534 if (ret < 0)
0535 goto done;
0536 }
0537
0538 if (mrq->stop) {
0539 ret = gb_sdio_command(host, mrq->stop);
0540 if (ret < 0)
0541 goto done;
0542 }
0543
0544 done:
0545 host->mrq = NULL;
0546 mutex_unlock(&host->lock);
0547 mmc_request_done(host->mmc, mrq);
0548 gbphy_runtime_put_autosuspend(host->gbphy_dev);
0549 }
0550
0551 static void gb_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
0552 {
0553 struct gb_sdio_host *host = mmc_priv(mmc);
0554 struct mmc_command *cmd = mrq->cmd;
0555
0556
0557 if (cmd->opcode == MMC_STOP_TRANSMISSION) {
0558 spin_lock(&host->xfer);
0559 host->xfer_stop = true;
0560 spin_unlock(&host->xfer);
0561 }
0562
0563 mutex_lock(&host->lock);
0564
0565 WARN_ON(host->mrq);
0566 host->mrq = mrq;
0567
0568 if (host->removed) {
0569 mrq->cmd->error = -ESHUTDOWN;
0570 goto out;
0571 }
0572 if (!host->card_present) {
0573 mrq->cmd->error = -ENOMEDIUM;
0574 goto out;
0575 }
0576
0577 queue_work(host->mrq_workqueue, &host->mrqwork);
0578
0579 mutex_unlock(&host->lock);
0580 return;
0581
0582 out:
0583 host->mrq = NULL;
0584 mutex_unlock(&host->lock);
0585 mmc_request_done(mmc, mrq);
0586 }
0587
0588 static void gb_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
0589 {
0590 struct gb_sdio_host *host = mmc_priv(mmc);
0591 struct gb_sdio_set_ios_request request;
0592 int ret;
0593 u8 power_mode;
0594 u8 bus_width;
0595 u8 timing;
0596 u8 signal_voltage;
0597 u8 drv_type;
0598 u32 vdd = 0;
0599
0600 mutex_lock(&host->lock);
0601 request.clock = cpu_to_le32(ios->clock);
0602
0603 if (ios->vdd)
0604 vdd = 1 << (ios->vdd - GB_SDIO_VDD_SHIFT);
0605 request.vdd = cpu_to_le32(vdd);
0606
0607 request.bus_mode = ios->bus_mode == MMC_BUSMODE_OPENDRAIN ?
0608 GB_SDIO_BUSMODE_OPENDRAIN :
0609 GB_SDIO_BUSMODE_PUSHPULL;
0610
0611 switch (ios->power_mode) {
0612 case MMC_POWER_OFF:
0613 default:
0614 power_mode = GB_SDIO_POWER_OFF;
0615 break;
0616 case MMC_POWER_UP:
0617 power_mode = GB_SDIO_POWER_UP;
0618 break;
0619 case MMC_POWER_ON:
0620 power_mode = GB_SDIO_POWER_ON;
0621 break;
0622 case MMC_POWER_UNDEFINED:
0623 power_mode = GB_SDIO_POWER_UNDEFINED;
0624 break;
0625 }
0626 request.power_mode = power_mode;
0627
0628 switch (ios->bus_width) {
0629 case MMC_BUS_WIDTH_1:
0630 bus_width = GB_SDIO_BUS_WIDTH_1;
0631 break;
0632 case MMC_BUS_WIDTH_4:
0633 default:
0634 bus_width = GB_SDIO_BUS_WIDTH_4;
0635 break;
0636 case MMC_BUS_WIDTH_8:
0637 bus_width = GB_SDIO_BUS_WIDTH_8;
0638 break;
0639 }
0640 request.bus_width = bus_width;
0641
0642 switch (ios->timing) {
0643 case MMC_TIMING_LEGACY:
0644 default:
0645 timing = GB_SDIO_TIMING_LEGACY;
0646 break;
0647 case MMC_TIMING_MMC_HS:
0648 timing = GB_SDIO_TIMING_MMC_HS;
0649 break;
0650 case MMC_TIMING_SD_HS:
0651 timing = GB_SDIO_TIMING_SD_HS;
0652 break;
0653 case MMC_TIMING_UHS_SDR12:
0654 timing = GB_SDIO_TIMING_UHS_SDR12;
0655 break;
0656 case MMC_TIMING_UHS_SDR25:
0657 timing = GB_SDIO_TIMING_UHS_SDR25;
0658 break;
0659 case MMC_TIMING_UHS_SDR50:
0660 timing = GB_SDIO_TIMING_UHS_SDR50;
0661 break;
0662 case MMC_TIMING_UHS_SDR104:
0663 timing = GB_SDIO_TIMING_UHS_SDR104;
0664 break;
0665 case MMC_TIMING_UHS_DDR50:
0666 timing = GB_SDIO_TIMING_UHS_DDR50;
0667 break;
0668 case MMC_TIMING_MMC_DDR52:
0669 timing = GB_SDIO_TIMING_MMC_DDR52;
0670 break;
0671 case MMC_TIMING_MMC_HS200:
0672 timing = GB_SDIO_TIMING_MMC_HS200;
0673 break;
0674 case MMC_TIMING_MMC_HS400:
0675 timing = GB_SDIO_TIMING_MMC_HS400;
0676 break;
0677 }
0678 request.timing = timing;
0679
0680 switch (ios->signal_voltage) {
0681 case MMC_SIGNAL_VOLTAGE_330:
0682 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_330;
0683 break;
0684 case MMC_SIGNAL_VOLTAGE_180:
0685 default:
0686 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_180;
0687 break;
0688 case MMC_SIGNAL_VOLTAGE_120:
0689 signal_voltage = GB_SDIO_SIGNAL_VOLTAGE_120;
0690 break;
0691 }
0692 request.signal_voltage = signal_voltage;
0693
0694 switch (ios->drv_type) {
0695 case MMC_SET_DRIVER_TYPE_A:
0696 drv_type = GB_SDIO_SET_DRIVER_TYPE_A;
0697 break;
0698 case MMC_SET_DRIVER_TYPE_C:
0699 drv_type = GB_SDIO_SET_DRIVER_TYPE_C;
0700 break;
0701 case MMC_SET_DRIVER_TYPE_D:
0702 drv_type = GB_SDIO_SET_DRIVER_TYPE_D;
0703 break;
0704 case MMC_SET_DRIVER_TYPE_B:
0705 default:
0706 drv_type = GB_SDIO_SET_DRIVER_TYPE_B;
0707 break;
0708 }
0709 request.drv_type = drv_type;
0710
0711 ret = gb_sdio_set_ios(host, &request);
0712 if (ret < 0)
0713 goto out;
0714
0715 memcpy(&mmc->ios, ios, sizeof(mmc->ios));
0716
0717 out:
0718 mutex_unlock(&host->lock);
0719 }
0720
0721 static int gb_mmc_get_ro(struct mmc_host *mmc)
0722 {
0723 struct gb_sdio_host *host = mmc_priv(mmc);
0724
0725 mutex_lock(&host->lock);
0726 if (host->removed) {
0727 mutex_unlock(&host->lock);
0728 return -ESHUTDOWN;
0729 }
0730 mutex_unlock(&host->lock);
0731
0732 return host->read_only;
0733 }
0734
0735 static int gb_mmc_get_cd(struct mmc_host *mmc)
0736 {
0737 struct gb_sdio_host *host = mmc_priv(mmc);
0738
0739 mutex_lock(&host->lock);
0740 if (host->removed) {
0741 mutex_unlock(&host->lock);
0742 return -ESHUTDOWN;
0743 }
0744 mutex_unlock(&host->lock);
0745
0746 return host->card_present;
0747 }
0748
0749 static int gb_mmc_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
0750 {
0751 return 0;
0752 }
0753
0754 static const struct mmc_host_ops gb_sdio_ops = {
0755 .request = gb_mmc_request,
0756 .set_ios = gb_mmc_set_ios,
0757 .get_ro = gb_mmc_get_ro,
0758 .get_cd = gb_mmc_get_cd,
0759 .start_signal_voltage_switch = gb_mmc_switch_voltage,
0760 };
0761
0762 static int gb_sdio_probe(struct gbphy_device *gbphy_dev,
0763 const struct gbphy_device_id *id)
0764 {
0765 struct gb_connection *connection;
0766 struct mmc_host *mmc;
0767 struct gb_sdio_host *host;
0768 int ret = 0;
0769
0770 mmc = mmc_alloc_host(sizeof(*host), &gbphy_dev->dev);
0771 if (!mmc)
0772 return -ENOMEM;
0773
0774 connection = gb_connection_create(gbphy_dev->bundle,
0775 le16_to_cpu(gbphy_dev->cport_desc->id),
0776 gb_sdio_request_handler);
0777 if (IS_ERR(connection)) {
0778 ret = PTR_ERR(connection);
0779 goto exit_mmc_free;
0780 }
0781
0782 host = mmc_priv(mmc);
0783 host->mmc = mmc;
0784 host->removed = true;
0785
0786 host->connection = connection;
0787 gb_connection_set_data(connection, host);
0788 host->gbphy_dev = gbphy_dev;
0789 gb_gbphy_set_data(gbphy_dev, host);
0790
0791 ret = gb_connection_enable_tx(connection);
0792 if (ret)
0793 goto exit_connection_destroy;
0794
0795 ret = gb_sdio_get_caps(host);
0796 if (ret < 0)
0797 goto exit_connection_disable;
0798
0799 mmc->ops = &gb_sdio_ops;
0800
0801 mmc->max_segs = host->mmc->max_blk_count;
0802
0803
0804 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
0805 mmc->max_seg_size = mmc->max_req_size;
0806
0807 mutex_init(&host->lock);
0808 spin_lock_init(&host->xfer);
0809 host->mrq_workqueue = alloc_workqueue("mmc-%s", 0, 1,
0810 dev_name(&gbphy_dev->dev));
0811 if (!host->mrq_workqueue) {
0812 ret = -ENOMEM;
0813 goto exit_connection_disable;
0814 }
0815 INIT_WORK(&host->mrqwork, gb_sdio_mrq_work);
0816
0817 ret = gb_connection_enable(connection);
0818 if (ret)
0819 goto exit_wq_destroy;
0820
0821 ret = mmc_add_host(mmc);
0822 if (ret < 0)
0823 goto exit_wq_destroy;
0824 host->removed = false;
0825 ret = _gb_sdio_process_events(host, host->queued_events);
0826 host->queued_events = 0;
0827
0828 gbphy_runtime_put_autosuspend(gbphy_dev);
0829
0830 return ret;
0831
0832 exit_wq_destroy:
0833 destroy_workqueue(host->mrq_workqueue);
0834 exit_connection_disable:
0835 gb_connection_disable(connection);
0836 exit_connection_destroy:
0837 gb_connection_destroy(connection);
0838 exit_mmc_free:
0839 mmc_free_host(mmc);
0840
0841 return ret;
0842 }
0843
0844 static void gb_sdio_remove(struct gbphy_device *gbphy_dev)
0845 {
0846 struct gb_sdio_host *host = gb_gbphy_get_data(gbphy_dev);
0847 struct gb_connection *connection = host->connection;
0848 struct mmc_host *mmc;
0849 int ret;
0850
0851 ret = gbphy_runtime_get_sync(gbphy_dev);
0852 if (ret)
0853 gbphy_runtime_get_noresume(gbphy_dev);
0854
0855 mutex_lock(&host->lock);
0856 host->removed = true;
0857 mmc = host->mmc;
0858 gb_connection_set_data(connection, NULL);
0859 mutex_unlock(&host->lock);
0860
0861 destroy_workqueue(host->mrq_workqueue);
0862 gb_connection_disable_rx(connection);
0863 mmc_remove_host(mmc);
0864 gb_connection_disable(connection);
0865 gb_connection_destroy(connection);
0866 mmc_free_host(mmc);
0867 }
0868
0869 static const struct gbphy_device_id gb_sdio_id_table[] = {
0870 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_SDIO) },
0871 { },
0872 };
0873 MODULE_DEVICE_TABLE(gbphy, gb_sdio_id_table);
0874
0875 static struct gbphy_driver sdio_driver = {
0876 .name = "sdio",
0877 .probe = gb_sdio_probe,
0878 .remove = gb_sdio_remove,
0879 .id_table = gb_sdio_id_table,
0880 };
0881
0882 module_gbphy_driver(sdio_driver);
0883 MODULE_LICENSE("GPL v2");