Skip to content

Commit 46b723d

Browse files
ludovicbarrestorulf
authored andcommitted
mmc: mmci: add stm32 sdmmc variant
This patch adds a stm32 sdmmc variant, rev 1.1. Introduces a new Manufacturer id "0x53, ascii 'S' to define new stm32 sdmmc family with clean range of amba revision/configurations bits (corresponding to sdmmc_ver register with major/minor fields). Add 2 variants properties: -dma_lli, to enable link list support. -stm32_idmabsize_mask, defines the range of SDMMC_IDMABSIZER register which specify the number bytes per buffer. DT properties for sdmmc: -Indicate signal directions (only one property for d0dir, d123dir, cmd_dir) -Select command and data phase relation. -Select "clock in" from an external driver. Signed-off-by: Ludovic Barre <ludovic.barre@st.com> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent 6202289 commit 46b723d

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

drivers/mmc/host/Kconfig

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ config MMC_QCOM_DML
3434

3535
if unsure, say N.
3636

37+
config MMC_STM32_SDMMC
38+
bool "STMicroelectronics STM32 SDMMC Controller"
39+
depends on MMC_ARMMMCI
40+
default y
41+
help
42+
This selects the STMicroelectronics STM32 SDMMC host controller.
43+
If you have a STM32 sdmmc host with internal DMA say Y here.
44+
45+
If unsure, say N.
46+
3747
config MMC_PXA
3848
tristate "Intel PXA25x/26x/27x Multimedia Card Interface support"
3949
depends on ARCH_PXA

drivers/mmc/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
obj-$(CONFIG_MMC_ARMMMCI) += armmmci.o
77
armmmci-y := mmci.o
88
armmmci-$(CONFIG_MMC_QCOM_DML) += mmci_qcom_dml.o
9+
armmmci-$(CONFIG_MMC_STM32_SDMMC) += mmci_stm32_sdmmc.o
910
obj-$(CONFIG_MMC_PXA) += pxamci.o
1011
obj-$(CONFIG_MMC_MXC) += mxcmmc.o
1112
obj-$(CONFIG_MMC_MXS) += mxs-mmc.o

drivers/mmc/host/mmci.c

+36
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ void mmci_variant_init(struct mmci_host *host);
5252
static inline void mmci_variant_init(struct mmci_host *host) {}
5353
#endif
5454

55+
#ifdef CONFIG_MMC_STM32_SDMMC
56+
void sdmmc_variant_init(struct mmci_host *host);
57+
#else
58+
static inline void sdmmc_variant_init(struct mmci_host *host) {}
59+
#endif
60+
5561
static unsigned int fmax = 515633;
5662

5763
static struct variant_data variant_arm = {
@@ -259,6 +265,25 @@ static struct variant_data variant_stm32 = {
259265
.init = mmci_variant_init,
260266
};
261267

268+
static struct variant_data variant_stm32_sdmmc = {
269+
.fifosize = 16 * 4,
270+
.fifohalfsize = 8 * 4,
271+
.f_max = 208000000,
272+
.stm32_clkdiv = true,
273+
.cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
274+
.cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
275+
.cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
276+
.cmdreg_srsp = MCI_CPSM_STM32_SRSP,
277+
.data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
278+
.irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
279+
.datactrl_first = true,
280+
.datacnt_useless = true,
281+
.datalength_bits = 25,
282+
.datactrl_blocksz = 14,
283+
.stm32_idmabsize_mask = GENMASK(12, 5),
284+
.init = sdmmc_variant_init,
285+
};
286+
262287
static struct variant_data variant_qcom = {
263288
.fifosize = 16 * 4,
264289
.fifohalfsize = 8 * 4,
@@ -1736,6 +1761,12 @@ static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
17361761
host->pwr_reg_add |= MCI_ST_CMDDIREN;
17371762
if (of_get_property(np, "st,sig-pin-fbclk", NULL))
17381763
host->pwr_reg_add |= MCI_ST_FBCLKEN;
1764+
if (of_get_property(np, "st,sig-dir", NULL))
1765+
host->pwr_reg_add |= MCI_STM32_DIRPOL;
1766+
if (of_get_property(np, "st,neg-edge", NULL))
1767+
host->clk_reg_add |= MCI_STM32_CLK_NEGEDGE;
1768+
if (of_get_property(np, "st,use-ckin", NULL))
1769+
host->clk_reg_add |= MCI_STM32_CLK_SELCKIN;
17391770

17401771
if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
17411772
mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
@@ -2177,6 +2208,11 @@ static const struct amba_id mmci_ids[] = {
21772208
.mask = 0x00ffffff,
21782209
.data = &variant_stm32,
21792210
},
2211+
{
2212+
.id = 0x10153180,
2213+
.mask = 0xf0ffffff,
2214+
.data = &variant_stm32_sdmmc,
2215+
},
21802216
/* Qualcomm variants */
21812217
{
21822218
.id = 0x00051180,

drivers/mmc/host/mmci.h

+5
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ struct mmci_host;
304304
* @start_err: bitmask identifying the STARTBITERR bit inside MMCISTATUS
305305
* register.
306306
* @opendrain: bitmask identifying the OPENDRAIN bit inside MMCIPOWER register
307+
* @dma_lli: true if variant has dma link list feature.
308+
* @stm32_idmabsize_mask: stm32 sdmmc idma buffer size.
307309
*/
308310
struct variant_data {
309311
unsigned int clkreg;
@@ -346,6 +348,8 @@ struct variant_data {
346348
unsigned int irq_pio_mask;
347349
u32 start_err;
348350
u32 opendrain;
351+
u8 dma_lli:1;
352+
u32 stm32_idmabsize_mask;
349353
void (*init)(struct mmci_host *host);
350354
};
351355

@@ -387,6 +391,7 @@ struct mmci_host {
387391
u32 pwr_reg;
388392
u32 pwr_reg_add;
389393
u32 clk_reg;
394+
u32 clk_reg_add;
390395
u32 datactrl_reg;
391396
u32 busy_status;
392397
u32 mask1_reg;

drivers/mmc/host/mmci_stm32_sdmmc.c

+282
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4+
* Author: Ludovic.barre@st.com for STMicroelectronics.
5+
*/
6+
#include <linux/delay.h>
7+
#include <linux/dma-mapping.h>
8+
#include <linux/mmc/host.h>
9+
#include <linux/mmc/card.h>
10+
#include <linux/reset.h>
11+
#include <linux/scatterlist.h>
12+
#include "mmci.h"
13+
14+
#define SDMMC_LLI_BUF_LEN PAGE_SIZE
15+
#define SDMMC_IDMA_BURST BIT(MMCI_STM32_IDMABNDT_SHIFT)
16+
17+
struct sdmmc_lli_desc {
18+
u32 idmalar;
19+
u32 idmabase;
20+
u32 idmasize;
21+
};
22+
23+
struct sdmmc_priv {
24+
dma_addr_t sg_dma;
25+
void *sg_cpu;
26+
};
27+
28+
int sdmmc_idma_validate_data(struct mmci_host *host,
29+
struct mmc_data *data)
30+
{
31+
struct scatterlist *sg;
32+
int i;
33+
34+
/*
35+
* idma has constraints on idmabase & idmasize for each element
36+
* excepted the last element which has no constraint on idmasize
37+
*/
38+
for_each_sg(data->sg, sg, data->sg_len - 1, i) {
39+
if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32)) ||
40+
!IS_ALIGNED(sg_dma_len(data->sg), SDMMC_IDMA_BURST)) {
41+
dev_err(mmc_dev(host->mmc),
42+
"unaligned scatterlist: ofst:%x length:%d\n",
43+
data->sg->offset, data->sg->length);
44+
return -EINVAL;
45+
}
46+
}
47+
48+
if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32))) {
49+
dev_err(mmc_dev(host->mmc),
50+
"unaligned last scatterlist: ofst:%x length:%d\n",
51+
data->sg->offset, data->sg->length);
52+
return -EINVAL;
53+
}
54+
55+
return 0;
56+
}
57+
58+
static int _sdmmc_idma_prep_data(struct mmci_host *host,
59+
struct mmc_data *data)
60+
{
61+
int n_elem;
62+
63+
n_elem = dma_map_sg(mmc_dev(host->mmc),
64+
data->sg,
65+
data->sg_len,
66+
mmc_get_dma_dir(data));
67+
68+
if (!n_elem) {
69+
dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
70+
return -EINVAL;
71+
}
72+
73+
return 0;
74+
}
75+
76+
static int sdmmc_idma_prep_data(struct mmci_host *host,
77+
struct mmc_data *data, bool next)
78+
{
79+
/* Check if job is already prepared. */
80+
if (!next && data->host_cookie == host->next_cookie)
81+
return 0;
82+
83+
return _sdmmc_idma_prep_data(host, data);
84+
}
85+
86+
static void sdmmc_idma_unprep_data(struct mmci_host *host,
87+
struct mmc_data *data, int err)
88+
{
89+
dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
90+
mmc_get_dma_dir(data));
91+
}
92+
93+
static int sdmmc_idma_setup(struct mmci_host *host)
94+
{
95+
struct sdmmc_priv *idma;
96+
97+
idma = devm_kzalloc(mmc_dev(host->mmc), sizeof(*idma), GFP_KERNEL);
98+
if (!idma)
99+
return -ENOMEM;
100+
101+
host->dma_priv = idma;
102+
103+
if (host->variant->dma_lli) {
104+
idma->sg_cpu = dmam_alloc_coherent(mmc_dev(host->mmc),
105+
SDMMC_LLI_BUF_LEN,
106+
&idma->sg_dma, GFP_KERNEL);
107+
if (!idma->sg_cpu) {
108+
dev_err(mmc_dev(host->mmc),
109+
"Failed to alloc IDMA descriptor\n");
110+
return -ENOMEM;
111+
}
112+
host->mmc->max_segs = SDMMC_LLI_BUF_LEN /
113+
sizeof(struct sdmmc_lli_desc);
114+
host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask;
115+
} else {
116+
host->mmc->max_segs = 1;
117+
host->mmc->max_seg_size = host->mmc->max_req_size;
118+
}
119+
120+
return 0;
121+
}
122+
123+
static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
124+
125+
{
126+
struct sdmmc_priv *idma = host->dma_priv;
127+
struct sdmmc_lli_desc *desc = (struct sdmmc_lli_desc *)idma->sg_cpu;
128+
struct mmc_data *data = host->data;
129+
struct scatterlist *sg;
130+
int i;
131+
132+
if (!host->variant->dma_lli || data->sg_len == 1) {
133+
writel_relaxed(sg_dma_address(data->sg),
134+
host->base + MMCI_STM32_IDMABASE0R);
135+
writel_relaxed(MMCI_STM32_IDMAEN,
136+
host->base + MMCI_STM32_IDMACTRLR);
137+
return 0;
138+
}
139+
140+
for_each_sg(data->sg, sg, data->sg_len, i) {
141+
desc[i].idmalar = (i + 1) * sizeof(struct sdmmc_lli_desc);
142+
desc[i].idmalar |= MMCI_STM32_ULA | MMCI_STM32_ULS
143+
| MMCI_STM32_ABR;
144+
desc[i].idmabase = sg_dma_address(sg);
145+
desc[i].idmasize = sg_dma_len(sg);
146+
}
147+
148+
/* notice the end of link list */
149+
desc[data->sg_len - 1].idmalar &= ~MMCI_STM32_ULA;
150+
151+
dma_wmb();
152+
writel_relaxed(idma->sg_dma, host->base + MMCI_STM32_IDMABAR);
153+
writel_relaxed(desc[0].idmalar, host->base + MMCI_STM32_IDMALAR);
154+
writel_relaxed(desc[0].idmabase, host->base + MMCI_STM32_IDMABASE0R);
155+
writel_relaxed(desc[0].idmasize, host->base + MMCI_STM32_IDMABSIZER);
156+
writel_relaxed(MMCI_STM32_IDMAEN | MMCI_STM32_IDMALLIEN,
157+
host->base + MMCI_STM32_IDMACTRLR);
158+
159+
return 0;
160+
}
161+
162+
static void sdmmc_idma_finalize(struct mmci_host *host, struct mmc_data *data)
163+
{
164+
writel_relaxed(0, host->base + MMCI_STM32_IDMACTRLR);
165+
}
166+
167+
static void mmci_sdmmc_set_clkreg(struct mmci_host *host, unsigned int desired)
168+
{
169+
unsigned int clk = 0, ddr = 0;
170+
171+
if (host->mmc->ios.timing == MMC_TIMING_MMC_DDR52 ||
172+
host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
173+
ddr = MCI_STM32_CLK_DDR;
174+
175+
/*
176+
* cclk = mclk / (2 * clkdiv)
177+
* clkdiv 0 => bypass
178+
* in ddr mode bypass is not possible
179+
*/
180+
if (desired) {
181+
if (desired >= host->mclk && !ddr) {
182+
host->cclk = host->mclk;
183+
} else {
184+
clk = DIV_ROUND_UP(host->mclk, 2 * desired);
185+
if (clk > MCI_STM32_CLK_CLKDIV_MSK)
186+
clk = MCI_STM32_CLK_CLKDIV_MSK;
187+
host->cclk = host->mclk / (2 * clk);
188+
}
189+
} else {
190+
/*
191+
* while power-on phase the clock can't be define to 0,
192+
* Only power-off and power-cyc deactivate the clock.
193+
* if desired clock is 0, set max divider
194+
*/
195+
clk = MCI_STM32_CLK_CLKDIV_MSK;
196+
host->cclk = host->mclk / (2 * clk);
197+
}
198+
199+
/* Set actual clock for debug */
200+
if (host->mmc->ios.power_mode == MMC_POWER_ON)
201+
host->mmc->actual_clock = host->cclk;
202+
else
203+
host->mmc->actual_clock = 0;
204+
205+
if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
206+
clk |= MCI_STM32_CLK_WIDEBUS_4;
207+
if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
208+
clk |= MCI_STM32_CLK_WIDEBUS_8;
209+
210+
clk |= MCI_STM32_CLK_HWFCEN;
211+
clk |= host->clk_reg_add;
212+
clk |= ddr;
213+
214+
/*
215+
* SDMMC_FBCK is selected when an external Delay Block is needed
216+
* with SDR104.
217+
*/
218+
if (host->mmc->ios.timing >= MMC_TIMING_UHS_SDR50) {
219+
clk |= MCI_STM32_CLK_BUSSPEED;
220+
if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) {
221+
clk &= ~MCI_STM32_CLK_SEL_MSK;
222+
clk |= MCI_STM32_CLK_SELFBCK;
223+
}
224+
}
225+
226+
mmci_write_clkreg(host, clk);
227+
}
228+
229+
static void mmci_sdmmc_set_pwrreg(struct mmci_host *host, unsigned int pwr)
230+
{
231+
struct mmc_ios ios = host->mmc->ios;
232+
233+
pwr = host->pwr_reg_add;
234+
235+
if (ios.power_mode == MMC_POWER_OFF) {
236+
/* Only a reset could power-off sdmmc */
237+
reset_control_assert(host->rst);
238+
udelay(2);
239+
reset_control_deassert(host->rst);
240+
241+
/*
242+
* Set the SDMMC in Power-cycle state.
243+
* This will make that the SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK
244+
* are driven low, to prevent the Card from being supplied
245+
* through the signal lines.
246+
*/
247+
mmci_write_pwrreg(host, MCI_STM32_PWR_CYC | pwr);
248+
} else if (ios.power_mode == MMC_POWER_ON) {
249+
/*
250+
* After power-off (reset): the irq mask defined in probe
251+
* functionis lost
252+
* ault irq mask (probe) must be activated
253+
*/
254+
writel(MCI_IRQENABLE | host->variant->start_err,
255+
host->base + MMCIMASK0);
256+
257+
/*
258+
* After a power-cycle state, we must set the SDMMC in
259+
* Power-off. The SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK are
260+
* driven high. Then we can set the SDMMC to Power-on state
261+
*/
262+
mmci_write_pwrreg(host, MCI_PWR_OFF | pwr);
263+
mdelay(1);
264+
mmci_write_pwrreg(host, MCI_PWR_ON | pwr);
265+
}
266+
}
267+
268+
static struct mmci_host_ops sdmmc_variant_ops = {
269+
.validate_data = sdmmc_idma_validate_data,
270+
.prep_data = sdmmc_idma_prep_data,
271+
.unprep_data = sdmmc_idma_unprep_data,
272+
.dma_setup = sdmmc_idma_setup,
273+
.dma_start = sdmmc_idma_start,
274+
.dma_finalize = sdmmc_idma_finalize,
275+
.set_clkreg = mmci_sdmmc_set_clkreg,
276+
.set_pwrreg = mmci_sdmmc_set_pwrreg,
277+
};
278+
279+
void sdmmc_variant_init(struct mmci_host *host)
280+
{
281+
host->ops = &sdmmc_variant_ops;
282+
}

0 commit comments

Comments
 (0)