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>
29 AHCI, /* standard platform ahci */
30 IMX53_AHCI, /* ahci on i.mx53 */
31 STRICT_AHCI, /* delayed DMA engine start */
34 static struct platform_device_id ahci_devtype[] = {
40 .driver_data = IMX53_AHCI,
42 .name = "strict-ahci",
43 .driver_data = STRICT_AHCI,
48 MODULE_DEVICE_TABLE(platform, ahci_devtype);
51 static const struct ata_port_info ahci_port_info[] = {
54 .flags = AHCI_FLAG_COMMON,
56 .udma_mask = ATA_UDMA6,
57 .port_ops = &ahci_ops,
60 .flags = AHCI_FLAG_COMMON,
62 .udma_mask = ATA_UDMA6,
63 .port_ops = &ahci_pmp_retry_srst_ops,
66 AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE),
67 .flags = AHCI_FLAG_COMMON,
69 .udma_mask = ATA_UDMA6,
70 .port_ops = &ahci_ops,
74 static struct scsi_host_template ahci_platform_sht = {
75 AHCI_SHT("ahci_platform"),
78 static int __init ahci_probe(struct platform_device *pdev)
80 struct device *dev = &pdev->dev;
81 struct ahci_platform_data *pdata = dev_get_platdata(dev);
82 const struct platform_device_id *id = platform_get_device_id(pdev);
83 struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
84 const struct ata_port_info *ppi[] = { &pi, NULL };
85 struct ahci_host_priv *hpriv;
86 struct ata_host *host;
93 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 dev_err(dev, "no mmio space\n");
99 irq = platform_get_irq(pdev, 0);
101 dev_err(dev, "no irq\n");
105 if (pdata && pdata->ata_port_info)
106 pi = *pdata->ata_port_info;
108 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
110 dev_err(dev, "can't alloc ahci_host_priv\n");
114 hpriv->flags |= (unsigned long)pi.private_data;
116 hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
118 dev_err(dev, "can't map %pR\n", mem);
122 hpriv->clk = clk_get(dev, NULL);
123 if (IS_ERR(hpriv->clk)) {
124 dev_err(dev, "can't get clock\n");
126 rc = clk_prepare_enable(hpriv->clk);
128 dev_err(dev, "clock prepare enable failed");
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.
139 if (pdata && pdata->init) {
140 rc = pdata->init(dev, hpriv->mmio);
142 goto disable_unprepare_clk;
145 ahci_save_initial_config(dev, hpriv,
146 pdata ? pdata->force_port_map : 0,
147 pdata ? pdata->mask_port_map : 0);
150 if (hpriv->cap & HOST_CAP_NCQ)
151 pi.flags |= ATA_FLAG_NCQ;
153 if (hpriv->cap & HOST_CAP_PMP)
154 pi.flags |= ATA_FLAG_PMP;
156 ahci_set_em_messages(hpriv, &pi);
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.
163 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
165 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
171 host->private_data = hpriv;
173 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
174 host->flags |= ATA_HOST_PARALLEL_SCAN;
176 printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
178 if (pi.flags & ATA_FLAG_EM)
181 for (i = 0; i < host->n_ports; i++) {
182 struct ata_port *ap = host->ports[i];
184 ata_port_desc(ap, "mmio %pR", mem);
185 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
187 /* set enclosure management message type */
188 if (ap->flags & ATA_FLAG_EM)
189 ap->em_message_type = hpriv->em_msg_type;
191 /* disabled/not-implemented port */
192 if (!(hpriv->port_map & (1 << i)))
193 ap->ops = &ata_dummy_port_ops;
196 rc = ahci_reset_controller(host);
200 ahci_init_controller(host);
201 ahci_print_info(host, "platform");
203 rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
210 if (pdata && pdata->exit)
212 disable_unprepare_clk:
213 if (!IS_ERR(hpriv->clk))
214 clk_disable_unprepare(hpriv->clk);
216 if (!IS_ERR(hpriv->clk))
221 static int __devexit ahci_remove(struct platform_device *pdev)
223 struct device *dev = &pdev->dev;
224 struct ahci_platform_data *pdata = dev_get_platdata(dev);
225 struct ata_host *host = dev_get_drvdata(dev);
226 struct ahci_host_priv *hpriv = host->private_data;
228 ata_host_detach(host);
230 if (pdata && pdata->exit)
233 if (!IS_ERR(hpriv->clk)) {
234 clk_disable_unprepare(hpriv->clk);
241 #ifdef CONFIG_PM_SLEEP
242 static int ahci_suspend(struct device *dev)
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;
251 if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
252 dev_err(dev, "firmware update required for suspend/resume\n");
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.
261 ctl = readl(mmio + HOST_CTL);
263 writel(ctl, mmio + HOST_CTL);
264 readl(mmio + HOST_CTL); /* flush */
266 rc = ata_host_suspend(host, PMSG_SUSPEND);
270 if (pdata && pdata->suspend)
271 return pdata->suspend(dev);
273 if (!IS_ERR(hpriv->clk))
274 clk_disable_unprepare(hpriv->clk);
279 static int ahci_resume(struct device *dev)
281 struct ahci_platform_data *pdata = dev_get_platdata(dev);
282 struct ata_host *host = dev_get_drvdata(dev);
283 struct ahci_host_priv *hpriv = host->private_data;
286 if (!IS_ERR(hpriv->clk)) {
287 rc = clk_prepare_enable(hpriv->clk);
289 dev_err(dev, "clock prepare enable failed");
294 if (pdata && pdata->resume) {
295 rc = pdata->resume(dev);
297 goto disable_unprepare_clk;
300 if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
301 rc = ahci_reset_controller(host);
303 goto disable_unprepare_clk;
305 ahci_init_controller(host);
308 ata_host_resume(host);
312 disable_unprepare_clk:
313 if (!IS_ERR(hpriv->clk))
314 clk_disable_unprepare(hpriv->clk);
320 SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
322 static const struct of_device_id ahci_of_match[] = {
323 { .compatible = "snps,spear-ahci", },
326 MODULE_DEVICE_TABLE(of, ahci_of_match);
328 static struct platform_driver ahci_driver = {
329 .remove = __devexit_p(ahci_remove),
332 .owner = THIS_MODULE,
333 .of_match_table = ahci_of_match,
336 .id_table = ahci_devtype,
339 static int __init ahci_init(void)
341 return platform_driver_probe(&ahci_driver, ahci_probe);
343 module_init(ahci_init);
345 static void __exit ahci_exit(void)
347 platform_driver_unregister(&ahci_driver);
349 module_exit(ahci_exit);
351 MODULE_DESCRIPTION("AHCI SATA platform driver");
352 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
353 MODULE_LICENSE("GPL");
354 MODULE_ALIAS("platform:ahci");