117
117
#define STM32H7_SPI_CFG2_CPHA BIT(24)
118
118
#define STM32H7_SPI_CFG2_CPOL BIT(25)
119
119
#define STM32H7_SPI_CFG2_SSM BIT(26)
120
+ #define STM32H7_SPI_CFG2_SSIOP BIT(28)
120
121
#define STM32H7_SPI_CFG2_AFCNTR BIT(31)
121
122
122
123
/* STM32H7_SPI_IER bit fields */
170
171
*/
171
172
#define SPI_DMA_MIN_BYTES 16
172
173
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
+
173
178
/**
174
179
* struct stm32_spi_reg - stm32 SPI register & bitfield desc
175
180
* @reg: register offset
@@ -190,6 +195,7 @@ struct stm32_spi_reg {
190
195
* @cpol: clock polarity register and polarity bit
191
196
* @cpha: clock phase register and phase bit
192
197
* @lsb_first: LSB transmitted first register and bit
198
+ * @cs_high: chips select active value
193
199
* @br: baud rate register and bitfields
194
200
* @rx: SPI RX data register
195
201
* @tx: SPI TX data register
@@ -201,6 +207,7 @@ struct stm32_spi_regspec {
201
207
const struct stm32_spi_reg cpol ;
202
208
const struct stm32_spi_reg cpha ;
203
209
const struct stm32_spi_reg lsb_first ;
210
+ const struct stm32_spi_reg cs_high ;
204
211
const struct stm32_spi_reg br ;
205
212
const struct stm32_spi_reg rx ;
206
213
const struct stm32_spi_reg tx ;
@@ -280,6 +287,7 @@ struct stm32_spi_cfg {
280
287
* @dma_tx: dma channel for TX transfer
281
288
* @dma_rx: dma channel for RX transfer
282
289
* @phys_addr: SPI registers physical base address
290
+ * @device_mode: the controller is configured as SPI device
283
291
*/
284
292
struct stm32_spi {
285
293
struct device * dev ;
@@ -307,6 +315,8 @@ struct stm32_spi {
307
315
struct dma_chan * dma_tx ;
308
316
struct dma_chan * dma_rx ;
309
317
dma_addr_t phys_addr ;
318
+
319
+ bool device_mode ;
310
320
};
311
321
312
322
static const struct stm32_spi_regspec stm32f4_spi_regspec = {
@@ -318,6 +328,7 @@ static const struct stm32_spi_regspec stm32f4_spi_regspec = {
318
328
.cpol = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_CPOL },
319
329
.cpha = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_CPHA },
320
330
.lsb_first = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_LSBFRST },
331
+ .cs_high = {},
321
332
.br = { STM32F4_SPI_CR1 , STM32F4_SPI_CR1_BR , STM32F4_SPI_CR1_BR_SHIFT },
322
333
323
334
.rx = { STM32F4_SPI_DR },
@@ -336,6 +347,7 @@ static const struct stm32_spi_regspec stm32h7_spi_regspec = {
336
347
.cpol = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_CPOL },
337
348
.cpha = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_CPHA },
338
349
.lsb_first = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_LSBFRST },
350
+ .cs_high = { STM32H7_SPI_CFG2 , STM32H7_SPI_CFG2_SSIOP },
339
351
.br = { STM32H7_SPI_CFG1 , STM32H7_SPI_CFG1_MBR ,
340
352
STM32H7_SPI_CFG1_MBR_SHIFT },
341
353
@@ -971,6 +983,11 @@ static int stm32_spi_prepare_msg(struct spi_controller *ctrl,
971
983
else
972
984
clrb |= spi -> cfg -> regs -> lsb_first .mask ;
973
985
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
+
974
991
dev_dbg (spi -> dev , "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n" ,
975
992
!!(spi_dev -> mode & SPI_CPOL ),
976
993
!!(spi_dev -> mode & SPI_CPHA ),
@@ -1161,7 +1178,8 @@ static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
1161
1178
if (spi -> tx_buf )
1162
1179
stm32h7_spi_write_txfifo (spi );
1163
1180
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 );
1165
1183
1166
1184
writel_relaxed (ier , spi -> base + STM32H7_SPI_IER );
1167
1185
@@ -1208,7 +1226,8 @@ static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1208
1226
1209
1227
stm32_spi_enable (spi );
1210
1228
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 );
1212
1231
}
1213
1232
1214
1233
/**
@@ -1536,16 +1555,18 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1536
1555
spi -> cfg -> set_bpw (spi );
1537
1556
1538
1557
/* 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
+ }
1546
1566
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
+ }
1549
1570
1550
1571
comm_type = stm32_spi_communication_type (spi_dev , transfer );
1551
1572
ret = spi -> cfg -> set_mode (spi , comm_type );
@@ -1554,7 +1575,7 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1554
1575
1555
1576
spi -> cur_comm = comm_type ;
1556
1577
1557
- if (spi -> cfg -> set_data_idleness )
1578
+ if (STM32_SPI_MASTER_MODE ( spi ) && spi -> cfg -> set_data_idleness )
1558
1579
spi -> cfg -> set_data_idleness (spi , transfer -> len );
1559
1580
1560
1581
if (spi -> cur_bpw <= 8 )
@@ -1575,7 +1596,8 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1575
1596
dev_dbg (spi -> dev ,
1576
1597
"data frame of %d-bit, data packet of %d data frames\n" ,
1577
1598
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 );
1579
1601
dev_dbg (spi -> dev , "transfer of %d bytes (%d data frames)\n" ,
1580
1602
spi -> cur_xferlen , nb_words );
1581
1603
dev_dbg (spi -> dev , "dma %s\n" ,
@@ -1670,37 +1692,42 @@ static int stm32f4_spi_config(struct stm32_spi *spi)
1670
1692
}
1671
1693
1672
1694
/**
1673
- * stm32h7_spi_config - Configure SPI controller as SPI master
1695
+ * stm32h7_spi_config - Configure SPI controller
1674
1696
* @spi: pointer to the spi controller data structure
1675
1697
*/
1676
1698
static int stm32h7_spi_config (struct stm32_spi * spi )
1677
1699
{
1678
1700
unsigned long flags ;
1701
+ u32 cr1 = 0 , cfg2 = 0 ;
1679
1702
1680
1703
spin_lock_irqsave (& spi -> lock , flags );
1681
1704
1682
1705
/* Ensure I2SMOD bit is kept cleared */
1683
1706
stm32_spi_clr_bits (spi , STM32H7_SPI_I2SCFGR ,
1684
1707
STM32H7_SPI_I2SCFGR_I2SMOD );
1685
1708
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 ;
1694
1719
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 );
1704
1731
1705
1732
spin_unlock_irqrestore (& spi -> lock , flags );
1706
1733
@@ -1756,24 +1783,38 @@ static const struct of_device_id stm32_spi_of_match[] = {
1756
1783
};
1757
1784
MODULE_DEVICE_TABLE (of , stm32_spi_of_match );
1758
1785
1786
+ static int stm32h7_spi_device_abort (struct spi_controller * ctrl )
1787
+ {
1788
+ spi_finalize_current_transfer (ctrl );
1789
+ return 0 ;
1790
+ }
1791
+
1759
1792
static int stm32_spi_probe (struct platform_device * pdev )
1760
1793
{
1761
1794
struct spi_controller * ctrl ;
1762
1795
struct stm32_spi * spi ;
1763
1796
struct resource * res ;
1764
1797
struct reset_control * rst ;
1798
+ struct device_node * np = pdev -> dev .of_node ;
1799
+ bool device_mode ;
1765
1800
int ret ;
1766
1801
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 ));
1768
1808
if (!ctrl ) {
1769
- dev_err (& pdev -> dev , "spi master allocation failed\n" );
1809
+ dev_err (& pdev -> dev , "spi controller allocation failed\n" );
1770
1810
return - ENOMEM ;
1771
1811
}
1772
1812
platform_set_drvdata (pdev , ctrl );
1773
1813
1774
1814
spi = spi_controller_get_devdata (ctrl );
1775
1815
spi -> dev = & pdev -> dev ;
1776
1816
spi -> ctrl = ctrl ;
1817
+ spi -> device_mode = device_mode ;
1777
1818
spin_lock_init (& spi -> lock );
1778
1819
1779
1820
spi -> cfg = (const struct stm32_spi_cfg * )
@@ -1856,6 +1897,8 @@ static int stm32_spi_probe(struct platform_device *pdev)
1856
1897
ctrl -> transfer_one = stm32_spi_transfer_one ;
1857
1898
ctrl -> unprepare_message = stm32_spi_unprepare_msg ;
1858
1899
ctrl -> flags = spi -> cfg -> flags ;
1900
+ if (STM32_SPI_DEVICE_MODE (spi ))
1901
+ ctrl -> slave_abort = stm32h7_spi_device_abort ;
1859
1902
1860
1903
spi -> dma_tx = dma_request_chan (spi -> dev , "tx" );
1861
1904
if (IS_ERR (spi -> dma_tx )) {
@@ -1901,7 +1944,8 @@ static int stm32_spi_probe(struct platform_device *pdev)
1901
1944
pm_runtime_mark_last_busy (& pdev -> dev );
1902
1945
pm_runtime_put_autosuspend (& pdev -> dev );
1903
1946
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" );
1905
1949
1906
1950
return 0 ;
1907
1951
0 commit comments