Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <dwmmc.h> |
| 11 | #include <errno.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 12 | #include <pwrseq.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 13 | #include <syscon.h> |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 15 | #include <asm/arch/clock.h> |
| 16 | #include <asm/arch/periph.h> |
| 17 | #include <linux/err.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 21 | struct rockchip_mmc_plat { |
| 22 | struct mmc_config cfg; |
| 23 | struct mmc mmc; |
| 24 | }; |
| 25 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 26 | struct rockchip_dwmmc_priv { |
| 27 | struct udevice *clk; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 28 | int periph; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 29 | struct dwmci_host host; |
| 30 | }; |
| 31 | |
| 32 | static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq) |
| 33 | { |
| 34 | struct udevice *dev = host->priv; |
| 35 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 36 | int ret; |
| 37 | |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 38 | ret = clk_set_periph_rate(priv->clk, priv->periph, freq); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 39 | if (ret < 0) { |
| 40 | debug("%s: err=%d\n", __func__, ret); |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | return freq; |
| 45 | } |
| 46 | |
| 47 | static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev) |
| 48 | { |
| 49 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 50 | struct dwmci_host *host = &priv->host; |
| 51 | |
| 52 | host->name = dev->name; |
| 53 | host->ioaddr = (void *)dev_get_addr(dev); |
| 54 | host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 55 | "bus-width", 4); |
| 56 | host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk; |
| 57 | host->priv = dev; |
| 58 | |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 59 | /* use non-removeable as sdcard and emmc as judgement */ |
| 60 | if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable")) |
huang lin | 6579385 | 2016-01-08 14:06:49 +0800 | [diff] [blame] | 61 | host->dev_index = 0; |
| 62 | else |
huang lin | ace2198 | 2015-11-18 09:37:25 +0800 | [diff] [blame] | 63 | host->dev_index = 1; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int rockchip_dwmmc_probe(struct udevice *dev) |
| 69 | { |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 70 | #ifdef CONFIG_BLK |
| 71 | struct rockchip_mmc_plat *plat = dev_get_platdata(dev); |
| 72 | #endif |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 73 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 74 | struct rockchip_dwmmc_priv *priv = dev_get_priv(dev); |
| 75 | struct dwmci_host *host = &priv->host; |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 76 | struct udevice *pwr_dev __maybe_unused; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 77 | u32 minmax[2]; |
| 78 | int ret; |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 79 | int fifo_depth; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 80 | |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 81 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 82 | if (ret < 0) |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 83 | return ret; |
Simon Glass | 898d643 | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 84 | priv->periph = ret; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 85 | |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 86 | if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset, |
| 87 | "clock-freq-min-max", minmax, 2)) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 91 | "fifo-depth", 0); |
| 92 | if (fifo_depth < 0) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | host->fifoth_val = MSIZE(0x2) | |
| 96 | RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2); |
| 97 | |
| 98 | if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "fifo-mode")) |
| 99 | host->fifo_mode = true; |
| 100 | |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 101 | #ifdef CONFIG_PWRSEQ |
| 102 | /* Enable power if needed */ |
| 103 | ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq", |
| 104 | &pwr_dev); |
| 105 | if (!ret) { |
| 106 | ret = pwrseq_set_power(pwr_dev, true); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | } |
| 110 | #endif |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 111 | #ifdef CONFIG_BLK |
| 112 | dwmci_setup_cfg(&plat->cfg, dev->name, host->buswidth, host->caps, |
| 113 | minmax[1], minmax[0]); |
| 114 | host->mmc = &plat->mmc; |
| 115 | #else |
huang lin | 2863724 | 2015-11-17 14:20:24 +0800 | [diff] [blame] | 116 | ret = add_dwmci(host, minmax[1], minmax[0]); |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 117 | if (ret) |
| 118 | return ret; |
| 119 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 120 | #endif |
| 121 | host->mmc->priv = &priv->host; |
Simon Glass | cffe5d8 | 2016-05-01 13:52:34 -0600 | [diff] [blame] | 122 | host->mmc->dev = dev; |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 123 | upriv->mmc = host->mmc; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 128 | static int rockchip_dwmmc_bind(struct udevice *dev) |
| 129 | { |
| 130 | #ifdef CONFIG_BLK |
| 131 | struct rockchip_mmc_plat *plat = dev_get_platdata(dev); |
| 132 | int ret; |
| 133 | |
| 134 | ret = dwmci_bind(dev, &plat->mmc, &plat->cfg); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | #endif |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 142 | static const struct udevice_id rockchip_dwmmc_ids[] = { |
| 143 | { .compatible = "rockchip,rk3288-dw-mshc" }, |
| 144 | { } |
| 145 | }; |
| 146 | |
| 147 | U_BOOT_DRIVER(rockchip_dwmmc_drv) = { |
| 148 | .name = "rockchip_dwmmc", |
| 149 | .id = UCLASS_MMC, |
| 150 | .of_match = rockchip_dwmmc_ids, |
| 151 | .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata, |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 152 | .bind = rockchip_dwmmc_bind, |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 153 | .probe = rockchip_dwmmc_probe, |
| 154 | .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv), |
Simon Glass | f6e41d1 | 2016-05-14 14:03:08 -0600 | [diff] [blame] | 155 | .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat), |
Simon Glass | a8cb4fb | 2015-08-30 16:55:37 -0600 | [diff] [blame] | 156 | }; |
Simon Glass | e1efec4 | 2016-01-21 19:43:34 -0700 | [diff] [blame] | 157 | |
| 158 | #ifdef CONFIG_PWRSEQ |
| 159 | static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable) |
| 160 | { |
| 161 | struct gpio_desc reset; |
| 162 | int ret; |
| 163 | |
| 164 | ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | dm_gpio_set_value(&reset, 1); |
| 168 | udelay(1); |
| 169 | dm_gpio_set_value(&reset, 0); |
| 170 | udelay(200); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = { |
| 176 | .set_power = rockchip_dwmmc_pwrseq_set_power, |
| 177 | }; |
| 178 | |
| 179 | static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = { |
| 180 | { .compatible = "mmc-pwrseq-emmc" }, |
| 181 | { } |
| 182 | }; |
| 183 | |
| 184 | U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = { |
| 185 | .name = "mmc_pwrseq_emmc", |
| 186 | .id = UCLASS_PWRSEQ, |
| 187 | .of_match = rockchip_dwmmc_pwrseq_ids, |
| 188 | .ops = &rockchip_dwmmc_pwrseq_ops, |
| 189 | }; |
| 190 | #endif |