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 = malloc(sizeof(*ds));
62 ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
65 * The hardware of andes_spi will set its frequency according
66 * to APB/AHB bus clock. Hence the hardware doesn't allow changing of
67 * requency and so the user requested speed is always ignored.
74 void spi_free_slave(struct spi_slave *slave)
76 struct andes_spi_slave *ds = to_andes_spi(slave);
81 int spi_claim_bus(struct spi_slave *slave)
83 struct andes_spi_slave *ds = to_andes_spi(slave);
87 /* Enable the SPI hardware */
88 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
92 baud = ((CONFIG_SYS_CLK_FREQ / CONFIG_SYS_SPI_CLK / 2) - 1) & 0xFF;
95 * SPI_CLK = AHB bus clock / ((BAUD + 1)*2)
96 * BAUD = AHB bus clock / SPI_CLK / 2) - 1
98 apb = (readl(&ds->regs->apb) & 0xffffff00) | baud;
99 writel(apb, &ds->regs->apb);
102 writel(0, &ds->regs->ie);
107 void spi_release_bus(struct spi_slave *slave)
109 struct andes_spi_slave *ds = to_andes_spi(slave);
111 /* Disable the SPI hardware */
112 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
115 static int andes_spi_read(struct spi_slave *slave, unsigned int len,
116 u8 *rxp, unsigned long flags)
118 struct andes_spi_slave *ds = to_andes_spi(slave);
119 unsigned int i, left;
122 debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n",
123 __func__, slave, len, rxp, flags);
125 debug("%s: data: ", __func__);
128 data = readl(&ds->regs->data);
131 for (i = 0; i < left; i++) {
132 debug("%02x ", data & 0xff);
143 static int andes_spi_write(struct spi_slave *slave, unsigned int wlen,
144 unsigned int rlen, const u8 *txp, unsigned long flags)
146 struct andes_spi_slave *ds = to_andes_spi(slave);
148 unsigned int i, left;
149 unsigned int spit_enabled = 0;
151 debug("%s: slave: %x, wlen: %d, rlen: %d, txp: %x, flags: %x\n",
152 __func__, slave, wlen, rlen, txp, flags);
154 /* The value of wlen and rlen wrote to register must minus 1 */
155 if (rlen == 0) /* write only */
156 writel(ANDES_SPI_DCR_MODE_WO | ANDES_SPI_DCR_WCNT(wlen-1) |
157 ANDES_SPI_DCR_RCNT(0), &ds->regs->dcr);
158 else /* write then read */
159 writel(ANDES_SPI_DCR_MODE_WR | ANDES_SPI_DCR_WCNT(wlen-1) |
160 ANDES_SPI_DCR_RCNT(rlen-1), &ds->regs->dcr);
162 /* wait till SPIBSY is cleared */
163 while (readl(&ds->regs->st) & ANDES_SPI_ST_SPIBSY)
166 /* data write process */
167 debug("%s: txp: ", __func__);
172 /* data are usually be read 32bits once a time */
175 for (i = 0; i < left; i++) {
177 data |= *txp++ << (i * 8);
182 debug("data: %08x\n", data);
183 debug("streg before write: %08x\n", readl(&ds->regs->st));
184 /* wait till TXFULL is deasserted */
185 while (readl(&ds->regs->st) & ANDES_SPI_ST_TXFEL)
187 writel(data, &ds->regs->data);
188 debug("streg after write: %08x\n", readl(&ds->regs->st));
191 if (spit_enabled == 0) {
192 /* enable SPIT bit - trigger the tx and rx progress */
193 andes_spi_spit_en(ds);
205 * Since andes_spi doesn't support independent command transaction,
206 * that is, write and than read must be operated in continuous
207 * execution, there is no need to set dcr and trigger spit again in
210 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
211 const void *dout, void *din, unsigned long flags)
214 static int op_nextime;
215 static u8 tmp_cmd[5];
220 /* Finish any previously submitted transfers */
224 /* Errors always terminate an ongoing transfer */
225 flags |= SPI_XFER_END;
231 debug("%s: slave: %08x, bitlen: %d, dout: "
232 "%08x, din: %08x, flags: %d, len: %d\n",
233 __func__, slave, bitlen, dout, din, flags, len);
237 * andes_spi's hardware doesn't support 2 data channel. The read
238 * and write cmd/data share the same register (data register).
240 * If a command has write and read transaction, you cannot do write
241 * this time and then do read on next time.
243 * A command writes first with a read response must indicating
244 * the read length in write operation. Hence the write action must
245 * be stored temporary and wait until the next read action has been
246 * arrived. Then we flush the write and read action out together.
249 if (op_nextime == 1) {
250 /* flags should be SPI_XFER_END, value is 2 */
252 andes_spi_write(slave, tmp_wlen, len, tmp_cmd, flags);
254 return andes_spi_read(slave, len, din, flags);
256 if (flags == SPI_XFER_BEGIN) {
257 /* store the write command and do operation next time */
259 memset(tmp_cmd, 0, sizeof(tmp_cmd));
260 memcpy(tmp_cmd, dout, len);
262 debug("%s: tmp_cmd: ", __func__);
263 for (i = 0; i < len; i++)
264 debug("%x ", *(tmp_cmd + i));
270 * flags should be (SPI_XFER_BEGIN | SPI_XFER_END),
273 if (op_nextime == 1) {
274 /* flags should be SPI_XFER_END, value is 2 */
276 /* flags 3 implies write only */
277 andes_spi_write(slave, tmp_wlen, 0, tmp_cmd, 3);
280 debug("flags: %x\n", flags);
281 return andes_spi_write(slave, len, 0, dout, flags);
289 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
291 return bus == 0 && cs == 0;
294 void spi_cs_activate(struct spi_slave *slave)
299 void spi_cs_deactivate(struct spi_slave *slave)