88#include <linux/dma-mapping.h>
99#include <linux/interrupt.h>
1010#include <linux/module.h>
11+ #include <linux/preempt.h>
1112#include <linux/highmem.h>
1213#include <linux/delay.h>
1314#include <linux/slab.h>
1415#include <linux/spi/spi.h>
16+ #include <linux/spi/spi-mem.h>
17+ #include <linux/string.h>
1518#include <linux/of.h>
1619
1720#include "spi-dw.h"
@@ -422,6 +425,301 @@ static void dw_spi_handle_err(struct spi_controller *master,
422425 spi_reset_chip (dws );
423426}
424427
428+ static int dw_spi_adjust_mem_op_size (struct spi_mem * mem , struct spi_mem_op * op )
429+ {
430+ if (op -> data .dir == SPI_MEM_DATA_IN )
431+ op -> data .nbytes = clamp_val (op -> data .nbytes , 0 , SPI_NDF_MASK + 1 );
432+
433+ return 0 ;
434+ }
435+
436+ static bool dw_spi_supports_mem_op (struct spi_mem * mem ,
437+ const struct spi_mem_op * op )
438+ {
439+ if (op -> data .buswidth > 1 || op -> addr .buswidth > 1 ||
440+ op -> dummy .buswidth > 1 || op -> cmd .buswidth > 1 )
441+ return false;
442+
443+ return spi_mem_default_supports_op (mem , op );
444+ }
445+
446+ static int dw_spi_init_mem_buf (struct dw_spi * dws , const struct spi_mem_op * op )
447+ {
448+ unsigned int i , j , len ;
449+ u8 * out ;
450+
451+ /*
452+ * Calculate the total length of the EEPROM command transfer and
453+ * either use the pre-allocated buffer or create a temporary one.
454+ */
455+ len = op -> cmd .nbytes + op -> addr .nbytes + op -> dummy .nbytes ;
456+ if (op -> data .dir == SPI_MEM_DATA_OUT )
457+ len += op -> data .nbytes ;
458+
459+ if (len <= SPI_BUF_SIZE ) {
460+ out = dws -> buf ;
461+ } else {
462+ out = kzalloc (len , GFP_KERNEL );
463+ if (!out )
464+ return - ENOMEM ;
465+ }
466+
467+ /*
468+ * Collect the operation code, address and dummy bytes into the single
469+ * buffer. If it's a transfer with data to be sent, also copy it into the
470+ * single buffer in order to speed the data transmission up.
471+ */
472+ for (i = 0 ; i < op -> cmd .nbytes ; ++ i )
473+ out [i ] = SPI_GET_BYTE (op -> cmd .opcode , op -> cmd .nbytes - i - 1 );
474+ for (j = 0 ; j < op -> addr .nbytes ; ++ i , ++ j )
475+ out [i ] = SPI_GET_BYTE (op -> addr .val , op -> addr .nbytes - j - 1 );
476+ for (j = 0 ; j < op -> dummy .nbytes ; ++ i , ++ j )
477+ out [i ] = 0x0 ;
478+
479+ if (op -> data .dir == SPI_MEM_DATA_OUT )
480+ memcpy (& out [i ], op -> data .buf .out , op -> data .nbytes );
481+
482+ dws -> n_bytes = 1 ;
483+ dws -> tx = out ;
484+ dws -> tx_len = len ;
485+ if (op -> data .dir == SPI_MEM_DATA_IN ) {
486+ dws -> rx = op -> data .buf .in ;
487+ dws -> rx_len = op -> data .nbytes ;
488+ } else {
489+ dws -> rx = NULL ;
490+ dws -> rx_len = 0 ;
491+ }
492+
493+ return 0 ;
494+ }
495+
496+ static void dw_spi_free_mem_buf (struct dw_spi * dws )
497+ {
498+ if (dws -> tx != dws -> buf )
499+ kfree (dws -> tx );
500+ }
501+
502+ static int dw_spi_write_then_read (struct dw_spi * dws , struct spi_device * spi )
503+ {
504+ u32 room , entries , sts ;
505+ unsigned int len ;
506+ u8 * buf ;
507+
508+ /*
509+ * At initial stage we just pre-fill the Tx FIFO in with no rush,
510+ * since native CS hasn't been enabled yet and the automatic data
511+ * transmission won't start til we do that.
512+ */
513+ len = min (dws -> fifo_len , dws -> tx_len );
514+ buf = dws -> tx ;
515+ while (len -- )
516+ dw_write_io_reg (dws , DW_SPI_DR , * buf ++ );
517+
518+ /*
519+ * After setting any bit in the SER register the transmission will
520+ * start automatically. We have to keep up with that procedure
521+ * otherwise the CS de-assertion will happen whereupon the memory
522+ * operation will be pre-terminated.
523+ */
524+ len = dws -> tx_len - ((void * )buf - dws -> tx );
525+ dw_spi_set_cs (spi , false);
526+ while (len ) {
527+ entries = readl_relaxed (dws -> regs + DW_SPI_TXFLR );
528+ if (!entries ) {
529+ dev_err (& dws -> master -> dev , "CS de-assertion on Tx\n" );
530+ return - EIO ;
531+ }
532+ room = min (dws -> fifo_len - entries , len );
533+ for (; room ; -- room , -- len )
534+ dw_write_io_reg (dws , DW_SPI_DR , * buf ++ );
535+ }
536+
537+ /*
538+ * Data fetching will start automatically if the EEPROM-read mode is
539+ * activated. We have to keep up with the incoming data pace to
540+ * prevent the Rx FIFO overflow causing the inbound data loss.
541+ */
542+ len = dws -> rx_len ;
543+ buf = dws -> rx ;
544+ while (len ) {
545+ entries = readl_relaxed (dws -> regs + DW_SPI_RXFLR );
546+ if (!entries ) {
547+ sts = readl_relaxed (dws -> regs + DW_SPI_RISR );
548+ if (sts & SPI_INT_RXOI ) {
549+ dev_err (& dws -> master -> dev , "FIFO overflow on Rx\n" );
550+ return - EIO ;
551+ }
552+ continue ;
553+ }
554+ entries = min (entries , len );
555+ for (; entries ; -- entries , -- len )
556+ * buf ++ = dw_read_io_reg (dws , DW_SPI_DR );
557+ }
558+
559+ return 0 ;
560+ }
561+
562+ static inline bool dw_spi_ctlr_busy (struct dw_spi * dws )
563+ {
564+ return dw_readl (dws , DW_SPI_SR ) & SR_BUSY ;
565+ }
566+
567+ static int dw_spi_wait_mem_op_done (struct dw_spi * dws )
568+ {
569+ int retry = SPI_WAIT_RETRIES ;
570+ struct spi_delay delay ;
571+ unsigned long ns , us ;
572+ u32 nents ;
573+
574+ nents = dw_readl (dws , DW_SPI_TXFLR );
575+ ns = NSEC_PER_SEC / dws -> current_freq * nents ;
576+ ns *= dws -> n_bytes * BITS_PER_BYTE ;
577+ if (ns <= NSEC_PER_USEC ) {
578+ delay .unit = SPI_DELAY_UNIT_NSECS ;
579+ delay .value = ns ;
580+ } else {
581+ us = DIV_ROUND_UP (ns , NSEC_PER_USEC );
582+ delay .unit = SPI_DELAY_UNIT_USECS ;
583+ delay .value = clamp_val (us , 0 , USHRT_MAX );
584+ }
585+
586+ while (dw_spi_ctlr_busy (dws ) && retry -- )
587+ spi_delay_exec (& delay , NULL );
588+
589+ if (retry < 0 ) {
590+ dev_err (& dws -> master -> dev , "Mem op hanged up\n" );
591+ return - EIO ;
592+ }
593+
594+ return 0 ;
595+ }
596+
597+ static void dw_spi_stop_mem_op (struct dw_spi * dws , struct spi_device * spi )
598+ {
599+ spi_enable_chip (dws , 0 );
600+ dw_spi_set_cs (spi , true);
601+ spi_enable_chip (dws , 1 );
602+ }
603+
604+ /*
605+ * The SPI memory operation implementation below is the best choice for the
606+ * devices, which are selected by the native chip-select lane. It's
607+ * specifically developed to workaround the problem with automatic chip-select
608+ * lane toggle when there is no data in the Tx FIFO buffer. Luckily the current
609+ * SPI-mem core calls exec_op() callback only if the GPIO-based CS is
610+ * unavailable.
611+ */
612+ static int dw_spi_exec_mem_op (struct spi_mem * mem , const struct spi_mem_op * op )
613+ {
614+ struct dw_spi * dws = spi_controller_get_devdata (mem -> spi -> controller );
615+ struct dw_spi_cfg cfg ;
616+ unsigned long flags ;
617+ int ret ;
618+
619+ /*
620+ * Collect the outbound data into a single buffer to speed the
621+ * transmission up at least on the initial stage.
622+ */
623+ ret = dw_spi_init_mem_buf (dws , op );
624+ if (ret )
625+ return ret ;
626+
627+ /*
628+ * DW SPI EEPROM-read mode is required only for the SPI memory Data-IN
629+ * operation. Transmit-only mode is suitable for the rest of them.
630+ */
631+ cfg .dfs = 8 ;
632+ cfg .freq = mem -> spi -> max_speed_hz ;
633+ if (op -> data .dir == SPI_MEM_DATA_IN ) {
634+ cfg .tmode = SPI_TMOD_EPROMREAD ;
635+ cfg .ndf = op -> data .nbytes ;
636+ } else {
637+ cfg .tmode = SPI_TMOD_TO ;
638+ }
639+
640+ spi_enable_chip (dws , 0 );
641+
642+ dw_spi_update_config (dws , mem -> spi , & cfg );
643+
644+ spi_mask_intr (dws , 0xff );
645+
646+ spi_enable_chip (dws , 1 );
647+
648+ /*
649+ * DW APB SSI controller has very nasty peculiarities. First originally
650+ * (without any vendor-specific modifications) it doesn't provide a
651+ * direct way to set and clear the native chip-select signal. Instead
652+ * the controller asserts the CS lane if Tx FIFO isn't empty and a
653+ * transmission is going on, and automatically de-asserts it back to
654+ * the high level if the Tx FIFO doesn't have anything to be pushed
655+ * out. Due to that a multi-tasking or heavy IRQs activity might be
656+ * fatal, since the transfer procedure preemption may cause the Tx FIFO
657+ * getting empty and sudden CS de-assertion, which in the middle of the
658+ * transfer will most likely cause the data loss. Secondly the
659+ * EEPROM-read or Read-only DW SPI transfer modes imply the incoming
660+ * data being automatically pulled in into the Rx FIFO. So if the
661+ * driver software is late in fetching the data from the FIFO before
662+ * it's overflown, new incoming data will be lost. In order to make
663+ * sure the executed memory operations are CS-atomic and to prevent the
664+ * Rx FIFO overflow we have to disable the local interrupts so to block
665+ * any preemption during the subsequent IO operations.
666+ *
667+ * Note. At some circumstances disabling IRQs may not help to prevent
668+ * the problems described above. The CS de-assertion and Rx FIFO
669+ * overflow may still happen due to the relatively slow system bus or
670+ * CPU not working fast enough, so the write-then-read algo implemented
671+ * here just won't keep up with the SPI bus data transfer. Such
672+ * situation is highly platform specific and is supposed to be fixed by
673+ * manually restricting the SPI bus frequency using the
674+ * dws->max_mem_freq parameter.
675+ */
676+ local_irq_save (flags );
677+ preempt_disable ();
678+
679+ ret = dw_spi_write_then_read (dws , mem -> spi );
680+
681+ local_irq_restore (flags );
682+ preempt_enable ();
683+
684+ /*
685+ * Wait for the operation being finished and check the controller
686+ * status only if there hasn't been any run-time error detected. In the
687+ * former case it's just pointless. In the later one to prevent an
688+ * additional error message printing since any hw error flag being set
689+ * would be due to an error detected on the data transfer.
690+ */
691+ if (!ret ) {
692+ ret = dw_spi_wait_mem_op_done (dws );
693+ if (!ret )
694+ ret = dw_spi_check_status (dws , true);
695+ }
696+
697+ dw_spi_stop_mem_op (dws , mem -> spi );
698+
699+ dw_spi_free_mem_buf (dws );
700+
701+ return ret ;
702+ }
703+
704+ /*
705+ * Initialize the default memory operations if a glue layer hasn't specified
706+ * custom ones. Direct mapping operations will be preserved anyway since DW SPI
707+ * controller doesn't have an embedded dirmap interface. Note the memory
708+ * operations implemented in this driver is the best choice only for the DW APB
709+ * SSI controller with standard native CS functionality. If a hardware vendor
710+ * has fixed the automatic CS assertion/de-assertion peculiarity, then it will
711+ * be safer to use the normal SPI-messages-based transfers implementation.
712+ */
713+ static void dw_spi_init_mem_ops (struct dw_spi * dws )
714+ {
715+ if (!dws -> mem_ops .exec_op && !(dws -> caps & DW_SPI_CAP_CS_OVERRIDE ) &&
716+ !dws -> set_cs ) {
717+ dws -> mem_ops .adjust_op_size = dw_spi_adjust_mem_op_size ;
718+ dws -> mem_ops .supports_op = dw_spi_supports_mem_op ;
719+ dws -> mem_ops .exec_op = dw_spi_exec_mem_op ;
720+ }
721+ }
722+
425723/* This may be called twice for each spi dev */
426724static int dw_spi_setup (struct spi_device * spi )
427725{
@@ -522,6 +820,8 @@ int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
522820 goto err_free_master ;
523821 }
524822
823+ dw_spi_init_mem_ops (dws );
824+
525825 master -> use_gpio_descriptors = true;
526826 master -> mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP ;
527827 master -> bits_per_word_mask = SPI_BPW_RANGE_MASK (4 , 16 );
@@ -535,6 +835,7 @@ int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
535835 master -> set_cs = dw_spi_set_cs ;
536836 master -> transfer_one = dw_spi_transfer_one ;
537837 master -> handle_err = dw_spi_handle_err ;
838+ master -> mem_ops = & dws -> mem_ops ;
538839 master -> max_speed_hz = dws -> max_freq ;
539840 master -> dev .of_node = dev -> of_node ;
540841 master -> dev .fwnode = dev -> fwnode ;
0 commit comments