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