blob: e6b602cf7b47b79e59be4ad762a12373940669bb [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
148#define RPC_PHYINT 0x0088 /* R/W */
149#define RPC_PHYINT_RSTEN BIT(18)
150#define RPC_PHYINT_WPEN BIT(17)
151#define RPC_PHYINT_INTEN BIT(16)
152#define RPC_PHYINT_RST BIT(2)
153#define RPC_PHYINT_WP BIT(1)
154#define RPC_PHYINT_INT BIT(0)
155
156#define RPC_WBUF 0x8000 /* R/W size=4/8/16/32/64Bytes */
157#define RPC_WBUF_SIZE 0x100
158
159DECLARE_GLOBAL_DATA_PTR;
160
Simon Glassb75b15b2020-12-03 16:55:23 -0700161struct rpc_spi_plat {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100162 fdt_addr_t regs;
163 fdt_addr_t extr;
164 s32 freq; /* Default clock freq, -1 for none */
165};
166
167struct rpc_spi_priv {
168 fdt_addr_t regs;
169 fdt_addr_t extr;
170 struct clk clk;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100171};
172
173static int rpc_spi_wait_sslf(struct udevice *dev)
174{
175 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
176
177 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_SSLF,
178 false, 1000, false);
179}
180
181static int rpc_spi_wait_tend(struct udevice *dev)
182{
183 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
184
185 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_TEND,
186 true, 1000, false);
187}
188
189static void rpc_spi_flush_read_cache(struct udevice *dev)
190{
191 struct udevice *bus = dev->parent;
192 struct rpc_spi_priv *priv = dev_get_priv(bus);
193
194 /* Flush read cache */
195 writel(RPC_DRCR_SSLN | RPC_DRCR_RBURST(0x1f) |
196 RPC_DRCR_RCF | RPC_DRCR_RBE | RPC_DRCR_SSLE,
197 priv->regs + RPC_DRCR);
198 readl(priv->regs + RPC_DRCR);
199
200}
201
Hai Pham286ed072021-08-16 09:26:36 +0700202static u32 rpc_spi_get_strobe_delay(void)
203{
204#ifndef CONFIG_RZA1
Marek Vasut30fe98e2024-02-27 17:05:45 +0100205 u32 cpu_type = renesas_get_cpu_type();
Hai Pham286ed072021-08-16 09:26:36 +0700206
207 /*
208 * NOTE: RPC_PHYCNT_STRTIM value:
209 * 0: On H3 ES1.x (not supported in mainline U-Boot)
210 * 6: On M3 ES1.x
211 * 7: On other R-Car Gen3
Hai Pham581d2532021-08-05 14:38:26 +0700212 * 15: On R-Car Gen4
Hai Pham286ed072021-08-16 09:26:36 +0700213 */
Marek Vasutf9726612024-02-27 17:05:47 +0100214 if (cpu_type == RENESAS_CPU_TYPE_R8A7796 && renesas_get_cpu_rev_integer() == 1)
Hai Pham286ed072021-08-16 09:26:36 +0700215 return RPC_PHYCNT_STRTIM(6);
Marek Vasutf9726612024-02-27 17:05:47 +0100216 else if (cpu_type == RENESAS_CPU_TYPE_R8A779F0 ||
217 cpu_type == RENESAS_CPU_TYPE_R8A779G0 ||
218 cpu_type == RENESAS_CPU_TYPE_R8A779H0)
Hai Pham581d2532021-08-05 14:38:26 +0700219 return RPC_PHYCNT_STRTIM2(15);
Hai Pham286ed072021-08-16 09:26:36 +0700220 else
221#endif
222 return RPC_PHYCNT_STRTIM(7);
223}
224
Marek Vasut6ca967b2017-11-29 06:29:46 +0100225static int rpc_spi_claim_bus(struct udevice *dev, bool manual)
226{
227 struct udevice *bus = dev->parent;
228 struct rpc_spi_priv *priv = dev_get_priv(bus);
229
Hai Pham286ed072021-08-16 09:26:36 +0700230 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
231 writel(RPC_PHYCNT_CAL | rpc_spi_get_strobe_delay() | 0x260,
Marek Vasut6ca967b2017-11-29 06:29:46 +0100232 priv->regs + RPC_PHYCNT);
233 writel((manual ? RPC_CMNCR_MD : 0) | RPC_CMNCR_SFDE |
234 RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ | RPC_CMNCR_BSZ(0),
235 priv->regs + RPC_CMNCR);
236
237 writel(RPC_SSLDR_SPNDL(7) | RPC_SSLDR_SLNDL(7) |
238 RPC_SSLDR_SCKDL(7), priv->regs + RPC_SSLDR);
239
240 rpc_spi_flush_read_cache(dev);
241
242 return 0;
243}
244
245static int rpc_spi_release_bus(struct udevice *dev)
246{
247 struct udevice *bus = dev->parent;
248 struct rpc_spi_priv *priv = dev_get_priv(bus);
249
250 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
Hai Pham286ed072021-08-16 09:26:36 +0700251 writel(rpc_spi_get_strobe_delay() | 0x260, priv->regs + RPC_PHYCNT);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100252
253 rpc_spi_flush_read_cache(dev);
254
255 return 0;
256}
257
Cong Dangce335632022-08-25 06:06:54 +0700258static int rpc_spi_mem_exec_op(struct spi_slave *spi,
259 const struct spi_mem_op *op)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100260{
Cong Dangce335632022-08-25 06:06:54 +0700261 struct udevice *bus = spi->dev->parent;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100262 struct rpc_spi_priv *priv = dev_get_priv(bus);
Cong Dangce335632022-08-25 06:06:54 +0700263 const void *dout = op->data.buf.out ? op->data.buf.out : NULL;
264 void *din = op->data.buf.in ? op->data.buf.in : NULL;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100265 int ret = 0;
Cong Dangce335632022-08-25 06:06:54 +0700266 u32 offset = 0;
267 u32 smenr, smcr;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100268
Cong Dangce335632022-08-25 06:06:54 +0700269 smenr = 0;
270 offset = op->addr.val;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100271
Cong Dangce335632022-08-25 06:06:54 +0700272 switch (op->data.dir) {
273 case SPI_MEM_DATA_IN:
274 rpc_spi_claim_bus(spi->dev, false);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100275
Cong Dangce335632022-08-25 06:06:54 +0700276 writel(0, priv->regs + RPC_DRCMR);
277 writel(RPC_DRCMR_CMD(op->cmd.opcode), priv->regs + RPC_DRCMR);
278 smenr |= RPC_DRENR_CDE;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100279
Cong Dangce335632022-08-25 06:06:54 +0700280 writel(0, priv->regs + RPC_DREAR);
281 if (op->addr.nbytes == 4) {
282 writel(RPC_DREAR_EAV(offset >> 25) | RPC_DREAR_EAC(1),
283 priv->regs + RPC_DREAR);
284 smenr |= RPC_DRENR_ADE(0xF);
285 } else if (op->addr.nbytes == 3) {
286 smenr |= RPC_DRENR_ADE(0x7);
287 } else {
288 smenr |= RPC_DRENR_ADE(0);
289 }
Marek Vasut6ca967b2017-11-29 06:29:46 +0100290
Cong Dangce335632022-08-25 06:06:54 +0700291 writel(0, priv->regs + RPC_DRDMCR);
292 if (op->dummy.nbytes) {
293 writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_DRDMCR);
294 smenr |= RPC_DRENR_DME;
295 }
Marek Vasut6ca967b2017-11-29 06:29:46 +0100296
Cong Dangce335632022-08-25 06:06:54 +0700297 writel(0, priv->regs + RPC_DROPR);
298 writel(smenr, priv->regs + RPC_DRENR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100299
Cong Dangce335632022-08-25 06:06:54 +0700300 memcpy_fromio(din, (void *)(priv->extr + offset), op->data.nbytes);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100301
Cong Dangce335632022-08-25 06:06:54 +0700302 rpc_spi_release_bus(spi->dev);
303 break;
304 case SPI_MEM_DATA_OUT:
305 case SPI_MEM_NO_DATA:
306 rpc_spi_claim_bus(spi->dev, true);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100307
Cong Dangce335632022-08-25 06:06:54 +0700308 writel(0, priv->regs + RPC_SMCR);
309 writel(0, priv->regs + RPC_SMCMR);
310 writel(RPC_SMCMR_CMD(op->cmd.opcode), priv->regs + RPC_SMCMR);
311 smenr |= RPC_SMENR_CDE;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100312
Cong Dangce335632022-08-25 06:06:54 +0700313 writel(0, priv->regs + RPC_SMADR);
314 if (op->addr.nbytes == 4)
315 smenr |= RPC_SMENR_ADE(0xF);
316 else if (op->addr.nbytes == 3)
317 smenr |= RPC_SMENR_ADE(0x7);
318 else
319 smenr |= RPC_SMENR_ADE(0);
320 writel(offset, priv->regs + RPC_SMADR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100321
Cong Dangce335632022-08-25 06:06:54 +0700322 writel(0, priv->regs + RPC_SMDMCR);
323 if (op->dummy.nbytes) {
324 writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_SMDMCR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100325 smenr |= RPC_SMENR_DME;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100326 }
327
328 writel(0, priv->regs + RPC_SMOPR);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100329 writel(0, priv->regs + RPC_SMDRENR);
330
Cong Dangce335632022-08-25 06:06:54 +0700331 if (dout && op->data.nbytes) {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100332 u32 *datout = (u32 *)dout;
Cong Dangce335632022-08-25 06:06:54 +0700333 u32 wloop = DIV_ROUND_UP(op->data.nbytes, 4);
334
335 smenr |= RPC_SMENR_SPIDE(0xF);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100336
337 while (wloop--) {
338 smcr = RPC_SMCR_SPIWE | RPC_SMCR_SPIE;
339 if (wloop >= 1)
340 smcr |= RPC_SMCR_SSLKP;
341 writel(smenr, priv->regs + RPC_SMENR);
342 writel(*datout, priv->regs + RPC_SMWDR0);
343 writel(smcr, priv->regs + RPC_SMCR);
Cong Dangce335632022-08-25 06:06:54 +0700344 ret = rpc_spi_wait_tend(spi->dev);
345 if (ret) {
346 rpc_spi_release_bus(spi->dev);
347 return ret;
348 }
Marek Vasut6ca967b2017-11-29 06:29:46 +0100349 datout++;
Cong Dangce335632022-08-25 06:06:54 +0700350 smenr &= (~RPC_SMENR_CDE & ~RPC_SMENR_ADE(0xF));
Marek Vasut6ca967b2017-11-29 06:29:46 +0100351 }
352
Cong Dangce335632022-08-25 06:06:54 +0700353 ret = rpc_spi_wait_sslf(spi->dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100354 } else {
355 writel(smenr, priv->regs + RPC_SMENR);
356 writel(RPC_SMCR_SPIE, priv->regs + RPC_SMCR);
Cong Dangce335632022-08-25 06:06:54 +0700357 ret = rpc_spi_wait_tend(spi->dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100358 }
359
Cong Dangce335632022-08-25 06:06:54 +0700360 rpc_spi_release_bus(spi->dev);
361 break;
362 default:
363 break;
Marek Vasut6ca967b2017-11-29 06:29:46 +0100364 }
365
Marek Vasut6ca967b2017-11-29 06:29:46 +0100366 return ret;
367}
368
369static int rpc_spi_set_speed(struct udevice *bus, uint speed)
370{
371 /* This is a SPI NOR controller, do nothing. */
372 return 0;
373}
374
375static int rpc_spi_set_mode(struct udevice *bus, uint mode)
376{
377 /* This is a SPI NOR controller, do nothing. */
378 return 0;
379}
380
Cong Dangce335632022-08-25 06:06:54 +0700381static const struct spi_controller_mem_ops rpc_spi_mem_ops = {
382 .exec_op = rpc_spi_mem_exec_op
383};
384
Marek Vasut6ca967b2017-11-29 06:29:46 +0100385static int rpc_spi_bind(struct udevice *parent)
386{
387 const void *fdt = gd->fdt_blob;
388 ofnode node;
389 int ret, off;
390
391 /*
392 * Check if there are any SPI NOR child nodes, if so, bind as
393 * this controller will be operated in SPI mode.
394 */
395 dev_for_each_subnode(node, parent) {
396 off = ofnode_to_offset(node);
397
398 ret = fdt_node_check_compatible(fdt, off, "spi-flash");
399 if (!ret)
400 return 0;
401
402 ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor");
403 if (!ret)
404 return 0;
405 }
406
407 return -ENODEV;
408}
409
410static int rpc_spi_probe(struct udevice *dev)
411{
Simon Glassb75b15b2020-12-03 16:55:23 -0700412 struct rpc_spi_plat *plat = dev_get_plat(dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100413 struct rpc_spi_priv *priv = dev_get_priv(dev);
414
415 priv->regs = plat->regs;
416 priv->extr = plat->extr;
Marek Vasutf9db3b32019-05-04 18:52:33 +0200417#if CONFIG_IS_ENABLED(CLK)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100418 clk_enable(&priv->clk);
Marek Vasutf9db3b32019-05-04 18:52:33 +0200419#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100420 return 0;
421}
422
Simon Glassaad29ae2020-12-03 16:55:21 -0700423static int rpc_spi_of_to_plat(struct udevice *bus)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100424{
Simon Glassb75b15b2020-12-03 16:55:23 -0700425 struct rpc_spi_plat *plat = dev_get_plat(bus);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100426
427 plat->regs = dev_read_addr_index(bus, 0);
428 plat->extr = dev_read_addr_index(bus, 1);
429
Marek Vasutf9db3b32019-05-04 18:52:33 +0200430#if CONFIG_IS_ENABLED(CLK)
431 struct rpc_spi_priv *priv = dev_get_priv(bus);
432 int ret;
433
Marek Vasut6ca967b2017-11-29 06:29:46 +0100434 ret = clk_get_by_index(bus, 0, &priv->clk);
435 if (ret < 0) {
436 printf("%s: Could not get clock for %s: %d\n",
437 __func__, bus->name, ret);
438 return ret;
439 }
Marek Vasutf9db3b32019-05-04 18:52:33 +0200440#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100441
442 plat->freq = dev_read_u32_default(bus, "spi-max-freq", 50000000);
443
444 return 0;
445}
446
447static const struct dm_spi_ops rpc_spi_ops = {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100448 .set_speed = rpc_spi_set_speed,
449 .set_mode = rpc_spi_set_mode,
Cong Dangce335632022-08-25 06:06:54 +0700450 .mem_ops = &rpc_spi_mem_ops
Marek Vasut6ca967b2017-11-29 06:29:46 +0100451};
452
453static const struct udevice_id rpc_spi_ids[] = {
Geert Uytterhoeven431ded62022-03-29 14:19:09 +0200454 { .compatible = "renesas,r7s72100-rpc-if" },
455 { .compatible = "renesas,rcar-gen3-rpc-if" },
Marek Vasut6ca967b2017-11-29 06:29:46 +0100456 { }
457};
458
459U_BOOT_DRIVER(rpc_spi) = {
460 .name = "rpc_spi",
461 .id = UCLASS_SPI,
462 .of_match = rpc_spi_ids,
463 .ops = &rpc_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700464 .of_to_plat = rpc_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700465 .plat_auto = sizeof(struct rpc_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700466 .priv_auto = sizeof(struct rpc_spi_priv),
Marek Vasut6ca967b2017-11-29 06:29:46 +0100467 .bind = rpc_spi_bind,
468 .probe = rpc_spi_probe,
469};