hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/mmc/host/dw_mmc.c
....@@ -1,14 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Synopsys DesignWare Multimedia Card Interface driver
34 * (Based on NXP driver for lpc 31xx)
45 *
56 * Copyright (C) 2009 NXP Semiconductors
67 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
128 */
139
1410 #include <linux/blkdev.h>
....@@ -40,6 +36,7 @@
4036 #include <linux/of.h>
4137 #include <linux/of_gpio.h>
4238 #include <linux/mmc/slot-gpio.h>
39
+#include <linux/soc/rockchip/rk_sdmmc.h>
4340 #include <linux/soc/rockchip/rockchip_decompress.h>
4441
4542 #include "dw_mmc.h"
....@@ -109,6 +106,24 @@
109106 /* Each descriptor can transfer up to 4KB of data in chained mode */
110107 #define DW_MCI_DESC_DATA_LENGTH 0x1000
111108
109
+#if IS_ENABLED(CONFIG_CPU_RV1106)
110
+static spinlock_t *g_sdmmc_ispvicap_lock;
111
+
112
+void rv1106_sdmmc_get_lock(void)
113
+{
114
+ if (g_sdmmc_ispvicap_lock)
115
+ spin_lock(g_sdmmc_ispvicap_lock);
116
+}
117
+EXPORT_SYMBOL(rv1106_sdmmc_get_lock);
118
+
119
+void rv1106_sdmmc_put_lock(void)
120
+{
121
+ if (g_sdmmc_ispvicap_lock)
122
+ spin_unlock(g_sdmmc_ispvicap_lock);
123
+}
124
+EXPORT_SYMBOL(rv1106_sdmmc_put_lock);
125
+#endif
126
+
112127 #if defined(CONFIG_DEBUG_FS)
113128 static int dw_mci_req_show(struct seq_file *s, void *v)
114129 {
....@@ -175,40 +190,18 @@
175190 struct mmc_host *mmc = slot->mmc;
176191 struct dw_mci *host = slot->host;
177192 struct dentry *root;
178
- struct dentry *node;
179193
180194 root = mmc->debugfs_root;
181195 if (!root)
182196 return;
183197
184
- node = debugfs_create_file("regs", S_IRUSR, root, host,
185
- &dw_mci_regs_fops);
186
- if (!node)
187
- goto err;
188
-
189
- node = debugfs_create_file("req", S_IRUSR, root, slot,
190
- &dw_mci_req_fops);
191
- if (!node)
192
- goto err;
193
-
194
- node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
195
- if (!node)
196
- goto err;
197
-
198
- node = debugfs_create_x32("pending_events", S_IRUSR, root,
199
- (u32 *)&host->pending_events);
200
- if (!node)
201
- goto err;
202
-
203
- node = debugfs_create_x32("completed_events", S_IRUSR, root,
204
- (u32 *)&host->completed_events);
205
- if (!node)
206
- goto err;
207
-
208
- return;
209
-
210
-err:
211
- dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
198
+ debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops);
199
+ debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops);
200
+ debugfs_create_u32("state", S_IRUSR, root, &host->state);
201
+ debugfs_create_xul("pending_events", S_IRUSR, root,
202
+ &host->pending_events);
203
+ debugfs_create_xul("completed_events", S_IRUSR, root,
204
+ &host->completed_events);
212205 }
213206 #endif /* defined(CONFIG_DEBUG_FS) */
214207
....@@ -247,7 +240,8 @@
247240 * expected.
248241 */
249242 #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
250
- if (host->slot->mmc->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
243
+ if (host->slot->mmc->caps2 & MMC_CAP2_NO_SD &&
244
+ host->slot->mmc->caps2 & MMC_CAP2_NO_SDIO)
251245 delay = 0;
252246 #endif
253247 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
....@@ -488,14 +482,20 @@
488482
489483 /* Disable and reset the IDMAC interface */
490484 temp = mci_readl(host, CTRL);
491
- temp &= ~SDMMC_CTRL_USE_IDMAC;
485
+ if (!host->is_rv1106_sd)
486
+ temp &= ~SDMMC_CTRL_USE_IDMAC;
487
+
492488 temp |= SDMMC_CTRL_DMA_RESET;
493489 mci_writel(host, CTRL, temp);
494490
495491 /* Stop the IDMAC running */
496492 temp = mci_readl(host, BMOD);
497
- temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
498
- temp |= SDMMC_IDMAC_SWRESET;
493
+ if (host->is_rv1106_sd) {
494
+ temp |= SDMMC_IDMAC_SWRESET;
495
+ } else {
496
+ temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
497
+ temp |= SDMMC_IDMAC_SWRESET;
498
+ }
499499 mci_writel(host, BMOD, temp);
500500 }
501501
....@@ -736,6 +736,13 @@
736736 }
737737 }
738738
739
+ if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE)) {
740
+ desc->des0 = desc_last->des0;
741
+ desc->des2 = desc_last->des2;
742
+ desc->des1 = 0x8; /* Random dirty data for last one desc */
743
+ desc_last = desc;
744
+ }
745
+
739746 /* Set first descriptor */
740747 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
741748
....@@ -871,12 +878,14 @@
871878 if (!host->dms)
872879 return -ENOMEM;
873880
874
- host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
875
- if (!host->dms->ch) {
881
+ host->dms->ch = dma_request_chan(host->dev, "rx-tx");
882
+ if (IS_ERR(host->dms->ch)) {
883
+ int ret = PTR_ERR(host->dms->ch);
884
+
876885 dev_err(host->dev, "Failed to get external DMA channel.\n");
877886 kfree(host->dms);
878887 host->dms = NULL;
879
- return -ENXIO;
888
+ return ret;
880889 }
881890
882891 return 0;
....@@ -918,7 +927,7 @@
918927 * non-word-aligned buffers or lengths. Also, we don't bother
919928 * with all the DMA setup overhead for short transfers.
920929 */
921
- if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
930
+ if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD && !host->is_rv1106_sd)
922931 return -EINVAL;
923932
924933 if (data->blksz & 3)
....@@ -976,39 +985,12 @@
976985 data->host_cookie = COOKIE_UNMAPPED;
977986 }
978987
979
-static int dw_mci_set_sdio_status(struct mmc_host *mmc, int val)
980
-{
981
- struct dw_mci_slot *slot = mmc_priv(mmc);
982
- struct dw_mci *host = slot->host;
983
-
984
- if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SDIO))
985
- return 0;
986
-
987
- spin_lock_bh(&host->lock);
988
-
989
- if (val)
990
- set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
991
- else
992
- clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
993
-
994
- spin_unlock_bh(&host->lock);
995
-
996
- mmc_detect_change(slot->mmc, 20);
997
-
998
- return 0;
999
-}
1000
-
1001988 static int dw_mci_get_cd(struct mmc_host *mmc)
1002989 {
1003990 int present;
1004991 struct dw_mci_slot *slot = mmc_priv(mmc);
1005992 struct dw_mci *host = slot->host;
1006993 int gpio_cd = mmc_gpio_get_cd(mmc);
1007
-
1008
-#ifdef CONFIG_SDIO_KEEPALIVE
1009
- if (mmc->logic_remove_card)
1010
- return test_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1011
-#endif
1012994
1013995 /* Use platform get_cd function, else try onboard card detect */
1014996 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
....@@ -1358,10 +1340,16 @@
13581340 host->data_status = 0;
13591341 host->dir_status = 0;
13601342
1343
+ if (host->is_rv1106_sd)
1344
+ mci_writel(host, CTYPE, (slot->ctype << slot->id));
1345
+
13611346 data = cmd->data;
13621347 if (data) {
13631348 mci_writel(host, TMOUT, 0xFFFFFFFF);
1364
- mci_writel(host, BYTCNT, data->blksz*data->blocks);
1349
+ if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE))
1350
+ mci_writel(host, BYTCNT, 0);
1351
+ else
1352
+ mci_writel(host, BYTCNT, data->blksz*data->blocks);
13651353 mci_writel(host, BLKSIZ, data->blksz);
13661354 }
13671355
....@@ -1439,6 +1427,7 @@
14391427 }
14401428 }
14411429
1430
+static bool dw_mci_reset(struct dw_mci *host);
14421431 static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
14431432 {
14441433 struct dw_mci_slot *slot = mmc_priv(mmc);
....@@ -1458,7 +1447,17 @@
14581447 return;
14591448 }
14601449
1450
+ if (host->is_rv1106_sd) {
1451
+ u32 reg;
1452
+
1453
+ readl_poll_timeout(host->regs + SDMMC_STATUS, reg,
1454
+ reg & BIT(2), USEC_PER_MSEC, 500 * USEC_PER_MSEC);
1455
+ }
1456
+
14611457 spin_lock_bh(&host->lock);
1458
+
1459
+ if (host->is_rv1106_sd)
1460
+ dw_mci_reset(host);
14621461
14631462 dw_mci_queue_request(host, slot, mrq);
14641463
....@@ -1563,11 +1562,8 @@
15631562 if (!IS_ERR(mmc->supply.vmmc))
15641563 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
15651564
1566
- if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) {
1567
- ios->signal_voltage = MMC_SIGNAL_VOLTAGE_330;
1568
- mmc_regulator_set_vqmmc(mmc, ios);
1565
+ if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
15691566 regulator_disable(mmc->supply.vqmmc);
1570
- }
15711567 slot->host->vqmmc_enabled = false;
15721568
15731569 regs = mci_readl(slot->host, PWREN);
....@@ -1621,8 +1617,7 @@
16211617
16221618 if (!IS_ERR(mmc->supply.vqmmc)) {
16231619 ret = mmc_regulator_set_vqmmc(mmc, ios);
1624
-
1625
- if (ret) {
1620
+ if (ret < 0) {
16261621 dev_dbg(&mmc->class_dev,
16271622 "Regulator set error %d - %s V\n",
16281623 ret, uhs & v18 ? "1.8" : "3.3");
....@@ -1852,7 +1847,6 @@
18521847 .pre_req = dw_mci_pre_req,
18531848 .post_req = dw_mci_post_req,
18541849 .set_ios = dw_mci_set_ios,
1855
- .set_sdio_status = dw_mci_set_sdio_status,
18561850 .get_ro = dw_mci_get_ro,
18571851 .get_cd = dw_mci_get_cd,
18581852 .hw_reset = dw_mci_hw_reset,
....@@ -1894,6 +1888,7 @@
18941888 }
18951889
18961890 spin_unlock(&host->lock);
1891
+
18971892 mmc_request_done(prev_mmc, mrq);
18981893 spin_lock(&host->lock);
18991894 }
....@@ -1935,6 +1930,9 @@
19351930 {
19361931 u32 status = host->data_status;
19371932
1933
+ if (host->is_rv1106_sd && (data->flags & MMC_DATA_WRITE) && (status & SDMMC_INT_DATA_OVER))
1934
+ goto finish;
1935
+
19381936 if (status & DW_MCI_DATA_ERROR_FLAGS) {
19391937 if (status & SDMMC_INT_DRTO) {
19401938 data->error = -ETIMEDOUT;
....@@ -1967,6 +1965,7 @@
19671965 */
19681966 dw_mci_reset(host);
19691967 } else {
1968
+finish:
19701969 data->bytes_xfered = data->blocks * data->blksz;
19711970 data->error = 0;
19721971 }
....@@ -2132,7 +2131,7 @@
21322131 }
21332132
21342133 prev_state = state = STATE_SENDING_DATA;
2135
- /* fall through */
2134
+ fallthrough;
21362135
21372136 case STATE_SENDING_DATA:
21382137 /*
....@@ -2193,7 +2192,7 @@
21932192 }
21942193 prev_state = state = STATE_DATA_BUSY;
21952194
2196
- /* fall through */
2195
+ fallthrough;
21972196
21982197 case STATE_DATA_BUSY:
21992198 if (!dw_mci_clear_pending_data_complete(host)) {
....@@ -2246,7 +2245,7 @@
22462245 */
22472246 prev_state = state = STATE_SENDING_STOP;
22482247
2249
- /* fall through */
2248
+ fallthrough;
22502249
22512250 case STATE_SENDING_STOP:
22522251 if (!dw_mci_clear_pending_cmd_complete(host))
....@@ -2771,11 +2770,14 @@
27712770 }
27722771
27732772 if (pending & SDMMC_INT_DATA_OVER) {
2773
+rv1106_sd:
27742774 spin_lock_irqsave(&host->irq_lock, irqflags);
27752775
27762776 del_timer(&host->dto_timer);
27772777
27782778 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2779
+ if (host->is_rv1106_sd)
2780
+ pending |= SDMMC_INT_DATA_OVER;
27792781 if (!host->data_status)
27802782 host->data_status = pending;
27812783 smp_wmb(); /* drain writebuffer */
....@@ -2845,6 +2847,9 @@
28452847 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
28462848 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
28472849 host->dma_ops->complete((void *)host);
2850
+
2851
+ if (host->is_rv1106_sd && (pending & SDMMC_IDMAC_INT_TI))
2852
+ goto rv1106_sd;
28482853 }
28492854 }
28502855
....@@ -2860,12 +2865,6 @@
28602865
28612866 if (host->pdata->caps)
28622867 mmc->caps = host->pdata->caps;
2863
-
2864
- /*
2865
- * Support MMC_CAP_ERASE by default.
2866
- * It needs to use trim/discard/erase commands.
2867
- */
2868
- mmc->caps |= MMC_CAP_ERASE;
28692868
28702869 if (host->pdata->pm_caps)
28712870 mmc->pm_caps = host->pdata->pm_caps;
....@@ -2884,15 +2883,7 @@
28842883 ctrl_id);
28852884 return -EINVAL;
28862885 }
2887
- /*
2888
- * Some sd cards violate the spec. They claim to support
2889
- * CMD23 but actually not. We don't have a good method to
2890
- * work around them except for adding MMC_QUIRK_BLK_NO_CMD23
2891
- * one by one. But it's not a acceptable way for our custmors.
2892
- * So removing CMD23 support for all sd cards to solve it.
2893
- */
2894
- if (!(mmc->restrict_caps & RESTRICT_CARD_TYPE_SD))
2895
- mmc->caps |= drv_data->caps[ctrl_id];
2886
+ mmc->caps |= drv_data->caps[ctrl_id];
28962887 }
28972888
28982889 if (host->pdata->caps2)
....@@ -2946,6 +2937,10 @@
29462937
29472938 /* Useful defaults if platform data is unset. */
29482939 if (host->use_dma == TRANS_MODE_IDMAC) {
2940
+ /* Reserve last desc for dirty data */
2941
+ if (host->is_rv1106_sd)
2942
+ host->ring_size--;
2943
+
29492944 mmc->max_segs = host->ring_size;
29502945 mmc->max_blk_size = 65535;
29512946 mmc->max_seg_size = 0x1000;
....@@ -2969,11 +2964,6 @@
29692964 }
29702965
29712966 dw_mci_get_cd(mmc);
2972
-
2973
-#ifdef CONFIG_SDIO_KEEPALIVE
2974
- if (mmc->logic_remove_card)
2975
- clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2976
-#endif
29772967
29782968 ret = mmc_add_host(mmc);
29792969 if (ret)
....@@ -3336,12 +3326,9 @@
33363326
33373327 if (!host->pdata) {
33383328 host->pdata = dw_mci_parse_dt(host);
3339
- if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3340
- return -EPROBE_DEFER;
3341
- } else if (IS_ERR(host->pdata)) {
3342
- dev_err(host->dev, "platform data not available\n");
3343
- return -EINVAL;
3344
- }
3329
+ if (IS_ERR(host->pdata))
3330
+ return dev_err_probe(host->dev, PTR_ERR(host->pdata),
3331
+ "platform data not available\n");
33453332 }
33463333
33473334 host->biu_clk = devm_clk_get(host->dev, "biu");
....@@ -3354,8 +3341,10 @@
33543341 return ret;
33553342 }
33563343 }
3344
+
33573345 #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT_MMC
3358
- if (device_property_read_bool(host->dev, "supports-emmc")) {
3346
+ if (device_property_read_bool(host->dev, "no-sd") &&
3347
+ device_property_read_bool(host->dev, "no-sdio")) {
33593348 if (readl_poll_timeout(host->regs + SDMMC_STATUS,
33603349 fifo_size,
33613350 !(fifo_size & (BIT(10) | GENMASK(7, 4))),
....@@ -3532,6 +3521,24 @@
35323521 goto err_dmaunmap;
35333522 }
35343523
3524
+ if (host->is_rv1106_sd) {
3525
+#if IS_ENABLED(CONFIG_CPU_RV1106)
3526
+ g_sdmmc_ispvicap_lock = &host->lock;
3527
+#endif
3528
+ /* Select IDMAC interface */
3529
+ fifo_size = mci_readl(host, CTRL);
3530
+ fifo_size |= SDMMC_CTRL_USE_IDMAC;
3531
+ mci_writel(host, CTRL, fifo_size);
3532
+
3533
+ fifo_size = mci_readl(host, INTMASK);
3534
+ fifo_size &= ~SDMMC_INT_HTO;
3535
+ mci_writel(host, INTMASK, fifo_size);
3536
+
3537
+ host->slot->mmc->caps &= ~(MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
3538
+ MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
3539
+ MMC_CAP_UHS_SDR12);
3540
+ }
3541
+
35353542 /* Now that slots are all setup, we can enable card detect */
35363543 dw_mci_enable_cd(host);
35373544
....@@ -3629,18 +3636,26 @@
36293636 * Restore the initial value at FIFOTH register
36303637 * And Invalidate the prev_blksz with zero
36313638 */
3632
- mci_writel(host, FIFOTH, host->fifoth_val);
3633
- host->prev_blksz = 0;
3639
+ mci_writel(host, FIFOTH, host->fifoth_val);
3640
+ host->prev_blksz = 0;
36343641
36353642 /* Put in max timeout */
36363643 mci_writel(host, TMOUT, 0xFFFFFFFF);
36373644
36383645 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3639
- mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3640
- SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3641
- DW_MCI_ERROR_FLAGS);
3646
+ mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | DW_MCI_ERROR_FLAGS);
36423647 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
36433648
3649
+ if (host->is_rv1106_sd) {
3650
+ /* Select IDMAC interface */
3651
+ ret = mci_readl(host, CTRL);
3652
+ ret |= SDMMC_CTRL_USE_IDMAC;
3653
+ mci_writel(host, CTRL, ret);
3654
+
3655
+ ret = mci_readl(host, INTMASK);
3656
+ ret &= ~SDMMC_INT_HTO;
3657
+ mci_writel(host, INTMASK, ret);
3658
+ }
36443659
36453660 if (host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
36463661 dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);