Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 Altera Corporation <www.altera.com> |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 8 | #include <asm/arch/clock_manager.h> |
Chee Hong Ang | 6b38cc2 | 2020-12-24 18:21:04 +0800 | [diff] [blame] | 9 | #include <asm/arch/secure_reg_helper.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 10 | #include <asm/arch/system_manager.h> |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 11 | #include <clk.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 13 | #include <dwmmc.h> |
Pavel Machek | 498d1a6 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 15 | #include <fdtdec.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Chee Hong Ang | 6b38cc2 | 2020-12-24 18:21:04 +0800 | [diff] [blame] | 18 | #include <linux/intel-smc.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | #include <malloc.h> |
Ley Foon Tan | 2d4d693 | 2018-06-14 18:45:21 +0800 | [diff] [blame] | 22 | #include <reset.h> |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 25 | |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 26 | struct socfpga_dwmci_plat { |
| 27 | struct mmc_config cfg; |
| 28 | struct mmc mmc; |
| 29 | }; |
| 30 | |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 31 | /* socfpga implmentation specific driver private data */ |
Chin Liang See | 9a41404 | 2015-11-26 09:43:43 +0800 | [diff] [blame] | 32 | struct dwmci_socfpga_priv_data { |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 33 | struct dwmci_host host; |
| 34 | unsigned int drvsel; |
| 35 | unsigned int smplsel; |
Chin Liang See | 9a41404 | 2015-11-26 09:43:43 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
Ley Foon Tan | 2d4d693 | 2018-06-14 18:45:21 +0800 | [diff] [blame] | 38 | static void socfpga_dwmci_reset(struct udevice *dev) |
| 39 | { |
| 40 | struct reset_ctl_bulk reset_bulk; |
| 41 | int ret; |
| 42 | |
| 43 | ret = reset_get_bulk(dev, &reset_bulk); |
| 44 | if (ret) { |
| 45 | dev_warn(dev, "Can't get reset: %d\n", ret); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | reset_deassert_bulk(&reset_bulk); |
| 50 | } |
| 51 | |
Siew Chin Lim | d456dfb | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 52 | static int socfpga_dwmci_clksel(struct dwmci_host *host) |
Chin Liang See | 9a41404 | 2015-11-26 09:43:43 +0800 | [diff] [blame] | 53 | { |
| 54 | struct dwmci_socfpga_priv_data *priv = host->priv; |
Dinh Nguyen | a1684b6 | 2015-12-02 13:31:33 -0600 | [diff] [blame] | 55 | u32 sdmmc_mask = ((priv->smplsel & 0x7) << SYSMGR_SDMMC_SMPLSEL_SHIFT) | |
| 56 | ((priv->drvsel & 0x7) << SYSMGR_SDMMC_DRVSEL_SHIFT); |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 57 | |
| 58 | /* Disable SDMMC clock. */ |
Ley Foon Tan | 94172c7 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 59 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_PERPLL_EN, |
| 60 | CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK); |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 61 | |
Chin Liang See | 9a41404 | 2015-11-26 09:43:43 +0800 | [diff] [blame] | 62 | debug("%s: drvsel %d smplsel %d\n", __func__, |
| 63 | priv->drvsel, priv->smplsel); |
Chee Hong Ang | 6b38cc2 | 2020-12-24 18:21:04 +0800 | [diff] [blame] | 64 | |
| 65 | #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_ATF) |
| 66 | int ret; |
| 67 | |
| 68 | ret = socfpga_secure_reg_write32(SOCFPGA_SECURE_REG_SYSMGR_SOC64_SDMMC, |
| 69 | sdmmc_mask); |
| 70 | if (ret) { |
| 71 | printf("DWMMC: Failed to set clksel via SMC call"); |
| 72 | return ret; |
| 73 | } |
| 74 | #else |
Ley Foon Tan | db5741f | 2019-11-08 10:38:20 +0800 | [diff] [blame] | 75 | writel(sdmmc_mask, socfpga_get_sysmgr_addr() + SYSMGR_SDMMC); |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 76 | |
| 77 | debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__, |
Ley Foon Tan | db5741f | 2019-11-08 10:38:20 +0800 | [diff] [blame] | 78 | readl(socfpga_get_sysmgr_addr() + SYSMGR_SDMMC)); |
Chee Hong Ang | 6b38cc2 | 2020-12-24 18:21:04 +0800 | [diff] [blame] | 79 | #endif |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 80 | |
| 81 | /* Enable SDMMC clock */ |
Ley Foon Tan | 94172c7 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 82 | setbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_PERPLL_EN, |
| 83 | CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK); |
Siew Chin Lim | d456dfb | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 84 | |
| 85 | return 0; |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 88 | static int socfpga_dwmmc_get_clk_rate(struct udevice *dev) |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 89 | { |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 90 | struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev); |
| 91 | struct dwmci_host *host = &priv->host; |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 92 | #if CONFIG_IS_ENABLED(CLK) |
| 93 | struct clk clk; |
| 94 | int ret; |
Pavel Machek | 498d1a6 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 95 | |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 96 | ret = clk_get_by_index(dev, 1, &clk); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
| 100 | host->bus_hz = clk_get_rate(&clk); |
| 101 | |
| 102 | clk_free(&clk); |
| 103 | #else |
| 104 | /* Fixed clock divide by 4 which due to the SDMMC wrapper */ |
| 105 | host->bus_hz = cm_get_mmc_controller_clk_hz(); |
| 106 | #endif |
| 107 | if (host->bus_hz == 0) { |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 108 | printf("DWMMC: MMC clock is zero!"); |
Pavel Machek | 498d1a6 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 109 | return -EINVAL; |
| 110 | } |
Pavel Machek | 7860649 | 2014-07-21 13:30:19 +0200 | [diff] [blame] | 111 | |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 115 | static int socfpga_dwmmc_of_to_plat(struct udevice *dev) |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 116 | { |
| 117 | struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev); |
| 118 | struct dwmci_host *host = &priv->host; |
| 119 | int fifo_depth; |
| 120 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 121 | fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 122 | "fifo-depth", 0); |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 123 | if (fifo_depth < 0) { |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 124 | printf("DWMMC: Can't get FIFO depth\n"); |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | } |
| 127 | |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 128 | host->name = dev->name; |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 129 | host->ioaddr = dev_read_addr_ptr(dev); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 130 | host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 131 | "bus-width", 4); |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 132 | host->clksel = socfpga_dwmci_clksel; |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * TODO(sjg@chromium.org): Remove the need for this hack. |
| 136 | * We only have one dwmmc block on gen5 SoCFPGA. |
| 137 | */ |
| 138 | host->dev_index = 0; |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 139 | host->fifoth_val = MSIZE(0x2) | |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 140 | RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 141 | priv->drvsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 142 | "drvsel", 3); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 143 | priv->smplsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 144 | "smplsel", 0); |
Chin Liang See | 9a41404 | 2015-11-26 09:43:43 +0800 | [diff] [blame] | 145 | host->priv = priv; |
Chin Liang See | c5c1af2 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 146 | |
Ley Foon Tan | 1925e65 | 2021-04-26 13:17:46 +0800 | [diff] [blame] | 147 | host->fifo_mode = dev_read_bool(dev, "fifo-mode"); |
| 148 | |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 152 | static int socfpga_dwmmc_probe(struct udevice *dev) |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 153 | { |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 154 | #ifdef CONFIG_BLK |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 155 | struct socfpga_dwmci_plat *plat = dev_get_plat(dev); |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 156 | #endif |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 157 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 158 | struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev); |
| 159 | struct dwmci_host *host = &priv->host; |
Marek Vasut | 12ea13a | 2018-08-01 18:28:35 +0200 | [diff] [blame] | 160 | int ret; |
| 161 | |
| 162 | ret = socfpga_dwmmc_get_clk_rate(dev); |
| 163 | if (ret) |
| 164 | return ret; |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 165 | |
Ley Foon Tan | 2d4d693 | 2018-06-14 18:45:21 +0800 | [diff] [blame] | 166 | socfpga_dwmci_reset(dev); |
| 167 | |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 168 | #ifdef CONFIG_BLK |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 169 | dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000); |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 170 | host->mmc = &plat->mmc; |
| 171 | #else |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 172 | |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 173 | ret = add_dwmci(host, host->bus_hz, 400000); |
| 174 | if (ret) |
| 175 | return ret; |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 176 | #endif |
| 177 | host->mmc->priv = &priv->host; |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 178 | upriv->mmc = host->mmc; |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 179 | host->mmc->dev = dev; |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 180 | |
Patrick Bruenn | 55118ec | 2018-03-06 09:07:23 +0100 | [diff] [blame] | 181 | return dwmci_probe(dev); |
Marek Vasut | 129adf5 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 182 | } |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 183 | |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 184 | static int socfpga_dwmmc_bind(struct udevice *dev) |
| 185 | { |
| 186 | #ifdef CONFIG_BLK |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 187 | struct socfpga_dwmci_plat *plat = dev_get_plat(dev); |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | ret = dwmci_bind(dev, &plat->mmc, &plat->cfg); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | #endif |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 198 | static const struct udevice_id socfpga_dwmmc_ids[] = { |
| 199 | { .compatible = "altr,socfpga-dw-mshc" }, |
| 200 | { } |
| 201 | }; |
| 202 | |
| 203 | U_BOOT_DRIVER(socfpga_dwmmc_drv) = { |
| 204 | .name = "socfpga_dwmmc", |
| 205 | .id = UCLASS_MMC, |
| 206 | .of_match = socfpga_dwmmc_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 207 | .of_to_plat = socfpga_dwmmc_of_to_plat, |
Sylvain Lesne | f55ae19 | 2016-10-24 18:24:37 +0200 | [diff] [blame] | 208 | .ops = &dm_dwmci_ops, |
Simon Glass | f1a485a | 2016-07-05 17:10:16 -0600 | [diff] [blame] | 209 | .bind = socfpga_dwmmc_bind, |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 210 | .probe = socfpga_dwmmc_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 211 | .priv_auto = sizeof(struct dwmci_socfpga_priv_data), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 212 | .plat_auto = sizeof(struct socfpga_dwmci_plat), |
Marek Vasut | c35ed77 | 2015-11-30 20:41:04 +0100 | [diff] [blame] | 213 | }; |