ASoC: arizona: Correct handling of FLL theta in synchroniser mode
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-of-arasan.c
blobe0f193f7e3e50c9f41efe074da079895199b6ca8
1 /*
2 * Arasan Secure Digital Host Controller Interface.
3 * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
4 * Copyright (c) 2012 Wind River Systems, Inc.
5 * Copyright (C) 2013 Pengutronix e.K.
6 * Copyright (C) 2013 Xilinx Inc.
8 * Based on sdhci-of-esdhc.c
10 * Copyright (c) 2007 Freescale Semiconductor, Inc.
11 * Copyright (c) 2009 MontaVista Software, Inc.
13 * Authors: Xiaobo Xie <X.Xie@freescale.com>
14 * Anton Vorontsov <avorontsov@ru.mvista.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or (at
19 * your option) any later version.
22 #include <linux/clk-provider.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/phy/phy.h>
27 #include <linux/regmap.h>
28 #include "sdhci-pltfm.h"
30 #define SDHCI_ARASAN_CLK_CTRL_OFFSET 0x2c
31 #define SDHCI_ARASAN_VENDOR_REGISTER 0x78
33 #define VENDOR_ENHANCED_STROBE BIT(0)
34 #define CLK_CTRL_TIMEOUT_SHIFT 16
35 #define CLK_CTRL_TIMEOUT_MASK (0xf << CLK_CTRL_TIMEOUT_SHIFT)
36 #define CLK_CTRL_TIMEOUT_MIN_EXP 13
39 * On some SoCs the syscon area has a feature where the upper 16-bits of
40 * each 32-bit register act as a write mask for the lower 16-bits. This allows
41 * atomic updates of the register without locking. This macro is used on SoCs
42 * that have that feature.
44 #define HIWORD_UPDATE(val, mask, shift) \
45 ((val) << (shift) | (mask) << ((shift) + 16))
47 /**
48 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
50 * @reg: Offset within the syscon of the register containing this field
51 * @width: Number of bits for this field
52 * @shift: Bit offset within @reg of this field (or -1 if not avail)
54 struct sdhci_arasan_soc_ctl_field {
55 u32 reg;
56 u16 width;
57 s16 shift;
60 /**
61 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
63 * It's up to the licensee of the Arsan IP block to make these available
64 * somewhere if needed. Presumably these will be scattered somewhere that's
65 * accessible via the syscon API.
67 * @baseclkfreq: Where to find corecfg_baseclkfreq
68 * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
70 struct sdhci_arasan_soc_ctl_map {
71 struct sdhci_arasan_soc_ctl_field baseclkfreq;
72 bool hiword_update;
75 /**
76 * struct sdhci_arasan_data
77 * @host: Pointer to the main SDHCI host structure.
78 * @clk_ahb: Pointer to the AHB clock
79 * @phy: Pointer to the generic phy
80 * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
81 * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
82 * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
83 * @soc_ctl_map: Map to get offsets into soc_ctl registers.
85 struct sdhci_arasan_data {
86 struct sdhci_host *host;
87 struct clk *clk_ahb;
88 struct phy *phy;
90 struct clk_hw sdcardclk_hw;
91 struct clk *sdcardclk;
93 struct regmap *soc_ctl_base;
94 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
97 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
98 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
99 .hiword_update = true,
103 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
105 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
106 * Note that if a field is specified as not available (shift < 0) then
107 * this function will silently return an error code. It will be noisy
108 * and print errors for any other (unexpected) errors.
110 * @host: The sdhci_host
111 * @fld: The field to write to
112 * @val: The value to write
114 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
115 const struct sdhci_arasan_soc_ctl_field *fld,
116 u32 val)
118 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
119 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
120 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
121 u32 reg = fld->reg;
122 u16 width = fld->width;
123 s16 shift = fld->shift;
124 int ret;
127 * Silently return errors for shift < 0 so caller doesn't have
128 * to check for fields which are optional. For fields that
129 * are required then caller needs to do something special
130 * anyway.
132 if (shift < 0)
133 return -EINVAL;
135 if (sdhci_arasan->soc_ctl_map->hiword_update)
136 ret = regmap_write(soc_ctl_base, reg,
137 HIWORD_UPDATE(val, GENMASK(width, 0),
138 shift));
139 else
140 ret = regmap_update_bits(soc_ctl_base, reg,
141 GENMASK(shift + width, shift),
142 val << shift);
144 /* Yell about (unexpected) regmap errors */
145 if (ret)
146 pr_warn("%s: Regmap write fail: %d\n",
147 mmc_hostname(host->mmc), ret);
149 return ret;
152 static unsigned int sdhci_arasan_get_timeout_clock(struct sdhci_host *host)
154 u32 div;
155 unsigned long freq;
156 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
158 div = readl(host->ioaddr + SDHCI_ARASAN_CLK_CTRL_OFFSET);
159 div = (div & CLK_CTRL_TIMEOUT_MASK) >> CLK_CTRL_TIMEOUT_SHIFT;
161 freq = clk_get_rate(pltfm_host->clk);
162 freq /= 1 << (CLK_CTRL_TIMEOUT_MIN_EXP + div);
164 return freq;
167 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
169 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
170 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
171 bool ctrl_phy = false;
173 if (clock > MMC_HIGH_52_MAX_DTR && (!IS_ERR(sdhci_arasan->phy)))
174 ctrl_phy = true;
176 if (ctrl_phy) {
177 spin_unlock_irq(&host->lock);
178 phy_power_off(sdhci_arasan->phy);
179 spin_lock_irq(&host->lock);
182 sdhci_set_clock(host, clock);
184 if (ctrl_phy) {
185 spin_unlock_irq(&host->lock);
186 phy_power_on(sdhci_arasan->phy);
187 spin_lock_irq(&host->lock);
191 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
192 struct mmc_ios *ios)
194 u32 vendor;
195 struct sdhci_host *host = mmc_priv(mmc);
197 vendor = readl(host->ioaddr + SDHCI_ARASAN_VENDOR_REGISTER);
198 if (ios->enhanced_strobe)
199 vendor |= VENDOR_ENHANCED_STROBE;
200 else
201 vendor &= ~VENDOR_ENHANCED_STROBE;
203 writel(vendor, host->ioaddr + SDHCI_ARASAN_VENDOR_REGISTER);
206 static struct sdhci_ops sdhci_arasan_ops = {
207 .set_clock = sdhci_arasan_set_clock,
208 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
209 .get_timeout_clock = sdhci_arasan_get_timeout_clock,
210 .set_bus_width = sdhci_set_bus_width,
211 .reset = sdhci_reset,
212 .set_uhs_signaling = sdhci_set_uhs_signaling,
215 static struct sdhci_pltfm_data sdhci_arasan_pdata = {
216 .ops = &sdhci_arasan_ops,
217 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
218 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
219 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
222 #ifdef CONFIG_PM_SLEEP
224 * sdhci_arasan_suspend - Suspend method for the driver
225 * @dev: Address of the device structure
226 * Returns 0 on success and error value on error
228 * Put the device in a low power state.
230 static int sdhci_arasan_suspend(struct device *dev)
232 struct platform_device *pdev = to_platform_device(dev);
233 struct sdhci_host *host = platform_get_drvdata(pdev);
234 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
235 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
236 int ret;
238 ret = sdhci_suspend_host(host);
239 if (ret)
240 return ret;
242 if (!IS_ERR(sdhci_arasan->phy)) {
243 ret = phy_power_off(sdhci_arasan->phy);
244 if (ret) {
245 dev_err(dev, "Cannot power off phy.\n");
246 sdhci_resume_host(host);
247 return ret;
251 clk_disable(pltfm_host->clk);
252 clk_disable(sdhci_arasan->clk_ahb);
254 return 0;
258 * sdhci_arasan_resume - Resume method for the driver
259 * @dev: Address of the device structure
260 * Returns 0 on success and error value on error
262 * Resume operation after suspend
264 static int sdhci_arasan_resume(struct device *dev)
266 struct platform_device *pdev = to_platform_device(dev);
267 struct sdhci_host *host = platform_get_drvdata(pdev);
268 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
269 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
270 int ret;
272 ret = clk_enable(sdhci_arasan->clk_ahb);
273 if (ret) {
274 dev_err(dev, "Cannot enable AHB clock.\n");
275 return ret;
278 ret = clk_enable(pltfm_host->clk);
279 if (ret) {
280 dev_err(dev, "Cannot enable SD clock.\n");
281 return ret;
284 if (!IS_ERR(sdhci_arasan->phy)) {
285 ret = phy_power_on(sdhci_arasan->phy);
286 if (ret) {
287 dev_err(dev, "Cannot power on phy.\n");
288 return ret;
292 return sdhci_resume_host(host);
294 #endif /* ! CONFIG_PM_SLEEP */
296 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
297 sdhci_arasan_resume);
299 static const struct of_device_id sdhci_arasan_of_match[] = {
300 /* SoC-specific compatible strings w/ soc_ctl_map */
302 .compatible = "rockchip,rk3399-sdhci-5.1",
303 .data = &rk3399_soc_ctl_map,
306 /* Generic compatible below here */
307 { .compatible = "arasan,sdhci-8.9a" },
308 { .compatible = "arasan,sdhci-5.1" },
309 { .compatible = "arasan,sdhci-4.9a" },
311 { /* sentinel */ }
313 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
316 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
318 * Return the current actual rate of the SD card clock. This can be used
319 * to communicate with out PHY.
321 * @hw: Pointer to the hardware clock structure.
322 * @parent_rate The parent rate (should be rate of clk_xin).
323 * Returns the card clock rate.
325 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
326 unsigned long parent_rate)
329 struct sdhci_arasan_data *sdhci_arasan =
330 container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
331 struct sdhci_host *host = sdhci_arasan->host;
333 return host->mmc->actual_clock;
336 static const struct clk_ops arasan_sdcardclk_ops = {
337 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
341 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
343 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
344 * function can be used to make that happen.
346 * NOTES:
347 * - Many existing devices don't seem to do this and work fine. To keep
348 * compatibility for old hardware where the device tree doesn't provide a
349 * register map, this function is a noop if a soc_ctl_map hasn't been provided
350 * for this platform.
351 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
352 * to achieve lower clock rates. That means that this function is called once
353 * at probe time and never called again.
355 * @host: The sdhci_host
357 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
359 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
360 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
361 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
362 sdhci_arasan->soc_ctl_map;
363 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
365 /* Having a map is optional */
366 if (!soc_ctl_map)
367 return;
369 /* If we have a map, we expect to have a syscon */
370 if (!sdhci_arasan->soc_ctl_base) {
371 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
372 mmc_hostname(host->mmc));
373 return;
376 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
380 * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
382 * Some PHY devices need to know what the actual card clock is. In order for
383 * them to find out, we'll provide a clock through the common clock framework
384 * for them to query.
386 * Note: without seriously re-architecting SDHCI's clock code and testing on
387 * all platforms, there's no way to create a totally beautiful clock here
388 * with all clock ops implemented. Instead, we'll just create a clock that can
389 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
390 * framework that we're doing things behind its back. This should be sufficient
391 * to create nice clean device tree bindings and later (if needed) we can try
392 * re-architecting SDHCI if we see some benefit to it.
394 * @sdhci_arasan: Our private data structure.
395 * @clk_xin: Pointer to the functional clock
396 * @dev: Pointer to our struct device.
397 * Returns 0 on success and error value on error
399 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
400 struct clk *clk_xin,
401 struct device *dev)
403 struct device_node *np = dev->of_node;
404 struct clk_init_data sdcardclk_init;
405 const char *parent_clk_name;
406 int ret;
408 /* Providing a clock to the PHY is optional; no error if missing */
409 if (!of_find_property(np, "#clock-cells", NULL))
410 return 0;
412 ret = of_property_read_string_index(np, "clock-output-names", 0,
413 &sdcardclk_init.name);
414 if (ret) {
415 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
416 return ret;
419 parent_clk_name = __clk_get_name(clk_xin);
420 sdcardclk_init.parent_names = &parent_clk_name;
421 sdcardclk_init.num_parents = 1;
422 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
423 sdcardclk_init.ops = &arasan_sdcardclk_ops;
425 sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
426 sdhci_arasan->sdcardclk =
427 devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
428 sdhci_arasan->sdcardclk_hw.init = NULL;
430 ret = of_clk_add_provider(np, of_clk_src_simple_get,
431 sdhci_arasan->sdcardclk);
432 if (ret)
433 dev_err(dev, "Failed to add clock provider\n");
435 return ret;
439 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
441 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
442 * returned success.
444 * @dev: Pointer to our struct device.
446 static void sdhci_arasan_unregister_sdclk(struct device *dev)
448 struct device_node *np = dev->of_node;
450 if (!of_find_property(np, "#clock-cells", NULL))
451 return;
453 of_clk_del_provider(dev->of_node);
456 static int sdhci_arasan_probe(struct platform_device *pdev)
458 int ret;
459 const struct of_device_id *match;
460 struct device_node *node;
461 struct clk *clk_xin;
462 struct sdhci_host *host;
463 struct sdhci_pltfm_host *pltfm_host;
464 struct sdhci_arasan_data *sdhci_arasan;
466 host = sdhci_pltfm_init(pdev, &sdhci_arasan_pdata,
467 sizeof(*sdhci_arasan));
468 if (IS_ERR(host))
469 return PTR_ERR(host);
471 pltfm_host = sdhci_priv(host);
472 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
473 sdhci_arasan->host = host;
475 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
476 sdhci_arasan->soc_ctl_map = match->data;
478 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
479 if (node) {
480 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
481 of_node_put(node);
483 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
484 ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
485 if (ret != -EPROBE_DEFER)
486 dev_err(&pdev->dev, "Can't get syscon: %d\n",
487 ret);
488 goto err_pltfm_free;
492 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
493 if (IS_ERR(sdhci_arasan->clk_ahb)) {
494 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
495 ret = PTR_ERR(sdhci_arasan->clk_ahb);
496 goto err_pltfm_free;
499 clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
500 if (IS_ERR(clk_xin)) {
501 dev_err(&pdev->dev, "clk_xin clock not found.\n");
502 ret = PTR_ERR(clk_xin);
503 goto err_pltfm_free;
506 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
507 if (ret) {
508 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
509 goto err_pltfm_free;
512 ret = clk_prepare_enable(clk_xin);
513 if (ret) {
514 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
515 goto clk_dis_ahb;
518 sdhci_get_of_property(pdev);
519 pltfm_host->clk = clk_xin;
521 sdhci_arasan_update_baseclkfreq(host);
523 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
524 if (ret)
525 goto clk_disable_all;
527 ret = mmc_of_parse(host->mmc);
528 if (ret) {
529 dev_err(&pdev->dev, "parsing dt failed (%u)\n", ret);
530 goto unreg_clk;
533 sdhci_arasan->phy = ERR_PTR(-ENODEV);
534 if (of_device_is_compatible(pdev->dev.of_node,
535 "arasan,sdhci-5.1")) {
536 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
537 "phy_arasan");
538 if (IS_ERR(sdhci_arasan->phy)) {
539 ret = PTR_ERR(sdhci_arasan->phy);
540 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
541 goto unreg_clk;
544 ret = phy_init(sdhci_arasan->phy);
545 if (ret < 0) {
546 dev_err(&pdev->dev, "phy_init err.\n");
547 goto unreg_clk;
550 ret = phy_power_on(sdhci_arasan->phy);
551 if (ret < 0) {
552 dev_err(&pdev->dev, "phy_power_on err.\n");
553 goto err_phy_power;
556 host->mmc_host_ops.hs400_enhanced_strobe =
557 sdhci_arasan_hs400_enhanced_strobe;
560 ret = sdhci_add_host(host);
561 if (ret)
562 goto err_add_host;
564 return 0;
566 err_add_host:
567 if (!IS_ERR(sdhci_arasan->phy))
568 phy_power_off(sdhci_arasan->phy);
569 err_phy_power:
570 if (!IS_ERR(sdhci_arasan->phy))
571 phy_exit(sdhci_arasan->phy);
572 unreg_clk:
573 sdhci_arasan_unregister_sdclk(&pdev->dev);
574 clk_disable_all:
575 clk_disable_unprepare(clk_xin);
576 clk_dis_ahb:
577 clk_disable_unprepare(sdhci_arasan->clk_ahb);
578 err_pltfm_free:
579 sdhci_pltfm_free(pdev);
580 return ret;
583 static int sdhci_arasan_remove(struct platform_device *pdev)
585 int ret;
586 struct sdhci_host *host = platform_get_drvdata(pdev);
587 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
588 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
589 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
591 if (!IS_ERR(sdhci_arasan->phy)) {
592 phy_power_off(sdhci_arasan->phy);
593 phy_exit(sdhci_arasan->phy);
596 sdhci_arasan_unregister_sdclk(&pdev->dev);
598 ret = sdhci_pltfm_unregister(pdev);
600 clk_disable_unprepare(clk_ahb);
602 return ret;
605 static struct platform_driver sdhci_arasan_driver = {
606 .driver = {
607 .name = "sdhci-arasan",
608 .of_match_table = sdhci_arasan_of_match,
609 .pm = &sdhci_arasan_dev_pm_ops,
611 .probe = sdhci_arasan_probe,
612 .remove = sdhci_arasan_remove,
615 module_platform_driver(sdhci_arasan_driver);
617 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
618 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
619 MODULE_LICENSE("GPL");