1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Broadcom SATA3 AHCI Controller Driver 4 * 5 * Copyright © 2009-2015 Broadcom Corporation 6 */ 7 8 #include <linux/ahci_platform.h> 9 #include <linux/compiler.h> 10 #include <linux/device.h> 11 #include <linux/init.h> 12 #include <linux/interrupt.h> 13 #include <linux/io.h> 14 #include <linux/kernel.h> 15 #include <linux/libata.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/reset.h> 21 #include <linux/string.h> 22 23 #include "ahci.h" 24 25 #define DRV_NAME "brcm-ahci" 26 27 #define SATA_TOP_CTRL_VERSION 0x0 28 #define SATA_TOP_CTRL_BUS_CTRL 0x4 29 #define MMIO_ENDIAN_SHIFT 0 /* CPU->AHCI */ 30 #define DMADESC_ENDIAN_SHIFT 2 /* AHCI->DDR */ 31 #define DMADATA_ENDIAN_SHIFT 4 /* AHCI->DDR */ 32 #define PIODATA_ENDIAN_SHIFT 6 33 #define ENDIAN_SWAP_NONE 0 34 #define ENDIAN_SWAP_FULL 2 35 #define SATA_TOP_CTRL_TP_CTRL 0x8 36 #define SATA_TOP_CTRL_PHY_CTRL 0xc 37 #define SATA_TOP_CTRL_PHY_CTRL_1 0x0 38 #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14) 39 #define SATA_TOP_CTRL_PHY_CTRL_2 0x4 40 #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0) 41 #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1) 42 #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2) 43 #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3) 44 #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14) 45 #define SATA_TOP_CTRL_PHY_OFFS 0x8 46 #define SATA_TOP_MAX_PHYS 2 47 48 #define SATA_FIRST_PORT_CTRL 0x700 49 #define SATA_NEXT_PORT_CTRL_OFFSET 0x80 50 #define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18) 51 52 /* On big-endian MIPS, buses are reversed to big endian, so switch them back */ 53 #if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN) 54 #define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */ 55 #define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */ 56 #else 57 #define DATA_ENDIAN 0 58 #define MMIO_ENDIAN 0 59 #endif 60 61 #define BUS_CTRL_ENDIAN_CONF \ 62 ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \ 63 (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \ 64 (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT)) 65 66 #define BUS_CTRL_ENDIAN_NSP_CONF \ 67 (0x02 << DMADATA_ENDIAN_SHIFT | 0x02 << DMADESC_ENDIAN_SHIFT) 68 69 #define BUS_CTRL_ENDIAN_CONF_MASK \ 70 (0x3 << MMIO_ENDIAN_SHIFT | 0x3 << DMADESC_ENDIAN_SHIFT | \ 71 0x3 << DMADATA_ENDIAN_SHIFT | 0x3 << PIODATA_ENDIAN_SHIFT) 72 73 enum brcm_ahci_version { 74 BRCM_SATA_BCM7425 = 1, 75 BRCM_SATA_BCM7445, 76 BRCM_SATA_NSP, 77 BRCM_SATA_BCM7216, 78 }; 79 80 enum brcm_ahci_quirks { 81 BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(0), 82 }; 83 84 struct brcm_ahci_priv { 85 struct device *dev; 86 void __iomem *top_ctrl; 87 u32 port_mask; 88 u32 quirks; 89 enum brcm_ahci_version version; 90 struct reset_control *rcdev_rescal; 91 struct reset_control *rcdev_ahci; 92 }; 93 94 static inline u32 brcm_sata_readreg(void __iomem *addr) 95 { 96 /* 97 * MIPS endianness is configured by boot strap, which also reverses all 98 * bus endianness (i.e., big-endian CPU + big endian bus ==> native 99 * endian I/O). 100 * 101 * Other architectures (e.g., ARM) either do not support big endian, or 102 * else leave I/O in little endian mode. 103 */ 104 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 105 return __raw_readl(addr); 106 else 107 return readl_relaxed(addr); 108 } 109 110 static inline void brcm_sata_writereg(u32 val, void __iomem *addr) 111 { 112 /* See brcm_sata_readreg() comments */ 113 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 114 __raw_writel(val, addr); 115 else 116 writel_relaxed(val, addr); 117 } 118 119 static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv) 120 { 121 struct brcm_ahci_priv *priv = hpriv->plat_data; 122 u32 port_ctrl, host_caps; 123 int i; 124 125 /* Enable support for ALPM */ 126 host_caps = readl(hpriv->mmio + HOST_CAP); 127 if (!(host_caps & HOST_CAP_ALPM)) 128 hpriv->flags |= AHCI_HFLAG_YES_ALPM; 129 130 /* 131 * Adjust timeout to allow PLL sufficient time to lock while waking 132 * up from slumber mode. 133 */ 134 for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL; 135 i < SATA_TOP_MAX_PHYS; 136 i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) { 137 if (priv->port_mask & BIT(i)) 138 writel(0xff1003fc, 139 hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl)); 140 } 141 } 142 143 static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port) 144 { 145 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL + 146 (port * SATA_TOP_CTRL_PHY_OFFS); 147 void __iomem *p; 148 u32 reg; 149 150 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE) 151 return; 152 153 /* clear PHY_DEFAULT_POWER_STATE */ 154 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1; 155 reg = brcm_sata_readreg(p); 156 reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE; 157 brcm_sata_writereg(reg, p); 158 159 /* reset the PHY digital logic */ 160 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2; 161 reg = brcm_sata_readreg(p); 162 reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB | 163 SATA_TOP_CTRL_2_SW_RST_RX); 164 reg |= SATA_TOP_CTRL_2_SW_RST_TX; 165 brcm_sata_writereg(reg, p); 166 reg = brcm_sata_readreg(p); 167 reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET; 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 (void)brcm_sata_readreg(p); 173 } 174 175 static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port) 176 { 177 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL + 178 (port * SATA_TOP_CTRL_PHY_OFFS); 179 void __iomem *p; 180 u32 reg; 181 182 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE) 183 return; 184 185 /* power-off the PHY digital logic */ 186 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2; 187 reg = brcm_sata_readreg(p); 188 reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB | 189 SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX | 190 SATA_TOP_CTRL_2_PHY_GLOBAL_RESET); 191 brcm_sata_writereg(reg, p); 192 193 /* set PHY_DEFAULT_POWER_STATE */ 194 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1; 195 reg = brcm_sata_readreg(p); 196 reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE; 197 brcm_sata_writereg(reg, p); 198 } 199 200 static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv) 201 { 202 int i; 203 204 for (i = 0; i < SATA_TOP_MAX_PHYS; i++) 205 if (priv->port_mask & BIT(i)) 206 brcm_sata_phy_enable(priv, i); 207 } 208 209 static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv) 210 { 211 int i; 212 213 for (i = 0; i < SATA_TOP_MAX_PHYS; i++) 214 if (priv->port_mask & BIT(i)) 215 brcm_sata_phy_disable(priv, i); 216 } 217 218 static u32 brcm_ahci_get_portmask(struct ahci_host_priv *hpriv, 219 struct brcm_ahci_priv *priv) 220 { 221 u32 impl; 222 223 impl = readl(hpriv->mmio + HOST_PORTS_IMPL); 224 225 if (fls(impl) > SATA_TOP_MAX_PHYS) 226 dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n", 227 impl); 228 else if (!impl) 229 dev_info(priv->dev, "no ports found\n"); 230 231 return impl; 232 } 233 234 static void brcm_sata_init(struct brcm_ahci_priv *priv) 235 { 236 void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL; 237 u32 data; 238 239 /* Configure endianness */ 240 data = brcm_sata_readreg(ctrl); 241 data &= ~BUS_CTRL_ENDIAN_CONF_MASK; 242 if (priv->version == BRCM_SATA_NSP) 243 data |= BUS_CTRL_ENDIAN_NSP_CONF; 244 else 245 data |= BUS_CTRL_ENDIAN_CONF; 246 brcm_sata_writereg(data, ctrl); 247 } 248 249 static unsigned int brcm_ahci_read_id(struct ata_device *dev, 250 struct ata_taskfile *tf, __le16 *id) 251 { 252 struct ata_port *ap = dev->link->ap; 253 struct ata_host *host = ap->host; 254 struct ahci_host_priv *hpriv = host->private_data; 255 struct brcm_ahci_priv *priv = hpriv->plat_data; 256 void __iomem *mmio = hpriv->mmio; 257 unsigned int err_mask; 258 unsigned long flags; 259 int i, rc; 260 u32 ctl; 261 262 /* Try to read the device ID and, if this fails, proceed with the 263 * recovery sequence below 264 */ 265 err_mask = ata_do_dev_read_id(dev, tf, id); 266 if (likely(!err_mask)) 267 return err_mask; 268 269 /* Disable host interrupts */ 270 spin_lock_irqsave(&host->lock, flags); 271 ctl = readl(mmio + HOST_CTL); 272 ctl &= ~HOST_IRQ_EN; 273 writel(ctl, mmio + HOST_CTL); 274 readl(mmio + HOST_CTL); /* flush */ 275 spin_unlock_irqrestore(&host->lock, flags); 276 277 /* Perform the SATA PHY reset sequence */ 278 brcm_sata_phy_disable(priv, ap->port_no); 279 280 /* Reset the SATA clock */ 281 ahci_platform_disable_clks(hpriv); 282 msleep(10); 283 284 ahci_platform_enable_clks(hpriv); 285 msleep(10); 286 287 /* Bring the PHY back on */ 288 brcm_sata_phy_enable(priv, ap->port_no); 289 290 /* Re-initialize and calibrate the PHY */ 291 for (i = 0; i < hpriv->nports; i++) { 292 if (ahci_ignore_port(hpriv, i)) 293 continue; 294 295 rc = phy_init(hpriv->phys[i]); 296 if (rc) 297 goto disable_phys; 298 299 rc = phy_calibrate(hpriv->phys[i]); 300 if (rc) { 301 phy_exit(hpriv->phys[i]); 302 goto disable_phys; 303 } 304 } 305 306 /* Re-enable host interrupts */ 307 spin_lock_irqsave(&host->lock, flags); 308 ctl = readl(mmio + HOST_CTL); 309 ctl |= HOST_IRQ_EN; 310 writel(ctl, mmio + HOST_CTL); 311 readl(mmio + HOST_CTL); /* flush */ 312 spin_unlock_irqrestore(&host->lock, flags); 313 314 return ata_do_dev_read_id(dev, tf, id); 315 316 disable_phys: 317 while (--i >= 0) { 318 phy_power_off(hpriv->phys[i]); 319 phy_exit(hpriv->phys[i]); 320 } 321 322 return AC_ERR_OTHER; 323 } 324 325 static void brcm_ahci_host_stop(struct ata_host *host) 326 { 327 struct ahci_host_priv *hpriv = host->private_data; 328 329 ahci_platform_disable_resources(hpriv); 330 } 331 332 static struct ata_port_operations ahci_brcm_platform_ops = { 333 .inherits = &ahci_ops, 334 .host_stop = brcm_ahci_host_stop, 335 .read_id = brcm_ahci_read_id, 336 }; 337 338 static const struct ata_port_info ahci_brcm_port_info = { 339 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM, 340 .link_flags = ATA_LFLAG_NO_DEBOUNCE_DELAY, 341 .pio_mask = ATA_PIO4, 342 .udma_mask = ATA_UDMA6, 343 .port_ops = &ahci_brcm_platform_ops, 344 }; 345 346 static int brcm_ahci_suspend(struct device *dev) 347 { 348 struct ata_host *host = dev_get_drvdata(dev); 349 struct ahci_host_priv *hpriv = host->private_data; 350 struct brcm_ahci_priv *priv = hpriv->plat_data; 351 int ret; 352 353 brcm_sata_phys_disable(priv); 354 355 if (IS_ENABLED(CONFIG_PM_SLEEP)) 356 ret = ahci_platform_suspend(dev); 357 else 358 ret = 0; 359 360 reset_control_assert(priv->rcdev_ahci); 361 reset_control_rearm(priv->rcdev_rescal); 362 363 return ret; 364 } 365 366 static int __maybe_unused brcm_ahci_resume(struct device *dev) 367 { 368 struct ata_host *host = dev_get_drvdata(dev); 369 struct ahci_host_priv *hpriv = host->private_data; 370 struct brcm_ahci_priv *priv = hpriv->plat_data; 371 int ret = 0; 372 373 ret = reset_control_deassert(priv->rcdev_ahci); 374 if (ret) 375 return ret; 376 ret = reset_control_reset(priv->rcdev_rescal); 377 if (ret) 378 return ret; 379 380 /* Make sure clocks are turned on before re-configuration */ 381 ret = ahci_platform_enable_clks(hpriv); 382 if (ret) 383 return ret; 384 385 ret = ahci_platform_enable_regulators(hpriv); 386 if (ret) 387 goto out_disable_clks; 388 389 brcm_sata_init(priv); 390 brcm_sata_phys_enable(priv); 391 brcm_sata_alpm_init(hpriv); 392 393 /* Since we had to enable clocks earlier on, we cannot use 394 * ahci_platform_resume() as-is since a second call to 395 * ahci_platform_enable_resources() would bump up the resources 396 * (regulators, clocks, PHYs) count artificially so we copy the part 397 * after ahci_platform_enable_resources(). 398 */ 399 ret = ahci_platform_enable_phys(hpriv); 400 if (ret) 401 goto out_disable_phys; 402 403 ret = ahci_platform_resume_host(dev); 404 if (ret) 405 goto out_disable_platform_phys; 406 407 /* We resumed so update PM runtime state */ 408 pm_runtime_disable(dev); 409 pm_runtime_set_active(dev); 410 pm_runtime_enable(dev); 411 412 return 0; 413 414 out_disable_platform_phys: 415 ahci_platform_disable_phys(hpriv); 416 out_disable_phys: 417 brcm_sata_phys_disable(priv); 418 ahci_platform_disable_regulators(hpriv); 419 out_disable_clks: 420 ahci_platform_disable_clks(hpriv); 421 return ret; 422 } 423 424 static const struct scsi_host_template ahci_platform_sht = { 425 AHCI_SHT(DRV_NAME), 426 }; 427 428 static const struct of_device_id ahci_of_match[] = { 429 {.compatible = "brcm,bcm7425-ahci", .data = (void *)BRCM_SATA_BCM7425}, 430 {.compatible = "brcm,bcm7445-ahci", .data = (void *)BRCM_SATA_BCM7445}, 431 {.compatible = "brcm,bcm63138-ahci", .data = (void *)BRCM_SATA_BCM7445}, 432 {.compatible = "brcm,bcm-nsp-ahci", .data = (void *)BRCM_SATA_NSP}, 433 {.compatible = "brcm,bcm7216-ahci", .data = (void *)BRCM_SATA_BCM7216}, 434 { /* sentinel */ } 435 }; 436 MODULE_DEVICE_TABLE(of, ahci_of_match); 437 438 static int brcm_ahci_probe(struct platform_device *pdev) 439 { 440 const struct of_device_id *of_id; 441 struct device *dev = &pdev->dev; 442 struct brcm_ahci_priv *priv; 443 struct ahci_host_priv *hpriv; 444 int ret; 445 446 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 447 if (!priv) 448 return -ENOMEM; 449 450 of_id = of_match_node(ahci_of_match, pdev->dev.of_node); 451 if (!of_id) 452 return -ENODEV; 453 454 priv->version = (unsigned long)of_id->data; 455 priv->dev = dev; 456 457 priv->top_ctrl = devm_platform_ioremap_resource_byname(pdev, "top-ctrl"); 458 if (IS_ERR(priv->top_ctrl)) 459 return PTR_ERR(priv->top_ctrl); 460 461 if (priv->version == BRCM_SATA_BCM7216) { 462 priv->rcdev_rescal = devm_reset_control_get_optional_shared( 463 &pdev->dev, "rescal"); 464 if (IS_ERR(priv->rcdev_rescal)) 465 return PTR_ERR(priv->rcdev_rescal); 466 } 467 priv->rcdev_ahci = devm_reset_control_get_optional(&pdev->dev, "ahci"); 468 if (IS_ERR(priv->rcdev_ahci)) 469 return PTR_ERR(priv->rcdev_ahci); 470 471 hpriv = ahci_platform_get_resources(pdev, 0); 472 if (IS_ERR(hpriv)) 473 return PTR_ERR(hpriv); 474 475 hpriv->plat_data = priv; 476 hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP | AHCI_HFLAG_NO_WRITE_TO_RO; 477 478 switch (priv->version) { 479 case BRCM_SATA_BCM7425: 480 hpriv->flags |= AHCI_HFLAG_DELAY_ENGINE; 481 fallthrough; 482 case BRCM_SATA_NSP: 483 hpriv->flags |= AHCI_HFLAG_NO_NCQ; 484 priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE; 485 break; 486 default: 487 break; 488 } 489 490 ret = reset_control_reset(priv->rcdev_rescal); 491 if (ret) 492 return ret; 493 ret = reset_control_deassert(priv->rcdev_ahci); 494 if (ret) 495 return ret; 496 497 ret = ahci_platform_enable_clks(hpriv); 498 if (ret) 499 goto out_reset; 500 501 ret = ahci_platform_enable_regulators(hpriv); 502 if (ret) 503 goto out_disable_clks; 504 505 /* Must be first so as to configure endianness including that 506 * of the standard AHCI register space. 507 */ 508 brcm_sata_init(priv); 509 510 /* Initializes priv->port_mask which is used below */ 511 priv->port_mask = brcm_ahci_get_portmask(hpriv, priv); 512 if (!priv->port_mask) { 513 ret = -ENODEV; 514 goto out_disable_regulators; 515 } 516 517 /* Must be done before ahci_platform_enable_phys() */ 518 brcm_sata_phys_enable(priv); 519 520 brcm_sata_alpm_init(hpriv); 521 522 ret = ahci_platform_enable_phys(hpriv); 523 if (ret) 524 goto out_disable_phys; 525 526 ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info, 527 &ahci_platform_sht); 528 if (ret) 529 goto out_disable_platform_phys; 530 531 dev_info(dev, "Broadcom AHCI SATA3 registered\n"); 532 533 return 0; 534 535 out_disable_platform_phys: 536 ahci_platform_disable_phys(hpriv); 537 out_disable_phys: 538 brcm_sata_phys_disable(priv); 539 out_disable_regulators: 540 ahci_platform_disable_regulators(hpriv); 541 out_disable_clks: 542 ahci_platform_disable_clks(hpriv); 543 out_reset: 544 reset_control_assert(priv->rcdev_ahci); 545 reset_control_rearm(priv->rcdev_rescal); 546 return ret; 547 } 548 549 static void brcm_ahci_remove(struct platform_device *pdev) 550 { 551 struct ata_host *host = dev_get_drvdata(&pdev->dev); 552 struct ahci_host_priv *hpriv = host->private_data; 553 struct brcm_ahci_priv *priv = hpriv->plat_data; 554 555 brcm_sata_phys_disable(priv); 556 557 ata_platform_remove_one(pdev); 558 } 559 560 static void brcm_ahci_shutdown(struct platform_device *pdev) 561 { 562 int ret; 563 564 /* All resources releasing happens via devres, but our device, unlike a 565 * proper remove is not disappearing, therefore using 566 * brcm_ahci_suspend() here which does explicit power management is 567 * appropriate. 568 */ 569 ret = brcm_ahci_suspend(&pdev->dev); 570 if (ret) 571 dev_err(&pdev->dev, "failed to shutdown\n"); 572 } 573 574 static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume); 575 576 static struct platform_driver brcm_ahci_driver = { 577 .probe = brcm_ahci_probe, 578 .remove = brcm_ahci_remove, 579 .shutdown = brcm_ahci_shutdown, 580 .driver = { 581 .name = DRV_NAME, 582 .of_match_table = ahci_of_match, 583 .pm = &ahci_brcm_pm_ops, 584 }, 585 }; 586 module_platform_driver(brcm_ahci_driver); 587 588 MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver"); 589 MODULE_AUTHOR("Brian Norris"); 590 MODULE_LICENSE("GPL"); 591 MODULE_ALIAS("platform:sata-brcmstb"); 592