2 * AHCI SATA platform driver
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 const struct ata_port_info ahci_port_info = {
38 .flags = AHCI_FLAG_COMMON,
40 .udma_mask = ATA_UDMA6,
41 .port_ops = &ahci_platform_ops,
44 static struct scsi_host_template ahci_platform_sht = {
45 AHCI_SHT("ahci_platform"),
49 * ahci_platform_enable_clks - Enable platform clocks
50 * @hpriv: host private area to store config values
52 * This function enables all the clks found in hpriv->clks, starting at
53 * index 0. If any clk fails to enable it disables all the clks already
54 * enabled in reverse order, and then returns an error.
57 * 0 on success otherwise a negative error code
59 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
63 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
64 rc = clk_prepare_enable(hpriv->clks[c]);
66 goto disable_unprepare_clk;
70 disable_unprepare_clk:
72 clk_disable_unprepare(hpriv->clks[c]);
75 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
78 * ahci_platform_disable_clks - Disable platform clocks
79 * @hpriv: host private area to store config values
81 * This function disables all the clks found in hpriv->clks, in reverse
82 * order of ahci_platform_enable_clks (starting at the end of the array).
84 void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
88 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
90 clk_disable_unprepare(hpriv->clks[c]);
92 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
95 * ahci_platform_enable_resources - Enable platform resources
96 * @hpriv: host private area to store config values
98 * This function enables all ahci_platform managed resources in the
101 * 2) Clocks (through ahci_platform_enable_clks)
104 * If resource enabling fails at any point the previous enabled resources
105 * are disabled in reverse order.
108 * 0 on success otherwise a negative error code
110 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
114 if (hpriv->target_pwr) {
115 rc = regulator_enable(hpriv->target_pwr);
120 rc = ahci_platform_enable_clks(hpriv);
122 goto disable_regulator;
125 rc = phy_init(hpriv->phy);
129 rc = phy_power_on(hpriv->phy);
131 phy_exit(hpriv->phy);
139 ahci_platform_disable_clks(hpriv);
142 if (hpriv->target_pwr)
143 regulator_disable(hpriv->target_pwr);
146 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
149 * ahci_platform_disable_resources - Disable platform resources
150 * @hpriv: host private area to store config values
152 * This function disables all ahci_platform managed resources in the
155 * 2) Clocks (through ahci_platform_disable_clks)
158 void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
161 phy_power_off(hpriv->phy);
162 phy_exit(hpriv->phy);
165 ahci_platform_disable_clks(hpriv);
167 if (hpriv->target_pwr)
168 regulator_disable(hpriv->target_pwr);
170 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
172 static void ahci_platform_put_resources(struct device *dev, void *res)
174 struct ahci_host_priv *hpriv = res;
177 if (hpriv->got_runtime_pm) {
178 pm_runtime_put_sync(dev);
179 pm_runtime_disable(dev);
182 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
183 clk_put(hpriv->clks[c]);
187 * ahci_platform_get_resources - Get platform resources
188 * @pdev: platform device to get resources for
190 * This function allocates an ahci_host_priv struct, and gets the following
191 * resources, storing a reference to them inside the returned struct:
193 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
194 * 2) regulator for controlling the targets power (optional)
195 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
196 * or for non devicetree enabled platforms a single clock
200 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
202 struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
204 struct device *dev = &pdev->dev;
205 struct ahci_host_priv *hpriv;
209 if (!devres_open_group(dev, NULL, GFP_KERNEL))
210 return ERR_PTR(-ENOMEM);
212 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
217 devres_add(dev, hpriv);
219 hpriv->mmio = devm_ioremap_resource(dev,
220 platform_get_resource(pdev, IORESOURCE_MEM, 0));
221 if (IS_ERR(hpriv->mmio)) {
222 dev_err(dev, "no mmio space\n");
223 rc = PTR_ERR(hpriv->mmio);
227 hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
228 if (IS_ERR(hpriv->target_pwr)) {
229 rc = PTR_ERR(hpriv->target_pwr);
230 if (rc == -EPROBE_DEFER)
232 hpriv->target_pwr = NULL;
235 for (i = 0; i < AHCI_MAX_CLKS; i++) {
237 * For now we must use clk_get(dev, NULL) for the first clock,
238 * because some platforms (da850, spear13xx) are not yet
239 * converted to use devicetree for clocks. For new platforms
240 * this is equivalent to of_clk_get(dev->of_node, 0).
243 clk = clk_get(dev, NULL);
245 clk = of_clk_get(dev->of_node, i);
249 if (rc == -EPROBE_DEFER)
253 hpriv->clks[i] = clk;
256 hpriv->phy = devm_phy_get(dev, "sata-phy");
257 if (IS_ERR(hpriv->phy)) {
258 rc = PTR_ERR(hpriv->phy);
262 /* continue normally */
270 dev_err(dev, "couldn't get sata-phy\n");
275 pm_runtime_enable(dev);
276 pm_runtime_get_sync(dev);
277 hpriv->got_runtime_pm = true;
279 devres_remove_group(dev, NULL);
283 devres_release_group(dev, NULL);
286 EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
289 * ahci_platform_init_host - Bring up an ahci-platform host
290 * @pdev: platform device pointer for the host
291 * @hpriv: ahci-host private data for the host
292 * @pi_template: template for the ata_port_info to use
293 * @force_port_map: param passed to ahci_save_initial_config
294 * @mask_port_map: param passed to ahci_save_initial_config
296 * This function does all the usual steps needed to bring up an
297 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
298 * must be initialized / enabled before calling this.
301 * 0 on success otherwise a negative error code
303 int ahci_platform_init_host(struct platform_device *pdev,
304 struct ahci_host_priv *hpriv,
305 const struct ata_port_info *pi_template,
306 unsigned int force_port_map,
307 unsigned int mask_port_map)
309 struct device *dev = &pdev->dev;
310 struct ata_port_info pi = *pi_template;
311 const struct ata_port_info *ppi[] = { &pi, NULL };
312 struct ata_host *host;
313 int i, irq, n_ports, rc;
315 irq = platform_get_irq(pdev, 0);
317 dev_err(dev, "no irq\n");
322 hpriv->flags |= (unsigned long)pi.private_data;
324 ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
326 if (hpriv->cap & HOST_CAP_NCQ)
327 pi.flags |= ATA_FLAG_NCQ;
329 if (hpriv->cap & HOST_CAP_PMP)
330 pi.flags |= ATA_FLAG_PMP;
332 ahci_set_em_messages(hpriv, &pi);
334 /* CAP.NP sometimes indicate the index of the last enabled
335 * port, at other times, that of the last possible port, so
336 * determining the maximum port number requires looking at
337 * both CAP.NP and port_map.
339 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
341 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
345 host->private_data = hpriv;
347 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
348 host->flags |= ATA_HOST_PARALLEL_SCAN;
350 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
352 if (pi.flags & ATA_FLAG_EM)
355 for (i = 0; i < host->n_ports; i++) {
356 struct ata_port *ap = host->ports[i];
358 ata_port_desc(ap, "mmio %pR",
359 platform_get_resource(pdev, IORESOURCE_MEM, 0));
360 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
362 /* set enclosure management message type */
363 if (ap->flags & ATA_FLAG_EM)
364 ap->em_message_type = hpriv->em_msg_type;
366 /* disabled/not-implemented port */
367 if (!(hpriv->port_map & (1 << i)))
368 ap->ops = &ata_dummy_port_ops;
371 rc = ahci_reset_controller(host);
375 ahci_init_controller(host);
376 ahci_print_info(host, "platform");
378 return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
381 EXPORT_SYMBOL_GPL(ahci_platform_init_host);
383 static int ahci_probe(struct platform_device *pdev)
385 struct device *dev = &pdev->dev;
386 struct ahci_platform_data *pdata = dev_get_platdata(dev);
387 struct ahci_host_priv *hpriv;
390 hpriv = ahci_platform_get_resources(pdev);
392 return PTR_ERR(hpriv);
394 rc = ahci_platform_enable_resources(hpriv);
399 * Some platforms might need to prepare for mmio region access,
400 * which could be done in the following init call. So, the mmio
401 * region shouldn't be accessed before init (if provided) has
402 * returned successfully.
404 if (pdata && pdata->init) {
405 rc = pdata->init(dev, hpriv->mmio);
407 goto disable_resources;
410 rc = ahci_platform_init_host(pdev, hpriv, &ahci_port_info, 0, 0);
416 if (pdata && pdata->exit)
419 ahci_platform_disable_resources(hpriv);
423 static void ahci_host_stop(struct ata_host *host)
425 struct device *dev = host->dev;
426 struct ahci_platform_data *pdata = dev_get_platdata(dev);
427 struct ahci_host_priv *hpriv = host->private_data;
429 if (pdata && pdata->exit)
432 ahci_platform_disable_resources(hpriv);
435 #ifdef CONFIG_PM_SLEEP
437 * ahci_platform_suspend_host - Suspend an ahci-platform host
438 * @dev: device pointer for the host
440 * This function does all the usual steps needed to suspend an
441 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
442 * must be disabled after calling this.
445 * 0 on success otherwise a negative error code
447 int ahci_platform_suspend_host(struct device *dev)
449 struct ata_host *host = dev_get_drvdata(dev);
450 struct ahci_host_priv *hpriv = host->private_data;
451 void __iomem *mmio = hpriv->mmio;
454 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
455 dev_err(dev, "firmware update required for suspend/resume\n");
460 * AHCI spec rev1.1 section 8.3.3:
461 * Software must disable interrupts prior to requesting a
462 * transition of the HBA to D3 state.
464 ctl = readl(mmio + HOST_CTL);
466 writel(ctl, mmio + HOST_CTL);
467 readl(mmio + HOST_CTL); /* flush */
469 return ata_host_suspend(host, PMSG_SUSPEND);
471 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
474 * ahci_platform_resume_host - Resume an ahci-platform host
475 * @dev: device pointer for the host
477 * This function does all the usual steps needed to resume an ahci-platform
478 * host, note any necessary resources (ie clks, phy, etc.) must be
479 * initialized / enabled before calling this.
482 * 0 on success otherwise a negative error code
484 int ahci_platform_resume_host(struct device *dev)
486 struct ata_host *host = dev_get_drvdata(dev);
489 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
490 rc = ahci_reset_controller(host);
494 ahci_init_controller(host);
497 ata_host_resume(host);
501 EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
504 * ahci_platform_suspend - Suspend an ahci-platform device
505 * @dev: the platform device to suspend
507 * This function suspends the host associated with the device, followed by
508 * disabling all the resources of the device.
511 * 0 on success otherwise a negative error code
513 int ahci_platform_suspend(struct device *dev)
515 struct ahci_platform_data *pdata = dev_get_platdata(dev);
516 struct ata_host *host = dev_get_drvdata(dev);
517 struct ahci_host_priv *hpriv = host->private_data;
520 rc = ahci_platform_suspend_host(dev);
524 if (pdata && pdata->suspend)
525 return pdata->suspend(dev);
527 ahci_platform_disable_resources(hpriv);
531 EXPORT_SYMBOL_GPL(ahci_platform_suspend);
534 * ahci_platform_resume - Resume an ahci-platform device
535 * @dev: the platform device to resume
537 * This function enables all the resources of the device followed by
538 * resuming the host associated with the device.
541 * 0 on success otherwise a negative error code
543 int ahci_platform_resume(struct device *dev)
545 struct ahci_platform_data *pdata = dev_get_platdata(dev);
546 struct ata_host *host = dev_get_drvdata(dev);
547 struct ahci_host_priv *hpriv = host->private_data;
550 rc = ahci_platform_enable_resources(hpriv);
554 if (pdata && pdata->resume) {
555 rc = pdata->resume(dev);
557 goto disable_resources;
560 rc = ahci_platform_resume_host(dev);
562 goto disable_resources;
564 /* We resumed so update PM runtime state */
565 pm_runtime_disable(dev);
566 pm_runtime_set_active(dev);
567 pm_runtime_enable(dev);
572 ahci_platform_disable_resources(hpriv);
576 EXPORT_SYMBOL_GPL(ahci_platform_resume);
579 static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
580 ahci_platform_resume);
582 static const struct of_device_id ahci_of_match[] = {
583 { .compatible = "snps,spear-ahci", },
584 { .compatible = "snps,exynos5440-ahci", },
585 { .compatible = "ibm,476gtr-ahci", },
586 { .compatible = "snps,dwc-ahci", },
589 MODULE_DEVICE_TABLE(of, ahci_of_match);
591 static struct platform_driver ahci_driver = {
593 .remove = ata_platform_remove_one,
596 .owner = THIS_MODULE,
597 .of_match_table = ahci_of_match,
601 module_platform_driver(ahci_driver);
603 MODULE_DESCRIPTION("AHCI SATA platform driver");
604 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
605 MODULE_LICENSE("GPL");
606 MODULE_ALIAS("platform:ahci");