blob: 5ef2781eedfed0bdd4b7675c35a55630b1dfb1e8 [file] [log] [blame]
Tianrui Wei8cfc7c52021-07-01 12:54:20 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2009 SAMSUNG Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
5 * Jaehoon Chung <jh80.chung@samsung.com>
6 * Portions Copyright 2011-2019 NVIDIA Corporation
7 * Portions Copyright 2021 Tianrui Wei
8 * This file is adapted from tegra_mmc.c
9 * Tianrui Wei <tianrui-wei@outlook.com>
10 */
11
12#include <asm/gpio.h>
13#include <asm/io.h>
Tianrui Wei8cfc7c52021-07-01 12:54:20 +080014#include <div64.h>
15#include <dm.h>
16#include <errno.h>
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/types.h>
21#include <linux/sizes.h>
22#include <log.h>
23#include <mmc.h>
24
Tianrui Wei8cfc7c52021-07-01 12:54:20 +080025#define PITON_MMC_DUMMY_F_MAX 20000000
26#define PITON_MMC_DUMMY_F_MIN 10000000
27#define PITON_MMC_DUMMY_CAPACITY SZ_4G << 3
28#define PITON_MMC_DUMMY_B_MAX SZ_4G
29
30struct piton_mmc_plat {
31 struct mmc_config cfg;
32 struct mmc mmc;
33};
34
35struct piton_mmc_priv {
36 void __iomem *base_addr;
37};
38
39static int piton_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
40 struct mmc_data *data)
41{
42 if (!data)
43 return 0;
44
45 struct piton_mmc_priv *priv = dev_get_priv(dev);
46 u32 *buff, *start_addr, *write_src;
47 size_t byte_cnt, start_block;
48
49 buff = (u32 *)data->dest;
50 write_src = (u32 *)data->src;
51 start_block = cmd->cmdarg;
52 start_addr = priv->base_addr + start_block;
53
54 /* if there is a read */
55 for (byte_cnt = data->blocks * data->blocksize; byte_cnt;
56 byte_cnt -= sizeof(u32)) {
57 if (data->flags & MMC_DATA_READ) {
58 *buff++ = readl(start_addr++);
59 }
60 else if (data->flags & MMC_DATA_WRITE) {
61 writel(*write_src++,start_addr++);
62 }
63 }
64 return 0;
65}
66
67static int piton_mmc_ofdata_to_platdata(struct udevice *dev)
68{
69 struct piton_mmc_priv *priv = dev_get_priv(dev);
70 struct piton_mmc_plat *plat = dev_get_plat(dev);
71 struct mmc_config *cfg;
72 struct mmc *mmc;
73 struct blk_desc *bdesc;
74
Johan Jonker8d5d8e02023-03-13 01:32:04 +010075 priv->base_addr = dev_read_addr_ptr(dev);
Tianrui Wei8cfc7c52021-07-01 12:54:20 +080076 cfg = &plat->cfg;
77 cfg->name = "PITON MMC";
78 cfg->host_caps = MMC_MODE_8BIT;
79 cfg->f_max = PITON_MMC_DUMMY_F_MAX;
80 cfg->f_min = PITON_MMC_DUMMY_F_MIN;
81 cfg->voltages = MMC_VDD_21_22;
82
83 mmc = &plat->mmc;
84 mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
85 mmc->capacity_user = PITON_MMC_DUMMY_CAPACITY;
86 mmc->capacity_user *= mmc->read_bl_len;
87 mmc->capacity_boot = 0;
88 mmc->capacity_rpmb = 0;
89 for (int i = 0; i < 4; i++)
90 mmc->capacity_gp[i] = 0;
91 mmc->capacity = PITON_MMC_DUMMY_CAPACITY;
92 mmc->has_init = 1;
93
94 bdesc = mmc_get_blk_desc(mmc);
95 bdesc->lun = 0;
96 bdesc->hwpart = 0;
97 bdesc->type = 0;
98 bdesc->blksz = mmc->read_bl_len;
99 bdesc->log2blksz = LOG2(bdesc->blksz);
100 bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
101
102 return 0;
103}
104
105static int piton_mmc_getcd(struct udevice *dev)
106{
107 return 1;
108}
109
110static const struct dm_mmc_ops piton_mmc_ops = {
111 .send_cmd = piton_mmc_send_cmd,
112 .get_cd = piton_mmc_getcd,
113};
114
115static int piton_mmc_probe(struct udevice *dev)
116{
117 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
118 struct piton_mmc_plat *plat = dev_get_plat(dev);
119 struct mmc_config *cfg = &plat->cfg;
120
121 cfg->name = dev->name;
122 upriv->mmc = &plat->mmc;
123 upriv->mmc->has_init = 1;
124 upriv->mmc->capacity = PITON_MMC_DUMMY_CAPACITY;
125 upriv->mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
126 return 0;
127}
128
129static int piton_mmc_bind(struct udevice *dev)
130{
131 struct piton_mmc_plat *plat = dev_get_plat(dev);
132 struct mmc_config *cfg = &plat->cfg;
133
134 cfg->name = dev->name;
135 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
136 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
137 cfg->f_min = PITON_MMC_DUMMY_F_MIN;
138 cfg->f_max = PITON_MMC_DUMMY_F_MAX;
139 cfg->b_max = MMC_MAX_BLOCK_LEN;
140
141 return mmc_bind(dev, &plat->mmc, cfg);
142}
143
144static const struct udevice_id piton_mmc_ids[] = {
145 {.compatible = "openpiton,piton-mmc"},
146 {/* sentinel */}
147};
148
149U_BOOT_DRIVER(piton_mmc_drv) = {
150 .name = "piton_mmc",
151 .id = UCLASS_MMC,
152 .of_match = piton_mmc_ids,
153 .of_to_plat = piton_mmc_ofdata_to_platdata,
154 .bind = piton_mmc_bind,
155 .probe = piton_mmc_probe,
156 .ops = &piton_mmc_ops,
157 .plat_auto = sizeof(struct piton_mmc_plat),
158 .priv_auto = sizeof(struct piton_mmc_priv),
159};