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+
15 #include "andes_spi.h"
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
))
40 ds
= spi_alloc_slave(struct andes_spi_slave
, bus
, cs
);
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.
56 void spi_free_slave(struct spi_slave
*slave
)
58 struct andes_spi_slave
*ds
= to_andes_spi(slave
);
63 int spi_claim_bus(struct spi_slave
*slave
)
65 struct andes_spi_slave
*ds
= to_andes_spi(slave
);
69 /* Enable the SPI hardware */
70 writel(ANDES_SPI_CR_SPIRST
, &ds
->regs
->cr
);
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
);
84 writel(0, &ds
->regs
->ie
);
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
;
104 debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n",
105 __func__
, slave
, len
, rxp
, flags
);
107 debug("%s: data: ", __func__
);
110 data
= readl(&ds
->regs
->data
);
113 for (i
= 0; i
< left
; i
++) {
114 debug("%02x ", data
& 0xff);
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
);
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__
);
154 /* data are usually be read 32bits once a time */
157 for (i
= 0; i
< left
; i
++) {
159 data
|= *txp
++ << (i
* 8);
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
);
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
192 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
,
193 const void *dout
, void *din
, unsigned long flags
)
196 static int op_nextime
;
197 static u8 tmp_cmd
[5];
202 /* Finish any previously submitted transfers */
206 /* Errors always terminate an ongoing transfer */
207 flags
|= SPI_XFER_END
;
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
);
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.
231 if (op_nextime
== 1) {
232 /* flags should be SPI_XFER_END, value is 2 */
234 andes_spi_write(slave
, tmp_wlen
, len
, tmp_cmd
, flags
);
236 return andes_spi_read(slave
, len
, din
, flags
);
238 if (flags
== SPI_XFER_BEGIN
) {
239 /* store the write command and do operation next time */
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
));
252 * flags should be (SPI_XFER_BEGIN | SPI_XFER_END),
255 if (op_nextime
== 1) {
256 /* flags should be SPI_XFER_END, value is 2 */
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
);
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
)
281 void spi_cs_deactivate(struct spi_slave
*slave
)