sunxi: driver refresh for 3.13
[openwrt.git] / target / linux / sunxi / patches-3.13 / 160-8-ahci-plat-libraryise-ahci_probe.patch
1 From 98601575da5276173233dd575bdd61cccdeb861a Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Mon, 20 Jan 2014 14:58:04 +0100
4 Subject: [PATCH] ahci-platform: "Library-ise" ahci_probe functionality
5
6 ahci_probe consists of 3 steps:
7 1) Get resources (get mmio, clks, regulator)
8 2) Enable resources, handled by ahci_platform_enable_resouces
9 3) The more or less standard ahci-host controller init sequence
10
11 This commit refactors step 1 and 3 into separate functions, so the platform
12 drivers for AHCI implementations which need a specific order in step 2,
13 and / or need to do some custom register poking at some time, can re-use
14 ahci-platform.c code without needing to copy and paste it.
15
16 Note that ahci_platform_init_host's prototype takes the 3 non function
17 members of ahci_platform_data as arguments, the idea is that drivers using
18 the new exported utility functions will not use ahci_platform_data at all,
19 and hopefully in the future ahci_platform_data can go away entirely.
20
21 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
22 ---
23  drivers/ata/ahci_platform.c   | 195 ++++++++++++++++++++++++++++--------------
24  include/linux/ahci_platform.h |  14 +++
25  2 files changed, 144 insertions(+), 65 deletions(-)
26
27 diff --git a/drivers/ata/ahci_platform.c b/drivers/ata/ahci_platform.c
28 index 6ebbc17..7f3f2ac 100644
29 --- a/drivers/ata/ahci_platform.c
30 +++ b/drivers/ata/ahci_platform.c
31 @@ -201,64 +201,64 @@ void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
32  }
33  EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
34  
35 -static void ahci_put_clks(struct ahci_host_priv *hpriv)
36 +static void ahci_platform_put_resources(struct device *dev, void *res)
37  {
38 +       struct ahci_host_priv *hpriv = res;
39         int c;
40  
41         for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
42                 clk_put(hpriv->clks[c]);
43  }
44  
45 -static int ahci_probe(struct platform_device *pdev)
46 +/**
47 + *     ahci_platform_get_resources - Get platform resources
48 + *     @pdev: platform device to get resources for
49 + *
50 + *     This function allocates an ahci_host_priv struct, and gets the
51 + *     following resources, storing a reference to them inside the returned
52 + *     struct:
53 + *
54 + *     1) mmio registers (IORESOURCE_MEM 0, mandatory)
55 + *     2) regulator for controlling the targets power (optional)
56 + *     3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
57 + *        or for non devicetree enabled platforms a single clock
58 + *
59 + *     LOCKING:
60 + *     None.
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 +       if (!devres_open_group(dev, NULL, GFP_KERNEL))
89 +               return ERR_PTR(-ENOMEM);
90  
91 -       irq = platform_get_irq(pdev, 0);
92 -       if (irq <= 0) {
93 -               dev_err(dev, "no irq\n");
94 -               return -EINVAL;
95 -       }
96 +       hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
97 +                            GFP_KERNEL);
98 +       if (!hpriv)
99 +               goto err_out;
100  
101 -       if (pdata && pdata->ata_port_info)
102 -               pi = *pdata->ata_port_info;
103 +       devres_add(dev, hpriv);
104  
105 -       hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
106 -       if (!hpriv) {
107 -               dev_err(dev, "can't alloc ahci_host_priv\n");
108 -               return -ENOMEM;
109 -       }
110 -
111 -       hpriv->flags |= (unsigned long)pi.private_data;
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 @@ -277,33 +277,62 @@ 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 + *     LOCKING:
179 + *     None.
180 + *
181 + *     RETURNS:
182 + *     0 on success otherwise a negative error code
183 + */
184 +int ahci_platform_init_host(struct platform_device *pdev,
185 +                           struct ahci_host_priv *hpriv,
186 +                           const struct ata_port_info *pi_template,
187 +                           unsigned int force_port_map,
188 +                           unsigned int mask_port_map)
189 +{
190 +       struct device *dev = &pdev->dev;
191 +       struct ata_port_info pi = *pi_template;
192 +       const struct ata_port_info *ppi[] = { &pi, NULL };
193 +       struct ata_host *host;
194 +       int i, irq, n_ports, rc;
195  
196 -       ahci_save_initial_config(dev, hpriv,
197 -               pdata ? pdata->force_port_map : 0,
198 -               pdata ? pdata->mask_port_map  : 0);
199 +       irq = platform_get_irq(pdev, 0);
200 +       if (irq <= 0) {
201 +               dev_err(dev, "no irq\n");
202 +               return -EINVAL;
203 +       }
204  
205         /* prepare host */
206 +       hpriv->flags |= (unsigned long)pi.private_data;
207 +
208 +       ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
209 +
210         if (hpriv->cap & HOST_CAP_NCQ)
211                 pi.flags |= ATA_FLAG_NCQ;
212  
213 @@ -320,10 +349,8 @@ static int ahci_probe(struct platform_device *pdev)
214         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
215  
216         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
217 -       if (!host) {
218 -               rc = -ENOMEM;
219 -               goto pdata_exit;
220 -       }
221 +       if (!host)
222 +               return -ENOMEM;
223  
224         host->private_data = hpriv;
225  
226 @@ -338,7 +365,8 @@ static int ahci_probe(struct platform_device *pdev)
227         for (i = 0; i < host->n_ports; i++) {
228                 struct ata_port *ap = host->ports[i];
229  
230 -               ata_port_desc(ap, "mmio %pR", mem);
231 +               ata_port_desc(ap, "mmio %pR",
232 +                             platform_get_resource(pdev, IORESOURCE_MEM, 0));
233                 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
234  
235                 /* set enclosure management message type */
236 @@ -352,13 +380,53 @@ static int ahci_probe(struct platform_device *pdev)
237  
238         rc = ahci_reset_controller(host);
239         if (rc)
240 -               goto pdata_exit;
241 +               return rc;
242  
243         ahci_init_controller(host);
244         ahci_print_info(host, "platform");
245  
246 -       rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
247 -                              &ahci_platform_sht);
248 +       return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
249 +                                &ahci_platform_sht);
250 +}
251 +EXPORT_SYMBOL_GPL(ahci_platform_init_host);
252 +
253 +static int ahci_probe(struct platform_device *pdev)
254 +{
255 +       struct device *dev = &pdev->dev;
256 +       struct ahci_platform_data *pdata = dev_get_platdata(dev);
257 +       const struct platform_device_id *id = platform_get_device_id(pdev);
258 +       const struct ata_port_info *pi_template;
259 +       struct ahci_host_priv *hpriv;
260 +       int rc;
261 +
262 +       hpriv = ahci_platform_get_resources(pdev);
263 +       if (IS_ERR(hpriv))
264 +               return PTR_ERR(hpriv);
265 +
266 +       rc = ahci_platform_enable_resources(hpriv);
267 +       if (rc)
268 +               return rc;
269 +
270 +       /*
271 +        * Some platforms might need to prepare for mmio region access,
272 +        * which could be done in the following init call. So, the mmio
273 +        * region shouldn't be accessed before init (if provided) has
274 +        * returned successfully.
275 +        */
276 +       if (pdata && pdata->init) {
277 +               rc = pdata->init(dev, hpriv->mmio);
278 +               if (rc)
279 +                       goto disable_resources;
280 +       }
281 +
282 +       if (pdata && pdata->ata_port_info)
283 +               pi_template = pdata->ata_port_info;
284 +       else
285 +               pi_template = &ahci_port_info[id ? id->driver_data : 0];
286 +
287 +       rc = ahci_platform_init_host(pdev, hpriv, pi_template,
288 +                                    pdata ? pdata->force_port_map : 0,
289 +                                    pdata ? pdata->mask_port_map  : 0);
290         if (rc)
291                 goto pdata_exit;
292  
293 @@ -368,8 +436,6 @@ static int ahci_probe(struct platform_device *pdev)
294                 pdata->exit(dev);
295  disable_resources:
296         ahci_platform_disable_resources(hpriv);
297 -free_clk:
298 -       ahci_put_clks(hpriv);
299         return rc;
300  }
301  
302 @@ -383,7 +449,6 @@ static void ahci_host_stop(struct ata_host *host)
303                 pdata->exit(dev);
304  
305         ahci_platform_disable_resources(hpriv);
306 -       ahci_put_clks(hpriv);
307  }
308  
309  #ifdef CONFIG_PM_SLEEP
310 diff --git a/include/linux/ahci_platform.h b/include/linux/ahci_platform.h
311 index b674b01..b80c51c 100644
312 --- a/include/linux/ahci_platform.h
313 +++ b/include/linux/ahci_platform.h
314 @@ -20,7 +20,14 @@
315  struct device;
316  struct ata_port_info;
317  struct ahci_host_priv;
318 +struct platform_device;
319  
320 +/*
321 + * Note ahci_platform_data is deprecated, it is only kept around for use
322 + * by the old da850 and spear13xx ahci code.
323 + * New drivers should instead declare their own platform_driver struct, and
324 + * use ahci_platform* functions in their own probe, suspend and resume methods.
325 + */
326  struct ahci_platform_data {
327         int (*init)(struct device *dev, void __iomem *addr);
328         void (*exit)(struct device *dev);
329 @@ -35,5 +42,12 @@ struct ahci_platform_data {
330  void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
331  int ahci_platform_enable_resources(struct ahci_host_priv *hpriv);
332  void ahci_platform_disable_resources(struct ahci_host_priv *hpriv);
333 +struct ahci_host_priv *ahci_platform_get_resources(
334 +       struct platform_device *pdev);
335 +int ahci_platform_init_host(struct platform_device *pdev,
336 +                           struct ahci_host_priv *hpriv,
337 +                           const struct ata_port_info *pi_template,
338 +                           unsigned int force_port_map,
339 +                           unsigned int mask_port_map);
340  
341  #endif /* _AHCI_PLATFORM_H */
342 -- 
343 1.8.5.5
344