Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 9 | #include <dt-structs.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 10 | #include <dwmmc.h> |
| 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 14 | #include <pwrseq.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 15 | #include <syscon.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 17 | #include <asm/arch-rockchip/clock.h> |
| 18 | #include <asm/arch-rockchip/periph.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 22 | struct rockchip_mmc_plat { |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 24 | struct dtd_rockchip_rk3288_dw_mshc dtplat; |
| 25 | #endif |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 26 | struct mmc_config cfg; |
| 27 | struct mmc mmc; |
| 28 | }; |
| 29 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 30 | struct rockchip_dwmmc_priv { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 31 | struct clk clk; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 32 | struct dwmci_host host; |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 33 | int fifo_depth; |
| 34 | bool fifo_mode; |
| 35 | u32 minmax[2]; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq) |
| 39 | { |
| 40 | struct udevice *dev = host->priv; |
| 41 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 42 | int ret; |
| 43 | |
John Keeping | ea0f766 | 2023-01-17 17:07:47 +0000 | [diff] [blame] | 44 | /* |
| 45 | * The clock frequency chosen here affects CLKDIV in the dw_mmc core. |
| 46 | * That can be either 0 or 1, but it must be set to 1 for eMMC DDR52 |
| 47 | * 8-bit mode. It will be set to 0 for all other modes. |
| 48 | */ |
| 49 | if (host->mmc->selected_mode == MMC_DDR_52 && host->mmc->bus_width == 8) |
| 50 | freq *= 2; |
| 51 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 52 | ret = clk_set_rate(&priv->clk, freq); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 53 | if (ret < 0) { |
Kever Yang | 419b080 | 2017-06-14 16:31:49 +0800 | [diff] [blame] | 54 | debug("%s: err=%d\n", __func__, ret); |
Jonas Karlman | d11f0da | 2023-03-14 00:38:32 +0000 | [diff] [blame] | 55 | return 0; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return freq; |
| 59 | } |
| 60 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 61 | static int rockchip_dwmmc_of_to_plat(struct udevice *dev) |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 62 | { |
| 63 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 64 | struct dwmci_host *host = &priv->host; |
| 65 | |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 66 | if (!CONFIG_IS_ENABLED(OF_REAL)) |
| 67 | return 0; |
| 68 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 69 | host->name = dev->name; |
Philipp Tomsich | be5f04e | 2017-09-11 22:04:15 +0200 | [diff] [blame] | 70 | host->ioaddr = dev_read_addr_ptr(dev); |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 71 | host->buswidth = dev_read_u32_default(dev, "bus-width", 4); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 72 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 73 | host->priv = dev; |
| 74 | |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 75 | /* use non-removeable as sdcard and emmc as judgement */ |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 76 | if (dev_read_bool(dev, "non-removable")) |
huang lin | 6579385 | 2016-01-08 14:06:49 +0800 | [diff] [blame] | 77 | host->dev_index = 0; |
| 78 | else |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 79 | host->dev_index = 1; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 80 | |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 81 | priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0); |
| 82 | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 83 | if (priv->fifo_depth < 0) |
| 84 | return -EINVAL; |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 85 | priv->fifo_mode = dev_read_bool(dev, "fifo-mode"); |
Philipp Tomsich | ff71f9a | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 86 | |
Heiko Stuebner | c8dd0e4 | 2019-11-19 12:04:01 +0100 | [diff] [blame] | 87 | #ifdef CONFIG_SPL_BUILD |
| 88 | if (!priv->fifo_mode) |
| 89 | priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode"); |
| 90 | #endif |
| 91 | |
Philipp Tomsich | ff71f9a | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 92 | /* |
| 93 | * 'clock-freq-min-max' is deprecated |
| 94 | * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b) |
| 95 | */ |
Philipp Tomsich | fd1bf8d | 2017-06-07 18:46:00 +0200 | [diff] [blame] | 96 | if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) { |
| 97 | int val = dev_read_u32_default(dev, "max-frequency", -EINVAL); |
Philipp Tomsich | ff71f9a | 2017-04-25 09:52:07 +0200 | [diff] [blame] | 98 | |
| 99 | if (val < 0) |
| 100 | return val; |
| 101 | |
| 102 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 103 | priv->minmax[1] = val; |
| 104 | } else { |
| 105 | debug("%s: 'clock-freq-min-max' property was deprecated.\n", |
| 106 | __func__); |
| 107 | } |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 108 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int rockchip_dwmmc_probe(struct udevice *dev) |
| 113 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 114 | struct rockchip_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 115 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 116 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 117 | struct dwmci_host *host = &priv->host; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 118 | int ret; |
| 119 | |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 120 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 121 | struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat; |
| 122 | |
| 123 | host->name = dev->name; |
| 124 | host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
| 125 | host->buswidth = dtplat->bus_width; |
| 126 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 127 | host->priv = dev; |
| 128 | host->dev_index = 0; |
| 129 | priv->fifo_depth = dtplat->fifo_depth; |
Johan Jonker | 2d3bb40 | 2022-04-09 18:55:09 +0200 | [diff] [blame] | 130 | priv->fifo_mode = dtplat->u_boot_spl_fifo_mode; |
Kever Yang | 8093529 | 2017-06-14 16:31:46 +0800 | [diff] [blame] | 131 | priv->minmax[0] = 400000; /* 400 kHz */ |
| 132 | priv->minmax[1] = dtplat->max_frequency; |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 133 | |
Johan Jonker | 9d75daf | 2022-04-09 18:55:08 +0200 | [diff] [blame] | 134 | ret = clk_get_by_phandle(dev, &dtplat->clocks[1], &priv->clk); |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | #else |
Johan Jonker | 9d75daf | 2022-04-09 18:55:08 +0200 | [diff] [blame] | 138 | ret = clk_get_by_index(dev, 1, &priv->clk); |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 139 | if (ret < 0) |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 140 | return ret; |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 141 | #endif |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 142 | host->fifoth_val = MSIZE(0x2) | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 143 | RX_WMARK(priv->fifo_depth / 2 - 1) | |
| 144 | TX_WMARK(priv->fifo_depth / 2); |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 145 | |
Simon Glass | 6809b04 | 2016-07-04 11:58:26 -0600 | [diff] [blame] | 146 | host->fifo_mode = priv->fifo_mode; |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 147 | |
Jaehoon Chung | 9d7e661 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 148 | #ifdef CONFIG_MMC_PWRSEQ |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 149 | /* Enable power if needed */ |
Jaehoon Chung | 9d7e661 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 150 | ret = mmc_pwrseq_get_power(dev, &plat->cfg); |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 151 | if (!ret) { |
Jaehoon Chung | 9d7e661 | 2021-02-16 10:16:54 +0900 | [diff] [blame] | 152 | ret = pwrseq_set_power(plat->cfg.pwr_dev, true); |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 153 | if (ret) |
| 154 | return ret; |
| 155 | } |
| 156 | #endif |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 157 | dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 158 | host->mmc = &plat->mmc; |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 159 | host->mmc->priv = &priv->host; |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 160 | host->mmc->dev = dev; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 161 | upriv->mmc = host->mmc; |
| 162 | |
Simon Glass | 42b37d8 | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 163 | return dwmci_probe(dev); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 164 | } |
| 165 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 166 | static int rockchip_dwmmc_bind(struct udevice *dev) |
| 167 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 168 | struct rockchip_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 169 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 170 | return dwmci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 173 | static const struct udevice_id rockchip_dwmmc_ids[] = { |
Heiko Stuebner | 26a52f3 | 2018-09-21 10:59:46 +0200 | [diff] [blame] | 174 | { .compatible = "rockchip,rk2928-dw-mshc" }, |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 175 | { .compatible = "rockchip,rk3288-dw-mshc" }, |
| 176 | { } |
| 177 | }; |
| 178 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 179 | U_BOOT_DRIVER(rockchip_rk3288_dw_mshc) = { |
Simon Glass | bfeb443 | 2016-07-04 11:58:27 -0600 | [diff] [blame] | 180 | .name = "rockchip_rk3288_dw_mshc", |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 181 | .id = UCLASS_MMC, |
| 182 | .of_match = rockchip_dwmmc_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 183 | .of_to_plat = rockchip_dwmmc_of_to_plat, |
Simon Glass | 42b37d8 | 2016-06-12 23:30:24 -0600 | [diff] [blame] | 184 | .ops = &dm_dwmci_ops, |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 185 | .bind = rockchip_dwmmc_bind, |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 186 | .probe = rockchip_dwmmc_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 187 | .priv_auto = sizeof(struct rockchip_dwmmc_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 188 | .plat_auto = sizeof(struct rockchip_mmc_plat), |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 189 | }; |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 190 | |
Johan Jonker | 2d3bb40 | 2022-04-09 18:55:09 +0200 | [diff] [blame] | 191 | DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk2928_dw_mshc) |
Simon Glass | bdf8fd7 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 192 | DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3328_dw_mshc) |
| 193 | DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3368_dw_mshc) |