Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom SATA3 AHCI Controller Driver |
| 3 | * |
| 4 | * Copyright © 2009-2015 Broadcom Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/ahci_platform.h> |
| 18 | #include <linux/compiler.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/libata.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/string.h> |
| 29 | |
| 30 | #include "ahci.h" |
| 31 | |
| 32 | #define DRV_NAME "brcm-ahci" |
| 33 | |
| 34 | #define SATA_TOP_CTRL_VERSION 0x0 |
| 35 | #define SATA_TOP_CTRL_BUS_CTRL 0x4 |
| 36 | #define MMIO_ENDIAN_SHIFT 0 /* CPU->AHCI */ |
| 37 | #define DMADESC_ENDIAN_SHIFT 2 /* AHCI->DDR */ |
| 38 | #define DMADATA_ENDIAN_SHIFT 4 /* AHCI->DDR */ |
| 39 | #define PIODATA_ENDIAN_SHIFT 6 |
| 40 | #define ENDIAN_SWAP_NONE 0 |
| 41 | #define ENDIAN_SWAP_FULL 2 |
| 42 | #define OVERRIDE_HWINIT BIT(16) |
| 43 | #define SATA_TOP_CTRL_TP_CTRL 0x8 |
| 44 | #define SATA_TOP_CTRL_PHY_CTRL 0xc |
| 45 | #define SATA_TOP_CTRL_PHY_CTRL_1 0x0 |
| 46 | #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14) |
| 47 | #define SATA_TOP_CTRL_PHY_CTRL_2 0x4 |
| 48 | #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0) |
| 49 | #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1) |
| 50 | #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2) |
| 51 | #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3) |
| 52 | #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14) |
| 53 | #define SATA_TOP_CTRL_PHY_OFFS 0x8 |
| 54 | #define SATA_TOP_MAX_PHYS 2 |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 55 | |
Danesh Petigara | 6863caa | 2016-01-07 16:03:30 -0800 | [diff] [blame^] | 56 | #define SATA_FIRST_PORT_CTRL 0x700 |
| 57 | #define SATA_NEXT_PORT_CTRL_OFFSET 0x80 |
| 58 | #define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18) |
| 59 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 60 | /* On big-endian MIPS, buses are reversed to big endian, so switch them back */ |
| 61 | #if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN) |
| 62 | #define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */ |
| 63 | #define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */ |
| 64 | #else |
| 65 | #define DATA_ENDIAN 0 |
| 66 | #define MMIO_ENDIAN 0 |
| 67 | #endif |
| 68 | |
| 69 | #define BUS_CTRL_ENDIAN_CONF \ |
| 70 | ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \ |
| 71 | (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \ |
| 72 | (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT)) |
| 73 | |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 74 | enum brcm_ahci_quirks { |
| 75 | BRCM_AHCI_QUIRK_NO_NCQ = BIT(0), |
Jaedon Shin | b46f79b | 2015-11-26 11:56:31 +0900 | [diff] [blame] | 76 | BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(1), |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 77 | }; |
| 78 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 79 | struct brcm_ahci_priv { |
| 80 | struct device *dev; |
| 81 | void __iomem *top_ctrl; |
| 82 | u32 port_mask; |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 83 | u32 quirks; |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static const struct ata_port_info ahci_brcm_port_info = { |
| 87 | .flags = AHCI_FLAG_COMMON, |
| 88 | .pio_mask = ATA_PIO4, |
| 89 | .udma_mask = ATA_UDMA6, |
| 90 | .port_ops = &ahci_platform_ops, |
| 91 | }; |
| 92 | |
| 93 | static inline u32 brcm_sata_readreg(void __iomem *addr) |
| 94 | { |
| 95 | /* |
| 96 | * MIPS endianness is configured by boot strap, which also reverses all |
| 97 | * bus endianness (i.e., big-endian CPU + big endian bus ==> native |
| 98 | * endian I/O). |
| 99 | * |
| 100 | * Other architectures (e.g., ARM) either do not support big endian, or |
| 101 | * else leave I/O in little endian mode. |
| 102 | */ |
Axel Lin | f9114d3 | 2015-08-06 12:28:18 +0800 | [diff] [blame] | 103 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 104 | return __raw_readl(addr); |
| 105 | else |
| 106 | return readl_relaxed(addr); |
| 107 | } |
| 108 | |
| 109 | static inline void brcm_sata_writereg(u32 val, void __iomem *addr) |
| 110 | { |
| 111 | /* See brcm_sata_readreg() comments */ |
Axel Lin | f9114d3 | 2015-08-06 12:28:18 +0800 | [diff] [blame] | 112 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 113 | __raw_writel(val, addr); |
| 114 | else |
| 115 | writel_relaxed(val, addr); |
| 116 | } |
| 117 | |
Danesh Petigara | 6863caa | 2016-01-07 16:03:30 -0800 | [diff] [blame^] | 118 | static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv) |
| 119 | { |
| 120 | struct brcm_ahci_priv *priv = hpriv->plat_data; |
| 121 | u32 bus_ctrl, port_ctrl, host_caps; |
| 122 | int i; |
| 123 | |
| 124 | /* Enable support for ALPM */ |
| 125 | bus_ctrl = brcm_sata_readreg(priv->top_ctrl + |
| 126 | SATA_TOP_CTRL_BUS_CTRL); |
| 127 | brcm_sata_writereg(bus_ctrl | OVERRIDE_HWINIT, |
| 128 | priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL); |
| 129 | host_caps = readl(hpriv->mmio + HOST_CAP); |
| 130 | writel(host_caps | HOST_CAP_ALPM, hpriv->mmio); |
| 131 | brcm_sata_writereg(bus_ctrl, priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL); |
| 132 | |
| 133 | /* |
| 134 | * Adjust timeout to allow PLL sufficient time to lock while waking |
| 135 | * up from slumber mode. |
| 136 | */ |
| 137 | for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL; |
| 138 | i < SATA_TOP_MAX_PHYS; |
| 139 | i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) { |
| 140 | if (priv->port_mask & BIT(i)) |
| 141 | writel(0xff1003fc, |
| 142 | hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl)); |
| 143 | } |
| 144 | } |
| 145 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 146 | static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port) |
| 147 | { |
| 148 | void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL + |
| 149 | (port * SATA_TOP_CTRL_PHY_OFFS); |
| 150 | void __iomem *p; |
| 151 | u32 reg; |
| 152 | |
Jaedon Shin | b46f79b | 2015-11-26 11:56:31 +0900 | [diff] [blame] | 153 | if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE) |
| 154 | return; |
| 155 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 156 | /* clear PHY_DEFAULT_POWER_STATE */ |
| 157 | p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1; |
| 158 | reg = brcm_sata_readreg(p); |
| 159 | reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE; |
| 160 | brcm_sata_writereg(reg, p); |
| 161 | |
| 162 | /* reset the PHY digital logic */ |
| 163 | p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2; |
| 164 | reg = brcm_sata_readreg(p); |
| 165 | reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB | |
| 166 | SATA_TOP_CTRL_2_SW_RST_RX); |
| 167 | reg |= SATA_TOP_CTRL_2_SW_RST_TX; |
| 168 | brcm_sata_writereg(reg, p); |
| 169 | reg = brcm_sata_readreg(p); |
| 170 | reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET; |
| 171 | brcm_sata_writereg(reg, p); |
| 172 | reg = brcm_sata_readreg(p); |
| 173 | reg &= ~SATA_TOP_CTRL_2_PHY_GLOBAL_RESET; |
| 174 | brcm_sata_writereg(reg, p); |
| 175 | (void)brcm_sata_readreg(p); |
| 176 | } |
| 177 | |
| 178 | static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port) |
| 179 | { |
| 180 | void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL + |
| 181 | (port * SATA_TOP_CTRL_PHY_OFFS); |
| 182 | void __iomem *p; |
| 183 | u32 reg; |
| 184 | |
Jaedon Shin | b46f79b | 2015-11-26 11:56:31 +0900 | [diff] [blame] | 185 | if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE) |
| 186 | return; |
| 187 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 188 | /* power-off the PHY digital logic */ |
| 189 | p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2; |
| 190 | reg = brcm_sata_readreg(p); |
| 191 | reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB | |
| 192 | SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX | |
| 193 | SATA_TOP_CTRL_2_PHY_GLOBAL_RESET); |
| 194 | brcm_sata_writereg(reg, p); |
| 195 | |
| 196 | /* set PHY_DEFAULT_POWER_STATE */ |
| 197 | p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1; |
| 198 | reg = brcm_sata_readreg(p); |
| 199 | reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE; |
| 200 | brcm_sata_writereg(reg, p); |
| 201 | } |
| 202 | |
| 203 | static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv) |
| 204 | { |
| 205 | int i; |
| 206 | |
| 207 | for (i = 0; i < SATA_TOP_MAX_PHYS; i++) |
| 208 | if (priv->port_mask & BIT(i)) |
| 209 | brcm_sata_phy_enable(priv, i); |
| 210 | } |
| 211 | |
| 212 | static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv) |
| 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = 0; i < SATA_TOP_MAX_PHYS; i++) |
| 217 | if (priv->port_mask & BIT(i)) |
| 218 | brcm_sata_phy_disable(priv, i); |
| 219 | } |
| 220 | |
| 221 | static u32 brcm_ahci_get_portmask(struct platform_device *pdev, |
| 222 | struct brcm_ahci_priv *priv) |
| 223 | { |
| 224 | void __iomem *ahci; |
| 225 | struct resource *res; |
| 226 | u32 impl; |
| 227 | |
| 228 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ahci"); |
| 229 | ahci = devm_ioremap_resource(&pdev->dev, res); |
| 230 | if (IS_ERR(ahci)) |
| 231 | return 0; |
| 232 | |
| 233 | impl = readl(ahci + HOST_PORTS_IMPL); |
| 234 | |
| 235 | if (fls(impl) > SATA_TOP_MAX_PHYS) |
| 236 | dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n", |
| 237 | impl); |
| 238 | else if (!impl) |
| 239 | dev_info(priv->dev, "no ports found\n"); |
| 240 | |
| 241 | devm_iounmap(&pdev->dev, ahci); |
| 242 | devm_release_mem_region(&pdev->dev, res->start, resource_size(res)); |
| 243 | |
| 244 | return impl; |
| 245 | } |
| 246 | |
| 247 | static void brcm_sata_init(struct brcm_ahci_priv *priv) |
| 248 | { |
| 249 | /* Configure endianness */ |
| 250 | brcm_sata_writereg(BUS_CTRL_ENDIAN_CONF, |
| 251 | priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL); |
| 252 | } |
| 253 | |
Florian Fainelli | 8b34fe5 | 2015-07-14 13:03:33 -0700 | [diff] [blame] | 254 | #ifdef CONFIG_PM_SLEEP |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 255 | static int brcm_ahci_suspend(struct device *dev) |
| 256 | { |
| 257 | struct ata_host *host = dev_get_drvdata(dev); |
| 258 | struct ahci_host_priv *hpriv = host->private_data; |
| 259 | struct brcm_ahci_priv *priv = hpriv->plat_data; |
| 260 | int ret; |
| 261 | |
| 262 | ret = ahci_platform_suspend(dev); |
| 263 | brcm_sata_phys_disable(priv); |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int brcm_ahci_resume(struct device *dev) |
| 268 | { |
| 269 | struct ata_host *host = dev_get_drvdata(dev); |
| 270 | struct ahci_host_priv *hpriv = host->private_data; |
| 271 | struct brcm_ahci_priv *priv = hpriv->plat_data; |
| 272 | |
| 273 | brcm_sata_init(priv); |
| 274 | brcm_sata_phys_enable(priv); |
Danesh Petigara | 6863caa | 2016-01-07 16:03:30 -0800 | [diff] [blame^] | 275 | brcm_sata_alpm_init(hpriv); |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 276 | return ahci_platform_resume(dev); |
| 277 | } |
Florian Fainelli | 8b34fe5 | 2015-07-14 13:03:33 -0700 | [diff] [blame] | 278 | #endif |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 279 | |
| 280 | static struct scsi_host_template ahci_platform_sht = { |
| 281 | AHCI_SHT(DRV_NAME), |
| 282 | }; |
| 283 | |
| 284 | static int brcm_ahci_probe(struct platform_device *pdev) |
| 285 | { |
| 286 | struct device *dev = &pdev->dev; |
| 287 | struct brcm_ahci_priv *priv; |
| 288 | struct ahci_host_priv *hpriv; |
| 289 | struct resource *res; |
| 290 | int ret; |
| 291 | |
| 292 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 293 | if (!priv) |
| 294 | return -ENOMEM; |
| 295 | priv->dev = dev; |
| 296 | |
| 297 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "top-ctrl"); |
| 298 | priv->top_ctrl = devm_ioremap_resource(dev, res); |
| 299 | if (IS_ERR(priv->top_ctrl)) |
| 300 | return PTR_ERR(priv->top_ctrl); |
| 301 | |
Jaedon Shin | b46f79b | 2015-11-26 11:56:31 +0900 | [diff] [blame] | 302 | if (of_device_is_compatible(dev->of_node, "brcm,bcm7425-ahci")) { |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 303 | priv->quirks |= BRCM_AHCI_QUIRK_NO_NCQ; |
Jaedon Shin | b46f79b | 2015-11-26 11:56:31 +0900 | [diff] [blame] | 304 | priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE; |
| 305 | } |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 306 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 307 | brcm_sata_init(priv); |
| 308 | |
| 309 | priv->port_mask = brcm_ahci_get_portmask(pdev, priv); |
| 310 | if (!priv->port_mask) |
| 311 | return -ENODEV; |
| 312 | |
| 313 | brcm_sata_phys_enable(priv); |
| 314 | |
| 315 | hpriv = ahci_platform_get_resources(pdev); |
| 316 | if (IS_ERR(hpriv)) |
| 317 | return PTR_ERR(hpriv); |
| 318 | hpriv->plat_data = priv; |
| 319 | |
Danesh Petigara | 6863caa | 2016-01-07 16:03:30 -0800 | [diff] [blame^] | 320 | brcm_sata_alpm_init(hpriv); |
| 321 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 322 | ret = ahci_platform_enable_resources(hpriv); |
| 323 | if (ret) |
| 324 | return ret; |
| 325 | |
Jaedon Shin | 7de3244 | 2015-11-26 11:56:30 +0900 | [diff] [blame] | 326 | if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ) |
| 327 | hpriv->flags |= AHCI_HFLAG_NO_NCQ; |
| 328 | |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 329 | ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info, |
| 330 | &ahci_platform_sht); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
| 334 | dev_info(dev, "Broadcom AHCI SATA3 registered\n"); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int brcm_ahci_remove(struct platform_device *pdev) |
| 340 | { |
| 341 | struct ata_host *host = dev_get_drvdata(&pdev->dev); |
| 342 | struct ahci_host_priv *hpriv = host->private_data; |
| 343 | struct brcm_ahci_priv *priv = hpriv->plat_data; |
| 344 | int ret; |
| 345 | |
| 346 | ret = ata_platform_remove_one(pdev); |
| 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | brcm_sata_phys_disable(priv); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static const struct of_device_id ahci_of_match[] = { |
Jaedon Shin | 1980eb9 | 2015-11-26 11:56:29 +0900 | [diff] [blame] | 356 | {.compatible = "brcm,bcm7425-ahci"}, |
Brian Norris | 766a2d9 | 2015-05-12 16:28:21 -0700 | [diff] [blame] | 357 | {.compatible = "brcm,bcm7445-ahci"}, |
| 358 | {}, |
| 359 | }; |
| 360 | MODULE_DEVICE_TABLE(of, ahci_of_match); |
| 361 | |
| 362 | static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume); |
| 363 | |
| 364 | static struct platform_driver brcm_ahci_driver = { |
| 365 | .probe = brcm_ahci_probe, |
| 366 | .remove = brcm_ahci_remove, |
| 367 | .driver = { |
| 368 | .name = DRV_NAME, |
| 369 | .of_match_table = ahci_of_match, |
| 370 | .pm = &ahci_brcm_pm_ops, |
| 371 | }, |
| 372 | }; |
| 373 | module_platform_driver(brcm_ahci_driver); |
| 374 | |
| 375 | MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver"); |
| 376 | MODULE_AUTHOR("Brian Norris"); |
| 377 | MODULE_LICENSE("GPL"); |
| 378 | MODULE_ALIAS("platform:sata-brcmstb"); |