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