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