1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * SuperH MSIOF SPI Controller Interface 4 * 5 * Copyright (c) 2009 Magnus Damm 6 * Copyright (C) 2014 Renesas Electronics Corporation 7 * Copyright (C) 2014-2017 Glider bvba 8 */ 9 10 #include <linux/bitmap.h> 11 #include <linux/clk.h> 12 #include <linux/completion.h> 13 #include <linux/delay.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/dmaengine.h> 16 #include <linux/err.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/iopoll.h> 20 #include <linux/kernel.h> 21 #include <linux/module.h> 22 #include <linux/of.h> 23 #include <linux/of_graph.h> 24 #include <linux/platform_device.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/sh_dma.h> 27 28 #include <linux/spi/sh_msiof.h> 29 #include <linux/spi/spi.h> 30 31 #include <linux/unaligned.h> 32 33 #define SH_MSIOF_FLAG_FIXED_DTDL_200 BIT(0) 34 35 struct sh_msiof_chipdata { 36 u32 bits_per_word_mask; 37 u16 tx_fifo_size; 38 u16 rx_fifo_size; 39 u16 ctlr_flags; 40 u16 min_div_pow; 41 u32 flags; 42 }; 43 44 struct sh_msiof_spi_priv { 45 struct spi_controller *ctlr; 46 void __iomem *mapbase; 47 struct clk *clk; 48 struct platform_device *pdev; 49 struct sh_msiof_spi_info *info; 50 struct completion done; 51 struct completion done_txdma; 52 unsigned int tx_fifo_size; 53 unsigned int rx_fifo_size; 54 unsigned int min_div_pow; 55 void *tx_dma_page; 56 void *rx_dma_page; 57 dma_addr_t tx_dma_addr; 58 dma_addr_t rx_dma_addr; 59 bool native_cs_inited; 60 bool native_cs_high; 61 bool target_aborted; 62 }; 63 64 #define MAX_SS 3 /* Maximum number of native chip selects */ 65 66 static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs) 67 { 68 switch (reg_offs) { 69 case SITSCR: 70 case SIRSCR: 71 return ioread16(p->mapbase + reg_offs); 72 default: 73 return ioread32(p->mapbase + reg_offs); 74 } 75 } 76 77 static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs, 78 u32 value) 79 { 80 switch (reg_offs) { 81 case SITSCR: 82 case SIRSCR: 83 iowrite16(value, p->mapbase + reg_offs); 84 break; 85 default: 86 iowrite32(value, p->mapbase + reg_offs); 87 break; 88 } 89 } 90 91 static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p, 92 u32 clr, u32 set) 93 { 94 u32 mask = clr | set; 95 u32 data; 96 97 data = sh_msiof_read(p, SICTR); 98 data &= ~clr; 99 data |= set; 100 sh_msiof_write(p, SICTR, data); 101 102 return readl_poll_timeout_atomic(p->mapbase + SICTR, data, 103 (data & mask) == set, 1, 100); 104 } 105 106 static irqreturn_t sh_msiof_spi_irq(int irq, void *data) 107 { 108 struct sh_msiof_spi_priv *p = data; 109 110 /* just disable the interrupt and wake up */ 111 sh_msiof_write(p, SIIER, 0); 112 complete(&p->done); 113 114 return IRQ_HANDLED; 115 } 116 117 static int sh_msiof_spi_reset_regs(struct sh_msiof_spi_priv *p) 118 { 119 u32 mask = SICTR_TXRST | SICTR_RXRST; 120 u32 data; 121 122 data = sh_msiof_read(p, SICTR); 123 data |= mask; 124 sh_msiof_write(p, SICTR, data); 125 126 return readl_poll_timeout_atomic(p->mapbase + SICTR, data, 127 !(data & mask), 1, 100); 128 } 129 130 static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p, 131 struct spi_transfer *t) 132 { 133 unsigned long parent_rate = clk_get_rate(p->clk); 134 unsigned int div_pow = p->min_div_pow; 135 u32 spi_hz = t->speed_hz; 136 unsigned long div; 137 u32 brps, scr; 138 139 if (!spi_hz || !parent_rate) { 140 WARN(1, "Invalid clock rate parameters %lu and %u\n", 141 parent_rate, spi_hz); 142 return; 143 } 144 145 div = DIV_ROUND_UP(parent_rate, spi_hz); 146 if (div <= 1024) { 147 /* SISCR_BRDV_DIV_1 is valid only if BRPS is x 1/1 or x 1/2 */ 148 if (!div_pow && div <= 32 && div > 2) 149 div_pow = 1; 150 151 if (div_pow) 152 brps = (div + 1) >> div_pow; 153 else 154 brps = div; 155 156 for (; brps > 32; div_pow++) 157 brps = (brps + 1) >> 1; 158 } else { 159 /* Set transfer rate composite divisor to 2^5 * 32 = 1024 */ 160 dev_err(&p->pdev->dev, 161 "Requested SPI transfer rate %d is too low\n", spi_hz); 162 div_pow = 5; 163 brps = 32; 164 } 165 166 t->effective_speed_hz = parent_rate / (brps << div_pow); 167 168 /* div_pow == 0 maps to SISCR_BRDV_DIV_1 == all ones */ 169 scr = FIELD_PREP(SISCR_BRDV, div_pow - 1) | 170 FIELD_PREP(SISCR_BRPS, brps - 1); 171 sh_msiof_write(p, SITSCR, scr); 172 if (!(p->ctlr->flags & SPI_CONTROLLER_MUST_TX)) 173 sh_msiof_write(p, SIRSCR, scr); 174 } 175 176 static u32 sh_msiof_get_delay_bit(u32 dtdl_or_syncdl) 177 { 178 /* 179 * DTDL/SYNCDL bit : p->info->dtdl or p->info->syncdl 180 * b'000 : 0 181 * b'001 : 100 182 * b'010 : 200 183 * b'011 (SYNCDL only) : 300 184 * b'101 : 50 185 * b'110 : 150 186 */ 187 if (dtdl_or_syncdl % 100) 188 return dtdl_or_syncdl / 100 + 5; 189 else 190 return dtdl_or_syncdl / 100; 191 } 192 193 static u32 sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv *p) 194 { 195 u32 val; 196 197 if (!p->info) 198 return 0; 199 200 /* check if DTDL and SYNCDL is allowed value */ 201 if (p->info->dtdl > 200 || p->info->syncdl > 300) { 202 dev_warn(&p->pdev->dev, "DTDL or SYNCDL is too large\n"); 203 return 0; 204 } 205 206 /* check if the sum of DTDL and SYNCDL becomes an integer value */ 207 if ((p->info->dtdl + p->info->syncdl) % 100) { 208 dev_warn(&p->pdev->dev, "the sum of DTDL/SYNCDL is not good\n"); 209 return 0; 210 } 211 212 val = FIELD_PREP(SIMDR1_DTDL, sh_msiof_get_delay_bit(p->info->dtdl)) | 213 FIELD_PREP(SIMDR1_SYNCDL, 214 sh_msiof_get_delay_bit(p->info->syncdl)); 215 216 return val; 217 } 218 219 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p, u32 ss, 220 bool cpol, bool cpha, bool tx_hi_z, 221 bool lsb_first, bool cs_high) 222 { 223 bool edge; 224 u32 tmp; 225 226 /* 227 * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG 228 * 0 0 10 10 1 1 229 * 0 1 10 10 0 0 230 * 1 0 11 11 0 0 231 * 1 1 11 11 1 1 232 */ 233 tmp = FIELD_PREP(SIMDR1_SYNCMD, SIMDR1_SYNCMD_SPI) | 234 FIELD_PREP(SIMDR1_FLD, 1) | SIMDR1_XXSTP | 235 FIELD_PREP(SIMDR1_SYNCAC, !cs_high) | 236 FIELD_PREP(SIMDR1_BITLSB, lsb_first); 237 tmp |= sh_msiof_spi_get_dtdl_and_syncdl(p); 238 if (spi_controller_is_target(p->ctlr)) { 239 sh_msiof_write(p, SITMDR1, tmp | SITMDR1_PCON); 240 } else { 241 sh_msiof_write(p, SITMDR1, 242 tmp | SIMDR1_TRMD | SITMDR1_PCON | 243 FIELD_PREP(SITMDR1_SYNCCH, 244 ss < MAX_SS ? ss : 0)); 245 } 246 if (p->ctlr->flags & SPI_CONTROLLER_MUST_TX) { 247 /* These bits are reserved if RX needs TX */ 248 tmp &= ~0x0000ffff; 249 } 250 sh_msiof_write(p, SIRMDR1, tmp); 251 252 tmp = 0; 253 tmp |= SICTR_TSCKIZ_SCK | FIELD_PREP(SICTR_TSCKIZ_POL, cpol); 254 tmp |= SICTR_RSCKIZ_SCK | FIELD_PREP(SICTR_RSCKIZ_POL, cpol); 255 256 edge = cpol ^ !cpha; 257 258 tmp |= FIELD_PREP(SICTR_TEDG, edge); 259 tmp |= FIELD_PREP(SICTR_REDG, edge); 260 tmp |= FIELD_PREP(SICTR_TXDIZ, 261 tx_hi_z ? SICTR_TXDIZ_HIZ : SICTR_TXDIZ_LOW); 262 sh_msiof_write(p, SICTR, tmp); 263 } 264 265 static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p, 266 const void *tx_buf, void *rx_buf, 267 u32 bits, u32 words1, u32 words2) 268 { 269 u32 dr2 = FIELD_PREP(SIMDR2_GRP, words2 ? 1 : 0) | 270 FIELD_PREP(SIMDR2_BITLEN1, bits - 1) | 271 FIELD_PREP(SIMDR2_WDLEN1, words1 - 1); 272 273 if (tx_buf || (p->ctlr->flags & SPI_CONTROLLER_MUST_TX)) 274 sh_msiof_write(p, SITMDR2, dr2); 275 else 276 sh_msiof_write(p, SITMDR2, dr2 | SIMDR2_GRPMASK); 277 278 if (rx_buf) 279 sh_msiof_write(p, SIRMDR2, dr2); 280 281 if (words2) { 282 u32 dr3 = FIELD_PREP(SIMDR3_BITLEN2, bits - 1) | 283 FIELD_PREP(SIMDR3_WDLEN2, words2 - 1); 284 285 sh_msiof_write(p, SITMDR3, dr3); 286 if (rx_buf) 287 sh_msiof_write(p, SIRMDR3, dr3); 288 } 289 } 290 291 static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p) 292 { 293 sh_msiof_write(p, SISTR, 294 sh_msiof_read(p, SISTR) & ~(SISTR_TDREQ | SISTR_RDREQ)); 295 } 296 297 static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p, 298 const void *tx_buf, unsigned int words, 299 unsigned int fs) 300 { 301 const u8 *buf_8 = tx_buf; 302 unsigned int k; 303 304 for (k = 0; k < words; k++) 305 sh_msiof_write(p, SITFDR, buf_8[k] << fs); 306 } 307 308 static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p, 309 const void *tx_buf, unsigned int words, 310 unsigned int fs) 311 { 312 const u16 *buf_16 = tx_buf; 313 unsigned int k; 314 315 for (k = 0; k < words; k++) 316 sh_msiof_write(p, SITFDR, buf_16[k] << fs); 317 } 318 319 static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p, 320 const void *tx_buf, unsigned int words, 321 unsigned int fs) 322 { 323 const u16 *buf_16 = tx_buf; 324 unsigned int k; 325 326 for (k = 0; k < words; k++) 327 sh_msiof_write(p, SITFDR, get_unaligned(&buf_16[k]) << fs); 328 } 329 330 static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p, 331 const void *tx_buf, unsigned int words, 332 unsigned int fs) 333 { 334 const u32 *buf_32 = tx_buf; 335 unsigned int k; 336 337 for (k = 0; k < words; k++) 338 sh_msiof_write(p, SITFDR, buf_32[k] << fs); 339 } 340 341 static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p, 342 const void *tx_buf, unsigned int words, 343 unsigned int fs) 344 { 345 const u32 *buf_32 = tx_buf; 346 unsigned int k; 347 348 for (k = 0; k < words; k++) 349 sh_msiof_write(p, SITFDR, get_unaligned(&buf_32[k]) << fs); 350 } 351 352 static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p, 353 const void *tx_buf, unsigned int words, 354 unsigned int fs) 355 { 356 const u32 *buf_32 = tx_buf; 357 unsigned int k; 358 359 for (k = 0; k < words; k++) 360 sh_msiof_write(p, SITFDR, swab32(buf_32[k] << fs)); 361 } 362 363 static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p, 364 const void *tx_buf, 365 unsigned int words, unsigned int fs) 366 { 367 const u32 *buf_32 = tx_buf; 368 unsigned int k; 369 370 for (k = 0; k < words; k++) 371 sh_msiof_write(p, SITFDR, swab32(get_unaligned(&buf_32[k]) << fs)); 372 } 373 374 static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p, 375 void *rx_buf, unsigned int words, 376 unsigned int fs) 377 { 378 u8 *buf_8 = rx_buf; 379 unsigned int k; 380 381 for (k = 0; k < words; k++) 382 buf_8[k] = sh_msiof_read(p, SIRFDR) >> fs; 383 } 384 385 static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p, 386 void *rx_buf, unsigned int words, 387 unsigned int fs) 388 { 389 u16 *buf_16 = rx_buf; 390 unsigned int k; 391 392 for (k = 0; k < words; k++) 393 buf_16[k] = sh_msiof_read(p, SIRFDR) >> fs; 394 } 395 396 static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p, 397 void *rx_buf, unsigned int words, 398 unsigned int fs) 399 { 400 u16 *buf_16 = rx_buf; 401 unsigned int k; 402 403 for (k = 0; k < words; k++) 404 put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_16[k]); 405 } 406 407 static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p, 408 void *rx_buf, unsigned int words, 409 unsigned int fs) 410 { 411 u32 *buf_32 = rx_buf; 412 unsigned int k; 413 414 for (k = 0; k < words; k++) 415 buf_32[k] = sh_msiof_read(p, SIRFDR) >> fs; 416 } 417 418 static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p, 419 void *rx_buf, unsigned int words, 420 unsigned int fs) 421 { 422 u32 *buf_32 = rx_buf; 423 unsigned int k; 424 425 for (k = 0; k < words; k++) 426 put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_32[k]); 427 } 428 429 static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p, 430 void *rx_buf, unsigned int words, 431 unsigned int fs) 432 { 433 u32 *buf_32 = rx_buf; 434 unsigned int k; 435 436 for (k = 0; k < words; k++) 437 buf_32[k] = swab32(sh_msiof_read(p, SIRFDR) >> fs); 438 } 439 440 static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p, 441 void *rx_buf, unsigned int words, 442 unsigned int fs) 443 { 444 u32 *buf_32 = rx_buf; 445 unsigned int k; 446 447 for (k = 0; k < words; k++) 448 put_unaligned(swab32(sh_msiof_read(p, SIRFDR) >> fs), &buf_32[k]); 449 } 450 451 static int sh_msiof_spi_setup(struct spi_device *spi) 452 { 453 struct sh_msiof_spi_priv *p = 454 spi_controller_get_devdata(spi->controller); 455 u32 clr, set, tmp; 456 int ret; 457 458 if (spi_get_csgpiod(spi, 0) || spi_controller_is_target(p->ctlr)) 459 return 0; 460 461 if (p->native_cs_inited && 462 (p->native_cs_high == !!(spi->mode & SPI_CS_HIGH))) 463 return 0; 464 465 /* Configure native chip select mode/polarity early */ 466 clr = SIMDR1_SYNCMD; 467 set = FIELD_PREP(SIMDR1_SYNCMD, SIMDR1_SYNCMD_SPI); 468 if (spi->mode & SPI_CS_HIGH) 469 clr |= SIMDR1_SYNCAC; 470 else 471 set |= SIMDR1_SYNCAC; 472 ret = pm_runtime_resume_and_get(&p->pdev->dev); 473 if (ret < 0) 474 return ret; 475 tmp = sh_msiof_read(p, SITMDR1) & ~clr; 476 sh_msiof_write(p, SITMDR1, tmp | set | SIMDR1_TRMD | SITMDR1_PCON); 477 tmp = sh_msiof_read(p, SIRMDR1) & ~clr; 478 sh_msiof_write(p, SIRMDR1, tmp | set); 479 pm_runtime_put(&p->pdev->dev); 480 p->native_cs_high = spi->mode & SPI_CS_HIGH; 481 p->native_cs_inited = true; 482 return 0; 483 } 484 485 static int sh_msiof_prepare_message(struct spi_controller *ctlr, 486 struct spi_message *msg) 487 { 488 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr); 489 const struct spi_device *spi = msg->spi; 490 bool cs_high; 491 u32 ss; 492 493 /* Configure pins before asserting CS */ 494 if (spi_get_csgpiod(spi, 0)) { 495 ss = ctlr->unused_native_cs; 496 cs_high = p->native_cs_high; 497 } else { 498 ss = spi_get_chipselect(spi, 0); 499 cs_high = spi->mode & SPI_CS_HIGH; 500 } 501 sh_msiof_spi_set_pin_regs(p, ss, spi->mode & SPI_CPOL, 502 spi->mode & SPI_CPHA, spi->mode & SPI_3WIRE, 503 spi->mode & SPI_LSB_FIRST, cs_high); 504 return 0; 505 } 506 507 static int sh_msiof_spi_start(struct sh_msiof_spi_priv *p, void *rx_buf) 508 { 509 bool target = spi_controller_is_target(p->ctlr); 510 int ret = 0; 511 512 /* setup clock and rx/tx signals */ 513 if (!target) 514 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TSCKE); 515 if (rx_buf && !ret) 516 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_RXE); 517 if (!ret) 518 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TXE); 519 520 /* start by setting frame bit */ 521 if (!ret && !target) 522 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TFSE); 523 524 return ret; 525 } 526 527 static int sh_msiof_spi_stop(struct sh_msiof_spi_priv *p, void *rx_buf) 528 { 529 bool target = spi_controller_is_target(p->ctlr); 530 int ret = 0; 531 532 /* shut down frame, rx/tx and clock signals */ 533 if (!target) 534 ret = sh_msiof_modify_ctr_wait(p, SICTR_TFSE, 0); 535 if (!ret) 536 ret = sh_msiof_modify_ctr_wait(p, SICTR_TXE, 0); 537 if (rx_buf && !ret) 538 ret = sh_msiof_modify_ctr_wait(p, SICTR_RXE, 0); 539 if (!ret && !target) 540 ret = sh_msiof_modify_ctr_wait(p, SICTR_TSCKE, 0); 541 542 return ret; 543 } 544 545 static int sh_msiof_target_abort(struct spi_controller *ctlr) 546 { 547 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr); 548 549 p->target_aborted = true; 550 complete(&p->done); 551 complete(&p->done_txdma); 552 return 0; 553 } 554 555 static int sh_msiof_wait_for_completion(struct sh_msiof_spi_priv *p, 556 struct completion *x) 557 { 558 if (spi_controller_is_target(p->ctlr)) { 559 if (wait_for_completion_interruptible(x) || 560 p->target_aborted) { 561 dev_dbg(&p->pdev->dev, "interrupted\n"); 562 return -EINTR; 563 } 564 } else { 565 if (!wait_for_completion_timeout(x, HZ)) { 566 dev_err(&p->pdev->dev, "timeout\n"); 567 return -ETIMEDOUT; 568 } 569 } 570 571 return 0; 572 } 573 574 static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p, 575 void (*tx_fifo)(struct sh_msiof_spi_priv *, 576 const void *, unsigned int, 577 unsigned int), 578 void (*rx_fifo)(struct sh_msiof_spi_priv *, 579 void *, unsigned int, 580 unsigned int), 581 const void *tx_buf, void *rx_buf, 582 unsigned int words, unsigned int bits) 583 { 584 unsigned int fifo_shift; 585 int ret; 586 587 /* limit maximum word transfer to rx/tx fifo size */ 588 if (tx_buf) 589 words = min(words, p->tx_fifo_size); 590 if (rx_buf) 591 words = min(words, p->rx_fifo_size); 592 593 /* the fifo contents need shifting */ 594 fifo_shift = 32 - bits; 595 596 /* default FIFO watermarks for PIO */ 597 sh_msiof_write(p, SIFCTR, 0); 598 599 /* setup msiof transfer mode registers */ 600 sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words, 0); 601 sh_msiof_write(p, SIIER, SIIER_TEOFE | SIIER_REOFE); 602 603 /* write tx fifo */ 604 if (tx_buf) 605 tx_fifo(p, tx_buf, words, fifo_shift); 606 607 reinit_completion(&p->done); 608 p->target_aborted = false; 609 610 ret = sh_msiof_spi_start(p, rx_buf); 611 if (ret) { 612 dev_err(&p->pdev->dev, "failed to start hardware\n"); 613 goto stop_ier; 614 } 615 616 /* wait for tx fifo to be emptied / rx fifo to be filled */ 617 ret = sh_msiof_wait_for_completion(p, &p->done); 618 if (ret) 619 goto stop_reset; 620 621 /* read rx fifo */ 622 if (rx_buf) 623 rx_fifo(p, rx_buf, words, fifo_shift); 624 625 /* clear status bits */ 626 sh_msiof_reset_str(p); 627 628 ret = sh_msiof_spi_stop(p, rx_buf); 629 if (ret) { 630 dev_err(&p->pdev->dev, "failed to shut down hardware\n"); 631 return ret; 632 } 633 634 return words; 635 636 stop_reset: 637 sh_msiof_reset_str(p); 638 sh_msiof_spi_stop(p, rx_buf); 639 stop_ier: 640 sh_msiof_write(p, SIIER, 0); 641 return ret; 642 } 643 644 static void sh_msiof_dma_complete(void *arg) 645 { 646 complete(arg); 647 } 648 649 static int sh_msiof_dma_once(struct sh_msiof_spi_priv *p, const void *tx, 650 void *rx, unsigned int len, 651 unsigned int max_wdlen) 652 { 653 u32 ier_bits = 0; 654 struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL; 655 unsigned int words1, words2; 656 dma_cookie_t cookie; 657 int ret; 658 659 /* First prepare and submit the DMA request(s), as this may fail */ 660 if (rx) { 661 ier_bits |= SIIER_RDREQE | SIIER_RDMAE; 662 desc_rx = dmaengine_prep_slave_single(p->ctlr->dma_rx, 663 p->rx_dma_addr, len, DMA_DEV_TO_MEM, 664 DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 665 if (!desc_rx) 666 return -EAGAIN; 667 668 desc_rx->callback = sh_msiof_dma_complete; 669 desc_rx->callback_param = &p->done; 670 cookie = dmaengine_submit(desc_rx); 671 if (dma_submit_error(cookie)) 672 return cookie; 673 } 674 675 if (tx) { 676 ier_bits |= SIIER_TDREQE | SIIER_TDMAE; 677 dma_sync_single_for_device(dmaengine_get_dma_device(p->ctlr->dma_tx), 678 p->tx_dma_addr, len, DMA_TO_DEVICE); 679 desc_tx = dmaengine_prep_slave_single(p->ctlr->dma_tx, 680 p->tx_dma_addr, len, DMA_MEM_TO_DEV, 681 DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 682 if (!desc_tx) { 683 ret = -EAGAIN; 684 goto no_dma_tx; 685 } 686 687 desc_tx->callback = sh_msiof_dma_complete; 688 desc_tx->callback_param = &p->done_txdma; 689 cookie = dmaengine_submit(desc_tx); 690 if (dma_submit_error(cookie)) { 691 ret = cookie; 692 goto no_dma_tx; 693 } 694 } 695 696 /* 1 stage FIFO watermarks for DMA */ 697 sh_msiof_write(p, SIFCTR, 698 FIELD_PREP(SIFCTR_TFWM, SIFCTR_TFWM_1) | 699 FIELD_PREP(SIFCTR_RFWM, SIFCTR_RFWM_1)); 700 701 /* setup msiof transfer mode registers (32-bit words) */ 702 words1 = min(len / 4, max_wdlen); 703 words2 = len / 4 - words1; 704 sh_msiof_spi_set_mode_regs(p, tx, rx, 32, words1, words2); 705 706 sh_msiof_write(p, SIIER, ier_bits); 707 708 reinit_completion(&p->done); 709 if (tx) 710 reinit_completion(&p->done_txdma); 711 p->target_aborted = false; 712 713 /* Now start DMA */ 714 if (rx) 715 dma_async_issue_pending(p->ctlr->dma_rx); 716 if (tx) 717 dma_async_issue_pending(p->ctlr->dma_tx); 718 719 ret = sh_msiof_spi_start(p, rx); 720 if (ret) { 721 dev_err(&p->pdev->dev, "failed to start hardware\n"); 722 goto stop_dma; 723 } 724 725 if (tx) { 726 /* wait for tx DMA completion */ 727 ret = sh_msiof_wait_for_completion(p, &p->done_txdma); 728 if (ret) 729 goto stop_reset; 730 } 731 732 if (rx) { 733 /* wait for rx DMA completion */ 734 ret = sh_msiof_wait_for_completion(p, &p->done); 735 if (ret) 736 goto stop_reset; 737 738 sh_msiof_write(p, SIIER, 0); 739 } else { 740 /* wait for tx fifo to be emptied */ 741 sh_msiof_write(p, SIIER, SIIER_TEOFE); 742 ret = sh_msiof_wait_for_completion(p, &p->done); 743 if (ret) 744 goto stop_reset; 745 } 746 747 /* clear status bits */ 748 sh_msiof_reset_str(p); 749 750 ret = sh_msiof_spi_stop(p, rx); 751 if (ret) { 752 dev_err(&p->pdev->dev, "failed to shut down hardware\n"); 753 return ret; 754 } 755 756 if (rx) 757 dma_sync_single_for_cpu(dmaengine_get_dma_device(p->ctlr->dma_rx), 758 p->rx_dma_addr, len, DMA_FROM_DEVICE); 759 760 return 0; 761 762 stop_reset: 763 sh_msiof_reset_str(p); 764 sh_msiof_spi_stop(p, rx); 765 stop_dma: 766 if (tx) 767 dmaengine_terminate_sync(p->ctlr->dma_tx); 768 no_dma_tx: 769 if (rx) 770 dmaengine_terminate_sync(p->ctlr->dma_rx); 771 sh_msiof_write(p, SIIER, 0); 772 return ret; 773 } 774 775 static void copy_bswap32(u32 *dst, const u32 *src, unsigned int words) 776 { 777 /* src or dst can be unaligned, but not both */ 778 if ((unsigned long)src & 3) { 779 while (words--) { 780 *dst++ = swab32(get_unaligned(src)); 781 src++; 782 } 783 } else if ((unsigned long)dst & 3) { 784 while (words--) { 785 put_unaligned(swab32(*src++), dst); 786 dst++; 787 } 788 } else { 789 while (words--) 790 *dst++ = swab32(*src++); 791 } 792 } 793 794 static void copy_wswap32(u32 *dst, const u32 *src, unsigned int words) 795 { 796 /* src or dst can be unaligned, but not both */ 797 if ((unsigned long)src & 3) { 798 while (words--) { 799 *dst++ = swahw32(get_unaligned(src)); 800 src++; 801 } 802 } else if ((unsigned long)dst & 3) { 803 while (words--) { 804 put_unaligned(swahw32(*src++), dst); 805 dst++; 806 } 807 } else { 808 while (words--) 809 *dst++ = swahw32(*src++); 810 } 811 } 812 813 static void copy_plain32(u32 *dst, const u32 *src, unsigned int words) 814 { 815 memcpy(dst, src, words * 4); 816 } 817 818 static int sh_msiof_transfer_one(struct spi_controller *ctlr, 819 struct spi_device *spi, 820 struct spi_transfer *t) 821 { 822 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr); 823 unsigned int max_wdlen = FIELD_MAX(SIMDR2_WDLEN1) + 1; 824 void (*copy32)(u32 *, const u32 *, unsigned int); 825 void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, unsigned int, 826 unsigned int); 827 void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, unsigned int, 828 unsigned int); 829 const void *tx_buf = t->tx_buf; 830 void *rx_buf = t->rx_buf; 831 unsigned int len = t->len; 832 unsigned int bits = t->bits_per_word; 833 unsigned int bytes_per_word; 834 unsigned int words; 835 int n; 836 bool swab; 837 int ret; 838 839 /* reset registers */ 840 ret = sh_msiof_spi_reset_regs(p); 841 if (ret) 842 return ret; 843 844 /* setup clocks (clock already enabled in chipselect()) */ 845 if (!spi_controller_is_target(p->ctlr)) 846 sh_msiof_spi_set_clk_regs(p, t); 847 848 if (tx_buf) 849 max_wdlen = min(max_wdlen, p->tx_fifo_size); 850 if (rx_buf) 851 max_wdlen = min(max_wdlen, p->rx_fifo_size); 852 853 while (ctlr->dma_tx && len > 15) { 854 /* 855 * DMA supports 32-bit words only, hence pack 8-bit and 16-bit 856 * words, with byte resp. word swapping. 857 */ 858 unsigned int l = min(round_down(len, 4), 2 * max_wdlen * 4); 859 860 if (bits <= 8) { 861 copy32 = copy_bswap32; 862 } else if (bits <= 16) { 863 copy32 = copy_wswap32; 864 } else { 865 copy32 = copy_plain32; 866 } 867 868 if (tx_buf) 869 copy32(p->tx_dma_page, tx_buf, l / 4); 870 871 ret = sh_msiof_dma_once(p, tx_buf, rx_buf, l, max_wdlen); 872 if (ret == -EAGAIN) { 873 dev_warn_once(&p->pdev->dev, 874 "DMA not available, falling back to PIO\n"); 875 break; 876 } 877 if (ret) 878 return ret; 879 880 if (rx_buf) { 881 copy32(rx_buf, p->rx_dma_page, l / 4); 882 rx_buf += l; 883 } 884 if (tx_buf) 885 tx_buf += l; 886 887 len -= l; 888 if (!len) 889 return 0; 890 } 891 892 if (bits <= 8 && len > 15) { 893 bits = 32; 894 swab = true; 895 } else { 896 swab = false; 897 } 898 899 /* setup bytes per word and fifo read/write functions */ 900 if (bits <= 8) { 901 bytes_per_word = 1; 902 tx_fifo = sh_msiof_spi_write_fifo_8; 903 rx_fifo = sh_msiof_spi_read_fifo_8; 904 } else if (bits <= 16) { 905 bytes_per_word = 2; 906 if ((unsigned long)tx_buf & 0x01) 907 tx_fifo = sh_msiof_spi_write_fifo_16u; 908 else 909 tx_fifo = sh_msiof_spi_write_fifo_16; 910 911 if ((unsigned long)rx_buf & 0x01) 912 rx_fifo = sh_msiof_spi_read_fifo_16u; 913 else 914 rx_fifo = sh_msiof_spi_read_fifo_16; 915 } else if (swab) { 916 bytes_per_word = 4; 917 if ((unsigned long)tx_buf & 0x03) 918 tx_fifo = sh_msiof_spi_write_fifo_s32u; 919 else 920 tx_fifo = sh_msiof_spi_write_fifo_s32; 921 922 if ((unsigned long)rx_buf & 0x03) 923 rx_fifo = sh_msiof_spi_read_fifo_s32u; 924 else 925 rx_fifo = sh_msiof_spi_read_fifo_s32; 926 } else { 927 bytes_per_word = 4; 928 if ((unsigned long)tx_buf & 0x03) 929 tx_fifo = sh_msiof_spi_write_fifo_32u; 930 else 931 tx_fifo = sh_msiof_spi_write_fifo_32; 932 933 if ((unsigned long)rx_buf & 0x03) 934 rx_fifo = sh_msiof_spi_read_fifo_32u; 935 else 936 rx_fifo = sh_msiof_spi_read_fifo_32; 937 } 938 939 /* transfer in fifo sized chunks */ 940 words = len / bytes_per_word; 941 942 while (words > 0) { 943 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, tx_buf, rx_buf, 944 words, bits); 945 if (n < 0) 946 return n; 947 948 if (tx_buf) 949 tx_buf += n * bytes_per_word; 950 if (rx_buf) 951 rx_buf += n * bytes_per_word; 952 words -= n; 953 954 if (words == 0 && (len % bytes_per_word)) { 955 words = len % bytes_per_word; 956 bits = t->bits_per_word; 957 bytes_per_word = 1; 958 tx_fifo = sh_msiof_spi_write_fifo_8; 959 rx_fifo = sh_msiof_spi_read_fifo_8; 960 } 961 } 962 963 return 0; 964 } 965 966 static const struct sh_msiof_chipdata sh_data = { 967 .bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32), 968 .tx_fifo_size = 64, 969 .rx_fifo_size = 64, 970 .ctlr_flags = 0, 971 .min_div_pow = 0, 972 }; 973 974 static const struct sh_msiof_chipdata rcar_gen2_data = { 975 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | 976 SPI_BPW_MASK(24) | SPI_BPW_MASK(32), 977 .tx_fifo_size = 64, 978 .rx_fifo_size = 128, 979 .ctlr_flags = SPI_CONTROLLER_MUST_TX, 980 .min_div_pow = 0, 981 }; 982 983 static const struct sh_msiof_chipdata rcar_gen3_data = { 984 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | 985 SPI_BPW_MASK(24) | SPI_BPW_MASK(32), 986 .tx_fifo_size = 64, 987 .rx_fifo_size = 256, 988 .ctlr_flags = SPI_CONTROLLER_MUST_TX, 989 .min_div_pow = 1, 990 }; 991 992 static const struct sh_msiof_chipdata rcar_gen4_data = { 993 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | 994 SPI_BPW_MASK(24) | SPI_BPW_MASK(32), 995 .tx_fifo_size = 256, 996 .rx_fifo_size = 256, 997 .ctlr_flags = SPI_CONTROLLER_MUST_TX, 998 .min_div_pow = 1, 999 }; 1000 1001 static const struct sh_msiof_chipdata rcar_r8a7795_data = { 1002 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | 1003 SPI_BPW_MASK(24) | SPI_BPW_MASK(32), 1004 .tx_fifo_size = 64, 1005 .rx_fifo_size = 256, 1006 .ctlr_flags = SPI_CONTROLLER_MUST_TX, 1007 .min_div_pow = 1, 1008 .flags = SH_MSIOF_FLAG_FIXED_DTDL_200, 1009 }; 1010 1011 static const struct of_device_id sh_msiof_match[] __maybe_unused = { 1012 { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data }, 1013 { .compatible = "renesas,rcar-gen2-msiof", .data = &rcar_gen2_data }, 1014 { .compatible = "renesas,msiof-r8a7795", .data = &rcar_r8a7795_data }, 1015 { .compatible = "renesas,rcar-gen3-msiof", .data = &rcar_gen3_data }, 1016 { .compatible = "renesas,msiof-r8a779a0", .data = &rcar_gen3_data }, 1017 { .compatible = "renesas,msiof-r8a779f0", .data = &rcar_gen3_data }, 1018 { .compatible = "renesas,rcar-gen4-msiof", .data = &rcar_gen4_data }, 1019 { .compatible = "renesas,rcar-gen5-msiof", .data = &rcar_gen4_data }, 1020 { .compatible = "renesas,sh-msiof", .data = &sh_data }, /* Deprecated */ 1021 { /* sentinel */ } 1022 }; 1023 MODULE_DEVICE_TABLE(of, sh_msiof_match); 1024 1025 #ifdef CONFIG_OF 1026 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) 1027 { 1028 struct sh_msiof_spi_info *info; 1029 struct device_node *np = dev->of_node; 1030 u32 num_cs = 1; 1031 1032 info = devm_kzalloc_obj(dev, struct sh_msiof_spi_info); 1033 if (!info) 1034 return NULL; 1035 1036 info->mode = of_property_read_bool(np, "spi-slave") ? MSIOF_SPI_TARGET 1037 : MSIOF_SPI_HOST; 1038 1039 /* Parse the MSIOF properties */ 1040 if (info->mode == MSIOF_SPI_HOST) 1041 of_property_read_u32(np, "num-cs", &num_cs); 1042 of_property_read_u32(np, "renesas,tx-fifo-size", 1043 &info->tx_fifo_override); 1044 of_property_read_u32(np, "renesas,rx-fifo-size", 1045 &info->rx_fifo_override); 1046 of_property_read_u32(np, "renesas,dtdl", &info->dtdl); 1047 of_property_read_u32(np, "renesas,syncdl", &info->syncdl); 1048 1049 info->num_chipselect = num_cs; 1050 1051 return info; 1052 } 1053 #else 1054 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) 1055 { 1056 return NULL; 1057 } 1058 #endif 1059 1060 static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev, 1061 enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr) 1062 { 1063 dma_cap_mask_t mask; 1064 struct dma_chan *chan; 1065 struct dma_slave_config cfg; 1066 int ret; 1067 1068 dma_cap_zero(mask); 1069 dma_cap_set(DMA_SLAVE, mask); 1070 1071 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, 1072 (void *)(unsigned long)id, dev, 1073 dir == DMA_MEM_TO_DEV ? "tx" : "rx"); 1074 if (!chan) { 1075 dev_warn(dev, "dma_request_slave_channel_compat failed\n"); 1076 return NULL; 1077 } 1078 1079 memset(&cfg, 0, sizeof(cfg)); 1080 cfg.direction = dir; 1081 if (dir == DMA_MEM_TO_DEV) { 1082 cfg.dst_addr = port_addr; 1083 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1084 } else { 1085 cfg.src_addr = port_addr; 1086 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1087 } 1088 1089 ret = dmaengine_slave_config(chan, &cfg); 1090 if (ret) { 1091 dev_warn(dev, "dmaengine_slave_config failed %d\n", ret); 1092 dma_release_channel(chan); 1093 return NULL; 1094 } 1095 1096 return chan; 1097 } 1098 1099 static int sh_msiof_request_dma(struct sh_msiof_spi_priv *p) 1100 { 1101 struct platform_device *pdev = p->pdev; 1102 struct device *dev = &pdev->dev; 1103 const struct sh_msiof_spi_info *info = p->info; 1104 unsigned int dma_tx_id, dma_rx_id; 1105 const struct resource *res; 1106 struct spi_controller *ctlr; 1107 struct device *tx_dev, *rx_dev; 1108 1109 if (dev->of_node) { 1110 /* In the OF case we will get the slave IDs from the DT */ 1111 dma_tx_id = 0; 1112 dma_rx_id = 0; 1113 } else if (info && info->dma_tx_id && info->dma_rx_id) { 1114 dma_tx_id = info->dma_tx_id; 1115 dma_rx_id = info->dma_rx_id; 1116 } else { 1117 /* The driver assumes no error */ 1118 return 0; 1119 } 1120 1121 /* The DMA engine uses the second register set, if present */ 1122 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1123 if (!res) 1124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1125 1126 ctlr = p->ctlr; 1127 ctlr->dma_tx = sh_msiof_request_dma_chan(dev, DMA_MEM_TO_DEV, 1128 dma_tx_id, res->start + SITFDR); 1129 if (!ctlr->dma_tx) 1130 return -ENODEV; 1131 1132 ctlr->dma_rx = sh_msiof_request_dma_chan(dev, DMA_DEV_TO_MEM, 1133 dma_rx_id, res->start + SIRFDR); 1134 if (!ctlr->dma_rx) 1135 goto free_tx_chan; 1136 1137 p->tx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); 1138 if (!p->tx_dma_page) 1139 goto free_rx_chan; 1140 1141 p->rx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); 1142 if (!p->rx_dma_page) 1143 goto free_tx_page; 1144 1145 tx_dev = dmaengine_get_dma_device(ctlr->dma_tx); 1146 p->tx_dma_addr = dma_map_single(tx_dev, p->tx_dma_page, PAGE_SIZE, 1147 DMA_TO_DEVICE); 1148 if (dma_mapping_error(tx_dev, p->tx_dma_addr)) 1149 goto free_rx_page; 1150 1151 rx_dev = dmaengine_get_dma_device(ctlr->dma_rx); 1152 p->rx_dma_addr = dma_map_single(rx_dev, p->rx_dma_page, PAGE_SIZE, 1153 DMA_FROM_DEVICE); 1154 if (dma_mapping_error(rx_dev, p->rx_dma_addr)) 1155 goto unmap_tx_page; 1156 1157 dev_info(dev, "DMA available"); 1158 return 0; 1159 1160 unmap_tx_page: 1161 dma_unmap_single(tx_dev, p->tx_dma_addr, PAGE_SIZE, DMA_TO_DEVICE); 1162 free_rx_page: 1163 free_page((unsigned long)p->rx_dma_page); 1164 free_tx_page: 1165 free_page((unsigned long)p->tx_dma_page); 1166 free_rx_chan: 1167 dma_release_channel(ctlr->dma_rx); 1168 free_tx_chan: 1169 dma_release_channel(ctlr->dma_tx); 1170 ctlr->dma_tx = NULL; 1171 return -ENODEV; 1172 } 1173 1174 static void sh_msiof_release_dma(struct sh_msiof_spi_priv *p) 1175 { 1176 struct spi_controller *ctlr = p->ctlr; 1177 1178 if (!ctlr->dma_tx) 1179 return; 1180 1181 dma_unmap_single(dmaengine_get_dma_device(ctlr->dma_rx), p->rx_dma_addr, PAGE_SIZE, 1182 DMA_FROM_DEVICE); 1183 dma_unmap_single(dmaengine_get_dma_device(ctlr->dma_tx), p->tx_dma_addr, PAGE_SIZE, 1184 DMA_TO_DEVICE); 1185 free_page((unsigned long)p->rx_dma_page); 1186 free_page((unsigned long)p->tx_dma_page); 1187 dma_release_channel(ctlr->dma_rx); 1188 dma_release_channel(ctlr->dma_tx); 1189 } 1190 1191 static int sh_msiof_spi_probe(struct platform_device *pdev) 1192 { 1193 struct spi_controller *ctlr; 1194 const struct sh_msiof_chipdata *chipdata; 1195 struct sh_msiof_spi_info *info; 1196 struct sh_msiof_spi_priv *p; 1197 struct device *dev = &pdev->dev; 1198 unsigned long clksrc; 1199 int i; 1200 int ret; 1201 1202 /* Check whether MSIOF is used as I2S mode or SPI mode by checking "port" node */ 1203 struct device_node *port __free(device_node) = of_graph_get_next_port(dev->of_node, NULL); 1204 if (port) /* It was MSIOF-I2S */ 1205 return -ENODEV; 1206 1207 chipdata = of_device_get_match_data(dev); 1208 if (chipdata) { 1209 info = sh_msiof_spi_parse_dt(dev); 1210 } else { 1211 chipdata = (const void *)pdev->id_entry->driver_data; 1212 info = dev_get_platdata(dev); 1213 } 1214 1215 if (!info) { 1216 dev_err(dev, "failed to obtain device info\n"); 1217 return -ENXIO; 1218 } 1219 1220 if (chipdata->flags & SH_MSIOF_FLAG_FIXED_DTDL_200) 1221 info->dtdl = 200; 1222 1223 if (info->mode == MSIOF_SPI_TARGET) 1224 ctlr = devm_spi_alloc_target(dev, sizeof(struct sh_msiof_spi_priv)); 1225 else 1226 ctlr = devm_spi_alloc_host(dev, sizeof(struct sh_msiof_spi_priv)); 1227 if (ctlr == NULL) 1228 return -ENOMEM; 1229 1230 p = spi_controller_get_devdata(ctlr); 1231 1232 platform_set_drvdata(pdev, p); 1233 p->ctlr = ctlr; 1234 p->info = info; 1235 p->min_div_pow = chipdata->min_div_pow; 1236 1237 init_completion(&p->done); 1238 init_completion(&p->done_txdma); 1239 1240 p->clk = devm_clk_get(dev, NULL); 1241 if (IS_ERR(p->clk)) { 1242 dev_err(dev, "cannot get clock\n"); 1243 return PTR_ERR(p->clk); 1244 } 1245 1246 i = platform_get_irq(pdev, 0); 1247 if (i < 0) 1248 return i; 1249 1250 p->mapbase = devm_platform_ioremap_resource(pdev, 0); 1251 if (IS_ERR(p->mapbase)) 1252 return PTR_ERR(p->mapbase); 1253 1254 ret = devm_request_irq(dev, i, sh_msiof_spi_irq, 0, dev_name(dev), p); 1255 if (ret) { 1256 dev_err(dev, "unable to request irq\n"); 1257 return ret; 1258 } 1259 1260 p->pdev = pdev; 1261 pm_runtime_enable(dev); 1262 1263 /* Platform data may override FIFO sizes */ 1264 p->tx_fifo_size = chipdata->tx_fifo_size; 1265 p->rx_fifo_size = chipdata->rx_fifo_size; 1266 if (p->info->tx_fifo_override) 1267 p->tx_fifo_size = p->info->tx_fifo_override; 1268 if (p->info->rx_fifo_override) 1269 p->rx_fifo_size = p->info->rx_fifo_override; 1270 1271 /* init controller code */ 1272 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; 1273 ctlr->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE; 1274 clksrc = clk_get_rate(p->clk); 1275 ctlr->min_speed_hz = DIV_ROUND_UP(clksrc, 1024); 1276 ctlr->max_speed_hz = DIV_ROUND_UP(clksrc, 1 << p->min_div_pow); 1277 ctlr->flags = chipdata->ctlr_flags; 1278 ctlr->bus_num = pdev->id; 1279 ctlr->num_chipselect = p->info->num_chipselect; 1280 ctlr->setup = sh_msiof_spi_setup; 1281 ctlr->prepare_message = sh_msiof_prepare_message; 1282 ctlr->target_abort = sh_msiof_target_abort; 1283 ctlr->bits_per_word_mask = chipdata->bits_per_word_mask; 1284 ctlr->auto_runtime_pm = true; 1285 ctlr->transfer_one = sh_msiof_transfer_one; 1286 ctlr->use_gpio_descriptors = true; 1287 ctlr->max_native_cs = MAX_SS; 1288 1289 ret = sh_msiof_request_dma(p); 1290 if (ret < 0) 1291 dev_warn(dev, "DMA not available, using PIO\n"); 1292 1293 ret = spi_register_controller(ctlr); 1294 if (ret < 0) { 1295 dev_err(dev, "failed to register controller\n"); 1296 goto err2; 1297 } 1298 1299 return 0; 1300 1301 err2: 1302 sh_msiof_release_dma(p); 1303 pm_runtime_disable(dev); 1304 1305 return ret; 1306 } 1307 1308 static void sh_msiof_spi_remove(struct platform_device *pdev) 1309 { 1310 struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev); 1311 1312 spi_unregister_controller(p->ctlr); 1313 1314 sh_msiof_release_dma(p); 1315 pm_runtime_disable(&pdev->dev); 1316 } 1317 1318 static const struct platform_device_id spi_driver_ids[] = { 1319 { .name = "spi_sh_msiof", .driver_data = (kernel_ulong_t)&sh_data }, 1320 { } 1321 }; 1322 MODULE_DEVICE_TABLE(platform, spi_driver_ids); 1323 1324 static int sh_msiof_spi_suspend(struct device *dev) 1325 { 1326 struct sh_msiof_spi_priv *p = dev_get_drvdata(dev); 1327 1328 return spi_controller_suspend(p->ctlr); 1329 } 1330 1331 static int sh_msiof_spi_resume(struct device *dev) 1332 { 1333 struct sh_msiof_spi_priv *p = dev_get_drvdata(dev); 1334 1335 return spi_controller_resume(p->ctlr); 1336 } 1337 1338 static DEFINE_SIMPLE_DEV_PM_OPS(sh_msiof_spi_pm_ops, sh_msiof_spi_suspend, 1339 sh_msiof_spi_resume); 1340 1341 static struct platform_driver sh_msiof_spi_drv = { 1342 .probe = sh_msiof_spi_probe, 1343 .remove = sh_msiof_spi_remove, 1344 .id_table = spi_driver_ids, 1345 .driver = { 1346 .name = "spi_sh_msiof", 1347 .pm = pm_sleep_ptr(&sh_msiof_spi_pm_ops), 1348 .of_match_table = of_match_ptr(sh_msiof_match), 1349 }, 1350 }; 1351 module_platform_driver(sh_msiof_spi_drv); 1352 1353 MODULE_DESCRIPTION("SuperH MSIOF SPI Controller Interface Driver"); 1354 MODULE_AUTHOR("Magnus Damm"); 1355 MODULE_LICENSE("GPL v2"); 1356