hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/mmc/host/bcm2835.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * bcm2835 sdhost driver.
34 *
....@@ -25,18 +26,6 @@
2526 * sdhci-bcm2708.c by Broadcom
2627 * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
2728 * sdhci.c and sdhci-pci.c by Pierre Ossman
28
- *
29
- * This program is free software; you can redistribute it and/or modify it
30
- * under the terms and conditions of the GNU General Public License,
31
- * version 2, as published by the Free Software Foundation.
32
- *
33
- * This program is distributed in the hope it will be useful, but WITHOUT
34
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36
- * more details.
37
- *
38
- * You should have received a copy of the GNU General Public License
39
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
4029 */
4130 #include <linux/clk.h>
4231 #include <linux/delay.h>
....@@ -159,7 +148,6 @@
159148 void __iomem *ioaddr;
160149 u32 phys_addr;
161150
162
- struct mmc_host *mmc;
163151 struct platform_device *pdev;
164152
165153 int clock; /* Current clock speed */
....@@ -464,7 +452,7 @@
464452 static
465453 void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
466454 {
467
- int len, dir_data, dir_slave;
455
+ int sg_len, dir_data, dir_slave;
468456 struct dma_async_tx_descriptor *desc = NULL;
469457 struct dma_chan *dma_chan;
470458
....@@ -510,23 +498,24 @@
510498 &host->dma_cfg_rx :
511499 &host->dma_cfg_tx);
512500
513
- len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
514
- dir_data);
501
+ sg_len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
502
+ dir_data);
503
+ if (!sg_len)
504
+ return;
515505
516
- if (len > 0) {
517
- desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
518
- len, dir_slave,
519
- DMA_PREP_INTERRUPT |
520
- DMA_CTRL_ACK);
506
+ desc = dmaengine_prep_slave_sg(dma_chan, data->sg, sg_len, dir_slave,
507
+ DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
508
+
509
+ if (!desc) {
510
+ dma_unmap_sg(dma_chan->device->dev, data->sg, sg_len, dir_data);
511
+ return;
521512 }
522513
523
- if (desc) {
524
- desc->callback = bcm2835_dma_complete;
525
- desc->callback_param = host;
526
- host->dma_desc = desc;
527
- host->dma_chan = dma_chan;
528
- host->dma_dir = dir_data;
529
- }
514
+ desc->callback = bcm2835_dma_complete;
515
+ desc->callback_param = host;
516
+ host->dma_desc = desc;
517
+ host->dma_chan = dma_chan;
518
+ host->dma_dir = dir_data;
530519 }
531520
532521 static void bcm2835_start_dma(struct bcm2835_host *host)
....@@ -628,7 +617,7 @@
628617 "failed to terminate DMA (%d)\n", err);
629618 }
630619
631
- mmc_request_done(host->mmc, mrq);
620
+ mmc_request_done(mmc_from_priv(host), mrq);
632621 }
633622
634623 static
....@@ -847,7 +836,7 @@
847836 dev_err(dev, "timeout waiting for hardware interrupt.\n");
848837 bcm2835_dumpregs(host);
849838
850
- bcm2835_reset(host->mmc);
839
+ bcm2835_reset(mmc_from_priv(host));
851840
852841 if (host->data) {
853842 host->data->error = -ETIMEDOUT;
....@@ -1064,9 +1053,11 @@
10641053 {
10651054 struct bcm2835_host *host =
10661055 container_of(work, struct bcm2835_host, dma_work);
1067
- struct mmc_data *data = host->data;
1056
+ struct mmc_data *data;
10681057
10691058 mutex_lock(&host->mutex);
1059
+
1060
+ data = host->data;
10701061
10711062 if (host->dma_chan) {
10721063 dma_unmap_sg(host->dma_chan->device->dev,
....@@ -1108,6 +1099,7 @@
11081099
11091100 static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
11101101 {
1102
+ struct mmc_host *mmc = mmc_from_priv(host);
11111103 int div;
11121104
11131105 /* The SDCDIV register has 11 bits, and holds (div - 2). But
....@@ -1151,18 +1143,18 @@
11511143 div = SDCDIV_MAX_CDIV;
11521144
11531145 clock = host->max_clk / (div + 2);
1154
- host->mmc->actual_clock = clock;
1146
+ mmc->actual_clock = clock;
11551147
11561148 /* Calibrate some delays */
11571149
11581150 host->ns_per_fifo_word = (1000000000 / clock) *
1159
- ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
1151
+ ((mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
11601152
11611153 host->cdiv = div;
11621154 writel(host->cdiv, host->ioaddr + SDCDIV);
11631155
11641156 /* Set the timeout to 500ms */
1165
- writel(host->mmc->actual_clock / 2, host->ioaddr + SDTOUT);
1157
+ writel(mmc->actual_clock / 2, host->ioaddr + SDTOUT);
11661158 }
11671159
11681160 static void bcm2835_request(struct mmc_host *mmc, struct mmc_request *mrq)
....@@ -1192,9 +1184,6 @@
11921184 return;
11931185 }
11941186
1195
- if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
1196
- bcm2835_prepare_dma(host, mrq->data);
1197
-
11981187 mutex_lock(&host->mutex);
11991188
12001189 WARN_ON(host->mrq);
....@@ -1217,6 +1206,9 @@
12171206 mutex_unlock(&host->mutex);
12181207 return;
12191208 }
1209
+
1210
+ if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
1211
+ bcm2835_prepare_dma(host, mrq->data);
12201212
12211213 host->use_sbc = !!mrq->sbc && host->mrq->data &&
12221214 (host->mrq->data->flags & MMC_DATA_READ);
....@@ -1272,7 +1264,7 @@
12721264
12731265 static int bcm2835_add_host(struct bcm2835_host *host)
12741266 {
1275
- struct mmc_host *mmc = host->mmc;
1267
+ struct mmc_host *mmc = mmc_from_priv(host);
12761268 struct device *dev = &host->pdev->dev;
12771269 char pio_limit_string[20];
12781270 int ret;
....@@ -1288,13 +1280,12 @@
12881280
12891281 /* host controller capabilities */
12901282 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
1291
- MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
1292
- MMC_CAP_CMD23;
1283
+ MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_CMD23;
12931284
12941285 spin_lock_init(&host->lock);
12951286 mutex_init(&host->mutex);
12961287
1297
- if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
1288
+ if (!host->dma_chan_rxtx) {
12981289 dev_warn(dev, "unable to initialise DMA channel. Falling back to PIO\n");
12991290 host->use_dma = false;
13001291 } else {
....@@ -1322,7 +1313,7 @@
13221313 }
13231314
13241315 mmc->max_segs = 128;
1325
- mmc->max_req_size = 524288;
1316
+ mmc->max_req_size = min_t(size_t, 524288, dma_max_mapping_size(dev));
13261317 mmc->max_seg_size = mmc->max_req_size;
13271318 mmc->max_blk_size = 1024;
13281319 mmc->max_blk_count = 65535;
....@@ -1365,7 +1356,6 @@
13651356 {
13661357 struct device *dev = &pdev->dev;
13671358 struct clk *clk;
1368
- struct resource *iomem;
13691359 struct bcm2835_host *host;
13701360 struct mmc_host *mmc;
13711361 const __be32 *regaddr_p;
....@@ -1378,12 +1368,10 @@
13781368
13791369 mmc->ops = &bcm2835_ops;
13801370 host = mmc_priv(mmc);
1381
- host->mmc = mmc;
13821371 host->pdev = pdev;
13831372 spin_lock_init(&host->lock);
13841373
1385
- iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1386
- host->ioaddr = devm_ioremap_resource(dev, iomem);
1374
+ host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
13871375 if (IS_ERR(host->ioaddr)) {
13881376 ret = PTR_ERR(host->ioaddr);
13891377 goto err;
....@@ -1404,13 +1392,21 @@
14041392 host->dma_chan = NULL;
14051393 host->dma_desc = NULL;
14061394
1407
- host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
1395
+ host->dma_chan_rxtx = dma_request_chan(dev, "rx-tx");
1396
+ if (IS_ERR(host->dma_chan_rxtx)) {
1397
+ ret = PTR_ERR(host->dma_chan_rxtx);
1398
+ host->dma_chan_rxtx = NULL;
1399
+
1400
+ if (ret == -EPROBE_DEFER)
1401
+ goto err;
1402
+
1403
+ /* Ignore errors to fall back to PIO mode */
1404
+ }
1405
+
14081406
14091407 clk = devm_clk_get(dev, NULL);
14101408 if (IS_ERR(clk)) {
1411
- ret = PTR_ERR(clk);
1412
- if (ret != -EPROBE_DEFER)
1413
- dev_err(dev, "could not get clk: %d\n", ret);
1409
+ ret = dev_err_probe(dev, PTR_ERR(clk), "could not get clk\n");
14141410 goto err;
14151411 }
14161412
....@@ -1418,7 +1414,6 @@
14181414
14191415 host->irq = platform_get_irq(pdev, 0);
14201416 if (host->irq <= 0) {
1421
- dev_err(dev, "get IRQ failed\n");
14221417 ret = -EINVAL;
14231418 goto err;
14241419 }
....@@ -1449,8 +1444,9 @@
14491444 static int bcm2835_remove(struct platform_device *pdev)
14501445 {
14511446 struct bcm2835_host *host = platform_get_drvdata(pdev);
1447
+ struct mmc_host *mmc = mmc_from_priv(host);
14521448
1453
- mmc_remove_host(host->mmc);
1449
+ mmc_remove_host(mmc);
14541450
14551451 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
14561452
....@@ -1459,8 +1455,10 @@
14591455 cancel_work_sync(&host->dma_work);
14601456 cancel_delayed_work_sync(&host->timeout_work);
14611457
1462
- mmc_free_host(host->mmc);
1463
- platform_set_drvdata(pdev, NULL);
1458
+ if (host->dma_chan_rxtx)
1459
+ dma_release_channel(host->dma_chan_rxtx);
1460
+
1461
+ mmc_free_host(mmc);
14641462
14651463 return 0;
14661464 }
....@@ -1476,6 +1474,7 @@
14761474 .remove = bcm2835_remove,
14771475 .driver = {
14781476 .name = "sdhost-bcm2835",
1477
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
14791478 .of_match_table = bcm2835_match,
14801479 },
14811480 };