Wolfram Sang | 9135bac | 2018-08-22 00:02:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SH RSPI driver |
| 4 | * |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 5 | * Copyright (C) 2012, 2013 Renesas Solutions Corp. |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 6 | * Copyright (C) 2014 Glider bvba |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 7 | * |
| 8 | * Based on spi-sh.c: |
| 9 | * Copyright (C) 2011 Renesas Solutions Corp. |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/errno.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/clk.h> |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 20 | #include <linux/dmaengine.h> |
| 21 | #include <linux/dma-mapping.h> |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Geert Uytterhoeven | 490c977 | 2014-03-11 10:59:12 +0100 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 24 | #include <linux/sh_dma.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 25 | #include <linux/spi/spi.h> |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 26 | #include <linux/spi/rspi.h> |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 28 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 29 | #define RSPI_SPCR 0x00 /* Control Register */ |
| 30 | #define RSPI_SSLP 0x01 /* Slave Select Polarity Register */ |
| 31 | #define RSPI_SPPCR 0x02 /* Pin Control Register */ |
| 32 | #define RSPI_SPSR 0x03 /* Status Register */ |
| 33 | #define RSPI_SPDR 0x04 /* Data Register */ |
| 34 | #define RSPI_SPSCR 0x08 /* Sequence Control Register */ |
| 35 | #define RSPI_SPSSR 0x09 /* Sequence Status Register */ |
| 36 | #define RSPI_SPBR 0x0a /* Bit Rate Register */ |
| 37 | #define RSPI_SPDCR 0x0b /* Data Control Register */ |
| 38 | #define RSPI_SPCKD 0x0c /* Clock Delay Register */ |
| 39 | #define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */ |
| 40 | #define RSPI_SPND 0x0e /* Next-Access Delay Register */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 41 | #define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 42 | #define RSPI_SPCMD0 0x10 /* Command Register 0 */ |
| 43 | #define RSPI_SPCMD1 0x12 /* Command Register 1 */ |
| 44 | #define RSPI_SPCMD2 0x14 /* Command Register 2 */ |
| 45 | #define RSPI_SPCMD3 0x16 /* Command Register 3 */ |
| 46 | #define RSPI_SPCMD4 0x18 /* Command Register 4 */ |
| 47 | #define RSPI_SPCMD5 0x1a /* Command Register 5 */ |
| 48 | #define RSPI_SPCMD6 0x1c /* Command Register 6 */ |
| 49 | #define RSPI_SPCMD7 0x1e /* Command Register 7 */ |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 50 | #define RSPI_SPCMD(i) (RSPI_SPCMD0 + (i) * 2) |
| 51 | #define RSPI_NUM_SPCMD 8 |
| 52 | #define RSPI_RZ_NUM_SPCMD 4 |
| 53 | #define QSPI_NUM_SPCMD 4 |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 54 | |
| 55 | /* RSPI on RZ only */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 56 | #define RSPI_SPBFCR 0x20 /* Buffer Control Register */ |
| 57 | #define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 58 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 59 | /* QSPI only */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 60 | #define QSPI_SPBFCR 0x18 /* Buffer Control Register */ |
| 61 | #define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */ |
| 62 | #define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */ |
| 63 | #define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */ |
| 64 | #define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */ |
| 65 | #define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */ |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 66 | #define QSPI_SPBMUL(i) (QSPI_SPBMUL0 + (i) * 4) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 67 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 68 | /* SPCR - Control Register */ |
| 69 | #define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */ |
| 70 | #define SPCR_SPE 0x40 /* Function Enable */ |
| 71 | #define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */ |
| 72 | #define SPCR_SPEIE 0x10 /* Error Interrupt Enable */ |
| 73 | #define SPCR_MSTR 0x08 /* Master/Slave Mode Select */ |
| 74 | #define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */ |
| 75 | /* RSPI on SH only */ |
| 76 | #define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */ |
| 77 | #define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */ |
Geert Uytterhoeven | 6089af7 | 2014-08-28 10:10:19 +0200 | [diff] [blame] | 78 | /* QSPI on R-Car Gen2 only */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 79 | #define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */ |
| 80 | #define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 81 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 82 | /* SSLP - Slave Select Polarity Register */ |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 83 | #define SSLP_SSLP(i) BIT(i) /* SSLi Signal Polarity Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 84 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 85 | /* SPPCR - Pin Control Register */ |
| 86 | #define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */ |
| 87 | #define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 88 | #define SPPCR_SPOM 0x04 |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 89 | #define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */ |
| 90 | #define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 91 | |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 92 | #define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */ |
| 93 | #define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */ |
| 94 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 95 | /* SPSR - Status Register */ |
| 96 | #define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */ |
| 97 | #define SPSR_TEND 0x40 /* Transmit End */ |
| 98 | #define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */ |
| 99 | #define SPSR_PERF 0x08 /* Parity Error Flag */ |
| 100 | #define SPSR_MODF 0x04 /* Mode Fault Error Flag */ |
| 101 | #define SPSR_IDLNF 0x02 /* RSPI Idle Flag */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 102 | #define SPSR_OVRF 0x01 /* Overrun Error Flag (RSPI only) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 103 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 104 | /* SPSCR - Sequence Control Register */ |
| 105 | #define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 106 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 107 | /* SPSSR - Sequence Status Register */ |
| 108 | #define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */ |
| 109 | #define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 110 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 111 | /* SPDCR - Data Control Register */ |
| 112 | #define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */ |
| 113 | #define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */ |
| 114 | #define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */ |
| 115 | #define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0) |
| 116 | #define SPDCR_SPLWORD SPDCR_SPLW1 |
| 117 | #define SPDCR_SPLBYTE SPDCR_SPLW0 |
| 118 | #define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 119 | #define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 120 | #define SPDCR_SLSEL1 0x08 |
| 121 | #define SPDCR_SLSEL0 0x04 |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 122 | #define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 123 | #define SPDCR_SPFC1 0x02 |
| 124 | #define SPDCR_SPFC0 0x01 |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 125 | #define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 126 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 127 | /* SPCKD - Clock Delay Register */ |
| 128 | #define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 129 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 130 | /* SSLND - Slave Select Negation Delay Register */ |
| 131 | #define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 132 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 133 | /* SPND - Next-Access Delay Register */ |
| 134 | #define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 135 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 136 | /* SPCR2 - Control Register 2 */ |
| 137 | #define SPCR2_PTE 0x08 /* Parity Self-Test Enable */ |
| 138 | #define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */ |
| 139 | #define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */ |
| 140 | #define SPCR2_SPPE 0x01 /* Parity Enable */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 141 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 142 | /* SPCMDn - Command Registers */ |
| 143 | #define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */ |
| 144 | #define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */ |
| 145 | #define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */ |
| 146 | #define SPCMD_LSBF 0x1000 /* LSB First */ |
| 147 | #define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 148 | #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK) |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 149 | #define SPCMD_SPB_8BIT 0x0000 /* QSPI only */ |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 150 | #define SPCMD_SPB_16BIT 0x0100 |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 151 | #define SPCMD_SPB_20BIT 0x0000 |
| 152 | #define SPCMD_SPB_24BIT 0x0100 |
| 153 | #define SPCMD_SPB_32BIT 0x0200 |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 154 | #define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 155 | #define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */ |
| 156 | #define SPCMD_SPIMOD1 0x0040 |
| 157 | #define SPCMD_SPIMOD0 0x0020 |
| 158 | #define SPCMD_SPIMOD_SINGLE 0 |
| 159 | #define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0 |
| 160 | #define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1 |
| 161 | #define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */ |
Geert Uytterhoeven | 9815ed8 | 2020-01-02 14:38:21 +0100 | [diff] [blame] | 162 | #define SPCMD_SSLA(i) ((i) << 4) /* SSL Assert Signal Setting */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 163 | #define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */ |
Geert Uytterhoeven | 8dd7169 | 2020-08-19 14:58:59 +0200 | [diff] [blame^] | 164 | #define SPCMD_BRDV(brdv) ((brdv) << 2) |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 165 | #define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */ |
| 166 | #define SPCMD_CPHA 0x0001 /* Clock Phase Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 167 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 168 | /* SPBFCR - Buffer Control Register */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 169 | #define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */ |
| 170 | #define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 171 | #define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */ |
| 172 | #define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */ |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 173 | /* QSPI on R-Car Gen2 */ |
| 174 | #define SPBFCR_TXTRG_1B 0x00 /* 31 bytes (1 byte available) */ |
| 175 | #define SPBFCR_TXTRG_32B 0x30 /* 0 byte (32 bytes available) */ |
| 176 | #define SPBFCR_RXTRG_1B 0x00 /* 1 byte (31 bytes available) */ |
| 177 | #define SPBFCR_RXTRG_32B 0x07 /* 32 bytes (0 byte available) */ |
| 178 | |
| 179 | #define QSPI_BUFFER_SIZE 32u |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 180 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 181 | struct rspi_data { |
| 182 | void __iomem *addr; |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 183 | u32 speed_hz; |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 184 | struct spi_controller *ctlr; |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 185 | struct platform_device *pdev; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 186 | wait_queue_head_t wait; |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 187 | spinlock_t lock; /* Protects RMW-access to RSPI_SSLP */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 188 | struct clk *clk; |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 189 | u16 spcmd; |
Geert Uytterhoeven | 06a7a3c | 2014-01-24 09:44:00 +0100 | [diff] [blame] | 190 | u8 spsr; |
| 191 | u8 sppcr; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 192 | int rx_irq, tx_irq; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 193 | const struct spi_ops *ops; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 194 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 195 | unsigned dma_callbacked:1; |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 196 | unsigned byte_access:1; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 197 | }; |
| 198 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 199 | static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 200 | { |
| 201 | iowrite8(data, rspi->addr + offset); |
| 202 | } |
| 203 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 204 | static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 205 | { |
| 206 | iowrite16(data, rspi->addr + offset); |
| 207 | } |
| 208 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 209 | static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 210 | { |
| 211 | iowrite32(data, rspi->addr + offset); |
| 212 | } |
| 213 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 214 | static u8 rspi_read8(const struct rspi_data *rspi, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 215 | { |
| 216 | return ioread8(rspi->addr + offset); |
| 217 | } |
| 218 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 219 | static u16 rspi_read16(const struct rspi_data *rspi, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 220 | { |
| 221 | return ioread16(rspi->addr + offset); |
| 222 | } |
| 223 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 224 | static void rspi_write_data(const struct rspi_data *rspi, u16 data) |
| 225 | { |
| 226 | if (rspi->byte_access) |
| 227 | rspi_write8(rspi, data, RSPI_SPDR); |
| 228 | else /* 16 bit */ |
| 229 | rspi_write16(rspi, data, RSPI_SPDR); |
| 230 | } |
| 231 | |
| 232 | static u16 rspi_read_data(const struct rspi_data *rspi) |
| 233 | { |
| 234 | if (rspi->byte_access) |
| 235 | return rspi_read8(rspi, RSPI_SPDR); |
| 236 | else /* 16 bit */ |
| 237 | return rspi_read16(rspi, RSPI_SPDR); |
| 238 | } |
| 239 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 240 | /* optional functions */ |
| 241 | struct spi_ops { |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 242 | int (*set_config_register)(struct rspi_data *rspi, int access_size); |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 243 | int (*transfer_one)(struct spi_controller *ctlr, |
| 244 | struct spi_device *spi, struct spi_transfer *xfer); |
Geert Uytterhoeven | cd982e6 | 2020-02-18 11:58:08 +0100 | [diff] [blame] | 245 | u16 extra_mode_bits; |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 246 | u16 flags; |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 247 | u16 fifo_size; |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 248 | u8 num_hw_ss; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | /* |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 252 | * functions for RSPI on legacy SH |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 253 | */ |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 254 | static int rspi_set_config_register(struct rspi_data *rspi, int access_size) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 255 | { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 256 | int spbr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 257 | |
Geert Uytterhoeven | 06a7a3c | 2014-01-24 09:44:00 +0100 | [diff] [blame] | 258 | /* Sets output mode, MOSI signal, and (optionally) loopback */ |
| 259 | rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 260 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 261 | /* Sets transfer bit rate */ |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 262 | spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz) - 1; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 263 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
| 264 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 265 | /* Disable dummy transmission, set 16-bit word access, 1 frame */ |
| 266 | rspi_write8(rspi, 0, RSPI_SPDCR); |
| 267 | rspi->byte_access = 0; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 268 | |
| 269 | /* Sets RSPCK, SSL, next-access delay value */ |
| 270 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 271 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 272 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 273 | |
| 274 | /* Sets parity, interrupt mask */ |
| 275 | rspi_write8(rspi, 0x00, RSPI_SPCR2); |
| 276 | |
Geert Uytterhoeven | 26843bb | 2019-03-12 19:45:13 +0100 | [diff] [blame] | 277 | /* Resets sequencer */ |
| 278 | rspi_write8(rspi, 0, RSPI_SPSCR); |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 279 | rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size); |
| 280 | rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 281 | |
| 282 | /* Sets RSPI mode */ |
| 283 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); |
| 284 | |
| 285 | return 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 286 | } |
| 287 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 288 | /* |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 289 | * functions for RSPI on RZ |
| 290 | */ |
| 291 | static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size) |
| 292 | { |
| 293 | int spbr; |
Geert Uytterhoeven | 8dd7169 | 2020-08-19 14:58:59 +0200 | [diff] [blame^] | 294 | int brdv = 0; |
Chris Brandt | aeb8f8c | 2016-08-05 09:36:03 -0400 | [diff] [blame] | 295 | unsigned long clksrc; |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 296 | |
Geert Uytterhoeven | 06a7a3c | 2014-01-24 09:44:00 +0100 | [diff] [blame] | 297 | /* Sets output mode, MOSI signal, and (optionally) loopback */ |
| 298 | rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR); |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 299 | |
Chris Brandt | aeb8f8c | 2016-08-05 09:36:03 -0400 | [diff] [blame] | 300 | clksrc = clk_get_rate(rspi->clk); |
Geert Uytterhoeven | 8dd7169 | 2020-08-19 14:58:59 +0200 | [diff] [blame^] | 301 | while (brdv < 3) { |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 302 | if (rspi->speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */ |
Chris Brandt | aeb8f8c | 2016-08-05 09:36:03 -0400 | [diff] [blame] | 303 | break; |
Geert Uytterhoeven | 8dd7169 | 2020-08-19 14:58:59 +0200 | [diff] [blame^] | 304 | brdv++; |
Chris Brandt | aeb8f8c | 2016-08-05 09:36:03 -0400 | [diff] [blame] | 305 | clksrc /= 2; |
| 306 | } |
| 307 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 308 | /* Sets transfer bit rate */ |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 309 | spbr = DIV_ROUND_UP(clksrc, 2 * rspi->speed_hz) - 1; |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 310 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
Geert Uytterhoeven | 8dd7169 | 2020-08-19 14:58:59 +0200 | [diff] [blame^] | 311 | rspi->spcmd |= SPCMD_BRDV(brdv); |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 312 | |
| 313 | /* Disable dummy transmission, set byte access */ |
| 314 | rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR); |
| 315 | rspi->byte_access = 1; |
| 316 | |
| 317 | /* Sets RSPCK, SSL, next-access delay value */ |
| 318 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 319 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 320 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 321 | |
Geert Uytterhoeven | 26843bb | 2019-03-12 19:45:13 +0100 | [diff] [blame] | 322 | /* Resets sequencer */ |
| 323 | rspi_write8(rspi, 0, RSPI_SPSCR); |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 324 | rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size); |
| 325 | rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0); |
| 326 | |
| 327 | /* Sets RSPI mode */ |
| 328 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 334 | * functions for QSPI |
| 335 | */ |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 336 | static int qspi_set_config_register(struct rspi_data *rspi, int access_size) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 337 | { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 338 | int spbr; |
| 339 | |
Geert Uytterhoeven | 06a7a3c | 2014-01-24 09:44:00 +0100 | [diff] [blame] | 340 | /* Sets output mode, MOSI signal, and (optionally) loopback */ |
| 341 | rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 342 | |
| 343 | /* Sets transfer bit rate */ |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 344 | spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 345 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
| 346 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 347 | /* Disable dummy transmission, set byte access */ |
| 348 | rspi_write8(rspi, 0, RSPI_SPDCR); |
| 349 | rspi->byte_access = 1; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 350 | |
| 351 | /* Sets RSPCK, SSL, next-access delay value */ |
| 352 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 353 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 354 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 355 | |
| 356 | /* Data Length Setting */ |
| 357 | if (access_size == 8) |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 358 | rspi->spcmd |= SPCMD_SPB_8BIT; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 359 | else if (access_size == 16) |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 360 | rspi->spcmd |= SPCMD_SPB_16BIT; |
Laurent Pinchart | 8e1c809 | 2013-11-27 01:41:44 +0100 | [diff] [blame] | 361 | else |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 362 | rspi->spcmd |= SPCMD_SPB_32BIT; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 363 | |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 364 | rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 365 | |
| 366 | /* Resets transfer data length */ |
| 367 | rspi_write32(rspi, 0, QSPI_SPBMUL0); |
| 368 | |
| 369 | /* Resets transmit and receive buffer */ |
| 370 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); |
| 371 | /* Sets buffer to allow normal operation */ |
| 372 | rspi_write8(rspi, 0x00, QSPI_SPBFCR); |
| 373 | |
Geert Uytterhoeven | 26843bb | 2019-03-12 19:45:13 +0100 | [diff] [blame] | 374 | /* Resets sequencer */ |
| 375 | rspi_write8(rspi, 0, RSPI_SPSCR); |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 376 | rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 377 | |
Geert Uytterhoeven | b458a34 | 2017-12-07 11:09:21 +0100 | [diff] [blame] | 378 | /* Sets RSPI mode */ |
| 379 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 384 | static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg) |
| 385 | { |
| 386 | u8 data; |
| 387 | |
| 388 | data = rspi_read8(rspi, reg); |
| 389 | data &= ~mask; |
| 390 | data |= (val & mask); |
| 391 | rspi_write8(rspi, data, reg); |
| 392 | } |
| 393 | |
Geert Uytterhoeven | cb76b1c | 2015-06-23 15:04:29 +0200 | [diff] [blame] | 394 | static unsigned int qspi_set_send_trigger(struct rspi_data *rspi, |
| 395 | unsigned int len) |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 396 | { |
| 397 | unsigned int n; |
| 398 | |
| 399 | n = min(len, QSPI_BUFFER_SIZE); |
| 400 | |
| 401 | if (len >= QSPI_BUFFER_SIZE) { |
| 402 | /* sets triggering number to 32 bytes */ |
| 403 | qspi_update(rspi, SPBFCR_TXTRG_MASK, |
| 404 | SPBFCR_TXTRG_32B, QSPI_SPBFCR); |
| 405 | } else { |
| 406 | /* sets triggering number to 1 byte */ |
| 407 | qspi_update(rspi, SPBFCR_TXTRG_MASK, |
| 408 | SPBFCR_TXTRG_1B, QSPI_SPBFCR); |
| 409 | } |
| 410 | |
| 411 | return n; |
| 412 | } |
| 413 | |
Hiep Cao Minh | 3be09be | 2016-11-04 17:38:54 +0900 | [diff] [blame] | 414 | static int qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len) |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 415 | { |
| 416 | unsigned int n; |
| 417 | |
| 418 | n = min(len, QSPI_BUFFER_SIZE); |
| 419 | |
| 420 | if (len >= QSPI_BUFFER_SIZE) { |
| 421 | /* sets triggering number to 32 bytes */ |
| 422 | qspi_update(rspi, SPBFCR_RXTRG_MASK, |
| 423 | SPBFCR_RXTRG_32B, QSPI_SPBFCR); |
| 424 | } else { |
| 425 | /* sets triggering number to 1 byte */ |
| 426 | qspi_update(rspi, SPBFCR_RXTRG_MASK, |
| 427 | SPBFCR_RXTRG_1B, QSPI_SPBFCR); |
| 428 | } |
Hiep Cao Minh | 3be09be | 2016-11-04 17:38:54 +0900 | [diff] [blame] | 429 | return n; |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 430 | } |
| 431 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 432 | static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 433 | { |
| 434 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR); |
| 435 | } |
| 436 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 437 | static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 438 | { |
| 439 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR); |
| 440 | } |
| 441 | |
| 442 | static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask, |
| 443 | u8 enable_bit) |
| 444 | { |
| 445 | int ret; |
| 446 | |
| 447 | rspi->spsr = rspi_read8(rspi, RSPI_SPSR); |
Geert Uytterhoeven | 5dd1ad2 | 2014-02-04 11:06:24 +0100 | [diff] [blame] | 448 | if (rspi->spsr & wait_mask) |
| 449 | return 0; |
| 450 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 451 | rspi_enable_irq(rspi, enable_bit); |
| 452 | ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ); |
| 453 | if (ret == 0 && !(rspi->spsr & wait_mask)) |
| 454 | return -ETIMEDOUT; |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 459 | static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi) |
| 460 | { |
| 461 | return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
| 462 | } |
| 463 | |
| 464 | static inline int rspi_wait_for_rx_full(struct rspi_data *rspi) |
| 465 | { |
| 466 | return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE); |
| 467 | } |
| 468 | |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 469 | static int rspi_data_out(struct rspi_data *rspi, u8 data) |
| 470 | { |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 471 | int error = rspi_wait_for_tx_empty(rspi); |
| 472 | if (error < 0) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 473 | dev_err(&rspi->ctlr->dev, "transmit timeout\n"); |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 474 | return error; |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 475 | } |
| 476 | rspi_write_data(rspi, data); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int rspi_data_in(struct rspi_data *rspi) |
| 481 | { |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 482 | int error; |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 483 | u8 data; |
| 484 | |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 485 | error = rspi_wait_for_rx_full(rspi); |
| 486 | if (error < 0) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 487 | dev_err(&rspi->ctlr->dev, "receive timeout\n"); |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 488 | return error; |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 489 | } |
| 490 | data = rspi_read_data(rspi); |
| 491 | return data; |
| 492 | } |
| 493 | |
Geert Uytterhoeven | 6837b8e9 | 2014-06-02 15:38:07 +0200 | [diff] [blame] | 494 | static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx, |
| 495 | unsigned int n) |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 496 | { |
Geert Uytterhoeven | 6837b8e9 | 2014-06-02 15:38:07 +0200 | [diff] [blame] | 497 | while (n-- > 0) { |
| 498 | if (tx) { |
| 499 | int ret = rspi_data_out(rspi, *tx++); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | } |
| 503 | if (rx) { |
| 504 | int ret = rspi_data_in(rspi); |
| 505 | if (ret < 0) |
| 506 | return ret; |
| 507 | *rx++ = ret; |
| 508 | } |
| 509 | } |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 510 | |
Geert Uytterhoeven | 6837b8e9 | 2014-06-02 15:38:07 +0200 | [diff] [blame] | 511 | return 0; |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 514 | static void rspi_dma_complete(void *arg) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 515 | { |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 516 | struct rspi_data *rspi = arg; |
| 517 | |
| 518 | rspi->dma_callbacked = 1; |
| 519 | wake_up_interruptible(&rspi->wait); |
| 520 | } |
| 521 | |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 522 | static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx, |
| 523 | struct sg_table *rx) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 524 | { |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 525 | struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL; |
| 526 | u8 irq_mask = 0; |
| 527 | unsigned int other_irq = 0; |
| 528 | dma_cookie_t cookie; |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 529 | int ret; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 530 | |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 531 | /* First prepare and submit the DMA request(s), as this may fail */ |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 532 | if (rx) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 533 | desc_rx = dmaengine_prep_slave_sg(rspi->ctlr->dma_rx, rx->sgl, |
| 534 | rx->nents, DMA_DEV_TO_MEM, |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 535 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 536 | if (!desc_rx) { |
| 537 | ret = -EAGAIN; |
| 538 | goto no_dma_rx; |
| 539 | } |
| 540 | |
| 541 | desc_rx->callback = rspi_dma_complete; |
| 542 | desc_rx->callback_param = rspi; |
| 543 | cookie = dmaengine_submit(desc_rx); |
| 544 | if (dma_submit_error(cookie)) { |
| 545 | ret = cookie; |
| 546 | goto no_dma_rx; |
| 547 | } |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 548 | |
| 549 | irq_mask |= SPCR_SPRIE; |
| 550 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 551 | |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 552 | if (tx) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 553 | desc_tx = dmaengine_prep_slave_sg(rspi->ctlr->dma_tx, tx->sgl, |
| 554 | tx->nents, DMA_MEM_TO_DEV, |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 555 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 556 | if (!desc_tx) { |
| 557 | ret = -EAGAIN; |
| 558 | goto no_dma_tx; |
| 559 | } |
| 560 | |
| 561 | if (rx) { |
| 562 | /* No callback */ |
| 563 | desc_tx->callback = NULL; |
| 564 | } else { |
| 565 | desc_tx->callback = rspi_dma_complete; |
| 566 | desc_tx->callback_param = rspi; |
| 567 | } |
| 568 | cookie = dmaengine_submit(desc_tx); |
| 569 | if (dma_submit_error(cookie)) { |
| 570 | ret = cookie; |
| 571 | goto no_dma_tx; |
| 572 | } |
| 573 | |
| 574 | irq_mask |= SPCR_SPTIE; |
| 575 | } |
| 576 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 577 | /* |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 578 | * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 579 | * called. So, this driver disables the IRQ while DMA transfer. |
| 580 | */ |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 581 | if (tx) |
| 582 | disable_irq(other_irq = rspi->tx_irq); |
| 583 | if (rx && rspi->rx_irq != other_irq) |
| 584 | disable_irq(rspi->rx_irq); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 585 | |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 586 | rspi_enable_irq(rspi, irq_mask); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 587 | rspi->dma_callbacked = 0; |
| 588 | |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 589 | /* Now start DMA */ |
| 590 | if (rx) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 591 | dma_async_issue_pending(rspi->ctlr->dma_rx); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 592 | if (tx) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 593 | dma_async_issue_pending(rspi->ctlr->dma_tx); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 594 | |
| 595 | ret = wait_event_interruptible_timeout(rspi->wait, |
| 596 | rspi->dma_callbacked, HZ); |
Geert Uytterhoeven | 8dbbaa4 | 2018-09-05 10:49:39 +0200 | [diff] [blame] | 597 | if (ret > 0 && rspi->dma_callbacked) { |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 598 | ret = 0; |
Geert Uytterhoeven | 8dbbaa4 | 2018-09-05 10:49:39 +0200 | [diff] [blame] | 599 | } else { |
| 600 | if (!ret) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 601 | dev_err(&rspi->ctlr->dev, "DMA timeout\n"); |
Geert Uytterhoeven | 8dbbaa4 | 2018-09-05 10:49:39 +0200 | [diff] [blame] | 602 | ret = -ETIMEDOUT; |
| 603 | } |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 604 | if (tx) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 605 | dmaengine_terminate_all(rspi->ctlr->dma_tx); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 606 | if (rx) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 607 | dmaengine_terminate_all(rspi->ctlr->dma_rx); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 608 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 609 | |
Geert Uytterhoeven | c52fb6d | 2014-06-02 15:38:15 +0200 | [diff] [blame] | 610 | rspi_disable_irq(rspi, irq_mask); |
| 611 | |
| 612 | if (tx) |
| 613 | enable_irq(rspi->tx_irq); |
| 614 | if (rx && rspi->rx_irq != other_irq) |
| 615 | enable_irq(rspi->rx_irq); |
| 616 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 617 | return ret; |
Geert Uytterhoeven | 85912a8 | 2014-07-09 12:26:22 +0200 | [diff] [blame] | 618 | |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 619 | no_dma_tx: |
| 620 | if (rx) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 621 | dmaengine_terminate_all(rspi->ctlr->dma_rx); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 622 | no_dma_rx: |
| 623 | if (ret == -EAGAIN) { |
Geert Uytterhoeven | 1bec84d | 2020-01-02 14:38:19 +0100 | [diff] [blame] | 624 | dev_warn_once(&rspi->ctlr->dev, |
| 625 | "DMA not available, falling back to PIO\n"); |
Geert Uytterhoeven | 3819bc8 | 2014-08-06 14:58:58 +0200 | [diff] [blame] | 626 | } |
| 627 | return ret; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 628 | } |
| 629 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 630 | static void rspi_receive_init(const struct rspi_data *rspi) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 631 | { |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 632 | u8 spsr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 633 | |
| 634 | spsr = rspi_read8(rspi, RSPI_SPSR); |
| 635 | if (spsr & SPSR_SPRF) |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 636 | rspi_read_data(rspi); /* dummy read */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 637 | if (spsr & SPSR_OVRF) |
| 638 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF, |
Geert Uytterhoeven | df900e6 | 2013-12-23 19:34:24 +0100 | [diff] [blame] | 639 | RSPI_SPSR); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 640 | } |
| 641 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 642 | static void rspi_rz_receive_init(const struct rspi_data *rspi) |
| 643 | { |
| 644 | rspi_receive_init(rspi); |
| 645 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR); |
| 646 | rspi_write8(rspi, 0, RSPI_SPBFCR); |
| 647 | } |
| 648 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 649 | static void qspi_receive_init(const struct rspi_data *rspi) |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 650 | { |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 651 | u8 spsr; |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 652 | |
| 653 | spsr = rspi_read8(rspi, RSPI_SPSR); |
| 654 | if (spsr & SPSR_SPRF) |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 655 | rspi_read_data(rspi); /* dummy read */ |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 656 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 657 | rspi_write8(rspi, 0, QSPI_SPBFCR); |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 658 | } |
| 659 | |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 660 | static bool __rspi_can_dma(const struct rspi_data *rspi, |
| 661 | const struct spi_transfer *xfer) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 662 | { |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 663 | return xfer->len > rspi->ops->fifo_size; |
| 664 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 665 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 666 | static bool rspi_can_dma(struct spi_controller *ctlr, struct spi_device *spi, |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 667 | struct spi_transfer *xfer) |
| 668 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 669 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 670 | |
| 671 | return __rspi_can_dma(rspi, xfer); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 672 | } |
| 673 | |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 674 | static int rspi_dma_check_then_transfer(struct rspi_data *rspi, |
| 675 | struct spi_transfer *xfer) |
| 676 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 677 | if (!rspi->ctlr->can_dma || !__rspi_can_dma(rspi, xfer)) |
Hiep Cao Minh | 6310372 | 2015-04-30 11:12:12 +0900 | [diff] [blame] | 678 | return -EAGAIN; |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 679 | |
Hiep Cao Minh | 6310372 | 2015-04-30 11:12:12 +0900 | [diff] [blame] | 680 | /* rx_buf can be NULL on RSPI on SH in TX-only Mode */ |
| 681 | return rspi_dma_transfer(rspi, &xfer->tx_sg, |
| 682 | xfer->rx_buf ? &xfer->rx_sg : NULL); |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 683 | } |
| 684 | |
Geert Uytterhoeven | 8b983e9 | 2014-06-02 15:38:19 +0200 | [diff] [blame] | 685 | static int rspi_common_transfer(struct rspi_data *rspi, |
| 686 | struct spi_transfer *xfer) |
| 687 | { |
| 688 | int ret; |
| 689 | |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 690 | ret = rspi_dma_check_then_transfer(rspi, xfer); |
| 691 | if (ret != -EAGAIN) |
| 692 | return ret; |
Geert Uytterhoeven | 8b983e9 | 2014-06-02 15:38:19 +0200 | [diff] [blame] | 693 | |
| 694 | ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len); |
| 695 | if (ret < 0) |
| 696 | return ret; |
| 697 | |
| 698 | /* Wait for the last transmission */ |
| 699 | rspi_wait_for_tx_empty(rspi); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 704 | static int rspi_transfer_one(struct spi_controller *ctlr, |
| 705 | struct spi_device *spi, struct spi_transfer *xfer) |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 706 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 707 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 708 | u8 spcr; |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 709 | |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 710 | spcr = rspi_read8(rspi, RSPI_SPCR); |
Geert Uytterhoeven | 6837b8e9 | 2014-06-02 15:38:07 +0200 | [diff] [blame] | 711 | if (xfer->rx_buf) { |
Geert Uytterhoeven | 32c6426 | 2014-06-02 15:38:04 +0200 | [diff] [blame] | 712 | rspi_receive_init(rspi); |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 713 | spcr &= ~SPCR_TXMD; |
Geert Uytterhoeven | 32c6426 | 2014-06-02 15:38:04 +0200 | [diff] [blame] | 714 | } else { |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 715 | spcr |= SPCR_TXMD; |
Geert Uytterhoeven | 32c6426 | 2014-06-02 15:38:04 +0200 | [diff] [blame] | 716 | } |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 717 | rspi_write8(rspi, spcr, RSPI_SPCR); |
| 718 | |
Geert Uytterhoeven | 8b983e9 | 2014-06-02 15:38:19 +0200 | [diff] [blame] | 719 | return rspi_common_transfer(rspi, xfer); |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 722 | static int rspi_rz_transfer_one(struct spi_controller *ctlr, |
Geert Uytterhoeven | 03e627c | 2014-06-02 15:38:16 +0200 | [diff] [blame] | 723 | struct spi_device *spi, |
| 724 | struct spi_transfer *xfer) |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 725 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 726 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 727 | |
| 728 | rspi_rz_receive_init(rspi); |
| 729 | |
Geert Uytterhoeven | 8b983e9 | 2014-06-02 15:38:19 +0200 | [diff] [blame] | 730 | return rspi_common_transfer(rspi, xfer); |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame] | 731 | } |
| 732 | |
Hiep Cao Minh | a91bbe7 | 2015-05-22 18:59:36 +0900 | [diff] [blame] | 733 | static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx, |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 734 | u8 *rx, unsigned int len) |
| 735 | { |
Geert Uytterhoeven | cb76b1c | 2015-06-23 15:04:29 +0200 | [diff] [blame] | 736 | unsigned int i, n; |
| 737 | int ret; |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 738 | |
| 739 | while (len > 0) { |
| 740 | n = qspi_set_send_trigger(rspi, len); |
| 741 | qspi_set_receive_trigger(rspi, len); |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 742 | ret = rspi_wait_for_tx_empty(rspi); |
| 743 | if (ret < 0) { |
| 744 | dev_err(&rspi->ctlr->dev, "transmit timeout\n"); |
| 745 | return ret; |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 746 | } |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 747 | for (i = 0; i < n; i++) |
| 748 | rspi_write_data(rspi, *tx++); |
| 749 | |
| 750 | ret = rspi_wait_for_rx_full(rspi); |
| 751 | if (ret < 0) { |
| 752 | dev_err(&rspi->ctlr->dev, "receive timeout\n"); |
| 753 | return ret; |
| 754 | } |
| 755 | for (i = 0; i < n; i++) |
| 756 | *rx++ = rspi_read_data(rspi); |
| 757 | |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 758 | len -= n; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 764 | static int qspi_transfer_out_in(struct rspi_data *rspi, |
| 765 | struct spi_transfer *xfer) |
| 766 | { |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 767 | int ret; |
| 768 | |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 769 | qspi_receive_init(rspi); |
| 770 | |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 771 | ret = rspi_dma_check_then_transfer(rspi, xfer); |
| 772 | if (ret != -EAGAIN) |
| 773 | return ret; |
| 774 | |
Hiep Cao Minh | cc2e932 | 2015-05-22 18:59:37 +0900 | [diff] [blame] | 775 | return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf, |
Hiep Cao Minh | 4b6fe3e | 2014-10-23 12:14:13 +0900 | [diff] [blame] | 776 | xfer->rx_buf, xfer->len); |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 779 | static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer) |
| 780 | { |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 781 | const u8 *tx = xfer->tx_buf; |
| 782 | unsigned int n = xfer->len; |
| 783 | unsigned int i, len; |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 784 | int ret; |
| 785 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 786 | if (rspi->ctlr->can_dma && __rspi_can_dma(rspi, xfer)) { |
Geert Uytterhoeven | 85912a8 | 2014-07-09 12:26:22 +0200 | [diff] [blame] | 787 | ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL); |
| 788 | if (ret != -EAGAIN) |
| 789 | return ret; |
| 790 | } |
Geert Uytterhoeven | 4f12b5e | 2014-06-02 15:38:17 +0200 | [diff] [blame] | 791 | |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 792 | while (n > 0) { |
| 793 | len = qspi_set_send_trigger(rspi, n); |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 794 | ret = rspi_wait_for_tx_empty(rspi); |
| 795 | if (ret < 0) { |
| 796 | dev_err(&rspi->ctlr->dev, "transmit timeout\n"); |
| 797 | return ret; |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 798 | } |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 799 | for (i = 0; i < len; i++) |
| 800 | rspi_write_data(rspi, *tx++); |
| 801 | |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 802 | n -= len; |
| 803 | } |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 804 | |
| 805 | /* Wait for the last transmission */ |
Geert Uytterhoeven | 5f684c3 | 2014-06-02 15:38:03 +0200 | [diff] [blame] | 806 | rspi_wait_for_tx_empty(rspi); |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer) |
| 812 | { |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 813 | u8 *rx = xfer->rx_buf; |
| 814 | unsigned int n = xfer->len; |
| 815 | unsigned int i, len; |
| 816 | int ret; |
| 817 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 818 | if (rspi->ctlr->can_dma && __rspi_can_dma(rspi, xfer)) { |
Geert Uytterhoeven | 85912a8 | 2014-07-09 12:26:22 +0200 | [diff] [blame] | 819 | int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg); |
| 820 | if (ret != -EAGAIN) |
| 821 | return ret; |
| 822 | } |
Geert Uytterhoeven | 4f12b5e | 2014-06-02 15:38:17 +0200 | [diff] [blame] | 823 | |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 824 | while (n > 0) { |
| 825 | len = qspi_set_receive_trigger(rspi, n); |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 826 | ret = rspi_wait_for_rx_full(rspi); |
| 827 | if (ret < 0) { |
| 828 | dev_err(&rspi->ctlr->dev, "receive timeout\n"); |
| 829 | return ret; |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 830 | } |
Hoan Nguyen An | 7e95b16 | 2019-04-23 18:19:21 +0900 | [diff] [blame] | 831 | for (i = 0; i < len; i++) |
| 832 | *rx++ = rspi_read_data(rspi); |
| 833 | |
Arnd Bergmann | db30083 | 2016-11-08 14:46:12 +0100 | [diff] [blame] | 834 | n -= len; |
| 835 | } |
| 836 | |
| 837 | return 0; |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 840 | static int qspi_transfer_one(struct spi_controller *ctlr, |
| 841 | struct spi_device *spi, struct spi_transfer *xfer) |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 842 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 843 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 844 | |
Geert Uytterhoeven | ba824d4 | 2014-02-21 17:29:18 +0100 | [diff] [blame] | 845 | if (spi->mode & SPI_LOOP) { |
| 846 | return qspi_transfer_out_in(rspi, xfer); |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 847 | } else if (xfer->tx_nbits > SPI_NBITS_SINGLE) { |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 848 | /* Quad or Dual SPI Write */ |
| 849 | return qspi_transfer_out(rspi, xfer); |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 850 | } else if (xfer->rx_nbits > SPI_NBITS_SINGLE) { |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 851 | /* Quad or Dual SPI Read */ |
| 852 | return qspi_transfer_in(rspi, xfer); |
| 853 | } else { |
| 854 | /* Single SPI Transfer */ |
| 855 | return qspi_transfer_out_in(rspi, xfer); |
| 856 | } |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 859 | static u16 qspi_transfer_mode(const struct spi_transfer *xfer) |
| 860 | { |
| 861 | if (xfer->tx_buf) |
| 862 | switch (xfer->tx_nbits) { |
| 863 | case SPI_NBITS_QUAD: |
| 864 | return SPCMD_SPIMOD_QUAD; |
| 865 | case SPI_NBITS_DUAL: |
| 866 | return SPCMD_SPIMOD_DUAL; |
| 867 | default: |
| 868 | return 0; |
| 869 | } |
| 870 | if (xfer->rx_buf) |
| 871 | switch (xfer->rx_nbits) { |
| 872 | case SPI_NBITS_QUAD: |
| 873 | return SPCMD_SPIMOD_QUAD | SPCMD_SPRW; |
| 874 | case SPI_NBITS_DUAL: |
| 875 | return SPCMD_SPIMOD_DUAL | SPCMD_SPRW; |
| 876 | default: |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | static int qspi_setup_sequencer(struct rspi_data *rspi, |
| 884 | const struct spi_message *msg) |
| 885 | { |
| 886 | const struct spi_transfer *xfer; |
| 887 | unsigned int i = 0, len = 0; |
| 888 | u16 current_mode = 0xffff, mode; |
| 889 | |
| 890 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 891 | mode = qspi_transfer_mode(xfer); |
| 892 | if (mode == current_mode) { |
| 893 | len += xfer->len; |
| 894 | continue; |
| 895 | } |
| 896 | |
| 897 | /* Transfer mode change */ |
| 898 | if (i) { |
| 899 | /* Set transfer data length of previous transfer */ |
| 900 | rspi_write32(rspi, len, QSPI_SPBMUL(i - 1)); |
| 901 | } |
| 902 | |
| 903 | if (i >= QSPI_NUM_SPCMD) { |
| 904 | dev_err(&msg->spi->dev, |
| 905 | "Too many different transfer modes"); |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | /* Program transfer mode for this transfer */ |
| 910 | rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i)); |
| 911 | current_mode = mode; |
| 912 | len = xfer->len; |
| 913 | i++; |
| 914 | } |
| 915 | if (i) { |
| 916 | /* Set final transfer data length and sequence length */ |
| 917 | rspi_write32(rspi, len, QSPI_SPBMUL(i - 1)); |
| 918 | rspi_write8(rspi, i - 1, RSPI_SPSCR); |
| 919 | } |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 924 | static int rspi_setup(struct spi_device *spi) |
| 925 | { |
| 926 | struct rspi_data *rspi = spi_controller_get_devdata(spi->controller); |
| 927 | u8 sslp; |
| 928 | |
| 929 | if (spi->cs_gpiod) |
| 930 | return 0; |
| 931 | |
| 932 | pm_runtime_get_sync(&rspi->pdev->dev); |
| 933 | spin_lock_irq(&rspi->lock); |
| 934 | |
| 935 | sslp = rspi_read8(rspi, RSPI_SSLP); |
| 936 | if (spi->mode & SPI_CS_HIGH) |
| 937 | sslp |= SSLP_SSLP(spi->chip_select); |
| 938 | else |
| 939 | sslp &= ~SSLP_SSLP(spi->chip_select); |
| 940 | rspi_write8(rspi, sslp, RSPI_SSLP); |
| 941 | |
| 942 | spin_unlock_irq(&rspi->lock); |
| 943 | pm_runtime_put(&rspi->pdev->dev); |
| 944 | return 0; |
| 945 | } |
| 946 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 947 | static int rspi_prepare_message(struct spi_controller *ctlr, |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 948 | struct spi_message *msg) |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 949 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 950 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | 42bdaae | 2019-03-12 19:43:31 +0100 | [diff] [blame] | 951 | struct spi_device *spi = msg->spi; |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 952 | const struct spi_transfer *xfer; |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 953 | int ret; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 954 | |
Geert Uytterhoeven | e0fe700 | 2020-06-08 11:59:34 +0200 | [diff] [blame] | 955 | /* |
| 956 | * As the Bit Rate Register must not be changed while the device is |
| 957 | * active, all transfers in a message must use the same bit rate. |
| 958 | * In theory, the sequencer could be enabled, and each Command Register |
| 959 | * could divide the base bit rate by a different value. |
| 960 | * However, most RSPI variants do not have Transfer Data Length |
| 961 | * Multiplier Setting Registers, so each sequence step would be limited |
| 962 | * to a single word, making this feature unsuitable for large |
| 963 | * transfers, which would gain most from it. |
| 964 | */ |
| 965 | rspi->speed_hz = spi->max_speed_hz; |
| 966 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 967 | if (xfer->speed_hz < rspi->speed_hz) |
| 968 | rspi->speed_hz = xfer->speed_hz; |
| 969 | } |
Geert Uytterhoeven | 42bdaae | 2019-03-12 19:43:31 +0100 | [diff] [blame] | 970 | |
| 971 | rspi->spcmd = SPCMD_SSLKP; |
| 972 | if (spi->mode & SPI_CPOL) |
| 973 | rspi->spcmd |= SPCMD_CPOL; |
| 974 | if (spi->mode & SPI_CPHA) |
| 975 | rspi->spcmd |= SPCMD_CPHA; |
Geert Uytterhoeven | c046f8f | 2020-02-18 11:58:09 +0100 | [diff] [blame] | 976 | if (spi->mode & SPI_LSB_FIRST) |
| 977 | rspi->spcmd |= SPCMD_LSBF; |
Geert Uytterhoeven | 42bdaae | 2019-03-12 19:43:31 +0100 | [diff] [blame] | 978 | |
Geert Uytterhoeven | 9815ed8 | 2020-01-02 14:38:21 +0100 | [diff] [blame] | 979 | /* Configure slave signal to assert */ |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 980 | rspi->spcmd |= SPCMD_SSLA(spi->cs_gpiod ? rspi->ctlr->unused_native_cs |
| 981 | : spi->chip_select); |
Geert Uytterhoeven | 9815ed8 | 2020-01-02 14:38:21 +0100 | [diff] [blame] | 982 | |
Geert Uytterhoeven | 42bdaae | 2019-03-12 19:43:31 +0100 | [diff] [blame] | 983 | /* CMOS output mode and MOSI signal from previous transfer */ |
| 984 | rspi->sppcr = 0; |
| 985 | if (spi->mode & SPI_LOOP) |
| 986 | rspi->sppcr |= SPPCR_SPLP; |
| 987 | |
Geert Uytterhoeven | 8f2344f | 2020-01-02 14:38:20 +0100 | [diff] [blame] | 988 | rspi->ops->set_config_register(rspi, 8); |
Geert Uytterhoeven | 42bdaae | 2019-03-12 19:43:31 +0100 | [diff] [blame] | 989 | |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 990 | if (msg->spi->mode & |
| 991 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) { |
| 992 | /* Setup sequencer for messages with multiple transfer modes */ |
| 993 | ret = qspi_setup_sequencer(rspi, msg); |
| 994 | if (ret < 0) |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | /* Enable SPI function in master mode */ |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 999 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1003 | static int rspi_unprepare_message(struct spi_controller *ctlr, |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 1004 | struct spi_message *msg) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1005 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1006 | struct rspi_data *rspi = spi_controller_get_devdata(ctlr); |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 1007 | |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 1008 | /* Disable SPI function */ |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 1009 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR); |
Geert Uytterhoeven | 880c6d1 | 2014-01-30 09:43:50 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* Reset sequencer for Single SPI Transfers */ |
| 1012 | rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0); |
| 1013 | rspi_write8(rspi, 0, RSPI_SPSCR); |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 1014 | return 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1015 | } |
| 1016 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1017 | static irqreturn_t rspi_irq_mux(int irq, void *_sr) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1018 | { |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 1019 | struct rspi_data *rspi = _sr; |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 1020 | u8 spsr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1021 | irqreturn_t ret = IRQ_NONE; |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 1022 | u8 disable_irq = 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1023 | |
| 1024 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 1025 | if (spsr & SPSR_SPRF) |
| 1026 | disable_irq |= SPCR_SPRIE; |
| 1027 | if (spsr & SPSR_SPTEF) |
| 1028 | disable_irq |= SPCR_SPTIE; |
| 1029 | |
| 1030 | if (disable_irq) { |
| 1031 | ret = IRQ_HANDLED; |
| 1032 | rspi_disable_irq(rspi, disable_irq); |
| 1033 | wake_up(&rspi->wait); |
| 1034 | } |
| 1035 | |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1039 | static irqreturn_t rspi_irq_rx(int irq, void *_sr) |
| 1040 | { |
| 1041 | struct rspi_data *rspi = _sr; |
| 1042 | u8 spsr; |
| 1043 | |
| 1044 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 1045 | if (spsr & SPSR_SPRF) { |
| 1046 | rspi_disable_irq(rspi, SPCR_SPRIE); |
| 1047 | wake_up(&rspi->wait); |
| 1048 | return IRQ_HANDLED; |
| 1049 | } |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static irqreturn_t rspi_irq_tx(int irq, void *_sr) |
| 1055 | { |
| 1056 | struct rspi_data *rspi = _sr; |
| 1057 | u8 spsr; |
| 1058 | |
| 1059 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 1060 | if (spsr & SPSR_SPTEF) { |
| 1061 | rspi_disable_irq(rspi, SPCR_SPTIE); |
| 1062 | wake_up(&rspi->wait); |
| 1063 | return IRQ_HANDLED; |
| 1064 | } |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1069 | static struct dma_chan *rspi_request_dma_chan(struct device *dev, |
| 1070 | enum dma_transfer_direction dir, |
| 1071 | unsigned int id, |
| 1072 | dma_addr_t port_addr) |
| 1073 | { |
| 1074 | dma_cap_mask_t mask; |
| 1075 | struct dma_chan *chan; |
| 1076 | struct dma_slave_config cfg; |
| 1077 | int ret; |
| 1078 | |
| 1079 | dma_cap_zero(mask); |
| 1080 | dma_cap_set(DMA_SLAVE, mask); |
| 1081 | |
Geert Uytterhoeven | e825b8d | 2014-08-06 14:59:02 +0200 | [diff] [blame] | 1082 | chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
| 1083 | (void *)(unsigned long)id, dev, |
| 1084 | dir == DMA_MEM_TO_DEV ? "tx" : "rx"); |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1085 | if (!chan) { |
Geert Uytterhoeven | e825b8d | 2014-08-06 14:59:02 +0200 | [diff] [blame] | 1086 | dev_warn(dev, "dma_request_slave_channel_compat failed\n"); |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | memset(&cfg, 0, sizeof(cfg)); |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1091 | cfg.direction = dir; |
Geert Uytterhoeven | a30b95a7 | 2014-08-06 14:59:01 +0200 | [diff] [blame] | 1092 | if (dir == DMA_MEM_TO_DEV) { |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1093 | cfg.dst_addr = port_addr; |
Geert Uytterhoeven | a30b95a7 | 2014-08-06 14:59:01 +0200 | [diff] [blame] | 1094 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1095 | } else { |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1096 | cfg.src_addr = port_addr; |
Geert Uytterhoeven | a30b95a7 | 2014-08-06 14:59:01 +0200 | [diff] [blame] | 1097 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1098 | } |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1099 | |
| 1100 | ret = dmaengine_slave_config(chan, &cfg); |
| 1101 | if (ret) { |
| 1102 | dev_warn(dev, "dmaengine_slave_config failed %d\n", ret); |
| 1103 | dma_release_channel(chan); |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | |
| 1107 | return chan; |
| 1108 | } |
| 1109 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1110 | static int rspi_request_dma(struct device *dev, struct spi_controller *ctlr, |
Geert Uytterhoeven | fcdc49a | 2014-06-02 15:38:10 +0200 | [diff] [blame] | 1111 | const struct resource *res) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1112 | { |
Geert Uytterhoeven | fcdc49a | 2014-06-02 15:38:10 +0200 | [diff] [blame] | 1113 | const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev); |
Geert Uytterhoeven | e825b8d | 2014-08-06 14:59:02 +0200 | [diff] [blame] | 1114 | unsigned int dma_tx_id, dma_rx_id; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1115 | |
Geert Uytterhoeven | e825b8d | 2014-08-06 14:59:02 +0200 | [diff] [blame] | 1116 | if (dev->of_node) { |
| 1117 | /* In the OF case we will get the slave IDs from the DT */ |
| 1118 | dma_tx_id = 0; |
| 1119 | dma_rx_id = 0; |
| 1120 | } else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) { |
| 1121 | dma_tx_id = rspi_pd->dma_tx_id; |
| 1122 | dma_rx_id = rspi_pd->dma_rx_id; |
| 1123 | } else { |
| 1124 | /* The driver assumes no error. */ |
| 1125 | return 0; |
| 1126 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1127 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1128 | ctlr->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id, |
| 1129 | res->start + RSPI_SPDR); |
| 1130 | if (!ctlr->dma_tx) |
Geert Uytterhoeven | 5f338d0 | 2014-06-02 15:38:11 +0200 | [diff] [blame] | 1131 | return -ENODEV; |
Geert Uytterhoeven | 65bf220 | 2014-06-02 15:38:09 +0200 | [diff] [blame] | 1132 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1133 | ctlr->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id, |
| 1134 | res->start + RSPI_SPDR); |
| 1135 | if (!ctlr->dma_rx) { |
| 1136 | dma_release_channel(ctlr->dma_tx); |
| 1137 | ctlr->dma_tx = NULL; |
Geert Uytterhoeven | 5f338d0 | 2014-06-02 15:38:11 +0200 | [diff] [blame] | 1138 | return -ENODEV; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1139 | } |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 1140 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1141 | ctlr->can_dma = rspi_can_dma; |
Geert Uytterhoeven | 5f338d0 | 2014-06-02 15:38:11 +0200 | [diff] [blame] | 1142 | dev_info(dev, "DMA available"); |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 1143 | return 0; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1144 | } |
| 1145 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1146 | static void rspi_release_dma(struct spi_controller *ctlr) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1147 | { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1148 | if (ctlr->dma_tx) |
| 1149 | dma_release_channel(ctlr->dma_tx); |
| 1150 | if (ctlr->dma_rx) |
| 1151 | dma_release_channel(ctlr->dma_rx); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1152 | } |
| 1153 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1154 | static int rspi_remove(struct platform_device *pdev) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1155 | { |
Laurent Pinchart | 5ffbe2d | 2013-11-27 01:41:45 +0100 | [diff] [blame] | 1156 | struct rspi_data *rspi = platform_get_drvdata(pdev); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1157 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1158 | rspi_release_dma(rspi->ctlr); |
Geert Uytterhoeven | 490c977 | 2014-03-11 10:59:12 +0100 | [diff] [blame] | 1159 | pm_runtime_disable(&pdev->dev); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1164 | static const struct spi_ops rspi_ops = { |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 1165 | .set_config_register = rspi_set_config_register, |
| 1166 | .transfer_one = rspi_transfer_one, |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1167 | .flags = SPI_CONTROLLER_MUST_TX, |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 1168 | .fifo_size = 8, |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 1169 | .num_hw_ss = 2, |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1170 | }; |
| 1171 | |
| 1172 | static const struct spi_ops rspi_rz_ops = { |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 1173 | .set_config_register = rspi_rz_set_config_register, |
| 1174 | .transfer_one = rspi_rz_transfer_one, |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1175 | .flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX, |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 1176 | .fifo_size = 8, /* 8 for TX, 32 for RX */ |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 1177 | .num_hw_ss = 1, |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1178 | }; |
| 1179 | |
| 1180 | static const struct spi_ops qspi_ops = { |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 1181 | .set_config_register = qspi_set_config_register, |
| 1182 | .transfer_one = qspi_transfer_one, |
Geert Uytterhoeven | cd982e6 | 2020-02-18 11:58:08 +0100 | [diff] [blame] | 1183 | .extra_mode_bits = SPI_TX_DUAL | SPI_TX_QUAD | |
Geert Uytterhoeven | b42e035 | 2014-06-02 15:38:06 +0200 | [diff] [blame] | 1184 | SPI_RX_DUAL | SPI_RX_QUAD, |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1185 | .flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX, |
Geert Uytterhoeven | 2f777ec | 2014-06-02 15:38:12 +0200 | [diff] [blame] | 1186 | .fifo_size = 32, |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 1187 | .num_hw_ss = 1, |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1188 | }; |
| 1189 | |
| 1190 | #ifdef CONFIG_OF |
| 1191 | static const struct of_device_id rspi_of_match[] = { |
| 1192 | /* RSPI on legacy SH */ |
| 1193 | { .compatible = "renesas,rspi", .data = &rspi_ops }, |
| 1194 | /* RSPI on RZ/A1H */ |
| 1195 | { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops }, |
| 1196 | /* QSPI on R-Car Gen2 */ |
| 1197 | { .compatible = "renesas,qspi", .data = &qspi_ops }, |
| 1198 | { /* sentinel */ } |
| 1199 | }; |
| 1200 | |
| 1201 | MODULE_DEVICE_TABLE(of, rspi_of_match); |
| 1202 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1203 | static int rspi_parse_dt(struct device *dev, struct spi_controller *ctlr) |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1204 | { |
| 1205 | u32 num_cs; |
| 1206 | int error; |
| 1207 | |
| 1208 | /* Parse DT properties */ |
| 1209 | error = of_property_read_u32(dev->of_node, "num-cs", &num_cs); |
| 1210 | if (error) { |
| 1211 | dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error); |
| 1212 | return error; |
| 1213 | } |
| 1214 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1215 | ctlr->num_chipselect = num_cs; |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1216 | return 0; |
| 1217 | } |
| 1218 | #else |
Shimoda, Yoshihiro | 64b67de | 2014-02-03 10:43:46 +0900 | [diff] [blame] | 1219 | #define rspi_of_match NULL |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1220 | static inline int rspi_parse_dt(struct device *dev, struct spi_controller *ctlr) |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1221 | { |
| 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | #endif /* CONFIG_OF */ |
| 1225 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1226 | static int rspi_request_irq(struct device *dev, unsigned int irq, |
| 1227 | irq_handler_t handler, const char *suffix, |
| 1228 | void *dev_id) |
| 1229 | { |
Geert Uytterhoeven | 4393745 | 2014-08-06 14:59:00 +0200 | [diff] [blame] | 1230 | const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", |
| 1231 | dev_name(dev), suffix); |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1232 | if (!name) |
| 1233 | return -ENOMEM; |
Geert Uytterhoeven | 4393745 | 2014-08-06 14:59:00 +0200 | [diff] [blame] | 1234 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1235 | return devm_request_irq(dev, irq, handler, 0, name, dev_id); |
| 1236 | } |
| 1237 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1238 | static int rspi_probe(struct platform_device *pdev) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1239 | { |
| 1240 | struct resource *res; |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1241 | struct spi_controller *ctlr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1242 | struct rspi_data *rspi; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1243 | int ret; |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1244 | const struct rspi_plat_data *rspi_pd; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1245 | const struct spi_ops *ops; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1246 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1247 | ctlr = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data)); |
| 1248 | if (ctlr == NULL) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1249 | return -ENOMEM; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1250 | |
Geert Uytterhoeven | 219a7bc | 2017-10-04 14:19:53 +0200 | [diff] [blame] | 1251 | ops = of_device_get_match_data(&pdev->dev); |
| 1252 | if (ops) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1253 | ret = rspi_parse_dt(&pdev->dev, ctlr); |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1254 | if (ret) |
| 1255 | goto error1; |
| 1256 | } else { |
| 1257 | ops = (struct spi_ops *)pdev->id_entry->driver_data; |
| 1258 | rspi_pd = dev_get_platdata(&pdev->dev); |
| 1259 | if (rspi_pd && rspi_pd->num_chipselect) |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1260 | ctlr->num_chipselect = rspi_pd->num_chipselect; |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1261 | else |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1262 | ctlr->num_chipselect = 2; /* default */ |
Geert Uytterhoeven | d64b472 | 2014-08-06 14:58:59 +0200 | [diff] [blame] | 1263 | } |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1264 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1265 | rspi = spi_controller_get_devdata(ctlr); |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 1266 | platform_set_drvdata(pdev, rspi); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1267 | rspi->ops = ops; |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1268 | rspi->ctlr = ctlr; |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1269 | |
| 1270 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1271 | rspi->addr = devm_ioremap_resource(&pdev->dev, res); |
| 1272 | if (IS_ERR(rspi->addr)) { |
| 1273 | ret = PTR_ERR(rspi->addr); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1274 | goto error1; |
| 1275 | } |
| 1276 | |
Geert Uytterhoeven | 29f397b | 2014-01-24 09:44:02 +0100 | [diff] [blame] | 1277 | rspi->clk = devm_clk_get(&pdev->dev, NULL); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1278 | if (IS_ERR(rspi->clk)) { |
| 1279 | dev_err(&pdev->dev, "cannot get clock\n"); |
| 1280 | ret = PTR_ERR(rspi->clk); |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1281 | goto error1; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1282 | } |
Geert Uytterhoeven | 17fe0d9 | 2014-01-24 09:44:01 +0100 | [diff] [blame] | 1283 | |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 1284 | rspi->pdev = pdev; |
Geert Uytterhoeven | 490c977 | 2014-03-11 10:59:12 +0100 | [diff] [blame] | 1285 | pm_runtime_enable(&pdev->dev); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1286 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1287 | init_waitqueue_head(&rspi->wait); |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 1288 | spin_lock_init(&rspi->lock); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1289 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1290 | ctlr->bus_num = pdev->id; |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 1291 | ctlr->setup = rspi_setup; |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1292 | ctlr->auto_runtime_pm = true; |
| 1293 | ctlr->transfer_one = ops->transfer_one; |
| 1294 | ctlr->prepare_message = rspi_prepare_message; |
| 1295 | ctlr->unprepare_message = rspi_unprepare_message; |
Geert Uytterhoeven | f3a14a3 | 2020-03-09 18:15:37 +0100 | [diff] [blame] | 1296 | ctlr->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST | |
| 1297 | SPI_LOOP | ops->extra_mode_bits; |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1298 | ctlr->flags = ops->flags; |
| 1299 | ctlr->dev.of_node = pdev->dev.of_node; |
Geert Uytterhoeven | 144d8f9 | 2020-01-02 14:38:22 +0100 | [diff] [blame] | 1300 | ctlr->use_gpio_descriptors = true; |
| 1301 | ctlr->max_native_cs = rspi->ops->num_hw_ss; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1302 | |
Geert Uytterhoeven | 2de860b | 2019-10-16 16:31:01 +0200 | [diff] [blame] | 1303 | ret = platform_get_irq_byname_optional(pdev, "rx"); |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1304 | if (ret < 0) { |
Geert Uytterhoeven | 2de860b | 2019-10-16 16:31:01 +0200 | [diff] [blame] | 1305 | ret = platform_get_irq_byname_optional(pdev, "mux"); |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1306 | if (ret < 0) |
| 1307 | ret = platform_get_irq(pdev, 0); |
| 1308 | if (ret >= 0) |
| 1309 | rspi->rx_irq = rspi->tx_irq = ret; |
| 1310 | } else { |
| 1311 | rspi->rx_irq = ret; |
| 1312 | ret = platform_get_irq_byname(pdev, "tx"); |
| 1313 | if (ret >= 0) |
| 1314 | rspi->tx_irq = ret; |
| 1315 | } |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1316 | |
| 1317 | if (rspi->rx_irq == rspi->tx_irq) { |
| 1318 | /* Single multiplexed interrupt */ |
| 1319 | ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux, |
| 1320 | "mux", rspi); |
| 1321 | } else { |
| 1322 | /* Multi-interrupt mode, only SPRI and SPTI are used */ |
| 1323 | ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx, |
| 1324 | "rx", rspi); |
| 1325 | if (!ret) |
| 1326 | ret = rspi_request_irq(&pdev->dev, rspi->tx_irq, |
| 1327 | rspi_irq_tx, "tx", rspi); |
| 1328 | } |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1329 | if (ret < 0) { |
| 1330 | dev_err(&pdev->dev, "request_irq error\n"); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1331 | goto error2; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1332 | } |
| 1333 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1334 | ret = rspi_request_dma(&pdev->dev, ctlr, res); |
Geert Uytterhoeven | 27e105a | 2014-06-02 15:38:08 +0200 | [diff] [blame] | 1335 | if (ret < 0) |
| 1336 | dev_warn(&pdev->dev, "DMA not available, using PIO\n"); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1337 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1338 | ret = devm_spi_register_controller(&pdev->dev, ctlr); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1339 | if (ret < 0) { |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1340 | dev_err(&pdev->dev, "devm_spi_register_controller error.\n"); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1341 | goto error3; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | dev_info(&pdev->dev, "probed\n"); |
| 1345 | |
| 1346 | return 0; |
| 1347 | |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1348 | error3: |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1349 | rspi_release_dma(ctlr); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1350 | error2: |
Geert Uytterhoeven | 490c977 | 2014-03-11 10:59:12 +0100 | [diff] [blame] | 1351 | pm_runtime_disable(&pdev->dev); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1352 | error1: |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1353 | spi_controller_put(ctlr); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1354 | |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
Krzysztof Kozlowski | 8634daf | 2015-05-02 00:44:05 +0900 | [diff] [blame] | 1358 | static const struct platform_device_id spi_driver_ids[] = { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1359 | { "rspi", (kernel_ulong_t)&rspi_ops }, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1360 | {}, |
| 1361 | }; |
| 1362 | |
| 1363 | MODULE_DEVICE_TABLE(platform, spi_driver_ids); |
| 1364 | |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1365 | #ifdef CONFIG_PM_SLEEP |
| 1366 | static int rspi_suspend(struct device *dev) |
| 1367 | { |
Wolfram Sang | be0bf62 | 2018-10-21 22:00:45 +0200 | [diff] [blame] | 1368 | struct rspi_data *rspi = dev_get_drvdata(dev); |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1369 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1370 | return spi_controller_suspend(rspi->ctlr); |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | static int rspi_resume(struct device *dev) |
| 1374 | { |
Wolfram Sang | be0bf62 | 2018-10-21 22:00:45 +0200 | [diff] [blame] | 1375 | struct rspi_data *rspi = dev_get_drvdata(dev); |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1376 | |
Geert Uytterhoeven | 9428a07 | 2019-02-08 10:09:07 +0100 | [diff] [blame] | 1377 | return spi_controller_resume(rspi->ctlr); |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume); |
| 1381 | #define DEV_PM_OPS &rspi_pm_ops |
| 1382 | #else |
| 1383 | #define DEV_PM_OPS NULL |
| 1384 | #endif /* CONFIG_PM_SLEEP */ |
| 1385 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1386 | static struct platform_driver rspi_driver = { |
| 1387 | .probe = rspi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1388 | .remove = rspi_remove, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1389 | .id_table = spi_driver_ids, |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1390 | .driver = { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1391 | .name = "renesas_spi", |
Geert Uytterhoeven | c1ca59c | 2018-09-05 10:49:38 +0200 | [diff] [blame] | 1392 | .pm = DEV_PM_OPS, |
Geert Uytterhoeven | 426ef76 | 2014-01-28 10:21:38 +0100 | [diff] [blame] | 1393 | .of_match_table = of_match_ptr(rspi_of_match), |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1394 | }, |
| 1395 | }; |
| 1396 | module_platform_driver(rspi_driver); |
| 1397 | |
| 1398 | MODULE_DESCRIPTION("Renesas RSPI bus driver"); |
| 1399 | MODULE_LICENSE("GPL v2"); |
| 1400 | MODULE_AUTHOR("Yoshihiro Shimoda"); |
| 1401 | MODULE_ALIAS("platform:rspi"); |