blob: 2b79356a20ed11afcbae5597957f89b33a51a602 [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>
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 Li171ca362020-05-21 17:24:22 -070022#define SD_CLK_SEL_50MHZ (0x0)
Arthur Li055e18e2020-02-20 18:19:35 -080023
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
29DECLARE_GLOBAL_DATA_PTR;
30
31struct ca_mmc_plat {
32 struct mmc_config cfg;
33 struct mmc mmc;
34};
35
36struct 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 Limc51e7e12020-12-24 18:21:03 +080043static int ca_dwmci_clksel(struct dwmci_host *host)
Arthur Li055e18e2020-02-20 18:19:35 -080044{
45 struct ca_dwmmc_priv_data *priv = host->priv;
46 u32 val = readl(priv->sd_dll_reg);
47
Arthur Li171ca362020-05-21 17:24:22 -070048 val &= ~SD_CLK_SEL_MASK;
49 if (host->bus_hz >= 200000000)
Arthur Li055e18e2020-02-20 18:19:35 -080050 val |= SD_CLK_SEL_200MHZ;
Arthur Li171ca362020-05-21 17:24:22 -070051 else if (host->bus_hz >= 100000000)
Arthur Li055e18e2020-02-20 18:19:35 -080052 val |= SD_CLK_SEL_100MHZ;
Arthur Li055e18e2020-02-20 18:19:35 -080053
54 writel(val, priv->sd_dll_reg);
Siew Chin Limc51e7e12020-12-24 18:21:03 +080055
56 return 0;
Arthur Li055e18e2020-02-20 18:19:35 -080057}
58
59static 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
72unsigned 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 Li171ca362020-05-21 17:24:22 -070079 case SD_CLK_SEL_50MHZ:
80 clk_div = 4;
Arthur Li055e18e2020-02-20 18:19:35 -080081 break;
Arthur Li171ca362020-05-21 17:24:22 -070082 case SD_CLK_SEL_100MHZ:
Arthur Li055e18e2020-02-20 18:19:35 -080083 clk_div = 2;
84 break;
85 default:
Arthur Li171ca362020-05-21 17:24:22 -070086 clk_div = 1;
Arthur Li055e18e2020-02-20 18:19:35 -080087 }
88
89 return SD_SCLK_MAX / clk_div / (host->div + 1);
90}
91
Simon Glassaad29ae2020-12-03 16:55:21 -070092static int ca_dwmmc_of_to_plat(struct udevice *dev)
Arthur Li055e18e2020-02-20 18:19:35 -080093{
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 Li055e18e2020-02-20 18:19:35 -0800102 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 Li171ca362020-05-21 17:24:22 -0700117 if (!host->ioaddr)
Arthur Li055e18e2020-02-20 18:19:35 -0800118 return -EINVAL;
Arthur Li055e18e2020-02-20 18:19:35 -0800119
120 host->priv = priv;
121
122 return 0;
123}
124
125struct dm_mmc_ops ca_dwmci_dm_ops;
126
127static int ca_dwmmc_probe(struct udevice *dev)
128{
Simon Glassfa20e932020-12-03 16:55:20 -0700129 struct ca_mmc_plat *plat = dev_get_plat(dev);
Arthur Li055e18e2020-02-20 18:19:35 -0800130 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 Li171ca362020-05-21 17:24:22 -0700137 if (host->buswidth == 1)
138 (&plat->cfg)->host_caps &= ~(MMC_MODE_8BIT | MMC_MODE_4BIT);
Arthur Li055e18e2020-02-20 18:19:35 -0800139
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
151static int ca_dwmmc_bind(struct udevice *dev)
152{
Simon Glassfa20e932020-12-03 16:55:20 -0700153 struct ca_mmc_plat *plat = dev_get_plat(dev);
Arthur Li055e18e2020-02-20 18:19:35 -0800154
155 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
156}
157
158static const struct udevice_id ca_dwmmc_ids[] = {
Arthur Li171ca362020-05-21 17:24:22 -0700159 { .compatible = "cortina,ca-mmc" },
Arthur Li055e18e2020-02-20 18:19:35 -0800160 { }
161};
162
163U_BOOT_DRIVER(ca_dwmmc_drv) = {
164 .name = "cortina_dwmmc",
165 .id = UCLASS_MMC,
166 .of_match = ca_dwmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700167 .of_to_plat = ca_dwmmc_of_to_plat,
Arthur Li055e18e2020-02-20 18:19:35 -0800168 .bind = ca_dwmmc_bind,
169 .ops = &ca_dwmci_dm_ops,
170 .probe = ca_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700171 .priv_auto = sizeof(struct ca_dwmmc_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700172 .plat_auto = sizeof(struct ca_mmc_plat),
Arthur Li055e18e2020-02-20 18:19:35 -0800173};