1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AHCI SATA platform library
5 * Copyright 2004-2005 Red Hat, Inc.
7 * Copyright 2010 MontaVista Software, LLC.
11 #include <linux/clk.h>
12 #include <linux/kernel.h>
13 #include <linux/gfp.h>
14 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/libata.h>
20 #include <linux/ahci_platform.h>
21 #include <linux/phy/phy.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/of_platform.h>
25 #include <linux/reset.h>
28 static void ahci_host_stop(struct ata_host *host);
30 struct ata_port_operations ahci_platform_ops = {
31 .inherits = &ahci_ops,
32 .host_stop = ahci_host_stop,
34 EXPORT_SYMBOL_GPL(ahci_platform_ops);
37 * ahci_platform_enable_phys - Enable PHYs
38 * @hpriv: host private area to store config values
40 * This function enables all the PHYs found in hpriv->phys, if any.
41 * If a PHY fails to be enabled, it disables all the PHYs already
42 * enabled in reverse order and returns an error.
45 * 0 on success otherwise a negative error code
47 int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
51 for (i = 0; i < hpriv->nports; i++) {
52 rc = phy_init(hpriv->phys[i]);
56 rc = phy_set_mode(hpriv->phys[i], PHY_MODE_SATA);
58 phy_exit(hpriv->phys[i]);
62 rc = phy_power_on(hpriv->phys[i]);
64 phy_exit(hpriv->phys[i]);
73 phy_power_off(hpriv->phys[i]);
74 phy_exit(hpriv->phys[i]);
78 EXPORT_SYMBOL_GPL(ahci_platform_enable_phys);
81 * ahci_platform_disable_phys - Disable PHYs
82 * @hpriv: host private area to store config values
84 * This function disables all PHYs found in hpriv->phys.
86 void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
90 for (i = 0; i < hpriv->nports; i++) {
91 phy_power_off(hpriv->phys[i]);
92 phy_exit(hpriv->phys[i]);
95 EXPORT_SYMBOL_GPL(ahci_platform_disable_phys);
98 * ahci_platform_find_clk - Find platform clock
99 * @hpriv: host private area to store config values
100 * @con_id: clock connection ID
102 * This function returns a pointer to the clock descriptor of the clock with
106 * Pointer to the clock descriptor on success otherwise NULL
108 struct clk *ahci_platform_find_clk(struct ahci_host_priv *hpriv, const char *con_id)
112 for (i = 0; i < hpriv->n_clks; i++) {
113 if (hpriv->clks[i].id && !strcmp(hpriv->clks[i].id, con_id))
114 return hpriv->clks[i].clk;
119 EXPORT_SYMBOL_GPL(ahci_platform_find_clk);
122 * ahci_platform_enable_clks - Enable platform clocks
123 * @hpriv: host private area to store config values
125 * This function enables all the clks found for the AHCI device.
128 * 0 on success otherwise a negative error code
130 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
132 return clk_bulk_prepare_enable(hpriv->n_clks, hpriv->clks);
134 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
137 * ahci_platform_disable_clks - Disable platform clocks
138 * @hpriv: host private area to store config values
140 * This function disables all the clocks enabled before
141 * (bulk-clocks-disable function is supposed to do that in reverse
142 * from the enabling procedure order).
144 void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
146 clk_bulk_disable_unprepare(hpriv->n_clks, hpriv->clks);
148 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
151 * ahci_platform_deassert_rsts - Deassert/trigger platform resets
152 * @hpriv: host private area to store config values
154 * This function deasserts or triggers all the reset lines found for
158 * 0 on success otherwise a negative error code
160 int ahci_platform_deassert_rsts(struct ahci_host_priv *hpriv)
162 if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)
163 return reset_control_reset(hpriv->rsts);
165 return reset_control_deassert(hpriv->rsts);
167 EXPORT_SYMBOL_GPL(ahci_platform_deassert_rsts);
170 * ahci_platform_assert_rsts - Assert/rearm platform resets
171 * @hpriv: host private area to store config values
173 * This function asserts or rearms (for self-deasserting resets) all
174 * the reset controls found for the AHCI device.
177 * 0 on success otherwise a negative error code
179 int ahci_platform_assert_rsts(struct ahci_host_priv *hpriv)
181 if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)
182 return reset_control_rearm(hpriv->rsts);
184 return reset_control_assert(hpriv->rsts);
186 EXPORT_SYMBOL_GPL(ahci_platform_assert_rsts);
189 * ahci_platform_enable_regulators - Enable regulators
190 * @hpriv: host private area to store config values
192 * This function enables all the regulators found in controller and
193 * hpriv->target_pwrs, if any. If a regulator fails to be enabled, it
194 * disables all the regulators already enabled in reverse order and
198 * 0 on success otherwise a negative error code
200 int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv)
204 rc = regulator_enable(hpriv->ahci_regulator);
208 rc = regulator_enable(hpriv->phy_regulator);
210 goto disable_ahci_pwrs;
212 for (i = 0; i < hpriv->nports; i++) {
213 if (!hpriv->target_pwrs[i])
216 rc = regulator_enable(hpriv->target_pwrs[i]);
218 goto disable_target_pwrs;
225 if (hpriv->target_pwrs[i])
226 regulator_disable(hpriv->target_pwrs[i]);
228 regulator_disable(hpriv->phy_regulator);
230 regulator_disable(hpriv->ahci_regulator);
233 EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators);
236 * ahci_platform_disable_regulators - Disable regulators
237 * @hpriv: host private area to store config values
239 * This function disables all regulators found in hpriv->target_pwrs and
242 void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv)
246 for (i = 0; i < hpriv->nports; i++) {
247 if (!hpriv->target_pwrs[i])
249 regulator_disable(hpriv->target_pwrs[i]);
252 regulator_disable(hpriv->ahci_regulator);
253 regulator_disable(hpriv->phy_regulator);
255 EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators);
257 * ahci_platform_enable_resources - Enable platform resources
258 * @hpriv: host private area to store config values
260 * This function enables all ahci_platform managed resources in the
263 * 2) Clocks (through ahci_platform_enable_clks)
267 * If resource enabling fails at any point the previous enabled resources
268 * are disabled in reverse order.
271 * 0 on success otherwise a negative error code
273 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
277 rc = ahci_platform_enable_regulators(hpriv);
281 rc = ahci_platform_enable_clks(hpriv);
283 goto disable_regulator;
285 rc = ahci_platform_deassert_rsts(hpriv);
289 rc = ahci_platform_enable_phys(hpriv);
296 ahci_platform_assert_rsts(hpriv);
299 ahci_platform_disable_clks(hpriv);
302 ahci_platform_disable_regulators(hpriv);
306 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
309 * ahci_platform_disable_resources - Disable platform resources
310 * @hpriv: host private area to store config values
312 * This function disables all ahci_platform managed resources in the
316 * 3) Clocks (through ahci_platform_disable_clks)
319 void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
321 ahci_platform_disable_phys(hpriv);
323 ahci_platform_assert_rsts(hpriv);
325 ahci_platform_disable_clks(hpriv);
327 ahci_platform_disable_regulators(hpriv);
329 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
331 static void ahci_platform_put_resources(struct device *dev, void *res)
333 struct ahci_host_priv *hpriv = res;
336 if (hpriv->got_runtime_pm) {
337 pm_runtime_put_sync(dev);
338 pm_runtime_disable(dev);
342 * The regulators are tied to child node device and not to the
343 * SATA device itself. So we can't use devm for automatically
344 * releasing them. We have to do it manually here.
346 for (c = 0; c < hpriv->nports; c++)
347 if (hpriv->target_pwrs && hpriv->target_pwrs[c])
348 regulator_put(hpriv->target_pwrs[c]);
350 kfree(hpriv->target_pwrs);
353 static int ahci_platform_get_phy(struct ahci_host_priv *hpriv, u32 port,
354 struct device *dev, struct device_node *node)
358 hpriv->phys[port] = devm_of_phy_get(dev, node, NULL);
360 if (!IS_ERR(hpriv->phys[port]))
363 rc = PTR_ERR(hpriv->phys[port]);
366 /* No PHY support. Check if PHY is required. */
367 if (of_property_present(node, "phys")) {
369 "couldn't get PHY in node %pOFn: ENOSYS\n",
375 /* continue normally */
376 hpriv->phys[port] = NULL;
380 /* Do not complain yet */
385 "couldn't get PHY in node %pOFn: %d\n",
394 static int ahci_platform_get_regulator(struct ahci_host_priv *hpriv, u32 port,
397 struct regulator *target_pwr;
400 target_pwr = regulator_get(dev, "target");
402 if (!IS_ERR(target_pwr))
403 hpriv->target_pwrs[port] = target_pwr;
405 rc = PTR_ERR(target_pwr);
410 static int ahci_platform_get_firmware(struct ahci_host_priv *hpriv,
415 if (!of_property_read_u32(dev->of_node, "hba-cap", &hpriv->saved_cap))
416 hpriv->saved_cap &= (HOST_CAP_SSS | HOST_CAP_MPS);
418 of_property_read_u32(dev->of_node,
419 "ports-implemented", &hpriv->saved_port_map);
421 for_each_child_of_node_scoped(dev->of_node, child) {
422 if (!of_device_is_available(child))
425 if (of_property_read_u32(child, "reg", &port))
428 if (!of_property_read_u32(child, "hba-port-cap", &hpriv->saved_port_cap[port]))
429 hpriv->saved_port_cap[port] &= PORT_CMD_CAP;
436 * ahci_platform_get_resources - Get platform resources
437 * @pdev: platform device to get resources for
438 * @flags: bitmap representing the resource to get
440 * This function allocates an ahci_host_priv struct, and gets the following
441 * resources, storing a reference to them inside the returned struct:
443 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
444 * 2) regulator for controlling the targets power (optional)
445 * regulator for controlling the AHCI controller (optional)
446 * 3) all clocks specified in the devicetree node, or a single
447 * clock for non-OF platforms (optional)
448 * 4) resets, if flags has AHCI_PLATFORM_GET_RESETS (optional)
452 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
454 struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
457 int child_nodes, rc = -ENOMEM, enabled_ports = 0;
458 struct device *dev = &pdev->dev;
459 struct ahci_host_priv *hpriv;
460 u32 mask_port_map = 0;
462 if (!devres_open_group(dev, NULL, GFP_KERNEL))
463 return ERR_PTR(-ENOMEM);
465 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
470 devres_add(dev, hpriv);
473 * If the DT provided an "ahci" named resource, use it. Otherwise,
474 * fallback to using the default first resource for the device node.
476 if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "ahci"))
477 hpriv->mmio = devm_platform_ioremap_resource_byname(pdev, "ahci");
479 hpriv->mmio = devm_platform_ioremap_resource(pdev, 0);
480 if (IS_ERR(hpriv->mmio)) {
481 rc = PTR_ERR(hpriv->mmio);
486 * Bulk clocks getting procedure can fail to find any clock due to
487 * running on a non-OF platform or due to the clocks being defined in
488 * bypass of the DT firmware (like da850, spear13xx). In that case we
489 * fallback to getting a single clock source right from the dev clocks
492 rc = devm_clk_bulk_get_all(dev, &hpriv->clks);
497 /* Got clocks in bulk */
501 * No clock bulk found: fallback to manually getting
502 * the optional clock.
504 hpriv->clks = devm_kzalloc(dev, sizeof(*hpriv->clks), GFP_KERNEL);
509 hpriv->clks->clk = devm_clk_get_optional(dev, NULL);
510 if (IS_ERR(hpriv->clks->clk)) {
511 rc = PTR_ERR(hpriv->clks->clk);
513 } else if (hpriv->clks->clk) {
514 hpriv->clks->id = "ahci";
519 hpriv->ahci_regulator = devm_regulator_get(dev, "ahci");
520 if (IS_ERR(hpriv->ahci_regulator)) {
521 rc = PTR_ERR(hpriv->ahci_regulator);
526 hpriv->phy_regulator = devm_regulator_get(dev, "phy");
527 if (IS_ERR(hpriv->phy_regulator)) {
528 rc = PTR_ERR(hpriv->phy_regulator);
532 if (flags & AHCI_PLATFORM_GET_RESETS) {
533 hpriv->rsts = devm_reset_control_array_get_optional_shared(dev);
534 if (IS_ERR(hpriv->rsts)) {
535 rc = PTR_ERR(hpriv->rsts);
539 hpriv->f_rsts = flags & AHCI_PLATFORM_RST_TRIGGER;
543 * Too many sub-nodes most likely means having something wrong with
546 child_nodes = of_get_child_count(dev->of_node);
547 if (child_nodes > AHCI_MAX_PORTS) {
553 * If no sub-node was found, we still need to set nports to
554 * one in order to be able to use the
555 * ahci_platform_[en|dis]able_[phys|regulators] functions.
558 hpriv->nports = child_nodes;
562 hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL);
568 * We cannot use devm_ here, since ahci_platform_put_resources() uses
569 * target_pwrs after devm_ have freed memory
571 hpriv->target_pwrs = kcalloc(hpriv->nports, sizeof(*hpriv->target_pwrs), GFP_KERNEL);
572 if (!hpriv->target_pwrs) {
578 for_each_child_of_node_scoped(dev->of_node, child) {
580 struct platform_device *port_dev __maybe_unused;
582 if (!of_device_is_available(child))
585 if (of_property_read_u32(child, "reg", &port)) {
590 if (port >= hpriv->nports) {
591 dev_warn(dev, "invalid port number %d\n", port);
594 mask_port_map |= BIT(port);
596 #ifdef CONFIG_OF_ADDRESS
597 of_platform_device_create(child, NULL, NULL);
599 port_dev = of_find_device_by_node(child);
602 rc = ahci_platform_get_regulator(hpriv, port,
604 if (rc == -EPROBE_DEFER)
609 rc = ahci_platform_get_phy(hpriv, port, dev, child);
615 if (!enabled_ports) {
616 dev_warn(dev, "No port enabled\n");
621 if (!hpriv->mask_port_map)
622 hpriv->mask_port_map = mask_port_map;
625 * If no sub-node was found, keep this for device tree
628 rc = ahci_platform_get_phy(hpriv, 0, dev, dev->of_node);
632 rc = ahci_platform_get_regulator(hpriv, 0, dev);
633 if (rc == -EPROBE_DEFER)
638 * Retrieve firmware-specific flags which then will be used to set
639 * the HW-init fields of HBA and its ports
641 rc = ahci_platform_get_firmware(hpriv, dev);
645 pm_runtime_enable(dev);
646 pm_runtime_get_sync(dev);
647 hpriv->got_runtime_pm = true;
649 devres_remove_group(dev, NULL);
653 devres_release_group(dev, NULL);
656 EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
659 * ahci_platform_init_host - Bring up an ahci-platform host
660 * @pdev: platform device pointer for the host
661 * @hpriv: ahci-host private data for the host
662 * @pi_template: template for the ata_port_info to use
663 * @sht: scsi_host_template to use when registering
665 * This function does all the usual steps needed to bring up an
666 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
667 * must be initialized / enabled before calling this.
670 * 0 on success otherwise a negative error code
672 int ahci_platform_init_host(struct platform_device *pdev,
673 struct ahci_host_priv *hpriv,
674 const struct ata_port_info *pi_template,
675 const struct scsi_host_template *sht)
677 struct device *dev = &pdev->dev;
678 struct ata_port_info pi = *pi_template;
679 const struct ata_port_info *ppi[] = { &pi, NULL };
680 struct ata_host *host;
681 int i, irq, n_ports, rc;
683 irq = platform_get_irq(pdev, 0);
692 pi.private_data = (void *)(unsigned long)hpriv->flags;
694 ahci_save_initial_config(dev, hpriv);
696 if (hpriv->cap & HOST_CAP_NCQ)
697 pi.flags |= ATA_FLAG_NCQ;
699 if (hpriv->cap & HOST_CAP_PMP)
700 pi.flags |= ATA_FLAG_PMP;
702 ahci_set_em_messages(hpriv, &pi);
704 /* CAP.NP sometimes indicate the index of the last enabled
705 * port, at other times, that of the last possible port, so
706 * determining the maximum port number requires looking at
707 * both CAP.NP and port_map.
709 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
711 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
715 host->private_data = hpriv;
717 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
718 host->flags |= ATA_HOST_PARALLEL_SCAN;
720 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
722 if (pi.flags & ATA_FLAG_EM)
725 for (i = 0; i < host->n_ports; i++) {
726 struct ata_port *ap = host->ports[i];
728 ata_port_desc(ap, "mmio %pR",
729 platform_get_resource(pdev, IORESOURCE_MEM, 0));
730 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
732 /* set enclosure management message type */
733 if (ap->flags & ATA_FLAG_EM)
734 ap->em_message_type = hpriv->em_msg_type;
736 /* disabled/not-implemented port */
737 if (!(hpriv->port_map & (1 << i)))
738 ap->ops = &ata_dummy_port_ops;
741 if (hpriv->cap & HOST_CAP_64) {
742 rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
744 dev_err(dev, "Failed to enable 64-bit DMA.\n");
749 rc = ahci_reset_controller(host);
753 ahci_init_controller(host);
754 ahci_print_info(host, "platform");
756 return ahci_host_activate(host, sht);
758 EXPORT_SYMBOL_GPL(ahci_platform_init_host);
760 static void ahci_host_stop(struct ata_host *host)
762 struct ahci_host_priv *hpriv = host->private_data;
764 ahci_platform_disable_resources(hpriv);
768 * ahci_platform_shutdown - Disable interrupts and stop DMA for host ports
769 * @pdev: platform device pointer for the host
771 * This function is called during system shutdown and performs the minimal
772 * deconfiguration required to ensure that an ahci_platform host cannot
773 * corrupt or otherwise interfere with a new kernel being started with kexec.
775 void ahci_platform_shutdown(struct platform_device *pdev)
777 struct ata_host *host = platform_get_drvdata(pdev);
778 struct ahci_host_priv *hpriv = host->private_data;
779 void __iomem *mmio = hpriv->mmio;
782 for (i = 0; i < host->n_ports; i++) {
783 struct ata_port *ap = host->ports[i];
785 /* Disable port interrupts */
789 /* Stop the port DMA engines */
790 if (ap->ops->port_stop)
791 ap->ops->port_stop(ap);
794 /* Disable and clear host interrupts */
795 writel(readl(mmio + HOST_CTL) & ~HOST_IRQ_EN, mmio + HOST_CTL);
796 readl(mmio + HOST_CTL); /* flush */
797 writel(GENMASK(host->n_ports, 0), mmio + HOST_IRQ_STAT);
799 EXPORT_SYMBOL_GPL(ahci_platform_shutdown);
801 #ifdef CONFIG_PM_SLEEP
803 * ahci_platform_suspend_host - Suspend an ahci-platform host
804 * @dev: device pointer for the host
806 * This function does all the usual steps needed to suspend an
807 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
808 * must be disabled after calling this.
811 * 0 on success otherwise a negative error code
813 int ahci_platform_suspend_host(struct device *dev)
815 struct ata_host *host = dev_get_drvdata(dev);
816 struct ahci_host_priv *hpriv = host->private_data;
817 void __iomem *mmio = hpriv->mmio;
820 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
821 dev_err(dev, "firmware update required for suspend/resume\n");
826 * AHCI spec rev1.1 section 8.3.3:
827 * Software must disable interrupts prior to requesting a
828 * transition of the HBA to D3 state.
830 ctl = readl(mmio + HOST_CTL);
832 writel(ctl, mmio + HOST_CTL);
833 readl(mmio + HOST_CTL); /* flush */
835 if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS)
836 ahci_platform_disable_phys(hpriv);
838 ata_host_suspend(host, PMSG_SUSPEND);
841 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
844 * ahci_platform_resume_host - Resume an ahci-platform host
845 * @dev: device pointer for the host
847 * This function does all the usual steps needed to resume an ahci-platform
848 * host, note any necessary resources (ie clks, phys, etc.) must be
849 * initialized / enabled before calling this.
852 * 0 on success otherwise a negative error code
854 int ahci_platform_resume_host(struct device *dev)
856 struct ata_host *host = dev_get_drvdata(dev);
857 struct ahci_host_priv *hpriv = host->private_data;
860 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
861 rc = ahci_reset_controller(host);
865 ahci_init_controller(host);
868 if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS)
869 ahci_platform_enable_phys(hpriv);
871 ata_host_resume(host);
875 EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
878 * ahci_platform_suspend - Suspend an ahci-platform device
879 * @dev: the platform device to suspend
881 * This function suspends the host associated with the device, followed by
882 * disabling all the resources of the device.
885 * 0 on success otherwise a negative error code
887 int ahci_platform_suspend(struct device *dev)
889 struct ata_host *host = dev_get_drvdata(dev);
890 struct ahci_host_priv *hpriv = host->private_data;
893 rc = ahci_platform_suspend_host(dev);
897 ahci_platform_disable_resources(hpriv);
901 EXPORT_SYMBOL_GPL(ahci_platform_suspend);
904 * ahci_platform_resume - Resume an ahci-platform device
905 * @dev: the platform device to resume
907 * This function enables all the resources of the device followed by
908 * resuming the host associated with the device.
911 * 0 on success otherwise a negative error code
913 int ahci_platform_resume(struct device *dev)
915 struct ata_host *host = dev_get_drvdata(dev);
916 struct ahci_host_priv *hpriv = host->private_data;
919 rc = ahci_platform_enable_resources(hpriv);
923 rc = ahci_platform_resume_host(dev);
925 goto disable_resources;
927 /* We resumed so update PM runtime state */
928 pm_runtime_disable(dev);
929 pm_runtime_set_active(dev);
930 pm_runtime_enable(dev);
935 ahci_platform_disable_resources(hpriv);
939 EXPORT_SYMBOL_GPL(ahci_platform_resume);
942 MODULE_DESCRIPTION("AHCI SATA platform library");
944 MODULE_LICENSE("GPL");