sunxi: driver refresh for 3.13
[openwrt.git] / target / linux / sunxi / patches-3.13 / 160-5-ahci-platform-devs-with-more-than-1-clock.patch
1 From bdad8090b5efdb24fa9db00d8e2891927b68dcec Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Thu, 16 Jan 2014 14:32:35 +0100
4 Subject: [PATCH] ahci-platform: Add support for devices with more then 1 clock
5
6 The allwinner-sun4i AHCI controller needs 2 clocks to be enabled and the
7 imx AHCI controller needs 3 clocks to be enabled.
8
9 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
10 ---
11  .../devicetree/bindings/ata/ahci-platform.txt      |   1 +
12  drivers/ata/ahci.h                                 |   3 +-
13  drivers/ata/ahci_platform.c                        | 119 ++++++++++++++++-----
14  include/linux/ahci_platform.h                      |   4 +
15  4 files changed, 99 insertions(+), 28 deletions(-)
16
17 diff --git a/Documentation/devicetree/bindings/ata/ahci-platform.txt b/Documentation/devicetree/bindings/ata/ahci-platform.txt
18 index 89de156..3ced07d 100644
19 --- a/Documentation/devicetree/bindings/ata/ahci-platform.txt
20 +++ b/Documentation/devicetree/bindings/ata/ahci-platform.txt
21 @@ -10,6 +10,7 @@ Required properties:
22  
23  Optional properties:
24  - dma-coherent      : Present if dma operations are coherent
25 +- clocks            : a list of phandle + clock specifier pairs
26  
27  Example:
28          sata@ffe08000 {
29 diff --git a/drivers/ata/ahci.h b/drivers/ata/ahci.h
30 index 64d1a99d..c12862b 100644
31 --- a/drivers/ata/ahci.h
32 +++ b/drivers/ata/ahci.h
33 @@ -51,6 +51,7 @@
34  
35  enum {
36         AHCI_MAX_PORTS          = 32,
37 +       AHCI_MAX_CLKS           = 3,
38         AHCI_MAX_SG             = 168, /* hardware max is 64K */
39         AHCI_DMA_BOUNDARY       = 0xffffffff,
40         AHCI_MAX_CMDS           = 32,
41 @@ -321,7 +322,7 @@ struct ahci_host_priv {
42         u32                     em_loc; /* enclosure management location */
43         u32                     em_buf_sz;      /* EM buffer size in byte */
44         u32                     em_msg_type;    /* EM message type */
45 -       struct clk              *clk;           /* Only for platforms supporting clk */
46 +       struct clk              *clks[AHCI_MAX_CLKS]; /* Optional */
47         void                    *plat_data;     /* Other platform data */
48         /*
49          * Optional ahci_start_engine override, if not set this gets set to the
50 diff --git a/drivers/ata/ahci_platform.c b/drivers/ata/ahci_platform.c
51 index 4b231ba..609975d 100644
52 --- a/drivers/ata/ahci_platform.c
53 +++ b/drivers/ata/ahci_platform.c
54 @@ -87,6 +87,66 @@ struct ata_port_operations ahci_platform_ops = {
55         AHCI_SHT("ahci_platform"),
56  };
57  
58 +/**
59 + *     ahci_platform_enable_clks - Enable platform clocks
60 + *     @hpriv: host private area to store config values
61 + *
62 + *     This function enables all the clks found in hpriv->clks, starting
63 + *     at index 0. If any clk fails to enable it disables all the clks
64 + *     already enabled in reverse order, and then returns an error.
65 + *
66 + *     LOCKING:
67 + *     None.
68 + *
69 + *     RETURNS:
70 + *     0 on success otherwise a negative error code
71 + */
72 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
73 +{
74 +       int c, rc;
75 +
76 +       for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
77 +               rc = clk_prepare_enable(hpriv->clks[c]);
78 +               if (rc)
79 +                       goto disable_unprepare_clk;
80 +       }
81 +       return 0;
82 +
83 +disable_unprepare_clk:
84 +       while (--c >= 0)
85 +               clk_disable_unprepare(hpriv->clks[c]);
86 +       return rc;
87 +}
88 +EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
89 +
90 +/**
91 + *     ahci_platform_disable_clks - Disable platform clocks
92 + *     @hpriv: host private area to store config values
93 + *
94 + *     This function disables all the clks found in hpriv->clks, in reverse
95 + *     order of ahci_platform_enable_clks (starting at the end of the array).
96 + *
97 + *     LOCKING:
98 + *     None.
99 + */
100 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
101 +{
102 +       int c;
103 +
104 +       for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
105 +               if (hpriv->clks[c])
106 +                       clk_disable_unprepare(hpriv->clks[c]);
107 +}
108 +EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
109 +
110 +static void ahci_put_clks(struct ahci_host_priv *hpriv)
111 +{
112 +       int c;
113 +
114 +       for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
115 +               clk_put(hpriv->clks[c]);
116 +}
117 +
118  static int ahci_probe(struct platform_device *pdev)
119  {
120         struct device *dev = &pdev->dev;
121 @@ -97,6 +157,7 @@ static int ahci_probe(struct platform_device *pdev)
122         struct ahci_host_priv *hpriv;
123         struct ata_host *host;
124         struct resource *mem;
125 +       struct clk *clk;
126         int irq;
127         int n_ports;
128         int i;
129 @@ -131,17 +192,31 @@ static int ahci_probe(struct platform_device *pdev)
130                 return -ENOMEM;
131         }
132  
133 -       hpriv->clk = clk_get(dev, NULL);
134 -       if (IS_ERR(hpriv->clk)) {
135 -               dev_err(dev, "can't get clock\n");
136 -       } else {
137 -               rc = clk_prepare_enable(hpriv->clk);
138 -               if (rc) {
139 -                       dev_err(dev, "clock prepare enable failed");
140 -                       goto free_clk;
141 +       for (i = 0; i < AHCI_MAX_CLKS; i++) {
142 +               /*
143 +                * For now we must use clk_get(dev, NULL) for the first clock,
144 +                * because some platforms (da850, spear13xx) are not yet
145 +                * converted to use devicetree for clocks.  For new platforms
146 +                * this is equivalent to of_clk_get(dev->of_node, 0).
147 +                */
148 +               if (i == 0)
149 +                       clk = clk_get(dev, NULL);
150 +               else
151 +                       clk = of_clk_get(dev->of_node, i);
152 +
153 +               if (IS_ERR(clk)) {
154 +                       rc = PTR_ERR(clk);
155 +                       if (rc == -EPROBE_DEFER)
156 +                               goto free_clk;
157 +                       break;
158                 }
159 +               hpriv->clks[i] = clk;
160         }
161  
162 +       rc = ahci_enable_clks(dev, hpriv);
163 +       if (rc)
164 +               goto free_clk;
165 +
166         /*
167          * Some platforms might need to prepare for mmio region access,
168          * which could be done in the following init call. So, the mmio
169 @@ -222,11 +297,9 @@ static int ahci_probe(struct platform_device *pdev)
170         if (pdata && pdata->exit)
171                 pdata->exit(dev);
172  disable_unprepare_clk:
173 -       if (!IS_ERR(hpriv->clk))
174 -               clk_disable_unprepare(hpriv->clk);
175 +       ahci_disable_clks(hpriv);
176  free_clk:
177 -       if (!IS_ERR(hpriv->clk))
178 -               clk_put(hpriv->clk);
179 +       ahci_put_clks(hpriv);
180         return rc;
181  }
182  
183 @@ -239,10 +312,8 @@ static void ahci_host_stop(struct ata_host *host)
184         if (pdata && pdata->exit)
185                 pdata->exit(dev);
186  
187 -       if (!IS_ERR(hpriv->clk)) {
188 -               clk_disable_unprepare(hpriv->clk);
189 -               clk_put(hpriv->clk);
190 -       }
191 +       ahci_disable_clks(hpriv);
192 +       ahci_put_clks(hpriv);
193  }
194  
195  #ifdef CONFIG_PM_SLEEP
196 @@ -277,8 +348,7 @@ static int ahci_suspend(struct device *dev)
197         if (pdata && pdata->suspend)
198                 return pdata->suspend(dev);
199  
200 -       if (!IS_ERR(hpriv->clk))
201 -               clk_disable_unprepare(hpriv->clk);
202 +       ahci_disable_clks(hpriv);
203  
204         return 0;
205  }
206 @@ -290,13 +360,9 @@ static int ahci_resume(struct device *dev)
207         struct ahci_host_priv *hpriv = host->private_data;
208         int rc;
209  
210 -       if (!IS_ERR(hpriv->clk)) {
211 -               rc = clk_prepare_enable(hpriv->clk);
212 -               if (rc) {
213 -                       dev_err(dev, "clock prepare enable failed");
214 -                       return rc;
215 -               }
216 -       }
217 +       rc = ahci_enable_clks(dev, hpriv);
218 +       if (rc)
219 +               return rc;
220  
221         if (pdata && pdata->resume) {
222                 rc = pdata->resume(dev);
223 @@ -317,8 +383,7 @@ static int ahci_resume(struct device *dev)
224         return 0;
225  
226  disable_unprepare_clk:
227 -       if (!IS_ERR(hpriv->clk))
228 -               clk_disable_unprepare(hpriv->clk);
229 +       ahci_disable_clks(hpriv);
230  
231         return rc;
232  }
233 diff --git a/include/linux/ahci_platform.h b/include/linux/ahci_platform.h
234 index 73a2500..769d065 100644
235 --- a/include/linux/ahci_platform.h
236 +++ b/include/linux/ahci_platform.h
237 @@ -19,6 +19,7 @@
238  
239  struct device;
240  struct ata_port_info;
241 +struct ahci_host_priv;
242  
243  struct ahci_platform_data {
244         int (*init)(struct device *dev, void __iomem *addr);
245 @@ -30,4 +31,7 @@ struct ahci_platform_data {
246         unsigned int mask_port_map;
247  };
248  
249 +int ahci_platform_enable_clks(struct ahci_host_priv *hpriv);
250 +void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
251 +
252  #endif /* _AHCI_PLATFORM_H */
253 -- 
254 1.8.5.5
255