]>
Commit | Line | Data |
---|---|---|
1c2a49f6 AV |
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 | ||
f1e70c2c | 15 | #include <linux/clk.h> |
1c2a49f6 | 16 | #include <linux/kernel.h> |
fbaf666b | 17 | #include <linux/gfp.h> |
1c2a49f6 | 18 | #include <linux/module.h> |
18c25ff4 | 19 | #include <linux/pm.h> |
1c2a49f6 AV |
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 "ahci.h" | |
27 | ||
1896b15e BN |
28 | static void ahci_host_stop(struct ata_host *host); |
29 | ||
904c04fe RZ |
30 | enum ahci_type { |
31 | AHCI, /* standard platform ahci */ | |
32 | IMX53_AHCI, /* ahci on i.mx53 */ | |
d408e2b1 | 33 | STRICT_AHCI, /* delayed DMA engine start */ |
904c04fe RZ |
34 | }; |
35 | ||
36 | static struct platform_device_id ahci_devtype[] = { | |
37 | { | |
38 | .name = "ahci", | |
39 | .driver_data = AHCI, | |
40 | }, { | |
41 | .name = "imx53-ahci", | |
42 | .driver_data = IMX53_AHCI, | |
d408e2b1 BN |
43 | }, { |
44 | .name = "strict-ahci", | |
45 | .driver_data = STRICT_AHCI, | |
904c04fe RZ |
46 | }, { |
47 | /* sentinel */ | |
48 | } | |
49 | }; | |
50 | MODULE_DEVICE_TABLE(platform, ahci_devtype); | |
51 | ||
1896b15e BN |
52 | struct ata_port_operations ahci_platform_ops = { |
53 | .inherits = &ahci_ops, | |
54 | .host_stop = ahci_host_stop, | |
55 | }; | |
56 | ||
57 | struct ata_port_operations ahci_platform_retry_srst_ops = { | |
58 | .inherits = &ahci_pmp_retry_srst_ops, | |
59 | .host_stop = ahci_host_stop, | |
60 | }; | |
904c04fe RZ |
61 | |
62 | static const struct ata_port_info ahci_port_info[] = { | |
63 | /* by features */ | |
64 | [AHCI] = { | |
65 | .flags = AHCI_FLAG_COMMON, | |
66 | .pio_mask = ATA_PIO4, | |
67 | .udma_mask = ATA_UDMA6, | |
1896b15e | 68 | .port_ops = &ahci_platform_ops, |
904c04fe RZ |
69 | }, |
70 | [IMX53_AHCI] = { | |
71 | .flags = AHCI_FLAG_COMMON, | |
72 | .pio_mask = ATA_PIO4, | |
73 | .udma_mask = ATA_UDMA6, | |
1896b15e | 74 | .port_ops = &ahci_platform_retry_srst_ops, |
904c04fe | 75 | }, |
d408e2b1 BN |
76 | [STRICT_AHCI] = { |
77 | AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE), | |
78 | .flags = AHCI_FLAG_COMMON, | |
79 | .pio_mask = ATA_PIO4, | |
80 | .udma_mask = ATA_UDMA6, | |
1896b15e | 81 | .port_ops = &ahci_platform_ops, |
d408e2b1 | 82 | }, |
904c04fe RZ |
83 | }; |
84 | ||
fad16e7a TH |
85 | static struct scsi_host_template ahci_platform_sht = { |
86 | AHCI_SHT("ahci_platform"), | |
87 | }; | |
88 | ||
941c77fd | 89 | static int __devinit ahci_probe(struct platform_device *pdev) |
1c2a49f6 AV |
90 | { |
91 | struct device *dev = &pdev->dev; | |
00345614 | 92 | struct ahci_platform_data *pdata = dev_get_platdata(dev); |
904c04fe | 93 | const struct platform_device_id *id = platform_get_device_id(pdev); |
ff956135 | 94 | struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0]; |
1c2a49f6 AV |
95 | const struct ata_port_info *ppi[] = { &pi, NULL }; |
96 | struct ahci_host_priv *hpriv; | |
97 | struct ata_host *host; | |
98 | struct resource *mem; | |
99 | int irq; | |
100 | int n_ports; | |
101 | int i; | |
102 | int rc; | |
103 | ||
104 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
105 | if (!mem) { | |
106 | dev_err(dev, "no mmio space\n"); | |
107 | return -EINVAL; | |
108 | } | |
109 | ||
110 | irq = platform_get_irq(pdev, 0); | |
111 | if (irq <= 0) { | |
112 | dev_err(dev, "no irq\n"); | |
113 | return -EINVAL; | |
114 | } | |
115 | ||
1c2a49f6 AV |
116 | if (pdata && pdata->ata_port_info) |
117 | pi = *pdata->ata_port_info; | |
118 | ||
119 | hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL); | |
120 | if (!hpriv) { | |
08354809 JB |
121 | dev_err(dev, "can't alloc ahci_host_priv\n"); |
122 | return -ENOMEM; | |
1c2a49f6 AV |
123 | } |
124 | ||
125 | hpriv->flags |= (unsigned long)pi.private_data; | |
126 | ||
127 | hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem)); | |
128 | if (!hpriv->mmio) { | |
129 | dev_err(dev, "can't map %pR\n", mem); | |
08354809 JB |
130 | return -ENOMEM; |
131 | } | |
132 | ||
f1e70c2c VK |
133 | hpriv->clk = clk_get(dev, NULL); |
134 | if (IS_ERR(hpriv->clk)) { | |
135 | dev_err(dev, "can't get clock\n"); | |
136 | } else { | |
137 | rc = clk_prepare_enable(hpriv->clk); | |
138 | if (rc) { | |
139 | dev_err(dev, "clock prepare enable failed"); | |
140 | goto free_clk; | |
141 | } | |
142 | } | |
143 | ||
08354809 JB |
144 | /* |
145 | * Some platforms might need to prepare for mmio region access, | |
146 | * which could be done in the following init call. So, the mmio | |
147 | * region shouldn't be accessed before init (if provided) has | |
148 | * returned successfully. | |
149 | */ | |
150 | if (pdata && pdata->init) { | |
151 | rc = pdata->init(dev, hpriv->mmio); | |
152 | if (rc) | |
f1e70c2c | 153 | goto disable_unprepare_clk; |
1c2a49f6 AV |
154 | } |
155 | ||
156 | ahci_save_initial_config(dev, hpriv, | |
157 | pdata ? pdata->force_port_map : 0, | |
158 | pdata ? pdata->mask_port_map : 0); | |
159 | ||
160 | /* prepare host */ | |
161 | if (hpriv->cap & HOST_CAP_NCQ) | |
162 | pi.flags |= ATA_FLAG_NCQ; | |
163 | ||
164 | if (hpriv->cap & HOST_CAP_PMP) | |
165 | pi.flags |= ATA_FLAG_PMP; | |
166 | ||
167 | ahci_set_em_messages(hpriv, &pi); | |
168 | ||
169 | /* CAP.NP sometimes indicate the index of the last enabled | |
170 | * port, at other times, that of the last possible port, so | |
171 | * determining the maximum port number requires looking at | |
172 | * both CAP.NP and port_map. | |
173 | */ | |
174 | n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map)); | |
175 | ||
176 | host = ata_host_alloc_pinfo(dev, ppi, n_ports); | |
177 | if (!host) { | |
178 | rc = -ENOMEM; | |
f1e70c2c | 179 | goto pdata_exit; |
1c2a49f6 AV |
180 | } |
181 | ||
182 | host->private_data = hpriv; | |
183 | ||
184 | if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss) | |
185 | host->flags |= ATA_HOST_PARALLEL_SCAN; | |
186 | else | |
187 | printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n"); | |
188 | ||
189 | if (pi.flags & ATA_FLAG_EM) | |
190 | ahci_reset_em(host); | |
191 | ||
192 | for (i = 0; i < host->n_ports; i++) { | |
193 | struct ata_port *ap = host->ports[i]; | |
194 | ||
195 | ata_port_desc(ap, "mmio %pR", mem); | |
196 | ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80); | |
197 | ||
1c2a49f6 AV |
198 | /* set enclosure management message type */ |
199 | if (ap->flags & ATA_FLAG_EM) | |
55787183 | 200 | ap->em_message_type = hpriv->em_msg_type; |
1c2a49f6 AV |
201 | |
202 | /* disabled/not-implemented port */ | |
203 | if (!(hpriv->port_map & (1 << i))) | |
204 | ap->ops = &ata_dummy_port_ops; | |
205 | } | |
206 | ||
207 | rc = ahci_reset_controller(host); | |
208 | if (rc) | |
f1e70c2c | 209 | goto pdata_exit; |
1c2a49f6 AV |
210 | |
211 | ahci_init_controller(host); | |
212 | ahci_print_info(host, "platform"); | |
213 | ||
214 | rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED, | |
fad16e7a | 215 | &ahci_platform_sht); |
1c2a49f6 | 216 | if (rc) |
f1e70c2c | 217 | goto pdata_exit; |
1c2a49f6 AV |
218 | |
219 | return 0; | |
f1e70c2c | 220 | pdata_exit: |
1c2a49f6 AV |
221 | if (pdata && pdata->exit) |
222 | pdata->exit(dev); | |
f1e70c2c VK |
223 | disable_unprepare_clk: |
224 | if (!IS_ERR(hpriv->clk)) | |
225 | clk_disable_unprepare(hpriv->clk); | |
226 | free_clk: | |
227 | if (!IS_ERR(hpriv->clk)) | |
228 | clk_put(hpriv->clk); | |
1c2a49f6 AV |
229 | return rc; |
230 | } | |
231 | ||
232 | static int __devexit ahci_remove(struct platform_device *pdev) | |
233 | { | |
234 | struct device *dev = &pdev->dev; | |
1c2a49f6 AV |
235 | struct ata_host *host = dev_get_drvdata(dev); |
236 | ||
237 | ata_host_detach(host); | |
238 | ||
1896b15e BN |
239 | return 0; |
240 | } | |
241 | ||
242 | static void ahci_host_stop(struct ata_host *host) | |
243 | { | |
244 | struct device *dev = host->dev; | |
245 | struct ahci_platform_data *pdata = dev_get_platdata(dev); | |
246 | struct ahci_host_priv *hpriv = host->private_data; | |
247 | ||
1c2a49f6 AV |
248 | if (pdata && pdata->exit) |
249 | pdata->exit(dev); | |
250 | ||
f1e70c2c VK |
251 | if (!IS_ERR(hpriv->clk)) { |
252 | clk_disable_unprepare(hpriv->clk); | |
253 | clk_put(hpriv->clk); | |
254 | } | |
1c2a49f6 AV |
255 | } |
256 | ||
29448ec1 | 257 | #ifdef CONFIG_PM_SLEEP |
17ab594f BN |
258 | static int ahci_suspend(struct device *dev) |
259 | { | |
260 | struct ahci_platform_data *pdata = dev_get_platdata(dev); | |
261 | struct ata_host *host = dev_get_drvdata(dev); | |
262 | struct ahci_host_priv *hpriv = host->private_data; | |
263 | void __iomem *mmio = hpriv->mmio; | |
264 | u32 ctl; | |
265 | int rc; | |
266 | ||
267 | if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) { | |
268 | dev_err(dev, "firmware update required for suspend/resume\n"); | |
269 | return -EIO; | |
270 | } | |
271 | ||
272 | /* | |
273 | * AHCI spec rev1.1 section 8.3.3: | |
274 | * Software must disable interrupts prior to requesting a | |
275 | * transition of the HBA to D3 state. | |
276 | */ | |
277 | ctl = readl(mmio + HOST_CTL); | |
278 | ctl &= ~HOST_IRQ_EN; | |
279 | writel(ctl, mmio + HOST_CTL); | |
280 | readl(mmio + HOST_CTL); /* flush */ | |
281 | ||
282 | rc = ata_host_suspend(host, PMSG_SUSPEND); | |
283 | if (rc) | |
284 | return rc; | |
285 | ||
286 | if (pdata && pdata->suspend) | |
287 | return pdata->suspend(dev); | |
f1e70c2c VK |
288 | |
289 | if (!IS_ERR(hpriv->clk)) | |
290 | clk_disable_unprepare(hpriv->clk); | |
291 | ||
17ab594f BN |
292 | return 0; |
293 | } | |
294 | ||
295 | static int ahci_resume(struct device *dev) | |
296 | { | |
297 | struct ahci_platform_data *pdata = dev_get_platdata(dev); | |
298 | struct ata_host *host = dev_get_drvdata(dev); | |
f1e70c2c | 299 | struct ahci_host_priv *hpriv = host->private_data; |
17ab594f BN |
300 | int rc; |
301 | ||
f1e70c2c VK |
302 | if (!IS_ERR(hpriv->clk)) { |
303 | rc = clk_prepare_enable(hpriv->clk); | |
304 | if (rc) { | |
305 | dev_err(dev, "clock prepare enable failed"); | |
306 | return rc; | |
307 | } | |
308 | } | |
309 | ||
17ab594f BN |
310 | if (pdata && pdata->resume) { |
311 | rc = pdata->resume(dev); | |
312 | if (rc) | |
f1e70c2c | 313 | goto disable_unprepare_clk; |
17ab594f BN |
314 | } |
315 | ||
316 | if (dev->power.power_state.event == PM_EVENT_SUSPEND) { | |
317 | rc = ahci_reset_controller(host); | |
318 | if (rc) | |
f1e70c2c | 319 | goto disable_unprepare_clk; |
17ab594f BN |
320 | |
321 | ahci_init_controller(host); | |
322 | } | |
323 | ||
324 | ata_host_resume(host); | |
325 | ||
326 | return 0; | |
f1e70c2c VK |
327 | |
328 | disable_unprepare_clk: | |
329 | if (!IS_ERR(hpriv->clk)) | |
330 | clk_disable_unprepare(hpriv->clk); | |
331 | ||
332 | return rc; | |
17ab594f | 333 | } |
17ab594f BN |
334 | #endif |
335 | ||
18c25ff4 SH |
336 | SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume); |
337 | ||
02aac316 | 338 | static const struct of_device_id ahci_of_match[] = { |
5f098a3e | 339 | { .compatible = "snps,spear-ahci", }, |
02aac316 RH |
340 | {}, |
341 | }; | |
342 | MODULE_DEVICE_TABLE(of, ahci_of_match); | |
343 | ||
1c2a49f6 | 344 | static struct platform_driver ahci_driver = { |
941c77fd | 345 | .probe = ahci_probe, |
1c2a49f6 AV |
346 | .remove = __devexit_p(ahci_remove), |
347 | .driver = { | |
348 | .name = "ahci", | |
349 | .owner = THIS_MODULE, | |
02aac316 | 350 | .of_match_table = ahci_of_match, |
17ab594f | 351 | .pm = &ahci_pm_ops, |
1c2a49f6 | 352 | }, |
904c04fe | 353 | .id_table = ahci_devtype, |
1c2a49f6 | 354 | }; |
9a99e476 | 355 | module_platform_driver(ahci_driver); |
1c2a49f6 AV |
356 | |
357 | MODULE_DESCRIPTION("AHCI SATA platform driver"); | |
358 | MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>"); | |
359 | MODULE_LICENSE("GPL"); | |
360 | MODULE_ALIAS("platform:ahci"); |