| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * AHCI SATA platform library |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * Jeff Garzik <jgarzik@pobox.com> |
|---|
| 6 | 7 | * Copyright 2010 MontaVista Software, LLC. |
|---|
| 7 | 8 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 10 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 11 | | - * the Free Software Foundation; either version 2, or (at your option) |
|---|
| 12 | | - * any later version. |
|---|
| 13 | 9 | */ |
|---|
| 14 | 10 | |
|---|
| 15 | 11 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 56 | 52 | if (rc) |
|---|
| 57 | 53 | goto disable_phys; |
|---|
| 58 | 54 | |
|---|
| 59 | | - rc = phy_power_on(hpriv->phys[i]); |
|---|
| 55 | + rc = phy_set_mode(hpriv->phys[i], PHY_MODE_SATA); |
|---|
| 60 | 56 | if (rc) { |
|---|
| 57 | + phy_exit(hpriv->phys[i]); |
|---|
| 58 | + goto disable_phys; |
|---|
| 59 | + } |
|---|
| 60 | + |
|---|
| 61 | + rc = phy_power_on(hpriv->phys[i]); |
|---|
| 62 | + if (rc && !(rc == -EOPNOTSUPP && (hpriv->flags & AHCI_HFLAG_IGN_NOTSUPP_POWER_ON))) { |
|---|
| 61 | 63 | phy_exit(hpriv->phys[i]); |
|---|
| 62 | 64 | goto disable_phys; |
|---|
| 63 | 65 | } |
|---|
| .. | .. |
|---|
| 141 | 143 | * ahci_platform_enable_regulators - Enable regulators |
|---|
| 142 | 144 | * @hpriv: host private area to store config values |
|---|
| 143 | 145 | * |
|---|
| 144 | | - * This function enables all the regulators found in |
|---|
| 146 | + * This function enables all the regulators found in controller and |
|---|
| 145 | 147 | * hpriv->target_pwrs, if any. If a regulator fails to be enabled, it |
|---|
| 146 | 148 | * disables all the regulators already enabled in reverse order and |
|---|
| 147 | 149 | * returns an error. |
|---|
| .. | .. |
|---|
| 152 | 154 | int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv) |
|---|
| 153 | 155 | { |
|---|
| 154 | 156 | int rc, i; |
|---|
| 157 | + |
|---|
| 158 | + rc = regulator_enable(hpriv->ahci_regulator); |
|---|
| 159 | + if (rc) |
|---|
| 160 | + return rc; |
|---|
| 161 | + |
|---|
| 162 | + rc = regulator_enable(hpriv->phy_regulator); |
|---|
| 163 | + if (rc) |
|---|
| 164 | + goto disable_ahci_pwrs; |
|---|
| 155 | 165 | |
|---|
| 156 | 166 | for (i = 0; i < hpriv->nports; i++) { |
|---|
| 157 | 167 | if (!hpriv->target_pwrs[i]) |
|---|
| .. | .. |
|---|
| 169 | 179 | if (hpriv->target_pwrs[i]) |
|---|
| 170 | 180 | regulator_disable(hpriv->target_pwrs[i]); |
|---|
| 171 | 181 | |
|---|
| 182 | + regulator_disable(hpriv->phy_regulator); |
|---|
| 183 | +disable_ahci_pwrs: |
|---|
| 184 | + regulator_disable(hpriv->ahci_regulator); |
|---|
| 172 | 185 | return rc; |
|---|
| 173 | 186 | } |
|---|
| 174 | 187 | EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators); |
|---|
| .. | .. |
|---|
| 177 | 190 | * ahci_platform_disable_regulators - Disable regulators |
|---|
| 178 | 191 | * @hpriv: host private area to store config values |
|---|
| 179 | 192 | * |
|---|
| 180 | | - * This function disables all regulators found in hpriv->target_pwrs. |
|---|
| 193 | + * This function disables all regulators found in hpriv->target_pwrs and |
|---|
| 194 | + * AHCI controller. |
|---|
| 181 | 195 | */ |
|---|
| 182 | 196 | void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv) |
|---|
| 183 | 197 | { |
|---|
| .. | .. |
|---|
| 188 | 202 | continue; |
|---|
| 189 | 203 | regulator_disable(hpriv->target_pwrs[i]); |
|---|
| 190 | 204 | } |
|---|
| 205 | + |
|---|
| 206 | + regulator_disable(hpriv->ahci_regulator); |
|---|
| 207 | + regulator_disable(hpriv->phy_regulator); |
|---|
| 191 | 208 | } |
|---|
| 192 | 209 | EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators); |
|---|
| 193 | 210 | /** |
|---|
| .. | .. |
|---|
| 305 | 322 | /* No PHY support. Check if PHY is required. */ |
|---|
| 306 | 323 | if (of_find_property(node, "phys", NULL)) { |
|---|
| 307 | 324 | dev_err(dev, |
|---|
| 308 | | - "couldn't get PHY in node %s: ENOSYS\n", |
|---|
| 309 | | - node->name); |
|---|
| 325 | + "couldn't get PHY in node %pOFn: ENOSYS\n", |
|---|
| 326 | + node); |
|---|
| 310 | 327 | break; |
|---|
| 311 | 328 | } |
|---|
| 312 | | - /* fall through */ |
|---|
| 329 | + fallthrough; |
|---|
| 313 | 330 | case -ENODEV: |
|---|
| 314 | 331 | /* continue normally */ |
|---|
| 315 | 332 | hpriv->phys[port] = NULL; |
|---|
| .. | .. |
|---|
| 321 | 338 | |
|---|
| 322 | 339 | default: |
|---|
| 323 | 340 | dev_err(dev, |
|---|
| 324 | | - "couldn't get PHY in node %s: %d\n", |
|---|
| 325 | | - node->name, rc); |
|---|
| 341 | + "couldn't get PHY in node %pOFn: %d\n", |
|---|
| 342 | + node, rc); |
|---|
| 326 | 343 | |
|---|
| 327 | 344 | break; |
|---|
| 328 | 345 | } |
|---|
| .. | .. |
|---|
| 336 | 353 | struct regulator *target_pwr; |
|---|
| 337 | 354 | int rc = 0; |
|---|
| 338 | 355 | |
|---|
| 339 | | - target_pwr = regulator_get_optional(dev, "target"); |
|---|
| 356 | + target_pwr = regulator_get(dev, "target"); |
|---|
| 340 | 357 | |
|---|
| 341 | 358 | if (!IS_ERR(target_pwr)) |
|---|
| 342 | 359 | hpriv->target_pwrs[port] = target_pwr; |
|---|
| .. | .. |
|---|
| 356 | 373 | * |
|---|
| 357 | 374 | * 1) mmio registers (IORESOURCE_MEM 0, mandatory) |
|---|
| 358 | 375 | * 2) regulator for controlling the targets power (optional) |
|---|
| 376 | + * regulator for controlling the AHCI controller (optional) |
|---|
| 359 | 377 | * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node, |
|---|
| 360 | 378 | * or for non devicetree enabled platforms a single clock |
|---|
| 361 | 379 | * 4) resets, if flags has AHCI_PLATFORM_GET_RESETS (optional) |
|---|
| .. | .. |
|---|
| 387 | 405 | hpriv->mmio = devm_ioremap_resource(dev, |
|---|
| 388 | 406 | platform_get_resource(pdev, IORESOURCE_MEM, 0)); |
|---|
| 389 | 407 | if (IS_ERR(hpriv->mmio)) { |
|---|
| 390 | | - dev_err(dev, "no mmio space\n"); |
|---|
| 391 | 408 | rc = PTR_ERR(hpriv->mmio); |
|---|
| 392 | 409 | goto err_out; |
|---|
| 393 | 410 | } |
|---|
| .. | .. |
|---|
| 413 | 430 | hpriv->clks[i] = clk; |
|---|
| 414 | 431 | } |
|---|
| 415 | 432 | |
|---|
| 433 | + hpriv->ahci_regulator = devm_regulator_get(dev, "ahci"); |
|---|
| 434 | + if (IS_ERR(hpriv->ahci_regulator)) { |
|---|
| 435 | + rc = PTR_ERR(hpriv->ahci_regulator); |
|---|
| 436 | + if (rc != 0) |
|---|
| 437 | + goto err_out; |
|---|
| 438 | + } |
|---|
| 439 | + |
|---|
| 440 | + hpriv->phy_regulator = devm_regulator_get(dev, "phy"); |
|---|
| 441 | + if (IS_ERR(hpriv->phy_regulator)) { |
|---|
| 442 | + rc = PTR_ERR(hpriv->phy_regulator); |
|---|
| 443 | + goto err_out; |
|---|
| 444 | + } |
|---|
| 445 | + |
|---|
| 416 | 446 | if (flags & AHCI_PLATFORM_GET_RESETS) { |
|---|
| 417 | 447 | hpriv->rsts = devm_reset_control_array_get_optional_shared(dev); |
|---|
| 418 | 448 | if (IS_ERR(hpriv->rsts)) { |
|---|
| .. | .. |
|---|
| 421 | 451 | } |
|---|
| 422 | 452 | } |
|---|
| 423 | 453 | |
|---|
| 424 | | - hpriv->nports = child_nodes = of_get_child_count(dev->of_node); |
|---|
| 454 | + /* |
|---|
| 455 | + * Too many sub-nodes most likely means having something wrong with |
|---|
| 456 | + * the firmware. |
|---|
| 457 | + */ |
|---|
| 458 | + child_nodes = of_get_child_count(dev->of_node); |
|---|
| 459 | + if (child_nodes > AHCI_MAX_PORTS) { |
|---|
| 460 | + rc = -EINVAL; |
|---|
| 461 | + goto err_out; |
|---|
| 462 | + } |
|---|
| 425 | 463 | |
|---|
| 426 | 464 | /* |
|---|
| 427 | 465 | * If no sub-node was found, we still need to set nports to |
|---|
| 428 | 466 | * one in order to be able to use the |
|---|
| 429 | 467 | * ahci_platform_[en|dis]able_[phys|regulators] functions. |
|---|
| 430 | 468 | */ |
|---|
| 431 | | - if (!child_nodes) |
|---|
| 469 | + if (child_nodes) |
|---|
| 470 | + hpriv->nports = child_nodes; |
|---|
| 471 | + else |
|---|
| 432 | 472 | hpriv->nports = 1; |
|---|
| 433 | 473 | |
|---|
| 434 | 474 | hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 456 | 496 | |
|---|
| 457 | 497 | if (of_property_read_u32(child, "reg", &port)) { |
|---|
| 458 | 498 | rc = -EINVAL; |
|---|
| 499 | + of_node_put(child); |
|---|
| 459 | 500 | goto err_out; |
|---|
| 460 | 501 | } |
|---|
| 461 | 502 | |
|---|
| .. | .. |
|---|
| 473 | 514 | if (port_dev) { |
|---|
| 474 | 515 | rc = ahci_platform_get_regulator(hpriv, port, |
|---|
| 475 | 516 | &port_dev->dev); |
|---|
| 476 | | - if (rc == -EPROBE_DEFER) |
|---|
| 517 | + if (rc == -EPROBE_DEFER) { |
|---|
| 518 | + of_node_put(child); |
|---|
| 477 | 519 | goto err_out; |
|---|
| 520 | + } |
|---|
| 478 | 521 | } |
|---|
| 479 | 522 | #endif |
|---|
| 480 | 523 | |
|---|
| 481 | 524 | rc = ahci_platform_get_phy(hpriv, port, dev, child); |
|---|
| 482 | | - if (rc) |
|---|
| 525 | + if (rc) { |
|---|
| 526 | + of_node_put(child); |
|---|
| 483 | 527 | goto err_out; |
|---|
| 528 | + } |
|---|
| 484 | 529 | |
|---|
| 485 | 530 | enabled_ports++; |
|---|
| 486 | 531 | } |
|---|
| .. | .. |
|---|
| 703 | 748 | writel(ctl, mmio + HOST_CTL); |
|---|
| 704 | 749 | readl(mmio + HOST_CTL); /* flush */ |
|---|
| 705 | 750 | |
|---|
| 751 | + if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS) |
|---|
| 752 | + ahci_platform_disable_phys(hpriv); |
|---|
| 753 | + |
|---|
| 706 | 754 | return ata_host_suspend(host, PMSG_SUSPEND); |
|---|
| 707 | 755 | } |
|---|
| 708 | 756 | EXPORT_SYMBOL_GPL(ahci_platform_suspend_host); |
|---|
| .. | .. |
|---|
| 721 | 769 | int ahci_platform_resume_host(struct device *dev) |
|---|
| 722 | 770 | { |
|---|
| 723 | 771 | struct ata_host *host = dev_get_drvdata(dev); |
|---|
| 772 | + struct ahci_host_priv *hpriv = host->private_data; |
|---|
| 724 | 773 | int rc; |
|---|
| 725 | 774 | |
|---|
| 726 | 775 | if (dev->power.power_state.event == PM_EVENT_SUSPEND) { |
|---|
| .. | .. |
|---|
| 731 | 780 | ahci_init_controller(host); |
|---|
| 732 | 781 | } |
|---|
| 733 | 782 | |
|---|
| 783 | + if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS) |
|---|
| 784 | + ahci_platform_enable_phys(hpriv); |
|---|
| 785 | + |
|---|
| 734 | 786 | ata_host_resume(host); |
|---|
| 735 | 787 | |
|---|
| 736 | 788 | return 0; |
|---|