Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 STMicroelectronics Limited |
| 3 | * |
| 4 | * Authors: Francesco Virlinzi <francesco.virlinzi@st.com> |
| 5 | * Alexandre Torgue <alexandre.torgue@st.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/ahci_platform.h> |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 19 | #include <linux/libata.h> |
| 20 | #include <linux/reset.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | |
| 24 | #include "ahci.h" |
| 25 | |
Akinobu Mita | 018d5ef | 2015-01-29 08:30:29 +0900 | [diff] [blame] | 26 | #define DRV_NAME "st_ahci" |
| 27 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 28 | #define ST_AHCI_OOBR 0xbc |
| 29 | #define ST_AHCI_OOBR_WE BIT(31) |
| 30 | #define ST_AHCI_OOBR_CWMIN_SHIFT 24 |
| 31 | #define ST_AHCI_OOBR_CWMAX_SHIFT 16 |
| 32 | #define ST_AHCI_OOBR_CIMIN_SHIFT 8 |
| 33 | #define ST_AHCI_OOBR_CIMAX_SHIFT 0 |
| 34 | |
| 35 | struct st_ahci_drv_data { |
| 36 | struct platform_device *ahci; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 37 | struct reset_control *pwr; |
| 38 | struct reset_control *sw_rst; |
| 39 | struct reset_control *pwr_rst; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static void st_ahci_configure_oob(void __iomem *mmio) |
| 43 | { |
| 44 | unsigned long old_val, new_val; |
| 45 | |
| 46 | new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) | |
| 47 | (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) | |
| 48 | (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) | |
| 49 | (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT); |
| 50 | |
| 51 | old_val = readl(mmio + ST_AHCI_OOBR); |
| 52 | writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR); |
| 53 | writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR); |
| 54 | writel(new_val, mmio + ST_AHCI_OOBR); |
| 55 | } |
| 56 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 57 | static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv, |
| 58 | struct device *dev) |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 59 | { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 60 | struct st_ahci_drv_data *drv_data = hpriv->plat_data; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 61 | int err; |
| 62 | |
| 63 | if (drv_data->pwr) { |
| 64 | err = reset_control_deassert(drv_data->pwr); |
| 65 | if (err) { |
| 66 | dev_err(dev, "unable to bring out of pwrdwn\n"); |
| 67 | return err; |
| 68 | } |
| 69 | } |
| 70 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 71 | if (drv_data->sw_rst) { |
| 72 | err = reset_control_deassert(drv_data->sw_rst); |
| 73 | if (err) { |
| 74 | dev_err(dev, "unable to bring out of sw-rst\n"); |
| 75 | return err; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (drv_data->pwr_rst) { |
| 80 | err = reset_control_deassert(drv_data->pwr_rst); |
| 81 | if (err) { |
| 82 | dev_err(dev, "unable to bring out of pwr-rst\n"); |
| 83 | return err; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Bartlomiej Zolnierkiewicz | b032378 | 2014-03-14 19:21:59 +0100 | [diff] [blame] | 90 | static void st_ahci_host_stop(struct ata_host *host) |
Lee Jones | a823708 | 2014-03-12 12:39:40 +0000 | [diff] [blame] | 91 | { |
Bartlomiej Zolnierkiewicz | b032378 | 2014-03-14 19:21:59 +0100 | [diff] [blame] | 92 | struct ahci_host_priv *hpriv = host->private_data; |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 93 | struct st_ahci_drv_data *drv_data = hpriv->plat_data; |
Bartlomiej Zolnierkiewicz | b032378 | 2014-03-14 19:21:59 +0100 | [diff] [blame] | 94 | struct device *dev = host->dev; |
Lee Jones | a823708 | 2014-03-12 12:39:40 +0000 | [diff] [blame] | 95 | int err; |
| 96 | |
| 97 | if (drv_data->pwr) { |
| 98 | err = reset_control_assert(drv_data->pwr); |
| 99 | if (err) |
Bartlomiej Zolnierkiewicz | 33081b3 | 2014-03-14 19:20:58 +0100 | [diff] [blame] | 100 | dev_err(dev, "unable to pwrdwn\n"); |
Lee Jones | a823708 | 2014-03-12 12:39:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | ahci_platform_disable_resources(hpriv); |
Lee Jones | a823708 | 2014-03-12 12:39:40 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 106 | static int st_ahci_probe_resets(struct ahci_host_priv *hpriv, |
| 107 | struct device *dev) |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 108 | { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 109 | struct st_ahci_drv_data *drv_data = hpriv->plat_data; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 110 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 111 | drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 112 | if (IS_ERR(drv_data->pwr)) { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 113 | dev_info(dev, "power reset control not defined\n"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 114 | drv_data->pwr = NULL; |
| 115 | } |
| 116 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 117 | drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 118 | if (IS_ERR(drv_data->sw_rst)) { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 119 | dev_info(dev, "soft reset control not defined\n"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 120 | drv_data->sw_rst = NULL; |
| 121 | } |
| 122 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 123 | drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 124 | if (IS_ERR(drv_data->pwr_rst)) { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 125 | dev_dbg(dev, "power soft reset control not defined\n"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 126 | drv_data->pwr_rst = NULL; |
| 127 | } |
| 128 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 129 | return st_ahci_deassert_resets(hpriv, dev); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Bartlomiej Zolnierkiewicz | b032378 | 2014-03-14 19:21:59 +0100 | [diff] [blame] | 132 | static struct ata_port_operations st_ahci_port_ops = { |
| 133 | .inherits = &ahci_platform_ops, |
| 134 | .host_stop = st_ahci_host_stop, |
| 135 | }; |
| 136 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 137 | static const struct ata_port_info st_ahci_port_info = { |
| 138 | .flags = AHCI_FLAG_COMMON, |
| 139 | .pio_mask = ATA_PIO4, |
| 140 | .udma_mask = ATA_UDMA6, |
Bartlomiej Zolnierkiewicz | b032378 | 2014-03-14 19:21:59 +0100 | [diff] [blame] | 141 | .port_ops = &st_ahci_port_ops, |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Akinobu Mita | 018d5ef | 2015-01-29 08:30:29 +0900 | [diff] [blame] | 144 | static struct scsi_host_template ahci_platform_sht = { |
| 145 | AHCI_SHT(DRV_NAME), |
| 146 | }; |
| 147 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 148 | static int st_ahci_probe(struct platform_device *pdev) |
| 149 | { |
Patrice Chotard | e719069 | 2016-08-10 17:37:29 +0200 | [diff] [blame] | 150 | struct device *dev = &pdev->dev; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 151 | struct st_ahci_drv_data *drv_data; |
| 152 | struct ahci_host_priv *hpriv; |
| 153 | int err; |
| 154 | |
| 155 | drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL); |
| 156 | if (!drv_data) |
| 157 | return -ENOMEM; |
| 158 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 159 | hpriv = ahci_platform_get_resources(pdev); |
| 160 | if (IS_ERR(hpriv)) |
| 161 | return PTR_ERR(hpriv); |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 162 | hpriv->plat_data = drv_data; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 163 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 164 | err = st_ahci_probe_resets(hpriv, &pdev->dev); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 165 | if (err) |
| 166 | return err; |
| 167 | |
| 168 | err = ahci_platform_enable_resources(hpriv); |
| 169 | if (err) |
| 170 | return err; |
| 171 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 172 | st_ahci_configure_oob(hpriv->mmio); |
Peter Griffin | 9a1e75e | 2015-03-31 08:35:09 +0100 | [diff] [blame] | 173 | |
Patrice Chotard | e719069 | 2016-08-10 17:37:29 +0200 | [diff] [blame] | 174 | of_property_read_u32(dev->of_node, |
| 175 | "ports-implemented", &hpriv->force_port_map); |
| 176 | |
Akinobu Mita | 018d5ef | 2015-01-29 08:30:29 +0900 | [diff] [blame] | 177 | err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info, |
| 178 | &ahci_platform_sht); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 179 | if (err) { |
| 180 | ahci_platform_disable_resources(hpriv); |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 187 | #ifdef CONFIG_PM_SLEEP |
| 188 | static int st_ahci_suspend(struct device *dev) |
| 189 | { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 190 | struct ata_host *host = dev_get_drvdata(dev); |
| 191 | struct ahci_host_priv *hpriv = host->private_data; |
| 192 | struct st_ahci_drv_data *drv_data = hpriv->plat_data; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 193 | int err; |
| 194 | |
Bartlomiej Zolnierkiewicz | 33081b3 | 2014-03-14 19:20:58 +0100 | [diff] [blame] | 195 | err = ahci_platform_suspend_host(dev); |
| 196 | if (err) |
| 197 | return err; |
Lee Jones | 761a8c2 | 2014-03-12 12:39:42 +0000 | [diff] [blame] | 198 | |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 199 | if (drv_data->pwr) { |
| 200 | err = reset_control_assert(drv_data->pwr); |
| 201 | if (err) { |
| 202 | dev_err(dev, "unable to pwrdwn"); |
| 203 | return err; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | ahci_platform_disable_resources(hpriv); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int st_ahci_resume(struct device *dev) |
| 213 | { |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 214 | struct ata_host *host = dev_get_drvdata(dev); |
| 215 | struct ahci_host_priv *hpriv = host->private_data; |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 216 | int err; |
| 217 | |
| 218 | err = ahci_platform_enable_resources(hpriv); |
| 219 | if (err) |
| 220 | return err; |
| 221 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 222 | err = st_ahci_deassert_resets(hpriv, dev); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 223 | if (err) { |
| 224 | ahci_platform_disable_resources(hpriv); |
| 225 | return err; |
| 226 | } |
| 227 | |
Peter Griffin | e0e2674 | 2015-04-20 14:41:05 +0100 | [diff] [blame] | 228 | st_ahci_configure_oob(hpriv->mmio); |
Peter Griffin | 9a1e75e | 2015-03-31 08:35:09 +0100 | [diff] [blame] | 229 | |
Lee Jones | 761a8c2 | 2014-03-12 12:39:42 +0000 | [diff] [blame] | 230 | return ahci_platform_resume_host(dev); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 231 | } |
| 232 | #endif |
| 233 | |
| 234 | static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume); |
| 235 | |
Kiran Padwal | 09de99d | 2014-07-22 19:45:43 +0530 | [diff] [blame] | 236 | static const struct of_device_id st_ahci_match[] = { |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 237 | { .compatible = "st,ahci", }, |
| 238 | {}, |
| 239 | }; |
| 240 | MODULE_DEVICE_TABLE(of, st_ahci_match); |
| 241 | |
| 242 | static struct platform_driver st_ahci_driver = { |
| 243 | .driver = { |
Akinobu Mita | 018d5ef | 2015-01-29 08:30:29 +0900 | [diff] [blame] | 244 | .name = DRV_NAME, |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 245 | .pm = &st_ahci_pm_ops, |
| 246 | .of_match_table = of_match_ptr(st_ahci_match), |
| 247 | }, |
| 248 | .probe = st_ahci_probe, |
Lee Jones | a823708 | 2014-03-12 12:39:40 +0000 | [diff] [blame] | 249 | .remove = ata_platform_remove_one, |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 250 | }; |
| 251 | module_platform_driver(st_ahci_driver); |
| 252 | |
| 253 | MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>"); |
| 254 | MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>"); |
Lee Jones | 4a2e512 | 2014-03-12 12:39:38 +0000 | [diff] [blame] | 255 | MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver"); |
Lee Jones | 76884cb | 2014-02-26 14:47:21 +0000 | [diff] [blame] | 256 | MODULE_LICENSE("GPL v2"); |