blob: fc32863d6a5afc03c1137c42a8bcb43c649b057b [file] [log] [blame]
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +03001/*
2 * AHCI SATA platform driver
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
Viresh Kumarf1e70c22012-08-27 10:37:19 +053015#include <linux/clk.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030016#include <linux/kernel.h>
Tejun Heofbaf6662010-03-30 02:52:43 +090017#include <linux/gfp.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030018#include <linux/module.h>
Shiraz Hashim18c25ff2012-03-16 15:49:55 +053019#include <linux/pm.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030020#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>
Roger Quadros21b5fae2014-02-22 16:53:40 +010025#include <linux/phy/phy.h>
Roger Quadrose708e462014-02-22 16:53:41 +010026#include <linux/pm_runtime.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030027#include "ahci.h"
28
Brian Norris1896b152012-11-02 00:46:17 -070029static void ahci_host_stop(struct ata_host *host);
30
Richard Zhu904c04f2011-09-28 15:41:54 +080031enum ahci_type {
32 AHCI, /* standard platform ahci */
33 IMX53_AHCI, /* ahci on i.mx53 */
Brian Norrisd408e2b2012-02-21 10:38:44 -080034 STRICT_AHCI, /* delayed DMA engine start */
Richard Zhu904c04f2011-09-28 15:41:54 +080035};
36
37static struct platform_device_id ahci_devtype[] = {
38 {
39 .name = "ahci",
40 .driver_data = AHCI,
41 }, {
42 .name = "imx53-ahci",
43 .driver_data = IMX53_AHCI,
44 }, {
Brian Norrisd408e2b2012-02-21 10:38:44 -080045 .name = "strict-ahci",
46 .driver_data = STRICT_AHCI,
47 }, {
Richard Zhu904c04f2011-09-28 15:41:54 +080048 /* sentinel */
49 }
50};
51MODULE_DEVICE_TABLE(platform, ahci_devtype);
52
Richard Zhu8b789d82013-10-15 10:44:54 +080053struct ata_port_operations ahci_platform_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070054 .inherits = &ahci_ops,
55 .host_stop = ahci_host_stop,
56};
Richard Zhu8b789d82013-10-15 10:44:54 +080057EXPORT_SYMBOL_GPL(ahci_platform_ops);
Brian Norris1896b152012-11-02 00:46:17 -070058
Brian Norris071d3ad2012-12-05 23:44:20 -080059static struct ata_port_operations ahci_platform_retry_srst_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070060 .inherits = &ahci_pmp_retry_srst_ops,
61 .host_stop = ahci_host_stop,
62};
Richard Zhu904c04f2011-09-28 15:41:54 +080063
64static const struct ata_port_info ahci_port_info[] = {
65 /* by features */
66 [AHCI] = {
67 .flags = AHCI_FLAG_COMMON,
68 .pio_mask = ATA_PIO4,
69 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070070 .port_ops = &ahci_platform_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080071 },
72 [IMX53_AHCI] = {
73 .flags = AHCI_FLAG_COMMON,
74 .pio_mask = ATA_PIO4,
75 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070076 .port_ops = &ahci_platform_retry_srst_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080077 },
Brian Norrisd408e2b2012-02-21 10:38:44 -080078 [STRICT_AHCI] = {
79 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
80 .flags = AHCI_FLAG_COMMON,
81 .pio_mask = ATA_PIO4,
82 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070083 .port_ops = &ahci_platform_ops,
Brian Norrisd408e2b2012-02-21 10:38:44 -080084 },
Richard Zhu904c04f2011-09-28 15:41:54 +080085};
86
Tejun Heofad16e72010-09-21 09:25:48 +020087static struct scsi_host_template ahci_platform_sht = {
88 AHCI_SHT("ahci_platform"),
89};
90
Hans de Goede156c5882014-02-22 16:53:31 +010091/**
92 * ahci_platform_enable_clks - Enable platform clocks
93 * @hpriv: host private area to store config values
94 *
95 * This function enables all the clks found in hpriv->clks, starting at
96 * index 0. If any clk fails to enable it disables all the clks already
97 * enabled in reverse order, and then returns an error.
98 *
99 * RETURNS:
100 * 0 on success otherwise a negative error code
101 */
102int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
103{
104 int c, rc;
105
106 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
107 rc = clk_prepare_enable(hpriv->clks[c]);
108 if (rc)
109 goto disable_unprepare_clk;
110 }
111 return 0;
112
113disable_unprepare_clk:
114 while (--c >= 0)
115 clk_disable_unprepare(hpriv->clks[c]);
116 return rc;
117}
118EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
119
120/**
121 * ahci_platform_disable_clks - Disable platform clocks
122 * @hpriv: host private area to store config values
123 *
124 * This function disables all the clks found in hpriv->clks, in reverse
125 * order of ahci_platform_enable_clks (starting at the end of the array).
126 */
127void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
128{
129 int c;
130
131 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
132 if (hpriv->clks[c])
133 clk_disable_unprepare(hpriv->clks[c]);
134}
135EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
136
Hans de Goede96a01ba2014-02-22 16:53:33 +0100137/**
138 * ahci_platform_enable_resources - Enable platform resources
139 * @hpriv: host private area to store config values
140 *
141 * This function enables all ahci_platform managed resources in the
142 * following order:
143 * 1) Regulator
144 * 2) Clocks (through ahci_platform_enable_clks)
Roger Quadros21b5fae2014-02-22 16:53:40 +0100145 * 3) Phy
Hans de Goede96a01ba2014-02-22 16:53:33 +0100146 *
147 * If resource enabling fails at any point the previous enabled resources
148 * are disabled in reverse order.
149 *
150 * RETURNS:
151 * 0 on success otherwise a negative error code
152 */
153int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
154{
155 int rc;
156
157 if (hpriv->target_pwr) {
158 rc = regulator_enable(hpriv->target_pwr);
159 if (rc)
160 return rc;
161 }
162
163 rc = ahci_platform_enable_clks(hpriv);
164 if (rc)
165 goto disable_regulator;
166
Roger Quadros21b5fae2014-02-22 16:53:40 +0100167 if (hpriv->phy) {
168 rc = phy_init(hpriv->phy);
169 if (rc)
170 goto disable_clks;
171
172 rc = phy_power_on(hpriv->phy);
173 if (rc) {
174 phy_exit(hpriv->phy);
175 goto disable_clks;
176 }
177 }
178
Hans de Goede96a01ba2014-02-22 16:53:33 +0100179 return 0;
180
Roger Quadros21b5fae2014-02-22 16:53:40 +0100181disable_clks:
182 ahci_platform_disable_clks(hpriv);
183
Hans de Goede96a01ba2014-02-22 16:53:33 +0100184disable_regulator:
185 if (hpriv->target_pwr)
186 regulator_disable(hpriv->target_pwr);
187 return rc;
188}
189EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
190
191/**
192 * ahci_platform_disable_resources - Disable platform resources
193 * @hpriv: host private area to store config values
194 *
195 * This function disables all ahci_platform managed resources in the
196 * following order:
Roger Quadros21b5fae2014-02-22 16:53:40 +0100197 * 1) Phy
198 * 2) Clocks (through ahci_platform_disable_clks)
199 * 3) Regulator
Hans de Goede96a01ba2014-02-22 16:53:33 +0100200 */
201void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
202{
Roger Quadros21b5fae2014-02-22 16:53:40 +0100203 if (hpriv->phy) {
204 phy_power_off(hpriv->phy);
205 phy_exit(hpriv->phy);
206 }
207
Hans de Goede96a01ba2014-02-22 16:53:33 +0100208 ahci_platform_disable_clks(hpriv);
209
210 if (hpriv->target_pwr)
211 regulator_disable(hpriv->target_pwr);
212}
213EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
214
Hans de Goede23b07d42014-02-22 16:53:34 +0100215static void ahci_platform_put_resources(struct device *dev, void *res)
Hans de Goede156c5882014-02-22 16:53:31 +0100216{
Hans de Goede23b07d42014-02-22 16:53:34 +0100217 struct ahci_host_priv *hpriv = res;
Hans de Goede156c5882014-02-22 16:53:31 +0100218 int c;
219
Roger Quadrose708e462014-02-22 16:53:41 +0100220 if (hpriv->got_runtime_pm) {
221 pm_runtime_put_sync(dev);
222 pm_runtime_disable(dev);
223 }
224
Hans de Goede156c5882014-02-22 16:53:31 +0100225 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
226 clk_put(hpriv->clks[c]);
227}
228
Hans de Goede23b07d42014-02-22 16:53:34 +0100229/**
230 * ahci_platform_get_resources - Get platform resources
231 * @pdev: platform device to get resources for
232 *
233 * This function allocates an ahci_host_priv struct, and gets the following
234 * resources, storing a reference to them inside the returned struct:
235 *
236 * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
237 * 2) regulator for controlling the targets power (optional)
238 * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
239 * or for non devicetree enabled platforms a single clock
Roger Quadros21b5fae2014-02-22 16:53:40 +0100240 * 4) phy (optional)
Hans de Goede23b07d42014-02-22 16:53:34 +0100241 *
242 * RETURNS:
243 * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
244 */
245struct ahci_host_priv *ahci_platform_get_resources(
246 struct platform_device *pdev)
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300247{
248 struct device *dev = &pdev->dev;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300249 struct ahci_host_priv *hpriv;
Hans de Goede156c5882014-02-22 16:53:31 +0100250 struct clk *clk;
Hans de Goede23b07d42014-02-22 16:53:34 +0100251 int i, rc = -ENOMEM;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300252
Hans de Goede23b07d42014-02-22 16:53:34 +0100253 if (!devres_open_group(dev, NULL, GFP_KERNEL))
254 return ERR_PTR(-ENOMEM);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300255
Hans de Goede23b07d42014-02-22 16:53:34 +0100256 hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
257 GFP_KERNEL);
258 if (!hpriv)
259 goto err_out;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300260
Hans de Goede23b07d42014-02-22 16:53:34 +0100261 devres_add(dev, hpriv);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300262
Hans de Goede23b07d42014-02-22 16:53:34 +0100263 hpriv->mmio = devm_ioremap_resource(dev,
264 platform_get_resource(pdev, IORESOURCE_MEM, 0));
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300265 if (!hpriv->mmio) {
Hans de Goede23b07d42014-02-22 16:53:34 +0100266 dev_err(dev, "no mmio space\n");
267 goto err_out;
Jassi Brar08354802010-06-25 18:21:19 +0900268 }
269
Hans de Goede4b3e6032014-02-22 16:53:32 +0100270 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)
Hans de Goede23b07d42014-02-22 16:53:34 +0100274 goto err_out;
Hans de Goede4b3e6032014-02-22 16:53:32 +0100275 hpriv->target_pwr = NULL;
276 }
277
Hans de Goede156c5882014-02-22 16:53:31 +0100278 for (i = 0; i < AHCI_MAX_CLKS; i++) {
279 /*
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).
284 */
285 if (i == 0)
286 clk = clk_get(dev, NULL);
287 else
288 clk = of_clk_get(dev->of_node, i);
289
290 if (IS_ERR(clk)) {
291 rc = PTR_ERR(clk);
292 if (rc == -EPROBE_DEFER)
Hans de Goede23b07d42014-02-22 16:53:34 +0100293 goto err_out;
Hans de Goede156c5882014-02-22 16:53:31 +0100294 break;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530295 }
Hans de Goede156c5882014-02-22 16:53:31 +0100296 hpriv->clks[i] = clk;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530297 }
298
Roger Quadros21b5fae2014-02-22 16:53:40 +0100299 hpriv->phy = devm_phy_get(dev, "sata-phy");
300 if (IS_ERR(hpriv->phy)) {
301 rc = PTR_ERR(hpriv->phy);
302 switch (rc) {
303 case -ENODEV:
304 case -ENOSYS:
305 /* continue normally */
306 hpriv->phy = NULL;
307 break;
308
309 case -EPROBE_DEFER:
310 goto err_out;
311
312 default:
313 dev_err(dev, "couldn't get sata-phy\n");
314 goto err_out;
315 }
316 }
317
Roger Quadrose708e462014-02-22 16:53:41 +0100318 pm_runtime_enable(dev);
319 pm_runtime_get_sync(dev);
320 hpriv->got_runtime_pm = true;
321
Hans de Goede23b07d42014-02-22 16:53:34 +0100322 devres_remove_group(dev, NULL);
323 return hpriv;
Hans de Goede156c5882014-02-22 16:53:31 +0100324
Hans de Goede23b07d42014-02-22 16:53:34 +0100325err_out:
326 devres_release_group(dev, NULL);
327 return ERR_PTR(rc);
328}
329EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
330
331/**
332 * ahci_platform_init_host - Bring up an ahci-platform host
333 * @pdev: platform device pointer for the host
334 * @hpriv: ahci-host private data for the host
335 * @pi_template: template for the ata_port_info to use
336 * @force_port_map: param passed to ahci_save_initial_config
337 * @mask_port_map: param passed to ahci_save_initial_config
338 *
339 * This function does all the usual steps needed to bring up an
340 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
341 * must be initialized / enabled before calling this.
342 *
343 * RETURNS:
344 * 0 on success otherwise a negative error code
345 */
346int ahci_platform_init_host(struct platform_device *pdev,
347 struct ahci_host_priv *hpriv,
348 const struct ata_port_info *pi_template,
349 unsigned int force_port_map,
350 unsigned int mask_port_map)
351{
352 struct device *dev = &pdev->dev;
353 struct ata_port_info pi = *pi_template;
354 const struct ata_port_info *ppi[] = { &pi, NULL };
355 struct ata_host *host;
356 int i, irq, n_ports, rc;
357
358 irq = platform_get_irq(pdev, 0);
359 if (irq <= 0) {
360 dev_err(dev, "no irq\n");
361 return -EINVAL;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300362 }
363
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300364 /* prepare host */
Hans de Goede23b07d42014-02-22 16:53:34 +0100365 hpriv->flags |= (unsigned long)pi.private_data;
366
367 ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
368
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300369 if (hpriv->cap & HOST_CAP_NCQ)
370 pi.flags |= ATA_FLAG_NCQ;
371
372 if (hpriv->cap & HOST_CAP_PMP)
373 pi.flags |= ATA_FLAG_PMP;
374
375 ahci_set_em_messages(hpriv, &pi);
376
377 /* CAP.NP sometimes indicate the index of the last enabled
378 * port, at other times, that of the last possible port, so
379 * determining the maximum port number requires looking at
380 * both CAP.NP and port_map.
381 */
382 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
383
384 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
Hans de Goede23b07d42014-02-22 16:53:34 +0100385 if (!host)
386 return -ENOMEM;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300387
388 host->private_data = hpriv;
389
390 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
391 host->flags |= ATA_HOST_PARALLEL_SCAN;
392 else
Jingoo Han0fed4c02013-10-05 09:15:59 +0900393 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300394
395 if (pi.flags & ATA_FLAG_EM)
396 ahci_reset_em(host);
397
398 for (i = 0; i < host->n_ports; i++) {
399 struct ata_port *ap = host->ports[i];
400
Hans de Goede23b07d42014-02-22 16:53:34 +0100401 ata_port_desc(ap, "mmio %pR",
402 platform_get_resource(pdev, IORESOURCE_MEM, 0));
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300403 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
404
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300405 /* set enclosure management message type */
406 if (ap->flags & ATA_FLAG_EM)
Jeff Garzik55787182010-05-14 17:23:37 -0400407 ap->em_message_type = hpriv->em_msg_type;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300408
409 /* disabled/not-implemented port */
410 if (!(hpriv->port_map & (1 << i)))
411 ap->ops = &ata_dummy_port_ops;
412 }
413
414 rc = ahci_reset_controller(host);
415 if (rc)
Hans de Goede23b07d42014-02-22 16:53:34 +0100416 return rc;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300417
418 ahci_init_controller(host);
419 ahci_print_info(host, "platform");
420
Hans de Goede23b07d42014-02-22 16:53:34 +0100421 return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
422 &ahci_platform_sht);
423}
424EXPORT_SYMBOL_GPL(ahci_platform_init_host);
425
426static int ahci_probe(struct platform_device *pdev)
427{
428 struct device *dev = &pdev->dev;
429 struct ahci_platform_data *pdata = dev_get_platdata(dev);
430 const struct platform_device_id *id = platform_get_device_id(pdev);
431 const struct ata_port_info *pi_template;
432 struct ahci_host_priv *hpriv;
433 int rc;
434
435 hpriv = ahci_platform_get_resources(pdev);
436 if (IS_ERR(hpriv))
437 return PTR_ERR(hpriv);
438
439 rc = ahci_platform_enable_resources(hpriv);
440 if (rc)
441 return rc;
442
443 /*
444 * Some platforms might need to prepare for mmio region access,
445 * which could be done in the following init call. So, the mmio
446 * region shouldn't be accessed before init (if provided) has
447 * returned successfully.
448 */
449 if (pdata && pdata->init) {
450 rc = pdata->init(dev, hpriv->mmio);
451 if (rc)
452 goto disable_resources;
453 }
454
455 if (pdata && pdata->ata_port_info)
456 pi_template = pdata->ata_port_info;
457 else
458 pi_template = &ahci_port_info[id ? id->driver_data : 0];
459
460 rc = ahci_platform_init_host(pdev, hpriv, pi_template,
461 pdata ? pdata->force_port_map : 0,
462 pdata ? pdata->mask_port_map : 0);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300463 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530464 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300465
466 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530467pdata_exit:
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300468 if (pdata && pdata->exit)
469 pdata->exit(dev);
Hans de Goede96a01ba2014-02-22 16:53:33 +0100470disable_resources:
471 ahci_platform_disable_resources(hpriv);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300472 return rc;
473}
474
Brian Norris1896b152012-11-02 00:46:17 -0700475static void ahci_host_stop(struct ata_host *host)
476{
477 struct device *dev = host->dev;
478 struct ahci_platform_data *pdata = dev_get_platdata(dev);
479 struct ahci_host_priv *hpriv = host->private_data;
480
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300481 if (pdata && pdata->exit)
482 pdata->exit(dev);
483
Hans de Goede96a01ba2014-02-22 16:53:33 +0100484 ahci_platform_disable_resources(hpriv);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300485}
486
Yuanhan Liu29448ec2012-10-16 22:59:01 +0800487#ifdef CONFIG_PM_SLEEP
Hans de Goede648cb6f2014-02-22 16:53:35 +0100488/**
489 * ahci_platform_suspend_host - Suspend an ahci-platform host
490 * @dev: device pointer for the host
491 *
492 * This function does all the usual steps needed to suspend an
493 * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
494 * must be disabled after calling this.
495 *
496 * RETURNS:
497 * 0 on success otherwise a negative error code
498 */
499int ahci_platform_suspend_host(struct device *dev)
Brian Norris17ab5942011-11-18 11:10:10 -0800500{
Brian Norris17ab5942011-11-18 11:10:10 -0800501 struct ata_host *host = dev_get_drvdata(dev);
502 struct ahci_host_priv *hpriv = host->private_data;
503 void __iomem *mmio = hpriv->mmio;
504 u32 ctl;
Brian Norris17ab5942011-11-18 11:10:10 -0800505
506 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
507 dev_err(dev, "firmware update required for suspend/resume\n");
508 return -EIO;
509 }
510
511 /*
512 * AHCI spec rev1.1 section 8.3.3:
513 * Software must disable interrupts prior to requesting a
514 * transition of the HBA to D3 state.
515 */
516 ctl = readl(mmio + HOST_CTL);
517 ctl &= ~HOST_IRQ_EN;
518 writel(ctl, mmio + HOST_CTL);
519 readl(mmio + HOST_CTL); /* flush */
520
Hans de Goede648cb6f2014-02-22 16:53:35 +0100521 return ata_host_suspend(host, PMSG_SUSPEND);
522}
523EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
524
525/**
526 * ahci_platform_resume_host - Resume an ahci-platform host
527 * @dev: device pointer for the host
528 *
529 * This function does all the usual steps needed to resume an ahci-platform
530 * host, note any necessary resources (ie clks, phy, etc.) must be
531 * initialized / enabled before calling this.
532 *
533 * RETURNS:
534 * 0 on success otherwise a negative error code
535 */
536int ahci_platform_resume_host(struct device *dev)
537{
538 struct ata_host *host = dev_get_drvdata(dev);
539 int rc;
540
541 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
542 rc = ahci_reset_controller(host);
543 if (rc)
544 return rc;
545
546 ahci_init_controller(host);
547 }
548
549 ata_host_resume(host);
550
551 return 0;
552}
553EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
554
555/**
556 * ahci_platform_suspend - Suspend an ahci-platform device
557 * @dev: the platform device to suspend
558 *
559 * This function suspends the host associated with the device, followed by
560 * disabling all the resources of the device.
561 *
562 * RETURNS:
563 * 0 on success otherwise a negative error code
564 */
565int ahci_platform_suspend(struct device *dev)
566{
567 struct ahci_platform_data *pdata = dev_get_platdata(dev);
568 struct ata_host *host = dev_get_drvdata(dev);
569 struct ahci_host_priv *hpriv = host->private_data;
570 int rc;
571
572 rc = ahci_platform_suspend_host(dev);
Brian Norris17ab5942011-11-18 11:10:10 -0800573 if (rc)
574 return rc;
575
576 if (pdata && pdata->suspend)
577 return pdata->suspend(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530578
Hans de Goede96a01ba2014-02-22 16:53:33 +0100579 ahci_platform_disable_resources(hpriv);
Hans de Goede4b3e6032014-02-22 16:53:32 +0100580
Brian Norris17ab5942011-11-18 11:10:10 -0800581 return 0;
582}
Hans de Goede648cb6f2014-02-22 16:53:35 +0100583EXPORT_SYMBOL_GPL(ahci_platform_suspend);
Brian Norris17ab5942011-11-18 11:10:10 -0800584
Hans de Goede648cb6f2014-02-22 16:53:35 +0100585/**
586 * ahci_platform_resume - Resume an ahci-platform device
587 * @dev: the platform device to resume
588 *
589 * This function enables all the resources of the device followed by
590 * resuming the host associated with the device.
591 *
592 * RETURNS:
593 * 0 on success otherwise a negative error code
594 */
595int ahci_platform_resume(struct device *dev)
Brian Norris17ab5942011-11-18 11:10:10 -0800596{
597 struct ahci_platform_data *pdata = dev_get_platdata(dev);
598 struct ata_host *host = dev_get_drvdata(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530599 struct ahci_host_priv *hpriv = host->private_data;
Brian Norris17ab5942011-11-18 11:10:10 -0800600 int rc;
601
Hans de Goede96a01ba2014-02-22 16:53:33 +0100602 rc = ahci_platform_enable_resources(hpriv);
Hans de Goede156c5882014-02-22 16:53:31 +0100603 if (rc)
Hans de Goede96a01ba2014-02-22 16:53:33 +0100604 return rc;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530605
Brian Norris17ab5942011-11-18 11:10:10 -0800606 if (pdata && pdata->resume) {
607 rc = pdata->resume(dev);
608 if (rc)
Hans de Goede96a01ba2014-02-22 16:53:33 +0100609 goto disable_resources;
Brian Norris17ab5942011-11-18 11:10:10 -0800610 }
611
Hans de Goede648cb6f2014-02-22 16:53:35 +0100612 rc = ahci_platform_resume_host(dev);
613 if (rc)
614 goto disable_resources;
Brian Norris17ab5942011-11-18 11:10:10 -0800615
Roger Quadrose708e462014-02-22 16:53:41 +0100616 /* We resumed so update PM runtime state */
617 pm_runtime_disable(dev);
618 pm_runtime_set_active(dev);
619 pm_runtime_enable(dev);
620
Brian Norris17ab5942011-11-18 11:10:10 -0800621 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530622
Hans de Goede96a01ba2014-02-22 16:53:33 +0100623disable_resources:
624 ahci_platform_disable_resources(hpriv);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530625
626 return rc;
Brian Norris17ab5942011-11-18 11:10:10 -0800627}
Hans de Goede648cb6f2014-02-22 16:53:35 +0100628EXPORT_SYMBOL_GPL(ahci_platform_resume);
Brian Norris17ab5942011-11-18 11:10:10 -0800629#endif
630
Hans de Goede648cb6f2014-02-22 16:53:35 +0100631static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
632 ahci_platform_resume);
Shiraz Hashim18c25ff2012-03-16 15:49:55 +0530633
Rob Herring02aac312010-11-03 21:04:59 -0500634static const struct of_device_id ahci_of_match[] = {
Viresh Kumar5f098a32012-04-21 17:40:12 +0530635 { .compatible = "snps,spear-ahci", },
Girish K S1e8f5f72013-04-16 14:58:02 +0530636 { .compatible = "snps,exynos5440-ahci", },
Alistair Popple2435dcb2013-11-22 13:08:29 +1100637 { .compatible = "ibm,476gtr-ahci", },
Roger Quadrosc4311472014-02-22 16:53:38 +0100638 { .compatible = "snps,dwc-ahci", },
Rob Herring02aac312010-11-03 21:04:59 -0500639 {},
640};
641MODULE_DEVICE_TABLE(of, ahci_of_match);
642
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300643static struct platform_driver ahci_driver = {
Brian Norris941c77f2012-11-02 00:46:15 -0700644 .probe = ahci_probe,
Brian Norris83291d62012-11-02 00:46:19 -0700645 .remove = ata_platform_remove_one,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300646 .driver = {
647 .name = "ahci",
648 .owner = THIS_MODULE,
Rob Herring02aac312010-11-03 21:04:59 -0500649 .of_match_table = ahci_of_match,
Brian Norris17ab5942011-11-18 11:10:10 -0800650 .pm = &ahci_pm_ops,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300651 },
Richard Zhu904c04f2011-09-28 15:41:54 +0800652 .id_table = ahci_devtype,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300653};
Brian Norris9a99e472012-11-02 00:46:16 -0700654module_platform_driver(ahci_driver);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300655
656MODULE_DESCRIPTION("AHCI SATA platform driver");
657MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
658MODULE_LICENSE("GPL");
659MODULE_ALIAS("platform:ahci");