xref: /linux/drivers/ata/libahci_platform.c (revision 988b0c541ed8b1c633c4d4df7169010635942e18)
1 /*
2  * AHCI SATA platform library
3  *
4  * Copyright 2004-2005  Red Hat, Inc.
5  *   Jeff Garzik <jgarzik@pobox.com>
6  * Copyright 2010  MontaVista Software, LLC.
7  *   Anton Vorontsov <avorontsov@ru.mvista.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/module.h>
19 #include <linux/pm.h>
20 #include <linux/interrupt.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/libata.h>
24 #include <linux/ahci_platform.h>
25 #include <linux/phy/phy.h>
26 #include <linux/pm_runtime.h>
27 #include "ahci.h"
28 
29 static void ahci_host_stop(struct ata_host *host);
30 
31 struct ata_port_operations ahci_platform_ops = {
32 	.inherits	= &ahci_ops,
33 	.host_stop	= ahci_host_stop,
34 };
35 EXPORT_SYMBOL_GPL(ahci_platform_ops);
36 
37 static struct scsi_host_template ahci_platform_sht = {
38 	AHCI_SHT("ahci_platform"),
39 };
40 
41 /**
42  * ahci_platform_enable_clks - Enable platform clocks
43  * @hpriv: host private area to store config values
44  *
45  * This function enables all the clks found in hpriv->clks, starting at
46  * index 0. If any clk fails to enable it disables all the clks already
47  * enabled in reverse order, and then returns an error.
48  *
49  * RETURNS:
50  * 0 on success otherwise a negative error code
51  */
52 int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
53 {
54 	int c, rc;
55 
56 	for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
57 		rc = clk_prepare_enable(hpriv->clks[c]);
58 		if (rc)
59 			goto disable_unprepare_clk;
60 	}
61 	return 0;
62 
63 disable_unprepare_clk:
64 	while (--c >= 0)
65 		clk_disable_unprepare(hpriv->clks[c]);
66 	return rc;
67 }
68 EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
69 
70 /**
71  * ahci_platform_disable_clks - Disable platform clocks
72  * @hpriv: host private area to store config values
73  *
74  * This function disables all the clks found in hpriv->clks, in reverse
75  * order of ahci_platform_enable_clks (starting at the end of the array).
76  */
77 void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
78 {
79 	int c;
80 
81 	for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
82 		if (hpriv->clks[c])
83 			clk_disable_unprepare(hpriv->clks[c]);
84 }
85 EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
86 
87 /**
88  * ahci_platform_enable_resources - Enable platform resources
89  * @hpriv: host private area to store config values
90  *
91  * This function enables all ahci_platform managed resources in the
92  * following order:
93  * 1) Regulator
94  * 2) Clocks (through ahci_platform_enable_clks)
95  * 3) Phy
96  *
97  * If resource enabling fails at any point the previous enabled resources
98  * are disabled in reverse order.
99  *
100  * RETURNS:
101  * 0 on success otherwise a negative error code
102  */
103 int ahci_platform_enable_resources(struct ahci_host_priv *hpriv)
104 {
105 	int rc;
106 
107 	if (hpriv->target_pwr) {
108 		rc = regulator_enable(hpriv->target_pwr);
109 		if (rc)
110 			return rc;
111 	}
112 
113 	rc = ahci_platform_enable_clks(hpriv);
114 	if (rc)
115 		goto disable_regulator;
116 
117 	if (hpriv->phy) {
118 		rc = phy_init(hpriv->phy);
119 		if (rc)
120 			goto disable_clks;
121 
122 		rc = phy_power_on(hpriv->phy);
123 		if (rc) {
124 			phy_exit(hpriv->phy);
125 			goto disable_clks;
126 		}
127 	}
128 
129 	return 0;
130 
131 disable_clks:
132 	ahci_platform_disable_clks(hpriv);
133 
134 disable_regulator:
135 	if (hpriv->target_pwr)
136 		regulator_disable(hpriv->target_pwr);
137 	return rc;
138 }
139 EXPORT_SYMBOL_GPL(ahci_platform_enable_resources);
140 
141 /**
142  * ahci_platform_disable_resources - Disable platform resources
143  * @hpriv: host private area to store config values
144  *
145  * This function disables all ahci_platform managed resources in the
146  * following order:
147  * 1) Phy
148  * 2) Clocks (through ahci_platform_disable_clks)
149  * 3) Regulator
150  */
151 void ahci_platform_disable_resources(struct ahci_host_priv *hpriv)
152 {
153 	if (hpriv->phy) {
154 		phy_power_off(hpriv->phy);
155 		phy_exit(hpriv->phy);
156 	}
157 
158 	ahci_platform_disable_clks(hpriv);
159 
160 	if (hpriv->target_pwr)
161 		regulator_disable(hpriv->target_pwr);
162 }
163 EXPORT_SYMBOL_GPL(ahci_platform_disable_resources);
164 
165 static void ahci_platform_put_resources(struct device *dev, void *res)
166 {
167 	struct ahci_host_priv *hpriv = res;
168 	int c;
169 
170 	if (hpriv->got_runtime_pm) {
171 		pm_runtime_put_sync(dev);
172 		pm_runtime_disable(dev);
173 	}
174 
175 	for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
176 		clk_put(hpriv->clks[c]);
177 }
178 
179 /**
180  * ahci_platform_get_resources - Get platform resources
181  * @pdev: platform device to get resources for
182  *
183  * This function allocates an ahci_host_priv struct, and gets the following
184  * resources, storing a reference to them inside the returned struct:
185  *
186  * 1) mmio registers (IORESOURCE_MEM 0, mandatory)
187  * 2) regulator for controlling the targets power (optional)
188  * 3) 0 - AHCI_MAX_CLKS clocks, as specified in the devs devicetree node,
189  *    or for non devicetree enabled platforms a single clock
190  *	4) phy (optional)
191  *
192  * RETURNS:
193  * The allocated ahci_host_priv on success, otherwise an ERR_PTR value
194  */
195 struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev)
196 {
197 	struct device *dev = &pdev->dev;
198 	struct ahci_host_priv *hpriv;
199 	struct clk *clk;
200 	int i, rc = -ENOMEM;
201 
202 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
203 		return ERR_PTR(-ENOMEM);
204 
205 	hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
206 			     GFP_KERNEL);
207 	if (!hpriv)
208 		goto err_out;
209 
210 	devres_add(dev, hpriv);
211 
212 	hpriv->mmio = devm_ioremap_resource(dev,
213 			      platform_get_resource(pdev, IORESOURCE_MEM, 0));
214 	if (IS_ERR(hpriv->mmio)) {
215 		dev_err(dev, "no mmio space\n");
216 		rc = PTR_ERR(hpriv->mmio);
217 		goto err_out;
218 	}
219 
220 	hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
221 	if (IS_ERR(hpriv->target_pwr)) {
222 		rc = PTR_ERR(hpriv->target_pwr);
223 		if (rc == -EPROBE_DEFER)
224 			goto err_out;
225 		hpriv->target_pwr = NULL;
226 	}
227 
228 	for (i = 0; i < AHCI_MAX_CLKS; i++) {
229 		/*
230 		 * For now we must use clk_get(dev, NULL) for the first clock,
231 		 * because some platforms (da850, spear13xx) are not yet
232 		 * converted to use devicetree for clocks.  For new platforms
233 		 * this is equivalent to of_clk_get(dev->of_node, 0).
234 		 */
235 		if (i == 0)
236 			clk = clk_get(dev, NULL);
237 		else
238 			clk = of_clk_get(dev->of_node, i);
239 
240 		if (IS_ERR(clk)) {
241 			rc = PTR_ERR(clk);
242 			if (rc == -EPROBE_DEFER)
243 				goto err_out;
244 			break;
245 		}
246 		hpriv->clks[i] = clk;
247 	}
248 
249 	hpriv->phy = devm_phy_get(dev, "sata-phy");
250 	if (IS_ERR(hpriv->phy)) {
251 		rc = PTR_ERR(hpriv->phy);
252 		switch (rc) {
253 		case -ENOSYS:
254 			/* No PHY support. Check if PHY is required. */
255 			if (of_find_property(dev->of_node, "phys", NULL)) {
256 				dev_err(dev, "couldn't get sata-phy: ENOSYS\n");
257 				goto err_out;
258 			}
259 		case -ENODEV:
260 			/* continue normally */
261 			hpriv->phy = NULL;
262 			break;
263 
264 		case -EPROBE_DEFER:
265 			goto err_out;
266 
267 		default:
268 			dev_err(dev, "couldn't get sata-phy\n");
269 			goto err_out;
270 		}
271 	}
272 
273 	pm_runtime_enable(dev);
274 	pm_runtime_get_sync(dev);
275 	hpriv->got_runtime_pm = true;
276 
277 	devres_remove_group(dev, NULL);
278 	return hpriv;
279 
280 err_out:
281 	devres_release_group(dev, NULL);
282 	return ERR_PTR(rc);
283 }
284 EXPORT_SYMBOL_GPL(ahci_platform_get_resources);
285 
286 /**
287  * ahci_platform_init_host - Bring up an ahci-platform host
288  * @pdev: platform device pointer for the host
289  * @hpriv: ahci-host private data for the host
290  * @pi_template: template for the ata_port_info to use
291  * @host_flags: ahci host flags used in ahci_host_priv
292  * @force_port_map: param passed to ahci_save_initial_config
293  * @mask_port_map: param passed to ahci_save_initial_config
294  *
295  * This function does all the usual steps needed to bring up an
296  * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
297  * must be initialized / enabled before calling this.
298  *
299  * RETURNS:
300  * 0 on success otherwise a negative error code
301  */
302 int ahci_platform_init_host(struct platform_device *pdev,
303 			    struct ahci_host_priv *hpriv,
304 			    const struct ata_port_info *pi_template,
305 			    unsigned long host_flags,
306 			    unsigned int force_port_map,
307 			    unsigned int mask_port_map)
308 {
309 	struct device *dev = &pdev->dev;
310 	struct ata_port_info pi = *pi_template;
311 	const struct ata_port_info *ppi[] = { &pi, NULL };
312 	struct ata_host *host;
313 	int i, irq, n_ports, rc;
314 
315 	irq = platform_get_irq(pdev, 0);
316 	if (irq <= 0) {
317 		dev_err(dev, "no irq\n");
318 		return -EINVAL;
319 	}
320 
321 	/* prepare host */
322 	pi.private_data = (void *)host_flags;
323 	hpriv->flags |= host_flags;
324 
325 	ahci_save_initial_config(dev, hpriv, force_port_map, mask_port_map);
326 
327 	if (hpriv->cap & HOST_CAP_NCQ)
328 		pi.flags |= ATA_FLAG_NCQ;
329 
330 	if (hpriv->cap & HOST_CAP_PMP)
331 		pi.flags |= ATA_FLAG_PMP;
332 
333 	ahci_set_em_messages(hpriv, &pi);
334 
335 	/* CAP.NP sometimes indicate the index of the last enabled
336 	 * port, at other times, that of the last possible port, so
337 	 * determining the maximum port number requires looking at
338 	 * both CAP.NP and port_map.
339 	 */
340 	n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
341 
342 	host = ata_host_alloc_pinfo(dev, ppi, n_ports);
343 	if (!host)
344 		return -ENOMEM;
345 
346 	host->private_data = hpriv;
347 
348 	if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
349 		host->flags |= ATA_HOST_PARALLEL_SCAN;
350 	else
351 		dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
352 
353 	if (pi.flags & ATA_FLAG_EM)
354 		ahci_reset_em(host);
355 
356 	for (i = 0; i < host->n_ports; i++) {
357 		struct ata_port *ap = host->ports[i];
358 
359 		ata_port_desc(ap, "mmio %pR",
360 			      platform_get_resource(pdev, IORESOURCE_MEM, 0));
361 		ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
362 
363 		/* set enclosure management message type */
364 		if (ap->flags & ATA_FLAG_EM)
365 			ap->em_message_type = hpriv->em_msg_type;
366 
367 		/* disabled/not-implemented port */
368 		if (!(hpriv->port_map & (1 << i)))
369 			ap->ops = &ata_dummy_port_ops;
370 	}
371 
372 	rc = ahci_reset_controller(host);
373 	if (rc)
374 		return rc;
375 
376 	ahci_init_controller(host);
377 	ahci_print_info(host, "platform");
378 
379 	return ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
380 				 &ahci_platform_sht);
381 }
382 EXPORT_SYMBOL_GPL(ahci_platform_init_host);
383 
384 static void ahci_host_stop(struct ata_host *host)
385 {
386 	struct device *dev = host->dev;
387 	struct ahci_platform_data *pdata = dev_get_platdata(dev);
388 	struct ahci_host_priv *hpriv = host->private_data;
389 
390 	if (pdata && pdata->exit)
391 		pdata->exit(dev);
392 
393 	ahci_platform_disable_resources(hpriv);
394 }
395 
396 #ifdef CONFIG_PM_SLEEP
397 /**
398  * ahci_platform_suspend_host - Suspend an ahci-platform host
399  * @dev: device pointer for the host
400  *
401  * This function does all the usual steps needed to suspend an
402  * ahci-platform host, note any necessary resources (ie clks, phy, etc.)
403  * must be disabled after calling this.
404  *
405  * RETURNS:
406  * 0 on success otherwise a negative error code
407  */
408 int ahci_platform_suspend_host(struct device *dev)
409 {
410 	struct ata_host *host = dev_get_drvdata(dev);
411 	struct ahci_host_priv *hpriv = host->private_data;
412 	void __iomem *mmio = hpriv->mmio;
413 	u32 ctl;
414 
415 	if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
416 		dev_err(dev, "firmware update required for suspend/resume\n");
417 		return -EIO;
418 	}
419 
420 	/*
421 	 * AHCI spec rev1.1 section 8.3.3:
422 	 * Software must disable interrupts prior to requesting a
423 	 * transition of the HBA to D3 state.
424 	 */
425 	ctl = readl(mmio + HOST_CTL);
426 	ctl &= ~HOST_IRQ_EN;
427 	writel(ctl, mmio + HOST_CTL);
428 	readl(mmio + HOST_CTL); /* flush */
429 
430 	return ata_host_suspend(host, PMSG_SUSPEND);
431 }
432 EXPORT_SYMBOL_GPL(ahci_platform_suspend_host);
433 
434 /**
435  * ahci_platform_resume_host - Resume an ahci-platform host
436  * @dev: device pointer for the host
437  *
438  * This function does all the usual steps needed to resume an ahci-platform
439  * host, note any necessary resources (ie clks, phy, etc.)  must be
440  * initialized / enabled before calling this.
441  *
442  * RETURNS:
443  * 0 on success otherwise a negative error code
444  */
445 int ahci_platform_resume_host(struct device *dev)
446 {
447 	struct ata_host *host = dev_get_drvdata(dev);
448 	int rc;
449 
450 	if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
451 		rc = ahci_reset_controller(host);
452 		if (rc)
453 			return rc;
454 
455 		ahci_init_controller(host);
456 	}
457 
458 	ata_host_resume(host);
459 
460 	return 0;
461 }
462 EXPORT_SYMBOL_GPL(ahci_platform_resume_host);
463 
464 /**
465  * ahci_platform_suspend - Suspend an ahci-platform device
466  * @dev: the platform device to suspend
467  *
468  * This function suspends the host associated with the device, followed by
469  * disabling all the resources of the device.
470  *
471  * RETURNS:
472  * 0 on success otherwise a negative error code
473  */
474 int ahci_platform_suspend(struct device *dev)
475 {
476 	struct ahci_platform_data *pdata = dev_get_platdata(dev);
477 	struct ata_host *host = dev_get_drvdata(dev);
478 	struct ahci_host_priv *hpriv = host->private_data;
479 	int rc;
480 
481 	rc = ahci_platform_suspend_host(dev);
482 	if (rc)
483 		return rc;
484 
485 	if (pdata && pdata->suspend) {
486 		rc = pdata->suspend(dev);
487 		if (rc)
488 			goto resume_host;
489 	}
490 
491 	ahci_platform_disable_resources(hpriv);
492 
493 	return 0;
494 
495 resume_host:
496 	ahci_platform_resume_host(dev);
497 	return rc;
498 }
499 EXPORT_SYMBOL_GPL(ahci_platform_suspend);
500 
501 /**
502  * ahci_platform_resume - Resume an ahci-platform device
503  * @dev: the platform device to resume
504  *
505  * This function enables all the resources of the device followed by
506  * resuming the host associated with the device.
507  *
508  * RETURNS:
509  * 0 on success otherwise a negative error code
510  */
511 int ahci_platform_resume(struct device *dev)
512 {
513 	struct ahci_platform_data *pdata = dev_get_platdata(dev);
514 	struct ata_host *host = dev_get_drvdata(dev);
515 	struct ahci_host_priv *hpriv = host->private_data;
516 	int rc;
517 
518 	rc = ahci_platform_enable_resources(hpriv);
519 	if (rc)
520 		return rc;
521 
522 	if (pdata && pdata->resume) {
523 		rc = pdata->resume(dev);
524 		if (rc)
525 			goto disable_resources;
526 	}
527 
528 	rc = ahci_platform_resume_host(dev);
529 	if (rc)
530 		goto disable_resources;
531 
532 	/* We resumed so update PM runtime state */
533 	pm_runtime_disable(dev);
534 	pm_runtime_set_active(dev);
535 	pm_runtime_enable(dev);
536 
537 	return 0;
538 
539 disable_resources:
540 	ahci_platform_disable_resources(hpriv);
541 
542 	return rc;
543 }
544 EXPORT_SYMBOL_GPL(ahci_platform_resume);
545 #endif
546 
547 MODULE_DESCRIPTION("AHCI SATA platform library");
548 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
549 MODULE_LICENSE("GPL");
550