c4d918d5da515e038cb1ba17819d7d8d7807b886
[openwrt.git] / target / linux / sunxi / patches-3.14 / 190-ahci-libahci-changes.patch
1 From 3b5db9d024f173c30ef4060c31bb8e9fbd194cc1 Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Mon, 2 Dec 2013 16:13:32 +0100
4 Subject: [PATCH] libahci: Allow drivers to override start_engine
5
6 Allwinner A10 and A20 ARM SoCs have an AHCI sata controller which needs a
7 special register to be poked before starting the DMA engine.
8
9 This register gets reset on an ahci_stop_engine call, so there is no other
10 place then ahci_start_engine where this poking can be done.
11
12 This commit allows drivers to override ahci_start_engine behavior for use by
13 the Allwinner AHCI driver (and potentially other drivers in the future).
14
15 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
16 ---
17  drivers/ata/ahci.c          |  6 ++++--
18  drivers/ata/ahci.h          |  6 ++++++
19  drivers/ata/libahci.c       | 26 +++++++++++++++++++-------
20  drivers/ata/sata_highbank.c |  3 ++-
21  4 files changed, 31 insertions(+), 10 deletions(-)
22
23 --- a/drivers/ata/ahci.c
24 +++ b/drivers/ata/ahci.c
25 @@ -606,6 +606,7 @@ static int ahci_vt8251_hardreset(struct 
26                                  unsigned long deadline)
27  {
28         struct ata_port *ap = link->ap;
29 +       struct ahci_host_priv *hpriv = ap->host->private_data;
30         bool online;
31         int rc;
32  
33 @@ -616,7 +617,7 @@ static int ahci_vt8251_hardreset(struct 
34         rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
35                                  deadline, &online, NULL);
36  
37 -       ahci_start_engine(ap);
38 +       hpriv->start_engine(ap);
39  
40         DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
41  
42 @@ -631,6 +632,7 @@ static int ahci_p5wdh_hardreset(struct a
43  {
44         struct ata_port *ap = link->ap;
45         struct ahci_port_priv *pp = ap->private_data;
46 +       struct ahci_host_priv *hpriv = ap->host->private_data;
47         u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
48         struct ata_taskfile tf;
49         bool online;
50 @@ -646,7 +648,7 @@ static int ahci_p5wdh_hardreset(struct a
51         rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
52                                  deadline, &online, NULL);
53  
54 -       ahci_start_engine(ap);
55 +       hpriv->start_engine(ap);
56  
57         /* The pseudo configuration device on SIMG4726 attached to
58          * ASUS P5W-DH Deluxe doesn't send signature FIS after
59 --- a/drivers/ata/ahci.h
60 +++ b/drivers/ata/ahci.h
61 @@ -37,6 +37,7 @@
62  
63  #include <linux/clk.h>
64  #include <linux/libata.h>
65 +#include <linux/regulator/consumer.h>
66  
67  /* Enclosure Management Control */
68  #define EM_CTRL_MSG_TYPE              0x000f0000
69 @@ -51,6 +52,7 @@
70  
71  enum {
72         AHCI_MAX_PORTS          = 32,
73 +       AHCI_MAX_CLKS           = 3,
74         AHCI_MAX_SG             = 168, /* hardware max is 64K */
75         AHCI_DMA_BOUNDARY       = 0xffffffff,
76         AHCI_MAX_CMDS           = 32,
77 @@ -322,8 +324,15 @@ struct ahci_host_priv {
78         u32                     em_loc; /* enclosure management location */
79         u32                     em_buf_sz;      /* EM buffer size in byte */
80         u32                     em_msg_type;    /* EM message type */
81 -       struct clk              *clk;           /* Only for platforms supporting clk */
82 +       struct clk              *clks[AHCI_MAX_CLKS]; /* Optional */
83 +       struct regulator        *target_pwr;    /* Optional */
84         void                    *plat_data;     /* Other platform data */
85 +       /*
86 +        * Optional ahci_start_engine override, if not set this gets set to the
87 +        * default ahci_start_engine during ahci_save_initial_config, this can
88 +        * be overridden anytime before the host is activated.
89 +        */
90 +       void                    (*start_engine)(struct ata_port *ap);
91  };
92  
93  extern int ahci_ignore_sss;
94 --- a/drivers/ata/libahci.c
95 +++ b/drivers/ata/libahci.c
96 @@ -394,6 +394,9 @@ static ssize_t ahci_show_em_supported(st
97   *
98   *     If inconsistent, config values are fixed up by this function.
99   *
100 + *     If it is not set already this function sets hpriv->start_engine to
101 + *     ahci_start_engine.
102 + *
103   *     LOCKING:
104   *     None.
105   */
106 @@ -500,6 +503,9 @@ void ahci_save_initial_config(struct dev
107         hpriv->cap = cap;
108         hpriv->cap2 = cap2;
109         hpriv->port_map = port_map;
110 +
111 +       if (!hpriv->start_engine)
112 +               hpriv->start_engine = ahci_start_engine;
113  }
114  EXPORT_SYMBOL_GPL(ahci_save_initial_config);
115  
116 @@ -766,7 +772,7 @@ static void ahci_start_port(struct ata_p
117  
118         /* enable DMA */
119         if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
120 -               ahci_start_engine(ap);
121 +               hpriv->start_engine(ap);
122  
123         /* turn on LEDs */
124         if (ap->flags & ATA_FLAG_EM) {
125 @@ -1234,7 +1240,7 @@ int ahci_kick_engine(struct ata_port *ap
126  
127         /* restart engine */
128   out_restart:
129 -       ahci_start_engine(ap);
130 +       hpriv->start_engine(ap);
131         return rc;
132  }
133  EXPORT_SYMBOL_GPL(ahci_kick_engine);
134 @@ -1426,6 +1432,7 @@ static int ahci_hardreset(struct ata_lin
135         const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
136         struct ata_port *ap = link->ap;
137         struct ahci_port_priv *pp = ap->private_data;
138 +       struct ahci_host_priv *hpriv = ap->host->private_data;
139         u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
140         struct ata_taskfile tf;
141         bool online;
142 @@ -1443,7 +1450,7 @@ static int ahci_hardreset(struct ata_lin
143         rc = sata_link_hardreset(link, timing, deadline, &online,
144                                  ahci_check_ready);
145  
146 -       ahci_start_engine(ap);
147 +       hpriv->start_engine(ap);
148  
149         if (online)
150                 *class = ahci_dev_classify(ap);
151 @@ -2007,10 +2014,12 @@ static void ahci_thaw(struct ata_port *a
152  
153  void ahci_error_handler(struct ata_port *ap)
154  {
155 +       struct ahci_host_priv *hpriv = ap->host->private_data;
156 +
157         if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
158                 /* restart engine */
159                 ahci_stop_engine(ap);
160 -               ahci_start_engine(ap);
161 +               hpriv->start_engine(ap);
162         }
163  
164         sata_pmp_error_handler(ap);
165 @@ -2031,6 +2040,7 @@ static void ahci_post_internal_cmd(struc
166  
167  static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
168  {
169 +       struct ahci_host_priv *hpriv = ap->host->private_data;
170         void __iomem *port_mmio = ahci_port_base(ap);
171         struct ata_device *dev = ap->link.device;
172         u32 devslp, dm, dito, mdat, deto;
173 @@ -2094,7 +2104,7 @@ static void ahci_set_aggressive_devslp(s
174                    PORT_DEVSLP_ADSE);
175         writel(devslp, port_mmio + PORT_DEVSLP);
176  
177 -       ahci_start_engine(ap);
178 +       hpriv->start_engine(ap);
179  
180         /* enable device sleep feature for the drive */
181         err_mask = ata_dev_set_feature(dev,
182 @@ -2106,6 +2116,7 @@ static void ahci_set_aggressive_devslp(s
183  
184  static void ahci_enable_fbs(struct ata_port *ap)
185  {
186 +       struct ahci_host_priv *hpriv = ap->host->private_data;
187         struct ahci_port_priv *pp = ap->private_data;
188         void __iomem *port_mmio = ahci_port_base(ap);
189         u32 fbs;
190 @@ -2134,11 +2145,12 @@ static void ahci_enable_fbs(struct ata_p
191         } else
192                 dev_err(ap->host->dev, "Failed to enable FBS\n");
193  
194 -       ahci_start_engine(ap);
195 +       hpriv->start_engine(ap);
196  }
197  
198  static void ahci_disable_fbs(struct ata_port *ap)
199  {
200 +       struct ahci_host_priv *hpriv = ap->host->private_data;
201         struct ahci_port_priv *pp = ap->private_data;
202         void __iomem *port_mmio = ahci_port_base(ap);
203         u32 fbs;
204 @@ -2166,7 +2178,7 @@ static void ahci_disable_fbs(struct ata_
205                 pp->fbs_enabled = false;
206         }
207  
208 -       ahci_start_engine(ap);
209 +       hpriv->start_engine(ap);
210  }
211  
212  static void ahci_pmp_attach(struct ata_port *ap)
213 --- a/drivers/ata/sata_highbank.c
214 +++ b/drivers/ata/sata_highbank.c
215 @@ -403,6 +403,7 @@ static int ahci_highbank_hardreset(struc
216         static const unsigned long timing[] = { 5, 100, 500};
217         struct ata_port *ap = link->ap;
218         struct ahci_port_priv *pp = ap->private_data;
219 +       struct ahci_host_priv *hpriv = ap->host->private_data;
220         u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
221         struct ata_taskfile tf;
222         bool online;
223 @@ -431,7 +432,7 @@ static int ahci_highbank_hardreset(struc
224                         break;
225         } while (!online && retry--);
226  
227 -       ahci_start_engine(ap);
228 +       hpriv->start_engine(ap);
229  
230         if (online)
231                 *class = ahci_dev_classify(ap);
232 --- a/Documentation/devicetree/bindings/ata/ahci-platform.txt
233 +++ b/Documentation/devicetree/bindings/ata/ahci-platform.txt
234 @@ -10,6 +10,8 @@ Required properties:
235  
236  Optional properties:
237  - dma-coherent      : Present if dma operations are coherent
238 +- clocks            : a list of phandle + clock specifier pairs
239 +- target-supply     : regulator for SATA target power
240  
241  Example:
242          sata@ffe08000 {
243 --- a/drivers/ata/ahci_platform.c
244 +++ b/drivers/ata/ahci_platform.c
245 @@ -87,78 +87,252 @@ static struct scsi_host_template ahci_pl
246         AHCI_SHT("ahci_platform"),
247  };
248  
249 -static int ahci_probe(struct platform_device *pdev)
250 +/**
251 + *     ahci_platform_enable_clks - Enable platform clocks
252 + *     @hpriv: host private area to store config values
253 + *
254 + *     This function enables all the clks found in hpriv->clks, starting
255 + *     at index 0. If any clk fails to enable it disables all the clks
256 + *     already enabled in reverse order, and then returns an error.
257 + *
258 + *     LOCKING:
259 + *     None.
260 + *
261 + *     RETURNS:
262 + *     0 on success otherwise a negative error code
263 + */
264 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
265  {
266 -       struct device *dev = &pdev->dev;
267 -       struct ahci_platform_data *pdata = dev_get_platdata(dev);
268 -       const struct platform_device_id *id = platform_get_device_id(pdev);
269 -       struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
270 -       const struct ata_port_info *ppi[] = { &pi, NULL };
271 -       struct ahci_host_priv *hpriv;
272 -       struct ata_host *host;
273 -       struct resource *mem;
274 -       int irq;
275 -       int n_ports;
276 -       int i;
277 -       int rc;
278 +       int c, rc;
279  
280 -       mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 -       if (!mem) {
282 -               dev_err(dev, "no mmio space\n");
283 -               return -EINVAL;
284 +       for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
285 +               rc = clk_prepare_enable(hpriv->clks[c]);
286 +               if (rc)
287 +                       goto disable_unprepare_clk;
288         }
289 +       return 0;
290  
291 -       irq = platform_get_irq(pdev, 0);
292 -       if (irq <= 0) {
293 -               dev_err(dev, "no irq\n");
294 -               return -EINVAL;
295 -       }
296 +disable_unprepare_clk:
297 +       while (--c >= 0)
298 +               clk_disable_unprepare(hpriv->clks[c]);
299 +       return rc;
300 +}
301 +EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
302  
303 -       if (pdata && pdata->ata_port_info)
304 -               pi = *pdata->ata_port_info;
305 +/**
306 + *     ahci_platform_disable_clks - Disable platform clocks
307 + *     @hpriv: host private area to store config values
308 + *
309 + *     This function disables all the clks found in hpriv->clks, in reverse
310 + *     order of ahci_platform_enable_clks (starting at the end of the array).
311 + *
312 + *     LOCKING:
313 + *     None.
314 + */
315 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
316 +{
317 +       int c;
318  
319 -       hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
320 -       if (!hpriv) {
321 -               dev_err(dev, "can't alloc ahci_host_priv\n");
322 -               return -ENOMEM;
323 +       for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
324 +               if (hpriv->clks[c])
325 +                       clk_disable_unprepare(hpriv->clks[c]);
326 +}
327 +EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
328 +
329 +/**
330 + *     ahci_platform_enable_resources - Enable platform resources
331 + *     @hpriv: host private area to store config values
332 + *
333 + *     This function enables all ahci_platform managed resources in
334 + *     the following order:
335 + *     1) Regulator
336 + *     2) Clocks (through ahci_platform_enable_clks)
337 + *
338 + *     If resource enabling fails at any point the previous enabled
339 + *     resources are disabled in reverse order.
340 + *
341 + *     LOCKING:
342 + *     None.
343 + *
344 + *     RETURNS:
345 + *     0 on success otherwise a negative error code
346 + */
347 +int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
348 +{
349 +       int rc;
350 +
351 +       if (hpriv->target_pwr) {
352 +               rc = regulator_enable(hpriv->target_pwr);
353 +               if (rc)
354 +                       return rc;
355         }
356  
357 -       hpriv->flags |= (unsigned long)pi.private_data;
358 +       rc = ahci_platform_enable_clks(hpriv);
359 +       if (rc)
360 +               goto disable_regulator;
361 +
362 +       return 0;
363 +
364 +disable_regulator:
365 +       if (hpriv->target_pwr)
366 +               regulator_disable(hpriv->target_pwr);
367 +       return rc;
368 +}
369 +EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
370 +
371 +/**
372 + *     ahci_platform_disable_resources - Disable platform resources
373 + *     @hpriv: host private area to store config values
374 + *
375 + *     This function disables all ahci_platform managed resources in
376 + *     the following order:
377 + *     1) Clocks (through ahci_platform_disable_clks)
378 + *     2) Regulator
379 + *
380 + *     LOCKING:
381 + *     None.
382 + */
383 +void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
384 +{
385 +       ahci_platform_disable_clks(hpriv);
386  
387 -       hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
388 +       if (hpriv->target_pwr)
389 +               regulator_disable(hpriv->target_pwr);
390 +}
391 +EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
392 +
393 +static void ahci_platform_put_resources(struct device *dev, void *res)
394 +{
395 +       struct ahci_host_priv *hpriv = res;
396 +       int c;
397 +
398 +       for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
399 +               clk_put(hpriv->clks[c]);
400 +}
401 +
402 +/**
403 + *     ahci_platform_get_resources - Get platform resources
404 + *     @pdev: platform device to get resources for
405 + *
406 + *     This function allocates an ahci_host_priv struct, and gets the
407 + *     following resources, storing a reference to them inside the returned
408 + *     struct:
409 + *
410 + *     1) mmio registers (IORESOURCE_MEM 0, mandatory)
411 + *     2) regulator for controlling the targets power (optional)
412 + *     3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
413 + *        or for non devicetree enabled platforms a single clock
414 + *
415 + *     LOCKING:
416 + *     None.
417 + *
418 + *     RETURNS:
419 + *     The allocated ahci_host_priv on success, otherwise an ERR_PTR value
420 + */
421 +struct ahci_host_priv *ahci_platform_get_resources(
422 +       struct platform_device *pdev)
423 +{
424 +       struct device *dev = &pdev->dev;
425 +       struct ahci_host_priv *hpriv;
426 +       struct clk *clk;
427 +       int i, rc = -ENOMEM;
428 +
429 +       if (!devres_open_group(dev, NULL, GFP_KERNEL))
430 +               return ERR_PTR(-ENOMEM);
431 +
432 +       hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
433 +                            GFP_KERNEL);
434 +       if (!hpriv)
435 +               goto err_out;
436 +
437 +       devres_add(dev, hpriv);
438 +
439 +       hpriv->mmio = devm_ioremap_resource(dev,
440 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
441         if (!hpriv->mmio) {
442 -               dev_err(dev, "can't map %pR\n", mem);
443 -               return -ENOMEM;
444 +               dev_err(dev, "no mmio space\n");
445 +               goto err_out;
446         }
447  
448 -       hpriv->clk = clk_get(dev, NULL);
449 -       if (IS_ERR(hpriv->clk)) {
450 -               dev_err(dev, "can't get clock\n");
451 -       } else {
452 -               rc = clk_prepare_enable(hpriv->clk);
453 -               if (rc) {
454 -                       dev_err(dev, "clock prepare enable failed");
455 -                       goto free_clk;
456 +       hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
457 +       if (IS_ERR(hpriv->target_pwr)) {
458 +               rc = PTR_ERR(hpriv->target_pwr);
459 +               if (rc == -EPROBE_DEFER)
460 +                       goto err_out;
461 +               hpriv->target_pwr = NULL;
462 +       }
463 +
464 +       for (i = 0; i < AHCI_MAX_CLKS; i++) {
465 +               /*
466 +                * For now we must use clk_get(dev, NULL) for the first clock,
467 +                * because some platforms (da850, spear13xx) are not yet
468 +                * converted to use devicetree for clocks.  For new platforms
469 +                * this is equivalent to of_clk_get(dev->of_node, 0).
470 +                */
471 +               if (i == 0)
472 +                       clk = clk_get(dev, NULL);
473 +               else
474 +                       clk = of_clk_get(dev->of_node, i);
475 +
476 +               if (IS_ERR(clk)) {
477 +                       rc = PTR_ERR(clk);
478 +                       if (rc == -EPROBE_DEFER)
479 +                               goto err_out;
480 +                       break;
481                 }
482 +               hpriv->clks[i] = clk;
483         }
484  
485 -       /*
486 -        * Some platforms might need to prepare for mmio region access,
487 -        * which could be done in the following init call. So, the mmio
488 -        * region shouldn't be accessed before init (if provided) has
489 -        * returned successfully.
490 -        */
491 -       if (pdata && pdata->init) {
492 -               rc = pdata->init(dev, hpriv->mmio);
493 -               if (rc)
494 -                       goto disable_unprepare_clk;
495 -       }
496 +       devres_remove_group(dev, NULL);
497 +       return hpriv;
498 +
499 +err_out:
500 +       devres_release_group(dev, NULL);
501 +       return ERR_PTR(rc);
502 +}
503 +EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
504 +
505 +/**
506 + *     ahci_platform_init_host - Bring up an ahci-platform host
507 + *     @pdev: platform device pointer for the host
508 + *     @hpriv: ahci-host private data for the host
509 + *     @pi_template: template for the ata_port_info to use
510 + *     @force_port_map: param passed to ahci_save_initial_config
511 + *     @mask_port_map: param passed to ahci_save_initial_config
512 + *
513 + *     This function does all the usual steps needed to bring up an
514 + *     ahci-platform host, note any necessary resources (ie clks, phy, etc.)
515 + *     must be initialized / enabled before calling this.
516 + *
517 + *     LOCKING:
518 + *     None.
519 + *
520 + *     RETURNS:
521 + *     0 on success otherwise a negative error code
522 + */
523 +int ahci_platform_init_host(struct platform_device *pdev,
524 +                           struct ahci_host_priv *hpriv,
525 +                           const struct ata_port_info *pi_template,
526 +                           unsigned int force_port_map,
527 +                           unsigned int mask_port_map)
528 +{
529 +       struct device *dev = &pdev->dev;
530 +       struct ata_port_info pi = *pi_template;
531 +       const struct ata_port_info *ppi[] = { &pi, NULL };
532 +       struct ata_host *host;
533 +       int i, irq, n_ports, rc;
534  
535 -       ahci_save_initial_config(dev, hpriv,
536 -               pdata ? pdata->force_port_map : 0,
537 -               pdata ? pdata->mask_port_map  : 0);
538 +       irq = platform_get_irq(pdev, 0);
539 +       if (irq <= 0) {
540 +               dev_err(dev, "no irq\n");
541 +               return -EINVAL;
542 +       }
543  
544         /* prepare host */
545 +       hpriv->flags |= (unsigned long)pi.private_data;
546 +
547 +       ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
548 +
549         if (hpriv->cap & HOST_CAP_NCQ)
550                 pi.flags |= ATA_FLAG_NCQ;
551  
552 @@ -175,10 +349,8 @@ static int ahci_probe(struct platform_de
553         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
554  
555         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
556 -       if (!host) {
557 -               rc = -ENOMEM;
558 -               goto pdata_exit;
559 -       }
560 +       if (!host)
561 +               return -ENOMEM;
562  
563         host->private_data = hpriv;
564  
565 @@ -193,7 +365,8 @@ static int ahci_probe(struct platform_de
566         for (i = 0; i < host->n_ports; i++) {
567                 struct ata_port *ap = host->ports[i];
568  
569 -               ata_port_desc(ap, "mmio %pR", mem);
570 +               ata_port_desc(ap, "mmio %pR",
571 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
572                 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
573  
574                 /* set enclosure management message type */
575 @@ -207,13 +380,53 @@ static int ahci_probe(struct platform_de
576  
577         rc = ahci_reset_controller(host);
578         if (rc)
579 -               goto pdata_exit;
580 +               return rc;
581  
582         ahci_init_controller(host);
583         ahci_print_info(host, "platform");
584  
585 -       rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
586 -                              &ahci_platform_sht);
587 +       return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
588 +                                &ahci_platform_sht);
589 +}
590 +EXPORT_SYMBOL_GPL(ahci_platform_init_host);
591 +
592 +static int ahci_probe(struct platform_device *pdev)
593 +{
594 +       struct device *dev = &pdev->dev;
595 +       struct ahci_platform_data *pdata = dev_get_platdata(dev);
596 +       const struct platform_device_id *id = platform_get_device_id(pdev);
597 +       const struct ata_port_info *pi_template;
598 +       struct ahci_host_priv *hpriv;
599 +       int rc;
600 +
601 +       hpriv = ahci_platform_get_resources(pdev);
602 +       if (IS_ERR(hpriv))
603 +               return PTR_ERR(hpriv);
604 +
605 +       rc = ahci_platform_enable_resources(hpriv);
606 +       if (rc)
607 +               return rc;
608 +
609 +       /*
610 +        * Some platforms might need to prepare for mmio region access,
611 +        * which could be done in the following init call. So, the mmio
612 +        * region shouldn't be accessed before init (if provided) has
613 +        * returned successfully.
614 +        */
615 +       if (pdata && pdata->init) {
616 +               rc = pdata->init(dev, hpriv->mmio);
617 +               if (rc)
618 +                       goto disable_resources;
619 +       }
620 +
621 +       if (pdata && pdata->ata_port_info)
622 +               pi_template = pdata->ata_port_info;
623 +       else
624 +               pi_template = &ahci_port_info[id ? id->driver_data : 0];
625 +
626 +       rc = ahci_platform_init_host(pdev, hpriv, pi_template,
627 +                                    pdata ? pdata->force_port_map : 0,
628 +                                    pdata ? pdata->mask_port_map  : 0);
629         if (rc)
630                 goto pdata_exit;
631  
632 @@ -221,12 +434,8 @@ static int ahci_probe(struct platform_de
633  pdata_exit:
634         if (pdata && pdata->exit)
635                 pdata->exit(dev);
636 -disable_unprepare_clk:
637 -       if (!IS_ERR(hpriv->clk))
638 -               clk_disable_unprepare(hpriv->clk);
639 -free_clk:
640 -       if (!IS_ERR(hpriv->clk))
641 -               clk_put(hpriv->clk);
642 +disable_resources:
643 +       ahci_platform_disable_resources(hpriv);
644         return rc;
645  }
646  
647 @@ -239,21 +448,30 @@ static void ahci_host_stop(struct ata_ho
648         if (pdata && pdata->exit)
649                 pdata->exit(dev);
650  
651 -       if (!IS_ERR(hpriv->clk)) {
652 -               clk_disable_unprepare(hpriv->clk);
653 -               clk_put(hpriv->clk);
654 -       }
655 +       ahci_platform_disable_resources(hpriv);
656  }
657  
658  #ifdef CONFIG_PM_SLEEP
659 -static int ahci_suspend(struct device *dev)
660 +/**
661 + *     ahci_platform_suspend_host - Suspend an ahci-platform host
662 + *     @dev: device pointer for the host
663 + *
664 + *     This function does all the usual steps needed to suspend an
665 + *     ahci-platform host, note any necessary resources (ie clks, phy, etc.)
666 + *     must be disabled after calling this.
667 + *
668 + *     LOCKING:
669 + *     None.
670 + *
671 + *     RETURNS:
672 + *     0 on success otherwise a negative error code
673 + */
674 +int ahci_platform_suspend_host(struct device *dev)
675  {
676 -       struct ahci_platform_data *pdata = dev_get_platdata(dev);
677         struct ata_host *host = dev_get_drvdata(dev);
678         struct ahci_host_priv *hpriv = host->private_data;
679         void __iomem *mmio = hpriv->mmio;
680         u32 ctl;
681 -       int rc;
682  
683         if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
684                 dev_err(dev, "firmware update required for suspend/resume\n");
685 @@ -270,61 +488,122 @@ static int ahci_suspend(struct device *d
686         writel(ctl, mmio + HOST_CTL);
687         readl(mmio + HOST_CTL); /* flush */
688  
689 -       rc = ata_host_suspend(host, PMSG_SUSPEND);
690 +       return ata_host_suspend(host, PMSG_SUSPEND);
691 +}
692 +EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
693 +
694 +/**
695 + *     ahci_platform_resume_host - Resume an ahci-platform host
696 + *     @dev: device pointer for the host
697 + *
698 + *     This function does all the usual steps needed to resume an
699 + *     ahci-platform host, note any necessary resources (ie clks, phy, etc.)
700 + *     must be initialized / enabled before calling this.
701 + *
702 + *     LOCKING:
703 + *     None.
704 + *
705 + *     RETURNS:
706 + *     0 on success otherwise a negative error code
707 + */
708 +int ahci_platform_resume_host(struct device *dev)
709 +{
710 +       struct ata_host *host = dev_get_drvdata(dev);
711 +       int rc;
712 +
713 +       if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
714 +               rc = ahci_reset_controller(host);
715 +               if (rc)
716 +                       return rc;
717 +
718 +               ahci_init_controller(host);
719 +       }
720 +
721 +       ata_host_resume(host);
722 +
723 +       return 0;
724 +}
725 +EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
726 +
727 +/**
728 + *     ahci_platform_suspend - Suspend an ahci-platform device
729 + *     @dev: the platform device to suspend
730 + *
731 + *     This function suspends the host associated with the device, followed
732 + *     by disabling all the resources of the device.
733 + *
734 + *     LOCKING:
735 + *     None.
736 + *
737 + *     RETURNS:
738 + *     0 on success otherwise a negative error code
739 + */
740 +int ahci_platform_suspend(struct device *dev)
741 +{
742 +       struct ahci_platform_data *pdata = dev_get_platdata(dev);
743 +       struct ata_host *host = dev_get_drvdata(dev);
744 +       struct ahci_host_priv *hpriv = host->private_data;
745 +       int rc;
746 +
747 +       rc = ahci_platform_suspend_host(dev);
748         if (rc)
749                 return rc;
750  
751         if (pdata && pdata->suspend)
752                 return pdata->suspend(dev);
753  
754 -       if (!IS_ERR(hpriv->clk))
755 -               clk_disable_unprepare(hpriv->clk);
756 +       ahci_platform_disable_resources(hpriv);
757  
758         return 0;
759  }
760 +EXPORT_SYMBOL_GPL(ahci_platform_suspend);
761  
762 -static int ahci_resume(struct device *dev)
763 +/**
764 + *     ahci_platform_resume - Resume an ahci-platform device
765 + *     @dev: the platform device to resume
766 + *
767 + *     This function enables all the resources of the device followed by
768 + *     resuming the host associated with the device.
769 + *
770 + *     LOCKING:
771 + *     None.
772 + *
773 + *     RETURNS:
774 + *     0 on success otherwise a negative error code
775 + */
776 +int ahci_platform_resume(struct device *dev)
777  {
778         struct ahci_platform_data *pdata = dev_get_platdata(dev);
779         struct ata_host *host = dev_get_drvdata(dev);
780         struct ahci_host_priv *hpriv = host->private_data;
781         int rc;
782  
783 -       if (!IS_ERR(hpriv->clk)) {
784 -               rc = clk_prepare_enable(hpriv->clk);
785 -               if (rc) {
786 -                       dev_err(dev, "clock prepare enable failed");
787 -                       return rc;
788 -               }
789 -       }
790 +       rc = ahci_platform_enable_resources(hpriv);
791 +       if (rc)
792 +               return rc;
793  
794         if (pdata && pdata->resume) {
795                 rc = pdata->resume(dev);
796                 if (rc)
797 -                       goto disable_unprepare_clk;
798 -       }
799 -
800 -       if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
801 -               rc = ahci_reset_controller(host);
802 -               if (rc)
803 -                       goto disable_unprepare_clk;
804 -
805 -               ahci_init_controller(host);
806 +                       goto disable_resources;
807         }
808  
809 -       ata_host_resume(host);
810 +       rc = ahci_platform_resume_host(dev);
811 +       if (rc)
812 +               goto disable_resources;
813  
814         return 0;
815  
816 -disable_unprepare_clk:
817 -       if (!IS_ERR(hpriv->clk))
818 -               clk_disable_unprepare(hpriv->clk);
819 +disable_resources:
820 +       ahci_platform_disable_resources(hpriv);
821  
822         return rc;
823  }
824 +EXPORT_SYMBOL_GPL(ahci_platform_resume);
825  #endif
826  
827 -static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
828 +static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
829 +                        ahci_platform_resume);
830  
831  static const struct of_device_id ahci_of_match[] = {
832         { .compatible = "snps,spear-ahci", },
833 --- a/include/linux/ahci_platform.h
834 +++ b/include/linux/ahci_platform.h
835 @@ -19,7 +19,15 @@
836  
837  struct device;
838  struct ata_port_info;
839 +struct ahci_host_priv;
840 +struct platform_device;
841  
842 +/*
843 + * Note ahci_platform_data is deprecated, it is only kept around for use
844 + * by the old da850 and spear13xx ahci code.
845 + * New drivers should instead declare their own platform_driver struct, and
846 + * use ahci_platform* functions in their own probe, suspend and resume methods.
847 + */
848  struct ahci_platform_data {
849         int (*init)(struct device *dev, void __iomem *addr);
850         void (*exit)(struct device *dev);
851 @@ -30,4 +38,21 @@ struct ahci_platform_data {
852         unsigned int mask_port_map;
853  };
854  
855 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv);
856 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
857 +int ahci_platform_enable_resources(struct ahci_host_priv *hpriv);
858 +void ahci_platform_disable_resources(struct ahci_host_priv *hpriv);
859 +struct ahci_host_priv *ahci_platform_get_resources(
860 +       struct platform_device *pdev);
861 +int ahci_platform_init_host(struct platform_device *pdev,
862 +                           struct ahci_host_priv *hpriv,
863 +                           const struct ata_port_info *pi_template,
864 +                           unsigned int force_port_map,
865 +                           unsigned int mask_port_map);
866 +
867 +int ahci_platform_suspend_host(struct device *dev);
868 +int ahci_platform_resume_host(struct device *dev);
869 +int ahci_platform_suspend(struct device *dev);
870 +int ahci_platform_resume(struct device *dev);
871 +
872  #endif /* _AHCI_PLATFORM_H */