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 14 #include "internals.h" 15 16 #define SPI_MEM_MAX_BUSWIDTH 8 17 18 /** 19 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a 20 * memory operation 21 * @ctlr: the SPI controller requesting this dma_map() 22 * @op: the memory operation containing the buffer to map 23 * @sgt: a pointer to a non-initialized sg_table that will be filled by this 24 * function 25 * 26 * Some controllers might want to do DMA on the data buffer embedded in @op. 27 * This helper prepares everything for you and provides a ready-to-use 28 * sg_table. This function is not intended to be called from spi drivers. 29 * Only SPI controller drivers should use it. 30 * Note that the caller must ensure the memory region pointed by 31 * op->data.buf.{in,out} is DMA-able before calling this function. 32 * 33 * Return: 0 in case of success, a negative error code otherwise. 34 */ 35 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 36 const struct spi_mem_op *op, 37 struct sg_table *sgt) 38 { 39 struct device *dmadev; 40 41 if (!op->data.nbytes) 42 return -EINVAL; 43 44 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) 45 dmadev = ctlr->dma_tx->device->dev; 46 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) 47 dmadev = ctlr->dma_rx->device->dev; 48 else 49 dmadev = ctlr->dev.parent; 50 51 if (!dmadev) 52 return -EINVAL; 53 54 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes, 55 op->data.dir == SPI_MEM_DATA_IN ? 56 DMA_FROM_DEVICE : DMA_TO_DEVICE); 57 } 58 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data); 59 60 /** 61 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a 62 * memory operation 63 * @ctlr: the SPI controller requesting this dma_unmap() 64 * @op: the memory operation containing the buffer to unmap 65 * @sgt: a pointer to an sg_table previously initialized by 66 * spi_controller_dma_map_mem_op_data() 67 * 68 * Some controllers might want to do DMA on the data buffer embedded in @op. 69 * This helper prepares things so that the CPU can access the 70 * op->data.buf.{in,out} buffer again. 71 * 72 * This function is not intended to be called from SPI drivers. Only SPI 73 * controller drivers should use it. 74 * 75 * This function should be called after the DMA operation has finished and is 76 * only valid if the previous spi_controller_dma_map_mem_op_data() call 77 * returned 0. 78 * 79 * Return: 0 in case of success, a negative error code otherwise. 80 */ 81 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 82 const struct spi_mem_op *op, 83 struct sg_table *sgt) 84 { 85 struct device *dmadev; 86 87 if (!op->data.nbytes) 88 return; 89 90 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) 91 dmadev = ctlr->dma_tx->device->dev; 92 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) 93 dmadev = ctlr->dma_rx->device->dev; 94 else 95 dmadev = ctlr->dev.parent; 96 97 spi_unmap_buf(ctlr, dmadev, sgt, 98 op->data.dir == SPI_MEM_DATA_IN ? 99 DMA_FROM_DEVICE : DMA_TO_DEVICE); 100 } 101 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data); 102 103 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx) 104 { 105 u32 mode = mem->spi->mode; 106 107 switch (buswidth) { 108 case 1: 109 return 0; 110 111 case 2: 112 if ((tx && 113 (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) || 114 (!tx && 115 (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))) 116 return 0; 117 118 break; 119 120 case 4: 121 if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) || 122 (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL)))) 123 return 0; 124 125 break; 126 127 case 8: 128 if ((tx && (mode & SPI_TX_OCTAL)) || 129 (!tx && (mode & SPI_RX_OCTAL))) 130 return 0; 131 132 break; 133 134 default: 135 break; 136 } 137 138 return -ENOTSUPP; 139 } 140 141 static bool spi_mem_check_buswidth(struct spi_mem *mem, 142 const struct spi_mem_op *op) 143 { 144 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true)) 145 return false; 146 147 if (op->addr.nbytes && 148 spi_check_buswidth_req(mem, op->addr.buswidth, true)) 149 return false; 150 151 if (op->dummy.nbytes && 152 spi_check_buswidth_req(mem, op->dummy.buswidth, true)) 153 return false; 154 155 if (op->data.dir != SPI_MEM_NO_DATA && 156 spi_check_buswidth_req(mem, op->data.buswidth, 157 op->data.dir == SPI_MEM_DATA_OUT)) 158 return false; 159 160 return true; 161 } 162 163 bool spi_mem_default_supports_op(struct spi_mem *mem, 164 const struct spi_mem_op *op) 165 { 166 struct spi_controller *ctlr = mem->spi->controller; 167 bool op_is_dtr = 168 op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr; 169 170 if (op_is_dtr) { 171 if (!spi_mem_controller_is_capable(ctlr, dtr)) 172 return false; 173 174 if (op->cmd.nbytes != 2) 175 return false; 176 } else { 177 if (op->cmd.nbytes != 1) 178 return false; 179 } 180 181 if (op->data.ecc) { 182 if (!spi_mem_controller_is_capable(ctlr, ecc)) 183 return false; 184 } 185 186 return spi_mem_check_buswidth(mem, op); 187 } 188 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op); 189 190 static bool spi_mem_buswidth_is_valid(u8 buswidth) 191 { 192 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH) 193 return false; 194 195 return true; 196 } 197 198 static int spi_mem_check_op(const struct spi_mem_op *op) 199 { 200 if (!op->cmd.buswidth || !op->cmd.nbytes) 201 return -EINVAL; 202 203 if ((op->addr.nbytes && !op->addr.buswidth) || 204 (op->dummy.nbytes && !op->dummy.buswidth) || 205 (op->data.nbytes && !op->data.buswidth)) 206 return -EINVAL; 207 208 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) || 209 !spi_mem_buswidth_is_valid(op->addr.buswidth) || 210 !spi_mem_buswidth_is_valid(op->dummy.buswidth) || 211 !spi_mem_buswidth_is_valid(op->data.buswidth)) 212 return -EINVAL; 213 214 return 0; 215 } 216 217 static bool spi_mem_internal_supports_op(struct spi_mem *mem, 218 const struct spi_mem_op *op) 219 { 220 struct spi_controller *ctlr = mem->spi->controller; 221 222 if (ctlr->mem_ops && ctlr->mem_ops->supports_op) 223 return ctlr->mem_ops->supports_op(mem, op); 224 225 return spi_mem_default_supports_op(mem, op); 226 } 227 228 /** 229 * spi_mem_supports_op() - Check if a memory device and the controller it is 230 * connected to support a specific memory operation 231 * @mem: the SPI memory 232 * @op: the memory operation to check 233 * 234 * Some controllers are only supporting Single or Dual IOs, others might only 235 * support specific opcodes, or it can even be that the controller and device 236 * both support Quad IOs but the hardware prevents you from using it because 237 * only 2 IO lines are connected. 238 * 239 * This function checks whether a specific operation is supported. 240 * 241 * Return: true if @op is supported, false otherwise. 242 */ 243 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) 244 { 245 if (spi_mem_check_op(op)) 246 return false; 247 248 return spi_mem_internal_supports_op(mem, op); 249 } 250 EXPORT_SYMBOL_GPL(spi_mem_supports_op); 251 252 static int spi_mem_access_start(struct spi_mem *mem) 253 { 254 struct spi_controller *ctlr = mem->spi->controller; 255 256 /* 257 * Flush the message queue before executing our SPI memory 258 * operation to prevent preemption of regular SPI transfers. 259 */ 260 spi_flush_queue(ctlr); 261 262 if (ctlr->auto_runtime_pm) { 263 int ret; 264 265 ret = pm_runtime_get_sync(ctlr->dev.parent); 266 if (ret < 0) { 267 pm_runtime_put_noidle(ctlr->dev.parent); 268 dev_err(&ctlr->dev, "Failed to power device: %d\n", 269 ret); 270 return ret; 271 } 272 } 273 274 mutex_lock(&ctlr->bus_lock_mutex); 275 mutex_lock(&ctlr->io_mutex); 276 277 return 0; 278 } 279 280 static void spi_mem_access_end(struct spi_mem *mem) 281 { 282 struct spi_controller *ctlr = mem->spi->controller; 283 284 mutex_unlock(&ctlr->io_mutex); 285 mutex_unlock(&ctlr->bus_lock_mutex); 286 287 if (ctlr->auto_runtime_pm) 288 pm_runtime_put(ctlr->dev.parent); 289 } 290 291 /** 292 * spi_mem_exec_op() - Execute a memory operation 293 * @mem: the SPI memory 294 * @op: the memory operation to execute 295 * 296 * Executes a memory operation. 297 * 298 * This function first checks that @op is supported and then tries to execute 299 * it. 300 * 301 * Return: 0 in case of success, a negative error code otherwise. 302 */ 303 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) 304 { 305 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0; 306 struct spi_controller *ctlr = mem->spi->controller; 307 struct spi_transfer xfers[4] = { }; 308 struct spi_message msg; 309 u8 *tmpbuf; 310 int ret; 311 312 ret = spi_mem_check_op(op); 313 if (ret) 314 return ret; 315 316 if (!spi_mem_internal_supports_op(mem, op)) 317 return -ENOTSUPP; 318 319 if (ctlr->mem_ops && !mem->spi->cs_gpiod) { 320 ret = spi_mem_access_start(mem); 321 if (ret) 322 return ret; 323 324 ret = ctlr->mem_ops->exec_op(mem, op); 325 326 spi_mem_access_end(mem); 327 328 /* 329 * Some controllers only optimize specific paths (typically the 330 * read path) and expect the core to use the regular SPI 331 * interface in other cases. 332 */ 333 if (!ret || ret != -ENOTSUPP) 334 return ret; 335 } 336 337 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; 338 339 /* 340 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so 341 * we're guaranteed that this buffer is DMA-able, as required by the 342 * SPI layer. 343 */ 344 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA); 345 if (!tmpbuf) 346 return -ENOMEM; 347 348 spi_message_init(&msg); 349 350 tmpbuf[0] = op->cmd.opcode; 351 xfers[xferpos].tx_buf = tmpbuf; 352 xfers[xferpos].len = op->cmd.nbytes; 353 xfers[xferpos].tx_nbits = op->cmd.buswidth; 354 spi_message_add_tail(&xfers[xferpos], &msg); 355 xferpos++; 356 totalxferlen++; 357 358 if (op->addr.nbytes) { 359 int i; 360 361 for (i = 0; i < op->addr.nbytes; i++) 362 tmpbuf[i + 1] = op->addr.val >> 363 (8 * (op->addr.nbytes - i - 1)); 364 365 xfers[xferpos].tx_buf = tmpbuf + 1; 366 xfers[xferpos].len = op->addr.nbytes; 367 xfers[xferpos].tx_nbits = op->addr.buswidth; 368 spi_message_add_tail(&xfers[xferpos], &msg); 369 xferpos++; 370 totalxferlen += op->addr.nbytes; 371 } 372 373 if (op->dummy.nbytes) { 374 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes); 375 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; 376 xfers[xferpos].len = op->dummy.nbytes; 377 xfers[xferpos].tx_nbits = op->dummy.buswidth; 378 xfers[xferpos].dummy_data = 1; 379 spi_message_add_tail(&xfers[xferpos], &msg); 380 xferpos++; 381 totalxferlen += op->dummy.nbytes; 382 } 383 384 if (op->data.nbytes) { 385 if (op->data.dir == SPI_MEM_DATA_IN) { 386 xfers[xferpos].rx_buf = op->data.buf.in; 387 xfers[xferpos].rx_nbits = op->data.buswidth; 388 } else { 389 xfers[xferpos].tx_buf = op->data.buf.out; 390 xfers[xferpos].tx_nbits = op->data.buswidth; 391 } 392 393 xfers[xferpos].len = op->data.nbytes; 394 spi_message_add_tail(&xfers[xferpos], &msg); 395 xferpos++; 396 totalxferlen += op->data.nbytes; 397 } 398 399 ret = spi_sync(mem->spi, &msg); 400 401 kfree(tmpbuf); 402 403 if (ret) 404 return ret; 405 406 if (msg.actual_length != totalxferlen) 407 return -EIO; 408 409 return 0; 410 } 411 EXPORT_SYMBOL_GPL(spi_mem_exec_op); 412 413 /** 414 * spi_mem_get_name() - Return the SPI mem device name to be used by the 415 * upper layer if necessary 416 * @mem: the SPI memory 417 * 418 * This function allows SPI mem users to retrieve the SPI mem device name. 419 * It is useful if the upper layer needs to expose a custom name for 420 * compatibility reasons. 421 * 422 * Return: a string containing the name of the memory device to be used 423 * by the SPI mem user 424 */ 425 const char *spi_mem_get_name(struct spi_mem *mem) 426 { 427 return mem->name; 428 } 429 EXPORT_SYMBOL_GPL(spi_mem_get_name); 430 431 /** 432 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to 433 * match controller limitations 434 * @mem: the SPI memory 435 * @op: the operation to adjust 436 * 437 * Some controllers have FIFO limitations and must split a data transfer 438 * operation into multiple ones, others require a specific alignment for 439 * optimized accesses. This function allows SPI mem drivers to split a single 440 * operation into multiple sub-operations when required. 441 * 442 * Return: a negative error code if the controller can't properly adjust @op, 443 * 0 otherwise. Note that @op->data.nbytes will be updated if @op 444 * can't be handled in a single step. 445 */ 446 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) 447 { 448 struct spi_controller *ctlr = mem->spi->controller; 449 size_t len; 450 451 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) 452 return ctlr->mem_ops->adjust_op_size(mem, op); 453 454 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { 455 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; 456 457 if (len > spi_max_transfer_size(mem->spi)) 458 return -EINVAL; 459 460 op->data.nbytes = min3((size_t)op->data.nbytes, 461 spi_max_transfer_size(mem->spi), 462 spi_max_message_size(mem->spi) - 463 len); 464 if (!op->data.nbytes) 465 return -EINVAL; 466 } 467 468 return 0; 469 } 470 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size); 471 472 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc, 473 u64 offs, size_t len, void *buf) 474 { 475 struct spi_mem_op op = desc->info.op_tmpl; 476 int ret; 477 478 op.addr.val = desc->info.offset + offs; 479 op.data.buf.in = buf; 480 op.data.nbytes = len; 481 ret = spi_mem_adjust_op_size(desc->mem, &op); 482 if (ret) 483 return ret; 484 485 ret = spi_mem_exec_op(desc->mem, &op); 486 if (ret) 487 return ret; 488 489 return op.data.nbytes; 490 } 491 492 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc, 493 u64 offs, size_t len, const void *buf) 494 { 495 struct spi_mem_op op = desc->info.op_tmpl; 496 int ret; 497 498 op.addr.val = desc->info.offset + offs; 499 op.data.buf.out = buf; 500 op.data.nbytes = len; 501 ret = spi_mem_adjust_op_size(desc->mem, &op); 502 if (ret) 503 return ret; 504 505 ret = spi_mem_exec_op(desc->mem, &op); 506 if (ret) 507 return ret; 508 509 return op.data.nbytes; 510 } 511 512 /** 513 * spi_mem_dirmap_create() - Create a direct mapping descriptor 514 * @mem: SPI mem device this direct mapping should be created for 515 * @info: direct mapping information 516 * 517 * This function is creating a direct mapping descriptor which can then be used 518 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write(). 519 * If the SPI controller driver does not support direct mapping, this function 520 * falls back to an implementation using spi_mem_exec_op(), so that the caller 521 * doesn't have to bother implementing a fallback on his own. 522 * 523 * Return: a valid pointer in case of success, and ERR_PTR() otherwise. 524 */ 525 struct spi_mem_dirmap_desc * 526 spi_mem_dirmap_create(struct spi_mem *mem, 527 const struct spi_mem_dirmap_info *info) 528 { 529 struct spi_controller *ctlr = mem->spi->controller; 530 struct spi_mem_dirmap_desc *desc; 531 int ret = -ENOTSUPP; 532 533 /* Make sure the number of address cycles is between 1 and 8 bytes. */ 534 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8) 535 return ERR_PTR(-EINVAL); 536 537 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */ 538 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA) 539 return ERR_PTR(-EINVAL); 540 541 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 542 if (!desc) 543 return ERR_PTR(-ENOMEM); 544 545 desc->mem = mem; 546 desc->info = *info; 547 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) 548 ret = ctlr->mem_ops->dirmap_create(desc); 549 550 if (ret) { 551 desc->nodirmap = true; 552 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl)) 553 ret = -ENOTSUPP; 554 else 555 ret = 0; 556 } 557 558 if (ret) { 559 kfree(desc); 560 return ERR_PTR(ret); 561 } 562 563 return desc; 564 } 565 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create); 566 567 /** 568 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor 569 * @desc: the direct mapping descriptor to destroy 570 * 571 * This function destroys a direct mapping descriptor previously created by 572 * spi_mem_dirmap_create(). 573 */ 574 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc) 575 { 576 struct spi_controller *ctlr = desc->mem->spi->controller; 577 578 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy) 579 ctlr->mem_ops->dirmap_destroy(desc); 580 581 kfree(desc); 582 } 583 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy); 584 585 static void devm_spi_mem_dirmap_release(struct device *dev, void *res) 586 { 587 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res; 588 589 spi_mem_dirmap_destroy(desc); 590 } 591 592 /** 593 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach 594 * it to a device 595 * @dev: device the dirmap desc will be attached to 596 * @mem: SPI mem device this direct mapping should be created for 597 * @info: direct mapping information 598 * 599 * devm_ variant of the spi_mem_dirmap_create() function. See 600 * spi_mem_dirmap_create() for more details. 601 * 602 * Return: a valid pointer in case of success, and ERR_PTR() otherwise. 603 */ 604 struct spi_mem_dirmap_desc * 605 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, 606 const struct spi_mem_dirmap_info *info) 607 { 608 struct spi_mem_dirmap_desc **ptr, *desc; 609 610 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr), 611 GFP_KERNEL); 612 if (!ptr) 613 return ERR_PTR(-ENOMEM); 614 615 desc = spi_mem_dirmap_create(mem, info); 616 if (IS_ERR(desc)) { 617 devres_free(ptr); 618 } else { 619 *ptr = desc; 620 devres_add(dev, ptr); 621 } 622 623 return desc; 624 } 625 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create); 626 627 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data) 628 { 629 struct spi_mem_dirmap_desc **ptr = res; 630 631 if (WARN_ON(!ptr || !*ptr)) 632 return 0; 633 634 return *ptr == data; 635 } 636 637 /** 638 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached 639 * to a device 640 * @dev: device the dirmap desc is attached to 641 * @desc: the direct mapping descriptor to destroy 642 * 643 * devm_ variant of the spi_mem_dirmap_destroy() function. See 644 * spi_mem_dirmap_destroy() for more details. 645 */ 646 void devm_spi_mem_dirmap_destroy(struct device *dev, 647 struct spi_mem_dirmap_desc *desc) 648 { 649 devres_release(dev, devm_spi_mem_dirmap_release, 650 devm_spi_mem_dirmap_match, desc); 651 } 652 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy); 653 654 /** 655 * spi_mem_dirmap_read() - Read data through a direct mapping 656 * @desc: direct mapping descriptor 657 * @offs: offset to start reading from. Note that this is not an absolute 658 * offset, but the offset within the direct mapping which already has 659 * its own offset 660 * @len: length in bytes 661 * @buf: destination buffer. This buffer must be DMA-able 662 * 663 * This function reads data from a memory device using a direct mapping 664 * previously instantiated with spi_mem_dirmap_create(). 665 * 666 * Return: the amount of data read from the memory device or a negative error 667 * code. Note that the returned size might be smaller than @len, and the caller 668 * is responsible for calling spi_mem_dirmap_read() again when that happens. 669 */ 670 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc, 671 u64 offs, size_t len, void *buf) 672 { 673 struct spi_controller *ctlr = desc->mem->spi->controller; 674 ssize_t ret; 675 676 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN) 677 return -EINVAL; 678 679 if (!len) 680 return 0; 681 682 if (desc->nodirmap) { 683 ret = spi_mem_no_dirmap_read(desc, offs, len, buf); 684 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) { 685 ret = spi_mem_access_start(desc->mem); 686 if (ret) 687 return ret; 688 689 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf); 690 691 spi_mem_access_end(desc->mem); 692 } else { 693 ret = -ENOTSUPP; 694 } 695 696 return ret; 697 } 698 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read); 699 700 /** 701 * spi_mem_dirmap_write() - Write data through a direct mapping 702 * @desc: direct mapping descriptor 703 * @offs: offset to start writing from. Note that this is not an absolute 704 * offset, but the offset within the direct mapping which already has 705 * its own offset 706 * @len: length in bytes 707 * @buf: source buffer. This buffer must be DMA-able 708 * 709 * This function writes data to a memory device using a direct mapping 710 * previously instantiated with spi_mem_dirmap_create(). 711 * 712 * Return: the amount of data written to the memory device or a negative error 713 * code. Note that the returned size might be smaller than @len, and the caller 714 * is responsible for calling spi_mem_dirmap_write() again when that happens. 715 */ 716 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc, 717 u64 offs, size_t len, const void *buf) 718 { 719 struct spi_controller *ctlr = desc->mem->spi->controller; 720 ssize_t ret; 721 722 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT) 723 return -EINVAL; 724 725 if (!len) 726 return 0; 727 728 if (desc->nodirmap) { 729 ret = spi_mem_no_dirmap_write(desc, offs, len, buf); 730 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) { 731 ret = spi_mem_access_start(desc->mem); 732 if (ret) 733 return ret; 734 735 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf); 736 737 spi_mem_access_end(desc->mem); 738 } else { 739 ret = -ENOTSUPP; 740 } 741 742 return ret; 743 } 744 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write); 745 746 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv) 747 { 748 return container_of(drv, struct spi_mem_driver, spidrv.driver); 749 } 750 751 static int spi_mem_read_status(struct spi_mem *mem, 752 const struct spi_mem_op *op, 753 u16 *status) 754 { 755 const u8 *bytes = (u8 *)op->data.buf.in; 756 int ret; 757 758 ret = spi_mem_exec_op(mem, op); 759 if (ret) 760 return ret; 761 762 if (op->data.nbytes > 1) 763 *status = ((u16)bytes[0] << 8) | bytes[1]; 764 else 765 *status = bytes[0]; 766 767 return 0; 768 } 769 770 /** 771 * spi_mem_poll_status() - Poll memory device status 772 * @mem: SPI memory device 773 * @op: the memory operation to execute 774 * @mask: status bitmask to ckeck 775 * @match: (status & mask) expected value 776 * @initial_delay_us: delay in us before starting to poll 777 * @polling_delay_us: time to sleep between reads in us 778 * @timeout_ms: timeout in milliseconds 779 * 780 * This function polls a status register and returns when 781 * (status & mask) == match or when the timeout has expired. 782 * 783 * Return: 0 in case of success, -ETIMEDOUT in case of error, 784 * -EOPNOTSUPP if not supported. 785 */ 786 int spi_mem_poll_status(struct spi_mem *mem, 787 const struct spi_mem_op *op, 788 u16 mask, u16 match, 789 unsigned long initial_delay_us, 790 unsigned long polling_delay_us, 791 u16 timeout_ms) 792 { 793 struct spi_controller *ctlr = mem->spi->controller; 794 int ret = -EOPNOTSUPP; 795 int read_status_ret; 796 u16 status; 797 798 if (op->data.nbytes < 1 || op->data.nbytes > 2 || 799 op->data.dir != SPI_MEM_DATA_IN) 800 return -EINVAL; 801 802 if (ctlr->mem_ops && ctlr->mem_ops->poll_status) { 803 ret = spi_mem_access_start(mem); 804 if (ret) 805 return ret; 806 807 ret = ctlr->mem_ops->poll_status(mem, op, mask, match, 808 initial_delay_us, polling_delay_us, 809 timeout_ms); 810 811 spi_mem_access_end(mem); 812 } 813 814 if (ret == -EOPNOTSUPP) { 815 if (!spi_mem_supports_op(mem, op)) 816 return ret; 817 818 if (initial_delay_us < 10) 819 udelay(initial_delay_us); 820 else 821 usleep_range((initial_delay_us >> 2) + 1, 822 initial_delay_us); 823 824 ret = read_poll_timeout(spi_mem_read_status, read_status_ret, 825 (read_status_ret || ((status) & mask) == match), 826 polling_delay_us, timeout_ms * 1000, false, mem, 827 op, &status); 828 if (read_status_ret) 829 return read_status_ret; 830 } 831 832 return ret; 833 } 834 EXPORT_SYMBOL_GPL(spi_mem_poll_status); 835 836 static int spi_mem_probe(struct spi_device *spi) 837 { 838 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 839 struct spi_controller *ctlr = spi->controller; 840 struct spi_mem *mem; 841 842 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL); 843 if (!mem) 844 return -ENOMEM; 845 846 mem->spi = spi; 847 848 if (ctlr->mem_ops && ctlr->mem_ops->get_name) 849 mem->name = ctlr->mem_ops->get_name(mem); 850 else 851 mem->name = dev_name(&spi->dev); 852 853 if (IS_ERR_OR_NULL(mem->name)) 854 return PTR_ERR_OR_ZERO(mem->name); 855 856 spi_set_drvdata(spi, mem); 857 858 return memdrv->probe(mem); 859 } 860 861 static void spi_mem_remove(struct spi_device *spi) 862 { 863 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 864 struct spi_mem *mem = spi_get_drvdata(spi); 865 866 if (memdrv->remove) 867 memdrv->remove(mem); 868 } 869 870 static void spi_mem_shutdown(struct spi_device *spi) 871 { 872 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); 873 struct spi_mem *mem = spi_get_drvdata(spi); 874 875 if (memdrv->shutdown) 876 memdrv->shutdown(mem); 877 } 878 879 /** 880 * spi_mem_driver_register_with_owner() - Register a SPI memory driver 881 * @memdrv: the SPI memory driver to register 882 * @owner: the owner of this driver 883 * 884 * Registers a SPI memory driver. 885 * 886 * Return: 0 in case of success, a negative error core otherwise. 887 */ 888 889 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv, 890 struct module *owner) 891 { 892 memdrv->spidrv.probe = spi_mem_probe; 893 memdrv->spidrv.remove = spi_mem_remove; 894 memdrv->spidrv.shutdown = spi_mem_shutdown; 895 896 return __spi_register_driver(owner, &memdrv->spidrv); 897 } 898 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner); 899 900 /** 901 * spi_mem_driver_unregister() - Unregister a SPI memory driver 902 * @memdrv: the SPI memory driver to unregister 903 * 904 * Unregisters a SPI memory driver. 905 */ 906 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv) 907 { 908 spi_unregister_driver(&memdrv->spidrv); 909 } 910 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister); 911