| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * OMAP2 McSPI controller driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2005, 2006 Nokia Corporation |
|---|
| 5 | 6 | * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and |
|---|
| 6 | 7 | * Juha Yrj�l� <juha.yrjola@nokia.com> |
|---|
| 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. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | | - * GNU General Public License for more details. |
|---|
| 17 | 8 | */ |
|---|
| 18 | 9 | |
|---|
| 19 | 10 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 35 | 26 | #include <linux/gcd.h> |
|---|
| 36 | 27 | |
|---|
| 37 | 28 | #include <linux/spi/spi.h> |
|---|
| 38 | | -#include <linux/gpio.h> |
|---|
| 39 | 29 | |
|---|
| 40 | 30 | #include <linux/platform_data/spi-omap2-mcspi.h> |
|---|
| 41 | 31 | |
|---|
| .. | .. |
|---|
| 126 | 116 | }; |
|---|
| 127 | 117 | |
|---|
| 128 | 118 | struct omap2_mcspi { |
|---|
| 119 | + struct completion txdone; |
|---|
| 129 | 120 | struct spi_master *master; |
|---|
| 130 | 121 | /* Virtual base address of the controller */ |
|---|
| 131 | 122 | void __iomem *base; |
|---|
| .. | .. |
|---|
| 135 | 126 | struct device *dev; |
|---|
| 136 | 127 | struct omap2_mcspi_regs ctx; |
|---|
| 137 | 128 | int fifo_depth; |
|---|
| 129 | + bool slave_aborted; |
|---|
| 138 | 130 | unsigned int pin_dir:1; |
|---|
| 131 | + size_t max_xfer_len; |
|---|
| 139 | 132 | }; |
|---|
| 140 | 133 | |
|---|
| 141 | 134 | struct omap2_mcspi_cs { |
|---|
| .. | .. |
|---|
| 274 | 267 | } |
|---|
| 275 | 268 | } |
|---|
| 276 | 269 | |
|---|
| 277 | | -static void omap2_mcspi_set_master_mode(struct spi_master *master) |
|---|
| 270 | +static void omap2_mcspi_set_mode(struct spi_master *master) |
|---|
| 278 | 271 | { |
|---|
| 279 | 272 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
|---|
| 280 | 273 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
|---|
| 281 | 274 | u32 l; |
|---|
| 282 | 275 | |
|---|
| 283 | 276 | /* |
|---|
| 284 | | - * Setup when switching from (reset default) slave mode |
|---|
| 285 | | - * to single-channel master mode |
|---|
| 277 | + * Choose master or slave mode |
|---|
| 286 | 278 | */ |
|---|
| 287 | 279 | l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL); |
|---|
| 288 | | - l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS); |
|---|
| 289 | | - l |= OMAP2_MCSPI_MODULCTRL_SINGLE; |
|---|
| 280 | + l &= ~(OMAP2_MCSPI_MODULCTRL_STEST); |
|---|
| 281 | + if (spi_controller_is_slave(master)) { |
|---|
| 282 | + l |= (OMAP2_MCSPI_MODULCTRL_MS); |
|---|
| 283 | + } else { |
|---|
| 284 | + l &= ~(OMAP2_MCSPI_MODULCTRL_MS); |
|---|
| 285 | + l |= OMAP2_MCSPI_MODULCTRL_SINGLE; |
|---|
| 286 | + } |
|---|
| 290 | 287 | mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l); |
|---|
| 291 | 288 | |
|---|
| 292 | 289 | ctx->modulctrl = l; |
|---|
| .. | .. |
|---|
| 365 | 362 | return 0; |
|---|
| 366 | 363 | } |
|---|
| 367 | 364 | |
|---|
| 365 | +static int mcspi_wait_for_completion(struct omap2_mcspi *mcspi, |
|---|
| 366 | + struct completion *x) |
|---|
| 367 | +{ |
|---|
| 368 | + if (spi_controller_is_slave(mcspi->master)) { |
|---|
| 369 | + if (wait_for_completion_interruptible(x) || |
|---|
| 370 | + mcspi->slave_aborted) |
|---|
| 371 | + return -EINTR; |
|---|
| 372 | + } else { |
|---|
| 373 | + wait_for_completion(x); |
|---|
| 374 | + } |
|---|
| 375 | + |
|---|
| 376 | + return 0; |
|---|
| 377 | +} |
|---|
| 378 | + |
|---|
| 368 | 379 | static void omap2_mcspi_rx_callback(void *data) |
|---|
| 369 | 380 | { |
|---|
| 370 | 381 | struct spi_device *spi = data; |
|---|
| .. | .. |
|---|
| 395 | 406 | { |
|---|
| 396 | 407 | struct omap2_mcspi *mcspi; |
|---|
| 397 | 408 | struct omap2_mcspi_dma *mcspi_dma; |
|---|
| 409 | + struct dma_async_tx_descriptor *tx; |
|---|
| 398 | 410 | |
|---|
| 399 | 411 | mcspi = spi_master_get_devdata(spi->master); |
|---|
| 400 | 412 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
|---|
| 401 | 413 | |
|---|
| 402 | | - if (mcspi_dma->dma_tx) { |
|---|
| 403 | | - struct dma_async_tx_descriptor *tx; |
|---|
| 414 | + dmaengine_slave_config(mcspi_dma->dma_tx, &cfg); |
|---|
| 404 | 415 | |
|---|
| 405 | | - dmaengine_slave_config(mcspi_dma->dma_tx, &cfg); |
|---|
| 406 | | - |
|---|
| 407 | | - tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl, |
|---|
| 408 | | - xfer->tx_sg.nents, |
|---|
| 409 | | - DMA_MEM_TO_DEV, |
|---|
| 410 | | - DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
|---|
| 411 | | - if (tx) { |
|---|
| 412 | | - tx->callback = omap2_mcspi_tx_callback; |
|---|
| 413 | | - tx->callback_param = spi; |
|---|
| 414 | | - dmaengine_submit(tx); |
|---|
| 415 | | - } else { |
|---|
| 416 | | - /* FIXME: fall back to PIO? */ |
|---|
| 417 | | - } |
|---|
| 416 | + tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl, |
|---|
| 417 | + xfer->tx_sg.nents, |
|---|
| 418 | + DMA_MEM_TO_DEV, |
|---|
| 419 | + DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
|---|
| 420 | + if (tx) { |
|---|
| 421 | + tx->callback = omap2_mcspi_tx_callback; |
|---|
| 422 | + tx->callback_param = spi; |
|---|
| 423 | + dmaengine_submit(tx); |
|---|
| 424 | + } else { |
|---|
| 425 | + /* FIXME: fall back to PIO? */ |
|---|
| 418 | 426 | } |
|---|
| 419 | 427 | dma_async_issue_pending(mcspi_dma->dma_tx); |
|---|
| 420 | 428 | omap2_mcspi_set_dma_req(spi, 0, 1); |
|---|
| 421 | | - |
|---|
| 422 | 429 | } |
|---|
| 423 | 430 | |
|---|
| 424 | 431 | static unsigned |
|---|
| .. | .. |
|---|
| 437 | 444 | int word_len, element_count; |
|---|
| 438 | 445 | struct omap2_mcspi_cs *cs = spi->controller_state; |
|---|
| 439 | 446 | void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0; |
|---|
| 447 | + struct dma_async_tx_descriptor *tx; |
|---|
| 440 | 448 | |
|---|
| 441 | 449 | mcspi = spi_master_get_devdata(spi->master); |
|---|
| 442 | 450 | mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
|---|
| .. | .. |
|---|
| 460 | 468 | else /* word_len <= 32 */ |
|---|
| 461 | 469 | element_count = count >> 2; |
|---|
| 462 | 470 | |
|---|
| 463 | | - if (mcspi_dma->dma_rx) { |
|---|
| 464 | | - struct dma_async_tx_descriptor *tx; |
|---|
| 465 | 471 | |
|---|
| 466 | | - dmaengine_slave_config(mcspi_dma->dma_rx, &cfg); |
|---|
| 472 | + dmaengine_slave_config(mcspi_dma->dma_rx, &cfg); |
|---|
| 467 | 473 | |
|---|
| 474 | + /* |
|---|
| 475 | + * Reduce DMA transfer length by one more if McSPI is |
|---|
| 476 | + * configured in turbo mode. |
|---|
| 477 | + */ |
|---|
| 478 | + if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0) |
|---|
| 479 | + transfer_reduction += es; |
|---|
| 480 | + |
|---|
| 481 | + if (transfer_reduction) { |
|---|
| 482 | + /* Split sgl into two. The second sgl won't be used. */ |
|---|
| 483 | + sizes[0] = count - transfer_reduction; |
|---|
| 484 | + sizes[1] = transfer_reduction; |
|---|
| 485 | + nb_sizes = 2; |
|---|
| 486 | + } else { |
|---|
| 468 | 487 | /* |
|---|
| 469 | | - * Reduce DMA transfer length by one more if McSPI is |
|---|
| 470 | | - * configured in turbo mode. |
|---|
| 488 | + * Don't bother splitting the sgl. This essentially |
|---|
| 489 | + * clones the original sgl. |
|---|
| 471 | 490 | */ |
|---|
| 472 | | - if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0) |
|---|
| 473 | | - transfer_reduction += es; |
|---|
| 491 | + sizes[0] = count; |
|---|
| 492 | + nb_sizes = 1; |
|---|
| 493 | + } |
|---|
| 474 | 494 | |
|---|
| 475 | | - if (transfer_reduction) { |
|---|
| 476 | | - /* Split sgl into two. The second sgl won't be used. */ |
|---|
| 477 | | - sizes[0] = count - transfer_reduction; |
|---|
| 478 | | - sizes[1] = transfer_reduction; |
|---|
| 479 | | - nb_sizes = 2; |
|---|
| 480 | | - } else { |
|---|
| 481 | | - /* |
|---|
| 482 | | - * Don't bother splitting the sgl. This essentially |
|---|
| 483 | | - * clones the original sgl. |
|---|
| 484 | | - */ |
|---|
| 485 | | - sizes[0] = count; |
|---|
| 486 | | - nb_sizes = 1; |
|---|
| 487 | | - } |
|---|
| 495 | + ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents, 0, nb_sizes, |
|---|
| 496 | + sizes, sg_out, out_mapped_nents, GFP_KERNEL); |
|---|
| 488 | 497 | |
|---|
| 489 | | - ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents, |
|---|
| 490 | | - 0, nb_sizes, |
|---|
| 491 | | - sizes, |
|---|
| 492 | | - sg_out, out_mapped_nents, |
|---|
| 493 | | - GFP_KERNEL); |
|---|
| 498 | + if (ret < 0) { |
|---|
| 499 | + dev_err(&spi->dev, "sg_split failed\n"); |
|---|
| 500 | + return 0; |
|---|
| 501 | + } |
|---|
| 494 | 502 | |
|---|
| 495 | | - if (ret < 0) { |
|---|
| 496 | | - dev_err(&spi->dev, "sg_split failed\n"); |
|---|
| 497 | | - return 0; |
|---|
| 498 | | - } |
|---|
| 499 | | - |
|---|
| 500 | | - tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, |
|---|
| 501 | | - sg_out[0], |
|---|
| 502 | | - out_mapped_nents[0], |
|---|
| 503 | | - DMA_DEV_TO_MEM, |
|---|
| 504 | | - DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
|---|
| 505 | | - if (tx) { |
|---|
| 506 | | - tx->callback = omap2_mcspi_rx_callback; |
|---|
| 507 | | - tx->callback_param = spi; |
|---|
| 508 | | - dmaengine_submit(tx); |
|---|
| 509 | | - } else { |
|---|
| 510 | | - /* FIXME: fall back to PIO? */ |
|---|
| 511 | | - } |
|---|
| 503 | + tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, sg_out[0], |
|---|
| 504 | + out_mapped_nents[0], DMA_DEV_TO_MEM, |
|---|
| 505 | + DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
|---|
| 506 | + if (tx) { |
|---|
| 507 | + tx->callback = omap2_mcspi_rx_callback; |
|---|
| 508 | + tx->callback_param = spi; |
|---|
| 509 | + dmaengine_submit(tx); |
|---|
| 510 | + } else { |
|---|
| 511 | + /* FIXME: fall back to PIO? */ |
|---|
| 512 | 512 | } |
|---|
| 513 | 513 | |
|---|
| 514 | 514 | dma_async_issue_pending(mcspi_dma->dma_rx); |
|---|
| 515 | 515 | omap2_mcspi_set_dma_req(spi, 1, 1); |
|---|
| 516 | 516 | |
|---|
| 517 | | - wait_for_completion(&mcspi_dma->dma_rx_completion); |
|---|
| 517 | + ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_rx_completion); |
|---|
| 518 | + if (ret || mcspi->slave_aborted) { |
|---|
| 519 | + dmaengine_terminate_sync(mcspi_dma->dma_rx); |
|---|
| 520 | + omap2_mcspi_set_dma_req(spi, 1, 0); |
|---|
| 521 | + return 0; |
|---|
| 522 | + } |
|---|
| 518 | 523 | |
|---|
| 519 | 524 | for (x = 0; x < nb_sizes; x++) |
|---|
| 520 | 525 | kfree(sg_out[x]); |
|---|
| .. | .. |
|---|
| 613 | 618 | rx = xfer->rx_buf; |
|---|
| 614 | 619 | tx = xfer->tx_buf; |
|---|
| 615 | 620 | |
|---|
| 616 | | - if (tx != NULL) |
|---|
| 621 | + mcspi->slave_aborted = false; |
|---|
| 622 | + reinit_completion(&mcspi_dma->dma_tx_completion); |
|---|
| 623 | + reinit_completion(&mcspi_dma->dma_rx_completion); |
|---|
| 624 | + reinit_completion(&mcspi->txdone); |
|---|
| 625 | + if (tx) { |
|---|
| 626 | + /* Enable EOW IRQ to know end of tx in slave mode */ |
|---|
| 627 | + if (spi_controller_is_slave(spi->master)) |
|---|
| 628 | + mcspi_write_reg(spi->master, |
|---|
| 629 | + OMAP2_MCSPI_IRQENABLE, |
|---|
| 630 | + OMAP2_MCSPI_IRQSTATUS_EOW); |
|---|
| 617 | 631 | omap2_mcspi_tx_dma(spi, xfer, cfg); |
|---|
| 632 | + } |
|---|
| 618 | 633 | |
|---|
| 619 | 634 | if (rx != NULL) |
|---|
| 620 | 635 | count = omap2_mcspi_rx_dma(spi, xfer, cfg, es); |
|---|
| 621 | 636 | |
|---|
| 622 | 637 | if (tx != NULL) { |
|---|
| 623 | | - wait_for_completion(&mcspi_dma->dma_tx_completion); |
|---|
| 638 | + int ret; |
|---|
| 639 | + |
|---|
| 640 | + ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion); |
|---|
| 641 | + if (ret || mcspi->slave_aborted) { |
|---|
| 642 | + dmaengine_terminate_sync(mcspi_dma->dma_tx); |
|---|
| 643 | + omap2_mcspi_set_dma_req(spi, 0, 0); |
|---|
| 644 | + return 0; |
|---|
| 645 | + } |
|---|
| 646 | + |
|---|
| 647 | + if (spi_controller_is_slave(mcspi->master)) { |
|---|
| 648 | + ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone); |
|---|
| 649 | + if (ret || mcspi->slave_aborted) |
|---|
| 650 | + return 0; |
|---|
| 651 | + } |
|---|
| 624 | 652 | |
|---|
| 625 | 653 | if (mcspi->fifo_depth > 0) { |
|---|
| 626 | 654 | irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS; |
|---|
| .. | .. |
|---|
| 955 | 983 | * Note that we currently allow DMA only if we get a channel |
|---|
| 956 | 984 | * for both rx and tx. Otherwise we'll do PIO for both rx and tx. |
|---|
| 957 | 985 | */ |
|---|
| 958 | | -static int omap2_mcspi_request_dma(struct spi_device *spi) |
|---|
| 986 | +static int omap2_mcspi_request_dma(struct omap2_mcspi *mcspi, |
|---|
| 987 | + struct omap2_mcspi_dma *mcspi_dma) |
|---|
| 959 | 988 | { |
|---|
| 960 | | - struct spi_master *master = spi->master; |
|---|
| 961 | | - struct omap2_mcspi *mcspi; |
|---|
| 962 | | - struct omap2_mcspi_dma *mcspi_dma; |
|---|
| 963 | 989 | int ret = 0; |
|---|
| 964 | 990 | |
|---|
| 965 | | - mcspi = spi_master_get_devdata(master); |
|---|
| 966 | | - mcspi_dma = mcspi->dma_channels + spi->chip_select; |
|---|
| 967 | | - |
|---|
| 968 | | - init_completion(&mcspi_dma->dma_rx_completion); |
|---|
| 969 | | - init_completion(&mcspi_dma->dma_tx_completion); |
|---|
| 970 | | - |
|---|
| 971 | | - mcspi_dma->dma_rx = dma_request_chan(&master->dev, |
|---|
| 991 | + mcspi_dma->dma_rx = dma_request_chan(mcspi->dev, |
|---|
| 972 | 992 | mcspi_dma->dma_rx_ch_name); |
|---|
| 973 | 993 | if (IS_ERR(mcspi_dma->dma_rx)) { |
|---|
| 974 | 994 | ret = PTR_ERR(mcspi_dma->dma_rx); |
|---|
| .. | .. |
|---|
| 976 | 996 | goto no_dma; |
|---|
| 977 | 997 | } |
|---|
| 978 | 998 | |
|---|
| 979 | | - mcspi_dma->dma_tx = dma_request_chan(&master->dev, |
|---|
| 999 | + mcspi_dma->dma_tx = dma_request_chan(mcspi->dev, |
|---|
| 980 | 1000 | mcspi_dma->dma_tx_ch_name); |
|---|
| 981 | 1001 | if (IS_ERR(mcspi_dma->dma_tx)) { |
|---|
| 982 | 1002 | ret = PTR_ERR(mcspi_dma->dma_tx); |
|---|
| .. | .. |
|---|
| 985 | 1005 | mcspi_dma->dma_rx = NULL; |
|---|
| 986 | 1006 | } |
|---|
| 987 | 1007 | |
|---|
| 1008 | + init_completion(&mcspi_dma->dma_rx_completion); |
|---|
| 1009 | + init_completion(&mcspi_dma->dma_tx_completion); |
|---|
| 1010 | + |
|---|
| 988 | 1011 | no_dma: |
|---|
| 989 | 1012 | return ret; |
|---|
| 990 | 1013 | } |
|---|
| 991 | 1014 | |
|---|
| 1015 | +static void omap2_mcspi_release_dma(struct spi_master *master) |
|---|
| 1016 | +{ |
|---|
| 1017 | + struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
|---|
| 1018 | + struct omap2_mcspi_dma *mcspi_dma; |
|---|
| 1019 | + int i; |
|---|
| 1020 | + |
|---|
| 1021 | + for (i = 0; i < master->num_chipselect; i++) { |
|---|
| 1022 | + mcspi_dma = &mcspi->dma_channels[i]; |
|---|
| 1023 | + |
|---|
| 1024 | + if (mcspi_dma->dma_rx) { |
|---|
| 1025 | + dma_release_channel(mcspi_dma->dma_rx); |
|---|
| 1026 | + mcspi_dma->dma_rx = NULL; |
|---|
| 1027 | + } |
|---|
| 1028 | + if (mcspi_dma->dma_tx) { |
|---|
| 1029 | + dma_release_channel(mcspi_dma->dma_tx); |
|---|
| 1030 | + mcspi_dma->dma_tx = NULL; |
|---|
| 1031 | + } |
|---|
| 1032 | + } |
|---|
| 1033 | +} |
|---|
| 1034 | + |
|---|
| 1035 | +static void omap2_mcspi_cleanup(struct spi_device *spi) |
|---|
| 1036 | +{ |
|---|
| 1037 | + struct omap2_mcspi_cs *cs; |
|---|
| 1038 | + |
|---|
| 1039 | + if (spi->controller_state) { |
|---|
| 1040 | + /* Unlink controller state from context save list */ |
|---|
| 1041 | + cs = spi->controller_state; |
|---|
| 1042 | + list_del(&cs->node); |
|---|
| 1043 | + |
|---|
| 1044 | + kfree(cs); |
|---|
| 1045 | + } |
|---|
| 1046 | +} |
|---|
| 1047 | + |
|---|
| 992 | 1048 | static int omap2_mcspi_setup(struct spi_device *spi) |
|---|
| 993 | 1049 | { |
|---|
| 1050 | + bool initial_setup = false; |
|---|
| 994 | 1051 | int ret; |
|---|
| 995 | 1052 | struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
|---|
| 996 | 1053 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
|---|
| 997 | | - struct omap2_mcspi_dma *mcspi_dma; |
|---|
| 998 | 1054 | struct omap2_mcspi_cs *cs = spi->controller_state; |
|---|
| 999 | | - |
|---|
| 1000 | | - mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
|---|
| 1001 | 1055 | |
|---|
| 1002 | 1056 | if (!cs) { |
|---|
| 1003 | 1057 | cs = kzalloc(sizeof *cs, GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 1011 | 1065 | spi->controller_state = cs; |
|---|
| 1012 | 1066 | /* Link this to context save list */ |
|---|
| 1013 | 1067 | list_add_tail(&cs->node, &ctx->cs); |
|---|
| 1014 | | - |
|---|
| 1015 | | - if (gpio_is_valid(spi->cs_gpio)) { |
|---|
| 1016 | | - ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev)); |
|---|
| 1017 | | - if (ret) { |
|---|
| 1018 | | - dev_err(&spi->dev, "failed to request gpio\n"); |
|---|
| 1019 | | - return ret; |
|---|
| 1020 | | - } |
|---|
| 1021 | | - gpio_direction_output(spi->cs_gpio, |
|---|
| 1022 | | - !(spi->mode & SPI_CS_HIGH)); |
|---|
| 1023 | | - } |
|---|
| 1024 | | - } |
|---|
| 1025 | | - |
|---|
| 1026 | | - if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) { |
|---|
| 1027 | | - ret = omap2_mcspi_request_dma(spi); |
|---|
| 1028 | | - if (ret) |
|---|
| 1029 | | - dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n", |
|---|
| 1030 | | - ret); |
|---|
| 1068 | + initial_setup = true; |
|---|
| 1031 | 1069 | } |
|---|
| 1032 | 1070 | |
|---|
| 1033 | 1071 | ret = pm_runtime_get_sync(mcspi->dev); |
|---|
| 1034 | 1072 | if (ret < 0) { |
|---|
| 1035 | 1073 | pm_runtime_put_noidle(mcspi->dev); |
|---|
| 1074 | + if (initial_setup) |
|---|
| 1075 | + omap2_mcspi_cleanup(spi); |
|---|
| 1036 | 1076 | |
|---|
| 1037 | 1077 | return ret; |
|---|
| 1038 | 1078 | } |
|---|
| 1039 | 1079 | |
|---|
| 1040 | 1080 | ret = omap2_mcspi_setup_transfer(spi, NULL); |
|---|
| 1081 | + if (ret && initial_setup) |
|---|
| 1082 | + omap2_mcspi_cleanup(spi); |
|---|
| 1083 | + |
|---|
| 1041 | 1084 | pm_runtime_mark_last_busy(mcspi->dev); |
|---|
| 1042 | 1085 | pm_runtime_put_autosuspend(mcspi->dev); |
|---|
| 1043 | 1086 | |
|---|
| 1044 | 1087 | return ret; |
|---|
| 1045 | 1088 | } |
|---|
| 1046 | 1089 | |
|---|
| 1047 | | -static void omap2_mcspi_cleanup(struct spi_device *spi) |
|---|
| 1090 | +static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data) |
|---|
| 1048 | 1091 | { |
|---|
| 1049 | | - struct omap2_mcspi *mcspi; |
|---|
| 1050 | | - struct omap2_mcspi_dma *mcspi_dma; |
|---|
| 1051 | | - struct omap2_mcspi_cs *cs; |
|---|
| 1092 | + struct omap2_mcspi *mcspi = data; |
|---|
| 1093 | + u32 irqstat; |
|---|
| 1052 | 1094 | |
|---|
| 1053 | | - mcspi = spi_master_get_devdata(spi->master); |
|---|
| 1095 | + irqstat = mcspi_read_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS); |
|---|
| 1096 | + if (!irqstat) |
|---|
| 1097 | + return IRQ_NONE; |
|---|
| 1054 | 1098 | |
|---|
| 1055 | | - if (spi->controller_state) { |
|---|
| 1056 | | - /* Unlink controller state from context save list */ |
|---|
| 1057 | | - cs = spi->controller_state; |
|---|
| 1058 | | - list_del(&cs->node); |
|---|
| 1099 | + /* Disable IRQ and wakeup slave xfer task */ |
|---|
| 1100 | + mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQENABLE, 0); |
|---|
| 1101 | + if (irqstat & OMAP2_MCSPI_IRQSTATUS_EOW) |
|---|
| 1102 | + complete(&mcspi->txdone); |
|---|
| 1059 | 1103 | |
|---|
| 1060 | | - kfree(cs); |
|---|
| 1061 | | - } |
|---|
| 1104 | + return IRQ_HANDLED; |
|---|
| 1105 | +} |
|---|
| 1062 | 1106 | |
|---|
| 1063 | | - if (spi->chip_select < spi->master->num_chipselect) { |
|---|
| 1064 | | - mcspi_dma = &mcspi->dma_channels[spi->chip_select]; |
|---|
| 1107 | +static int omap2_mcspi_slave_abort(struct spi_master *master) |
|---|
| 1108 | +{ |
|---|
| 1109 | + struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
|---|
| 1110 | + struct omap2_mcspi_dma *mcspi_dma = mcspi->dma_channels; |
|---|
| 1065 | 1111 | |
|---|
| 1066 | | - if (mcspi_dma->dma_rx) { |
|---|
| 1067 | | - dma_release_channel(mcspi_dma->dma_rx); |
|---|
| 1068 | | - mcspi_dma->dma_rx = NULL; |
|---|
| 1069 | | - } |
|---|
| 1070 | | - if (mcspi_dma->dma_tx) { |
|---|
| 1071 | | - dma_release_channel(mcspi_dma->dma_tx); |
|---|
| 1072 | | - mcspi_dma->dma_tx = NULL; |
|---|
| 1073 | | - } |
|---|
| 1074 | | - } |
|---|
| 1112 | + mcspi->slave_aborted = true; |
|---|
| 1113 | + complete(&mcspi_dma->dma_rx_completion); |
|---|
| 1114 | + complete(&mcspi_dma->dma_tx_completion); |
|---|
| 1115 | + complete(&mcspi->txdone); |
|---|
| 1075 | 1116 | |
|---|
| 1076 | | - if (gpio_is_valid(spi->cs_gpio)) |
|---|
| 1077 | | - gpio_free(spi->cs_gpio); |
|---|
| 1117 | + return 0; |
|---|
| 1078 | 1118 | } |
|---|
| 1079 | 1119 | |
|---|
| 1080 | 1120 | static int omap2_mcspi_transfer_one(struct spi_master *master, |
|---|
| .. | .. |
|---|
| 1114 | 1154 | |
|---|
| 1115 | 1155 | omap2_mcspi_set_enable(spi, 0); |
|---|
| 1116 | 1156 | |
|---|
| 1117 | | - if (gpio_is_valid(spi->cs_gpio)) |
|---|
| 1157 | + if (spi->cs_gpiod) |
|---|
| 1118 | 1158 | omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH); |
|---|
| 1119 | 1159 | |
|---|
| 1120 | 1160 | if (par_override || |
|---|
| .. | .. |
|---|
| 1203 | 1243 | |
|---|
| 1204 | 1244 | omap2_mcspi_set_enable(spi, 0); |
|---|
| 1205 | 1245 | |
|---|
| 1206 | | - if (gpio_is_valid(spi->cs_gpio)) |
|---|
| 1246 | + if (spi->cs_gpiod) |
|---|
| 1207 | 1247 | omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH)); |
|---|
| 1208 | 1248 | |
|---|
| 1209 | 1249 | if (mcspi->fifo_depth > 0 && t) |
|---|
| .. | .. |
|---|
| 1243 | 1283 | struct spi_device *spi, |
|---|
| 1244 | 1284 | struct spi_transfer *xfer) |
|---|
| 1245 | 1285 | { |
|---|
| 1286 | + struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
|---|
| 1287 | + struct omap2_mcspi_dma *mcspi_dma = |
|---|
| 1288 | + &mcspi->dma_channels[spi->chip_select]; |
|---|
| 1289 | + |
|---|
| 1290 | + if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) |
|---|
| 1291 | + return false; |
|---|
| 1292 | + |
|---|
| 1293 | + if (spi_controller_is_slave(master)) |
|---|
| 1294 | + return true; |
|---|
| 1295 | + |
|---|
| 1296 | + master->dma_rx = mcspi_dma->dma_rx; |
|---|
| 1297 | + master->dma_tx = mcspi_dma->dma_tx; |
|---|
| 1298 | + |
|---|
| 1246 | 1299 | return (xfer->len >= DMA_MIN_BYTES); |
|---|
| 1247 | 1300 | } |
|---|
| 1248 | 1301 | |
|---|
| 1249 | | -static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi) |
|---|
| 1302 | +static size_t omap2_mcspi_max_xfer_size(struct spi_device *spi) |
|---|
| 1303 | +{ |
|---|
| 1304 | + struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master); |
|---|
| 1305 | + struct omap2_mcspi_dma *mcspi_dma = |
|---|
| 1306 | + &mcspi->dma_channels[spi->chip_select]; |
|---|
| 1307 | + |
|---|
| 1308 | + if (mcspi->max_xfer_len && mcspi_dma->dma_rx) |
|---|
| 1309 | + return mcspi->max_xfer_len; |
|---|
| 1310 | + |
|---|
| 1311 | + return SIZE_MAX; |
|---|
| 1312 | +} |
|---|
| 1313 | + |
|---|
| 1314 | +static int omap2_mcspi_controller_setup(struct omap2_mcspi *mcspi) |
|---|
| 1250 | 1315 | { |
|---|
| 1251 | 1316 | struct spi_master *master = mcspi->master; |
|---|
| 1252 | 1317 | struct omap2_mcspi_regs *ctx = &mcspi->ctx; |
|---|
| .. | .. |
|---|
| 1263 | 1328 | OMAP2_MCSPI_WAKEUPENABLE_WKEN); |
|---|
| 1264 | 1329 | ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN; |
|---|
| 1265 | 1330 | |
|---|
| 1266 | | - omap2_mcspi_set_master_mode(master); |
|---|
| 1331 | + omap2_mcspi_set_mode(master); |
|---|
| 1267 | 1332 | pm_runtime_mark_last_busy(mcspi->dev); |
|---|
| 1268 | 1333 | pm_runtime_put_autosuspend(mcspi->dev); |
|---|
| 1269 | 1334 | return 0; |
|---|
| .. | .. |
|---|
| 1314 | 1379 | .regs_offset = OMAP4_MCSPI_REG_OFFSET, |
|---|
| 1315 | 1380 | }; |
|---|
| 1316 | 1381 | |
|---|
| 1382 | +static struct omap2_mcspi_platform_config am654_pdata = { |
|---|
| 1383 | + .regs_offset = OMAP4_MCSPI_REG_OFFSET, |
|---|
| 1384 | + .max_xfer_len = SZ_4K - 1, |
|---|
| 1385 | +}; |
|---|
| 1386 | + |
|---|
| 1317 | 1387 | static const struct of_device_id omap_mcspi_of_match[] = { |
|---|
| 1318 | 1388 | { |
|---|
| 1319 | 1389 | .compatible = "ti,omap2-mcspi", |
|---|
| .. | .. |
|---|
| 1322 | 1392 | { |
|---|
| 1323 | 1393 | .compatible = "ti,omap4-mcspi", |
|---|
| 1324 | 1394 | .data = &omap4_pdata, |
|---|
| 1395 | + }, |
|---|
| 1396 | + { |
|---|
| 1397 | + .compatible = "ti,am654-mcspi", |
|---|
| 1398 | + .data = &am654_pdata, |
|---|
| 1325 | 1399 | }, |
|---|
| 1326 | 1400 | { }, |
|---|
| 1327 | 1401 | }; |
|---|
| .. | .. |
|---|
| 1338 | 1412 | struct device_node *node = pdev->dev.of_node; |
|---|
| 1339 | 1413 | const struct of_device_id *match; |
|---|
| 1340 | 1414 | |
|---|
| 1341 | | - master = spi_alloc_master(&pdev->dev, sizeof *mcspi); |
|---|
| 1342 | | - if (master == NULL) { |
|---|
| 1343 | | - dev_dbg(&pdev->dev, "master allocation failed\n"); |
|---|
| 1415 | + if (of_property_read_bool(node, "spi-slave")) |
|---|
| 1416 | + master = spi_alloc_slave(&pdev->dev, sizeof(*mcspi)); |
|---|
| 1417 | + else |
|---|
| 1418 | + master = spi_alloc_master(&pdev->dev, sizeof(*mcspi)); |
|---|
| 1419 | + if (!master) |
|---|
| 1344 | 1420 | return -ENOMEM; |
|---|
| 1345 | | - } |
|---|
| 1346 | 1421 | |
|---|
| 1347 | 1422 | /* the spi->mode bits understood by this driver: */ |
|---|
| 1348 | 1423 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
|---|
| .. | .. |
|---|
| 1354 | 1429 | master->transfer_one = omap2_mcspi_transfer_one; |
|---|
| 1355 | 1430 | master->set_cs = omap2_mcspi_set_cs; |
|---|
| 1356 | 1431 | master->cleanup = omap2_mcspi_cleanup; |
|---|
| 1432 | + master->slave_abort = omap2_mcspi_slave_abort; |
|---|
| 1357 | 1433 | master->dev.of_node = node; |
|---|
| 1358 | 1434 | master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ; |
|---|
| 1359 | 1435 | master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15; |
|---|
| 1436 | + master->use_gpio_descriptors = true; |
|---|
| 1360 | 1437 | |
|---|
| 1361 | 1438 | platform_set_drvdata(pdev, master); |
|---|
| 1362 | 1439 | |
|---|
| .. | .. |
|---|
| 1378 | 1455 | mcspi->pin_dir = pdata->pin_dir; |
|---|
| 1379 | 1456 | } |
|---|
| 1380 | 1457 | regs_offset = pdata->regs_offset; |
|---|
| 1458 | + if (pdata->max_xfer_len) { |
|---|
| 1459 | + mcspi->max_xfer_len = pdata->max_xfer_len; |
|---|
| 1460 | + master->max_transfer_size = omap2_mcspi_max_xfer_size; |
|---|
| 1461 | + } |
|---|
| 1381 | 1462 | |
|---|
| 1382 | 1463 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 1383 | 1464 | mcspi->base = devm_ioremap_resource(&pdev->dev, r); |
|---|
| .. | .. |
|---|
| 1403 | 1484 | for (i = 0; i < master->num_chipselect; i++) { |
|---|
| 1404 | 1485 | sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i); |
|---|
| 1405 | 1486 | sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i); |
|---|
| 1487 | + |
|---|
| 1488 | + status = omap2_mcspi_request_dma(mcspi, |
|---|
| 1489 | + &mcspi->dma_channels[i]); |
|---|
| 1490 | + if (status == -EPROBE_DEFER) |
|---|
| 1491 | + goto free_master; |
|---|
| 1492 | + } |
|---|
| 1493 | + |
|---|
| 1494 | + status = platform_get_irq(pdev, 0); |
|---|
| 1495 | + if (status == -EPROBE_DEFER) |
|---|
| 1496 | + goto free_master; |
|---|
| 1497 | + if (status < 0) { |
|---|
| 1498 | + dev_err(&pdev->dev, "no irq resource found\n"); |
|---|
| 1499 | + goto free_master; |
|---|
| 1500 | + } |
|---|
| 1501 | + init_completion(&mcspi->txdone); |
|---|
| 1502 | + status = devm_request_irq(&pdev->dev, status, |
|---|
| 1503 | + omap2_mcspi_irq_handler, 0, pdev->name, |
|---|
| 1504 | + mcspi); |
|---|
| 1505 | + if (status) { |
|---|
| 1506 | + dev_err(&pdev->dev, "Cannot request IRQ"); |
|---|
| 1507 | + goto free_master; |
|---|
| 1406 | 1508 | } |
|---|
| 1407 | 1509 | |
|---|
| 1408 | 1510 | pm_runtime_use_autosuspend(&pdev->dev); |
|---|
| 1409 | 1511 | pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); |
|---|
| 1410 | 1512 | pm_runtime_enable(&pdev->dev); |
|---|
| 1411 | 1513 | |
|---|
| 1412 | | - status = omap2_mcspi_master_setup(mcspi); |
|---|
| 1514 | + status = omap2_mcspi_controller_setup(mcspi); |
|---|
| 1413 | 1515 | if (status < 0) |
|---|
| 1414 | 1516 | goto disable_pm; |
|---|
| 1415 | 1517 | |
|---|
| 1416 | | - status = devm_spi_register_master(&pdev->dev, master); |
|---|
| 1518 | + status = devm_spi_register_controller(&pdev->dev, master); |
|---|
| 1417 | 1519 | if (status < 0) |
|---|
| 1418 | 1520 | goto disable_pm; |
|---|
| 1419 | 1521 | |
|---|
| .. | .. |
|---|
| 1424 | 1526 | pm_runtime_put_sync(&pdev->dev); |
|---|
| 1425 | 1527 | pm_runtime_disable(&pdev->dev); |
|---|
| 1426 | 1528 | free_master: |
|---|
| 1529 | + omap2_mcspi_release_dma(master); |
|---|
| 1427 | 1530 | spi_master_put(master); |
|---|
| 1428 | 1531 | return status; |
|---|
| 1429 | 1532 | } |
|---|
| .. | .. |
|---|
| 1433 | 1536 | struct spi_master *master = platform_get_drvdata(pdev); |
|---|
| 1434 | 1537 | struct omap2_mcspi *mcspi = spi_master_get_devdata(master); |
|---|
| 1435 | 1538 | |
|---|
| 1539 | + omap2_mcspi_release_dma(master); |
|---|
| 1540 | + |
|---|
| 1436 | 1541 | pm_runtime_dont_use_autosuspend(mcspi->dev); |
|---|
| 1437 | 1542 | pm_runtime_put_sync(mcspi->dev); |
|---|
| 1438 | 1543 | pm_runtime_disable(&pdev->dev); |
|---|