2 * AHCI SATA platform library
4 * Copyright 2004-2005 Red Hat, Inc.
5 * Jeff Garzik <jgarzik@pobox.com>
6 * Copyright 2010 MontaVista Software, LLC.
7 * Anton Vorontsov <avorontsov@ru.mvista.com>
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)
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/libata.h>
24 #include <linux/ahci_platform.h>
25 #include <linux/phy/phy.h>
26 #include <linux/pm_runtime.h>
29 static void ahci_host_stop(struct ata_host
*host
);
31 struct ata_port_operations ahci_platform_ops
= {
32 .inherits
= &ahci_ops
,
33 .host_stop
= ahci_host_stop
,
35 EXPORT_SYMBOL_GPL(ahci_platform_ops
);
37 static struct scsi_host_template ahci_platform_sht
= {
38 AHCI_SHT("ahci_platform"),
42 * ahci_platform_enable_phys - Enable PHYs
43 * @hpriv: host private area to store config values
45 * This function enables all the PHYs found in hpriv->phys, if any.
46 * If a PHY fails to be enabled, it disables all the PHYs already
47 * enabled in reverse order and returns an error.
50 * 0 on success otherwise a negative error code
52 int ahci_platform_enable_phys(struct ahci_host_priv
*hpriv
)
56 for (i
= 0; i
< hpriv
->nports
; i
++) {
60 rc
= phy_init(hpriv
->phys
[i
]);
64 rc
= phy_power_on(hpriv
->phys
[i
]);
66 phy_exit(hpriv
->phys
[i
]);
75 phy_power_off(hpriv
->phys
[i
]);
76 phy_exit(hpriv
->phys
[i
]);
80 EXPORT_SYMBOL_GPL(ahci_platform_enable_phys
);
83 * ahci_platform_disable_phys - Disable PHYs
84 * @hpriv: host private area to store config values
86 * This function disables all PHYs found in hpriv->phys.
88 void ahci_platform_disable_phys(struct ahci_host_priv
*hpriv
)
92 for (i
= 0; i
< hpriv
->nports
; i
++) {
96 phy_power_off(hpriv
->phys
[i
]);
97 phy_exit(hpriv
->phys
[i
]);
100 EXPORT_SYMBOL_GPL(ahci_platform_disable_phys
);
103 * ahci_platform_enable_clks - Enable platform clocks
104 * @hpriv: host private area to store config values
106 * This function enables all the clks found in hpriv->clks, starting at
107 * index 0. If any clk fails to enable it disables all the clks already
108 * enabled in reverse order, and then returns an error.
111 * 0 on success otherwise a negative error code
113 int ahci_platform_enable_clks(struct ahci_host_priv
*hpriv
)
117 for (c
= 0; c
< AHCI_MAX_CLKS
&& hpriv
->clks
[c
]; c
++) {
118 rc
= clk_prepare_enable(hpriv
->clks
[c
]);
120 goto disable_unprepare_clk
;
124 disable_unprepare_clk
:
126 clk_disable_unprepare(hpriv
->clks
[c
]);
129 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks
);
132 * ahci_platform_disable_clks - Disable platform clocks
133 * @hpriv: host private area to store config values
135 * This function disables all the clks found in hpriv->clks, in reverse
136 * order of ahci_platform_enable_clks (starting at the end of the array).
138 void ahci_platform_disable_clks(struct ahci_host_priv
*hpriv
)
142 for (c
= AHCI_MAX_CLKS
- 1; c
>= 0; c
--)
144 clk_disable_unprepare(hpriv
->clks
[c
]);
146 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks
);
149 * ahci_platform_enable_resources - Enable platform resources
150 * @hpriv: host private area to store config values
152 * This function enables all ahci_platform managed resources in the
155 * 2) Clocks (through ahci_platform_enable_clks)
158 * If resource enabling fails at any point the previous enabled resources
159 * are disabled in reverse order.
162 * 0 on success otherwise a negative error code
164 int ahci_platform_enable_resources(struct ahci_host_priv
*hpriv
)
168 if (hpriv
->target_pwr
) {
169 rc
= regulator_enable(hpriv
->target_pwr
);
174 rc
= ahci_platform_enable_clks(hpriv
);
176 goto disable_regulator
;
178 rc
= ahci_platform_enable_phys(hpriv
);
185 ahci_platform_disable_clks(hpriv
);
188 if (hpriv
->target_pwr
)
189 regulator_disable(hpriv
->target_pwr
);
192 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources
);
195 * ahci_platform_disable_resources - Disable platform resources
196 * @hpriv: host private area to store config values
198 * This function disables all ahci_platform managed resources in the
201 * 2) Clocks (through ahci_platform_disable_clks)
204 void ahci_platform_disable_resources(struct ahci_host_priv
*hpriv
)
206 ahci_platform_disable_phys(hpriv
);
208 ahci_platform_disable_clks(hpriv
);
210 if (hpriv
->target_pwr
)
211 regulator_disable(hpriv
->target_pwr
);
213 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources
);
215 static void ahci_platform_put_resources(struct device
*dev
, void *res
)
217 struct ahci_host_priv
*hpriv
= res
;
220 if (hpriv
->got_runtime_pm
) {
221 pm_runtime_put_sync(dev
);
222 pm_runtime_disable(dev
);
225 for (c
= 0; c
< AHCI_MAX_CLKS
&& hpriv
->clks
[c
]; c
++)
226 clk_put(hpriv
->clks
[c
]);
230 * ahci_platform_get_resources - Get platform resources
231 * @pdev: platform device to get resources for
233 * This function allocates an ahci_host_priv struct, and gets the following
234 * resources, storing a reference to them inside the returned struct:
236 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
237 * 2) regulator for controlling the targets power (optional)
238 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
239 * or for non devicetree enabled platforms a single clock
243 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
245 struct ahci_host_priv
*ahci_platform_get_resources(struct platform_device
*pdev
)
247 struct device
*dev
= &pdev
->dev
;
248 struct ahci_host_priv
*hpriv
;
250 struct device_node
*child
;
251 int i
, enabled_ports
= 0, rc
= -ENOMEM
;
252 u32 mask_port_map
= 0;
254 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
255 return ERR_PTR(-ENOMEM
);
257 hpriv
= devres_alloc(ahci_platform_put_resources
, sizeof(*hpriv
),
262 devres_add(dev
, hpriv
);
264 hpriv
->mmio
= devm_ioremap_resource(dev
,
265 platform_get_resource(pdev
, IORESOURCE_MEM
, 0));
266 if (IS_ERR(hpriv
->mmio
)) {
267 dev_err(dev
, "no mmio space\n");
268 rc
= PTR_ERR(hpriv
->mmio
);
272 hpriv
->target_pwr
= devm_regulator_get_optional(dev
, "target");
273 if (IS_ERR(hpriv
->target_pwr
)) {
274 rc
= PTR_ERR(hpriv
->target_pwr
);
275 if (rc
== -EPROBE_DEFER
)
277 hpriv
->target_pwr
= NULL
;
280 for (i
= 0; i
< AHCI_MAX_CLKS
; i
++) {
282 * For now we must use clk_get(dev, NULL) for the first clock,
283 * because some platforms (da850, spear13xx) are not yet
284 * converted to use devicetree for clocks. For new platforms
285 * this is equivalent to of_clk_get(dev->of_node, 0).
288 clk
= clk_get(dev
, NULL
);
290 clk
= of_clk_get(dev
->of_node
, i
);
294 if (rc
== -EPROBE_DEFER
)
298 hpriv
->clks
[i
] = clk
;
301 hpriv
->nports
= of_get_child_count(dev
->of_node
);
304 hpriv
->phys
= devm_kzalloc(dev
,
305 hpriv
->nports
* sizeof(*hpriv
->phys
),
312 for_each_child_of_node(dev
->of_node
, child
) {
315 if (!of_device_is_available(child
))
318 if (of_property_read_u32(child
, "reg", &port
)) {
323 if (port
>= hpriv
->nports
) {
324 dev_warn(dev
, "invalid port number %d\n", port
);
328 mask_port_map
|= BIT(port
);
330 hpriv
->phys
[port
] = devm_of_phy_get(dev
, child
, NULL
);
331 if (IS_ERR(hpriv
->phys
[port
])) {
332 rc
= PTR_ERR(hpriv
->phys
[port
]);
334 "couldn't get PHY in node %s: %d\n",
341 if (!enabled_ports
) {
342 dev_warn(dev
, "No port enabled\n");
347 if (!hpriv
->mask_port_map
)
348 hpriv
->mask_port_map
= mask_port_map
;
351 * If no sub-node was found, keep this for device tree
354 struct phy
*phy
= devm_phy_get(dev
, "sata-phy");
356 hpriv
->phys
= devm_kzalloc(dev
, sizeof(*hpriv
->phys
),
363 hpriv
->phys
[0] = phy
;
369 /* No PHY support. Check if PHY is required. */
370 if (of_find_property(dev
->of_node
, "phys", NULL
)) {
371 dev_err(dev
, "couldn't get sata-phy: ENOSYS\n");
375 /* continue normally */
386 pm_runtime_enable(dev
);
387 pm_runtime_get_sync(dev
);
388 hpriv
->got_runtime_pm
= true;
390 devres_remove_group(dev
, NULL
);
394 devres_release_group(dev
, NULL
);
397 EXPORT_SYMBOL_GPL(ahci_platform_get_resources
);
400 * ahci_platform_init_host - Bring up an ahci-platform host
401 * @pdev: platform device pointer for the host
402 * @hpriv: ahci-host private data for the host
403 * @pi_template: template for the ata_port_info to use
405 * This function does all the usual steps needed to bring up an
406 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
407 * must be initialized / enabled before calling this.
410 * 0 on success otherwise a negative error code
412 int ahci_platform_init_host(struct platform_device
*pdev
,
413 struct ahci_host_priv
*hpriv
,
414 const struct ata_port_info
*pi_template
)
416 struct device
*dev
= &pdev
->dev
;
417 struct ata_port_info pi
= *pi_template
;
418 const struct ata_port_info
*ppi
[] = { &pi
, NULL
};
419 struct ata_host
*host
;
420 int i
, irq
, n_ports
, rc
;
422 irq
= platform_get_irq(pdev
, 0);
424 dev_err(dev
, "no irq\n");
429 pi
.private_data
= (void *)(unsigned long)hpriv
->flags
;
431 ahci_save_initial_config(dev
, hpriv
);
433 if (hpriv
->cap
& HOST_CAP_NCQ
)
434 pi
.flags
|= ATA_FLAG_NCQ
;
436 if (hpriv
->cap
& HOST_CAP_PMP
)
437 pi
.flags
|= ATA_FLAG_PMP
;
439 ahci_set_em_messages(hpriv
, &pi
);
441 /* CAP.NP sometimes indicate the index of the last enabled
442 * port, at other times, that of the last possible port, so
443 * determining the maximum port number requires looking at
444 * both CAP.NP and port_map.
446 n_ports
= max(ahci_nr_ports(hpriv
->cap
), fls(hpriv
->port_map
));
448 host
= ata_host_alloc_pinfo(dev
, ppi
, n_ports
);
452 host
->private_data
= hpriv
;
454 if (!(hpriv
->cap
& HOST_CAP_SSS
) || ahci_ignore_sss
)
455 host
->flags
|= ATA_HOST_PARALLEL_SCAN
;
457 dev_info(dev
, "SSS flag set, parallel bus scan disabled\n");
459 if (pi
.flags
& ATA_FLAG_EM
)
462 for (i
= 0; i
< host
->n_ports
; i
++) {
463 struct ata_port
*ap
= host
->ports
[i
];
465 ata_port_desc(ap
, "mmio %pR",
466 platform_get_resource(pdev
, IORESOURCE_MEM
, 0));
467 ata_port_desc(ap
, "port 0x%x", 0x100 + ap
->port_no
* 0x80);
469 /* set enclosure management message type */
470 if (ap
->flags
& ATA_FLAG_EM
)
471 ap
->em_message_type
= hpriv
->em_msg_type
;
473 /* disabled/not-implemented port */
474 if (!(hpriv
->port_map
& (1 << i
)))
475 ap
->ops
= &ata_dummy_port_ops
;
478 if (hpriv
->cap
& HOST_CAP_64
) {
479 rc
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(64));
481 rc
= dma_coerce_mask_and_coherent(dev
,
484 dev_err(dev
, "Failed to enable 64-bit DMA.\n");
487 dev_warn(dev
, "Enable 32-bit DMA instead of 64-bit.\n");
491 rc
= ahci_reset_controller(host
);
495 ahci_init_controller(host
);
496 ahci_print_info(host
, "platform");
498 return ata_host_activate(host
, irq
, ahci_interrupt
, IRQF_SHARED
,
501 EXPORT_SYMBOL_GPL(ahci_platform_init_host
);
503 static void ahci_host_stop(struct ata_host
*host
)
505 struct device
*dev
= host
->dev
;
506 struct ahci_platform_data
*pdata
= dev_get_platdata(dev
);
507 struct ahci_host_priv
*hpriv
= host
->private_data
;
509 if (pdata
&& pdata
->exit
)
512 ahci_platform_disable_resources(hpriv
);
515 #ifdef CONFIG_PM_SLEEP
517 * ahci_platform_suspend_host - Suspend an ahci-platform host
518 * @dev: device pointer for the host
520 * This function does all the usual steps needed to suspend an
521 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
522 * must be disabled after calling this.
525 * 0 on success otherwise a negative error code
527 int ahci_platform_suspend_host(struct device
*dev
)
529 struct ata_host
*host
= dev_get_drvdata(dev
);
530 struct ahci_host_priv
*hpriv
= host
->private_data
;
531 void __iomem
*mmio
= hpriv
->mmio
;
534 if (hpriv
->flags
& AHCI_HFLAG_NO_SUSPEND
) {
535 dev_err(dev
, "firmware update required for suspend/resume\n");
540 * AHCI spec rev1.1 section 8.3.3:
541 * Software must disable interrupts prior to requesting a
542 * transition of the HBA to D3 state.
544 ctl
= readl(mmio
+ HOST_CTL
);
546 writel(ctl
, mmio
+ HOST_CTL
);
547 readl(mmio
+ HOST_CTL
); /* flush */
549 return ata_host_suspend(host
, PMSG_SUSPEND
);
551 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host
);
554 * ahci_platform_resume_host - Resume an ahci-platform host
555 * @dev: device pointer for the host
557 * This function does all the usual steps needed to resume an ahci-platform
558 * host, note any necessary resources (ie clks, phys, etc.) must be
559 * initialized / enabled before calling this.
562 * 0 on success otherwise a negative error code
564 int ahci_platform_resume_host(struct device
*dev
)
566 struct ata_host
*host
= dev_get_drvdata(dev
);
569 if (dev
->power
.power_state
.event
== PM_EVENT_SUSPEND
) {
570 rc
= ahci_reset_controller(host
);
574 ahci_init_controller(host
);
577 ata_host_resume(host
);
581 EXPORT_SYMBOL_GPL(ahci_platform_resume_host
);
584 * ahci_platform_suspend - Suspend an ahci-platform device
585 * @dev: the platform device to suspend
587 * This function suspends the host associated with the device, followed by
588 * disabling all the resources of the device.
591 * 0 on success otherwise a negative error code
593 int ahci_platform_suspend(struct device
*dev
)
595 struct ahci_platform_data
*pdata
= dev_get_platdata(dev
);
596 struct ata_host
*host
= dev_get_drvdata(dev
);
597 struct ahci_host_priv
*hpriv
= host
->private_data
;
600 rc
= ahci_platform_suspend_host(dev
);
604 if (pdata
&& pdata
->suspend
) {
605 rc
= pdata
->suspend(dev
);
610 ahci_platform_disable_resources(hpriv
);
615 ahci_platform_resume_host(dev
);
618 EXPORT_SYMBOL_GPL(ahci_platform_suspend
);
621 * ahci_platform_resume - Resume an ahci-platform device
622 * @dev: the platform device to resume
624 * This function enables all the resources of the device followed by
625 * resuming the host associated with the device.
628 * 0 on success otherwise a negative error code
630 int ahci_platform_resume(struct device
*dev
)
632 struct ahci_platform_data
*pdata
= dev_get_platdata(dev
);
633 struct ata_host
*host
= dev_get_drvdata(dev
);
634 struct ahci_host_priv
*hpriv
= host
->private_data
;
637 rc
= ahci_platform_enable_resources(hpriv
);
641 if (pdata
&& pdata
->resume
) {
642 rc
= pdata
->resume(dev
);
644 goto disable_resources
;
647 rc
= ahci_platform_resume_host(dev
);
649 goto disable_resources
;
651 /* We resumed so update PM runtime state */
652 pm_runtime_disable(dev
);
653 pm_runtime_set_active(dev
);
654 pm_runtime_enable(dev
);
659 ahci_platform_disable_resources(hpriv
);
663 EXPORT_SYMBOL_GPL(ahci_platform_resume
);
666 MODULE_DESCRIPTION("AHCI SATA platform library");
667 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
668 MODULE_LICENSE("GPL");