Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Copyright 2013 Freescale Semiconductor, Inc. |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4 | // Copyright 2020 NXP |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | // |
| 6 | // Freescale DSPI driver |
| 7 | // This file contains a driver for the Freescale DSPI |
| 8 | |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/dmaengine.h> |
| 12 | #include <linux/dma-mapping.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | #include <linux/module.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | #include <linux/of_device.h> |
| 17 | #include <linux/pinctrl/consumer.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | #include <linux/spi/spi.h> |
| 20 | #include <linux/spi/spi-fsl-dspi.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | #define DRIVER_NAME "fsl-dspi" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_M5441x |
| 25 | #define DSPI_FIFO_SIZE 16 |
| 26 | #else |
| 27 | #define DSPI_FIFO_SIZE 4 |
| 28 | #endif |
| 29 | #define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024) |
| 30 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 31 | #define SPI_MCR 0x00 |
| 32 | #define SPI_MCR_MASTER BIT(31) |
| 33 | #define SPI_MCR_PCSIS (0x3F << 16) |
| 34 | #define SPI_MCR_CLR_TXF BIT(11) |
| 35 | #define SPI_MCR_CLR_RXF BIT(10) |
| 36 | #define SPI_MCR_XSPI BIT(3) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 37 | #define SPI_MCR_DIS_TXF BIT(13) |
| 38 | #define SPI_MCR_DIS_RXF BIT(12) |
| 39 | #define SPI_MCR_HALT BIT(0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 40 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 41 | #define SPI_TCR 0x08 |
| 42 | #define SPI_TCR_GET_TCNT(x) (((x) & GENMASK(31, 16)) >> 16) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 43 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 44 | #define SPI_CTAR(x) (0x0c + (((x) & GENMASK(1, 0)) * 4)) |
| 45 | #define SPI_CTAR_FMSZ(x) (((x) << 27) & GENMASK(30, 27)) |
| 46 | #define SPI_CTAR_CPOL BIT(26) |
| 47 | #define SPI_CTAR_CPHA BIT(25) |
| 48 | #define SPI_CTAR_LSBFE BIT(24) |
| 49 | #define SPI_CTAR_PCSSCK(x) (((x) << 22) & GENMASK(23, 22)) |
| 50 | #define SPI_CTAR_PASC(x) (((x) << 20) & GENMASK(21, 20)) |
| 51 | #define SPI_CTAR_PDT(x) (((x) << 18) & GENMASK(19, 18)) |
| 52 | #define SPI_CTAR_PBR(x) (((x) << 16) & GENMASK(17, 16)) |
| 53 | #define SPI_CTAR_CSSCK(x) (((x) << 12) & GENMASK(15, 12)) |
| 54 | #define SPI_CTAR_ASC(x) (((x) << 8) & GENMASK(11, 8)) |
| 55 | #define SPI_CTAR_DT(x) (((x) << 4) & GENMASK(7, 4)) |
| 56 | #define SPI_CTAR_BR(x) ((x) & GENMASK(3, 0)) |
| 57 | #define SPI_CTAR_SCALE_BITS 0xf |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 59 | #define SPI_CTAR0_SLAVE 0x0c |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 61 | #define SPI_SR 0x2c |
| 62 | #define SPI_SR_TCFQF BIT(31) |
| 63 | #define SPI_SR_EOQF BIT(28) |
| 64 | #define SPI_SR_TFUF BIT(27) |
| 65 | #define SPI_SR_TFFF BIT(25) |
| 66 | #define SPI_SR_CMDTCF BIT(23) |
| 67 | #define SPI_SR_SPEF BIT(21) |
| 68 | #define SPI_SR_RFOF BIT(19) |
| 69 | #define SPI_SR_TFIWF BIT(18) |
| 70 | #define SPI_SR_RFDF BIT(17) |
| 71 | #define SPI_SR_CMDFFF BIT(16) |
| 72 | #define SPI_SR_CLEAR (SPI_SR_TCFQF | SPI_SR_EOQF | \ |
| 73 | SPI_SR_TFUF | SPI_SR_TFFF | \ |
| 74 | SPI_SR_CMDTCF | SPI_SR_SPEF | \ |
| 75 | SPI_SR_RFOF | SPI_SR_TFIWF | \ |
| 76 | SPI_SR_RFDF | SPI_SR_CMDFFF) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 78 | #define SPI_RSER_TFFFE BIT(25) |
| 79 | #define SPI_RSER_TFFFD BIT(24) |
| 80 | #define SPI_RSER_RFDFE BIT(17) |
| 81 | #define SPI_RSER_RFDFD BIT(16) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 82 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 83 | #define SPI_RSER 0x30 |
| 84 | #define SPI_RSER_TCFQE BIT(31) |
| 85 | #define SPI_RSER_EOQFE BIT(28) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 87 | #define SPI_PUSHR 0x34 |
| 88 | #define SPI_PUSHR_CMD_CONT BIT(15) |
| 89 | #define SPI_PUSHR_CMD_CTAS(x) (((x) << 12 & GENMASK(14, 12))) |
| 90 | #define SPI_PUSHR_CMD_EOQ BIT(11) |
| 91 | #define SPI_PUSHR_CMD_CTCNT BIT(10) |
| 92 | #define SPI_PUSHR_CMD_PCS(x) (BIT(x) & GENMASK(5, 0)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 93 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 94 | #define SPI_PUSHR_SLAVE 0x34 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 95 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 96 | #define SPI_POPR 0x38 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 98 | #define SPI_TXFR0 0x3c |
| 99 | #define SPI_TXFR1 0x40 |
| 100 | #define SPI_TXFR2 0x44 |
| 101 | #define SPI_TXFR3 0x48 |
| 102 | #define SPI_RXFR0 0x7c |
| 103 | #define SPI_RXFR1 0x80 |
| 104 | #define SPI_RXFR2 0x84 |
| 105 | #define SPI_RXFR3 0x88 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 106 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 107 | #define SPI_CTARE(x) (0x11c + (((x) & GENMASK(1, 0)) * 4)) |
| 108 | #define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16) |
| 109 | #define SPI_CTARE_DTCP(x) ((x) & 0x7ff) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 111 | #define SPI_SREX 0x13c |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 113 | #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1) |
| 114 | #define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 115 | |
| 116 | /* Register offsets for regmap_pushr */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 117 | #define PUSHR_CMD 0x0 |
| 118 | #define PUSHR_TX 0x2 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 119 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 120 | #define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | |
| 122 | struct chip_data { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 123 | u32 ctar_val; |
| 124 | u16 void_write_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | enum dspi_trans_mode { |
| 128 | DSPI_EOQ_MODE = 0, |
| 129 | DSPI_TCFQ_MODE, |
| 130 | DSPI_DMA_MODE, |
| 131 | }; |
| 132 | |
| 133 | struct fsl_dspi_devtype_data { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 134 | enum dspi_trans_mode trans_mode; |
| 135 | u8 max_clock_factor; |
| 136 | bool xspi_mode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | static const struct fsl_dspi_devtype_data vf610_data = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 140 | .trans_mode = DSPI_DMA_MODE, |
| 141 | .max_clock_factor = 2, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | static const struct fsl_dspi_devtype_data ls1021a_v1_data = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 145 | .trans_mode = DSPI_TCFQ_MODE, |
| 146 | .max_clock_factor = 8, |
| 147 | .xspi_mode = true, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static const struct fsl_dspi_devtype_data ls2085a_data = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 151 | .trans_mode = DSPI_TCFQ_MODE, |
| 152 | .max_clock_factor = 8, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | static const struct fsl_dspi_devtype_data coldfire_data = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 156 | .trans_mode = DSPI_EOQ_MODE, |
| 157 | .max_clock_factor = 8, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct fsl_dspi_dma { |
| 161 | /* Length of transfer in words of DSPI_FIFO_SIZE */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 162 | u32 curr_xfer_len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 163 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 164 | u32 *tx_dma_buf; |
| 165 | struct dma_chan *chan_tx; |
| 166 | dma_addr_t tx_dma_phys; |
| 167 | struct completion cmd_tx_complete; |
| 168 | struct dma_async_tx_descriptor *tx_desc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 169 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 170 | u32 *rx_dma_buf; |
| 171 | struct dma_chan *chan_rx; |
| 172 | dma_addr_t rx_dma_phys; |
| 173 | struct completion cmd_rx_complete; |
| 174 | struct dma_async_tx_descriptor *rx_desc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | struct fsl_dspi { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 178 | struct spi_controller *ctlr; |
| 179 | struct platform_device *pdev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 181 | struct regmap *regmap; |
| 182 | struct regmap *regmap_pushr; |
| 183 | int irq; |
| 184 | struct clk *clk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 185 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 186 | struct spi_transfer *cur_transfer; |
| 187 | struct spi_message *cur_msg; |
| 188 | struct chip_data *cur_chip; |
| 189 | size_t len; |
| 190 | const void *tx; |
| 191 | void *rx; |
| 192 | void *rx_end; |
| 193 | u16 void_write_data; |
| 194 | u16 tx_cmd; |
| 195 | u8 bits_per_word; |
| 196 | u8 bytes_per_word; |
| 197 | const struct fsl_dspi_devtype_data *devtype_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 198 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 199 | struct completion xfer_done; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 200 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 201 | struct fsl_dspi_dma *dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static u32 dspi_pop_tx(struct fsl_dspi *dspi) |
| 205 | { |
| 206 | u32 txdata = 0; |
| 207 | |
| 208 | if (dspi->tx) { |
| 209 | if (dspi->bytes_per_word == 1) |
| 210 | txdata = *(u8 *)dspi->tx; |
| 211 | else if (dspi->bytes_per_word == 2) |
| 212 | txdata = *(u16 *)dspi->tx; |
| 213 | else /* dspi->bytes_per_word == 4 */ |
| 214 | txdata = *(u32 *)dspi->tx; |
| 215 | dspi->tx += dspi->bytes_per_word; |
| 216 | } |
| 217 | dspi->len -= dspi->bytes_per_word; |
| 218 | return txdata; |
| 219 | } |
| 220 | |
| 221 | static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi) |
| 222 | { |
| 223 | u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi); |
| 224 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | if (spi_controller_is_slave(dspi->ctlr)) |
| 226 | return data; |
| 227 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 228 | if (dspi->len > 0) |
| 229 | cmd |= SPI_PUSHR_CMD_CONT; |
| 230 | return cmd << 16 | data; |
| 231 | } |
| 232 | |
| 233 | static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata) |
| 234 | { |
| 235 | if (!dspi->rx) |
| 236 | return; |
| 237 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 238 | /* Mask off undefined bits */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 239 | rxdata &= (1 << dspi->bits_per_word) - 1; |
| 240 | |
| 241 | if (dspi->bytes_per_word == 1) |
| 242 | *(u8 *)dspi->rx = rxdata; |
| 243 | else if (dspi->bytes_per_word == 2) |
| 244 | *(u16 *)dspi->rx = rxdata; |
| 245 | else /* dspi->bytes_per_word == 4 */ |
| 246 | *(u32 *)dspi->rx = rxdata; |
| 247 | dspi->rx += dspi->bytes_per_word; |
| 248 | } |
| 249 | |
| 250 | static void dspi_tx_dma_callback(void *arg) |
| 251 | { |
| 252 | struct fsl_dspi *dspi = arg; |
| 253 | struct fsl_dspi_dma *dma = dspi->dma; |
| 254 | |
| 255 | complete(&dma->cmd_tx_complete); |
| 256 | } |
| 257 | |
| 258 | static void dspi_rx_dma_callback(void *arg) |
| 259 | { |
| 260 | struct fsl_dspi *dspi = arg; |
| 261 | struct fsl_dspi_dma *dma = dspi->dma; |
| 262 | int i; |
| 263 | |
| 264 | if (dspi->rx) { |
| 265 | for (i = 0; i < dma->curr_xfer_len; i++) |
| 266 | dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]); |
| 267 | } |
| 268 | |
| 269 | complete(&dma->cmd_rx_complete); |
| 270 | } |
| 271 | |
| 272 | static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi) |
| 273 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 274 | struct device *dev = &dspi->pdev->dev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 275 | struct fsl_dspi_dma *dma = dspi->dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 276 | int time_left; |
| 277 | int i; |
| 278 | |
| 279 | for (i = 0; i < dma->curr_xfer_len; i++) |
| 280 | dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi); |
| 281 | |
| 282 | dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx, |
| 283 | dma->tx_dma_phys, |
| 284 | dma->curr_xfer_len * |
| 285 | DMA_SLAVE_BUSWIDTH_4_BYTES, |
| 286 | DMA_MEM_TO_DEV, |
| 287 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 288 | if (!dma->tx_desc) { |
| 289 | dev_err(dev, "Not able to get desc for DMA xfer\n"); |
| 290 | return -EIO; |
| 291 | } |
| 292 | |
| 293 | dma->tx_desc->callback = dspi_tx_dma_callback; |
| 294 | dma->tx_desc->callback_param = dspi; |
| 295 | if (dma_submit_error(dmaengine_submit(dma->tx_desc))) { |
| 296 | dev_err(dev, "DMA submit failed\n"); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx, |
| 301 | dma->rx_dma_phys, |
| 302 | dma->curr_xfer_len * |
| 303 | DMA_SLAVE_BUSWIDTH_4_BYTES, |
| 304 | DMA_DEV_TO_MEM, |
| 305 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 306 | if (!dma->rx_desc) { |
| 307 | dev_err(dev, "Not able to get desc for DMA xfer\n"); |
| 308 | return -EIO; |
| 309 | } |
| 310 | |
| 311 | dma->rx_desc->callback = dspi_rx_dma_callback; |
| 312 | dma->rx_desc->callback_param = dspi; |
| 313 | if (dma_submit_error(dmaengine_submit(dma->rx_desc))) { |
| 314 | dev_err(dev, "DMA submit failed\n"); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | |
| 318 | reinit_completion(&dspi->dma->cmd_rx_complete); |
| 319 | reinit_completion(&dspi->dma->cmd_tx_complete); |
| 320 | |
| 321 | dma_async_issue_pending(dma->chan_rx); |
| 322 | dma_async_issue_pending(dma->chan_tx); |
| 323 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 324 | if (spi_controller_is_slave(dspi->ctlr)) { |
| 325 | wait_for_completion_interruptible(&dspi->dma->cmd_rx_complete); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 329 | time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 330 | DMA_COMPLETION_TIMEOUT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 331 | if (time_left == 0) { |
| 332 | dev_err(dev, "DMA tx timeout\n"); |
| 333 | dmaengine_terminate_all(dma->chan_tx); |
| 334 | dmaengine_terminate_all(dma->chan_rx); |
| 335 | return -ETIMEDOUT; |
| 336 | } |
| 337 | |
| 338 | time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 339 | DMA_COMPLETION_TIMEOUT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 340 | if (time_left == 0) { |
| 341 | dev_err(dev, "DMA rx timeout\n"); |
| 342 | dmaengine_terminate_all(dma->chan_tx); |
| 343 | dmaengine_terminate_all(dma->chan_rx); |
| 344 | return -ETIMEDOUT; |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static int dspi_dma_xfer(struct fsl_dspi *dspi) |
| 351 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 352 | struct spi_message *message = dspi->cur_msg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 353 | struct device *dev = &dspi->pdev->dev; |
| 354 | struct fsl_dspi_dma *dma = dspi->dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 355 | int curr_remaining_bytes; |
| 356 | int bytes_per_buffer; |
| 357 | int ret = 0; |
| 358 | |
| 359 | curr_remaining_bytes = dspi->len; |
| 360 | bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE; |
| 361 | while (curr_remaining_bytes) { |
| 362 | /* Check if current transfer fits the DMA buffer */ |
| 363 | dma->curr_xfer_len = curr_remaining_bytes |
| 364 | / dspi->bytes_per_word; |
| 365 | if (dma->curr_xfer_len > bytes_per_buffer) |
| 366 | dma->curr_xfer_len = bytes_per_buffer; |
| 367 | |
| 368 | ret = dspi_next_xfer_dma_submit(dspi); |
| 369 | if (ret) { |
| 370 | dev_err(dev, "DMA transfer failed\n"); |
| 371 | goto exit; |
| 372 | |
| 373 | } else { |
| 374 | const int len = |
| 375 | dma->curr_xfer_len * dspi->bytes_per_word; |
| 376 | curr_remaining_bytes -= len; |
| 377 | message->actual_length += len; |
| 378 | if (curr_remaining_bytes < 0) |
| 379 | curr_remaining_bytes = 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | exit: |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr) |
| 388 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 389 | struct device *dev = &dspi->pdev->dev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 390 | struct dma_slave_config cfg; |
| 391 | struct fsl_dspi_dma *dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 392 | int ret; |
| 393 | |
| 394 | dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL); |
| 395 | if (!dma) |
| 396 | return -ENOMEM; |
| 397 | |
| 398 | dma->chan_rx = dma_request_slave_channel(dev, "rx"); |
| 399 | if (!dma->chan_rx) { |
| 400 | dev_err(dev, "rx dma channel not available\n"); |
| 401 | ret = -ENODEV; |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | dma->chan_tx = dma_request_slave_channel(dev, "tx"); |
| 406 | if (!dma->chan_tx) { |
| 407 | dev_err(dev, "tx dma channel not available\n"); |
| 408 | ret = -ENODEV; |
| 409 | goto err_tx_channel; |
| 410 | } |
| 411 | |
| 412 | dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 413 | &dma->tx_dma_phys, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 414 | if (!dma->tx_dma_buf) { |
| 415 | ret = -ENOMEM; |
| 416 | goto err_tx_dma_buf; |
| 417 | } |
| 418 | |
| 419 | dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 420 | &dma->rx_dma_phys, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 421 | if (!dma->rx_dma_buf) { |
| 422 | ret = -ENOMEM; |
| 423 | goto err_rx_dma_buf; |
| 424 | } |
| 425 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 426 | memset(&cfg, 0, sizeof(cfg)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 427 | cfg.src_addr = phy_addr + SPI_POPR; |
| 428 | cfg.dst_addr = phy_addr + SPI_PUSHR; |
| 429 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 430 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 431 | cfg.src_maxburst = 1; |
| 432 | cfg.dst_maxburst = 1; |
| 433 | |
| 434 | cfg.direction = DMA_DEV_TO_MEM; |
| 435 | ret = dmaengine_slave_config(dma->chan_rx, &cfg); |
| 436 | if (ret) { |
| 437 | dev_err(dev, "can't configure rx dma channel\n"); |
| 438 | ret = -EINVAL; |
| 439 | goto err_slave_config; |
| 440 | } |
| 441 | |
| 442 | cfg.direction = DMA_MEM_TO_DEV; |
| 443 | ret = dmaengine_slave_config(dma->chan_tx, &cfg); |
| 444 | if (ret) { |
| 445 | dev_err(dev, "can't configure tx dma channel\n"); |
| 446 | ret = -EINVAL; |
| 447 | goto err_slave_config; |
| 448 | } |
| 449 | |
| 450 | dspi->dma = dma; |
| 451 | init_completion(&dma->cmd_tx_complete); |
| 452 | init_completion(&dma->cmd_rx_complete); |
| 453 | |
| 454 | return 0; |
| 455 | |
| 456 | err_slave_config: |
| 457 | dma_free_coherent(dev, DSPI_DMA_BUFSIZE, |
| 458 | dma->rx_dma_buf, dma->rx_dma_phys); |
| 459 | err_rx_dma_buf: |
| 460 | dma_free_coherent(dev, DSPI_DMA_BUFSIZE, |
| 461 | dma->tx_dma_buf, dma->tx_dma_phys); |
| 462 | err_tx_dma_buf: |
| 463 | dma_release_channel(dma->chan_tx); |
| 464 | err_tx_channel: |
| 465 | dma_release_channel(dma->chan_rx); |
| 466 | |
| 467 | devm_kfree(dev, dma); |
| 468 | dspi->dma = NULL; |
| 469 | |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | static void dspi_release_dma(struct fsl_dspi *dspi) |
| 474 | { |
| 475 | struct fsl_dspi_dma *dma = dspi->dma; |
| 476 | struct device *dev = &dspi->pdev->dev; |
| 477 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 478 | if (!dma) |
| 479 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 480 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 481 | if (dma->chan_tx) { |
| 482 | dma_unmap_single(dev, dma->tx_dma_phys, |
| 483 | DSPI_DMA_BUFSIZE, DMA_TO_DEVICE); |
| 484 | dma_release_channel(dma->chan_tx); |
| 485 | } |
| 486 | |
| 487 | if (dma->chan_rx) { |
| 488 | dma_unmap_single(dev, dma->rx_dma_phys, |
| 489 | DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE); |
| 490 | dma_release_channel(dma->chan_rx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | static void hz_to_spi_baud(char *pbr, char *br, int speed_hz, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 495 | unsigned long clkrate) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 496 | { |
| 497 | /* Valid baud rate pre-scaler values */ |
| 498 | int pbr_tbl[4] = {2, 3, 5, 7}; |
| 499 | int brs[16] = { 2, 4, 6, 8, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 500 | 16, 32, 64, 128, |
| 501 | 256, 512, 1024, 2048, |
| 502 | 4096, 8192, 16384, 32768 }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 503 | int scale_needed, scale, minscale = INT_MAX; |
| 504 | int i, j; |
| 505 | |
| 506 | scale_needed = clkrate / speed_hz; |
| 507 | if (clkrate % speed_hz) |
| 508 | scale_needed++; |
| 509 | |
| 510 | for (i = 0; i < ARRAY_SIZE(brs); i++) |
| 511 | for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) { |
| 512 | scale = brs[i] * pbr_tbl[j]; |
| 513 | if (scale >= scale_needed) { |
| 514 | if (scale < minscale) { |
| 515 | minscale = scale; |
| 516 | *br = i; |
| 517 | *pbr = j; |
| 518 | } |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (minscale == INT_MAX) { |
| 524 | pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n", |
| 525 | speed_hz, clkrate); |
| 526 | *pbr = ARRAY_SIZE(pbr_tbl) - 1; |
| 527 | *br = ARRAY_SIZE(brs) - 1; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static void ns_delay_scale(char *psc, char *sc, int delay_ns, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 532 | unsigned long clkrate) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 533 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 534 | int scale_needed, scale, minscale = INT_MAX; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 535 | int pscale_tbl[4] = {1, 3, 5, 7}; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | u32 remainder; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 537 | int i, j; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 538 | |
| 539 | scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 540 | &remainder); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 541 | if (remainder) |
| 542 | scale_needed++; |
| 543 | |
| 544 | for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++) |
| 545 | for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) { |
| 546 | scale = pscale_tbl[i] * (2 << j); |
| 547 | if (scale >= scale_needed) { |
| 548 | if (scale < minscale) { |
| 549 | minscale = scale; |
| 550 | *psc = i; |
| 551 | *sc = j; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (minscale == INT_MAX) { |
| 558 | pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value", |
| 559 | delay_ns, clkrate); |
| 560 | *psc = ARRAY_SIZE(pscale_tbl) - 1; |
| 561 | *sc = SPI_CTAR_SCALE_BITS; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static void fifo_write(struct fsl_dspi *dspi) |
| 566 | { |
| 567 | regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi)); |
| 568 | } |
| 569 | |
| 570 | static void cmd_fifo_write(struct fsl_dspi *dspi) |
| 571 | { |
| 572 | u16 cmd = dspi->tx_cmd; |
| 573 | |
| 574 | if (dspi->len > 0) |
| 575 | cmd |= SPI_PUSHR_CMD_CONT; |
| 576 | regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd); |
| 577 | } |
| 578 | |
| 579 | static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata) |
| 580 | { |
| 581 | regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata); |
| 582 | } |
| 583 | |
| 584 | static void dspi_tcfq_write(struct fsl_dspi *dspi) |
| 585 | { |
| 586 | /* Clear transfer count */ |
| 587 | dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT; |
| 588 | |
| 589 | if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 590 | /* Write the CMD FIFO entry first, and then the two |
| 591 | * corresponding TX FIFO entries. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 592 | */ |
| 593 | u32 data = dspi_pop_tx(dspi); |
| 594 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 595 | cmd_fifo_write(dspi); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 596 | tx_fifo_write(dspi, data & 0xFFFF); |
| 597 | tx_fifo_write(dspi, data >> 16); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 598 | } else { |
| 599 | /* Write one entry to both TX FIFO and CMD FIFO |
| 600 | * simultaneously. |
| 601 | */ |
| 602 | fifo_write(dspi); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | static u32 fifo_read(struct fsl_dspi *dspi) |
| 607 | { |
| 608 | u32 rxdata = 0; |
| 609 | |
| 610 | regmap_read(dspi->regmap, SPI_POPR, &rxdata); |
| 611 | return rxdata; |
| 612 | } |
| 613 | |
| 614 | static void dspi_tcfq_read(struct fsl_dspi *dspi) |
| 615 | { |
| 616 | dspi_push_rx(dspi, fifo_read(dspi)); |
| 617 | } |
| 618 | |
| 619 | static void dspi_eoq_write(struct fsl_dspi *dspi) |
| 620 | { |
| 621 | int fifo_size = DSPI_FIFO_SIZE; |
| 622 | u16 xfer_cmd = dspi->tx_cmd; |
| 623 | |
| 624 | /* Fill TX FIFO with as many transfers as possible */ |
| 625 | while (dspi->len && fifo_size--) { |
| 626 | dspi->tx_cmd = xfer_cmd; |
| 627 | /* Request EOQF for last transfer in FIFO */ |
| 628 | if (dspi->len == dspi->bytes_per_word || fifo_size == 0) |
| 629 | dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ; |
| 630 | /* Clear transfer count for first transfer in FIFO */ |
| 631 | if (fifo_size == (DSPI_FIFO_SIZE - 1)) |
| 632 | dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT; |
| 633 | /* Write combined TX FIFO and CMD FIFO entry */ |
| 634 | fifo_write(dspi); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static void dspi_eoq_read(struct fsl_dspi *dspi) |
| 639 | { |
| 640 | int fifo_size = DSPI_FIFO_SIZE; |
| 641 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 642 | /* Read one FIFO entry and push to rx buffer */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 643 | while ((dspi->rx < dspi->rx_end) && fifo_size--) |
| 644 | dspi_push_rx(dspi, fifo_read(dspi)); |
| 645 | } |
| 646 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 647 | static int dspi_rxtx(struct fsl_dspi *dspi) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 648 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 649 | struct spi_message *msg = dspi->cur_msg; |
| 650 | enum dspi_trans_mode trans_mode; |
| 651 | u16 spi_tcnt; |
| 652 | u32 spi_tcr; |
| 653 | |
| 654 | /* Get transfer counter (in number of SPI transfers). It was |
| 655 | * reset to 0 when transfer(s) were started. |
| 656 | */ |
| 657 | regmap_read(dspi->regmap, SPI_TCR, &spi_tcr); |
| 658 | spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr); |
| 659 | /* Update total number of bytes that were transferred */ |
| 660 | msg->actual_length += spi_tcnt * dspi->bytes_per_word; |
| 661 | |
| 662 | trans_mode = dspi->devtype_data->trans_mode; |
| 663 | if (trans_mode == DSPI_EOQ_MODE) |
| 664 | dspi_eoq_read(dspi); |
| 665 | else if (trans_mode == DSPI_TCFQ_MODE) |
| 666 | dspi_tcfq_read(dspi); |
| 667 | |
| 668 | if (!dspi->len) |
| 669 | /* Success! */ |
| 670 | return 0; |
| 671 | |
| 672 | if (trans_mode == DSPI_EOQ_MODE) |
| 673 | dspi_eoq_write(dspi); |
| 674 | else if (trans_mode == DSPI_TCFQ_MODE) |
| 675 | dspi_tcfq_write(dspi); |
| 676 | |
| 677 | return -EINPROGRESS; |
| 678 | } |
| 679 | |
| 680 | static int dspi_poll(struct fsl_dspi *dspi) |
| 681 | { |
| 682 | int tries = 1000; |
| 683 | u32 spi_sr; |
| 684 | |
| 685 | do { |
| 686 | regmap_read(dspi->regmap, SPI_SR, &spi_sr); |
| 687 | regmap_write(dspi->regmap, SPI_SR, spi_sr); |
| 688 | |
| 689 | if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) |
| 690 | break; |
| 691 | } while (--tries); |
| 692 | |
| 693 | if (!tries) |
| 694 | return -ETIMEDOUT; |
| 695 | |
| 696 | return dspi_rxtx(dspi); |
| 697 | } |
| 698 | |
| 699 | static irqreturn_t dspi_interrupt(int irq, void *dev_id) |
| 700 | { |
| 701 | struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id; |
| 702 | u32 spi_sr; |
| 703 | |
| 704 | regmap_read(dspi->regmap, SPI_SR, &spi_sr); |
| 705 | regmap_write(dspi->regmap, SPI_SR, spi_sr); |
| 706 | |
| 707 | if (!(spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF))) |
| 708 | return IRQ_NONE; |
| 709 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 710 | if (dspi_rxtx(dspi) == 0) |
| 711 | complete(&dspi->xfer_done); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 712 | |
| 713 | return IRQ_HANDLED; |
| 714 | } |
| 715 | |
| 716 | static int dspi_transfer_one_message(struct spi_controller *ctlr, |
| 717 | struct spi_message *message) |
| 718 | { |
| 719 | struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 720 | struct spi_device *spi = message->spi; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 721 | enum dspi_trans_mode trans_mode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 722 | struct spi_transfer *transfer; |
| 723 | int status = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 724 | |
| 725 | message->actual_length = 0; |
| 726 | |
| 727 | list_for_each_entry(transfer, &message->transfers, transfer_list) { |
| 728 | dspi->cur_transfer = transfer; |
| 729 | dspi->cur_msg = message; |
| 730 | dspi->cur_chip = spi_get_ctldata(spi); |
| 731 | /* Prepare command word for CMD FIFO */ |
| 732 | dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 733 | SPI_PUSHR_CMD_PCS(spi->chip_select); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 734 | if (list_is_last(&dspi->cur_transfer->transfer_list, |
| 735 | &dspi->cur_msg->transfers)) { |
| 736 | /* Leave PCS activated after last transfer when |
| 737 | * cs_change is set. |
| 738 | */ |
| 739 | if (transfer->cs_change) |
| 740 | dspi->tx_cmd |= SPI_PUSHR_CMD_CONT; |
| 741 | } else { |
| 742 | /* Keep PCS active between transfers in same message |
| 743 | * when cs_change is not set, and de-activate PCS |
| 744 | * between transfers in the same message when |
| 745 | * cs_change is set. |
| 746 | */ |
| 747 | if (!transfer->cs_change) |
| 748 | dspi->tx_cmd |= SPI_PUSHR_CMD_CONT; |
| 749 | } |
| 750 | |
| 751 | dspi->void_write_data = dspi->cur_chip->void_write_data; |
| 752 | |
| 753 | dspi->tx = transfer->tx_buf; |
| 754 | dspi->rx = transfer->rx_buf; |
| 755 | dspi->rx_end = dspi->rx + transfer->len; |
| 756 | dspi->len = transfer->len; |
| 757 | /* Validated transfer specific frame size (defaults applied) */ |
| 758 | dspi->bits_per_word = transfer->bits_per_word; |
| 759 | if (transfer->bits_per_word <= 8) |
| 760 | dspi->bytes_per_word = 1; |
| 761 | else if (transfer->bits_per_word <= 16) |
| 762 | dspi->bytes_per_word = 2; |
| 763 | else |
| 764 | dspi->bytes_per_word = 4; |
| 765 | |
| 766 | regmap_update_bits(dspi->regmap, SPI_MCR, |
| 767 | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF, |
| 768 | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF); |
| 769 | regmap_write(dspi->regmap, SPI_CTAR(0), |
| 770 | dspi->cur_chip->ctar_val | |
| 771 | SPI_FRAME_BITS(transfer->bits_per_word)); |
| 772 | if (dspi->devtype_data->xspi_mode) |
| 773 | regmap_write(dspi->regmap, SPI_CTARE(0), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 774 | SPI_FRAME_EBITS(transfer->bits_per_word) | |
| 775 | SPI_CTARE_DTCP(1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 776 | |
| 777 | trans_mode = dspi->devtype_data->trans_mode; |
| 778 | switch (trans_mode) { |
| 779 | case DSPI_EOQ_MODE: |
| 780 | regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE); |
| 781 | dspi_eoq_write(dspi); |
| 782 | break; |
| 783 | case DSPI_TCFQ_MODE: |
| 784 | regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE); |
| 785 | dspi_tcfq_write(dspi); |
| 786 | break; |
| 787 | case DSPI_DMA_MODE: |
| 788 | regmap_write(dspi->regmap, SPI_RSER, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 789 | SPI_RSER_TFFFE | SPI_RSER_TFFFD | |
| 790 | SPI_RSER_RFDFE | SPI_RSER_RFDFD); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 791 | status = dspi_dma_xfer(dspi); |
| 792 | break; |
| 793 | default: |
| 794 | dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n", |
| 795 | trans_mode); |
| 796 | status = -EINVAL; |
| 797 | goto out; |
| 798 | } |
| 799 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 800 | if (!dspi->irq) { |
| 801 | do { |
| 802 | status = dspi_poll(dspi); |
| 803 | } while (status == -EINPROGRESS); |
| 804 | } else if (trans_mode != DSPI_DMA_MODE) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 805 | wait_for_completion(&dspi->xfer_done); |
| 806 | reinit_completion(&dspi->xfer_done); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (transfer->delay_usecs) |
| 810 | udelay(transfer->delay_usecs); |
| 811 | } |
| 812 | |
| 813 | out: |
| 814 | message->status = status; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 815 | spi_finalize_current_message(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 816 | |
| 817 | return status; |
| 818 | } |
| 819 | |
| 820 | static int dspi_setup(struct spi_device *spi) |
| 821 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 822 | struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 823 | unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 824 | u32 cs_sck_delay = 0, sck_cs_delay = 0; |
| 825 | struct fsl_dspi_platform_data *pdata; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 826 | unsigned char pasc = 0, asc = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 827 | struct chip_data *chip; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 828 | unsigned long clkrate; |
| 829 | |
| 830 | /* Only alloc on first setup */ |
| 831 | chip = spi_get_ctldata(spi); |
| 832 | if (chip == NULL) { |
| 833 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
| 834 | if (!chip) |
| 835 | return -ENOMEM; |
| 836 | } |
| 837 | |
| 838 | pdata = dev_get_platdata(&dspi->pdev->dev); |
| 839 | |
| 840 | if (!pdata) { |
| 841 | of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 842 | &cs_sck_delay); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 843 | |
| 844 | of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 845 | &sck_cs_delay); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 846 | } else { |
| 847 | cs_sck_delay = pdata->cs_sck_delay; |
| 848 | sck_cs_delay = pdata->sck_cs_delay; |
| 849 | } |
| 850 | |
| 851 | chip->void_write_data = 0; |
| 852 | |
| 853 | clkrate = clk_get_rate(dspi->clk); |
| 854 | hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate); |
| 855 | |
| 856 | /* Set PCS to SCK delay scale values */ |
| 857 | ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate); |
| 858 | |
| 859 | /* Set After SCK delay scale values */ |
| 860 | ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate); |
| 861 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 862 | chip->ctar_val = 0; |
| 863 | if (spi->mode & SPI_CPOL) |
| 864 | chip->ctar_val |= SPI_CTAR_CPOL; |
| 865 | if (spi->mode & SPI_CPHA) |
| 866 | chip->ctar_val |= SPI_CTAR_CPHA; |
| 867 | |
| 868 | if (!spi_controller_is_slave(dspi->ctlr)) { |
| 869 | chip->ctar_val |= SPI_CTAR_PCSSCK(pcssck) | |
| 870 | SPI_CTAR_CSSCK(cssck) | |
| 871 | SPI_CTAR_PASC(pasc) | |
| 872 | SPI_CTAR_ASC(asc) | |
| 873 | SPI_CTAR_PBR(pbr) | |
| 874 | SPI_CTAR_BR(br); |
| 875 | |
| 876 | if (spi->mode & SPI_LSB_FIRST) |
| 877 | chip->ctar_val |= SPI_CTAR_LSBFE; |
| 878 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 879 | |
| 880 | spi_set_ctldata(spi, chip); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static void dspi_cleanup(struct spi_device *spi) |
| 886 | { |
| 887 | struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi); |
| 888 | |
| 889 | dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 890 | spi->controller->bus_num, spi->chip_select); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 891 | |
| 892 | kfree(chip); |
| 893 | } |
| 894 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 895 | static const struct of_device_id fsl_dspi_dt_ids[] = { |
| 896 | { .compatible = "fsl,vf610-dspi", .data = &vf610_data, }, |
| 897 | { .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, }, |
| 898 | { .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, }, |
| 899 | { /* sentinel */ } |
| 900 | }; |
| 901 | MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids); |
| 902 | |
| 903 | #ifdef CONFIG_PM_SLEEP |
| 904 | static int dspi_suspend(struct device *dev) |
| 905 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 906 | struct spi_controller *ctlr = dev_get_drvdata(dev); |
| 907 | struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 908 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 909 | if (dspi->irq) |
| 910 | disable_irq(dspi->irq); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 911 | spi_controller_suspend(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 912 | clk_disable_unprepare(dspi->clk); |
| 913 | |
| 914 | pinctrl_pm_select_sleep_state(dev); |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | static int dspi_resume(struct device *dev) |
| 920 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 921 | struct spi_controller *ctlr = dev_get_drvdata(dev); |
| 922 | struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 923 | int ret; |
| 924 | |
| 925 | pinctrl_pm_select_default_state(dev); |
| 926 | |
| 927 | ret = clk_prepare_enable(dspi->clk); |
| 928 | if (ret) |
| 929 | return ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 930 | spi_controller_resume(ctlr); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 931 | if (dspi->irq) |
| 932 | enable_irq(dspi->irq); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | #endif /* CONFIG_PM_SLEEP */ |
| 937 | |
| 938 | static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume); |
| 939 | |
| 940 | static const struct regmap_range dspi_volatile_ranges[] = { |
| 941 | regmap_reg_range(SPI_MCR, SPI_TCR), |
| 942 | regmap_reg_range(SPI_SR, SPI_SR), |
| 943 | regmap_reg_range(SPI_PUSHR, SPI_RXFR3), |
| 944 | }; |
| 945 | |
| 946 | static const struct regmap_access_table dspi_volatile_table = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 947 | .yes_ranges = dspi_volatile_ranges, |
| 948 | .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 949 | }; |
| 950 | |
| 951 | static const struct regmap_config dspi_regmap_config = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 952 | .reg_bits = 32, |
| 953 | .val_bits = 32, |
| 954 | .reg_stride = 4, |
| 955 | .max_register = 0x88, |
| 956 | .volatile_table = &dspi_volatile_table, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 957 | }; |
| 958 | |
| 959 | static const struct regmap_range dspi_xspi_volatile_ranges[] = { |
| 960 | regmap_reg_range(SPI_MCR, SPI_TCR), |
| 961 | regmap_reg_range(SPI_SR, SPI_SR), |
| 962 | regmap_reg_range(SPI_PUSHR, SPI_RXFR3), |
| 963 | regmap_reg_range(SPI_SREX, SPI_SREX), |
| 964 | }; |
| 965 | |
| 966 | static const struct regmap_access_table dspi_xspi_volatile_table = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 967 | .yes_ranges = dspi_xspi_volatile_ranges, |
| 968 | .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | static const struct regmap_config dspi_xspi_regmap_config[] = { |
| 972 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 973 | .reg_bits = 32, |
| 974 | .val_bits = 32, |
| 975 | .reg_stride = 4, |
| 976 | .max_register = 0x13c, |
| 977 | .volatile_table = &dspi_xspi_volatile_table, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 978 | }, |
| 979 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 980 | .name = "pushr", |
| 981 | .reg_bits = 16, |
| 982 | .val_bits = 16, |
| 983 | .reg_stride = 2, |
| 984 | .max_register = 0x2, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 985 | }, |
| 986 | }; |
| 987 | |
| 988 | static void dspi_init(struct fsl_dspi *dspi) |
| 989 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 990 | unsigned int mcr = SPI_MCR_PCSIS; |
| 991 | |
| 992 | if (dspi->devtype_data->xspi_mode) |
| 993 | mcr |= SPI_MCR_XSPI; |
| 994 | if (!spi_controller_is_slave(dspi->ctlr)) |
| 995 | mcr |= SPI_MCR_MASTER; |
| 996 | |
| 997 | regmap_write(dspi->regmap, SPI_MCR, mcr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 998 | regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR); |
| 999 | if (dspi->devtype_data->xspi_mode) |
| 1000 | regmap_write(dspi->regmap, SPI_CTARE(0), |
| 1001 | SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1)); |
| 1002 | } |
| 1003 | |
| 1004 | static int dspi_probe(struct platform_device *pdev) |
| 1005 | { |
| 1006 | struct device_node *np = pdev->dev.of_node; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1007 | const struct regmap_config *regmap_config; |
| 1008 | struct fsl_dspi_platform_data *pdata; |
| 1009 | struct spi_controller *ctlr; |
| 1010 | int ret, cs_num, bus_num; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1011 | struct fsl_dspi *dspi; |
| 1012 | struct resource *res; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1013 | void __iomem *base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1014 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1015 | ctlr = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi)); |
| 1016 | if (!ctlr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1017 | return -ENOMEM; |
| 1018 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1019 | dspi = spi_controller_get_devdata(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1020 | dspi->pdev = pdev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1021 | dspi->ctlr = ctlr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1022 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1023 | ctlr->setup = dspi_setup; |
| 1024 | ctlr->transfer_one_message = dspi_transfer_one_message; |
| 1025 | ctlr->dev.of_node = pdev->dev.of_node; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1026 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1027 | ctlr->cleanup = dspi_cleanup; |
| 1028 | ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1029 | |
| 1030 | pdata = dev_get_platdata(&pdev->dev); |
| 1031 | if (pdata) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1032 | ctlr->num_chipselect = pdata->cs_num; |
| 1033 | ctlr->bus_num = pdata->bus_num; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1034 | |
| 1035 | dspi->devtype_data = &coldfire_data; |
| 1036 | } else { |
| 1037 | |
| 1038 | ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num); |
| 1039 | if (ret < 0) { |
| 1040 | dev_err(&pdev->dev, "can't get spi-num-chipselects\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1041 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1042 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1043 | ctlr->num_chipselect = cs_num; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1044 | |
| 1045 | ret = of_property_read_u32(np, "bus-num", &bus_num); |
| 1046 | if (ret < 0) { |
| 1047 | dev_err(&pdev->dev, "can't get bus-num\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1048 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1049 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1050 | ctlr->bus_num = bus_num; |
| 1051 | |
| 1052 | if (of_property_read_bool(np, "spi-slave")) |
| 1053 | ctlr->slave = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1054 | |
| 1055 | dspi->devtype_data = of_device_get_match_data(&pdev->dev); |
| 1056 | if (!dspi->devtype_data) { |
| 1057 | dev_err(&pdev->dev, "can't get devtype_data\n"); |
| 1058 | ret = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1059 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | if (dspi->devtype_data->xspi_mode) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1064 | ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1065 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1066 | ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1067 | |
| 1068 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1069 | base = devm_ioremap_resource(&pdev->dev, res); |
| 1070 | if (IS_ERR(base)) { |
| 1071 | ret = PTR_ERR(base); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1072 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | if (dspi->devtype_data->xspi_mode) |
| 1076 | regmap_config = &dspi_xspi_regmap_config[0]; |
| 1077 | else |
| 1078 | regmap_config = &dspi_regmap_config; |
| 1079 | dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config); |
| 1080 | if (IS_ERR(dspi->regmap)) { |
| 1081 | dev_err(&pdev->dev, "failed to init regmap: %ld\n", |
| 1082 | PTR_ERR(dspi->regmap)); |
| 1083 | ret = PTR_ERR(dspi->regmap); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1084 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | if (dspi->devtype_data->xspi_mode) { |
| 1088 | dspi->regmap_pushr = devm_regmap_init_mmio( |
| 1089 | &pdev->dev, base + SPI_PUSHR, |
| 1090 | &dspi_xspi_regmap_config[1]); |
| 1091 | if (IS_ERR(dspi->regmap_pushr)) { |
| 1092 | dev_err(&pdev->dev, |
| 1093 | "failed to init pushr regmap: %ld\n", |
| 1094 | PTR_ERR(dspi->regmap_pushr)); |
| 1095 | ret = PTR_ERR(dspi->regmap_pushr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1096 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | dspi->clk = devm_clk_get(&pdev->dev, "dspi"); |
| 1101 | if (IS_ERR(dspi->clk)) { |
| 1102 | ret = PTR_ERR(dspi->clk); |
| 1103 | dev_err(&pdev->dev, "unable to get clock\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1104 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1105 | } |
| 1106 | ret = clk_prepare_enable(dspi->clk); |
| 1107 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1108 | goto out_ctlr_put; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1109 | |
| 1110 | dspi_init(dspi); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1111 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1112 | dspi->irq = platform_get_irq(pdev, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1113 | if (dspi->irq <= 0) { |
| 1114 | dev_info(&pdev->dev, |
| 1115 | "can't get platform irq, using poll mode\n"); |
| 1116 | dspi->irq = 0; |
| 1117 | goto poll_mode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1120 | ret = request_threaded_irq(dspi->irq, dspi_interrupt, NULL, |
| 1121 | IRQF_SHARED, pdev->name, dspi); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1122 | if (ret < 0) { |
| 1123 | dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n"); |
| 1124 | goto out_clk_put; |
| 1125 | } |
| 1126 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1127 | init_completion(&dspi->xfer_done); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1128 | |
| 1129 | poll_mode: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1130 | if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) { |
| 1131 | ret = dspi_request_dma(dspi, res->start); |
| 1132 | if (ret < 0) { |
| 1133 | dev_err(&pdev->dev, "can't get dma channels\n"); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1134 | goto out_free_irq; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1138 | ctlr->max_speed_hz = |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1139 | clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor; |
| 1140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1141 | platform_set_drvdata(pdev, ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1142 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1143 | ret = spi_register_controller(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1144 | if (ret != 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1145 | dev_err(&pdev->dev, "Problem registering DSPI ctlr\n"); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1146 | goto out_release_dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | return ret; |
| 1150 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1151 | out_release_dma: |
| 1152 | dspi_release_dma(dspi); |
| 1153 | out_free_irq: |
| 1154 | if (dspi->irq) |
| 1155 | free_irq(dspi->irq, dspi); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1156 | out_clk_put: |
| 1157 | clk_disable_unprepare(dspi->clk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1158 | out_ctlr_put: |
| 1159 | spi_controller_put(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1160 | |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | static int dspi_remove(struct platform_device *pdev) |
| 1165 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1166 | struct spi_controller *ctlr = platform_get_drvdata(pdev); |
| 1167 | struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* Disconnect from the SPI framework */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1170 | spi_unregister_controller(dspi->ctlr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1171 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1172 | /* Disable RX and TX */ |
| 1173 | regmap_update_bits(dspi->regmap, SPI_MCR, |
| 1174 | SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF, |
| 1175 | SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF); |
| 1176 | |
| 1177 | /* Stop Running */ |
| 1178 | regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT); |
| 1179 | |
| 1180 | dspi_release_dma(dspi); |
| 1181 | if (dspi->irq) |
| 1182 | free_irq(dspi->irq, dspi); |
| 1183 | clk_disable_unprepare(dspi->clk); |
| 1184 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1188 | static void dspi_shutdown(struct platform_device *pdev) |
| 1189 | { |
| 1190 | dspi_remove(pdev); |
| 1191 | } |
| 1192 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1193 | static struct platform_driver fsl_dspi_driver = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1194 | .driver.name = DRIVER_NAME, |
| 1195 | .driver.of_match_table = fsl_dspi_dt_ids, |
| 1196 | .driver.owner = THIS_MODULE, |
| 1197 | .driver.pm = &dspi_pm, |
| 1198 | .probe = dspi_probe, |
| 1199 | .remove = dspi_remove, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1200 | .shutdown = dspi_shutdown, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1201 | }; |
| 1202 | module_platform_driver(fsl_dspi_driver); |
| 1203 | |
| 1204 | MODULE_DESCRIPTION("Freescale DSPI Controller Driver"); |
| 1205 | MODULE_LICENSE("GPL"); |
| 1206 | MODULE_ALIAS("platform:" DRIVER_NAME); |