1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
5 #include <console/console.h>
8 #include <soc/addressmap.h>
10 #include <soc/clock.h>
11 #include <spi-generic.h>
12 #include <spi_flash.h>
16 struct rockchip_spi_slave
{
17 struct rockchip_spi
*regs
;
20 #define SPI_TIMEOUT_US 1000
21 #define SPI_SRCCLK_HZ (198*MHz)
22 #define SPI_FIFO_DEPTH 32
24 static struct rockchip_spi_slave rockchip_spi_slaves
[] = {
26 .regs
= (void *)SPI0_BASE
,
29 .regs
= (void *)SPI1_BASE
,
32 .regs
= (void *)SPI2_BASE
,
36 .regs
= (void *)SPI3_BASE
,
40 .regs
= (void *)SPI4_BASE
,
44 .regs
= (void *)SPI5_BASE
,
51 static struct rockchip_spi_slave
*to_rockchip_spi(const struct spi_slave
*slave
)
53 assert(slave
->bus
< ARRAY_SIZE(rockchip_spi_slaves
));
54 return &rockchip_spi_slaves
[slave
->bus
];
57 static void spi_cs_activate(const struct spi_slave
*slave
)
59 struct rockchip_spi
*regs
= to_rockchip_spi(slave
)->regs
;
60 setbits32(®s
->ser
, 1);
63 static void spi_cs_deactivate(const struct spi_slave
*slave
)
65 struct rockchip_spi
*regs
= to_rockchip_spi(slave
)->regs
;
66 clrbits32(®s
->ser
, 1);
69 static void rockchip_spi_enable_chip(struct rockchip_spi
*regs
, int enable
)
72 write32(®s
->spienr
, 1);
74 write32(®s
->spienr
, 0);
77 static void rockchip_spi_set_clk(struct rockchip_spi
*regs
, unsigned int hz
)
79 unsigned short clk_div
= SPI_SRCCLK_HZ
/ hz
;
80 assert(clk_div
* hz
== SPI_SRCCLK_HZ
&& !(clk_div
& 1));
81 write32(®s
->baudr
, clk_div
);
84 void rockchip_spi_init(unsigned int bus
, unsigned int speed_hz
)
86 assert(bus
< ARRAY_SIZE(rockchip_spi_slaves
));
87 struct rockchip_spi
*regs
= rockchip_spi_slaves
[bus
].regs
;
88 unsigned int ctrlr0
= 0;
90 rkclk_configure_spi(bus
, SPI_SRCCLK_HZ
);
91 rockchip_spi_enable_chip(regs
, 0);
92 rockchip_spi_set_clk(regs
, speed_hz
);
95 ctrlr0
= (SPI_OMOD_MASTER
<< SPI_OMOD_OFFSET
);
98 ctrlr0
|= SPI_DFS_8BIT
<< SPI_DFS_OFFSET
;
100 /* Chip Select Mode */
101 ctrlr0
|= (SPI_CSM_KEEP
<< SPI_CSM_OFFSET
);
103 /* SSN to Sclk_out delay */
104 ctrlr0
|= (SPI_SSN_DELAY_ONE
<< SPI_SSN_DELAY_OFFSET
);
106 /* Serial Endian Mode */
107 ctrlr0
|= (SPI_SEM_LITTLE
<< SPI_SEM_OFFSET
);
110 ctrlr0
|= (SPI_FBM_MSB
<< SPI_FBM_OFFSET
);
113 ctrlr0
|= (SPI_FRF_SPI
<< SPI_FRF_OFFSET
);
115 write32(®s
->ctrlr0
, ctrlr0
);
118 write32(®s
->txftlr
, SPI_FIFO_DEPTH
/ 2 - 1);
119 write32(®s
->rxftlr
, SPI_FIFO_DEPTH
/ 2 - 1);
122 void rockchip_spi_set_sample_delay(unsigned int bus
, unsigned int delay_ns
)
124 assert(bus
< ARRAY_SIZE(rockchip_spi_slaves
));
125 struct rockchip_spi
*regs
= rockchip_spi_slaves
[bus
].regs
;
128 /* Rxd Sample Delay */
129 rsd
= DIV_ROUND_CLOSEST(delay_ns
* (SPI_SRCCLK_HZ
>> 8), 1*GHz
>> 8);
131 clrsetbits32(®s
->ctrlr0
, SPI_RXDSD_MASK
<< SPI_RXDSD_OFFSET
,
132 rsd
<< SPI_RXDSD_OFFSET
);
135 static int spi_ctrlr_claim_bus(const struct spi_slave
*slave
)
137 spi_cs_activate(slave
);
141 static void spi_ctrlr_release_bus(const struct spi_slave
*slave
)
143 spi_cs_deactivate(slave
);
146 static int rockchip_spi_wait_till_not_busy(struct rockchip_spi
*regs
)
150 stopwatch_init_usecs_expire(&sw
, SPI_TIMEOUT_US
);
152 if (!(read32(®s
->sr
) & SR_BUSY
))
154 } while (!stopwatch_expired(&sw
));
156 "RK SPI: Status keeps busy for 1000us after a read/write!\n");
160 static void set_tmod(struct rockchip_spi
*regs
, unsigned int tmod
)
162 clrsetbits32(®s
->ctrlr0
, SPI_TMOD_MASK
<< SPI_TMOD_OFFSET
,
163 tmod
<< SPI_TMOD_OFFSET
);
166 static void set_transfer_mode(struct rockchip_spi
*regs
,
167 unsigned int sout
, unsigned int sin
)
171 else if (sin
&& sout
)
172 set_tmod(regs
, SPI_TMOD_TR
); /* tx and rx */
174 set_tmod(regs
, SPI_TMOD_TO
); /* tx only */
176 set_tmod(regs
, SPI_TMOD_RO
); /* rx only */
179 /* returns 0 to indicate success, <0 otherwise */
180 static int do_xfer(struct rockchip_spi
*regs
, bool use_16bit
, const void *dout
,
181 size_t *bytes_out
, void *din
, size_t *bytes_in
)
183 uint8_t *in_buf
= din
;
184 uint8_t *out_buf
= (uint8_t *)dout
;
188 min_xfer
= *bytes_in
;
189 else if (*bytes_in
== 0)
190 min_xfer
= *bytes_out
;
192 min_xfer
= MIN(*bytes_in
, *bytes_out
);
195 uint32_t sr
= read32(®s
->sr
);
196 int xferred
= 0; /* in either (or both) directions */
198 if (*bytes_out
&& !(sr
& SR_TF_FULL
)) {
199 write32(®s
->txdr
, *out_buf
);
206 * Try to read as many bytes as are available in one go.
207 * Reading the status registers probably requires
208 * sychronizing with the SPI clock which is pretty slow.
210 if (*bytes_in
&& !(sr
& SR_RF_EMPT
)) {
211 int w
= use_16bit
? 2 : 1;
212 xferred
= (read32(®s
->rxflr
) & RXFLR_LEVEL_MASK
) * w
;
213 buffer_from_fifo32(in_buf
, xferred
, ®s
->rxdr
, 0, w
);
214 *bytes_in
-= xferred
;
221 if (rockchip_spi_wait_till_not_busy(regs
)) {
222 printk(BIOS_ERR
, "Timed out waiting on SPI transfer\n");
229 static int spi_ctrlr_xfer(const struct spi_slave
*slave
, const void *dout
,
230 size_t bytes_out
, void *din
, size_t bytes_in
)
232 struct rockchip_spi
*regs
= to_rockchip_spi(slave
)->regs
;
236 * RK3288 SPI controller can transfer up to 65536 data frames (bytes
237 * in our case) continuously. Break apart large requests as necessary.
239 * FIXME: And by 65536, we really mean 65535. If 0xffff is written to
240 * ctrlr1, all bytes that we see in rxdr end up being 0x00. 0xffff - 1
241 * seems to work fine.
243 while (bytes_out
|| bytes_in
) {
244 size_t in_now
= MIN(bytes_in
, 0xfffe);
245 size_t out_now
= MIN(bytes_out
, 0xfffe);
246 size_t in_rem
, out_rem
;
250 rockchip_spi_enable_chip(regs
, 0);
253 * Use 16-bit transfers for higher-speed reads. If we are
254 * transferring an odd number of bytes, try to make it even.
257 if (bytes_out
== 0) {
258 if ((in_now
& 1) && in_now
> 1)
263 mask
= SPI_APB_8BIT
<< SPI_HALF_WORLD_TX_OFFSET
;
265 clrbits32(®s
->ctrlr0
, mask
);
267 setbits32(®s
->ctrlr0
, mask
);
269 /* Enable/disable transmitter and receiver as needed to
270 * avoid sending or reading spurious bits. */
271 set_transfer_mode(regs
, bytes_out
, bytes_in
);
273 /* MAX() in case either counter is 0 */
274 write32(®s
->ctrlr1
, MAX(in_now
, out_now
) - 1);
276 rockchip_spi_enable_chip(regs
, 1);
280 ret
= do_xfer(regs
, use_16bit
, dout
, &out_rem
, din
, &in_rem
);
285 size_t sent
= out_now
- out_rem
;
291 size_t received
= in_now
- in_rem
;
292 bytes_in
-= received
;
297 rockchip_spi_enable_chip(regs
, 0);
298 return ret
< 0 ? ret
: 0;
301 static const struct spi_ctrlr spi_ctrlr
= {
302 .claim_bus
= spi_ctrlr_claim_bus
,
303 .release_bus
= spi_ctrlr_release_bus
,
304 .xfer
= spi_ctrlr_xfer
,
305 .max_xfer_size
= 65535,
308 const struct spi_ctrlr_buses spi_ctrlr_bus_map
[] = {
312 .bus_end
= ARRAY_SIZE(rockchip_spi_slaves
) - 1,
316 const size_t spi_ctrlr_bus_map_count
= ARRAY_SIZE(spi_ctrlr_bus_map
);