4 * Copyright (C) 2011-2012 Renesas Solutions Corp.
6 * SPDX-License-Identifier: GPL-2.0
16 static void sh_spi_write(unsigned long data, unsigned long *reg)
21 static unsigned long sh_spi_read(unsigned long *reg)
26 static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
30 tmp = sh_spi_read(reg);
32 sh_spi_write(tmp, reg);
35 static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
39 tmp = sh_spi_read(reg);
41 sh_spi_write(tmp, reg);
44 static void clear_fifo(struct sh_spi *ss)
46 sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
47 sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
50 static int recvbuf_wait(struct sh_spi *ss)
52 while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
60 static int write_fifo_empty_wait(struct sh_spi *ss)
62 while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
74 static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
76 unsigned long val = 0;
83 sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
84 sh_spi_set_bit(val, &ss->regs->cr4);
87 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
88 unsigned int max_hz, unsigned int mode)
92 if (!spi_cs_is_valid(bus, cs))
95 ss = spi_alloc_slave(struct sh_spi, bus, cs);
99 ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
102 sh_spi_write(0xfe, &ss->regs->cr1);
104 sh_spi_write(0x00, &ss->regs->cr1);
106 sh_spi_write(0x00, &ss->regs->cr3);
107 sh_spi_set_cs(ss, cs);
112 sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
118 void spi_free_slave(struct spi_slave *slave)
120 struct sh_spi *spi = to_sh_spi(slave);
125 int spi_claim_bus(struct spi_slave *slave)
130 void spi_release_bus(struct spi_slave *slave)
132 struct sh_spi *ss = to_sh_spi(slave);
134 sh_spi_write(sh_spi_read(&ss->regs->cr1) &
135 ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
138 static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
139 unsigned int len, unsigned long flags)
141 int i, cur_len, ret = 0;
142 int remain = (int)len;
144 if (len >= SH_SPI_FIFO_SIZE)
145 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
148 cur_len = (remain < SH_SPI_FIFO_SIZE) ?
149 remain : SH_SPI_FIFO_SIZE;
150 for (i = 0; i < cur_len &&
151 !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
152 !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
154 sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
158 if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
159 /* Abort the transaction */
160 flags |= SPI_XFER_END;
161 sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
170 write_fifo_empty_wait(ss);
173 if (flags & SPI_XFER_END) {
174 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
175 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
177 write_fifo_empty_wait(ss);
183 static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
184 unsigned int len, unsigned long flags)
188 if (len > SH_SPI_MAX_BYTE)
189 sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
191 sh_spi_write(len, &ss->regs->cr3);
193 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
194 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
196 for (i = 0; i < len; i++) {
197 if (recvbuf_wait(ss))
200 rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
202 sh_spi_write(0, &ss->regs->cr3);
207 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
208 void *din, unsigned long flags)
210 struct sh_spi *ss = to_sh_spi(slave);
211 const unsigned char *tx_data = dout;
212 unsigned char *rx_data = din;
213 unsigned int len = bitlen / 8;
216 if (flags & SPI_XFER_BEGIN)
217 sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
221 ret = sh_spi_send(ss, tx_data, len, flags);
223 if (ret == 0 && rx_data)
224 ret = sh_spi_receive(ss, rx_data, len, flags);
226 if (flags & SPI_XFER_END) {
227 sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
230 sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
238 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
240 if (!bus && cs < SH_SPI_NUM_CS)
246 void spi_cs_activate(struct spi_slave *slave)
251 void spi_cs_deactivate(struct spi_slave *slave)