1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2725b0e3eSSerge Semin #ifndef __SPI_DW_H__ 3725b0e3eSSerge Semin #define __SPI_DW_H__ 4ca632f55SGrant Likely 5cc760f31SSerge Semin #include <linux/bits.h> 6bdbdf0f0SSerge Semin #include <linux/completion.h> 78378449dSSerge Semin #include <linux/debugfs.h> 8e62a15d9SAndy Shevchenko #include <linux/irqreturn.h> 9ca632f55SGrant Likely #include <linux/io.h> 10ca632f55SGrant Likely #include <linux/scatterlist.h> 116423207eSSerge Semin #include <linux/spi/spi-mem.h> 12a51acc24SDamien Le Moal #include <linux/bitfield.h> 13ca632f55SGrant Likely 142cc8d922SSerge Semin /* Synopsys DW SSI IP-core virtual IDs */ 152cc8d922SSerge Semin #define DW_PSSI_ID 0 162cc8d922SSerge Semin #define DW_HSSI_ID 1 172cc8d922SSerge Semin 182cc8d922SSerge Semin /* Synopsys DW SSI component versions (FourCC sequence) */ 192cc8d922SSerge Semin #define DW_HSSI_102A 0x3130322a 202cc8d922SSerge Semin 212cc8d922SSerge Semin /* DW SSI IP-core ID and version check helpers */ 222cc8d922SSerge Semin #define dw_spi_ip_is(_dws, _ip) \ 232cc8d922SSerge Semin ((_dws)->ip == DW_ ## _ip ## _ID) 242cc8d922SSerge Semin 252cc8d922SSerge Semin #define __dw_spi_ver_cmp(_dws, _ip, _ver, _op) \ 265d76b750SNandhini Srikandan (dw_spi_ip_is(_dws, _ip) && (_dws)->ver _op DW_ ## _ip ## _ ## _ver) 272cc8d922SSerge Semin 282cc8d922SSerge Semin #define dw_spi_ver_is(_dws, _ip, _ver) __dw_spi_ver_cmp(_dws, _ip, _ver, ==) 292cc8d922SSerge Semin 302cc8d922SSerge Semin #define dw_spi_ver_is_ge(_dws, _ip, _ver) __dw_spi_ver_cmp(_dws, _ip, _ver, >=) 312cc8d922SSerge Semin 322cc8d922SSerge Semin /* DW SPI controller capabilities */ 332cc8d922SSerge Semin #define DW_SPI_CAP_CS_OVERRIDE BIT(0) 34dc4e6d9fSNandhini Srikandan #define DW_SPI_CAP_DFS32 BIT(1) 352cc8d922SSerge Semin 36725b0e3eSSerge Semin /* Register offsets (Generic for both DWC APB SSI and DWC SSI IP-cores) */ 37299cb65cSWan Ahmad Zainie #define DW_SPI_CTRLR0 0x00 38299cb65cSWan Ahmad Zainie #define DW_SPI_CTRLR1 0x04 397eb187b3SH Hartley Sweeten #define DW_SPI_SSIENR 0x08 407eb187b3SH Hartley Sweeten #define DW_SPI_MWCR 0x0c 417eb187b3SH Hartley Sweeten #define DW_SPI_SER 0x10 427eb187b3SH Hartley Sweeten #define DW_SPI_BAUDR 0x14 43299cb65cSWan Ahmad Zainie #define DW_SPI_TXFTLR 0x18 44299cb65cSWan Ahmad Zainie #define DW_SPI_RXFTLR 0x1c 457eb187b3SH Hartley Sweeten #define DW_SPI_TXFLR 0x20 467eb187b3SH Hartley Sweeten #define DW_SPI_RXFLR 0x24 477eb187b3SH Hartley Sweeten #define DW_SPI_SR 0x28 487eb187b3SH Hartley Sweeten #define DW_SPI_IMR 0x2c 497eb187b3SH Hartley Sweeten #define DW_SPI_ISR 0x30 507eb187b3SH Hartley Sweeten #define DW_SPI_RISR 0x34 517eb187b3SH Hartley Sweeten #define DW_SPI_TXOICR 0x38 527eb187b3SH Hartley Sweeten #define DW_SPI_RXOICR 0x3c 537eb187b3SH Hartley Sweeten #define DW_SPI_RXUICR 0x40 547eb187b3SH Hartley Sweeten #define DW_SPI_MSTICR 0x44 557eb187b3SH Hartley Sweeten #define DW_SPI_ICR 0x48 567eb187b3SH Hartley Sweeten #define DW_SPI_DMACR 0x4c 577eb187b3SH Hartley Sweeten #define DW_SPI_DMATDLR 0x50 587eb187b3SH Hartley Sweeten #define DW_SPI_DMARDLR 0x54 597eb187b3SH Hartley Sweeten #define DW_SPI_IDR 0x58 607eb187b3SH Hartley Sweeten #define DW_SPI_VERSION 0x5c 617eb187b3SH Hartley Sweeten #define DW_SPI_DR 0x60 62bac70b54SLars Povlsen #define DW_SPI_RX_SAMPLE_DLY 0xf0 63f2d70479STalel Shenhar #define DW_SPI_CS_OVERRIDE 0xf4 647eb187b3SH Hartley Sweeten 65725b0e3eSSerge Semin /* Bit fields in CTRLR0 (DWC APB SSI) */ 66725b0e3eSSerge Semin #define DW_PSSI_CTRLR0_DFS_MASK GENMASK(3, 0) 67ec77c086SSerge Semin #define DW_PSSI_CTRLR0_DFS32_MASK GENMASK(20, 16) 68ca632f55SGrant Likely 69ec77c086SSerge Semin #define DW_PSSI_CTRLR0_FRF_MASK GENMASK(5, 4) 70725b0e3eSSerge Semin #define DW_SPI_CTRLR0_FRF_MOTO_SPI 0x0 71725b0e3eSSerge Semin #define DW_SPI_CTRLR0_FRF_TI_SSP 0x1 72725b0e3eSSerge Semin #define DW_SPI_CTRLR0_FRF_NS_MICROWIRE 0x2 73725b0e3eSSerge Semin #define DW_SPI_CTRLR0_FRF_RESV 0x3 74ca632f55SGrant Likely 75ec77c086SSerge Semin #define DW_PSSI_CTRLR0_MODE_MASK GENMASK(7, 6) 76ec77c086SSerge Semin #define DW_PSSI_CTRLR0_SCPHA BIT(6) 77ec77c086SSerge Semin #define DW_PSSI_CTRLR0_SCPOL BIT(7) 78ca632f55SGrant Likely 79ec77c086SSerge Semin #define DW_PSSI_CTRLR0_TMOD_MASK GENMASK(9, 8) 80725b0e3eSSerge Semin #define DW_SPI_CTRLR0_TMOD_TR 0x0 /* xmit & recv */ 81725b0e3eSSerge Semin #define DW_SPI_CTRLR0_TMOD_TO 0x1 /* xmit only */ 82725b0e3eSSerge Semin #define DW_SPI_CTRLR0_TMOD_RO 0x2 /* recv only */ 83725b0e3eSSerge Semin #define DW_SPI_CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */ 84ca632f55SGrant Likely 85ec77c086SSerge Semin #define DW_PSSI_CTRLR0_SLV_OE BIT(10) 86ec77c086SSerge Semin #define DW_PSSI_CTRLR0_SRL BIT(11) 87ec77c086SSerge Semin #define DW_PSSI_CTRLR0_CFS BIT(12) 88ca632f55SGrant Likely 89725b0e3eSSerge Semin /* Bit fields in CTRLR0 (DWC SSI with AHB interface) */ 90ec77c086SSerge Semin #define DW_HSSI_CTRLR0_DFS_MASK GENMASK(4, 0) 91ec77c086SSerge Semin #define DW_HSSI_CTRLR0_FRF_MASK GENMASK(7, 6) 92ec77c086SSerge Semin #define DW_HSSI_CTRLR0_SCPHA BIT(8) 93ec77c086SSerge Semin #define DW_HSSI_CTRLR0_SCPOL BIT(9) 94725b0e3eSSerge Semin #define DW_HSSI_CTRLR0_TMOD_MASK GENMASK(11, 10) 95ec77c086SSerge Semin #define DW_HSSI_CTRLR0_SRL BIT(13) 9651e41dc2SNandhini Srikandan #define DW_HSSI_CTRLR0_MST BIT(31) 97ffb7ca54SSerge Semin 986423207eSSerge Semin /* Bit fields in CTRLR1 */ 99725b0e3eSSerge Semin #define DW_SPI_NDF_MASK GENMASK(15, 0) 1006423207eSSerge Semin 101ca632f55SGrant Likely /* Bit fields in SR, 7 bits */ 102ec77c086SSerge Semin #define DW_SPI_SR_MASK GENMASK(6, 0) 103ec77c086SSerge Semin #define DW_SPI_SR_BUSY BIT(0) 104ec77c086SSerge Semin #define DW_SPI_SR_TF_NOT_FULL BIT(1) 105ec77c086SSerge Semin #define DW_SPI_SR_TF_EMPT BIT(2) 106ec77c086SSerge Semin #define DW_SPI_SR_RF_NOT_EMPT BIT(3) 107ec77c086SSerge Semin #define DW_SPI_SR_RF_FULL BIT(4) 108ec77c086SSerge Semin #define DW_SPI_SR_TX_ERR BIT(5) 109ec77c086SSerge Semin #define DW_SPI_SR_DCOL BIT(6) 110ca632f55SGrant Likely 111ca632f55SGrant Likely /* Bit fields in ISR, IMR, RISR, 7 bits */ 112ec77c086SSerge Semin #define DW_SPI_INT_MASK GENMASK(5, 0) 113ec77c086SSerge Semin #define DW_SPI_INT_TXEI BIT(0) 114ec77c086SSerge Semin #define DW_SPI_INT_TXOI BIT(1) 115ec77c086SSerge Semin #define DW_SPI_INT_RXUI BIT(2) 116ec77c086SSerge Semin #define DW_SPI_INT_RXOI BIT(3) 117ec77c086SSerge Semin #define DW_SPI_INT_RXFI BIT(4) 118ec77c086SSerge Semin #define DW_SPI_INT_MSTI BIT(5) 119ca632f55SGrant Likely 12015ee3be7SAndy Shevchenko /* Bit fields in DMACR */ 121ec77c086SSerge Semin #define DW_SPI_DMACR_RDMAE BIT(0) 122ec77c086SSerge Semin #define DW_SPI_DMACR_TDMAE BIT(1) 12315ee3be7SAndy Shevchenko 124725b0e3eSSerge Semin /* Mem/DMA operations helpers */ 125725b0e3eSSerge Semin #define DW_SPI_WAIT_RETRIES 5 126725b0e3eSSerge Semin #define DW_SPI_BUF_SIZE \ 1276423207eSSerge Semin (sizeof_field(struct spi_mem_op, cmd.opcode) + \ 1286423207eSSerge Semin sizeof_field(struct spi_mem_op, addr.val) + 256) 129725b0e3eSSerge Semin #define DW_SPI_GET_BYTE(_val, _idx) \ 1306423207eSSerge Semin ((_val) >> (BITS_PER_BYTE * (_idx)) & 0xff) 131cf75baeaSSerge Semin 1323ff60c6bSSerge Semin /* Slave spi_transfer/spi_mem_op related */ 1333ff60c6bSSerge Semin struct dw_spi_cfg { 1343ff60c6bSSerge Semin u8 tmode; 1353ff60c6bSSerge Semin u8 dfs; 1363ff60c6bSSerge Semin u32 ndf; 1373ff60c6bSSerge Semin u32 freq; 1383ff60c6bSSerge Semin }; 1393ff60c6bSSerge Semin 140ca632f55SGrant Likely struct dw_spi; 141ca632f55SGrant Likely struct dw_spi_dma_ops { 1426370ababSAndy Shevchenko int (*dma_init)(struct device *dev, struct dw_spi *dws); 143ca632f55SGrant Likely void (*dma_exit)(struct dw_spi *dws); 144f89a6d8fSAndy Shevchenko int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer); 145721483e2SJarkko Nikula bool (*can_dma)(struct spi_controller *master, struct spi_device *spi, 146f89a6d8fSAndy Shevchenko struct spi_transfer *xfer); 147f89a6d8fSAndy Shevchenko int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer); 1484d5ac1edSAndy Shevchenko void (*dma_stop)(struct dw_spi *dws); 149ca632f55SGrant Likely }; 150ca632f55SGrant Likely 151ca632f55SGrant Likely struct dw_spi { 152721483e2SJarkko Nikula struct spi_controller *master; 153ca632f55SGrant Likely 1542cc8d922SSerge Semin u32 ip; /* Synopsys DW SSI IP-core ID */ 1552cc8d922SSerge Semin u32 ver; /* Synopsys component version */ 1562cc8d922SSerge Semin u32 caps; /* DW SPI capabilities */ 1572cc8d922SSerge Semin 158ca632f55SGrant Likely void __iomem *regs; 159ca632f55SGrant Likely unsigned long paddr; 160ca632f55SGrant Likely int irq; 161ca632f55SGrant Likely u32 fifo_len; /* depth of the FIFO buffer */ 162a51acc24SDamien Le Moal unsigned int dfs_offset; /* CTRLR0 DFS field offset */ 16384ecaf4aSSerge Semin u32 max_mem_freq; /* max mem-ops bus freq */ 164ca632f55SGrant Likely u32 max_freq; /* max bus freq supported */ 165ca632f55SGrant Likely 166c4fe57f7SMichael van der Westhuizen u32 reg_io_width; /* DR I/O width in bytes */ 167ca632f55SGrant Likely u16 bus_num; 168ca632f55SGrant Likely u16 num_cs; /* supported slave numbers */ 16962dbbae4SAlexandre Belloni void (*set_cs)(struct spi_device *spi, bool enable); 170ca632f55SGrant Likely 171ca632f55SGrant Likely /* Current message transfer state info */ 172ca632f55SGrant Likely void *tx; 1738dedbeacSSerge Semin unsigned int tx_len; 174ca632f55SGrant Likely void *rx; 1758dedbeacSSerge Semin unsigned int rx_len; 176725b0e3eSSerge Semin u8 buf[DW_SPI_BUF_SIZE]; 177ca632f55SGrant Likely int dma_mapped; 178ca632f55SGrant Likely u8 n_bytes; /* current is a 1/2 bytes op */ 179ca632f55SGrant Likely irqreturn_t (*transfer_handler)(struct dw_spi *dws); 18013b10301SMatthias Seidel u32 current_freq; /* frequency in hz */ 181bac70b54SLars Povlsen u32 cur_rx_sample_dly; 182bac70b54SLars Povlsen u32 def_rx_sample_dly_ns; 183ca632f55SGrant Likely 1846423207eSSerge Semin /* Custom memory operations */ 1856423207eSSerge Semin struct spi_controller_mem_ops mem_ops; 1866423207eSSerge Semin 187f89a6d8fSAndy Shevchenko /* DMA info */ 188ca632f55SGrant Likely struct dma_chan *txchan; 1890b2b6651SSerge Semin u32 txburst; 190ca632f55SGrant Likely struct dma_chan *rxchan; 1910b2b6651SSerge Semin u32 rxburst; 192ad4fe126SSerge Semin u32 dma_sg_burst; 193*020a3947SJoy Chakraborty u32 dma_addr_widths; 19430c8eb52SAndy Shevchenko unsigned long dma_chan_busy; 195ca632f55SGrant Likely dma_addr_t dma_addr; /* phy address of the Data register */ 1964fe338c9SJulia Lawall const struct dw_spi_dma_ops *dma_ops; 197bdbdf0f0SSerge Semin struct completion dma_completion; 198ca632f55SGrant Likely 199ca632f55SGrant Likely #ifdef CONFIG_DEBUG_FS 200ca632f55SGrant Likely struct dentry *debugfs; 2018378449dSSerge Semin struct debugfs_regset32 regset; 202ca632f55SGrant Likely #endif 203ca632f55SGrant Likely }; 204ca632f55SGrant Likely 2057eb187b3SH Hartley Sweeten static inline u32 dw_readl(struct dw_spi *dws, u32 offset) 2067eb187b3SH Hartley Sweeten { 2077eb187b3SH Hartley Sweeten return __raw_readl(dws->regs + offset); 2087eb187b3SH Hartley Sweeten } 2097eb187b3SH Hartley Sweeten 2107eb187b3SH Hartley Sweeten static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val) 2117eb187b3SH Hartley Sweeten { 2127eb187b3SH Hartley Sweeten __raw_writel(val, dws->regs + offset); 2137eb187b3SH Hartley Sweeten } 2147eb187b3SH Hartley Sweeten 215c4fe57f7SMichael van der Westhuizen static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset) 216c4fe57f7SMichael van der Westhuizen { 217c4fe57f7SMichael van der Westhuizen switch (dws->reg_io_width) { 218c4fe57f7SMichael van der Westhuizen case 2: 2197e31cea7SSerge Semin return readw_relaxed(dws->regs + offset); 220c4fe57f7SMichael van der Westhuizen case 4: 221c4fe57f7SMichael van der Westhuizen default: 2227e31cea7SSerge Semin return readl_relaxed(dws->regs + offset); 223c4fe57f7SMichael van der Westhuizen } 224c4fe57f7SMichael van der Westhuizen } 225c4fe57f7SMichael van der Westhuizen 226c4fe57f7SMichael van der Westhuizen static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val) 227c4fe57f7SMichael van der Westhuizen { 228c4fe57f7SMichael van der Westhuizen switch (dws->reg_io_width) { 229c4fe57f7SMichael van der Westhuizen case 2: 2307e31cea7SSerge Semin writew_relaxed(val, dws->regs + offset); 231c4fe57f7SMichael van der Westhuizen break; 232c4fe57f7SMichael van der Westhuizen case 4: 233c4fe57f7SMichael van der Westhuizen default: 2347e31cea7SSerge Semin writel_relaxed(val, dws->regs + offset); 235c4fe57f7SMichael van der Westhuizen break; 236c4fe57f7SMichael van der Westhuizen } 237c4fe57f7SMichael van der Westhuizen } 238c4fe57f7SMichael van der Westhuizen 239725b0e3eSSerge Semin static inline void dw_spi_enable_chip(struct dw_spi *dws, int enable) 240ca632f55SGrant Likely { 2417eb187b3SH Hartley Sweeten dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0)); 242ca632f55SGrant Likely } 243ca632f55SGrant Likely 244725b0e3eSSerge Semin static inline void dw_spi_set_clk(struct dw_spi *dws, u16 div) 245ca632f55SGrant Likely { 2467eb187b3SH Hartley Sweeten dw_writel(dws, DW_SPI_BAUDR, div); 247ca632f55SGrant Likely } 248ca632f55SGrant Likely 249ca632f55SGrant Likely /* Disable IRQ bits */ 250725b0e3eSSerge Semin static inline void dw_spi_mask_intr(struct dw_spi *dws, u32 mask) 251ca632f55SGrant Likely { 252ca632f55SGrant Likely u32 new_mask; 253ca632f55SGrant Likely 2547eb187b3SH Hartley Sweeten new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask; 2557eb187b3SH Hartley Sweeten dw_writel(dws, DW_SPI_IMR, new_mask); 256ca632f55SGrant Likely } 257ca632f55SGrant Likely 258ca632f55SGrant Likely /* Enable IRQ bits */ 259725b0e3eSSerge Semin static inline void dw_spi_umask_intr(struct dw_spi *dws, u32 mask) 260ca632f55SGrant Likely { 261ca632f55SGrant Likely u32 new_mask; 262ca632f55SGrant Likely 2637eb187b3SH Hartley Sweeten new_mask = dw_readl(dws, DW_SPI_IMR) | mask; 2647eb187b3SH Hartley Sweeten dw_writel(dws, DW_SPI_IMR, new_mask); 265ca632f55SGrant Likely } 266ca632f55SGrant Likely 267ca632f55SGrant Likely /* 268fbddc989SSerge Semin * This disables the SPI controller, interrupts, clears the interrupts status 269fbddc989SSerge Semin * and CS, then re-enables the controller back. Transmit and receive FIFO 270fbddc989SSerge Semin * buffers are cleared when the device is disabled. 27145746e82SAndy Shevchenko */ 272725b0e3eSSerge Semin static inline void dw_spi_reset_chip(struct dw_spi *dws) 27345746e82SAndy Shevchenko { 274725b0e3eSSerge Semin dw_spi_enable_chip(dws, 0); 275725b0e3eSSerge Semin dw_spi_mask_intr(dws, 0xff); 276a128f6ecSSerge Semin dw_readl(dws, DW_SPI_ICR); 277fbddc989SSerge Semin dw_writel(dws, DW_SPI_SER, 0); 278725b0e3eSSerge Semin dw_spi_enable_chip(dws, 1); 27945746e82SAndy Shevchenko } 28045746e82SAndy Shevchenko 281725b0e3eSSerge Semin static inline void dw_spi_shutdown_chip(struct dw_spi *dws) 2821cc3f141SAndy Shevchenko { 283725b0e3eSSerge Semin dw_spi_enable_chip(dws, 0); 284725b0e3eSSerge Semin dw_spi_set_clk(dws, 0); 2851cc3f141SAndy Shevchenko } 2861cc3f141SAndy Shevchenko 287c79bdbb4SAlexandre Belloni extern void dw_spi_set_cs(struct spi_device *spi, bool enable); 2883ff60c6bSSerge Semin extern void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi, 2893ff60c6bSSerge Semin struct dw_spi_cfg *cfg); 290bf64b660SSerge Semin extern int dw_spi_check_status(struct dw_spi *dws, bool raw); 29104f421e7SBaruch Siach extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws); 292ca632f55SGrant Likely extern void dw_spi_remove_host(struct dw_spi *dws); 293ca632f55SGrant Likely extern int dw_spi_suspend_host(struct dw_spi *dws); 294ca632f55SGrant Likely extern int dw_spi_resume_host(struct dw_spi *dws); 295ca632f55SGrant Likely 2966c710c0cSSerge Semin #ifdef CONFIG_SPI_DW_DMA 2976c710c0cSSerge Semin 29857784411SSerge Semin extern void dw_spi_dma_setup_mfld(struct dw_spi *dws); 29957784411SSerge Semin extern void dw_spi_dma_setup_generic(struct dw_spi *dws); 3006c710c0cSSerge Semin 3016c710c0cSSerge Semin #else 3026c710c0cSSerge Semin 30357784411SSerge Semin static inline void dw_spi_dma_setup_mfld(struct dw_spi *dws) {} 30457784411SSerge Semin static inline void dw_spi_dma_setup_generic(struct dw_spi *dws) {} 3056c710c0cSSerge Semin 3066c710c0cSSerge Semin #endif /* !CONFIG_SPI_DW_DMA */ 30737aa8aa6SAndy Shevchenko 308725b0e3eSSerge Semin #endif /* __SPI_DW_H__ */ 309