Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Cortina Access |
| 4 | * Arthur Li <arthur.li@cortina-access.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dwmmc.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 10 | #include <asm/global_data.h> |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
| 12 | #include <malloc.h> |
| 13 | #include <errno.h> |
| 14 | #include <dm.h> |
| 15 | #include <mapmem.h> |
| 16 | |
| 17 | #define SD_CLK_SEL_MASK (0x3) |
| 18 | #define SD_DLL_DEFAULT (0x143000) |
| 19 | #define SD_SCLK_MAX (200000000) |
| 20 | |
| 21 | #define SD_CLK_SEL_200MHZ (0x2) |
| 22 | #define SD_CLK_SEL_100MHZ (0x1) |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 23 | #define SD_CLK_SEL_50MHZ (0x0) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 24 | |
| 25 | #define IO_DRV_SD_DS_OFFSET (16) |
| 26 | #define IO_DRV_SD_DS_MASK (0xff << IO_DRV_SD_DS_OFFSET) |
| 27 | |
| 28 | #define MIN_FREQ (400000) |
| 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
| 32 | struct ca_mmc_plat { |
| 33 | struct mmc_config cfg; |
| 34 | struct mmc mmc; |
| 35 | }; |
| 36 | |
| 37 | struct ca_dwmmc_priv_data { |
| 38 | struct dwmci_host host; |
| 39 | void __iomem *sd_dll_reg; |
| 40 | void __iomem *io_drv_reg; |
| 41 | u8 ds; |
| 42 | }; |
| 43 | |
Siew Chin Lim | d456dfb | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 44 | static int ca_dwmci_clksel(struct dwmci_host *host) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 45 | { |
| 46 | struct ca_dwmmc_priv_data *priv = host->priv; |
| 47 | u32 val = readl(priv->sd_dll_reg); |
| 48 | |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 49 | val &= ~SD_CLK_SEL_MASK; |
| 50 | if (host->bus_hz >= 200000000) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 51 | val |= SD_CLK_SEL_200MHZ; |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 52 | else if (host->bus_hz >= 100000000) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 53 | val |= SD_CLK_SEL_100MHZ; |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 54 | |
| 55 | writel(val, priv->sd_dll_reg); |
Siew Chin Lim | d456dfb | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 56 | |
| 57 | return 0; |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void ca_dwmci_board_init(struct dwmci_host *host) |
| 61 | { |
| 62 | struct ca_dwmmc_priv_data *priv = host->priv; |
| 63 | u32 val = readl(priv->io_drv_reg); |
| 64 | |
| 65 | writel(SD_DLL_DEFAULT, priv->sd_dll_reg); |
| 66 | |
| 67 | val &= ~IO_DRV_SD_DS_MASK; |
| 68 | if (priv && priv->ds) |
| 69 | val |= priv->ds << IO_DRV_SD_DS_OFFSET; |
| 70 | writel(val, priv->io_drv_reg); |
| 71 | } |
| 72 | |
| 73 | unsigned int ca_dwmci_get_mmc_clock(struct dwmci_host *host, uint freq) |
| 74 | { |
| 75 | struct ca_dwmmc_priv_data *priv = host->priv; |
| 76 | u8 sd_clk_sel = readl(priv->sd_dll_reg) & SD_CLK_SEL_MASK; |
| 77 | u8 clk_div; |
| 78 | |
| 79 | switch (sd_clk_sel) { |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 80 | case SD_CLK_SEL_50MHZ: |
| 81 | clk_div = 4; |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 82 | break; |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 83 | case SD_CLK_SEL_100MHZ: |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 84 | clk_div = 2; |
| 85 | break; |
| 86 | default: |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 87 | clk_div = 1; |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return SD_SCLK_MAX / clk_div / (host->div + 1); |
| 91 | } |
| 92 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 93 | static int ca_dwmmc_of_to_plat(struct udevice *dev) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 94 | { |
| 95 | struct ca_dwmmc_priv_data *priv = dev_get_priv(dev); |
| 96 | struct dwmci_host *host = &priv->host; |
| 97 | u32 tmp; |
| 98 | |
| 99 | host->name = dev->name; |
| 100 | host->dev_index = 0; |
| 101 | |
| 102 | host->buswidth = dev_read_u32_default(dev, "bus-width", 1); |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 103 | host->bus_hz = dev_read_u32_default(dev, "max-frequency", 50000000); |
| 104 | priv->ds = dev_read_u32_default(dev, "io_ds", 0x33); |
| 105 | host->fifo_mode = dev_read_bool(dev, "fifo-mode"); |
| 106 | |
| 107 | dev_read_u32(dev, "sd_dll_ctrl", &tmp); |
| 108 | priv->sd_dll_reg = map_sysmem((uintptr_t)tmp, sizeof(uintptr_t)); |
| 109 | if (!priv->sd_dll_reg) |
| 110 | return -EINVAL; |
| 111 | |
| 112 | dev_read_u32(dev, "io_drv_ctrl", &tmp); |
| 113 | priv->io_drv_reg = map_sysmem((uintptr_t)tmp, sizeof(uintptr_t)); |
| 114 | if (!priv->io_drv_reg) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | host->ioaddr = dev_read_addr_ptr(dev); |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 118 | if (!host->ioaddr) |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 119 | return -EINVAL; |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 120 | |
| 121 | host->priv = priv; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | struct dm_mmc_ops ca_dwmci_dm_ops; |
| 127 | |
| 128 | static int ca_dwmmc_probe(struct udevice *dev) |
| 129 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 130 | struct ca_mmc_plat *plat = dev_get_plat(dev); |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 131 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 132 | struct ca_dwmmc_priv_data *priv = dev_get_priv(dev); |
| 133 | struct dwmci_host *host = &priv->host; |
| 134 | |
| 135 | memcpy(&ca_dwmci_dm_ops, &dm_dwmci_ops, sizeof(struct dm_mmc_ops)); |
| 136 | |
| 137 | dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, MIN_FREQ); |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 138 | if (host->buswidth == 1) |
| 139 | (&plat->cfg)->host_caps &= ~(MMC_MODE_8BIT | MMC_MODE_4BIT); |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 140 | |
| 141 | host->mmc = &plat->mmc; |
| 142 | host->mmc->priv = &priv->host; |
| 143 | upriv->mmc = host->mmc; |
| 144 | host->mmc->dev = dev; |
| 145 | host->clksel = ca_dwmci_clksel; |
| 146 | host->board_init = ca_dwmci_board_init; |
| 147 | host->get_mmc_clk = ca_dwmci_get_mmc_clock; |
| 148 | |
| 149 | return dwmci_probe(dev); |
| 150 | } |
| 151 | |
| 152 | static int ca_dwmmc_bind(struct udevice *dev) |
| 153 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 154 | struct ca_mmc_plat *plat = dev_get_plat(dev); |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 155 | |
| 156 | return dwmci_bind(dev, &plat->mmc, &plat->cfg); |
| 157 | } |
| 158 | |
| 159 | static const struct udevice_id ca_dwmmc_ids[] = { |
Arthur Li | dbd8a8d | 2020-05-21 17:24:22 -0700 | [diff] [blame] | 160 | { .compatible = "cortina,ca-mmc" }, |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 161 | { } |
| 162 | }; |
| 163 | |
| 164 | U_BOOT_DRIVER(ca_dwmmc_drv) = { |
| 165 | .name = "cortina_dwmmc", |
| 166 | .id = UCLASS_MMC, |
| 167 | .of_match = ca_dwmmc_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 168 | .of_to_plat = ca_dwmmc_of_to_plat, |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 169 | .bind = ca_dwmmc_bind, |
| 170 | .ops = &ca_dwmci_dm_ops, |
| 171 | .probe = ca_dwmmc_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 172 | .priv_auto = sizeof(struct ca_dwmmc_priv_data), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 173 | .plat_auto = sizeof(struct ca_mmc_plat), |
Arthur Li | d55e4e7 | 2020-02-20 18:19:35 -0800 | [diff] [blame] | 174 | }; |