blob: df9e8ccb1e136170664f217bf30ed909cb9c5b47 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang Seecca9f452013-12-30 18:26:14 -06002/*
3 * (C) Copyright 2013 Altera Corporation <www.altera.com>
Chin Liang Seecca9f452013-12-30 18:26:14 -06004 */
5
6#include <common.h>
Chin Liang Seecca9f452013-12-30 18:26:14 -06007#include <asm/arch/clock_manager.h>
8#include <asm/arch/system_manager.h>
Marek Vasut26608602018-08-01 18:28:35 +02009#include <clk.h>
Marek Vasutae66f3c2015-11-30 20:41:04 +010010#include <dm.h>
11#include <dwmmc.h>
12#include <errno.h>
13#include <fdtdec.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Marek Vasutae66f3c2015-11-30 20:41:04 +010015#include <linux/err.h>
16#include <malloc.h>
Ley Foon Tan5a694d02018-06-14 18:45:21 +080017#include <reset.h>
Marek Vasutae66f3c2015-11-30 20:41:04 +010018
19DECLARE_GLOBAL_DATA_PTR;
Chin Liang Seecca9f452013-12-30 18:26:14 -060020
21static const struct socfpga_clock_manager *clock_manager_base =
22 (void *)SOCFPGA_CLKMGR_ADDRESS;
Chin Liang Seecca9f452013-12-30 18:26:14 -060023
Simon Glassa3a43202016-07-05 17:10:16 -060024struct socfpga_dwmci_plat {
25 struct mmc_config cfg;
26 struct mmc mmc;
27};
28
Marek Vasutae66f3c2015-11-30 20:41:04 +010029/* socfpga implmentation specific driver private data */
Chin Liang See48e7bf92015-11-26 09:43:43 +080030struct dwmci_socfpga_priv_data {
Marek Vasutae66f3c2015-11-30 20:41:04 +010031 struct dwmci_host host;
32 unsigned int drvsel;
33 unsigned int smplsel;
Chin Liang See48e7bf92015-11-26 09:43:43 +080034};
35
Ley Foon Tan5a694d02018-06-14 18:45:21 +080036static void socfpga_dwmci_reset(struct udevice *dev)
37{
38 struct reset_ctl_bulk reset_bulk;
39 int ret;
40
41 ret = reset_get_bulk(dev, &reset_bulk);
42 if (ret) {
43 dev_warn(dev, "Can't get reset: %d\n", ret);
44 return;
45 }
46
47 reset_deassert_bulk(&reset_bulk);
48}
49
Chin Liang See48e7bf92015-11-26 09:43:43 +080050static void socfpga_dwmci_clksel(struct dwmci_host *host)
51{
52 struct dwmci_socfpga_priv_data *priv = host->priv;
Dinh Nguyenc4b66c42015-12-02 13:31:33 -060053 u32 sdmmc_mask = ((priv->smplsel & 0x7) << SYSMGR_SDMMC_SMPLSEL_SHIFT) |
54 ((priv->drvsel & 0x7) << SYSMGR_SDMMC_DRVSEL_SHIFT);
Chin Liang Seecca9f452013-12-30 18:26:14 -060055
56 /* Disable SDMMC clock. */
Pavel Machek91c2f8f2014-07-19 23:57:59 +020057 clrbits_le32(&clock_manager_base->per_pll.en,
Chin Liang Seecca9f452013-12-30 18:26:14 -060058 CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
59
Chin Liang See48e7bf92015-11-26 09:43:43 +080060 debug("%s: drvsel %d smplsel %d\n", __func__,
61 priv->drvsel, priv->smplsel);
Ley Foon Tan3d3a8602019-11-08 10:38:20 +080062 writel(sdmmc_mask, socfpga_get_sysmgr_addr() + SYSMGR_SDMMC);
Chin Liang Seecca9f452013-12-30 18:26:14 -060063
64 debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__,
Ley Foon Tan3d3a8602019-11-08 10:38:20 +080065 readl(socfpga_get_sysmgr_addr() + SYSMGR_SDMMC));
Chin Liang Seecca9f452013-12-30 18:26:14 -060066
67 /* Enable SDMMC clock */
Pavel Machek91c2f8f2014-07-19 23:57:59 +020068 setbits_le32(&clock_manager_base->per_pll.en,
Chin Liang Seecca9f452013-12-30 18:26:14 -060069 CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
70}
71
Marek Vasut26608602018-08-01 18:28:35 +020072static int socfpga_dwmmc_get_clk_rate(struct udevice *dev)
Chin Liang Seecca9f452013-12-30 18:26:14 -060073{
Marek Vasutae66f3c2015-11-30 20:41:04 +010074 struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
75 struct dwmci_host *host = &priv->host;
Marek Vasut26608602018-08-01 18:28:35 +020076#if CONFIG_IS_ENABLED(CLK)
77 struct clk clk;
78 int ret;
79
80 ret = clk_get_by_index(dev, 1, &clk);
81 if (ret)
82 return ret;
83
84 host->bus_hz = clk_get_rate(&clk);
Pavel Machek51d21132014-09-08 14:08:45 +020085
Marek Vasut26608602018-08-01 18:28:35 +020086 clk_free(&clk);
87#else
88 /* Fixed clock divide by 4 which due to the SDMMC wrapper */
89 host->bus_hz = cm_get_mmc_controller_clk_hz();
90#endif
91 if (host->bus_hz == 0) {
Marek Vasutae66f3c2015-11-30 20:41:04 +010092 printf("DWMMC: MMC clock is zero!");
Marek Vasut17497232015-07-25 10:48:14 +020093 return -EINVAL;
Chin Liang Seecca9f452013-12-30 18:26:14 -060094 }
95
Marek Vasut26608602018-08-01 18:28:35 +020096 return 0;
97}
98
99static int socfpga_dwmmc_ofdata_to_platdata(struct udevice *dev)
100{
101 struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
102 struct dwmci_host *host = &priv->host;
103 int fifo_depth;
104
Simon Glassdd79d6e2017-01-17 16:52:55 -0700105 fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Marek Vasutae66f3c2015-11-30 20:41:04 +0100106 "fifo-depth", 0);
Marek Vasut17497232015-07-25 10:48:14 +0200107 if (fifo_depth < 0) {
Marek Vasutae66f3c2015-11-30 20:41:04 +0100108 printf("DWMMC: Can't get FIFO depth\n");
Marek Vasut17497232015-07-25 10:48:14 +0200109 return -EINVAL;
110 }
111
Marek Vasutae66f3c2015-11-30 20:41:04 +0100112 host->name = dev->name;
Simon Glassba1dea42017-05-17 17:18:05 -0600113 host->ioaddr = (void *)devfdt_get_addr(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700114 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Marek Vasutae66f3c2015-11-30 20:41:04 +0100115 "bus-width", 4);
Chin Liang Seecca9f452013-12-30 18:26:14 -0600116 host->clksel = socfpga_dwmci_clksel;
Marek Vasutae66f3c2015-11-30 20:41:04 +0100117
118 /*
119 * TODO(sjg@chromium.org): Remove the need for this hack.
120 * We only have one dwmmc block on gen5 SoCFPGA.
121 */
122 host->dev_index = 0;
Chin Liang Seecca9f452013-12-30 18:26:14 -0600123 host->fifoth_val = MSIZE(0x2) |
Marek Vasut17497232015-07-25 10:48:14 +0200124 RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700125 priv->drvsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
Marek Vasutae66f3c2015-11-30 20:41:04 +0100126 "drvsel", 3);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700127 priv->smplsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
Marek Vasutae66f3c2015-11-30 20:41:04 +0100128 "smplsel", 0);
Chin Liang See48e7bf92015-11-26 09:43:43 +0800129 host->priv = priv;
Chin Liang Seecca9f452013-12-30 18:26:14 -0600130
Marek Vasutae66f3c2015-11-30 20:41:04 +0100131 return 0;
Chin Liang Seecca9f452013-12-30 18:26:14 -0600132}
133
Marek Vasutae66f3c2015-11-30 20:41:04 +0100134static int socfpga_dwmmc_probe(struct udevice *dev)
Marek Vasut17497232015-07-25 10:48:14 +0200135{
Simon Glassa3a43202016-07-05 17:10:16 -0600136#ifdef CONFIG_BLK
137 struct socfpga_dwmci_plat *plat = dev_get_platdata(dev);
138#endif
Marek Vasutae66f3c2015-11-30 20:41:04 +0100139 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
140 struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
141 struct dwmci_host *host = &priv->host;
Marek Vasut26608602018-08-01 18:28:35 +0200142 int ret;
143
144 ret = socfpga_dwmmc_get_clk_rate(dev);
145 if (ret)
146 return ret;
Simon Glassa3a43202016-07-05 17:10:16 -0600147
Ley Foon Tan5a694d02018-06-14 18:45:21 +0800148 socfpga_dwmci_reset(dev);
149
Simon Glassa3a43202016-07-05 17:10:16 -0600150#ifdef CONFIG_BLK
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900151 dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000);
Simon Glassa3a43202016-07-05 17:10:16 -0600152 host->mmc = &plat->mmc;
153#else
Marek Vasut17497232015-07-25 10:48:14 +0200154
Marek Vasutae66f3c2015-11-30 20:41:04 +0100155 ret = add_dwmci(host, host->bus_hz, 400000);
156 if (ret)
157 return ret;
Simon Glassa3a43202016-07-05 17:10:16 -0600158#endif
159 host->mmc->priv = &priv->host;
Marek Vasutae66f3c2015-11-30 20:41:04 +0100160 upriv->mmc = host->mmc;
Simon Glass77ca42b2016-05-01 13:52:34 -0600161 host->mmc->dev = dev;
Marek Vasutae66f3c2015-11-30 20:41:04 +0100162
Patrick Bruenn3eab2202018-03-06 09:07:23 +0100163 return dwmci_probe(dev);
Marek Vasut17497232015-07-25 10:48:14 +0200164}
165
Simon Glassa3a43202016-07-05 17:10:16 -0600166static int socfpga_dwmmc_bind(struct udevice *dev)
167{
168#ifdef CONFIG_BLK
169 struct socfpga_dwmci_plat *plat = dev_get_platdata(dev);
170 int ret;
171
172 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
173 if (ret)
174 return ret;
175#endif
176
177 return 0;
178}
179
Marek Vasutae66f3c2015-11-30 20:41:04 +0100180static const struct udevice_id socfpga_dwmmc_ids[] = {
181 { .compatible = "altr,socfpga-dw-mshc" },
182 { }
183};
Marek Vasut17497232015-07-25 10:48:14 +0200184
Marek Vasutae66f3c2015-11-30 20:41:04 +0100185U_BOOT_DRIVER(socfpga_dwmmc_drv) = {
186 .name = "socfpga_dwmmc",
187 .id = UCLASS_MMC,
188 .of_match = socfpga_dwmmc_ids,
189 .ofdata_to_platdata = socfpga_dwmmc_ofdata_to_platdata,
Sylvain Lesne7083f912016-10-24 18:24:37 +0200190 .ops = &dm_dwmci_ops,
Simon Glassa3a43202016-07-05 17:10:16 -0600191 .bind = socfpga_dwmmc_bind,
Marek Vasutae66f3c2015-11-30 20:41:04 +0100192 .probe = socfpga_dwmmc_probe,
193 .priv_auto_alloc_size = sizeof(struct dwmci_socfpga_priv_data),
Sylvain Lesne7083f912016-10-24 18:24:37 +0200194 .platdata_auto_alloc_size = sizeof(struct socfpga_dwmci_plat),
Marek Vasutae66f3c2015-11-30 20:41:04 +0100195};