.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | | - * mmc_spi.c - Access SD/MMC cards through SPI master controllers |
---|
| 3 | + * Access SD/MMC cards through SPI master controllers |
---|
3 | 4 | * |
---|
4 | 5 | * (C) Copyright 2005, Intec Automation, |
---|
5 | 6 | * Mike Lavender (mike@steroidmicros) |
---|
.. | .. |
---|
8 | 9 | * Hans-Peter Nilsson (hp@axis.com) |
---|
9 | 10 | * (C) Copyright 2007, ATRON electronic GmbH, |
---|
10 | 11 | * 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. |
---|
26 | 12 | */ |
---|
27 | 13 | #include <linux/sched.h> |
---|
28 | 14 | #include <linux/delay.h> |
---|
.. | .. |
---|
91 | 77 | |
---|
92 | 78 | #define MMC_SPI_BLOCKSIZE 512 |
---|
93 | 79 | |
---|
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 |
---|
102 | 82 | |
---|
103 | 83 | /* One of the critical speed parameters is the amount of data which may |
---|
104 | 84 | * be transferred in one command. If this value is too low, the SD card |
---|
.. | .. |
---|
197 | 177 | static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout, |
---|
198 | 178 | unsigned n, u8 byte) |
---|
199 | 179 | { |
---|
200 | | - u8 *cp = host->data->status; |
---|
| 180 | + u8 *cp = host->data->status; |
---|
201 | 181 | unsigned long start = jiffies; |
---|
202 | 182 | |
---|
203 | 183 | while (1) { |
---|
.. | .. |
---|
220 | 200 | * We use jiffies here because we want to have a relation |
---|
221 | 201 | * between elapsed time and the blocking of the scheduler. |
---|
222 | 202 | */ |
---|
223 | | - if (time_is_before_jiffies(start+1)) |
---|
| 203 | + if (time_is_before_jiffies(start + 1)) |
---|
224 | 204 | schedule(); |
---|
225 | 205 | } |
---|
226 | 206 | return -ETIMEDOUT; |
---|
.. | .. |
---|
262 | 242 | static int mmc_spi_response_get(struct mmc_spi_host *host, |
---|
263 | 243 | struct mmc_command *cmd, int cs_on) |
---|
264 | 244 | { |
---|
| 245 | + unsigned long timeout_ms; |
---|
265 | 246 | u8 *cp = host->data->status; |
---|
266 | 247 | u8 *end = cp + host->t.len; |
---|
267 | 248 | int value = 0; |
---|
.. | .. |
---|
360 | 341 | /* maybe we read all the busy tokens already */ |
---|
361 | 342 | while (cp < end && *cp == 0) |
---|
362 | 343 | 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 | + } |
---|
365 | 349 | break; |
---|
366 | 350 | |
---|
367 | 351 | /* SPI R2 == R1 + second status byte; SEND_STATUS |
---|
.. | .. |
---|
415 | 399 | |
---|
416 | 400 | default: |
---|
417 | 401 | dev_dbg(&host->spi->dev, "bad response type %04x\n", |
---|
418 | | - mmc_spi_resp_type(cmd)); |
---|
| 402 | + mmc_spi_resp_type(cmd)); |
---|
419 | 403 | if (value >= 0) |
---|
420 | 404 | value = -EINVAL; |
---|
421 | 405 | goto done; |
---|
.. | .. |
---|
467 | 451 | memset(cp, 0xff, sizeof(data->status)); |
---|
468 | 452 | |
---|
469 | 453 | 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; |
---|
472 | 456 | cp += 7; |
---|
473 | 457 | |
---|
474 | 458 | /* Then, read up to 13 bytes (while writing all-ones): |
---|
.. | .. |
---|
642 | 626 | if (multiple || direction == DMA_TO_DEVICE) { |
---|
643 | 627 | t = &host->early_status; |
---|
644 | 628 | 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; |
---|
648 | 630 | t->tx_buf = host->ones; |
---|
649 | 631 | t->tx_dma = host->ones_dma; |
---|
650 | 632 | t->rx_buf = scratch->status; |
---|
.. | .. |
---|
677 | 659 | u32 pattern; |
---|
678 | 660 | |
---|
679 | 661 | 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)); |
---|
682 | 663 | if (host->dma_dev) |
---|
683 | 664 | dma_sync_single_for_device(host->dma_dev, |
---|
684 | 665 | host->data_dma, sizeof(*scratch), |
---|
.. | .. |
---|
859 | 840 | |
---|
860 | 841 | be16_to_cpus(&scratch->crc_val); |
---|
861 | 842 | 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); |
---|
865 | 846 | return -EILSEQ; |
---|
866 | 847 | } |
---|
867 | 848 | } |
---|
.. | .. |
---|
901 | 882 | else |
---|
902 | 883 | clock_rate = spi->max_speed_hz; |
---|
903 | 884 | |
---|
904 | | - timeout = data->timeout_ns + |
---|
| 885 | + timeout = data->timeout_ns / 1000 + |
---|
905 | 886 | 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; |
---|
907 | 888 | |
---|
908 | 889 | /* Handle scatterlist segments one at a time, with synch for |
---|
909 | 890 | * each 512-byte block |
---|
910 | 891 | */ |
---|
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) { |
---|
912 | 893 | int status = 0; |
---|
913 | 894 | dma_addr_t dma_addr = 0; |
---|
914 | 895 | void *kmap_addr; |
---|
.. | .. |
---|
949 | 930 | |
---|
950 | 931 | dev_dbg(&host->spi->dev, |
---|
951 | 932 | " mmc_spi: %s block, %d bytes\n", |
---|
952 | | - (direction == DMA_TO_DEVICE) |
---|
953 | | - ? "write" |
---|
954 | | - : "read", |
---|
| 933 | + (direction == DMA_TO_DEVICE) ? "write" : "read", |
---|
955 | 934 | t->len); |
---|
956 | 935 | |
---|
957 | 936 | if (direction == DMA_TO_DEVICE) |
---|
.. | .. |
---|
978 | 957 | if (status < 0) { |
---|
979 | 958 | data->error = status; |
---|
980 | 959 | dev_dbg(&spi->dev, "%s status %d\n", |
---|
981 | | - (direction == DMA_TO_DEVICE) |
---|
982 | | - ? "write" : "read", |
---|
| 960 | + (direction == DMA_TO_DEVICE) ? "write" : "read", |
---|
983 | 961 | status); |
---|
984 | 962 | break; |
---|
985 | 963 | } |
---|
.. | .. |
---|
1138 | 1116 | /* Try to be very sure any previous command has completed; |
---|
1139 | 1117 | * wait till not-busy, skip debris from any old commands. |
---|
1140 | 1118 | */ |
---|
1141 | | - mmc_spi_wait_unbusy(host, r1b_timeout); |
---|
| 1119 | + mmc_spi_wait_unbusy(host, msecs_to_jiffies(MMC_SPI_INIT_TIMEOUT_MS)); |
---|
1142 | 1120 | mmc_spi_readbytes(host, 10); |
---|
1143 | 1121 | |
---|
1144 | 1122 | /* |
---|
.. | .. |
---|
1258 | 1236 | mres = spi_setup(host->spi); |
---|
1259 | 1237 | if (mres < 0) |
---|
1260 | 1238 | dev_dbg(&host->spi->dev, |
---|
1261 | | - "switch back to SPI mode 3" |
---|
1262 | | - " failed\n"); |
---|
| 1239 | + "switch back to SPI mode 3 failed\n"); |
---|
1263 | 1240 | } |
---|
1264 | 1241 | } |
---|
1265 | 1242 | |
---|
.. | .. |
---|
1300 | 1277 | mmc_detect_change(mmc, msecs_to_jiffies(delay_msec)); |
---|
1301 | 1278 | return IRQ_HANDLED; |
---|
1302 | 1279 | } |
---|
| 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 |
---|
1303 | 1326 | |
---|
1304 | 1327 | static int mmc_spi_probe(struct spi_device *spi) |
---|
1305 | 1328 | { |
---|
.. | .. |
---|
1397 | 1420 | if (!host->data) |
---|
1398 | 1421 | goto fail_nobuf1; |
---|
1399 | 1422 | |
---|
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; |
---|
1417 | 1426 | |
---|
1418 | 1427 | /* setup message for status/busy readback */ |
---|
1419 | 1428 | spi_message_init(&host->readback); |
---|
.. | .. |
---|
1441 | 1450 | |
---|
1442 | 1451 | status = mmc_add_host(mmc); |
---|
1443 | 1452 | if (status != 0) |
---|
1444 | | - goto fail_add_host; |
---|
| 1453 | + goto fail_glue_init; |
---|
1445 | 1454 | |
---|
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 |
---|
1453 | 1465 | * interrupts, so let mmc_gpiod_request_cd_irq() decide |
---|
1454 | 1466 | * if polling is needed or not. |
---|
1455 | 1467 | */ |
---|
.. | .. |
---|
1458 | 1470 | } |
---|
1459 | 1471 | mmc_detect_change(mmc, 0); |
---|
1460 | 1472 | |
---|
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) |
---|
1462 | 1478 | has_ro = true; |
---|
1463 | | - status = mmc_gpio_request_ro(mmc, host->pdata->ro_gpio); |
---|
1464 | | - if (status != 0) |
---|
1465 | | - goto fail_add_host; |
---|
1466 | | - } |
---|
1467 | 1479 | |
---|
1468 | 1480 | dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n", |
---|
1469 | 1481 | dev_name(&mmc->class_dev), |
---|
.. | .. |
---|
1475 | 1487 | ? ", cd polling" : ""); |
---|
1476 | 1488 | return 0; |
---|
1477 | 1489 | |
---|
1478 | | -fail_add_host: |
---|
1479 | | - mmc_remove_host (mmc); |
---|
| 1490 | +fail_gpiod_request: |
---|
| 1491 | + mmc_remove_host(mmc); |
---|
1480 | 1492 | 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: |
---|
1489 | 1495 | kfree(host->data); |
---|
1490 | | - |
---|
1491 | 1496 | fail_nobuf1: |
---|
1492 | 1497 | mmc_free_host(mmc); |
---|
1493 | 1498 | mmc_spi_put_pdata(spi); |
---|
1494 | | - dev_set_drvdata(&spi->dev, NULL); |
---|
1495 | | - |
---|
1496 | 1499 | nomem: |
---|
1497 | 1500 | kfree(ones); |
---|
1498 | 1501 | return status; |
---|
.. | .. |
---|
1502 | 1505 | static int mmc_spi_remove(struct spi_device *spi) |
---|
1503 | 1506 | { |
---|
1504 | 1507 | struct mmc_host *mmc = dev_get_drvdata(&spi->dev); |
---|
1505 | | - struct mmc_spi_host *host; |
---|
| 1508 | + struct mmc_spi_host *host = mmc_priv(mmc); |
---|
1506 | 1509 | |
---|
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); |
---|
1509 | 1513 | |
---|
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); |
---|
1513 | 1515 | |
---|
1514 | | - mmc_remove_host(mmc); |
---|
| 1516 | + mmc_spi_dma_free(host); |
---|
| 1517 | + kfree(host->data); |
---|
| 1518 | + kfree(host->ones); |
---|
1515 | 1519 | |
---|
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); |
---|
1531 | 1523 | return 0; |
---|
1532 | 1524 | } |
---|
1533 | 1525 | |
---|
.. | .. |
---|
1548 | 1540 | |
---|
1549 | 1541 | module_spi_driver(mmc_spi_driver); |
---|
1550 | 1542 | |
---|
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"); |
---|
1553 | 1544 | MODULE_DESCRIPTION("SPI SD/MMC host driver"); |
---|
1554 | 1545 | MODULE_LICENSE("GPL"); |
---|
1555 | 1546 | MODULE_ALIAS("spi:mmc_spi"); |
---|