Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / spi / andes_spi.c
blob82aed75ca187b0dcf2060cb9691965c4b1b243d1
1 /*
2 * Driver of Andes SPI Controller
4 * (C) Copyright 2011 Andes Technology
5 * Macpaul Lin <macpaul@andestech.com>
7 * SPDX-License-Identifier: GPL-2.0+
8 */
10 #include <common.h>
11 #include <malloc.h>
12 #include <spi.h>
14 #include <asm/io.h>
15 #include "andes_spi.h"
17 void spi_init(void)
19 /* do nothing */
22 static void andes_spi_spit_en(struct andes_spi_slave *ds)
24 unsigned int dcr = readl(&ds->regs->dcr);
26 debug("%s: dcr: %x, write value: %x\n",
27 __func__, dcr, (dcr | ANDES_SPI_DCR_SPIT));
29 writel((dcr | ANDES_SPI_DCR_SPIT), &ds->regs->dcr);
32 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
33 unsigned int max_hz, unsigned int mode)
35 struct andes_spi_slave *ds;
37 if (!spi_cs_is_valid(bus, cs))
38 return NULL;
40 ds = spi_alloc_slave(struct andes_spi_slave, bus, cs);
41 if (!ds)
42 return NULL;
44 ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
47 * The hardware of andes_spi will set its frequency according
48 * to APB/AHB bus clock. Hence the hardware doesn't allow changing of
49 * requency and so the user requested speed is always ignored.
51 ds->freq = max_hz;
53 return &ds->slave;
56 void spi_free_slave(struct spi_slave *slave)
58 struct andes_spi_slave *ds = to_andes_spi(slave);
60 free(ds);
63 int spi_claim_bus(struct spi_slave *slave)
65 struct andes_spi_slave *ds = to_andes_spi(slave);
66 unsigned int apb;
67 unsigned int baud;
69 /* Enable the SPI hardware */
70 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
71 udelay(1000);
73 /* setup format */
74 baud = ((CONFIG_SYS_CLK_FREQ / CONFIG_SYS_SPI_CLK / 2) - 1) & 0xFF;
77 * SPI_CLK = AHB bus clock / ((BAUD + 1)*2)
78 * BAUD = AHB bus clock / SPI_CLK / 2) - 1
80 apb = (readl(&ds->regs->apb) & 0xffffff00) | baud;
81 writel(apb, &ds->regs->apb);
83 /* no interrupts */
84 writel(0, &ds->regs->ie);
86 return 0;
89 void spi_release_bus(struct spi_slave *slave)
91 struct andes_spi_slave *ds = to_andes_spi(slave);
93 /* Disable the SPI hardware */
94 writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
97 static int andes_spi_read(struct spi_slave *slave, unsigned int len,
98 u8 *rxp, unsigned long flags)
100 struct andes_spi_slave *ds = to_andes_spi(slave);
101 unsigned int i, left;
102 unsigned int data;
104 debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n",
105 __func__, slave, len, rxp, flags);
107 debug("%s: data: ", __func__);
108 while (len > 0) {
109 left = min(len, 4);
110 data = readl(&ds->regs->data);
112 debug(" ");
113 for (i = 0; i < left; i++) {
114 debug("%02x ", data & 0xff);
115 *rxp++ = data;
116 data >>= 8;
117 len--;
120 debug("\n");
122 return 0;
125 static int andes_spi_write(struct spi_slave *slave, unsigned int wlen,
126 unsigned int rlen, const u8 *txp, unsigned long flags)
128 struct andes_spi_slave *ds = to_andes_spi(slave);
129 unsigned int data;
130 unsigned int i, left;
131 unsigned int spit_enabled = 0;
133 debug("%s: slave: %x, wlen: %d, rlen: %d, txp: %x, flags: %x\n",
134 __func__, slave, wlen, rlen, txp, flags);
136 /* The value of wlen and rlen wrote to register must minus 1 */
137 if (rlen == 0) /* write only */
138 writel(ANDES_SPI_DCR_MODE_WO | ANDES_SPI_DCR_WCNT(wlen-1) |
139 ANDES_SPI_DCR_RCNT(0), &ds->regs->dcr);
140 else /* write then read */
141 writel(ANDES_SPI_DCR_MODE_WR | ANDES_SPI_DCR_WCNT(wlen-1) |
142 ANDES_SPI_DCR_RCNT(rlen-1), &ds->regs->dcr);
144 /* wait till SPIBSY is cleared */
145 while (readl(&ds->regs->st) & ANDES_SPI_ST_SPIBSY)
148 /* data write process */
149 debug("%s: txp: ", __func__);
150 while (wlen > 0) {
151 /* clear the data */
152 data = 0;
154 /* data are usually be read 32bits once a time */
155 left = min(wlen, 4);
157 for (i = 0; i < left; i++) {
158 debug("%x ", *txp);
159 data |= *txp++ << (i * 8);
160 wlen--;
162 debug("\n");
164 debug("data: %08x\n", data);
165 debug("streg before write: %08x\n", readl(&ds->regs->st));
166 /* wait till TXFULL is deasserted */
167 while (readl(&ds->regs->st) & ANDES_SPI_ST_TXFEL)
169 writel(data, &ds->regs->data);
170 debug("streg after write: %08x\n", readl(&ds->regs->st));
173 if (spit_enabled == 0) {
174 /* enable SPIT bit - trigger the tx and rx progress */
175 andes_spi_spit_en(ds);
176 spit_enabled = 1;
180 debug("\n");
182 return 0;
186 * spi_xfer:
187 * Since andes_spi doesn't support independent command transaction,
188 * that is, write and than read must be operated in continuous
189 * execution, there is no need to set dcr and trigger spit again in
190 * RX process.
192 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
193 const void *dout, void *din, unsigned long flags)
195 unsigned int len;
196 static int op_nextime;
197 static u8 tmp_cmd[5];
198 static int tmp_wlen;
199 unsigned int i;
201 if (bitlen == 0)
202 /* Finish any previously submitted transfers */
203 goto out;
205 if (bitlen % 8) {
206 /* Errors always terminate an ongoing transfer */
207 flags |= SPI_XFER_END;
208 goto out;
211 len = bitlen / 8;
213 debug("%s: slave: %08x, bitlen: %d, dout: "
214 "%08x, din: %08x, flags: %d, len: %d\n",
215 __func__, slave, bitlen, dout, din, flags, len);
218 * Important:
219 * andes_spi's hardware doesn't support 2 data channel. The read
220 * and write cmd/data share the same register (data register).
222 * If a command has write and read transaction, you cannot do write
223 * this time and then do read on next time.
225 * A command writes first with a read response must indicating
226 * the read length in write operation. Hence the write action must
227 * be stored temporary and wait until the next read action has been
228 * arrived. Then we flush the write and read action out together.
230 if (!dout) {
231 if (op_nextime == 1) {
232 /* flags should be SPI_XFER_END, value is 2 */
233 op_nextime = 0;
234 andes_spi_write(slave, tmp_wlen, len, tmp_cmd, flags);
236 return andes_spi_read(slave, len, din, flags);
237 } else if (!din) {
238 if (flags == SPI_XFER_BEGIN) {
239 /* store the write command and do operation next time */
240 op_nextime = 1;
241 memset(tmp_cmd, 0, sizeof(tmp_cmd));
242 memcpy(tmp_cmd, dout, len);
244 debug("%s: tmp_cmd: ", __func__);
245 for (i = 0; i < len; i++)
246 debug("%x ", *(tmp_cmd + i));
247 debug("\n");
249 tmp_wlen = len;
250 } else {
252 * flags should be (SPI_XFER_BEGIN | SPI_XFER_END),
253 * the value is 3.
255 if (op_nextime == 1) {
256 /* flags should be SPI_XFER_END, value is 2 */
257 op_nextime = 0;
258 /* flags 3 implies write only */
259 andes_spi_write(slave, tmp_wlen, 0, tmp_cmd, 3);
262 debug("flags: %x\n", flags);
263 return andes_spi_write(slave, len, 0, dout, flags);
267 out:
268 return 0;
271 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
273 return bus == 0 && cs == 0;
276 void spi_cs_activate(struct spi_slave *slave)
278 /* do nothing */
281 void spi_cs_deactivate(struct spi_slave *slave)
283 /* do nothing */