11// SPDX-License-Identifier: GPL-2.0-or-later
22/*
3- * Cadence SPI controller driver (master mode only )
3+ * Cadence SPI controller driver (master and slave mode )
44 *
55 * Copyright (C) 2008 - 2014 Xilinx, Inc.
66 *
@@ -139,17 +139,21 @@ static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)
139139/**
140140 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
141141 * @xspi: Pointer to the cdns_spi structure
142+ * @is_slave: Flag to indicate slave or master mode
143+ * * On reset the SPI controller is configured to slave or master mode.
144+ * In master mode baud rate divisor is set to 4, threshold value for TX FIFO
145+ * not full interrupt is set to 1 and size of the word to be transferred as 8 bit.
142146 *
143- * On reset the SPI controller is configured to be in master mode, baud rate
144- * divisor is set to 4, threshold value for TX FIFO not full interrupt is set
145- * to 1 and size of the word to be transferred as 8 bit.
146147 * This function initializes the SPI controller to disable and clear all the
147148 * interrupts, enable manual slave select and manual start, deselect all the
148149 * chip select lines, and enable the SPI controller.
149150 */
150- static void cdns_spi_init_hw (struct cdns_spi * xspi )
151+ static void cdns_spi_init_hw (struct cdns_spi * xspi , bool is_slave )
151152{
152- u32 ctrl_reg = CDNS_SPI_CR_DEFAULT ;
153+ u32 ctrl_reg = 0 ;
154+
155+ if (!is_slave )
156+ ctrl_reg |= CDNS_SPI_CR_DEFAULT ;
153157
154158 if (xspi -> is_decoded_cs )
155159 ctrl_reg |= CDNS_SPI_CR_PERI_SEL ;
@@ -324,6 +328,25 @@ static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi)
324328 }
325329}
326330
331+ /**
332+ * cdns_spi_read_rx_fifo - Reads the RX FIFO with as many bytes as possible
333+ * @xspi: Pointer to the cdns_spi structure
334+ * @count: Read byte count
335+ */
336+ static void cdns_spi_read_rx_fifo (struct cdns_spi * xspi , unsigned long count )
337+ {
338+ u8 data ;
339+
340+ /* Read out the data from the RX FIFO */
341+ while (count > 0 ) {
342+ data = cdns_spi_read (xspi , CDNS_SPI_RXD );
343+ if (xspi -> rxbuf )
344+ * xspi -> rxbuf ++ = data ;
345+ xspi -> rx_bytes -- ;
346+ count -- ;
347+ }
348+ }
349+
327350/**
328351 * cdns_spi_irq - Interrupt service routine of the SPI controller
329352 * @irq: IRQ number
@@ -358,27 +381,33 @@ static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
358381 spi_finalize_current_transfer (ctlr );
359382 status = IRQ_HANDLED ;
360383 } else if (intr_status & CDNS_SPI_IXR_TXOW ) {
361- unsigned long trans_cnt ;
362-
363- trans_cnt = xspi -> rx_bytes - xspi -> tx_bytes ;
384+ int trans_cnt = cdns_spi_read (xspi , CDNS_SPI_THLD );
385+ /* Set threshold to one if number of pending are
386+ * less than half fifo
387+ */
388+ if (xspi -> tx_bytes < xspi -> tx_fifo_depth >> 1 )
389+ cdns_spi_write (xspi , CDNS_SPI_THLD , 1 );
364390
365- /* Read out the data from the RX FIFO */
366391 while (trans_cnt ) {
367- u8 data ;
368-
369- data = cdns_spi_read (xspi , CDNS_SPI_RXD );
370- if (xspi -> rxbuf )
371- * xspi -> rxbuf ++ = data ;
372-
373- xspi -> rx_bytes -- ;
392+ cdns_spi_read_rx_fifo (xspi , 1 );
393+
394+ if (xspi -> tx_bytes ) {
395+ if (xspi -> txbuf )
396+ cdns_spi_write (xspi , CDNS_SPI_TXD ,
397+ * xspi -> txbuf ++ );
398+ else
399+ cdns_spi_write (xspi , CDNS_SPI_TXD , 0 );
400+ xspi -> tx_bytes -- ;
401+ }
374402 trans_cnt -- ;
375403 }
376-
377- if (xspi -> tx_bytes ) {
378- /* There is more data to send */
379- cdns_spi_fill_tx_fifo (xspi );
380- } else {
381- /* Transfer is completed */
404+ if (!xspi -> tx_bytes ) {
405+ /* Fixed delay due to controller limitation with
406+ * RX_NEMPTY incorrect status
407+ * Xilinx AR:65885 contains more details
408+ */
409+ udelay (10 );
410+ cdns_spi_read_rx_fifo (xspi , xspi -> rx_bytes );
382411 cdns_spi_write (xspi , CDNS_SPI_IDR ,
383412 CDNS_SPI_IXR_DEFAULT );
384413 spi_finalize_current_transfer (ctlr );
@@ -392,7 +421,8 @@ static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
392421static int cdns_prepare_message (struct spi_controller * ctlr ,
393422 struct spi_message * msg )
394423{
395- cdns_spi_config_clock_mode (msg -> spi );
424+ if (!spi_controller_is_slave (ctlr ))
425+ cdns_spi_config_clock_mode (msg -> spi );
396426 return 0 ;
397427}
398428
@@ -403,8 +433,9 @@ static int cdns_prepare_message(struct spi_controller *ctlr,
403433 * @transfer: Pointer to the spi_transfer structure which provides
404434 * information about next transfer parameters
405435 *
406- * This function fills the TX FIFO, starts the SPI transfer and
436+ * This function in master mode fills the TX FIFO, starts the SPI transfer and
407437 * returns a positive transfer count so that core will wait for completion.
438+ * This function in slave mode fills the TX FIFO and wait for transfer trigger.
408439 *
409440 * Return: Number of bytes transferred in the last transfer
410441 */
@@ -419,7 +450,15 @@ static int cdns_transfer_one(struct spi_controller *ctlr,
419450 xspi -> tx_bytes = transfer -> len ;
420451 xspi -> rx_bytes = transfer -> len ;
421452
422- cdns_spi_setup_transfer (spi , transfer );
453+ if (!spi_controller_is_slave (ctlr ))
454+ cdns_spi_setup_transfer (spi , transfer );
455+
456+ /* Set TX empty threshold to half of FIFO depth
457+ * only if TX bytes are more than half FIFO depth.
458+ */
459+ if (xspi -> tx_bytes > (xspi -> tx_fifo_depth >> 1 ))
460+ cdns_spi_write (xspi , CDNS_SPI_THLD , xspi -> tx_fifo_depth >> 1 );
461+
423462 cdns_spi_fill_tx_fifo (xspi );
424463 spi_transfer_delay_exec (transfer );
425464
@@ -451,20 +490,29 @@ static int cdns_prepare_transfer_hardware(struct spi_controller *ctlr)
451490 * information about the controller.
452491 *
453492 * This function disables the SPI master controller when no slave selected.
493+ * This function flush out if any pending data in FIFO.
454494 *
455495 * Return: 0 always
456496 */
457497static int cdns_unprepare_transfer_hardware (struct spi_controller * ctlr )
458498{
459499 struct cdns_spi * xspi = spi_controller_get_devdata (ctlr );
460500 u32 ctrl_reg ;
501+ unsigned int cnt = xspi -> tx_fifo_depth ;
502+
503+ if (spi_controller_is_slave (ctlr )) {
504+ while (cnt -- )
505+ cdns_spi_read (xspi , CDNS_SPI_RXD );
506+ }
461507
462508 /* Disable the SPI if slave is deselected */
463509 ctrl_reg = cdns_spi_read (xspi , CDNS_SPI_CR );
464510 ctrl_reg = (ctrl_reg & CDNS_SPI_CR_SSCTRL ) >> CDNS_SPI_SS_SHIFT ;
465- if (ctrl_reg == CDNS_SPI_NOSS )
511+ if (ctrl_reg == CDNS_SPI_NOSS || spi_controller_is_slave ( ctlr ) )
466512 cdns_spi_write (xspi , CDNS_SPI_ER , CDNS_SPI_ER_DISABLE );
467513
514+ /* Reset to default */
515+ cdns_spi_write (xspi , CDNS_SPI_THLD , 0x1 );
468516 return 0 ;
469517}
470518
@@ -486,6 +534,27 @@ static void cdns_spi_detect_fifo_depth(struct cdns_spi *xspi)
486534 cdns_spi_write (xspi , CDNS_SPI_THLD , 0x1 );
487535}
488536
537+ /**
538+ * cdns_slave_abort - Abort slave transfer
539+ * @ctlr: Pointer to the spi_controller structure
540+ *
541+ * This function abort slave transfer if there any transfer timeout.
542+ *
543+ * Return: 0 always
544+ */
545+ static int cdns_slave_abort (struct spi_controller * ctlr )
546+ {
547+ struct cdns_spi * xspi = spi_controller_get_devdata (ctlr );
548+ u32 intr_status ;
549+
550+ intr_status = cdns_spi_read (xspi , CDNS_SPI_ISR );
551+ cdns_spi_write (xspi , CDNS_SPI_ISR , intr_status );
552+ cdns_spi_write (xspi , CDNS_SPI_IDR , (CDNS_SPI_IXR_MODF | CDNS_SPI_IXR_RXNEMTY ));
553+ spi_finalize_current_transfer (ctlr );
554+
555+ return 0 ;
556+ }
557+
489558/**
490559 * cdns_spi_probe - Probe method for the SPI driver
491560 * @pdev: Pointer to the platform_device structure
@@ -500,8 +569,14 @@ static int cdns_spi_probe(struct platform_device *pdev)
500569 struct spi_controller * ctlr ;
501570 struct cdns_spi * xspi ;
502571 u32 num_cs ;
572+ bool slave ;
573+
574+ slave = of_property_read_bool (pdev -> dev .of_node , "spi-slave" );
575+ if (slave )
576+ ctlr = spi_alloc_slave (& pdev -> dev , sizeof (* xspi ));
577+ else
578+ ctlr = spi_alloc_master (& pdev -> dev , sizeof (* xspi ));
503579
504- ctlr = spi_alloc_master (& pdev -> dev , sizeof (* xspi ));
505580 if (!ctlr )
506581 return - ENOMEM ;
507582
@@ -522,46 +597,48 @@ static int cdns_spi_probe(struct platform_device *pdev)
522597 goto remove_ctlr ;
523598 }
524599
525- xspi -> ref_clk = devm_clk_get (& pdev -> dev , "ref_clk" );
526- if (IS_ERR (xspi -> ref_clk )) {
527- dev_err (& pdev -> dev , "ref_clk clock not found.\n" );
528- ret = PTR_ERR (xspi -> ref_clk );
529- goto remove_ctlr ;
530- }
531-
532600 ret = clk_prepare_enable (xspi -> pclk );
533601 if (ret ) {
534602 dev_err (& pdev -> dev , "Unable to enable APB clock.\n" );
535603 goto remove_ctlr ;
536604 }
537605
538- ret = clk_prepare_enable (xspi -> ref_clk );
539- if (ret ) {
540- dev_err (& pdev -> dev , "Unable to enable device clock.\n" );
541- goto clk_dis_apb ;
542- }
606+ if (!spi_controller_is_slave (ctlr )) {
607+ xspi -> ref_clk = devm_clk_get (& pdev -> dev , "ref_clk" );
608+ if (IS_ERR (xspi -> ref_clk )) {
609+ dev_err (& pdev -> dev , "ref_clk clock not found.\n" );
610+ ret = PTR_ERR (xspi -> ref_clk );
611+ goto clk_dis_apb ;
612+ }
543613
544- pm_runtime_use_autosuspend ( & pdev -> dev );
545- pm_runtime_set_autosuspend_delay ( & pdev -> dev , SPI_AUTOSUSPEND_TIMEOUT );
546- pm_runtime_get_noresume (& pdev -> dev );
547- pm_runtime_set_active ( & pdev -> dev ) ;
548- pm_runtime_enable ( & pdev -> dev );
614+ ret = clk_prepare_enable ( xspi -> ref_clk );
615+ if ( ret ) {
616+ dev_err (& pdev -> dev , "Unable to enable device clock.\n" );
617+ goto clk_dis_apb ;
618+ }
549619
550- ret = of_property_read_u32 ( pdev -> dev . of_node , "num-cs" , & num_cs );
551- if ( ret < 0 )
552- ctlr -> num_chipselect = CDNS_SPI_DEFAULT_NUM_CS ;
553- else
554- ctlr -> num_chipselect = num_cs ;
620+ pm_runtime_use_autosuspend ( & pdev -> dev );
621+ pm_runtime_set_autosuspend_delay ( & pdev -> dev , SPI_AUTOSUSPEND_TIMEOUT );
622+ pm_runtime_get_noresume ( & pdev -> dev ) ;
623+ pm_runtime_set_active ( & pdev -> dev );
624+ pm_runtime_enable ( & pdev -> dev ) ;
555625
556- ret = of_property_read_u32 (pdev -> dev .of_node , "is-decoded-cs" ,
557- & xspi -> is_decoded_cs );
558- if (ret < 0 )
559- xspi -> is_decoded_cs = 0 ;
626+ ret = of_property_read_u32 (pdev -> dev .of_node , "num-cs" , & num_cs );
627+ if (ret < 0 )
628+ ctlr -> num_chipselect = CDNS_SPI_DEFAULT_NUM_CS ;
629+ else
630+ ctlr -> num_chipselect = num_cs ;
631+
632+ ret = of_property_read_u32 (pdev -> dev .of_node , "is-decoded-cs" ,
633+ & xspi -> is_decoded_cs );
634+ if (ret < 0 )
635+ xspi -> is_decoded_cs = 0 ;
636+ }
560637
561638 cdns_spi_detect_fifo_depth (xspi );
562639
563640 /* SPI controller initializations */
564- cdns_spi_init_hw (xspi );
641+ cdns_spi_init_hw (xspi , spi_controller_is_slave ( ctlr ) );
565642
566643 irq = platform_get_irq (pdev , 0 );
567644 if (irq <= 0 ) {
@@ -582,20 +659,23 @@ static int cdns_spi_probe(struct platform_device *pdev)
582659 ctlr -> prepare_message = cdns_prepare_message ;
583660 ctlr -> transfer_one = cdns_transfer_one ;
584661 ctlr -> unprepare_transfer_hardware = cdns_unprepare_transfer_hardware ;
585- ctlr -> set_cs = cdns_spi_chipselect ;
586- ctlr -> auto_runtime_pm = true;
587- ctlr -> mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH ;
588-
589- xspi -> clk_rate = clk_get_rate (xspi -> ref_clk );
590- /* Set to default valid value */
591- ctlr -> max_speed_hz = xspi -> clk_rate / 4 ;
592- xspi -> speed_hz = ctlr -> max_speed_hz ;
593-
662+ ctlr -> mode_bits = SPI_CPOL | SPI_CPHA ;
594663 ctlr -> bits_per_word_mask = SPI_BPW_MASK (8 );
595664
596- pm_runtime_mark_last_busy (& pdev -> dev );
597- pm_runtime_put_autosuspend (& pdev -> dev );
598-
665+ if (!spi_controller_is_slave (ctlr )) {
666+ ctlr -> mode_bits |= SPI_CS_HIGH ;
667+ ctlr -> set_cs = cdns_spi_chipselect ;
668+ ctlr -> auto_runtime_pm = true;
669+ xspi -> clk_rate = clk_get_rate (xspi -> ref_clk );
670+ /* Set to default valid value */
671+ ctlr -> max_speed_hz = xspi -> clk_rate / 4 ;
672+ xspi -> speed_hz = ctlr -> max_speed_hz ;
673+ pm_runtime_mark_last_busy (& pdev -> dev );
674+ pm_runtime_put_autosuspend (& pdev -> dev );
675+ } else {
676+ ctlr -> mode_bits |= SPI_NO_CS ;
677+ ctlr -> slave_abort = cdns_slave_abort ;
678+ }
599679 ret = spi_register_controller (ctlr );
600680 if (ret ) {
601681 dev_err (& pdev -> dev , "spi_register_controller failed\n" );
@@ -605,9 +685,11 @@ static int cdns_spi_probe(struct platform_device *pdev)
605685 return ret ;
606686
607687clk_dis_all :
608- pm_runtime_set_suspended (& pdev -> dev );
609- pm_runtime_disable (& pdev -> dev );
610- clk_disable_unprepare (xspi -> ref_clk );
688+ if (!spi_controller_is_slave (ctlr )) {
689+ pm_runtime_set_suspended (& pdev -> dev );
690+ pm_runtime_disable (& pdev -> dev );
691+ clk_disable_unprepare (xspi -> ref_clk );
692+ }
611693clk_dis_apb :
612694 clk_disable_unprepare (xspi -> pclk );
613695remove_ctlr :
@@ -669,7 +751,7 @@ static int __maybe_unused cdns_spi_resume(struct device *dev)
669751 struct spi_controller * ctlr = dev_get_drvdata (dev );
670752 struct cdns_spi * xspi = spi_controller_get_devdata (ctlr );
671753
672- cdns_spi_init_hw (xspi );
754+ cdns_spi_init_hw (xspi , spi_controller_is_slave ( ctlr ) );
673755 return spi_controller_resume (ctlr );
674756}
675757
0 commit comments