1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2018 Exceet Electronics GmbH 4 * Copyright (C) 2018 Bootlin 5 * 6 * Author: Boris Brezillon <boris.brezillon@bootlin.com> 7 */ 8 #include <linux/dmaengine.h> 9 #include <linux/iopoll.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/spi/spi.h> 12 #include <linux/spi/spi-mem.h> 13 #include <linux/sched/task_stack.h> 14 15 #define CREATE_TRACE_POINTS 16 #include <trace/events/spi-mem.h> 17 18 #include "internals.h" 19 20 #define SPI_MEM_MAX_BUSWIDTH 8 21 22 /** 23 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a 24 * memory operation 25 * @ctlr: the SPI controller requesting this dma_map() 26 * @op: the memory operation containing the buffer to map 27 * @sgt: a pointer to a non-initialized sg_table that will be filled by this 28 * function 29 * 30 * Some controllers might want to do DMA on the data buffer embedded in @op. 31 * This helper prepares everything for you and provides a ready-to-use 32 * sg_table. This function is not intended to be called from spi drivers. 33 * Only SPI controller drivers should use it. 34 * Note that the caller must ensure the memory region pointed by 35 * op->data.buf.{in,out} is DMA-able before calling this function. 36 * 37 * Return: 0 in case of success, a negative error code otherwise. 38 */ 39 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 40 const struct spi_mem_op *op, 41 struct sg_table *sgt) 42 { 43 struct device *dmadev; 44 45 if (!op->data.nbytes) 46 return -EINVAL; 47 48 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) 49 dmadev = ctlr->dma_tx->device->dev; 50 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) 51 dmadev = ctlr->dma_rx->device->dev; 52 else 53 dmadev = ctlr->dev.parent; 54 55 if (!dmadev) 56 return -EINVAL; 57 58 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes, 59 op->data.dir == SPI_MEM_DATA_IN ? 60 DMA_FROM_DEVICE : DMA_TO_DEVICE); 61 } 62 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data); 63 64 /** 65 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a 66 * memory operation 67 * @ctlr: the SPI controller requesting this dma_unmap() 68 * @op: the memory operation containing the buffer to unmap 69 * @sgt: a pointer to an sg_table previously initialized by 70 * spi_controller_dma_map_mem_op_data() 71 * 72 * Some controllers might want to do DMA on the data buffer embedded in @op. 73 * This helper prepares things so that the CPU can access the 74 * op->data.buf.{in,out} buffer again. 75 * 76 * This function is not intended to be called from SPI drivers. Only SPI 77 * controller drivers should use it. 78 * 79 * This function should be called after the DMA operation has finished and is 80 * only valid if the previous spi_controller_dma_map_mem_op_data() call 81 * returned 0. 82 * 83 * Return: 0 in case of success, a negative error code otherwise. 84 */ 85 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 86 const struct spi_mem_op *op, 87 struct sg_table *sgt) 88 { 89 struct device *dmadev; 90 91 if (!op->data.nbytes) 92 return; 93 94 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) 95 dmadev = ctlr->dma_tx->device->dev; 96 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) 97 dmadev = ctlr->dma_rx->device->dev; 98 else 99 dmadev = ctlr->dev.parent; 100 101 spi_unmap_buf(ctlr, dmadev, sgt, 102 op->data.dir == SPI_MEM_DATA_IN ? 103 DMA_FROM_DEVICE : DMA_TO_DEVICE); 104 } 105 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data); 106 107 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx) 108 { 109 u32 mode = mem->spi->mode; 110 111 switch (buswidth) { 112 case 1: 113 return 0; 114 115 case 2: 116 if ((tx && 117 (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) || 118 (!tx && 119 (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))) 120 return 0; 121 122 break; 123 124 case 4: 125 if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) || 126 (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL)))) 127 return 0; 128 129 break; 130 131 case 8: 132 if ((tx && (mode & SPI_TX_OCTAL)) || 133 (!tx && (mode & SPI_RX_OCTAL))) 134 return 0; 135 136 break; 137 138 default: 139 break; 140 } 141 142 return -ENOTSUPP; 143 } 144 145 static bool spi_mem_check_buswidth(struct spi_mem *mem, 146 const struct spi_mem_op *op) 147 { 148 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true)) 149 return false; 150 151 if (op->addr.nbytes && 152 spi_check_buswidth_req(mem, op->addr.buswidth, true)) 153 return false; 154 155 if (op->dummy.nbytes && 156 spi_check_buswidth_req(mem, op->dummy.buswidth, true)) 157 return false; 158 159 if (op->data.dir != SPI_MEM_NO_DATA && 160 spi_check_buswidth_req(mem, op->data.buswidth, 161 op->data.dir == SPI_MEM_DATA_OUT)) 162 return false; 163 164 return true; 165 } 166 167 bool spi_mem_default_supports_op(struct spi_mem *mem, 168 const struct spi_mem_op *op) 169 { 170 struct spi_controller *ctlr = mem->spi->controller; 171 bool op_is_dtr = 172 op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr; 173 174 if (op_is_dtr) { 175 if (!spi_mem_controller_is_capable(ctlr, dtr)) 176 return false; 177 178 if (op->data.swap16 && !spi_mem_controller_is_capable(ctlr, swap16)) 179 return false; 180 181 /* Extra 8D-8D-8D limitations */ 182 if (op->cmd.dtr && op->cmd.buswidth == 8) { 183 if (op->cmd.nbytes != 2) 184 return false; 185 186 if ((op->addr.nbytes % 2) || 187 (op->dummy.nbytes % 2) || 188 (op->data.nbytes % 2)) { 189 dev_err(&ctlr->dev, 190 "Even byte numbers not allowed in octal DTR operations\n"); 191 return false; 192 } 193 } 194 } else { 195 if (op->cmd.nbytes != 1) 196 return false; 197 } 198 199 if (op->data.ecc) { 200 if (!spi_mem_controller_is_capable(ctlr, ecc)) 201 return false; 202 } 203 204 if (op->max_freq && mem->spi->controller->min_speed_hz && 205 op->max_freq < mem->spi->controller->min_speed_hz) 206 return false; 207 208 if (op->max_freq && 209 op->max_freq < mem->spi->max_speed_hz) { 210 if (!spi_mem_controller_is_capable(ctlr, per_op_freq)) 211 return false; 212 } 213 214 return spi_mem_check_buswidth(mem, op); 215 } 216 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op); 217 218 static bool spi_mem_buswidth_is_valid(u8 buswidth) 219 { 220 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH) 221 return false; 222 223 return true; 224 } 225 226 static int spi_mem_check_op(const struct spi_mem_op *op) 227 { 228 if (!op->cmd.buswidth || !op->cmd.nbytes) 229 return -EINVAL; 230 231 if ((op->addr.nbytes && !op->addr.buswidth) || 232 (op->dummy.nbytes && !op->dummy.buswidth) || 233 (op->data.nbytes && !op->data.buswidth)) 234 return -EINVAL; 235 236 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) || 237 !spi_mem_buswidth_is_valid(op->addr.buswidth) || 238 !spi_mem_buswidth_is_valid(op->dummy.buswidth) || 239 !spi_mem_buswidth_is_valid(op->data.buswidth)) 240 return -EINVAL; 241 242 /* Buffers must be DMA-able. */ 243 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_IN && 244 object_is_on_stack(op->data.buf.in))) 245 return -EINVAL; 246 247 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_OUT && 248 object_is_on_stack(op->data.buf.out))) 249 return -EINVAL; 250 251 return 0; 252 } 253 254 static bool spi_mem_internal_supports_op(struct spi_mem *mem, 255 const struct spi_mem_op *op) 256 { 257 struct spi_controller *ctlr = mem->spi->controller; 258 259 if (ctlr->mem_ops && ctlr->mem_ops->supports_op) 260 return ctlr->mem_ops->supports_op(mem, op); 261 262 return spi_mem_default_supports_op(mem, op); 263 } 264 265 /** 266 * spi_mem_supports_op() - Check if a memory device and the controller it is 267 * connected to support a specific memory operation 268 * @mem: the SPI memory 269 * @op: the memory operation to check 270 * 271 * Some controllers are only supporting Single or Dual IOs, others might only 272 * support specific opcodes, or it can even be that the controller and device 273 * both support Quad IOs but the hardware prevents you from using it because 274 * only 2 IO lines are connected. 275 * 276 * This function checks whether a specific operation is supported. 277 * 278 * Return: true if @op is supported, false otherwise. 279 */ 280 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) 281 { 282 struct spi_mem_op eval_op = *op; 283 284 /* 285 * Work on a local copy; this is a pure capability check and must 286 * not modify the caller's op. Stored templates with max_freq == 0 287 * must remain unset so their frequency is always re-capped to the 288 * current device maximum at execution time. 289 */ 290 spi_mem_adjust_op_freq(mem, &eval_op); 291 292 if (spi_mem_check_op(&eval_op)) 293 return false; 294 295 return spi_mem_internal_supports_op(mem, &eval_op); 296 } 297 EXPORT_SYMBOL_GPL(spi_mem_supports_op); 298 299 static int spi_mem_access_start(struct spi_mem *mem) 300 { 301 struct spi_controller *ctlr = mem->spi->controller; 302 303 /* 304 * Flush the message queue before executing our SPI memory 305 * operation to prevent preemption of regular SPI transfers. 306 */ 307 spi_flush_queue(ctlr); 308 309 if (ctlr->auto_runtime_pm) { 310 int ret; 311 312 ret = pm_runtime_resume_and_get(ctlr->dev.parent); 313 if (ret < 0) { 314 dev_err(&ctlr->dev, "Failed to power device: %d\n", 315 ret); 316 return ret; 317 } 318 } 319 320 mutex_lock(&ctlr->bus_lock_mutex); 321 mutex_lock(&ctlr->io_mutex); 322 323 return 0; 324 } 325 326 static void spi_mem_access_end(struct spi_mem *mem) 327 { 328 struct spi_controller *ctlr = mem->spi->controller; 329 330 mutex_unlock(&ctlr->io_mutex); 331 mutex_unlock(&ctlr->bus_lock_mutex); 332 333 if (ctlr->auto_runtime_pm) 334 pm_runtime_put(ctlr->dev.parent); 335 } 336 337 static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats, 338 const struct spi_mem_op *op, int exec_op_ret) 339 { 340 struct spi_statistics *stats; 341 u64 len, l2len; 342 343 get_cpu(); 344 stats = this_cpu_ptr(pcpu_stats); 345 u64_stats_update_begin(&stats->syncp); 346 347 /* 348 * We do not have the concept of messages or transfers. Let's consider 349 * that one operation is equivalent to one message and one transfer. 350 */ 351 u64_stats_inc(&stats->messages); 352 u64_stats_inc(&stats->transfers); 353 354 /* Use the sum of all lengths as bytes count and histogram value. */ 355 len = op->cmd.nbytes + op->addr.nbytes; 356 len += op->dummy.nbytes + op->data.nbytes; 357 u64_stats_add(&stats->bytes, len); 358 l2len = min(fls(len), SPI_STATISTICS_HISTO_SIZE) - 1; 359 u64_stats_inc(&stats->transfer_bytes_histo[l2len]); 360 361 /* Only account for data bytes as transferred bytes. */ 362 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) 363 u64_stats_add(&stats->bytes_tx, op->data.nbytes); 364 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) 365 u64_stats_add(&stats->bytes_rx, op->data.nbytes); 366 367 /* 368 * A timeout is not an error, following the same behavior as 369 * spi_transfer_one_message(). 370 */ 371 if (exec_op_ret == -ETIMEDOUT) 372 u64_stats_inc(&stats->timedout); 373 else if (exec_op_ret) 374 u64_stats_inc(&stats->errors); 375 376 u64_stats_update_end(&stats->syncp); 377 put_cpu(); 378 } 379 380 /** 381 * spi_mem_exec_op() - Execute a memory operation 382 * @mem: the SPI memory 383 * @op: the memory operation to execute 384 * 385 * Executes a memory operation. 386 * 387 * This function first checks that @op is supported and then tries to execute 388 * it. 389 * 390 * Return: 0 in case of success, a negative error code otherwise. 391 */ 392 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) 393 { 394 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0; 395 struct spi_controller *ctlr = mem->spi->controller; 396 struct spi_transfer xfers[4] = { }; 397 struct spi_message msg; 398 u8 *tmpbuf; 399 int ret; 400 401 /* Make sure the operation frequency is correct before going futher */ 402 spi_mem_adjust_op_freq(mem, (struct spi_mem_op *)op); 403 404 dev_vdbg(&mem->spi->dev, "[cmd: 0x%02x][%dB addr: %#8llx][%2dB dummy][%4dB data %s] %d%c-%d%c-%d%c-%d%c @ %uHz\n", 405 op->cmd.opcode, 406 op->addr.nbytes, (op->addr.nbytes ? op->addr.val : 0), 407 op->dummy.nbytes, 408 op->data.nbytes, (op->data.nbytes ? (op->data.dir == SPI_MEM_DATA_IN ? " read" : "write") : " "), 409 op->cmd.buswidth, op->cmd.dtr ? 'D' : 'S', 410 op->addr.buswidth, op->addr.dtr ? 'D' : 'S', 411 op->dummy.buswidth, op->dummy.dtr ? 'D' : 'S', 412 op->data.buswidth, op->data.dtr ? 'D' : 'S', 413 op->max_freq ? op->max_freq : mem->spi->max_speed_hz); 414 415 ret = spi_mem_check_op(op); 416 if (ret) 417 return ret; 418 419 if (!spi_mem_internal_supports_op(mem, op)) 420 return -EOPNOTSUPP; 421 422 if (ctlr->mem_ops && ctlr->mem_ops->exec_op && !spi_get_csgpiod(mem->spi, 0)) { 423 ret = spi_mem_access_start(mem); 424 if (ret) 425 return ret; 426 427 trace_spi_mem_start_op(mem, op); 428 ret = ctlr->mem_ops->exec_op(mem, op); 429 trace_spi_mem_stop_op(mem, op); 430 431 spi_mem_access_end(mem); 432 433 /* 434 * Some controllers only optimize specific paths (typically the 435 * read path) and expect the core to use the regular SPI 436 * interface in other cases. 437 */ 438 if (!ret || (ret != -ENOTSUPP && ret != -EOPNOTSUPP)) { 439 spi_mem_add_op_stats(ctlr->pcpu_statistics, op, ret); 440 spi_mem_add_op_stats(mem->spi->pcpu_statistics, op, ret); 441 442 return ret; 443 } 444 } 445 446 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; 447 448 /* 449 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so 450 * we're guaranteed that this buffer is DMA-able, as required by the 451 * SPI layer. 452 */ 453 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA); 454 if (!tmpbuf) 455 return -ENOMEM; 456 457 spi_message_init(&msg); 458 459 tmpbuf[0] = op->cmd.opcode; 460 xfers[xferpos].tx_buf = tmpbuf; 461 xfers[xferpos].len = op->cmd.nbytes; 462 xfers[xferpos].tx_nbits = op->cmd.buswidth; 463 xfers[xferpos].speed_hz = op->max_freq; 464 spi_message_add_tail(&xfers[xferpos], &msg); 465 xferpos++; 466 totalxferlen++; 467 468 if (op->addr.nbytes) { 469 int i; 470 471 for (i = 0; i < op->addr.nbytes; i++) 472 tmpbuf[i + 1] = op->addr.val >> 473 (8 * (op->addr.nbytes - i - 1)); 474 475 xfers[xferpos].tx_buf = tmpbuf + 1; 476 xfers[xferpos].len = op->addr.nbytes; 477 xfers[xferpos].tx_nbits = op->addr.buswidth; 478 xfers[xferpos].speed_hz = op->max_freq; 479 spi_message_add_tail(&xfers[xferpos], &msg); 480 xferpos++; 481 totalxferlen += op->addr.nbytes; 482 } 483 484 if (op->dummy.nbytes) { 485 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes); 486 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; 487 xfers[xferpos].len = op->dummy.nbytes; 488 xfers[xferpos].tx_nbits = op->dummy.buswidth; 489 xfers[xferpos].dummy_data = 1; 490 xfers[xferpos].speed_hz = op->max_freq; 491 spi_message_add_tail(&xfers[xferpos], &msg); 492 xferpos++; 493 totalxferlen += op->dummy.nbytes; 494 } 495 496 if (op->data.nbytes) { 497 if (op->data.dir == SPI_MEM_DATA_IN) { 498 xfers[xferpos].rx_buf = op->data.buf.in; 499 xfers[xferpos].rx_nbits = op->data.buswidth; 500 } else { 501 xfers[xferpos].tx_buf = op->data.buf.out; 502 xfers[xferpos].tx_nbits = op->data.buswidth; 503 } 504 505 xfers[xferpos].len = op->data.nbytes; 506 xfers[xferpos].speed_hz = op->max_freq; 507 spi_message_add_tail(&xfers[xferpos], &msg); 508 xferpos++; 509 totalxferlen += op->data.nbytes; 510 } 511 512 ret = spi_sync(mem->spi, &msg); 513 514 kfree(tmpbuf); 515 516 if (ret) 517 return ret; 518 519 if (msg.actual_length != totalxferlen) 520 return -EIO; 521 522 return 0; 523 } 524 EXPORT_SYMBOL_GPL(spi_mem_exec_op); 525 526 /** 527 * spi_mem_get_name() - Return the SPI mem device name to be used by the 528 * upper layer if necessary 529 * @mem: the SPI memory 530 * 531 * This function allows SPI mem users to retrieve the SPI mem device name. 532 * It is useful if the upper layer needs to expose a custom name for 533 * compatibility reasons. 534 * 535 * Return: a string containing the name of the memory device to be used 536 * by the SPI mem user 537 */ 538 const char *spi_mem_get_name(struct spi_mem *mem) 539 { 540 return mem->name; 541 } 542 EXPORT_SYMBOL_GPL(spi_mem_get_name); 543 544 /** 545 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to 546 * match controller limitations 547 * @mem: the SPI memory 548 * @op: the operation to adjust 549 * 550 * Some controllers have FIFO limitations and must split a data transfer 551 * operation into multiple ones, others require a specific alignment for 552 * optimized accesses. This function allows SPI mem drivers to split a single 553 * operation into multiple sub-operations when required. 554 * 555 * Return: a negative error code if the controller can't properly adjust @op, 556 * 0 otherwise. Note that @op->data.nbytes will be updated if @op 557 * can't be handled in a single step. 558 */ 559 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) 560 { 561 struct spi_controller *ctlr = mem->spi->controller; 562 size_t len; 563 564 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) 565 return ctlr->mem_ops->adjust_op_size(mem, op); 566 567 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { 568 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; 569 570 if (len > spi_max_transfer_size(mem->spi)) 571 return -EINVAL; 572 573 op->data.nbytes = min3((size_t)op->data.nbytes, 574 spi_max_transfer_size(mem->spi), 575 spi_max_message_size(mem->spi) - 576 len); 577 if (!op->data.nbytes) 578 return -EINVAL; 579 } 580 581 return 0; 582 } 583 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size); 584 585 /** 586 * spi_mem_adjust_op_freq() - Adjust the frequency of a SPI mem operation to 587 * match controller, PCB and chip limitations 588 * @mem: the SPI memory 589 * @op: the operation to adjust 590 * 591 * Some chips have per-op frequency limitations and must adapt the maximum 592 * speed. This function allows SPI mem drivers to set @op->max_freq to the 593 * maximum supported value. 594 */ 595 void spi_mem_adjust_op_freq(struct spi_mem *mem, struct spi_mem_op *op) 596 { 597 if (!op->max_freq || op->max_freq > mem->spi->max_speed_hz) 598 op->max_freq = mem->spi->max_speed_hz; 599 } 600 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_freq); 601 602 /** 603 * spi_mem_calc_op_duration() - Derives the theoretical length (in ns) of an 604 * operation. This helps finding the best variant 605 * among a list of possible choices. 606 * @mem: the SPI memory 607 * @op: the operation to benchmark 608 * 609 * Some chips have per-op frequency limitations, PCBs usually have their own 610 * limitations as well, and controllers can support dual, quad or even octal 611 * modes, sometimes in DTR. All these combinations make it impossible to 612 * statically list the best combination for all situations. If we want something 613 * accurate, all these combinations should be rated (eg. with a time estimate) 614 * and the best pick should be taken based on these calculations. 615 * 616 * Returns a ns estimate for the time this op would take, except if no 617 * frequency limit has been set, in this case we return the number of 618 * cycles nevertheless to allow callers to distinguish which operation 619 * would be the fastest at iso-frequency. 620 */ 621 u64 spi_mem_calc_op_duration(struct spi_mem *mem, struct spi_mem_op *op) 622 { 623 u64 ncycles = 0; 624 u64 ps_per_cycles, duration; 625 626 spi_mem_adjust_op_freq(mem, op); 627 628 if (op->max_freq) { 629 ps_per_cycles = 1000000000000ULL; 630 do_div(ps_per_cycles, op->max_freq); 631 } else { 632 /* In this case, the unit is no longer a time unit */ 633 ps_per_cycles = 1; 634 } 635 636 ncycles += ((op->cmd.nbytes * 8) / op->cmd.buswidth) / (op->cmd.dtr ? 2 : 1); 637 ncycles += ((op->addr.nbytes * 8) / op->addr.buswidth) / (op->addr.dtr ? 2 : 1); 638 639 /* Dummy bytes are optional for some SPI flash memory operations */ 640 if (op->dummy.nbytes) 641 ncycles += ((op->dummy.nbytes * 8) / op->dummy.buswidth) / (op->dummy.dtr ? 2 : 1); 642 643 ncycles += ((op->data.nbytes * 8) / op->data.buswidth) / (op->data.dtr ? 2 : 1); 644 645 /* Derive the duration in ps */ 646 duration = ncycles * ps_per_cycles; 647 /* Convert into ns */ 648 do_div(duration, 1000); 649 650 return duration; 651 } 652 EXPORT_SYMBOL_GPL(spi_mem_calc_op_duration); 653 654 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc, 655 u64 offs, size_t len, void *buf) 656 { 657 struct spi_mem_op op = *desc->info.op_tmpl; 658 int ret; 659 660 op.addr.val = desc->info.offset + offs; 661 op.data.buf.in = buf; 662 op.data.nbytes = len; 663 ret = spi_mem_adjust_op_size(desc->mem, &op); 664 if (ret) 665 return ret; 666 667 ret = spi_mem_exec_op(desc->mem, &op); 668 if (ret) 669 return ret; 670 671 return op.data.nbytes; 672 } 673 674 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc, 675 u64 offs, size_t len, const void *buf) 676 { 677 struct spi_mem_op op = *desc->info.op_tmpl; 678 int ret; 679 680 op.addr.val = desc->info.offset + offs; 681 op.data.buf.out = buf; 682 op.data.nbytes = len; 683 ret = spi_mem_adjust_op_size(desc->mem, &op); 684 if (ret) 685 return ret; 686 687 ret = spi_mem_exec_op(desc->mem, &op); 688 if (ret) 689 return ret; 690 691 return op.data.nbytes; 692 } 693 694 /** 695 * spi_mem_dirmap_create() - Create a direct mapping descriptor 696 * @mem: SPI mem device this direct mapping should be created for 697 * @info: direct mapping information 698 * 699 * This function is creating a direct mapping descriptor which can then be used 700 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write(). 701 * If the SPI controller driver does not support direct mapping, this function 702 * falls back to an implementation using spi_mem_exec_op(), so that the caller 703 * doesn't have to bother implementing a fallback on his own. 704 * 705 * Return: a valid pointer in case of success, and ERR_PTR() otherwise. 706 */ 707 struct spi_mem_dirmap_desc * 708 spi_mem_dirmap_create(struct spi_mem *mem, 709 const struct spi_mem_dirmap_info *info) 710 { 711 struct spi_controller *ctlr = mem->spi->controller; 712 struct spi_mem_dirmap_desc *desc; 713 int ret = -ENOTSUPP; 714 715 /* Make sure the number of address cycles is between 1 and 8 bytes. */ 716 if (!info->primary_op_tmpl.addr.nbytes || info->primary_op_tmpl.addr.nbytes > 8) 717 return ERR_PTR(-EINVAL); 718 719 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */ 720 if (info->primary_op_tmpl.data.dir == SPI_MEM_NO_DATA) 721 return ERR_PTR(-EINVAL); 722 723 /* Apply similar constraints to the secondary template */ 724 if (info->secondary_op_tmpl.cmd.opcode) { 725 if (!info->secondary_op_tmpl.addr.nbytes || 726 info->secondary_op_tmpl.addr.nbytes > 8) 727 return ERR_PTR(-EINVAL); 728 729 if (info->secondary_op_tmpl.data.dir == SPI_MEM_NO_DATA) 730 return ERR_PTR(-EINVAL); 731 732 if (!spi_mem_supports_op(mem, &info->secondary_op_tmpl)) 733 return ERR_PTR(-EOPNOTSUPP); 734 735 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create && 736 !spi_mem_controller_is_capable(ctlr, secondary_op_tmpl)) 737 return ERR_PTR(-EOPNOTSUPP); 738 } 739 740 desc = kzalloc_obj(*desc); 741 if (!desc) 742 return ERR_PTR(-ENOMEM); 743 744 desc->mem = mem; 745 desc->info = *info; 746 desc->info.op_tmpl = &desc->info.primary_op_tmpl; 747 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) { 748 ret = spi_mem_access_start(mem); 749 if (ret) { 750 kfree(desc); 751 return ERR_PTR(ret); 752 } 753 754 ret = ctlr->mem_ops->dirmap_create(desc); 755 756 spi_mem_access_end(mem); 757 } 758 759 if (ret) { 760 desc->nodirmap = true; 761 if (!spi_mem_supports_op(desc->mem, &desc->info.primary_op_tmpl)) 762 ret = -EOPNOTSUPP; 763 else 764 ret = 0; 765 } 766 767 if (ret) { 768 kfree(desc); 769 return ERR_PTR(ret); 770 } 771 772 return desc; 773 } 774 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create); 775 776 /** 777 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor 778 * @desc: the direct mapping descriptor to destroy 779 * 780 * This function destroys a direct mapping descriptor previously created by 781 * spi_mem_dirmap_create(). 782 */ 783 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc) 784 { 785 struct spi_controller *ctlr = desc->mem->spi->controller; 786 787 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy) 788 ctlr->mem_ops->dirmap_destroy(desc); 789 790 kfree(desc); 791 } 792 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy); 793 794 static void devm_spi_mem_dirmap_release(struct device *dev, void *res) 795 { 796 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res; 797 798 spi_mem_dirmap_destroy(desc); 799 } 800 801 /** 802 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach 803 * it to a device 804 * @dev: device the dirmap desc will be attached to 805 * @mem: SPI mem device this direct mapping should be created for 806 * @info: direct mapping information 807 * 808 * devm_ variant of the spi_mem_dirmap_create() function. See 809 * spi_mem_dirmap_create() for more details. 810 * 811 * Return: a valid pointer in case of success, and ERR_PTR() otherwise. 812 */ 813 struct spi_mem_dirmap_desc * 814 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, 815 const struct spi_mem_dirmap_info *info) 816 { 817 struct spi_mem_dirmap_desc **ptr, *desc; 818 819 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr), 820 GFP_KERNEL); 821 if (!ptr) 822 return ERR_PTR(-ENOMEM); 823 824 desc = spi_mem_dirmap_create(mem, info); 825 if (IS_ERR(desc)) { 826 devres_free(ptr); 827 } else { 828 *ptr = desc; 829 devres_add(dev, ptr); 830 } 831 832 return desc; 833 } 834 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create); 835 836 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data) 837 { 838 struct spi_mem_dirmap_desc **ptr = res; 839 840 if (WARN_ON(!ptr || !*ptr)) 841 return 0; 842 843 return *ptr == data; 844 } 845 846 /** 847 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached 848 * to a device 849 * @dev: device the dirmap desc is attached to 850 * @desc: the direct mapping descriptor to destroy 851 * 852 * devm_ variant of the spi_mem_dirmap_destroy() function. See 853 * spi_mem_dirmap_destroy() for more details. 854 */ 855 void devm_spi_mem_dirmap_destroy(struct device *dev, 856 struct spi_mem_dirmap_desc *desc) 857 { 858 devres_release(dev, devm_spi_mem_dirmap_release, 859 devm_spi_mem_dirmap_match, desc); 860 } 861 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy); 862 863 /** 864 * spi_mem_dirmap_read() - Read data through a direct mapping 865 * @desc: direct mapping descriptor 866 * @offs: offset to start reading from. Note that this is not an absolute 867 * offset, but the offset within the direct mapping which already has 868 * its own offset 869 * @len: length in bytes 870 * @buf: destination buffer. This buffer must be DMA-able 871 * 872 * This function reads data from a memory device using a direct mapping 873 * previously instantiated with spi_mem_dirmap_create(). 874 * 875 * Return: the amount of data read from the memory device or a negative error 876 * code. Note that the returned size might be smaller than @len, and the caller 877 * is responsible for calling spi_mem_dirmap_read() again when that happens. 878 */ 879 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc, 880 u64 offs, size_t len, void *buf) 881 { 882 struct spi_controller *ctlr = desc->mem->spi->controller; 883 ssize_t ret; 884 885 if (desc->info.op_tmpl->data.dir != SPI_MEM_DATA_IN) 886 return -EINVAL; 887 888 if (!len) 889 return 0; 890 891 if (desc->nodirmap) { 892 ret = spi_mem_no_dirmap_read(desc, offs, len, buf); 893 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) { 894 ret = spi_mem_access_start(desc->mem); 895 if (ret) 896 return ret; 897 898 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf); 899 900 spi_mem_access_end(desc->mem); 901 } else { 902 ret = -ENOTSUPP; 903 } 904 905 return ret; 906 } 907 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read); 908 909 /** 910 * spi_mem_dirmap_write() - Write data through a direct mapping 911 * @desc: direct mapping descriptor 912 * @offs: offset to start writing from. Note that this is not an absolute 913 * offset, but the offset within the direct mapping which already has 914 * its own offset 915 * @len: length in bytes 916 * @buf: source buffer. This buffer must be DMA-able 917 * 918 * This function writes data to a memory device using a direct mapping 919 * previously instantiated with spi_mem_dirmap_create(). 920 * 921 * Return: the amount of data written to the memory device or a negative error 922 * code. Note that the returned size might be smaller than @len, and the caller 923 * is responsible for calling spi_mem_dirmap_write() again when that happens. 924 */ 925 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc, 926 u64 offs, size_t len, const void *buf) 927 { 928 struct spi_controller *ctlr = desc->mem->spi->controller; 929 ssize_t ret; 930 931 if (desc->info.op_tmpl->data.dir != SPI_MEM_DATA_OUT) 932 return -EINVAL; 933 934 if (!len) 935 return 0; 936 937 if (desc->nodirmap) { 938 ret = spi_mem_no_dirmap_write(desc, offs, len, buf); 939 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) { 940 ret = spi_mem_access_start(desc->mem); 941 if (ret) 942 return ret; 943 944 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf); 945 946 spi_mem_access_end(desc->mem); 947 } else { 948 ret = -ENOTSUPP; 949 } 950 951 return ret; 952 } 953 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write); 954 955 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv) 956 { 957 return container_of(drv, struct spi_mem_driver, spidrv.driver); 958 } 959 960 static int spi_mem_read_status(struct spi_mem *mem, 961 const struct spi_mem_op *op, 962 u16 *status) 963 { 964 const u8 *bytes = (u8 *)op->data.buf.in; 965 int ret; 966 967 ret = spi_mem_exec_op(mem, op); 968 if (ret) 969 return ret; 970 971 if (op->data.nbytes > 1) 972 *status = ((u16)bytes[0] << 8) | bytes[1]; 973 else 974 *status = bytes[0]; 975 976 return 0; 977 } 978 979 /** 980 * spi_mem_poll_status() - Poll memory device status 981 * @mem: SPI memory device 982 * @op: the memory operation to execute 983 * @mask: status bitmask to ckeck 984 * @match: (status & mask) expected value 985 * @initial_delay_us: delay in us before starting to poll 986 * @polling_delay_us: time to sleep between reads in us 987 * @timeout_ms: timeout in milliseconds 988 * 989 * This function polls a status register and returns when 990 * (status & mask) == match or when the timeout has expired. 991 * 992 * Return: 0 in case of success, -ETIMEDOUT in case of error, 993 * -EOPNOTSUPP if not supported. 994 */ 995 int spi_mem_poll_status(struct spi_mem *mem, 996 const struct spi_mem_op *op, 997 u16 mask, u16 match, 998 unsigned long initial_delay_us, 999 unsigned long polling_delay_us, 1000 u16 timeout_ms) 1001 { 1002 struct spi_controller *ctlr = mem->spi->controller; 1003 int ret = -EOPNOTSUPP; 1004 int read_status_ret; 1005 u16 status; 1006 1007 if (op->data.nbytes < 1 || op->data.nbytes > 2 || 1008 op->data.dir != SPI_MEM_DATA_IN) 1009 return -EINVAL; 1010 1011 if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !spi_get_csgpiod(mem->spi, 0)) { 1012 ret = spi_mem_access_start(mem); 1013 if (ret) 1014 return ret; 1015 1016 ret = ctlr->mem_ops->poll_status(mem, op, mask, match, 1017 initial_delay_us, polling_delay_us, 1018 timeout_ms); 1019 1020 spi_mem_access_end(mem); 1021 } 1022 1023 if (ret == -EOPNOTSUPP) { 1024 if (!spi_mem_supports_op(mem, op)) 1025 return ret; 1026 1027 if (initial_delay_us < 10) 1028 udelay(initial_delay_us); 1029 else 1030 usleep_range((initial_delay_us >> 2) + 1, 1031 initial_delay_us); 1032 1033 ret = read_poll_timeout(spi_mem_read_status, read_status_ret, 1034 (read_status_ret || ((status) & mask) == match), 1035 polling_delay_us, timeout_ms * 1000, false, mem, 1036 op, &status); 1037 if (read_status_ret) 1038 return read_status_ret; 1039 } 1040 1041 return ret; 1042 } 1043 EXPORT_SYMBOL_GPL(spi_mem_poll_status); 1044 1045 static int spi_mem_probe(struct spi_device *spi) 1046 { 1047 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 1048 struct spi_controller *ctlr = spi->controller; 1049 struct spi_mem *mem; 1050 1051 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL); 1052 if (!mem) 1053 return -ENOMEM; 1054 1055 mem->spi = spi; 1056 1057 if (ctlr->mem_ops && ctlr->mem_ops->get_name) 1058 mem->name = ctlr->mem_ops->get_name(mem); 1059 else 1060 mem->name = dev_name(&spi->dev); 1061 1062 if (IS_ERR_OR_NULL(mem->name)) 1063 return PTR_ERR_OR_ZERO(mem->name); 1064 1065 spi_set_drvdata(spi, mem); 1066 1067 return memdrv->probe(mem); 1068 } 1069 1070 static void spi_mem_remove(struct spi_device *spi) 1071 { 1072 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 1073 struct spi_mem *mem = spi_get_drvdata(spi); 1074 1075 if (memdrv->remove) 1076 memdrv->remove(mem); 1077 } 1078 1079 static void spi_mem_shutdown(struct spi_device *spi) 1080 { 1081 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 1082 struct spi_mem *mem = spi_get_drvdata(spi); 1083 1084 if (memdrv->shutdown) 1085 memdrv->shutdown(mem); 1086 } 1087 1088 /** 1089 * spi_mem_driver_register_with_owner() - Register a SPI memory driver 1090 * @memdrv: the SPI memory driver to register 1091 * @owner: the owner of this driver 1092 * 1093 * Registers a SPI memory driver. 1094 * 1095 * Return: 0 in case of success, a negative error core otherwise. 1096 */ 1097 1098 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv, 1099 struct module *owner) 1100 { 1101 memdrv->spidrv.probe = spi_mem_probe; 1102 memdrv->spidrv.remove = spi_mem_remove; 1103 memdrv->spidrv.shutdown = spi_mem_shutdown; 1104 1105 return __spi_register_driver(owner, &memdrv->spidrv); 1106 } 1107 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner); 1108 1109 /** 1110 * spi_mem_driver_unregister() - Unregister a SPI memory driver 1111 * @memdrv: the SPI memory driver to unregister 1112 * 1113 * Unregisters a SPI memory driver. 1114 */ 1115 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv) 1116 { 1117 spi_unregister_driver(&memdrv->spidrv); 1118 } 1119 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister); 1120