blob: b1ae48054dc5773eab42917d5e9d10f9764602ae [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
Richard Zhu904c04fe2011-09-28 15:41:54 +080028enum ahci_type {
29 AHCI, /* standard platform ahci */
30 IMX53_AHCI, /* ahci on i.mx53 */
Brian Norrisd408e2b2012-02-21 10:38:44 -080031 STRICT_AHCI, /* delayed DMA engine start */
Richard Zhu904c04fe2011-09-28 15:41:54 +080032};
33
34static struct platform_device_id ahci_devtype[] = {
35 {
36 .name = "ahci",
37 .driver_data = AHCI,
38 }, {
39 .name = "imx53-ahci",
40 .driver_data = IMX53_AHCI,
41 }, {
Brian Norrisd408e2b2012-02-21 10:38:44 -080042 .name = "strict-ahci",
43 .driver_data = STRICT_AHCI,
44 }, {
Richard Zhu904c04fe2011-09-28 15:41:54 +080045 /* sentinel */
46 }
47};
48MODULE_DEVICE_TABLE(platform, ahci_devtype);
49
50
51static const struct ata_port_info ahci_port_info[] = {
52 /* by features */
53 [AHCI] = {
54 .flags = AHCI_FLAG_COMMON,
55 .pio_mask = ATA_PIO4,
56 .udma_mask = ATA_UDMA6,
57 .port_ops = &ahci_ops,
58 },
59 [IMX53_AHCI] = {
60 .flags = AHCI_FLAG_COMMON,
61 .pio_mask = ATA_PIO4,
62 .udma_mask = ATA_UDMA6,
63 .port_ops = &ahci_pmp_retry_srst_ops,
64 },
Brian Norrisd408e2b2012-02-21 10:38:44 -080065 [STRICT_AHCI] = {
66 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
67 .flags = AHCI_FLAG_COMMON,
68 .pio_mask = ATA_PIO4,
69 .udma_mask = ATA_UDMA6,
70 .port_ops = &ahci_ops,
71 },
Richard Zhu904c04fe2011-09-28 15:41:54 +080072};
73
Tejun Heofad16e72010-09-21 09:25:48 +020074static struct scsi_host_template ahci_platform_sht = {
75 AHCI_SHT("ahci_platform"),
76};
77
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030078static int __init ahci_probe(struct platform_device *pdev)
79{
80 struct device *dev = &pdev->dev;
JiSheng Zhang00345612011-10-31 21:20:10 +080081 struct ahci_platform_data *pdata = dev_get_platdata(dev);
Richard Zhu904c04fe2011-09-28 15:41:54 +080082 const struct platform_device_id *id = platform_get_device_id(pdev);
Rob Herringff956132011-11-15 21:00:56 -060083 struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +030084 const struct ata_port_info *ppi[] = { &pi, NULL };
85 struct ahci_host_priv *hpriv;
86 struct ata_host *host;
87 struct resource *mem;
88 int irq;
89 int n_ports;
90 int i;
91 int rc;
92
93 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 if (!mem) {
95 dev_err(dev, "no mmio space\n");
96 return -EINVAL;
97 }
98
99 irq = platform_get_irq(pdev, 0);
100 if (irq <= 0) {
101 dev_err(dev, "no irq\n");
102 return -EINVAL;
103 }
104
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300105 if (pdata && pdata->ata_port_info)
106 pi = *pdata->ata_port_info;
107
108 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
109 if (!hpriv) {
Jassi Brar08354802010-06-25 18:21:19 +0900110 dev_err(dev, "can't alloc ahci_host_priv\n");
111 return -ENOMEM;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300112 }
113
114 hpriv->flags |= (unsigned long)pi.private_data;
115
116 hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
117 if (!hpriv->mmio) {
118 dev_err(dev, "can't map %pR\n", mem);
Jassi Brar08354802010-06-25 18:21:19 +0900119 return -ENOMEM;
120 }
121
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530122 hpriv->clk = clk_get(dev, NULL);
123 if (IS_ERR(hpriv->clk)) {
124 dev_err(dev, "can't get clock\n");
125 } else {
126 rc = clk_prepare_enable(hpriv->clk);
127 if (rc) {
128 dev_err(dev, "clock prepare enable failed");
129 goto free_clk;
130 }
131 }
132
Jassi Brar08354802010-06-25 18:21:19 +0900133 /*
134 * Some platforms might need to prepare for mmio region access,
135 * which could be done in the following init call. So, the mmio
136 * region shouldn't be accessed before init (if provided) has
137 * returned successfully.
138 */
139 if (pdata && pdata->init) {
140 rc = pdata->init(dev, hpriv->mmio);
141 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530142 goto disable_unprepare_clk;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300143 }
144
145 ahci_save_initial_config(dev, hpriv,
146 pdata ? pdata->force_port_map : 0,
147 pdata ? pdata->mask_port_map : 0);
148
149 /* prepare host */
150 if (hpriv->cap & HOST_CAP_NCQ)
151 pi.flags |= ATA_FLAG_NCQ;
152
153 if (hpriv->cap & HOST_CAP_PMP)
154 pi.flags |= ATA_FLAG_PMP;
155
156 ahci_set_em_messages(hpriv, &pi);
157
158 /* CAP.NP sometimes indicate the index of the last enabled
159 * port, at other times, that of the last possible port, so
160 * determining the maximum port number requires looking at
161 * both CAP.NP and port_map.
162 */
163 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
164
165 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
166 if (!host) {
167 rc = -ENOMEM;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530168 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300169 }
170
171 host->private_data = hpriv;
172
173 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
174 host->flags |= ATA_HOST_PARALLEL_SCAN;
175 else
176 printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
177
178 if (pi.flags & ATA_FLAG_EM)
179 ahci_reset_em(host);
180
181 for (i = 0; i < host->n_ports; i++) {
182 struct ata_port *ap = host->ports[i];
183
184 ata_port_desc(ap, "mmio %pR", mem);
185 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
186
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300187 /* set enclosure management message type */
188 if (ap->flags & ATA_FLAG_EM)
Jeff Garzik55787182010-05-14 17:23:37 -0400189 ap->em_message_type = hpriv->em_msg_type;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300190
191 /* disabled/not-implemented port */
192 if (!(hpriv->port_map & (1 << i)))
193 ap->ops = &ata_dummy_port_ops;
194 }
195
196 rc = ahci_reset_controller(host);
197 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530198 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300199
200 ahci_init_controller(host);
201 ahci_print_info(host, "platform");
202
203 rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
Tejun Heofad16e72010-09-21 09:25:48 +0200204 &ahci_platform_sht);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300205 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530206 goto pdata_exit;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300207
208 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530209pdata_exit:
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300210 if (pdata && pdata->exit)
211 pdata->exit(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530212disable_unprepare_clk:
213 if (!IS_ERR(hpriv->clk))
214 clk_disable_unprepare(hpriv->clk);
215free_clk:
216 if (!IS_ERR(hpriv->clk))
217 clk_put(hpriv->clk);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300218 return rc;
219}
220
221static int __devexit ahci_remove(struct platform_device *pdev)
222{
223 struct device *dev = &pdev->dev;
JiSheng Zhang00345612011-10-31 21:20:10 +0800224 struct ahci_platform_data *pdata = dev_get_platdata(dev);
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300225 struct ata_host *host = dev_get_drvdata(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530226 struct ahci_host_priv *hpriv = host->private_data;
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300227
228 ata_host_detach(host);
229
230 if (pdata && pdata->exit)
231 pdata->exit(dev);
232
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530233 if (!IS_ERR(hpriv->clk)) {
234 clk_disable_unprepare(hpriv->clk);
235 clk_put(hpriv->clk);
236 }
237
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300238 return 0;
239}
240
Brian Norris17ab5942011-11-18 11:10:10 -0800241#ifdef CONFIG_PM
242static int ahci_suspend(struct device *dev)
243{
244 struct ahci_platform_data *pdata = dev_get_platdata(dev);
245 struct ata_host *host = dev_get_drvdata(dev);
246 struct ahci_host_priv *hpriv = host->private_data;
247 void __iomem *mmio = hpriv->mmio;
248 u32 ctl;
249 int rc;
250
251 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
252 dev_err(dev, "firmware update required for suspend/resume\n");
253 return -EIO;
254 }
255
256 /*
257 * AHCI spec rev1.1 section 8.3.3:
258 * Software must disable interrupts prior to requesting a
259 * transition of the HBA to D3 state.
260 */
261 ctl = readl(mmio + HOST_CTL);
262 ctl &= ~HOST_IRQ_EN;
263 writel(ctl, mmio + HOST_CTL);
264 readl(mmio + HOST_CTL); /* flush */
265
266 rc = ata_host_suspend(host, PMSG_SUSPEND);
267 if (rc)
268 return rc;
269
270 if (pdata && pdata->suspend)
271 return pdata->suspend(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530272
273 if (!IS_ERR(hpriv->clk))
274 clk_disable_unprepare(hpriv->clk);
275
Brian Norris17ab5942011-11-18 11:10:10 -0800276 return 0;
277}
278
279static int ahci_resume(struct device *dev)
280{
281 struct ahci_platform_data *pdata = dev_get_platdata(dev);
282 struct ata_host *host = dev_get_drvdata(dev);
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530283 struct ahci_host_priv *hpriv = host->private_data;
Brian Norris17ab5942011-11-18 11:10:10 -0800284 int rc;
285
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530286 if (!IS_ERR(hpriv->clk)) {
287 rc = clk_prepare_enable(hpriv->clk);
288 if (rc) {
289 dev_err(dev, "clock prepare enable failed");
290 return rc;
291 }
292 }
293
Brian Norris17ab5942011-11-18 11:10:10 -0800294 if (pdata && pdata->resume) {
295 rc = pdata->resume(dev);
296 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530297 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800298 }
299
300 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
301 rc = ahci_reset_controller(host);
302 if (rc)
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530303 goto disable_unprepare_clk;
Brian Norris17ab5942011-11-18 11:10:10 -0800304
305 ahci_init_controller(host);
306 }
307
308 ata_host_resume(host);
309
310 return 0;
Viresh Kumarf1e70c22012-08-27 10:37:19 +0530311
312disable_unprepare_clk:
313 if (!IS_ERR(hpriv->clk))
314 clk_disable_unprepare(hpriv->clk);
315
316 return rc;
Brian Norris17ab5942011-11-18 11:10:10 -0800317}
Brian Norris17ab5942011-11-18 11:10:10 -0800318#endif
319
Shiraz Hashim18c25ff2012-03-16 15:49:55 +0530320SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
321
Rob Herring02aac312010-11-03 21:04:59 -0500322static const struct of_device_id ahci_of_match[] = {
Viresh Kumar5f098a32012-04-21 17:40:12 +0530323 { .compatible = "snps,spear-ahci", },
Rob Herring02aac312010-11-03 21:04:59 -0500324 {},
325};
326MODULE_DEVICE_TABLE(of, ahci_of_match);
327
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300328static struct platform_driver ahci_driver = {
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300329 .remove = __devexit_p(ahci_remove),
330 .driver = {
331 .name = "ahci",
332 .owner = THIS_MODULE,
Rob Herring02aac312010-11-03 21:04:59 -0500333 .of_match_table = ahci_of_match,
Brian Norris17ab5942011-11-18 11:10:10 -0800334 .pm = &ahci_pm_ops,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300335 },
Richard Zhu904c04fe2011-09-28 15:41:54 +0800336 .id_table = ahci_devtype,
Anton Vorontsov1c2a49f2010-03-04 20:06:06 +0300337};
338
339static int __init ahci_init(void)
340{
341 return platform_driver_probe(&ahci_driver, ahci_probe);
342}
343module_init(ahci_init);
344
345static void __exit ahci_exit(void)
346{
347 platform_driver_unregister(&ahci_driver);
348}
349module_exit(ahci_exit);
350
351MODULE_DESCRIPTION("AHCI SATA platform driver");
352MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
353MODULE_LICENSE("GPL");
354MODULE_ALIAS("platform:ahci");