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_clks - Enable platform clocks
43 * @hpriv: host private area to store config values
45 * This function enables all the clks found in hpriv->clks, starting at
46 * index 0. If any clk fails to enable it disables all the clks already
47 * enabled in reverse order, and then returns an error.
50 * 0 on success otherwise a negative error code
52 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
56 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
57 rc = clk_prepare_enable(hpriv->clks[c]);
59 goto disable_unprepare_clk;
63 disable_unprepare_clk:
65 clk_disable_unprepare(hpriv->clks[c]);
68 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
71 * ahci_platform_disable_clks - Disable platform clocks
72 * @hpriv: host private area to store config values
74 * This function disables all the clks found in hpriv->clks, in reverse
75 * order of ahci_platform_enable_clks (starting at the end of the array).
77 void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
81 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
83 clk_disable_unprepare(hpriv->clks[c]);
85 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
88 * ahci_platform_enable_resources - Enable platform resources
89 * @hpriv: host private area to store config values
91 * This function enables all ahci_platform managed resources in the
94 * 2) Clocks (through ahci_platform_enable_clks)
97 * If resource enabling fails at any point the previous enabled resources
98 * are disabled in reverse order.
101 * 0 on success otherwise a negative error code
103 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
107 if (hpriv->target_pwr) {
108 rc = regulator_enable(hpriv->target_pwr);
113 rc = ahci_platform_enable_clks(hpriv);
115 goto disable_regulator;
118 rc = phy_init(hpriv->phy);
122 rc = phy_power_on(hpriv->phy);
124 phy_exit(hpriv->phy);
132 ahci_platform_disable_clks(hpriv);
135 if (hpriv->target_pwr)
136 regulator_disable(hpriv->target_pwr);
139 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
142 * ahci_platform_disable_resources - Disable platform resources
143 * @hpriv: host private area to store config values
145 * This function disables all ahci_platform managed resources in the
148 * 2) Clocks (through ahci_platform_disable_clks)
151 void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
154 phy_power_off(hpriv->phy);
155 phy_exit(hpriv->phy);
158 ahci_platform_disable_clks(hpriv);
160 if (hpriv->target_pwr)
161 regulator_disable(hpriv->target_pwr);
163 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
165 static void ahci_platform_put_resources(struct device *dev, void *res)
167 struct ahci_host_priv *hpriv = res;
170 if (hpriv->got_runtime_pm) {
171 pm_runtime_put_sync(dev);
172 pm_runtime_disable(dev);
175 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
176 clk_put(hpriv->clks[c]);
180 * ahci_platform_get_resources - Get platform resources
181 * @pdev: platform device to get resources for
183 * This function allocates an ahci_host_priv struct, and gets the following
184 * resources, storing a reference to them inside the returned struct:
186 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
187 * 2) regulator for controlling the targets power (optional)
188 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
189 * or for non devicetree enabled platforms a single clock
193 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
195 struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
197 struct device *dev = &pdev->dev;
198 struct ahci_host_priv *hpriv;
202 if (!devres_open_group(dev, NULL, GFP_KERNEL))
203 return ERR_PTR(-ENOMEM);
205 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
210 devres_add(dev, hpriv);
212 hpriv->mmio = devm_ioremap_resource(dev,
213 platform_get_resource(pdev, IORESOURCE_MEM, 0));
214 if (IS_ERR(hpriv->mmio)) {
215 dev_err(dev, "no mmio space\n");
216 rc = PTR_ERR(hpriv->mmio);
220 hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
221 if (IS_ERR(hpriv->target_pwr)) {
222 rc = PTR_ERR(hpriv->target_pwr);
223 if (rc == -EPROBE_DEFER)
225 hpriv->target_pwr = NULL;
228 for (i = 0; i < AHCI_MAX_CLKS; i++) {
230 * For now we must use clk_get(dev, NULL) for the first clock,
231 * because some platforms (da850, spear13xx) are not yet
232 * converted to use devicetree for clocks. For new platforms
233 * this is equivalent to of_clk_get(dev->of_node, 0).
236 clk = clk_get(dev, NULL);
238 clk = of_clk_get(dev->of_node, i);
242 if (rc == -EPROBE_DEFER)
246 hpriv->clks[i] = clk;
249 hpriv->phy = devm_phy_get(dev, "sata-phy");
250 if (IS_ERR(hpriv->phy)) {
251 rc = PTR_ERR(hpriv->phy);
255 /* continue normally */
263 dev_err(dev, "couldn't get sata-phy\n");
268 pm_runtime_enable(dev);
269 pm_runtime_get_sync(dev);
270 hpriv->got_runtime_pm = true;
272 devres_remove_group(dev, NULL);
276 devres_release_group(dev, NULL);
279 EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
282 * ahci_platform_init_host - Bring up an ahci-platform host
283 * @pdev: platform device pointer for the host
284 * @hpriv: ahci-host private data for the host
285 * @pi_template: template for the ata_port_info to use
286 * @host_flags: ahci host flags used in ahci_host_priv
287 * @force_port_map: param passed to ahci_save_initial_config
288 * @mask_port_map: param passed to ahci_save_initial_config
290 * This function does all the usual steps needed to bring up an
291 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
292 * must be initialized / enabled before calling this.
295 * 0 on success otherwise a negative error code
297 int ahci_platform_init_host(struct platform_device *pdev,
298 struct ahci_host_priv *hpriv,
299 const struct ata_port_info *pi_template,
300 unsigned long host_flags,
301 unsigned int force_port_map,
302 unsigned int mask_port_map)
304 struct device *dev = &pdev->dev;
305 struct ata_port_info pi = *pi_template;
306 const struct ata_port_info *ppi[] = { &pi, NULL };
307 struct ata_host *host;
308 int i, irq, n_ports, rc;
310 irq = platform_get_irq(pdev, 0);
312 dev_err(dev, "no irq\n");
317 pi.private_data = (void *)host_flags;
318 hpriv->flags |= host_flags;
320 ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
322 if (hpriv->cap & HOST_CAP_NCQ)
323 pi.flags |= ATA_FLAG_NCQ;
325 if (hpriv->cap & HOST_CAP_PMP)
326 pi.flags |= ATA_FLAG_PMP;
328 ahci_set_em_messages(hpriv, &pi);
330 /* CAP.NP sometimes indicate the index of the last enabled
331 * port, at other times, that of the last possible port, so
332 * determining the maximum port number requires looking at
333 * both CAP.NP and port_map.
335 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
337 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
341 host->private_data = hpriv;
343 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
344 host->flags |= ATA_HOST_PARALLEL_SCAN;
346 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
348 if (pi.flags & ATA_FLAG_EM)
351 for (i = 0; i < host->n_ports; i++) {
352 struct ata_port *ap = host->ports[i];
354 ata_port_desc(ap, "mmio %pR",
355 platform_get_resource(pdev, IORESOURCE_MEM, 0));
356 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
358 /* set enclosure management message type */
359 if (ap->flags & ATA_FLAG_EM)
360 ap->em_message_type = hpriv->em_msg_type;
362 /* disabled/not-implemented port */
363 if (!(hpriv->port_map & (1 << i)))
364 ap->ops = &ata_dummy_port_ops;
367 if (hpriv->cap & HOST_CAP_64) {
368 rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
370 rc = dma_coerce_mask_and_coherent(dev,
373 dev_err(dev, "Failed to enable 64-bit DMA.\n");
376 dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
380 rc = ahci_reset_controller(host);
384 ahci_init_controller(host);
385 ahci_print_info(host, "platform");
387 return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
390 EXPORT_SYMBOL_GPL(ahci_platform_init_host);
392 static void ahci_host_stop(struct ata_host *host)
394 struct device *dev = host->dev;
395 struct ahci_platform_data *pdata = dev_get_platdata(dev);
396 struct ahci_host_priv *hpriv = host->private_data;
398 if (pdata && pdata->exit)
401 ahci_platform_disable_resources(hpriv);
404 #ifdef CONFIG_PM_SLEEP
406 * ahci_platform_suspend_host - Suspend an ahci-platform host
407 * @dev: device pointer for the host
409 * This function does all the usual steps needed to suspend an
410 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
411 * must be disabled after calling this.
414 * 0 on success otherwise a negative error code
416 int ahci_platform_suspend_host(struct device *dev)
418 struct ata_host *host = dev_get_drvdata(dev);
419 struct ahci_host_priv *hpriv = host->private_data;
420 void __iomem *mmio = hpriv->mmio;
423 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
424 dev_err(dev, "firmware update required for suspend/resume\n");
429 * AHCI spec rev1.1 section 8.3.3:
430 * Software must disable interrupts prior to requesting a
431 * transition of the HBA to D3 state.
433 ctl = readl(mmio + HOST_CTL);
435 writel(ctl, mmio + HOST_CTL);
436 readl(mmio + HOST_CTL); /* flush */
438 return ata_host_suspend(host, PMSG_SUSPEND);
440 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
443 * ahci_platform_resume_host - Resume an ahci-platform host
444 * @dev: device pointer for the host
446 * This function does all the usual steps needed to resume an ahci-platform
447 * host, note any necessary resources (ie clks, phy, etc.) must be
448 * initialized / enabled before calling this.
451 * 0 on success otherwise a negative error code
453 int ahci_platform_resume_host(struct device *dev)
455 struct ata_host *host = dev_get_drvdata(dev);
458 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
459 rc = ahci_reset_controller(host);
463 ahci_init_controller(host);
466 ata_host_resume(host);
470 EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
473 * ahci_platform_suspend - Suspend an ahci-platform device
474 * @dev: the platform device to suspend
476 * This function suspends the host associated with the device, followed by
477 * disabling all the resources of the device.
480 * 0 on success otherwise a negative error code
482 int ahci_platform_suspend(struct device *dev)
484 struct ahci_platform_data *pdata = dev_get_platdata(dev);
485 struct ata_host *host = dev_get_drvdata(dev);
486 struct ahci_host_priv *hpriv = host->private_data;
489 rc = ahci_platform_suspend_host(dev);
493 if (pdata && pdata->suspend) {
494 rc = pdata->suspend(dev);
499 ahci_platform_disable_resources(hpriv);
504 ahci_platform_resume_host(dev);
507 EXPORT_SYMBOL_GPL(ahci_platform_suspend);
510 * ahci_platform_resume - Resume an ahci-platform device
511 * @dev: the platform device to resume
513 * This function enables all the resources of the device followed by
514 * resuming the host associated with the device.
517 * 0 on success otherwise a negative error code
519 int ahci_platform_resume(struct device *dev)
521 struct ahci_platform_data *pdata = dev_get_platdata(dev);
522 struct ata_host *host = dev_get_drvdata(dev);
523 struct ahci_host_priv *hpriv = host->private_data;
526 rc = ahci_platform_enable_resources(hpriv);
530 if (pdata && pdata->resume) {
531 rc = pdata->resume(dev);
533 goto disable_resources;
536 rc = ahci_platform_resume_host(dev);
538 goto disable_resources;
540 /* We resumed so update PM runtime state */
541 pm_runtime_disable(dev);
542 pm_runtime_set_active(dev);
543 pm_runtime_enable(dev);
548 ahci_platform_disable_resources(hpriv);
552 EXPORT_SYMBOL_GPL(ahci_platform_resume);
555 MODULE_DESCRIPTION("AHCI SATA platform library");
556 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
557 MODULE_LICENSE("GPL");