Lines Matching +full:max +full:- +full:clk +full:- +full:rate +full:- +full:hz
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* linux/drivers/mmc/host/sdhci-s3c.c
14 #include <linux/dma-mapping.h>
16 #include <linux/platform_data/mmc-sdhci-s3c.h>
18 #include <linux/clk.h>
104 * struct sdhci_s3c - S3C SDHCI instance
114 * @no_divider: No or non-standard internal clock divider.
124 struct clk *clk_io;
125 struct clk *clk_bus[MAX_BUS_CLK];
132 * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
134 * @no_divider: no or non-standard internal clock divider.
151 * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
154 * Callback to return the maximum clock rate acheivable by the controller.
159 unsigned long rate, max = 0; in sdhci_s3c_get_max_clk() local
163 rate = ourhost->clk_rates[src]; in sdhci_s3c_get_max_clk()
164 if (rate > max) in sdhci_s3c_get_max_clk()
165 max = rate; in sdhci_s3c_get_max_clk()
168 return max; in sdhci_s3c_get_max_clk()
172 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
181 unsigned long rate; in sdhci_s3c_consider_clock() local
182 struct clk *clksrc = ourhost->clk_bus[src]; in sdhci_s3c_consider_clock()
189 * If controller uses a non-standard clock division, find the best clock in sdhci_s3c_consider_clock()
192 if (ourhost->no_divider) { in sdhci_s3c_consider_clock()
193 rate = clk_round_rate(clksrc, wanted); in sdhci_s3c_consider_clock()
194 return wanted - rate; in sdhci_s3c_consider_clock()
197 rate = ourhost->clk_rates[src]; in sdhci_s3c_consider_clock()
200 if ((rate >> shift) <= wanted) in sdhci_s3c_consider_clock()
205 dev_dbg(&ourhost->pdev->dev, in sdhci_s3c_consider_clock()
206 "clk %d: rate %ld, min rate %lu > wanted %u\n", in sdhci_s3c_consider_clock()
207 src, rate, rate / 256, wanted); in sdhci_s3c_consider_clock()
211 dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n", in sdhci_s3c_consider_clock()
212 src, rate, wanted, rate >> shift); in sdhci_s3c_consider_clock()
214 return wanted - (rate >> shift); in sdhci_s3c_consider_clock()
218 * sdhci_s3c_set_clock - callback on clock change
220 * @clock: The clock rate being requested.
234 host->mmc->actual_clock = 0; in sdhci_s3c_set_clock()
250 dev_dbg(&ourhost->pdev->dev, in sdhci_s3c_set_clock()
255 if (ourhost->cur_clk != best_src) { in sdhci_s3c_set_clock()
256 struct clk *clk = ourhost->clk_bus[best_src]; in sdhci_s3c_set_clock() local
258 clk_prepare_enable(clk); in sdhci_s3c_set_clock()
259 if (ourhost->cur_clk >= 0) in sdhci_s3c_set_clock()
261 ourhost->clk_bus[ourhost->cur_clk]); in sdhci_s3c_set_clock()
263 ourhost->cur_clk = best_src; in sdhci_s3c_set_clock()
264 host->max_clk = ourhost->clk_rates[best_src]; in sdhci_s3c_set_clock()
268 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL); in sdhci_s3c_set_clock()
270 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2); in sdhci_s3c_set_clock()
273 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2); in sdhci_s3c_set_clock()
277 host->ioaddr + S3C64XX_SDHCI_CONTROL4); in sdhci_s3c_set_clock()
279 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2); in sdhci_s3c_set_clock()
285 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2); in sdhci_s3c_set_clock()
287 /* reconfigure the controller for new clock rate */ in sdhci_s3c_set_clock()
291 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3); in sdhci_s3c_set_clock()
297 * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
308 unsigned long rate, min = ULONG_MAX; in sdhci_s3c_get_min_clock() local
312 rate = ourhost->clk_rates[src] / 256; in sdhci_s3c_get_min_clock()
313 if (!rate) in sdhci_s3c_get_min_clock()
315 if (rate < min) in sdhci_s3c_get_min_clock()
316 min = rate; in sdhci_s3c_get_min_clock()
322 /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
326 unsigned long rate, max = 0; in sdhci_cmu_get_max_clock() local
330 struct clk *clk; in sdhci_cmu_get_max_clock() local
332 clk = ourhost->clk_bus[src]; in sdhci_cmu_get_max_clock()
333 if (IS_ERR(clk)) in sdhci_cmu_get_max_clock()
336 rate = clk_round_rate(clk, ULONG_MAX); in sdhci_cmu_get_max_clock()
337 if (rate > max) in sdhci_cmu_get_max_clock()
338 max = rate; in sdhci_cmu_get_max_clock()
341 return max; in sdhci_cmu_get_max_clock()
344 /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
348 unsigned long rate, min = ULONG_MAX; in sdhci_cmu_get_min_clock() local
352 struct clk *clk; in sdhci_cmu_get_min_clock() local
354 clk = ourhost->clk_bus[src]; in sdhci_cmu_get_min_clock()
355 if (IS_ERR(clk)) in sdhci_cmu_get_min_clock()
358 rate = clk_round_rate(clk, 0); in sdhci_cmu_get_min_clock()
359 if (rate < min) in sdhci_cmu_get_min_clock()
360 min = rate; in sdhci_cmu_get_min_clock()
366 /* sdhci_cmu_set_clock - callback on clock change.*/
370 struct device *dev = &ourhost->pdev->dev; in sdhci_cmu_set_clock()
372 u16 clk = 0; in sdhci_cmu_set_clock() local
375 host->mmc->actual_clock = 0; in sdhci_cmu_set_clock()
386 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); in sdhci_cmu_set_clock()
387 clk &= ~SDHCI_CLOCK_CARD_EN; in sdhci_cmu_set_clock()
388 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); in sdhci_cmu_set_clock()
390 ret = clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock); in sdhci_cmu_set_clock()
392 dev_err(dev, "%s: failed to set clock rate %uHz\n", in sdhci_cmu_set_clock()
393 mmc_hostname(host->mmc), clock); in sdhci_cmu_set_clock()
397 clk = SDHCI_CLOCK_INT_EN; in sdhci_cmu_set_clock()
398 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); in sdhci_cmu_set_clock()
400 /* Wait max 20 ms */ in sdhci_cmu_set_clock()
402 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) in sdhci_cmu_set_clock()
406 mmc_hostname(host->mmc)); in sdhci_cmu_set_clock()
409 timeout--; in sdhci_cmu_set_clock()
413 clk |= SDHCI_CLOCK_CARD_EN; in sdhci_cmu_set_clock()
414 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); in sdhci_cmu_set_clock()
430 struct device_node *node = dev->of_node; in sdhci_s3c_parse_dt()
433 /* if the bus-width property is not specified, assume width as 1 */ in sdhci_s3c_parse_dt()
434 if (of_property_read_u32(node, "bus-width", &max_width)) in sdhci_s3c_parse_dt()
436 pdata->max_width = max_width; in sdhci_s3c_parse_dt()
439 if (of_get_property(node, "broken-cd", NULL)) { in sdhci_s3c_parse_dt()
440 pdata->cd_type = S3C_SDHCI_CD_NONE; in sdhci_s3c_parse_dt()
444 if (of_get_property(node, "non-removable", NULL)) { in sdhci_s3c_parse_dt()
445 pdata->cd_type = S3C_SDHCI_CD_PERMANENT; in sdhci_s3c_parse_dt()
449 if (of_get_named_gpio(node, "cd-gpios", 0)) in sdhci_s3c_parse_dt()
453 pdata->cd_type = S3C_SDHCI_CD_INTERNAL; in sdhci_s3c_parse_dt()
460 return -EINVAL; in sdhci_s3c_parse_dt()
472 if (pdev->dev.of_node) { in sdhci_s3c_get_driver_data()
474 match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node); in sdhci_s3c_get_driver_data()
475 return (struct sdhci_s3c_drv_data *)match->data; in sdhci_s3c_get_driver_data()
479 platform_get_device_id(pdev)->driver_data; in sdhci_s3c_get_driver_data()
486 struct device *dev = &pdev->dev; in sdhci_s3c_probe()
491 if (!pdev->dev.platform_data && !pdev->dev.of_node) { in sdhci_s3c_probe()
493 return -ENOENT; in sdhci_s3c_probe()
507 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); in sdhci_s3c_probe()
509 ret = -ENOMEM; in sdhci_s3c_probe()
513 if (pdev->dev.of_node) { in sdhci_s3c_probe()
514 ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata); in sdhci_s3c_probe()
518 memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata)); in sdhci_s3c_probe()
523 sc->host = host; in sdhci_s3c_probe()
524 sc->pdev = pdev; in sdhci_s3c_probe()
525 sc->pdata = pdata; in sdhci_s3c_probe()
526 sc->cur_clk = -1; in sdhci_s3c_probe()
530 sc->clk_io = devm_clk_get(dev, "hsmmc"); in sdhci_s3c_probe()
531 if (IS_ERR(sc->clk_io)) { in sdhci_s3c_probe()
533 ret = PTR_ERR(sc->clk_io); in sdhci_s3c_probe()
538 clk_prepare_enable(sc->clk_io); in sdhci_s3c_probe()
544 sc->clk_bus[ptr] = devm_clk_get(dev, name); in sdhci_s3c_probe()
545 if (IS_ERR(sc->clk_bus[ptr])) in sdhci_s3c_probe()
549 sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]); in sdhci_s3c_probe()
551 dev_info(dev, "clock source %d: %s (%ld Hz)\n", in sdhci_s3c_probe()
552 ptr, name, sc->clk_rates[ptr]); in sdhci_s3c_probe()
557 ret = -ENOENT; in sdhci_s3c_probe()
561 host->ioaddr = devm_platform_ioremap_resource(pdev, 0); in sdhci_s3c_probe()
562 if (IS_ERR(host->ioaddr)) { in sdhci_s3c_probe()
563 ret = PTR_ERR(host->ioaddr); in sdhci_s3c_probe()
567 /* Ensure we have minimal gpio selected CMD/CLK/Detect */ in sdhci_s3c_probe()
568 if (pdata->cfg_gpio) in sdhci_s3c_probe()
569 pdata->cfg_gpio(pdev, pdata->max_width); in sdhci_s3c_probe()
571 host->hw_name = "samsung-hsmmc"; in sdhci_s3c_probe()
572 host->ops = &sdhci_s3c_ops; in sdhci_s3c_probe()
573 host->quirks = 0; in sdhci_s3c_probe()
574 host->quirks2 = 0; in sdhci_s3c_probe()
575 host->irq = irq; in sdhci_s3c_probe()
578 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC; in sdhci_s3c_probe()
579 host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT; in sdhci_s3c_probe()
581 host->quirks |= drv_data->sdhci_quirks; in sdhci_s3c_probe()
582 sc->no_divider = drv_data->no_divider; in sdhci_s3c_probe()
589 host->quirks |= SDHCI_QUIRK_BROKEN_DMA; in sdhci_s3c_probe()
593 /* It seems we do not get an DATA transfer complete on non-busy in sdhci_s3c_probe()
596 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ; in sdhci_s3c_probe()
599 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12; in sdhci_s3c_probe()
602 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC; in sdhci_s3c_probe()
604 if (pdata->cd_type == S3C_SDHCI_CD_NONE || in sdhci_s3c_probe()
605 pdata->cd_type == S3C_SDHCI_CD_PERMANENT) in sdhci_s3c_probe()
606 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; in sdhci_s3c_probe()
608 if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT) in sdhci_s3c_probe()
609 host->mmc->caps = MMC_CAP_NONREMOVABLE; in sdhci_s3c_probe()
611 switch (pdata->max_width) { in sdhci_s3c_probe()
613 host->mmc->caps |= MMC_CAP_8_BIT_DATA; in sdhci_s3c_probe()
616 host->mmc->caps |= MMC_CAP_4_BIT_DATA; in sdhci_s3c_probe()
620 if (pdata->pm_caps) in sdhci_s3c_probe()
621 host->mmc->pm_caps |= pdata->pm_caps; in sdhci_s3c_probe()
623 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR | in sdhci_s3c_probe()
627 host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK; in sdhci_s3c_probe()
633 if (sc->no_divider) { in sdhci_s3c_probe()
640 if (pdata->host_caps) in sdhci_s3c_probe()
641 host->mmc->caps |= pdata->host_caps; in sdhci_s3c_probe()
643 if (pdata->host_caps2) in sdhci_s3c_probe()
644 host->mmc->caps2 |= pdata->host_caps2; in sdhci_s3c_probe()
646 pm_runtime_enable(&pdev->dev); in sdhci_s3c_probe()
647 pm_runtime_set_autosuspend_delay(&pdev->dev, 50); in sdhci_s3c_probe()
648 pm_runtime_use_autosuspend(&pdev->dev); in sdhci_s3c_probe()
649 pm_suspend_ignore_children(&pdev->dev, 1); in sdhci_s3c_probe()
651 ret = mmc_of_parse(host->mmc); in sdhci_s3c_probe()
660 if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL) in sdhci_s3c_probe()
661 clk_disable_unprepare(sc->clk_io); in sdhci_s3c_probe()
666 pm_runtime_disable(&pdev->dev); in sdhci_s3c_probe()
669 clk_disable_unprepare(sc->clk_io); in sdhci_s3c_probe()
682 if (sc->ext_cd_irq) in sdhci_s3c_remove()
683 free_irq(sc->ext_cd_irq, sc); in sdhci_s3c_remove()
686 if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL) in sdhci_s3c_remove()
687 clk_prepare_enable(sc->clk_io); in sdhci_s3c_remove()
691 pm_runtime_dont_use_autosuspend(&pdev->dev); in sdhci_s3c_remove()
692 pm_runtime_disable(&pdev->dev); in sdhci_s3c_remove()
694 clk_disable_unprepare(sc->clk_io); in sdhci_s3c_remove()
706 if (host->tuning_mode != SDHCI_TUNING_MODE_3) in sdhci_s3c_suspend()
707 mmc_retune_needed(host->mmc); in sdhci_s3c_suspend()
725 struct clk *busclk = ourhost->clk_io; in sdhci_s3c_runtime_suspend()
730 if (host->tuning_mode != SDHCI_TUNING_MODE_3) in sdhci_s3c_runtime_suspend()
731 mmc_retune_needed(host->mmc); in sdhci_s3c_runtime_suspend()
733 if (ourhost->cur_clk >= 0) in sdhci_s3c_runtime_suspend()
734 clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]); in sdhci_s3c_runtime_suspend()
743 struct clk *busclk = ourhost->clk_io; in sdhci_s3c_runtime_resume()
747 if (ourhost->cur_clk >= 0) in sdhci_s3c_runtime_resume()
748 clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]); in sdhci_s3c_runtime_resume()
762 .name = "s3c-sdhci",
775 { .compatible = "samsung,s3c6410-sdhci", },
776 { .compatible = "samsung,exynos4210-sdhci",
788 .name = "s3c-sdhci",
800 MODULE_ALIAS("platform:s3c-sdhci");