Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 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. |
| 7 | * |
| 8 | * Based on sdhci-of-esdhc.c |
| 9 | * |
| 10 | * Copyright (c) 2007 Freescale Semiconductor, Inc. |
| 11 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 12 | * |
| 13 | * Authors: Xiaobo Xie <X.Xie@freescale.com> |
| 14 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
| 15 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
Suman Tripathi | 308f3f8 | 2015-05-04 19:09:51 +0530 | [diff] [blame] | 23 | #include <linux/of_device.h> |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 24 | #include "sdhci-pltfm.h" |
| 25 | |
| 26 | #define SDHCI_ARASAN_CLK_CTRL_OFFSET 0x2c |
| 27 | |
| 28 | #define CLK_CTRL_TIMEOUT_SHIFT 16 |
| 29 | #define CLK_CTRL_TIMEOUT_MASK (0xf << CLK_CTRL_TIMEOUT_SHIFT) |
| 30 | #define CLK_CTRL_TIMEOUT_MIN_EXP 13 |
| 31 | |
| 32 | /** |
| 33 | * struct sdhci_arasan_data |
| 34 | * @clk_ahb: Pointer to the AHB clock |
| 35 | */ |
| 36 | struct sdhci_arasan_data { |
| 37 | struct clk *clk_ahb; |
| 38 | }; |
| 39 | |
| 40 | static unsigned int sdhci_arasan_get_timeout_clock(struct sdhci_host *host) |
| 41 | { |
| 42 | u32 div; |
| 43 | unsigned long freq; |
| 44 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 45 | |
| 46 | div = readl(host->ioaddr + SDHCI_ARASAN_CLK_CTRL_OFFSET); |
| 47 | div = (div & CLK_CTRL_TIMEOUT_MASK) >> CLK_CTRL_TIMEOUT_SHIFT; |
| 48 | |
| 49 | freq = clk_get_rate(pltfm_host->clk); |
| 50 | freq /= 1 << (CLK_CTRL_TIMEOUT_MIN_EXP + div); |
| 51 | |
| 52 | return freq; |
| 53 | } |
| 54 | |
| 55 | static struct sdhci_ops sdhci_arasan_ops = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 56 | .set_clock = sdhci_set_clock, |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 57 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
| 58 | .get_timeout_clock = sdhci_arasan_get_timeout_clock, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 59 | .set_bus_width = sdhci_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 60 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 61 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static struct sdhci_pltfm_data sdhci_arasan_pdata = { |
| 65 | .ops = &sdhci_arasan_ops, |
Suneel Garapati | 2d532d4 | 2015-06-09 13:01:51 +0530 | [diff] [blame] | 66 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 67 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 68 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | #ifdef CONFIG_PM_SLEEP |
| 72 | /** |
| 73 | * sdhci_arasan_suspend - Suspend method for the driver |
| 74 | * @dev: Address of the device structure |
| 75 | * Returns 0 on success and error value on error |
| 76 | * |
| 77 | * Put the device in a low power state. |
| 78 | */ |
| 79 | static int sdhci_arasan_suspend(struct device *dev) |
| 80 | { |
| 81 | struct platform_device *pdev = to_platform_device(dev); |
| 82 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 83 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8921141 | 2016-02-16 21:08:24 +0800 | [diff] [blame] | 84 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 85 | int ret; |
| 86 | |
| 87 | ret = sdhci_suspend_host(host); |
| 88 | if (ret) |
| 89 | return ret; |
| 90 | |
| 91 | clk_disable(pltfm_host->clk); |
| 92 | clk_disable(sdhci_arasan->clk_ahb); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * sdhci_arasan_resume - Resume method for the driver |
| 99 | * @dev: Address of the device structure |
| 100 | * Returns 0 on success and error value on error |
| 101 | * |
| 102 | * Resume operation after suspend |
| 103 | */ |
| 104 | static int sdhci_arasan_resume(struct device *dev) |
| 105 | { |
| 106 | struct platform_device *pdev = to_platform_device(dev); |
| 107 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 108 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8921141 | 2016-02-16 21:08:24 +0800 | [diff] [blame] | 109 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 110 | int ret; |
| 111 | |
| 112 | ret = clk_enable(sdhci_arasan->clk_ahb); |
| 113 | if (ret) { |
| 114 | dev_err(dev, "Cannot enable AHB clock.\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | ret = clk_enable(pltfm_host->clk); |
| 119 | if (ret) { |
| 120 | dev_err(dev, "Cannot enable SD clock.\n"); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return sdhci_resume_host(host); |
| 125 | } |
| 126 | #endif /* ! CONFIG_PM_SLEEP */ |
| 127 | |
| 128 | static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend, |
| 129 | sdhci_arasan_resume); |
| 130 | |
| 131 | static int sdhci_arasan_probe(struct platform_device *pdev) |
| 132 | { |
| 133 | int ret; |
| 134 | struct clk *clk_xin; |
| 135 | struct sdhci_host *host; |
| 136 | struct sdhci_pltfm_host *pltfm_host; |
| 137 | struct sdhci_arasan_data *sdhci_arasan; |
| 138 | |
Jisheng Zhang | 8921141 | 2016-02-16 21:08:24 +0800 | [diff] [blame] | 139 | host = sdhci_pltfm_init(pdev, &sdhci_arasan_pdata, |
| 140 | sizeof(*sdhci_arasan)); |
| 141 | if (IS_ERR(host)) |
| 142 | return PTR_ERR(host); |
| 143 | |
| 144 | pltfm_host = sdhci_priv(host); |
| 145 | sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 146 | |
| 147 | sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb"); |
| 148 | if (IS_ERR(sdhci_arasan->clk_ahb)) { |
| 149 | dev_err(&pdev->dev, "clk_ahb clock not found.\n"); |
Shawn Lin | 278d096 | 2016-03-07 23:39:08 +0800 | [diff] [blame^] | 150 | ret = PTR_ERR(sdhci_arasan->clk_ahb); |
| 151 | goto err_pltfm_free; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | clk_xin = devm_clk_get(&pdev->dev, "clk_xin"); |
| 155 | if (IS_ERR(clk_xin)) { |
| 156 | dev_err(&pdev->dev, "clk_xin clock not found.\n"); |
Shawn Lin | 278d096 | 2016-03-07 23:39:08 +0800 | [diff] [blame^] | 157 | ret = PTR_ERR(clk_xin); |
| 158 | goto err_pltfm_free; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | ret = clk_prepare_enable(sdhci_arasan->clk_ahb); |
| 162 | if (ret) { |
| 163 | dev_err(&pdev->dev, "Unable to enable AHB clock.\n"); |
Shawn Lin | 278d096 | 2016-03-07 23:39:08 +0800 | [diff] [blame^] | 164 | goto err_pltfm_free; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | ret = clk_prepare_enable(clk_xin); |
| 168 | if (ret) { |
| 169 | dev_err(&pdev->dev, "Unable to enable SD clock.\n"); |
| 170 | goto clk_dis_ahb; |
| 171 | } |
| 172 | |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 173 | sdhci_get_of_property(pdev); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 174 | pltfm_host->clk = clk_xin; |
| 175 | |
Michal Simek | 16b2378 | 2015-04-07 07:57:32 +0200 | [diff] [blame] | 176 | ret = mmc_of_parse(host->mmc); |
| 177 | if (ret) { |
| 178 | dev_err(&pdev->dev, "parsing dt failed (%u)\n", ret); |
| 179 | goto clk_disable_all; |
| 180 | } |
| 181 | |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 182 | ret = sdhci_add_host(host); |
Mike Looijmans | b1df9de | 2014-10-28 08:53:21 +0100 | [diff] [blame] | 183 | if (ret) |
Shawn Lin | 278d096 | 2016-03-07 23:39:08 +0800 | [diff] [blame^] | 184 | goto clk_disable_all; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 188 | clk_disable_all: |
| 189 | clk_disable_unprepare(clk_xin); |
| 190 | clk_dis_ahb: |
| 191 | clk_disable_unprepare(sdhci_arasan->clk_ahb); |
Shawn Lin | 278d096 | 2016-03-07 23:39:08 +0800 | [diff] [blame^] | 192 | err_pltfm_free: |
| 193 | sdhci_pltfm_free(pdev); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static int sdhci_arasan_remove(struct platform_device *pdev) |
| 198 | { |
Jisheng Zhang | 0c7fe32 | 2016-02-16 21:08:23 +0800 | [diff] [blame] | 199 | int ret; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 200 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 201 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 8921141 | 2016-02-16 21:08:24 +0800 | [diff] [blame] | 202 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
| 203 | struct clk *clk_ahb = sdhci_arasan->clk_ahb; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 204 | |
Jisheng Zhang | 0c7fe32 | 2016-02-16 21:08:23 +0800 | [diff] [blame] | 205 | ret = sdhci_pltfm_unregister(pdev); |
| 206 | |
Jisheng Zhang | 8921141 | 2016-02-16 21:08:24 +0800 | [diff] [blame] | 207 | clk_disable_unprepare(clk_ahb); |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 208 | |
Jisheng Zhang | 0c7fe32 | 2016-02-16 21:08:23 +0800 | [diff] [blame] | 209 | return ret; |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static const struct of_device_id sdhci_arasan_of_match[] = { |
| 213 | { .compatible = "arasan,sdhci-8.9a" }, |
Shawn Lin | da795ec | 2015-08-11 15:57:05 +0800 | [diff] [blame] | 214 | { .compatible = "arasan,sdhci-5.1" }, |
Suman Tripathi | 308f3f8 | 2015-05-04 19:09:51 +0530 | [diff] [blame] | 215 | { .compatible = "arasan,sdhci-4.9a" }, |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 216 | { } |
| 217 | }; |
| 218 | MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match); |
| 219 | |
| 220 | static struct platform_driver sdhci_arasan_driver = { |
| 221 | .driver = { |
| 222 | .name = "sdhci-arasan", |
Soren Brinkmann | e3ec3a3 | 2013-12-02 10:02:36 -0800 | [diff] [blame] | 223 | .of_match_table = sdhci_arasan_of_match, |
| 224 | .pm = &sdhci_arasan_dev_pm_ops, |
| 225 | }, |
| 226 | .probe = sdhci_arasan_probe, |
| 227 | .remove = sdhci_arasan_remove, |
| 228 | }; |
| 229 | |
| 230 | module_platform_driver(sdhci_arasan_driver); |
| 231 | |
| 232 | MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller"); |
| 233 | MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>"); |
| 234 | MODULE_LICENSE("GPL"); |