3b638d45acae2ced8fffe4b45206473cffb1f86f
[15.05/openwrt.git] / target / linux / ipq806x / patches / 0114-ahci-platform-Library-ise-ahci_probe-functionality.patch
1 From 8be4987340a101253fb871556894a002f1afb51f Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Sat, 22 Feb 2014 16:53:34 +0100
4 Subject: [PATCH 114/182] ahci-platform: "Library-ise" ahci_probe
5  functionality
6
7 ahci_probe consists of 3 steps:
8 1) Get resources (get mmio, clks, regulator)
9 2) Enable resources, handled by ahci_platform_enable_resouces
10 3) The more or less standard ahci-host controller init sequence
11
12 This commit refactors step 1 and 3 into separate functions, so the platform
13 drivers for AHCI implementations which need a specific order in step 2,
14 and / or need to do some custom register poking at some time, can re-use
15 ahci-platform.c code without needing to copy and paste it.
16
17 Note that ahci_platform_init_host's prototype takes the 3 non function
18 members of ahci_platform_data as arguments, the idea is that drivers using
19 the new exported utility functions will not use ahci_platform_data at all,
20 and hopefully in the future ahci_platform_data can go away entirely.
21
22 tj: Minor comment formatting updates.
23
24 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
25 Signed-off-by: Tejun Heo <tj@kernel.org>
26 ---
27  drivers/ata/ahci_platform.c   |  188 +++++++++++++++++++++++++++--------------
28  include/linux/ahci_platform.h |   14 +++
29  2 files changed, 137 insertions(+), 65 deletions(-)
30
31 --- a/drivers/ata/ahci_platform.c
32 +++ b/drivers/ata/ahci_platform.c
33 @@ -188,64 +188,60 @@ void ahci_platform_disable_resources(str
34  }
35  EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
36  
37 -static void ahci_put_clks(struct ahci_host_priv *hpriv)
38 +static void ahci_platform_put_resources(struct device *dev, void *res)
39  {
40 +       struct ahci_host_priv *hpriv = res;
41         int c;
42  
43         for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
44                 clk_put(hpriv->clks[c]);
45  }
46  
47 -static int ahci_probe(struct platform_device *pdev)
48 +/**
49 + * ahci_platform_get_resources - Get platform resources
50 + * @pdev: platform device to get resources for
51 + *
52 + * This function allocates an ahci_host_priv struct, and gets the following
53 + * resources, storing a reference to them inside the returned struct:
54 + *
55 + * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
56 + * 2) regulator for controlling the targets power (optional)
57 + * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
58 + *    or for non devicetree enabled platforms a single clock
59 + *
60 + * RETURNS:
61 + * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
62 + */
63 +struct ahci_host_priv *ahci_platform_get_resources(
64 +       struct platform_device *pdev)
65  {
66         struct device *dev = &pdev->dev;
67 -       struct ahci_platform_data *pdata = dev_get_platdata(dev);
68 -       const struct platform_device_id *id = platform_get_device_id(pdev);
69 -       struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
70 -       const struct ata_port_info *ppi[] = { &pi, NULL };
71         struct ahci_host_priv *hpriv;
72 -       struct ata_host *host;
73 -       struct resource *mem;
74         struct clk *clk;
75 -       int irq;
76 -       int n_ports;
77 -       int i;
78 -       int rc;
79 +       int i, rc = -ENOMEM;
80  
81 -       mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82 -       if (!mem) {
83 -               dev_err(dev, "no mmio space\n");
84 -               return -EINVAL;
85 -       }
86 +       if (!devres_open_group(dev, NULL, GFP_KERNEL))
87 +               return ERR_PTR(-ENOMEM);
88  
89 -       irq = platform_get_irq(pdev, 0);
90 -       if (irq <= 0) {
91 -               dev_err(dev, "no irq\n");
92 -               return -EINVAL;
93 -       }
94 -
95 -       if (pdata && pdata->ata_port_info)
96 -               pi = *pdata->ata_port_info;
97 +       hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
98 +                            GFP_KERNEL);
99 +       if (!hpriv)
100 +               goto err_out;
101  
102 -       hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
103 -       if (!hpriv) {
104 -               dev_err(dev, "can't alloc ahci_host_priv\n");
105 -               return -ENOMEM;
106 -       }
107 +       devres_add(dev, hpriv);
108  
109 -       hpriv->flags |= (unsigned long)pi.private_data;
110 -
111 -       hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
112 +       hpriv->mmio = devm_ioremap_resource(dev,
113 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
114         if (!hpriv->mmio) {
115 -               dev_err(dev, "can't map %pR\n", mem);
116 -               return -ENOMEM;
117 +               dev_err(dev, "no mmio space\n");
118 +               goto err_out;
119         }
120  
121         hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
122         if (IS_ERR(hpriv->target_pwr)) {
123                 rc = PTR_ERR(hpriv->target_pwr);
124                 if (rc == -EPROBE_DEFER)
125 -                       return -EPROBE_DEFER;
126 +                       goto err_out;
127                 hpriv->target_pwr = NULL;
128         }
129  
130 @@ -264,33 +260,59 @@ static int ahci_probe(struct platform_de
131                 if (IS_ERR(clk)) {
132                         rc = PTR_ERR(clk);
133                         if (rc == -EPROBE_DEFER)
134 -                               goto free_clk;
135 +                               goto err_out;
136                         break;
137                 }
138                 hpriv->clks[i] = clk;
139         }
140  
141 -       rc = ahci_platform_enable_resources(hpriv);
142 -       if (rc)
143 -               goto free_clk;
144 +       devres_remove_group(dev, NULL);
145 +       return hpriv;
146  
147 -       /*
148 -        * Some platforms might need to prepare for mmio region access,
149 -        * which could be done in the following init call. So, the mmio
150 -        * region shouldn't be accessed before init (if provided) has
151 -        * returned successfully.
152 -        */
153 -       if (pdata && pdata->init) {
154 -               rc = pdata->init(dev, hpriv->mmio);
155 -               if (rc)
156 -                       goto disable_resources;
157 -       }
158 +err_out:
159 +       devres_release_group(dev, NULL);
160 +       return ERR_PTR(rc);
161 +}
162 +EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
163 +
164 +/**
165 + * ahci_platform_init_host - Bring up an ahci-platform host
166 + * @pdev: platform device pointer for the host
167 + * @hpriv: ahci-host private data for the host
168 + * @pi_template: template for the ata_port_info to use
169 + * @force_port_map: param passed to ahci_save_initial_config
170 + * @mask_port_map: param passed to ahci_save_initial_config
171 + *
172 + * This function does all the usual steps needed to bring up an
173 + * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
174 + * must be initialized / enabled before calling this.
175 + *
176 + * RETURNS:
177 + * 0 on success otherwise a negative error code
178 + */
179 +int ahci_platform_init_host(struct platform_device *pdev,
180 +                           struct ahci_host_priv *hpriv,
181 +                           const struct ata_port_info *pi_template,
182 +                           unsigned int force_port_map,
183 +                           unsigned int mask_port_map)
184 +{
185 +       struct device *dev = &pdev->dev;
186 +       struct ata_port_info pi = *pi_template;
187 +       const struct ata_port_info *ppi[] = { &pi, NULL };
188 +       struct ata_host *host;
189 +       int i, irq, n_ports, rc;
190  
191 -       ahci_save_initial_config(dev, hpriv,
192 -               pdata ? pdata->force_port_map : 0,
193 -               pdata ? pdata->mask_port_map  : 0);
194 +       irq = platform_get_irq(pdev, 0);
195 +       if (irq <= 0) {
196 +               dev_err(dev, "no irq\n");
197 +               return -EINVAL;
198 +       }
199  
200         /* prepare host */
201 +       hpriv->flags |= (unsigned long)pi.private_data;
202 +
203 +       ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
204 +
205         if (hpriv->cap & HOST_CAP_NCQ)
206                 pi.flags |= ATA_FLAG_NCQ;
207  
208 @@ -307,10 +329,8 @@ static int ahci_probe(struct platform_de
209         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
210  
211         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
212 -       if (!host) {
213 -               rc = -ENOMEM;
214 -               goto pdata_exit;
215 -       }
216 +       if (!host)
217 +               return -ENOMEM;
218  
219         host->private_data = hpriv;
220  
221 @@ -325,7 +345,8 @@ static int ahci_probe(struct platform_de
222         for (i = 0; i < host->n_ports; i++) {
223                 struct ata_port *ap = host->ports[i];
224  
225 -               ata_port_desc(ap, "mmio %pR", mem);
226 +               ata_port_desc(ap, "mmio %pR",
227 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
228                 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
229  
230                 /* set enclosure management message type */
231 @@ -339,13 +360,53 @@ static int ahci_probe(struct platform_de
232  
233         rc = ahci_reset_controller(host);
234         if (rc)
235 -               goto pdata_exit;
236 +               return rc;
237  
238         ahci_init_controller(host);
239         ahci_print_info(host, "platform");
240  
241 -       rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
242 -                              &ahci_platform_sht);
243 +       return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
244 +                                &ahci_platform_sht);
245 +}
246 +EXPORT_SYMBOL_GPL(ahci_platform_init_host);
247 +
248 +static int ahci_probe(struct platform_device *pdev)
249 +{
250 +       struct device *dev = &pdev->dev;
251 +       struct ahci_platform_data *pdata = dev_get_platdata(dev);
252 +       const struct platform_device_id *id = platform_get_device_id(pdev);
253 +       const struct ata_port_info *pi_template;
254 +       struct ahci_host_priv *hpriv;
255 +       int rc;
256 +
257 +       hpriv = ahci_platform_get_resources(pdev);
258 +       if (IS_ERR(hpriv))
259 +               return PTR_ERR(hpriv);
260 +
261 +       rc = ahci_platform_enable_resources(hpriv);
262 +       if (rc)
263 +               return rc;
264 +
265 +       /*
266 +        * Some platforms might need to prepare for mmio region access,
267 +        * which could be done in the following init call. So, the mmio
268 +        * region shouldn't be accessed before init (if provided) has
269 +        * returned successfully.
270 +        */
271 +       if (pdata && pdata->init) {
272 +               rc = pdata->init(dev, hpriv->mmio);
273 +               if (rc)
274 +                       goto disable_resources;
275 +       }
276 +
277 +       if (pdata && pdata->ata_port_info)
278 +               pi_template = pdata->ata_port_info;
279 +       else
280 +               pi_template = &ahci_port_info[id ? id->driver_data : 0];
281 +
282 +       rc = ahci_platform_init_host(pdev, hpriv, pi_template,
283 +                                    pdata ? pdata->force_port_map : 0,
284 +                                    pdata ? pdata->mask_port_map  : 0);
285         if (rc)
286                 goto pdata_exit;
287  
288 @@ -355,8 +416,6 @@ pdata_exit:
289                 pdata->exit(dev);
290  disable_resources:
291         ahci_platform_disable_resources(hpriv);
292 -free_clk:
293 -       ahci_put_clks(hpriv);
294         return rc;
295  }
296  
297 @@ -370,7 +429,6 @@ static void ahci_host_stop(struct ata_ho
298                 pdata->exit(dev);
299  
300         ahci_platform_disable_resources(hpriv);
301 -       ahci_put_clks(hpriv);
302  }
303  
304  #ifdef CONFIG_PM_SLEEP
305 --- a/include/linux/ahci_platform.h
306 +++ b/include/linux/ahci_platform.h
307 @@ -20,7 +20,14 @@
308  struct device;
309  struct ata_port_info;
310  struct ahci_host_priv;
311 +struct platform_device;
312  
313 +/*
314 + * Note ahci_platform_data is deprecated, it is only kept around for use
315 + * by the old da850 and spear13xx ahci code.
316 + * New drivers should instead declare their own platform_driver struct, and
317 + * use ahci_platform* functions in their own probe, suspend and resume methods.
318 + */
319  struct ahci_platform_data {
320         int (*init)(struct device *dev, void __iomem *addr);
321         void (*exit)(struct device *dev);
322 @@ -35,5 +42,12 @@ int ahci_platform_enable_clks(struct ahc
323  void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
324  int ahci_platform_enable_resources(struct ahci_host_priv *hpriv);
325  void ahci_platform_disable_resources(struct ahci_host_priv *hpriv);
326 +struct ahci_host_priv *ahci_platform_get_resources(
327 +       struct platform_device *pdev);
328 +int ahci_platform_init_host(struct platform_device *pdev,
329 +                           struct ahci_host_priv *hpriv,
330 +                           const struct ata_port_info *pi_template,
331 +                           unsigned int force_port_map,
332 +                           unsigned int mask_port_map);
333  
334  #endif /* _AHCI_PLATFORM_H */