xref: /openbmc/linux/drivers/ata/ahci_mvebu.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1a3464ed2SThomas Petazzoni /*
2a3464ed2SThomas Petazzoni  * AHCI glue platform driver for Marvell EBU SOCs
3a3464ed2SThomas Petazzoni  *
4a3464ed2SThomas Petazzoni  * Copyright (C) 2014 Marvell
5a3464ed2SThomas Petazzoni  *
6a3464ed2SThomas Petazzoni  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7a3464ed2SThomas Petazzoni  * Marcin Wojtas <mw@semihalf.com>
8a3464ed2SThomas Petazzoni  *
9a3464ed2SThomas Petazzoni  * This file is licensed under the terms of the GNU General Public
10a3464ed2SThomas Petazzoni  * License version 2.  This program is licensed "as is" without any
11a3464ed2SThomas Petazzoni  * warranty of any kind, whether express or implied.
12a3464ed2SThomas Petazzoni  */
13a3464ed2SThomas Petazzoni 
14a3464ed2SThomas Petazzoni #include <linux/ahci_platform.h>
15a3464ed2SThomas Petazzoni #include <linux/kernel.h>
16a3464ed2SThomas Petazzoni #include <linux/mbus.h>
17a3464ed2SThomas Petazzoni #include <linux/module.h>
18*61e6ae71SRob Herring #include <linux/of.h>
19a3464ed2SThomas Petazzoni #include <linux/platform_device.h>
20a3464ed2SThomas Petazzoni #include "ahci.h"
21a3464ed2SThomas Petazzoni 
22018d5ef2SAkinobu Mita #define DRV_NAME "ahci-mvebu"
23018d5ef2SAkinobu Mita 
24a3464ed2SThomas Petazzoni #define AHCI_VENDOR_SPECIFIC_0_ADDR  0xa0
25a3464ed2SThomas Petazzoni #define AHCI_VENDOR_SPECIFIC_0_DATA  0xa4
26a3464ed2SThomas Petazzoni 
27a3464ed2SThomas Petazzoni #define AHCI_WINDOW_CTRL(win)	(0x60 + ((win) << 4))
28a3464ed2SThomas Petazzoni #define AHCI_WINDOW_BASE(win)	(0x64 + ((win) << 4))
29a3464ed2SThomas Petazzoni #define AHCI_WINDOW_SIZE(win)	(0x68 + ((win) << 4))
30a3464ed2SThomas Petazzoni 
3196dbcb40SMiquel Raynal struct ahci_mvebu_plat_data {
3296dbcb40SMiquel Raynal 	int (*plat_config)(struct ahci_host_priv *hpriv);
33bde0b5c1SMiquel Raynal 	unsigned int flags;
3496dbcb40SMiquel Raynal };
3596dbcb40SMiquel Raynal 
ahci_mvebu_mbus_config(struct ahci_host_priv * hpriv,const struct mbus_dram_target_info * dram)36a3464ed2SThomas Petazzoni static void ahci_mvebu_mbus_config(struct ahci_host_priv *hpriv,
37a3464ed2SThomas Petazzoni 				   const struct mbus_dram_target_info *dram)
38a3464ed2SThomas Petazzoni {
39a3464ed2SThomas Petazzoni 	int i;
40a3464ed2SThomas Petazzoni 
41a3464ed2SThomas Petazzoni 	for (i = 0; i < 4; i++) {
42a3464ed2SThomas Petazzoni 		writel(0, hpriv->mmio + AHCI_WINDOW_CTRL(i));
43a3464ed2SThomas Petazzoni 		writel(0, hpriv->mmio + AHCI_WINDOW_BASE(i));
44a3464ed2SThomas Petazzoni 		writel(0, hpriv->mmio + AHCI_WINDOW_SIZE(i));
45a3464ed2SThomas Petazzoni 	}
46a3464ed2SThomas Petazzoni 
47a3464ed2SThomas Petazzoni 	for (i = 0; i < dram->num_cs; i++) {
48a3464ed2SThomas Petazzoni 		const struct mbus_dram_window *cs = dram->cs + i;
49a3464ed2SThomas Petazzoni 
50a3464ed2SThomas Petazzoni 		writel((cs->mbus_attr << 8) |
51a3464ed2SThomas Petazzoni 		       (dram->mbus_dram_target_id << 4) | 1,
52a3464ed2SThomas Petazzoni 		       hpriv->mmio + AHCI_WINDOW_CTRL(i));
53e96998fcSNadav Haklai 		writel(cs->base >> 16, hpriv->mmio + AHCI_WINDOW_BASE(i));
54a3464ed2SThomas Petazzoni 		writel(((cs->size - 1) & 0xffff0000),
55a3464ed2SThomas Petazzoni 		       hpriv->mmio + AHCI_WINDOW_SIZE(i));
56a3464ed2SThomas Petazzoni 	}
57a3464ed2SThomas Petazzoni }
58a3464ed2SThomas Petazzoni 
ahci_mvebu_regret_option(struct ahci_host_priv * hpriv)59a3464ed2SThomas Petazzoni static void ahci_mvebu_regret_option(struct ahci_host_priv *hpriv)
60a3464ed2SThomas Petazzoni {
61a3464ed2SThomas Petazzoni 	/*
62a3464ed2SThomas Petazzoni 	 * Enable the regret bit to allow the SATA unit to regret a
63a3464ed2SThomas Petazzoni 	 * request that didn't receive an acknowlegde and avoid a
64a3464ed2SThomas Petazzoni 	 * deadlock
65a3464ed2SThomas Petazzoni 	 */
66a3464ed2SThomas Petazzoni 	writel(0x4, hpriv->mmio + AHCI_VENDOR_SPECIFIC_0_ADDR);
67a3464ed2SThomas Petazzoni 	writel(0x80, hpriv->mmio + AHCI_VENDOR_SPECIFIC_0_DATA);
68a3464ed2SThomas Petazzoni }
69a3464ed2SThomas Petazzoni 
ahci_mvebu_armada_380_config(struct ahci_host_priv * hpriv)7096dbcb40SMiquel Raynal static int ahci_mvebu_armada_380_config(struct ahci_host_priv *hpriv)
7196dbcb40SMiquel Raynal {
7296dbcb40SMiquel Raynal 	const struct mbus_dram_target_info *dram;
7396dbcb40SMiquel Raynal 	int rc = 0;
7496dbcb40SMiquel Raynal 
7596dbcb40SMiquel Raynal 	dram = mv_mbus_dram_info();
7696dbcb40SMiquel Raynal 	if (dram)
7796dbcb40SMiquel Raynal 		ahci_mvebu_mbus_config(hpriv, dram);
7896dbcb40SMiquel Raynal 	else
7996dbcb40SMiquel Raynal 		rc = -ENODEV;
8096dbcb40SMiquel Raynal 
8196dbcb40SMiquel Raynal 	ahci_mvebu_regret_option(hpriv);
8296dbcb40SMiquel Raynal 
8396dbcb40SMiquel Raynal 	return rc;
8496dbcb40SMiquel Raynal }
8596dbcb40SMiquel Raynal 
ahci_mvebu_armada_3700_config(struct ahci_host_priv * hpriv)862f558bc3SMiquel Raynal static int ahci_mvebu_armada_3700_config(struct ahci_host_priv *hpriv)
872f558bc3SMiquel Raynal {
882f558bc3SMiquel Raynal 	u32 reg;
892f558bc3SMiquel Raynal 
902f558bc3SMiquel Raynal 	writel(0, hpriv->mmio + AHCI_VENDOR_SPECIFIC_0_ADDR);
912f558bc3SMiquel Raynal 
922f558bc3SMiquel Raynal 	reg = readl(hpriv->mmio + AHCI_VENDOR_SPECIFIC_0_DATA);
932f558bc3SMiquel Raynal 	reg |= BIT(6);
942f558bc3SMiquel Raynal 	writel(reg, hpriv->mmio + AHCI_VENDOR_SPECIFIC_0_DATA);
952f558bc3SMiquel Raynal 
962f558bc3SMiquel Raynal 	return 0;
972f558bc3SMiquel Raynal }
982f558bc3SMiquel Raynal 
99daa2e3bdSEvan Wang /**
100daa2e3bdSEvan Wang  * ahci_mvebu_stop_engine
101daa2e3bdSEvan Wang  *
102daa2e3bdSEvan Wang  * @ap:	Target ata port
103daa2e3bdSEvan Wang  *
104daa2e3bdSEvan Wang  * Errata Ref#226 - SATA Disk HOT swap issue when connected through
105daa2e3bdSEvan Wang  * Port Multiplier in FIS-based Switching mode.
106daa2e3bdSEvan Wang  *
107daa2e3bdSEvan Wang  * To avoid the issue, according to design, the bits[11:8, 0] of
108daa2e3bdSEvan Wang  * register PxFBS are cleared when Port Command and Status (0x18) bit[0]
109daa2e3bdSEvan Wang  * changes its value from 1 to 0, i.e. falling edge of Port
110daa2e3bdSEvan Wang  * Command and Status bit[0] sends PULSE that resets PxFBS
111daa2e3bdSEvan Wang  * bits[11:8; 0].
112daa2e3bdSEvan Wang  *
113daa2e3bdSEvan Wang  * This function is used to override function of "ahci_stop_engine"
114daa2e3bdSEvan Wang  * from libahci.c by adding the mvebu work around(WA) to save PxFBS
115daa2e3bdSEvan Wang  * value before the PxCMD ST write of 0, then restore PxFBS value.
116daa2e3bdSEvan Wang  *
117daa2e3bdSEvan Wang  * Return: 0 on success; Error code otherwise.
118daa2e3bdSEvan Wang  */
ahci_mvebu_stop_engine(struct ata_port * ap)11995ffcf47SWei Yongjun static int ahci_mvebu_stop_engine(struct ata_port *ap)
120daa2e3bdSEvan Wang {
121daa2e3bdSEvan Wang 	void __iomem *port_mmio = ahci_port_base(ap);
122daa2e3bdSEvan Wang 	u32 tmp, port_fbs;
123daa2e3bdSEvan Wang 
124daa2e3bdSEvan Wang 	tmp = readl(port_mmio + PORT_CMD);
125daa2e3bdSEvan Wang 
126daa2e3bdSEvan Wang 	/* check if the HBA is idle */
127daa2e3bdSEvan Wang 	if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
128daa2e3bdSEvan Wang 		return 0;
129daa2e3bdSEvan Wang 
130daa2e3bdSEvan Wang 	/* save the port PxFBS register for later restore */
131daa2e3bdSEvan Wang 	port_fbs = readl(port_mmio + PORT_FBS);
132daa2e3bdSEvan Wang 
133daa2e3bdSEvan Wang 	/* setting HBA to idle */
134daa2e3bdSEvan Wang 	tmp &= ~PORT_CMD_START;
135daa2e3bdSEvan Wang 	writel(tmp, port_mmio + PORT_CMD);
136daa2e3bdSEvan Wang 
137daa2e3bdSEvan Wang 	/*
138daa2e3bdSEvan Wang 	 * bit #15 PxCMD signal doesn't clear PxFBS,
139daa2e3bdSEvan Wang 	 * restore the PxFBS register right after clearing the PxCMD ST,
140daa2e3bdSEvan Wang 	 * no need to wait for the PxCMD bit #15.
141daa2e3bdSEvan Wang 	 */
142daa2e3bdSEvan Wang 	writel(port_fbs, port_mmio + PORT_FBS);
143daa2e3bdSEvan Wang 
144daa2e3bdSEvan Wang 	/* wait for engine to stop. This could be as long as 500 msec */
145daa2e3bdSEvan Wang 	tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
146daa2e3bdSEvan Wang 				PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
147daa2e3bdSEvan Wang 	if (tmp & PORT_CMD_LIST_ON)
148daa2e3bdSEvan Wang 		return -EIO;
149daa2e3bdSEvan Wang 
150daa2e3bdSEvan Wang 	return 0;
151daa2e3bdSEvan Wang }
152daa2e3bdSEvan Wang 
1534f1dd973SArnd Bergmann #ifdef CONFIG_PM_SLEEP
ahci_mvebu_suspend(struct platform_device * pdev,pm_message_t state)154d6ecf158SThomas Petazzoni static int ahci_mvebu_suspend(struct platform_device *pdev, pm_message_t state)
155d6ecf158SThomas Petazzoni {
156d6ecf158SThomas Petazzoni 	return ahci_platform_suspend_host(&pdev->dev);
157d6ecf158SThomas Petazzoni }
158d6ecf158SThomas Petazzoni 
ahci_mvebu_resume(struct platform_device * pdev)159d6ecf158SThomas Petazzoni static int ahci_mvebu_resume(struct platform_device *pdev)
160d6ecf158SThomas Petazzoni {
161d6ecf158SThomas Petazzoni 	struct ata_host *host = platform_get_drvdata(pdev);
162d6ecf158SThomas Petazzoni 	struct ahci_host_priv *hpriv = host->private_data;
16396dbcb40SMiquel Raynal 	const struct ahci_mvebu_plat_data *pdata = hpriv->plat_data;
164d6ecf158SThomas Petazzoni 
16596dbcb40SMiquel Raynal 	pdata->plat_config(hpriv);
166d6ecf158SThomas Petazzoni 
167d6ecf158SThomas Petazzoni 	return ahci_platform_resume_host(&pdev->dev);
168d6ecf158SThomas Petazzoni }
1694f1dd973SArnd Bergmann #else
1704f1dd973SArnd Bergmann #define ahci_mvebu_suspend NULL
1714f1dd973SArnd Bergmann #define ahci_mvebu_resume NULL
1724f1dd973SArnd Bergmann #endif
173d6ecf158SThomas Petazzoni 
174a3464ed2SThomas Petazzoni static const struct ata_port_info ahci_mvebu_port_info = {
175a3464ed2SThomas Petazzoni 	.flags	   = AHCI_FLAG_COMMON,
176a3464ed2SThomas Petazzoni 	.pio_mask  = ATA_PIO4,
177a3464ed2SThomas Petazzoni 	.udma_mask = ATA_UDMA6,
178a3464ed2SThomas Petazzoni 	.port_ops  = &ahci_platform_ops,
179a3464ed2SThomas Petazzoni };
180a3464ed2SThomas Petazzoni 
18125df73d9SBart Van Assche static const struct scsi_host_template ahci_platform_sht = {
182018d5ef2SAkinobu Mita 	AHCI_SHT(DRV_NAME),
183018d5ef2SAkinobu Mita };
184018d5ef2SAkinobu Mita 
ahci_mvebu_probe(struct platform_device * pdev)185a3464ed2SThomas Petazzoni static int ahci_mvebu_probe(struct platform_device *pdev)
186a3464ed2SThomas Petazzoni {
18796dbcb40SMiquel Raynal 	const struct ahci_mvebu_plat_data *pdata;
188a3464ed2SThomas Petazzoni 	struct ahci_host_priv *hpriv;
189a3464ed2SThomas Petazzoni 	int rc;
190a3464ed2SThomas Petazzoni 
19196dbcb40SMiquel Raynal 	pdata = of_device_get_match_data(&pdev->dev);
19296dbcb40SMiquel Raynal 	if (!pdata)
19396dbcb40SMiquel Raynal 		return -EINVAL;
19496dbcb40SMiquel Raynal 
19516af2d65SKunihiko Hayashi 	hpriv = ahci_platform_get_resources(pdev, 0);
196a3464ed2SThomas Petazzoni 	if (IS_ERR(hpriv))
197a3464ed2SThomas Petazzoni 		return PTR_ERR(hpriv);
198a3464ed2SThomas Petazzoni 
199bde0b5c1SMiquel Raynal 	hpriv->flags |= pdata->flags;
20096dbcb40SMiquel Raynal 	hpriv->plat_data = (void *)pdata;
20196dbcb40SMiquel Raynal 
202a3464ed2SThomas Petazzoni 	rc = ahci_platform_enable_resources(hpriv);
203a3464ed2SThomas Petazzoni 	if (rc)
204a3464ed2SThomas Petazzoni 		return rc;
205a3464ed2SThomas Petazzoni 
206daa2e3bdSEvan Wang 	hpriv->stop_engine = ahci_mvebu_stop_engine;
207daa2e3bdSEvan Wang 
20896dbcb40SMiquel Raynal 	rc = pdata->plat_config(hpriv);
20996dbcb40SMiquel Raynal 	if (rc)
21096dbcb40SMiquel Raynal 		goto disable_resources;
211a3464ed2SThomas Petazzoni 
212018d5ef2SAkinobu Mita 	rc = ahci_platform_init_host(pdev, hpriv, &ahci_mvebu_port_info,
213018d5ef2SAkinobu Mita 				     &ahci_platform_sht);
214a3464ed2SThomas Petazzoni 	if (rc)
215a3464ed2SThomas Petazzoni 		goto disable_resources;
216a3464ed2SThomas Petazzoni 
217a3464ed2SThomas Petazzoni 	return 0;
218a3464ed2SThomas Petazzoni 
219a3464ed2SThomas Petazzoni disable_resources:
220a3464ed2SThomas Petazzoni 	ahci_platform_disable_resources(hpriv);
221a3464ed2SThomas Petazzoni 	return rc;
222a3464ed2SThomas Petazzoni }
223a3464ed2SThomas Petazzoni 
22496dbcb40SMiquel Raynal static const struct ahci_mvebu_plat_data ahci_mvebu_armada_380_plat_data = {
22596dbcb40SMiquel Raynal 	.plat_config = ahci_mvebu_armada_380_config,
22696dbcb40SMiquel Raynal };
22796dbcb40SMiquel Raynal 
22896dbcb40SMiquel Raynal static const struct ahci_mvebu_plat_data ahci_mvebu_armada_3700_plat_data = {
2292f558bc3SMiquel Raynal 	.plat_config = ahci_mvebu_armada_3700_config,
230ee995101SPali Rohár 	.flags = AHCI_HFLAG_SUSPEND_PHYS,
23196dbcb40SMiquel Raynal };
23296dbcb40SMiquel Raynal 
233a3464ed2SThomas Petazzoni static const struct of_device_id ahci_mvebu_of_match[] = {
23496dbcb40SMiquel Raynal 	{
23596dbcb40SMiquel Raynal 		.compatible = "marvell,armada-380-ahci",
23696dbcb40SMiquel Raynal 		.data = &ahci_mvebu_armada_380_plat_data,
23796dbcb40SMiquel Raynal 	},
23896dbcb40SMiquel Raynal 	{
23996dbcb40SMiquel Raynal 		.compatible = "marvell,armada-3700-ahci",
24096dbcb40SMiquel Raynal 		.data = &ahci_mvebu_armada_3700_plat_data,
24196dbcb40SMiquel Raynal 	},
2425e776d7bSGeert Uytterhoeven 	{ /* sentinel */ }
243a3464ed2SThomas Petazzoni };
244a3464ed2SThomas Petazzoni MODULE_DEVICE_TABLE(of, ahci_mvebu_of_match);
245a3464ed2SThomas Petazzoni 
246a3464ed2SThomas Petazzoni static struct platform_driver ahci_mvebu_driver = {
247a3464ed2SThomas Petazzoni 	.probe = ahci_mvebu_probe,
248a7eb54d4SUwe Kleine-König 	.remove_new = ata_platform_remove_one,
249d6ecf158SThomas Petazzoni 	.suspend = ahci_mvebu_suspend,
250d6ecf158SThomas Petazzoni 	.resume = ahci_mvebu_resume,
251a3464ed2SThomas Petazzoni 	.driver = {
252018d5ef2SAkinobu Mita 		.name = DRV_NAME,
253a3464ed2SThomas Petazzoni 		.of_match_table = ahci_mvebu_of_match,
254a3464ed2SThomas Petazzoni 	},
255a3464ed2SThomas Petazzoni };
256a3464ed2SThomas Petazzoni module_platform_driver(ahci_mvebu_driver);
257a3464ed2SThomas Petazzoni 
258a3464ed2SThomas Petazzoni MODULE_DESCRIPTION("Marvell EBU AHCI SATA driver");
259a3464ed2SThomas Petazzoni MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>, Marcin Wojtas <mw@semihalf.com>");
260a3464ed2SThomas Petazzoni MODULE_LICENSE("GPL");
261a3464ed2SThomas Petazzoni MODULE_ALIAS("platform:ahci_mvebu");
262