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
27 static void sh_spi_write(unsigned long data, unsigned long *reg)
32 static unsigned long sh_spi_read(unsigned long *reg)
37 static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
41 tmp = sh_spi_read(reg);
43 sh_spi_write(tmp, reg);
46 static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
50 tmp = sh_spi_read(reg);
52 sh_spi_write(tmp, reg);
55 static void clear_fifo(struct sh_spi *ss)
57 sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
58 sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
61 static int recvbuf_wait(struct sh_spi *ss)
63 while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
71 static int write_fifo_empty_wait(struct sh_spi *ss)
73 while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
85 static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
87 unsigned long val = 0;
94 sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
95 sh_spi_set_bit(val, &ss->regs->cr4);
98 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
99 unsigned int max_hz, unsigned int mode)
103 if (!spi_cs_is_valid(bus, cs))
106 ss = malloc(sizeof(struct spi_slave));
112 ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
115 sh_spi_write(0xfe, &ss->regs->cr1);
117 sh_spi_write(0x00, &ss->regs->cr1);
119 sh_spi_write(0x00, &ss->regs->cr3);
120 sh_spi_set_cs(ss, cs);
125 sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
131 void spi_free_slave(struct spi_slave *slave)
133 struct sh_spi *spi = to_sh_spi(slave);
138 int spi_claim_bus(struct spi_slave *slave)
143 void spi_release_bus(struct spi_slave *slave)
145 struct sh_spi *ss = to_sh_spi(slave);
147 sh_spi_write(sh_spi_read(&ss->regs->cr1) &
148 ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
151 static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
152 unsigned int len, unsigned long flags)
154 int i, cur_len, ret = 0;
155 int remain = (int)len;
158 if (len >= SH_SPI_FIFO_SIZE)
159 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
162 cur_len = (remain < SH_SPI_FIFO_SIZE) ?
163 remain : SH_SPI_FIFO_SIZE;
164 for (i = 0; i < cur_len &&
165 !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
166 !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
168 sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
172 if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
173 /* Abort the transaction */
174 flags |= SPI_XFER_END;
175 sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
184 write_fifo_empty_wait(ss);
187 if (flags & SPI_XFER_END) {
188 tmp = sh_spi_read(&ss->regs->cr1);
189 tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
190 sh_spi_write(tmp, &ss->regs->cr1);
191 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
193 write_fifo_empty_wait(ss);
199 static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
200 unsigned int len, unsigned long flags)
205 if (len > SH_SPI_MAX_BYTE)
206 sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
208 sh_spi_write(len, &ss->regs->cr3);
210 tmp = sh_spi_read(&ss->regs->cr1);
211 tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
212 sh_spi_write(tmp, &ss->regs->cr1);
213 sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
215 for (i = 0; i < len; i++) {
216 if (recvbuf_wait(ss))
219 rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
221 sh_spi_write(0, &ss->regs->cr3);
226 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
227 void *din, unsigned long flags)
229 struct sh_spi *ss = to_sh_spi(slave);
230 const unsigned char *tx_data = dout;
231 unsigned char *rx_data = din;
232 unsigned int len = bitlen / 8;
235 if (flags & SPI_XFER_BEGIN)
236 sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
240 ret = sh_spi_send(ss, tx_data, len, flags);
242 if (ret == 0 && rx_data)
243 ret = sh_spi_receive(ss, rx_data, len, flags);
245 if (flags & SPI_XFER_END) {
246 sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
249 sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
257 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
259 if (!bus && cs < SH_SPI_NUM_CS)
265 void spi_cs_activate(struct spi_slave *slave)
270 void spi_cs_deactivate(struct spi_slave *slave)