2 * Driver of Andes SPI Controller
4 * (C) Copyright 2011 Andes Technology
5 * Macpaul Lin <macpaul@andestech.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include "andes_spi.h"
38 static void andes_spi_spit_en(struct andes_spi_slave *ds)
40 unsigned int dcr = readl(&ds->regs->dcr);
42 debug("%s: dcr: %x, write value: %x\n",
43 __func__, dcr, (dcr | ANDES_SPI_DCR_SPIT));
45 writel((dcr | ANDES_SPI_DCR_SPIT), &ds->regs->dcr);
48 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
49 unsigned int max_hz, unsigned int mode)
51 struct andes_spi_slave *ds;
53 if (!spi_cs_is_valid(bus, cs))
56 ds = spi_alloc_slave(struct andes_spi_slave, bus, cs);
60 ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
63 * The hardware of andes_spi will set its frequency according
64 * to APB/AHB bus clock. Hence the hardware doesn't allow changing of
65 * requency and so the user requested speed is always ignored.
72 void spi_free_slave(struct spi_slave *slave)
74 struct andes_spi_slave *ds = to_andes_spi(slave);
79 int spi_claim_bus(struct spi_slave *slave)
81 struct andes_spi_slave *ds = to_andes_spi(slave);
85 /* Enable the SPI hardware */
86 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
90 baud = ((CONFIG_SYS_CLK_FREQ / CONFIG_SYS_SPI_CLK / 2) - 1) & 0xFF;
93 * SPI_CLK = AHB bus clock / ((BAUD + 1)*2)
94 * BAUD = AHB bus clock / SPI_CLK / 2) - 1
96 apb = (readl(&ds->regs->apb) & 0xffffff00) | baud;
97 writel(apb, &ds->regs->apb);
100 writel(0, &ds->regs->ie);
105 void spi_release_bus(struct spi_slave *slave)
107 struct andes_spi_slave *ds = to_andes_spi(slave);
109 /* Disable the SPI hardware */
110 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
113 static int andes_spi_read(struct spi_slave *slave, unsigned int len,
114 u8 *rxp, unsigned long flags)
116 struct andes_spi_slave *ds = to_andes_spi(slave);
117 unsigned int i, left;
120 debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n",
121 __func__, slave, len, rxp, flags);
123 debug("%s: data: ", __func__);
126 data = readl(&ds->regs->data);
129 for (i = 0; i < left; i++) {
130 debug("%02x ", data & 0xff);
141 static int andes_spi_write(struct spi_slave *slave, unsigned int wlen,
142 unsigned int rlen, const u8 *txp, unsigned long flags)
144 struct andes_spi_slave *ds = to_andes_spi(slave);
146 unsigned int i, left;
147 unsigned int spit_enabled = 0;
149 debug("%s: slave: %x, wlen: %d, rlen: %d, txp: %x, flags: %x\n",
150 __func__, slave, wlen, rlen, txp, flags);
152 /* The value of wlen and rlen wrote to register must minus 1 */
153 if (rlen == 0) /* write only */
154 writel(ANDES_SPI_DCR_MODE_WO | ANDES_SPI_DCR_WCNT(wlen-1) |
155 ANDES_SPI_DCR_RCNT(0), &ds->regs->dcr);
156 else /* write then read */
157 writel(ANDES_SPI_DCR_MODE_WR | ANDES_SPI_DCR_WCNT(wlen-1) |
158 ANDES_SPI_DCR_RCNT(rlen-1), &ds->regs->dcr);
160 /* wait till SPIBSY is cleared */
161 while (readl(&ds->regs->st) & ANDES_SPI_ST_SPIBSY)
164 /* data write process */
165 debug("%s: txp: ", __func__);
170 /* data are usually be read 32bits once a time */
173 for (i = 0; i < left; i++) {
175 data |= *txp++ << (i * 8);
180 debug("data: %08x\n", data);
181 debug("streg before write: %08x\n", readl(&ds->regs->st));
182 /* wait till TXFULL is deasserted */
183 while (readl(&ds->regs->st) & ANDES_SPI_ST_TXFEL)
185 writel(data, &ds->regs->data);
186 debug("streg after write: %08x\n", readl(&ds->regs->st));
189 if (spit_enabled == 0) {
190 /* enable SPIT bit - trigger the tx and rx progress */
191 andes_spi_spit_en(ds);
203 * Since andes_spi doesn't support independent command transaction,
204 * that is, write and than read must be operated in continuous
205 * execution, there is no need to set dcr and trigger spit again in
208 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
209 const void *dout, void *din, unsigned long flags)
212 static int op_nextime;
213 static u8 tmp_cmd[5];
218 /* Finish any previously submitted transfers */
222 /* Errors always terminate an ongoing transfer */
223 flags |= SPI_XFER_END;
229 debug("%s: slave: %08x, bitlen: %d, dout: "
230 "%08x, din: %08x, flags: %d, len: %d\n",
231 __func__, slave, bitlen, dout, din, flags, len);
235 * andes_spi's hardware doesn't support 2 data channel. The read
236 * and write cmd/data share the same register (data register).
238 * If a command has write and read transaction, you cannot do write
239 * this time and then do read on next time.
241 * A command writes first with a read response must indicating
242 * the read length in write operation. Hence the write action must
243 * be stored temporary and wait until the next read action has been
244 * arrived. Then we flush the write and read action out together.
247 if (op_nextime == 1) {
248 /* flags should be SPI_XFER_END, value is 2 */
250 andes_spi_write(slave, tmp_wlen, len, tmp_cmd, flags);
252 return andes_spi_read(slave, len, din, flags);
254 if (flags == SPI_XFER_BEGIN) {
255 /* store the write command and do operation next time */
257 memset(tmp_cmd, 0, sizeof(tmp_cmd));
258 memcpy(tmp_cmd, dout, len);
260 debug("%s: tmp_cmd: ", __func__);
261 for (i = 0; i < len; i++)
262 debug("%x ", *(tmp_cmd + i));
268 * flags should be (SPI_XFER_BEGIN | SPI_XFER_END),
271 if (op_nextime == 1) {
272 /* flags should be SPI_XFER_END, value is 2 */
274 /* flags 3 implies write only */
275 andes_spi_write(slave, tmp_wlen, 0, tmp_cmd, 3);
278 debug("flags: %x\n", flags);
279 return andes_spi_write(slave, len, 0, dout, flags);
287 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
289 return bus == 0 && cs == 0;
292 void spi_cs_activate(struct spi_slave *slave)
297 void spi_cs_deactivate(struct spi_slave *slave)