Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 1 | /* |
Richard Zhu | 8b789d8 | 2013-10-15 10:44:54 +0800 | [diff] [blame] | 2 | * copyright (c) 2013 Freescale Semiconductor, Inc. |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 3 | * Freescale IMX AHCI SATA platform driver |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 4 | * |
| 5 | * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/ahci_platform.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/mfd/syscon.h> |
| 27 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
Richard Zhu | 8b789d8 | 2013-10-15 10:44:54 +0800 | [diff] [blame] | 28 | #include <linux/libata.h> |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 29 | #include "ahci.h" |
| 30 | |
| 31 | enum { |
Shawn Guo | 24a9ad5 | 2014-05-04 21:48:05 +0800 | [diff] [blame^] | 32 | /* Timer 1-ms Register */ |
| 33 | IMX_TIMER1MS = 0x00e0, |
| 34 | /* Port0 PHY Control Register */ |
| 35 | IMX_P0PHYCR = 0x0178, |
| 36 | IMX_P0PHYCR_TEST_PDDQ = 1 << 20, |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 39 | enum ahci_imx_type { |
| 40 | AHCI_IMX53, |
| 41 | AHCI_IMX6Q, |
| 42 | }; |
| 43 | |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 44 | struct imx_ahci_priv { |
| 45 | struct platform_device *ahci_pdev; |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 46 | enum ahci_imx_type type; |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 47 | struct clk *ahb_clk; |
| 48 | struct regmap *gpr; |
Richard Zhu | 8b789d8 | 2013-10-15 10:44:54 +0800 | [diff] [blame] | 49 | bool no_device; |
| 50 | bool first_time; |
| 51 | }; |
| 52 | |
| 53 | static int ahci_imx_hotplug; |
| 54 | module_param_named(hotplug, ahci_imx_hotplug, int, 0644); |
| 55 | MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)"); |
| 56 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 57 | static void ahci_imx_host_stop(struct ata_host *host); |
| 58 | |
| 59 | static int imx_sata_enable(struct ahci_host_priv *hpriv) |
Marek Vasut | 8403e2e | 2013-11-25 09:47:01 +0100 | [diff] [blame] | 60 | { |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 61 | struct imx_ahci_priv *imxpriv = hpriv->plat_data; |
Marek Vasut | 8403e2e | 2013-11-25 09:47:01 +0100 | [diff] [blame] | 62 | int ret; |
| 63 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 64 | if (imxpriv->no_device) |
| 65 | return 0; |
| 66 | |
| 67 | if (hpriv->target_pwr) { |
| 68 | ret = regulator_enable(hpriv->target_pwr); |
| 69 | if (ret) |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 70 | return ret; |
Marek Vasut | 8403e2e | 2013-11-25 09:47:01 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 73 | ret = ahci_platform_enable_clks(hpriv); |
Marek Vasut | 8403e2e | 2013-11-25 09:47:01 +0100 | [diff] [blame] | 74 | if (ret < 0) |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 75 | goto disable_regulator; |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 76 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 77 | if (imxpriv->type == AHCI_IMX6Q) { |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 78 | /* |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 79 | * set PHY Paremeters, two steps to configure the GPR13, |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 80 | * one write for rest of parameters, mask of first write |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 81 | * is 0x07ffffff, and the other one write for setting |
| 82 | * the mpll_clk_en. |
Marek Vasut | 4a23d17 | 2013-11-25 09:47:02 +0100 | [diff] [blame] | 83 | */ |
| 84 | regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13, |
| 85 | IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK | |
| 86 | IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK | |
| 87 | IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK | |
| 88 | IMX6Q_GPR13_SATA_SPD_MODE_MASK | |
| 89 | IMX6Q_GPR13_SATA_MPLL_SS_EN | |
| 90 | IMX6Q_GPR13_SATA_TX_ATTEN_MASK | |
| 91 | IMX6Q_GPR13_SATA_TX_BOOST_MASK | |
| 92 | IMX6Q_GPR13_SATA_TX_LVL_MASK | |
| 93 | IMX6Q_GPR13_SATA_MPLL_CLK_EN | |
| 94 | IMX6Q_GPR13_SATA_TX_EDGE_RATE, |
| 95 | IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB | |
| 96 | IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M | |
| 97 | IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F | |
| 98 | IMX6Q_GPR13_SATA_SPD_MODE_3P0G | |
| 99 | IMX6Q_GPR13_SATA_MPLL_SS_EN | |
| 100 | IMX6Q_GPR13_SATA_TX_ATTEN_9_16 | |
| 101 | IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB | |
| 102 | IMX6Q_GPR13_SATA_TX_LVL_1_025_V); |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 103 | regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13, |
| 104 | IMX6Q_GPR13_SATA_MPLL_CLK_EN, |
| 105 | IMX6Q_GPR13_SATA_MPLL_CLK_EN); |
Marek Vasut | 8403e2e | 2013-11-25 09:47:01 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 108 | usleep_range(1000, 2000); |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 109 | |
| 110 | return 0; |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 111 | |
| 112 | disable_regulator: |
| 113 | if (hpriv->target_pwr) |
| 114 | regulator_disable(hpriv->target_pwr); |
| 115 | |
| 116 | return ret; |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 119 | static void imx_sata_disable(struct ahci_host_priv *hpriv) |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 120 | { |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 121 | struct imx_ahci_priv *imxpriv = hpriv->plat_data; |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 122 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 123 | if (imxpriv->no_device) |
| 124 | return; |
| 125 | |
| 126 | if (imxpriv->type == AHCI_IMX6Q) { |
| 127 | regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13, |
| 128 | IMX6Q_GPR13_SATA_MPLL_CLK_EN, |
| 129 | !IMX6Q_GPR13_SATA_MPLL_CLK_EN); |
| 130 | } |
| 131 | |
| 132 | ahci_platform_disable_clks(hpriv); |
| 133 | |
| 134 | if (hpriv->target_pwr) |
| 135 | regulator_disable(hpriv->target_pwr); |
| 136 | } |
| 137 | |
| 138 | static void ahci_imx_error_handler(struct ata_port *ap) |
| 139 | { |
| 140 | u32 reg_val; |
| 141 | struct ata_device *dev; |
| 142 | struct ata_host *host = dev_get_drvdata(ap->dev); |
| 143 | struct ahci_host_priv *hpriv = host->private_data; |
| 144 | void __iomem *mmio = hpriv->mmio; |
| 145 | struct imx_ahci_priv *imxpriv = hpriv->plat_data; |
| 146 | |
| 147 | ahci_error_handler(ap); |
| 148 | |
| 149 | if (!(imxpriv->first_time) || ahci_imx_hotplug) |
| 150 | return; |
| 151 | |
| 152 | imxpriv->first_time = false; |
| 153 | |
| 154 | ata_for_each_dev(dev, &ap->link, ENABLED) |
| 155 | return; |
| 156 | /* |
| 157 | * Disable link to save power. An imx ahci port can't be recovered |
| 158 | * without full reset once the pddq mode is enabled making it |
| 159 | * impossible to use as part of libata LPM. |
| 160 | */ |
Shawn Guo | 24a9ad5 | 2014-05-04 21:48:05 +0800 | [diff] [blame^] | 161 | reg_val = readl(mmio + IMX_P0PHYCR); |
| 162 | writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR); |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 163 | imx_sata_disable(hpriv); |
| 164 | imxpriv->no_device = true; |
| 165 | } |
| 166 | |
| 167 | static int ahci_imx_softreset(struct ata_link *link, unsigned int *class, |
| 168 | unsigned long deadline) |
| 169 | { |
| 170 | struct ata_port *ap = link->ap; |
| 171 | struct ata_host *host = dev_get_drvdata(ap->dev); |
| 172 | struct ahci_host_priv *hpriv = host->private_data; |
| 173 | struct imx_ahci_priv *imxpriv = hpriv->plat_data; |
| 174 | int ret = -EIO; |
| 175 | |
| 176 | if (imxpriv->type == AHCI_IMX53) |
| 177 | ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline); |
| 178 | else if (imxpriv->type == AHCI_IMX6Q) |
| 179 | ret = ahci_ops.softreset(link, class, deadline); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static struct ata_port_operations ahci_imx_ops = { |
| 185 | .inherits = &ahci_ops, |
| 186 | .host_stop = ahci_imx_host_stop, |
| 187 | .error_handler = ahci_imx_error_handler, |
| 188 | .softreset = ahci_imx_softreset, |
| 189 | }; |
| 190 | |
| 191 | static const struct ata_port_info ahci_imx_port_info = { |
| 192 | .flags = AHCI_FLAG_COMMON, |
| 193 | .pio_mask = ATA_PIO4, |
| 194 | .udma_mask = ATA_UDMA6, |
| 195 | .port_ops = &ahci_imx_ops, |
| 196 | }; |
| 197 | |
| 198 | static const struct of_device_id imx_ahci_of_match[] = { |
| 199 | { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 }, |
| 200 | { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q }, |
| 201 | {}, |
| 202 | }; |
| 203 | MODULE_DEVICE_TABLE(of, imx_ahci_of_match); |
| 204 | |
| 205 | static int imx_ahci_probe(struct platform_device *pdev) |
| 206 | { |
| 207 | struct device *dev = &pdev->dev; |
| 208 | const struct of_device_id *of_id; |
| 209 | struct ahci_host_priv *hpriv; |
| 210 | struct imx_ahci_priv *imxpriv; |
| 211 | unsigned int reg_val; |
| 212 | int ret; |
| 213 | |
| 214 | of_id = of_match_device(imx_ahci_of_match, dev); |
| 215 | if (!of_id) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL); |
| 219 | if (!imxpriv) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | imxpriv->no_device = false; |
| 223 | imxpriv->first_time = true; |
| 224 | imxpriv->type = (enum ahci_imx_type)of_id->data; |
| 225 | imxpriv->ahb_clk = devm_clk_get(dev, "ahb"); |
| 226 | if (IS_ERR(imxpriv->ahb_clk)) { |
| 227 | dev_err(dev, "can't get ahb clock.\n"); |
| 228 | return PTR_ERR(imxpriv->ahb_clk); |
| 229 | } |
| 230 | |
| 231 | if (imxpriv->type == AHCI_IMX6Q) { |
| 232 | imxpriv->gpr = syscon_regmap_lookup_by_compatible( |
| 233 | "fsl,imx6q-iomuxc-gpr"); |
| 234 | if (IS_ERR(imxpriv->gpr)) { |
| 235 | dev_err(dev, |
| 236 | "failed to find fsl,imx6q-iomux-gpr regmap\n"); |
| 237 | return PTR_ERR(imxpriv->gpr); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | hpriv = ahci_platform_get_resources(pdev); |
| 242 | if (IS_ERR(hpriv)) |
| 243 | return PTR_ERR(hpriv); |
| 244 | |
| 245 | hpriv->plat_data = imxpriv; |
| 246 | |
| 247 | ret = imx_sata_enable(hpriv); |
| 248 | if (ret) |
| 249 | return ret; |
| 250 | |
| 251 | /* |
| 252 | * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL, |
Shawn Guo | 24a9ad5 | 2014-05-04 21:48:05 +0800 | [diff] [blame^] | 253 | * and IP vendor specific register IMX_TIMER1MS. |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 254 | * Configure CAP_SSS (support stagered spin up). |
| 255 | * Implement the port0. |
| 256 | * Get the ahb clock rate, and configure the TIMER1MS register. |
| 257 | */ |
| 258 | reg_val = readl(hpriv->mmio + HOST_CAP); |
| 259 | if (!(reg_val & HOST_CAP_SSS)) { |
| 260 | reg_val |= HOST_CAP_SSS; |
| 261 | writel(reg_val, hpriv->mmio + HOST_CAP); |
| 262 | } |
| 263 | reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL); |
| 264 | if (!(reg_val & 0x1)) { |
| 265 | reg_val |= 0x1; |
| 266 | writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL); |
| 267 | } |
| 268 | |
| 269 | reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000; |
Shawn Guo | 24a9ad5 | 2014-05-04 21:48:05 +0800 | [diff] [blame^] | 270 | writel(reg_val, hpriv->mmio + IMX_TIMER1MS); |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 271 | |
| 272 | ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info, 0, 0); |
| 273 | if (ret) |
| 274 | imx_sata_disable(hpriv); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static void ahci_imx_host_stop(struct ata_host *host) |
| 280 | { |
| 281 | struct ahci_host_priv *hpriv = host->private_data; |
| 282 | |
| 283 | imx_sata_disable(hpriv); |
| 284 | } |
| 285 | |
Hans de Goede | 46ce6b7 | 2014-03-05 20:17:49 +0100 | [diff] [blame] | 286 | #ifdef CONFIG_PM_SLEEP |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 287 | static int imx_ahci_suspend(struct device *dev) |
| 288 | { |
| 289 | struct ata_host *host = dev_get_drvdata(dev); |
| 290 | struct ahci_host_priv *hpriv = host->private_data; |
| 291 | int ret; |
| 292 | |
| 293 | ret = ahci_platform_suspend_host(dev); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | imx_sata_disable(hpriv); |
| 298 | |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 302 | static int imx_ahci_resume(struct device *dev) |
| 303 | { |
| 304 | struct ata_host *host = dev_get_drvdata(dev); |
| 305 | struct ahci_host_priv *hpriv = host->private_data; |
| 306 | int ret; |
| 307 | |
| 308 | ret = imx_sata_enable(hpriv); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | return ahci_platform_resume_host(dev); |
| 313 | } |
Hans de Goede | 46ce6b7 | 2014-03-05 20:17:49 +0100 | [diff] [blame] | 314 | #endif |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 315 | |
| 316 | static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume); |
| 317 | |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 318 | static struct platform_driver imx_ahci_driver = { |
| 319 | .probe = imx_ahci_probe, |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 320 | .remove = ata_platform_remove_one, |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 321 | .driver = { |
| 322 | .name = "ahci-imx", |
| 323 | .owner = THIS_MODULE, |
| 324 | .of_match_table = imx_ahci_of_match, |
Hans de Goede | 90870d7 | 2014-02-22 16:53:37 +0100 | [diff] [blame] | 325 | .pm = &ahci_imx_pm_ops, |
Richard Zhu | 9e54eae | 2013-07-24 14:15:29 +0800 | [diff] [blame] | 326 | }, |
| 327 | }; |
| 328 | module_platform_driver(imx_ahci_driver); |
| 329 | |
| 330 | MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver"); |
| 331 | MODULE_AUTHOR("Richard Zhu <Hong-Xing.Zhu@freescale.com>"); |
| 332 | MODULE_LICENSE("GPL"); |
| 333 | MODULE_ALIAS("ahci:imx"); |