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
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
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.
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.
22 tj: Minor comment formatting updates.
24 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
25 Signed-off-by: Tejun Heo <tj@kernel.org>
27 drivers/ata/ahci_platform.c | 188 +++++++++++++++++++++++++++--------------
28 include/linux/ahci_platform.h | 14 +++
29 2 files changed, 137 insertions(+), 65 deletions(-)
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)
37 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
39 -static void ahci_put_clks(struct ahci_host_priv *hpriv)
40 +static void ahci_platform_put_resources(struct device *dev, void *res)
42 + struct ahci_host_priv *hpriv = res;
45 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
46 clk_put(hpriv->clks[c]);
49 -static int ahci_probe(struct platform_device *pdev)
51 + * ahci_platform_get_resources - Get platform resources
52 + * @pdev: platform device to get resources for
54 + * This function allocates an ahci_host_priv struct, and gets the following
55 + * resources, storing a reference to them inside the returned struct:
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
63 + * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
65 +struct ahci_host_priv *ahci_platform_get_resources(
66 + struct platform_device *pdev)
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;
81 + int i, rc = -ENOMEM;
83 - mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 - dev_err(dev, "no mmio space\n");
89 - irq = platform_get_irq(pdev, 0);
91 - dev_err(dev, "no irq\n");
94 + if (!devres_open_group(dev, NULL, GFP_KERNEL))
95 + return ERR_PTR(-ENOMEM);
97 - if (pdata && pdata->ata_port_info)
98 - pi = *pdata->ata_port_info;
100 - hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
102 - dev_err(dev, "can't alloc ahci_host_priv\n");
105 + hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
110 - hpriv->flags |= (unsigned long)pi.private_data;
111 + devres_add(dev, hpriv);
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));
117 - dev_err(dev, "can't map %pR\n", mem);
119 + dev_err(dev, "no mmio space\n");
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;
129 hpriv->target_pwr = NULL;
132 @@ -264,33 +260,59 @@ static int ahci_probe(struct platform_device *pdev)
135 if (rc == -EPROBE_DEFER)
140 hpriv->clks[i] = clk;
143 - rc = ahci_platform_enable_resources(hpriv);
146 + devres_remove_group(dev, NULL);
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.
155 - if (pdata && pdata->init) {
156 - rc = pdata->init(dev, hpriv->mmio);
158 - goto disable_resources;
161 + devres_release_group(dev, NULL);
162 + return ERR_PTR(rc);
164 +EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
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
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.
179 + * 0 on success otherwise a negative error code
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)
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;
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);
198 + dev_err(dev, "no irq\n");
203 + hpriv->flags |= (unsigned long)pi.private_data;
205 + ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
207 if (hpriv->cap & HOST_CAP_NCQ)
208 pi.flags |= ATA_FLAG_NCQ;
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));
213 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
221 host->private_data = hpriv;
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];
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);
232 /* set enclosure management message type */
233 @@ -339,13 +360,53 @@ static int ahci_probe(struct platform_device *pdev)
235 rc = ahci_reset_controller(host);
240 ahci_init_controller(host);
241 ahci_print_info(host, "platform");
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);
248 +EXPORT_SYMBOL_GPL(ahci_platform_init_host);
250 +static int ahci_probe(struct platform_device *pdev)
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;
259 + hpriv = ahci_platform_get_resources(pdev);
261 + return PTR_ERR(hpriv);
263 + rc = ahci_platform_enable_resources(hpriv);
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.
273 + if (pdata && pdata->init) {
274 + rc = pdata->init(dev, hpriv->mmio);
276 + goto disable_resources;
279 + if (pdata && pdata->ata_port_info)
280 + pi_template = pdata->ata_port_info;
282 + pi_template = &ahci_port_info[id ? id->driver_data : 0];
284 + rc = ahci_platform_init_host(pdev, hpriv, pi_template,
285 + pdata ? pdata->force_port_map : 0,
286 + pdata ? pdata->mask_port_map : 0);
290 @@ -355,8 +416,6 @@ pdata_exit:
293 ahci_platform_disable_resources(hpriv);
295 - ahci_put_clks(hpriv);
299 @@ -370,7 +429,6 @@ static void ahci_host_stop(struct ata_host *host)
302 ahci_platform_disable_resources(hpriv);
303 - ahci_put_clks(hpriv);
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
313 struct ata_port_info;
314 struct ahci_host_priv;
315 +struct platform_device;
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.
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);
338 #endif /* _AHCI_PLATFORM_H */