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 static 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
]);
82 * ahci_platform_disable_phys - Disable PHYs
83 * @hpriv: host private area to store config values
85 * This function disables all PHYs found in hpriv->phys.
87 static void ahci_platform_disable_phys(struct ahci_host_priv
*hpriv
)
91 for (i
= 0; i
< hpriv
->nports
; i
++) {
95 phy_power_off(hpriv
->phys
[i
]);
96 phy_exit(hpriv
->phys
[i
]);
101 * ahci_platform_enable_clks - Enable platform clocks
102 * @hpriv: host private area to store config values
104 * This function enables all the clks found in hpriv->clks, starting at
105 * index 0. If any clk fails to enable it disables all the clks already
106 * enabled in reverse order, and then returns an error.
109 * 0 on success otherwise a negative error code
111 int ahci_platform_enable_clks(struct ahci_host_priv
*hpriv
)
115 for (c
= 0; c
< AHCI_MAX_CLKS
&& hpriv
->clks
[c
]; c
++) {
116 rc
= clk_prepare_enable(hpriv
->clks
[c
]);
118 goto disable_unprepare_clk
;
122 disable_unprepare_clk
:
124 clk_disable_unprepare(hpriv
->clks
[c
]);
127 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks
);
130 * ahci_platform_disable_clks - Disable platform clocks
131 * @hpriv: host private area to store config values
133 * This function disables all the clks found in hpriv->clks, in reverse
134 * order of ahci_platform_enable_clks (starting at the end of the array).
136 void ahci_platform_disable_clks(struct ahci_host_priv
*hpriv
)
140 for (c
= AHCI_MAX_CLKS
- 1; c
>= 0; c
--)
142 clk_disable_unprepare(hpriv
->clks
[c
]);
144 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks
);
147 * ahci_platform_enable_resources - Enable platform resources
148 * @hpriv: host private area to store config values
150 * This function enables all ahci_platform managed resources in the
153 * 2) Clocks (through ahci_platform_enable_clks)
156 * If resource enabling fails at any point the previous enabled resources
157 * are disabled in reverse order.
160 * 0 on success otherwise a negative error code
162 int ahci_platform_enable_resources(struct ahci_host_priv
*hpriv
)
166 if (hpriv
->target_pwr
) {
167 rc
= regulator_enable(hpriv
->target_pwr
);
172 rc
= ahci_platform_enable_clks(hpriv
);
174 goto disable_regulator
;
176 rc
= ahci_platform_enable_phys(hpriv
);
183 ahci_platform_disable_clks(hpriv
);
186 if (hpriv
->target_pwr
)
187 regulator_disable(hpriv
->target_pwr
);
190 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources
);
193 * ahci_platform_disable_resources - Disable platform resources
194 * @hpriv: host private area to store config values
196 * This function disables all ahci_platform managed resources in the
199 * 2) Clocks (through ahci_platform_disable_clks)
202 void ahci_platform_disable_resources(struct ahci_host_priv
*hpriv
)
204 ahci_platform_disable_phys(hpriv
);
206 ahci_platform_disable_clks(hpriv
);
208 if (hpriv
->target_pwr
)
209 regulator_disable(hpriv
->target_pwr
);
211 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources
);
213 static void ahci_platform_put_resources(struct device
*dev
, void *res
)
215 struct ahci_host_priv
*hpriv
= res
;
218 if (hpriv
->got_runtime_pm
) {
219 pm_runtime_put_sync(dev
);
220 pm_runtime_disable(dev
);
223 for (c
= 0; c
< AHCI_MAX_CLKS
&& hpriv
->clks
[c
]; c
++)
224 clk_put(hpriv
->clks
[c
]);
228 * ahci_platform_get_resources - Get platform resources
229 * @pdev: platform device to get resources for
231 * This function allocates an ahci_host_priv struct, and gets the following
232 * resources, storing a reference to them inside the returned struct:
234 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
235 * 2) regulator for controlling the targets power (optional)
236 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
237 * or for non devicetree enabled platforms a single clock
241 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
243 struct ahci_host_priv
*ahci_platform_get_resources(struct platform_device
*pdev
)
245 struct device
*dev
= &pdev
->dev
;
246 struct ahci_host_priv
*hpriv
;
248 struct device_node
*child
;
249 int i
, enabled_ports
= 0, rc
= -ENOMEM
;
250 u32 mask_port_map
= 0;
252 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
253 return ERR_PTR(-ENOMEM
);
255 hpriv
= devres_alloc(ahci_platform_put_resources
, sizeof(*hpriv
),
260 devres_add(dev
, hpriv
);
262 hpriv
->mmio
= devm_ioremap_resource(dev
,
263 platform_get_resource(pdev
, IORESOURCE_MEM
, 0));
264 if (IS_ERR(hpriv
->mmio
)) {
265 dev_err(dev
, "no mmio space\n");
266 rc
= PTR_ERR(hpriv
->mmio
);
270 hpriv
->target_pwr
= devm_regulator_get_optional(dev
, "target");
271 if (IS_ERR(hpriv
->target_pwr
)) {
272 rc
= PTR_ERR(hpriv
->target_pwr
);
273 if (rc
== -EPROBE_DEFER
)
275 hpriv
->target_pwr
= NULL
;
278 for (i
= 0; i
< AHCI_MAX_CLKS
; i
++) {
280 * For now we must use clk_get(dev, NULL) for the first clock,
281 * because some platforms (da850, spear13xx) are not yet
282 * converted to use devicetree for clocks. For new platforms
283 * this is equivalent to of_clk_get(dev->of_node, 0).
286 clk
= clk_get(dev
, NULL
);
288 clk
= of_clk_get(dev
->of_node
, i
);
292 if (rc
== -EPROBE_DEFER
)
296 hpriv
->clks
[i
] = clk
;
299 hpriv
->nports
= of_get_child_count(dev
->of_node
);
302 hpriv
->phys
= devm_kzalloc(dev
,
303 hpriv
->nports
* sizeof(*hpriv
->phys
),
310 for_each_child_of_node(dev
->of_node
, child
) {
313 if (!of_device_is_available(child
))
316 if (of_property_read_u32(child
, "reg", &port
)) {
321 if (port
>= hpriv
->nports
) {
322 dev_warn(dev
, "invalid port number %d\n", port
);
326 mask_port_map
|= BIT(port
);
328 hpriv
->phys
[port
] = devm_of_phy_get(dev
, child
, NULL
);
329 if (IS_ERR(hpriv
->phys
[port
])) {
330 rc
= PTR_ERR(hpriv
->phys
[port
]);
332 "couldn't get PHY in node %s: %d\n",
339 if (!enabled_ports
) {
340 dev_warn(dev
, "No port enabled\n");
345 if (!hpriv
->mask_port_map
)
346 hpriv
->mask_port_map
= mask_port_map
;
349 * If no sub-node was found, keep this for device tree
352 struct phy
*phy
= devm_phy_get(dev
, "sata-phy");
354 hpriv
->phys
= devm_kzalloc(dev
, sizeof(*hpriv
->phys
),
361 hpriv
->phys
[0] = phy
;
367 /* No PHY support. Check if PHY is required. */
368 if (of_find_property(dev
->of_node
, "phys", NULL
)) {
369 dev_err(dev
, "couldn't get sata-phy: ENOSYS\n");
373 /* continue normally */
384 pm_runtime_enable(dev
);
385 pm_runtime_get_sync(dev
);
386 hpriv
->got_runtime_pm
= true;
388 devres_remove_group(dev
, NULL
);
392 devres_release_group(dev
, NULL
);
395 EXPORT_SYMBOL_GPL(ahci_platform_get_resources
);
398 * ahci_platform_init_host - Bring up an ahci-platform host
399 * @pdev: platform device pointer for the host
400 * @hpriv: ahci-host private data for the host
401 * @pi_template: template for the ata_port_info to use
403 * This function does all the usual steps needed to bring up an
404 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
405 * must be initialized / enabled before calling this.
408 * 0 on success otherwise a negative error code
410 int ahci_platform_init_host(struct platform_device
*pdev
,
411 struct ahci_host_priv
*hpriv
,
412 const struct ata_port_info
*pi_template
)
414 struct device
*dev
= &pdev
->dev
;
415 struct ata_port_info pi
= *pi_template
;
416 const struct ata_port_info
*ppi
[] = { &pi
, NULL
};
417 struct ata_host
*host
;
418 int i
, irq
, n_ports
, rc
;
420 irq
= platform_get_irq(pdev
, 0);
422 dev_err(dev
, "no irq\n");
427 pi
.private_data
= (void *)(unsigned long)hpriv
->flags
;
429 ahci_save_initial_config(dev
, hpriv
);
431 if (hpriv
->cap
& HOST_CAP_NCQ
)
432 pi
.flags
|= ATA_FLAG_NCQ
;
434 if (hpriv
->cap
& HOST_CAP_PMP
)
435 pi
.flags
|= ATA_FLAG_PMP
;
437 ahci_set_em_messages(hpriv
, &pi
);
439 /* CAP.NP sometimes indicate the index of the last enabled
440 * port, at other times, that of the last possible port, so
441 * determining the maximum port number requires looking at
442 * both CAP.NP and port_map.
444 n_ports
= max(ahci_nr_ports(hpriv
->cap
), fls(hpriv
->port_map
));
446 host
= ata_host_alloc_pinfo(dev
, ppi
, n_ports
);
450 host
->private_data
= hpriv
;
452 if (!(hpriv
->cap
& HOST_CAP_SSS
) || ahci_ignore_sss
)
453 host
->flags
|= ATA_HOST_PARALLEL_SCAN
;
455 dev_info(dev
, "SSS flag set, parallel bus scan disabled\n");
457 if (pi
.flags
& ATA_FLAG_EM
)
460 for (i
= 0; i
< host
->n_ports
; i
++) {
461 struct ata_port
*ap
= host
->ports
[i
];
463 ata_port_desc(ap
, "mmio %pR",
464 platform_get_resource(pdev
, IORESOURCE_MEM
, 0));
465 ata_port_desc(ap
, "port 0x%x", 0x100 + ap
->port_no
* 0x80);
467 /* set enclosure management message type */
468 if (ap
->flags
& ATA_FLAG_EM
)
469 ap
->em_message_type
= hpriv
->em_msg_type
;
471 /* disabled/not-implemented port */
472 if (!(hpriv
->port_map
& (1 << i
)))
473 ap
->ops
= &ata_dummy_port_ops
;
476 if (hpriv
->cap
& HOST_CAP_64
) {
477 rc
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(64));
479 rc
= dma_coerce_mask_and_coherent(dev
,
482 dev_err(dev
, "Failed to enable 64-bit DMA.\n");
485 dev_warn(dev
, "Enable 32-bit DMA instead of 64-bit.\n");
489 rc
= ahci_reset_controller(host
);
493 ahci_init_controller(host
);
494 ahci_print_info(host
, "platform");
496 return ahci_host_activate(host
, irq
, &ahci_platform_sht
);
498 EXPORT_SYMBOL_GPL(ahci_platform_init_host
);
500 static void ahci_host_stop(struct ata_host
*host
)
502 struct ahci_host_priv
*hpriv
= host
->private_data
;
504 ahci_platform_disable_resources(hpriv
);
507 #ifdef CONFIG_PM_SLEEP
509 * ahci_platform_suspend_host - Suspend an ahci-platform host
510 * @dev: device pointer for the host
512 * This function does all the usual steps needed to suspend an
513 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
514 * must be disabled after calling this.
517 * 0 on success otherwise a negative error code
519 int ahci_platform_suspend_host(struct device
*dev
)
521 struct ata_host
*host
= dev_get_drvdata(dev
);
522 struct ahci_host_priv
*hpriv
= host
->private_data
;
523 void __iomem
*mmio
= hpriv
->mmio
;
526 if (hpriv
->flags
& AHCI_HFLAG_NO_SUSPEND
) {
527 dev_err(dev
, "firmware update required for suspend/resume\n");
532 * AHCI spec rev1.1 section 8.3.3:
533 * Software must disable interrupts prior to requesting a
534 * transition of the HBA to D3 state.
536 ctl
= readl(mmio
+ HOST_CTL
);
538 writel(ctl
, mmio
+ HOST_CTL
);
539 readl(mmio
+ HOST_CTL
); /* flush */
541 return ata_host_suspend(host
, PMSG_SUSPEND
);
543 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host
);
546 * ahci_platform_resume_host - Resume an ahci-platform host
547 * @dev: device pointer for the host
549 * This function does all the usual steps needed to resume an ahci-platform
550 * host, note any necessary resources (ie clks, phys, etc.) must be
551 * initialized / enabled before calling this.
554 * 0 on success otherwise a negative error code
556 int ahci_platform_resume_host(struct device
*dev
)
558 struct ata_host
*host
= dev_get_drvdata(dev
);
561 if (dev
->power
.power_state
.event
== PM_EVENT_SUSPEND
) {
562 rc
= ahci_reset_controller(host
);
566 ahci_init_controller(host
);
569 ata_host_resume(host
);
573 EXPORT_SYMBOL_GPL(ahci_platform_resume_host
);
576 * ahci_platform_suspend - Suspend an ahci-platform device
577 * @dev: the platform device to suspend
579 * This function suspends the host associated with the device, followed by
580 * disabling all the resources of the device.
583 * 0 on success otherwise a negative error code
585 int ahci_platform_suspend(struct device
*dev
)
587 struct ata_host
*host
= dev_get_drvdata(dev
);
588 struct ahci_host_priv
*hpriv
= host
->private_data
;
591 rc
= ahci_platform_suspend_host(dev
);
595 ahci_platform_disable_resources(hpriv
);
599 EXPORT_SYMBOL_GPL(ahci_platform_suspend
);
602 * ahci_platform_resume - Resume an ahci-platform device
603 * @dev: the platform device to resume
605 * This function enables all the resources of the device followed by
606 * resuming the host associated with the device.
609 * 0 on success otherwise a negative error code
611 int ahci_platform_resume(struct device
*dev
)
613 struct ata_host
*host
= dev_get_drvdata(dev
);
614 struct ahci_host_priv
*hpriv
= host
->private_data
;
617 rc
= ahci_platform_enable_resources(hpriv
);
621 rc
= ahci_platform_resume_host(dev
);
623 goto disable_resources
;
625 /* We resumed so update PM runtime state */
626 pm_runtime_disable(dev
);
627 pm_runtime_set_active(dev
);
628 pm_runtime_enable(dev
);
633 ahci_platform_disable_resources(hpriv
);
637 EXPORT_SYMBOL_GPL(ahci_platform_resume
);
640 MODULE_DESCRIPTION("AHCI SATA platform library");
641 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
642 MODULE_LICENSE("GPL");