Fabio Estevam | 7965059 | 2018-05-02 16:18:27 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | // Copyright (C) 2008 Juergen Beisert |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 4 | |
| 5 | #include <linux/clk.h> |
| 6 | #include <linux/completion.h> |
| 7 | #include <linux/delay.h> |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 8 | #include <linux/dmaengine.h> |
| 9 | #include <linux/dma-mapping.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 10 | #include <linux/err.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 16 | #include <linux/pinctrl/consumer.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 20 | #include <linux/spi/spi.h> |
| 21 | #include <linux/spi/spi_bitbang.h> |
| 22 | #include <linux/types.h> |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 25 | #include <linux/property.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 26 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 27 | #include <linux/platform_data/dma-imx.h> |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 28 | |
| 29 | #define DRIVER_NAME "spi_imx" |
| 30 | |
Trent Piepho | 0a9c899 | 2019-03-04 23:02:36 +0000 | [diff] [blame] | 31 | static bool use_dma = true; |
| 32 | module_param(use_dma, bool, 0644); |
| 33 | MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)"); |
| 34 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 35 | #define MXC_RPM_TIMEOUT 2000 /* 2000ms */ |
| 36 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 37 | #define MXC_CSPIRXDATA 0x00 |
| 38 | #define MXC_CSPITXDATA 0x04 |
| 39 | #define MXC_CSPICTRL 0x08 |
| 40 | #define MXC_CSPIINT 0x0c |
| 41 | #define MXC_RESET 0x1c |
| 42 | |
| 43 | /* generic defines to abstract from the different register layouts */ |
| 44 | #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */ |
| 45 | #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */ |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 46 | #define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */ |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 47 | |
Uwe Kleine-König | 30d6714 | 2018-11-30 07:47:07 +0100 | [diff] [blame] | 48 | /* The maximum bytes that a sdma BD can transfer. */ |
| 49 | #define MAX_SDMA_BD_BYTES (1 << 15) |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 50 | #define MX51_ECSPI_CTRL_MAX_BURST 512 |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 51 | /* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/ |
| 52 | #define MX53_MAX_TRANSFER_BYTES 512 |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 53 | |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 54 | enum spi_imx_devtype { |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 55 | IMX1_CSPI, |
| 56 | IMX21_CSPI, |
| 57 | IMX27_CSPI, |
| 58 | IMX31_CSPI, |
| 59 | IMX35_CSPI, /* CSPI on all i.mx except above */ |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 60 | IMX51_ECSPI, /* ECSPI on i.mx51 */ |
| 61 | IMX53_ECSPI, /* ECSPI on i.mx53 and later */ |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct spi_imx_data; |
| 65 | |
| 66 | struct spi_imx_devtype_data { |
| 67 | void (*intctrl)(struct spi_imx_data *, int); |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 68 | int (*prepare_message)(struct spi_imx_data *, struct spi_message *); |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 69 | int (*prepare_transfer)(struct spi_imx_data *, struct spi_device *); |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 70 | void (*trigger)(struct spi_imx_data *); |
| 71 | int (*rx_available)(struct spi_imx_data *); |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 72 | void (*reset)(struct spi_imx_data *); |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 73 | void (*setup_wml)(struct spi_imx_data *); |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 74 | void (*disable)(struct spi_imx_data *); |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 75 | void (*disable_dma)(struct spi_imx_data *); |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 76 | bool has_dmamode; |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 77 | bool has_slavemode; |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 78 | unsigned int fifo_size; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 79 | bool dynamic_burst; |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 80 | enum spi_imx_devtype devtype; |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 83 | struct spi_imx_data { |
| 84 | struct spi_bitbang bitbang; |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 85 | struct device *dev; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 86 | |
| 87 | struct completion xfer_done; |
Uwe Kleine-König | cc4d22a | 2012-03-29 21:54:18 +0200 | [diff] [blame] | 88 | void __iomem *base; |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 89 | unsigned long base_phys; |
| 90 | |
Sascha Hauer | aa29d840 | 2012-03-07 09:30:22 +0100 | [diff] [blame] | 91 | struct clk *clk_per; |
| 92 | struct clk *clk_ipg; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 93 | unsigned long spi_clk; |
Anton Bondarenko | 4bfe927 | 2016-02-19 08:43:03 +0100 | [diff] [blame] | 94 | unsigned int spi_bus_clk; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 95 | |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 96 | unsigned int bits_per_word; |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 97 | unsigned int spi_drctl; |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 98 | |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 99 | unsigned int count, remainder; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 100 | void (*tx)(struct spi_imx_data *); |
| 101 | void (*rx)(struct spi_imx_data *); |
| 102 | void *rx_buf; |
| 103 | const void *tx_buf; |
| 104 | unsigned int txfifo; /* number of words pushed in tx FIFO */ |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 105 | unsigned int dynamic_burst; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 106 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 107 | /* Slave mode */ |
| 108 | bool slave_mode; |
| 109 | bool slave_aborted; |
| 110 | unsigned int slave_burst; |
| 111 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 112 | /* DMA */ |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 113 | bool usedma; |
Anton Bondarenko | 0dfbaa8 | 2015-12-05 17:57:01 +0100 | [diff] [blame] | 114 | u32 wml; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 115 | struct completion dma_rx_completion; |
| 116 | struct completion dma_tx_completion; |
| 117 | |
Uwe Kleine-König | 80023cb | 2012-05-21 21:49:35 +0200 | [diff] [blame] | 118 | const struct spi_imx_devtype_data *devtype_data; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 121 | static inline int is_imx27_cspi(struct spi_imx_data *d) |
| 122 | { |
| 123 | return d->devtype_data->devtype == IMX27_CSPI; |
| 124 | } |
| 125 | |
| 126 | static inline int is_imx35_cspi(struct spi_imx_data *d) |
| 127 | { |
| 128 | return d->devtype_data->devtype == IMX35_CSPI; |
| 129 | } |
| 130 | |
Anton Bondarenko | f8a87617 | 2015-12-05 17:57:02 +0100 | [diff] [blame] | 131 | static inline int is_imx51_ecspi(struct spi_imx_data *d) |
| 132 | { |
| 133 | return d->devtype_data->devtype == IMX51_ECSPI; |
| 134 | } |
| 135 | |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 136 | static inline int is_imx53_ecspi(struct spi_imx_data *d) |
| 137 | { |
| 138 | return d->devtype_data->devtype == IMX53_ECSPI; |
| 139 | } |
| 140 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 141 | #define MXC_SPI_BUF_RX(type) \ |
| 142 | static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \ |
| 143 | { \ |
| 144 | unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \ |
| 145 | \ |
| 146 | if (spi_imx->rx_buf) { \ |
| 147 | *(type *)spi_imx->rx_buf = val; \ |
| 148 | spi_imx->rx_buf += sizeof(type); \ |
| 149 | } \ |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 150 | \ |
| 151 | spi_imx->remainder -= sizeof(type); \ |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | #define MXC_SPI_BUF_TX(type) \ |
| 155 | static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \ |
| 156 | { \ |
| 157 | type val = 0; \ |
| 158 | \ |
| 159 | if (spi_imx->tx_buf) { \ |
| 160 | val = *(type *)spi_imx->tx_buf; \ |
| 161 | spi_imx->tx_buf += sizeof(type); \ |
| 162 | } \ |
| 163 | \ |
| 164 | spi_imx->count -= sizeof(type); \ |
| 165 | \ |
| 166 | writel(val, spi_imx->base + MXC_CSPITXDATA); \ |
| 167 | } |
| 168 | |
| 169 | MXC_SPI_BUF_RX(u8) |
| 170 | MXC_SPI_BUF_TX(u8) |
| 171 | MXC_SPI_BUF_RX(u16) |
| 172 | MXC_SPI_BUF_TX(u16) |
| 173 | MXC_SPI_BUF_RX(u32) |
| 174 | MXC_SPI_BUF_TX(u32) |
| 175 | |
| 176 | /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set |
| 177 | * (which is currently not the case in this driver) |
| 178 | */ |
| 179 | static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, |
| 180 | 256, 384, 512, 768, 1024}; |
| 181 | |
| 182 | /* MX21, MX27 */ |
| 183 | static unsigned int spi_imx_clkdiv_1(unsigned int fin, |
Robert Baldyga | 32df9ff | 2016-11-01 22:18:39 +0100 | [diff] [blame] | 184 | unsigned int fspi, unsigned int max, unsigned int *fres) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 185 | { |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 186 | int i; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 187 | |
| 188 | for (i = 2; i < max; i++) |
| 189 | if (fspi * mxc_clkdivs[i] >= fin) |
Robert Baldyga | 32df9ff | 2016-11-01 22:18:39 +0100 | [diff] [blame] | 190 | break; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 191 | |
Robert Baldyga | 32df9ff | 2016-11-01 22:18:39 +0100 | [diff] [blame] | 192 | *fres = fin / mxc_clkdivs[i]; |
| 193 | return i; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 196 | /* MX1, MX31, MX35, MX51 CSPI */ |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 197 | static unsigned int spi_imx_clkdiv_2(unsigned int fin, |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 198 | unsigned int fspi, unsigned int *fres) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 199 | { |
| 200 | int i, div = 4; |
| 201 | |
| 202 | for (i = 0; i < 7; i++) { |
| 203 | if (fspi * div >= fin) |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 204 | goto out; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 205 | div <<= 1; |
| 206 | } |
| 207 | |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 208 | out: |
| 209 | *fres = fin / div; |
| 210 | return i; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Sascha Hauer | 2e312f6 | 2017-06-02 07:38:04 +0200 | [diff] [blame] | 213 | static int spi_imx_bytes_per_word(const int bits_per_word) |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 214 | { |
Maxime Chevallier | afb27208 | 2018-07-17 16:31:52 +0200 | [diff] [blame] | 215 | if (bits_per_word <= 8) |
| 216 | return 1; |
| 217 | else if (bits_per_word <= 16) |
| 218 | return 2; |
| 219 | else |
| 220 | return 4; |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 223 | static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi, |
| 224 | struct spi_transfer *transfer) |
| 225 | { |
| 226 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 227 | |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 228 | if (!use_dma || master->fallback) |
Trent Piepho | 0a9c899 | 2019-03-04 23:02:36 +0000 | [diff] [blame] | 229 | return false; |
| 230 | |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 231 | if (!master->dma_rx) |
| 232 | return false; |
| 233 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 234 | if (spi_imx->slave_mode) |
| 235 | return false; |
| 236 | |
Robin Gong | 133eb8e | 2018-10-10 10:32:48 +0000 | [diff] [blame] | 237 | if (transfer->len < spi_imx->devtype_data->fifo_size) |
| 238 | return false; |
| 239 | |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 240 | spi_imx->dynamic_burst = 0; |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 241 | |
| 242 | return true; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 243 | } |
| 244 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 245 | #define MX51_ECSPI_CTRL 0x08 |
| 246 | #define MX51_ECSPI_CTRL_ENABLE (1 << 0) |
| 247 | #define MX51_ECSPI_CTRL_XCH (1 << 2) |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 248 | #define MX51_ECSPI_CTRL_SMC (1 << 3) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 249 | #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4) |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 250 | #define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 251 | #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8 |
| 252 | #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12 |
| 253 | #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18) |
| 254 | #define MX51_ECSPI_CTRL_BL_OFFSET 20 |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 255 | #define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 256 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 257 | #define MX51_ECSPI_CONFIG 0x0c |
| 258 | #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0)) |
| 259 | #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4)) |
| 260 | #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8)) |
| 261 | #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12)) |
Knut Wohlrab | c09b890 | 2012-09-25 13:21:57 +0200 | [diff] [blame] | 262 | #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20)) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 263 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 264 | #define MX51_ECSPI_INT 0x10 |
| 265 | #define MX51_ECSPI_INT_TEEN (1 << 0) |
| 266 | #define MX51_ECSPI_INT_RREN (1 << 3) |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 267 | #define MX51_ECSPI_INT_RDREN (1 << 4) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 268 | |
Uwe Kleine-König | 30d6714 | 2018-11-30 07:47:07 +0100 | [diff] [blame] | 269 | #define MX51_ECSPI_DMA 0x14 |
Sascha Hauer | d629c2a | 2016-02-24 09:20:31 +0100 | [diff] [blame] | 270 | #define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f) |
| 271 | #define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16) |
| 272 | #define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24) |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 273 | |
Sascha Hauer | 2b0fd06 | 2016-02-24 09:20:27 +0100 | [diff] [blame] | 274 | #define MX51_ECSPI_DMA_TEDEN (1 << 7) |
| 275 | #define MX51_ECSPI_DMA_RXDEN (1 << 23) |
| 276 | #define MX51_ECSPI_DMA_RXTDEN (1 << 31) |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 277 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 278 | #define MX51_ECSPI_STAT 0x18 |
| 279 | #define MX51_ECSPI_STAT_RR (1 << 3) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 280 | |
Fabio Estevam | 9f6aa42 | 2015-12-03 23:23:24 -0200 | [diff] [blame] | 281 | #define MX51_ECSPI_TESTREG 0x20 |
| 282 | #define MX51_ECSPI_TESTREG_LBC BIT(31) |
| 283 | |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 284 | static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx) |
| 285 | { |
| 286 | unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); |
Arnd Bergmann | 5904c9d | 2017-08-23 15:34:43 +0200 | [diff] [blame] | 287 | #ifdef __LITTLE_ENDIAN |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 288 | unsigned int bytes_per_word; |
Arnd Bergmann | 5904c9d | 2017-08-23 15:34:43 +0200 | [diff] [blame] | 289 | #endif |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 290 | |
| 291 | if (spi_imx->rx_buf) { |
| 292 | #ifdef __LITTLE_ENDIAN |
| 293 | bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word); |
| 294 | if (bytes_per_word == 1) |
| 295 | val = cpu_to_be32(val); |
| 296 | else if (bytes_per_word == 2) |
| 297 | val = (val << 16) | (val >> 16); |
| 298 | #endif |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 299 | *(u32 *)spi_imx->rx_buf = val; |
| 300 | spi_imx->rx_buf += sizeof(u32); |
| 301 | } |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 302 | |
| 303 | spi_imx->remainder -= sizeof(u32); |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx) |
| 307 | { |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 308 | int unaligned; |
| 309 | u32 val; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 310 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 311 | unaligned = spi_imx->remainder % 4; |
| 312 | |
| 313 | if (!unaligned) { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 314 | spi_imx_buf_rx_swap_u32(spi_imx); |
| 315 | return; |
| 316 | } |
| 317 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 318 | if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 319 | spi_imx_buf_rx_u16(spi_imx); |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
| 323 | val = readl(spi_imx->base + MXC_CSPIRXDATA); |
| 324 | |
| 325 | while (unaligned--) { |
| 326 | if (spi_imx->rx_buf) { |
| 327 | *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff; |
| 328 | spi_imx->rx_buf++; |
| 329 | } |
| 330 | spi_imx->remainder--; |
| 331 | } |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx) |
| 335 | { |
| 336 | u32 val = 0; |
Arnd Bergmann | 5904c9d | 2017-08-23 15:34:43 +0200 | [diff] [blame] | 337 | #ifdef __LITTLE_ENDIAN |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 338 | unsigned int bytes_per_word; |
Arnd Bergmann | 5904c9d | 2017-08-23 15:34:43 +0200 | [diff] [blame] | 339 | #endif |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 340 | |
| 341 | if (spi_imx->tx_buf) { |
| 342 | val = *(u32 *)spi_imx->tx_buf; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 343 | spi_imx->tx_buf += sizeof(u32); |
| 344 | } |
| 345 | |
| 346 | spi_imx->count -= sizeof(u32); |
| 347 | #ifdef __LITTLE_ENDIAN |
| 348 | bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word); |
| 349 | |
| 350 | if (bytes_per_word == 1) |
| 351 | val = cpu_to_be32(val); |
| 352 | else if (bytes_per_word == 2) |
| 353 | val = (val << 16) | (val >> 16); |
| 354 | #endif |
| 355 | writel(val, spi_imx->base + MXC_CSPITXDATA); |
| 356 | } |
| 357 | |
| 358 | static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx) |
| 359 | { |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 360 | int unaligned; |
| 361 | u32 val = 0; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 362 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 363 | unaligned = spi_imx->count % 4; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 364 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 365 | if (!unaligned) { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 366 | spi_imx_buf_tx_swap_u32(spi_imx); |
| 367 | return; |
| 368 | } |
| 369 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 370 | if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 371 | spi_imx_buf_tx_u16(spi_imx); |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
| 375 | while (unaligned--) { |
| 376 | if (spi_imx->tx_buf) { |
| 377 | val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned); |
| 378 | spi_imx->tx_buf++; |
| 379 | } |
| 380 | spi_imx->count--; |
| 381 | } |
| 382 | |
| 383 | writel(val, spi_imx->base + MXC_CSPITXDATA); |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 384 | } |
| 385 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 386 | static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx) |
| 387 | { |
| 388 | u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA)); |
| 389 | |
| 390 | if (spi_imx->rx_buf) { |
| 391 | int n_bytes = spi_imx->slave_burst % sizeof(val); |
| 392 | |
| 393 | if (!n_bytes) |
| 394 | n_bytes = sizeof(val); |
| 395 | |
| 396 | memcpy(spi_imx->rx_buf, |
| 397 | ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes); |
| 398 | |
| 399 | spi_imx->rx_buf += n_bytes; |
| 400 | spi_imx->slave_burst -= n_bytes; |
| 401 | } |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 402 | |
| 403 | spi_imx->remainder -= sizeof(u32); |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx) |
| 407 | { |
| 408 | u32 val = 0; |
| 409 | int n_bytes = spi_imx->count % sizeof(val); |
| 410 | |
| 411 | if (!n_bytes) |
| 412 | n_bytes = sizeof(val); |
| 413 | |
| 414 | if (spi_imx->tx_buf) { |
| 415 | memcpy(((u8 *)&val) + sizeof(val) - n_bytes, |
| 416 | spi_imx->tx_buf, n_bytes); |
| 417 | val = cpu_to_be32(val); |
| 418 | spi_imx->tx_buf += n_bytes; |
| 419 | } |
| 420 | |
| 421 | spi_imx->count -= n_bytes; |
| 422 | |
| 423 | writel(val, spi_imx->base + MXC_CSPITXDATA); |
| 424 | } |
| 425 | |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 426 | /* MX51 eCSPI */ |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 427 | static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx, |
| 428 | unsigned int fspi, unsigned int *fres) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 429 | { |
| 430 | /* |
| 431 | * there are two 4-bit dividers, the pre-divider divides by |
| 432 | * $pre, the post-divider by 2^$post |
| 433 | */ |
| 434 | unsigned int pre, post; |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 435 | unsigned int fin = spi_imx->spi_clk; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 436 | |
| 437 | if (unlikely(fspi > fin)) |
| 438 | return 0; |
| 439 | |
| 440 | post = fls(fin) - fls(fspi); |
| 441 | if (fin > fspi << post) |
| 442 | post++; |
| 443 | |
| 444 | /* now we have: (fin <= fspi << post) with post being minimal */ |
| 445 | |
| 446 | post = max(4U, post) - 4; |
| 447 | if (unlikely(post > 0xf)) { |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 448 | dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n", |
| 449 | fspi, fin); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 450 | return 0xff; |
| 451 | } |
| 452 | |
| 453 | pre = DIV_ROUND_UP(fin, fspi << post) - 1; |
| 454 | |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 455 | dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n", |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 456 | __func__, fin, fspi, post, pre); |
Marek Vasut | 6fd8b85 | 2013-12-18 18:31:47 +0100 | [diff] [blame] | 457 | |
| 458 | /* Resulting frequency for the SCLK line. */ |
| 459 | *fres = (fin / (pre + 1)) >> post; |
| 460 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 461 | return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) | |
| 462 | (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 465 | static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 466 | { |
| 467 | unsigned val = 0; |
| 468 | |
| 469 | if (enable & MXC_INT_TE) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 470 | val |= MX51_ECSPI_INT_TEEN; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 471 | |
| 472 | if (enable & MXC_INT_RR) |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 473 | val |= MX51_ECSPI_INT_RREN; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 474 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 475 | if (enable & MXC_INT_RDR) |
| 476 | val |= MX51_ECSPI_INT_RDREN; |
| 477 | |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 478 | writel(val, spi_imx->base + MX51_ECSPI_INT); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 481 | static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 482 | { |
Sascha Hauer | b03c388 | 2016-02-24 09:20:32 +0100 | [diff] [blame] | 483 | u32 reg; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 484 | |
Sascha Hauer | b03c388 | 2016-02-24 09:20:32 +0100 | [diff] [blame] | 485 | reg = readl(spi_imx->base + MX51_ECSPI_CTRL); |
| 486 | reg |= MX51_ECSPI_CTRL_XCH; |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 487 | writel(reg, spi_imx->base + MX51_ECSPI_CTRL); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 490 | static void mx51_disable_dma(struct spi_imx_data *spi_imx) |
| 491 | { |
| 492 | writel(0, spi_imx->base + MX51_ECSPI_DMA); |
| 493 | } |
| 494 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 495 | static void mx51_ecspi_disable(struct spi_imx_data *spi_imx) |
| 496 | { |
| 497 | u32 ctrl; |
| 498 | |
| 499 | ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL); |
| 500 | ctrl &= ~MX51_ECSPI_CTRL_ENABLE; |
| 501 | writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); |
| 502 | } |
| 503 | |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 504 | static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx, |
| 505 | struct spi_message *msg) |
| 506 | { |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 507 | struct spi_device *spi = msg->spi; |
Knut Wohlrab | 793c7f9 | 2016-03-15 14:24:36 +0100 | [diff] [blame] | 508 | u32 ctrl = MX51_ECSPI_CTRL_ENABLE; |
Marek Vasut | 135cbd3 | 2021-07-03 04:23:00 +0200 | [diff] [blame] | 509 | u32 testreg, delay; |
Knut Wohlrab | 793c7f9 | 2016-03-15 14:24:36 +0100 | [diff] [blame] | 510 | u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 511 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 512 | /* set Master or Slave mode */ |
| 513 | if (spi_imx->slave_mode) |
| 514 | ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK; |
| 515 | else |
| 516 | ctrl |= MX51_ECSPI_CTRL_MODE_MASK; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 517 | |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 518 | /* |
| 519 | * Enable SPI_RDY handling (falling edge/level triggered). |
| 520 | */ |
| 521 | if (spi->mode & SPI_READY) |
| 522 | ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl); |
| 523 | |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 524 | /* set chip select to use */ |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 525 | ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 526 | |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 527 | /* |
| 528 | * The ctrl register must be written first, with the EN bit set other |
| 529 | * registers must not be written to. |
| 530 | */ |
| 531 | writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); |
| 532 | |
| 533 | testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG); |
| 534 | if (spi->mode & SPI_LOOP) |
| 535 | testreg |= MX51_ECSPI_TESTREG_LBC; |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 536 | else |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 537 | testreg &= ~MX51_ECSPI_TESTREG_LBC; |
| 538 | writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 539 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 540 | /* |
| 541 | * eCSPI burst completion by Chip Select signal in Slave mode |
| 542 | * is not functional for imx53 Soc, config SPI burst completed when |
| 543 | * BURST_LENGTH + 1 bits are received |
| 544 | */ |
| 545 | if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx)) |
| 546 | cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select); |
| 547 | else |
| 548 | cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 549 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 550 | if (spi->mode & SPI_CPHA) |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 551 | cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select); |
Knut Wohlrab | 793c7f9 | 2016-03-15 14:24:36 +0100 | [diff] [blame] | 552 | else |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 553 | cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 554 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 555 | if (spi->mode & SPI_CPOL) { |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 556 | cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select); |
| 557 | cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select); |
Knut Wohlrab | 793c7f9 | 2016-03-15 14:24:36 +0100 | [diff] [blame] | 558 | } else { |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 559 | cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select); |
| 560 | cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select); |
Knut Wohlrab | c09b890 | 2012-09-25 13:21:57 +0200 | [diff] [blame] | 561 | } |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 562 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 563 | if (spi->mode & SPI_CS_HIGH) |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 564 | cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select); |
Knut Wohlrab | 793c7f9 | 2016-03-15 14:24:36 +0100 | [diff] [blame] | 565 | else |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 566 | cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 567 | |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 568 | writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG); |
| 569 | |
Marek Vasut | 135cbd3 | 2021-07-03 04:23:00 +0200 | [diff] [blame] | 570 | /* |
| 571 | * Wait until the changes in the configuration register CONFIGREG |
| 572 | * propagate into the hardware. It takes exactly one tick of the |
| 573 | * SCLK clock, but we will wait two SCLK clock just to be sure. The |
| 574 | * effect of the delay it takes for the hardware to apply changes |
| 575 | * is noticable if the SCLK clock run very slow. In such a case, if |
| 576 | * the polarity of SCLK should be inverted, the GPIO ChipSelect might |
| 577 | * be asserted before the SCLK polarity changes, which would disrupt |
| 578 | * the SPI communication as the device on the other end would consider |
| 579 | * the change of SCLK polarity as a clock tick already. |
| 580 | */ |
| 581 | delay = (2 * 1000000) / spi_imx->spi_bus_clk; |
| 582 | if (likely(delay < 10)) /* SCLK is faster than 100 kHz */ |
| 583 | udelay(delay); |
| 584 | else /* SCLK is _very_ slow */ |
| 585 | usleep_range(delay, delay + 10); |
| 586 | |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 590 | static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx, |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 591 | struct spi_device *spi) |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 592 | { |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 593 | u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL); |
Marek Vasut | 135cbd3 | 2021-07-03 04:23:00 +0200 | [diff] [blame] | 594 | u32 clk; |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 595 | |
| 596 | /* Clear BL field and set the right value */ |
| 597 | ctrl &= ~MX51_ECSPI_CTRL_BL_MASK; |
| 598 | if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx)) |
| 599 | ctrl |= (spi_imx->slave_burst * 8 - 1) |
| 600 | << MX51_ECSPI_CTRL_BL_OFFSET; |
| 601 | else |
| 602 | ctrl |= (spi_imx->bits_per_word - 1) |
| 603 | << MX51_ECSPI_CTRL_BL_OFFSET; |
| 604 | |
| 605 | /* set clock speed */ |
| 606 | ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET | |
| 607 | 0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET); |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 608 | ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk); |
Uwe Kleine-König | 00b80ac | 2018-11-30 07:47:06 +0100 | [diff] [blame] | 609 | spi_imx->spi_bus_clk = clk; |
| 610 | |
Sascha Hauer | b03c388 | 2016-02-24 09:20:32 +0100 | [diff] [blame] | 611 | if (spi_imx->usedma) |
| 612 | ctrl |= MX51_ECSPI_CTRL_SMC; |
| 613 | |
Anton Bondarenko | f677f17 | 2015-12-08 07:43:43 +0100 | [diff] [blame] | 614 | writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); |
| 615 | |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static void mx51_setup_wml(struct spi_imx_data *spi_imx) |
| 620 | { |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 621 | /* |
| 622 | * Configure the DMA register: setup the watermark |
| 623 | * and enable DMA request. |
| 624 | */ |
Robin Gong | 5ba5a37 | 2018-10-10 10:32:45 +0000 | [diff] [blame] | 625 | writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) | |
Sascha Hauer | d629c2a | 2016-02-24 09:20:31 +0100 | [diff] [blame] | 626 | MX51_ECSPI_DMA_TX_WML(spi_imx->wml) | |
| 627 | MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) | |
Sascha Hauer | 2b0fd06 | 2016-02-24 09:20:27 +0100 | [diff] [blame] | 628 | MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN | |
| 629 | MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA); |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 632 | static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 633 | { |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 634 | return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR; |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 637 | static void mx51_ecspi_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 638 | { |
| 639 | /* drain receive buffer */ |
Shawn Guo | 66de757 | 2011-07-10 01:16:37 +0800 | [diff] [blame] | 640 | while (mx51_ecspi_rx_available(spi_imx)) |
Uwe Kleine-König | 0b59960 | 2010-09-09 21:02:48 +0200 | [diff] [blame] | 641 | readl(spi_imx->base + MXC_CSPIRXDATA); |
| 642 | } |
| 643 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 644 | #define MX31_INTREG_TEEN (1 << 0) |
| 645 | #define MX31_INTREG_RREN (1 << 3) |
| 646 | |
| 647 | #define MX31_CSPICTRL_ENABLE (1 << 0) |
| 648 | #define MX31_CSPICTRL_MASTER (1 << 1) |
| 649 | #define MX31_CSPICTRL_XCH (1 << 2) |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 650 | #define MX31_CSPICTRL_SMC (1 << 3) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 651 | #define MX31_CSPICTRL_POL (1 << 4) |
| 652 | #define MX31_CSPICTRL_PHA (1 << 5) |
| 653 | #define MX31_CSPICTRL_SSCTL (1 << 6) |
| 654 | #define MX31_CSPICTRL_SSPOL (1 << 7) |
| 655 | #define MX31_CSPICTRL_BC_SHIFT 8 |
| 656 | #define MX35_CSPICTRL_BL_SHIFT 20 |
| 657 | #define MX31_CSPICTRL_CS_SHIFT 24 |
| 658 | #define MX35_CSPICTRL_CS_SHIFT 12 |
| 659 | #define MX31_CSPICTRL_DR_SHIFT 16 |
| 660 | |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 661 | #define MX31_CSPI_DMAREG 0x10 |
| 662 | #define MX31_DMAREG_RH_DEN (1<<4) |
| 663 | #define MX31_DMAREG_TH_DEN (1<<1) |
| 664 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 665 | #define MX31_CSPISTATUS 0x14 |
| 666 | #define MX31_STATUS_RR (1 << 3) |
| 667 | |
Martin Kaiser | 15ca921 | 2016-09-01 22:39:58 +0200 | [diff] [blame] | 668 | #define MX31_CSPI_TESTREG 0x1C |
| 669 | #define MX31_TEST_LBC (1 << 14) |
| 670 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 671 | /* These functions also work for the i.MX35, but be aware that |
| 672 | * the i.MX35 has a slightly different register layout for bits |
| 673 | * we do not use here. |
| 674 | */ |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 675 | static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 676 | { |
| 677 | unsigned int val = 0; |
| 678 | |
| 679 | if (enable & MXC_INT_TE) |
| 680 | val |= MX31_INTREG_TEEN; |
| 681 | if (enable & MXC_INT_RR) |
| 682 | val |= MX31_INTREG_RREN; |
| 683 | |
| 684 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 685 | } |
| 686 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 687 | static void mx31_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 688 | { |
| 689 | unsigned int reg; |
| 690 | |
| 691 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 692 | reg |= MX31_CSPICTRL_XCH; |
| 693 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 694 | } |
| 695 | |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 696 | static int mx31_prepare_message(struct spi_imx_data *spi_imx, |
| 697 | struct spi_message *msg) |
| 698 | { |
| 699 | return 0; |
| 700 | } |
| 701 | |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 702 | static int mx31_prepare_transfer(struct spi_imx_data *spi_imx, |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 703 | struct spi_device *spi) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 704 | { |
| 705 | unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER; |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 706 | unsigned int clk; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 707 | |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 708 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) << |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 709 | MX31_CSPICTRL_DR_SHIFT; |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 710 | spi_imx->spi_bus_clk = clk; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 711 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 712 | if (is_imx35_cspi(spi_imx)) { |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 713 | reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT; |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 714 | reg |= MX31_CSPICTRL_SSCTL; |
| 715 | } else { |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 716 | reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT; |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 717 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 718 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 719 | if (spi->mode & SPI_CPHA) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 720 | reg |= MX31_CSPICTRL_PHA; |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 721 | if (spi->mode & SPI_CPOL) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 722 | reg |= MX31_CSPICTRL_POL; |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 723 | if (spi->mode & SPI_CS_HIGH) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 724 | reg |= MX31_CSPICTRL_SSPOL; |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 725 | if (!spi->cs_gpiod) |
Greg Ungerer | 602c8f4 | 2017-07-11 14:22:11 +1000 | [diff] [blame] | 726 | reg |= (spi->chip_select) << |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 727 | (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT : |
| 728 | MX31_CSPICTRL_CS_SHIFT); |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 729 | |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 730 | if (spi_imx->usedma) |
| 731 | reg |= MX31_CSPICTRL_SMC; |
| 732 | |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 733 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 734 | |
Martin Kaiser | 15ca921 | 2016-09-01 22:39:58 +0200 | [diff] [blame] | 735 | reg = readl(spi_imx->base + MX31_CSPI_TESTREG); |
| 736 | if (spi->mode & SPI_LOOP) |
| 737 | reg |= MX31_TEST_LBC; |
| 738 | else |
| 739 | reg &= ~MX31_TEST_LBC; |
| 740 | writel(reg, spi_imx->base + MX31_CSPI_TESTREG); |
| 741 | |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 742 | if (spi_imx->usedma) { |
Uwe Kleine-König | 30d6714 | 2018-11-30 07:47:07 +0100 | [diff] [blame] | 743 | /* |
| 744 | * configure DMA requests when RXFIFO is half full and |
| 745 | * when TXFIFO is half empty |
| 746 | */ |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 747 | writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN, |
| 748 | spi_imx->base + MX31_CSPI_DMAREG); |
| 749 | } |
| 750 | |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 754 | static int mx31_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 755 | { |
| 756 | return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR; |
| 757 | } |
| 758 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 759 | static void mx31_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 760 | { |
| 761 | /* drain receive buffer */ |
Shawn Guo | 2a64a90 | 2011-07-10 01:16:38 +0800 | [diff] [blame] | 762 | while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 763 | readl(spi_imx->base + MXC_CSPIRXDATA); |
| 764 | } |
| 765 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 766 | #define MX21_INTREG_RR (1 << 4) |
| 767 | #define MX21_INTREG_TEEN (1 << 9) |
| 768 | #define MX21_INTREG_RREN (1 << 13) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 769 | |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 770 | #define MX21_CSPICTRL_POL (1 << 5) |
| 771 | #define MX21_CSPICTRL_PHA (1 << 6) |
| 772 | #define MX21_CSPICTRL_SSPOL (1 << 8) |
| 773 | #define MX21_CSPICTRL_XCH (1 << 9) |
| 774 | #define MX21_CSPICTRL_ENABLE (1 << 10) |
| 775 | #define MX21_CSPICTRL_MASTER (1 << 11) |
| 776 | #define MX21_CSPICTRL_DR_SHIFT 14 |
| 777 | #define MX21_CSPICTRL_CS_SHIFT 19 |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 778 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 779 | static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 780 | { |
| 781 | unsigned int val = 0; |
| 782 | |
| 783 | if (enable & MXC_INT_TE) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 784 | val |= MX21_INTREG_TEEN; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 785 | if (enable & MXC_INT_RR) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 786 | val |= MX21_INTREG_RREN; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 787 | |
| 788 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 789 | } |
| 790 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 791 | static void mx21_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 792 | { |
| 793 | unsigned int reg; |
| 794 | |
| 795 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 796 | reg |= MX21_CSPICTRL_XCH; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 797 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 798 | } |
| 799 | |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 800 | static int mx21_prepare_message(struct spi_imx_data *spi_imx, |
| 801 | struct spi_message *msg) |
| 802 | { |
| 803 | return 0; |
| 804 | } |
| 805 | |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 806 | static int mx21_prepare_transfer(struct spi_imx_data *spi_imx, |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 807 | struct spi_device *spi) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 808 | { |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 809 | unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER; |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 810 | unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18; |
Robert Baldyga | 32df9ff | 2016-11-01 22:18:39 +0100 | [diff] [blame] | 811 | unsigned int clk; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 812 | |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 813 | reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk) |
Robert Baldyga | 32df9ff | 2016-11-01 22:18:39 +0100 | [diff] [blame] | 814 | << MX21_CSPICTRL_DR_SHIFT; |
| 815 | spi_imx->spi_bus_clk = clk; |
| 816 | |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 817 | reg |= spi_imx->bits_per_word - 1; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 818 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 819 | if (spi->mode & SPI_CPHA) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 820 | reg |= MX21_CSPICTRL_PHA; |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 821 | if (spi->mode & SPI_CPOL) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 822 | reg |= MX21_CSPICTRL_POL; |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 823 | if (spi->mode & SPI_CS_HIGH) |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 824 | reg |= MX21_CSPICTRL_SSPOL; |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 825 | if (!spi->cs_gpiod) |
Greg Ungerer | 602c8f4 | 2017-07-11 14:22:11 +1000 | [diff] [blame] | 826 | reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 827 | |
| 828 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 833 | static int mx21_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 834 | { |
Shawn Guo | 3451fb1 | 2011-07-10 01:16:36 +0800 | [diff] [blame] | 835 | return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 838 | static void mx21_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 839 | { |
| 840 | writel(1, spi_imx->base + MXC_RESET); |
| 841 | } |
| 842 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 843 | #define MX1_INTREG_RR (1 << 3) |
| 844 | #define MX1_INTREG_TEEN (1 << 8) |
| 845 | #define MX1_INTREG_RREN (1 << 11) |
| 846 | |
| 847 | #define MX1_CSPICTRL_POL (1 << 4) |
| 848 | #define MX1_CSPICTRL_PHA (1 << 5) |
| 849 | #define MX1_CSPICTRL_XCH (1 << 8) |
| 850 | #define MX1_CSPICTRL_ENABLE (1 << 9) |
| 851 | #define MX1_CSPICTRL_MASTER (1 << 10) |
| 852 | #define MX1_CSPICTRL_DR_SHIFT 13 |
| 853 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 854 | static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 855 | { |
| 856 | unsigned int val = 0; |
| 857 | |
| 858 | if (enable & MXC_INT_TE) |
| 859 | val |= MX1_INTREG_TEEN; |
| 860 | if (enable & MXC_INT_RR) |
| 861 | val |= MX1_INTREG_RREN; |
| 862 | |
| 863 | writel(val, spi_imx->base + MXC_CSPIINT); |
| 864 | } |
| 865 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 866 | static void mx1_trigger(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 867 | { |
| 868 | unsigned int reg; |
| 869 | |
| 870 | reg = readl(spi_imx->base + MXC_CSPICTRL); |
| 871 | reg |= MX1_CSPICTRL_XCH; |
| 872 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 873 | } |
| 874 | |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 875 | static int mx1_prepare_message(struct spi_imx_data *spi_imx, |
| 876 | struct spi_message *msg) |
| 877 | { |
| 878 | return 0; |
| 879 | } |
| 880 | |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 881 | static int mx1_prepare_transfer(struct spi_imx_data *spi_imx, |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 882 | struct spi_device *spi) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 883 | { |
| 884 | unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER; |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 885 | unsigned int clk; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 886 | |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 887 | reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) << |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 888 | MX1_CSPICTRL_DR_SHIFT; |
Martin Kaiser | 2636ba8 | 2016-09-01 22:38:40 +0200 | [diff] [blame] | 889 | spi_imx->spi_bus_clk = clk; |
| 890 | |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 891 | reg |= spi_imx->bits_per_word - 1; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 892 | |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 893 | if (spi->mode & SPI_CPHA) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 894 | reg |= MX1_CSPICTRL_PHA; |
Alexander Shiyan | c0c7a5d | 2016-06-08 20:02:07 +0300 | [diff] [blame] | 895 | if (spi->mode & SPI_CPOL) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 896 | reg |= MX1_CSPICTRL_POL; |
| 897 | |
| 898 | writel(reg, spi_imx->base + MXC_CSPICTRL); |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 903 | static int mx1_rx_available(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 904 | { |
| 905 | return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR; |
| 906 | } |
| 907 | |
Alexander Shiyan | f989bc6 | 2016-06-08 20:02:08 +0300 | [diff] [blame] | 908 | static void mx1_reset(struct spi_imx_data *spi_imx) |
Uwe Kleine-König | 1723e66 | 2010-09-10 09:19:18 +0200 | [diff] [blame] | 909 | { |
| 910 | writel(1, spi_imx->base + MXC_RESET); |
| 911 | } |
| 912 | |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 913 | static struct spi_imx_devtype_data imx1_cspi_devtype_data = { |
| 914 | .intctrl = mx1_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 915 | .prepare_message = mx1_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 916 | .prepare_transfer = mx1_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 917 | .trigger = mx1_trigger, |
| 918 | .rx_available = mx1_rx_available, |
| 919 | .reset = mx1_reset, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 920 | .fifo_size = 8, |
| 921 | .has_dmamode = false, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 922 | .dynamic_burst = false, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 923 | .has_slavemode = false, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 924 | .devtype = IMX1_CSPI, |
| 925 | }; |
| 926 | |
| 927 | static struct spi_imx_devtype_data imx21_cspi_devtype_data = { |
| 928 | .intctrl = mx21_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 929 | .prepare_message = mx21_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 930 | .prepare_transfer = mx21_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 931 | .trigger = mx21_trigger, |
| 932 | .rx_available = mx21_rx_available, |
| 933 | .reset = mx21_reset, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 934 | .fifo_size = 8, |
| 935 | .has_dmamode = false, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 936 | .dynamic_burst = false, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 937 | .has_slavemode = false, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 938 | .devtype = IMX21_CSPI, |
| 939 | }; |
| 940 | |
| 941 | static struct spi_imx_devtype_data imx27_cspi_devtype_data = { |
| 942 | /* i.mx27 cspi shares the functions with i.mx21 one */ |
| 943 | .intctrl = mx21_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 944 | .prepare_message = mx21_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 945 | .prepare_transfer = mx21_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 946 | .trigger = mx21_trigger, |
| 947 | .rx_available = mx21_rx_available, |
| 948 | .reset = mx21_reset, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 949 | .fifo_size = 8, |
| 950 | .has_dmamode = false, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 951 | .dynamic_burst = false, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 952 | .has_slavemode = false, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 953 | .devtype = IMX27_CSPI, |
| 954 | }; |
| 955 | |
| 956 | static struct spi_imx_devtype_data imx31_cspi_devtype_data = { |
| 957 | .intctrl = mx31_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 958 | .prepare_message = mx31_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 959 | .prepare_transfer = mx31_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 960 | .trigger = mx31_trigger, |
| 961 | .rx_available = mx31_rx_available, |
| 962 | .reset = mx31_reset, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 963 | .fifo_size = 8, |
| 964 | .has_dmamode = false, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 965 | .dynamic_burst = false, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 966 | .has_slavemode = false, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 967 | .devtype = IMX31_CSPI, |
| 968 | }; |
| 969 | |
| 970 | static struct spi_imx_devtype_data imx35_cspi_devtype_data = { |
| 971 | /* i.mx35 and later cspi shares the functions with i.mx31 one */ |
| 972 | .intctrl = mx31_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 973 | .prepare_message = mx31_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 974 | .prepare_transfer = mx31_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 975 | .trigger = mx31_trigger, |
| 976 | .rx_available = mx31_rx_available, |
| 977 | .reset = mx31_reset, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 978 | .fifo_size = 8, |
| 979 | .has_dmamode = true, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 980 | .dynamic_burst = false, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 981 | .has_slavemode = false, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 982 | .devtype = IMX35_CSPI, |
| 983 | }; |
| 984 | |
| 985 | static struct spi_imx_devtype_data imx51_ecspi_devtype_data = { |
| 986 | .intctrl = mx51_ecspi_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 987 | .prepare_message = mx51_ecspi_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 988 | .prepare_transfer = mx51_ecspi_prepare_transfer, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 989 | .trigger = mx51_ecspi_trigger, |
| 990 | .rx_available = mx51_ecspi_rx_available, |
| 991 | .reset = mx51_ecspi_reset, |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 992 | .setup_wml = mx51_setup_wml, |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 993 | .disable_dma = mx51_disable_dma, |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 994 | .fifo_size = 64, |
| 995 | .has_dmamode = true, |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 996 | .dynamic_burst = true, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 997 | .has_slavemode = true, |
| 998 | .disable = mx51_ecspi_disable, |
Shawn Guo | 04ee585 | 2011-07-10 01:16:39 +0800 | [diff] [blame] | 999 | .devtype = IMX51_ECSPI, |
| 1000 | }; |
| 1001 | |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1002 | static struct spi_imx_devtype_data imx53_ecspi_devtype_data = { |
| 1003 | .intctrl = mx51_ecspi_intctrl, |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 1004 | .prepare_message = mx51_ecspi_prepare_message, |
Uwe Kleine-König | 1d37470 | 2018-11-30 07:47:08 +0100 | [diff] [blame] | 1005 | .prepare_transfer = mx51_ecspi_prepare_transfer, |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1006 | .trigger = mx51_ecspi_trigger, |
| 1007 | .rx_available = mx51_ecspi_rx_available, |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 1008 | .disable_dma = mx51_disable_dma, |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1009 | .reset = mx51_ecspi_reset, |
| 1010 | .fifo_size = 64, |
| 1011 | .has_dmamode = true, |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1012 | .has_slavemode = true, |
| 1013 | .disable = mx51_ecspi_disable, |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1014 | .devtype = IMX53_ECSPI, |
| 1015 | }; |
| 1016 | |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1017 | static const struct of_device_id spi_imx_dt_ids[] = { |
| 1018 | { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, }, |
| 1019 | { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, }, |
| 1020 | { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, }, |
| 1021 | { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, }, |
| 1022 | { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, }, |
| 1023 | { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, }, |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1024 | { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, }, |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1025 | { /* sentinel */ } |
| 1026 | }; |
Niels de Vos | 27743e0 | 2013-07-29 09:38:05 +0200 | [diff] [blame] | 1027 | MODULE_DEVICE_TABLE(of, spi_imx_dt_ids); |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1028 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1029 | static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits) |
| 1030 | { |
| 1031 | u32 ctrl; |
| 1032 | |
| 1033 | ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL); |
| 1034 | ctrl &= ~MX51_ECSPI_CTRL_BL_MASK; |
| 1035 | ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET); |
| 1036 | writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); |
| 1037 | } |
| 1038 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1039 | static void spi_imx_push(struct spi_imx_data *spi_imx) |
| 1040 | { |
Uwe Kleine-König | bd96169 | 2021-07-16 19:39:27 +0200 | [diff] [blame] | 1041 | unsigned int burst_len; |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1042 | |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1043 | /* |
| 1044 | * Reload the FIFO when the remaining bytes to be transferred in the |
| 1045 | * current burst is 0. This only applies when bits_per_word is a |
| 1046 | * multiple of 8. |
| 1047 | */ |
| 1048 | if (!spi_imx->remainder) { |
| 1049 | if (spi_imx->dynamic_burst) { |
| 1050 | |
| 1051 | /* We need to deal unaligned data first */ |
| 1052 | burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST; |
| 1053 | |
| 1054 | if (!burst_len) |
| 1055 | burst_len = MX51_ECSPI_CTRL_MAX_BURST; |
| 1056 | |
| 1057 | spi_imx_set_burst_len(spi_imx, burst_len * 8); |
| 1058 | |
| 1059 | spi_imx->remainder = burst_len; |
| 1060 | } else { |
Uwe Kleine-König | bd96169 | 2021-07-16 19:39:27 +0200 | [diff] [blame] | 1061 | spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word); |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 1065 | while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1066 | if (!spi_imx->count) |
| 1067 | break; |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1068 | if (spi_imx->dynamic_burst && |
Uwe Kleine-König | bd96169 | 2021-07-16 19:39:27 +0200 | [diff] [blame] | 1069 | spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4)) |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 1070 | break; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1071 | spi_imx->tx(spi_imx); |
| 1072 | spi_imx->txfifo++; |
| 1073 | } |
| 1074 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1075 | if (!spi_imx->slave_mode) |
| 1076 | spi_imx->devtype_data->trigger(spi_imx); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static irqreturn_t spi_imx_isr(int irq, void *dev_id) |
| 1080 | { |
| 1081 | struct spi_imx_data *spi_imx = dev_id; |
| 1082 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1083 | while (spi_imx->txfifo && |
| 1084 | spi_imx->devtype_data->rx_available(spi_imx)) { |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1085 | spi_imx->rx(spi_imx); |
| 1086 | spi_imx->txfifo--; |
| 1087 | } |
| 1088 | |
| 1089 | if (spi_imx->count) { |
| 1090 | spi_imx_push(spi_imx); |
| 1091 | return IRQ_HANDLED; |
| 1092 | } |
| 1093 | |
| 1094 | if (spi_imx->txfifo) { |
| 1095 | /* No data left to push, but still waiting for rx data, |
| 1096 | * enable receive data available interrupt. |
| 1097 | */ |
Shawn Guo | edd501bb | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 1098 | spi_imx->devtype_data->intctrl( |
Uwe Kleine-König | f4ba631 | 2010-09-09 15:29:01 +0200 | [diff] [blame] | 1099 | spi_imx, MXC_INT_RR); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1100 | return IRQ_HANDLED; |
| 1101 | } |
| 1102 | |
Shawn Guo | edd501bb | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 1103 | spi_imx->devtype_data->intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1104 | complete(&spi_imx->xfer_done); |
| 1105 | |
| 1106 | return IRQ_HANDLED; |
| 1107 | } |
| 1108 | |
Sascha Hauer | 65017ee | 2017-06-02 07:38:03 +0200 | [diff] [blame] | 1109 | static int spi_imx_dma_configure(struct spi_master *master) |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1110 | { |
| 1111 | int ret; |
| 1112 | enum dma_slave_buswidth buswidth; |
| 1113 | struct dma_slave_config rx = {}, tx = {}; |
| 1114 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 1115 | |
Sascha Hauer | 65017ee | 2017-06-02 07:38:03 +0200 | [diff] [blame] | 1116 | switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) { |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1117 | case 4: |
| 1118 | buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1119 | break; |
| 1120 | case 2: |
| 1121 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 1122 | break; |
| 1123 | case 1: |
| 1124 | buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1125 | break; |
| 1126 | default: |
| 1127 | return -EINVAL; |
| 1128 | } |
| 1129 | |
| 1130 | tx.direction = DMA_MEM_TO_DEV; |
| 1131 | tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA; |
| 1132 | tx.dst_addr_width = buswidth; |
| 1133 | tx.dst_maxburst = spi_imx->wml; |
| 1134 | ret = dmaengine_slave_config(master->dma_tx, &tx); |
| 1135 | if (ret) { |
| 1136 | dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | rx.direction = DMA_DEV_TO_MEM; |
| 1141 | rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA; |
| 1142 | rx.src_addr_width = buswidth; |
| 1143 | rx.src_maxburst = spi_imx->wml; |
| 1144 | ret = dmaengine_slave_config(master->dma_rx, &rx); |
| 1145 | if (ret) { |
| 1146 | dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1150 | return 0; |
| 1151 | } |
| 1152 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1153 | static int spi_imx_setupxfer(struct spi_device *spi, |
| 1154 | struct spi_transfer *t) |
| 1155 | { |
| 1156 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1157 | |
Sascha Hauer | abb1ff1 | 2017-06-02 07:37:59 +0200 | [diff] [blame] | 1158 | if (!t) |
| 1159 | return 0; |
| 1160 | |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 1161 | if (!t->speed_hz) { |
| 1162 | if (!spi->max_speed_hz) { |
| 1163 | dev_err(&spi->dev, "no speed_hz provided!\n"); |
| 1164 | return -EINVAL; |
| 1165 | } |
| 1166 | dev_dbg(&spi->dev, "using spi->max_speed_hz!\n"); |
| 1167 | spi_imx->spi_bus_clk = spi->max_speed_hz; |
| 1168 | } else |
| 1169 | spi_imx->spi_bus_clk = t->speed_hz; |
| 1170 | |
Sascha Hauer | d52345b | 2017-06-02 07:38:01 +0200 | [diff] [blame] | 1171 | spi_imx->bits_per_word = t->bits_per_word; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1172 | |
Maxime Chevallier | 2801b2f5 | 2018-07-17 16:31:51 +0200 | [diff] [blame] | 1173 | /* |
| 1174 | * Initialize the functions for transfer. To transfer non byte-aligned |
| 1175 | * words, we have to use multiple word-size bursts, we can't use |
| 1176 | * dynamic_burst in that case. |
| 1177 | */ |
| 1178 | if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode && |
Uwe Kleine-König | 6e95b23 | 2021-07-27 14:42:26 +0200 | [diff] [blame^] | 1179 | !(spi->mode & SPI_CS_WORD) && |
Maxime Chevallier | 2801b2f5 | 2018-07-17 16:31:51 +0200 | [diff] [blame] | 1180 | (spi_imx->bits_per_word == 8 || |
| 1181 | spi_imx->bits_per_word == 16 || |
| 1182 | spi_imx->bits_per_word == 32)) { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 1183 | |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 1184 | spi_imx->rx = spi_imx_buf_rx_swap; |
| 1185 | spi_imx->tx = spi_imx_buf_tx_swap; |
| 1186 | spi_imx->dynamic_burst = 1; |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 1187 | |
Sachin Kamat | 6051426 | 2013-05-30 13:38:09 +0530 | [diff] [blame] | 1188 | } else { |
jiada wang | 1673c81 | 2017-08-10 13:50:08 +0900 | [diff] [blame] | 1189 | if (spi_imx->bits_per_word <= 8) { |
| 1190 | spi_imx->rx = spi_imx_buf_rx_u8; |
| 1191 | spi_imx->tx = spi_imx_buf_tx_u8; |
| 1192 | } else if (spi_imx->bits_per_word <= 16) { |
| 1193 | spi_imx->rx = spi_imx_buf_rx_u16; |
| 1194 | spi_imx->tx = spi_imx_buf_tx_u16; |
| 1195 | } else { |
| 1196 | spi_imx->rx = spi_imx_buf_rx_u32; |
| 1197 | spi_imx->tx = spi_imx_buf_tx_u32; |
| 1198 | } |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1199 | spi_imx->dynamic_burst = 0; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1200 | } |
Uwe Kleine-König | e6a0a8b | 2009-10-01 15:44:33 -0700 | [diff] [blame] | 1201 | |
Sascha Hauer | c008a80 | 2016-02-24 09:20:26 +0100 | [diff] [blame] | 1202 | if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t)) |
zhengbin | e6a8b2c | 2019-12-24 11:52:05 +0800 | [diff] [blame] | 1203 | spi_imx->usedma = true; |
Sascha Hauer | c008a80 | 2016-02-24 09:20:26 +0100 | [diff] [blame] | 1204 | else |
zhengbin | e6a8b2c | 2019-12-24 11:52:05 +0800 | [diff] [blame] | 1205 | spi_imx->usedma = false; |
Sascha Hauer | c008a80 | 2016-02-24 09:20:26 +0100 | [diff] [blame] | 1206 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1207 | if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) { |
| 1208 | spi_imx->rx = mx53_ecspi_rx_slave; |
| 1209 | spi_imx->tx = mx53_ecspi_tx_slave; |
| 1210 | spi_imx->slave_burst = t->len; |
| 1211 | } |
| 1212 | |
Clark Wang | 4df2f5e | 2021-04-08 18:33:47 +0800 | [diff] [blame] | 1213 | spi_imx->devtype_data->prepare_transfer(spi_imx, spi); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1218 | static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx) |
| 1219 | { |
| 1220 | struct spi_master *master = spi_imx->bitbang.master; |
| 1221 | |
| 1222 | if (master->dma_rx) { |
| 1223 | dma_release_channel(master->dma_rx); |
| 1224 | master->dma_rx = NULL; |
| 1225 | } |
| 1226 | |
| 1227 | if (master->dma_tx) { |
| 1228 | dma_release_channel(master->dma_tx); |
| 1229 | master->dma_tx = NULL; |
| 1230 | } |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx, |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1234 | struct spi_master *master) |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1235 | { |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1236 | int ret; |
| 1237 | |
Robin Gong | a02bb40 | 2015-02-03 10:25:53 +0800 | [diff] [blame] | 1238 | /* use pio mode for i.mx6dl chip TKT238285 */ |
| 1239 | if (of_machine_is_compatible("fsl,imx6dl")) |
| 1240 | return 0; |
| 1241 | |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 1242 | spi_imx->wml = spi_imx->devtype_data->fifo_size / 2; |
Anton Bondarenko | 0dfbaa8 | 2015-12-05 17:57:01 +0100 | [diff] [blame] | 1243 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1244 | /* Prepare for TX DMA: */ |
Peter Ujfalusi | 5d3aa9c | 2019-11-13 11:42:51 +0200 | [diff] [blame] | 1245 | master->dma_tx = dma_request_chan(dev, "tx"); |
Anton Bondarenko | 3760047 | 2015-12-08 07:43:45 +0100 | [diff] [blame] | 1246 | if (IS_ERR(master->dma_tx)) { |
| 1247 | ret = PTR_ERR(master->dma_tx); |
| 1248 | dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret); |
| 1249 | master->dma_tx = NULL; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1250 | goto err; |
| 1251 | } |
| 1252 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1253 | /* Prepare for RX : */ |
Peter Ujfalusi | 5d3aa9c | 2019-11-13 11:42:51 +0200 | [diff] [blame] | 1254 | master->dma_rx = dma_request_chan(dev, "rx"); |
Anton Bondarenko | 3760047 | 2015-12-08 07:43:45 +0100 | [diff] [blame] | 1255 | if (IS_ERR(master->dma_rx)) { |
| 1256 | ret = PTR_ERR(master->dma_rx); |
| 1257 | dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret); |
| 1258 | master->dma_rx = NULL; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1259 | goto err; |
| 1260 | } |
| 1261 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1262 | init_completion(&spi_imx->dma_rx_completion); |
| 1263 | init_completion(&spi_imx->dma_tx_completion); |
| 1264 | master->can_dma = spi_imx_can_dma; |
| 1265 | master->max_dma_len = MAX_SDMA_BD_BYTES; |
| 1266 | spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX | |
| 1267 | SPI_MASTER_MUST_TX; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1268 | |
| 1269 | return 0; |
| 1270 | err: |
| 1271 | spi_imx_sdma_exit(spi_imx); |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | static void spi_imx_dma_rx_callback(void *cookie) |
| 1276 | { |
| 1277 | struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie; |
| 1278 | |
| 1279 | complete(&spi_imx->dma_rx_completion); |
| 1280 | } |
| 1281 | |
| 1282 | static void spi_imx_dma_tx_callback(void *cookie) |
| 1283 | { |
| 1284 | struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie; |
| 1285 | |
| 1286 | complete(&spi_imx->dma_tx_completion); |
| 1287 | } |
| 1288 | |
Anton Bondarenko | 4bfe927 | 2016-02-19 08:43:03 +0100 | [diff] [blame] | 1289 | static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size) |
| 1290 | { |
| 1291 | unsigned long timeout = 0; |
| 1292 | |
| 1293 | /* Time with actual data transfer and CS change delay related to HW */ |
| 1294 | timeout = (8 + 4) * size / spi_imx->spi_bus_clk; |
| 1295 | |
| 1296 | /* Add extra second for scheduler related activities */ |
| 1297 | timeout += 1; |
| 1298 | |
| 1299 | /* Double calculated timeout */ |
| 1300 | return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC); |
| 1301 | } |
| 1302 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1303 | static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, |
| 1304 | struct spi_transfer *transfer) |
| 1305 | { |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1306 | struct dma_async_tx_descriptor *desc_tx, *desc_rx; |
Anton Bondarenko | 4bfe927 | 2016-02-19 08:43:03 +0100 | [diff] [blame] | 1307 | unsigned long transfer_timeout; |
Nicholas Mc Guire | 56536a7 | 2015-02-02 03:30:35 -0500 | [diff] [blame] | 1308 | unsigned long timeout; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1309 | struct spi_master *master = spi_imx->bitbang.master; |
| 1310 | struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg; |
Robin Gong | 5ba5a37 | 2018-10-10 10:32:45 +0000 | [diff] [blame] | 1311 | struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents); |
| 1312 | unsigned int bytes_per_word, i; |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 1313 | int ret; |
| 1314 | |
Robin Gong | 5ba5a37 | 2018-10-10 10:32:45 +0000 | [diff] [blame] | 1315 | /* Get the right burst length from the last sg to ensure no tail data */ |
| 1316 | bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word); |
| 1317 | for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) { |
| 1318 | if (!(sg_dma_len(last_sg) % (i * bytes_per_word))) |
| 1319 | break; |
| 1320 | } |
| 1321 | /* Use 1 as wml in case no available burst length got */ |
| 1322 | if (i == 0) |
| 1323 | i = 1; |
| 1324 | |
| 1325 | spi_imx->wml = i; |
| 1326 | |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 1327 | ret = spi_imx_dma_configure(master); |
| 1328 | if (ret) |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 1329 | goto dma_failure_no_start; |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 1330 | |
Robin Gong | 5ba5a37 | 2018-10-10 10:32:45 +0000 | [diff] [blame] | 1331 | if (!spi_imx->devtype_data->setup_wml) { |
| 1332 | dev_err(spi_imx->dev, "No setup_wml()?\n"); |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 1333 | ret = -EINVAL; |
| 1334 | goto dma_failure_no_start; |
Robin Gong | 5ba5a37 | 2018-10-10 10:32:45 +0000 | [diff] [blame] | 1335 | } |
Robin Gong | 987a2df | 2018-10-10 10:32:42 +0000 | [diff] [blame] | 1336 | spi_imx->devtype_data->setup_wml(spi_imx); |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1337 | |
Anton Bondarenko | fab44ef | 2015-12-05 17:57:00 +0100 | [diff] [blame] | 1338 | /* |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1339 | * The TX DMA setup starts the transfer, so make sure RX is configured |
| 1340 | * before TX. |
Anton Bondarenko | fab44ef | 2015-12-05 17:57:00 +0100 | [diff] [blame] | 1341 | */ |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1342 | desc_rx = dmaengine_prep_slave_sg(master->dma_rx, |
| 1343 | rx->sgl, rx->nents, DMA_DEV_TO_MEM, |
| 1344 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 1345 | if (!desc_rx) { |
| 1346 | ret = -EINVAL; |
| 1347 | goto dma_failure_no_start; |
| 1348 | } |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1349 | |
| 1350 | desc_rx->callback = spi_imx_dma_rx_callback; |
| 1351 | desc_rx->callback_param = (void *)spi_imx; |
| 1352 | dmaengine_submit(desc_rx); |
| 1353 | reinit_completion(&spi_imx->dma_rx_completion); |
Anton Bondarenko | fab44ef | 2015-12-05 17:57:00 +0100 | [diff] [blame] | 1354 | dma_async_issue_pending(master->dma_rx); |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1355 | |
| 1356 | desc_tx = dmaengine_prep_slave_sg(master->dma_tx, |
| 1357 | tx->sgl, tx->nents, DMA_MEM_TO_DEV, |
| 1358 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1359 | if (!desc_tx) { |
| 1360 | dmaengine_terminate_all(master->dma_tx); |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 1361 | dmaengine_terminate_all(master->dma_rx); |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1362 | return -EINVAL; |
| 1363 | } |
| 1364 | |
| 1365 | desc_tx->callback = spi_imx_dma_tx_callback; |
| 1366 | desc_tx->callback_param = (void *)spi_imx; |
| 1367 | dmaengine_submit(desc_tx); |
| 1368 | reinit_completion(&spi_imx->dma_tx_completion); |
Anton Bondarenko | fab44ef | 2015-12-05 17:57:00 +0100 | [diff] [blame] | 1369 | dma_async_issue_pending(master->dma_tx); |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1370 | |
Anton Bondarenko | 4bfe927 | 2016-02-19 08:43:03 +0100 | [diff] [blame] | 1371 | transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len); |
| 1372 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1373 | /* Wait SDMA to finish the data transfer.*/ |
Nicholas Mc Guire | 56536a7 | 2015-02-02 03:30:35 -0500 | [diff] [blame] | 1374 | timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion, |
Anton Bondarenko | 4bfe927 | 2016-02-19 08:43:03 +0100 | [diff] [blame] | 1375 | transfer_timeout); |
Nicholas Mc Guire | 56536a7 | 2015-02-02 03:30:35 -0500 | [diff] [blame] | 1376 | if (!timeout) { |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 1377 | dev_err(spi_imx->dev, "I/O Error in DMA TX\n"); |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1378 | dmaengine_terminate_all(master->dma_tx); |
Anton Bondarenko | e47b33c | 2015-12-05 17:56:59 +0100 | [diff] [blame] | 1379 | dmaengine_terminate_all(master->dma_rx); |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1380 | return -ETIMEDOUT; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1383 | timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion, |
| 1384 | transfer_timeout); |
| 1385 | if (!timeout) { |
| 1386 | dev_err(&master->dev, "I/O Error in DMA RX\n"); |
| 1387 | spi_imx->devtype_data->reset(spi_imx); |
| 1388 | dmaengine_terminate_all(master->dma_rx); |
| 1389 | return -ETIMEDOUT; |
| 1390 | } |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1391 | |
Sascha Hauer | 6b6192c | 2016-02-24 09:20:33 +0100 | [diff] [blame] | 1392 | return transfer->len; |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 1393 | /* fallback to pio */ |
| 1394 | dma_failure_no_start: |
| 1395 | transfer->error |= SPI_TRANS_FAIL_NO_START; |
| 1396 | return ret; |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static int spi_imx_pio_transfer(struct spi_device *spi, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1400 | struct spi_transfer *transfer) |
| 1401 | { |
| 1402 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
Christian Gmeiner | ff1ba3d | 2016-06-21 14:12:54 +0200 | [diff] [blame] | 1403 | unsigned long transfer_timeout; |
| 1404 | unsigned long timeout; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1405 | |
| 1406 | spi_imx->tx_buf = transfer->tx_buf; |
| 1407 | spi_imx->rx_buf = transfer->rx_buf; |
| 1408 | spi_imx->count = transfer->len; |
| 1409 | spi_imx->txfifo = 0; |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1410 | spi_imx->remainder = 0; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1411 | |
Axel Lin | aa0fe82 | 2014-02-09 11:06:04 +0800 | [diff] [blame] | 1412 | reinit_completion(&spi_imx->xfer_done); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1413 | |
| 1414 | spi_imx_push(spi_imx); |
| 1415 | |
Shawn Guo | edd501bb | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 1416 | spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1417 | |
Christian Gmeiner | ff1ba3d | 2016-06-21 14:12:54 +0200 | [diff] [blame] | 1418 | transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len); |
| 1419 | |
| 1420 | timeout = wait_for_completion_timeout(&spi_imx->xfer_done, |
| 1421 | transfer_timeout); |
| 1422 | if (!timeout) { |
| 1423 | dev_err(&spi->dev, "I/O Error in PIO\n"); |
| 1424 | spi_imx->devtype_data->reset(spi_imx); |
| 1425 | return -ETIMEDOUT; |
| 1426 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1427 | |
| 1428 | return transfer->len; |
| 1429 | } |
| 1430 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1431 | static int spi_imx_pio_transfer_slave(struct spi_device *spi, |
| 1432 | struct spi_transfer *transfer) |
| 1433 | { |
| 1434 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 1435 | int ret = transfer->len; |
| 1436 | |
| 1437 | if (is_imx53_ecspi(spi_imx) && |
| 1438 | transfer->len > MX53_MAX_TRANSFER_BYTES) { |
| 1439 | dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n", |
| 1440 | MX53_MAX_TRANSFER_BYTES); |
| 1441 | return -EMSGSIZE; |
| 1442 | } |
| 1443 | |
| 1444 | spi_imx->tx_buf = transfer->tx_buf; |
| 1445 | spi_imx->rx_buf = transfer->rx_buf; |
| 1446 | spi_imx->count = transfer->len; |
| 1447 | spi_imx->txfifo = 0; |
Maxime Chevallier | 2ca300a | 2018-07-17 16:31:54 +0200 | [diff] [blame] | 1448 | spi_imx->remainder = 0; |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1449 | |
| 1450 | reinit_completion(&spi_imx->xfer_done); |
| 1451 | spi_imx->slave_aborted = false; |
| 1452 | |
| 1453 | spi_imx_push(spi_imx); |
| 1454 | |
| 1455 | spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR); |
| 1456 | |
| 1457 | if (wait_for_completion_interruptible(&spi_imx->xfer_done) || |
| 1458 | spi_imx->slave_aborted) { |
| 1459 | dev_dbg(&spi->dev, "interrupted\n"); |
| 1460 | ret = -EINTR; |
| 1461 | } |
| 1462 | |
| 1463 | /* ecspi has a HW issue when works in Slave mode, |
| 1464 | * after 64 words writtern to TXFIFO, even TXFIFO becomes empty, |
| 1465 | * ECSPI_TXDATA keeps shift out the last word data, |
| 1466 | * so we have to disable ECSPI when in slave mode after the |
| 1467 | * transfer completes |
| 1468 | */ |
| 1469 | if (spi_imx->devtype_data->disable) |
| 1470 | spi_imx->devtype_data->disable(spi_imx); |
| 1471 | |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1475 | static int spi_imx_transfer(struct spi_device *spi, |
| 1476 | struct spi_transfer *transfer) |
| 1477 | { |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1478 | struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); |
| 1479 | |
Marc Kleine-Budde | bf253e6 | 2020-09-17 22:24:20 +0200 | [diff] [blame] | 1480 | transfer->effective_speed_hz = spi_imx->spi_bus_clk; |
| 1481 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1482 | /* flush rxfifo before transfer */ |
| 1483 | while (spi_imx->devtype_data->rx_available(spi_imx)) |
Trent Piepho | c842749 | 2019-03-04 20:18:49 +0000 | [diff] [blame] | 1484 | readl(spi_imx->base + MXC_CSPIRXDATA); |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1485 | |
| 1486 | if (spi_imx->slave_mode) |
| 1487 | return spi_imx_pio_transfer_slave(spi, transfer); |
| 1488 | |
Robin Gong | 7a90883 | 2020-06-17 06:42:09 +0800 | [diff] [blame] | 1489 | if (spi_imx->usedma) |
| 1490 | return spi_imx_dma_transfer(spi_imx, transfer); |
Robin Gong | bcd8e77 | 2020-05-21 04:34:17 +0800 | [diff] [blame] | 1491 | |
| 1492 | return spi_imx_pio_transfer(spi, transfer); |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1495 | static int spi_imx_setup(struct spi_device *spi) |
| 1496 | { |
Alberto Panizzo | f4d4ecf | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 1497 | dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1498 | spi->mode, spi->bits_per_word, spi->max_speed_hz); |
| 1499 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static void spi_imx_cleanup(struct spi_device *spi) |
| 1504 | { |
| 1505 | } |
| 1506 | |
Huang Shijie | 9e556dc | 2013-10-23 16:31:50 +0800 | [diff] [blame] | 1507 | static int |
| 1508 | spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg) |
| 1509 | { |
| 1510 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 1511 | int ret; |
| 1512 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1513 | ret = pm_runtime_get_sync(spi_imx->dev); |
| 1514 | if (ret < 0) { |
Zhang Qilong | 1dcbdd9 | 2020-11-02 22:58:35 +0800 | [diff] [blame] | 1515 | pm_runtime_put_noidle(spi_imx->dev); |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1516 | dev_err(spi_imx->dev, "failed to enable clock\n"); |
Huang Shijie | 9e556dc | 2013-10-23 16:31:50 +0800 | [diff] [blame] | 1517 | return ret; |
| 1518 | } |
| 1519 | |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 1520 | ret = spi_imx->devtype_data->prepare_message(spi_imx, msg); |
| 1521 | if (ret) { |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1522 | pm_runtime_mark_last_busy(spi_imx->dev); |
| 1523 | pm_runtime_put_autosuspend(spi_imx->dev); |
Uwe Kleine-König | e697271 | 2018-11-30 07:47:05 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | return ret; |
Huang Shijie | 9e556dc | 2013-10-23 16:31:50 +0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | static int |
| 1530 | spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg) |
| 1531 | { |
| 1532 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 1533 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1534 | pm_runtime_mark_last_busy(spi_imx->dev); |
| 1535 | pm_runtime_put_autosuspend(spi_imx->dev); |
Huang Shijie | 9e556dc | 2013-10-23 16:31:50 +0800 | [diff] [blame] | 1536 | return 0; |
| 1537 | } |
| 1538 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1539 | static int spi_imx_slave_abort(struct spi_master *master) |
| 1540 | { |
| 1541 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
| 1542 | |
| 1543 | spi_imx->slave_aborted = true; |
| 1544 | complete(&spi_imx->xfer_done); |
| 1545 | |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1549 | static int spi_imx_probe(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1550 | { |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1551 | struct device_node *np = pdev->dev.of_node; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1552 | struct spi_master *master; |
| 1553 | struct spi_imx_data *spi_imx; |
| 1554 | struct resource *res; |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 1555 | int ret, irq, spi_drctl; |
Tian Tao | 200d925e | 2021-03-22 11:57:56 +0800 | [diff] [blame] | 1556 | const struct spi_imx_devtype_data *devtype_data = |
| 1557 | of_device_get_match_data(&pdev->dev); |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1558 | bool slave_mode; |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 1559 | u32 val; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1560 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1561 | slave_mode = devtype_data->has_slavemode && |
| 1562 | of_property_read_bool(np, "spi-slave"); |
| 1563 | if (slave_mode) |
| 1564 | master = spi_alloc_slave(&pdev->dev, |
| 1565 | sizeof(struct spi_imx_data)); |
| 1566 | else |
| 1567 | master = spi_alloc_master(&pdev->dev, |
| 1568 | sizeof(struct spi_imx_data)); |
Fabio Estevam | 2c14777 | 2017-06-20 13:50:55 -0300 | [diff] [blame] | 1569 | if (!master) |
| 1570 | return -ENOMEM; |
| 1571 | |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 1572 | ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl); |
| 1573 | if ((ret < 0) || (spi_drctl >= 0x3)) { |
| 1574 | /* '11' is reserved */ |
| 1575 | spi_drctl = 0; |
| 1576 | } |
| 1577 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1578 | platform_set_drvdata(pdev, master); |
| 1579 | |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1580 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); |
Alexander Shiyan | b36581d | 2016-06-08 20:02:06 +0300 | [diff] [blame] | 1581 | master->bus_num = np ? -1 : pdev->id; |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 1582 | master->use_gpio_descriptors = true; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1583 | |
| 1584 | spi_imx = spi_master_get_devdata(master); |
Axel Lin | 94c69f7 | 2013-09-10 15:43:41 +0800 | [diff] [blame] | 1585 | spi_imx->bitbang.master = master; |
Sascha Hauer | 6aa800c | 2016-02-17 14:28:48 +0100 | [diff] [blame] | 1586 | spi_imx->dev = &pdev->dev; |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1587 | spi_imx->slave_mode = slave_mode; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1588 | |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1589 | spi_imx->devtype_data = devtype_data; |
Anton Bondarenko | 4686d1c | 2015-12-08 07:43:44 +0100 | [diff] [blame] | 1590 | |
Linus Walleij | 8cdcd8a | 2020-06-25 22:02:52 +0200 | [diff] [blame] | 1591 | /* |
| 1592 | * Get number of chip selects from device properties. This can be |
| 1593 | * coming from device tree or boardfiles, if it is not defined, |
| 1594 | * a default value of 3 chip selects will be used, as all the legacy |
| 1595 | * board files have <= 3 chip selects. |
| 1596 | */ |
| 1597 | if (!device_property_read_u32(&pdev->dev, "num-cs", &val)) |
| 1598 | master->num_chipselect = val; |
| 1599 | else |
| 1600 | master->num_chipselect = 3; |
Fabio Estevam | 4cc122a | 2011-09-15 17:21:15 -0300 | [diff] [blame] | 1601 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1602 | spi_imx->bitbang.setup_transfer = spi_imx_setupxfer; |
| 1603 | spi_imx->bitbang.txrx_bufs = spi_imx_transfer; |
| 1604 | spi_imx->bitbang.master->setup = spi_imx_setup; |
| 1605 | spi_imx->bitbang.master->cleanup = spi_imx_cleanup; |
Huang Shijie | 9e556dc | 2013-10-23 16:31:50 +0800 | [diff] [blame] | 1606 | spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message; |
| 1607 | spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message; |
jiada wang | 71abd29 | 2017-09-05 14:12:32 +0900 | [diff] [blame] | 1608 | spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort; |
Oleksij Rempel | ab2f357 | 2017-07-25 09:57:09 +0200 | [diff] [blame] | 1609 | spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \ |
| 1610 | | SPI_NO_CS; |
jiada wang | 26e4bb8 | 2017-06-08 14:16:01 +0900 | [diff] [blame] | 1611 | if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) || |
| 1612 | is_imx53_ecspi(spi_imx)) |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 1613 | spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY; |
| 1614 | |
Uwe Kleine-König | 6e95b23 | 2021-07-27 14:42:26 +0200 | [diff] [blame^] | 1615 | if (is_imx51_ecspi(spi_imx) && |
| 1616 | device_property_read_u32(&pdev->dev, "cs-gpios", NULL)) |
| 1617 | /* |
| 1618 | * When using HW-CS implementing SPI_CS_WORD can be done by just |
| 1619 | * setting the burst length to the word size. This is |
| 1620 | * considerably faster than manually controlling the CS. |
| 1621 | */ |
| 1622 | spi_imx->bitbang.master->mode_bits |= SPI_CS_WORD; |
| 1623 | |
Leif Middelschulte | f72efa7 | 2017-04-23 21:19:58 +0200 | [diff] [blame] | 1624 | spi_imx->spi_drctl = spi_drctl; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1625 | |
| 1626 | init_completion(&spi_imx->xfer_done); |
| 1627 | |
| 1628 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1629 | spi_imx->base = devm_ioremap_resource(&pdev->dev, res); |
| 1630 | if (IS_ERR(spi_imx->base)) { |
| 1631 | ret = PTR_ERR(spi_imx->base); |
| 1632 | goto out_master_put; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1633 | } |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1634 | spi_imx->base_phys = res->start; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1635 | |
Fabio Estevam | 4b5d6aa | 2014-12-29 19:38:51 -0200 | [diff] [blame] | 1636 | irq = platform_get_irq(pdev, 0); |
| 1637 | if (irq < 0) { |
| 1638 | ret = irq; |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1639 | goto out_master_put; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Fabio Estevam | 4b5d6aa | 2014-12-29 19:38:51 -0200 | [diff] [blame] | 1642 | ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0, |
Alexander Shiyan | 8fc39b5 | 2014-02-22 17:23:46 +0400 | [diff] [blame] | 1643 | dev_name(&pdev->dev), spi_imx); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1644 | if (ret) { |
Fabio Estevam | 4b5d6aa | 2014-12-29 19:38:51 -0200 | [diff] [blame] | 1645 | dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1646 | goto out_master_put; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Sascha Hauer | aa29d840 | 2012-03-07 09:30:22 +0100 | [diff] [blame] | 1649 | spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1650 | if (IS_ERR(spi_imx->clk_ipg)) { |
| 1651 | ret = PTR_ERR(spi_imx->clk_ipg); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1652 | goto out_master_put; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Sascha Hauer | aa29d840 | 2012-03-07 09:30:22 +0100 | [diff] [blame] | 1655 | spi_imx->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 1656 | if (IS_ERR(spi_imx->clk_per)) { |
| 1657 | ret = PTR_ERR(spi_imx->clk_per); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1658 | goto out_master_put; |
Sascha Hauer | aa29d840 | 2012-03-07 09:30:22 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
Sascha Hauer | 43b6bf4 | 2020-10-21 12:45:13 +0200 | [diff] [blame] | 1661 | ret = clk_prepare_enable(spi_imx->clk_per); |
| 1662 | if (ret) |
| 1663 | goto out_master_put; |
| 1664 | |
| 1665 | ret = clk_prepare_enable(spi_imx->clk_ipg); |
| 1666 | if (ret) |
| 1667 | goto out_put_per; |
| 1668 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1669 | pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT); |
| 1670 | pm_runtime_use_autosuspend(spi_imx->dev); |
Clark Wang | 7cd7120 | 2020-11-24 16:52:47 +0800 | [diff] [blame] | 1671 | pm_runtime_get_noresume(spi_imx->dev); |
Sascha Hauer | 43b6bf4 | 2020-10-21 12:45:13 +0200 | [diff] [blame] | 1672 | pm_runtime_set_active(spi_imx->dev); |
| 1673 | pm_runtime_enable(spi_imx->dev); |
Sascha Hauer | aa29d840 | 2012-03-07 09:30:22 +0100 | [diff] [blame] | 1674 | |
| 1675 | spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per); |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1676 | /* |
Martin Kaiser | 2dd33f9 | 2016-10-20 00:42:25 +0200 | [diff] [blame] | 1677 | * Only validated on i.mx35 and i.mx6 now, can remove the constraint |
| 1678 | * if validated on other chips. |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1679 | */ |
jiada wang | fd8d4e2 | 2017-06-08 14:16:00 +0900 | [diff] [blame] | 1680 | if (spi_imx->devtype_data->has_dmamode) { |
Anton Bondarenko | f12ae17 | 2016-02-24 09:20:29 +0100 | [diff] [blame] | 1681 | ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master); |
Anton Bondarenko | bf9af08 | 2015-12-08 07:43:46 +0100 | [diff] [blame] | 1682 | if (ret == -EPROBE_DEFER) |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1683 | goto out_runtime_pm_put; |
Anton Bondarenko | bf9af08 | 2015-12-08 07:43:46 +0100 | [diff] [blame] | 1684 | |
Anton Bondarenko | 3760047 | 2015-12-08 07:43:45 +0100 | [diff] [blame] | 1685 | if (ret < 0) |
Fabio Estevam | 0ec0da7 | 2020-08-18 19:35:18 -0300 | [diff] [blame] | 1686 | dev_dbg(&pdev->dev, "dma setup error %d, use pio\n", |
Anton Bondarenko | 3760047 | 2015-12-08 07:43:45 +0100 | [diff] [blame] | 1687 | ret); |
| 1688 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1689 | |
Shawn Guo | edd501bb | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 1690 | spi_imx->devtype_data->reset(spi_imx); |
Daniel Mack | ce1807b | 2009-11-19 19:01:42 +0000 | [diff] [blame] | 1691 | |
Shawn Guo | edd501bb | 2011-07-10 01:16:35 +0800 | [diff] [blame] | 1692 | spi_imx->devtype_data->intctrl(spi_imx, 0); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1693 | |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1694 | master->dev.of_node = pdev->dev.of_node; |
Trent Piepho | 8197f48 | 2017-11-06 10:38:23 -0800 | [diff] [blame] | 1695 | ret = spi_bitbang_start(&spi_imx->bitbang); |
| 1696 | if (ret) { |
Guido Günther | 8346633 | 2021-01-18 17:31:10 +0100 | [diff] [blame] | 1697 | dev_err_probe(&pdev->dev, ret, "bitbang start failed\n"); |
Marek Vasut | 45f0bbd | 2020-10-05 15:22:29 +0200 | [diff] [blame] | 1698 | goto out_bitbang_start; |
Trent Piepho | 8197f48 | 2017-11-06 10:38:23 -0800 | [diff] [blame] | 1699 | } |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1700 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1701 | pm_runtime_mark_last_busy(spi_imx->dev); |
| 1702 | pm_runtime_put_autosuspend(spi_imx->dev); |
| 1703 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1704 | return ret; |
| 1705 | |
Marek Vasut | 45f0bbd | 2020-10-05 15:22:29 +0200 | [diff] [blame] | 1706 | out_bitbang_start: |
| 1707 | if (spi_imx->devtype_data->has_dmamode) |
| 1708 | spi_imx_sdma_exit(spi_imx); |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1709 | out_runtime_pm_put: |
| 1710 | pm_runtime_dont_use_autosuspend(spi_imx->dev); |
Sascha Hauer | 43b6bf4 | 2020-10-21 12:45:13 +0200 | [diff] [blame] | 1711 | pm_runtime_set_suspended(&pdev->dev); |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1712 | pm_runtime_disable(spi_imx->dev); |
Sascha Hauer | 43b6bf4 | 2020-10-21 12:45:13 +0200 | [diff] [blame] | 1713 | |
| 1714 | clk_disable_unprepare(spi_imx->clk_ipg); |
| 1715 | out_put_per: |
| 1716 | clk_disable_unprepare(spi_imx->clk_per); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1717 | out_master_put: |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1718 | spi_master_put(master); |
Fabio Estevam | 130b82c | 2013-07-11 01:26:48 -0300 | [diff] [blame] | 1719 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1720 | return ret; |
| 1721 | } |
| 1722 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1723 | static int spi_imx_remove(struct platform_device *pdev) |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1724 | { |
| 1725 | struct spi_master *master = platform_get_drvdata(pdev); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1726 | struct spi_imx_data *spi_imx = spi_master_get_devdata(master); |
Stefan Agner | d593574 | 2018-01-07 15:05:49 +0100 | [diff] [blame] | 1727 | int ret; |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1728 | |
| 1729 | spi_bitbang_stop(&spi_imx->bitbang); |
| 1730 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1731 | ret = pm_runtime_get_sync(spi_imx->dev); |
| 1732 | if (ret < 0) { |
Zhang Qilong | 1dcbdd9 | 2020-11-02 22:58:35 +0800 | [diff] [blame] | 1733 | pm_runtime_put_noidle(spi_imx->dev); |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1734 | dev_err(spi_imx->dev, "failed to enable clock\n"); |
Stefan Agner | d593574 | 2018-01-07 15:05:49 +0100 | [diff] [blame] | 1735 | return ret; |
| 1736 | } |
| 1737 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1738 | writel(0, spi_imx->base + MXC_CSPICTRL); |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1739 | |
| 1740 | pm_runtime_dont_use_autosuspend(spi_imx->dev); |
| 1741 | pm_runtime_put_sync(spi_imx->dev); |
| 1742 | pm_runtime_disable(spi_imx->dev); |
| 1743 | |
Robin Gong | f62cacc | 2014-09-11 09:18:44 +0800 | [diff] [blame] | 1744 | spi_imx_sdma_exit(spi_imx); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1745 | spi_master_put(master); |
| 1746 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1747 | return 0; |
| 1748 | } |
| 1749 | |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1750 | static int __maybe_unused spi_imx_runtime_resume(struct device *dev) |
| 1751 | { |
| 1752 | struct spi_master *master = dev_get_drvdata(dev); |
| 1753 | struct spi_imx_data *spi_imx; |
| 1754 | int ret; |
| 1755 | |
| 1756 | spi_imx = spi_master_get_devdata(master); |
| 1757 | |
| 1758 | ret = clk_prepare_enable(spi_imx->clk_per); |
| 1759 | if (ret) |
| 1760 | return ret; |
| 1761 | |
| 1762 | ret = clk_prepare_enable(spi_imx->clk_ipg); |
| 1763 | if (ret) { |
| 1764 | clk_disable_unprepare(spi_imx->clk_per); |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | static int __maybe_unused spi_imx_runtime_suspend(struct device *dev) |
| 1772 | { |
| 1773 | struct spi_master *master = dev_get_drvdata(dev); |
| 1774 | struct spi_imx_data *spi_imx; |
| 1775 | |
| 1776 | spi_imx = spi_master_get_devdata(master); |
| 1777 | |
| 1778 | clk_disable_unprepare(spi_imx->clk_per); |
| 1779 | clk_disable_unprepare(spi_imx->clk_ipg); |
| 1780 | |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static int __maybe_unused spi_imx_suspend(struct device *dev) |
| 1785 | { |
| 1786 | pinctrl_pm_select_sleep_state(dev); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | static int __maybe_unused spi_imx_resume(struct device *dev) |
| 1791 | { |
| 1792 | pinctrl_pm_select_default_state(dev); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | static const struct dev_pm_ops imx_spi_pm = { |
| 1797 | SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend, |
| 1798 | spi_imx_runtime_resume, NULL) |
| 1799 | SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume) |
| 1800 | }; |
| 1801 | |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1802 | static struct platform_driver spi_imx_driver = { |
| 1803 | .driver = { |
| 1804 | .name = DRIVER_NAME, |
Shawn Guo | 22a85e4 | 2011-07-10 01:16:41 +0800 | [diff] [blame] | 1805 | .of_match_table = spi_imx_dt_ids, |
Clark Wang | 525c9e5 | 2020-07-27 14:33:54 +0800 | [diff] [blame] | 1806 | .pm = &imx_spi_pm, |
| 1807 | }, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1808 | .probe = spi_imx_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1809 | .remove = spi_imx_remove, |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1810 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 1811 | module_platform_driver(spi_imx_driver); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1812 | |
Fabio Estevam | 92bad4a4 | 2021-03-16 15:09:22 -0300 | [diff] [blame] | 1813 | MODULE_DESCRIPTION("i.MX SPI Controller driver"); |
Uwe Kleine-König | 6cdeb00 | 2009-10-01 15:44:28 -0700 | [diff] [blame] | 1814 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 1815 | MODULE_LICENSE("GPL"); |
Fabio Estevam | 3133fba3 | 2013-01-07 20:42:55 -0200 | [diff] [blame] | 1816 | MODULE_ALIAS("platform:" DRIVER_NAME); |