Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008-2014, The Linux foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License rev 2 and |
| 6 | * only rev 2 as published by the free Software foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or fITNESS fOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/pm_runtime.h> |
| 24 | #include <linux/spi/spi.h> |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 25 | #include <linux/dmaengine.h> |
| 26 | #include <linux/dma-mapping.h> |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 27 | |
| 28 | #define QUP_CONFIG 0x0000 |
| 29 | #define QUP_STATE 0x0004 |
| 30 | #define QUP_IO_M_MODES 0x0008 |
| 31 | #define QUP_SW_RESET 0x000c |
| 32 | #define QUP_OPERATIONAL 0x0018 |
| 33 | #define QUP_ERROR_FLAGS 0x001c |
| 34 | #define QUP_ERROR_FLAGS_EN 0x0020 |
| 35 | #define QUP_OPERATIONAL_MASK 0x0028 |
| 36 | #define QUP_HW_VERSION 0x0030 |
| 37 | #define QUP_MX_OUTPUT_CNT 0x0100 |
| 38 | #define QUP_OUTPUT_FIFO 0x0110 |
| 39 | #define QUP_MX_WRITE_CNT 0x0150 |
| 40 | #define QUP_MX_INPUT_CNT 0x0200 |
| 41 | #define QUP_MX_READ_CNT 0x0208 |
| 42 | #define QUP_INPUT_FIFO 0x0218 |
| 43 | |
| 44 | #define SPI_CONFIG 0x0300 |
| 45 | #define SPI_IO_CONTROL 0x0304 |
| 46 | #define SPI_ERROR_FLAGS 0x0308 |
| 47 | #define SPI_ERROR_FLAGS_EN 0x030c |
| 48 | |
| 49 | /* QUP_CONFIG fields */ |
| 50 | #define QUP_CONFIG_SPI_MODE (1 << 8) |
| 51 | #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13) |
| 52 | #define QUP_CONFIG_NO_INPUT BIT(7) |
| 53 | #define QUP_CONFIG_NO_OUTPUT BIT(6) |
| 54 | #define QUP_CONFIG_N 0x001f |
| 55 | |
| 56 | /* QUP_STATE fields */ |
| 57 | #define QUP_STATE_VALID BIT(2) |
| 58 | #define QUP_STATE_RESET 0 |
| 59 | #define QUP_STATE_RUN 1 |
| 60 | #define QUP_STATE_PAUSE 3 |
| 61 | #define QUP_STATE_MASK 3 |
| 62 | #define QUP_STATE_CLEAR 2 |
| 63 | |
| 64 | #define QUP_HW_VERSION_2_1_1 0x20010001 |
| 65 | |
| 66 | /* QUP_IO_M_MODES fields */ |
| 67 | #define QUP_IO_M_PACK_EN BIT(15) |
| 68 | #define QUP_IO_M_UNPACK_EN BIT(14) |
| 69 | #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12 |
| 70 | #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10 |
| 71 | #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT) |
| 72 | #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT) |
| 73 | |
| 74 | #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0) |
| 75 | #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2) |
| 76 | #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5) |
| 77 | #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7) |
| 78 | |
| 79 | #define QUP_IO_M_MODE_FIFO 0 |
| 80 | #define QUP_IO_M_MODE_BLOCK 1 |
| 81 | #define QUP_IO_M_MODE_DMOV 2 |
| 82 | #define QUP_IO_M_MODE_BAM 3 |
| 83 | |
| 84 | /* QUP_OPERATIONAL fields */ |
| 85 | #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11) |
| 86 | #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10) |
| 87 | #define QUP_OP_IN_SERVICE_FLAG BIT(9) |
| 88 | #define QUP_OP_OUT_SERVICE_FLAG BIT(8) |
| 89 | #define QUP_OP_IN_FIFO_FULL BIT(7) |
| 90 | #define QUP_OP_OUT_FIFO_FULL BIT(6) |
| 91 | #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5) |
| 92 | #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4) |
| 93 | |
| 94 | /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */ |
| 95 | #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5) |
| 96 | #define QUP_ERROR_INPUT_UNDER_RUN BIT(4) |
| 97 | #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3) |
| 98 | #define QUP_ERROR_INPUT_OVER_RUN BIT(2) |
| 99 | |
| 100 | /* SPI_CONFIG fields */ |
| 101 | #define SPI_CONFIG_HS_MODE BIT(10) |
| 102 | #define SPI_CONFIG_INPUT_FIRST BIT(9) |
| 103 | #define SPI_CONFIG_LOOPBACK BIT(8) |
| 104 | |
| 105 | /* SPI_IO_CONTROL fields */ |
| 106 | #define SPI_IO_C_FORCE_CS BIT(11) |
| 107 | #define SPI_IO_C_CLK_IDLE_HIGH BIT(10) |
| 108 | #define SPI_IO_C_MX_CS_MODE BIT(8) |
| 109 | #define SPI_IO_C_CS_N_POLARITY_0 BIT(4) |
| 110 | #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2) |
| 111 | #define SPI_IO_C_CS_SELECT_MASK 0x000c |
| 112 | #define SPI_IO_C_TRISTATE_CS BIT(1) |
| 113 | #define SPI_IO_C_NO_TRI_STATE BIT(0) |
| 114 | |
| 115 | /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */ |
| 116 | #define SPI_ERROR_CLK_OVER_RUN BIT(1) |
| 117 | #define SPI_ERROR_CLK_UNDER_RUN BIT(0) |
| 118 | |
| 119 | #define SPI_NUM_CHIPSELECTS 4 |
| 120 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 121 | #define SPI_MAX_DMA_XFER (SZ_64K - 64) |
| 122 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 123 | /* high speed mode is when bus rate is greater then 26MHz */ |
| 124 | #define SPI_HS_MIN_RATE 26000000 |
| 125 | #define SPI_MAX_RATE 50000000 |
| 126 | |
| 127 | #define SPI_DELAY_THRESHOLD 1 |
| 128 | #define SPI_DELAY_RETRY 10 |
| 129 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 130 | struct spi_qup { |
| 131 | void __iomem *base; |
| 132 | struct device *dev; |
| 133 | struct clk *cclk; /* core clock */ |
| 134 | struct clk *iclk; /* interface clock */ |
| 135 | int irq; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 136 | spinlock_t lock; |
| 137 | |
| 138 | int in_fifo_sz; |
| 139 | int out_fifo_sz; |
| 140 | int in_blk_sz; |
| 141 | int out_blk_sz; |
| 142 | |
| 143 | struct spi_transfer *xfer; |
| 144 | struct completion done; |
| 145 | int error; |
| 146 | int w_size; /* bytes per SPI word */ |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 147 | int n_words; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 148 | int tx_bytes; |
| 149 | int rx_bytes; |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 150 | int qup_v1; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 151 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 152 | int mode; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 153 | struct dma_slave_config rx_conf; |
| 154 | struct dma_slave_config tx_conf; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 157 | static inline bool spi_qup_is_dma_xfer(int mode) |
| 158 | { |
| 159 | if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM) |
| 160 | return true; |
| 161 | |
| 162 | return false; |
| 163 | } |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 164 | |
| 165 | static inline bool spi_qup_is_valid_state(struct spi_qup *controller) |
| 166 | { |
| 167 | u32 opstate = readl_relaxed(controller->base + QUP_STATE); |
| 168 | |
| 169 | return opstate & QUP_STATE_VALID; |
| 170 | } |
| 171 | |
| 172 | static int spi_qup_set_state(struct spi_qup *controller, u32 state) |
| 173 | { |
| 174 | unsigned long loop; |
| 175 | u32 cur_state; |
| 176 | |
| 177 | loop = 0; |
| 178 | while (!spi_qup_is_valid_state(controller)) { |
| 179 | |
| 180 | usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2); |
| 181 | |
| 182 | if (++loop > SPI_DELAY_RETRY) |
| 183 | return -EIO; |
| 184 | } |
| 185 | |
| 186 | if (loop) |
| 187 | dev_dbg(controller->dev, "invalid state for %ld,us %d\n", |
| 188 | loop, state); |
| 189 | |
| 190 | cur_state = readl_relaxed(controller->base + QUP_STATE); |
| 191 | /* |
| 192 | * Per spec: for PAUSE_STATE to RESET_STATE, two writes |
| 193 | * of (b10) are required |
| 194 | */ |
| 195 | if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) && |
| 196 | (state == QUP_STATE_RESET)) { |
| 197 | writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE); |
| 198 | writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE); |
| 199 | } else { |
| 200 | cur_state &= ~QUP_STATE_MASK; |
| 201 | cur_state |= state; |
| 202 | writel_relaxed(cur_state, controller->base + QUP_STATE); |
| 203 | } |
| 204 | |
| 205 | loop = 0; |
| 206 | while (!spi_qup_is_valid_state(controller)) { |
| 207 | |
| 208 | usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2); |
| 209 | |
| 210 | if (++loop > SPI_DELAY_RETRY) |
| 211 | return -EIO; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 217 | static void spi_qup_fifo_read(struct spi_qup *controller, |
| 218 | struct spi_transfer *xfer) |
| 219 | { |
| 220 | u8 *rx_buf = xfer->rx_buf; |
| 221 | u32 word, state; |
| 222 | int idx, shift, w_size; |
| 223 | |
| 224 | w_size = controller->w_size; |
| 225 | |
| 226 | while (controller->rx_bytes < xfer->len) { |
| 227 | |
| 228 | state = readl_relaxed(controller->base + QUP_OPERATIONAL); |
| 229 | if (0 == (state & QUP_OP_IN_FIFO_NOT_EMPTY)) |
| 230 | break; |
| 231 | |
| 232 | word = readl_relaxed(controller->base + QUP_INPUT_FIFO); |
| 233 | |
| 234 | if (!rx_buf) { |
| 235 | controller->rx_bytes += w_size; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | for (idx = 0; idx < w_size; idx++, controller->rx_bytes++) { |
| 240 | /* |
| 241 | * The data format depends on bytes per SPI word: |
| 242 | * 4 bytes: 0x12345678 |
| 243 | * 2 bytes: 0x00001234 |
| 244 | * 1 byte : 0x00000012 |
| 245 | */ |
| 246 | shift = BITS_PER_BYTE; |
| 247 | shift *= (w_size - idx - 1); |
| 248 | rx_buf[controller->rx_bytes] = word >> shift; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static void spi_qup_fifo_write(struct spi_qup *controller, |
| 254 | struct spi_transfer *xfer) |
| 255 | { |
| 256 | const u8 *tx_buf = xfer->tx_buf; |
| 257 | u32 word, state, data; |
| 258 | int idx, w_size; |
| 259 | |
| 260 | w_size = controller->w_size; |
| 261 | |
| 262 | while (controller->tx_bytes < xfer->len) { |
| 263 | |
| 264 | state = readl_relaxed(controller->base + QUP_OPERATIONAL); |
| 265 | if (state & QUP_OP_OUT_FIFO_FULL) |
| 266 | break; |
| 267 | |
| 268 | word = 0; |
| 269 | for (idx = 0; idx < w_size; idx++, controller->tx_bytes++) { |
| 270 | |
| 271 | if (!tx_buf) { |
| 272 | controller->tx_bytes += w_size; |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | data = tx_buf[controller->tx_bytes]; |
| 277 | word |= data << (BITS_PER_BYTE * (3 - idx)); |
| 278 | } |
| 279 | |
| 280 | writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO); |
| 281 | } |
| 282 | } |
| 283 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 284 | static void spi_qup_dma_done(void *data) |
| 285 | { |
| 286 | struct spi_qup *qup = data; |
| 287 | |
| 288 | complete(&qup->done); |
| 289 | } |
| 290 | |
| 291 | static int spi_qup_prep_sg(struct spi_master *master, struct spi_transfer *xfer, |
| 292 | enum dma_transfer_direction dir, |
| 293 | dma_async_tx_callback callback) |
| 294 | { |
| 295 | struct spi_qup *qup = spi_master_get_devdata(master); |
| 296 | unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE; |
| 297 | struct dma_async_tx_descriptor *desc; |
| 298 | struct scatterlist *sgl; |
| 299 | struct dma_chan *chan; |
| 300 | dma_cookie_t cookie; |
| 301 | unsigned int nents; |
| 302 | |
| 303 | if (dir == DMA_MEM_TO_DEV) { |
| 304 | chan = master->dma_tx; |
| 305 | nents = xfer->tx_sg.nents; |
| 306 | sgl = xfer->tx_sg.sgl; |
| 307 | } else { |
| 308 | chan = master->dma_rx; |
| 309 | nents = xfer->rx_sg.nents; |
| 310 | sgl = xfer->rx_sg.sgl; |
| 311 | } |
| 312 | |
| 313 | desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags); |
Varadarajan Narayanan | d9a09a6 | 2017-07-28 12:22:52 +0530 | [diff] [blame] | 314 | if (IS_ERR_OR_NULL(desc)) |
| 315 | return desc ? PTR_ERR(desc) : -EINVAL; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 316 | |
| 317 | desc->callback = callback; |
| 318 | desc->callback_param = qup; |
| 319 | |
| 320 | cookie = dmaengine_submit(desc); |
| 321 | |
| 322 | return dma_submit_error(cookie); |
| 323 | } |
| 324 | |
| 325 | static void spi_qup_dma_terminate(struct spi_master *master, |
| 326 | struct spi_transfer *xfer) |
| 327 | { |
| 328 | if (xfer->tx_buf) |
| 329 | dmaengine_terminate_all(master->dma_tx); |
| 330 | if (xfer->rx_buf) |
| 331 | dmaengine_terminate_all(master->dma_rx); |
| 332 | } |
| 333 | |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 334 | static int spi_qup_do_dma(struct spi_master *master, struct spi_transfer *xfer, |
| 335 | unsigned long timeout) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 336 | { |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 337 | struct spi_qup *qup = spi_master_get_devdata(master); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 338 | dma_async_tx_callback rx_done = NULL, tx_done = NULL; |
| 339 | int ret; |
| 340 | |
| 341 | if (xfer->rx_buf) |
| 342 | rx_done = spi_qup_dma_done; |
| 343 | else if (xfer->tx_buf) |
| 344 | tx_done = spi_qup_dma_done; |
| 345 | |
Varadarajan Narayanan | ce00bab | 2017-07-28 12:22:51 +0530 | [diff] [blame] | 346 | /* before issuing the descriptors, set the QUP to run */ |
| 347 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); |
| 348 | if (ret) { |
| 349 | dev_warn(qup->dev, "%s(%d): cannot set RUN state\n", |
| 350 | __func__, __LINE__); |
| 351 | return ret; |
| 352 | } |
| 353 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 354 | if (xfer->rx_buf) { |
| 355 | ret = spi_qup_prep_sg(master, xfer, DMA_DEV_TO_MEM, rx_done); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | |
| 359 | dma_async_issue_pending(master->dma_rx); |
| 360 | } |
| 361 | |
| 362 | if (xfer->tx_buf) { |
| 363 | ret = spi_qup_prep_sg(master, xfer, DMA_MEM_TO_DEV, tx_done); |
| 364 | if (ret) |
| 365 | return ret; |
| 366 | |
| 367 | dma_async_issue_pending(master->dma_tx); |
| 368 | } |
| 369 | |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 370 | if (!wait_for_completion_timeout(&qup->done, timeout)) |
| 371 | return -ETIMEDOUT; |
| 372 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 376 | static int spi_qup_do_pio(struct spi_master *master, struct spi_transfer *xfer, |
| 377 | unsigned long timeout) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 378 | { |
| 379 | struct spi_qup *qup = spi_master_get_devdata(master); |
| 380 | int ret; |
| 381 | |
| 382 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); |
| 383 | if (ret) { |
| 384 | dev_warn(qup->dev, "cannot set RUN state\n"); |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | ret = spi_qup_set_state(qup, QUP_STATE_PAUSE); |
| 389 | if (ret) { |
| 390 | dev_warn(qup->dev, "cannot set PAUSE state\n"); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | spi_qup_fifo_write(qup, xfer); |
| 395 | |
Varadarajan Narayanan | ce00bab | 2017-07-28 12:22:51 +0530 | [diff] [blame] | 396 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); |
| 397 | if (ret) { |
| 398 | dev_warn(qup->dev, "%s(%d): cannot set RUN state\n", |
| 399 | __func__, __LINE__); |
| 400 | return ret; |
| 401 | } |
| 402 | |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 403 | if (!wait_for_completion_timeout(&qup->done, timeout)) |
| 404 | return -ETIMEDOUT; |
| 405 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 409 | static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id) |
| 410 | { |
| 411 | struct spi_qup *controller = dev_id; |
Varadarajan Narayanan | ce7dfc7 | 2017-07-28 12:22:53 +0530 | [diff] [blame^] | 412 | struct spi_transfer *xfer = controller->xfer; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 413 | u32 opflags, qup_err, spi_err; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 414 | int error = 0; |
| 415 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 416 | qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS); |
| 417 | spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS); |
| 418 | opflags = readl_relaxed(controller->base + QUP_OPERATIONAL); |
| 419 | |
| 420 | writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS); |
| 421 | writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 422 | |
| 423 | if (qup_err) { |
| 424 | if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN) |
| 425 | dev_warn(controller->dev, "OUTPUT_OVER_RUN\n"); |
| 426 | if (qup_err & QUP_ERROR_INPUT_UNDER_RUN) |
| 427 | dev_warn(controller->dev, "INPUT_UNDER_RUN\n"); |
| 428 | if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN) |
| 429 | dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n"); |
| 430 | if (qup_err & QUP_ERROR_INPUT_OVER_RUN) |
| 431 | dev_warn(controller->dev, "INPUT_OVER_RUN\n"); |
| 432 | |
| 433 | error = -EIO; |
| 434 | } |
| 435 | |
| 436 | if (spi_err) { |
| 437 | if (spi_err & SPI_ERROR_CLK_OVER_RUN) |
| 438 | dev_warn(controller->dev, "CLK_OVER_RUN\n"); |
| 439 | if (spi_err & SPI_ERROR_CLK_UNDER_RUN) |
| 440 | dev_warn(controller->dev, "CLK_UNDER_RUN\n"); |
| 441 | |
| 442 | error = -EIO; |
| 443 | } |
| 444 | |
Varadarajan Narayanan | ce7dfc7 | 2017-07-28 12:22:53 +0530 | [diff] [blame^] | 445 | if (spi_qup_is_dma_xfer(controller->mode)) { |
| 446 | writel_relaxed(opflags, controller->base + QUP_OPERATIONAL); |
| 447 | } else { |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 448 | if (opflags & QUP_OP_IN_SERVICE_FLAG) |
| 449 | spi_qup_fifo_read(controller, xfer); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 450 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 451 | if (opflags & QUP_OP_OUT_SERVICE_FLAG) |
| 452 | spi_qup_fifo_write(controller, xfer); |
| 453 | } |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 454 | |
Varadarajan Narayanan | ce7dfc7 | 2017-07-28 12:22:53 +0530 | [diff] [blame^] | 455 | if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 456 | complete(&controller->done); |
| 457 | |
| 458 | return IRQ_HANDLED; |
| 459 | } |
| 460 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 461 | /* set clock freq ... bits per word */ |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 462 | static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 463 | { |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 464 | struct spi_qup *controller = spi_master_get_devdata(spi->master); |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 465 | u32 config, iomode, control; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 466 | int ret, n_words; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 467 | |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 468 | if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) { |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 469 | dev_err(controller->dev, "too big size for loopback %d > %d\n", |
| 470 | xfer->len, controller->in_fifo_sz); |
| 471 | return -EIO; |
| 472 | } |
| 473 | |
| 474 | ret = clk_set_rate(controller->cclk, xfer->speed_hz); |
| 475 | if (ret) { |
| 476 | dev_err(controller->dev, "fail to set frequency %d", |
| 477 | xfer->speed_hz); |
| 478 | return -EIO; |
| 479 | } |
| 480 | |
| 481 | if (spi_qup_set_state(controller, QUP_STATE_RESET)) { |
| 482 | dev_err(controller->dev, "cannot set RESET state\n"); |
| 483 | return -EIO; |
| 484 | } |
| 485 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 486 | controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8); |
| 487 | controller->n_words = xfer->len / controller->w_size; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 488 | n_words = controller->n_words; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 489 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 490 | if (n_words <= (controller->in_fifo_sz / sizeof(u32))) { |
| 491 | |
| 492 | controller->mode = QUP_IO_M_MODE_FIFO; |
| 493 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 494 | writel_relaxed(n_words, controller->base + QUP_MX_READ_CNT); |
| 495 | writel_relaxed(n_words, controller->base + QUP_MX_WRITE_CNT); |
| 496 | /* must be zero for FIFO */ |
| 497 | writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT); |
| 498 | writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT); |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 499 | } else if (spi->master->can_dma && |
| 500 | spi->master->can_dma(spi->master, spi, xfer) && |
| 501 | spi->master->cur_msg_mapped) { |
| 502 | |
| 503 | controller->mode = QUP_IO_M_MODE_BAM; |
| 504 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 505 | writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT); |
| 506 | writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT); |
| 507 | /* must be zero for BLOCK and BAM */ |
| 508 | writel_relaxed(0, controller->base + QUP_MX_READ_CNT); |
| 509 | writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 510 | |
| 511 | if (!controller->qup_v1) { |
| 512 | void __iomem *input_cnt; |
| 513 | |
| 514 | input_cnt = controller->base + QUP_MX_INPUT_CNT; |
| 515 | /* |
| 516 | * for DMA transfers, both QUP_MX_INPUT_CNT and |
| 517 | * QUP_MX_OUTPUT_CNT must be zero to all cases but one. |
| 518 | * That case is a non-balanced transfer when there is |
| 519 | * only a rx_buf. |
| 520 | */ |
| 521 | if (xfer->tx_buf) |
| 522 | writel_relaxed(0, input_cnt); |
| 523 | else |
| 524 | writel_relaxed(n_words, input_cnt); |
| 525 | |
| 526 | writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT); |
| 527 | } |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 528 | } else { |
| 529 | |
| 530 | controller->mode = QUP_IO_M_MODE_BLOCK; |
| 531 | |
| 532 | writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT); |
| 533 | writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT); |
| 534 | /* must be zero for BLOCK and BAM */ |
| 535 | writel_relaxed(0, controller->base + QUP_MX_READ_CNT); |
| 536 | writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | iomode = readl_relaxed(controller->base + QUP_IO_M_MODES); |
| 540 | /* Set input and output transfer mode */ |
| 541 | iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 542 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 543 | if (!spi_qup_is_dma_xfer(controller->mode)) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 544 | iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN); |
| 545 | else |
| 546 | iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN; |
| 547 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 548 | iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT); |
| 549 | iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 550 | |
| 551 | writel_relaxed(iomode, controller->base + QUP_IO_M_MODES); |
| 552 | |
Ivan T. Ivanov | 0667dd5 | 2014-12-16 12:21:55 +0200 | [diff] [blame] | 553 | control = readl_relaxed(controller->base + SPI_IO_CONTROL); |
| 554 | |
| 555 | if (spi->mode & SPI_CPOL) |
| 556 | control |= SPI_IO_C_CLK_IDLE_HIGH; |
| 557 | else |
| 558 | control &= ~SPI_IO_C_CLK_IDLE_HIGH; |
| 559 | |
| 560 | writel_relaxed(control, controller->base + SPI_IO_CONTROL); |
| 561 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 562 | config = readl_relaxed(controller->base + SPI_CONFIG); |
| 563 | |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 564 | if (spi->mode & SPI_LOOP) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 565 | config |= SPI_CONFIG_LOOPBACK; |
| 566 | else |
| 567 | config &= ~SPI_CONFIG_LOOPBACK; |
| 568 | |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 569 | if (spi->mode & SPI_CPHA) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 570 | config &= ~SPI_CONFIG_INPUT_FIRST; |
| 571 | else |
| 572 | config |= SPI_CONFIG_INPUT_FIRST; |
| 573 | |
| 574 | /* |
| 575 | * HS_MODE improves signal stability for spi-clk high rates, |
| 576 | * but is invalid in loop back mode. |
| 577 | */ |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 578 | if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP)) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 579 | config |= SPI_CONFIG_HS_MODE; |
| 580 | else |
| 581 | config &= ~SPI_CONFIG_HS_MODE; |
| 582 | |
| 583 | writel_relaxed(config, controller->base + SPI_CONFIG); |
| 584 | |
| 585 | config = readl_relaxed(controller->base + QUP_CONFIG); |
| 586 | config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N); |
| 587 | config |= xfer->bits_per_word - 1; |
| 588 | config |= QUP_CONFIG_SPI_MODE; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 589 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 590 | if (spi_qup_is_dma_xfer(controller->mode)) { |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 591 | if (!xfer->tx_buf) |
| 592 | config |= QUP_CONFIG_NO_OUTPUT; |
| 593 | if (!xfer->rx_buf) |
| 594 | config |= QUP_CONFIG_NO_INPUT; |
| 595 | } |
| 596 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 597 | writel_relaxed(config, controller->base + QUP_CONFIG); |
| 598 | |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 599 | /* only write to OPERATIONAL_MASK when register is present */ |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 600 | if (!controller->qup_v1) { |
| 601 | u32 mask = 0; |
| 602 | |
| 603 | /* |
| 604 | * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO |
| 605 | * status change in BAM mode |
| 606 | */ |
| 607 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 608 | if (spi_qup_is_dma_xfer(controller->mode)) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 609 | mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG; |
| 610 | |
| 611 | writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK); |
| 612 | } |
| 613 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 617 | static int spi_qup_transfer_one(struct spi_master *master, |
| 618 | struct spi_device *spi, |
| 619 | struct spi_transfer *xfer) |
| 620 | { |
| 621 | struct spi_qup *controller = spi_master_get_devdata(master); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 622 | unsigned long timeout, flags; |
| 623 | int ret = -EIO; |
| 624 | |
Axel Lin | 00cce74 | 2014-02-24 23:07:36 +0800 | [diff] [blame] | 625 | ret = spi_qup_io_config(spi, xfer); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 626 | if (ret) |
| 627 | return ret; |
| 628 | |
| 629 | timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC); |
| 630 | timeout = DIV_ROUND_UP(xfer->len * 8, timeout); |
| 631 | timeout = 100 * msecs_to_jiffies(timeout); |
| 632 | |
| 633 | reinit_completion(&controller->done); |
| 634 | |
| 635 | spin_lock_irqsave(&controller->lock, flags); |
| 636 | controller->xfer = xfer; |
| 637 | controller->error = 0; |
| 638 | controller->rx_bytes = 0; |
| 639 | controller->tx_bytes = 0; |
| 640 | spin_unlock_irqrestore(&controller->lock, flags); |
| 641 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 642 | if (spi_qup_is_dma_xfer(controller->mode)) |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 643 | ret = spi_qup_do_dma(master, xfer, timeout); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 644 | else |
Varadarajan Narayanan | 5f13fd6 | 2017-07-28 12:22:50 +0530 | [diff] [blame] | 645 | ret = spi_qup_do_pio(master, xfer, timeout); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 646 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 647 | if (ret) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 648 | goto exit; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 649 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 650 | exit: |
| 651 | spi_qup_set_state(controller, QUP_STATE_RESET); |
| 652 | spin_lock_irqsave(&controller->lock, flags); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 653 | if (!ret) |
| 654 | ret = controller->error; |
| 655 | spin_unlock_irqrestore(&controller->lock, flags); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 656 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 657 | if (ret && spi_qup_is_dma_xfer(controller->mode)) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 658 | spi_qup_dma_terminate(master, xfer); |
| 659 | |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | static bool spi_qup_can_dma(struct spi_master *master, struct spi_device *spi, |
| 664 | struct spi_transfer *xfer) |
| 665 | { |
| 666 | struct spi_qup *qup = spi_master_get_devdata(master); |
| 667 | size_t dma_align = dma_get_cache_alignment(); |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 668 | int n_words; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 669 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 670 | if (xfer->rx_buf) { |
| 671 | if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) || |
| 672 | IS_ERR_OR_NULL(master->dma_rx)) |
| 673 | return false; |
| 674 | if (qup->qup_v1 && (xfer->len % qup->in_blk_sz)) |
| 675 | return false; |
| 676 | } |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 677 | |
Varadarajan Narayanan | 32ecab9 | 2017-07-28 12:22:49 +0530 | [diff] [blame] | 678 | if (xfer->tx_buf) { |
| 679 | if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) || |
| 680 | IS_ERR_OR_NULL(master->dma_tx)) |
| 681 | return false; |
| 682 | if (qup->qup_v1 && (xfer->len % qup->out_blk_sz)) |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8); |
| 687 | if (n_words <= (qup->in_fifo_sz / sizeof(u32))) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 688 | return false; |
| 689 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 690 | return true; |
| 691 | } |
| 692 | |
| 693 | static void spi_qup_release_dma(struct spi_master *master) |
| 694 | { |
| 695 | if (!IS_ERR_OR_NULL(master->dma_rx)) |
| 696 | dma_release_channel(master->dma_rx); |
| 697 | if (!IS_ERR_OR_NULL(master->dma_tx)) |
| 698 | dma_release_channel(master->dma_tx); |
| 699 | } |
| 700 | |
| 701 | static int spi_qup_init_dma(struct spi_master *master, resource_size_t base) |
| 702 | { |
| 703 | struct spi_qup *spi = spi_master_get_devdata(master); |
| 704 | struct dma_slave_config *rx_conf = &spi->rx_conf, |
| 705 | *tx_conf = &spi->tx_conf; |
| 706 | struct device *dev = spi->dev; |
| 707 | int ret; |
| 708 | |
| 709 | /* allocate dma resources, if available */ |
| 710 | master->dma_rx = dma_request_slave_channel_reason(dev, "rx"); |
| 711 | if (IS_ERR(master->dma_rx)) |
| 712 | return PTR_ERR(master->dma_rx); |
| 713 | |
| 714 | master->dma_tx = dma_request_slave_channel_reason(dev, "tx"); |
| 715 | if (IS_ERR(master->dma_tx)) { |
| 716 | ret = PTR_ERR(master->dma_tx); |
| 717 | goto err_tx; |
| 718 | } |
| 719 | |
| 720 | /* set DMA parameters */ |
| 721 | rx_conf->direction = DMA_DEV_TO_MEM; |
| 722 | rx_conf->device_fc = 1; |
| 723 | rx_conf->src_addr = base + QUP_INPUT_FIFO; |
| 724 | rx_conf->src_maxburst = spi->in_blk_sz; |
| 725 | |
| 726 | tx_conf->direction = DMA_MEM_TO_DEV; |
| 727 | tx_conf->device_fc = 1; |
| 728 | tx_conf->dst_addr = base + QUP_OUTPUT_FIFO; |
| 729 | tx_conf->dst_maxburst = spi->out_blk_sz; |
| 730 | |
| 731 | ret = dmaengine_slave_config(master->dma_rx, rx_conf); |
| 732 | if (ret) { |
| 733 | dev_err(dev, "failed to configure RX channel\n"); |
| 734 | goto err; |
| 735 | } |
| 736 | |
| 737 | ret = dmaengine_slave_config(master->dma_tx, tx_conf); |
| 738 | if (ret) { |
| 739 | dev_err(dev, "failed to configure TX channel\n"); |
| 740 | goto err; |
| 741 | } |
| 742 | |
| 743 | return 0; |
| 744 | |
| 745 | err: |
| 746 | dma_release_channel(master->dma_tx); |
| 747 | err_tx: |
| 748 | dma_release_channel(master->dma_rx); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 749 | return ret; |
| 750 | } |
| 751 | |
Varadarajan Narayanan | b702b9f | 2017-07-28 12:22:48 +0530 | [diff] [blame] | 752 | static void spi_qup_set_cs(struct spi_device *spi, bool val) |
| 753 | { |
| 754 | struct spi_qup *controller; |
| 755 | u32 spi_ioc; |
| 756 | u32 spi_ioc_orig; |
| 757 | |
| 758 | controller = spi_master_get_devdata(spi->master); |
| 759 | spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL); |
| 760 | spi_ioc_orig = spi_ioc; |
| 761 | if (!val) |
| 762 | spi_ioc |= SPI_IO_C_FORCE_CS; |
| 763 | else |
| 764 | spi_ioc &= ~SPI_IO_C_FORCE_CS; |
| 765 | |
| 766 | if (spi_ioc != spi_ioc_orig) |
| 767 | writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL); |
| 768 | } |
| 769 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 770 | static int spi_qup_probe(struct platform_device *pdev) |
| 771 | { |
| 772 | struct spi_master *master; |
| 773 | struct clk *iclk, *cclk; |
| 774 | struct spi_qup *controller; |
| 775 | struct resource *res; |
| 776 | struct device *dev; |
| 777 | void __iomem *base; |
Ivan T. Ivanov | 12cb89e | 2015-03-06 17:26:17 +0200 | [diff] [blame] | 778 | u32 max_freq, iomode, num_cs; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 779 | int ret, irq, size; |
| 780 | |
| 781 | dev = &pdev->dev; |
| 782 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 783 | base = devm_ioremap_resource(dev, res); |
| 784 | if (IS_ERR(base)) |
| 785 | return PTR_ERR(base); |
| 786 | |
| 787 | irq = platform_get_irq(pdev, 0); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 788 | if (irq < 0) |
| 789 | return irq; |
| 790 | |
| 791 | cclk = devm_clk_get(dev, "core"); |
| 792 | if (IS_ERR(cclk)) |
| 793 | return PTR_ERR(cclk); |
| 794 | |
| 795 | iclk = devm_clk_get(dev, "iface"); |
| 796 | if (IS_ERR(iclk)) |
| 797 | return PTR_ERR(iclk); |
| 798 | |
| 799 | /* This is optional parameter */ |
| 800 | if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq)) |
| 801 | max_freq = SPI_MAX_RATE; |
| 802 | |
| 803 | if (!max_freq || max_freq > SPI_MAX_RATE) { |
| 804 | dev_err(dev, "invalid clock frequency %d\n", max_freq); |
| 805 | return -ENXIO; |
| 806 | } |
| 807 | |
| 808 | ret = clk_prepare_enable(cclk); |
| 809 | if (ret) { |
| 810 | dev_err(dev, "cannot enable core clock\n"); |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | ret = clk_prepare_enable(iclk); |
| 815 | if (ret) { |
| 816 | clk_disable_unprepare(cclk); |
| 817 | dev_err(dev, "cannot enable iface clock\n"); |
| 818 | return ret; |
| 819 | } |
| 820 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 821 | master = spi_alloc_master(dev, sizeof(struct spi_qup)); |
| 822 | if (!master) { |
| 823 | clk_disable_unprepare(cclk); |
| 824 | clk_disable_unprepare(iclk); |
| 825 | dev_err(dev, "cannot allocate master\n"); |
| 826 | return -ENOMEM; |
| 827 | } |
| 828 | |
Andy Gross | 4a8573a | 2014-06-12 14:34:10 -0500 | [diff] [blame] | 829 | /* use num-cs unless not present or out of range */ |
Ivan T. Ivanov | 12cb89e | 2015-03-06 17:26:17 +0200 | [diff] [blame] | 830 | if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) || |
| 831 | num_cs > SPI_NUM_CHIPSELECTS) |
Andy Gross | 4a8573a | 2014-06-12 14:34:10 -0500 | [diff] [blame] | 832 | master->num_chipselect = SPI_NUM_CHIPSELECTS; |
Ivan T. Ivanov | 12cb89e | 2015-03-06 17:26:17 +0200 | [diff] [blame] | 833 | else |
| 834 | master->num_chipselect = num_cs; |
Andy Gross | 4a8573a | 2014-06-12 14:34:10 -0500 | [diff] [blame] | 835 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 836 | master->bus_num = pdev->id; |
| 837 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 838 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Axel Lin | cb64ca5 | 2014-02-21 09:34:16 +0800 | [diff] [blame] | 839 | master->max_speed_hz = max_freq; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 840 | master->transfer_one = spi_qup_transfer_one; |
| 841 | master->dev.of_node = pdev->dev.of_node; |
| 842 | master->auto_runtime_pm = true; |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 843 | master->dma_alignment = dma_get_cache_alignment(); |
| 844 | master->max_dma_len = SPI_MAX_DMA_XFER; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 845 | |
| 846 | platform_set_drvdata(pdev, master); |
| 847 | |
| 848 | controller = spi_master_get_devdata(master); |
| 849 | |
| 850 | controller->dev = dev; |
| 851 | controller->base = base; |
| 852 | controller->iclk = iclk; |
| 853 | controller->cclk = cclk; |
| 854 | controller->irq = irq; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 855 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 856 | ret = spi_qup_init_dma(master, res->start); |
| 857 | if (ret == -EPROBE_DEFER) |
| 858 | goto error; |
| 859 | else if (!ret) |
| 860 | master->can_dma = spi_qup_can_dma; |
| 861 | |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 862 | /* set v1 flag if device is version 1 */ |
| 863 | if (of_device_is_compatible(dev->of_node, "qcom,spi-qup-v1.1.1")) |
| 864 | controller->qup_v1 = 1; |
| 865 | |
Varadarajan Narayanan | b702b9f | 2017-07-28 12:22:48 +0530 | [diff] [blame] | 866 | if (!controller->qup_v1) |
| 867 | master->set_cs = spi_qup_set_cs; |
| 868 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 869 | spin_lock_init(&controller->lock); |
| 870 | init_completion(&controller->done); |
| 871 | |
| 872 | iomode = readl_relaxed(base + QUP_IO_M_MODES); |
| 873 | |
| 874 | size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode); |
| 875 | if (size) |
| 876 | controller->out_blk_sz = size * 16; |
| 877 | else |
| 878 | controller->out_blk_sz = 4; |
| 879 | |
| 880 | size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode); |
| 881 | if (size) |
| 882 | controller->in_blk_sz = size * 16; |
| 883 | else |
| 884 | controller->in_blk_sz = 4; |
| 885 | |
| 886 | size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode); |
| 887 | controller->out_fifo_sz = controller->out_blk_sz * (2 << size); |
| 888 | |
| 889 | size = QUP_IO_M_INPUT_FIFO_SIZE(iomode); |
| 890 | controller->in_fifo_sz = controller->in_blk_sz * (2 << size); |
| 891 | |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 892 | dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n", |
| 893 | controller->in_blk_sz, controller->in_fifo_sz, |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 894 | controller->out_blk_sz, controller->out_fifo_sz); |
| 895 | |
| 896 | writel_relaxed(1, base + QUP_SW_RESET); |
| 897 | |
| 898 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); |
| 899 | if (ret) { |
| 900 | dev_err(dev, "cannot set RESET state\n"); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 901 | goto error_dma; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | writel_relaxed(0, base + QUP_OPERATIONAL); |
| 905 | writel_relaxed(0, base + QUP_IO_M_MODES); |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 906 | |
| 907 | if (!controller->qup_v1) |
| 908 | writel_relaxed(0, base + QUP_OPERATIONAL_MASK); |
| 909 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 910 | writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN, |
| 911 | base + SPI_ERROR_FLAGS_EN); |
| 912 | |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 913 | /* if earlier version of the QUP, disable INPUT_OVERRUN */ |
| 914 | if (controller->qup_v1) |
| 915 | writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN | |
| 916 | QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN, |
| 917 | base + QUP_ERROR_FLAGS_EN); |
| 918 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 919 | writel_relaxed(0, base + SPI_CONFIG); |
| 920 | writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL); |
| 921 | |
| 922 | ret = devm_request_irq(dev, irq, spi_qup_qup_irq, |
| 923 | IRQF_TRIGGER_HIGH, pdev->name, controller); |
| 924 | if (ret) |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 925 | goto error_dma; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 926 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 927 | pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC); |
| 928 | pm_runtime_use_autosuspend(dev); |
| 929 | pm_runtime_set_active(dev); |
| 930 | pm_runtime_enable(dev); |
Andy Gross | 045c243 | 2014-06-12 14:34:11 -0500 | [diff] [blame] | 931 | |
| 932 | ret = devm_spi_register_master(dev, master); |
| 933 | if (ret) |
| 934 | goto disable_pm; |
| 935 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 936 | return 0; |
| 937 | |
Andy Gross | 045c243 | 2014-06-12 14:34:11 -0500 | [diff] [blame] | 938 | disable_pm: |
| 939 | pm_runtime_disable(&pdev->dev); |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 940 | error_dma: |
| 941 | spi_qup_release_dma(master); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 942 | error: |
| 943 | clk_disable_unprepare(cclk); |
| 944 | clk_disable_unprepare(iclk); |
| 945 | spi_master_put(master); |
| 946 | return ret; |
| 947 | } |
| 948 | |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 949 | #ifdef CONFIG_PM |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 950 | static int spi_qup_pm_suspend_runtime(struct device *device) |
| 951 | { |
| 952 | struct spi_master *master = dev_get_drvdata(device); |
| 953 | struct spi_qup *controller = spi_master_get_devdata(master); |
| 954 | u32 config; |
| 955 | |
| 956 | /* Enable clocks auto gaiting */ |
| 957 | config = readl(controller->base + QUP_CONFIG); |
Axel Lin | f0ceb11 | 2014-02-23 13:27:16 +0800 | [diff] [blame] | 958 | config |= QUP_CONFIG_CLOCK_AUTO_GATE; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 959 | writel_relaxed(config, controller->base + QUP_CONFIG); |
Pramod Gurav | dae1a77 | 2016-05-02 17:44:03 +0530 | [diff] [blame] | 960 | |
| 961 | clk_disable_unprepare(controller->cclk); |
| 962 | clk_disable_unprepare(controller->iclk); |
| 963 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static int spi_qup_pm_resume_runtime(struct device *device) |
| 968 | { |
| 969 | struct spi_master *master = dev_get_drvdata(device); |
| 970 | struct spi_qup *controller = spi_master_get_devdata(master); |
| 971 | u32 config; |
Pramod Gurav | dae1a77 | 2016-05-02 17:44:03 +0530 | [diff] [blame] | 972 | int ret; |
| 973 | |
| 974 | ret = clk_prepare_enable(controller->iclk); |
| 975 | if (ret) |
| 976 | return ret; |
| 977 | |
| 978 | ret = clk_prepare_enable(controller->cclk); |
| 979 | if (ret) |
| 980 | return ret; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 981 | |
| 982 | /* Disable clocks auto gaiting */ |
| 983 | config = readl_relaxed(controller->base + QUP_CONFIG); |
Axel Lin | f0ceb11 | 2014-02-23 13:27:16 +0800 | [diff] [blame] | 984 | config &= ~QUP_CONFIG_CLOCK_AUTO_GATE; |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 985 | writel_relaxed(config, controller->base + QUP_CONFIG); |
| 986 | return 0; |
| 987 | } |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 988 | #endif /* CONFIG_PM */ |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 989 | |
| 990 | #ifdef CONFIG_PM_SLEEP |
| 991 | static int spi_qup_suspend(struct device *device) |
| 992 | { |
| 993 | struct spi_master *master = dev_get_drvdata(device); |
| 994 | struct spi_qup *controller = spi_master_get_devdata(master); |
| 995 | int ret; |
| 996 | |
| 997 | ret = spi_master_suspend(master); |
| 998 | if (ret) |
| 999 | return ret; |
| 1000 | |
| 1001 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); |
| 1002 | if (ret) |
| 1003 | return ret; |
| 1004 | |
Sudeep Holla | 9d04d8b | 2016-08-25 13:33:28 +0100 | [diff] [blame] | 1005 | if (!pm_runtime_suspended(device)) { |
| 1006 | clk_disable_unprepare(controller->cclk); |
| 1007 | clk_disable_unprepare(controller->iclk); |
| 1008 | } |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int spi_qup_resume(struct device *device) |
| 1013 | { |
| 1014 | struct spi_master *master = dev_get_drvdata(device); |
| 1015 | struct spi_qup *controller = spi_master_get_devdata(master); |
| 1016 | int ret; |
| 1017 | |
| 1018 | ret = clk_prepare_enable(controller->iclk); |
| 1019 | if (ret) |
| 1020 | return ret; |
| 1021 | |
| 1022 | ret = clk_prepare_enable(controller->cclk); |
| 1023 | if (ret) |
| 1024 | return ret; |
| 1025 | |
| 1026 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); |
| 1027 | if (ret) |
| 1028 | return ret; |
| 1029 | |
| 1030 | return spi_master_resume(master); |
| 1031 | } |
| 1032 | #endif /* CONFIG_PM_SLEEP */ |
| 1033 | |
| 1034 | static int spi_qup_remove(struct platform_device *pdev) |
| 1035 | { |
| 1036 | struct spi_master *master = dev_get_drvdata(&pdev->dev); |
| 1037 | struct spi_qup *controller = spi_master_get_devdata(master); |
| 1038 | int ret; |
| 1039 | |
| 1040 | ret = pm_runtime_get_sync(&pdev->dev); |
Axel Lin | 3d89e14 | 2014-05-03 10:57:57 +0800 | [diff] [blame] | 1041 | if (ret < 0) |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1042 | return ret; |
| 1043 | |
| 1044 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | |
Andy Gross | 612762e | 2015-03-04 12:02:05 +0200 | [diff] [blame] | 1048 | spi_qup_release_dma(master); |
| 1049 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1050 | clk_disable_unprepare(controller->cclk); |
| 1051 | clk_disable_unprepare(controller->iclk); |
| 1052 | |
| 1053 | pm_runtime_put_noidle(&pdev->dev); |
| 1054 | pm_runtime_disable(&pdev->dev); |
Pramod Gurav | d244228 | 2016-05-02 17:44:04 +0530 | [diff] [blame] | 1055 | |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | |
Jingoo Han | 113b1a0 | 2014-05-07 16:50:04 +0900 | [diff] [blame] | 1059 | static const struct of_device_id spi_qup_dt_match[] = { |
Andy Gross | 70cea0a | 2014-06-12 14:34:12 -0500 | [diff] [blame] | 1060 | { .compatible = "qcom,spi-qup-v1.1.1", }, |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1061 | { .compatible = "qcom,spi-qup-v2.1.1", }, |
| 1062 | { .compatible = "qcom,spi-qup-v2.2.1", }, |
| 1063 | { } |
| 1064 | }; |
| 1065 | MODULE_DEVICE_TABLE(of, spi_qup_dt_match); |
| 1066 | |
| 1067 | static const struct dev_pm_ops spi_qup_dev_pm_ops = { |
| 1068 | SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume) |
| 1069 | SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime, |
| 1070 | spi_qup_pm_resume_runtime, |
| 1071 | NULL) |
| 1072 | }; |
| 1073 | |
| 1074 | static struct platform_driver spi_qup_driver = { |
| 1075 | .driver = { |
| 1076 | .name = "spi_qup", |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1077 | .pm = &spi_qup_dev_pm_ops, |
| 1078 | .of_match_table = spi_qup_dt_match, |
| 1079 | }, |
| 1080 | .probe = spi_qup_probe, |
| 1081 | .remove = spi_qup_remove, |
| 1082 | }; |
| 1083 | module_platform_driver(spi_qup_driver); |
| 1084 | |
| 1085 | MODULE_LICENSE("GPL v2"); |
Ivan T. Ivanov | 64ff247 | 2014-02-13 18:21:38 +0200 | [diff] [blame] | 1086 | MODULE_ALIAS("platform:spi_qup"); |