3 * eInfochips Ltd. <www.einfochips.com>
4 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
7 * Marvell Semiconductor <www.marvell.com>
9 * Written-by: Lei Wen <leiwen@marvell.com>
11 * SPDX-License-Identifier: GPL-2.0+
20 #include <asm/arch/spi.h>
23 #define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave)
25 struct armd_spi_slave {
26 struct spi_slave slave;
27 struct ssp_reg *spi_reg;
36 static int spi_armd_write(struct armd_spi_slave *pss)
38 int wait_timeout = SSP_FLUSH_NUM;
39 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF))
42 debug("%s: timeout error\n", __func__);
46 if (pss->tx != NULL) {
47 writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr);
50 writel(0, &pss->spi_reg->ssdr);
55 static int spi_armd_read(struct armd_spi_slave *pss)
57 int wait_timeout = SSP_FLUSH_NUM;
58 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE))
61 debug("%s: timeout error\n", __func__);
65 if (pss->rx != NULL) {
66 *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr);
69 readl(&pss->spi_reg->ssdr);
74 static int spi_armd_flush(struct armd_spi_slave *pss)
76 unsigned long limit = SSP_FLUSH_NUM;
79 while (readl(&pss->spi_reg->sssr) & SSSR_RNE)
80 readl(&pss->spi_reg->ssdr);
81 } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--);
83 writel(SSSR_ROR, &pss->spi_reg->sssr);
88 void spi_cs_activate(struct spi_slave *slave)
90 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
92 gpio_set_value(slave->cs, pss->gpio_cs_inverted);
95 void spi_cs_deactivate(struct spi_slave *slave)
97 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
99 gpio_set_value(slave->cs, !pss->gpio_cs_inverted);
102 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
103 unsigned int max_hz, unsigned int mode)
105 struct armd_spi_slave *pss;
107 pss = spi_alloc_slave(struct armd_spi_slave, bus, cs);
111 pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
113 pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
115 pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
116 (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
117 pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
118 pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
119 | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
121 pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
122 pss->clear_sr = SSSR_ROR | SSSR_TINT;
124 pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
125 gpio_set_value(cs, !pss->gpio_cs_inverted);
130 void spi_free_slave(struct spi_slave *slave)
132 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
137 int spi_claim_bus(struct spi_slave *slave)
139 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
141 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
142 if (spi_armd_flush(pss) == 0)
148 void spi_release_bus(struct spi_slave *slave)
152 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
153 void *din, unsigned long flags)
155 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
156 uint bytes = bitlen / 8;
163 /* we can only do 8 bit transfers */
165 flags |= SPI_XFER_END;
172 if (flags & SPI_XFER_BEGIN) {
173 spi_cs_activate(slave);
174 writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
175 writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
176 writel(pss->cr0, &pss->spi_reg->sscr0);
180 limit = SSP_FLUSH_NUM;
181 ret = spi_armd_write(pss);
185 while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
188 ret = spi_armd_read(pss);
194 if (flags & SPI_XFER_END) {
196 writel(pss->clear_sr, &pss->spi_reg->sssr);
197 clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
198 writel(0, &pss->spi_reg->ssto);
199 spi_cs_deactivate(slave);