Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / drivers / mmc / host / dw_mmc-socfpga.c
1 /*
2  * Altera SoCFPGA Specific Extensions for Synopsys DW Multimedia Card Interface
3  * driver
4  *
5  *  Copyright (C) 2012, Samsung Electronics Co., Ltd.
6  *  Copyright (C) 2013 Altera Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Taken from dw_mmc-exynos.c
14  */
15 #include <linux/clk.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/dw_mmc.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23
24 #include "dw_mmc.h"
25 #include "dw_mmc-pltfm.h"
26
27 #define SYSMGR_SDMMCGRP_CTRL_OFFSET             0x108
28 #define DRV_CLK_PHASE_SHIFT_SEL_MASK    0x7
29 #define SYSMGR_SDMMC_CTRL_SET(smplsel, drvsel)          \
30         ((((smplsel) & 0x7) << 3) | (((drvsel) & 0x7) << 0))
31
32 /* SOCFPGA implementation specific driver private data */
33 struct dw_mci_socfpga_priv_data {
34         u8      ciu_div; /* card interface unit divisor */
35         u32     hs_timing; /* bitmask for CIU clock phase shift */
36         struct regmap   *sysreg; /* regmap for system manager register */
37 };
38
39 static int dw_mci_socfpga_priv_init(struct dw_mci *host)
40 {
41         struct dw_mci_socfpga_priv_data *priv;
42
43         priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
44         if (!priv) {
45                 dev_err(host->dev, "mem alloc failed for private data\n");
46                 return -ENOMEM;
47         }
48
49         priv->sysreg = syscon_regmap_lookup_by_compatible("altr,sys-mgr");
50         if (IS_ERR(priv->sysreg)) {
51                 dev_err(host->dev, "regmap for altr,sys-mgr lookup failed.\n");
52                 return PTR_ERR(priv->sysreg);
53         }
54         host->priv = priv;
55
56         return 0;
57 }
58
59 static int dw_mci_socfpga_setup_clock(struct dw_mci *host)
60 {
61         struct dw_mci_socfpga_priv_data *priv = host->priv;
62
63         clk_disable_unprepare(host->ciu_clk);
64         regmap_write(priv->sysreg, SYSMGR_SDMMCGRP_CTRL_OFFSET,
65                 priv->hs_timing);
66         clk_prepare_enable(host->ciu_clk);
67
68         host->bus_hz /= (priv->ciu_div + 1);
69         return 0;
70 }
71
72 static void dw_mci_socfpga_prepare_command(struct dw_mci *host, u32 *cmdr)
73 {
74         struct dw_mci_socfpga_priv_data *priv = host->priv;
75
76         if (priv->hs_timing & DRV_CLK_PHASE_SHIFT_SEL_MASK)
77                 *cmdr |= SDMMC_CMD_USE_HOLD_REG;
78 }
79
80 static int dw_mci_socfpga_parse_dt(struct dw_mci *host)
81 {
82         struct dw_mci_socfpga_priv_data *priv = host->priv;
83         struct device_node *np = host->dev->of_node;
84         u32 timing[2];
85         u32 div = 0;
86         int ret;
87
88         ret = of_property_read_u32(np, "altr,dw-mshc-ciu-div", &div);
89         if (ret)
90                 dev_info(host->dev, "No dw-mshc-ciu-div specified, assuming 1");
91         priv->ciu_div = div;
92
93         ret = of_property_read_u32_array(np,
94                         "altr,dw-mshc-sdr-timing", timing, 2);
95         if (ret)
96                 return ret;
97
98         priv->hs_timing = SYSMGR_SDMMC_CTRL_SET(timing[0], timing[1]);
99         return 0;
100 }
101
102 static const struct dw_mci_drv_data socfpga_drv_data = {
103         .init                   = dw_mci_socfpga_priv_init,
104         .setup_clock            = dw_mci_socfpga_setup_clock,
105         .prepare_command        = dw_mci_socfpga_prepare_command,
106         .parse_dt               = dw_mci_socfpga_parse_dt,
107 };
108
109 static const struct of_device_id dw_mci_socfpga_match[] = {
110         { .compatible = "altr,socfpga-dw-mshc",
111                         .data = &socfpga_drv_data, },
112         {},
113 };
114 MODULE_DEVICE_TABLE(of, dw_mci_socfpga_match);
115
116 int dw_mci_socfpga_probe(struct platform_device *pdev)
117 {
118         const struct dw_mci_drv_data *drv_data;
119         const struct of_device_id *match;
120
121         match = of_match_node(dw_mci_socfpga_match, pdev->dev.of_node);
122         drv_data = match->data;
123         return dw_mci_pltfm_register(pdev, drv_data);
124 }
125
126 static struct platform_driver dw_mci_socfpga_pltfm_driver = {
127         .probe          = dw_mci_socfpga_probe,
128         .remove         = __exit_p(dw_mci_pltfm_remove),
129         .driver         = {
130                 .name           = "dwmmc_socfpga",
131                 .of_match_table = of_match_ptr(dw_mci_socfpga_match),
132                 .pm             = &dw_mci_pltfm_pmops,
133         },
134 };
135
136 module_platform_driver(dw_mci_socfpga_pltfm_driver);
137
138 MODULE_DESCRIPTION("Altera SOCFPGA Specific DW-MSHC Driver Extension");
139 MODULE_LICENSE("GPL v2");
140 MODULE_ALIAS("platform:dwmmc-socfpga");