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