xref: /openbmc/linux/drivers/mmc/host/sdhci-brcmstb.c (revision d46ba2d1)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2476bf3d6SAl Cooper /*
3476bf3d6SAl Cooper  * sdhci-brcmstb.c Support for SDHCI on Broadcom BRCMSTB SoC's
4476bf3d6SAl Cooper  *
5476bf3d6SAl Cooper  * Copyright (C) 2015 Broadcom Corporation
6476bf3d6SAl Cooper  */
7476bf3d6SAl Cooper 
8476bf3d6SAl Cooper #include <linux/io.h>
9476bf3d6SAl Cooper #include <linux/mmc/host.h>
10476bf3d6SAl Cooper #include <linux/module.h>
11476bf3d6SAl Cooper #include <linux/of.h>
1278ab82fdSAl Cooper #include <linux/bitops.h>
13*d46ba2d1SAl Cooper #include <linux/delay.h>
14476bf3d6SAl Cooper 
15476bf3d6SAl Cooper #include "sdhci-pltfm.h"
16*d46ba2d1SAl Cooper #include "cqhci.h"
17476bf3d6SAl Cooper 
1878ab82fdSAl Cooper #define SDHCI_VENDOR 0x78
1978ab82fdSAl Cooper #define  SDHCI_VENDOR_ENHANCED_STRB 0x1
2078ab82fdSAl Cooper 
2178ab82fdSAl Cooper #define BRCMSTB_PRIV_FLAGS_NO_64BIT		BIT(0)
2278ab82fdSAl Cooper #define BRCMSTB_PRIV_FLAGS_BROKEN_TIMEOUT	BIT(1)
2378ab82fdSAl Cooper 
24*d46ba2d1SAl Cooper #define SDHCI_ARASAN_CQE_BASE_ADDR		0x200
25*d46ba2d1SAl Cooper 
2678ab82fdSAl Cooper struct sdhci_brcmstb_priv {
2778ab82fdSAl Cooper 	void __iomem *cfg_regs;
28*d46ba2d1SAl Cooper 	bool has_cqe;
2978ab82fdSAl Cooper };
3078ab82fdSAl Cooper 
3178ab82fdSAl Cooper struct brcmstb_match_priv {
3278ab82fdSAl Cooper 	void (*hs400es)(struct mmc_host *mmc, struct mmc_ios *ios);
33*d46ba2d1SAl Cooper 	struct sdhci_ops *ops;
3478ab82fdSAl Cooper 	unsigned int flags;
3578ab82fdSAl Cooper };
3678ab82fdSAl Cooper 
3778ab82fdSAl Cooper static void sdhci_brcmstb_hs400es(struct mmc_host *mmc, struct mmc_ios *ios)
3878ab82fdSAl Cooper {
3978ab82fdSAl Cooper 	struct sdhci_host *host = mmc_priv(mmc);
4078ab82fdSAl Cooper 
4178ab82fdSAl Cooper 	u32 reg;
4278ab82fdSAl Cooper 
4378ab82fdSAl Cooper 	dev_dbg(mmc_dev(mmc), "%s(): Setting HS400-Enhanced-Strobe mode\n",
4478ab82fdSAl Cooper 		__func__);
4578ab82fdSAl Cooper 	reg = readl(host->ioaddr + SDHCI_VENDOR);
4678ab82fdSAl Cooper 	if (ios->enhanced_strobe)
4778ab82fdSAl Cooper 		reg |= SDHCI_VENDOR_ENHANCED_STRB;
4878ab82fdSAl Cooper 	else
4978ab82fdSAl Cooper 		reg &= ~SDHCI_VENDOR_ENHANCED_STRB;
5078ab82fdSAl Cooper 	writel(reg, host->ioaddr + SDHCI_VENDOR);
5178ab82fdSAl Cooper }
5278ab82fdSAl Cooper 
53*d46ba2d1SAl Cooper static void sdhci_brcmstb_set_clock(struct sdhci_host *host, unsigned int clock)
54*d46ba2d1SAl Cooper {
55*d46ba2d1SAl Cooper 	u16 clk;
56*d46ba2d1SAl Cooper 
57*d46ba2d1SAl Cooper 	host->mmc->actual_clock = 0;
58*d46ba2d1SAl Cooper 
59*d46ba2d1SAl Cooper 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
60*d46ba2d1SAl Cooper 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
61*d46ba2d1SAl Cooper 
62*d46ba2d1SAl Cooper 	if (clock == 0)
63*d46ba2d1SAl Cooper 		return;
64*d46ba2d1SAl Cooper 
65*d46ba2d1SAl Cooper 	sdhci_enable_clk(host, clk);
66*d46ba2d1SAl Cooper }
67*d46ba2d1SAl Cooper 
68*d46ba2d1SAl Cooper static void sdhci_brcmstb_dumpregs(struct mmc_host *mmc)
69*d46ba2d1SAl Cooper {
70*d46ba2d1SAl Cooper 	sdhci_dumpregs(mmc_priv(mmc));
71*d46ba2d1SAl Cooper }
72*d46ba2d1SAl Cooper 
73*d46ba2d1SAl Cooper static void sdhci_brcmstb_cqe_enable(struct mmc_host *mmc)
74*d46ba2d1SAl Cooper {
75*d46ba2d1SAl Cooper 	struct sdhci_host *host = mmc_priv(mmc);
76*d46ba2d1SAl Cooper 	u32 reg;
77*d46ba2d1SAl Cooper 
78*d46ba2d1SAl Cooper 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
79*d46ba2d1SAl Cooper 	while (reg & SDHCI_DATA_AVAILABLE) {
80*d46ba2d1SAl Cooper 		sdhci_readl(host, SDHCI_BUFFER);
81*d46ba2d1SAl Cooper 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
82*d46ba2d1SAl Cooper 	}
83*d46ba2d1SAl Cooper 
84*d46ba2d1SAl Cooper 	sdhci_cqe_enable(mmc);
85*d46ba2d1SAl Cooper }
86*d46ba2d1SAl Cooper 
87*d46ba2d1SAl Cooper static const struct cqhci_host_ops sdhci_brcmstb_cqhci_ops = {
88*d46ba2d1SAl Cooper 	.enable         = sdhci_brcmstb_cqe_enable,
89*d46ba2d1SAl Cooper 	.disable        = sdhci_cqe_disable,
90*d46ba2d1SAl Cooper 	.dumpregs       = sdhci_brcmstb_dumpregs,
91*d46ba2d1SAl Cooper };
92*d46ba2d1SAl Cooper 
93*d46ba2d1SAl Cooper static struct sdhci_ops sdhci_brcmstb_ops = {
94476bf3d6SAl Cooper 	.set_clock = sdhci_set_clock,
95476bf3d6SAl Cooper 	.set_bus_width = sdhci_set_bus_width,
96476bf3d6SAl Cooper 	.reset = sdhci_reset,
97476bf3d6SAl Cooper 	.set_uhs_signaling = sdhci_set_uhs_signaling,
98476bf3d6SAl Cooper };
99476bf3d6SAl Cooper 
100*d46ba2d1SAl Cooper static struct sdhci_ops sdhci_brcmstb_ops_7216 = {
101*d46ba2d1SAl Cooper 	.set_clock = sdhci_brcmstb_set_clock,
102*d46ba2d1SAl Cooper 	.set_bus_width = sdhci_set_bus_width,
103*d46ba2d1SAl Cooper 	.reset = sdhci_reset,
104*d46ba2d1SAl Cooper 	.set_uhs_signaling = sdhci_set_uhs_signaling,
105*d46ba2d1SAl Cooper };
106*d46ba2d1SAl Cooper 
107*d46ba2d1SAl Cooper static struct brcmstb_match_priv match_priv_7425 = {
108*d46ba2d1SAl Cooper 	.flags = BRCMSTB_PRIV_FLAGS_NO_64BIT |
109*d46ba2d1SAl Cooper 	BRCMSTB_PRIV_FLAGS_BROKEN_TIMEOUT,
110476bf3d6SAl Cooper 	.ops = &sdhci_brcmstb_ops,
111476bf3d6SAl Cooper };
112476bf3d6SAl Cooper 
113*d46ba2d1SAl Cooper static struct brcmstb_match_priv match_priv_7445 = {
11478ab82fdSAl Cooper 	.flags = BRCMSTB_PRIV_FLAGS_BROKEN_TIMEOUT,
115*d46ba2d1SAl Cooper 	.ops = &sdhci_brcmstb_ops,
11678ab82fdSAl Cooper };
11778ab82fdSAl Cooper 
11878ab82fdSAl Cooper static const struct brcmstb_match_priv match_priv_7216 = {
11978ab82fdSAl Cooper 	.hs400es = sdhci_brcmstb_hs400es,
120*d46ba2d1SAl Cooper 	.ops = &sdhci_brcmstb_ops_7216,
12178ab82fdSAl Cooper };
12278ab82fdSAl Cooper 
12378ab82fdSAl Cooper static const struct of_device_id sdhci_brcm_of_match[] = {
12478ab82fdSAl Cooper 	{ .compatible = "brcm,bcm7425-sdhci", .data = &match_priv_7425 },
12578ab82fdSAl Cooper 	{ .compatible = "brcm,bcm7445-sdhci", .data = &match_priv_7445 },
12678ab82fdSAl Cooper 	{ .compatible = "brcm,bcm7216-sdhci", .data = &match_priv_7216 },
12778ab82fdSAl Cooper 	{},
12878ab82fdSAl Cooper };
12978ab82fdSAl Cooper 
130*d46ba2d1SAl Cooper static u32 sdhci_brcmstb_cqhci_irq(struct sdhci_host *host, u32 intmask)
131*d46ba2d1SAl Cooper {
132*d46ba2d1SAl Cooper 	int cmd_error = 0;
133*d46ba2d1SAl Cooper 	int data_error = 0;
134*d46ba2d1SAl Cooper 
135*d46ba2d1SAl Cooper 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
136*d46ba2d1SAl Cooper 		return intmask;
137*d46ba2d1SAl Cooper 
138*d46ba2d1SAl Cooper 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
139*d46ba2d1SAl Cooper 
140*d46ba2d1SAl Cooper 	return 0;
141*d46ba2d1SAl Cooper }
142*d46ba2d1SAl Cooper 
143*d46ba2d1SAl Cooper static int sdhci_brcmstb_add_host(struct sdhci_host *host,
144*d46ba2d1SAl Cooper 				  struct sdhci_brcmstb_priv *priv)
145*d46ba2d1SAl Cooper {
146*d46ba2d1SAl Cooper 	struct cqhci_host *cq_host;
147*d46ba2d1SAl Cooper 	bool dma64;
148*d46ba2d1SAl Cooper 	int ret;
149*d46ba2d1SAl Cooper 
150*d46ba2d1SAl Cooper 	if (!priv->has_cqe)
151*d46ba2d1SAl Cooper 		return sdhci_add_host(host);
152*d46ba2d1SAl Cooper 
153*d46ba2d1SAl Cooper 	dev_dbg(mmc_dev(host->mmc), "CQE is enabled\n");
154*d46ba2d1SAl Cooper 	host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
155*d46ba2d1SAl Cooper 	ret = sdhci_setup_host(host);
156*d46ba2d1SAl Cooper 	if (ret)
157*d46ba2d1SAl Cooper 		return ret;
158*d46ba2d1SAl Cooper 
159*d46ba2d1SAl Cooper 	cq_host = devm_kzalloc(mmc_dev(host->mmc),
160*d46ba2d1SAl Cooper 			       sizeof(*cq_host), GFP_KERNEL);
161*d46ba2d1SAl Cooper 	if (!cq_host) {
162*d46ba2d1SAl Cooper 		ret = -ENOMEM;
163*d46ba2d1SAl Cooper 		goto cleanup;
164*d46ba2d1SAl Cooper 	}
165*d46ba2d1SAl Cooper 
166*d46ba2d1SAl Cooper 	cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
167*d46ba2d1SAl Cooper 	cq_host->ops = &sdhci_brcmstb_cqhci_ops;
168*d46ba2d1SAl Cooper 
169*d46ba2d1SAl Cooper 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
170*d46ba2d1SAl Cooper 	if (dma64) {
171*d46ba2d1SAl Cooper 		dev_dbg(mmc_dev(host->mmc), "Using 64 bit DMA\n");
172*d46ba2d1SAl Cooper 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
173*d46ba2d1SAl Cooper 		cq_host->quirks |= CQHCI_QUIRK_SHORT_TXFR_DESC_SZ;
174*d46ba2d1SAl Cooper 	}
175*d46ba2d1SAl Cooper 
176*d46ba2d1SAl Cooper 	ret = cqhci_init(cq_host, host->mmc, dma64);
177*d46ba2d1SAl Cooper 	if (ret)
178*d46ba2d1SAl Cooper 		goto cleanup;
179*d46ba2d1SAl Cooper 
180*d46ba2d1SAl Cooper 	ret = __sdhci_add_host(host);
181*d46ba2d1SAl Cooper 	if (ret)
182*d46ba2d1SAl Cooper 		goto cleanup;
183*d46ba2d1SAl Cooper 
184*d46ba2d1SAl Cooper 	return 0;
185*d46ba2d1SAl Cooper 
186*d46ba2d1SAl Cooper cleanup:
187*d46ba2d1SAl Cooper 	sdhci_cleanup_host(host);
188*d46ba2d1SAl Cooper 	return ret;
189*d46ba2d1SAl Cooper }
190*d46ba2d1SAl Cooper 
191476bf3d6SAl Cooper static int sdhci_brcmstb_probe(struct platform_device *pdev)
192476bf3d6SAl Cooper {
19378ab82fdSAl Cooper 	const struct brcmstb_match_priv *match_priv;
194*d46ba2d1SAl Cooper 	struct sdhci_pltfm_data brcmstb_pdata;
195476bf3d6SAl Cooper 	struct sdhci_pltfm_host *pltfm_host;
19678ab82fdSAl Cooper 	const struct of_device_id *match;
19778ab82fdSAl Cooper 	struct sdhci_brcmstb_priv *priv;
19878ab82fdSAl Cooper 	struct sdhci_host *host;
19978ab82fdSAl Cooper 	struct resource *iomem;
200*d46ba2d1SAl Cooper 	bool has_cqe = false;
201476bf3d6SAl Cooper 	struct clk *clk;
202476bf3d6SAl Cooper 	int res;
203476bf3d6SAl Cooper 
20478ab82fdSAl Cooper 	match = of_match_node(sdhci_brcm_of_match, pdev->dev.of_node);
20578ab82fdSAl Cooper 	match_priv = match->data;
20678ab82fdSAl Cooper 
207*d46ba2d1SAl Cooper 	dev_dbg(&pdev->dev, "Probe found match for %s\n",  match->compatible);
208*d46ba2d1SAl Cooper 
209476bf3d6SAl Cooper 	clk = devm_clk_get(&pdev->dev, NULL);
210476bf3d6SAl Cooper 	if (IS_ERR(clk)) {
2116f2aa55bSAl Cooper 		if (PTR_ERR(clk) == -EPROBE_DEFER)
2126f2aa55bSAl Cooper 			return -EPROBE_DEFER;
213476bf3d6SAl Cooper 		dev_err(&pdev->dev, "Clock not found in Device Tree\n");
214476bf3d6SAl Cooper 		clk = NULL;
215476bf3d6SAl Cooper 	}
216476bf3d6SAl Cooper 	res = clk_prepare_enable(clk);
217476bf3d6SAl Cooper 	if (res)
218476bf3d6SAl Cooper 		return res;
219476bf3d6SAl Cooper 
220*d46ba2d1SAl Cooper 	memset(&brcmstb_pdata, 0, sizeof(brcmstb_pdata));
221*d46ba2d1SAl Cooper 	if (device_property_read_bool(&pdev->dev, "supports-cqe")) {
222*d46ba2d1SAl Cooper 		has_cqe = true;
223*d46ba2d1SAl Cooper 		match_priv->ops->irq = sdhci_brcmstb_cqhci_irq;
224*d46ba2d1SAl Cooper 	}
225*d46ba2d1SAl Cooper 	brcmstb_pdata.ops = match_priv->ops;
226*d46ba2d1SAl Cooper 	host = sdhci_pltfm_init(pdev, &brcmstb_pdata,
22778ab82fdSAl Cooper 				sizeof(struct sdhci_brcmstb_priv));
228476bf3d6SAl Cooper 	if (IS_ERR(host)) {
229476bf3d6SAl Cooper 		res = PTR_ERR(host);
230476bf3d6SAl Cooper 		goto err_clk;
231476bf3d6SAl Cooper 	}
232476bf3d6SAl Cooper 
23378ab82fdSAl Cooper 	pltfm_host = sdhci_priv(host);
23478ab82fdSAl Cooper 	priv = sdhci_pltfm_priv(pltfm_host);
235*d46ba2d1SAl Cooper 	priv->has_cqe = has_cqe;
23678ab82fdSAl Cooper 
23778ab82fdSAl Cooper 	/* Map in the non-standard CFG registers */
23878ab82fdSAl Cooper 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
23978ab82fdSAl Cooper 	priv->cfg_regs = devm_ioremap_resource(&pdev->dev, iomem);
24078ab82fdSAl Cooper 	if (IS_ERR(priv->cfg_regs)) {
24178ab82fdSAl Cooper 		res = PTR_ERR(priv->cfg_regs);
24278ab82fdSAl Cooper 		goto err;
24378ab82fdSAl Cooper 	}
24478ab82fdSAl Cooper 
245476bf3d6SAl Cooper 	sdhci_get_of_property(pdev);
2461e20186eSStefan Wahren 	res = mmc_of_parse(host->mmc);
2471e20186eSStefan Wahren 	if (res)
2481e20186eSStefan Wahren 		goto err;
249476bf3d6SAl Cooper 
250476bf3d6SAl Cooper 	/*
25178ab82fdSAl Cooper 	 * If the chip has enhanced strobe and it's enabled, add
25278ab82fdSAl Cooper 	 * callback
25378ab82fdSAl Cooper 	 */
25478ab82fdSAl Cooper 	if (match_priv->hs400es &&
25578ab82fdSAl Cooper 	    (host->mmc->caps2 & MMC_CAP2_HS400_ES))
25678ab82fdSAl Cooper 		host->mmc_host_ops.hs400_enhanced_strobe = match_priv->hs400es;
25778ab82fdSAl Cooper 
25878ab82fdSAl Cooper 	/*
259476bf3d6SAl Cooper 	 * Supply the existing CAPS, but clear the UHS modes. This
260476bf3d6SAl Cooper 	 * will allow these modes to be specified by device tree
261476bf3d6SAl Cooper 	 * properties through mmc_of_parse().
262476bf3d6SAl Cooper 	 */
263476bf3d6SAl Cooper 	host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
26478ab82fdSAl Cooper 	if (match_priv->flags & BRCMSTB_PRIV_FLAGS_NO_64BIT)
2656a3d8cedSJaedon Shin 		host->caps &= ~SDHCI_CAN_64BIT;
266476bf3d6SAl Cooper 	host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
267476bf3d6SAl Cooper 	host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
268476bf3d6SAl Cooper 			 SDHCI_SUPPORT_DDR50);
26978ab82fdSAl Cooper 	host->quirks |= SDHCI_QUIRK_MISSING_CAPS;
27078ab82fdSAl Cooper 
27178ab82fdSAl Cooper 	if (match_priv->flags & BRCMSTB_PRIV_FLAGS_BROKEN_TIMEOUT)
27278ab82fdSAl Cooper 		host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
273476bf3d6SAl Cooper 
274*d46ba2d1SAl Cooper 	res = sdhci_brcmstb_add_host(host, priv);
275476bf3d6SAl Cooper 	if (res)
276476bf3d6SAl Cooper 		goto err;
277476bf3d6SAl Cooper 
278476bf3d6SAl Cooper 	pltfm_host->clk = clk;
279476bf3d6SAl Cooper 	return res;
280476bf3d6SAl Cooper 
281476bf3d6SAl Cooper err:
282476bf3d6SAl Cooper 	sdhci_pltfm_free(pdev);
283476bf3d6SAl Cooper err_clk:
284476bf3d6SAl Cooper 	clk_disable_unprepare(clk);
285476bf3d6SAl Cooper 	return res;
286476bf3d6SAl Cooper }
287476bf3d6SAl Cooper 
288e7b5d63aSAl Cooper static void sdhci_brcmstb_shutdown(struct platform_device *pdev)
289e7b5d63aSAl Cooper {
290e7b5d63aSAl Cooper 	int ret;
291e7b5d63aSAl Cooper 
292e7b5d63aSAl Cooper 	ret = sdhci_pltfm_unregister(pdev);
293e7b5d63aSAl Cooper 	if (ret)
294e7b5d63aSAl Cooper 		dev_err(&pdev->dev, "failed to shutdown\n");
295e7b5d63aSAl Cooper }
296e7b5d63aSAl Cooper 
297476bf3d6SAl Cooper MODULE_DEVICE_TABLE(of, sdhci_brcm_of_match);
298476bf3d6SAl Cooper 
299476bf3d6SAl Cooper static struct platform_driver sdhci_brcmstb_driver = {
300476bf3d6SAl Cooper 	.driver		= {
301476bf3d6SAl Cooper 		.name	= "sdhci-brcmstb",
3021ab0d2d7SMasahiro Yamada 		.pm	= &sdhci_pltfm_pmops,
303476bf3d6SAl Cooper 		.of_match_table = of_match_ptr(sdhci_brcm_of_match),
304476bf3d6SAl Cooper 	},
305476bf3d6SAl Cooper 	.probe		= sdhci_brcmstb_probe,
306476bf3d6SAl Cooper 	.remove		= sdhci_pltfm_unregister,
307e7b5d63aSAl Cooper 	.shutdown	= sdhci_brcmstb_shutdown,
308476bf3d6SAl Cooper };
309476bf3d6SAl Cooper 
310476bf3d6SAl Cooper module_platform_driver(sdhci_brcmstb_driver);
311476bf3d6SAl Cooper 
312476bf3d6SAl Cooper MODULE_DESCRIPTION("SDHCI driver for Broadcom BRCMSTB SoCs");
313476bf3d6SAl Cooper MODULE_AUTHOR("Broadcom");
314476bf3d6SAl Cooper MODULE_LICENSE("GPL v2");
315