blob: 36e22bd2b8cc6e6caa2c1b8c1cb8b8a65137de7b [file] [log] [blame]
Hu Ziji3a3748d2017-03-30 17:22:59 +02001/*
2 * Driver for Marvell Xenon SDHC as a platform device
3 *
4 * Copyright (C) 2016 Marvell, All Rights Reserved.
5 *
6 * Author: Hu Ziji <huziji@marvell.com>
7 * Date: 2016-8-24
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation version 2.
12 *
13 * Inspired by Jisheng Zhang <jszhang@marvell.com>
14 * Special thanks to Video BG4 project team.
15 */
16
17#include <linux/delay.h>
18#include <linux/ktime.h>
19#include <linux/module.h>
20#include <linux/of.h>
21
22#include "sdhci-pltfm.h"
23#include "sdhci-xenon.h"
24
25static int xenon_enable_internal_clk(struct sdhci_host *host)
26{
27 u32 reg;
28 ktime_t timeout;
29
30 reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
31 reg |= SDHCI_CLOCK_INT_EN;
32 sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
33 /* Wait max 20 ms */
34 timeout = ktime_add_ms(ktime_get(), 20);
35 while (!((reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
36 & SDHCI_CLOCK_INT_STABLE)) {
37 if (ktime_after(ktime_get(), timeout)) {
38 dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
39 return -ETIMEDOUT;
40 }
41 usleep_range(900, 1100);
42 }
43
44 return 0;
45}
46
47/* Set SDCLK-off-while-idle */
48static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
49 unsigned char sdhc_id, bool enable)
50{
51 u32 reg;
52 u32 mask;
53
54 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
55 /* Get the bit shift basing on the SDHC index */
56 mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
57 if (enable)
58 reg |= mask;
59 else
60 reg &= ~mask;
61
62 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
63}
64
65/* Enable/Disable the Auto Clock Gating function */
66static void xenon_set_acg(struct sdhci_host *host, bool enable)
67{
68 u32 reg;
69
70 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
71 if (enable)
72 reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
73 else
74 reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
75 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
76}
77
78/* Enable this SDHC */
79static void xenon_enable_sdhc(struct sdhci_host *host,
80 unsigned char sdhc_id)
81{
82 u32 reg;
83
84 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
85 reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
86 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
87
88 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
89 /*
90 * Force to clear BUS_TEST to
91 * skip bus_test_pre and bus_test_post
92 */
93 host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
94}
95
96/* Disable this SDHC */
97static void xenon_disable_sdhc(struct sdhci_host *host,
98 unsigned char sdhc_id)
99{
100 u32 reg;
101
102 reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
103 reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
104 sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
105}
106
107/* Enable Parallel Transfer Mode */
108static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
109 unsigned char sdhc_id)
110{
111 u32 reg;
112
113 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
114 reg |= BIT(sdhc_id);
115 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
116}
117
118/* Mask command conflict error */
119static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
120{
121 u32 reg;
122
123 reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
124 reg |= XENON_MASK_CMD_CONFLICT_ERR;
125 sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
126}
127
128static void xenon_retune_setup(struct sdhci_host *host)
129{
130 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
131 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
132 u32 reg;
133
134 /* Disable the Re-Tuning Request functionality */
135 reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
136 reg &= ~XENON_RETUNING_COMPATIBLE;
137 sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
138
139 /* Disable the Re-tuning Interrupt */
140 reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
141 reg &= ~SDHCI_INT_RETUNE;
142 sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
143 reg = sdhci_readl(host, SDHCI_INT_ENABLE);
144 reg &= ~SDHCI_INT_RETUNE;
145 sdhci_writel(host, reg, SDHCI_INT_ENABLE);
146
147 /* Force to use Tuning Mode 1 */
148 host->tuning_mode = SDHCI_TUNING_MODE_1;
149 /* Set re-tuning period */
150 host->tuning_count = 1 << (priv->tuning_count - 1);
151}
152
153/*
154 * Operations inside struct sdhci_ops
155 */
156/* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
157static void xenon_reset_exit(struct sdhci_host *host,
158 unsigned char sdhc_id, u8 mask)
159{
160 /* Only SOFTWARE RESET ALL will clear the register setting */
161 if (!(mask & SDHCI_RESET_ALL))
162 return;
163
164 /* Disable tuning request and auto-retuning again */
165 xenon_retune_setup(host);
166
167 xenon_set_acg(host, true);
168
169 xenon_set_sdclk_off_idle(host, sdhc_id, false);
170
171 xenon_mask_cmd_conflict_err(host);
172}
173
174static void xenon_reset(struct sdhci_host *host, u8 mask)
175{
176 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
177 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
178
179 sdhci_reset(host, mask);
180 xenon_reset_exit(host, priv->sdhc_id, mask);
181}
182
183/*
184 * Xenon defines different values for HS200 and HS400
185 * in Host_Control_2
186 */
187static void xenon_set_uhs_signaling(struct sdhci_host *host,
188 unsigned int timing)
189{
190 u16 ctrl_2;
191
192 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
193 /* Select Bus Speed Mode for host */
194 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
195 if (timing == MMC_TIMING_MMC_HS200)
196 ctrl_2 |= XENON_CTRL_HS200;
197 else if (timing == MMC_TIMING_UHS_SDR104)
198 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
199 else if (timing == MMC_TIMING_UHS_SDR12)
200 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
201 else if (timing == MMC_TIMING_UHS_SDR25)
202 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
203 else if (timing == MMC_TIMING_UHS_SDR50)
204 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
205 else if ((timing == MMC_TIMING_UHS_DDR50) ||
206 (timing == MMC_TIMING_MMC_DDR52))
207 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
208 else if (timing == MMC_TIMING_MMC_HS400)
209 ctrl_2 |= XENON_CTRL_HS400;
210 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
211}
212
213static const struct sdhci_ops sdhci_xenon_ops = {
214 .set_clock = sdhci_set_clock,
215 .set_bus_width = sdhci_set_bus_width,
216 .reset = xenon_reset,
217 .set_uhs_signaling = xenon_set_uhs_signaling,
218 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
219};
220
221static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
222 .ops = &sdhci_xenon_ops,
223 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
224 SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
225 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
226};
227
228/*
229 * Xenon Specific Operations in mmc_host_ops
230 */
231static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
232{
233 struct sdhci_host *host = mmc_priv(mmc);
234 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
235 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
236 u32 reg;
237
238 /*
239 * HS400/HS200/eMMC HS doesn't have Preset Value register.
240 * However, sdhci_set_ios will read HS400/HS200 Preset register.
241 * Disable Preset Value register for HS400/HS200.
242 * eMMC HS with preset_enabled set will trigger a bug in
243 * get_preset_value().
244 */
245 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
246 (ios->timing == MMC_TIMING_MMC_HS200) ||
247 (ios->timing == MMC_TIMING_MMC_HS)) {
248 host->preset_enabled = false;
249 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
250 host->flags &= ~SDHCI_PV_ENABLED;
251
252 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
253 reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
254 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
255 } else {
256 host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
257 }
258
259 sdhci_set_ios(mmc, ios);
Hu Ziji06c8b662017-03-30 17:23:00 +0200260 xenon_phy_adj(host, ios);
Hu Ziji3a3748d2017-03-30 17:22:59 +0200261
262 if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
263 xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
264}
265
266static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
267 struct mmc_ios *ios)
268{
269 struct sdhci_host *host = mmc_priv(mmc);
270
271 /*
272 * Before SD/SDIO set signal voltage, SD bus clock should be
273 * disabled. However, sdhci_set_clock will also disable the Internal
274 * clock in mmc_set_signal_voltage().
275 * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
276 * Thus here manually enable internal clock.
277 *
278 * After switch completes, it is unnecessary to disable internal clock,
279 * since keeping internal clock active obeys SD spec.
280 */
281 xenon_enable_internal_clk(host);
282
283 /*
284 * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
285 * Thus SDHCI_CTRL_VDD_180 bit might not work then.
286 * Skip the standard voltage switch to avoid any issue.
287 */
288 if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
289 return 0;
290
291 return sdhci_start_signal_voltage_switch(mmc, ios);
292}
293
294/*
295 * Update card type.
296 * priv->init_card_type will be used in PHY timing adjustment.
297 */
298static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
299{
300 struct sdhci_host *host = mmc_priv(mmc);
301 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
302 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
303
304 /* Update card type*/
305 priv->init_card_type = card->type;
306}
307
308static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
309{
310 struct sdhci_host *host = mmc_priv(mmc);
311
312 if (host->timing == MMC_TIMING_UHS_DDR50)
313 return 0;
314
315 /*
316 * Currently force Xenon driver back to support mode 1 only,
317 * even though Xenon might claim to support mode 2 or mode 3.
318 * It requires more time to test mode 2/mode 3 on more platforms.
319 */
320 if (host->tuning_mode != SDHCI_TUNING_MODE_1)
321 xenon_retune_setup(host);
322
323 return sdhci_execute_tuning(mmc, opcode);
324}
325
326static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
327{
328 struct sdhci_host *host = mmc_priv(mmc);
329 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
330 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
331 u32 reg;
332 u8 sdhc_id = priv->sdhc_id;
333
334 sdhci_enable_sdio_irq(mmc, enable);
335
336 if (enable) {
337 /*
338 * Set SDIO Card Inserted indication
339 * to enable detecting SDIO async irq.
340 */
341 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
342 reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
343 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
344 } else {
345 /* Clear SDIO Card Inserted indication */
346 reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
347 reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
348 sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
349 }
350}
351
352static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
353{
354 host->mmc_host_ops.set_ios = xenon_set_ios;
355 host->mmc_host_ops.start_signal_voltage_switch =
356 xenon_start_signal_voltage_switch;
357 host->mmc_host_ops.init_card = xenon_init_card;
358 host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
359 host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
360}
361
362/*
363 * Parse Xenon specific DT properties:
364 * sdhc-id: the index of current SDHC.
365 * Refer to XENON_SYS_CFG_INFO register
366 * tun-count: the interval between re-tuning
367 */
368static int xenon_probe_dt(struct platform_device *pdev)
369{
370 struct device_node *np = pdev->dev.of_node;
371 struct sdhci_host *host = platform_get_drvdata(pdev);
372 struct mmc_host *mmc = host->mmc;
373 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
374 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
375 u32 sdhc_id, nr_sdhc;
376 u32 tuning_count;
377
378 /* Disable HS200 on Armada AP806 */
379 if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
380 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
381
382 sdhc_id = 0x0;
383 if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
384 nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
385 nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
386 if (unlikely(sdhc_id > nr_sdhc)) {
387 dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
388 sdhc_id, nr_sdhc);
389 return -EINVAL;
390 }
391 }
392 priv->sdhc_id = sdhc_id;
393
394 tuning_count = XENON_DEF_TUNING_COUNT;
395 if (!of_property_read_u32(np, "marvell,xenon-tun-count",
396 &tuning_count)) {
397 if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
398 dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
399 XENON_DEF_TUNING_COUNT);
400 tuning_count = XENON_DEF_TUNING_COUNT;
401 }
402 }
403 priv->tuning_count = tuning_count;
404
Hu Ziji06c8b662017-03-30 17:23:00 +0200405 return xenon_phy_parse_dt(np, host);
Hu Ziji3a3748d2017-03-30 17:22:59 +0200406}
407
408static int xenon_sdhc_prepare(struct sdhci_host *host)
409{
410 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
411 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
412 u8 sdhc_id = priv->sdhc_id;
413
414 /* Enable SDHC */
415 xenon_enable_sdhc(host, sdhc_id);
416
417 /* Enable ACG */
418 xenon_set_acg(host, true);
419
420 /* Enable Parallel Transfer Mode */
421 xenon_enable_sdhc_parallel_tran(host, sdhc_id);
422
423 /* Disable SDCLK-Off-While-Idle before card init */
424 xenon_set_sdclk_off_idle(host, sdhc_id, false);
425
426 xenon_mask_cmd_conflict_err(host);
427
428 return 0;
429}
430
431static void xenon_sdhc_unprepare(struct sdhci_host *host)
432{
433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
434 struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
435 u8 sdhc_id = priv->sdhc_id;
436
437 /* disable SDHC */
438 xenon_disable_sdhc(host, sdhc_id);
439}
440
441static int xenon_probe(struct platform_device *pdev)
442{
443 struct sdhci_pltfm_host *pltfm_host;
444 struct sdhci_host *host;
445 struct xenon_priv *priv;
446 int err;
447
448 host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
449 sizeof(struct xenon_priv));
450 if (IS_ERR(host))
451 return PTR_ERR(host);
452
453 pltfm_host = sdhci_priv(host);
454 priv = sdhci_pltfm_priv(pltfm_host);
455
456 /*
457 * Link Xenon specific mmc_host_ops function,
458 * to replace standard ones in sdhci_ops.
459 */
460 xenon_replace_mmc_host_ops(host);
461
462 pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
463 if (IS_ERR(pltfm_host->clk)) {
464 err = PTR_ERR(pltfm_host->clk);
465 dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
466 goto free_pltfm;
467 }
468 err = clk_prepare_enable(pltfm_host->clk);
469 if (err)
470 goto free_pltfm;
471
472 err = mmc_of_parse(host->mmc);
473 if (err)
474 goto err_clk;
475
476 sdhci_get_of_property(pdev);
477
478 xenon_set_acg(host, false);
479
480 /* Xenon specific dt parse */
481 err = xenon_probe_dt(pdev);
482 if (err)
483 goto err_clk;
484
485 err = xenon_sdhc_prepare(host);
486 if (err)
Hu Ziji06c8b662017-03-30 17:23:00 +0200487 goto clean_phy_param;
Hu Ziji3a3748d2017-03-30 17:22:59 +0200488
489 err = sdhci_add_host(host);
490 if (err)
491 goto remove_sdhc;
492
493 return 0;
494
495remove_sdhc:
496 xenon_sdhc_unprepare(host);
Hu Ziji06c8b662017-03-30 17:23:00 +0200497clean_phy_param:
498 xenon_clean_phy(host);
Hu Ziji3a3748d2017-03-30 17:22:59 +0200499err_clk:
500 clk_disable_unprepare(pltfm_host->clk);
501free_pltfm:
502 sdhci_pltfm_free(pdev);
503 return err;
504}
505
506static int xenon_remove(struct platform_device *pdev)
507{
508 struct sdhci_host *host = platform_get_drvdata(pdev);
509 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
510
Hu Ziji06c8b662017-03-30 17:23:00 +0200511 xenon_clean_phy(host);
512
Hu Ziji3a3748d2017-03-30 17:22:59 +0200513 xenon_sdhc_unprepare(host);
514
515 sdhci_remove_host(host, 0);
516
517 clk_disable_unprepare(pltfm_host->clk);
518
519 sdhci_pltfm_free(pdev);
520
521 return 0;
522}
523
524static const struct of_device_id sdhci_xenon_dt_ids[] = {
525 { .compatible = "marvell,armada-ap806-sdhci",},
526 { .compatible = "marvell,armada-cp110-sdhci",},
527 { .compatible = "marvell,armada-3700-sdhci",},
528 {}
529};
530MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
531
532static struct platform_driver sdhci_xenon_driver = {
533 .driver = {
534 .name = "xenon-sdhci",
535 .of_match_table = sdhci_xenon_dt_ids,
536 .pm = &sdhci_pltfm_pmops,
537 },
538 .probe = xenon_probe,
539 .remove = xenon_remove,
540};
541
542module_platform_driver(sdhci_xenon_driver);
543
544MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
545MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
546MODULE_LICENSE("GPL v2");