blob: a17ed8c11cbef70ca80c34babee54abc465fa58d [file] [log] [blame]
Arthur Li055e18e2020-02-20 18:19:35 -08001// 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 Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Arthur Li055e18e2020-02-20 18:19:35 -080011#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 Li171ca362020-05-21 17:24:22 -070023#define SD_CLK_SEL_50MHZ (0x0)
Arthur Li055e18e2020-02-20 18:19:35 -080024
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
30DECLARE_GLOBAL_DATA_PTR;
31
32struct ca_mmc_plat {
33 struct mmc_config cfg;
34 struct mmc mmc;
35};
36
37struct 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 Limc51e7e12020-12-24 18:21:03 +080044static int ca_dwmci_clksel(struct dwmci_host *host)
Arthur Li055e18e2020-02-20 18:19:35 -080045{
46 struct ca_dwmmc_priv_data *priv = host->priv;
47 u32 val = readl(priv->sd_dll_reg);
48
Arthur Li171ca362020-05-21 17:24:22 -070049 val &= ~SD_CLK_SEL_MASK;
50 if (host->bus_hz >= 200000000)
Arthur Li055e18e2020-02-20 18:19:35 -080051 val |= SD_CLK_SEL_200MHZ;
Arthur Li171ca362020-05-21 17:24:22 -070052 else if (host->bus_hz >= 100000000)
Arthur Li055e18e2020-02-20 18:19:35 -080053 val |= SD_CLK_SEL_100MHZ;
Arthur Li055e18e2020-02-20 18:19:35 -080054
55 writel(val, priv->sd_dll_reg);
Siew Chin Limc51e7e12020-12-24 18:21:03 +080056
57 return 0;
Arthur Li055e18e2020-02-20 18:19:35 -080058}
59
60static 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
73unsigned 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 Li171ca362020-05-21 17:24:22 -070080 case SD_CLK_SEL_50MHZ:
81 clk_div = 4;
Arthur Li055e18e2020-02-20 18:19:35 -080082 break;
Arthur Li171ca362020-05-21 17:24:22 -070083 case SD_CLK_SEL_100MHZ:
Arthur Li055e18e2020-02-20 18:19:35 -080084 clk_div = 2;
85 break;
86 default:
Arthur Li171ca362020-05-21 17:24:22 -070087 clk_div = 1;
Arthur Li055e18e2020-02-20 18:19:35 -080088 }
89
90 return SD_SCLK_MAX / clk_div / (host->div + 1);
91}
92
Simon Glassaad29ae2020-12-03 16:55:21 -070093static int ca_dwmmc_of_to_plat(struct udevice *dev)
Arthur Li055e18e2020-02-20 18:19:35 -080094{
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 Li055e18e2020-02-20 18:19:35 -0800103 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 Li171ca362020-05-21 17:24:22 -0700118 if (!host->ioaddr)
Arthur Li055e18e2020-02-20 18:19:35 -0800119 return -EINVAL;
Arthur Li055e18e2020-02-20 18:19:35 -0800120
121 host->priv = priv;
122
123 return 0;
124}
125
126struct dm_mmc_ops ca_dwmci_dm_ops;
127
128static int ca_dwmmc_probe(struct udevice *dev)
129{
Simon Glassfa20e932020-12-03 16:55:20 -0700130 struct ca_mmc_plat *plat = dev_get_plat(dev);
Arthur Li055e18e2020-02-20 18:19:35 -0800131 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 Li171ca362020-05-21 17:24:22 -0700138 if (host->buswidth == 1)
139 (&plat->cfg)->host_caps &= ~(MMC_MODE_8BIT | MMC_MODE_4BIT);
Arthur Li055e18e2020-02-20 18:19:35 -0800140
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
152static int ca_dwmmc_bind(struct udevice *dev)
153{
Simon Glassfa20e932020-12-03 16:55:20 -0700154 struct ca_mmc_plat *plat = dev_get_plat(dev);
Arthur Li055e18e2020-02-20 18:19:35 -0800155
156 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
157}
158
159static const struct udevice_id ca_dwmmc_ids[] = {
Arthur Li171ca362020-05-21 17:24:22 -0700160 { .compatible = "cortina,ca-mmc" },
Arthur Li055e18e2020-02-20 18:19:35 -0800161 { }
162};
163
164U_BOOT_DRIVER(ca_dwmmc_drv) = {
165 .name = "cortina_dwmmc",
166 .id = UCLASS_MMC,
167 .of_match = ca_dwmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700168 .of_to_plat = ca_dwmmc_of_to_plat,
Arthur Li055e18e2020-02-20 18:19:35 -0800169 .bind = ca_dwmmc_bind,
170 .ops = &ca_dwmci_dm_ops,
171 .probe = ca_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700172 .priv_auto = sizeof(struct ca_dwmmc_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700173 .plat_auto = sizeof(struct ca_mmc_plat),
Arthur Li055e18e2020-02-20 18:19:35 -0800174};