blob: f1e6f9f4e011023ee06d9a1ab249ba1819f5ef4b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut6ca967b2017-11-29 06:29:46 +01002/*
3 * Renesas RCar Gen3 RPC QSPI driver
4 *
5 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
Marek Vasut6ca967b2017-11-29 06:29:46 +01006 */
7
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Marek Vasut6ca967b2017-11-29 06:29:46 +01009#include <asm/io.h>
10#include <clk.h>
11#include <dm.h>
12#include <dm/of_access.h>
13#include <dt-structs.h>
14#include <errno.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060016#include <linux/bug.h>
Marek Vasut6ca967b2017-11-29 06:29:46 +010017#include <linux/errno.h>
18#include <spi.h>
Cong Dangce335632022-08-25 06:06:54 +070019#include <spi-mem.h>
Marek Vasut6ca967b2017-11-29 06:29:46 +010020#include <wait_bit.h>
21
22#define RPC_CMNCR 0x0000 /* R/W */
23#define RPC_CMNCR_MD BIT(31)
24#define RPC_CMNCR_SFDE BIT(24)
25#define RPC_CMNCR_MOIIO3(val) (((val) & 0x3) << 22)
26#define RPC_CMNCR_MOIIO2(val) (((val) & 0x3) << 20)
27#define RPC_CMNCR_MOIIO1(val) (((val) & 0x3) << 18)
28#define RPC_CMNCR_MOIIO0(val) (((val) & 0x3) << 16)
29#define RPC_CMNCR_MOIIO_HIZ (RPC_CMNCR_MOIIO0(3) | RPC_CMNCR_MOIIO1(3) | \
30 RPC_CMNCR_MOIIO2(3) | RPC_CMNCR_MOIIO3(3))
31#define RPC_CMNCR_IO3FV(val) (((val) & 0x3) << 14)
32#define RPC_CMNCR_IO2FV(val) (((val) & 0x3) << 12)
33#define RPC_CMNCR_IO0FV(val) (((val) & 0x3) << 8)
34#define RPC_CMNCR_IOFV_HIZ (RPC_CMNCR_IO0FV(3) | RPC_CMNCR_IO2FV(3) | \
35 RPC_CMNCR_IO3FV(3))
36#define RPC_CMNCR_CPHAT BIT(6)
37#define RPC_CMNCR_CPHAR BIT(5)
38#define RPC_CMNCR_SSLP BIT(4)
39#define RPC_CMNCR_CPOL BIT(3)
40#define RPC_CMNCR_BSZ(val) (((val) & 0x3) << 0)
41
42#define RPC_SSLDR 0x0004 /* R/W */
43#define RPC_SSLDR_SPNDL(d) (((d) & 0x7) << 16)
44#define RPC_SSLDR_SLNDL(d) (((d) & 0x7) << 8)
45#define RPC_SSLDR_SCKDL(d) (((d) & 0x7) << 0)
46
47#define RPC_DRCR 0x000C /* R/W */
48#define RPC_DRCR_SSLN BIT(24)
49#define RPC_DRCR_RBURST(v) (((v) & 0x1F) << 16)
50#define RPC_DRCR_RCF BIT(9)
51#define RPC_DRCR_RBE BIT(8)
52#define RPC_DRCR_SSLE BIT(0)
53
54#define RPC_DRCMR 0x0010 /* R/W */
55#define RPC_DRCMR_CMD(c) (((c) & 0xFF) << 16)
56#define RPC_DRCMR_OCMD(c) (((c) & 0xFF) << 0)
57
58#define RPC_DREAR 0x0014 /* R/W */
59#define RPC_DREAR_EAV(v) (((v) & 0xFF) << 16)
60#define RPC_DREAR_EAC(v) (((v) & 0x7) << 0)
61
62#define RPC_DROPR 0x0018 /* R/W */
63#define RPC_DROPR_OPD3(o) (((o) & 0xFF) << 24)
64#define RPC_DROPR_OPD2(o) (((o) & 0xFF) << 16)
65#define RPC_DROPR_OPD1(o) (((o) & 0xFF) << 8)
66#define RPC_DROPR_OPD0(o) (((o) & 0xFF) << 0)
67
68#define RPC_DRENR 0x001C /* R/W */
69#define RPC_DRENR_CDB(o) (u32)((((o) & 0x3) << 30))
70#define RPC_DRENR_OCDB(o) (((o) & 0x3) << 28)
71#define RPC_DRENR_ADB(o) (((o) & 0x3) << 24)
72#define RPC_DRENR_OPDB(o) (((o) & 0x3) << 20)
73#define RPC_DRENR_SPIDB(o) (((o) & 0x3) << 16)
74#define RPC_DRENR_DME BIT(15)
75#define RPC_DRENR_CDE BIT(14)
76#define RPC_DRENR_OCDE BIT(12)
77#define RPC_DRENR_ADE(v) (((v) & 0xF) << 8)
78#define RPC_DRENR_OPDE(v) (((v) & 0xF) << 4)
79
80#define RPC_SMCR 0x0020 /* R/W */
81#define RPC_SMCR_SSLKP BIT(8)
82#define RPC_SMCR_SPIRE BIT(2)
83#define RPC_SMCR_SPIWE BIT(1)
84#define RPC_SMCR_SPIE BIT(0)
85
86#define RPC_SMCMR 0x0024 /* R/W */
87#define RPC_SMCMR_CMD(c) (((c) & 0xFF) << 16)
88#define RPC_SMCMR_OCMD(c) (((c) & 0xFF) << 0)
89
90#define RPC_SMADR 0x0028 /* R/W */
91#define RPC_SMOPR 0x002C /* R/W */
92#define RPC_SMOPR_OPD0(o) (((o) & 0xFF) << 0)
93#define RPC_SMOPR_OPD1(o) (((o) & 0xFF) << 8)
94#define RPC_SMOPR_OPD2(o) (((o) & 0xFF) << 16)
95#define RPC_SMOPR_OPD3(o) (((o) & 0xFF) << 24)
96
97#define RPC_SMENR 0x0030 /* R/W */
98#define RPC_SMENR_CDB(o) (((o) & 0x3) << 30)
99#define RPC_SMENR_OCDB(o) (((o) & 0x3) << 28)
100#define RPC_SMENR_ADB(o) (((o) & 0x3) << 24)
101#define RPC_SMENR_OPDB(o) (((o) & 0x3) << 20)
102#define RPC_SMENR_SPIDB(o) (((o) & 0x3) << 16)
103#define RPC_SMENR_DME BIT(15)
104#define RPC_SMENR_CDE BIT(14)
105#define RPC_SMENR_OCDE BIT(12)
106#define RPC_SMENR_ADE(v) (((v) & 0xF) << 8)
107#define RPC_SMENR_OPDE(v) (((v) & 0xF) << 4)
108#define RPC_SMENR_SPIDE(v) (((v) & 0xF) << 0)
109
110#define RPC_SMRDR0 0x0038 /* R */
111#define RPC_SMRDR1 0x003C /* R */
112#define RPC_SMWDR0 0x0040 /* R/W */
113#define RPC_SMWDR1 0x0044 /* R/W */
114#define RPC_CMNSR 0x0048 /* R */
115#define RPC_CMNSR_SSLF BIT(1)
116#define RPC_CMNSR_TEND BIT(0)
117
118#define RPC_DRDMCR 0x0058 /* R/W */
119#define RPC_DRDMCR_DMCYC(v) (((v) & 0xF) << 0)
120
121#define RPC_DRDRENR 0x005C /* R/W */
122#define RPC_DRDRENR_HYPE (0x5 << 12)
123#define RPC_DRDRENR_ADDRE BIT(8)
124#define RPC_DRDRENR_OPDRE BIT(4)
125#define RPC_DRDRENR_DRDRE BIT(0)
126
127#define RPC_SMDMCR 0x0060 /* R/W */
128#define RPC_SMDMCR_DMCYC(v) (((v) & 0xF) << 0)
129
130#define RPC_SMDRENR 0x0064 /* R/W */
131#define RPC_SMDRENR_HYPE (0x5 << 12)
132#define RPC_SMDRENR_ADDRE BIT(8)
133#define RPC_SMDRENR_OPDRE BIT(4)
134#define RPC_SMDRENR_SPIDRE BIT(0)
135
136#define RPC_PHYCNT 0x007C /* R/W */
137#define RPC_PHYCNT_CAL BIT(31)
138#define PRC_PHYCNT_OCTA_AA BIT(22)
139#define PRC_PHYCNT_OCTA_SA BIT(23)
140#define PRC_PHYCNT_EXDS BIT(21)
141#define RPC_PHYCNT_OCT BIT(20)
142#define RPC_PHYCNT_STRTIM(v) (((v) & 0x7) << 15)
Hai Pham581d2532021-08-05 14:38:26 +0700143#define RPC_PHYCNT_STRTIM2(v) ((((v) & 0x7) << 15) | (((v) & 0x8) << 24))
Marek Vasut6ca967b2017-11-29 06:29:46 +0100144#define RPC_PHYCNT_WBUF2 BIT(4)
145#define RPC_PHYCNT_WBUF BIT(2)
146#define RPC_PHYCNT_MEM(v) (((v) & 0x3) << 0)
147
Marek Vasut9e274792024-08-31 22:31:47 +0200148#define RPCIF_PHYOFFSET1 0x0080 /* R/W */
149#define RPCIF_PHYOFFSET1_DDRTMG(v) (((v) & 0x3) << 28)
150
151#define RPCIF_PHYOFFSET2 0x0084 /* R/W */
152#define RPCIF_PHYOFFSET2_OCTTMG(v) (((v) & 0x7) << 8)
153
Marek Vasut6ca967b2017-11-29 06:29:46 +0100154#define RPC_PHYINT 0x0088 /* R/W */
155#define RPC_PHYINT_RSTEN BIT(18)
156#define RPC_PHYINT_WPEN BIT(17)
157#define RPC_PHYINT_INTEN BIT(16)
158#define RPC_PHYINT_RST BIT(2)
159#define RPC_PHYINT_WP BIT(1)
160#define RPC_PHYINT_INT BIT(0)
161
162#define RPC_WBUF 0x8000 /* R/W size=4/8/16/32/64Bytes */
163#define RPC_WBUF_SIZE 0x100
164
165DECLARE_GLOBAL_DATA_PTR;
166
Simon Glassb75b15b2020-12-03 16:55:23 -0700167struct rpc_spi_plat {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100168 fdt_addr_t regs;
169 fdt_addr_t extr;
170 s32 freq; /* Default clock freq, -1 for none */
171};
172
173struct rpc_spi_priv {
174 fdt_addr_t regs;
175 fdt_addr_t extr;
176 struct clk clk;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100177};
178
179static int rpc_spi_wait_sslf(struct udevice *dev)
180{
181 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
182
183 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_SSLF,
184 false, 1000, false);
185}
186
187static int rpc_spi_wait_tend(struct udevice *dev)
188{
189 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
190
191 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_TEND,
192 true, 1000, false);
193}
194
195static void rpc_spi_flush_read_cache(struct udevice *dev)
196{
197 struct udevice *bus = dev->parent;
198 struct rpc_spi_priv *priv = dev_get_priv(bus);
199
200 /* Flush read cache */
201 writel(RPC_DRCR_SSLN | RPC_DRCR_RBURST(0x1f) |
202 RPC_DRCR_RCF | RPC_DRCR_RBE | RPC_DRCR_SSLE,
203 priv->regs + RPC_DRCR);
204 readl(priv->regs + RPC_DRCR);
205
206}
207
Hai Pham286ed072021-08-16 09:26:36 +0700208static u32 rpc_spi_get_strobe_delay(void)
209{
210#ifndef CONFIG_RZA1
Marek Vasut30fe98e2024-02-27 17:05:45 +0100211 u32 cpu_type = renesas_get_cpu_type();
Hai Pham286ed072021-08-16 09:26:36 +0700212
213 /*
214 * NOTE: RPC_PHYCNT_STRTIM value:
215 * 0: On H3 ES1.x (not supported in mainline U-Boot)
216 * 6: On M3 ES1.x
217 * 7: On other R-Car Gen3
Hai Pham581d2532021-08-05 14:38:26 +0700218 * 15: On R-Car Gen4
Hai Pham286ed072021-08-16 09:26:36 +0700219 */
Marek Vasutf9726612024-02-27 17:05:47 +0100220 if (cpu_type == RENESAS_CPU_TYPE_R8A7796 && renesas_get_cpu_rev_integer() == 1)
Hai Pham286ed072021-08-16 09:26:36 +0700221 return RPC_PHYCNT_STRTIM(6);
Marek Vasutf9726612024-02-27 17:05:47 +0100222 else if (cpu_type == RENESAS_CPU_TYPE_R8A779F0 ||
223 cpu_type == RENESAS_CPU_TYPE_R8A779G0 ||
224 cpu_type == RENESAS_CPU_TYPE_R8A779H0)
Hai Pham581d2532021-08-05 14:38:26 +0700225 return RPC_PHYCNT_STRTIM2(15);
Hai Pham286ed072021-08-16 09:26:36 +0700226 else
227#endif
228 return RPC_PHYCNT_STRTIM(7);
229}
230
Marek Vasut6ca967b2017-11-29 06:29:46 +0100231static int rpc_spi_claim_bus(struct udevice *dev, bool manual)
232{
233 struct udevice *bus = dev->parent;
234 struct rpc_spi_priv *priv = dev_get_priv(bus);
235
Marek Vasut9e274792024-08-31 22:31:47 +0200236 setbits_le32(priv->regs + RPCIF_PHYOFFSET1,
237 RPCIF_PHYOFFSET1_DDRTMG(3));
238 clrsetbits_le32(priv->regs + RPCIF_PHYOFFSET2,
239 RPCIF_PHYOFFSET2_OCTTMG(7),
240 RPCIF_PHYOFFSET2_OCTTMG(4));
241
Hai Pham286ed072021-08-16 09:26:36 +0700242 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
243 writel(RPC_PHYCNT_CAL | rpc_spi_get_strobe_delay() | 0x260,
Marek Vasut6ca967b2017-11-29 06:29:46 +0100244 priv->regs + RPC_PHYCNT);
245 writel((manual ? RPC_CMNCR_MD : 0) | RPC_CMNCR_SFDE |
246 RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ | RPC_CMNCR_BSZ(0),
247 priv->regs + RPC_CMNCR);
248
249 writel(RPC_SSLDR_SPNDL(7) | RPC_SSLDR_SLNDL(7) |
250 RPC_SSLDR_SCKDL(7), priv->regs + RPC_SSLDR);
251
252 rpc_spi_flush_read_cache(dev);
253
254 return 0;
255}
256
257static int rpc_spi_release_bus(struct udevice *dev)
258{
259 struct udevice *bus = dev->parent;
260 struct rpc_spi_priv *priv = dev_get_priv(bus);
261
262 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
Hai Pham286ed072021-08-16 09:26:36 +0700263 writel(rpc_spi_get_strobe_delay() | 0x260, priv->regs + RPC_PHYCNT);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100264
265 rpc_spi_flush_read_cache(dev);
266
267 return 0;
268}
269
Cong Dangce335632022-08-25 06:06:54 +0700270static int rpc_spi_mem_exec_op(struct spi_slave *spi,
271 const struct spi_mem_op *op)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100272{
Cong Dangce335632022-08-25 06:06:54 +0700273 struct udevice *bus = spi->dev->parent;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100274 struct rpc_spi_priv *priv = dev_get_priv(bus);
Cong Dangce335632022-08-25 06:06:54 +0700275 const void *dout = op->data.buf.out ? op->data.buf.out : NULL;
276 void *din = op->data.buf.in ? op->data.buf.in : NULL;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100277 int ret = 0;
Cong Dangce335632022-08-25 06:06:54 +0700278 u32 offset = 0;
279 u32 smenr, smcr;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100280
Cong Dangce335632022-08-25 06:06:54 +0700281 smenr = 0;
282 offset = op->addr.val;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100283
Cong Dangce335632022-08-25 06:06:54 +0700284 switch (op->data.dir) {
285 case SPI_MEM_DATA_IN:
286 rpc_spi_claim_bus(spi->dev, false);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100287
Cong Dangce335632022-08-25 06:06:54 +0700288 writel(0, priv->regs + RPC_DRCMR);
289 writel(RPC_DRCMR_CMD(op->cmd.opcode), priv->regs + RPC_DRCMR);
290 smenr |= RPC_DRENR_CDE;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100291
Cong Dangce335632022-08-25 06:06:54 +0700292 if (op->addr.nbytes == 4) {
293 writel(RPC_DREAR_EAV(offset >> 25) | RPC_DREAR_EAC(1),
294 priv->regs + RPC_DREAR);
295 smenr |= RPC_DRENR_ADE(0xF);
296 } else if (op->addr.nbytes == 3) {
Marek Vasutbe610152024-08-31 22:31:44 +0200297 writel(0, priv->regs + RPC_DREAR);
Cong Dangce335632022-08-25 06:06:54 +0700298 smenr |= RPC_DRENR_ADE(0x7);
299 } else {
Marek Vasutbe610152024-08-31 22:31:44 +0200300 writel(0, priv->regs + RPC_DREAR);
Cong Dangce335632022-08-25 06:06:54 +0700301 smenr |= RPC_DRENR_ADE(0);
302 }
Marek Vasut6ca967b2017-11-29 06:29:46 +0100303
Marek Vasut4d799b62024-08-31 22:31:45 +0200304 if (op->dummy.nbytes)
Cong Dangce335632022-08-25 06:06:54 +0700305 smenr |= RPC_DRENR_DME;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100306
Marek Vasut4d799b62024-08-31 22:31:45 +0200307 writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_DRDMCR);
Cong Dangce335632022-08-25 06:06:54 +0700308 writel(0, priv->regs + RPC_DROPR);
Marek Vasutfec46b92024-08-31 22:31:46 +0200309 writel(0, priv->regs + RPC_DRDRENR);
Cong Dangce335632022-08-25 06:06:54 +0700310 writel(smenr, priv->regs + RPC_DRENR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100311
Cong Dangce335632022-08-25 06:06:54 +0700312 memcpy_fromio(din, (void *)(priv->extr + offset), op->data.nbytes);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100313
Cong Dangce335632022-08-25 06:06:54 +0700314 rpc_spi_release_bus(spi->dev);
315 break;
316 case SPI_MEM_DATA_OUT:
317 case SPI_MEM_NO_DATA:
318 rpc_spi_claim_bus(spi->dev, true);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100319
Cong Dangce335632022-08-25 06:06:54 +0700320 writel(0, priv->regs + RPC_SMCR);
321 writel(0, priv->regs + RPC_SMCMR);
322 writel(RPC_SMCMR_CMD(op->cmd.opcode), priv->regs + RPC_SMCMR);
323 smenr |= RPC_SMENR_CDE;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100324
Cong Dangce335632022-08-25 06:06:54 +0700325 writel(0, priv->regs + RPC_SMADR);
326 if (op->addr.nbytes == 4)
327 smenr |= RPC_SMENR_ADE(0xF);
328 else if (op->addr.nbytes == 3)
329 smenr |= RPC_SMENR_ADE(0x7);
330 else
331 smenr |= RPC_SMENR_ADE(0);
332 writel(offset, priv->regs + RPC_SMADR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100333
Cong Dangce335632022-08-25 06:06:54 +0700334 writel(0, priv->regs + RPC_SMDMCR);
335 if (op->dummy.nbytes) {
336 writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_SMDMCR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100337 smenr |= RPC_SMENR_DME;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100338 }
339
340 writel(0, priv->regs + RPC_SMOPR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100341 writel(0, priv->regs + RPC_SMDRENR);
342
Cong Dangce335632022-08-25 06:06:54 +0700343 if (dout && op->data.nbytes) {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100344 u32 *datout = (u32 *)dout;
Cong Dangce335632022-08-25 06:06:54 +0700345 u32 wloop = DIV_ROUND_UP(op->data.nbytes, 4);
346
347 smenr |= RPC_SMENR_SPIDE(0xF);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100348
349 while (wloop--) {
350 smcr = RPC_SMCR_SPIWE | RPC_SMCR_SPIE;
351 if (wloop >= 1)
352 smcr |= RPC_SMCR_SSLKP;
353 writel(smenr, priv->regs + RPC_SMENR);
354 writel(*datout, priv->regs + RPC_SMWDR0);
355 writel(smcr, priv->regs + RPC_SMCR);
Cong Dangce335632022-08-25 06:06:54 +0700356 ret = rpc_spi_wait_tend(spi->dev);
357 if (ret) {
358 rpc_spi_release_bus(spi->dev);
359 return ret;
360 }
Marek Vasut6ca967b2017-11-29 06:29:46 +0100361 datout++;
Cong Dangce335632022-08-25 06:06:54 +0700362 smenr &= (~RPC_SMENR_CDE & ~RPC_SMENR_ADE(0xF));
Marek Vasut6ca967b2017-11-29 06:29:46 +0100363 }
364
Cong Dangce335632022-08-25 06:06:54 +0700365 ret = rpc_spi_wait_sslf(spi->dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100366 } else {
367 writel(smenr, priv->regs + RPC_SMENR);
368 writel(RPC_SMCR_SPIE, priv->regs + RPC_SMCR);
Cong Dangce335632022-08-25 06:06:54 +0700369 ret = rpc_spi_wait_tend(spi->dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100370 }
371
Cong Dangce335632022-08-25 06:06:54 +0700372 rpc_spi_release_bus(spi->dev);
373 break;
374 default:
375 break;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100376 }
377
Marek Vasut6ca967b2017-11-29 06:29:46 +0100378 return ret;
379}
380
381static int rpc_spi_set_speed(struct udevice *bus, uint speed)
382{
383 /* This is a SPI NOR controller, do nothing. */
384 return 0;
385}
386
387static int rpc_spi_set_mode(struct udevice *bus, uint mode)
388{
389 /* This is a SPI NOR controller, do nothing. */
390 return 0;
391}
392
Cong Dangce335632022-08-25 06:06:54 +0700393static const struct spi_controller_mem_ops rpc_spi_mem_ops = {
394 .exec_op = rpc_spi_mem_exec_op
395};
396
Marek Vasut6ca967b2017-11-29 06:29:46 +0100397static int rpc_spi_bind(struct udevice *parent)
398{
399 const void *fdt = gd->fdt_blob;
400 ofnode node;
401 int ret, off;
402
403 /*
404 * Check if there are any SPI NOR child nodes, if so, bind as
405 * this controller will be operated in SPI mode.
406 */
407 dev_for_each_subnode(node, parent) {
408 off = ofnode_to_offset(node);
409
410 ret = fdt_node_check_compatible(fdt, off, "spi-flash");
411 if (!ret)
412 return 0;
413
414 ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor");
415 if (!ret)
416 return 0;
417 }
418
419 return -ENODEV;
420}
421
422static int rpc_spi_probe(struct udevice *dev)
423{
Simon Glassb75b15b2020-12-03 16:55:23 -0700424 struct rpc_spi_plat *plat = dev_get_plat(dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100425 struct rpc_spi_priv *priv = dev_get_priv(dev);
426
427 priv->regs = plat->regs;
428 priv->extr = plat->extr;
Marek Vasutf9db3b32019-05-04 18:52:33 +0200429#if CONFIG_IS_ENABLED(CLK)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100430 clk_enable(&priv->clk);
Marek Vasutf9db3b32019-05-04 18:52:33 +0200431#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100432 return 0;
433}
434
Simon Glassaad29ae2020-12-03 16:55:21 -0700435static int rpc_spi_of_to_plat(struct udevice *bus)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100436{
Simon Glassb75b15b2020-12-03 16:55:23 -0700437 struct rpc_spi_plat *plat = dev_get_plat(bus);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100438
439 plat->regs = dev_read_addr_index(bus, 0);
440 plat->extr = dev_read_addr_index(bus, 1);
441
Marek Vasutf9db3b32019-05-04 18:52:33 +0200442#if CONFIG_IS_ENABLED(CLK)
443 struct rpc_spi_priv *priv = dev_get_priv(bus);
444 int ret;
445
Marek Vasut6ca967b2017-11-29 06:29:46 +0100446 ret = clk_get_by_index(bus, 0, &priv->clk);
447 if (ret < 0) {
448 printf("%s: Could not get clock for %s: %d\n",
449 __func__, bus->name, ret);
450 return ret;
451 }
Marek Vasutf9db3b32019-05-04 18:52:33 +0200452#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100453
454 plat->freq = dev_read_u32_default(bus, "spi-max-freq", 50000000);
455
456 return 0;
457}
458
459static const struct dm_spi_ops rpc_spi_ops = {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100460 .set_speed = rpc_spi_set_speed,
461 .set_mode = rpc_spi_set_mode,
Cong Dangce335632022-08-25 06:06:54 +0700462 .mem_ops = &rpc_spi_mem_ops
Marek Vasut6ca967b2017-11-29 06:29:46 +0100463};
464
465static const struct udevice_id rpc_spi_ids[] = {
Geert Uytterhoeven431ded62022-03-29 14:19:09 +0200466 { .compatible = "renesas,r7s72100-rpc-if" },
467 { .compatible = "renesas,rcar-gen3-rpc-if" },
Marek Vasutdb7f76d2024-08-31 22:31:48 +0200468 { .compatible = "renesas,rcar-gen4-rpc-if" },
Marek Vasut6ca967b2017-11-29 06:29:46 +0100469 { }
470};
471
472U_BOOT_DRIVER(rpc_spi) = {
473 .name = "rpc_spi",
474 .id = UCLASS_SPI,
475 .of_match = rpc_spi_ids,
476 .ops = &rpc_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700477 .of_to_plat = rpc_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700478 .plat_auto = sizeof(struct rpc_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700479 .priv_auto = sizeof(struct rpc_spi_priv),
Marek Vasut6ca967b2017-11-29 06:29:46 +0100480 .bind = rpc_spi_bind,
481 .probe = rpc_spi_probe,
482};