Cezary Gapinski | d57a984 | 2018-12-24 23:00:27 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // STMicroelectronics STM32 SPI Controller driver (master mode only) |
| 4 | // |
| 5 | // Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 6 | // Author(s): Amelie Delaunay <amelie.delaunay@st.com> for STMicroelectronics. |
| 7 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/dmaengine.h> |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/iopoll.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_platform.h> |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 17 | #include <linux/reset.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | |
| 20 | #define DRIVER_NAME "spi_stm32" |
| 21 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 22 | /* STM32F4 SPI registers */ |
| 23 | #define STM32F4_SPI_CR1 0x00 |
| 24 | #define STM32F4_SPI_CR2 0x04 |
| 25 | #define STM32F4_SPI_SR 0x08 |
| 26 | #define STM32F4_SPI_DR 0x0C |
| 27 | #define STM32F4_SPI_I2SCFGR 0x1C |
| 28 | |
| 29 | /* STM32F4_SPI_CR1 bit fields */ |
| 30 | #define STM32F4_SPI_CR1_CPHA BIT(0) |
| 31 | #define STM32F4_SPI_CR1_CPOL BIT(1) |
| 32 | #define STM32F4_SPI_CR1_MSTR BIT(2) |
| 33 | #define STM32F4_SPI_CR1_BR_SHIFT 3 |
| 34 | #define STM32F4_SPI_CR1_BR GENMASK(5, 3) |
| 35 | #define STM32F4_SPI_CR1_SPE BIT(6) |
| 36 | #define STM32F4_SPI_CR1_LSBFRST BIT(7) |
| 37 | #define STM32F4_SPI_CR1_SSI BIT(8) |
| 38 | #define STM32F4_SPI_CR1_SSM BIT(9) |
| 39 | #define STM32F4_SPI_CR1_RXONLY BIT(10) |
| 40 | #define STM32F4_SPI_CR1_DFF BIT(11) |
| 41 | #define STM32F4_SPI_CR1_CRCNEXT BIT(12) |
| 42 | #define STM32F4_SPI_CR1_CRCEN BIT(13) |
| 43 | #define STM32F4_SPI_CR1_BIDIOE BIT(14) |
| 44 | #define STM32F4_SPI_CR1_BIDIMODE BIT(15) |
| 45 | #define STM32F4_SPI_CR1_BR_MIN 0 |
| 46 | #define STM32F4_SPI_CR1_BR_MAX (GENMASK(5, 3) >> 3) |
| 47 | |
| 48 | /* STM32F4_SPI_CR2 bit fields */ |
| 49 | #define STM32F4_SPI_CR2_RXDMAEN BIT(0) |
| 50 | #define STM32F4_SPI_CR2_TXDMAEN BIT(1) |
| 51 | #define STM32F4_SPI_CR2_SSOE BIT(2) |
| 52 | #define STM32F4_SPI_CR2_FRF BIT(4) |
| 53 | #define STM32F4_SPI_CR2_ERRIE BIT(5) |
| 54 | #define STM32F4_SPI_CR2_RXNEIE BIT(6) |
| 55 | #define STM32F4_SPI_CR2_TXEIE BIT(7) |
| 56 | |
| 57 | /* STM32F4_SPI_SR bit fields */ |
| 58 | #define STM32F4_SPI_SR_RXNE BIT(0) |
| 59 | #define STM32F4_SPI_SR_TXE BIT(1) |
| 60 | #define STM32F4_SPI_SR_CHSIDE BIT(2) |
| 61 | #define STM32F4_SPI_SR_UDR BIT(3) |
| 62 | #define STM32F4_SPI_SR_CRCERR BIT(4) |
| 63 | #define STM32F4_SPI_SR_MODF BIT(5) |
| 64 | #define STM32F4_SPI_SR_OVR BIT(6) |
| 65 | #define STM32F4_SPI_SR_BSY BIT(7) |
| 66 | #define STM32F4_SPI_SR_FRE BIT(8) |
| 67 | |
| 68 | /* STM32F4_SPI_I2SCFGR bit fields */ |
| 69 | #define STM32F4_SPI_I2SCFGR_I2SMOD BIT(11) |
| 70 | |
| 71 | /* STM32F4 SPI Baud Rate min/max divisor */ |
| 72 | #define STM32F4_SPI_BR_DIV_MIN (2 << STM32F4_SPI_CR1_BR_MIN) |
| 73 | #define STM32F4_SPI_BR_DIV_MAX (2 << STM32F4_SPI_CR1_BR_MAX) |
| 74 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 75 | /* STM32H7 SPI registers */ |
| 76 | #define STM32H7_SPI_CR1 0x00 |
| 77 | #define STM32H7_SPI_CR2 0x04 |
| 78 | #define STM32H7_SPI_CFG1 0x08 |
| 79 | #define STM32H7_SPI_CFG2 0x0C |
| 80 | #define STM32H7_SPI_IER 0x10 |
| 81 | #define STM32H7_SPI_SR 0x14 |
| 82 | #define STM32H7_SPI_IFCR 0x18 |
| 83 | #define STM32H7_SPI_TXDR 0x20 |
| 84 | #define STM32H7_SPI_RXDR 0x30 |
| 85 | #define STM32H7_SPI_I2SCFGR 0x50 |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 86 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 87 | /* STM32H7_SPI_CR1 bit fields */ |
| 88 | #define STM32H7_SPI_CR1_SPE BIT(0) |
| 89 | #define STM32H7_SPI_CR1_MASRX BIT(8) |
| 90 | #define STM32H7_SPI_CR1_CSTART BIT(9) |
| 91 | #define STM32H7_SPI_CR1_CSUSP BIT(10) |
| 92 | #define STM32H7_SPI_CR1_HDDIR BIT(11) |
| 93 | #define STM32H7_SPI_CR1_SSI BIT(12) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 94 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 95 | /* STM32H7_SPI_CR2 bit fields */ |
| 96 | #define STM32H7_SPI_CR2_TSIZE_SHIFT 0 |
| 97 | #define STM32H7_SPI_CR2_TSIZE GENMASK(15, 0) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 98 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 99 | /* STM32H7_SPI_CFG1 bit fields */ |
| 100 | #define STM32H7_SPI_CFG1_DSIZE_SHIFT 0 |
| 101 | #define STM32H7_SPI_CFG1_DSIZE GENMASK(4, 0) |
| 102 | #define STM32H7_SPI_CFG1_FTHLV_SHIFT 5 |
| 103 | #define STM32H7_SPI_CFG1_FTHLV GENMASK(8, 5) |
| 104 | #define STM32H7_SPI_CFG1_RXDMAEN BIT(14) |
| 105 | #define STM32H7_SPI_CFG1_TXDMAEN BIT(15) |
| 106 | #define STM32H7_SPI_CFG1_MBR_SHIFT 28 |
| 107 | #define STM32H7_SPI_CFG1_MBR GENMASK(30, 28) |
| 108 | #define STM32H7_SPI_CFG1_MBR_MIN 0 |
| 109 | #define STM32H7_SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 110 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 111 | /* STM32H7_SPI_CFG2 bit fields */ |
| 112 | #define STM32H7_SPI_CFG2_MIDI_SHIFT 4 |
| 113 | #define STM32H7_SPI_CFG2_MIDI GENMASK(7, 4) |
| 114 | #define STM32H7_SPI_CFG2_COMM_SHIFT 17 |
| 115 | #define STM32H7_SPI_CFG2_COMM GENMASK(18, 17) |
| 116 | #define STM32H7_SPI_CFG2_SP_SHIFT 19 |
| 117 | #define STM32H7_SPI_CFG2_SP GENMASK(21, 19) |
| 118 | #define STM32H7_SPI_CFG2_MASTER BIT(22) |
| 119 | #define STM32H7_SPI_CFG2_LSBFRST BIT(23) |
| 120 | #define STM32H7_SPI_CFG2_CPHA BIT(24) |
| 121 | #define STM32H7_SPI_CFG2_CPOL BIT(25) |
| 122 | #define STM32H7_SPI_CFG2_SSM BIT(26) |
| 123 | #define STM32H7_SPI_CFG2_AFCNTR BIT(31) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 124 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 125 | /* STM32H7_SPI_IER bit fields */ |
| 126 | #define STM32H7_SPI_IER_RXPIE BIT(0) |
| 127 | #define STM32H7_SPI_IER_TXPIE BIT(1) |
| 128 | #define STM32H7_SPI_IER_DXPIE BIT(2) |
| 129 | #define STM32H7_SPI_IER_EOTIE BIT(3) |
| 130 | #define STM32H7_SPI_IER_TXTFIE BIT(4) |
| 131 | #define STM32H7_SPI_IER_OVRIE BIT(6) |
| 132 | #define STM32H7_SPI_IER_MODFIE BIT(9) |
| 133 | #define STM32H7_SPI_IER_ALL GENMASK(10, 0) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 134 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 135 | /* STM32H7_SPI_SR bit fields */ |
| 136 | #define STM32H7_SPI_SR_RXP BIT(0) |
| 137 | #define STM32H7_SPI_SR_TXP BIT(1) |
| 138 | #define STM32H7_SPI_SR_EOT BIT(3) |
| 139 | #define STM32H7_SPI_SR_OVR BIT(6) |
| 140 | #define STM32H7_SPI_SR_MODF BIT(9) |
| 141 | #define STM32H7_SPI_SR_SUSP BIT(11) |
| 142 | #define STM32H7_SPI_SR_RXPLVL_SHIFT 13 |
| 143 | #define STM32H7_SPI_SR_RXPLVL GENMASK(14, 13) |
| 144 | #define STM32H7_SPI_SR_RXWNE BIT(15) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 145 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 146 | /* STM32H7_SPI_IFCR bit fields */ |
| 147 | #define STM32H7_SPI_IFCR_ALL GENMASK(11, 3) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 148 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 149 | /* STM32H7_SPI_I2SCFGR bit fields */ |
| 150 | #define STM32H7_SPI_I2SCFGR_I2SMOD BIT(0) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 151 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 152 | /* STM32H7 SPI Master Baud Rate min/max divisor */ |
| 153 | #define STM32H7_SPI_MBR_DIV_MIN (2 << STM32H7_SPI_CFG1_MBR_MIN) |
| 154 | #define STM32H7_SPI_MBR_DIV_MAX (2 << STM32H7_SPI_CFG1_MBR_MAX) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 155 | |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 156 | /* STM32H7 SPI Communication mode */ |
| 157 | #define STM32H7_SPI_FULL_DUPLEX 0 |
| 158 | #define STM32H7_SPI_SIMPLEX_TX 1 |
| 159 | #define STM32H7_SPI_SIMPLEX_RX 2 |
| 160 | #define STM32H7_SPI_HALF_DUPLEX 3 |
| 161 | |
| 162 | /* SPI Communication type */ |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 163 | #define SPI_FULL_DUPLEX 0 |
| 164 | #define SPI_SIMPLEX_TX 1 |
| 165 | #define SPI_SIMPLEX_RX 2 |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 166 | #define SPI_3WIRE_TX 3 |
| 167 | #define SPI_3WIRE_RX 4 |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 168 | |
| 169 | #define SPI_1HZ_NS 1000000000 |
| 170 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 171 | /* |
| 172 | * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers |
| 173 | * without fifo buffers. |
| 174 | */ |
| 175 | #define SPI_DMA_MIN_BYTES 16 |
| 176 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 177 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 178 | * stm32_spi_reg - stm32 SPI register & bitfield desc |
| 179 | * @reg: register offset |
| 180 | * @mask: bitfield mask |
| 181 | * @shift: left shift |
| 182 | */ |
| 183 | struct stm32_spi_reg { |
| 184 | int reg; |
| 185 | int mask; |
| 186 | int shift; |
| 187 | }; |
| 188 | |
| 189 | /** |
| 190 | * stm32_spi_regspec - stm32 registers definition, compatible dependent data |
| 191 | * en: enable register and SPI enable bit |
| 192 | * dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit |
| 193 | * dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit |
| 194 | * cpol: clock polarity register and polarity bit |
| 195 | * cpha: clock phase register and phase bit |
| 196 | * lsb_first: LSB transmitted first register and bit |
| 197 | * br: baud rate register and bitfields |
| 198 | * rx: SPI RX data register |
| 199 | * tx: SPI TX data register |
| 200 | */ |
| 201 | struct stm32_spi_regspec { |
| 202 | const struct stm32_spi_reg en; |
| 203 | const struct stm32_spi_reg dma_rx_en; |
| 204 | const struct stm32_spi_reg dma_tx_en; |
| 205 | const struct stm32_spi_reg cpol; |
| 206 | const struct stm32_spi_reg cpha; |
| 207 | const struct stm32_spi_reg lsb_first; |
| 208 | const struct stm32_spi_reg br; |
| 209 | const struct stm32_spi_reg rx; |
| 210 | const struct stm32_spi_reg tx; |
| 211 | }; |
| 212 | |
| 213 | struct stm32_spi; |
| 214 | |
| 215 | /** |
| 216 | * stm32_spi_cfg - stm32 compatible configuration data |
| 217 | * @regs: registers descriptions |
| 218 | * @get_fifo_size: routine to get fifo size |
| 219 | * @get_bpw_mask: routine to get bits per word mask |
| 220 | * @disable: routine to disable controller |
| 221 | * @config: routine to configure controller as SPI Master |
| 222 | * @set_bpw: routine to configure registers to for bits per word |
| 223 | * @set_mode: routine to configure registers to desired mode |
| 224 | * @set_data_idleness: optional routine to configure registers to desired idle |
| 225 | * time between frames (if driver has this functionality) |
| 226 | * set_number_of_data: optional routine to configure registers to desired |
| 227 | * number of data (if driver has this functionality) |
| 228 | * @can_dma: routine to determine if the transfer is eligible for DMA use |
| 229 | * @transfer_one_dma_start: routine to start transfer a single spi_transfer |
| 230 | * using DMA |
| 231 | * @dma_rx cb: routine to call after DMA RX channel operation is complete |
| 232 | * @dma_tx cb: routine to call after DMA TX channel operation is complete |
| 233 | * @transfer_one_irq: routine to configure interrupts for driver |
| 234 | * @irq_handler_event: Interrupt handler for SPI controller events |
| 235 | * @irq_handler_thread: thread of interrupt handler for SPI controller |
| 236 | * @baud_rate_div_min: minimum baud rate divisor |
| 237 | * @baud_rate_div_max: maximum baud rate divisor |
| 238 | * @has_fifo: boolean to know if fifo is used for driver |
| 239 | * @has_startbit: boolean to know if start bit is used to start transfer |
| 240 | */ |
| 241 | struct stm32_spi_cfg { |
| 242 | const struct stm32_spi_regspec *regs; |
| 243 | int (*get_fifo_size)(struct stm32_spi *spi); |
| 244 | int (*get_bpw_mask)(struct stm32_spi *spi); |
| 245 | void (*disable)(struct stm32_spi *spi); |
| 246 | int (*config)(struct stm32_spi *spi); |
| 247 | void (*set_bpw)(struct stm32_spi *spi); |
| 248 | int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type); |
| 249 | void (*set_data_idleness)(struct stm32_spi *spi, u32 length); |
| 250 | int (*set_number_of_data)(struct stm32_spi *spi, u32 length); |
| 251 | void (*transfer_one_dma_start)(struct stm32_spi *spi); |
| 252 | void (*dma_rx_cb)(void *data); |
| 253 | void (*dma_tx_cb)(void *data); |
| 254 | int (*transfer_one_irq)(struct stm32_spi *spi); |
| 255 | irqreturn_t (*irq_handler_event)(int irq, void *dev_id); |
| 256 | irqreturn_t (*irq_handler_thread)(int irq, void *dev_id); |
| 257 | unsigned int baud_rate_div_min; |
| 258 | unsigned int baud_rate_div_max; |
| 259 | bool has_fifo; |
| 260 | }; |
| 261 | |
| 262 | /** |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 263 | * struct stm32_spi - private data of the SPI controller |
| 264 | * @dev: driver model representation of the controller |
| 265 | * @master: controller master interface |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 266 | * @cfg: compatible configuration data |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 267 | * @base: virtual memory area |
| 268 | * @clk: hw kernel clock feeding the SPI clock generator |
| 269 | * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator |
| 270 | * @rst: SPI controller reset line |
| 271 | * @lock: prevent I/O concurrent access |
| 272 | * @irq: SPI controller interrupt line |
| 273 | * @fifo_size: size of the embedded fifo in bytes |
| 274 | * @cur_midi: master inter-data idleness in ns |
| 275 | * @cur_speed: speed configured in Hz |
| 276 | * @cur_bpw: number of bits in a single SPI data frame |
| 277 | * @cur_fthlv: fifo threshold level (data frames in a single data packet) |
| 278 | * @cur_comm: SPI communication mode |
| 279 | * @cur_xferlen: current transfer length in bytes |
| 280 | * @cur_usedma: boolean to know if dma is used in current transfer |
| 281 | * @tx_buf: data to be written, or NULL |
| 282 | * @rx_buf: data to be read, or NULL |
| 283 | * @tx_len: number of data to be written in bytes |
| 284 | * @rx_len: number of data to be read in bytes |
| 285 | * @dma_tx: dma channel for TX transfer |
| 286 | * @dma_rx: dma channel for RX transfer |
| 287 | * @phys_addr: SPI registers physical base address |
| 288 | */ |
| 289 | struct stm32_spi { |
| 290 | struct device *dev; |
| 291 | struct spi_master *master; |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 292 | const struct stm32_spi_cfg *cfg; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 293 | void __iomem *base; |
| 294 | struct clk *clk; |
| 295 | u32 clk_rate; |
| 296 | struct reset_control *rst; |
| 297 | spinlock_t lock; /* prevent I/O concurrent access */ |
| 298 | int irq; |
| 299 | unsigned int fifo_size; |
| 300 | |
| 301 | unsigned int cur_midi; |
| 302 | unsigned int cur_speed; |
| 303 | unsigned int cur_bpw; |
| 304 | unsigned int cur_fthlv; |
| 305 | unsigned int cur_comm; |
| 306 | unsigned int cur_xferlen; |
| 307 | bool cur_usedma; |
| 308 | |
| 309 | const void *tx_buf; |
| 310 | void *rx_buf; |
| 311 | int tx_len; |
| 312 | int rx_len; |
| 313 | struct dma_chan *dma_tx; |
| 314 | struct dma_chan *dma_rx; |
| 315 | dma_addr_t phys_addr; |
| 316 | }; |
| 317 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 318 | static const struct stm32_spi_regspec stm32f4_spi_regspec = { |
| 319 | .en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE }, |
| 320 | |
| 321 | .dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN }, |
| 322 | .dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN }, |
| 323 | |
| 324 | .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL }, |
| 325 | .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA }, |
| 326 | .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST }, |
| 327 | .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT }, |
| 328 | |
| 329 | .rx = { STM32F4_SPI_DR }, |
| 330 | .tx = { STM32F4_SPI_DR }, |
| 331 | }; |
| 332 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 333 | static const struct stm32_spi_regspec stm32h7_spi_regspec = { |
| 334 | /* SPI data transfer is enabled but spi_ker_ck is idle. |
| 335 | * CFG1 and CFG2 registers are write protected when SPE is enabled. |
| 336 | */ |
| 337 | .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE }, |
| 338 | |
| 339 | .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN }, |
| 340 | .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN }, |
| 341 | |
| 342 | .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL }, |
| 343 | .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA }, |
| 344 | .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST }, |
| 345 | .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR, |
| 346 | STM32H7_SPI_CFG1_MBR_SHIFT }, |
| 347 | |
| 348 | .rx = { STM32H7_SPI_RXDR }, |
| 349 | .tx = { STM32H7_SPI_TXDR }, |
| 350 | }; |
| 351 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 352 | static inline void stm32_spi_set_bits(struct stm32_spi *spi, |
| 353 | u32 offset, u32 bits) |
| 354 | { |
| 355 | writel_relaxed(readl_relaxed(spi->base + offset) | bits, |
| 356 | spi->base + offset); |
| 357 | } |
| 358 | |
| 359 | static inline void stm32_spi_clr_bits(struct stm32_spi *spi, |
| 360 | u32 offset, u32 bits) |
| 361 | { |
| 362 | writel_relaxed(readl_relaxed(spi->base + offset) & ~bits, |
| 363 | spi->base + offset); |
| 364 | } |
| 365 | |
| 366 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 367 | * stm32h7_spi_get_fifo_size - Return fifo size |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 368 | * @spi: pointer to the spi controller data structure |
| 369 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 370 | static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 371 | { |
| 372 | unsigned long flags; |
| 373 | u32 count = 0; |
| 374 | |
| 375 | spin_lock_irqsave(&spi->lock, flags); |
| 376 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 377 | stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 378 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 379 | while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP) |
| 380 | writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 381 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 382 | stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 383 | |
| 384 | spin_unlock_irqrestore(&spi->lock, flags); |
| 385 | |
| 386 | dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count); |
| 387 | |
| 388 | return count; |
| 389 | } |
| 390 | |
| 391 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 392 | * stm32f4_spi_get_bpw_mask - Return bits per word mask |
| 393 | * @spi: pointer to the spi controller data structure |
| 394 | */ |
| 395 | static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi) |
| 396 | { |
| 397 | dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n"); |
| 398 | return SPI_BPW_MASK(8) | SPI_BPW_MASK(16); |
| 399 | } |
| 400 | |
| 401 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 402 | * stm32h7_spi_get_bpw_mask - Return bits per word mask |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 403 | * @spi: pointer to the spi controller data structure |
| 404 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 405 | static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 406 | { |
| 407 | unsigned long flags; |
| 408 | u32 cfg1, max_bpw; |
| 409 | |
| 410 | spin_lock_irqsave(&spi->lock, flags); |
| 411 | |
| 412 | /* |
| 413 | * The most significant bit at DSIZE bit field is reserved when the |
| 414 | * maximum data size of periperal instances is limited to 16-bit |
| 415 | */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 416 | stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 417 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 418 | cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1); |
| 419 | max_bpw = (cfg1 & STM32H7_SPI_CFG1_DSIZE) >> |
| 420 | STM32H7_SPI_CFG1_DSIZE_SHIFT; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 421 | max_bpw += 1; |
| 422 | |
| 423 | spin_unlock_irqrestore(&spi->lock, flags); |
| 424 | |
| 425 | dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw); |
| 426 | |
| 427 | return SPI_BPW_RANGE_MASK(4, max_bpw); |
| 428 | } |
| 429 | |
| 430 | /** |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 431 | * stm32_spi_prepare_mbr - Determine baud rate divisor value |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 432 | * @spi: pointer to the spi controller data structure |
| 433 | * @speed_hz: requested speed |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 434 | * @min_div: minimum baud rate divisor |
| 435 | * @max_div: maximum baud rate divisor |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 436 | * |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 437 | * Return baud rate divisor value in case of success or -EINVAL |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 438 | */ |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 439 | static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz, |
| 440 | u32 min_div, u32 max_div) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 441 | { |
| 442 | u32 div, mbrdiv; |
| 443 | |
| 444 | div = DIV_ROUND_UP(spi->clk_rate, speed_hz); |
| 445 | |
| 446 | /* |
| 447 | * SPI framework set xfer->speed_hz to master->max_speed_hz if |
| 448 | * xfer->speed_hz is greater than master->max_speed_hz, and it returns |
| 449 | * an error when xfer->speed_hz is lower than master->min_speed_hz, so |
| 450 | * no need to check it there. |
| 451 | * However, we need to ensure the following calculations. |
| 452 | */ |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 453 | if ((div < min_div) || (div > max_div)) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 454 | return -EINVAL; |
| 455 | |
| 456 | /* Determine the first power of 2 greater than or equal to div */ |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 457 | if (div & (div - 1)) |
| 458 | mbrdiv = fls(div); |
| 459 | else |
| 460 | mbrdiv = fls(div) - 1; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 461 | |
| 462 | spi->cur_speed = spi->clk_rate / (1 << mbrdiv); |
| 463 | |
| 464 | return mbrdiv - 1; |
| 465 | } |
| 466 | |
| 467 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 468 | * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 469 | * @spi: pointer to the spi controller data structure |
| 470 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 471 | static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 472 | { |
| 473 | u32 fthlv, half_fifo; |
| 474 | |
| 475 | /* data packet should not exceed 1/2 of fifo space */ |
| 476 | half_fifo = (spi->fifo_size / 2); |
| 477 | |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 478 | if (spi->cur_bpw <= 8) |
| 479 | fthlv = half_fifo; |
| 480 | else if (spi->cur_bpw <= 16) |
| 481 | fthlv = half_fifo / 2; |
| 482 | else |
| 483 | fthlv = half_fifo / 4; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 484 | |
| 485 | /* align packet size with data registers access */ |
| 486 | if (spi->cur_bpw > 8) |
| 487 | fthlv -= (fthlv % 2); /* multiple of 2 */ |
| 488 | else |
| 489 | fthlv -= (fthlv % 4); /* multiple of 4 */ |
| 490 | |
| 491 | return fthlv; |
| 492 | } |
| 493 | |
| 494 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 495 | * stm32f4_spi_write_tx - Write bytes to Transmit Data Register |
| 496 | * @spi: pointer to the spi controller data structure |
| 497 | * |
| 498 | * Read from tx_buf depends on remaining bytes to avoid to read beyond |
| 499 | * tx_buf end. |
| 500 | */ |
| 501 | static void stm32f4_spi_write_tx(struct stm32_spi *spi) |
| 502 | { |
| 503 | if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) & |
| 504 | STM32F4_SPI_SR_TXE)) { |
| 505 | u32 offs = spi->cur_xferlen - spi->tx_len; |
| 506 | |
| 507 | if (spi->cur_bpw == 16) { |
| 508 | const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs); |
| 509 | |
| 510 | writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR); |
| 511 | spi->tx_len -= sizeof(u16); |
| 512 | } else { |
| 513 | const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs); |
| 514 | |
| 515 | writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR); |
| 516 | spi->tx_len -= sizeof(u8); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len); |
| 521 | } |
| 522 | |
| 523 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 524 | * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 525 | * @spi: pointer to the spi controller data structure |
| 526 | * |
| 527 | * Read from tx_buf depends on remaining bytes to avoid to read beyond |
| 528 | * tx_buf end. |
| 529 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 530 | static void stm32h7_spi_write_txfifo(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 531 | { |
| 532 | while ((spi->tx_len > 0) && |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 533 | (readl_relaxed(spi->base + STM32H7_SPI_SR) & |
| 534 | STM32H7_SPI_SR_TXP)) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 535 | u32 offs = spi->cur_xferlen - spi->tx_len; |
| 536 | |
| 537 | if (spi->tx_len >= sizeof(u32)) { |
| 538 | const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs); |
| 539 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 540 | writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 541 | spi->tx_len -= sizeof(u32); |
| 542 | } else if (spi->tx_len >= sizeof(u16)) { |
| 543 | const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs); |
| 544 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 545 | writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 546 | spi->tx_len -= sizeof(u16); |
| 547 | } else { |
| 548 | const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs); |
| 549 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 550 | writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 551 | spi->tx_len -= sizeof(u8); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len); |
| 556 | } |
| 557 | |
| 558 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 559 | * stm32f4_spi_read_rx - Read bytes from Receive Data Register |
| 560 | * @spi: pointer to the spi controller data structure |
| 561 | * |
| 562 | * Write in rx_buf depends on remaining bytes to avoid to write beyond |
| 563 | * rx_buf end. |
| 564 | */ |
| 565 | static void stm32f4_spi_read_rx(struct stm32_spi *spi) |
| 566 | { |
| 567 | if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) & |
| 568 | STM32F4_SPI_SR_RXNE)) { |
| 569 | u32 offs = spi->cur_xferlen - spi->rx_len; |
| 570 | |
| 571 | if (spi->cur_bpw == 16) { |
| 572 | u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs); |
| 573 | |
| 574 | *rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR); |
| 575 | spi->rx_len -= sizeof(u16); |
| 576 | } else { |
| 577 | u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs); |
| 578 | |
| 579 | *rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR); |
| 580 | spi->rx_len -= sizeof(u8); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len); |
| 585 | } |
| 586 | |
| 587 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 588 | * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 589 | * @spi: pointer to the spi controller data structure |
| 590 | * |
| 591 | * Write in rx_buf depends on remaining bytes to avoid to write beyond |
| 592 | * rx_buf end. |
| 593 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 594 | static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi, bool flush) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 595 | { |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 596 | u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR); |
| 597 | u32 rxplvl = (sr & STM32H7_SPI_SR_RXPLVL) >> |
| 598 | STM32H7_SPI_SR_RXPLVL_SHIFT; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 599 | |
| 600 | while ((spi->rx_len > 0) && |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 601 | ((sr & STM32H7_SPI_SR_RXP) || |
| 602 | (flush && ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 603 | u32 offs = spi->cur_xferlen - spi->rx_len; |
| 604 | |
| 605 | if ((spi->rx_len >= sizeof(u32)) || |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 606 | (flush && (sr & STM32H7_SPI_SR_RXWNE))) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 607 | u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs); |
| 608 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 609 | *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 610 | spi->rx_len -= sizeof(u32); |
| 611 | } else if ((spi->rx_len >= sizeof(u16)) || |
| 612 | (flush && (rxplvl >= 2 || spi->cur_bpw > 8))) { |
| 613 | u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs); |
| 614 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 615 | *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 616 | spi->rx_len -= sizeof(u16); |
| 617 | } else { |
| 618 | u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs); |
| 619 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 620 | *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 621 | spi->rx_len -= sizeof(u8); |
| 622 | } |
| 623 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 624 | sr = readl_relaxed(spi->base + STM32H7_SPI_SR); |
| 625 | rxplvl = (sr & STM32H7_SPI_SR_RXPLVL) >> |
| 626 | STM32H7_SPI_SR_RXPLVL_SHIFT; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | dev_dbg(spi->dev, "%s%s: %d bytes left\n", __func__, |
| 630 | flush ? "(flush)" : "", spi->rx_len); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * stm32_spi_enable - Enable SPI controller |
| 635 | * @spi: pointer to the spi controller data structure |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 636 | */ |
| 637 | static void stm32_spi_enable(struct stm32_spi *spi) |
| 638 | { |
| 639 | dev_dbg(spi->dev, "enable controller\n"); |
| 640 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 641 | stm32_spi_set_bits(spi, spi->cfg->regs->en.reg, |
| 642 | spi->cfg->regs->en.mask); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 646 | * stm32f4_spi_disable - Disable SPI controller |
| 647 | * @spi: pointer to the spi controller data structure |
| 648 | */ |
| 649 | static void stm32f4_spi_disable(struct stm32_spi *spi) |
| 650 | { |
| 651 | unsigned long flags; |
| 652 | u32 sr; |
| 653 | |
| 654 | dev_dbg(spi->dev, "disable controller\n"); |
| 655 | |
| 656 | spin_lock_irqsave(&spi->lock, flags); |
| 657 | |
| 658 | if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) & |
| 659 | STM32F4_SPI_CR1_SPE)) { |
| 660 | spin_unlock_irqrestore(&spi->lock, flags); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | /* Disable interrupts */ |
| 665 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE | |
| 666 | STM32F4_SPI_CR2_RXNEIE | |
| 667 | STM32F4_SPI_CR2_ERRIE); |
| 668 | |
| 669 | /* Wait until BSY = 0 */ |
| 670 | if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR, |
| 671 | sr, !(sr & STM32F4_SPI_SR_BSY), |
| 672 | 10, 100000) < 0) { |
| 673 | dev_warn(spi->dev, "disabling condition timeout\n"); |
| 674 | } |
| 675 | |
| 676 | if (spi->cur_usedma && spi->dma_tx) |
| 677 | dmaengine_terminate_all(spi->dma_tx); |
| 678 | if (spi->cur_usedma && spi->dma_rx) |
| 679 | dmaengine_terminate_all(spi->dma_rx); |
| 680 | |
| 681 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE); |
| 682 | |
| 683 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN | |
| 684 | STM32F4_SPI_CR2_RXDMAEN); |
| 685 | |
| 686 | /* Sequence to clear OVR flag */ |
| 687 | readl_relaxed(spi->base + STM32F4_SPI_DR); |
| 688 | readl_relaxed(spi->base + STM32F4_SPI_SR); |
| 689 | |
| 690 | spin_unlock_irqrestore(&spi->lock, flags); |
| 691 | } |
| 692 | |
| 693 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 694 | * stm32h7_spi_disable - Disable SPI controller |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 695 | * @spi: pointer to the spi controller data structure |
| 696 | * |
| 697 | * RX-Fifo is flushed when SPI controller is disabled. To prevent any data |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 698 | * loss, use stm32h7_spi_read_rxfifo(flush) to read the remaining bytes in |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 699 | * RX-Fifo. |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 700 | * Normally, if TSIZE has been configured, we should relax the hardware at the |
| 701 | * reception of the EOT interrupt. But in case of error, EOT will not be |
| 702 | * raised. So the subsystem unprepare_message call allows us to properly |
| 703 | * complete the transfer from an hardware point of view. |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 704 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 705 | static void stm32h7_spi_disable(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 706 | { |
| 707 | unsigned long flags; |
| 708 | u32 cr1, sr; |
| 709 | |
| 710 | dev_dbg(spi->dev, "disable controller\n"); |
| 711 | |
| 712 | spin_lock_irqsave(&spi->lock, flags); |
| 713 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 714 | cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 715 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 716 | if (!(cr1 & STM32H7_SPI_CR1_SPE)) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 717 | spin_unlock_irqrestore(&spi->lock, flags); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | /* Wait on EOT or suspend the flow */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 722 | if (readl_relaxed_poll_timeout_atomic(spi->base + STM32H7_SPI_SR, |
| 723 | sr, !(sr & STM32H7_SPI_SR_EOT), |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 724 | 10, 100000) < 0) { |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 725 | if (cr1 & STM32H7_SPI_CR1_CSTART) { |
| 726 | writel_relaxed(cr1 | STM32H7_SPI_CR1_CSUSP, |
| 727 | spi->base + STM32H7_SPI_CR1); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 728 | if (readl_relaxed_poll_timeout_atomic( |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 729 | spi->base + STM32H7_SPI_SR, |
| 730 | sr, !(sr & STM32H7_SPI_SR_SUSP), |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 731 | 10, 100000) < 0) |
| 732 | dev_warn(spi->dev, |
| 733 | "Suspend request timeout\n"); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (!spi->cur_usedma && spi->rx_buf && (spi->rx_len > 0)) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 738 | stm32h7_spi_read_rxfifo(spi, true); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 739 | |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 740 | if (spi->cur_usedma && spi->dma_tx) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 741 | dmaengine_terminate_all(spi->dma_tx); |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 742 | if (spi->cur_usedma && spi->dma_rx) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 743 | dmaengine_terminate_all(spi->dma_rx); |
| 744 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 745 | stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 746 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 747 | stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN | |
| 748 | STM32H7_SPI_CFG1_RXDMAEN); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 749 | |
| 750 | /* Disable interrupts and clear status flags */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 751 | writel_relaxed(0, spi->base + STM32H7_SPI_IER); |
| 752 | writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 753 | |
| 754 | spin_unlock_irqrestore(&spi->lock, flags); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use |
| 759 | * |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 760 | * If driver has fifo and the current transfer size is greater than fifo size, |
| 761 | * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes. |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 762 | */ |
| 763 | static bool stm32_spi_can_dma(struct spi_master *master, |
| 764 | struct spi_device *spi_dev, |
| 765 | struct spi_transfer *transfer) |
| 766 | { |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 767 | unsigned int dma_size; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 768 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 769 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 770 | if (spi->cfg->has_fifo) |
| 771 | dma_size = spi->fifo_size; |
| 772 | else |
| 773 | dma_size = SPI_DMA_MIN_BYTES; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 774 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 775 | dev_dbg(spi->dev, "%s: %s\n", __func__, |
| 776 | (transfer->len > dma_size) ? "true" : "false"); |
| 777 | |
| 778 | return (transfer->len > dma_size); |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * stm32f4_spi_irq_event - Interrupt handler for SPI controller events |
| 783 | * @irq: interrupt line |
| 784 | * @dev_id: SPI controller master interface |
| 785 | */ |
| 786 | static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id) |
| 787 | { |
| 788 | struct spi_master *master = dev_id; |
| 789 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 790 | u32 sr, mask = 0; |
| 791 | unsigned long flags; |
| 792 | bool end = false; |
| 793 | |
| 794 | spin_lock_irqsave(&spi->lock, flags); |
| 795 | |
| 796 | sr = readl_relaxed(spi->base + STM32F4_SPI_SR); |
| 797 | /* |
| 798 | * BSY flag is not handled in interrupt but it is normal behavior when |
| 799 | * this flag is set. |
| 800 | */ |
| 801 | sr &= ~STM32F4_SPI_SR_BSY; |
| 802 | |
| 803 | if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX || |
| 804 | spi->cur_comm == SPI_3WIRE_TX)) { |
| 805 | /* OVR flag shouldn't be handled for TX only mode */ |
| 806 | sr &= ~STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE; |
| 807 | mask |= STM32F4_SPI_SR_TXE; |
| 808 | } |
| 809 | |
| 810 | if (!spi->cur_usedma && spi->cur_comm == SPI_FULL_DUPLEX) { |
| 811 | /* TXE flag is set and is handled when RXNE flag occurs */ |
| 812 | sr &= ~STM32F4_SPI_SR_TXE; |
| 813 | mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR; |
| 814 | } |
| 815 | |
| 816 | if (!(sr & mask)) { |
| 817 | dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr); |
| 818 | spin_unlock_irqrestore(&spi->lock, flags); |
| 819 | return IRQ_NONE; |
| 820 | } |
| 821 | |
| 822 | if (sr & STM32F4_SPI_SR_OVR) { |
| 823 | dev_warn(spi->dev, "Overrun: received value discarded\n"); |
| 824 | |
| 825 | /* Sequence to clear OVR flag */ |
| 826 | readl_relaxed(spi->base + STM32F4_SPI_DR); |
| 827 | readl_relaxed(spi->base + STM32F4_SPI_SR); |
| 828 | |
| 829 | /* |
| 830 | * If overrun is detected, it means that something went wrong, |
| 831 | * so stop the current transfer. Transfer can wait for next |
| 832 | * RXNE but DR is already read and end never happens. |
| 833 | */ |
| 834 | end = true; |
| 835 | goto end_irq; |
| 836 | } |
| 837 | |
| 838 | if (sr & STM32F4_SPI_SR_TXE) { |
| 839 | if (spi->tx_buf) |
| 840 | stm32f4_spi_write_tx(spi); |
| 841 | if (spi->tx_len == 0) |
| 842 | end = true; |
| 843 | } |
| 844 | |
| 845 | if (sr & STM32F4_SPI_SR_RXNE) { |
| 846 | stm32f4_spi_read_rx(spi); |
| 847 | if (spi->rx_len == 0) |
| 848 | end = true; |
| 849 | else /* Load data for discontinuous mode */ |
| 850 | stm32f4_spi_write_tx(spi); |
| 851 | } |
| 852 | |
| 853 | end_irq: |
| 854 | if (end) { |
| 855 | /* Immediately disable interrupts to do not generate new one */ |
| 856 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, |
| 857 | STM32F4_SPI_CR2_TXEIE | |
| 858 | STM32F4_SPI_CR2_RXNEIE | |
| 859 | STM32F4_SPI_CR2_ERRIE); |
| 860 | spin_unlock_irqrestore(&spi->lock, flags); |
| 861 | return IRQ_WAKE_THREAD; |
| 862 | } |
| 863 | |
| 864 | spin_unlock_irqrestore(&spi->lock, flags); |
| 865 | return IRQ_HANDLED; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * stm32f4_spi_irq_thread - Thread of interrupt handler for SPI controller |
| 870 | * @irq: interrupt line |
| 871 | * @dev_id: SPI controller master interface |
| 872 | */ |
| 873 | static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id) |
| 874 | { |
| 875 | struct spi_master *master = dev_id; |
| 876 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 877 | |
| 878 | spi_finalize_current_transfer(master); |
| 879 | stm32f4_spi_disable(spi); |
| 880 | |
| 881 | return IRQ_HANDLED; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 885 | * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 886 | * @irq: interrupt line |
| 887 | * @dev_id: SPI controller master interface |
| 888 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 889 | static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 890 | { |
| 891 | struct spi_master *master = dev_id; |
| 892 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 893 | u32 sr, ier, mask; |
| 894 | unsigned long flags; |
| 895 | bool end = false; |
| 896 | |
| 897 | spin_lock_irqsave(&spi->lock, flags); |
| 898 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 899 | sr = readl_relaxed(spi->base + STM32H7_SPI_SR); |
| 900 | ier = readl_relaxed(spi->base + STM32H7_SPI_IER); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 901 | |
| 902 | mask = ier; |
| 903 | /* EOTIE is triggered on EOT, SUSP and TXC events. */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 904 | mask |= STM32H7_SPI_SR_SUSP; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 905 | /* |
| 906 | * When TXTF is set, DXPIE and TXPIE are cleared. So in case of |
| 907 | * Full-Duplex, need to poll RXP event to know if there are remaining |
| 908 | * data, before disabling SPI. |
| 909 | */ |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 910 | if (spi->rx_buf && !spi->cur_usedma) |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 911 | mask |= STM32H7_SPI_SR_RXP; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 912 | |
| 913 | if (!(sr & mask)) { |
| 914 | dev_dbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n", |
| 915 | sr, ier); |
| 916 | spin_unlock_irqrestore(&spi->lock, flags); |
| 917 | return IRQ_NONE; |
| 918 | } |
| 919 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 920 | if (sr & STM32H7_SPI_SR_SUSP) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 921 | dev_warn(spi->dev, "Communication suspended\n"); |
| 922 | if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0))) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 923 | stm32h7_spi_read_rxfifo(spi, false); |
Amelie Delaunay | c67ad36 | 2017-06-27 17:45:19 +0200 | [diff] [blame] | 924 | /* |
| 925 | * If communication is suspended while using DMA, it means |
| 926 | * that something went wrong, so stop the current transfer |
| 927 | */ |
| 928 | if (spi->cur_usedma) |
| 929 | end = true; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 932 | if (sr & STM32H7_SPI_SR_MODF) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 933 | dev_warn(spi->dev, "Mode fault: transfer aborted\n"); |
| 934 | end = true; |
| 935 | } |
| 936 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 937 | if (sr & STM32H7_SPI_SR_OVR) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 938 | dev_warn(spi->dev, "Overrun: received value discarded\n"); |
| 939 | if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0))) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 940 | stm32h7_spi_read_rxfifo(spi, false); |
Amelie Delaunay | c67ad36 | 2017-06-27 17:45:19 +0200 | [diff] [blame] | 941 | /* |
| 942 | * If overrun is detected while using DMA, it means that |
| 943 | * something went wrong, so stop the current transfer |
| 944 | */ |
| 945 | if (spi->cur_usedma) |
| 946 | end = true; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 949 | if (sr & STM32H7_SPI_SR_EOT) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 950 | if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0))) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 951 | stm32h7_spi_read_rxfifo(spi, true); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 952 | end = true; |
| 953 | } |
| 954 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 955 | if (sr & STM32H7_SPI_SR_TXP) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 956 | if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0))) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 957 | stm32h7_spi_write_txfifo(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 958 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 959 | if (sr & STM32H7_SPI_SR_RXP) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 960 | if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0))) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 961 | stm32h7_spi_read_rxfifo(spi, false); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 962 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 963 | writel_relaxed(mask, spi->base + STM32H7_SPI_IFCR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 964 | |
| 965 | spin_unlock_irqrestore(&spi->lock, flags); |
| 966 | |
| 967 | if (end) { |
| 968 | spi_finalize_current_transfer(master); |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 969 | stm32h7_spi_disable(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | return IRQ_HANDLED; |
| 973 | } |
| 974 | |
| 975 | /** |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 976 | * stm32_spi_prepare_msg - set up the controller to transfer a single message |
| 977 | */ |
| 978 | static int stm32_spi_prepare_msg(struct spi_master *master, |
| 979 | struct spi_message *msg) |
| 980 | { |
| 981 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 982 | struct spi_device *spi_dev = msg->spi; |
| 983 | struct device_node *np = spi_dev->dev.of_node; |
| 984 | unsigned long flags; |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 985 | u32 clrb = 0, setb = 0; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 986 | |
| 987 | /* SPI slave device may need time between data frames */ |
| 988 | spi->cur_midi = 0; |
Amelie Delaunay | 042c1c6 | 2017-06-27 17:45:16 +0200 | [diff] [blame] | 989 | if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi)) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 990 | dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi); |
| 991 | |
| 992 | if (spi_dev->mode & SPI_CPOL) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 993 | setb |= spi->cfg->regs->cpol.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 994 | else |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 995 | clrb |= spi->cfg->regs->cpol.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 996 | |
| 997 | if (spi_dev->mode & SPI_CPHA) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 998 | setb |= spi->cfg->regs->cpha.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 999 | else |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1000 | clrb |= spi->cfg->regs->cpha.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1001 | |
| 1002 | if (spi_dev->mode & SPI_LSB_FIRST) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1003 | setb |= spi->cfg->regs->lsb_first.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1004 | else |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1005 | clrb |= spi->cfg->regs->lsb_first.mask; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1006 | |
| 1007 | dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n", |
| 1008 | spi_dev->mode & SPI_CPOL, |
| 1009 | spi_dev->mode & SPI_CPHA, |
| 1010 | spi_dev->mode & SPI_LSB_FIRST, |
| 1011 | spi_dev->mode & SPI_CS_HIGH); |
| 1012 | |
| 1013 | spin_lock_irqsave(&spi->lock, flags); |
| 1014 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1015 | /* CPOL, CPHA and LSB FIRST bits have common register */ |
| 1016 | if (clrb || setb) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1017 | writel_relaxed( |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1018 | (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) & |
| 1019 | ~clrb) | setb, |
| 1020 | spi->base + spi->cfg->regs->cpol.reg); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1021 | |
| 1022 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1028 | * stm32f4_spi_dma_tx_cb - dma callback |
| 1029 | * |
| 1030 | * DMA callback is called when the transfer is complete for DMA TX channel. |
| 1031 | */ |
| 1032 | static void stm32f4_spi_dma_tx_cb(void *data) |
| 1033 | { |
| 1034 | struct stm32_spi *spi = data; |
| 1035 | |
| 1036 | if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) { |
| 1037 | spi_finalize_current_transfer(spi->master); |
| 1038 | stm32f4_spi_disable(spi); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * stm32f4_spi_dma_rx_cb - dma callback |
| 1044 | * |
| 1045 | * DMA callback is called when the transfer is complete for DMA RX channel. |
| 1046 | */ |
| 1047 | static void stm32f4_spi_dma_rx_cb(void *data) |
| 1048 | { |
| 1049 | struct stm32_spi *spi = data; |
| 1050 | |
| 1051 | spi_finalize_current_transfer(spi->master); |
| 1052 | stm32f4_spi_disable(spi); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1056 | * stm32h7_spi_dma_cb - dma callback |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1057 | * |
| 1058 | * DMA callback is called when the transfer is complete or when an error |
| 1059 | * occurs. If the transfer is complete, EOT flag is raised. |
| 1060 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1061 | static void stm32h7_spi_dma_cb(void *data) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1062 | { |
| 1063 | struct stm32_spi *spi = data; |
| 1064 | unsigned long flags; |
| 1065 | u32 sr; |
| 1066 | |
| 1067 | spin_lock_irqsave(&spi->lock, flags); |
| 1068 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1069 | sr = readl_relaxed(spi->base + STM32H7_SPI_SR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1070 | |
| 1071 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1072 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1073 | if (!(sr & STM32H7_SPI_SR_EOT)) |
Amelie Delaunay | c67ad36 | 2017-06-27 17:45:19 +0200 | [diff] [blame] | 1074 | dev_warn(spi->dev, "DMA error (sr=0x%08x)\n", sr); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1075 | |
Amelie Delaunay | c67ad36 | 2017-06-27 17:45:19 +0200 | [diff] [blame] | 1076 | /* Now wait for EOT, or SUSP or OVR in case of error */ |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * stm32_spi_dma_config - configure dma slave channel depending on current |
| 1081 | * transfer bits_per_word. |
| 1082 | */ |
| 1083 | static void stm32_spi_dma_config(struct stm32_spi *spi, |
| 1084 | struct dma_slave_config *dma_conf, |
| 1085 | enum dma_transfer_direction dir) |
| 1086 | { |
| 1087 | enum dma_slave_buswidth buswidth; |
| 1088 | u32 maxburst; |
| 1089 | |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 1090 | if (spi->cur_bpw <= 8) |
| 1091 | buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1092 | else if (spi->cur_bpw <= 16) |
| 1093 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 1094 | else |
| 1095 | buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1096 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1097 | if (spi->cfg->has_fifo) { |
| 1098 | /* Valid for DMA Half or Full Fifo threshold */ |
| 1099 | if (spi->cur_fthlv == 2) |
| 1100 | maxburst = 1; |
| 1101 | else |
| 1102 | maxburst = spi->cur_fthlv; |
| 1103 | } else { |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 1104 | maxburst = 1; |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1105 | } |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1106 | |
| 1107 | memset(dma_conf, 0, sizeof(struct dma_slave_config)); |
| 1108 | dma_conf->direction = dir; |
| 1109 | if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1110 | dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1111 | dma_conf->src_addr_width = buswidth; |
| 1112 | dma_conf->src_maxburst = maxburst; |
| 1113 | |
| 1114 | dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n", |
| 1115 | buswidth, maxburst); |
| 1116 | } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1117 | dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1118 | dma_conf->dst_addr_width = buswidth; |
| 1119 | dma_conf->dst_maxburst = maxburst; |
| 1120 | |
| 1121 | dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n", |
| 1122 | buswidth, maxburst); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1127 | * stm32f4_spi_transfer_one_irq - transfer a single spi_transfer using |
| 1128 | * interrupts |
| 1129 | * |
| 1130 | * It must returns 0 if the transfer is finished or 1 if the transfer is still |
| 1131 | * in progress. |
| 1132 | */ |
| 1133 | static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi) |
| 1134 | { |
| 1135 | unsigned long flags; |
| 1136 | u32 cr2 = 0; |
| 1137 | |
| 1138 | /* Enable the interrupts relative to the current communication mode */ |
| 1139 | if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) { |
| 1140 | cr2 |= STM32F4_SPI_CR2_TXEIE; |
| 1141 | } else if (spi->cur_comm == SPI_FULL_DUPLEX) { |
| 1142 | /* In transmit-only mode, the OVR flag is set in the SR register |
| 1143 | * since the received data are never read. Therefore set OVR |
| 1144 | * interrupt only when rx buffer is available. |
| 1145 | */ |
| 1146 | cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE; |
| 1147 | } else { |
| 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | |
| 1151 | spin_lock_irqsave(&spi->lock, flags); |
| 1152 | |
| 1153 | stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2); |
| 1154 | |
| 1155 | stm32_spi_enable(spi); |
| 1156 | |
| 1157 | /* starting data transfer when buffer is loaded */ |
| 1158 | if (spi->tx_buf) |
| 1159 | stm32f4_spi_write_tx(spi); |
| 1160 | |
| 1161 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1162 | |
| 1163 | return 1; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1167 | * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using |
| 1168 | * interrupts |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1169 | * |
| 1170 | * It must returns 0 if the transfer is finished or 1 if the transfer is still |
| 1171 | * in progress. |
| 1172 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1173 | static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1174 | { |
| 1175 | unsigned long flags; |
| 1176 | u32 ier = 0; |
| 1177 | |
| 1178 | /* Enable the interrupts relative to the current communication mode */ |
| 1179 | if (spi->tx_buf && spi->rx_buf) /* Full Duplex */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1180 | ier |= STM32H7_SPI_IER_DXPIE; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1181 | else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1182 | ier |= STM32H7_SPI_IER_TXPIE; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1183 | else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1184 | ier |= STM32H7_SPI_IER_RXPIE; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1185 | |
| 1186 | /* Enable the interrupts relative to the end of transfer */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1187 | ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE | |
| 1188 | STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1189 | |
| 1190 | spin_lock_irqsave(&spi->lock, flags); |
| 1191 | |
| 1192 | stm32_spi_enable(spi); |
| 1193 | |
| 1194 | /* Be sure to have data in fifo before starting data transfer */ |
| 1195 | if (spi->tx_buf) |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1196 | stm32h7_spi_write_txfifo(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1197 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1198 | stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1199 | |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1200 | writel_relaxed(ier, spi->base + STM32H7_SPI_IER); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1201 | |
| 1202 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1203 | |
| 1204 | return 1; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1208 | * stm32f4_spi_transfer_one_dma_start - Set SPI driver registers to start |
| 1209 | * transfer using DMA |
| 1210 | */ |
| 1211 | static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi) |
| 1212 | { |
| 1213 | /* In DMA mode end of transfer is handled by DMA TX or RX callback. */ |
| 1214 | if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX || |
| 1215 | spi->cur_comm == SPI_FULL_DUPLEX) { |
| 1216 | /* |
| 1217 | * In transmit-only mode, the OVR flag is set in the SR register |
| 1218 | * since the received data are never read. Therefore set OVR |
| 1219 | * interrupt only when rx buffer is available. |
| 1220 | */ |
| 1221 | stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE); |
| 1222 | } |
| 1223 | |
| 1224 | stm32_spi_enable(spi); |
| 1225 | } |
| 1226 | |
| 1227 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1228 | * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start |
| 1229 | * transfer using DMA |
Cezary Gapinski | f8bb12f | 2018-12-24 23:00:36 +0100 | [diff] [blame] | 1230 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1231 | static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi) |
Cezary Gapinski | f8bb12f | 2018-12-24 23:00:36 +0100 | [diff] [blame] | 1232 | { |
| 1233 | /* Enable the interrupts relative to the end of transfer */ |
| 1234 | stm32_spi_set_bits(spi, STM32H7_SPI_IER, STM32H7_SPI_IER_EOTIE | |
| 1235 | STM32H7_SPI_IER_TXTFIE | |
| 1236 | STM32H7_SPI_IER_OVRIE | |
| 1237 | STM32H7_SPI_IER_MODFIE); |
| 1238 | |
| 1239 | stm32_spi_enable(spi); |
| 1240 | |
| 1241 | stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); |
| 1242 | } |
| 1243 | |
| 1244 | /** |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1245 | * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA |
| 1246 | * |
| 1247 | * It must returns 0 if the transfer is finished or 1 if the transfer is still |
| 1248 | * in progress. |
| 1249 | */ |
| 1250 | static int stm32_spi_transfer_one_dma(struct stm32_spi *spi, |
| 1251 | struct spi_transfer *xfer) |
| 1252 | { |
| 1253 | struct dma_slave_config tx_dma_conf, rx_dma_conf; |
| 1254 | struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc; |
| 1255 | unsigned long flags; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1256 | |
| 1257 | spin_lock_irqsave(&spi->lock, flags); |
| 1258 | |
| 1259 | rx_dma_desc = NULL; |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1260 | if (spi->rx_buf && spi->dma_rx) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1261 | stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM); |
| 1262 | dmaengine_slave_config(spi->dma_rx, &rx_dma_conf); |
| 1263 | |
| 1264 | /* Enable Rx DMA request */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1265 | stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg, |
| 1266 | spi->cfg->regs->dma_rx_en.mask); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1267 | |
| 1268 | rx_dma_desc = dmaengine_prep_slave_sg( |
| 1269 | spi->dma_rx, xfer->rx_sg.sgl, |
| 1270 | xfer->rx_sg.nents, |
| 1271 | rx_dma_conf.direction, |
| 1272 | DMA_PREP_INTERRUPT); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | tx_dma_desc = NULL; |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1276 | if (spi->tx_buf && spi->dma_tx) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1277 | stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV); |
| 1278 | dmaengine_slave_config(spi->dma_tx, &tx_dma_conf); |
| 1279 | |
| 1280 | tx_dma_desc = dmaengine_prep_slave_sg( |
| 1281 | spi->dma_tx, xfer->tx_sg.sgl, |
| 1282 | xfer->tx_sg.nents, |
| 1283 | tx_dma_conf.direction, |
| 1284 | DMA_PREP_INTERRUPT); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1287 | if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) || |
| 1288 | (spi->rx_buf && spi->dma_rx && !rx_dma_desc)) |
| 1289 | goto dma_desc_error; |
| 1290 | |
| 1291 | if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc)) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1292 | goto dma_desc_error; |
| 1293 | |
| 1294 | if (rx_dma_desc) { |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1295 | rx_dma_desc->callback = spi->cfg->dma_rx_cb; |
Amelie Delaunay | 7b821a6 | 2017-06-27 17:45:20 +0200 | [diff] [blame] | 1296 | rx_dma_desc->callback_param = spi; |
| 1297 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1298 | if (dma_submit_error(dmaengine_submit(rx_dma_desc))) { |
| 1299 | dev_err(spi->dev, "Rx DMA submit failed\n"); |
| 1300 | goto dma_desc_error; |
| 1301 | } |
| 1302 | /* Enable Rx DMA channel */ |
| 1303 | dma_async_issue_pending(spi->dma_rx); |
| 1304 | } |
| 1305 | |
| 1306 | if (tx_dma_desc) { |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1307 | if (spi->cur_comm == SPI_SIMPLEX_TX || |
| 1308 | spi->cur_comm == SPI_3WIRE_TX) { |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1309 | tx_dma_desc->callback = spi->cfg->dma_tx_cb; |
Amelie Delaunay | 7b821a6 | 2017-06-27 17:45:20 +0200 | [diff] [blame] | 1310 | tx_dma_desc->callback_param = spi; |
| 1311 | } |
| 1312 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1313 | if (dma_submit_error(dmaengine_submit(tx_dma_desc))) { |
| 1314 | dev_err(spi->dev, "Tx DMA submit failed\n"); |
| 1315 | goto dma_submit_error; |
| 1316 | } |
| 1317 | /* Enable Tx DMA channel */ |
| 1318 | dma_async_issue_pending(spi->dma_tx); |
| 1319 | |
| 1320 | /* Enable Tx DMA request */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1321 | stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg, |
| 1322 | spi->cfg->regs->dma_tx_en.mask); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1325 | spi->cfg->transfer_one_dma_start(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1326 | |
| 1327 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1328 | |
| 1329 | return 1; |
| 1330 | |
| 1331 | dma_submit_error: |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1332 | if (spi->dma_rx) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1333 | dmaengine_terminate_all(spi->dma_rx); |
| 1334 | |
| 1335 | dma_desc_error: |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1336 | stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg, |
| 1337 | spi->cfg->regs->dma_rx_en.mask); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1338 | |
| 1339 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1340 | |
| 1341 | dev_info(spi->dev, "DMA issue: fall back to irq transfer\n"); |
| 1342 | |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1343 | spi->cur_usedma = false; |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1344 | return spi->cfg->transfer_one_irq(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1348 | * stm32f4_spi_set_bpw - Configure bits per word |
| 1349 | * @spi: pointer to the spi controller data structure |
| 1350 | */ |
| 1351 | static void stm32f4_spi_set_bpw(struct stm32_spi *spi) |
| 1352 | { |
| 1353 | if (spi->cur_bpw == 16) |
| 1354 | stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF); |
| 1355 | else |
| 1356 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF); |
| 1357 | } |
| 1358 | |
| 1359 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1360 | * stm32h7_spi_set_bpw - configure bits per word |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1361 | * @spi: pointer to the spi controller data structure |
| 1362 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1363 | static void stm32h7_spi_set_bpw(struct stm32_spi *spi) |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1364 | { |
| 1365 | u32 bpw, fthlv; |
| 1366 | u32 cfg1_clrb = 0, cfg1_setb = 0; |
| 1367 | |
| 1368 | bpw = spi->cur_bpw - 1; |
| 1369 | |
| 1370 | cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE; |
| 1371 | cfg1_setb |= (bpw << STM32H7_SPI_CFG1_DSIZE_SHIFT) & |
| 1372 | STM32H7_SPI_CFG1_DSIZE; |
| 1373 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1374 | spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi); |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1375 | fthlv = spi->cur_fthlv - 1; |
| 1376 | |
| 1377 | cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV; |
| 1378 | cfg1_setb |= (fthlv << STM32H7_SPI_CFG1_FTHLV_SHIFT) & |
| 1379 | STM32H7_SPI_CFG1_FTHLV; |
| 1380 | |
| 1381 | writel_relaxed( |
| 1382 | (readl_relaxed(spi->base + STM32H7_SPI_CFG1) & |
| 1383 | ~cfg1_clrb) | cfg1_setb, |
| 1384 | spi->base + STM32H7_SPI_CFG1); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * stm32_spi_set_mbr - Configure baud rate divisor in master mode |
| 1389 | * @spi: pointer to the spi controller data structure |
| 1390 | * @mbrdiv: baud rate divisor value |
| 1391 | */ |
| 1392 | static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv) |
| 1393 | { |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1394 | u32 clrb = 0, setb = 0; |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1395 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1396 | clrb |= spi->cfg->regs->br.mask; |
| 1397 | setb |= ((u32)mbrdiv << spi->cfg->regs->br.shift) & |
| 1398 | spi->cfg->regs->br.mask; |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1399 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1400 | writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) & |
| 1401 | ~clrb) | setb, |
| 1402 | spi->base + spi->cfg->regs->br.reg); |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * stm32_spi_communication_type - return transfer communication type |
| 1407 | * @spi_dev: pointer to the spi device |
| 1408 | * transfer: pointer to spi transfer |
| 1409 | */ |
| 1410 | static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev, |
| 1411 | struct spi_transfer *transfer) |
| 1412 | { |
| 1413 | unsigned int type = SPI_FULL_DUPLEX; |
| 1414 | |
| 1415 | if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */ |
| 1416 | /* |
| 1417 | * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL |
| 1418 | * is forbidden and unvalidated by SPI subsystem so depending |
| 1419 | * on the valid buffer, we can determine the direction of the |
| 1420 | * transfer. |
| 1421 | */ |
| 1422 | if (!transfer->tx_buf) |
| 1423 | type = SPI_3WIRE_RX; |
| 1424 | else |
| 1425 | type = SPI_3WIRE_TX; |
| 1426 | } else { |
| 1427 | if (!transfer->tx_buf) |
| 1428 | type = SPI_SIMPLEX_RX; |
| 1429 | else if (!transfer->rx_buf) |
| 1430 | type = SPI_SIMPLEX_TX; |
| 1431 | } |
| 1432 | |
| 1433 | return type; |
| 1434 | } |
| 1435 | |
| 1436 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1437 | * stm32f4_spi_set_mode - configure communication mode |
| 1438 | * @spi: pointer to the spi controller data structure |
| 1439 | * @comm_type: type of communication to configure |
| 1440 | */ |
| 1441 | static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type) |
| 1442 | { |
| 1443 | if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) { |
| 1444 | stm32_spi_set_bits(spi, STM32F4_SPI_CR1, |
| 1445 | STM32F4_SPI_CR1_BIDIMODE | |
| 1446 | STM32F4_SPI_CR1_BIDIOE); |
| 1447 | } else if (comm_type == SPI_FULL_DUPLEX) { |
| 1448 | stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, |
| 1449 | STM32F4_SPI_CR1_BIDIMODE | |
| 1450 | STM32F4_SPI_CR1_BIDIOE); |
| 1451 | } else { |
| 1452 | return -EINVAL; |
| 1453 | } |
| 1454 | |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1459 | * stm32h7_spi_set_mode - configure communication mode |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1460 | * @spi: pointer to the spi controller data structure |
| 1461 | * @comm_type: type of communication to configure |
| 1462 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1463 | static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type) |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1464 | { |
| 1465 | u32 mode; |
| 1466 | u32 cfg2_clrb = 0, cfg2_setb = 0; |
| 1467 | |
| 1468 | if (comm_type == SPI_3WIRE_RX) { |
| 1469 | mode = STM32H7_SPI_HALF_DUPLEX; |
| 1470 | stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR); |
| 1471 | } else if (comm_type == SPI_3WIRE_TX) { |
| 1472 | mode = STM32H7_SPI_HALF_DUPLEX; |
| 1473 | stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR); |
| 1474 | } else if (comm_type == SPI_SIMPLEX_RX) { |
| 1475 | mode = STM32H7_SPI_SIMPLEX_RX; |
| 1476 | } else if (comm_type == SPI_SIMPLEX_TX) { |
| 1477 | mode = STM32H7_SPI_SIMPLEX_TX; |
| 1478 | } else { |
| 1479 | mode = STM32H7_SPI_FULL_DUPLEX; |
| 1480 | } |
| 1481 | |
| 1482 | cfg2_clrb |= STM32H7_SPI_CFG2_COMM; |
| 1483 | cfg2_setb |= (mode << STM32H7_SPI_CFG2_COMM_SHIFT) & |
| 1484 | STM32H7_SPI_CFG2_COMM; |
| 1485 | |
| 1486 | writel_relaxed( |
| 1487 | (readl_relaxed(spi->base + STM32H7_SPI_CFG2) & |
| 1488 | ~cfg2_clrb) | cfg2_setb, |
| 1489 | spi->base + STM32H7_SPI_CFG2); |
| 1490 | |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1495 | * stm32h7_spi_data_idleness - configure minimum time delay inserted between two |
| 1496 | * consecutive data frames in master mode |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1497 | * @spi: pointer to the spi controller data structure |
| 1498 | * @len: transfer len |
| 1499 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1500 | static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len) |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1501 | { |
| 1502 | u32 cfg2_clrb = 0, cfg2_setb = 0; |
| 1503 | |
| 1504 | cfg2_clrb |= STM32H7_SPI_CFG2_MIDI; |
| 1505 | if ((len > 1) && (spi->cur_midi > 0)) { |
| 1506 | u32 sck_period_ns = DIV_ROUND_UP(SPI_1HZ_NS, spi->cur_speed); |
| 1507 | u32 midi = min((u32)DIV_ROUND_UP(spi->cur_midi, sck_period_ns), |
| 1508 | (u32)STM32H7_SPI_CFG2_MIDI >> |
| 1509 | STM32H7_SPI_CFG2_MIDI_SHIFT); |
| 1510 | |
| 1511 | dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n", |
| 1512 | sck_period_ns, midi, midi * sck_period_ns); |
| 1513 | cfg2_setb |= (midi << STM32H7_SPI_CFG2_MIDI_SHIFT) & |
| 1514 | STM32H7_SPI_CFG2_MIDI; |
| 1515 | } |
| 1516 | |
| 1517 | writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) & |
| 1518 | ~cfg2_clrb) | cfg2_setb, |
| 1519 | spi->base + STM32H7_SPI_CFG2); |
| 1520 | } |
| 1521 | |
| 1522 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1523 | * stm32h7_spi_number_of_data - configure number of data at current transfer |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1524 | * @spi: pointer to the spi controller data structure |
| 1525 | * @len: transfer length |
| 1526 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1527 | static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words) |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1528 | { |
| 1529 | u32 cr2_clrb = 0, cr2_setb = 0; |
| 1530 | |
| 1531 | if (nb_words <= (STM32H7_SPI_CR2_TSIZE >> |
| 1532 | STM32H7_SPI_CR2_TSIZE_SHIFT)) { |
| 1533 | cr2_clrb |= STM32H7_SPI_CR2_TSIZE; |
| 1534 | cr2_setb = nb_words << STM32H7_SPI_CR2_TSIZE_SHIFT; |
| 1535 | writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CR2) & |
| 1536 | ~cr2_clrb) | cr2_setb, |
| 1537 | spi->base + STM32H7_SPI_CR2); |
| 1538 | } else { |
| 1539 | return -EMSGSIZE; |
| 1540 | } |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /** |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1546 | * stm32_spi_transfer_one_setup - common setup to transfer a single |
| 1547 | * spi_transfer either using DMA or |
| 1548 | * interrupts. |
| 1549 | */ |
| 1550 | static int stm32_spi_transfer_one_setup(struct stm32_spi *spi, |
| 1551 | struct spi_device *spi_dev, |
| 1552 | struct spi_transfer *transfer) |
| 1553 | { |
| 1554 | unsigned long flags; |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1555 | unsigned int comm_type; |
| 1556 | int nb_words, ret = 0; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1557 | |
| 1558 | spin_lock_irqsave(&spi->lock, flags); |
| 1559 | |
| 1560 | if (spi->cur_bpw != transfer->bits_per_word) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1561 | spi->cur_bpw = transfer->bits_per_word; |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1562 | spi->cfg->set_bpw(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if (spi->cur_speed != transfer->speed_hz) { |
Colin Ian King | a2f07d38 | 2017-06-22 17:34:49 +0100 | [diff] [blame] | 1566 | int mbr; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1567 | |
| 1568 | /* Update spi->cur_speed with real clock speed */ |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1569 | mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz, |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1570 | spi->cfg->baud_rate_div_min, |
| 1571 | spi->cfg->baud_rate_div_max); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1572 | if (mbr < 0) { |
| 1573 | ret = mbr; |
| 1574 | goto out; |
| 1575 | } |
| 1576 | |
| 1577 | transfer->speed_hz = spi->cur_speed; |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1578 | stm32_spi_set_mbr(spi, mbr); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1581 | comm_type = stm32_spi_communication_type(spi_dev, transfer); |
| 1582 | if (spi->cur_comm != comm_type) { |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1583 | ret = spi->cfg->set_mode(spi, comm_type); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1584 | |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1585 | if (ret < 0) |
| 1586 | goto out; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1587 | |
Cezary Gapinski | 9d5fce1 | 2018-12-24 23:00:35 +0100 | [diff] [blame] | 1588 | spi->cur_comm = comm_type; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1591 | if (spi->cfg->set_data_idleness) |
| 1592 | spi->cfg->set_data_idleness(spi, transfer->len); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1593 | |
Amelie Delaunay | 128ebb8 | 2017-06-27 17:45:17 +0200 | [diff] [blame] | 1594 | if (spi->cur_bpw <= 8) |
| 1595 | nb_words = transfer->len; |
| 1596 | else if (spi->cur_bpw <= 16) |
| 1597 | nb_words = DIV_ROUND_UP(transfer->len * 8, 16); |
| 1598 | else |
| 1599 | nb_words = DIV_ROUND_UP(transfer->len * 8, 32); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1600 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1601 | if (spi->cfg->set_number_of_data) { |
| 1602 | ret = spi->cfg->set_number_of_data(spi, nb_words); |
| 1603 | if (ret < 0) |
| 1604 | goto out; |
| 1605 | } |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1606 | |
| 1607 | spi->cur_xferlen = transfer->len; |
| 1608 | |
| 1609 | dev_dbg(spi->dev, "transfer communication mode set to %d\n", |
| 1610 | spi->cur_comm); |
| 1611 | dev_dbg(spi->dev, |
| 1612 | "data frame of %d-bit, data packet of %d data frames\n", |
| 1613 | spi->cur_bpw, spi->cur_fthlv); |
| 1614 | dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed); |
| 1615 | dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n", |
| 1616 | spi->cur_xferlen, nb_words); |
| 1617 | dev_dbg(spi->dev, "dma %s\n", |
| 1618 | (spi->cur_usedma) ? "enabled" : "disabled"); |
| 1619 | |
| 1620 | out: |
| 1621 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1622 | |
| 1623 | return ret; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
| 1627 | * stm32_spi_transfer_one - transfer a single spi_transfer |
| 1628 | * |
| 1629 | * It must return 0 if the transfer is finished or 1 if the transfer is still |
| 1630 | * in progress. |
| 1631 | */ |
| 1632 | static int stm32_spi_transfer_one(struct spi_master *master, |
| 1633 | struct spi_device *spi_dev, |
| 1634 | struct spi_transfer *transfer) |
| 1635 | { |
| 1636 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1637 | int ret; |
| 1638 | |
| 1639 | spi->tx_buf = transfer->tx_buf; |
| 1640 | spi->rx_buf = transfer->rx_buf; |
| 1641 | spi->tx_len = spi->tx_buf ? transfer->len : 0; |
| 1642 | spi->rx_len = spi->rx_buf ? transfer->len : 0; |
| 1643 | |
Amelie Delaunay | c67ad36 | 2017-06-27 17:45:19 +0200 | [diff] [blame] | 1644 | spi->cur_usedma = (master->can_dma && |
Cezary Gapinski | 2cbee7f | 2018-12-24 23:00:29 +0100 | [diff] [blame] | 1645 | master->can_dma(master, spi_dev, transfer)); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1646 | |
| 1647 | ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer); |
| 1648 | if (ret) { |
| 1649 | dev_err(spi->dev, "SPI transfer setup failed\n"); |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
| 1653 | if (spi->cur_usedma) |
| 1654 | return stm32_spi_transfer_one_dma(spi, transfer); |
| 1655 | else |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1656 | return spi->cfg->transfer_one_irq(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * stm32_spi_unprepare_msg - relax the hardware |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1661 | */ |
| 1662 | static int stm32_spi_unprepare_msg(struct spi_master *master, |
| 1663 | struct spi_message *msg) |
| 1664 | { |
| 1665 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1666 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1667 | spi->cfg->disable(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1668 | |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | /** |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1673 | * stm32f4_spi_config - Configure SPI controller as SPI master |
| 1674 | */ |
| 1675 | static int stm32f4_spi_config(struct stm32_spi *spi) |
| 1676 | { |
| 1677 | unsigned long flags; |
| 1678 | |
| 1679 | spin_lock_irqsave(&spi->lock, flags); |
| 1680 | |
| 1681 | /* Ensure I2SMOD bit is kept cleared */ |
| 1682 | stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR, |
| 1683 | STM32F4_SPI_I2SCFGR_I2SMOD); |
| 1684 | |
| 1685 | /* |
| 1686 | * - SS input value high |
| 1687 | * - transmitter half duplex direction |
| 1688 | * - Set the master mode (default Motorola mode) |
| 1689 | * - Consider 1 master/n slaves configuration and |
| 1690 | * SS input value is determined by the SSI bit |
| 1691 | */ |
| 1692 | stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI | |
| 1693 | STM32F4_SPI_CR1_BIDIOE | |
| 1694 | STM32F4_SPI_CR1_MSTR | |
| 1695 | STM32F4_SPI_CR1_SSM); |
| 1696 | |
| 1697 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1698 | |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | /** |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1703 | * stm32h7_spi_config - Configure SPI controller as SPI master |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1704 | */ |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1705 | static int stm32h7_spi_config(struct stm32_spi *spi) |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1706 | { |
| 1707 | unsigned long flags; |
| 1708 | |
| 1709 | spin_lock_irqsave(&spi->lock, flags); |
| 1710 | |
| 1711 | /* Ensure I2SMOD bit is kept cleared */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1712 | stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR, |
| 1713 | STM32H7_SPI_I2SCFGR_I2SMOD); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* |
| 1716 | * - SS input value high |
| 1717 | * - transmitter half duplex direction |
| 1718 | * - automatic communication suspend when RX-Fifo is full |
| 1719 | */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1720 | stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI | |
| 1721 | STM32H7_SPI_CR1_HDDIR | |
| 1722 | STM32H7_SPI_CR1_MASRX); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | * - Set the master mode (default Motorola mode) |
| 1726 | * - Consider 1 master/n slaves configuration and |
| 1727 | * SS input value is determined by the SSI bit |
| 1728 | * - keep control of all associated GPIOs |
| 1729 | */ |
Cezary Gapinski | 8602663 | 2018-12-24 23:00:33 +0100 | [diff] [blame] | 1730 | stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER | |
| 1731 | STM32H7_SPI_CFG2_SSM | |
| 1732 | STM32H7_SPI_CFG2_AFCNTR); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1733 | |
| 1734 | spin_unlock_irqrestore(&spi->lock, flags); |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1739 | static const struct stm32_spi_cfg stm32f4_spi_cfg = { |
| 1740 | .regs = &stm32f4_spi_regspec, |
| 1741 | .get_bpw_mask = stm32f4_spi_get_bpw_mask, |
| 1742 | .disable = stm32f4_spi_disable, |
| 1743 | .config = stm32f4_spi_config, |
| 1744 | .set_bpw = stm32f4_spi_set_bpw, |
| 1745 | .set_mode = stm32f4_spi_set_mode, |
| 1746 | .transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start, |
| 1747 | .dma_tx_cb = stm32f4_spi_dma_tx_cb, |
| 1748 | .dma_rx_cb = stm32f4_spi_dma_rx_cb, |
| 1749 | .transfer_one_irq = stm32f4_spi_transfer_one_irq, |
| 1750 | .irq_handler_event = stm32f4_spi_irq_event, |
| 1751 | .irq_handler_thread = stm32f4_spi_irq_thread, |
| 1752 | .baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN, |
| 1753 | .baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX, |
| 1754 | .has_fifo = false, |
| 1755 | }; |
| 1756 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1757 | static const struct stm32_spi_cfg stm32h7_spi_cfg = { |
| 1758 | .regs = &stm32h7_spi_regspec, |
| 1759 | .get_fifo_size = stm32h7_spi_get_fifo_size, |
| 1760 | .get_bpw_mask = stm32h7_spi_get_bpw_mask, |
| 1761 | .disable = stm32h7_spi_disable, |
| 1762 | .config = stm32h7_spi_config, |
| 1763 | .set_bpw = stm32h7_spi_set_bpw, |
| 1764 | .set_mode = stm32h7_spi_set_mode, |
| 1765 | .set_data_idleness = stm32h7_spi_data_idleness, |
| 1766 | .set_number_of_data = stm32h7_spi_number_of_data, |
| 1767 | .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start, |
| 1768 | .dma_rx_cb = stm32h7_spi_dma_cb, |
| 1769 | .dma_tx_cb = stm32h7_spi_dma_cb, |
| 1770 | .transfer_one_irq = stm32h7_spi_transfer_one_irq, |
| 1771 | .irq_handler_thread = stm32h7_spi_irq_thread, |
| 1772 | .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN, |
| 1773 | .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX, |
| 1774 | .has_fifo = true, |
| 1775 | }; |
| 1776 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1777 | static const struct of_device_id stm32_spi_of_match[] = { |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1778 | { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg }, |
Cezary Gapinski | 00505ed | 2018-12-24 23:00:38 +0100 | [diff] [blame] | 1779 | { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg }, |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1780 | {}, |
| 1781 | }; |
| 1782 | MODULE_DEVICE_TABLE(of, stm32_spi_of_match); |
| 1783 | |
| 1784 | static int stm32_spi_probe(struct platform_device *pdev) |
| 1785 | { |
| 1786 | struct spi_master *master; |
| 1787 | struct stm32_spi *spi; |
| 1788 | struct resource *res; |
Linus Walleij | 8a6553e | 2019-12-05 09:34:01 +0100 | [diff] [blame^] | 1789 | int ret; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1790 | |
| 1791 | master = spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi)); |
| 1792 | if (!master) { |
| 1793 | dev_err(&pdev->dev, "spi master allocation failed\n"); |
| 1794 | return -ENOMEM; |
| 1795 | } |
| 1796 | platform_set_drvdata(pdev, master); |
| 1797 | |
| 1798 | spi = spi_master_get_devdata(master); |
| 1799 | spi->dev = &pdev->dev; |
| 1800 | spi->master = master; |
| 1801 | spin_lock_init(&spi->lock); |
| 1802 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1803 | spi->cfg = (const struct stm32_spi_cfg *) |
| 1804 | of_match_device(pdev->dev.driver->of_match_table, |
| 1805 | &pdev->dev)->data; |
| 1806 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1807 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1808 | spi->base = devm_ioremap_resource(&pdev->dev, res); |
| 1809 | if (IS_ERR(spi->base)) { |
| 1810 | ret = PTR_ERR(spi->base); |
| 1811 | goto err_master_put; |
| 1812 | } |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1813 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1814 | spi->phys_addr = (dma_addr_t)res->start; |
| 1815 | |
| 1816 | spi->irq = platform_get_irq(pdev, 0); |
| 1817 | if (spi->irq <= 0) { |
Fabien Dessenne | 8d1467a | 2019-04-24 14:38:44 +0200 | [diff] [blame] | 1818 | ret = spi->irq; |
| 1819 | if (ret != -EPROBE_DEFER) |
| 1820 | dev_err(&pdev->dev, "failed to get irq: %d\n", ret); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1821 | goto err_master_put; |
| 1822 | } |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1823 | ret = devm_request_threaded_irq(&pdev->dev, spi->irq, |
| 1824 | spi->cfg->irq_handler_event, |
| 1825 | spi->cfg->irq_handler_thread, |
| 1826 | IRQF_ONESHOT, pdev->name, master); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1827 | if (ret) { |
| 1828 | dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq, |
| 1829 | ret); |
| 1830 | goto err_master_put; |
| 1831 | } |
| 1832 | |
Cezary Gapinski | d4c9134 | 2018-12-24 23:00:28 +0100 | [diff] [blame] | 1833 | spi->clk = devm_clk_get(&pdev->dev, NULL); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1834 | if (IS_ERR(spi->clk)) { |
| 1835 | ret = PTR_ERR(spi->clk); |
| 1836 | dev_err(&pdev->dev, "clk get failed: %d\n", ret); |
| 1837 | goto err_master_put; |
| 1838 | } |
| 1839 | |
| 1840 | ret = clk_prepare_enable(spi->clk); |
| 1841 | if (ret) { |
| 1842 | dev_err(&pdev->dev, "clk enable failed: %d\n", ret); |
| 1843 | goto err_master_put; |
| 1844 | } |
| 1845 | spi->clk_rate = clk_get_rate(spi->clk); |
| 1846 | if (!spi->clk_rate) { |
| 1847 | dev_err(&pdev->dev, "clk rate = 0\n"); |
| 1848 | ret = -EINVAL; |
Alexey Khoroshilov | 3dbb3ee | 2018-03-30 22:54:44 +0300 | [diff] [blame] | 1849 | goto err_clk_disable; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
Philipp Zabel | d5e9a4a | 2017-07-19 17:26:20 +0200 | [diff] [blame] | 1852 | spi->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1853 | if (!IS_ERR(spi->rst)) { |
| 1854 | reset_control_assert(spi->rst); |
| 1855 | udelay(2); |
| 1856 | reset_control_deassert(spi->rst); |
| 1857 | } |
| 1858 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1859 | if (spi->cfg->has_fifo) |
| 1860 | spi->fifo_size = spi->cfg->get_fifo_size(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1861 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1862 | ret = spi->cfg->config(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1863 | if (ret) { |
| 1864 | dev_err(&pdev->dev, "controller configuration failed: %d\n", |
| 1865 | ret); |
| 1866 | goto err_clk_disable; |
| 1867 | } |
| 1868 | |
| 1869 | master->dev.of_node = pdev->dev.of_node; |
| 1870 | master->auto_runtime_pm = true; |
| 1871 | master->bus_num = pdev->id; |
Cezary Gapinski | d6cea11 | 2018-12-24 23:00:31 +0100 | [diff] [blame] | 1872 | master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST | |
Cezary Gapinski | 6962b05 | 2018-12-24 23:00:32 +0100 | [diff] [blame] | 1873 | SPI_3WIRE; |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1874 | master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi); |
| 1875 | master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min; |
| 1876 | master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max; |
Linus Walleij | 8a6553e | 2019-12-05 09:34:01 +0100 | [diff] [blame^] | 1877 | master->use_gpio_descriptors = true; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1878 | master->prepare_message = stm32_spi_prepare_msg; |
| 1879 | master->transfer_one = stm32_spi_transfer_one; |
| 1880 | master->unprepare_message = stm32_spi_unprepare_msg; |
| 1881 | |
| 1882 | spi->dma_tx = dma_request_slave_channel(spi->dev, "tx"); |
| 1883 | if (!spi->dma_tx) |
| 1884 | dev_warn(&pdev->dev, "failed to request tx dma channel\n"); |
| 1885 | else |
| 1886 | master->dma_tx = spi->dma_tx; |
| 1887 | |
| 1888 | spi->dma_rx = dma_request_slave_channel(spi->dev, "rx"); |
| 1889 | if (!spi->dma_rx) |
| 1890 | dev_warn(&pdev->dev, "failed to request rx dma channel\n"); |
| 1891 | else |
| 1892 | master->dma_rx = spi->dma_rx; |
| 1893 | |
| 1894 | if (spi->dma_tx || spi->dma_rx) |
| 1895 | master->can_dma = stm32_spi_can_dma; |
| 1896 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1897 | pm_runtime_set_active(&pdev->dev); |
| 1898 | pm_runtime_enable(&pdev->dev); |
| 1899 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1900 | ret = devm_spi_register_master(&pdev->dev, master); |
| 1901 | if (ret) { |
| 1902 | dev_err(&pdev->dev, "spi master registration failed: %d\n", |
| 1903 | ret); |
| 1904 | goto err_dma_release; |
| 1905 | } |
| 1906 | |
Linus Walleij | 8a6553e | 2019-12-05 09:34:01 +0100 | [diff] [blame^] | 1907 | if (!master->cs_gpiods) { |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1908 | dev_err(&pdev->dev, "no CS gpios available\n"); |
| 1909 | ret = -EINVAL; |
| 1910 | goto err_dma_release; |
| 1911 | } |
| 1912 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1913 | dev_info(&pdev->dev, "driver initialized\n"); |
| 1914 | |
| 1915 | return 0; |
| 1916 | |
| 1917 | err_dma_release: |
| 1918 | if (spi->dma_tx) |
| 1919 | dma_release_channel(spi->dma_tx); |
| 1920 | if (spi->dma_rx) |
| 1921 | dma_release_channel(spi->dma_rx); |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1922 | |
| 1923 | pm_runtime_disable(&pdev->dev); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1924 | err_clk_disable: |
| 1925 | clk_disable_unprepare(spi->clk); |
| 1926 | err_master_put: |
| 1927 | spi_master_put(master); |
| 1928 | |
| 1929 | return ret; |
| 1930 | } |
| 1931 | |
| 1932 | static int stm32_spi_remove(struct platform_device *pdev) |
| 1933 | { |
| 1934 | struct spi_master *master = platform_get_drvdata(pdev); |
| 1935 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1936 | |
Cezary Gapinski | 5516685 | 2018-12-24 23:00:37 +0100 | [diff] [blame] | 1937 | spi->cfg->disable(spi); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (master->dma_tx) |
| 1940 | dma_release_channel(master->dma_tx); |
| 1941 | if (master->dma_rx) |
| 1942 | dma_release_channel(master->dma_rx); |
| 1943 | |
| 1944 | clk_disable_unprepare(spi->clk); |
| 1945 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1946 | pm_runtime_disable(&pdev->dev); |
| 1947 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1951 | #ifdef CONFIG_PM |
| 1952 | static int stm32_spi_runtime_suspend(struct device *dev) |
| 1953 | { |
| 1954 | struct spi_master *master = dev_get_drvdata(dev); |
| 1955 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1956 | |
| 1957 | clk_disable_unprepare(spi->clk); |
| 1958 | |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static int stm32_spi_runtime_resume(struct device *dev) |
| 1963 | { |
| 1964 | struct spi_master *master = dev_get_drvdata(dev); |
| 1965 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1966 | |
| 1967 | return clk_prepare_enable(spi->clk); |
| 1968 | } |
| 1969 | #endif |
| 1970 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1971 | #ifdef CONFIG_PM_SLEEP |
| 1972 | static int stm32_spi_suspend(struct device *dev) |
| 1973 | { |
| 1974 | struct spi_master *master = dev_get_drvdata(dev); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1975 | int ret; |
| 1976 | |
| 1977 | ret = spi_master_suspend(master); |
| 1978 | if (ret) |
| 1979 | return ret; |
| 1980 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1981 | return pm_runtime_force_suspend(dev); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static int stm32_spi_resume(struct device *dev) |
| 1985 | { |
| 1986 | struct spi_master *master = dev_get_drvdata(dev); |
| 1987 | struct stm32_spi *spi = spi_master_get_devdata(master); |
| 1988 | int ret; |
| 1989 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1990 | ret = pm_runtime_force_resume(dev); |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1991 | if (ret) |
| 1992 | return ret; |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 1993 | |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 1994 | ret = spi_master_resume(master); |
| 1995 | if (ret) |
| 1996 | clk_disable_unprepare(spi->clk); |
| 1997 | |
| 1998 | return ret; |
| 1999 | } |
| 2000 | #endif |
| 2001 | |
Amelie Delaunay | 038ac86 | 2017-06-27 17:45:18 +0200 | [diff] [blame] | 2002 | static const struct dev_pm_ops stm32_spi_pm_ops = { |
| 2003 | SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume) |
| 2004 | SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend, |
| 2005 | stm32_spi_runtime_resume, NULL) |
| 2006 | }; |
Amelie Delaunay | dcbe0d8 | 2017-06-21 16:32:06 +0200 | [diff] [blame] | 2007 | |
| 2008 | static struct platform_driver stm32_spi_driver = { |
| 2009 | .probe = stm32_spi_probe, |
| 2010 | .remove = stm32_spi_remove, |
| 2011 | .driver = { |
| 2012 | .name = DRIVER_NAME, |
| 2013 | .pm = &stm32_spi_pm_ops, |
| 2014 | .of_match_table = stm32_spi_of_match, |
| 2015 | }, |
| 2016 | }; |
| 2017 | |
| 2018 | module_platform_driver(stm32_spi_driver); |
| 2019 | |
| 2020 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 2021 | MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver"); |
| 2022 | MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>"); |
| 2023 | MODULE_LICENSE("GPL v2"); |