1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2011-2012 Renesas Solutions Corp.
15 static void sh_spi_write(unsigned long data, unsigned long *reg)
20 static unsigned long sh_spi_read(unsigned long *reg)
25 static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
29 tmp = sh_spi_read(reg);
31 sh_spi_write(tmp, reg);
34 static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
38 tmp = sh_spi_read(reg);
40 sh_spi_write(tmp, reg);
43 static void clear_fifo(struct sh_spi *ss)
45 sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
46 sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
49 static int recvbuf_wait(struct sh_spi *ss)
51 while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
59 static int write_fifo_empty_wait(struct sh_spi *ss)
61 while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
73 static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
75 unsigned long val = 0;
82 sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
83 sh_spi_set_bit(val, &ss->regs->cr4);
86 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
87 unsigned int max_hz, unsigned int mode)
91 if (!spi_cs_is_valid(bus, cs))
94 ss = spi_alloc_slave(struct sh_spi, bus, cs);
98 ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
101 sh_spi_write(0xfe, &ss->regs->cr1);
103 sh_spi_write(0x00, &ss->regs->cr1);
105 sh_spi_write(0x00, &ss->regs->cr3);
106 sh_spi_set_cs(ss, cs);
111 sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
117 void spi_free_slave(struct spi_slave *slave)
119 struct sh_spi *spi = to_sh_spi(slave);
124 int spi_claim_bus(struct spi_slave *slave)
129 void spi_release_bus(struct spi_slave *slave)
131 struct sh_spi *ss = to_sh_spi(slave);
133 sh_spi_write(sh_spi_read(&ss->regs->cr1) &
134 ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
137 static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
138 unsigned int len, unsigned long flags)
140 int i, cur_len, ret = 0;
141 int remain = (int)len;
143 if (len >= SH_SPI_FIFO_SIZE)
144 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
147 cur_len = (remain < SH_SPI_FIFO_SIZE) ?
148 remain : SH_SPI_FIFO_SIZE;
149 for (i = 0; i < cur_len &&
150 !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
151 !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
153 sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
157 if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
158 /* Abort the transaction */
159 flags |= SPI_XFER_END;
160 sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
169 write_fifo_empty_wait(ss);
172 if (flags & SPI_XFER_END) {
173 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
174 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
176 write_fifo_empty_wait(ss);
182 static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
183 unsigned int len, unsigned long flags)
187 if (len > SH_SPI_MAX_BYTE)
188 sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
190 sh_spi_write(len, &ss->regs->cr3);
192 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
193 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
195 for (i = 0; i < len; i++) {
196 if (recvbuf_wait(ss))
199 rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
201 sh_spi_write(0, &ss->regs->cr3);
206 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
207 void *din, unsigned long flags)
209 struct sh_spi *ss = to_sh_spi(slave);
210 const unsigned char *tx_data = dout;
211 unsigned char *rx_data = din;
212 unsigned int len = bitlen / 8;
215 if (flags & SPI_XFER_BEGIN)
216 sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
220 ret = sh_spi_send(ss, tx_data, len, flags);
222 if (ret == 0 && rx_data)
223 ret = sh_spi_receive(ss, rx_data, len, flags);
225 if (flags & SPI_XFER_END) {
226 sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
229 sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
237 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
239 if (!bus && cs < SH_SPI_NUM_CS)
245 void spi_cs_activate(struct spi_slave *slave)
250 void spi_cs_deactivate(struct spi_slave *slave)