2 * AHCI SATA platform driver
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>
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)
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/module.h>
20 #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 <linux/of_device.h>
29 static void ahci_host_stop(struct ata_host *host);
32 AHCI, /* standard platform ahci */
33 IMX53_AHCI, /* ahci on i.mx53 */
34 STRICT_AHCI, /* delayed DMA engine start */
37 struct ata_port_operations ahci_platform_ops = {
38 .inherits = &ahci_ops,
39 .host_stop = ahci_host_stop,
41 EXPORT_SYMBOL_GPL(ahci_platform_ops);
43 static struct ata_port_operations ahci_platform_retry_srst_ops = {
44 .inherits = &ahci_pmp_retry_srst_ops,
45 .host_stop = ahci_host_stop,
48 static const struct ata_port_info ahci_port_info = {
49 .flags = AHCI_FLAG_COMMON,
51 .udma_mask = ATA_UDMA6,
52 .port_ops = &ahci_platform_ops,
55 static const struct ata_port_info imx53_ahci_port_info = {
56 .flags = AHCI_FLAG_COMMON,
58 .udma_mask = ATA_UDMA6,
59 .port_ops = &ahci_platform_retry_srst_ops,
62 static const struct ata_port_info strict_ahci_port_info = {
63 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
64 .flags = AHCI_FLAG_COMMON,
66 .udma_mask = ATA_UDMA6,
67 .port_ops = &ahci_platform_ops,
70 static struct platform_device_id ahci_devtype[] = {
73 .driver_data = (kernel_ulong_t)&ahci_port_info,
77 .driver_data = (kernel_ulong_t)&imx53_ahci_port_info,
80 .name = "strict-ahci",
81 .driver_data = (kernel_ulong_t)&strict_ahci_port_info,
85 MODULE_DEVICE_TABLE(platform, ahci_devtype);
87 static const struct of_device_id ahci_of_match[] = {
88 { .compatible = "snps,spear-ahci", },
89 { .compatible = "snps,exynos5440-ahci", },
90 { .compatible = "fsl,imx53-ahci", .data = &imx53_ahci_port_info, },
93 MODULE_DEVICE_TABLE(of, ahci_of_match);
95 static struct scsi_host_template ahci_platform_sht = {
96 AHCI_SHT("ahci_platform"),
99 static int ahci_probe(struct platform_device *pdev)
101 struct device *dev = &pdev->dev;
102 struct ahci_platform_data *pdata = dev_get_platdata(dev);
103 const struct of_device_id *of_id;
104 struct ata_port_info pi;
105 const struct ata_port_info *ppi[] = { &pi, NULL, };
106 struct ahci_host_priv *hpriv;
107 struct ata_host *host;
108 struct resource *mem;
113 const struct ata_port_info *p;
115 of_id = of_match_device(ahci_of_match, &pdev->dev);
119 const struct platform_device_id *id;
121 id = platform_get_device_id(pdev);
122 p = (struct ata_port_info *)id->driver_data;
126 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 dev_err(dev, "no mmio space\n");
132 irq = platform_get_irq(pdev, 0);
134 dev_err(dev, "no irq\n");
138 if (pdata && pdata->ata_port_info)
139 pi = *pdata->ata_port_info;
141 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
143 dev_err(dev, "can't alloc ahci_host_priv\n");
147 hpriv->flags |= (unsigned long)pi.private_data;
149 hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
151 dev_err(dev, "can't map %pR\n", mem);
155 hpriv->clk = clk_get(dev, NULL);
156 if (IS_ERR(hpriv->clk)) {
157 dev_err(dev, "can't get clock\n");
159 rc = clk_prepare_enable(hpriv->clk);
161 dev_err(dev, "clock prepare enable failed");
167 * Some platforms might need to prepare for mmio region access,
168 * which could be done in the following init call. So, the mmio
169 * region shouldn't be accessed before init (if provided) has
170 * returned successfully.
172 if (pdata && pdata->init) {
173 rc = pdata->init(dev, hpriv->mmio);
175 goto disable_unprepare_clk;
178 ahci_save_initial_config(dev, hpriv,
179 pdata ? pdata->force_port_map : 0,
180 pdata ? pdata->mask_port_map : 0);
183 if (hpriv->cap & HOST_CAP_NCQ)
184 pi.flags |= ATA_FLAG_NCQ;
186 if (hpriv->cap & HOST_CAP_PMP)
187 pi.flags |= ATA_FLAG_PMP;
189 ahci_set_em_messages(hpriv, &pi);
191 /* CAP.NP sometimes indicate the index of the last enabled
192 * port, at other times, that of the last possible port, so
193 * determining the maximum port number requires looking at
194 * both CAP.NP and port_map.
196 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
198 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
204 host->private_data = hpriv;
206 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
207 host->flags |= ATA_HOST_PARALLEL_SCAN;
209 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
211 if (pi.flags & ATA_FLAG_EM)
214 for (i = 0; i < host->n_ports; i++) {
215 struct ata_port *ap = host->ports[i];
217 ata_port_desc(ap, "mmio %pR", mem);
218 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
220 /* set enclosure management message type */
221 if (ap->flags & ATA_FLAG_EM)
222 ap->em_message_type = hpriv->em_msg_type;
224 /* disabled/not-implemented port */
225 if (!(hpriv->port_map & (1 << i)))
226 ap->ops = &ata_dummy_port_ops;
229 rc = ahci_reset_controller(host);
233 ahci_init_controller(host);
234 ahci_print_info(host, "platform");
236 rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
243 if (pdata && pdata->exit)
245 disable_unprepare_clk:
246 if (!IS_ERR(hpriv->clk))
247 clk_disable_unprepare(hpriv->clk);
249 if (!IS_ERR(hpriv->clk))
254 static void ahci_host_stop(struct ata_host *host)
256 struct device *dev = host->dev;
257 struct ahci_platform_data *pdata = dev_get_platdata(dev);
258 struct ahci_host_priv *hpriv = host->private_data;
260 if (pdata && pdata->exit)
263 if (!IS_ERR(hpriv->clk)) {
264 clk_disable_unprepare(hpriv->clk);
269 #ifdef CONFIG_PM_SLEEP
270 static int ahci_suspend(struct device *dev)
272 struct ahci_platform_data *pdata = dev_get_platdata(dev);
273 struct ata_host *host = dev_get_drvdata(dev);
274 struct ahci_host_priv *hpriv = host->private_data;
275 void __iomem *mmio = hpriv->mmio;
279 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
280 dev_err(dev, "firmware update required for suspend/resume\n");
285 * AHCI spec rev1.1 section 8.3.3:
286 * Software must disable interrupts prior to requesting a
287 * transition of the HBA to D3 state.
289 ctl = readl(mmio + HOST_CTL);
291 writel(ctl, mmio + HOST_CTL);
292 readl(mmio + HOST_CTL); /* flush */
294 rc = ata_host_suspend(host, PMSG_SUSPEND);
298 if (pdata && pdata->suspend)
299 return pdata->suspend(dev);
301 if (!IS_ERR(hpriv->clk))
302 clk_disable_unprepare(hpriv->clk);
307 static int ahci_resume(struct device *dev)
309 struct ahci_platform_data *pdata = dev_get_platdata(dev);
310 struct ata_host *host = dev_get_drvdata(dev);
311 struct ahci_host_priv *hpriv = host->private_data;
314 if (!IS_ERR(hpriv->clk)) {
315 rc = clk_prepare_enable(hpriv->clk);
317 dev_err(dev, "clock prepare enable failed");
322 if (pdata && pdata->resume) {
323 rc = pdata->resume(dev);
325 goto disable_unprepare_clk;
328 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
329 rc = ahci_reset_controller(host);
331 goto disable_unprepare_clk;
333 ahci_init_controller(host);
336 ata_host_resume(host);
340 disable_unprepare_clk:
341 if (!IS_ERR(hpriv->clk))
342 clk_disable_unprepare(hpriv->clk);
348 static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
350 static struct platform_driver ahci_driver = {
352 .remove = ata_platform_remove_one,
355 .owner = THIS_MODULE,
356 .of_match_table = ahci_of_match,
359 .id_table = ahci_devtype,
361 module_platform_driver(ahci_driver);
363 MODULE_DESCRIPTION("AHCI SATA platform driver");
364 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
365 MODULE_LICENSE("GPL");
366 MODULE_ALIAS("platform:ahci");