blob: ad3b8826ec79e3a1f2ec63fc59e35baa2cd64aca [file] [log] [blame]
Brian Norris766a2d92015-05-12 16:28:21 -07001/*
2 * Broadcom SATA3 AHCI Controller Driver
3 *
4 * Copyright © 2009-2015 Broadcom Corporation
5 *
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.
10 *
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.
15 */
16
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>
29
30#include "ahci.h"
31
32#define DRV_NAME "brcm-ahci"
33
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
Brian Norris766a2d92015-05-12 16:28:21 -070042#define SATA_TOP_CTRL_TP_CTRL 0x8
43#define SATA_TOP_CTRL_PHY_CTRL 0xc
44 #define SATA_TOP_CTRL_PHY_CTRL_1 0x0
45 #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14)
46 #define SATA_TOP_CTRL_PHY_CTRL_2 0x4
47 #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0)
48 #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1)
49 #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2)
50 #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3)
51 #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14)
52 #define SATA_TOP_CTRL_PHY_OFFS 0x8
53 #define SATA_TOP_MAX_PHYS 2
Brian Norris766a2d92015-05-12 16:28:21 -070054
Danesh Petigara6863caa2016-01-07 16:03:30 -080055#define SATA_FIRST_PORT_CTRL 0x700
56#define SATA_NEXT_PORT_CTRL_OFFSET 0x80
57#define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18)
58
Brian Norris766a2d92015-05-12 16:28:21 -070059/* On big-endian MIPS, buses are reversed to big endian, so switch them back */
60#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
61#define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */
62#define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */
63#else
64#define DATA_ENDIAN 0
65#define MMIO_ENDIAN 0
66#endif
67
68#define BUS_CTRL_ENDIAN_CONF \
69 ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \
70 (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \
71 (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT))
72
Florian Fainelli36fffd62017-12-22 11:43:08 -080073#define BUS_CTRL_ENDIAN_NSP_CONF \
74 (0x02 << DMADATA_ENDIAN_SHIFT | 0x02 << DMADESC_ENDIAN_SHIFT)
75
76#define BUS_CTRL_ENDIAN_CONF_MASK \
77 (0x3 << MMIO_ENDIAN_SHIFT | 0x3 << DMADESC_ENDIAN_SHIFT | \
78 0x3 << DMADATA_ENDIAN_SHIFT | 0x3 << PIODATA_ENDIAN_SHIFT)
79
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -040080enum brcm_ahci_version {
81 BRCM_SATA_BCM7425 = 1,
82 BRCM_SATA_BCM7445,
83 BRCM_SATA_NSP,
84};
85
Jaedon Shin7de32442015-11-26 11:56:30 +090086enum brcm_ahci_quirks {
87 BRCM_AHCI_QUIRK_NO_NCQ = BIT(0),
Jaedon Shinb46f79b2015-11-26 11:56:31 +090088 BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(1),
Jaedon Shin7de32442015-11-26 11:56:30 +090089};
90
Brian Norris766a2d92015-05-12 16:28:21 -070091struct brcm_ahci_priv {
92 struct device *dev;
93 void __iomem *top_ctrl;
94 u32 port_mask;
Jaedon Shin7de32442015-11-26 11:56:30 +090095 u32 quirks;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -040096 enum brcm_ahci_version version;
Brian Norris766a2d92015-05-12 16:28:21 -070097};
98
99static const struct ata_port_info ahci_brcm_port_info = {
Danesh Petigara6ca92dd2016-01-07 16:03:31 -0800100 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
Danesh Petigarae39b2bb2016-01-07 16:03:33 -0800101 .link_flags = ATA_LFLAG_NO_DB_DELAY,
Brian Norris766a2d92015-05-12 16:28:21 -0700102 .pio_mask = ATA_PIO4,
103 .udma_mask = ATA_UDMA6,
104 .port_ops = &ahci_platform_ops,
105};
106
107static inline u32 brcm_sata_readreg(void __iomem *addr)
108{
109 /*
110 * MIPS endianness is configured by boot strap, which also reverses all
111 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
112 * endian I/O).
113 *
114 * Other architectures (e.g., ARM) either do not support big endian, or
115 * else leave I/O in little endian mode.
116 */
Axel Linf9114d32015-08-06 12:28:18 +0800117 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Brian Norris766a2d92015-05-12 16:28:21 -0700118 return __raw_readl(addr);
119 else
120 return readl_relaxed(addr);
121}
122
123static inline void brcm_sata_writereg(u32 val, void __iomem *addr)
124{
125 /* See brcm_sata_readreg() comments */
Axel Linf9114d32015-08-06 12:28:18 +0800126 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Brian Norris766a2d92015-05-12 16:28:21 -0700127 __raw_writel(val, addr);
128 else
129 writel_relaxed(val, addr);
130}
131
Danesh Petigara6863caa2016-01-07 16:03:30 -0800132static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv)
133{
134 struct brcm_ahci_priv *priv = hpriv->plat_data;
Doug Bergerda8fa9c2017-06-21 16:20:14 -0700135 u32 port_ctrl, host_caps;
Danesh Petigara6863caa2016-01-07 16:03:30 -0800136 int i;
137
138 /* Enable support for ALPM */
Danesh Petigara6863caa2016-01-07 16:03:30 -0800139 host_caps = readl(hpriv->mmio + HOST_CAP);
Doug Bergerda8fa9c2017-06-21 16:20:14 -0700140 if (!(host_caps & HOST_CAP_ALPM))
141 hpriv->flags |= AHCI_HFLAG_YES_ALPM;
Danesh Petigara6863caa2016-01-07 16:03:30 -0800142
143 /*
144 * Adjust timeout to allow PLL sufficient time to lock while waking
145 * up from slumber mode.
146 */
147 for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL;
148 i < SATA_TOP_MAX_PHYS;
149 i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) {
150 if (priv->port_mask & BIT(i))
151 writel(0xff1003fc,
152 hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl));
153 }
154}
155
Brian Norris766a2d92015-05-12 16:28:21 -0700156static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port)
157{
158 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
159 (port * SATA_TOP_CTRL_PHY_OFFS);
160 void __iomem *p;
161 u32 reg;
162
Jaedon Shinb46f79b2015-11-26 11:56:31 +0900163 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
164 return;
165
Brian Norris766a2d92015-05-12 16:28:21 -0700166 /* clear PHY_DEFAULT_POWER_STATE */
167 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
168 reg = brcm_sata_readreg(p);
169 reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
170 brcm_sata_writereg(reg, p);
171
172 /* reset the PHY digital logic */
173 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
174 reg = brcm_sata_readreg(p);
175 reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
176 SATA_TOP_CTRL_2_SW_RST_RX);
177 reg |= SATA_TOP_CTRL_2_SW_RST_TX;
178 brcm_sata_writereg(reg, p);
179 reg = brcm_sata_readreg(p);
180 reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
181 brcm_sata_writereg(reg, p);
182 reg = brcm_sata_readreg(p);
183 reg &= ~SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
184 brcm_sata_writereg(reg, p);
185 (void)brcm_sata_readreg(p);
186}
187
188static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port)
189{
190 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
191 (port * SATA_TOP_CTRL_PHY_OFFS);
192 void __iomem *p;
193 u32 reg;
194
Jaedon Shinb46f79b2015-11-26 11:56:31 +0900195 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
196 return;
197
Brian Norris766a2d92015-05-12 16:28:21 -0700198 /* power-off the PHY digital logic */
199 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
200 reg = brcm_sata_readreg(p);
201 reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
202 SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX |
203 SATA_TOP_CTRL_2_PHY_GLOBAL_RESET);
204 brcm_sata_writereg(reg, p);
205
206 /* set PHY_DEFAULT_POWER_STATE */
207 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
208 reg = brcm_sata_readreg(p);
209 reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
210 brcm_sata_writereg(reg, p);
211}
212
213static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv)
214{
215 int i;
216
217 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
218 if (priv->port_mask & BIT(i))
219 brcm_sata_phy_enable(priv, i);
220}
221
222static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv)
223{
224 int i;
225
226 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
227 if (priv->port_mask & BIT(i))
228 brcm_sata_phy_disable(priv, i);
229}
230
231static u32 brcm_ahci_get_portmask(struct platform_device *pdev,
232 struct brcm_ahci_priv *priv)
233{
234 void __iomem *ahci;
235 struct resource *res;
236 u32 impl;
237
238 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ahci");
239 ahci = devm_ioremap_resource(&pdev->dev, res);
240 if (IS_ERR(ahci))
241 return 0;
242
243 impl = readl(ahci + HOST_PORTS_IMPL);
244
245 if (fls(impl) > SATA_TOP_MAX_PHYS)
246 dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
247 impl);
248 else if (!impl)
249 dev_info(priv->dev, "no ports found\n");
250
251 devm_iounmap(&pdev->dev, ahci);
252 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
253
254 return impl;
255}
256
257static void brcm_sata_init(struct brcm_ahci_priv *priv)
258{
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400259 void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL;
Florian Fainelli36fffd62017-12-22 11:43:08 -0800260 u32 data;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400261
Brian Norris766a2d92015-05-12 16:28:21 -0700262 /* Configure endianness */
Florian Fainelli36fffd62017-12-22 11:43:08 -0800263 data = brcm_sata_readreg(ctrl);
264 data &= ~BUS_CTRL_ENDIAN_CONF_MASK;
265 if (priv->version == BRCM_SATA_NSP)
266 data |= BUS_CTRL_ENDIAN_NSP_CONF;
267 else
268 data |= BUS_CTRL_ENDIAN_CONF;
269 brcm_sata_writereg(data, ctrl);
Brian Norris766a2d92015-05-12 16:28:21 -0700270}
271
Florian Fainelli8b34fe52015-07-14 13:03:33 -0700272#ifdef CONFIG_PM_SLEEP
Brian Norris766a2d92015-05-12 16:28:21 -0700273static int brcm_ahci_suspend(struct device *dev)
274{
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;
279
280 ret = ahci_platform_suspend(dev);
281 brcm_sata_phys_disable(priv);
282 return ret;
283}
284
285static int brcm_ahci_resume(struct device *dev)
286{
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;
290
291 brcm_sata_init(priv);
292 brcm_sata_phys_enable(priv);
Danesh Petigara6863caa2016-01-07 16:03:30 -0800293 brcm_sata_alpm_init(hpriv);
Brian Norris766a2d92015-05-12 16:28:21 -0700294 return ahci_platform_resume(dev);
295}
Florian Fainelli8b34fe52015-07-14 13:03:33 -0700296#endif
Brian Norris766a2d92015-05-12 16:28:21 -0700297
298static struct scsi_host_template ahci_platform_sht = {
299 AHCI_SHT(DRV_NAME),
300};
301
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400302static 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},
306 {},
307};
308MODULE_DEVICE_TABLE(of, ahci_of_match);
309
Brian Norris766a2d92015-05-12 16:28:21 -0700310static int brcm_ahci_probe(struct platform_device *pdev)
311{
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400312 const struct of_device_id *of_id;
Brian Norris766a2d92015-05-12 16:28:21 -0700313 struct device *dev = &pdev->dev;
314 struct brcm_ahci_priv *priv;
315 struct ahci_host_priv *hpriv;
316 struct resource *res;
317 int ret;
318
319 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
320 if (!priv)
321 return -ENOMEM;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400322
323 of_id = of_match_node(ahci_of_match, pdev->dev.of_node);
324 if (!of_id)
325 return -ENODEV;
326
327 priv->version = (enum brcm_ahci_version)of_id->data;
Brian Norris766a2d92015-05-12 16:28:21 -0700328 priv->dev = dev;
329
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);
334
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400335 if ((priv->version == BRCM_SATA_BCM7425) ||
336 (priv->version == BRCM_SATA_NSP)) {
Jaedon Shin7de32442015-11-26 11:56:30 +0900337 priv->quirks |= BRCM_AHCI_QUIRK_NO_NCQ;
Jaedon Shinb46f79b2015-11-26 11:56:31 +0900338 priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
339 }
Jaedon Shin7de32442015-11-26 11:56:30 +0900340
Brian Norris766a2d92015-05-12 16:28:21 -0700341 brcm_sata_init(priv);
342
343 priv->port_mask = brcm_ahci_get_portmask(pdev, priv);
344 if (!priv->port_mask)
345 return -ENODEV;
346
347 brcm_sata_phys_enable(priv);
348
349 hpriv = ahci_platform_get_resources(pdev);
350 if (IS_ERR(hpriv))
351 return PTR_ERR(hpriv);
352 hpriv->plat_data = priv;
Danesh Petigarafb329632016-01-11 13:22:26 -0800353 hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP;
Brian Norris766a2d92015-05-12 16:28:21 -0700354
Danesh Petigara6863caa2016-01-07 16:03:30 -0800355 brcm_sata_alpm_init(hpriv);
356
Brian Norris766a2d92015-05-12 16:28:21 -0700357 ret = ahci_platform_enable_resources(hpriv);
358 if (ret)
359 return ret;
360
Jaedon Shin7de32442015-11-26 11:56:30 +0900361 if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ)
362 hpriv->flags |= AHCI_HFLAG_NO_NCQ;
Doug Bergerda8fa9c2017-06-21 16:20:14 -0700363 hpriv->flags |= AHCI_HFLAG_NO_WRITE_TO_RO;
Jaedon Shin7de32442015-11-26 11:56:30 +0900364
Brian Norris766a2d92015-05-12 16:28:21 -0700365 ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
366 &ahci_platform_sht);
367 if (ret)
368 return ret;
369
370 dev_info(dev, "Broadcom AHCI SATA3 registered\n");
371
372 return 0;
373}
374
375static int brcm_ahci_remove(struct platform_device *pdev)
376{
377 struct ata_host *host = dev_get_drvdata(&pdev->dev);
378 struct ahci_host_priv *hpriv = host->private_data;
379 struct brcm_ahci_priv *priv = hpriv->plat_data;
380 int ret;
381
382 ret = ata_platform_remove_one(pdev);
383 if (ret)
384 return ret;
385
386 brcm_sata_phys_disable(priv);
387
388 return 0;
389}
390
Brian Norris766a2d92015-05-12 16:28:21 -0700391static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume);
392
393static struct platform_driver brcm_ahci_driver = {
394 .probe = brcm_ahci_probe,
395 .remove = brcm_ahci_remove,
396 .driver = {
397 .name = DRV_NAME,
398 .of_match_table = ahci_of_match,
399 .pm = &ahci_brcm_pm_ops,
400 },
401};
402module_platform_driver(brcm_ahci_driver);
403
404MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver");
405MODULE_AUTHOR("Brian Norris");
406MODULE_LICENSE("GPL");
407MODULE_ALIAS("platform:sata-brcmstb");