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