Lanqing Liu | e7d973a | 2018-08-16 20:54:51 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2018 Spreadtrum Communications Inc. |
| 3 | |
| 4 | #include <linux/clk.h> |
| 5 | #include <linux/interrupt.h> |
| 6 | #include <linux/io.h> |
| 7 | #include <linux/iopoll.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/of_device.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/pm_runtime.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | |
| 16 | #define SPRD_SPI_TXD 0x0 |
| 17 | #define SPRD_SPI_CLKD 0x4 |
| 18 | #define SPRD_SPI_CTL0 0x8 |
| 19 | #define SPRD_SPI_CTL1 0xc |
| 20 | #define SPRD_SPI_CTL2 0x10 |
| 21 | #define SPRD_SPI_CTL3 0x14 |
| 22 | #define SPRD_SPI_CTL4 0x18 |
| 23 | #define SPRD_SPI_CTL5 0x1c |
| 24 | #define SPRD_SPI_INT_EN 0x20 |
| 25 | #define SPRD_SPI_INT_CLR 0x24 |
| 26 | #define SPRD_SPI_INT_RAW_STS 0x28 |
| 27 | #define SPRD_SPI_INT_MASK_STS 0x2c |
| 28 | #define SPRD_SPI_STS1 0x30 |
| 29 | #define SPRD_SPI_STS2 0x34 |
| 30 | #define SPRD_SPI_DSP_WAIT 0x38 |
| 31 | #define SPRD_SPI_STS3 0x3c |
| 32 | #define SPRD_SPI_CTL6 0x40 |
| 33 | #define SPRD_SPI_STS4 0x44 |
| 34 | #define SPRD_SPI_FIFO_RST 0x48 |
| 35 | #define SPRD_SPI_CTL7 0x4c |
| 36 | #define SPRD_SPI_STS5 0x50 |
| 37 | #define SPRD_SPI_CTL8 0x54 |
| 38 | #define SPRD_SPI_CTL9 0x58 |
| 39 | #define SPRD_SPI_CTL10 0x5c |
| 40 | #define SPRD_SPI_CTL11 0x60 |
| 41 | #define SPRD_SPI_CTL12 0x64 |
| 42 | #define SPRD_SPI_STS6 0x68 |
| 43 | #define SPRD_SPI_STS7 0x6c |
| 44 | #define SPRD_SPI_STS8 0x70 |
| 45 | #define SPRD_SPI_STS9 0x74 |
| 46 | |
| 47 | /* Bits & mask definition for register CTL0 */ |
| 48 | #define SPRD_SPI_SCK_REV BIT(13) |
| 49 | #define SPRD_SPI_NG_TX BIT(1) |
| 50 | #define SPRD_SPI_NG_RX BIT(0) |
| 51 | #define SPRD_SPI_CHNL_LEN_MASK GENMASK(4, 0) |
| 52 | #define SPRD_SPI_CSN_MASK GENMASK(11, 8) |
| 53 | #define SPRD_SPI_CS0_VALID BIT(8) |
| 54 | |
| 55 | /* Bits & mask definition for register SPI_INT_EN */ |
| 56 | #define SPRD_SPI_TX_END_INT_EN BIT(8) |
| 57 | #define SPRD_SPI_RX_END_INT_EN BIT(9) |
| 58 | |
| 59 | /* Bits & mask definition for register SPI_INT_RAW_STS */ |
| 60 | #define SPRD_SPI_TX_END_RAW BIT(8) |
| 61 | #define SPRD_SPI_RX_END_RAW BIT(9) |
| 62 | |
| 63 | /* Bits & mask definition for register SPI_INT_CLR */ |
| 64 | #define SPRD_SPI_TX_END_CLR BIT(8) |
| 65 | #define SPRD_SPI_RX_END_CLR BIT(9) |
| 66 | |
| 67 | /* Bits & mask definition for register INT_MASK_STS */ |
| 68 | #define SPRD_SPI_MASK_RX_END BIT(9) |
| 69 | #define SPRD_SPI_MASK_TX_END BIT(8) |
| 70 | |
| 71 | /* Bits & mask definition for register STS2 */ |
| 72 | #define SPRD_SPI_TX_BUSY BIT(8) |
| 73 | |
| 74 | /* Bits & mask definition for register CTL1 */ |
| 75 | #define SPRD_SPI_RX_MODE BIT(12) |
| 76 | #define SPRD_SPI_TX_MODE BIT(13) |
| 77 | #define SPRD_SPI_RTX_MD_MASK GENMASK(13, 12) |
| 78 | |
| 79 | /* Bits & mask definition for register CTL2 */ |
| 80 | #define SPRD_SPI_DMA_EN BIT(6) |
| 81 | |
| 82 | /* Bits & mask definition for register CTL4 */ |
| 83 | #define SPRD_SPI_START_RX BIT(9) |
| 84 | #define SPRD_SPI_ONLY_RECV_MASK GENMASK(8, 0) |
| 85 | |
| 86 | /* Bits & mask definition for register SPI_INT_CLR */ |
| 87 | #define SPRD_SPI_RX_END_INT_CLR BIT(9) |
| 88 | #define SPRD_SPI_TX_END_INT_CLR BIT(8) |
| 89 | |
| 90 | /* Bits & mask definition for register SPI_INT_RAW */ |
| 91 | #define SPRD_SPI_RX_END_IRQ BIT(9) |
| 92 | #define SPRD_SPI_TX_END_IRQ BIT(8) |
| 93 | |
| 94 | /* Bits & mask definition for register CTL12 */ |
| 95 | #define SPRD_SPI_SW_RX_REQ BIT(0) |
| 96 | #define SPRD_SPI_SW_TX_REQ BIT(1) |
| 97 | |
| 98 | /* Bits & mask definition for register CTL7 */ |
| 99 | #define SPRD_SPI_DATA_LINE2_EN BIT(15) |
| 100 | #define SPRD_SPI_MODE_MASK GENMASK(5, 3) |
| 101 | #define SPRD_SPI_MODE_OFFSET 3 |
| 102 | #define SPRD_SPI_3WIRE_MODE 4 |
| 103 | #define SPRD_SPI_4WIRE_MODE 0 |
| 104 | |
| 105 | /* Bits & mask definition for register CTL8 */ |
| 106 | #define SPRD_SPI_TX_MAX_LEN_MASK GENMASK(19, 0) |
| 107 | #define SPRD_SPI_TX_LEN_H_MASK GENMASK(3, 0) |
| 108 | #define SPRD_SPI_TX_LEN_H_OFFSET 16 |
| 109 | |
| 110 | /* Bits & mask definition for register CTL9 */ |
| 111 | #define SPRD_SPI_TX_LEN_L_MASK GENMASK(15, 0) |
| 112 | |
| 113 | /* Bits & mask definition for register CTL10 */ |
| 114 | #define SPRD_SPI_RX_MAX_LEN_MASK GENMASK(19, 0) |
| 115 | #define SPRD_SPI_RX_LEN_H_MASK GENMASK(3, 0) |
| 116 | #define SPRD_SPI_RX_LEN_H_OFFSET 16 |
| 117 | |
| 118 | /* Bits & mask definition for register CTL11 */ |
| 119 | #define SPRD_SPI_RX_LEN_L_MASK GENMASK(15, 0) |
| 120 | |
| 121 | /* Default & maximum word delay cycles */ |
| 122 | #define SPRD_SPI_MIN_DELAY_CYCLE 14 |
| 123 | #define SPRD_SPI_MAX_DELAY_CYCLE 130 |
| 124 | |
| 125 | #define SPRD_SPI_FIFO_SIZE 32 |
| 126 | #define SPRD_SPI_CHIP_CS_NUM 0x4 |
| 127 | #define SPRD_SPI_CHNL_LEN 2 |
| 128 | #define SPRD_SPI_DEFAULT_SOURCE 26000000 |
| 129 | #define SPRD_SPI_MAX_SPEED_HZ 48000000 |
| 130 | #define SPRD_SPI_AUTOSUSPEND_DELAY 100 |
| 131 | |
| 132 | struct sprd_spi { |
| 133 | void __iomem *base; |
| 134 | struct device *dev; |
| 135 | struct clk *clk; |
| 136 | u32 src_clk; |
| 137 | u32 hw_mode; |
| 138 | u32 trans_len; |
| 139 | u32 trans_mode; |
| 140 | u32 word_delay; |
| 141 | u32 hw_speed_hz; |
| 142 | u32 len; |
| 143 | int status; |
| 144 | const void *tx_buf; |
| 145 | void *rx_buf; |
| 146 | int (*read_bufs)(struct sprd_spi *ss, u32 len); |
| 147 | int (*write_bufs)(struct sprd_spi *ss, u32 len); |
| 148 | }; |
| 149 | |
| 150 | static u32 sprd_spi_transfer_max_timeout(struct sprd_spi *ss, |
| 151 | struct spi_transfer *t) |
| 152 | { |
| 153 | /* |
| 154 | * The time spent on transmission of the full FIFO data is the maximum |
| 155 | * SPI transmission time. |
| 156 | */ |
| 157 | u32 size = t->bits_per_word * SPRD_SPI_FIFO_SIZE; |
| 158 | u32 bit_time_us = DIV_ROUND_UP(USEC_PER_SEC, ss->hw_speed_hz); |
| 159 | u32 total_time_us = size * bit_time_us; |
| 160 | /* |
| 161 | * There is an interval between data and the data in our SPI hardware, |
| 162 | * so the total transmission time need add the interval time. |
| 163 | */ |
| 164 | u32 interval_cycle = SPRD_SPI_FIFO_SIZE * ss->word_delay; |
| 165 | u32 interval_time_us = DIV_ROUND_UP(interval_cycle * USEC_PER_SEC, |
| 166 | ss->src_clk); |
| 167 | |
| 168 | return total_time_us + interval_time_us; |
| 169 | } |
| 170 | |
| 171 | static int sprd_spi_wait_for_tx_end(struct sprd_spi *ss, struct spi_transfer *t) |
| 172 | { |
| 173 | u32 val, us; |
| 174 | int ret; |
| 175 | |
| 176 | us = sprd_spi_transfer_max_timeout(ss, t); |
| 177 | ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_INT_RAW_STS, val, |
| 178 | val & SPRD_SPI_TX_END_IRQ, 0, us); |
| 179 | if (ret) { |
| 180 | dev_err(ss->dev, "SPI error, spi send timeout!\n"); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_STS2, val, |
| 185 | !(val & SPRD_SPI_TX_BUSY), 0, us); |
| 186 | if (ret) { |
| 187 | dev_err(ss->dev, "SPI error, spi busy timeout!\n"); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | writel_relaxed(SPRD_SPI_TX_END_INT_CLR, ss->base + SPRD_SPI_INT_CLR); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int sprd_spi_wait_for_rx_end(struct sprd_spi *ss, struct spi_transfer *t) |
| 197 | { |
| 198 | u32 val, us; |
| 199 | int ret; |
| 200 | |
| 201 | us = sprd_spi_transfer_max_timeout(ss, t); |
| 202 | ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_INT_RAW_STS, val, |
| 203 | val & SPRD_SPI_RX_END_IRQ, 0, us); |
| 204 | if (ret) { |
| 205 | dev_err(ss->dev, "SPI error, spi rx timeout!\n"); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | writel_relaxed(SPRD_SPI_RX_END_INT_CLR, ss->base + SPRD_SPI_INT_CLR); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static void sprd_spi_tx_req(struct sprd_spi *ss) |
| 215 | { |
| 216 | writel_relaxed(SPRD_SPI_SW_TX_REQ, ss->base + SPRD_SPI_CTL12); |
| 217 | } |
| 218 | |
| 219 | static void sprd_spi_rx_req(struct sprd_spi *ss) |
| 220 | { |
| 221 | writel_relaxed(SPRD_SPI_SW_RX_REQ, ss->base + SPRD_SPI_CTL12); |
| 222 | } |
| 223 | |
| 224 | static void sprd_spi_enter_idle(struct sprd_spi *ss) |
| 225 | { |
| 226 | u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL1); |
| 227 | |
| 228 | val &= ~SPRD_SPI_RTX_MD_MASK; |
| 229 | writel_relaxed(val, ss->base + SPRD_SPI_CTL1); |
| 230 | } |
| 231 | |
| 232 | static void sprd_spi_set_transfer_bits(struct sprd_spi *ss, u32 bits) |
| 233 | { |
| 234 | u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL0); |
| 235 | |
| 236 | /* Set the valid bits for every transaction */ |
| 237 | val &= ~(SPRD_SPI_CHNL_LEN_MASK << SPRD_SPI_CHNL_LEN); |
| 238 | val |= bits << SPRD_SPI_CHNL_LEN; |
| 239 | writel_relaxed(val, ss->base + SPRD_SPI_CTL0); |
| 240 | } |
| 241 | |
| 242 | static void sprd_spi_set_tx_length(struct sprd_spi *ss, u32 length) |
| 243 | { |
| 244 | u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL8); |
| 245 | |
| 246 | length &= SPRD_SPI_TX_MAX_LEN_MASK; |
| 247 | val &= ~SPRD_SPI_TX_LEN_H_MASK; |
| 248 | val |= length >> SPRD_SPI_TX_LEN_H_OFFSET; |
| 249 | writel_relaxed(val, ss->base + SPRD_SPI_CTL8); |
| 250 | |
| 251 | val = length & SPRD_SPI_TX_LEN_L_MASK; |
| 252 | writel_relaxed(val, ss->base + SPRD_SPI_CTL9); |
| 253 | } |
| 254 | |
| 255 | static void sprd_spi_set_rx_length(struct sprd_spi *ss, u32 length) |
| 256 | { |
| 257 | u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL10); |
| 258 | |
| 259 | length &= SPRD_SPI_RX_MAX_LEN_MASK; |
| 260 | val &= ~SPRD_SPI_RX_LEN_H_MASK; |
| 261 | val |= length >> SPRD_SPI_RX_LEN_H_OFFSET; |
| 262 | writel_relaxed(val, ss->base + SPRD_SPI_CTL10); |
| 263 | |
| 264 | val = length & SPRD_SPI_RX_LEN_L_MASK; |
| 265 | writel_relaxed(val, ss->base + SPRD_SPI_CTL11); |
| 266 | } |
| 267 | |
| 268 | static void sprd_spi_chipselect(struct spi_device *sdev, bool cs) |
| 269 | { |
| 270 | struct spi_controller *sctlr = sdev->controller; |
| 271 | struct sprd_spi *ss = spi_controller_get_devdata(sctlr); |
| 272 | u32 val; |
| 273 | |
| 274 | val = readl_relaxed(ss->base + SPRD_SPI_CTL0); |
| 275 | /* The SPI controller will pull down CS pin if cs is 0 */ |
| 276 | if (!cs) { |
| 277 | val &= ~SPRD_SPI_CS0_VALID; |
| 278 | writel_relaxed(val, ss->base + SPRD_SPI_CTL0); |
| 279 | } else { |
| 280 | val |= SPRD_SPI_CSN_MASK; |
| 281 | writel_relaxed(val, ss->base + SPRD_SPI_CTL0); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static int sprd_spi_write_only_receive(struct sprd_spi *ss, u32 len) |
| 286 | { |
| 287 | u32 val; |
| 288 | |
| 289 | /* Clear the start receive bit and reset receive data number */ |
| 290 | val = readl_relaxed(ss->base + SPRD_SPI_CTL4); |
| 291 | val &= ~(SPRD_SPI_START_RX | SPRD_SPI_ONLY_RECV_MASK); |
| 292 | writel_relaxed(val, ss->base + SPRD_SPI_CTL4); |
| 293 | |
| 294 | /* Set the receive data length */ |
| 295 | val = readl_relaxed(ss->base + SPRD_SPI_CTL4); |
| 296 | val |= len & SPRD_SPI_ONLY_RECV_MASK; |
| 297 | writel_relaxed(val, ss->base + SPRD_SPI_CTL4); |
| 298 | |
| 299 | /* Trigger to receive data */ |
| 300 | val = readl_relaxed(ss->base + SPRD_SPI_CTL4); |
| 301 | val |= SPRD_SPI_START_RX; |
| 302 | writel_relaxed(val, ss->base + SPRD_SPI_CTL4); |
| 303 | |
| 304 | return len; |
| 305 | } |
| 306 | |
| 307 | static int sprd_spi_write_bufs_u8(struct sprd_spi *ss, u32 len) |
| 308 | { |
| 309 | u8 *tx_p = (u8 *)ss->tx_buf; |
| 310 | int i; |
| 311 | |
| 312 | for (i = 0; i < len; i++) |
| 313 | writeb_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD); |
| 314 | |
| 315 | ss->tx_buf += i; |
| 316 | return i; |
| 317 | } |
| 318 | |
| 319 | static int sprd_spi_write_bufs_u16(struct sprd_spi *ss, u32 len) |
| 320 | { |
| 321 | u16 *tx_p = (u16 *)ss->tx_buf; |
| 322 | int i; |
| 323 | |
| 324 | for (i = 0; i < len; i++) |
| 325 | writew_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD); |
| 326 | |
| 327 | ss->tx_buf += i << 1; |
| 328 | return i << 1; |
| 329 | } |
| 330 | |
| 331 | static int sprd_spi_write_bufs_u32(struct sprd_spi *ss, u32 len) |
| 332 | { |
| 333 | u32 *tx_p = (u32 *)ss->tx_buf; |
| 334 | int i; |
| 335 | |
| 336 | for (i = 0; i < len; i++) |
| 337 | writel_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD); |
| 338 | |
| 339 | ss->tx_buf += i << 2; |
| 340 | return i << 2; |
| 341 | } |
| 342 | |
| 343 | static int sprd_spi_read_bufs_u8(struct sprd_spi *ss, u32 len) |
| 344 | { |
| 345 | u8 *rx_p = (u8 *)ss->rx_buf; |
| 346 | int i; |
| 347 | |
| 348 | for (i = 0; i < len; i++) |
| 349 | rx_p[i] = readb_relaxed(ss->base + SPRD_SPI_TXD); |
| 350 | |
| 351 | ss->rx_buf += i; |
| 352 | return i; |
| 353 | } |
| 354 | |
| 355 | static int sprd_spi_read_bufs_u16(struct sprd_spi *ss, u32 len) |
| 356 | { |
| 357 | u16 *rx_p = (u16 *)ss->rx_buf; |
| 358 | int i; |
| 359 | |
| 360 | for (i = 0; i < len; i++) |
| 361 | rx_p[i] = readw_relaxed(ss->base + SPRD_SPI_TXD); |
| 362 | |
| 363 | ss->rx_buf += i << 1; |
| 364 | return i << 1; |
| 365 | } |
| 366 | |
| 367 | static int sprd_spi_read_bufs_u32(struct sprd_spi *ss, u32 len) |
| 368 | { |
| 369 | u32 *rx_p = (u32 *)ss->rx_buf; |
| 370 | int i; |
| 371 | |
| 372 | for (i = 0; i < len; i++) |
| 373 | rx_p[i] = readl_relaxed(ss->base + SPRD_SPI_TXD); |
| 374 | |
| 375 | ss->rx_buf += i << 2; |
| 376 | return i << 2; |
| 377 | } |
| 378 | |
| 379 | static int sprd_spi_txrx_bufs(struct spi_device *sdev, struct spi_transfer *t) |
| 380 | { |
| 381 | struct sprd_spi *ss = spi_controller_get_devdata(sdev->controller); |
| 382 | u32 trans_len = ss->trans_len, len; |
Lanqing Liu | 63f5ffc | 2019-01-15 21:46:50 +0800 | [diff] [blame^] | 383 | int ret, write_size = 0, read_size = 0; |
Lanqing Liu | e7d973a | 2018-08-16 20:54:51 +0800 | [diff] [blame] | 384 | |
| 385 | while (trans_len) { |
| 386 | len = trans_len > SPRD_SPI_FIFO_SIZE ? SPRD_SPI_FIFO_SIZE : |
| 387 | trans_len; |
| 388 | if (ss->trans_mode & SPRD_SPI_TX_MODE) { |
| 389 | sprd_spi_set_tx_length(ss, len); |
| 390 | write_size += ss->write_bufs(ss, len); |
| 391 | |
| 392 | /* |
| 393 | * For our 3 wires mode or dual TX line mode, we need |
| 394 | * to request the controller to transfer. |
| 395 | */ |
| 396 | if (ss->hw_mode & SPI_3WIRE || ss->hw_mode & SPI_TX_DUAL) |
| 397 | sprd_spi_tx_req(ss); |
| 398 | |
| 399 | ret = sprd_spi_wait_for_tx_end(ss, t); |
| 400 | } else { |
| 401 | sprd_spi_set_rx_length(ss, len); |
| 402 | |
| 403 | /* |
| 404 | * For our 3 wires mode or dual TX line mode, we need |
| 405 | * to request the controller to read. |
| 406 | */ |
| 407 | if (ss->hw_mode & SPI_3WIRE || ss->hw_mode & SPI_TX_DUAL) |
| 408 | sprd_spi_rx_req(ss); |
| 409 | else |
| 410 | write_size += ss->write_bufs(ss, len); |
| 411 | |
| 412 | ret = sprd_spi_wait_for_rx_end(ss, t); |
| 413 | } |
| 414 | |
| 415 | if (ret) |
| 416 | goto complete; |
| 417 | |
| 418 | if (ss->trans_mode & SPRD_SPI_RX_MODE) |
Lanqing Liu | 63f5ffc | 2019-01-15 21:46:50 +0800 | [diff] [blame^] | 419 | read_size += ss->read_bufs(ss, len); |
Lanqing Liu | e7d973a | 2018-08-16 20:54:51 +0800 | [diff] [blame] | 420 | |
| 421 | trans_len -= len; |
| 422 | } |
| 423 | |
Lanqing Liu | 63f5ffc | 2019-01-15 21:46:50 +0800 | [diff] [blame^] | 424 | if (ss->trans_mode & SPRD_SPI_TX_MODE) |
| 425 | ret = write_size; |
| 426 | else |
| 427 | ret = read_size; |
Lanqing Liu | e7d973a | 2018-08-16 20:54:51 +0800 | [diff] [blame] | 428 | complete: |
| 429 | sprd_spi_enter_idle(ss); |
| 430 | |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static void sprd_spi_set_speed(struct sprd_spi *ss, u32 speed_hz) |
| 435 | { |
| 436 | /* |
| 437 | * From SPI datasheet, the prescale calculation formula: |
| 438 | * prescale = SPI source clock / (2 * SPI_freq) - 1; |
| 439 | */ |
| 440 | u32 clk_div = DIV_ROUND_UP(ss->src_clk, speed_hz << 1) - 1; |
| 441 | |
| 442 | /* Save the real hardware speed */ |
| 443 | ss->hw_speed_hz = (ss->src_clk >> 1) / (clk_div + 1); |
| 444 | writel_relaxed(clk_div, ss->base + SPRD_SPI_CLKD); |
| 445 | } |
| 446 | |
| 447 | static void sprd_spi_init_hw(struct sprd_spi *ss, struct spi_transfer *t) |
| 448 | { |
| 449 | u16 word_delay, interval; |
| 450 | u32 val; |
| 451 | |
| 452 | val = readl_relaxed(ss->base + SPRD_SPI_CTL7); |
| 453 | val &= ~(SPRD_SPI_SCK_REV | SPRD_SPI_NG_TX | SPRD_SPI_NG_RX); |
| 454 | /* Set default chip selection, clock phase and clock polarity */ |
| 455 | val |= ss->hw_mode & SPI_CPHA ? SPRD_SPI_NG_RX : SPRD_SPI_NG_TX; |
| 456 | val |= ss->hw_mode & SPI_CPOL ? SPRD_SPI_SCK_REV : 0; |
| 457 | writel_relaxed(val, ss->base + SPRD_SPI_CTL0); |
| 458 | |
| 459 | /* |
| 460 | * Set the intervals of two SPI frames, and the inteval calculation |
| 461 | * formula as below per datasheet: |
| 462 | * interval time (source clock cycles) = interval * 4 + 10. |
| 463 | */ |
| 464 | word_delay = clamp_t(u16, t->word_delay, SPRD_SPI_MIN_DELAY_CYCLE, |
| 465 | SPRD_SPI_MAX_DELAY_CYCLE); |
| 466 | interval = DIV_ROUND_UP(word_delay - 10, 4); |
| 467 | ss->word_delay = interval * 4 + 10; |
| 468 | writel_relaxed(interval, ss->base + SPRD_SPI_CTL5); |
| 469 | |
| 470 | /* Reset SPI fifo */ |
| 471 | writel_relaxed(1, ss->base + SPRD_SPI_FIFO_RST); |
| 472 | writel_relaxed(0, ss->base + SPRD_SPI_FIFO_RST); |
| 473 | |
| 474 | /* Set SPI work mode */ |
| 475 | val = readl_relaxed(ss->base + SPRD_SPI_CTL7); |
| 476 | val &= ~SPRD_SPI_MODE_MASK; |
| 477 | |
| 478 | if (ss->hw_mode & SPI_3WIRE) |
| 479 | val |= SPRD_SPI_3WIRE_MODE << SPRD_SPI_MODE_OFFSET; |
| 480 | else |
| 481 | val |= SPRD_SPI_4WIRE_MODE << SPRD_SPI_MODE_OFFSET; |
| 482 | |
| 483 | if (ss->hw_mode & SPI_TX_DUAL) |
| 484 | val |= SPRD_SPI_DATA_LINE2_EN; |
| 485 | else |
| 486 | val &= ~SPRD_SPI_DATA_LINE2_EN; |
| 487 | |
| 488 | writel_relaxed(val, ss->base + SPRD_SPI_CTL7); |
| 489 | } |
| 490 | |
| 491 | static int sprd_spi_setup_transfer(struct spi_device *sdev, |
| 492 | struct spi_transfer *t) |
| 493 | { |
| 494 | struct sprd_spi *ss = spi_controller_get_devdata(sdev->controller); |
| 495 | u8 bits_per_word = t->bits_per_word; |
| 496 | u32 val, mode = 0; |
| 497 | |
| 498 | ss->len = t->len; |
| 499 | ss->tx_buf = t->tx_buf; |
| 500 | ss->rx_buf = t->rx_buf; |
| 501 | |
| 502 | ss->hw_mode = sdev->mode; |
| 503 | sprd_spi_init_hw(ss, t); |
| 504 | |
| 505 | /* Set tansfer speed and valid bits */ |
| 506 | sprd_spi_set_speed(ss, t->speed_hz); |
| 507 | sprd_spi_set_transfer_bits(ss, bits_per_word); |
| 508 | |
| 509 | if (bits_per_word > 16) |
| 510 | bits_per_word = round_up(bits_per_word, 16); |
| 511 | else |
| 512 | bits_per_word = round_up(bits_per_word, 8); |
| 513 | |
| 514 | switch (bits_per_word) { |
| 515 | case 8: |
| 516 | ss->trans_len = t->len; |
| 517 | ss->read_bufs = sprd_spi_read_bufs_u8; |
| 518 | ss->write_bufs = sprd_spi_write_bufs_u8; |
| 519 | break; |
| 520 | case 16: |
| 521 | ss->trans_len = t->len >> 1; |
| 522 | ss->read_bufs = sprd_spi_read_bufs_u16; |
| 523 | ss->write_bufs = sprd_spi_write_bufs_u16; |
| 524 | break; |
| 525 | case 32: |
| 526 | ss->trans_len = t->len >> 2; |
| 527 | ss->read_bufs = sprd_spi_read_bufs_u32; |
| 528 | ss->write_bufs = sprd_spi_write_bufs_u32; |
| 529 | break; |
| 530 | default: |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | /* Set transfer read or write mode */ |
| 535 | val = readl_relaxed(ss->base + SPRD_SPI_CTL1); |
| 536 | val &= ~SPRD_SPI_RTX_MD_MASK; |
| 537 | if (t->tx_buf) |
| 538 | mode |= SPRD_SPI_TX_MODE; |
| 539 | if (t->rx_buf) |
| 540 | mode |= SPRD_SPI_RX_MODE; |
| 541 | |
| 542 | writel_relaxed(val | mode, ss->base + SPRD_SPI_CTL1); |
| 543 | |
| 544 | ss->trans_mode = mode; |
| 545 | |
| 546 | /* |
| 547 | * If in only receive mode, we need to trigger the SPI controller to |
| 548 | * receive data automatically. |
| 549 | */ |
| 550 | if (ss->trans_mode == SPRD_SPI_RX_MODE) |
| 551 | ss->write_bufs = sprd_spi_write_only_receive; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int sprd_spi_transfer_one(struct spi_controller *sctlr, |
| 557 | struct spi_device *sdev, |
| 558 | struct spi_transfer *t) |
| 559 | { |
| 560 | int ret; |
| 561 | |
| 562 | ret = sprd_spi_setup_transfer(sdev, t); |
| 563 | if (ret) |
| 564 | goto setup_err; |
| 565 | |
| 566 | ret = sprd_spi_txrx_bufs(sdev, t); |
| 567 | if (ret == t->len) |
| 568 | ret = 0; |
| 569 | else if (ret >= 0) |
| 570 | ret = -EREMOTEIO; |
| 571 | |
| 572 | setup_err: |
| 573 | spi_finalize_current_transfer(sctlr); |
| 574 | |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | static int sprd_spi_clk_init(struct platform_device *pdev, struct sprd_spi *ss) |
| 579 | { |
| 580 | struct clk *clk_spi, *clk_parent; |
| 581 | |
| 582 | clk_spi = devm_clk_get(&pdev->dev, "spi"); |
| 583 | if (IS_ERR(clk_spi)) { |
| 584 | dev_warn(&pdev->dev, "can't get the spi clock\n"); |
| 585 | clk_spi = NULL; |
| 586 | } |
| 587 | |
| 588 | clk_parent = devm_clk_get(&pdev->dev, "source"); |
| 589 | if (IS_ERR(clk_parent)) { |
| 590 | dev_warn(&pdev->dev, "can't get the source clock\n"); |
| 591 | clk_parent = NULL; |
| 592 | } |
| 593 | |
| 594 | ss->clk = devm_clk_get(&pdev->dev, "enable"); |
| 595 | if (IS_ERR(ss->clk)) { |
| 596 | dev_err(&pdev->dev, "can't get the enable clock\n"); |
| 597 | return PTR_ERR(ss->clk); |
| 598 | } |
| 599 | |
| 600 | if (!clk_set_parent(clk_spi, clk_parent)) |
| 601 | ss->src_clk = clk_get_rate(clk_spi); |
| 602 | else |
| 603 | ss->src_clk = SPRD_SPI_DEFAULT_SOURCE; |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int sprd_spi_probe(struct platform_device *pdev) |
| 609 | { |
| 610 | struct spi_controller *sctlr; |
| 611 | struct resource *res; |
| 612 | struct sprd_spi *ss; |
| 613 | int ret; |
| 614 | |
| 615 | pdev->id = of_alias_get_id(pdev->dev.of_node, "spi"); |
| 616 | sctlr = spi_alloc_master(&pdev->dev, sizeof(*ss)); |
| 617 | if (!sctlr) |
| 618 | return -ENOMEM; |
| 619 | |
| 620 | ss = spi_controller_get_devdata(sctlr); |
| 621 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 622 | ss->base = devm_ioremap_resource(&pdev->dev, res); |
| 623 | if (IS_ERR(ss->base)) { |
| 624 | ret = PTR_ERR(ss->base); |
| 625 | goto free_controller; |
| 626 | } |
| 627 | |
| 628 | ss->dev = &pdev->dev; |
| 629 | sctlr->dev.of_node = pdev->dev.of_node; |
| 630 | sctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_3WIRE | SPI_TX_DUAL; |
| 631 | sctlr->bus_num = pdev->id; |
| 632 | sctlr->set_cs = sprd_spi_chipselect; |
| 633 | sctlr->transfer_one = sprd_spi_transfer_one; |
| 634 | sctlr->auto_runtime_pm = true; |
| 635 | sctlr->max_speed_hz = min_t(u32, ss->src_clk >> 1, |
| 636 | SPRD_SPI_MAX_SPEED_HZ); |
| 637 | |
| 638 | platform_set_drvdata(pdev, sctlr); |
| 639 | ret = sprd_spi_clk_init(pdev, ss); |
| 640 | if (ret) |
| 641 | goto free_controller; |
| 642 | |
| 643 | ret = clk_prepare_enable(ss->clk); |
| 644 | if (ret) |
| 645 | goto free_controller; |
| 646 | |
| 647 | ret = pm_runtime_set_active(&pdev->dev); |
| 648 | if (ret < 0) |
| 649 | goto disable_clk; |
| 650 | |
| 651 | pm_runtime_set_autosuspend_delay(&pdev->dev, |
| 652 | SPRD_SPI_AUTOSUSPEND_DELAY); |
| 653 | pm_runtime_use_autosuspend(&pdev->dev); |
| 654 | pm_runtime_enable(&pdev->dev); |
| 655 | ret = pm_runtime_get_sync(&pdev->dev); |
| 656 | if (ret < 0) { |
| 657 | dev_err(&pdev->dev, "failed to resume SPI controller\n"); |
| 658 | goto err_rpm_put; |
| 659 | } |
| 660 | |
| 661 | ret = devm_spi_register_controller(&pdev->dev, sctlr); |
| 662 | if (ret) |
| 663 | goto err_rpm_put; |
| 664 | |
| 665 | pm_runtime_mark_last_busy(&pdev->dev); |
| 666 | pm_runtime_put_autosuspend(&pdev->dev); |
| 667 | |
| 668 | return 0; |
| 669 | |
| 670 | err_rpm_put: |
| 671 | pm_runtime_put_noidle(&pdev->dev); |
| 672 | pm_runtime_disable(&pdev->dev); |
| 673 | disable_clk: |
| 674 | clk_disable_unprepare(ss->clk); |
| 675 | free_controller: |
| 676 | spi_controller_put(sctlr); |
| 677 | |
| 678 | return ret; |
| 679 | } |
| 680 | |
Arnd Bergmann | 71d9a84 | 2018-09-26 14:58:21 +0200 | [diff] [blame] | 681 | static int sprd_spi_remove(struct platform_device *pdev) |
Lanqing Liu | e7d973a | 2018-08-16 20:54:51 +0800 | [diff] [blame] | 682 | { |
| 683 | struct spi_controller *sctlr = platform_get_drvdata(pdev); |
| 684 | struct sprd_spi *ss = spi_controller_get_devdata(sctlr); |
| 685 | int ret; |
| 686 | |
| 687 | ret = pm_runtime_get_sync(ss->dev); |
| 688 | if (ret < 0) { |
| 689 | dev_err(ss->dev, "failed to resume SPI controller\n"); |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | clk_disable_unprepare(ss->clk); |
| 694 | pm_runtime_put_noidle(&pdev->dev); |
| 695 | pm_runtime_disable(&pdev->dev); |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int __maybe_unused sprd_spi_runtime_suspend(struct device *dev) |
| 701 | { |
| 702 | struct spi_controller *sctlr = dev_get_drvdata(dev); |
| 703 | struct sprd_spi *ss = spi_controller_get_devdata(sctlr); |
| 704 | |
| 705 | clk_disable_unprepare(ss->clk); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static int __maybe_unused sprd_spi_runtime_resume(struct device *dev) |
| 711 | { |
| 712 | struct spi_controller *sctlr = dev_get_drvdata(dev); |
| 713 | struct sprd_spi *ss = spi_controller_get_devdata(sctlr); |
| 714 | int ret; |
| 715 | |
| 716 | ret = clk_prepare_enable(ss->clk); |
| 717 | if (ret) |
| 718 | return ret; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static const struct dev_pm_ops sprd_spi_pm_ops = { |
| 724 | SET_RUNTIME_PM_OPS(sprd_spi_runtime_suspend, |
| 725 | sprd_spi_runtime_resume, NULL) |
| 726 | }; |
| 727 | |
| 728 | static const struct of_device_id sprd_spi_of_match[] = { |
| 729 | { .compatible = "sprd,sc9860-spi", }, |
| 730 | { /* sentinel */ } |
| 731 | }; |
| 732 | |
| 733 | static struct platform_driver sprd_spi_driver = { |
| 734 | .driver = { |
| 735 | .name = "sprd-spi", |
| 736 | .of_match_table = sprd_spi_of_match, |
| 737 | .pm = &sprd_spi_pm_ops, |
| 738 | }, |
| 739 | .probe = sprd_spi_probe, |
| 740 | .remove = sprd_spi_remove, |
| 741 | }; |
| 742 | |
| 743 | module_platform_driver(sprd_spi_driver); |
| 744 | |
| 745 | MODULE_DESCRIPTION("Spreadtrum SPI Controller driver"); |
| 746 | MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>"); |
| 747 | MODULE_LICENSE("GPL v2"); |