]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/staging/greybus/sdio.c
greybus: gbphy: Remove protocol specific version handling
[karo-tx-linux.git] / drivers / staging / greybus / sdio.c
index bfa1181074d670cd3645706a98f91f90061e9cc8..a270517c90c87d9fd54c0f2c2f10aefde2b8a41b 100644 (file)
 #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 */
@@ -55,30 +57,30 @@ 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) |
+       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) |
+               ((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);
+               ((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;
@@ -87,12 +89,35 @@ static void _gb_sdio_set_host_caps(struct gb_sdio_host *host, u32 r)
                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;
 
@@ -117,11 +142,16 @@ static int gb_sdio_get_caps(struct gb_sdio_host *host)
        host->data_max = data_max;
 
        /* get ocr supported values */
-       mmc->ocr_avail = le32_to_cpu(response.ocr);
+       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;
 }
 
@@ -140,7 +170,7 @@ static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
        u8 state_changed = 0;
 
        if (event & GB_SDIO_CARD_INSERTED) {
-               if (!mmc_card_is_removable(host->mmc))
+               if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
                        return 0;
                if (host->card_present)
                        return 0;
@@ -149,7 +179,7 @@ static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
        }
 
        if (event & GB_SDIO_CARD_REMOVED) {
-               if (!mmc_card_is_removable(host->mmc))
+               if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
                        return 0;
                if (!(host->card_present))
                        return 0;
@@ -170,17 +200,17 @@ static int _gb_sdio_process_events(struct gb_sdio_host *host, u8 event)
        return 0;
 }
 
-static int gb_sdio_event_recv(u8 type, struct gb_operation *op)
+static int gb_sdio_request_handler(struct gb_operation *op)
 {
-       struct gb_connection *connection = op->connection;
-       struct gb_sdio_host *host = connection->private;
+       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(&connection->dev,
+               dev_err(mmc_dev(host->mmc),
                        "unsupported unsolicited event: %u\n", type);
                return -EINVAL;
        }
@@ -345,12 +375,13 @@ out:
 
 static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
 {
-       struct gb_sdio_command_request request;
+       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 = 0;
+       int ret;
 
        switch (mmc_resp_type(cmd)) {
        case MMC_RSP_NONE:
@@ -369,7 +400,7 @@ static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
                cmd_flags = GB_SDIO_RSP_R3_R4;
                break;
        default:
-               dev_err(mmc_dev(host->mmc), "cmd flag invalid %04x\n",
+               dev_err(mmc_dev(host->mmc), "cmd flag invalid 0x%04x\n",
                        mmc_resp_type(cmd));
                ret = -EINVAL;
                goto out;
@@ -389,7 +420,7 @@ static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
                cmd_type = GB_SDIO_CMD_ADTC;
                break;
        default:
-               dev_err(mmc_dev(host->mmc), "cmd type invalid %04x\n",
+               dev_err(mmc_dev(host->mmc), "cmd type invalid 0x%04x\n",
                        mmc_cmd_type(cmd));
                ret = -EINVAL;
                goto out;
@@ -399,6 +430,11 @@ static int gb_sdio_command(struct gb_sdio_host *host, struct mmc_command *cmd)
        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,
@@ -454,7 +490,7 @@ static void gb_sdio_mrq_work(struct work_struct *work)
                goto done;
 
        if (mrq->data) {
-               ret = gb_sdio_transfer(host, host->mrq->data);
+               ret = gb_sdio_transfer(host, mrq->data);
                if (ret < 0)
                        goto done;
        }
@@ -648,9 +684,12 @@ 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)
+       if (host->removed) {
+               mutex_unlock(&host->lock);
                return -ESHUTDOWN;
+       }
        mutex_unlock(&host->lock);
+
        return host->read_only;
 }
 
@@ -659,9 +698,12 @@ 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)
+       if (host->removed) {
+               mutex_unlock(&host->lock);
                return -ESHUTDOWN;
+       }
        mutex_unlock(&host->lock);
+
        return host->card_present;
 }
 
@@ -672,27 +714,43 @@ static const struct mmc_host_ops gb_sdio_ops = {
        .get_cd         = gb_mmc_get_cd,
 };
 
-static int gb_sdio_connection_init(struct gb_connection *connection)
+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;
        size_t max_buffer;
        int ret = 0;
 
-       mmc = mmc_alloc_host(sizeof(*host), &connection->dev);
+       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;
-       connection->private = host;
+       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 free_mmc;
+               goto exit_connection_disable;
 
        mmc->ops = &gb_sdio_ops;
 
@@ -706,67 +764,79 @@ static int gb_sdio_connection_init(struct gb_connection *connection)
        host->xfer_buffer = kzalloc(max_buffer, GFP_KERNEL);
        if (!host->xfer_buffer) {
                ret = -ENOMEM;
-               goto free_mmc;
+               goto exit_connection_disable;
        }
        mutex_init(&host->lock);
        spin_lock_init(&host->xfer);
        host->mrq_workqueue = alloc_workqueue("mmc-%s", 0, 1,
-                                               dev_name(&connection->dev));
+                                             dev_name(&gbphy_dev->dev));
        if (!host->mrq_workqueue) {
                ret = -ENOMEM;
-               goto free_buffer;
+               goto exit_buf_free;
        }
        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 free_work;
+               goto exit_wq_destroy;
        host->removed = false;
        ret = _gb_sdio_process_events(host, host->queued_events);
        host->queued_events = 0;
 
        return ret;
 
-free_work:
+exit_wq_destroy:
        destroy_workqueue(host->mrq_workqueue);
-free_buffer:
+exit_buf_free:
        kfree(host->xfer_buffer);
-free_mmc:
-       connection->private = NULL;
+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_connection_exit(struct gb_connection *connection)
+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;
-       struct gb_sdio_host *host = connection->private;
-
-       if (!host)
-               return;
 
        mutex_lock(&host->lock);
        host->removed = true;
        mmc = host->mmc;
-       connection->private = NULL;
+       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);
        kfree(host->xfer_buffer);
        mmc_free_host(mmc);
 }
 
-static struct gb_protocol sdio_protocol = {
-       .name                   = "sdio",
-       .id                     = GREYBUS_PROTOCOL_SDIO,
-       .major                  = GB_SDIO_VERSION_MAJOR,
-       .minor                  = GB_SDIO_VERSION_MINOR,
-       .connection_init        = gb_sdio_connection_init,
-       .connection_exit        = gb_sdio_connection_exit,
-       .request_recv           = gb_sdio_event_recv,
+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,
 };
 
-gb_builtin_protocol_driver(sdio_protocol);
+module_gbphy_driver(sdio_driver);
+MODULE_LICENSE("GPL v2");