117117#define STM32H7_SPI_CFG2_CPHA BIT(24)
118118#define STM32H7_SPI_CFG2_CPOL BIT(25)
119119#define STM32H7_SPI_CFG2_SSM BIT(26)
120+ #define STM32H7_SPI_CFG2_SSIOP BIT(28)
120121#define STM32H7_SPI_CFG2_AFCNTR BIT(31)
121122
122123/* STM32H7_SPI_IER bit fields */
170171 */
171172#define SPI_DMA_MIN_BYTES 16
172173
174+ /* STM32 SPI driver helpers */
175+ #define STM32_SPI_MASTER_MODE (stm32_spi ) (!(stm32_spi)->device_mode)
176+ #define STM32_SPI_DEVICE_MODE (stm32_spi ) ((stm32_spi)->device_mode)
177+
173178/**
174179 * struct stm32_spi_reg - stm32 SPI register & bitfield desc
175180 * @reg: register offset
@@ -190,6 +195,7 @@ struct stm32_spi_reg {
190195 * @cpol: clock polarity register and polarity bit
191196 * @cpha: clock phase register and phase bit
192197 * @lsb_first: LSB transmitted first register and bit
198+ * @cs_high: chips select active value
193199 * @br: baud rate register and bitfields
194200 * @rx: SPI RX data register
195201 * @tx: SPI TX data register
@@ -201,6 +207,7 @@ struct stm32_spi_regspec {
201207 const struct stm32_spi_reg cpol ;
202208 const struct stm32_spi_reg cpha ;
203209 const struct stm32_spi_reg lsb_first ;
210+ const struct stm32_spi_reg cs_high ;
204211 const struct stm32_spi_reg br ;
205212 const struct stm32_spi_reg rx ;
206213 const struct stm32_spi_reg tx ;
@@ -280,6 +287,7 @@ struct stm32_spi_cfg {
280287 * @dma_tx: dma channel for TX transfer
281288 * @dma_rx: dma channel for RX transfer
282289 * @phys_addr: SPI registers physical base address
290+ * @device_mode: the controller is configured as SPI device
283291 */
284292struct stm32_spi {
285293 struct device * dev ;
@@ -307,6 +315,8 @@ struct stm32_spi {
307315 struct dma_chan * dma_tx ;
308316 struct dma_chan * dma_rx ;
309317 dma_addr_t phys_addr ;
318+
319+ bool device_mode ;
310320};
311321
312322static const struct stm32_spi_regspec stm32f4_spi_regspec = {
@@ -318,6 +328,7 @@ static const struct stm32_spi_regspec stm32f4_spi_regspec = {
318328 .cpol = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_CPOL },
319329 .cpha = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_CPHA },
320330 .lsb_first = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_LSBFRST },
331+ .cs_high = {},
321332 .br = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_BR , STM32F4_SPI_CR1_BR_SHIFT },
322333
323334 .rx = { STM32F4_SPI_DR },
@@ -336,6 +347,7 @@ static const struct stm32_spi_regspec stm32h7_spi_regspec = {
336347 .cpol = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_CPOL },
337348 .cpha = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_CPHA },
338349 .lsb_first = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_LSBFRST },
350+ .cs_high = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_SSIOP },
339351 .br = { STM32H7_SPI_CFG1 , STM32H7_SPI_CFG1_MBR ,
340352 STM32H7_SPI_CFG1_MBR_SHIFT },
341353
@@ -971,6 +983,11 @@ static int stm32_spi_prepare_msg(struct spi_controller *ctrl,
971983 else
972984 clrb |= spi -> cfg -> regs -> lsb_first .mask ;
973985
986+ if (STM32_SPI_DEVICE_MODE (spi ) && spi_dev -> mode & SPI_CS_HIGH )
987+ setb |= spi -> cfg -> regs -> cs_high .mask ;
988+ else
989+ clrb |= spi -> cfg -> regs -> cs_high .mask ;
990+
974991 dev_dbg (spi -> dev , "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n" ,
975992 !!(spi_dev -> mode & SPI_CPOL ),
976993 !!(spi_dev -> mode & SPI_CPHA ),
@@ -1161,7 +1178,8 @@ static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
11611178 if (spi -> tx_buf )
11621179 stm32h7_spi_write_txfifo (spi );
11631180
1164- stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , STM32H7_SPI_CR1_CSTART );
1181+ if (STM32_SPI_MASTER_MODE (spi ))
1182+ stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , STM32H7_SPI_CR1_CSTART );
11651183
11661184 writel_relaxed (ier , spi -> base + STM32H7_SPI_IER );
11671185
@@ -1208,7 +1226,8 @@ static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
12081226
12091227 stm32_spi_enable (spi );
12101228
1211- stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , STM32H7_SPI_CR1_CSTART );
1229+ if (STM32_SPI_MASTER_MODE (spi ))
1230+ stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , STM32H7_SPI_CR1_CSTART );
12121231}
12131232
12141233/**
@@ -1536,16 +1555,18 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
15361555 spi -> cfg -> set_bpw (spi );
15371556
15381557 /* Update spi->cur_speed with real clock speed */
1539- mbr = stm32_spi_prepare_mbr (spi , transfer -> speed_hz ,
1540- spi -> cfg -> baud_rate_div_min ,
1541- spi -> cfg -> baud_rate_div_max );
1542- if (mbr < 0 ) {
1543- ret = mbr ;
1544- goto out ;
1545- }
1558+ if (STM32_SPI_MASTER_MODE (spi )) {
1559+ mbr = stm32_spi_prepare_mbr (spi , transfer -> speed_hz ,
1560+ spi -> cfg -> baud_rate_div_min ,
1561+ spi -> cfg -> baud_rate_div_max );
1562+ if (mbr < 0 ) {
1563+ ret = mbr ;
1564+ goto out ;
1565+ }
15461566
1547- transfer -> speed_hz = spi -> cur_speed ;
1548- stm32_spi_set_mbr (spi , mbr );
1567+ transfer -> speed_hz = spi -> cur_speed ;
1568+ stm32_spi_set_mbr (spi , mbr );
1569+ }
15491570
15501571 comm_type = stm32_spi_communication_type (spi_dev , transfer );
15511572 ret = spi -> cfg -> set_mode (spi , comm_type );
@@ -1554,7 +1575,7 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
15541575
15551576 spi -> cur_comm = comm_type ;
15561577
1557- if (spi -> cfg -> set_data_idleness )
1578+ if (STM32_SPI_MASTER_MODE ( spi ) && spi -> cfg -> set_data_idleness )
15581579 spi -> cfg -> set_data_idleness (spi , transfer -> len );
15591580
15601581 if (spi -> cur_bpw <= 8 )
@@ -1575,7 +1596,8 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
15751596 dev_dbg (spi -> dev ,
15761597 "data frame of %d-bit, data packet of %d data frames\n" ,
15771598 spi -> cur_bpw , spi -> cur_fthlv );
1578- dev_dbg (spi -> dev , "speed set to %dHz\n" , spi -> cur_speed );
1599+ if (STM32_SPI_MASTER_MODE (spi ))
1600+ dev_dbg (spi -> dev , "speed set to %dHz\n" , spi -> cur_speed );
15791601 dev_dbg (spi -> dev , "transfer of %d bytes (%d data frames)\n" ,
15801602 spi -> cur_xferlen , nb_words );
15811603 dev_dbg (spi -> dev , "dma %s\n" ,
@@ -1670,37 +1692,42 @@ static int stm32f4_spi_config(struct stm32_spi *spi)
16701692}
16711693
16721694/**
1673- * stm32h7_spi_config - Configure SPI controller as SPI master
1695+ * stm32h7_spi_config - Configure SPI controller
16741696 * @spi: pointer to the spi controller data structure
16751697 */
16761698static int stm32h7_spi_config (struct stm32_spi * spi )
16771699{
16781700 unsigned long flags ;
1701+ u32 cr1 = 0 , cfg2 = 0 ;
16791702
16801703 spin_lock_irqsave (& spi -> lock , flags );
16811704
16821705 /* Ensure I2SMOD bit is kept cleared */
16831706 stm32_spi_clr_bits (spi , STM32H7_SPI_I2SCFGR ,
16841707 STM32H7_SPI_I2SCFGR_I2SMOD );
16851708
1686- /*
1687- * - SS input value high
1688- * - transmitter half duplex direction
1689- * - automatic communication suspend when RX-Fifo is full
1690- */
1691- stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , STM32H7_SPI_CR1_SSI |
1692- STM32H7_SPI_CR1_HDDIR |
1693- STM32H7_SPI_CR1_MASRX );
1709+ if (STM32_SPI_DEVICE_MODE (spi )) {
1710+ /* Use native device select */
1711+ cfg2 &= ~STM32H7_SPI_CFG2_SSM ;
1712+ } else {
1713+ /*
1714+ * - Transmitter half duplex direction
1715+ * - Automatic communication suspend when RX-Fifo is full
1716+ * - SS input value high
1717+ */
1718+ cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI ;
16941719
1695- /*
1696- * - Set the master mode (default Motorola mode)
1697- * - Consider 1 master/n slaves configuration and
1698- * SS input value is determined by the SSI bit
1699- * - keep control of all associated GPIOs
1700- */
1701- stm32_spi_set_bits (spi , STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_MASTER |
1702- STM32H7_SPI_CFG2_SSM |
1703- STM32H7_SPI_CFG2_AFCNTR );
1720+ /*
1721+ * - Set the master mode (default Motorola mode)
1722+ * - Consider 1 master/n devices configuration and
1723+ * SS input value is determined by the SSI bit
1724+ * - keep control of all associated GPIOs
1725+ */
1726+ cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR ;
1727+ }
1728+
1729+ stm32_spi_set_bits (spi , STM32H7_SPI_CR1 , cr1 );
1730+ stm32_spi_set_bits (spi , STM32H7_SPI_CFG2 , cfg2 );
17041731
17051732 spin_unlock_irqrestore (& spi -> lock , flags );
17061733
@@ -1756,24 +1783,38 @@ static const struct of_device_id stm32_spi_of_match[] = {
17561783};
17571784MODULE_DEVICE_TABLE (of , stm32_spi_of_match );
17581785
1786+ static int stm32h7_spi_device_abort (struct spi_controller * ctrl )
1787+ {
1788+ spi_finalize_current_transfer (ctrl );
1789+ return 0 ;
1790+ }
1791+
17591792static int stm32_spi_probe (struct platform_device * pdev )
17601793{
17611794 struct spi_controller * ctrl ;
17621795 struct stm32_spi * spi ;
17631796 struct resource * res ;
17641797 struct reset_control * rst ;
1798+ struct device_node * np = pdev -> dev .of_node ;
1799+ bool device_mode ;
17651800 int ret ;
17661801
1767- ctrl = devm_spi_alloc_master (& pdev -> dev , sizeof (struct stm32_spi ));
1802+ device_mode = of_property_read_bool (np , "spi-slave" );
1803+
1804+ if (device_mode )
1805+ ctrl = devm_spi_alloc_slave (& pdev -> dev , sizeof (struct stm32_spi ));
1806+ else
1807+ ctrl = devm_spi_alloc_master (& pdev -> dev , sizeof (struct stm32_spi ));
17681808 if (!ctrl ) {
1769- dev_err (& pdev -> dev , "spi master allocation failed\n" );
1809+ dev_err (& pdev -> dev , "spi controller allocation failed\n" );
17701810 return - ENOMEM ;
17711811 }
17721812 platform_set_drvdata (pdev , ctrl );
17731813
17741814 spi = spi_controller_get_devdata (ctrl );
17751815 spi -> dev = & pdev -> dev ;
17761816 spi -> ctrl = ctrl ;
1817+ spi -> device_mode = device_mode ;
17771818 spin_lock_init (& spi -> lock );
17781819
17791820 spi -> cfg = (const struct stm32_spi_cfg * )
@@ -1856,6 +1897,8 @@ static int stm32_spi_probe(struct platform_device *pdev)
18561897 ctrl -> transfer_one = stm32_spi_transfer_one ;
18571898 ctrl -> unprepare_message = stm32_spi_unprepare_msg ;
18581899 ctrl -> flags = spi -> cfg -> flags ;
1900+ if (STM32_SPI_DEVICE_MODE (spi ))
1901+ ctrl -> slave_abort = stm32h7_spi_device_abort ;
18591902
18601903 spi -> dma_tx = dma_request_chan (spi -> dev , "tx" );
18611904 if (IS_ERR (spi -> dma_tx )) {
@@ -1901,7 +1944,8 @@ static int stm32_spi_probe(struct platform_device *pdev)
19011944 pm_runtime_mark_last_busy (& pdev -> dev );
19021945 pm_runtime_put_autosuspend (& pdev -> dev );
19031946
1904- dev_info (& pdev -> dev , "driver initialized\n" );
1947+ dev_info (& pdev -> dev , "driver initialized (%s mode)\n" ,
1948+ STM32_SPI_MASTER_MODE (spi ) ? "master" : "device" );
19051949
19061950 return 0 ;
19071951
0 commit comments