0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/platform_device.h>
0008 #include <linux/clk.h>
0009 #include <linux/mmc/host.h>
0010 #include <linux/of_address.h>
0011 #include <linux/mmc/slot-gpio.h>
0012 #include <linux/pm_runtime.h>
0013 #include <linux/slab.h>
0014
0015 #include "dw_mmc.h"
0016 #include "dw_mmc-pltfm.h"
0017
0018 #define RK3288_CLKGEN_DIV 2
0019
0020 static const unsigned int freqs[] = { 100000, 200000, 300000, 400000 };
0021
0022 struct dw_mci_rockchip_priv_data {
0023 struct clk *drv_clk;
0024 struct clk *sample_clk;
0025 int default_sample_phase;
0026 int num_phases;
0027 };
0028
0029 static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
0030 {
0031 struct dw_mci_rockchip_priv_data *priv = host->priv;
0032 int ret;
0033 unsigned int cclkin;
0034 u32 bus_hz;
0035
0036 if (ios->clock == 0)
0037 return;
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 if (ios->bus_width == MMC_BUS_WIDTH_8 &&
0049 ios->timing == MMC_TIMING_MMC_DDR52)
0050 cclkin = 2 * ios->clock * RK3288_CLKGEN_DIV;
0051 else
0052 cclkin = ios->clock * RK3288_CLKGEN_DIV;
0053
0054 ret = clk_set_rate(host->ciu_clk, cclkin);
0055 if (ret)
0056 dev_warn(host->dev, "failed to set rate %uHz err: %d\n", cclkin, ret);
0057
0058 bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
0059 if (bus_hz != host->bus_hz) {
0060 host->bus_hz = bus_hz;
0061
0062 host->current_speed = 0;
0063 }
0064
0065
0066 if (!IS_ERR(priv->sample_clk) && ios->timing <= MMC_TIMING_SD_HS)
0067 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 if (!IS_ERR(priv->drv_clk)) {
0096 int phase;
0097
0098
0099
0100
0101
0102
0103
0104 phase = 90;
0105
0106 switch (ios->timing) {
0107 case MMC_TIMING_MMC_DDR52:
0108
0109
0110
0111
0112
0113 if (ios->bus_width == MMC_BUS_WIDTH_8)
0114 phase = 180;
0115 break;
0116 case MMC_TIMING_UHS_SDR104:
0117 case MMC_TIMING_MMC_HS200:
0118
0119
0120
0121
0122
0123
0124
0125
0126 phase = 180;
0127 break;
0128 }
0129
0130 clk_set_phase(priv->drv_clk, phase);
0131 }
0132 }
0133
0134 #define TUNING_ITERATION_TO_PHASE(i, num_phases) \
0135 (DIV_ROUND_UP((i) * 360, num_phases))
0136
0137 static int dw_mci_rk3288_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
0138 {
0139 struct dw_mci *host = slot->host;
0140 struct dw_mci_rockchip_priv_data *priv = host->priv;
0141 struct mmc_host *mmc = slot->mmc;
0142 int ret = 0;
0143 int i;
0144 bool v, prev_v = 0, first_v;
0145 struct range_t {
0146 int start;
0147 int end;
0148 };
0149 struct range_t *ranges;
0150 unsigned int range_count = 0;
0151 int longest_range_len = -1;
0152 int longest_range = -1;
0153 int middle_phase;
0154
0155 if (IS_ERR(priv->sample_clk)) {
0156 dev_err(host->dev, "Tuning clock (sample_clk) not defined.\n");
0157 return -EIO;
0158 }
0159
0160 ranges = kmalloc_array(priv->num_phases / 2 + 1,
0161 sizeof(*ranges), GFP_KERNEL);
0162 if (!ranges)
0163 return -ENOMEM;
0164
0165
0166 for (i = 0; i < priv->num_phases; ) {
0167 clk_set_phase(priv->sample_clk,
0168 TUNING_ITERATION_TO_PHASE(i, priv->num_phases));
0169
0170 v = !mmc_send_tuning(mmc, opcode, NULL);
0171
0172 if (i == 0)
0173 first_v = v;
0174
0175 if ((!prev_v) && v) {
0176 range_count++;
0177 ranges[range_count-1].start = i;
0178 }
0179 if (v) {
0180 ranges[range_count-1].end = i;
0181 i++;
0182 } else if (i == priv->num_phases - 1) {
0183
0184 i++;
0185 } else {
0186
0187
0188
0189
0190
0191 i += DIV_ROUND_UP(20 * priv->num_phases, 360);
0192
0193
0194 if (i >= priv->num_phases)
0195 i = priv->num_phases - 1;
0196 }
0197
0198 prev_v = v;
0199 }
0200
0201 if (range_count == 0) {
0202 dev_warn(host->dev, "All phases bad!");
0203 ret = -EIO;
0204 goto free;
0205 }
0206
0207
0208 if ((range_count > 1) && first_v && v) {
0209 ranges[0].start = ranges[range_count-1].start;
0210 range_count--;
0211 }
0212
0213 if (ranges[0].start == 0 && ranges[0].end == priv->num_phases - 1) {
0214 clk_set_phase(priv->sample_clk, priv->default_sample_phase);
0215 dev_info(host->dev, "All phases work, using default phase %d.",
0216 priv->default_sample_phase);
0217 goto free;
0218 }
0219
0220
0221 for (i = 0; i < range_count; i++) {
0222 int len = (ranges[i].end - ranges[i].start + 1);
0223
0224 if (len < 0)
0225 len += priv->num_phases;
0226
0227 if (longest_range_len < len) {
0228 longest_range_len = len;
0229 longest_range = i;
0230 }
0231
0232 dev_dbg(host->dev, "Good phase range %d-%d (%d len)\n",
0233 TUNING_ITERATION_TO_PHASE(ranges[i].start,
0234 priv->num_phases),
0235 TUNING_ITERATION_TO_PHASE(ranges[i].end,
0236 priv->num_phases),
0237 len
0238 );
0239 }
0240
0241 dev_dbg(host->dev, "Best phase range %d-%d (%d len)\n",
0242 TUNING_ITERATION_TO_PHASE(ranges[longest_range].start,
0243 priv->num_phases),
0244 TUNING_ITERATION_TO_PHASE(ranges[longest_range].end,
0245 priv->num_phases),
0246 longest_range_len
0247 );
0248
0249 middle_phase = ranges[longest_range].start + longest_range_len / 2;
0250 middle_phase %= priv->num_phases;
0251 dev_info(host->dev, "Successfully tuned phase to %d\n",
0252 TUNING_ITERATION_TO_PHASE(middle_phase, priv->num_phases));
0253
0254 clk_set_phase(priv->sample_clk,
0255 TUNING_ITERATION_TO_PHASE(middle_phase,
0256 priv->num_phases));
0257
0258 free:
0259 kfree(ranges);
0260 return ret;
0261 }
0262
0263 static int dw_mci_rk3288_parse_dt(struct dw_mci *host)
0264 {
0265 struct device_node *np = host->dev->of_node;
0266 struct dw_mci_rockchip_priv_data *priv;
0267
0268 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
0269 if (!priv)
0270 return -ENOMEM;
0271
0272 if (of_property_read_u32(np, "rockchip,desired-num-phases",
0273 &priv->num_phases))
0274 priv->num_phases = 360;
0275
0276 if (of_property_read_u32(np, "rockchip,default-sample-phase",
0277 &priv->default_sample_phase))
0278 priv->default_sample_phase = 0;
0279
0280 priv->drv_clk = devm_clk_get(host->dev, "ciu-drive");
0281 if (IS_ERR(priv->drv_clk))
0282 dev_dbg(host->dev, "ciu-drive not available\n");
0283
0284 priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
0285 if (IS_ERR(priv->sample_clk))
0286 dev_dbg(host->dev, "ciu-sample not available\n");
0287
0288 host->priv = priv;
0289
0290 return 0;
0291 }
0292
0293 static int dw_mci_rockchip_init(struct dw_mci *host)
0294 {
0295 int ret, i;
0296
0297
0298 host->sdio_id0 = 8;
0299
0300 if (of_device_is_compatible(host->dev->of_node, "rockchip,rk3288-dw-mshc")) {
0301 host->bus_hz /= RK3288_CLKGEN_DIV;
0302
0303
0304
0305
0306
0307
0308 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
0309 ret = clk_round_rate(host->ciu_clk, freqs[i] * RK3288_CLKGEN_DIV);
0310 if (ret > 0) {
0311 host->minimum_speed = ret / RK3288_CLKGEN_DIV;
0312 break;
0313 }
0314 }
0315 if (ret < 0)
0316 dev_warn(host->dev, "no valid minimum freq: %d\n", ret);
0317 }
0318
0319 return 0;
0320 }
0321
0322 static const struct dw_mci_drv_data rk2928_drv_data = {
0323 .init = dw_mci_rockchip_init,
0324 };
0325
0326 static const struct dw_mci_drv_data rk3288_drv_data = {
0327 .common_caps = MMC_CAP_CMD23,
0328 .set_ios = dw_mci_rk3288_set_ios,
0329 .execute_tuning = dw_mci_rk3288_execute_tuning,
0330 .parse_dt = dw_mci_rk3288_parse_dt,
0331 .init = dw_mci_rockchip_init,
0332 };
0333
0334 static const struct of_device_id dw_mci_rockchip_match[] = {
0335 { .compatible = "rockchip,rk2928-dw-mshc",
0336 .data = &rk2928_drv_data },
0337 { .compatible = "rockchip,rk3288-dw-mshc",
0338 .data = &rk3288_drv_data },
0339 {},
0340 };
0341 MODULE_DEVICE_TABLE(of, dw_mci_rockchip_match);
0342
0343 static int dw_mci_rockchip_probe(struct platform_device *pdev)
0344 {
0345 const struct dw_mci_drv_data *drv_data;
0346 const struct of_device_id *match;
0347 int ret;
0348
0349 if (!pdev->dev.of_node)
0350 return -ENODEV;
0351
0352 match = of_match_node(dw_mci_rockchip_match, pdev->dev.of_node);
0353 drv_data = match->data;
0354
0355 pm_runtime_get_noresume(&pdev->dev);
0356 pm_runtime_set_active(&pdev->dev);
0357 pm_runtime_enable(&pdev->dev);
0358 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
0359 pm_runtime_use_autosuspend(&pdev->dev);
0360
0361 ret = dw_mci_pltfm_register(pdev, drv_data);
0362 if (ret) {
0363 pm_runtime_disable(&pdev->dev);
0364 pm_runtime_set_suspended(&pdev->dev);
0365 pm_runtime_put_noidle(&pdev->dev);
0366 return ret;
0367 }
0368
0369 pm_runtime_put_autosuspend(&pdev->dev);
0370
0371 return 0;
0372 }
0373
0374 static int dw_mci_rockchip_remove(struct platform_device *pdev)
0375 {
0376 pm_runtime_get_sync(&pdev->dev);
0377 pm_runtime_disable(&pdev->dev);
0378 pm_runtime_put_noidle(&pdev->dev);
0379
0380 dw_mci_pltfm_remove(pdev);
0381
0382 return 0;
0383 }
0384
0385 static const struct dev_pm_ops dw_mci_rockchip_dev_pm_ops = {
0386 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
0387 pm_runtime_force_resume)
0388 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
0389 dw_mci_runtime_resume,
0390 NULL)
0391 };
0392
0393 static struct platform_driver dw_mci_rockchip_pltfm_driver = {
0394 .probe = dw_mci_rockchip_probe,
0395 .remove = dw_mci_rockchip_remove,
0396 .driver = {
0397 .name = "dwmmc_rockchip",
0398 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0399 .of_match_table = dw_mci_rockchip_match,
0400 .pm = &dw_mci_rockchip_dev_pm_ops,
0401 },
0402 };
0403
0404 module_platform_driver(dw_mci_rockchip_pltfm_driver);
0405
0406 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
0407 MODULE_DESCRIPTION("Rockchip Specific DW-MSHC Driver Extension");
0408 MODULE_ALIAS("platform:dwmmc_rockchip");
0409 MODULE_LICENSE("GPL v2");