23 #include <linux/device.h>
25 #include <linux/bitops.h>
27 #include <linux/module.h>
30 #include <linux/sched.h>
38 #define SSPCR0_MODE_SHIFT 6
39 #define SSPCR0_SCR_SHIFT 8
42 #define SSPCR1_RIE BIT(0)
43 #define SSPCR1_TIE BIT(1)
44 #define SSPCR1_RORIE BIT(2)
45 #define SSPCR1_LBM BIT(3)
46 #define SSPCR1_SSE BIT(4)
47 #define SSPCR1_MS BIT(5)
48 #define SSPCR1_SOD BIT(6)
53 #define SSPSR_TFE BIT(0)
54 #define SSPSR_TNF BIT(1)
55 #define SSPSR_RNE BIT(2)
56 #define SSPSR_RFF BIT(3)
57 #define SSPSR_BSY BIT(4)
58 #define SSPCPSR 0x0010
61 #define SSPIIR_RIS BIT(0)
62 #define SSPIIR_TIS BIT(1)
63 #define SSPIIR_RORIS BIT(2)
69 #define SPI_FIFO_SIZE 8
159 #define bits_per_word_to_dss(bpw) ((bpw) - 1)
185 static int ep93xx_spi_enable(
const struct ep93xx_spi *espi)
194 regval = ep93xx_spi_read_u8(espi,
SSPCR1);
196 ep93xx_spi_write_u8(espi,
SSPCR1, regval);
201 static void ep93xx_spi_disable(
const struct ep93xx_spi *espi)
205 regval = ep93xx_spi_read_u8(espi,
SSPCR1);
207 ep93xx_spi_write_u8(espi,
SSPCR1, regval);
212 static void ep93xx_spi_enable_interrupts(
const struct ep93xx_spi *espi)
216 regval = ep93xx_spi_read_u8(espi,
SSPCR1);
218 ep93xx_spi_write_u8(espi,
SSPCR1, regval);
221 static void ep93xx_spi_disable_interrupts(
const struct ep93xx_spi *espi)
225 regval = ep93xx_spi_read_u8(espi,
SSPCR1);
227 ep93xx_spi_write_u8(espi,
SSPCR1, regval);
241 static int ep93xx_spi_calc_divisors(
const struct ep93xx_spi *espi,
263 for (cpsr = 2; cpsr <= 254; cpsr += 2) {
264 for (scr = 0; scr <= 255; scr++) {
265 if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
281 if (chip->
ops && chip->
ops->cs_control)
282 chip->
ops->cs_control(spi, value);
294 static int ep93xx_spi_setup(
struct spi_device *spi)
300 dev_err(&espi->
pdev->dev,
"invalid bits per word %d\n",
305 chip = spi_get_ctldata(spi);
307 dev_dbg(&espi->
pdev->dev,
"initial setup for %s\n",
317 if (chip->
ops && chip->
ops->setup) {
318 int ret = chip->
ops->setup(spi);
325 spi_set_ctldata(spi, chip);
331 err = ep93xx_spi_calc_divisors(espi, chip, spi->
max_speed_hz);
333 spi_set_ctldata(spi,
NULL);
342 ep93xx_spi_cs_control(spi,
false);
388 spin_unlock_irqrestore(&espi->
lock, flags);
393 spin_unlock_irqrestore(&espi->
lock, flags);
405 static void ep93xx_spi_cleanup(
struct spi_device *spi)
409 chip = spi_get_ctldata(spi);
411 if (chip->
ops && chip->
ops->cleanup)
412 chip->
ops->cleanup(spi);
413 spi_set_ctldata(spi,
NULL);
427 static void ep93xx_spi_chip_setup(
const struct ep93xx_spi *espi,
436 dev_dbg(&espi->
pdev->dev,
"setup: mode %d, cpsr %d, scr %d, dss %d\n",
441 ep93xx_spi_write_u16(espi,
SSPCR0, cr0);
444 static inline int bits_per_word(
const struct ep93xx_spi *espi)
459 ep93xx_spi_write_u16(espi,
SSPDR, tx_val);
460 espi->
tx +=
sizeof(tx_val);
466 ep93xx_spi_write_u8(espi,
SSPDR, tx_val);
467 espi->
tx +=
sizeof(tx_val);
476 rx_val = ep93xx_spi_read_u16(espi,
SSPDR);
479 espi->
rx +=
sizeof(rx_val);
483 rx_val = ep93xx_spi_read_u8(espi,
SSPDR);
486 espi->
rx +=
sizeof(rx_val);
501 static int ep93xx_spi_read_write(
struct ep93xx_spi *espi)
508 ep93xx_do_read(espi, t);
514 ep93xx_do_write(espi, t);
518 if (espi->
rx == t->
len)
524 static void ep93xx_spi_pio_transfer(
struct ep93xx_spi *espi)
530 if (ep93xx_spi_read_write(espi)) {
531 ep93xx_spi_enable_interrupts(espi);
555 const void *
buf, *pbuf;
559 if (bits_per_word(espi) > 8)
564 memset(&conf, 0,
sizeof(conf));
565 conf.direction = dir;
583 ret = dmaengine_slave_config(chan, &conf);
598 if (nents != sgt->
nents) {
623 dev_warn(&espi->
pdev->dev,
"len = %d expected 0!", len);
631 txd = dmaengine_prep_slave_sg(chan, sgt->
sgl, nents, dir,
DMA_CTRL_ACK);
647 static void ep93xx_spi_dma_finish(
struct ep93xx_spi *espi,
664 static void ep93xx_spi_dma_callback(
void *callback_param)
669 static void ep93xx_spi_dma_transfer(
struct ep93xx_spi *espi)
676 dev_err(&espi->
pdev->dev,
"DMA RX failed: %ld\n", PTR_ERR(rxd));
677 msg->
status = PTR_ERR(rxd);
684 dev_err(&espi->
pdev->dev,
"DMA TX failed: %ld\n", PTR_ERR(rxd));
685 msg->
status = PTR_ERR(txd);
690 rxd->
callback = ep93xx_spi_dma_callback;
694 dmaengine_submit(rxd);
695 dmaengine_submit(txd);
697 dma_async_issue_pending(espi->
dma_rx);
698 dma_async_issue_pending(espi->
dma_tx);
716 static void ep93xx_spi_process_transfer(
struct ep93xx_spi *espi,
735 err = ep93xx_spi_calc_divisors(espi, &tmp_chip,
739 "failed to adjust speed\n");
751 ep93xx_spi_chip_setup(espi, &tmp_chip);
763 ep93xx_spi_dma_transfer(espi);
765 ep93xx_spi_pio_transfer(espi);
791 ep93xx_spi_cs_control(msg->
spi,
false);
793 ep93xx_spi_cs_control(msg->
spi,
true);
798 ep93xx_spi_chip_setup(espi, chip);
813 static void ep93xx_spi_process_message(
struct ep93xx_spi *espi,
823 err = ep93xx_spi_enable(espi);
825 dev_err(&espi->
pdev->dev,
"failed to enable SPI controller\n");
834 while (ep93xx_spi_read_u16(espi,
SSPSR) & SSPSR_RNE) {
837 "timeout while flushing RX FIFO\n");
841 ep93xx_spi_read_u16(espi,
SSPDR);
854 ep93xx_spi_chip_setup(espi, spi_get_ctldata(msg->
spi));
855 ep93xx_spi_cs_control(msg->
spi,
true);
858 ep93xx_spi_process_transfer(espi, msg, t);
867 ep93xx_spi_cs_control(msg->
spi,
false);
868 ep93xx_spi_disable(espi);
871 #define work_to_espi(work) (container_of((work), struct ep93xx_spi, msg_work))
890 spin_lock_irq(&espi->
lock);
893 spin_unlock_irq(&espi->
lock);
897 list_del_init(&msg->
queue);
899 spin_unlock_irq(&espi->
lock);
901 ep93xx_spi_process_message(espi, msg);
907 spin_lock_irq(&espi->
lock);
911 spin_unlock_irq(&espi->
lock);
920 u8 irq_status = ep93xx_spi_read_u8(espi,
SSPIIR);
928 ep93xx_spi_write_u8(espi,
SSPICR, 0);
930 "receive overrun, aborting the message\n");
937 if (ep93xx_spi_read_write(espi)) {
952 ep93xx_spi_disable_interrupts(espi);
957 static bool ep93xx_spi_dma_filter(
struct dma_chan *chan,
void *filter_param)
959 if (ep93xx_dma_chan_is_m2p(chan))
966 static int ep93xx_spi_setup_dma(
struct ep93xx_spi *espi)
997 goto fail_release_rx;
1011 static void ep93xx_spi_release_dma(
struct ep93xx_spi *espi)
1035 info = pdev->
dev.platform_data;
1039 dev_err(&pdev->
dev,
"failed to allocate spi master\n");
1043 master->
setup = ep93xx_spi_setup;
1044 master->
transfer = ep93xx_spi_transfer;
1045 master->
cleanup = ep93xx_spi_cleanup;
1050 platform_set_drvdata(pdev, master);
1052 espi = spi_master_get_devdata(master);
1055 if (IS_ERR(espi->
clk)) {
1056 dev_err(&pdev->
dev,
"unable to get spi clock\n");
1057 error = PTR_ERR(espi->
clk);
1058 goto fail_release_master;
1062 init_completion(&espi->
wait);
1075 dev_err(&pdev->
dev,
"failed to get irq resources\n");
1076 goto fail_put_clock;
1081 dev_err(&pdev->
dev,
"unable to get iomem resource\n");
1083 goto fail_put_clock;
1090 dev_err(&pdev->
dev,
"failed to map resources\n");
1092 goto fail_put_clock;
1095 error = devm_request_irq(&pdev->
dev, irq, ep93xx_spi_interrupt,
1096 0,
"ep93xx-spi", espi);
1098 dev_err(&pdev->
dev,
"failed to request irq\n");
1099 goto fail_put_clock;
1102 if (info->
use_dma && ep93xx_spi_setup_dma(espi))
1103 dev_warn(&pdev->
dev,
"DMA setup failed. Falling back to PIO\n");
1107 dev_err(&pdev->
dev,
"unable to create workqueue\n");
1115 ep93xx_spi_write_u8(espi,
SSPCR1, 0);
1119 dev_err(&pdev->
dev,
"failed to register SPI master\n");
1120 goto fail_free_queue;
1123 dev_info(&pdev->
dev,
"EP93xx SPI Controller at 0x%08lx irq %d\n",
1124 (
unsigned long)res->
start, irq);
1131 ep93xx_spi_release_dma(espi);
1134 fail_release_master:
1135 spi_master_put(master);
1136 platform_set_drvdata(pdev,
NULL);
1143 struct spi_master *master = platform_get_drvdata(pdev);
1144 struct ep93xx_spi *espi = spi_master_get_devdata(master);
1146 spin_lock_irq(&espi->
lock);
1148 spin_unlock_irq(&espi->
lock);
1155 spin_lock_irq(&espi->
lock);
1161 list_del_init(&msg->
queue);
1163 spin_unlock_irq(&espi->
lock);
1165 spin_lock_irq(&espi->
lock);
1167 spin_unlock_irq(&espi->
lock);
1169 ep93xx_spi_release_dma(espi);
1171 platform_set_drvdata(pdev,
NULL);
1179 .name =
"ep93xx-spi",
1182 .probe = ep93xx_spi_probe,