1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale eSDHC ColdFire family controller driver, platform bus.
5 * Copyright (c) 2020 Timesys Corporation
6 * Author: Angelo Dureghello <angelo.dureghello@timesys.it>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/platform_data/mmc-esdhc-mcf.h>
12 #include <linux/mmc/mmc.h>
13 #include "sdhci-pltfm.h"
14 #include "sdhci-esdhc.h"
16 #define ESDHC_PROCTL_D3CD 0x08
17 #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
18 #define ESDHC_DEFAULT_HOST_CONTROL 0x28
21 * Freescale eSDHC has DMA ERR flag at bit 28, not as std spec says, bit 25.
23 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR BIT(28)
25 struct pltfm_mcf_data
{
30 int current_bus_width
;
33 static inline void esdhc_mcf_buffer_swap32(u32
*buf
, int len
)
40 for (i
= 0; i
< len
; i
++) {
46 static inline void esdhc_clrset_be(struct sdhci_host
*host
,
47 u32 mask
, u32 val
, int reg
)
49 void __iomem
*base
= host
->ioaddr
+ (reg
& ~3);
50 u8 shift
= (reg
& 3) << 3;
55 if (reg
== SDHCI_HOST_CONTROL
)
56 val
|= ESDHC_PROCTL_D3CD
;
58 writel((readl(base
) & ~mask
) | val
, base
);
62 * Note: mcf is big-endian, single bytes need to be accessed at big endian
65 static void esdhc_mcf_writeb_be(struct sdhci_host
*host
, u8 val
, int reg
)
67 void __iomem
*base
= host
->ioaddr
+ (reg
& ~3);
68 u8 shift
= (reg
& 3) << 3;
69 u32 mask
= ~(0xff << shift
);
71 if (reg
== SDHCI_HOST_CONTROL
) {
72 u32 host_ctrl
= ESDHC_DEFAULT_HOST_CONTROL
;
73 u8 dma_bits
= (val
& SDHCI_CTRL_DMA_MASK
) >> 3;
74 u8 tmp
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
+ 1);
80 * Recomposition needed, restore always endianness and
81 * keep D3CD and AI, just setting bus width.
84 host_ctrl
|= (dma_bits
<< 8);
85 writel(host_ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
90 writel((readl(base
) & mask
) | (val
<< shift
), base
);
93 static void esdhc_mcf_writew_be(struct sdhci_host
*host
, u16 val
, int reg
)
95 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
96 struct pltfm_mcf_data
*mcf_data
= sdhci_pltfm_priv(pltfm_host
);
97 void __iomem
*base
= host
->ioaddr
+ (reg
& ~3);
98 u8 shift
= (reg
& 3) << 3;
99 u32 mask
= ~(0xffff << shift
);
102 case SDHCI_TRANSFER_MODE
:
103 mcf_data
->aside
= val
;
106 if (host
->cmd
->opcode
== MMC_STOP_TRANSMISSION
)
107 val
|= SDHCI_CMD_ABORTCMD
;
110 * As for the fsl driver,
111 * we have to set the mode in a single write here.
113 writel(val
<< 16 | mcf_data
->aside
,
114 host
->ioaddr
+ SDHCI_TRANSFER_MODE
);
118 writel((readl(base
) & mask
) | (val
<< shift
), base
);
121 static void esdhc_mcf_writel_be(struct sdhci_host
*host
, u32 val
, int reg
)
123 writel(val
, host
->ioaddr
+ reg
);
126 static u8
esdhc_mcf_readb_be(struct sdhci_host
*host
, int reg
)
128 if (reg
== SDHCI_HOST_CONTROL
) {
129 u8 __iomem
*base
= host
->ioaddr
+ (reg
& ~3);
130 u16 val
= readw(base
+ 2);
131 u8 dma_bits
= (val
>> 5) & SDHCI_CTRL_DMA_MASK
;
132 u8 host_ctrl
= val
& 0xff;
134 host_ctrl
&= ~SDHCI_CTRL_DMA_MASK
;
135 host_ctrl
|= dma_bits
;
140 return readb(host
->ioaddr
+ (reg
^ 0x3));
143 static u16
esdhc_mcf_readw_be(struct sdhci_host
*host
, int reg
)
146 * For SDHCI_HOST_VERSION, sdhci specs defines 0xFE,
147 * a wrong offset for us, we are at 0xFC.
149 if (reg
== SDHCI_HOST_VERSION
)
152 return readw(host
->ioaddr
+ (reg
^ 0x2));
155 static u32
esdhc_mcf_readl_be(struct sdhci_host
*host
, int reg
)
159 val
= readl(host
->ioaddr
+ reg
);
162 * RM (25.3.9) sd pin clock must never exceed 25Mhz.
163 * So forcing legacy mode at 25Mhz.
165 if (unlikely(reg
== SDHCI_CAPABILITIES
))
166 val
&= ~SDHCI_CAN_DO_HISPD
;
168 if (unlikely(reg
== SDHCI_INT_STATUS
)) {
169 if (val
& ESDHC_INT_VENDOR_SPEC_DMA_ERR
) {
170 val
&= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR
;
171 val
|= SDHCI_INT_ADMA_ERROR
;
178 static unsigned int esdhc_mcf_get_max_timeout_count(struct sdhci_host
*host
)
183 static void esdhc_mcf_set_timeout(struct sdhci_host
*host
,
184 struct mmc_command
*cmd
)
186 /* Use maximum timeout counter */
187 esdhc_clrset_be(host
, ESDHC_SYS_CTRL_DTOCV_MASK
, 0xE,
188 SDHCI_TIMEOUT_CONTROL
);
191 static void esdhc_mcf_reset(struct sdhci_host
*host
, u8 mask
)
193 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
194 struct pltfm_mcf_data
*mcf_data
= sdhci_pltfm_priv(pltfm_host
);
196 sdhci_reset(host
, mask
);
198 esdhc_clrset_be(host
, ESDHC_CTRL_BUSWIDTH_MASK
,
199 mcf_data
->current_bus_width
, SDHCI_HOST_CONTROL
);
201 sdhci_writel(host
, host
->ier
, SDHCI_INT_ENABLE
);
202 sdhci_writel(host
, host
->ier
, SDHCI_SIGNAL_ENABLE
);
205 static unsigned int esdhc_mcf_pltfm_get_max_clock(struct sdhci_host
*host
)
207 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
209 return pltfm_host
->clock
;
212 static unsigned int esdhc_mcf_pltfm_get_min_clock(struct sdhci_host
*host
)
214 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
216 return pltfm_host
->clock
/ 256 / 16;
219 static void esdhc_mcf_pltfm_set_clock(struct sdhci_host
*host
,
222 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
223 unsigned long *pll_dr
= (unsigned long *)MCF_PLL_DR
;
224 u32 fvco
, fsys
, fesdhc
, temp
;
225 const int sdclkfs
[] = {2, 4, 8, 16, 32, 64, 128, 256};
226 int delta
, old_delta
= clock
;
230 host
->mmc
->actual_clock
= 0;
235 * ColdFire eSDHC clock.s
237 * pll -+-> / outdiv1 --> fsys
238 * +-> / outdiv3 --> eSDHC clock ---> / SDCCLKFS / DVS
240 * mcf5441x datasheet says:
241 * (8.1.2) eSDHC should be 40 MHz max
242 * (25.3.9) eSDHC input is, as example, 96 Mhz ...
243 * (25.3.9) sd pin clock must never exceed 25Mhz
245 * fvco = fsys * outdvi1 + 1
246 * fshdc = fvco / outdiv3 + 1
248 temp
= readl(pll_dr
);
249 fsys
= pltfm_host
->clock
;
250 fvco
= fsys
* ((temp
& 0x1f) + 1);
251 fesdhc
= fvco
/ (((temp
>> 10) & 0x1f) + 1);
253 for (i
= 0; i
< 8; ++i
) {
254 int result
= fesdhc
/ sdclkfs
[i
];
256 for (q
= 1; q
< 17; ++q
) {
257 int finale
= result
/ q
;
259 delta
= abs(clock
- finale
);
261 if (delta
< old_delta
) {
270 * Apply divisors and re-enable all the clocks
272 temp
= ((sdclkfs
[ri
] >> 1) << 8) | ((rq
- 1) << 4) |
273 (ESDHC_CLOCK_IPGEN
| ESDHC_CLOCK_HCKEN
| ESDHC_CLOCK_PEREN
);
274 esdhc_clrset_be(host
, 0x0000fff7, temp
, SDHCI_CLOCK_CONTROL
);
276 host
->mmc
->actual_clock
= clock
;
281 static void esdhc_mcf_pltfm_set_bus_width(struct sdhci_host
*host
, int width
)
283 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
284 struct pltfm_mcf_data
*mcf_data
= sdhci_pltfm_priv(pltfm_host
);
287 case MMC_BUS_WIDTH_4
:
288 mcf_data
->current_bus_width
= ESDHC_CTRL_4BITBUS
;
291 mcf_data
->current_bus_width
= 0;
295 esdhc_clrset_be(host
, ESDHC_CTRL_BUSWIDTH_MASK
,
296 mcf_data
->current_bus_width
, SDHCI_HOST_CONTROL
);
299 static void esdhc_mcf_request_done(struct sdhci_host
*host
,
300 struct mmc_request
*mrq
)
302 struct scatterlist
*sg
;
306 if (!mrq
->data
|| !mrq
->data
->bytes_xfered
)
309 if (mmc_get_dma_dir(mrq
->data
) != DMA_FROM_DEVICE
)
313 * On mcf5441x there is no hw sdma option/flag to select the dma
314 * transfer endiannes. A swap after the transfer is needed.
316 for_each_sg(mrq
->data
->sg
, sg
, mrq
->data
->sg_len
, i
) {
317 buffer
= (u32
*)sg_virt(sg
);
318 esdhc_mcf_buffer_swap32(buffer
, sg
->length
);
322 mmc_request_done(host
->mmc
, mrq
);
325 static void esdhc_mcf_copy_to_bounce_buffer(struct sdhci_host
*host
,
326 struct mmc_data
*data
,
329 sg_copy_to_buffer(data
->sg
, data
->sg_len
,
330 host
->bounce_buffer
, length
);
332 esdhc_mcf_buffer_swap32((u32
*)host
->bounce_buffer
,
333 data
->blksz
* data
->blocks
);
336 static struct sdhci_ops sdhci_esdhc_ops
= {
337 .reset
= esdhc_mcf_reset
,
338 .set_clock
= esdhc_mcf_pltfm_set_clock
,
339 .get_max_clock
= esdhc_mcf_pltfm_get_max_clock
,
340 .get_min_clock
= esdhc_mcf_pltfm_get_min_clock
,
341 .set_bus_width
= esdhc_mcf_pltfm_set_bus_width
,
342 .get_max_timeout_count
= esdhc_mcf_get_max_timeout_count
,
343 .set_timeout
= esdhc_mcf_set_timeout
,
344 .write_b
= esdhc_mcf_writeb_be
,
345 .write_w
= esdhc_mcf_writew_be
,
346 .write_l
= esdhc_mcf_writel_be
,
347 .read_b
= esdhc_mcf_readb_be
,
348 .read_w
= esdhc_mcf_readw_be
,
349 .read_l
= esdhc_mcf_readl_be
,
350 .copy_to_bounce_buffer
= esdhc_mcf_copy_to_bounce_buffer
,
351 .request_done
= esdhc_mcf_request_done
,
354 static const struct sdhci_pltfm_data sdhci_esdhc_mcf_pdata
= {
355 .ops
= &sdhci_esdhc_ops
,
356 .quirks
= ESDHC_DEFAULT_QUIRKS
| SDHCI_QUIRK_FORCE_DMA
,
359 * controller does not support cmd23,
360 * without, on > 8G cards cmd23 is used, and
363 SDHCI_QUIRK2_HOST_NO_CMD23
,
366 static int esdhc_mcf_plat_init(struct sdhci_host
*host
,
367 struct pltfm_mcf_data
*mcf_data
)
369 struct mcf_esdhc_platform_data
*plat_data
;
371 if (!host
->mmc
->parent
->platform_data
) {
372 dev_err(mmc_dev(host
->mmc
), "no platform data!\n");
376 plat_data
= (struct mcf_esdhc_platform_data
*)
377 host
->mmc
->parent
->platform_data
;
380 switch (plat_data
->cd_type
) {
382 case ESDHC_CD_CONTROLLER
:
383 /* We have a working card_detect back */
384 host
->quirks
&= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION
;
386 case ESDHC_CD_PERMANENT
:
387 host
->mmc
->caps
|= MMC_CAP_NONREMOVABLE
;
393 switch (plat_data
->max_bus_width
) {
395 host
->mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
399 host
->quirks
|= SDHCI_QUIRK_FORCE_1_BIT_DATA
;
406 static int sdhci_esdhc_mcf_probe(struct platform_device
*pdev
)
408 struct sdhci_host
*host
;
409 struct sdhci_pltfm_host
*pltfm_host
;
410 struct pltfm_mcf_data
*mcf_data
;
413 host
= sdhci_pltfm_init(pdev
, &sdhci_esdhc_mcf_pdata
,
417 return PTR_ERR(host
);
419 pltfm_host
= sdhci_priv(host
);
420 mcf_data
= sdhci_pltfm_priv(pltfm_host
);
422 host
->sdma_boundary
= 0;
424 host
->flags
|= SDHCI_AUTO_CMD12
;
426 mcf_data
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
427 if (IS_ERR(mcf_data
->clk_ipg
)) {
428 err
= PTR_ERR(mcf_data
->clk_ipg
);
432 mcf_data
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
433 if (IS_ERR(mcf_data
->clk_ahb
)) {
434 err
= PTR_ERR(mcf_data
->clk_ahb
);
438 mcf_data
->clk_per
= devm_clk_get(&pdev
->dev
, "per");
439 if (IS_ERR(mcf_data
->clk_per
)) {
440 err
= PTR_ERR(mcf_data
->clk_per
);
444 pltfm_host
->clk
= mcf_data
->clk_per
;
445 pltfm_host
->clock
= clk_get_rate(pltfm_host
->clk
);
446 err
= clk_prepare_enable(mcf_data
->clk_per
);
450 err
= clk_prepare_enable(mcf_data
->clk_ipg
);
454 err
= clk_prepare_enable(mcf_data
->clk_ahb
);
458 err
= esdhc_mcf_plat_init(host
, mcf_data
);
462 err
= sdhci_setup_host(host
);
466 if (!host
->bounce_buffer
) {
467 dev_err(&pdev
->dev
, "bounce buffer not allocated");
472 err
= __sdhci_add_host(host
);
479 sdhci_cleanup_host(host
);
481 clk_disable_unprepare(mcf_data
->clk_ahb
);
483 clk_disable_unprepare(mcf_data
->clk_ipg
);
485 clk_disable_unprepare(mcf_data
->clk_per
);
487 sdhci_pltfm_free(pdev
);
492 static int sdhci_esdhc_mcf_remove(struct platform_device
*pdev
)
494 struct sdhci_host
*host
= platform_get_drvdata(pdev
);
495 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
496 struct pltfm_mcf_data
*mcf_data
= sdhci_pltfm_priv(pltfm_host
);
498 sdhci_remove_host(host
, 0);
500 clk_disable_unprepare(mcf_data
->clk_ipg
);
501 clk_disable_unprepare(mcf_data
->clk_ahb
);
502 clk_disable_unprepare(mcf_data
->clk_per
);
504 sdhci_pltfm_free(pdev
);
509 static struct platform_driver sdhci_esdhc_mcf_driver
= {
511 .name
= "sdhci-esdhc-mcf",
512 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
514 .probe
= sdhci_esdhc_mcf_probe
,
515 .remove
= sdhci_esdhc_mcf_remove
,
518 module_platform_driver(sdhci_esdhc_mcf_driver
);
520 MODULE_DESCRIPTION("SDHCI driver for Freescale ColdFire eSDHC");
521 MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com>");
522 MODULE_LICENSE("GPL v2");