ahci_platform.c 8.0 KB
Newer Older
A
Anton Vorontsov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * AHCI SATA platform driver
 *
 * Copyright 2004-2005  Red Hat, Inc.
 *   Jeff Garzik <jgarzik@pobox.com>
 * Copyright 2010  MontaVista Software, LLC.
 *   Anton Vorontsov <avorontsov@ru.mvista.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 */

15
#include <linux/clk.h>
A
Anton Vorontsov 已提交
16
#include <linux/kernel.h>
T
Tejun Heo 已提交
17
#include <linux/gfp.h>
A
Anton Vorontsov 已提交
18
#include <linux/module.h>
19
#include <linux/pm.h>
A
Anton Vorontsov 已提交
20 21 22 23 24 25 26 27
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/libata.h>
#include <linux/ahci_platform.h>
#include "ahci.h"

28 29 30
enum ahci_type {
	AHCI,		/* standard platform ahci */
	IMX53_AHCI,	/* ahci on i.mx53 */
31
	STRICT_AHCI,	/* delayed DMA engine start */
32 33 34 35 36 37 38 39 40
};

static struct platform_device_id ahci_devtype[] = {
	{
		.name = "ahci",
		.driver_data = AHCI,
	}, {
		.name = "imx53-ahci",
		.driver_data = IMX53_AHCI,
41 42 43
	}, {
		.name = "strict-ahci",
		.driver_data = STRICT_AHCI,
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(platform, ahci_devtype);


static const struct ata_port_info ahci_port_info[] = {
	/* by features */
	[AHCI] = {
		.flags		= AHCI_FLAG_COMMON,
		.pio_mask	= ATA_PIO4,
		.udma_mask	= ATA_UDMA6,
		.port_ops	= &ahci_ops,
	},
	[IMX53_AHCI] = {
		.flags		= AHCI_FLAG_COMMON,
		.pio_mask	= ATA_PIO4,
		.udma_mask	= ATA_UDMA6,
		.port_ops	= &ahci_pmp_retry_srst_ops,
	},
65 66 67 68 69 70 71
	[STRICT_AHCI] = {
		AHCI_HFLAGS	(AHCI_HFLAG_DELAY_ENGINE),
		.flags		= AHCI_FLAG_COMMON,
		.pio_mask	= ATA_PIO4,
		.udma_mask	= ATA_UDMA6,
		.port_ops	= &ahci_ops,
	},
72 73
};

74 75 76 77
static struct scsi_host_template ahci_platform_sht = {
	AHCI_SHT("ahci_platform"),
};

A
Anton Vorontsov 已提交
78 79 80
static int __init ahci_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
81
	struct ahci_platform_data *pdata = dev_get_platdata(dev);
82
	const struct platform_device_id *id = platform_get_device_id(pdev);
83
	struct ata_port_info pi = ahci_port_info[id ? id->driver_data : 0];
A
Anton Vorontsov 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	const struct ata_port_info *ppi[] = { &pi, NULL };
	struct ahci_host_priv *hpriv;
	struct ata_host *host;
	struct resource *mem;
	int irq;
	int n_ports;
	int i;
	int rc;

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		dev_err(dev, "no mmio space\n");
		return -EINVAL;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		dev_err(dev, "no irq\n");
		return -EINVAL;
	}

	if (pdata && pdata->ata_port_info)
		pi = *pdata->ata_port_info;

	hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
	if (!hpriv) {
110 111
		dev_err(dev, "can't alloc ahci_host_priv\n");
		return -ENOMEM;
A
Anton Vorontsov 已提交
112 113 114 115 116 117 118
	}

	hpriv->flags |= (unsigned long)pi.private_data;

	hpriv->mmio = devm_ioremap(dev, mem->start, resource_size(mem));
	if (!hpriv->mmio) {
		dev_err(dev, "can't map %pR\n", mem);
119 120 121
		return -ENOMEM;
	}

122 123 124 125 126 127 128 129 130 131 132
	hpriv->clk = clk_get(dev, NULL);
	if (IS_ERR(hpriv->clk)) {
		dev_err(dev, "can't get clock\n");
	} else {
		rc = clk_prepare_enable(hpriv->clk);
		if (rc) {
			dev_err(dev, "clock prepare enable failed");
			goto free_clk;
		}
	}

133 134 135 136 137 138 139 140 141
	/*
	 * Some platforms might need to prepare for mmio region access,
	 * which could be done in the following init call. So, the mmio
	 * region shouldn't be accessed before init (if provided) has
	 * returned successfully.
	 */
	if (pdata && pdata->init) {
		rc = pdata->init(dev, hpriv->mmio);
		if (rc)
142
			goto disable_unprepare_clk;
A
Anton Vorontsov 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

	ahci_save_initial_config(dev, hpriv,
		pdata ? pdata->force_port_map : 0,
		pdata ? pdata->mask_port_map  : 0);

	/* prepare host */
	if (hpriv->cap & HOST_CAP_NCQ)
		pi.flags |= ATA_FLAG_NCQ;

	if (hpriv->cap & HOST_CAP_PMP)
		pi.flags |= ATA_FLAG_PMP;

	ahci_set_em_messages(hpriv, &pi);

	/* CAP.NP sometimes indicate the index of the last enabled
	 * port, at other times, that of the last possible port, so
	 * determining the maximum port number requires looking at
	 * both CAP.NP and port_map.
	 */
	n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));

	host = ata_host_alloc_pinfo(dev, ppi, n_ports);
	if (!host) {
		rc = -ENOMEM;
168
		goto pdata_exit;
A
Anton Vorontsov 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	}

	host->private_data = hpriv;

	if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
		host->flags |= ATA_HOST_PARALLEL_SCAN;
	else
		printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");

	if (pi.flags & ATA_FLAG_EM)
		ahci_reset_em(host);

	for (i = 0; i < host->n_ports; i++) {
		struct ata_port *ap = host->ports[i];

		ata_port_desc(ap, "mmio %pR", mem);
		ata_port_desc(ap, "port 0x%x", 0x100 + ap->port_no * 0x80);

		/* set enclosure management message type */
		if (ap->flags & ATA_FLAG_EM)
189
			ap->em_message_type = hpriv->em_msg_type;
A
Anton Vorontsov 已提交
190 191 192 193 194 195 196 197

		/* disabled/not-implemented port */
		if (!(hpriv->port_map & (1 << i)))
			ap->ops = &ata_dummy_port_ops;
	}

	rc = ahci_reset_controller(host);
	if (rc)
198
		goto pdata_exit;
A
Anton Vorontsov 已提交
199 200 201 202 203

	ahci_init_controller(host);
	ahci_print_info(host, "platform");

	rc = ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,
204
			       &ahci_platform_sht);
A
Anton Vorontsov 已提交
205
	if (rc)
206
		goto pdata_exit;
A
Anton Vorontsov 已提交
207 208

	return 0;
209
pdata_exit:
A
Anton Vorontsov 已提交
210 211
	if (pdata && pdata->exit)
		pdata->exit(dev);
212 213 214 215 216 217
disable_unprepare_clk:
	if (!IS_ERR(hpriv->clk))
		clk_disable_unprepare(hpriv->clk);
free_clk:
	if (!IS_ERR(hpriv->clk))
		clk_put(hpriv->clk);
A
Anton Vorontsov 已提交
218 219 220 221 222 223
	return rc;
}

static int __devexit ahci_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
224
	struct ahci_platform_data *pdata = dev_get_platdata(dev);
A
Anton Vorontsov 已提交
225
	struct ata_host *host = dev_get_drvdata(dev);
226
	struct ahci_host_priv *hpriv = host->private_data;
A
Anton Vorontsov 已提交
227 228 229 230 231 232

	ata_host_detach(host);

	if (pdata && pdata->exit)
		pdata->exit(dev);

233 234 235 236 237
	if (!IS_ERR(hpriv->clk)) {
		clk_disable_unprepare(hpriv->clk);
		clk_put(hpriv->clk);
	}

A
Anton Vorontsov 已提交
238 239 240
	return 0;
}

241
#ifdef CONFIG_PM_SLEEP
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static int ahci_suspend(struct device *dev)
{
	struct ahci_platform_data *pdata = dev_get_platdata(dev);
	struct ata_host *host = dev_get_drvdata(dev);
	struct ahci_host_priv *hpriv = host->private_data;
	void __iomem *mmio = hpriv->mmio;
	u32 ctl;
	int rc;

	if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
		dev_err(dev, "firmware update required for suspend/resume\n");
		return -EIO;
	}

	/*
	 * AHCI spec rev1.1 section 8.3.3:
	 * Software must disable interrupts prior to requesting a
	 * transition of the HBA to D3 state.
	 */
	ctl = readl(mmio + HOST_CTL);
	ctl &= ~HOST_IRQ_EN;
	writel(ctl, mmio + HOST_CTL);
	readl(mmio + HOST_CTL); /* flush */

	rc = ata_host_suspend(host, PMSG_SUSPEND);
	if (rc)
		return rc;

	if (pdata && pdata->suspend)
		return pdata->suspend(dev);
272 273 274 275

	if (!IS_ERR(hpriv->clk))
		clk_disable_unprepare(hpriv->clk);

276 277 278 279 280 281 282
	return 0;
}

static int ahci_resume(struct device *dev)
{
	struct ahci_platform_data *pdata = dev_get_platdata(dev);
	struct ata_host *host = dev_get_drvdata(dev);
283
	struct ahci_host_priv *hpriv = host->private_data;
284 285
	int rc;

286 287 288 289 290 291 292 293
	if (!IS_ERR(hpriv->clk)) {
		rc = clk_prepare_enable(hpriv->clk);
		if (rc) {
			dev_err(dev, "clock prepare enable failed");
			return rc;
		}
	}

294 295 296
	if (pdata && pdata->resume) {
		rc = pdata->resume(dev);
		if (rc)
297
			goto disable_unprepare_clk;
298 299 300 301 302
	}

	if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
		rc = ahci_reset_controller(host);
		if (rc)
303
			goto disable_unprepare_clk;
304 305 306 307 308 309 310

		ahci_init_controller(host);
	}

	ata_host_resume(host);

	return 0;
311 312 313 314 315 316

disable_unprepare_clk:
	if (!IS_ERR(hpriv->clk))
		clk_disable_unprepare(hpriv->clk);

	return rc;
317 318 319
}
#endif

320 321
SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_suspend, ahci_resume);

322
static const struct of_device_id ahci_of_match[] = {
323
	{ .compatible = "snps,spear-ahci", },
324 325 326 327
	{},
};
MODULE_DEVICE_TABLE(of, ahci_of_match);

A
Anton Vorontsov 已提交
328 329 330 331 332
static struct platform_driver ahci_driver = {
	.remove = __devexit_p(ahci_remove),
	.driver = {
		.name = "ahci",
		.owner = THIS_MODULE,
333
		.of_match_table = ahci_of_match,
334
		.pm = &ahci_pm_ops,
A
Anton Vorontsov 已提交
335
	},
336
	.id_table	= ahci_devtype,
A
Anton Vorontsov 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
};

static int __init ahci_init(void)
{
	return platform_driver_probe(&ahci_driver, ahci_probe);
}
module_init(ahci_init);

static void __exit ahci_exit(void)
{
	platform_driver_unregister(&ahci_driver);
}
module_exit(ahci_exit);

MODULE_DESCRIPTION("AHCI SATA platform driver");
MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ahci");