1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AHCI SATA platform library
4 *
5 * Copyright 2004-2005 Red Hat, Inc.
6 * Jeff Garzik <jgarzik@pobox.com>
7 * Copyright 2010 MontaVista Software, LLC.
8 * Anton Vorontsov <avorontsov@ru.mvista.com>
9 */
10
11 #include <linux/clk.h>
12 #include <linux/kernel.h>
13 #include <linux/gfp.h>
14 #include <linux/module.h>
15 #include <linux/pm.h>
16 #include <linux/interrupt.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/libata.h>
20 #include <linux/ahci_platform.h>
21 #include <linux/phy/phy.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of_platform.h>
24 #include <linux/reset.h>
25 #include "ahci.h"
26
27 static void ahci_host_stop(struct ata_host *host);
28
29 struct ata_port_operations ahci_platform_ops = {
30 .inherits = &ahci_ops,
31 .host_stop = ahci_host_stop,
32 };
33 EXPORT_SYMBOL_GPL(ahci_platform_ops);
34
35 /**
36 * ahci_platform_enable_phys - Enable PHYs
37 * @hpriv: host private area to store config values
38 *
39 * This function enables all the PHYs found in hpriv->phys, if any.
40 * If a PHY fails to be enabled, it disables all the PHYs already
41 * enabled in reverse order and returns an error.
42 *
43 * RETURNS:
44 * 0 on success otherwise a negative error code
45 */
ahci_platform_enable_phys(struct ahci_host_priv * hpriv)46 int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
47 {
48 int rc, i;
49
50 for (i = 0; i < hpriv->nports; i++) {
51 rc = phy_init(hpriv->phys[i]);
52 if (rc)
53 goto disable_phys;
54
55 rc = phy_set_mode(hpriv->phys[i], PHY_MODE_SATA);
56 if (rc) {
57 phy_exit(hpriv->phys[i]);
58 goto disable_phys;
59 }
60
61 rc = phy_power_on(hpriv->phys[i]);
62 if (rc && !(rc == -EOPNOTSUPP && (hpriv->flags & AHCI_HFLAG_IGN_NOTSUPP_POWER_ON))) {
63 phy_exit(hpriv->phys[i]);
64 goto disable_phys;
65 }
66 }
67
68 return 0;
69
70 disable_phys:
71 while (--i >= 0) {
72 phy_power_off(hpriv->phys[i]);
73 phy_exit(hpriv->phys[i]);
74 }
75 return rc;
76 }
77 EXPORT_SYMBOL_GPL(ahci_platform_enable_phys);
78
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 */
ahci_platform_disable_phys(struct ahci_host_priv * hpriv)85 void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
86 {
87 int i;
88
89 for (i = 0; i < hpriv->nports; i++) {
90 phy_power_off(hpriv->phys[i]);
91 phy_exit(hpriv->phys[i]);
92 }
93 }
94 EXPORT_SYMBOL_GPL(ahci_platform_disable_phys);
95
96 /**
97 * ahci_platform_enable_clks - Enable platform clocks
98 * @hpriv: host private area to store config values
99 *
100 * This function enables all the clks found for the AHCI device.
101 *
102 * RETURNS:
103 * 0 on success otherwise a negative error code
104 */
ahci_platform_enable_clks(struct ahci_host_priv * hpriv)105 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
106 {
107 return clk_bulk_prepare_enable(hpriv->n_clks, hpriv->clks);
108 }
109 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
110
111 /**
112 * ahci_platform_disable_clks - Disable platform clocks
113 * @hpriv: host private area to store config values
114 *
115 * This function disables all the clocks enabled before
116 * (bulk-clocks-disable function is supposed to do that in reverse
117 * from the enabling procedure order).
118 */
ahci_platform_disable_clks(struct ahci_host_priv * hpriv)119 void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
120 {
121 clk_bulk_disable_unprepare(hpriv->n_clks, hpriv->clks);
122 }
123 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
124
125 /**
126 * ahci_platform_deassert_rsts - Deassert/trigger platform resets
127 * @hpriv: host private area to store config values
128 *
129 * This function deasserts or triggers all the reset lines found for
130 * the AHCI device.
131 *
132 * RETURNS:
133 * 0 on success otherwise a negative error code
134 */
ahci_platform_deassert_rsts(struct ahci_host_priv * hpriv)135 int ahci_platform_deassert_rsts(struct ahci_host_priv *hpriv)
136 {
137 if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)
138 return reset_control_reset(hpriv->rsts);
139
140 return reset_control_deassert(hpriv->rsts);
141 }
142 EXPORT_SYMBOL_GPL(ahci_platform_deassert_rsts);
143
144 /**
145 * ahci_platform_assert_rsts - Assert/rearm platform resets
146 * @hpriv: host private area to store config values
147 *
148 * This function asserts or rearms (for self-deasserting resets) all
149 * the reset controls found for the AHCI device.
150 *
151 * RETURNS:
152 * 0 on success otherwise a negative error code
153 */
ahci_platform_assert_rsts(struct ahci_host_priv * hpriv)154 int ahci_platform_assert_rsts(struct ahci_host_priv *hpriv)
155 {
156 if (hpriv->f_rsts & AHCI_PLATFORM_RST_TRIGGER)
157 return reset_control_rearm(hpriv->rsts);
158
159 return reset_control_assert(hpriv->rsts);
160 }
161 EXPORT_SYMBOL_GPL(ahci_platform_assert_rsts);
162
163 /**
164 * ahci_platform_enable_regulators - Enable regulators
165 * @hpriv: host private area to store config values
166 *
167 * This function enables all the regulators found in controller and
168 * hpriv->target_pwrs, if any. If a regulator fails to be enabled, it
169 * disables all the regulators already enabled in reverse order and
170 * returns an error.
171 *
172 * RETURNS:
173 * 0 on success otherwise a negative error code
174 */
ahci_platform_enable_regulators(struct ahci_host_priv * hpriv)175 int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv)
176 {
177 int rc, i;
178
179 rc = regulator_enable(hpriv->ahci_regulator);
180 if (rc)
181 return rc;
182
183 rc = regulator_enable(hpriv->phy_regulator);
184 if (rc)
185 goto disable_ahci_pwrs;
186
187 for (i = 0; i < hpriv->nports; i++) {
188 if (!hpriv->target_pwrs[i])
189 continue;
190
191 rc = regulator_enable(hpriv->target_pwrs[i]);
192 if (rc)
193 goto disable_target_pwrs;
194 }
195
196 return 0;
197
198 disable_target_pwrs:
199 while (--i >= 0)
200 if (hpriv->target_pwrs[i])
201 regulator_disable(hpriv->target_pwrs[i]);
202
203 regulator_disable(hpriv->phy_regulator);
204 disable_ahci_pwrs:
205 regulator_disable(hpriv->ahci_regulator);
206 return rc;
207 }
208 EXPORT_SYMBOL_GPL(ahci_platform_enable_regulators);
209
210 /**
211 * ahci_platform_disable_regulators - Disable regulators
212 * @hpriv: host private area to store config values
213 *
214 * This function disables all regulators found in hpriv->target_pwrs and
215 * AHCI controller.
216 */
ahci_platform_disable_regulators(struct ahci_host_priv * hpriv)217 void ahci_platform_disable_regulators(struct ahci_host_priv *hpriv)
218 {
219 int i;
220
221 for (i = 0; i < hpriv->nports; i++) {
222 if (!hpriv->target_pwrs[i])
223 continue;
224 regulator_disable(hpriv->target_pwrs[i]);
225 }
226
227 regulator_disable(hpriv->ahci_regulator);
228 regulator_disable(hpriv->phy_regulator);
229 }
230 EXPORT_SYMBOL_GPL(ahci_platform_disable_regulators);
231 /**
232 * ahci_platform_enable_resources - Enable platform resources
233 * @hpriv: host private area to store config values
234 *
235 * This function enables all ahci_platform managed resources in the
236 * following order:
237 * 1) Regulator
238 * 2) Clocks (through ahci_platform_enable_clks)
239 * 3) Resets
240 * 4) Phys
241 *
242 * If resource enabling fails at any point the previous enabled resources
243 * are disabled in reverse order.
244 *
245 * RETURNS:
246 * 0 on success otherwise a negative error code
247 */
ahci_platform_enable_resources(struct ahci_host_priv * hpriv)248 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
249 {
250 int rc;
251
252 rc = ahci_platform_enable_regulators(hpriv);
253 if (rc)
254 return rc;
255
256 rc = ahci_platform_enable_clks(hpriv);
257 if (rc)
258 goto disable_regulator;
259
260 rc = ahci_platform_deassert_rsts(hpriv);
261 if (rc)
262 goto disable_clks;
263
264 rc = ahci_platform_enable_phys(hpriv);
265 if (rc)
266 goto disable_rsts;
267
268 return 0;
269
270 disable_rsts:
271 ahci_platform_assert_rsts(hpriv);
272
273 disable_clks:
274 ahci_platform_disable_clks(hpriv);
275
276 disable_regulator:
277 ahci_platform_disable_regulators(hpriv);
278
279 return rc;
280 }
281 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
282
283 /**
284 * ahci_platform_disable_resources - Disable platform resources
285 * @hpriv: host private area to store config values
286 *
287 * This function disables all ahci_platform managed resources in the
288 * following order:
289 * 1) Phys
290 * 2) Resets
291 * 3) Clocks (through ahci_platform_disable_clks)
292 * 4) Regulator
293 */
ahci_platform_disable_resources(struct ahci_host_priv * hpriv)294 void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
295 {
296 ahci_platform_disable_phys(hpriv);
297
298 ahci_platform_assert_rsts(hpriv);
299
300 ahci_platform_disable_clks(hpriv);
301
302 ahci_platform_disable_regulators(hpriv);
303 }
304 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
305
ahci_platform_put_resources(struct device * dev,void * res)306 static void ahci_platform_put_resources(struct device *dev, void *res)
307 {
308 struct ahci_host_priv *hpriv = res;
309 int c;
310
311 if (hpriv->got_runtime_pm) {
312 pm_runtime_put_sync(dev);
313 pm_runtime_disable(dev);
314 }
315
316 /*
317 * The regulators are tied to child node device and not to the
318 * SATA device itself. So we can't use devm for automatically
319 * releasing them. We have to do it manually here.
320 */
321 for (c = 0; c < hpriv->nports; c++)
322 if (hpriv->target_pwrs && hpriv->target_pwrs[c])
323 regulator_put(hpriv->target_pwrs[c]);
324
325 kfree(hpriv->target_pwrs);
326 }
327
ahci_platform_get_phy(struct ahci_host_priv * hpriv,u32 port,struct device * dev,struct device_node * node)328 static int ahci_platform_get_phy(struct ahci_host_priv *hpriv, u32 port,
329 struct device *dev, struct device_node *node)
330 {
331 int rc;
332
333 hpriv->phys[port] = devm_of_phy_get(dev, node, NULL);
334
335 if (!IS_ERR(hpriv->phys[port]))
336 return 0;
337
338 rc = PTR_ERR(hpriv->phys[port]);
339 switch (rc) {
340 case -ENOSYS:
341 /* No PHY support. Check if PHY is required. */
342 if (of_find_property(node, "phys", NULL)) {
343 dev_err(dev,
344 "couldn't get PHY in node %pOFn: ENOSYS\n",
345 node);
346 break;
347 }
348 fallthrough;
349 case -ENODEV:
350 /* continue normally */
351 hpriv->phys[port] = NULL;
352 rc = 0;
353 break;
354 case -EPROBE_DEFER:
355 /* Do not complain yet */
356 break;
357
358 default:
359 dev_err(dev,
360 "couldn't get PHY in node %pOFn: %d\n",
361 node, rc);
362
363 break;
364 }
365
366 return rc;
367 }
368
ahci_platform_get_regulator(struct ahci_host_priv * hpriv,u32 port,struct device * dev)369 static int ahci_platform_get_regulator(struct ahci_host_priv *hpriv, u32 port,
370 struct device *dev)
371 {
372 struct regulator *target_pwr;
373 int rc = 0;
374
375 target_pwr = regulator_get(dev, "target");
376
377 if (!IS_ERR(target_pwr))
378 hpriv->target_pwrs[port] = target_pwr;
379 else
380 rc = PTR_ERR(target_pwr);
381
382 return rc;
383 }
384
385 /**
386 * ahci_platform_get_resources - Get platform resources
387 * @pdev: platform device to get resources for
388 * @flags: bitmap representing the resource to get
389 *
390 * This function allocates an ahci_host_priv struct, and gets the following
391 * resources, storing a reference to them inside the returned struct:
392 *
393 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
394 * 2) regulator for controlling the targets power (optional)
395 * regulator for controlling the AHCI controller (optional)
396 * 3) all clocks specified in the devicetree node, or a single
397 * clock for non-OF platforms (optional)
398 * 4) resets, if flags has AHCI_PLATFORM_GET_RESETS (optional)
399 * 5) phys (optional)
400 *
401 * RETURNS:
402 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
403 */
ahci_platform_get_resources(struct platform_device * pdev,unsigned int flags)404 struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
405 unsigned int flags)
406 {
407 int child_nodes, rc = -ENOMEM, enabled_ports = 0;
408 struct device *dev = &pdev->dev;
409 struct ahci_host_priv *hpriv;
410 struct device_node *child;
411 u32 mask_port_map = 0;
412
413 if (!devres_open_group(dev, NULL, GFP_KERNEL))
414 return ERR_PTR(-ENOMEM);
415
416 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
417 GFP_KERNEL);
418 if (!hpriv)
419 goto err_out;
420
421 devres_add(dev, hpriv);
422
423 hpriv->mmio = devm_ioremap_resource(dev,
424 platform_get_resource(pdev, IORESOURCE_MEM, 0));
425 if (IS_ERR(hpriv->mmio)) {
426 rc = PTR_ERR(hpriv->mmio);
427 goto err_out;
428 }
429
430 /*
431 * Bulk clocks getting procedure can fail to find any clock due to
432 * running on a non-OF platform or due to the clocks being defined in
433 * bypass of the DT firmware (like da850, spear13xx). In that case we
434 * fallback to getting a single clock source right from the dev clocks
435 * list.
436 */
437 rc = devm_clk_bulk_get_all(dev, &hpriv->clks);
438 if (rc < 0)
439 goto err_out;
440
441 if (rc > 0) {
442 /* Got clocks in bulk */
443 hpriv->n_clks = rc;
444 } else {
445 /*
446 * No clock bulk found: fallback to manually getting
447 * the optional clock.
448 */
449 hpriv->clks = devm_kzalloc(dev, sizeof(*hpriv->clks), GFP_KERNEL);
450 if (!hpriv->clks) {
451 rc = -ENOMEM;
452 goto err_out;
453 }
454 hpriv->clks->clk = devm_clk_get_optional(dev, NULL);
455 if (IS_ERR(hpriv->clks->clk)) {
456 rc = PTR_ERR(hpriv->clks->clk);
457 goto err_out;
458 } else if (hpriv->clks->clk) {
459 hpriv->clks->id = "ahci";
460 hpriv->n_clks = 1;
461 }
462 }
463
464 hpriv->ahci_regulator = devm_regulator_get(dev, "ahci");
465 if (IS_ERR(hpriv->ahci_regulator)) {
466 rc = PTR_ERR(hpriv->ahci_regulator);
467 if (rc != 0)
468 goto err_out;
469 }
470
471 hpriv->phy_regulator = devm_regulator_get(dev, "phy");
472 if (IS_ERR(hpriv->phy_regulator)) {
473 rc = PTR_ERR(hpriv->phy_regulator);
474 goto err_out;
475 }
476
477 if (flags & AHCI_PLATFORM_GET_RESETS) {
478 hpriv->rsts = devm_reset_control_array_get_optional_shared(dev);
479 if (IS_ERR(hpriv->rsts)) {
480 rc = PTR_ERR(hpriv->rsts);
481 goto err_out;
482 }
483
484 hpriv->f_rsts = flags & AHCI_PLATFORM_RST_TRIGGER;
485 }
486
487 /*
488 * Too many sub-nodes most likely means having something wrong with
489 * the firmware.
490 */
491 child_nodes = of_get_child_count(dev->of_node);
492 if (child_nodes > AHCI_MAX_PORTS) {
493 rc = -EINVAL;
494 goto err_out;
495 }
496
497 /*
498 * If no sub-node was found, we still need to set nports to
499 * one in order to be able to use the
500 * ahci_platform_[en|dis]able_[phys|regulators] functions.
501 */
502 if (child_nodes)
503 hpriv->nports = child_nodes;
504 else
505 hpriv->nports = 1;
506
507 hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL);
508 if (!hpriv->phys) {
509 rc = -ENOMEM;
510 goto err_out;
511 }
512 /*
513 * We cannot use devm_ here, since ahci_platform_put_resources() uses
514 * target_pwrs after devm_ have freed memory
515 */
516 hpriv->target_pwrs = kcalloc(hpriv->nports, sizeof(*hpriv->target_pwrs), GFP_KERNEL);
517 if (!hpriv->target_pwrs) {
518 rc = -ENOMEM;
519 goto err_out;
520 }
521
522 if (child_nodes) {
523 for_each_child_of_node(dev->of_node, child) {
524 u32 port;
525 struct platform_device *port_dev __maybe_unused;
526
527 if (!of_device_is_available(child))
528 continue;
529
530 if (of_property_read_u32(child, "reg", &port)) {
531 rc = -EINVAL;
532 of_node_put(child);
533 goto err_out;
534 }
535
536 if (port >= hpriv->nports) {
537 dev_warn(dev, "invalid port number %d\n", port);
538 continue;
539 }
540 mask_port_map |= BIT(port);
541
542 #ifdef CONFIG_OF_ADDRESS
543 of_platform_device_create(child, NULL, NULL);
544
545 port_dev = of_find_device_by_node(child);
546
547 if (port_dev) {
548 rc = ahci_platform_get_regulator(hpriv, port,
549 &port_dev->dev);
550 if (rc == -EPROBE_DEFER) {
551 of_node_put(child);
552 goto err_out;
553 }
554 }
555 #endif
556
557 rc = ahci_platform_get_phy(hpriv, port, dev, child);
558 if (rc) {
559 of_node_put(child);
560 goto err_out;
561 }
562
563 enabled_ports++;
564 }
565 if (!enabled_ports) {
566 dev_warn(dev, "No port enabled\n");
567 rc = -ENODEV;
568 goto err_out;
569 }
570
571 if (!hpriv->mask_port_map)
572 hpriv->mask_port_map = mask_port_map;
573 } else {
574 /*
575 * If no sub-node was found, keep this for device tree
576 * compatibility
577 */
578 rc = ahci_platform_get_phy(hpriv, 0, dev, dev->of_node);
579 if (rc)
580 goto err_out;
581
582 rc = ahci_platform_get_regulator(hpriv, 0, dev);
583 if (rc == -EPROBE_DEFER)
584 goto err_out;
585 }
586 pm_runtime_enable(dev);
587 pm_runtime_get_sync(dev);
588 hpriv->got_runtime_pm = true;
589
590 devres_remove_group(dev, NULL);
591 return hpriv;
592
593 err_out:
594 devres_release_group(dev, NULL);
595 return ERR_PTR(rc);
596 }
597 EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
598
599 /**
600 * ahci_platform_init_host - Bring up an ahci-platform host
601 * @pdev: platform device pointer for the host
602 * @hpriv: ahci-host private data for the host
603 * @pi_template: template for the ata_port_info to use
604 * @sht: scsi_host_template to use when registering
605 *
606 * This function does all the usual steps needed to bring up an
607 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
608 * must be initialized / enabled before calling this.
609 *
610 * RETURNS:
611 * 0 on success otherwise a negative error code
612 */
ahci_platform_init_host(struct platform_device * pdev,struct ahci_host_priv * hpriv,const struct ata_port_info * pi_template,struct scsi_host_template * sht)613 int ahci_platform_init_host(struct platform_device *pdev,
614 struct ahci_host_priv *hpriv,
615 const struct ata_port_info *pi_template,
616 struct scsi_host_template *sht)
617 {
618 struct device *dev = &pdev->dev;
619 struct ata_port_info pi = *pi_template;
620 const struct ata_port_info *ppi[] = { &pi, NULL };
621 struct ata_host *host;
622 int i, irq, n_ports, rc;
623
624 irq = platform_get_irq(pdev, 0);
625 if (irq < 0) {
626 if (irq != -EPROBE_DEFER)
627 dev_err(dev, "no irq\n");
628 return irq;
629 }
630 if (!irq)
631 return -EINVAL;
632
633 hpriv->irq = irq;
634
635 /* prepare host */
636 pi.private_data = (void *)(unsigned long)hpriv->flags;
637
638 ahci_save_initial_config(dev, hpriv);
639
640 if (hpriv->cap & HOST_CAP_NCQ)
641 pi.flags |= ATA_FLAG_NCQ;
642
643 if (hpriv->cap & HOST_CAP_PMP)
644 pi.flags |= ATA_FLAG_PMP;
645
646 ahci_set_em_messages(hpriv, &pi);
647
648 /* CAP.NP sometimes indicate the index of the last enabled
649 * port, at other times, that of the last possible port, so
650 * determining the maximum port number requires looking at
651 * both CAP.NP and port_map.
652 */
653 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
654
655 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
656 if (!host)
657 return -ENOMEM;
658
659 host->private_data = hpriv;
660
661 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
662 host->flags |= ATA_HOST_PARALLEL_SCAN;
663 else
664 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
665
666 if (pi.flags & ATA_FLAG_EM)
667 ahci_reset_em(host);
668
669 for (i = 0; i < host->n_ports; i++) {
670 struct ata_port *ap = host->ports[i];
671
672 ata_port_desc(ap, "mmio %pR",
673 platform_get_resource(pdev, IORESOURCE_MEM, 0));
674 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
675
676 /* set enclosure management message type */
677 if (ap->flags & ATA_FLAG_EM)
678 ap->em_message_type = hpriv->em_msg_type;
679
680 /* disabled/not-implemented port */
681 if (!(hpriv->port_map & (1 << i)))
682 ap->ops = &ata_dummy_port_ops;
683 }
684
685 if (hpriv->cap & HOST_CAP_64) {
686 rc = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
687 if (rc) {
688 rc = dma_coerce_mask_and_coherent(dev,
689 DMA_BIT_MASK(32));
690 if (rc) {
691 dev_err(dev, "Failed to enable 64-bit DMA.\n");
692 return rc;
693 }
694 dev_warn(dev, "Enable 32-bit DMA instead of 64-bit.\n");
695 }
696 }
697
698 rc = ahci_reset_controller(host);
699 if (rc)
700 return rc;
701
702 ahci_init_controller(host);
703 ahci_print_info(host, "platform");
704
705 return ahci_host_activate(host, sht);
706 }
707 EXPORT_SYMBOL_GPL(ahci_platform_init_host);
708
ahci_host_stop(struct ata_host * host)709 static void ahci_host_stop(struct ata_host *host)
710 {
711 struct ahci_host_priv *hpriv = host->private_data;
712
713 ahci_platform_disable_resources(hpriv);
714 }
715
716 /**
717 * ahci_platform_shutdown - Disable interrupts and stop DMA for host ports
718 * @pdev: platform device pointer for the host
719 *
720 * This function is called during system shutdown and performs the minimal
721 * deconfiguration required to ensure that an ahci_platform host cannot
722 * corrupt or otherwise interfere with a new kernel being started with kexec.
723 */
ahci_platform_shutdown(struct platform_device * pdev)724 void ahci_platform_shutdown(struct platform_device *pdev)
725 {
726 struct ata_host *host = platform_get_drvdata(pdev);
727 struct ahci_host_priv *hpriv = host->private_data;
728 void __iomem *mmio = hpriv->mmio;
729 int i;
730
731 for (i = 0; i < host->n_ports; i++) {
732 struct ata_port *ap = host->ports[i];
733
734 /* Disable port interrupts */
735 if (ap->ops->freeze)
736 ap->ops->freeze(ap);
737
738 /* Stop the port DMA engines */
739 if (ap->ops->port_stop)
740 ap->ops->port_stop(ap);
741 }
742
743 /* Disable and clear host interrupts */
744 writel(readl(mmio + HOST_CTL) & ~HOST_IRQ_EN, mmio + HOST_CTL);
745 readl(mmio + HOST_CTL); /* flush */
746 writel(GENMASK(host->n_ports, 0), mmio + HOST_IRQ_STAT);
747 }
748 EXPORT_SYMBOL_GPL(ahci_platform_shutdown);
749
750 #ifdef CONFIG_PM_SLEEP
751 /**
752 * ahci_platform_suspend_host - Suspend an ahci-platform host
753 * @dev: device pointer for the host
754 *
755 * This function does all the usual steps needed to suspend an
756 * ahci-platform host, note any necessary resources (ie clks, phys, etc.)
757 * must be disabled after calling this.
758 *
759 * RETURNS:
760 * 0 on success otherwise a negative error code
761 */
ahci_platform_suspend_host(struct device * dev)762 int ahci_platform_suspend_host(struct device *dev)
763 {
764 struct ata_host *host = dev_get_drvdata(dev);
765 struct ahci_host_priv *hpriv = host->private_data;
766 void __iomem *mmio = hpriv->mmio;
767 u32 ctl;
768
769 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
770 dev_err(dev, "firmware update required for suspend/resume\n");
771 return -EIO;
772 }
773
774 /*
775 * AHCI spec rev1.1 section 8.3.3:
776 * Software must disable interrupts prior to requesting a
777 * transition of the HBA to D3 state.
778 */
779 ctl = readl(mmio + HOST_CTL);
780 ctl &= ~HOST_IRQ_EN;
781 writel(ctl, mmio + HOST_CTL);
782 readl(mmio + HOST_CTL); /* flush */
783
784 if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS)
785 ahci_platform_disable_phys(hpriv);
786
787 return ata_host_suspend(host, PMSG_SUSPEND);
788 }
789 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
790
791 /**
792 * ahci_platform_resume_host - Resume an ahci-platform host
793 * @dev: device pointer for the host
794 *
795 * This function does all the usual steps needed to resume an ahci-platform
796 * host, note any necessary resources (ie clks, phys, etc.) must be
797 * initialized / enabled before calling this.
798 *
799 * RETURNS:
800 * 0 on success otherwise a negative error code
801 */
ahci_platform_resume_host(struct device * dev)802 int ahci_platform_resume_host(struct device *dev)
803 {
804 struct ata_host *host = dev_get_drvdata(dev);
805 struct ahci_host_priv *hpriv = host->private_data;
806 int rc;
807
808 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
809 rc = ahci_reset_controller(host);
810 if (rc)
811 return rc;
812
813 ahci_init_controller(host);
814 }
815
816 if (hpriv->flags & AHCI_HFLAG_SUSPEND_PHYS)
817 ahci_platform_enable_phys(hpriv);
818
819 ata_host_resume(host);
820
821 return 0;
822 }
823 EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
824
825 /**
826 * ahci_platform_suspend - Suspend an ahci-platform device
827 * @dev: the platform device to suspend
828 *
829 * This function suspends the host associated with the device, followed by
830 * disabling all the resources of the device.
831 *
832 * RETURNS:
833 * 0 on success otherwise a negative error code
834 */
ahci_platform_suspend(struct device * dev)835 int ahci_platform_suspend(struct device *dev)
836 {
837 struct ata_host *host = dev_get_drvdata(dev);
838 struct ahci_host_priv *hpriv = host->private_data;
839 int rc;
840
841 rc = ahci_platform_suspend_host(dev);
842 if (rc)
843 return rc;
844
845 ahci_platform_disable_resources(hpriv);
846
847 return 0;
848 }
849 EXPORT_SYMBOL_GPL(ahci_platform_suspend);
850
851 /**
852 * ahci_platform_resume - Resume an ahci-platform device
853 * @dev: the platform device to resume
854 *
855 * This function enables all the resources of the device followed by
856 * resuming the host associated with the device.
857 *
858 * RETURNS:
859 * 0 on success otherwise a negative error code
860 */
ahci_platform_resume(struct device * dev)861 int ahci_platform_resume(struct device *dev)
862 {
863 struct ata_host *host = dev_get_drvdata(dev);
864 struct ahci_host_priv *hpriv = host->private_data;
865 int rc;
866
867 rc = ahci_platform_enable_resources(hpriv);
868 if (rc)
869 return rc;
870
871 rc = ahci_platform_resume_host(dev);
872 if (rc)
873 goto disable_resources;
874
875 /* We resumed so update PM runtime state */
876 pm_runtime_disable(dev);
877 pm_runtime_set_active(dev);
878 pm_runtime_enable(dev);
879
880 return 0;
881
882 disable_resources:
883 ahci_platform_disable_resources(hpriv);
884
885 return rc;
886 }
887 EXPORT_SYMBOL_GPL(ahci_platform_resume);
888 #endif
889
890 MODULE_DESCRIPTION("AHCI SATA platform library");
891 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
892 MODULE_LICENSE("GPL");
893