blob: 73a086664ee7da2b8403fa988d4baeb7cc81113e [file] [log] [blame]
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +01001/*
2 * AHCI SATA platform library
3 *
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>
8 *
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.
13 */
14
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>
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +010027#include <linux/of_platform.h>
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +010028#include "ahci.h"
29
30static void ahci_host_stop(struct ata_host *host);
31
32struct ata_port_operations ahci_platform_ops = {
33 .inherits = &ahci_ops,
34 .host_stop = ahci_host_stop,
35};
36EXPORT_SYMBOL_GPL(ahci_platform_ops);
37
38static struct scsi_host_template ahci_platform_sht = {
39 AHCI_SHT("ahci_platform"),
40};
41
42/**
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020043 * ahci_platform_enable_phys - Enable PHYs
44 * @hpriv: host private area to store config values
45 *
46 * This function enables all the PHYs found in hpriv->phys, if any.
47 * If a PHY fails to be enabled, it disables all the PHYs already
48 * enabled in reverse order and returns an error.
49 *
50 * RETURNS:
51 * 0 on success otherwise a negative error code
52 */
Fabio Estevam6bb86fe2014-08-30 16:39:09 -030053static int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020054{
55 int rc, i;
56
57 for (i = 0; i < hpriv->nports; i++) {
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020058 rc = phy_init(hpriv->phys[i]);
59 if (rc)
60 goto disable_phys;
61
62 rc = phy_power_on(hpriv->phys[i]);
63 if (rc) {
64 phy_exit(hpriv->phys[i]);
65 goto disable_phys;
66 }
67 }
68
69 return 0;
70
71disable_phys:
72 while (--i >= 0) {
73 phy_power_off(hpriv->phys[i]);
74 phy_exit(hpriv->phys[i]);
75 }
76 return rc;
77}
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020078
79/**
80 * ahci_platform_disable_phys - Disable PHYs
81 * @hpriv: host private area to store config values
82 *
83 * This function disables all PHYs found in hpriv->phys.
84 */
Fabio Estevam6bb86fe2014-08-30 16:39:09 -030085static void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020086{
87 int i;
88
89 for (i = 0; i < hpriv->nports; i++) {
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020090 phy_power_off(hpriv->phys[i]);
91 phy_exit(hpriv->phys[i]);
92 }
93}
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +020094
95/**
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +010096 * ahci_platform_enable_clks - Enable platform clocks
97 * @hpriv: host private area to store config values
98 *
99 * This function enables all the clks found in hpriv->clks, starting at
100 * index 0. If any clk fails to enable it disables all the clks already
101 * enabled in reverse order, and then returns an error.
102 *
103 * RETURNS:
104 * 0 on success otherwise a negative error code
105 */
106int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
107{
108 int c, rc;
109
110 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
111 rc = clk_prepare_enable(hpriv->clks[c]);
112 if (rc)
113 goto disable_unprepare_clk;
114 }
115 return 0;
116
117disable_unprepare_clk:
118 while (--c >= 0)
119 clk_disable_unprepare(hpriv->clks[c]);
120 return rc;
121}
122EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
123
124/**
125 * ahci_platform_disable_clks - Disable platform clocks
126 * @hpriv: host private area to store config values
127 *
128 * This function disables all the clks found in hpriv->clks, in reverse
129 * order of ahci_platform_enable_clks (starting at the end of the array).
130 */
131void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
132{
133 int c;
134
135 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
136 if (hpriv->clks[c])
137 clk_disable_unprepare(hpriv->clks[c]);
138}
139EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
140
141/**
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100142 * ahci_platform_enable_regulators - Enable regulators
143 * @hpriv: host private area to store config values
144 *
145 * This function enables all the regulators found in
146 * hpriv->target_pwrs, if any. If a regulator fails to be enabled, it
147 * disables all the regulators already enabled in reverse order and
148 * returns an error.
149 *
150 * RETURNS:
151 * 0 on success otherwise a negative error code
152 */
153int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv)
154{
155 int rc, i;
156
157 for (i = 0; i < hpriv->nports; i++) {
158 if (!hpriv->target_pwrs[i])
159 continue;
160
161 rc = regulator_enable(hpriv->target_pwrs[i]);
162 if (rc)
163 goto disable_target_pwrs;
164 }
165
166 return 0;
167
168disable_target_pwrs:
169 while (--i >= 0)
170 if (hpriv->target_pwrs[i])
171 regulator_disable(hpriv->target_pwrs[i]);
172
173 return rc;
174}
175EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators);
176
177/**
178 * ahci_platform_disable_regulators - Disable regulators
179 * @hpriv: host private area to store config values
180 *
181 * This function disables all regulators found in hpriv->target_pwrs.
182 */
183void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv)
184{
185 int i;
186
187 for (i = 0; i < hpriv->nports; i++) {
188 if (!hpriv->target_pwrs[i])
189 continue;
190 regulator_disable(hpriv->target_pwrs[i]);
191 }
192}
193EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators);
194/**
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100195 * ahci_platform_enable_resources - Enable platform resources
196 * @hpriv: host private area to store config values
197 *
198 * This function enables all ahci_platform managed resources in the
199 * following order:
200 * 1) Regulator
201 * 2) Clocks (through ahci_platform_enable_clks)
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200202 * 3) Phys
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100203 *
204 * If resource enabling fails at any point the previous enabled resources
205 * are disabled in reverse order.
206 *
207 * RETURNS:
208 * 0 on success otherwise a negative error code
209 */
210int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
211{
212 int rc;
213
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100214 rc = ahci_platform_enable_regulators(hpriv);
215 if (rc)
216 return rc;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100217
218 rc = ahci_platform_enable_clks(hpriv);
219 if (rc)
220 goto disable_regulator;
221
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200222 rc = ahci_platform_enable_phys(hpriv);
223 if (rc)
224 goto disable_clks;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100225
226 return 0;
227
228disable_clks:
229 ahci_platform_disable_clks(hpriv);
230
231disable_regulator:
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100232 ahci_platform_disable_regulators(hpriv);
233
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100234 return rc;
235}
236EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
237
238/**
239 * ahci_platform_disable_resources - Disable platform resources
240 * @hpriv: host private area to store config values
241 *
242 * This function disables all ahci_platform managed resources in the
243 * following order:
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200244 * 1) Phys
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100245 * 2) Clocks (through ahci_platform_disable_clks)
246 * 3) Regulator
247 */
248void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
249{
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200250 ahci_platform_disable_phys(hpriv);
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100251
252 ahci_platform_disable_clks(hpriv);
253
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100254 ahci_platform_disable_regulators(hpriv);
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100255}
256EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
257
258static void ahci_platform_put_resources(struct device *dev, void *res)
259{
260 struct ahci_host_priv *hpriv = res;
261 int c;
262
263 if (hpriv->got_runtime_pm) {
264 pm_runtime_put_sync(dev);
265 pm_runtime_disable(dev);
266 }
267
268 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
269 clk_put(hpriv->clks[c]);
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100270 /*
271 * The regulators are tied to child node device and not to the
272 * SATA device itself. So we can't use devm for automatically
273 * releasing them. We have to do it manually here.
274 */
275 for (c = 0; c < hpriv->nports; c++)
276 if (hpriv->target_pwrs && hpriv->target_pwrs[c])
277 regulator_put(hpriv->target_pwrs[c]);
278
279}
280
281static int ahci_platform_get_phy(struct ahci_host_priv *hpriv, u32 port,
282 struct device *dev, struct device_node *node)
283{
284 int rc;
285
286 hpriv->phys[port] = devm_of_phy_get(dev, node, NULL);
287
288 if (!IS_ERR(hpriv->phys[port]))
289 return 0;
290
291 rc = PTR_ERR(hpriv->phys[port]);
292 switch (rc) {
293 case -ENOSYS:
294 /* No PHY support. Check if PHY is required. */
295 if (of_find_property(node, "phys", NULL)) {
296 dev_err(dev,
297 "couldn't get PHY in node %s: ENOSYS\n",
298 node->name);
299 break;
300 }
301 case -ENODEV:
302 /* continue normally */
303 hpriv->phys[port] = NULL;
304 rc = 0;
305 break;
306
307 default:
308 dev_err(dev,
309 "couldn't get PHY in node %s: %d\n",
310 node->name, rc);
311
312 break;
313 }
314
315 return rc;
316}
317
318static int ahci_platform_get_regulator(struct ahci_host_priv *hpriv, u32 port,
319 struct device *dev)
320{
321 struct regulator *target_pwr;
322 int rc = 0;
323
324 target_pwr = regulator_get_optional(dev, "target");
325
326 if (!IS_ERR(target_pwr))
327 hpriv->target_pwrs[port] = target_pwr;
328 else
329 rc = PTR_ERR(target_pwr);
330
331 return rc;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100332}
333
334/**
335 * ahci_platform_get_resources - Get platform resources
336 * @pdev: platform device to get resources for
337 *
338 * This function allocates an ahci_host_priv struct, and gets the following
339 * resources, storing a reference to them inside the returned struct:
340 *
341 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
342 * 2) regulator for controlling the targets power (optional)
343 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
344 * or for non devicetree enabled platforms a single clock
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200345 * 4) phys (optional)
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100346 *
347 * RETURNS:
348 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
349 */
350struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
351{
352 struct device *dev = &pdev->dev;
353 struct ahci_host_priv *hpriv;
354 struct clk *clk;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200355 struct device_node *child;
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100356 int i, sz, enabled_ports = 0, rc = -ENOMEM, child_nodes;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200357 u32 mask_port_map = 0;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100358
359 if (!devres_open_group(dev, NULL, GFP_KERNEL))
360 return ERR_PTR(-ENOMEM);
361
362 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
363 GFP_KERNEL);
364 if (!hpriv)
365 goto err_out;
366
367 devres_add(dev, hpriv);
368
369 hpriv->mmio = devm_ioremap_resource(dev,
370 platform_get_resource(pdev, IORESOURCE_MEM, 0));
371 if (IS_ERR(hpriv->mmio)) {
372 dev_err(dev, "no mmio space\n");
373 rc = PTR_ERR(hpriv->mmio);
374 goto err_out;
375 }
376
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100377 for (i = 0; i < AHCI_MAX_CLKS; i++) {
378 /*
379 * For now we must use clk_get(dev, NULL) for the first clock,
380 * because some platforms (da850, spear13xx) are not yet
381 * converted to use devicetree for clocks. For new platforms
382 * this is equivalent to of_clk_get(dev->of_node, 0).
383 */
384 if (i == 0)
385 clk = clk_get(dev, NULL);
386 else
387 clk = of_clk_get(dev->of_node, i);
388
389 if (IS_ERR(clk)) {
390 rc = PTR_ERR(clk);
391 if (rc == -EPROBE_DEFER)
392 goto err_out;
393 break;
394 }
395 hpriv->clks[i] = clk;
396 }
397
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100398 hpriv->nports = child_nodes = of_get_child_count(dev->of_node);
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200399
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100400 /*
401 * If no sub-node was found, we still need to set nports to
402 * one in order to be able to use the
403 * ahci_platform_[en|dis]able_[phys|regulators] functions.
404 */
405 if (!child_nodes)
406 hpriv->nports = 1;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200407
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100408 sz = hpriv->nports * sizeof(*hpriv->phys);
409 hpriv->phys = devm_kzalloc(dev, sz, GFP_KERNEL);
410 if (!hpriv->phys) {
411 rc = -ENOMEM;
412 goto err_out;
413 }
414 sz = hpriv->nports * sizeof(*hpriv->target_pwrs);
415 hpriv->target_pwrs = devm_kzalloc(dev, sz, GFP_KERNEL);
416 if (!hpriv->target_pwrs) {
417 rc = -ENOMEM;
418 goto err_out;
419 }
420
421 if (child_nodes) {
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200422 for_each_child_of_node(dev->of_node, child) {
423 u32 port;
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100424 struct platform_device *port_dev;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200425
426 if (!of_device_is_available(child))
427 continue;
428
429 if (of_property_read_u32(child, "reg", &port)) {
430 rc = -EINVAL;
Mikko Perttunenacbd5732014-06-17 15:07:55 +0300431 goto err_out;
432 }
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100433
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200434 if (port >= hpriv->nports) {
435 dev_warn(dev, "invalid port number %d\n", port);
436 continue;
437 }
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200438 mask_port_map |= BIT(port);
439
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100440 of_platform_device_create(child, NULL, NULL);
441
442 port_dev = of_find_device_by_node(child);
443
444 if (port_dev) {
445 rc = ahci_platform_get_regulator(hpriv, port,
446 &port_dev->dev);
447 if (rc == -EPROBE_DEFER)
448 goto err_out;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200449 }
450
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100451 rc = ahci_platform_get_phy(hpriv, port, dev, child);
452 if (rc)
453 goto err_out;
454
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200455 enabled_ports++;
456 }
457 if (!enabled_ports) {
458 dev_warn(dev, "No port enabled\n");
459 rc = -ENODEV;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100460 goto err_out;
461 }
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200462
463 if (!hpriv->mask_port_map)
464 hpriv->mask_port_map = mask_port_map;
465 } else {
466 /*
467 * If no sub-node was found, keep this for device tree
468 * compatibility
469 */
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100470 rc = ahci_platform_get_phy(hpriv, 0, dev, dev->of_node);
471 if (rc)
472 goto err_out;
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200473
Gregory CLEMENTc7d7dde2015-01-15 15:09:37 +0100474 rc = ahci_platform_get_regulator(hpriv, 0, dev);
475 if (rc == -EPROBE_DEFER)
476 goto err_out;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100477 }
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100478 pm_runtime_enable(dev);
479 pm_runtime_get_sync(dev);
480 hpriv->got_runtime_pm = true;
481
482 devres_remove_group(dev, NULL);
483 return hpriv;
484
485err_out:
486 devres_release_group(dev, NULL);
487 return ERR_PTR(rc);
488}
489EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
490
491/**
492 * ahci_platform_init_host - Bring up an ahci-platform host
493 * @pdev: platform device pointer for the host
494 * @hpriv: ahci-host private data for the host
495 * @pi_template: template for the ata_port_info to use
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100496 *
497 * This function does all the usual steps needed to bring up an
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200498 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100499 * must be initialized / enabled before calling this.
500 *
501 * RETURNS:
502 * 0 on success otherwise a negative error code
503 */
504int ahci_platform_init_host(struct platform_device *pdev,
505 struct ahci_host_priv *hpriv,
Antoine Ténart725c7b52014-07-30 20:13:56 +0200506 const struct ata_port_info *pi_template)
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100507{
508 struct device *dev = &pdev->dev;
509 struct ata_port_info pi = *pi_template;
510 const struct ata_port_info *ppi[] = { &pi, NULL };
511 struct ata_host *host;
512 int i, irq, n_ports, rc;
513
514 irq = platform_get_irq(pdev, 0);
515 if (irq <= 0) {
516 dev_err(dev, "no irq\n");
517 return -EINVAL;
518 }
519
520 /* prepare host */
Thierry Redingc4121c62014-08-01 16:30:37 +0200521 pi.private_data = (void *)(unsigned long)hpriv->flags;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100522
Antoine Ténart725c7b52014-07-30 20:13:56 +0200523 ahci_save_initial_config(dev, hpriv);
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100524
525 if (hpriv->cap & HOST_CAP_NCQ)
526 pi.flags |= ATA_FLAG_NCQ;
527
528 if (hpriv->cap & HOST_CAP_PMP)
529 pi.flags |= ATA_FLAG_PMP;
530
531 ahci_set_em_messages(hpriv, &pi);
532
533 /* CAP.NP sometimes indicate the index of the last enabled
534 * port, at other times, that of the last possible port, so
535 * determining the maximum port number requires looking at
536 * both CAP.NP and port_map.
537 */
538 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
539
540 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
541 if (!host)
542 return -ENOMEM;
543
544 host->private_data = hpriv;
545
546 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
547 host->flags |= ATA_HOST_PARALLEL_SCAN;
548 else
549 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
550
551 if (pi.flags & ATA_FLAG_EM)
552 ahci_reset_em(host);
553
554 for (i = 0; i < host->n_ports; i++) {
555 struct ata_port *ap = host->ports[i];
556
557 ata_port_desc(ap, "mmio %pR",
558 platform_get_resource(pdev, IORESOURCE_MEM, 0));
559 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
560
561 /* set enclosure management message type */
562 if (ap->flags & ATA_FLAG_EM)
563 ap->em_message_type = hpriv->em_msg_type;
564
565 /* disabled/not-implemented port */
566 if (!(hpriv->port_map & (1 << i)))
567 ap->ops = &ata_dummy_port_ops;
568 }
569
Suravee Suthikulpanitcc7a9e22014-06-12 12:40:23 -0500570 if (hpriv->cap & HOST_CAP_64) {
571 rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
572 if (rc) {
573 rc = dma_coerce_mask_and_coherent(dev,
574 DMA_BIT_MASK(32));
575 if (rc) {
576 dev_err(dev, "Failed to enable 64-bit DMA.\n");
577 return rc;
578 }
579 dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
580 }
581 }
582
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100583 rc = ahci_reset_controller(host);
584 if (rc)
585 return rc;
586
587 ahci_init_controller(host);
588 ahci_print_info(host, "platform");
589
Alexander Gordeevd1028e22014-09-29 18:25:59 +0200590 return ahci_host_activate(host, irq, &ahci_platform_sht);
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100591}
592EXPORT_SYMBOL_GPL(ahci_platform_init_host);
593
594static void ahci_host_stop(struct ata_host *host)
595{
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100596 struct ahci_host_priv *hpriv = host->private_data;
597
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100598 ahci_platform_disable_resources(hpriv);
599}
600
601#ifdef CONFIG_PM_SLEEP
602/**
603 * ahci_platform_suspend_host - Suspend an ahci-platform host
604 * @dev: device pointer for the host
605 *
606 * This function does all the usual steps needed to suspend an
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200607 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100608 * must be disabled after calling this.
609 *
610 * RETURNS:
611 * 0 on success otherwise a negative error code
612 */
613int ahci_platform_suspend_host(struct device *dev)
614{
615 struct ata_host *host = dev_get_drvdata(dev);
616 struct ahci_host_priv *hpriv = host->private_data;
617 void __iomem *mmio = hpriv->mmio;
618 u32 ctl;
619
620 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
621 dev_err(dev, "firmware update required for suspend/resume\n");
622 return -EIO;
623 }
624
625 /*
626 * AHCI spec rev1.1 section 8.3.3:
627 * Software must disable interrupts prior to requesting a
628 * transition of the HBA to D3 state.
629 */
630 ctl = readl(mmio + HOST_CTL);
631 ctl &= ~HOST_IRQ_EN;
632 writel(ctl, mmio + HOST_CTL);
633 readl(mmio + HOST_CTL); /* flush */
634
635 return ata_host_suspend(host, PMSG_SUSPEND);
636}
637EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
638
639/**
640 * ahci_platform_resume_host - Resume an ahci-platform host
641 * @dev: device pointer for the host
642 *
643 * This function does all the usual steps needed to resume an ahci-platform
Antoine Ténartb1a9edbd2014-07-30 20:13:57 +0200644 * host, note any necessary resources (ie clks, phys, etc.) must be
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100645 * initialized / enabled before calling this.
646 *
647 * RETURNS:
648 * 0 on success otherwise a negative error code
649 */
650int ahci_platform_resume_host(struct device *dev)
651{
652 struct ata_host *host = dev_get_drvdata(dev);
653 int rc;
654
655 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
656 rc = ahci_reset_controller(host);
657 if (rc)
658 return rc;
659
660 ahci_init_controller(host);
661 }
662
663 ata_host_resume(host);
664
665 return 0;
666}
667EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
668
669/**
670 * ahci_platform_suspend - Suspend an ahci-platform device
671 * @dev: the platform device to suspend
672 *
673 * This function suspends the host associated with the device, followed by
674 * disabling all the resources of the device.
675 *
676 * RETURNS:
677 * 0 on success otherwise a negative error code
678 */
679int ahci_platform_suspend(struct device *dev)
680{
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100681 struct ata_host *host = dev_get_drvdata(dev);
682 struct ahci_host_priv *hpriv = host->private_data;
683 int rc;
684
685 rc = ahci_platform_suspend_host(dev);
686 if (rc)
687 return rc;
688
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100689 ahci_platform_disable_resources(hpriv);
690
691 return 0;
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100692}
693EXPORT_SYMBOL_GPL(ahci_platform_suspend);
694
695/**
696 * ahci_platform_resume - Resume an ahci-platform device
697 * @dev: the platform device to resume
698 *
699 * This function enables all the resources of the device followed by
700 * resuming the host associated with the device.
701 *
702 * RETURNS:
703 * 0 on success otherwise a negative error code
704 */
705int ahci_platform_resume(struct device *dev)
706{
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100707 struct ata_host *host = dev_get_drvdata(dev);
708 struct ahci_host_priv *hpriv = host->private_data;
709 int rc;
710
711 rc = ahci_platform_enable_resources(hpriv);
712 if (rc)
713 return rc;
714
Bartlomiej Zolnierkiewiczfd990552014-03-25 19:51:39 +0100715 rc = ahci_platform_resume_host(dev);
716 if (rc)
717 goto disable_resources;
718
719 /* We resumed so update PM runtime state */
720 pm_runtime_disable(dev);
721 pm_runtime_set_active(dev);
722 pm_runtime_enable(dev);
723
724 return 0;
725
726disable_resources:
727 ahci_platform_disable_resources(hpriv);
728
729 return rc;
730}
731EXPORT_SYMBOL_GPL(ahci_platform_resume);
732#endif
733
734MODULE_DESCRIPTION("AHCI SATA platform library");
735MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
736MODULE_LICENSE("GPL");