4 * Copyright (C) 2011-2012 Renesas Solutions Corp.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 static void sh_spi_write(unsigned long data, unsigned long *reg)
33 static unsigned long sh_spi_read(unsigned long *reg)
38 static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
42 tmp = sh_spi_read(reg);
44 sh_spi_write(tmp, reg);
47 static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
51 tmp = sh_spi_read(reg);
53 sh_spi_write(tmp, reg);
56 static void clear_fifo(struct sh_spi *ss)
58 sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
59 sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
62 static int recvbuf_wait(struct sh_spi *ss)
64 while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
72 static int write_fifo_empty_wait(struct sh_spi *ss)
74 while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
86 static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
88 unsigned long val = 0;
95 sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
96 sh_spi_set_bit(val, &ss->regs->cr4);
99 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
100 unsigned int max_hz, unsigned int mode)
104 if (!spi_cs_is_valid(bus, cs))
107 ss = spi_alloc_slave(struct sh_spi, bus, cs);
111 ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
114 sh_spi_write(0xfe, &ss->regs->cr1);
116 sh_spi_write(0x00, &ss->regs->cr1);
118 sh_spi_write(0x00, &ss->regs->cr3);
119 sh_spi_set_cs(ss, cs);
124 sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
130 void spi_free_slave(struct spi_slave *slave)
132 struct sh_spi *spi = to_sh_spi(slave);
137 int spi_claim_bus(struct spi_slave *slave)
142 void spi_release_bus(struct spi_slave *slave)
144 struct sh_spi *ss = to_sh_spi(slave);
146 sh_spi_write(sh_spi_read(&ss->regs->cr1) &
147 ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
150 static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
151 unsigned int len, unsigned long flags)
153 int i, cur_len, ret = 0;
154 int remain = (int)len;
156 if (len >= SH_SPI_FIFO_SIZE)
157 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
160 cur_len = (remain < SH_SPI_FIFO_SIZE) ?
161 remain : SH_SPI_FIFO_SIZE;
162 for (i = 0; i < cur_len &&
163 !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
164 !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
166 sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
170 if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
171 /* Abort the transaction */
172 flags |= SPI_XFER_END;
173 sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
182 write_fifo_empty_wait(ss);
185 if (flags & SPI_XFER_END) {
186 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
187 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
189 write_fifo_empty_wait(ss);
195 static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
196 unsigned int len, unsigned long flags)
200 if (len > SH_SPI_MAX_BYTE)
201 sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
203 sh_spi_write(len, &ss->regs->cr3);
205 sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
206 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
208 for (i = 0; i < len; i++) {
209 if (recvbuf_wait(ss))
212 rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
214 sh_spi_write(0, &ss->regs->cr3);
219 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
220 void *din, unsigned long flags)
222 struct sh_spi *ss = to_sh_spi(slave);
223 const unsigned char *tx_data = dout;
224 unsigned char *rx_data = din;
225 unsigned int len = bitlen / 8;
228 if (flags & SPI_XFER_BEGIN)
229 sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
233 ret = sh_spi_send(ss, tx_data, len, flags);
235 if (ret == 0 && rx_data)
236 ret = sh_spi_receive(ss, rx_data, len, flags);
238 if (flags & SPI_XFER_END) {
239 sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
242 sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
250 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
252 if (!bus && cs < SH_SPI_NUM_CS)
258 void spi_cs_activate(struct spi_slave *slave)
263 void spi_cs_deactivate(struct spi_slave *slave)