openvswitch: actions: use skb_postpull_rcsum when possible
[linux-2.6/btrfs-unstable.git] / drivers / ata / libahci_platform.c
blob0b03f90566924182e4c0411a798c66837d4530ac
1 /*
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)
12 * any later version.
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/module.h>
19 #include <linux/pm.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>
27 #include "ahci.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"),
41 /**
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.
49 * RETURNS:
50 * 0 on success otherwise a negative error code
52 static int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
54 int rc, i;
56 for (i = 0; i < hpriv->nports; i++) {
57 if (!hpriv->phys[i])
58 continue;
60 rc = phy_init(hpriv->phys[i]);
61 if (rc)
62 goto disable_phys;
64 rc = phy_power_on(hpriv->phys[i]);
65 if (rc) {
66 phy_exit(hpriv->phys[i]);
67 goto disable_phys;
71 return 0;
73 disable_phys:
74 while (--i >= 0) {
75 phy_power_off(hpriv->phys[i]);
76 phy_exit(hpriv->phys[i]);
78 return rc;
81 /**
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)
89 int i;
91 for (i = 0; i < hpriv->nports; i++) {
92 if (!hpriv->phys[i])
93 continue;
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.
108 * RETURNS:
109 * 0 on success otherwise a negative error code
111 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
113 int c, rc;
115 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
116 rc = clk_prepare_enable(hpriv->clks[c]);
117 if (rc)
118 goto disable_unprepare_clk;
120 return 0;
122 disable_unprepare_clk:
123 while (--c >= 0)
124 clk_disable_unprepare(hpriv->clks[c]);
125 return rc;
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)
138 int c;
140 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
141 if (hpriv->clks[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
151 * following order:
152 * 1) Regulator
153 * 2) Clocks (through ahci_platform_enable_clks)
154 * 3) Phys
156 * If resource enabling fails at any point the previous enabled resources
157 * are disabled in reverse order.
159 * RETURNS:
160 * 0 on success otherwise a negative error code
162 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
164 int rc;
166 if (hpriv->target_pwr) {
167 rc = regulator_enable(hpriv->target_pwr);
168 if (rc)
169 return rc;
172 rc = ahci_platform_enable_clks(hpriv);
173 if (rc)
174 goto disable_regulator;
176 rc = ahci_platform_enable_phys(hpriv);
177 if (rc)
178 goto disable_clks;
180 return 0;
182 disable_clks:
183 ahci_platform_disable_clks(hpriv);
185 disable_regulator:
186 if (hpriv->target_pwr)
187 regulator_disable(hpriv->target_pwr);
188 return rc;
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
197 * following order:
198 * 1) Phys
199 * 2) Clocks (through ahci_platform_disable_clks)
200 * 3) Regulator
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;
216 int c;
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
238 * 4) phys (optional)
240 * RETURNS:
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;
247 struct clk *clk;
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),
256 GFP_KERNEL);
257 if (!hpriv)
258 goto err_out;
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);
267 goto err_out;
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)
274 goto err_out;
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).
285 if (i == 0)
286 clk = clk_get(dev, NULL);
287 else
288 clk = of_clk_get(dev->of_node, i);
290 if (IS_ERR(clk)) {
291 rc = PTR_ERR(clk);
292 if (rc == -EPROBE_DEFER)
293 goto err_out;
294 break;
296 hpriv->clks[i] = clk;
299 hpriv->nports = of_get_child_count(dev->of_node);
301 if (hpriv->nports) {
302 hpriv->phys = devm_kzalloc(dev,
303 hpriv->nports * sizeof(*hpriv->phys),
304 GFP_KERNEL);
305 if (!hpriv->phys) {
306 rc = -ENOMEM;
307 goto err_out;
310 for_each_child_of_node(dev->of_node, child) {
311 u32 port;
313 if (!of_device_is_available(child))
314 continue;
316 if (of_property_read_u32(child, "reg", &port)) {
317 rc = -EINVAL;
318 goto err_out;
321 if (port >= hpriv->nports) {
322 dev_warn(dev, "invalid port number %d\n", port);
323 continue;
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]);
331 dev_err(dev,
332 "couldn't get PHY in node %s: %d\n",
333 child->name, rc);
334 goto err_out;
337 enabled_ports++;
339 if (!enabled_ports) {
340 dev_warn(dev, "No port enabled\n");
341 rc = -ENODEV;
342 goto err_out;
345 if (!hpriv->mask_port_map)
346 hpriv->mask_port_map = mask_port_map;
347 } else {
349 * If no sub-node was found, keep this for device tree
350 * compatibility
352 struct phy *phy = devm_phy_get(dev, "sata-phy");
353 if (!IS_ERR(phy)) {
354 hpriv->phys = devm_kzalloc(dev, sizeof(*hpriv->phys),
355 GFP_KERNEL);
356 if (!hpriv->phys) {
357 rc = -ENOMEM;
358 goto err_out;
361 hpriv->phys[0] = phy;
362 hpriv->nports = 1;
363 } else {
364 rc = PTR_ERR(phy);
365 switch (rc) {
366 case -ENOSYS:
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");
370 goto err_out;
372 case -ENODEV:
373 /* continue normally */
374 hpriv->phys = NULL;
375 break;
377 default:
378 goto err_out;
384 pm_runtime_enable(dev);
385 pm_runtime_get_sync(dev);
386 hpriv->got_runtime_pm = true;
388 devres_remove_group(dev, NULL);
389 return hpriv;
391 err_out:
392 devres_release_group(dev, NULL);
393 return ERR_PTR(rc);
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.
407 * RETURNS:
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);
421 if (irq <= 0) {
422 dev_err(dev, "no irq\n");
423 return -EINVAL;
426 /* prepare host */
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);
447 if (!host)
448 return -ENOMEM;
450 host->private_data = hpriv;
452 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
453 host->flags |= ATA_HOST_PARALLEL_SCAN;
454 else
455 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
457 if (pi.flags & ATA_FLAG_EM)
458 ahci_reset_em(host);
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));
478 if (rc) {
479 rc = dma_coerce_mask_and_coherent(dev,
480 DMA_BIT_MASK(32));
481 if (rc) {
482 dev_err(dev, "Failed to enable 64-bit DMA.\n");
483 return rc;
485 dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
489 rc = ahci_reset_controller(host);
490 if (rc)
491 return rc;
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.
516 * RETURNS:
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;
524 u32 ctl;
526 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
527 dev_err(dev, "firmware update required for suspend/resume\n");
528 return -EIO;
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);
537 ctl &= ~HOST_IRQ_EN;
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.
553 * RETURNS:
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);
559 int rc;
561 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
562 rc = ahci_reset_controller(host);
563 if (rc)
564 return rc;
566 ahci_init_controller(host);
569 ata_host_resume(host);
571 return 0;
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.
582 * RETURNS:
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;
589 int rc;
591 rc = ahci_platform_suspend_host(dev);
592 if (rc)
593 return rc;
595 ahci_platform_disable_resources(hpriv);
597 return 0;
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.
608 * RETURNS:
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;
615 int rc;
617 rc = ahci_platform_enable_resources(hpriv);
618 if (rc)
619 return rc;
621 rc = ahci_platform_resume_host(dev);
622 if (rc)
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);
630 return 0;
632 disable_resources:
633 ahci_platform_disable_resources(hpriv);
635 return rc;
637 EXPORT_SYMBOL_GPL(ahci_platform_resume);
638 #endif
640 MODULE_DESCRIPTION("AHCI SATA platform library");
641 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
642 MODULE_LICENSE("GPL");