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