2 * Copyright (c) 2013 Linaro Ltd.
3 * Copyright (c) 2013 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/bitops.h>
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/mmc/host.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
24 #include "dw_mmc-pltfm.h"
27 * hi6220 sd only support io voltage 1.8v and 3v
28 * Also need config AO_SCTRL_SEL18 accordingly
30 #define AO_SCTRL_SEL18 BIT(10)
31 #define AO_SCTRL_CTRL3 0x40C
33 #define DWMMC_SDIO_ID 2
35 #define SOC_SCTRL_SCPERCTRL5 (0x314)
36 #define SDCARD_IO_SEL18 BIT(2)
38 #define SDCARD_RD_THRESHOLD (512)
40 #define GENCLK_DIV (7)
42 #define GPIO_CLK_ENABLE BIT(16)
43 #define GPIO_CLK_DIV_MASK GENMASK(11, 8)
44 #define GPIO_USE_SAMPLE_DLY_MASK GENMASK(13, 13)
45 #define UHS_REG_EXT_SAMPLE_PHASE_MASK GENMASK(20, 16)
46 #define UHS_REG_EXT_SAMPLE_DRVPHASE_MASK GENMASK(25, 21)
47 #define UHS_REG_EXT_SAMPLE_DLY_MASK GENMASK(30, 26)
50 #define TIMING_CFG_NUM 10
52 #define NUM_PHASES (40)
54 #define ENABLE_SHIFT_MIN_SMPL (4)
55 #define ENABLE_SHIFT_MAX_SMPL (12)
56 #define USE_DLY_MIN_SMPL (11)
57 #define USE_DLY_MAX_SMPL (14)
65 static unsigned long dw_mci_hi6220_caps
[] = {
78 static struct hs_timing hs_timing_cfg
[TIMING_MODE
][TIMING_CFG_NUM
] = {
81 {7, 0, 15, 15,}, /* 0: LEGACY 400k */
82 {6, 0, 4, 4,}, /* 1: MMC_HS */
83 {6, 0, 3, 3,}, /* 2: SD_HS */
84 {6, 0, 15, 15,}, /* 3: SDR12 */
85 {6, 0, 2, 2,}, /* 4: SDR25 */
86 {4, 0, 11, 0,}, /* 5: SDR50 */
87 {6, 4, 15, 0,}, /* 6: SDR104 */
93 {7, 0, 15, 15,}, /* 0: LEGACY 400k */
95 {6, 0, 15, 15,}, /* 2: SD_HS */
96 {6, 0, 15, 15,}, /* 3: SDR12 */
97 {6, 0, 0, 0,}, /* 4: SDR25 */
98 {4, 0, 12, 0,}, /* 5: SDR50 */
99 {5, 4, 15, 0,}, /* 6: SDR104 */
106 static void dw_mci_k3_set_ios(struct dw_mci
*host
, struct mmc_ios
*ios
)
110 ret
= clk_set_rate(host
->ciu_clk
, ios
->clock
);
112 dev_warn(host
->dev
, "failed to set rate %uHz\n", ios
->clock
);
114 host
->bus_hz
= clk_get_rate(host
->ciu_clk
);
117 static const struct dw_mci_drv_data k3_drv_data
= {
118 .set_ios
= dw_mci_k3_set_ios
,
121 static int dw_mci_hi6220_parse_dt(struct dw_mci
*host
)
123 struct k3_priv
*priv
;
125 priv
= devm_kzalloc(host
->dev
, sizeof(*priv
), GFP_KERNEL
);
129 priv
->reg
= syscon_regmap_lookup_by_phandle(host
->dev
->of_node
,
130 "hisilicon,peripheral-syscon");
131 if (IS_ERR(priv
->reg
))
134 priv
->ctrl_id
= of_alias_get_id(host
->dev
->of_node
, "mshc");
135 if (priv
->ctrl_id
< 0)
138 if (priv
->ctrl_id
>= TIMING_MODE
)
145 static int dw_mci_hi6220_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
147 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
148 struct k3_priv
*priv
;
156 if (!priv
|| !priv
->reg
)
159 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
) {
160 ret
= regmap_update_bits(priv
->reg
, AO_SCTRL_CTRL3
,
164 } else if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_180
) {
165 ret
= regmap_update_bits(priv
->reg
, AO_SCTRL_CTRL3
,
166 AO_SCTRL_SEL18
, AO_SCTRL_SEL18
);
170 dev_dbg(host
->dev
, "voltage not supported\n");
175 dev_dbg(host
->dev
, "switch voltage failed\n");
179 if (IS_ERR_OR_NULL(mmc
->supply
.vqmmc
))
182 ret
= regulator_set_voltage(mmc
->supply
.vqmmc
, min_uv
, max_uv
);
184 dev_dbg(host
->dev
, "Regulator set error %d: %d - %d\n",
185 ret
, min_uv
, max_uv
);
192 static void dw_mci_hi6220_set_ios(struct dw_mci
*host
, struct mmc_ios
*ios
)
197 clock
= (ios
->clock
<= 25000000) ? 25000000 : ios
->clock
;
199 ret
= clk_set_rate(host
->biu_clk
, clock
);
201 dev_warn(host
->dev
, "failed to set rate %uHz\n", clock
);
203 host
->bus_hz
= clk_get_rate(host
->biu_clk
);
206 static int dw_mci_hi6220_execute_tuning(struct dw_mci_slot
*slot
, u32 opcode
)
211 static const struct dw_mci_drv_data hi6220_data
= {
212 .caps
= dw_mci_hi6220_caps
,
213 .switch_voltage
= dw_mci_hi6220_switch_voltage
,
214 .set_ios
= dw_mci_hi6220_set_ios
,
215 .parse_dt
= dw_mci_hi6220_parse_dt
,
216 .execute_tuning
= dw_mci_hi6220_execute_tuning
,
219 static void dw_mci_hs_set_timing(struct dw_mci
*host
, int timing
,
224 u32 use_smpl_dly
= 0;
225 u32 enable_shift
= 0;
228 struct k3_priv
*priv
;
231 ctrl_id
= priv
->ctrl_id
;
233 drv_phase
= hs_timing_cfg
[ctrl_id
][timing
].drv_phase
;
234 smpl_dly
= hs_timing_cfg
[ctrl_id
][timing
].smpl_dly
;
235 if (smpl_phase
== -1)
236 smpl_phase
= (hs_timing_cfg
[ctrl_id
][timing
].smpl_phase_max
+
237 hs_timing_cfg
[ctrl_id
][timing
].smpl_phase_min
) / 2;
240 case MMC_TIMING_UHS_SDR104
:
241 if (smpl_phase
>= USE_DLY_MIN_SMPL
&&
242 smpl_phase
<= USE_DLY_MAX_SMPL
)
245 case MMC_TIMING_UHS_SDR50
:
246 if (smpl_phase
>= ENABLE_SHIFT_MIN_SMPL
&&
247 smpl_phase
<= ENABLE_SHIFT_MAX_SMPL
)
252 mci_writel(host
, GPIO
, 0x0);
255 reg_value
= FIELD_PREP(UHS_REG_EXT_SAMPLE_PHASE_MASK
, smpl_phase
) |
256 FIELD_PREP(UHS_REG_EXT_SAMPLE_DLY_MASK
, smpl_dly
) |
257 FIELD_PREP(UHS_REG_EXT_SAMPLE_DRVPHASE_MASK
, drv_phase
);
258 mci_writel(host
, UHS_REG_EXT
, reg_value
);
260 mci_writel(host
, ENABLE_SHIFT
, enable_shift
);
262 reg_value
= FIELD_PREP(GPIO_CLK_DIV_MASK
, GENCLK_DIV
) |
263 FIELD_PREP(GPIO_USE_SAMPLE_DLY_MASK
, use_smpl_dly
);
264 mci_writel(host
, GPIO
, (unsigned int)reg_value
| GPIO_CLK_ENABLE
);
266 /* We should delay 1ms wait for timing setting finished. */
267 usleep_range(1000, 2000);
270 static int dw_mci_hi3660_init(struct dw_mci
*host
)
272 mci_writel(host
, CDTHRCTL
, SDMMC_SET_THLD(SDCARD_RD_THRESHOLD
,
273 SDMMC_CARD_RD_THR_EN
));
275 dw_mci_hs_set_timing(host
, MMC_TIMING_LEGACY
, -1);
276 host
->bus_hz
/= (GENCLK_DIV
+ 1);
281 static int dw_mci_set_sel18(struct dw_mci
*host
, bool set
)
285 struct k3_priv
*priv
;
289 val
= set
? SDCARD_IO_SEL18
: 0;
290 ret
= regmap_update_bits(priv
->reg
, SOC_SCTRL_SCPERCTRL5
,
291 SDCARD_IO_SEL18
, val
);
293 dev_err(host
->dev
, "sel18 %u error\n", val
);
300 static void dw_mci_hi3660_set_ios(struct dw_mci
*host
, struct mmc_ios
*ios
)
303 unsigned long wanted
;
304 unsigned long actual
;
305 struct k3_priv
*priv
= host
->priv
;
307 if (!ios
->clock
|| ios
->clock
== priv
->cur_speed
)
310 wanted
= ios
->clock
* (GENCLK_DIV
+ 1);
311 ret
= clk_set_rate(host
->ciu_clk
, wanted
);
313 dev_err(host
->dev
, "failed to set rate %luHz\n", wanted
);
316 actual
= clk_get_rate(host
->ciu_clk
);
318 dw_mci_hs_set_timing(host
, ios
->timing
, -1);
319 host
->bus_hz
= actual
/ (GENCLK_DIV
+ 1);
320 host
->current_speed
= 0;
321 priv
->cur_speed
= host
->bus_hz
;
324 static int dw_mci_get_best_clksmpl(unsigned int sample_flag
)
330 unsigned int range_start
= 0;
331 unsigned int range_length
= 0;
332 unsigned int middle_range
= 0;
337 if (~sample_flag
== 0)
340 i
= ffs(sample_flag
) - 1;
343 * A clock cycle is divided into 32 phases,
344 * each of which is represented by a bit,
345 * finding the optimal phase.
348 v
= ror32(sample_flag
, i
);
351 if (len
> range_length
) {
356 interval
= ffs(v
>> len
) - 1;
363 middle_range
= range_start
+ range_length
/ 2;
364 if (middle_range
>= 32)
370 static int dw_mci_hi3660_execute_tuning(struct dw_mci_slot
*slot
, u32 opcode
)
373 struct dw_mci
*host
= slot
->host
;
374 struct mmc_host
*mmc
= slot
->mmc
;
376 u32 tuning_sample_flag
= 0;
377 int best_clksmpl
= 0;
379 for (i
= 0; i
< NUM_PHASES
; ++i
, ++smpl_phase
) {
382 mci_writel(host
, TMOUT
, ~0);
383 dw_mci_hs_set_timing(host
, mmc
->ios
.timing
, smpl_phase
);
385 if (!mmc_send_tuning(mmc
, opcode
, NULL
))
386 tuning_sample_flag
|= (1 << smpl_phase
);
388 tuning_sample_flag
&= ~(1 << smpl_phase
);
391 best_clksmpl
= dw_mci_get_best_clksmpl(tuning_sample_flag
);
392 if (best_clksmpl
< 0) {
393 dev_err(host
->dev
, "All phases bad!\n");
397 dw_mci_hs_set_timing(host
, mmc
->ios
.timing
, best_clksmpl
);
399 dev_info(host
->dev
, "tuning ok best_clksmpl %u tuning_sample_flag %x\n",
400 best_clksmpl
, tuning_sample_flag
);
404 static int dw_mci_hi3660_switch_voltage(struct mmc_host
*mmc
,
408 struct dw_mci_slot
*slot
= mmc_priv(mmc
);
409 struct k3_priv
*priv
;
415 if (!priv
|| !priv
->reg
)
418 if (priv
->ctrl_id
== DWMMC_SDIO_ID
)
421 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
422 ret
= dw_mci_set_sel18(host
, 0);
423 else if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_180
)
424 ret
= dw_mci_set_sel18(host
, 1);
428 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
429 ret
= mmc_regulator_set_vqmmc(mmc
, ios
);
431 dev_err(host
->dev
, "Regulator set error %d\n", ret
);
439 static const struct dw_mci_drv_data hi3660_data
= {
440 .init
= dw_mci_hi3660_init
,
441 .set_ios
= dw_mci_hi3660_set_ios
,
442 .parse_dt
= dw_mci_hi6220_parse_dt
,
443 .execute_tuning
= dw_mci_hi3660_execute_tuning
,
444 .switch_voltage
= dw_mci_hi3660_switch_voltage
,
447 static const struct of_device_id dw_mci_k3_match
[] = {
448 { .compatible
= "hisilicon,hi3660-dw-mshc", .data
= &hi3660_data
, },
449 { .compatible
= "hisilicon,hi4511-dw-mshc", .data
= &k3_drv_data
, },
450 { .compatible
= "hisilicon,hi6220-dw-mshc", .data
= &hi6220_data
, },
453 MODULE_DEVICE_TABLE(of
, dw_mci_k3_match
);
455 static int dw_mci_k3_probe(struct platform_device
*pdev
)
457 const struct dw_mci_drv_data
*drv_data
;
458 const struct of_device_id
*match
;
460 match
= of_match_node(dw_mci_k3_match
, pdev
->dev
.of_node
);
461 drv_data
= match
->data
;
463 return dw_mci_pltfm_register(pdev
, drv_data
);
466 static const struct dev_pm_ops dw_mci_k3_dev_pm_ops
= {
467 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
468 pm_runtime_force_resume
)
469 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend
,
470 dw_mci_runtime_resume
,
474 static struct platform_driver dw_mci_k3_pltfm_driver
= {
475 .probe
= dw_mci_k3_probe
,
476 .remove
= dw_mci_pltfm_remove
,
479 .of_match_table
= dw_mci_k3_match
,
480 .pm
= &dw_mci_k3_dev_pm_ops
,
484 module_platform_driver(dw_mci_k3_pltfm_driver
);
486 MODULE_DESCRIPTION("K3 Specific DW-MSHC Driver Extension");
487 MODULE_LICENSE("GPL v2");
488 MODULE_ALIAS("platform:dwmmc_k3");