blob: 58e1a6e5478d972142b2507ee03a15f31c38ad46 [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Brian Norris766a2d92015-05-12 16:28:21 -07002/*
3 * Broadcom SATA3 AHCI Controller Driver
4 *
5 * Copyright © 2009-2015 Broadcom Corporation
Brian Norris766a2d92015-05-12 16:28:21 -07006 */
7
8#include <linux/ahci_platform.h>
9#include <linux/compiler.h>
10#include <linux/device.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/libata.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
Florian Fainelli2b2c47d2018-10-01 10:33:00 -070019#include <linux/reset.h>
Brian Norris766a2d92015-05-12 16:28:21 -070020#include <linux/string.h>
21
22#include "ahci.h"
23
24#define DRV_NAME "brcm-ahci"
25
26#define SATA_TOP_CTRL_VERSION 0x0
27#define SATA_TOP_CTRL_BUS_CTRL 0x4
28 #define MMIO_ENDIAN_SHIFT 0 /* CPU->AHCI */
29 #define DMADESC_ENDIAN_SHIFT 2 /* AHCI->DDR */
30 #define DMADATA_ENDIAN_SHIFT 4 /* AHCI->DDR */
31 #define PIODATA_ENDIAN_SHIFT 6
32 #define ENDIAN_SWAP_NONE 0
33 #define ENDIAN_SWAP_FULL 2
Brian Norris766a2d92015-05-12 16:28:21 -070034#define SATA_TOP_CTRL_TP_CTRL 0x8
35#define SATA_TOP_CTRL_PHY_CTRL 0xc
36 #define SATA_TOP_CTRL_PHY_CTRL_1 0x0
37 #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14)
38 #define SATA_TOP_CTRL_PHY_CTRL_2 0x4
39 #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0)
40 #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1)
41 #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2)
42 #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3)
43 #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14)
44 #define SATA_TOP_CTRL_PHY_OFFS 0x8
45 #define SATA_TOP_MAX_PHYS 2
Brian Norris766a2d92015-05-12 16:28:21 -070046
Danesh Petigara6863caa2016-01-07 16:03:30 -080047#define SATA_FIRST_PORT_CTRL 0x700
48#define SATA_NEXT_PORT_CTRL_OFFSET 0x80
49#define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18)
50
Brian Norris766a2d92015-05-12 16:28:21 -070051/* On big-endian MIPS, buses are reversed to big endian, so switch them back */
52#if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
53#define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */
54#define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */
55#else
56#define DATA_ENDIAN 0
57#define MMIO_ENDIAN 0
58#endif
59
60#define BUS_CTRL_ENDIAN_CONF \
61 ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \
62 (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \
63 (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT))
64
Florian Fainelli36fffd62017-12-22 11:43:08 -080065#define BUS_CTRL_ENDIAN_NSP_CONF \
66 (0x02 << DMADATA_ENDIAN_SHIFT | 0x02 << DMADESC_ENDIAN_SHIFT)
67
68#define BUS_CTRL_ENDIAN_CONF_MASK \
69 (0x3 << MMIO_ENDIAN_SHIFT | 0x3 << DMADESC_ENDIAN_SHIFT | \
70 0x3 << DMADATA_ENDIAN_SHIFT | 0x3 << PIODATA_ENDIAN_SHIFT)
71
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -040072enum brcm_ahci_version {
73 BRCM_SATA_BCM7425 = 1,
74 BRCM_SATA_BCM7445,
75 BRCM_SATA_NSP,
76};
77
Jaedon Shin7de32442015-11-26 11:56:30 +090078enum brcm_ahci_quirks {
Florian Fainelli1a3d78c2019-12-10 10:53:46 -080079 BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(0),
Jaedon Shin7de32442015-11-26 11:56:30 +090080};
81
Brian Norris766a2d92015-05-12 16:28:21 -070082struct brcm_ahci_priv {
83 struct device *dev;
84 void __iomem *top_ctrl;
85 u32 port_mask;
Jaedon Shin7de32442015-11-26 11:56:30 +090086 u32 quirks;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -040087 enum brcm_ahci_version version;
Florian Fainelli2b2c47d2018-10-01 10:33:00 -070088 struct reset_control *rcdev;
Brian Norris766a2d92015-05-12 16:28:21 -070089};
90
Brian Norris766a2d92015-05-12 16:28:21 -070091static inline u32 brcm_sata_readreg(void __iomem *addr)
92{
93 /*
94 * MIPS endianness is configured by boot strap, which also reverses all
95 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
96 * endian I/O).
97 *
98 * Other architectures (e.g., ARM) either do not support big endian, or
99 * else leave I/O in little endian mode.
100 */
Axel Linf9114d32015-08-06 12:28:18 +0800101 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Brian Norris766a2d92015-05-12 16:28:21 -0700102 return __raw_readl(addr);
103 else
104 return readl_relaxed(addr);
105}
106
107static inline void brcm_sata_writereg(u32 val, void __iomem *addr)
108{
109 /* See brcm_sata_readreg() comments */
Axel Linf9114d32015-08-06 12:28:18 +0800110 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Brian Norris766a2d92015-05-12 16:28:21 -0700111 __raw_writel(val, addr);
112 else
113 writel_relaxed(val, addr);
114}
115
Danesh Petigara6863caa2016-01-07 16:03:30 -0800116static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv)
117{
118 struct brcm_ahci_priv *priv = hpriv->plat_data;
Doug Bergerda8fa9c2017-06-21 16:20:14 -0700119 u32 port_ctrl, host_caps;
Danesh Petigara6863caa2016-01-07 16:03:30 -0800120 int i;
121
122 /* Enable support for ALPM */
Danesh Petigara6863caa2016-01-07 16:03:30 -0800123 host_caps = readl(hpriv->mmio + HOST_CAP);
Doug Bergerda8fa9c2017-06-21 16:20:14 -0700124 if (!(host_caps & HOST_CAP_ALPM))
125 hpriv->flags |= AHCI_HFLAG_YES_ALPM;
Danesh Petigara6863caa2016-01-07 16:03:30 -0800126
127 /*
128 * Adjust timeout to allow PLL sufficient time to lock while waking
129 * up from slumber mode.
130 */
131 for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL;
132 i < SATA_TOP_MAX_PHYS;
133 i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) {
134 if (priv->port_mask & BIT(i))
135 writel(0xff1003fc,
136 hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl));
137 }
138}
139
Brian Norris766a2d92015-05-12 16:28:21 -0700140static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port)
141{
142 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
143 (port * SATA_TOP_CTRL_PHY_OFFS);
144 void __iomem *p;
145 u32 reg;
146
Jaedon Shinb46f79b2015-11-26 11:56:31 +0900147 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
148 return;
149
Brian Norris766a2d92015-05-12 16:28:21 -0700150 /* clear PHY_DEFAULT_POWER_STATE */
151 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
152 reg = brcm_sata_readreg(p);
153 reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
154 brcm_sata_writereg(reg, p);
155
156 /* reset the PHY digital logic */
157 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
158 reg = brcm_sata_readreg(p);
159 reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
160 SATA_TOP_CTRL_2_SW_RST_RX);
161 reg |= SATA_TOP_CTRL_2_SW_RST_TX;
162 brcm_sata_writereg(reg, p);
163 reg = brcm_sata_readreg(p);
164 reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
165 brcm_sata_writereg(reg, p);
166 reg = brcm_sata_readreg(p);
167 reg &= ~SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
168 brcm_sata_writereg(reg, p);
169 (void)brcm_sata_readreg(p);
170}
171
172static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port)
173{
174 void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
175 (port * SATA_TOP_CTRL_PHY_OFFS);
176 void __iomem *p;
177 u32 reg;
178
Jaedon Shinb46f79b2015-11-26 11:56:31 +0900179 if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
180 return;
181
Brian Norris766a2d92015-05-12 16:28:21 -0700182 /* power-off the PHY digital logic */
183 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
184 reg = brcm_sata_readreg(p);
185 reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
186 SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX |
187 SATA_TOP_CTRL_2_PHY_GLOBAL_RESET);
188 brcm_sata_writereg(reg, p);
189
190 /* set PHY_DEFAULT_POWER_STATE */
191 p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
192 reg = brcm_sata_readreg(p);
193 reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
194 brcm_sata_writereg(reg, p);
195}
196
197static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv)
198{
199 int i;
200
201 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
202 if (priv->port_mask & BIT(i))
203 brcm_sata_phy_enable(priv, i);
204}
205
206static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv)
207{
208 int i;
209
210 for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
211 if (priv->port_mask & BIT(i))
212 brcm_sata_phy_disable(priv, i);
213}
214
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800215static u32 brcm_ahci_get_portmask(struct ahci_host_priv *hpriv,
Brian Norris766a2d92015-05-12 16:28:21 -0700216 struct brcm_ahci_priv *priv)
217{
Brian Norris766a2d92015-05-12 16:28:21 -0700218 u32 impl;
219
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800220 impl = readl(hpriv->mmio + HOST_PORTS_IMPL);
Brian Norris766a2d92015-05-12 16:28:21 -0700221
222 if (fls(impl) > SATA_TOP_MAX_PHYS)
223 dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
224 impl);
225 else if (!impl)
226 dev_info(priv->dev, "no ports found\n");
227
Brian Norris766a2d92015-05-12 16:28:21 -0700228 return impl;
229}
230
231static void brcm_sata_init(struct brcm_ahci_priv *priv)
232{
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400233 void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL;
Florian Fainelli36fffd62017-12-22 11:43:08 -0800234 u32 data;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400235
Brian Norris766a2d92015-05-12 16:28:21 -0700236 /* Configure endianness */
Florian Fainelli36fffd62017-12-22 11:43:08 -0800237 data = brcm_sata_readreg(ctrl);
238 data &= ~BUS_CTRL_ENDIAN_CONF_MASK;
239 if (priv->version == BRCM_SATA_NSP)
240 data |= BUS_CTRL_ENDIAN_NSP_CONF;
241 else
242 data |= BUS_CTRL_ENDIAN_CONF;
243 brcm_sata_writereg(data, ctrl);
Brian Norris766a2d92015-05-12 16:28:21 -0700244}
245
Florian Fainellieb733902018-01-11 17:31:08 -0800246static unsigned int brcm_ahci_read_id(struct ata_device *dev,
247 struct ata_taskfile *tf, u16 *id)
248{
249 struct ata_port *ap = dev->link->ap;
250 struct ata_host *host = ap->host;
251 struct ahci_host_priv *hpriv = host->private_data;
252 struct brcm_ahci_priv *priv = hpriv->plat_data;
253 void __iomem *mmio = hpriv->mmio;
254 unsigned int err_mask;
255 unsigned long flags;
256 int i, rc;
257 u32 ctl;
258
259 /* Try to read the device ID and, if this fails, proceed with the
260 * recovery sequence below
261 */
262 err_mask = ata_do_dev_read_id(dev, tf, id);
263 if (likely(!err_mask))
264 return err_mask;
265
266 /* Disable host interrupts */
267 spin_lock_irqsave(&host->lock, flags);
268 ctl = readl(mmio + HOST_CTL);
269 ctl &= ~HOST_IRQ_EN;
270 writel(ctl, mmio + HOST_CTL);
271 readl(mmio + HOST_CTL); /* flush */
272 spin_unlock_irqrestore(&host->lock, flags);
273
274 /* Perform the SATA PHY reset sequence */
275 brcm_sata_phy_disable(priv, ap->port_no);
276
Florian Fainellibf0e5012019-12-10 10:53:47 -0800277 /* Reset the SATA clock */
278 ahci_platform_disable_clks(hpriv);
279 msleep(10);
280
281 ahci_platform_enable_clks(hpriv);
282 msleep(10);
283
Florian Fainellieb733902018-01-11 17:31:08 -0800284 /* Bring the PHY back on */
285 brcm_sata_phy_enable(priv, ap->port_no);
286
287 /* Re-initialize and calibrate the PHY */
288 for (i = 0; i < hpriv->nports; i++) {
289 rc = phy_init(hpriv->phys[i]);
290 if (rc)
291 goto disable_phys;
292
293 rc = phy_calibrate(hpriv->phys[i]);
294 if (rc) {
295 phy_exit(hpriv->phys[i]);
296 goto disable_phys;
297 }
298 }
299
300 /* Re-enable host interrupts */
301 spin_lock_irqsave(&host->lock, flags);
302 ctl = readl(mmio + HOST_CTL);
303 ctl |= HOST_IRQ_EN;
304 writel(ctl, mmio + HOST_CTL);
305 readl(mmio + HOST_CTL); /* flush */
306 spin_unlock_irqrestore(&host->lock, flags);
307
308 return ata_do_dev_read_id(dev, tf, id);
309
310disable_phys:
311 while (--i >= 0) {
312 phy_power_off(hpriv->phys[i]);
313 phy_exit(hpriv->phys[i]);
314 }
315
316 return AC_ERR_OTHER;
317}
318
319static void brcm_ahci_host_stop(struct ata_host *host)
320{
321 struct ahci_host_priv *hpriv = host->private_data;
322
323 ahci_platform_disable_resources(hpriv);
324}
325
326static struct ata_port_operations ahci_brcm_platform_ops = {
327 .inherits = &ahci_ops,
328 .host_stop = brcm_ahci_host_stop,
329 .read_id = brcm_ahci_read_id,
330};
331
332static const struct ata_port_info ahci_brcm_port_info = {
333 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
334 .link_flags = ATA_LFLAG_NO_DB_DELAY,
335 .pio_mask = ATA_PIO4,
336 .udma_mask = ATA_UDMA6,
337 .port_ops = &ahci_brcm_platform_ops,
338};
339
Florian Fainelli8b34fe52015-07-14 13:03:33 -0700340#ifdef CONFIG_PM_SLEEP
Brian Norris766a2d92015-05-12 16:28:21 -0700341static int brcm_ahci_suspend(struct device *dev)
342{
343 struct ata_host *host = dev_get_drvdata(dev);
344 struct ahci_host_priv *hpriv = host->private_data;
345 struct brcm_ahci_priv *priv = hpriv->plat_data;
Florian Fainelli3c696ac42019-12-10 10:53:48 -0800346 int ret;
Brian Norris766a2d92015-05-12 16:28:21 -0700347
Brian Norris766a2d92015-05-12 16:28:21 -0700348 brcm_sata_phys_disable(priv);
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800349
Florian Fainelli3c696ac42019-12-10 10:53:48 -0800350 ret = ahci_platform_suspend(dev);
351
352 if (!IS_ERR_OR_NULL(priv->rcdev))
353 reset_control_assert(priv->rcdev);
354
355 return ret;
Brian Norris766a2d92015-05-12 16:28:21 -0700356}
357
358static int brcm_ahci_resume(struct device *dev)
359{
360 struct ata_host *host = dev_get_drvdata(dev);
361 struct ahci_host_priv *hpriv = host->private_data;
362 struct brcm_ahci_priv *priv = hpriv->plat_data;
Florian Fainelli3c696ac42019-12-10 10:53:48 -0800363 int ret = 0;
364
365 if (!IS_ERR_OR_NULL(priv->rcdev))
366 ret = reset_control_deassert(priv->rcdev);
367 if (ret)
368 return ret;
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800369
370 /* Make sure clocks are turned on before re-configuration */
371 ret = ahci_platform_enable_clks(hpriv);
372 if (ret)
373 return ret;
Brian Norris766a2d92015-05-12 16:28:21 -0700374
375 brcm_sata_init(priv);
376 brcm_sata_phys_enable(priv);
Danesh Petigara6863caa2016-01-07 16:03:30 -0800377 brcm_sata_alpm_init(hpriv);
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800378
379 /* Since we had to enable clocks earlier on, we cannot use
380 * ahci_platform_resume() as-is since a second call to
381 * ahci_platform_enable_resources() would bump up the resources
382 * (regulators, clocks, PHYs) count artificially so we copy the part
383 * after ahci_platform_enable_resources().
384 */
385 ret = ahci_platform_enable_phys(hpriv);
386 if (ret)
387 goto out_disable_phys;
388
389 ret = ahci_platform_resume_host(dev);
390 if (ret)
391 goto out_disable_platform_phys;
392
393 /* We resumed so update PM runtime state */
394 pm_runtime_disable(dev);
395 pm_runtime_set_active(dev);
396 pm_runtime_enable(dev);
397
398 return 0;
399
400out_disable_platform_phys:
401 ahci_platform_disable_phys(hpriv);
402out_disable_phys:
403 brcm_sata_phys_disable(priv);
404 ahci_platform_disable_clks(hpriv);
405 return ret;
Brian Norris766a2d92015-05-12 16:28:21 -0700406}
Florian Fainelli8b34fe52015-07-14 13:03:33 -0700407#endif
Brian Norris766a2d92015-05-12 16:28:21 -0700408
409static struct scsi_host_template ahci_platform_sht = {
410 AHCI_SHT(DRV_NAME),
411};
412
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400413static const struct of_device_id ahci_of_match[] = {
414 {.compatible = "brcm,bcm7425-ahci", .data = (void *)BRCM_SATA_BCM7425},
415 {.compatible = "brcm,bcm7445-ahci", .data = (void *)BRCM_SATA_BCM7445},
Florian Fainellifb8506f2018-10-01 10:33:01 -0700416 {.compatible = "brcm,bcm63138-ahci", .data = (void *)BRCM_SATA_BCM7445},
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400417 {.compatible = "brcm,bcm-nsp-ahci", .data = (void *)BRCM_SATA_NSP},
418 {},
419};
420MODULE_DEVICE_TABLE(of, ahci_of_match);
421
Brian Norris766a2d92015-05-12 16:28:21 -0700422static int brcm_ahci_probe(struct platform_device *pdev)
423{
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400424 const struct of_device_id *of_id;
Brian Norris766a2d92015-05-12 16:28:21 -0700425 struct device *dev = &pdev->dev;
426 struct brcm_ahci_priv *priv;
427 struct ahci_host_priv *hpriv;
428 struct resource *res;
429 int ret;
430
431 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
432 if (!priv)
433 return -ENOMEM;
Yendapally Reddy Dhananjaya Reddy3ee2e6d2016-06-16 09:53:33 -0400434
435 of_id = of_match_node(ahci_of_match, pdev->dev.of_node);
436 if (!of_id)
437 return -ENODEV;
438
439 priv->version = (enum brcm_ahci_version)of_id->data;
Brian Norris766a2d92015-05-12 16:28:21 -0700440 priv->dev = dev;
441
442 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "top-ctrl");
443 priv->top_ctrl = devm_ioremap_resource(dev, res);
444 if (IS_ERR(priv->top_ctrl))
445 return PTR_ERR(priv->top_ctrl);
446
Florian Fainelli2b2c47d2018-10-01 10:33:00 -0700447 /* Reset is optional depending on platform */
448 priv->rcdev = devm_reset_control_get(&pdev->dev, "ahci");
449 if (!IS_ERR_OR_NULL(priv->rcdev))
450 reset_control_deassert(priv->rcdev);
451
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800452 hpriv = ahci_platform_get_resources(pdev, 0);
453 if (IS_ERR(hpriv)) {
454 ret = PTR_ERR(hpriv);
455 goto out_reset;
456 }
457
Florian Fainelli1a3d78c2019-12-10 10:53:46 -0800458 hpriv->plat_data = priv;
459 hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP | AHCI_HFLAG_NO_WRITE_TO_RO;
460
461 switch (priv->version) {
462 case BRCM_SATA_BCM7425:
463 hpriv->flags |= AHCI_HFLAG_DELAY_ENGINE;
464 /* fall through */
465 case BRCM_SATA_NSP:
466 hpriv->flags |= AHCI_HFLAG_NO_NCQ;
467 priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
468 break;
469 default:
470 break;
471 }
472
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800473 ret = ahci_platform_enable_clks(hpriv);
474 if (ret)
475 goto out_reset;
476
477 /* Must be first so as to configure endianness including that
478 * of the standard AHCI register space.
479 */
Brian Norris766a2d92015-05-12 16:28:21 -0700480 brcm_sata_init(priv);
481
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800482 /* Initializes priv->port_mask which is used below */
483 priv->port_mask = brcm_ahci_get_portmask(hpriv, priv);
484 if (!priv->port_mask) {
485 ret = -ENODEV;
486 goto out_disable_clks;
487 }
Brian Norris766a2d92015-05-12 16:28:21 -0700488
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800489 /* Must be done before ahci_platform_enable_phys() */
Brian Norris766a2d92015-05-12 16:28:21 -0700490 brcm_sata_phys_enable(priv);
491
Danesh Petigara6863caa2016-01-07 16:03:30 -0800492 brcm_sata_alpm_init(hpriv);
493
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800494 ret = ahci_platform_enable_phys(hpriv);
495 if (ret)
496 goto out_disable_phys;
497
Brian Norris766a2d92015-05-12 16:28:21 -0700498 ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
499 &ahci_platform_sht);
500 if (ret)
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800501 goto out_disable_platform_phys;
Brian Norris766a2d92015-05-12 16:28:21 -0700502
503 dev_info(dev, "Broadcom AHCI SATA3 registered\n");
504
505 return 0;
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800506
507out_disable_platform_phys:
508 ahci_platform_disable_phys(hpriv);
509out_disable_phys:
510 brcm_sata_phys_disable(priv);
511out_disable_clks:
512 ahci_platform_disable_clks(hpriv);
513out_reset:
514 if (!IS_ERR_OR_NULL(priv->rcdev))
515 reset_control_assert(priv->rcdev);
516 return ret;
Brian Norris766a2d92015-05-12 16:28:21 -0700517}
518
519static int brcm_ahci_remove(struct platform_device *pdev)
520{
521 struct ata_host *host = dev_get_drvdata(&pdev->dev);
522 struct ahci_host_priv *hpriv = host->private_data;
523 struct brcm_ahci_priv *priv = hpriv->plat_data;
524 int ret;
525
Florian Fainellic0cdf2a2019-12-10 10:53:45 -0800526 brcm_sata_phys_disable(priv);
527
Brian Norris766a2d92015-05-12 16:28:21 -0700528 ret = ata_platform_remove_one(pdev);
529 if (ret)
530 return ret;
531
Brian Norris766a2d92015-05-12 16:28:21 -0700532 return 0;
533}
534
Florian Fainelli7de9b162019-12-10 10:53:49 -0800535static void brcm_ahci_shutdown(struct platform_device *pdev)
536{
537 int ret;
538
539 /* All resources releasing happens via devres, but our device, unlike a
540 * proper remove is not disappearing, therefore using
541 * brcm_ahci_suspend() here which does explicit power management is
542 * appropriate.
543 */
544 ret = brcm_ahci_suspend(&pdev->dev);
545 if (ret)
546 dev_err(&pdev->dev, "failed to shutdown\n");
547}
548
Brian Norris766a2d92015-05-12 16:28:21 -0700549static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume);
550
551static struct platform_driver brcm_ahci_driver = {
552 .probe = brcm_ahci_probe,
553 .remove = brcm_ahci_remove,
Florian Fainelli7de9b162019-12-10 10:53:49 -0800554 .shutdown = brcm_ahci_shutdown,
Brian Norris766a2d92015-05-12 16:28:21 -0700555 .driver = {
556 .name = DRV_NAME,
557 .of_match_table = ahci_of_match,
558 .pm = &ahci_brcm_pm_ops,
559 },
560};
561module_platform_driver(brcm_ahci_driver);
562
563MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver");
564MODULE_AUTHOR("Brian Norris");
565MODULE_LICENSE("GPL");
566MODULE_ALIAS("platform:sata-brcmstb");