hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/mmc/host/mmc_spi.c
....@@ -1,5 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
2
- * mmc_spi.c - Access SD/MMC cards through SPI master controllers
3
+ * Access SD/MMC cards through SPI master controllers
34 *
45 * (C) Copyright 2005, Intec Automation,
56 * Mike Lavender (mike@steroidmicros)
....@@ -8,21 +9,6 @@
89 * Hans-Peter Nilsson (hp@axis.com)
910 * (C) Copyright 2007, ATRON electronic GmbH,
1011 * Jan Nikitenko <jan.nikitenko@gmail.com>
11
- *
12
- *
13
- * This program is free software; you can redistribute it and/or modify
14
- * it under the terms of the GNU General Public License as published by
15
- * the Free Software Foundation; either version 2 of the License, or
16
- * (at your option) any later version.
17
- *
18
- * This program is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License
24
- * along with this program; if not, write to the Free Software
25
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2612 */
2713 #include <linux/sched.h>
2814 #include <linux/delay.h>
....@@ -91,14 +77,8 @@
9177
9278 #define MMC_SPI_BLOCKSIZE 512
9379
94
-
95
-/* These fixed timeouts come from the latest SD specs, which say to ignore
96
- * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
97
- * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
98
- * reads which takes nowhere near that long. Older cards may be able to use
99
- * shorter timeouts ... but why bother?
100
- */
101
-#define r1b_timeout (HZ * 3)
80
+#define MMC_SPI_R1B_TIMEOUT_MS 3000
81
+#define MMC_SPI_INIT_TIMEOUT_MS 3000
10282
10383 /* One of the critical speed parameters is the amount of data which may
10484 * be transferred in one command. If this value is too low, the SD card
....@@ -197,7 +177,7 @@
197177 static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
198178 unsigned n, u8 byte)
199179 {
200
- u8 *cp = host->data->status;
180
+ u8 *cp = host->data->status;
201181 unsigned long start = jiffies;
202182
203183 while (1) {
....@@ -220,7 +200,7 @@
220200 * We use jiffies here because we want to have a relation
221201 * between elapsed time and the blocking of the scheduler.
222202 */
223
- if (time_is_before_jiffies(start+1))
203
+ if (time_is_before_jiffies(start + 1))
224204 schedule();
225205 }
226206 return -ETIMEDOUT;
....@@ -262,6 +242,7 @@
262242 static int mmc_spi_response_get(struct mmc_spi_host *host,
263243 struct mmc_command *cmd, int cs_on)
264244 {
245
+ unsigned long timeout_ms;
265246 u8 *cp = host->data->status;
266247 u8 *end = cp + host->t.len;
267248 int value = 0;
....@@ -360,8 +341,11 @@
360341 /* maybe we read all the busy tokens already */
361342 while (cp < end && *cp == 0)
362343 cp++;
363
- if (cp == end)
364
- mmc_spi_wait_unbusy(host, r1b_timeout);
344
+ if (cp == end) {
345
+ timeout_ms = cmd->busy_timeout ? cmd->busy_timeout :
346
+ MMC_SPI_R1B_TIMEOUT_MS;
347
+ mmc_spi_wait_unbusy(host, msecs_to_jiffies(timeout_ms));
348
+ }
365349 break;
366350
367351 /* SPI R2 == R1 + second status byte; SEND_STATUS
....@@ -415,7 +399,7 @@
415399
416400 default:
417401 dev_dbg(&host->spi->dev, "bad response type %04x\n",
418
- mmc_spi_resp_type(cmd));
402
+ mmc_spi_resp_type(cmd));
419403 if (value >= 0)
420404 value = -EINVAL;
421405 goto done;
....@@ -467,8 +451,8 @@
467451 memset(cp, 0xff, sizeof(data->status));
468452
469453 cp[1] = 0x40 | cmd->opcode;
470
- put_unaligned_be32(cmd->arg, cp+2);
471
- cp[6] = crc7_be(0, cp+1, 5) | 0x01;
454
+ put_unaligned_be32(cmd->arg, cp + 2);
455
+ cp[6] = crc7_be(0, cp + 1, 5) | 0x01;
472456 cp += 7;
473457
474458 /* Then, read up to 13 bytes (while writing all-ones):
....@@ -642,9 +626,7 @@
642626 if (multiple || direction == DMA_TO_DEVICE) {
643627 t = &host->early_status;
644628 memset(t, 0, sizeof(*t));
645
- t->len = (direction == DMA_TO_DEVICE)
646
- ? sizeof(scratch->status)
647
- : 1;
629
+ t->len = (direction == DMA_TO_DEVICE) ? sizeof(scratch->status) : 1;
648630 t->tx_buf = host->ones;
649631 t->tx_dma = host->ones_dma;
650632 t->rx_buf = scratch->status;
....@@ -677,8 +659,7 @@
677659 u32 pattern;
678660
679661 if (host->mmc->use_spi_crc)
680
- scratch->crc_val = cpu_to_be16(
681
- crc_itu_t(0, t->tx_buf, t->len));
662
+ scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len));
682663 if (host->dma_dev)
683664 dma_sync_single_for_device(host->dma_dev,
684665 host->data_dma, sizeof(*scratch),
....@@ -859,9 +840,9 @@
859840
860841 be16_to_cpus(&scratch->crc_val);
861842 if (scratch->crc_val != crc) {
862
- dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
863
- "computed=0x%04x len=%d\n",
864
- scratch->crc_val, crc, t->len);
843
+ dev_dbg(&spi->dev,
844
+ "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n",
845
+ scratch->crc_val, crc, t->len);
865846 return -EILSEQ;
866847 }
867848 }
....@@ -901,14 +882,14 @@
901882 else
902883 clock_rate = spi->max_speed_hz;
903884
904
- timeout = data->timeout_ns +
885
+ timeout = data->timeout_ns / 1000 +
905886 data->timeout_clks * 1000000 / clock_rate;
906
- timeout = usecs_to_jiffies((unsigned int)(timeout / 1000)) + 1;
887
+ timeout = usecs_to_jiffies((unsigned int)timeout) + 1;
907888
908889 /* Handle scatterlist segments one at a time, with synch for
909890 * each 512-byte block
910891 */
911
- for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
892
+ for_each_sg(data->sg, sg, data->sg_len, n_sg) {
912893 int status = 0;
913894 dma_addr_t dma_addr = 0;
914895 void *kmap_addr;
....@@ -949,9 +930,7 @@
949930
950931 dev_dbg(&host->spi->dev,
951932 " mmc_spi: %s block, %d bytes\n",
952
- (direction == DMA_TO_DEVICE)
953
- ? "write"
954
- : "read",
933
+ (direction == DMA_TO_DEVICE) ? "write" : "read",
955934 t->len);
956935
957936 if (direction == DMA_TO_DEVICE)
....@@ -978,8 +957,7 @@
978957 if (status < 0) {
979958 data->error = status;
980959 dev_dbg(&spi->dev, "%s status %d\n",
981
- (direction == DMA_TO_DEVICE)
982
- ? "write" : "read",
960
+ (direction == DMA_TO_DEVICE) ? "write" : "read",
983961 status);
984962 break;
985963 }
....@@ -1138,7 +1116,7 @@
11381116 /* Try to be very sure any previous command has completed;
11391117 * wait till not-busy, skip debris from any old commands.
11401118 */
1141
- mmc_spi_wait_unbusy(host, r1b_timeout);
1119
+ mmc_spi_wait_unbusy(host, msecs_to_jiffies(MMC_SPI_INIT_TIMEOUT_MS));
11421120 mmc_spi_readbytes(host, 10);
11431121
11441122 /*
....@@ -1258,8 +1236,7 @@
12581236 mres = spi_setup(host->spi);
12591237 if (mres < 0)
12601238 dev_dbg(&host->spi->dev,
1261
- "switch back to SPI mode 3"
1262
- " failed\n");
1239
+ "switch back to SPI mode 3 failed\n");
12631240 }
12641241 }
12651242
....@@ -1300,6 +1277,52 @@
13001277 mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
13011278 return IRQ_HANDLED;
13021279 }
1280
+
1281
+#ifdef CONFIG_HAS_DMA
1282
+static int mmc_spi_dma_alloc(struct mmc_spi_host *host)
1283
+{
1284
+ struct spi_device *spi = host->spi;
1285
+ struct device *dev;
1286
+
1287
+ if (!spi->master->dev.parent->dma_mask)
1288
+ return 0;
1289
+
1290
+ dev = spi->master->dev.parent;
1291
+
1292
+ host->ones_dma = dma_map_single(dev, host->ones, MMC_SPI_BLOCKSIZE,
1293
+ DMA_TO_DEVICE);
1294
+ if (dma_mapping_error(dev, host->ones_dma))
1295
+ return -ENOMEM;
1296
+
1297
+ host->data_dma = dma_map_single(dev, host->data, sizeof(*host->data),
1298
+ DMA_BIDIRECTIONAL);
1299
+ if (dma_mapping_error(dev, host->data_dma)) {
1300
+ dma_unmap_single(dev, host->ones_dma, MMC_SPI_BLOCKSIZE,
1301
+ DMA_TO_DEVICE);
1302
+ return -ENOMEM;
1303
+ }
1304
+
1305
+ dma_sync_single_for_cpu(dev, host->data_dma, sizeof(*host->data),
1306
+ DMA_BIDIRECTIONAL);
1307
+
1308
+ host->dma_dev = dev;
1309
+ return 0;
1310
+}
1311
+
1312
+static void mmc_spi_dma_free(struct mmc_spi_host *host)
1313
+{
1314
+ if (!host->dma_dev)
1315
+ return;
1316
+
1317
+ dma_unmap_single(host->dma_dev, host->ones_dma, MMC_SPI_BLOCKSIZE,
1318
+ DMA_TO_DEVICE);
1319
+ dma_unmap_single(host->dma_dev, host->data_dma, sizeof(*host->data),
1320
+ DMA_BIDIRECTIONAL);
1321
+}
1322
+#else
1323
+static inline int mmc_spi_dma_alloc(struct mmc_spi_host *host) { return 0; }
1324
+static inline void mmc_spi_dma_free(struct mmc_spi_host *host) {}
1325
+#endif
13031326
13041327 static int mmc_spi_probe(struct spi_device *spi)
13051328 {
....@@ -1397,23 +1420,9 @@
13971420 if (!host->data)
13981421 goto fail_nobuf1;
13991422
1400
- if (spi->master->dev.parent->dma_mask) {
1401
- struct device *dev = spi->master->dev.parent;
1402
-
1403
- host->dma_dev = dev;
1404
- host->ones_dma = dma_map_single(dev, ones,
1405
- MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1406
- if (dma_mapping_error(dev, host->ones_dma))
1407
- goto fail_ones_dma;
1408
- host->data_dma = dma_map_single(dev, host->data,
1409
- sizeof(*host->data), DMA_BIDIRECTIONAL);
1410
- if (dma_mapping_error(dev, host->data_dma))
1411
- goto fail_data_dma;
1412
-
1413
- dma_sync_single_for_cpu(host->dma_dev,
1414
- host->data_dma, sizeof(*host->data),
1415
- DMA_BIDIRECTIONAL);
1416
- }
1423
+ status = mmc_spi_dma_alloc(host);
1424
+ if (status)
1425
+ goto fail_dma;
14171426
14181427 /* setup message for status/busy readback */
14191428 spi_message_init(&host->readback);
....@@ -1441,15 +1450,18 @@
14411450
14421451 status = mmc_add_host(mmc);
14431452 if (status != 0)
1444
- goto fail_add_host;
1453
+ goto fail_glue_init;
14451454
1446
- if (host->pdata && host->pdata->flags & MMC_SPI_USE_CD_GPIO) {
1447
- status = mmc_gpio_request_cd(mmc, host->pdata->cd_gpio,
1448
- host->pdata->cd_debounce);
1449
- if (status != 0)
1450
- goto fail_add_host;
1451
-
1452
- /* The platform has a CD GPIO signal that may support
1455
+ /*
1456
+ * Index 0 is card detect
1457
+ * Old boardfiles were specifying 1 ms as debounce
1458
+ */
1459
+ status = mmc_gpiod_request_cd(mmc, NULL, 0, false, 1000);
1460
+ if (status == -EPROBE_DEFER)
1461
+ goto fail_gpiod_request;
1462
+ if (!status) {
1463
+ /*
1464
+ * The platform has a CD GPIO signal that may support
14531465 * interrupts, so let mmc_gpiod_request_cd_irq() decide
14541466 * if polling is needed or not.
14551467 */
....@@ -1458,12 +1470,12 @@
14581470 }
14591471 mmc_detect_change(mmc, 0);
14601472
1461
- if (host->pdata && host->pdata->flags & MMC_SPI_USE_RO_GPIO) {
1473
+ /* Index 1 is write protect/read only */
1474
+ status = mmc_gpiod_request_ro(mmc, NULL, 1, 0);
1475
+ if (status == -EPROBE_DEFER)
1476
+ goto fail_gpiod_request;
1477
+ if (!status)
14621478 has_ro = true;
1463
- status = mmc_gpio_request_ro(mmc, host->pdata->ro_gpio);
1464
- if (status != 0)
1465
- goto fail_add_host;
1466
- }
14671479
14681480 dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
14691481 dev_name(&mmc->class_dev),
....@@ -1475,24 +1487,15 @@
14751487 ? ", cd polling" : "");
14761488 return 0;
14771489
1478
-fail_add_host:
1479
- mmc_remove_host (mmc);
1490
+fail_gpiod_request:
1491
+ mmc_remove_host(mmc);
14801492 fail_glue_init:
1481
- if (host->dma_dev)
1482
- dma_unmap_single(host->dma_dev, host->data_dma,
1483
- sizeof(*host->data), DMA_BIDIRECTIONAL);
1484
-fail_data_dma:
1485
- if (host->dma_dev)
1486
- dma_unmap_single(host->dma_dev, host->ones_dma,
1487
- MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1488
-fail_ones_dma:
1493
+ mmc_spi_dma_free(host);
1494
+fail_dma:
14891495 kfree(host->data);
1490
-
14911496 fail_nobuf1:
14921497 mmc_free_host(mmc);
14931498 mmc_spi_put_pdata(spi);
1494
- dev_set_drvdata(&spi->dev, NULL);
1495
-
14961499 nomem:
14971500 kfree(ones);
14981501 return status;
....@@ -1502,32 +1505,21 @@
15021505 static int mmc_spi_remove(struct spi_device *spi)
15031506 {
15041507 struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1505
- struct mmc_spi_host *host;
1508
+ struct mmc_spi_host *host = mmc_priv(mmc);
15061509
1507
- if (mmc) {
1508
- host = mmc_priv(mmc);
1510
+ /* prevent new mmc_detect_change() calls */
1511
+ if (host->pdata && host->pdata->exit)
1512
+ host->pdata->exit(&spi->dev, mmc);
15091513
1510
- /* prevent new mmc_detect_change() calls */
1511
- if (host->pdata && host->pdata->exit)
1512
- host->pdata->exit(&spi->dev, mmc);
1514
+ mmc_remove_host(mmc);
15131515
1514
- mmc_remove_host(mmc);
1516
+ mmc_spi_dma_free(host);
1517
+ kfree(host->data);
1518
+ kfree(host->ones);
15151519
1516
- if (host->dma_dev) {
1517
- dma_unmap_single(host->dma_dev, host->ones_dma,
1518
- MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1519
- dma_unmap_single(host->dma_dev, host->data_dma,
1520
- sizeof(*host->data), DMA_BIDIRECTIONAL);
1521
- }
1522
-
1523
- kfree(host->data);
1524
- kfree(host->ones);
1525
-
1526
- spi->max_speed_hz = mmc->f_max;
1527
- mmc_free_host(mmc);
1528
- mmc_spi_put_pdata(spi);
1529
- dev_set_drvdata(&spi->dev, NULL);
1530
- }
1520
+ spi->max_speed_hz = mmc->f_max;
1521
+ mmc_free_host(mmc);
1522
+ mmc_spi_put_pdata(spi);
15311523 return 0;
15321524 }
15331525
....@@ -1548,8 +1540,7 @@
15481540
15491541 module_spi_driver(mmc_spi_driver);
15501542
1551
-MODULE_AUTHOR("Mike Lavender, David Brownell, "
1552
- "Hans-Peter Nilsson, Jan Nikitenko");
1543
+MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
15531544 MODULE_DESCRIPTION("SPI SD/MMC host driver");
15541545 MODULE_LICENSE("GPL");
15551546 MODULE_ALIAS("spi:mmc_spi");