Linux 4.9.74
[linux/fpc-iii.git] / drivers / ata / ahci_brcm.c
blob6f8a7341fa08a701af61226fab156206b506be20
1 /*
2 * Broadcom SATA3 AHCI Controller Driver
4 * Copyright © 2009-2015 Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/ahci_platform.h>
18 #include <linux/compiler.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/libata.h>
25 #include <linux/module.h>
26 #include <linux/of.h>
27 #include <linux/platform_device.h>
28 #include <linux/string.h>
30 #include "ahci.h"
32 #define DRV_NAME "brcm-ahci"
34 #define SATA_TOP_CTRL_VERSION 0x0
35 #define SATA_TOP_CTRL_BUS_CTRL 0x4
36 #define MMIO_ENDIAN_SHIFT 0 /* CPU->AHCI */
37 #define DMADESC_ENDIAN_SHIFT 2 /* AHCI->DDR */
38 #define DMADATA_ENDIAN_SHIFT 4 /* AHCI->DDR */
39 #define PIODATA_ENDIAN_SHIFT 6
40 #define ENDIAN_SWAP_NONE 0
41 #define ENDIAN_SWAP_FULL 2
42 #define OVERRIDE_HWINIT BIT(16)
43 #define SATA_TOP_CTRL_TP_CTRL 0x8
44 #define SATA_TOP_CTRL_PHY_CTRL 0xc
45 #define SATA_TOP_CTRL_PHY_CTRL_1 0x0
46 #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14)
47 #define SATA_TOP_CTRL_PHY_CTRL_2 0x4
48 #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0)
49 #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1)
50 #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2)
51 #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3)
52 #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14)
53 #define SATA_TOP_CTRL_PHY_OFFS 0x8
54 #define SATA_TOP_MAX_PHYS 2
56 #define SATA_FIRST_PORT_CTRL 0x700
57 #define SATA_NEXT_PORT_CTRL_OFFSET 0x80
58 #define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18)
60 /* On big-endian MIPS, buses are reversed to big endian, so switch them back */
61 #if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
62 #define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */
63 #define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */
64 #else
65 #define DATA_ENDIAN 0
66 #define MMIO_ENDIAN 0
67 #endif
69 #define BUS_CTRL_ENDIAN_CONF \
70 ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \
71 (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \
72 (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT))
74 enum brcm_ahci_version {
75 BRCM_SATA_BCM7425 = 1,
76 BRCM_SATA_BCM7445,
77 BRCM_SATA_NSP,
80 enum brcm_ahci_quirks {
81 BRCM_AHCI_QUIRK_NO_NCQ = BIT(0),
82 BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(1),
85 struct brcm_ahci_priv {
86 struct device *dev;
87 void __iomem *top_ctrl;
88 u32 port_mask;
89 u32 quirks;
90 enum brcm_ahci_version version;
93 static const struct ata_port_info ahci_brcm_port_info = {
94 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
95 .link_flags = ATA_LFLAG_NO_DB_DELAY,
96 .pio_mask = ATA_PIO4,
97 .udma_mask = ATA_UDMA6,
98 .port_ops = &ahci_platform_ops,
101 static inline u32 brcm_sata_readreg(void __iomem *addr)
104 * MIPS endianness is configured by boot strap, which also reverses all
105 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
106 * endian I/O).
108 * Other architectures (e.g., ARM) either do not support big endian, or
109 * else leave I/O in little endian mode.
111 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
112 return __raw_readl(addr);
113 else
114 return readl_relaxed(addr);
117 static inline void brcm_sata_writereg(u32 val, void __iomem *addr)
119 /* See brcm_sata_readreg() comments */
120 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
121 __raw_writel(val, addr);
122 else
123 writel_relaxed(val, addr);
126 static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv)
128 struct brcm_ahci_priv *priv = hpriv->plat_data;
129 u32 bus_ctrl, port_ctrl, host_caps;
130 int i;
132 /* Enable support for ALPM */
133 bus_ctrl = brcm_sata_readreg(priv->top_ctrl +
134 SATA_TOP_CTRL_BUS_CTRL);
135 brcm_sata_writereg(bus_ctrl | OVERRIDE_HWINIT,
136 priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL);
137 host_caps = readl(hpriv->mmio + HOST_CAP);
138 writel(host_caps | HOST_CAP_ALPM, hpriv->mmio);
139 brcm_sata_writereg(bus_ctrl, priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL);
142 * Adjust timeout to allow PLL sufficient time to lock while waking
143 * up from slumber mode.
145 for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL;
146 i < SATA_TOP_MAX_PHYS;
147 i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) {
148 if (priv->port_mask & BIT(i))
149 writel(0xff1003fc,
150 hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl));
154 static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port)
156 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
157 (port * SATA_TOP_CTRL_PHY_OFFS);
158 void __iomem *p;
159 u32 reg;
161 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
162 return;
164 /* clear PHY_DEFAULT_POWER_STATE */
165 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
166 reg = brcm_sata_readreg(p);
167 reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
168 brcm_sata_writereg(reg, p);
170 /* reset the PHY digital logic */
171 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
172 reg = brcm_sata_readreg(p);
173 reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
174 SATA_TOP_CTRL_2_SW_RST_RX);
175 reg |= SATA_TOP_CTRL_2_SW_RST_TX;
176 brcm_sata_writereg(reg, p);
177 reg = brcm_sata_readreg(p);
178 reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
179 brcm_sata_writereg(reg, p);
180 reg = brcm_sata_readreg(p);
181 reg &= ~SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
182 brcm_sata_writereg(reg, p);
183 (void)brcm_sata_readreg(p);
186 static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port)
188 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
189 (port * SATA_TOP_CTRL_PHY_OFFS);
190 void __iomem *p;
191 u32 reg;
193 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
194 return;
196 /* power-off the PHY digital logic */
197 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
198 reg = brcm_sata_readreg(p);
199 reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
200 SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX |
201 SATA_TOP_CTRL_2_PHY_GLOBAL_RESET);
202 brcm_sata_writereg(reg, p);
204 /* set PHY_DEFAULT_POWER_STATE */
205 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
206 reg = brcm_sata_readreg(p);
207 reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
208 brcm_sata_writereg(reg, p);
211 static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv)
213 int i;
215 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
216 if (priv->port_mask & BIT(i))
217 brcm_sata_phy_enable(priv, i);
220 static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv)
222 int i;
224 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
225 if (priv->port_mask & BIT(i))
226 brcm_sata_phy_disable(priv, i);
229 static u32 brcm_ahci_get_portmask(struct platform_device *pdev,
230 struct brcm_ahci_priv *priv)
232 void __iomem *ahci;
233 struct resource *res;
234 u32 impl;
236 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ahci");
237 ahci = devm_ioremap_resource(&pdev->dev, res);
238 if (IS_ERR(ahci))
239 return 0;
241 impl = readl(ahci + HOST_PORTS_IMPL);
243 if (fls(impl) > SATA_TOP_MAX_PHYS)
244 dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
245 impl);
246 else if (!impl)
247 dev_info(priv->dev, "no ports found\n");
249 devm_iounmap(&pdev->dev, ahci);
250 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
252 return impl;
255 static void brcm_sata_init(struct brcm_ahci_priv *priv)
257 void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL;
259 /* Configure endianness */
260 if (priv->version == BRCM_SATA_NSP) {
261 u32 data = brcm_sata_readreg(ctrl);
263 data &= ~((0x03 << DMADATA_ENDIAN_SHIFT) |
264 (0x03 << DMADESC_ENDIAN_SHIFT));
265 data |= (0x02 << DMADATA_ENDIAN_SHIFT) |
266 (0x02 << DMADESC_ENDIAN_SHIFT);
267 brcm_sata_writereg(data, ctrl);
268 } else
269 brcm_sata_writereg(BUS_CTRL_ENDIAN_CONF, ctrl);
272 #ifdef CONFIG_PM_SLEEP
273 static int brcm_ahci_suspend(struct device *dev)
275 struct ata_host *host = dev_get_drvdata(dev);
276 struct ahci_host_priv *hpriv = host->private_data;
277 struct brcm_ahci_priv *priv = hpriv->plat_data;
278 int ret;
280 ret = ahci_platform_suspend(dev);
281 brcm_sata_phys_disable(priv);
282 return ret;
285 static int brcm_ahci_resume(struct device *dev)
287 struct ata_host *host = dev_get_drvdata(dev);
288 struct ahci_host_priv *hpriv = host->private_data;
289 struct brcm_ahci_priv *priv = hpriv->plat_data;
291 brcm_sata_init(priv);
292 brcm_sata_phys_enable(priv);
293 brcm_sata_alpm_init(hpriv);
294 return ahci_platform_resume(dev);
296 #endif
298 static struct scsi_host_template ahci_platform_sht = {
299 AHCI_SHT(DRV_NAME),
302 static const struct of_device_id ahci_of_match[] = {
303 {.compatible = "brcm,bcm7425-ahci", .data = (void *)BRCM_SATA_BCM7425},
304 {.compatible = "brcm,bcm7445-ahci", .data = (void *)BRCM_SATA_BCM7445},
305 {.compatible = "brcm,bcm-nsp-ahci", .data = (void *)BRCM_SATA_NSP},
308 MODULE_DEVICE_TABLE(of, ahci_of_match);
310 static int brcm_ahci_probe(struct platform_device *pdev)
312 const struct of_device_id *of_id;
313 struct device *dev = &pdev->dev;
314 struct brcm_ahci_priv *priv;
315 struct ahci_host_priv *hpriv;
316 struct resource *res;
317 int ret;
319 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
320 if (!priv)
321 return -ENOMEM;
323 of_id = of_match_node(ahci_of_match, pdev->dev.of_node);
324 if (!of_id)
325 return -ENODEV;
327 priv->version = (enum brcm_ahci_version)of_id->data;
328 priv->dev = dev;
330 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "top-ctrl");
331 priv->top_ctrl = devm_ioremap_resource(dev, res);
332 if (IS_ERR(priv->top_ctrl))
333 return PTR_ERR(priv->top_ctrl);
335 if ((priv->version == BRCM_SATA_BCM7425) ||
336 (priv->version == BRCM_SATA_NSP)) {
337 priv->quirks |= BRCM_AHCI_QUIRK_NO_NCQ;
338 priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
341 brcm_sata_init(priv);
343 priv->port_mask = brcm_ahci_get_portmask(pdev, priv);
344 if (!priv->port_mask)
345 return -ENODEV;
347 brcm_sata_phys_enable(priv);
349 hpriv = ahci_platform_get_resources(pdev);
350 if (IS_ERR(hpriv))
351 return PTR_ERR(hpriv);
352 hpriv->plat_data = priv;
353 hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP;
355 brcm_sata_alpm_init(hpriv);
357 ret = ahci_platform_enable_resources(hpriv);
358 if (ret)
359 return ret;
361 if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ)
362 hpriv->flags |= AHCI_HFLAG_NO_NCQ;
364 ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
365 &ahci_platform_sht);
366 if (ret)
367 return ret;
369 dev_info(dev, "Broadcom AHCI SATA3 registered\n");
371 return 0;
374 static int brcm_ahci_remove(struct platform_device *pdev)
376 struct ata_host *host = dev_get_drvdata(&pdev->dev);
377 struct ahci_host_priv *hpriv = host->private_data;
378 struct brcm_ahci_priv *priv = hpriv->plat_data;
379 int ret;
381 ret = ata_platform_remove_one(pdev);
382 if (ret)
383 return ret;
385 brcm_sata_phys_disable(priv);
387 return 0;
390 static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume);
392 static struct platform_driver brcm_ahci_driver = {
393 .probe = brcm_ahci_probe,
394 .remove = brcm_ahci_remove,
395 .driver = {
396 .name = DRV_NAME,
397 .of_match_table = ahci_of_match,
398 .pm = &ahci_brcm_pm_ops,
401 module_platform_driver(brcm_ahci_driver);
403 MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver");
404 MODULE_AUTHOR("Brian Norris");
405 MODULE_LICENSE("GPL");
406 MODULE_ALIAS("platform:sata-brcmstb");