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