addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 3 | * Author: Addy Ke <addy.ke@rock-chips.com> |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 16 | #include <linux/clk.h> |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 17 | #include <linux/dmaengine.h> |
Shawn Lin | ec5c5d8 | 2016-03-10 14:51:48 +0800 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
Brian Norris | 23e291c | 2016-12-16 16:59:16 -0800 | [diff] [blame] | 20 | #include <linux/pinctrl/consumer.h> |
Shawn Lin | ec5c5d8 | 2016-03-10 14:51:48 +0800 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spi/spi.h> |
| 23 | #include <linux/pm_runtime.h> |
| 24 | #include <linux/scatterlist.h> |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 25 | |
| 26 | #define DRIVER_NAME "rockchip-spi" |
| 27 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 28 | #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \ |
| 29 | writel_relaxed(readl_relaxed(reg) & ~(bits), reg) |
| 30 | #define ROCKCHIP_SPI_SET_BITS(reg, bits) \ |
| 31 | writel_relaxed(readl_relaxed(reg) | (bits), reg) |
| 32 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 33 | /* SPI register offsets */ |
| 34 | #define ROCKCHIP_SPI_CTRLR0 0x0000 |
| 35 | #define ROCKCHIP_SPI_CTRLR1 0x0004 |
| 36 | #define ROCKCHIP_SPI_SSIENR 0x0008 |
| 37 | #define ROCKCHIP_SPI_SER 0x000c |
| 38 | #define ROCKCHIP_SPI_BAUDR 0x0010 |
| 39 | #define ROCKCHIP_SPI_TXFTLR 0x0014 |
| 40 | #define ROCKCHIP_SPI_RXFTLR 0x0018 |
| 41 | #define ROCKCHIP_SPI_TXFLR 0x001c |
| 42 | #define ROCKCHIP_SPI_RXFLR 0x0020 |
| 43 | #define ROCKCHIP_SPI_SR 0x0024 |
| 44 | #define ROCKCHIP_SPI_IPR 0x0028 |
| 45 | #define ROCKCHIP_SPI_IMR 0x002c |
| 46 | #define ROCKCHIP_SPI_ISR 0x0030 |
| 47 | #define ROCKCHIP_SPI_RISR 0x0034 |
| 48 | #define ROCKCHIP_SPI_ICR 0x0038 |
| 49 | #define ROCKCHIP_SPI_DMACR 0x003c |
| 50 | #define ROCKCHIP_SPI_DMATDLR 0x0040 |
| 51 | #define ROCKCHIP_SPI_DMARDLR 0x0044 |
| 52 | #define ROCKCHIP_SPI_TXDR 0x0400 |
| 53 | #define ROCKCHIP_SPI_RXDR 0x0800 |
| 54 | |
| 55 | /* Bit fields in CTRLR0 */ |
| 56 | #define CR0_DFS_OFFSET 0 |
| 57 | |
| 58 | #define CR0_CFS_OFFSET 2 |
| 59 | |
| 60 | #define CR0_SCPH_OFFSET 6 |
| 61 | |
| 62 | #define CR0_SCPOL_OFFSET 7 |
| 63 | |
| 64 | #define CR0_CSM_OFFSET 8 |
| 65 | #define CR0_CSM_KEEP 0x0 |
| 66 | /* ss_n be high for half sclk_out cycles */ |
| 67 | #define CR0_CSM_HALF 0X1 |
| 68 | /* ss_n be high for one sclk_out cycle */ |
| 69 | #define CR0_CSM_ONE 0x2 |
| 70 | |
| 71 | /* ss_n to sclk_out delay */ |
| 72 | #define CR0_SSD_OFFSET 10 |
| 73 | /* |
| 74 | * The period between ss_n active and |
| 75 | * sclk_out active is half sclk_out cycles |
| 76 | */ |
| 77 | #define CR0_SSD_HALF 0x0 |
| 78 | /* |
| 79 | * The period between ss_n active and |
| 80 | * sclk_out active is one sclk_out cycle |
| 81 | */ |
| 82 | #define CR0_SSD_ONE 0x1 |
| 83 | |
| 84 | #define CR0_EM_OFFSET 11 |
| 85 | #define CR0_EM_LITTLE 0x0 |
| 86 | #define CR0_EM_BIG 0x1 |
| 87 | |
| 88 | #define CR0_FBM_OFFSET 12 |
| 89 | #define CR0_FBM_MSB 0x0 |
| 90 | #define CR0_FBM_LSB 0x1 |
| 91 | |
| 92 | #define CR0_BHT_OFFSET 13 |
| 93 | #define CR0_BHT_16BIT 0x0 |
| 94 | #define CR0_BHT_8BIT 0x1 |
| 95 | |
| 96 | #define CR0_RSD_OFFSET 14 |
| 97 | |
| 98 | #define CR0_FRF_OFFSET 16 |
| 99 | #define CR0_FRF_SPI 0x0 |
| 100 | #define CR0_FRF_SSP 0x1 |
| 101 | #define CR0_FRF_MICROWIRE 0x2 |
| 102 | |
| 103 | #define CR0_XFM_OFFSET 18 |
| 104 | #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET) |
| 105 | #define CR0_XFM_TR 0x0 |
| 106 | #define CR0_XFM_TO 0x1 |
| 107 | #define CR0_XFM_RO 0x2 |
| 108 | |
| 109 | #define CR0_OPM_OFFSET 20 |
| 110 | #define CR0_OPM_MASTER 0x0 |
| 111 | #define CR0_OPM_SLAVE 0x1 |
| 112 | |
| 113 | #define CR0_MTM_OFFSET 0x21 |
| 114 | |
| 115 | /* Bit fields in SER, 2bit */ |
| 116 | #define SER_MASK 0x3 |
| 117 | |
| 118 | /* Bit fields in SR, 5bit */ |
| 119 | #define SR_MASK 0x1f |
| 120 | #define SR_BUSY (1 << 0) |
| 121 | #define SR_TF_FULL (1 << 1) |
| 122 | #define SR_TF_EMPTY (1 << 2) |
| 123 | #define SR_RF_EMPTY (1 << 3) |
| 124 | #define SR_RF_FULL (1 << 4) |
| 125 | |
| 126 | /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ |
| 127 | #define INT_MASK 0x1f |
| 128 | #define INT_TF_EMPTY (1 << 0) |
| 129 | #define INT_TF_OVERFLOW (1 << 1) |
| 130 | #define INT_RF_UNDERFLOW (1 << 2) |
| 131 | #define INT_RF_OVERFLOW (1 << 3) |
| 132 | #define INT_RF_FULL (1 << 4) |
| 133 | |
| 134 | /* Bit fields in ICR, 4bit */ |
| 135 | #define ICR_MASK 0x0f |
| 136 | #define ICR_ALL (1 << 0) |
| 137 | #define ICR_RF_UNDERFLOW (1 << 1) |
| 138 | #define ICR_RF_OVERFLOW (1 << 2) |
| 139 | #define ICR_TF_OVERFLOW (1 << 3) |
| 140 | |
| 141 | /* Bit fields in DMACR */ |
| 142 | #define RF_DMA_EN (1 << 0) |
| 143 | #define TF_DMA_EN (1 << 1) |
| 144 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 145 | /* Driver state flags */ |
| 146 | #define RXDMA (1 << 0) |
| 147 | #define TXDMA (1 << 1) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 148 | |
Addy Ke | f9cfd52 | 2014-10-15 19:25:49 +0800 | [diff] [blame] | 149 | /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ |
| 150 | #define MAX_SCLK_OUT 50000000 |
| 151 | |
Brian Norris | 5185a81 | 2016-07-14 18:30:59 -0700 | [diff] [blame] | 152 | /* |
| 153 | * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However, |
| 154 | * the controller seems to hang when given 0x10000, so stick with this for now. |
| 155 | */ |
| 156 | #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff |
| 157 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 158 | #define ROCKCHIP_SPI_MAX_CS_NUM 2 |
| 159 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 160 | struct rockchip_spi_dma_data { |
| 161 | struct dma_chan *ch; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 162 | dma_addr_t addr; |
| 163 | }; |
| 164 | |
| 165 | struct rockchip_spi { |
| 166 | struct device *dev; |
| 167 | struct spi_master *master; |
| 168 | |
| 169 | struct clk *spiclk; |
| 170 | struct clk *apb_pclk; |
| 171 | |
| 172 | void __iomem *regs; |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 173 | |
| 174 | atomic_t state; |
| 175 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 176 | /*depth of the FIFO buffer */ |
| 177 | u32 fifo_len; |
| 178 | /* max bus freq supported */ |
| 179 | u32 max_freq; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 180 | |
| 181 | u16 mode; |
| 182 | u8 tmode; |
| 183 | u8 bpw; |
| 184 | u8 n_bytes; |
Shawn Lin | 108b5c8 | 2016-03-10 14:52:27 +0800 | [diff] [blame] | 185 | u32 rsd_nsecs; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 186 | unsigned len; |
| 187 | u32 speed; |
| 188 | |
| 189 | const void *tx; |
| 190 | const void *tx_end; |
| 191 | void *rx; |
| 192 | void *rx_end; |
| 193 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 194 | bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM]; |
| 195 | |
Emil Renner Berthing | f340b92 | 2018-10-10 11:00:36 +0200 | [diff] [blame] | 196 | bool use_dma; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 197 | struct sg_table tx_sg; |
| 198 | struct sg_table rx_sg; |
| 199 | struct rockchip_spi_dma_data dma_rx; |
| 200 | struct rockchip_spi_dma_data dma_tx; |
| 201 | }; |
| 202 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 203 | static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 204 | { |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 205 | writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static inline void spi_set_clk(struct rockchip_spi *rs, u16 div) |
| 209 | { |
| 210 | writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR); |
| 211 | } |
| 212 | |
| 213 | static inline void flush_fifo(struct rockchip_spi *rs) |
| 214 | { |
| 215 | while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR)) |
| 216 | readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); |
| 217 | } |
| 218 | |
Addy Ke | 2df08e7 | 2014-07-11 10:08:24 +0800 | [diff] [blame] | 219 | static inline void wait_for_idle(struct rockchip_spi *rs) |
| 220 | { |
| 221 | unsigned long timeout = jiffies + msecs_to_jiffies(5); |
| 222 | |
| 223 | do { |
| 224 | if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) |
| 225 | return; |
Doug Anderson | 64bc011 | 2014-09-03 13:44:25 -0700 | [diff] [blame] | 226 | } while (!time_after(jiffies, timeout)); |
Addy Ke | 2df08e7 | 2014-07-11 10:08:24 +0800 | [diff] [blame] | 227 | |
| 228 | dev_warn(rs->dev, "spi controller is in busy state!\n"); |
| 229 | } |
| 230 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 231 | static u32 get_fifo_len(struct rockchip_spi *rs) |
| 232 | { |
| 233 | u32 fifo; |
| 234 | |
| 235 | for (fifo = 2; fifo < 32; fifo++) { |
| 236 | writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR); |
| 237 | if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR)) |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR); |
| 242 | |
| 243 | return (fifo == 31) ? 0 : fifo; |
| 244 | } |
| 245 | |
| 246 | static inline u32 tx_max(struct rockchip_spi *rs) |
| 247 | { |
| 248 | u32 tx_left, tx_room; |
| 249 | |
| 250 | tx_left = (rs->tx_end - rs->tx) / rs->n_bytes; |
| 251 | tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); |
| 252 | |
| 253 | return min(tx_left, tx_room); |
| 254 | } |
| 255 | |
| 256 | static inline u32 rx_max(struct rockchip_spi *rs) |
| 257 | { |
| 258 | u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes; |
| 259 | u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); |
| 260 | |
| 261 | return min(rx_left, rx_room); |
| 262 | } |
| 263 | |
| 264 | static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) |
| 265 | { |
Huibin Hong | b920cc3 | 2016-02-24 18:00:04 +0800 | [diff] [blame] | 266 | struct spi_master *master = spi->master; |
| 267 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 268 | bool cs_asserted = !enable; |
Huibin Hong | b920cc3 | 2016-02-24 18:00:04 +0800 | [diff] [blame] | 269 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 270 | /* Return immediately for no-op */ |
| 271 | if (cs_asserted == rs->cs_asserted[spi->chip_select]) |
| 272 | return; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 273 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 274 | if (cs_asserted) { |
| 275 | /* Keep things powered as long as CS is asserted */ |
| 276 | pm_runtime_get_sync(rs->dev); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 277 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 278 | ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, |
| 279 | BIT(spi->chip_select)); |
| 280 | } else { |
| 281 | ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, |
| 282 | BIT(spi->chip_select)); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 283 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 284 | /* Drop reference from when we first asserted CS */ |
| 285 | pm_runtime_put(rs->dev); |
| 286 | } |
Huibin Hong | b920cc3 | 2016-02-24 18:00:04 +0800 | [diff] [blame] | 287 | |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 288 | rs->cs_asserted[spi->chip_select] = cs_asserted; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int rockchip_spi_prepare_message(struct spi_master *master, |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 292 | struct spi_message *msg) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 293 | { |
| 294 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 295 | struct spi_device *spi = msg->spi; |
| 296 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 297 | rs->mode = spi->mode; |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Andy Shevchenko | 2291793 | 2015-02-27 17:34:16 +0200 | [diff] [blame] | 302 | static void rockchip_spi_handle_err(struct spi_master *master, |
| 303 | struct spi_message *msg) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 304 | { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 305 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 306 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 307 | /* |
| 308 | * For DMA mode, we need terminate DMA channel and flush |
| 309 | * fifo for the next transfer if DMA thansfer timeout. |
Andy Shevchenko | 2291793 | 2015-02-27 17:34:16 +0200 | [diff] [blame] | 310 | * handle_err() was called by core if transfer failed. |
| 311 | * Maybe it is reasonable for error handling here. |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 312 | */ |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 313 | if (atomic_read(&rs->state) & TXDMA) |
| 314 | dmaengine_terminate_async(rs->dma_tx.ch); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 315 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 316 | if (atomic_read(&rs->state) & RXDMA) { |
| 317 | dmaengine_terminate_async(rs->dma_rx.ch); |
| 318 | flush_fifo(rs); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 319 | } |
Andy Shevchenko | 2291793 | 2015-02-27 17:34:16 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static int rockchip_spi_unprepare_message(struct spi_master *master, |
| 323 | struct spi_message *msg) |
| 324 | { |
| 325 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 326 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 327 | spi_enable_chip(rs, false); |
Addy Ke | c28be31 | 2014-10-15 19:26:18 +0800 | [diff] [blame] | 328 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void rockchip_spi_pio_writer(struct rockchip_spi *rs) |
| 333 | { |
| 334 | u32 max = tx_max(rs); |
| 335 | u32 txw = 0; |
| 336 | |
| 337 | while (max--) { |
| 338 | if (rs->n_bytes == 1) |
| 339 | txw = *(u8 *)(rs->tx); |
| 340 | else |
| 341 | txw = *(u16 *)(rs->tx); |
| 342 | |
| 343 | writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); |
| 344 | rs->tx += rs->n_bytes; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void rockchip_spi_pio_reader(struct rockchip_spi *rs) |
| 349 | { |
| 350 | u32 max = rx_max(rs); |
| 351 | u32 rxw; |
| 352 | |
| 353 | while (max--) { |
| 354 | rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); |
| 355 | if (rs->n_bytes == 1) |
| 356 | *(u8 *)(rs->rx) = (u8)rxw; |
| 357 | else |
| 358 | *(u16 *)(rs->rx) = (u16)rxw; |
| 359 | rs->rx += rs->n_bytes; |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 360 | } |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static int rockchip_spi_pio_transfer(struct rockchip_spi *rs) |
| 364 | { |
| 365 | int remain = 0; |
| 366 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 367 | spi_enable_chip(rs, true); |
Emil Renner Berthing | a3c1740 | 2018-10-10 11:00:38 +0200 | [diff] [blame] | 368 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 369 | do { |
| 370 | if (rs->tx) { |
| 371 | remain = rs->tx_end - rs->tx; |
| 372 | rockchip_spi_pio_writer(rs); |
| 373 | } |
| 374 | |
| 375 | if (rs->rx) { |
| 376 | remain = rs->rx_end - rs->rx; |
| 377 | rockchip_spi_pio_reader(rs); |
| 378 | } |
| 379 | |
| 380 | cpu_relax(); |
| 381 | } while (remain); |
| 382 | |
Addy Ke | 2df08e7 | 2014-07-11 10:08:24 +0800 | [diff] [blame] | 383 | /* If tx, wait until the FIFO data completely. */ |
| 384 | if (rs->tx) |
| 385 | wait_for_idle(rs); |
| 386 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 387 | spi_enable_chip(rs, false); |
Addy Ke | c28be31 | 2014-10-15 19:26:18 +0800 | [diff] [blame] | 388 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static void rockchip_spi_dma_rxcb(void *data) |
| 393 | { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 394 | struct rockchip_spi *rs = data; |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 395 | int state = atomic_fetch_andnot(RXDMA, &rs->state); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 396 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 397 | if (state & TXDMA) |
| 398 | return; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 399 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 400 | spi_enable_chip(rs, false); |
| 401 | spi_finalize_current_transfer(rs->master); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static void rockchip_spi_dma_txcb(void *data) |
| 405 | { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 406 | struct rockchip_spi *rs = data; |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 407 | int state = atomic_fetch_andnot(TXDMA, &rs->state); |
| 408 | |
| 409 | if (state & RXDMA) |
| 410 | return; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 411 | |
Addy Ke | 2df08e7 | 2014-07-11 10:08:24 +0800 | [diff] [blame] | 412 | /* Wait until the FIFO data completely. */ |
| 413 | wait_for_idle(rs); |
| 414 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 415 | spi_enable_chip(rs, false); |
| 416 | spi_finalize_current_transfer(rs->master); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Shawn Lin | ea98491 | 2016-03-09 16:11:15 +0800 | [diff] [blame] | 419 | static int rockchip_spi_prepare_dma(struct rockchip_spi *rs) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 420 | { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 421 | struct dma_async_tx_descriptor *rxdesc, *txdesc; |
| 422 | |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 423 | atomic_set(&rs->state, 0); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 424 | |
Arnd Bergmann | 97cf566 | 2015-01-28 14:25:10 +0100 | [diff] [blame] | 425 | rxdesc = NULL; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 426 | if (rs->rx) { |
Emil Renner Berthing | 31bcb57 | 2018-10-31 11:56:59 +0100 | [diff] [blame] | 427 | struct dma_slave_config rxconf = { |
| 428 | .direction = DMA_DEV_TO_MEM, |
| 429 | .src_addr = rs->dma_rx.addr, |
| 430 | .src_addr_width = rs->n_bytes, |
| 431 | .src_maxburst = 1, |
| 432 | }; |
| 433 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 434 | dmaengine_slave_config(rs->dma_rx.ch, &rxconf); |
| 435 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 436 | rxdesc = dmaengine_prep_slave_sg( |
| 437 | rs->dma_rx.ch, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 438 | rs->rx_sg.sgl, rs->rx_sg.nents, |
Emil Renner Berthing | d9071b7 | 2018-10-10 11:00:37 +0200 | [diff] [blame] | 439 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); |
Shawn Lin | ea98491 | 2016-03-09 16:11:15 +0800 | [diff] [blame] | 440 | if (!rxdesc) |
| 441 | return -EINVAL; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 442 | |
| 443 | rxdesc->callback = rockchip_spi_dma_rxcb; |
| 444 | rxdesc->callback_param = rs; |
| 445 | } |
| 446 | |
Arnd Bergmann | 97cf566 | 2015-01-28 14:25:10 +0100 | [diff] [blame] | 447 | txdesc = NULL; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 448 | if (rs->tx) { |
Emil Renner Berthing | 31bcb57 | 2018-10-31 11:56:59 +0100 | [diff] [blame] | 449 | struct dma_slave_config txconf = { |
| 450 | .direction = DMA_MEM_TO_DEV, |
| 451 | .dst_addr = rs->dma_tx.addr, |
| 452 | .dst_addr_width = rs->n_bytes, |
| 453 | .dst_maxburst = rs->fifo_len / 2, |
| 454 | }; |
| 455 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 456 | dmaengine_slave_config(rs->dma_tx.ch, &txconf); |
| 457 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 458 | txdesc = dmaengine_prep_slave_sg( |
| 459 | rs->dma_tx.ch, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 460 | rs->tx_sg.sgl, rs->tx_sg.nents, |
Emil Renner Berthing | d9071b7 | 2018-10-10 11:00:37 +0200 | [diff] [blame] | 461 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); |
Shawn Lin | ea98491 | 2016-03-09 16:11:15 +0800 | [diff] [blame] | 462 | if (!txdesc) { |
| 463 | if (rxdesc) |
| 464 | dmaengine_terminate_sync(rs->dma_rx.ch); |
| 465 | return -EINVAL; |
| 466 | } |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 467 | |
| 468 | txdesc->callback = rockchip_spi_dma_txcb; |
| 469 | txdesc->callback_param = rs; |
| 470 | } |
| 471 | |
| 472 | /* rx must be started before tx due to spi instinct */ |
Arnd Bergmann | 97cf566 | 2015-01-28 14:25:10 +0100 | [diff] [blame] | 473 | if (rxdesc) { |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 474 | atomic_or(RXDMA, &rs->state); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 475 | dmaengine_submit(rxdesc); |
| 476 | dma_async_issue_pending(rs->dma_rx.ch); |
| 477 | } |
| 478 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 479 | spi_enable_chip(rs, true); |
Emil Renner Berthing | a3c1740 | 2018-10-10 11:00:38 +0200 | [diff] [blame] | 480 | |
Arnd Bergmann | 97cf566 | 2015-01-28 14:25:10 +0100 | [diff] [blame] | 481 | if (txdesc) { |
Emil Renner Berthing | fab3e48 | 2018-10-31 11:57:01 +0100 | [diff] [blame^] | 482 | atomic_or(TXDMA, &rs->state); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 483 | dmaengine_submit(txdesc); |
| 484 | dma_async_issue_pending(rs->dma_tx.ch); |
| 485 | } |
Shawn Lin | ea98491 | 2016-03-09 16:11:15 +0800 | [diff] [blame] | 486 | |
Emil Renner Berthing | a3c1740 | 2018-10-10 11:00:38 +0200 | [diff] [blame] | 487 | /* 1 means the transfer is in progress */ |
| 488 | return 1; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static void rockchip_spi_config(struct rockchip_spi *rs) |
| 492 | { |
| 493 | u32 div = 0; |
| 494 | u32 dmacr = 0; |
Julius Werner | 76b17e6 | 2015-03-26 16:30:25 -0700 | [diff] [blame] | 495 | int rsd = 0; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 496 | |
Emil Renner Berthing | 2410d6a | 2018-10-31 11:57:00 +0100 | [diff] [blame] | 497 | u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET |
| 498 | | CR0_BHT_8BIT << CR0_BHT_OFFSET |
| 499 | | CR0_SSD_ONE << CR0_SSD_OFFSET |
| 500 | | CR0_EM_BIG << CR0_EM_OFFSET; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 501 | |
| 502 | cr0 |= (rs->n_bytes << CR0_DFS_OFFSET); |
| 503 | cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET); |
| 504 | cr0 |= (rs->tmode << CR0_XFM_OFFSET); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 505 | |
| 506 | if (rs->use_dma) { |
| 507 | if (rs->tx) |
| 508 | dmacr |= TF_DMA_EN; |
| 509 | if (rs->rx) |
| 510 | dmacr |= RF_DMA_EN; |
| 511 | } |
| 512 | |
Addy Ke | f9cfd52 | 2014-10-15 19:25:49 +0800 | [diff] [blame] | 513 | if (WARN_ON(rs->speed > MAX_SCLK_OUT)) |
| 514 | rs->speed = MAX_SCLK_OUT; |
| 515 | |
Geert Uytterhoeven | bb51537 | 2016-03-14 16:30:16 +0100 | [diff] [blame] | 516 | /* the minimum divisor is 2 */ |
Addy Ke | f9cfd52 | 2014-10-15 19:25:49 +0800 | [diff] [blame] | 517 | if (rs->max_freq < 2 * rs->speed) { |
| 518 | clk_set_rate(rs->spiclk, 2 * rs->speed); |
| 519 | rs->max_freq = clk_get_rate(rs->spiclk); |
| 520 | } |
| 521 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 522 | /* div doesn't support odd number */ |
Julius Werner | 754ec43 | 2015-03-26 16:30:24 -0700 | [diff] [blame] | 523 | div = DIV_ROUND_UP(rs->max_freq, rs->speed); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 524 | div = (div + 1) & 0xfffe; |
| 525 | |
Julius Werner | 76b17e6 | 2015-03-26 16:30:25 -0700 | [diff] [blame] | 526 | /* Rx sample delay is expressed in parent clock cycles (max 3) */ |
| 527 | rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8), |
| 528 | 1000000000 >> 8); |
| 529 | if (!rsd && rs->rsd_nsecs) { |
| 530 | pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n", |
| 531 | rs->max_freq, rs->rsd_nsecs); |
| 532 | } else if (rsd > 3) { |
| 533 | rsd = 3; |
| 534 | pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n", |
| 535 | rs->max_freq, rs->rsd_nsecs, |
| 536 | rsd * 1000000000U / rs->max_freq); |
| 537 | } |
| 538 | cr0 |= rsd << CR0_RSD_OFFSET; |
| 539 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 540 | writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); |
| 541 | |
Huibin Hong | 04b37d2 | 2017-08-16 10:12:02 +0800 | [diff] [blame] | 542 | if (rs->n_bytes == 1) |
| 543 | writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); |
| 544 | else if (rs->n_bytes == 2) |
| 545 | writel_relaxed((rs->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); |
| 546 | else |
| 547 | writel_relaxed((rs->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1); |
| 548 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 549 | writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR); |
| 550 | writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); |
| 551 | |
Huibin Hong | dcfc861 | 2018-10-10 11:00:33 +0200 | [diff] [blame] | 552 | writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 553 | writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR); |
| 554 | writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); |
| 555 | |
| 556 | spi_set_clk(rs, div); |
| 557 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 558 | dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 559 | } |
| 560 | |
Brian Norris | 5185a81 | 2016-07-14 18:30:59 -0700 | [diff] [blame] | 561 | static size_t rockchip_spi_max_transfer_size(struct spi_device *spi) |
| 562 | { |
| 563 | return ROCKCHIP_SPI_MAX_TRANLEN; |
| 564 | } |
| 565 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 566 | static int rockchip_spi_transfer_one( |
| 567 | struct spi_master *master, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 568 | struct spi_device *spi, |
| 569 | struct spi_transfer *xfer) |
| 570 | { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 571 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 572 | |
Doug Anderson | 6294617 | 2014-09-03 13:44:26 -0700 | [diff] [blame] | 573 | WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && |
| 574 | (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 575 | |
| 576 | if (!xfer->tx_buf && !xfer->rx_buf) { |
| 577 | dev_err(rs->dev, "No buffer for transfer\n"); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
Brian Norris | 5185a81 | 2016-07-14 18:30:59 -0700 | [diff] [blame] | 581 | if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) { |
| 582 | dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 586 | rs->speed = xfer->speed_hz; |
| 587 | rs->bpw = xfer->bits_per_word; |
| 588 | rs->n_bytes = rs->bpw >> 3; |
| 589 | |
| 590 | rs->tx = xfer->tx_buf; |
| 591 | rs->tx_end = rs->tx + xfer->len; |
| 592 | rs->rx = xfer->rx_buf; |
| 593 | rs->rx_end = rs->rx + xfer->len; |
| 594 | rs->len = xfer->len; |
| 595 | |
| 596 | rs->tx_sg = xfer->tx_sg; |
| 597 | rs->rx_sg = xfer->rx_sg; |
| 598 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 599 | if (rs->tx && rs->rx) |
| 600 | rs->tmode = CR0_XFM_TR; |
| 601 | else if (rs->tx) |
| 602 | rs->tmode = CR0_XFM_TO; |
| 603 | else if (rs->rx) |
| 604 | rs->tmode = CR0_XFM_RO; |
| 605 | |
Addy Ke | a24e70c | 2014-09-25 14:59:41 +0800 | [diff] [blame] | 606 | /* we need prepare dma before spi was enabled */ |
Addy Ke | c28be31 | 2014-10-15 19:26:18 +0800 | [diff] [blame] | 607 | if (master->can_dma && master->can_dma(master, spi, xfer)) |
Emil Renner Berthing | f340b92 | 2018-10-10 11:00:36 +0200 | [diff] [blame] | 608 | rs->use_dma = true; |
Addy Ke | c28be31 | 2014-10-15 19:26:18 +0800 | [diff] [blame] | 609 | else |
Emil Renner Berthing | f340b92 | 2018-10-10 11:00:36 +0200 | [diff] [blame] | 610 | rs->use_dma = false; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 611 | |
| 612 | rockchip_spi_config(rs); |
| 613 | |
Emil Renner Berthing | a3c1740 | 2018-10-10 11:00:38 +0200 | [diff] [blame] | 614 | if (rs->use_dma) |
| 615 | return rockchip_spi_prepare_dma(rs); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 616 | |
Emil Renner Berthing | a3c1740 | 2018-10-10 11:00:38 +0200 | [diff] [blame] | 617 | return rockchip_spi_pio_transfer(rs); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static bool rockchip_spi_can_dma(struct spi_master *master, |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 621 | struct spi_device *spi, |
| 622 | struct spi_transfer *xfer) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 623 | { |
| 624 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 625 | |
| 626 | return (xfer->len > rs->fifo_len); |
| 627 | } |
| 628 | |
| 629 | static int rockchip_spi_probe(struct platform_device *pdev) |
| 630 | { |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 631 | int ret; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 632 | struct rockchip_spi *rs; |
| 633 | struct spi_master *master; |
| 634 | struct resource *mem; |
Julius Werner | 76b17e6 | 2015-03-26 16:30:25 -0700 | [diff] [blame] | 635 | u32 rsd_nsecs; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 636 | |
| 637 | master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi)); |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 638 | if (!master) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 639 | return -ENOMEM; |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 640 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 641 | platform_set_drvdata(pdev, master); |
| 642 | |
| 643 | rs = spi_master_get_devdata(master); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 644 | |
| 645 | /* Get basic io resource and map it */ |
| 646 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 647 | rs->regs = devm_ioremap_resource(&pdev->dev, mem); |
| 648 | if (IS_ERR(rs->regs)) { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 649 | ret = PTR_ERR(rs->regs); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 650 | goto err_put_master; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); |
| 654 | if (IS_ERR(rs->apb_pclk)) { |
| 655 | dev_err(&pdev->dev, "Failed to get apb_pclk\n"); |
| 656 | ret = PTR_ERR(rs->apb_pclk); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 657 | goto err_put_master; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); |
| 661 | if (IS_ERR(rs->spiclk)) { |
| 662 | dev_err(&pdev->dev, "Failed to get spi_pclk\n"); |
| 663 | ret = PTR_ERR(rs->spiclk); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 664 | goto err_put_master; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | ret = clk_prepare_enable(rs->apb_pclk); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 668 | if (ret < 0) { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 669 | dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 670 | goto err_put_master; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | ret = clk_prepare_enable(rs->spiclk); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 674 | if (ret < 0) { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 675 | dev_err(&pdev->dev, "Failed to enable spi_clk\n"); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 676 | goto err_disable_apbclk; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Emil Renner Berthing | 30688e4 | 2018-10-31 11:56:58 +0100 | [diff] [blame] | 679 | spi_enable_chip(rs, false); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 680 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 681 | rs->master = master; |
| 682 | rs->dev = &pdev->dev; |
| 683 | rs->max_freq = clk_get_rate(rs->spiclk); |
| 684 | |
Julius Werner | 76b17e6 | 2015-03-26 16:30:25 -0700 | [diff] [blame] | 685 | if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", |
| 686 | &rsd_nsecs)) |
| 687 | rs->rsd_nsecs = rsd_nsecs; |
| 688 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 689 | rs->fifo_len = get_fifo_len(rs); |
| 690 | if (!rs->fifo_len) { |
| 691 | dev_err(&pdev->dev, "Failed to get fifo length\n"); |
Wei Yongjun | db7e8d9 | 2014-07-20 22:02:04 +0800 | [diff] [blame] | 692 | ret = -EINVAL; |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 693 | goto err_disable_spiclk; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 694 | } |
| 695 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 696 | pm_runtime_set_active(&pdev->dev); |
| 697 | pm_runtime_enable(&pdev->dev); |
| 698 | |
| 699 | master->auto_runtime_pm = true; |
| 700 | master->bus_num = pdev->id; |
Addy Ke | ee78099 | 2014-07-11 10:08:51 +0800 | [diff] [blame] | 701 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP; |
Jeffy Chen | aa09938 | 2017-06-28 12:38:43 +0800 | [diff] [blame] | 702 | master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 703 | master->dev.of_node = pdev->dev.of_node; |
| 704 | master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8); |
| 705 | |
| 706 | master->set_cs = rockchip_spi_set_cs; |
| 707 | master->prepare_message = rockchip_spi_prepare_message; |
| 708 | master->unprepare_message = rockchip_spi_unprepare_message; |
| 709 | master->transfer_one = rockchip_spi_transfer_one; |
Brian Norris | 5185a81 | 2016-07-14 18:30:59 -0700 | [diff] [blame] | 710 | master->max_transfer_size = rockchip_spi_max_transfer_size; |
Andy Shevchenko | 2291793 | 2015-02-27 17:34:16 +0200 | [diff] [blame] | 711 | master->handle_err = rockchip_spi_handle_err; |
Jeffy Chen | c863795 | 2017-06-28 12:38:42 +0800 | [diff] [blame] | 712 | master->flags = SPI_MASTER_GPIO_SS; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 713 | |
Shawn Lin | e4c0e06 | 2016-03-31 11:11:41 +0800 | [diff] [blame] | 714 | rs->dma_tx.ch = dma_request_chan(rs->dev, "tx"); |
| 715 | if (IS_ERR(rs->dma_tx.ch)) { |
Shawn Lin | 61cadcf | 2016-03-09 16:11:32 +0800 | [diff] [blame] | 716 | /* Check tx to see if we need defer probing driver */ |
| 717 | if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) { |
| 718 | ret = -EPROBE_DEFER; |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 719 | goto err_disable_pm_runtime; |
Shawn Lin | 61cadcf | 2016-03-09 16:11:32 +0800 | [diff] [blame] | 720 | } |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 721 | dev_warn(rs->dev, "Failed to request TX DMA channel\n"); |
Shawn Lin | e4c0e06 | 2016-03-31 11:11:41 +0800 | [diff] [blame] | 722 | rs->dma_tx.ch = NULL; |
Shawn Lin | 61cadcf | 2016-03-09 16:11:32 +0800 | [diff] [blame] | 723 | } |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 724 | |
Shawn Lin | e4c0e06 | 2016-03-31 11:11:41 +0800 | [diff] [blame] | 725 | rs->dma_rx.ch = dma_request_chan(rs->dev, "rx"); |
| 726 | if (IS_ERR(rs->dma_rx.ch)) { |
| 727 | if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) { |
Shawn Lin | e4c0e06 | 2016-03-31 11:11:41 +0800 | [diff] [blame] | 728 | ret = -EPROBE_DEFER; |
Dan Carpenter | 5de7ed0 | 2016-05-04 09:25:46 +0300 | [diff] [blame] | 729 | goto err_free_dma_tx; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 730 | } |
| 731 | dev_warn(rs->dev, "Failed to request RX DMA channel\n"); |
Shawn Lin | e4c0e06 | 2016-03-31 11:11:41 +0800 | [diff] [blame] | 732 | rs->dma_rx.ch = NULL; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (rs->dma_tx.ch && rs->dma_rx.ch) { |
| 736 | rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR); |
| 737 | rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 738 | |
| 739 | master->can_dma = rockchip_spi_can_dma; |
| 740 | master->dma_tx = rs->dma_tx.ch; |
| 741 | master->dma_rx = rs->dma_rx.ch; |
| 742 | } |
| 743 | |
| 744 | ret = devm_spi_register_master(&pdev->dev, master); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 745 | if (ret < 0) { |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 746 | dev_err(&pdev->dev, "Failed to register master\n"); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 747 | goto err_free_dma_rx; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 748 | } |
| 749 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 750 | return 0; |
| 751 | |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 752 | err_free_dma_rx: |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 753 | if (rs->dma_rx.ch) |
| 754 | dma_release_channel(rs->dma_rx.ch); |
Dan Carpenter | 5de7ed0 | 2016-05-04 09:25:46 +0300 | [diff] [blame] | 755 | err_free_dma_tx: |
| 756 | if (rs->dma_tx.ch) |
| 757 | dma_release_channel(rs->dma_tx.ch); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 758 | err_disable_pm_runtime: |
| 759 | pm_runtime_disable(&pdev->dev); |
| 760 | err_disable_spiclk: |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 761 | clk_disable_unprepare(rs->spiclk); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 762 | err_disable_apbclk: |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 763 | clk_disable_unprepare(rs->apb_pclk); |
Jeffy Chen | c351587 | 2017-06-13 13:25:40 +0800 | [diff] [blame] | 764 | err_put_master: |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 765 | spi_master_put(master); |
| 766 | |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | static int rockchip_spi_remove(struct platform_device *pdev) |
| 771 | { |
| 772 | struct spi_master *master = spi_master_get(platform_get_drvdata(pdev)); |
| 773 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 774 | |
Jeffy Chen | 6a06e89 | 2017-08-07 20:40:19 +0800 | [diff] [blame] | 775 | pm_runtime_get_sync(&pdev->dev); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 776 | |
| 777 | clk_disable_unprepare(rs->spiclk); |
| 778 | clk_disable_unprepare(rs->apb_pclk); |
| 779 | |
Jeffy Chen | 6a06e89 | 2017-08-07 20:40:19 +0800 | [diff] [blame] | 780 | pm_runtime_put_noidle(&pdev->dev); |
| 781 | pm_runtime_disable(&pdev->dev); |
| 782 | pm_runtime_set_suspended(&pdev->dev); |
| 783 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 784 | if (rs->dma_tx.ch) |
| 785 | dma_release_channel(rs->dma_tx.ch); |
| 786 | if (rs->dma_rx.ch) |
| 787 | dma_release_channel(rs->dma_rx.ch); |
| 788 | |
Shawn Lin | 844c9f4 | 2016-02-15 16:28:12 +0800 | [diff] [blame] | 789 | spi_master_put(master); |
| 790 | |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | #ifdef CONFIG_PM_SLEEP |
| 795 | static int rockchip_spi_suspend(struct device *dev) |
| 796 | { |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 797 | int ret; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 798 | struct spi_master *master = dev_get_drvdata(dev); |
| 799 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 800 | |
| 801 | ret = spi_master_suspend(rs->master); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 802 | if (ret < 0) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 803 | return ret; |
| 804 | |
Jeffy Chen | d38c4ae1 | 2017-08-07 20:40:20 +0800 | [diff] [blame] | 805 | ret = pm_runtime_force_suspend(dev); |
| 806 | if (ret < 0) |
| 807 | return ret; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 808 | |
Brian Norris | 23e291c | 2016-12-16 16:59:16 -0800 | [diff] [blame] | 809 | pinctrl_pm_select_sleep_state(dev); |
| 810 | |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 811 | return 0; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int rockchip_spi_resume(struct device *dev) |
| 815 | { |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 816 | int ret; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 817 | struct spi_master *master = dev_get_drvdata(dev); |
| 818 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 819 | |
Brian Norris | 23e291c | 2016-12-16 16:59:16 -0800 | [diff] [blame] | 820 | pinctrl_pm_select_default_state(dev); |
| 821 | |
Jeffy Chen | d38c4ae1 | 2017-08-07 20:40:20 +0800 | [diff] [blame] | 822 | ret = pm_runtime_force_resume(dev); |
| 823 | if (ret < 0) |
| 824 | return ret; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 825 | |
| 826 | ret = spi_master_resume(rs->master); |
| 827 | if (ret < 0) { |
| 828 | clk_disable_unprepare(rs->spiclk); |
| 829 | clk_disable_unprepare(rs->apb_pclk); |
| 830 | } |
| 831 | |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 832 | return 0; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 833 | } |
| 834 | #endif /* CONFIG_PM_SLEEP */ |
| 835 | |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 836 | #ifdef CONFIG_PM |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 837 | static int rockchip_spi_runtime_suspend(struct device *dev) |
| 838 | { |
| 839 | struct spi_master *master = dev_get_drvdata(dev); |
| 840 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 841 | |
| 842 | clk_disable_unprepare(rs->spiclk); |
| 843 | clk_disable_unprepare(rs->apb_pclk); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int rockchip_spi_runtime_resume(struct device *dev) |
| 849 | { |
| 850 | int ret; |
| 851 | struct spi_master *master = dev_get_drvdata(dev); |
| 852 | struct rockchip_spi *rs = spi_master_get_devdata(master); |
| 853 | |
| 854 | ret = clk_prepare_enable(rs->apb_pclk); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 855 | if (ret < 0) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 856 | return ret; |
| 857 | |
| 858 | ret = clk_prepare_enable(rs->spiclk); |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 859 | if (ret < 0) |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 860 | clk_disable_unprepare(rs->apb_pclk); |
| 861 | |
Jeffy Chen | 43de979 | 2017-08-07 20:40:18 +0800 | [diff] [blame] | 862 | return 0; |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 863 | } |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 864 | #endif /* CONFIG_PM */ |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 865 | |
| 866 | static const struct dev_pm_ops rockchip_spi_pm = { |
| 867 | SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) |
| 868 | SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, |
| 869 | rockchip_spi_runtime_resume, NULL) |
| 870 | }; |
| 871 | |
| 872 | static const struct of_device_id rockchip_spi_dt_match[] = { |
Andy Yan | 6b860e6 | 2017-08-14 16:34:22 +0800 | [diff] [blame] | 873 | { .compatible = "rockchip,rv1108-spi", }, |
Caesar Wang | aa29ea3 | 2016-05-20 07:56:21 +0800 | [diff] [blame] | 874 | { .compatible = "rockchip,rk3036-spi", }, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 875 | { .compatible = "rockchip,rk3066-spi", }, |
Addy Ke | b839b78 | 2014-07-11 10:09:19 +0800 | [diff] [blame] | 876 | { .compatible = "rockchip,rk3188-spi", }, |
Caesar Wang | aa29ea3 | 2016-05-20 07:56:21 +0800 | [diff] [blame] | 877 | { .compatible = "rockchip,rk3228-spi", }, |
Addy Ke | b839b78 | 2014-07-11 10:09:19 +0800 | [diff] [blame] | 878 | { .compatible = "rockchip,rk3288-spi", }, |
Caesar Wang | aa29ea3 | 2016-05-20 07:56:21 +0800 | [diff] [blame] | 879 | { .compatible = "rockchip,rk3368-spi", }, |
Xu Jianqun | 9b7a562 | 2016-02-18 19:16:31 +0800 | [diff] [blame] | 880 | { .compatible = "rockchip,rk3399-spi", }, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 881 | { }, |
| 882 | }; |
| 883 | MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); |
| 884 | |
| 885 | static struct platform_driver rockchip_spi_driver = { |
| 886 | .driver = { |
| 887 | .name = DRIVER_NAME, |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 888 | .pm = &rockchip_spi_pm, |
| 889 | .of_match_table = of_match_ptr(rockchip_spi_dt_match), |
| 890 | }, |
| 891 | .probe = rockchip_spi_probe, |
| 892 | .remove = rockchip_spi_remove, |
| 893 | }; |
| 894 | |
| 895 | module_platform_driver(rockchip_spi_driver); |
| 896 | |
Addy Ke | 5dcc44e | 2014-07-11 10:07:56 +0800 | [diff] [blame] | 897 | MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); |
addy ke | 64e3682 | 2014-07-01 09:03:59 +0800 | [diff] [blame] | 898 | MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); |
| 899 | MODULE_LICENSE("GPL v2"); |