Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 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> |
Tejun Heo | fbaf666 | 2010-03-30 02:52:43 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 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> |
Sujit Reddy Thumma | db4b5d0 | 2013-01-03 22:28:39 +0530 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 25 | #include "ahci.h" |
| 26 | |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 27 | enum ahci_type { |
| 28 | AHCI, /* standard platform ahci */ |
| 29 | IMX53_AHCI, /* ahci on i.mx53 */ |
Brian Norris | d408e2b | 2012-02-21 10:38:44 -0800 | [diff] [blame] | 30 | STRICT_AHCI, /* delayed DMA engine start */ |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static struct platform_device_id ahci_devtype[] = { |
| 34 | { |
| 35 | .name = "ahci", |
| 36 | .driver_data = AHCI, |
| 37 | }, { |
| 38 | .name = "imx53-ahci", |
| 39 | .driver_data = IMX53_AHCI, |
| 40 | }, { |
Brian Norris | d408e2b | 2012-02-21 10:38:44 -0800 | [diff] [blame] | 41 | .name = "strict-ahci", |
| 42 | .driver_data = STRICT_AHCI, |
| 43 | }, { |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 44 | /* sentinel */ |
| 45 | } |
| 46 | }; |
| 47 | MODULE_DEVICE_TABLE(platform, ahci_devtype); |
| 48 | |
| 49 | |
| 50 | static const struct ata_port_info ahci_port_info[] = { |
| 51 | /* by features */ |
| 52 | [AHCI] = { |
| 53 | .flags = AHCI_FLAG_COMMON, |
| 54 | .pio_mask = ATA_PIO4, |
| 55 | .udma_mask = ATA_UDMA6, |
| 56 | .port_ops = &ahci_ops, |
| 57 | }, |
| 58 | [IMX53_AHCI] = { |
| 59 | .flags = AHCI_FLAG_COMMON, |
| 60 | .pio_mask = ATA_PIO4, |
| 61 | .udma_mask = ATA_UDMA6, |
| 62 | .port_ops = &ahci_pmp_retry_srst_ops, |
| 63 | }, |
Brian Norris | d408e2b | 2012-02-21 10:38:44 -0800 | [diff] [blame] | 64 | [STRICT_AHCI] = { |
| 65 | AHCI_HFLAGS (AHCI_HFLAG_DELAY_ENGINE), |
| 66 | .flags = AHCI_FLAG_COMMON, |
| 67 | .pio_mask = ATA_PIO4, |
| 68 | .udma_mask = ATA_UDMA6, |
| 69 | .port_ops = &ahci_ops, |
| 70 | }, |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 71 | }; |
| 72 | |
Tejun Heo | fad16e7 | 2010-09-21 09:25:48 +0200 | [diff] [blame] | 73 | static struct scsi_host_template ahci_platform_sht = { |
| 74 | AHCI_SHT("ahci_platform"), |
| 75 | }; |
| 76 | |
Sujit Reddy Thumma | 021bf99 | 2013-01-03 14:18:13 +0530 | [diff] [blame] | 77 | static int __devinit ahci_probe(struct platform_device *pdev) |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 78 | { |
| 79 | struct device *dev = &pdev->dev; |
JiSheng Zhang | 0034561 | 2011-10-31 21:20:10 +0800 | [diff] [blame] | 80 | struct ahci_platform_data *pdata = dev_get_platdata(dev); |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 81 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Rob Herring | ff95613 | 2011-11-15 21:00:56 -0600 | [diff] [blame] | 82 | struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0]; |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 83 | const struct ata_port_info *ppi[] = { &pi, NULL }; |
| 84 | struct ahci_host_priv *hpriv; |
| 85 | struct ata_host *host; |
| 86 | struct resource *mem; |
| 87 | int irq; |
| 88 | int n_ports; |
| 89 | int i; |
| 90 | int rc; |
| 91 | |
| 92 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 93 | if (!mem) { |
| 94 | dev_err(dev, "no mmio space\n"); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | irq = platform_get_irq(pdev, 0); |
| 99 | if (irq <= 0) { |
| 100 | dev_err(dev, "no irq\n"); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 104 | if (pdata && pdata->ata_port_info) |
| 105 | pi = *pdata->ata_port_info; |
| 106 | |
| 107 | hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL); |
| 108 | if (!hpriv) { |
Jassi Brar | 0835480 | 2010-06-25 18:21:19 +0900 | [diff] [blame] | 109 | dev_err(dev, "can't alloc ahci_host_priv\n"); |
| 110 | return -ENOMEM; |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | hpriv->flags |= (unsigned long)pi.private_data; |
| 114 | |
| 115 | hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem)); |
| 116 | if (!hpriv->mmio) { |
| 117 | dev_err(dev, "can't map %pR\n", mem); |
Jassi Brar | 0835480 | 2010-06-25 18:21:19 +0900 | [diff] [blame] | 118 | return -ENOMEM; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Some platforms might need to prepare for mmio region access, |
| 123 | * which could be done in the following init call. So, the mmio |
| 124 | * region shouldn't be accessed before init (if provided) has |
| 125 | * returned successfully. |
| 126 | */ |
| 127 | if (pdata && pdata->init) { |
| 128 | rc = pdata->init(dev, hpriv->mmio); |
| 129 | if (rc) |
| 130 | return rc; |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ahci_save_initial_config(dev, hpriv, |
| 134 | pdata ? pdata->force_port_map : 0, |
| 135 | pdata ? pdata->mask_port_map : 0); |
| 136 | |
| 137 | /* prepare host */ |
| 138 | if (hpriv->cap & HOST_CAP_NCQ) |
| 139 | pi.flags |= ATA_FLAG_NCQ; |
| 140 | |
| 141 | if (hpriv->cap & HOST_CAP_PMP) |
| 142 | pi.flags |= ATA_FLAG_PMP; |
| 143 | |
| 144 | ahci_set_em_messages(hpriv, &pi); |
| 145 | |
| 146 | /* CAP.NP sometimes indicate the index of the last enabled |
| 147 | * port, at other times, that of the last possible port, so |
| 148 | * determining the maximum port number requires looking at |
| 149 | * both CAP.NP and port_map. |
| 150 | */ |
| 151 | n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map)); |
| 152 | |
| 153 | host = ata_host_alloc_pinfo(dev, ppi, n_ports); |
| 154 | if (!host) { |
| 155 | rc = -ENOMEM; |
| 156 | goto err0; |
| 157 | } |
| 158 | |
| 159 | host->private_data = hpriv; |
| 160 | |
| 161 | if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss) |
| 162 | host->flags |= ATA_HOST_PARALLEL_SCAN; |
| 163 | else |
| 164 | printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n"); |
| 165 | |
| 166 | if (pi.flags & ATA_FLAG_EM) |
| 167 | ahci_reset_em(host); |
| 168 | |
| 169 | for (i = 0; i < host->n_ports; i++) { |
| 170 | struct ata_port *ap = host->ports[i]; |
| 171 | |
| 172 | ata_port_desc(ap, "mmio %pR", mem); |
| 173 | ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80); |
| 174 | |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 175 | /* set enclosure management message type */ |
| 176 | if (ap->flags & ATA_FLAG_EM) |
Jeff Garzik | 5578718 | 2010-05-14 17:23:37 -0400 | [diff] [blame] | 177 | ap->em_message_type = hpriv->em_msg_type; |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 178 | |
| 179 | /* disabled/not-implemented port */ |
| 180 | if (!(hpriv->port_map & (1 << i))) |
| 181 | ap->ops = &ata_dummy_port_ops; |
| 182 | } |
| 183 | |
| 184 | rc = ahci_reset_controller(host); |
| 185 | if (rc) |
| 186 | goto err0; |
| 187 | |
| 188 | ahci_init_controller(host); |
| 189 | ahci_print_info(host, "platform"); |
| 190 | |
| 191 | rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED, |
Tejun Heo | fad16e7 | 2010-09-21 09:25:48 +0200 | [diff] [blame] | 192 | &ahci_platform_sht); |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 193 | if (rc) |
| 194 | goto err0; |
| 195 | |
Sujit Reddy Thumma | db4b5d0 | 2013-01-03 22:28:39 +0530 | [diff] [blame] | 196 | rc = pm_runtime_set_active(dev); |
| 197 | if (rc) { |
| 198 | dev_warn(dev, "Unable to set runtime pm active err=%d\n", rc); |
| 199 | } else { |
| 200 | pm_runtime_enable(dev); |
| 201 | pm_runtime_forbid(dev); |
| 202 | } |
| 203 | |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 204 | return 0; |
| 205 | err0: |
| 206 | if (pdata && pdata->exit) |
| 207 | pdata->exit(dev); |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | static int __devexit ahci_remove(struct platform_device *pdev) |
| 212 | { |
| 213 | struct device *dev = &pdev->dev; |
JiSheng Zhang | 0034561 | 2011-10-31 21:20:10 +0800 | [diff] [blame] | 214 | struct ahci_platform_data *pdata = dev_get_platdata(dev); |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 215 | struct ata_host *host = dev_get_drvdata(dev); |
| 216 | |
| 217 | ata_host_detach(host); |
| 218 | |
| 219 | if (pdata && pdata->exit) |
| 220 | pdata->exit(dev); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 225 | #ifdef CONFIG_PM |
| 226 | static int ahci_suspend(struct device *dev) |
| 227 | { |
| 228 | struct ahci_platform_data *pdata = dev_get_platdata(dev); |
| 229 | struct ata_host *host = dev_get_drvdata(dev); |
| 230 | struct ahci_host_priv *hpriv = host->private_data; |
| 231 | void __iomem *mmio = hpriv->mmio; |
| 232 | u32 ctl; |
| 233 | int rc; |
| 234 | |
Sujit Reddy Thumma | 049edda | 2013-01-18 20:34:43 +0530 | [diff] [blame] | 235 | if (pm_runtime_suspended(dev)) |
| 236 | return 0; |
| 237 | |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 238 | if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) { |
| 239 | dev_err(dev, "firmware update required for suspend/resume\n"); |
| 240 | return -EIO; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * AHCI spec rev1.1 section 8.3.3: |
| 245 | * Software must disable interrupts prior to requesting a |
| 246 | * transition of the HBA to D3 state. |
| 247 | */ |
| 248 | ctl = readl(mmio + HOST_CTL); |
| 249 | ctl &= ~HOST_IRQ_EN; |
| 250 | writel(ctl, mmio + HOST_CTL); |
| 251 | readl(mmio + HOST_CTL); /* flush */ |
| 252 | |
| 253 | rc = ata_host_suspend(host, PMSG_SUSPEND); |
| 254 | if (rc) |
| 255 | return rc; |
| 256 | |
| 257 | if (pdata && pdata->suspend) |
| 258 | return pdata->suspend(dev); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Sujit Reddy Thumma | 049edda | 2013-01-18 20:34:43 +0530 | [diff] [blame] | 262 | static int ahci_resume_common(struct device *dev) |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 263 | { |
| 264 | struct ahci_platform_data *pdata = dev_get_platdata(dev); |
| 265 | struct ata_host *host = dev_get_drvdata(dev); |
| 266 | int rc; |
| 267 | |
| 268 | if (pdata && pdata->resume) { |
| 269 | rc = pdata->resume(dev); |
| 270 | if (rc) |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | if (dev->power.power_state.event == PM_EVENT_SUSPEND) { |
| 275 | rc = ahci_reset_controller(host); |
| 276 | if (rc) |
| 277 | return rc; |
| 278 | |
| 279 | ahci_init_controller(host); |
| 280 | } |
| 281 | |
| 282 | ata_host_resume(host); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Sujit Reddy Thumma | 049edda | 2013-01-18 20:34:43 +0530 | [diff] [blame] | 287 | static int ahci_resume(struct device *dev) |
| 288 | { |
| 289 | int rc; |
| 290 | |
| 291 | rc = ahci_resume_common(dev); |
| 292 | if (!rc) { |
| 293 | pm_runtime_disable(dev); |
| 294 | pm_runtime_set_active(dev); |
| 295 | pm_runtime_enable(dev); |
| 296 | } |
| 297 | |
| 298 | return rc; |
| 299 | } |
| 300 | |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 301 | static struct dev_pm_ops ahci_pm_ops = { |
| 302 | .suspend = &ahci_suspend, |
| 303 | .resume = &ahci_resume, |
Sujit Reddy Thumma | db4b5d0 | 2013-01-03 22:28:39 +0530 | [diff] [blame] | 304 | .runtime_suspend = &ahci_suspend, |
Sujit Reddy Thumma | 049edda | 2013-01-18 20:34:43 +0530 | [diff] [blame] | 305 | .runtime_resume = &ahci_resume_common, |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 306 | }; |
| 307 | #endif |
| 308 | |
Rob Herring | 02aac31 | 2010-11-03 21:04:59 -0500 | [diff] [blame] | 309 | static const struct of_device_id ahci_of_match[] = { |
| 310 | { .compatible = "calxeda,hb-ahci", }, |
Viresh Kumar | 5f098a3 | 2012-04-21 17:40:12 +0530 | [diff] [blame] | 311 | { .compatible = "snps,spear-ahci", }, |
Sujit Reddy Thumma | 3510638 | 2012-10-12 20:36:53 +0530 | [diff] [blame] | 312 | { .compatible = "qcom,msm-ahci", }, |
Rob Herring | 02aac31 | 2010-11-03 21:04:59 -0500 | [diff] [blame] | 313 | {}, |
| 314 | }; |
| 315 | MODULE_DEVICE_TABLE(of, ahci_of_match); |
| 316 | |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 317 | static struct platform_driver ahci_driver = { |
Sujit Reddy Thumma | 021bf99 | 2013-01-03 14:18:13 +0530 | [diff] [blame] | 318 | .probe = ahci_probe, |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 319 | .remove = __devexit_p(ahci_remove), |
| 320 | .driver = { |
| 321 | .name = "ahci", |
| 322 | .owner = THIS_MODULE, |
Rob Herring | 02aac31 | 2010-11-03 21:04:59 -0500 | [diff] [blame] | 323 | .of_match_table = ahci_of_match, |
Brian Norris | 17ab594 | 2011-11-18 11:10:10 -0800 | [diff] [blame] | 324 | #ifdef CONFIG_PM |
| 325 | .pm = &ahci_pm_ops, |
| 326 | #endif |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 327 | }, |
Richard Zhu | 904c04f | 2011-09-28 15:41:54 +0800 | [diff] [blame] | 328 | .id_table = ahci_devtype, |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | static int __init ahci_init(void) |
| 332 | { |
Sujit Reddy Thumma | 021bf99 | 2013-01-03 14:18:13 +0530 | [diff] [blame] | 333 | return platform_driver_register(&ahci_driver); |
Anton Vorontsov | 1c2a49f | 2010-03-04 20:06:06 +0300 | [diff] [blame] | 334 | } |
| 335 | module_init(ahci_init); |
| 336 | |
| 337 | static void __exit ahci_exit(void) |
| 338 | { |
| 339 | platform_driver_unregister(&ahci_driver); |
| 340 | } |
| 341 | module_exit(ahci_exit); |
| 342 | |
| 343 | MODULE_DESCRIPTION("AHCI SATA platform driver"); |
| 344 | MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>"); |
| 345 | MODULE_LICENSE("GPL"); |
| 346 | MODULE_ALIAS("platform:ahci"); |