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