blob: 4befd78133ad809ab5b617a0529fb08ced5fed54 [file] [log] [blame]
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +03001/*
2 * AHCI SATA platform driver
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
Viresh Kumarf1e70c22012-08-27 10:37:19 +053015#include <linux/clk.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030016#include <linux/kernel.h>
Tejun Heofbaf6662010-03-30 02:52:43 +090017#include <linux/gfp.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030018#include <linux/module.h>
Shiraz Hashim18c25ff2012-03-16 15:49:55 +053019#include <linux/pm.h>
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030020#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 "ahci.h"
26
Brian Norris1896b152012-11-02 00:46:17 -070027static void ahci_host_stop(struct ata_host *host);
28
Richard Zhu904c04f2011-09-28 15:41:54 +080029enum ahci_type {
30 AHCI, /* standard platform ahci */
31 IMX53_AHCI, /* ahci on i.mx53 */
Brian Norrisd408e2b2012-02-21 10:38:44 -080032 STRICT_AHCI, /* delayed DMA engine start */
Richard Zhu904c04f2011-09-28 15:41:54 +080033};
34
35static struct platform_device_id ahci_devtype[] = {
36 {
37 .name = "ahci",
38 .driver_data = AHCI,
39 }, {
40 .name = "imx53-ahci",
41 .driver_data = IMX53_AHCI,
42 }, {
Brian Norrisd408e2b2012-02-21 10:38:44 -080043 .name = "strict-ahci",
44 .driver_data = STRICT_AHCI,
45 }, {
Richard Zhu904c04f2011-09-28 15:41:54 +080046 /* sentinel */
47 }
48};
49MODULE_DEVICE_TABLE(platform, ahci_devtype);
50
Richard Zhu8b789d82013-10-15 10:44:54 +080051struct ata_port_operations ahci_platform_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070052 .inherits = &ahci_ops,
53 .host_stop = ahci_host_stop,
54};
Richard Zhu8b789d82013-10-15 10:44:54 +080055EXPORT_SYMBOL_GPL(ahci_platform_ops);
Brian Norris1896b152012-11-02 00:46:17 -070056
Brian Norris071d3ad2012-12-05 23:44:20 -080057static struct ata_port_operations ahci_platform_retry_srst_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070058 .inherits = &ahci_pmp_retry_srst_ops,
59 .host_stop = ahci_host_stop,
60};
Richard Zhu904c04f2011-09-28 15:41:54 +080061
62static const struct ata_port_info ahci_port_info[] = {
63 /* by features */
64 [AHCI] = {
65 .flags = AHCI_FLAG_COMMON,
66 .pio_mask = ATA_PIO4,
67 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070068 .port_ops = &ahci_platform_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080069 },
70 [IMX53_AHCI] = {
71 .flags = AHCI_FLAG_COMMON,
72 .pio_mask = ATA_PIO4,
73 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070074 .port_ops = &ahci_platform_retry_srst_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080075 },
Brian Norrisd408e2b2012-02-21 10:38:44 -080076 [STRICT_AHCI] = {
77 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
78 .flags = AHCI_FLAG_COMMON,
79 .pio_mask = ATA_PIO4,
80 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070081 .port_ops = &ahci_platform_ops,
Brian Norrisd408e2b2012-02-21 10:38:44 -080082 },
Richard Zhu904c04f2011-09-28 15:41:54 +080083};
84
Tejun Heofad16e72010-09-21 09:25:48 +020085static struct scsi_host_template ahci_platform_sht = {
86 AHCI_SHT("ahci_platform"),
87};
88
Hans de Goede156c5882014-02-22 16:53:31 +010089/**
90 * ahci_platform_enable_clks - Enable platform clocks
91 * @hpriv: host private area to store config values
92 *
93 * This function enables all the clks found in hpriv->clks, starting at
94 * index 0. If any clk fails to enable it disables all the clks already
95 * enabled in reverse order, and then returns an error.
96 *
97 * RETURNS:
98 * 0 on success otherwise a negative error code
99 */
100int ahci_platform_enable_clks(struct ahci_host_priv *hpriv)
101{
102 int c, rc;
103
104 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++) {
105 rc = clk_prepare_enable(hpriv->clks[c]);
106 if (rc)
107 goto disable_unprepare_clk;
108 }
109 return 0;
110
111disable_unprepare_clk:
112 while (--c >= 0)
113 clk_disable_unprepare(hpriv->clks[c]);
114 return rc;
115}
116EXPORT_SYMBOL_GPL(ahci_platform_enable_clks);
117
118/**
119 * ahci_platform_disable_clks - Disable platform clocks
120 * @hpriv: host private area to store config values
121 *
122 * This function disables all the clks found in hpriv->clks, in reverse
123 * order of ahci_platform_enable_clks (starting at the end of the array).
124 */
125void ahci_platform_disable_clks(struct ahci_host_priv *hpriv)
126{
127 int c;
128
129 for (c = AHCI_MAX_CLKS - 1; c >= 0; c--)
130 if (hpriv->clks[c])
131 clk_disable_unprepare(hpriv->clks[c]);
132}
133EXPORT_SYMBOL_GPL(ahci_platform_disable_clks);
134
135static void ahci_put_clks(struct ahci_host_priv *hpriv)
136{
137 int c;
138
139 for (c = 0; c < AHCI_MAX_CLKS && hpriv->clks[c]; c++)
140 clk_put(hpriv->clks[c]);
141}
142
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -0800143static int ahci_probe(struct platform_device *pdev)
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300144{
145 struct device *dev = &pdev->dev;
JiSheng Zhang00345612011-10-31 21:20:10 +0800146 struct ahci_platform_data *pdata = dev_get_platdata(dev);
Richard Zhu904c04f2011-09-28 15:41:54 +0800147 const struct platform_device_id *id = platform_get_device_id(pdev);
Rob Herringff956132011-11-15 21:00:56 -0600148 struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300149 const struct ata_port_info *ppi[] = { &pi, NULL };
150 struct ahci_host_priv *hpriv;
151 struct ata_host *host;
152 struct resource *mem;
Hans de Goede156c5882014-02-22 16:53:31 +0100153 struct clk *clk;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300154 int irq;
155 int n_ports;
156 int i;
157 int rc;
158
159 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160 if (!mem) {
161 dev_err(dev, "no mmio space\n");
162 return -EINVAL;
163 }
164
165 irq = platform_get_irq(pdev, 0);
166 if (irq <= 0) {
167 dev_err(dev, "no irq\n");
168 return -EINVAL;
169 }
170
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300171 if (pdata && pdata->ata_port_info)
172 pi = *pdata->ata_port_info;
173
174 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
175 if (!hpriv) {
Jassi Brar08354802010-06-25 18:21:19 +0900176 dev_err(dev, "can't alloc ahci_host_priv\n");
177 return -ENOMEM;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300178 }
179
180 hpriv->flags |= (unsigned long)pi.private_data;
181
182 hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
183 if (!hpriv->mmio) {
184 dev_err(dev, "can't map %pR\n", mem);
Jassi Brar08354802010-06-25 18:21:19 +0900185 return -ENOMEM;
186 }
187
Hans de Goede4b3e6032014-02-22 16:53:32 +0100188 hpriv->target_pwr = devm_regulator_get_optional(dev, "target");
189 if (IS_ERR(hpriv->target_pwr)) {
190 rc = PTR_ERR(hpriv->target_pwr);
191 if (rc == -EPROBE_DEFER)
192 return -EPROBE_DEFER;
193 hpriv->target_pwr = NULL;
194 }
195
Hans de Goede156c5882014-02-22 16:53:31 +0100196 for (i = 0; i < AHCI_MAX_CLKS; i++) {
197 /*
198 * For now we must use clk_get(dev, NULL) for the first clock,
199 * because some platforms (da850, spear13xx) are not yet
200 * converted to use devicetree for clocks. For new platforms
201 * this is equivalent to of_clk_get(dev->of_node, 0).
202 */
203 if (i == 0)
204 clk = clk_get(dev, NULL);
205 else
206 clk = of_clk_get(dev->of_node, i);
207
208 if (IS_ERR(clk)) {
209 rc = PTR_ERR(clk);
210 if (rc == -EPROBE_DEFER)
211 goto free_clk;
212 break;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530213 }
Hans de Goede156c5882014-02-22 16:53:31 +0100214 hpriv->clks[i] = clk;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530215 }
216
Hans de Goede4b3e6032014-02-22 16:53:32 +0100217 if (hpriv->target_pwr) {
218 rc = regulator_enable(hpriv->target_pwr);
219 if (rc)
220 goto free_clk;
221 }
222
Hans de Goede156c5882014-02-22 16:53:31 +0100223 rc = ahci_enable_clks(dev, hpriv);
224 if (rc)
Hans de Goede4b3e6032014-02-22 16:53:32 +0100225 goto disable_regulator;
Hans de Goede156c5882014-02-22 16:53:31 +0100226
Jassi Brar08354802010-06-25 18:21:19 +0900227 /*
228 * Some platforms might need to prepare for mmio region access,
229 * which could be done in the following init call. So, the mmio
230 * region shouldn't be accessed before init (if provided) has
231 * returned successfully.
232 */
233 if (pdata && pdata->init) {
234 rc = pdata->init(dev, hpriv->mmio);
235 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530236 goto disable_unprepare_clk;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300237 }
238
239 ahci_save_initial_config(dev, hpriv,
240 pdata ? pdata->force_port_map : 0,
241 pdata ? pdata->mask_port_map : 0);
242
243 /* prepare host */
244 if (hpriv->cap & HOST_CAP_NCQ)
245 pi.flags |= ATA_FLAG_NCQ;
246
247 if (hpriv->cap & HOST_CAP_PMP)
248 pi.flags |= ATA_FLAG_PMP;
249
250 ahci_set_em_messages(hpriv, &pi);
251
252 /* CAP.NP sometimes indicate the index of the last enabled
253 * port, at other times, that of the last possible port, so
254 * determining the maximum port number requires looking at
255 * both CAP.NP and port_map.
256 */
257 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
258
259 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
260 if (!host) {
261 rc = -ENOMEM;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530262 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300263 }
264
265 host->private_data = hpriv;
266
267 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
268 host->flags |= ATA_HOST_PARALLEL_SCAN;
269 else
Jingoo Han0fed4c02013-10-05 09:15:59 +0900270 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300271
272 if (pi.flags & ATA_FLAG_EM)
273 ahci_reset_em(host);
274
275 for (i = 0; i < host->n_ports; i++) {
276 struct ata_port *ap = host->ports[i];
277
278 ata_port_desc(ap, "mmio %pR", mem);
279 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
280
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300281 /* set enclosure management message type */
282 if (ap->flags & ATA_FLAG_EM)
Jeff Garzik55787182010-05-14 17:23:37 -0400283 ap->em_message_type = hpriv->em_msg_type;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300284
285 /* disabled/not-implemented port */
286 if (!(hpriv->port_map & (1 << i)))
287 ap->ops = &ata_dummy_port_ops;
288 }
289
290 rc = ahci_reset_controller(host);
291 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530292 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300293
294 ahci_init_controller(host);
295 ahci_print_info(host, "platform");
296
297 rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
Tejun Heofad16e72010-09-21 09:25:48 +0200298 &ahci_platform_sht);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300299 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530300 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300301
302 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530303pdata_exit:
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300304 if (pdata && pdata->exit)
305 pdata->exit(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530306disable_unprepare_clk:
Hans de Goede156c5882014-02-22 16:53:31 +0100307 ahci_disable_clks(hpriv);
Hans de Goede4b3e6032014-02-22 16:53:32 +0100308disable_regulator:
309 if (hpriv->target_pwr)
310 regulator_disable(hpriv->target_pwr);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530311free_clk:
Hans de Goede156c5882014-02-22 16:53:31 +0100312 ahci_put_clks(hpriv);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300313 return rc;
314}
315
Brian Norris1896b152012-11-02 00:46:17 -0700316static void ahci_host_stop(struct ata_host *host)
317{
318 struct device *dev = host->dev;
319 struct ahci_platform_data *pdata = dev_get_platdata(dev);
320 struct ahci_host_priv *hpriv = host->private_data;
321
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300322 if (pdata && pdata->exit)
323 pdata->exit(dev);
324
Hans de Goede156c5882014-02-22 16:53:31 +0100325 ahci_disable_clks(hpriv);
326 ahci_put_clks(hpriv);
Hans de Goede4b3e6032014-02-22 16:53:32 +0100327
328 if (hpriv->target_pwr)
329 regulator_disable(hpriv->target_pwr);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300330}
331
Yuanhan Liu29448ec2012-10-16 22:59:01 +0800332#ifdef CONFIG_PM_SLEEP
Brian Norris17ab5942011-11-18 11:10:10 -0800333static int ahci_suspend(struct device *dev)
334{
335 struct ahci_platform_data *pdata = dev_get_platdata(dev);
336 struct ata_host *host = dev_get_drvdata(dev);
337 struct ahci_host_priv *hpriv = host->private_data;
338 void __iomem *mmio = hpriv->mmio;
339 u32 ctl;
340 int rc;
341
342 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
343 dev_err(dev, "firmware update required for suspend/resume\n");
344 return -EIO;
345 }
346
347 /*
348 * AHCI spec rev1.1 section 8.3.3:
349 * Software must disable interrupts prior to requesting a
350 * transition of the HBA to D3 state.
351 */
352 ctl = readl(mmio + HOST_CTL);
353 ctl &= ~HOST_IRQ_EN;
354 writel(ctl, mmio + HOST_CTL);
355 readl(mmio + HOST_CTL); /* flush */
356
357 rc = ata_host_suspend(host, PMSG_SUSPEND);
358 if (rc)
359 return rc;
360
361 if (pdata && pdata->suspend)
362 return pdata->suspend(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530363
Hans de Goede156c5882014-02-22 16:53:31 +0100364 ahci_disable_clks(hpriv);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530365
Hans de Goede4b3e6032014-02-22 16:53:32 +0100366 if (hpriv->target_pwr)
367 regulator_disable(hpriv->target_pwr);
368
Brian Norris17ab5942011-11-18 11:10:10 -0800369 return 0;
370}
371
372static int ahci_resume(struct device *dev)
373{
374 struct ahci_platform_data *pdata = dev_get_platdata(dev);
375 struct ata_host *host = dev_get_drvdata(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530376 struct ahci_host_priv *hpriv = host->private_data;
Brian Norris17ab5942011-11-18 11:10:10 -0800377 int rc;
378
Hans de Goede4b3e6032014-02-22 16:53:32 +0100379 if (hpriv->target_pwr) {
380 rc = regulator_enable(hpriv->target_pwr);
381 if (rc)
382 return rc;
383 }
384
Hans de Goede156c5882014-02-22 16:53:31 +0100385 rc = ahci_enable_clks(dev, hpriv);
386 if (rc)
Hans de Goede4b3e6032014-02-22 16:53:32 +0100387 goto disable_regulator;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530388
Brian Norris17ab5942011-11-18 11:10:10 -0800389 if (pdata && pdata->resume) {
390 rc = pdata->resume(dev);
391 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530392 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800393 }
394
395 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
396 rc = ahci_reset_controller(host);
397 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530398 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800399
400 ahci_init_controller(host);
401 }
402
403 ata_host_resume(host);
404
405 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530406
407disable_unprepare_clk:
Hans de Goede156c5882014-02-22 16:53:31 +0100408 ahci_disable_clks(hpriv);
Hans de Goede4b3e6032014-02-22 16:53:32 +0100409disable_regulator:
410 if (hpriv->target_pwr)
411 regulator_disable(hpriv->target_pwr);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530412
413 return rc;
Brian Norris17ab5942011-11-18 11:10:10 -0800414}
Brian Norris17ab5942011-11-18 11:10:10 -0800415#endif
416
Brian Norris071d3ad2012-12-05 23:44:20 -0800417static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
Shiraz Hashim18c25ff2012-03-16 15:49:55 +0530418
Rob Herring02aac312010-11-03 21:04:59 -0500419static const struct of_device_id ahci_of_match[] = {
Viresh Kumar5f098a32012-04-21 17:40:12 +0530420 { .compatible = "snps,spear-ahci", },
Girish K S1e8f5f72013-04-16 14:58:02 +0530421 { .compatible = "snps,exynos5440-ahci", },
Alistair Popple2435dcb2013-11-22 13:08:29 +1100422 { .compatible = "ibm,476gtr-ahci", },
Rob Herring02aac312010-11-03 21:04:59 -0500423 {},
424};
425MODULE_DEVICE_TABLE(of, ahci_of_match);
426
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300427static struct platform_driver ahci_driver = {
Brian Norris941c77f2012-11-02 00:46:15 -0700428 .probe = ahci_probe,
Brian Norris83291d62012-11-02 00:46:19 -0700429 .remove = ata_platform_remove_one,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300430 .driver = {
431 .name = "ahci",
432 .owner = THIS_MODULE,
Rob Herring02aac312010-11-03 21:04:59 -0500433 .of_match_table = ahci_of_match,
Brian Norris17ab5942011-11-18 11:10:10 -0800434 .pm = &ahci_pm_ops,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300435 },
Richard Zhu904c04f2011-09-28 15:41:54 +0800436 .id_table = ahci_devtype,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300437};
Brian Norris9a99e472012-11-02 00:46:16 -0700438module_platform_driver(ahci_driver);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300439
440MODULE_DESCRIPTION("AHCI SATA platform driver");
441MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
442MODULE_LICENSE("GPL");
443MODULE_ALIAS("platform:ahci");