]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/ata/ahci_platform.c
ahci_platform: enable DT probing
[karo-tx-linux.git] / drivers / ata / ahci_platform.c
1 /*
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
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/module.h>
19 #include <linux/pm.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>
27 #include "ahci.h"
28
29 static void ahci_host_stop(struct ata_host *host);
30
31 enum ahci_type {
32         AHCI,           /* standard platform ahci */
33         IMX53_AHCI,     /* ahci on i.mx53 */
34         STRICT_AHCI,    /* delayed DMA engine start */
35 };
36
37 struct ata_port_operations ahci_platform_ops = {
38         .inherits       = &ahci_ops,
39         .host_stop      = ahci_host_stop,
40 };
41 EXPORT_SYMBOL_GPL(ahci_platform_ops);
42
43 static struct ata_port_operations ahci_platform_retry_srst_ops = {
44         .inherits       = &ahci_pmp_retry_srst_ops,
45         .host_stop      = ahci_host_stop,
46 };
47
48 static const struct ata_port_info ahci_port_info = {
49         .flags          = AHCI_FLAG_COMMON,
50         .pio_mask       = ATA_PIO4,
51         .udma_mask      = ATA_UDMA6,
52         .port_ops       = &ahci_platform_ops,
53 };
54
55 static const struct ata_port_info imx53_ahci_port_info = {
56         .flags          = AHCI_FLAG_COMMON,
57         .pio_mask       = ATA_PIO4,
58         .udma_mask      = ATA_UDMA6,
59         .port_ops       = &ahci_platform_retry_srst_ops,
60 };
61
62 static const struct ata_port_info strict_ahci_port_info = {
63         AHCI_HFLAGS     (AHCI_HFLAG_DELAY_ENGINE),
64         .flags          = AHCI_FLAG_COMMON,
65         .pio_mask       = ATA_PIO4,
66         .udma_mask      = ATA_UDMA6,
67         .port_ops       = &ahci_platform_ops,
68 };
69
70 static struct platform_device_id ahci_devtype[] = {
71         {
72                 .name = "ahci",
73                 .driver_data = (kernel_ulong_t)&ahci_port_info,
74         },
75         {
76                 .name = "imx53-ahci",
77                 .driver_data = (kernel_ulong_t)&imx53_ahci_port_info,
78         },
79         {
80                 .name = "strict-ahci",
81                 .driver_data = (kernel_ulong_t)&strict_ahci_port_info,
82         },
83         { /* sentinel */ }
84 };
85 MODULE_DEVICE_TABLE(platform, ahci_devtype);
86
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, },
91         { /* sentinel */ }
92 };
93 MODULE_DEVICE_TABLE(of, ahci_of_match);
94
95 static struct scsi_host_template ahci_platform_sht = {
96         AHCI_SHT("ahci_platform"),
97 };
98
99 static int ahci_probe(struct platform_device *pdev)
100 {
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;
109         int irq;
110         int n_ports;
111         int i;
112         int rc;
113         const struct ata_port_info *p;
114
115         of_id = of_match_device(ahci_of_match, &pdev->dev);
116         if (of_id) {
117                 p = of_id->data;
118         } else {
119                 const struct platform_device_id *id;
120
121                 id = platform_get_device_id(pdev);
122                 p = (struct ata_port_info *)id->driver_data;
123         }
124         pi = *p;
125
126         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127         if (!mem) {
128                 dev_err(dev, "no mmio space\n");
129                 return -ENODEV;
130         }
131
132         irq = platform_get_irq(pdev, 0);
133         if (irq < 0) {
134                 dev_err(dev, "no irq\n");
135                 return irq;
136         }
137
138         if (pdata && pdata->ata_port_info)
139                 pi = *pdata->ata_port_info;
140
141         hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
142         if (!hpriv) {
143                 dev_err(dev, "can't alloc ahci_host_priv\n");
144                 return -ENOMEM;
145         }
146
147         hpriv->flags |= (unsigned long)pi.private_data;
148
149         hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
150         if (!hpriv->mmio) {
151                 dev_err(dev, "can't map %pR\n", mem);
152                 return -ENOMEM;
153         }
154
155         hpriv->clk = clk_get(dev, NULL);
156         if (IS_ERR(hpriv->clk)) {
157                 dev_err(dev, "can't get clock\n");
158         } else {
159                 rc = clk_prepare_enable(hpriv->clk);
160                 if (rc) {
161                         dev_err(dev, "clock prepare enable failed");
162                         goto free_clk;
163                 }
164         }
165
166         /*
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.
171          */
172         if (pdata && pdata->init) {
173                 rc = pdata->init(dev, hpriv->mmio);
174                 if (rc)
175                         goto disable_unprepare_clk;
176         }
177
178         ahci_save_initial_config(dev, hpriv,
179                 pdata ? pdata->force_port_map : 0,
180                 pdata ? pdata->mask_port_map  : 0);
181
182         /* prepare host */
183         if (hpriv->cap & HOST_CAP_NCQ)
184                 pi.flags |= ATA_FLAG_NCQ;
185
186         if (hpriv->cap & HOST_CAP_PMP)
187                 pi.flags |= ATA_FLAG_PMP;
188
189         ahci_set_em_messages(hpriv, &pi);
190
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.
195          */
196         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
197
198         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
199         if (!host) {
200                 rc = -ENOMEM;
201                 goto pdata_exit;
202         }
203
204         host->private_data = hpriv;
205
206         if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
207                 host->flags |= ATA_HOST_PARALLEL_SCAN;
208         else
209                 dev_info(dev, "SSS flag set, parallel bus scan disabled\n");
210
211         if (pi.flags & ATA_FLAG_EM)
212                 ahci_reset_em(host);
213
214         for (i = 0; i < host->n_ports; i++) {
215                 struct ata_port *ap = host->ports[i];
216
217                 ata_port_desc(ap, "mmio %pR", mem);
218                 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
219
220                 /* set enclosure management message type */
221                 if (ap->flags & ATA_FLAG_EM)
222                         ap->em_message_type = hpriv->em_msg_type;
223
224                 /* disabled/not-implemented port */
225                 if (!(hpriv->port_map & (1 << i)))
226                         ap->ops = &ata_dummy_port_ops;
227         }
228
229         rc = ahci_reset_controller(host);
230         if (rc)
231                 goto pdata_exit;
232
233         ahci_init_controller(host);
234         ahci_print_info(host, "platform");
235
236         rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
237                                &ahci_platform_sht);
238         if (rc)
239                 goto pdata_exit;
240
241         return 0;
242 pdata_exit:
243         if (pdata && pdata->exit)
244                 pdata->exit(dev);
245 disable_unprepare_clk:
246         if (!IS_ERR(hpriv->clk))
247                 clk_disable_unprepare(hpriv->clk);
248 free_clk:
249         if (!IS_ERR(hpriv->clk))
250                 clk_put(hpriv->clk);
251         return rc;
252 }
253
254 static void ahci_host_stop(struct ata_host *host)
255 {
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;
259
260         if (pdata && pdata->exit)
261                 pdata->exit(dev);
262
263         if (!IS_ERR(hpriv->clk)) {
264                 clk_disable_unprepare(hpriv->clk);
265                 clk_put(hpriv->clk);
266         }
267 }
268
269 #ifdef CONFIG_PM_SLEEP
270 static int ahci_suspend(struct device *dev)
271 {
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;
276         u32 ctl;
277         int rc;
278
279         if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
280                 dev_err(dev, "firmware update required for suspend/resume\n");
281                 return -EIO;
282         }
283
284         /*
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.
288          */
289         ctl = readl(mmio + HOST_CTL);
290         ctl &= ~HOST_IRQ_EN;
291         writel(ctl, mmio + HOST_CTL);
292         readl(mmio + HOST_CTL); /* flush */
293
294         rc = ata_host_suspend(host, PMSG_SUSPEND);
295         if (rc)
296                 return rc;
297
298         if (pdata && pdata->suspend)
299                 return pdata->suspend(dev);
300
301         if (!IS_ERR(hpriv->clk))
302                 clk_disable_unprepare(hpriv->clk);
303
304         return 0;
305 }
306
307 static int ahci_resume(struct device *dev)
308 {
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;
312         int rc;
313
314         if (!IS_ERR(hpriv->clk)) {
315                 rc = clk_prepare_enable(hpriv->clk);
316                 if (rc) {
317                         dev_err(dev, "clock prepare enable failed");
318                         return rc;
319                 }
320         }
321
322         if (pdata && pdata->resume) {
323                 rc = pdata->resume(dev);
324                 if (rc)
325                         goto disable_unprepare_clk;
326         }
327
328         if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
329                 rc = ahci_reset_controller(host);
330                 if (rc)
331                         goto disable_unprepare_clk;
332
333                 ahci_init_controller(host);
334         }
335
336         ata_host_resume(host);
337
338         return 0;
339
340 disable_unprepare_clk:
341         if (!IS_ERR(hpriv->clk))
342                 clk_disable_unprepare(hpriv->clk);
343
344         return rc;
345 }
346 #endif
347
348 static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);
349
350 static struct platform_driver ahci_driver = {
351         .probe = ahci_probe,
352         .remove = ata_platform_remove_one,
353         .driver = {
354                 .name = "ahci",
355                 .owner = THIS_MODULE,
356                 .of_match_table = ahci_of_match,
357                 .pm = &ahci_pm_ops,
358         },
359         .id_table       = ahci_devtype,
360 };
361 module_platform_driver(ahci_driver);
362
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");