ahci: Add platform driver
[safe/jmp/linux-2.6] / 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/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/libata.h>
22 #include <linux/ahci_platform.h>
23 #include "ahci.h"
24
25 static int __init ahci_probe(struct platform_device *pdev)
26 {
27         struct device *dev = &pdev->dev;
28         struct ahci_platform_data *pdata = dev->platform_data;
29         struct ata_port_info pi = {
30                 .flags          = AHCI_FLAG_COMMON,
31                 .pio_mask       = ATA_PIO4,
32                 .udma_mask      = ATA_UDMA6,
33                 .port_ops       = &ahci_ops,
34         };
35         const struct ata_port_info *ppi[] = { &pi, NULL };
36         struct ahci_host_priv *hpriv;
37         struct ata_host *host;
38         struct resource *mem;
39         int irq;
40         int n_ports;
41         int i;
42         int rc;
43
44         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
45         if (!mem) {
46                 dev_err(dev, "no mmio space\n");
47                 return -EINVAL;
48         }
49
50         irq = platform_get_irq(pdev, 0);
51         if (irq <= 0) {
52                 dev_err(dev, "no irq\n");
53                 return -EINVAL;
54         }
55
56         if (pdata && pdata->init) {
57                 rc = pdata->init(dev);
58                 if (rc)
59                         return rc;
60         }
61
62         if (pdata && pdata->ata_port_info)
63                 pi = *pdata->ata_port_info;
64
65         hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
66         if (!hpriv) {
67                 rc = -ENOMEM;
68                 goto err0;
69         }
70
71         hpriv->flags |= (unsigned long)pi.private_data;
72
73         hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
74         if (!hpriv->mmio) {
75                 dev_err(dev, "can't map %pR\n", mem);
76                 rc = -ENOMEM;
77                 goto err0;
78         }
79
80         ahci_save_initial_config(dev, hpriv,
81                 pdata ? pdata->force_port_map : 0,
82                 pdata ? pdata->mask_port_map  : 0);
83
84         /* prepare host */
85         if (hpriv->cap & HOST_CAP_NCQ)
86                 pi.flags |= ATA_FLAG_NCQ;
87
88         if (hpriv->cap & HOST_CAP_PMP)
89                 pi.flags |= ATA_FLAG_PMP;
90
91         ahci_set_em_messages(hpriv, &pi);
92
93         /* CAP.NP sometimes indicate the index of the last enabled
94          * port, at other times, that of the last possible port, so
95          * determining the maximum port number requires looking at
96          * both CAP.NP and port_map.
97          */
98         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
99
100         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
101         if (!host) {
102                 rc = -ENOMEM;
103                 goto err0;
104         }
105
106         host->private_data = hpriv;
107
108         if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
109                 host->flags |= ATA_HOST_PARALLEL_SCAN;
110         else
111                 printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
112
113         if (pi.flags & ATA_FLAG_EM)
114                 ahci_reset_em(host);
115
116         for (i = 0; i < host->n_ports; i++) {
117                 struct ata_port *ap = host->ports[i];
118
119                 ata_port_desc(ap, "mmio %pR", mem);
120                 ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);
121
122                 /* set initial link pm policy */
123                 ap->pm_policy = NOT_AVAILABLE;
124
125                 /* set enclosure management message type */
126                 if (ap->flags & ATA_FLAG_EM)
127                         ap->em_message_type = ahci_em_messages;
128
129                 /* disabled/not-implemented port */
130                 if (!(hpriv->port_map & (1 << i)))
131                         ap->ops = &ata_dummy_port_ops;
132         }
133
134         rc = ahci_reset_controller(host);
135         if (rc)
136                 goto err0;
137
138         ahci_init_controller(host);
139         ahci_print_info(host, "platform");
140
141         rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
142                                &ahci_sht);
143         if (rc)
144                 goto err0;
145
146         return 0;
147 err0:
148         if (pdata && pdata->exit)
149                 pdata->exit(dev);
150         return rc;
151 }
152
153 static int __devexit ahci_remove(struct platform_device *pdev)
154 {
155         struct device *dev = &pdev->dev;
156         struct ahci_platform_data *pdata = dev->platform_data;
157         struct ata_host *host = dev_get_drvdata(dev);
158
159         ata_host_detach(host);
160
161         if (pdata && pdata->exit)
162                 pdata->exit(dev);
163
164         return 0;
165 }
166
167 static struct platform_driver ahci_driver = {
168         .probe = ahci_probe,
169         .remove = __devexit_p(ahci_remove),
170         .driver = {
171                 .name = "ahci",
172                 .owner = THIS_MODULE,
173         },
174 };
175
176 static int __init ahci_init(void)
177 {
178         return platform_driver_probe(&ahci_driver, ahci_probe);
179 }
180 module_init(ahci_init);
181
182 static void __exit ahci_exit(void)
183 {
184         platform_driver_unregister(&ahci_driver);
185 }
186 module_exit(ahci_exit);
187
188 MODULE_DESCRIPTION("AHCI SATA platform driver");
189 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
190 MODULE_LICENSE("GPL");
191 MODULE_ALIAS("platform:ahci");