blob: 4b231baceb0995557c2cae40e501f6d3449dc365 [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/init.h>
21#include <linux/interrupt.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24#include <linux/libata.h>
25#include <linux/ahci_platform.h>
26#include "ahci.h"
27
Brian Norris1896b152012-11-02 00:46:17 -070028static void ahci_host_stop(struct ata_host *host);
29
Richard Zhu904c04f2011-09-28 15:41:54 +080030enum ahci_type {
31 AHCI, /* standard platform ahci */
32 IMX53_AHCI, /* ahci on i.mx53 */
Brian Norrisd408e2b2012-02-21 10:38:44 -080033 STRICT_AHCI, /* delayed DMA engine start */
Richard Zhu904c04f2011-09-28 15:41:54 +080034};
35
36static struct platform_device_id ahci_devtype[] = {
37 {
38 .name = "ahci",
39 .driver_data = AHCI,
40 }, {
41 .name = "imx53-ahci",
42 .driver_data = IMX53_AHCI,
43 }, {
Brian Norrisd408e2b2012-02-21 10:38:44 -080044 .name = "strict-ahci",
45 .driver_data = STRICT_AHCI,
46 }, {
Richard Zhu904c04f2011-09-28 15:41:54 +080047 /* sentinel */
48 }
49};
50MODULE_DEVICE_TABLE(platform, ahci_devtype);
51
Richard Zhu8b789d82013-10-15 10:44:54 +080052struct ata_port_operations ahci_platform_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070053 .inherits = &ahci_ops,
54 .host_stop = ahci_host_stop,
55};
Richard Zhu8b789d82013-10-15 10:44:54 +080056EXPORT_SYMBOL_GPL(ahci_platform_ops);
Brian Norris1896b152012-11-02 00:46:17 -070057
Brian Norris071d3ad2012-12-05 23:44:20 -080058static struct ata_port_operations ahci_platform_retry_srst_ops = {
Brian Norris1896b152012-11-02 00:46:17 -070059 .inherits = &ahci_pmp_retry_srst_ops,
60 .host_stop = ahci_host_stop,
61};
Richard Zhu904c04f2011-09-28 15:41:54 +080062
63static const struct ata_port_info ahci_port_info[] = {
64 /* by features */
65 [AHCI] = {
66 .flags = AHCI_FLAG_COMMON,
67 .pio_mask = ATA_PIO4,
68 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070069 .port_ops = &ahci_platform_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080070 },
71 [IMX53_AHCI] = {
72 .flags = AHCI_FLAG_COMMON,
73 .pio_mask = ATA_PIO4,
74 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070075 .port_ops = &ahci_platform_retry_srst_ops,
Richard Zhu904c04f2011-09-28 15:41:54 +080076 },
Brian Norrisd408e2b2012-02-21 10:38:44 -080077 [STRICT_AHCI] = {
78 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
79 .flags = AHCI_FLAG_COMMON,
80 .pio_mask = ATA_PIO4,
81 .udma_mask = ATA_UDMA6,
Brian Norris1896b152012-11-02 00:46:17 -070082 .port_ops = &ahci_platform_ops,
Brian Norrisd408e2b2012-02-21 10:38:44 -080083 },
Richard Zhu904c04f2011-09-28 15:41:54 +080084};
85
Tejun Heofad16e72010-09-21 09:25:48 +020086static struct scsi_host_template ahci_platform_sht = {
87 AHCI_SHT("ahci_platform"),
88};
89
Greg Kroah-Hartman0ec24912012-12-21 13:19:58 -080090static int ahci_probe(struct platform_device *pdev)
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030091{
92 struct device *dev = &pdev->dev;
JiSheng Zhang00345612011-10-31 21:20:10 +080093 struct ahci_platform_data *pdata = dev_get_platdata(dev);
Richard Zhu904c04f2011-09-28 15:41:54 +080094 const struct platform_device_id *id = platform_get_device_id(pdev);
Rob Herringff956132011-11-15 21:00:56 -060095 struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030096 const struct ata_port_info *ppi[] = { &pi, NULL };
97 struct ahci_host_priv *hpriv;
98 struct ata_host *host;
99 struct resource *mem;
100 int irq;
101 int n_ports;
102 int i;
103 int rc;
104
105 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106 if (!mem) {
107 dev_err(dev, "no mmio space\n");
108 return -EINVAL;
109 }
110
111 irq = platform_get_irq(pdev, 0);
112 if (irq <= 0) {
113 dev_err(dev, "no irq\n");
114 return -EINVAL;
115 }
116
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300117 if (pdata && pdata->ata_port_info)
118 pi = *pdata->ata_port_info;
119
120 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
121 if (!hpriv) {
Jassi Brar08354802010-06-25 18:21:19 +0900122 dev_err(dev, "can't alloc ahci_host_priv\n");
123 return -ENOMEM;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300124 }
125
126 hpriv->flags |= (unsigned long)pi.private_data;
127
128 hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
129 if (!hpriv->mmio) {
130 dev_err(dev, "can't map %pR\n", mem);
Jassi Brar08354802010-06-25 18:21:19 +0900131 return -ENOMEM;
132 }
133
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530134 hpriv->clk = clk_get(dev, NULL);
135 if (IS_ERR(hpriv->clk)) {
136 dev_err(dev, "can't get clock\n");
137 } else {
138 rc = clk_prepare_enable(hpriv->clk);
139 if (rc) {
140 dev_err(dev, "clock prepare enable failed");
141 goto free_clk;
142 }
143 }
144
Jassi Brar08354802010-06-25 18:21:19 +0900145 /*
146 * Some platforms might need to prepare for mmio region access,
147 * which could be done in the following init call. So, the mmio
148 * region shouldn't be accessed before init (if provided) has
149 * returned successfully.
150 */
151 if (pdata && pdata->init) {
152 rc = pdata->init(dev, hpriv->mmio);
153 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530154 goto disable_unprepare_clk;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300155 }
156
157 ahci_save_initial_config(dev, hpriv,
158 pdata ? pdata->force_port_map : 0,
159 pdata ? pdata->mask_port_map : 0);
160
161 /* prepare host */
162 if (hpriv->cap & HOST_CAP_NCQ)
163 pi.flags |= ATA_FLAG_NCQ;
164
165 if (hpriv->cap & HOST_CAP_PMP)
166 pi.flags |= ATA_FLAG_PMP;
167
168 ahci_set_em_messages(hpriv, &pi);
169
170 /* CAP.NP sometimes indicate the index of the last enabled
171 * port, at other times, that of the last possible port, so
172 * determining the maximum port number requires looking at
173 * both CAP.NP and port_map.
174 */
175 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
176
177 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
178 if (!host) {
179 rc = -ENOMEM;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530180 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300181 }
182
183 host->private_data = hpriv;
184
185 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
186 host->flags |= ATA_HOST_PARALLEL_SCAN;
187 else
Jingoo Han0fed4c02013-10-05 09:15:59 +0900188 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300189
190 if (pi.flags & ATA_FLAG_EM)
191 ahci_reset_em(host);
192
193 for (i = 0; i < host->n_ports; i++) {
194 struct ata_port *ap = host->ports[i];
195
196 ata_port_desc(ap, "mmio %pR", mem);
197 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
198
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300199 /* set enclosure management message type */
200 if (ap->flags & ATA_FLAG_EM)
Jeff Garzik55787182010-05-14 17:23:37 -0400201 ap->em_message_type = hpriv->em_msg_type;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300202
203 /* disabled/not-implemented port */
204 if (!(hpriv->port_map & (1 << i)))
205 ap->ops = &ata_dummy_port_ops;
206 }
207
208 rc = ahci_reset_controller(host);
209 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530210 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300211
212 ahci_init_controller(host);
213 ahci_print_info(host, "platform");
214
215 rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
Tejun Heofad16e72010-09-21 09:25:48 +0200216 &ahci_platform_sht);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300217 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530218 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300219
220 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530221pdata_exit:
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300222 if (pdata && pdata->exit)
223 pdata->exit(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530224disable_unprepare_clk:
225 if (!IS_ERR(hpriv->clk))
226 clk_disable_unprepare(hpriv->clk);
227free_clk:
228 if (!IS_ERR(hpriv->clk))
229 clk_put(hpriv->clk);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300230 return rc;
231}
232
Brian Norris1896b152012-11-02 00:46:17 -0700233static void ahci_host_stop(struct ata_host *host)
234{
235 struct device *dev = host->dev;
236 struct ahci_platform_data *pdata = dev_get_platdata(dev);
237 struct ahci_host_priv *hpriv = host->private_data;
238
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300239 if (pdata && pdata->exit)
240 pdata->exit(dev);
241
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530242 if (!IS_ERR(hpriv->clk)) {
243 clk_disable_unprepare(hpriv->clk);
244 clk_put(hpriv->clk);
245 }
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300246}
247
Yuanhan Liu29448ec2012-10-16 22:59:01 +0800248#ifdef CONFIG_PM_SLEEP
Brian Norris17ab5942011-11-18 11:10:10 -0800249static int ahci_suspend(struct device *dev)
250{
251 struct ahci_platform_data *pdata = dev_get_platdata(dev);
252 struct ata_host *host = dev_get_drvdata(dev);
253 struct ahci_host_priv *hpriv = host->private_data;
254 void __iomem *mmio = hpriv->mmio;
255 u32 ctl;
256 int rc;
257
258 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
259 dev_err(dev, "firmware update required for suspend/resume\n");
260 return -EIO;
261 }
262
263 /*
264 * AHCI spec rev1.1 section 8.3.3:
265 * Software must disable interrupts prior to requesting a
266 * transition of the HBA to D3 state.
267 */
268 ctl = readl(mmio + HOST_CTL);
269 ctl &= ~HOST_IRQ_EN;
270 writel(ctl, mmio + HOST_CTL);
271 readl(mmio + HOST_CTL); /* flush */
272
273 rc = ata_host_suspend(host, PMSG_SUSPEND);
274 if (rc)
275 return rc;
276
277 if (pdata && pdata->suspend)
278 return pdata->suspend(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530279
280 if (!IS_ERR(hpriv->clk))
281 clk_disable_unprepare(hpriv->clk);
282
Brian Norris17ab5942011-11-18 11:10:10 -0800283 return 0;
284}
285
286static int ahci_resume(struct device *dev)
287{
288 struct ahci_platform_data *pdata = dev_get_platdata(dev);
289 struct ata_host *host = dev_get_drvdata(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530290 struct ahci_host_priv *hpriv = host->private_data;
Brian Norris17ab5942011-11-18 11:10:10 -0800291 int rc;
292
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530293 if (!IS_ERR(hpriv->clk)) {
294 rc = clk_prepare_enable(hpriv->clk);
295 if (rc) {
296 dev_err(dev, "clock prepare enable failed");
297 return rc;
298 }
299 }
300
Brian Norris17ab5942011-11-18 11:10:10 -0800301 if (pdata && pdata->resume) {
302 rc = pdata->resume(dev);
303 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530304 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800305 }
306
307 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
308 rc = ahci_reset_controller(host);
309 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530310 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800311
312 ahci_init_controller(host);
313 }
314
315 ata_host_resume(host);
316
317 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530318
319disable_unprepare_clk:
320 if (!IS_ERR(hpriv->clk))
321 clk_disable_unprepare(hpriv->clk);
322
323 return rc;
Brian Norris17ab5942011-11-18 11:10:10 -0800324}
Brian Norris17ab5942011-11-18 11:10:10 -0800325#endif
326
Brian Norris071d3ad2012-12-05 23:44:20 -0800327static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
Shiraz Hashim18c25ff2012-03-16 15:49:55 +0530328
Rob Herring02aac312010-11-03 21:04:59 -0500329static const struct of_device_id ahci_of_match[] = {
Viresh Kumar5f098a32012-04-21 17:40:12 +0530330 { .compatible = "snps,spear-ahci", },
Girish K S1e8f5f72013-04-16 14:58:02 +0530331 { .compatible = "snps,exynos5440-ahci", },
Alistair Popple2435dcb2013-11-22 13:08:29 +1100332 { .compatible = "ibm,476gtr-ahci", },
Rob Herring02aac312010-11-03 21:04:59 -0500333 {},
334};
335MODULE_DEVICE_TABLE(of, ahci_of_match);
336
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300337static struct platform_driver ahci_driver = {
Brian Norris941c77f2012-11-02 00:46:15 -0700338 .probe = ahci_probe,
Brian Norris83291d62012-11-02 00:46:19 -0700339 .remove = ata_platform_remove_one,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300340 .driver = {
341 .name = "ahci",
342 .owner = THIS_MODULE,
Rob Herring02aac312010-11-03 21:04:59 -0500343 .of_match_table = ahci_of_match,
Brian Norris17ab5942011-11-18 11:10:10 -0800344 .pm = &ahci_pm_ops,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300345 },
Richard Zhu904c04f2011-09-28 15:41:54 +0800346 .id_table = ahci_devtype,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300347};
Brian Norris9a99e472012-11-02 00:46:16 -0700348module_platform_driver(ahci_driver);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300349
350MODULE_DESCRIPTION("AHCI SATA platform driver");
351MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
352MODULE_LICENSE("GPL");
353MODULE_ALIAS("platform:ahci");