blob: b7364a61929f19930cf6e4fb7ef3f4e5ac209921 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +09002/*
3 * SH QSPI (Quad SPI) driver
4 *
5 * Copyright (C) 2013 Renesas Electronics Corporation
6 * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +09007 */
8
Pengfei Fan05d13882022-12-09 09:39:51 +08009#define LOG_CATEGORY UCLASS_SPI
10
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090012#include <malloc.h>
13#include <spi.h>
Marek Vasutf6bdbcc2018-04-10 16:58:46 +020014#include <wait_bit.h>
Marek Vasut97a070b2024-02-27 17:05:54 +010015#include <asm/arch/renesas.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090016#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090018
19/* SH QSPI register bit masks <REG>_<BIT> */
20#define SPCR_MSTR 0x08
21#define SPCR_SPE 0x40
22#define SPSR_SPRFF 0x80
23#define SPSR_SPTEF 0x20
24#define SPPCR_IO3FV 0x04
25#define SPPCR_IO2FV 0x02
26#define SPPCR_IO1FV 0x01
Jagan Teki3857b342015-10-23 01:38:47 +053027#define SPBDCR_RXBC0 BIT(0)
28#define SPCMD_SCKDEN BIT(15)
29#define SPCMD_SLNDEN BIT(14)
30#define SPCMD_SPNDEN BIT(13)
31#define SPCMD_SSLKP BIT(7)
32#define SPCMD_BRDV0 BIT(2)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090033#define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \
34 SPCMD_SPNDEN | SPCMD_SSLKP | \
35 SPCMD_BRDV0
36#define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \
37 SPCMD_BRDV0
Jagan Teki3857b342015-10-23 01:38:47 +053038#define SPBFCR_TXRST BIT(7)
39#define SPBFCR_RXRST BIT(6)
Marek Vasut0bad3452018-04-10 16:43:47 +020040#define SPBFCR_TXTRG 0x30
41#define SPBFCR_RXTRG 0x07
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090042
43/* SH QSPI register set */
44struct sh_qspi_regs {
Marek Vasut6a1fe372018-04-10 16:47:38 +020045 u8 spcr;
46 u8 sslp;
47 u8 sppcr;
48 u8 spsr;
49 u32 spdr;
50 u8 spscr;
51 u8 spssr;
52 u8 spbr;
53 u8 spdcr;
54 u8 spckd;
55 u8 sslnd;
56 u8 spnd;
57 u8 dummy0;
58 u16 spcmd0;
59 u16 spcmd1;
60 u16 spcmd2;
61 u16 spcmd3;
62 u8 spbfcr;
63 u8 dummy1;
64 u16 spbdcr;
65 u32 spbmul0;
66 u32 spbmul1;
67 u32 spbmul2;
68 u32 spbmul3;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090069};
70
71struct sh_qspi_slave {
Lukasz Majewski76f442982020-06-04 23:11:53 +080072#if !CONFIG_IS_ENABLED(DM_SPI)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090073 struct spi_slave slave;
Marek Vasutd2da0452018-08-25 19:34:24 +020074#endif
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090075 struct sh_qspi_regs *regs;
76};
77
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090078static void sh_qspi_init(struct sh_qspi_slave *ss)
79{
80 /* QSPI initialize */
81 /* Set master mode only */
82 writeb(SPCR_MSTR, &ss->regs->spcr);
83
84 /* Set SSL signal level */
85 writeb(0x00, &ss->regs->sslp);
86
87 /* Set MOSI signal value when transfer is in idle state */
88 writeb(SPPCR_IO3FV|SPPCR_IO2FV, &ss->regs->sppcr);
89
90 /* Set bit rate. See 58.3.8 Quad Serial Peripheral Interface */
91 writeb(0x01, &ss->regs->spbr);
92
93 /* Disable Dummy Data Transmission */
94 writeb(0x00, &ss->regs->spdcr);
95
96 /* Set clock delay value */
97 writeb(0x00, &ss->regs->spckd);
98
99 /* Set SSL negation delay value */
100 writeb(0x00, &ss->regs->sslnd);
101
102 /* Set next-access delay value */
103 writeb(0x00, &ss->regs->spnd);
104
105 /* Set equence command */
106 writew(SPCMD_INIT2, &ss->regs->spcmd0);
107
108 /* Reset transfer and receive Buffer */
109 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
110
111 /* Clear transfer and receive Buffer control bit */
112 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
113
114 /* Set equence control method. Use equence0 only */
115 writeb(0x00, &ss->regs->spscr);
116
117 /* Enable SPI function */
118 setbits_8(&ss->regs->spcr, SPCR_SPE);
119}
120
Marek Vasutd2da0452018-08-25 19:34:24 +0200121static void sh_qspi_cs_activate(struct sh_qspi_slave *ss)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900122{
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900123 /* Set master mode only */
124 writeb(SPCR_MSTR, &ss->regs->spcr);
125
126 /* Set command */
127 writew(SPCMD_INIT1, &ss->regs->spcmd0);
128
129 /* Reset transfer and receive Buffer */
130 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
131
132 /* Clear transfer and receive Buffer control bit */
133 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
134
135 /* Set equence control method. Use equence0 only */
136 writeb(0x00, &ss->regs->spscr);
137
138 /* Enable SPI function */
139 setbits_8(&ss->regs->spcr, SPCR_SPE);
140}
141
Marek Vasutd2da0452018-08-25 19:34:24 +0200142static void sh_qspi_cs_deactivate(struct sh_qspi_slave *ss)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900143{
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900144 /* Disable SPI Function */
145 clrbits_8(&ss->regs->spcr, SPCR_SPE);
146}
147
Marek Vasutd2da0452018-08-25 19:34:24 +0200148static int sh_qspi_xfer_common(struct sh_qspi_slave *ss, unsigned int bitlen,
149 const void *dout, void *din, unsigned long flags)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900150{
Marek Vasut0bad3452018-04-10 16:43:47 +0200151 u32 nbyte, chunk;
152 int i, ret = 0;
Marek Vasut6a1fe372018-04-10 16:47:38 +0200153 u8 dtdata = 0, drdata;
154 u8 *tdata = &dtdata, *rdata = &drdata;
155 u32 *spbmul0 = &ss->regs->spbmul0;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900156
157 if (dout == NULL && din == NULL) {
158 if (flags & SPI_XFER_END)
Marek Vasutd2da0452018-08-25 19:34:24 +0200159 sh_qspi_cs_deactivate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900160 return 0;
161 }
162
163 if (bitlen % 8) {
Pengfei Fan05d13882022-12-09 09:39:51 +0800164 log_warning("bitlen is not 8bit aligned %d", bitlen);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900165 return 1;
166 }
167
168 nbyte = bitlen / 8;
169
170 if (flags & SPI_XFER_BEGIN) {
Marek Vasutd2da0452018-08-25 19:34:24 +0200171 sh_qspi_cs_activate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900172
173 /* Set 1048576 byte */
174 writel(0x100000, spbmul0);
175 }
176
177 if (flags & SPI_XFER_END)
178 writel(nbyte, spbmul0);
179
180 if (dout != NULL)
Marek Vasut6a1fe372018-04-10 16:47:38 +0200181 tdata = (u8 *)dout;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900182
183 if (din != NULL)
184 rdata = din;
185
186 while (nbyte > 0) {
Marek Vasut0bad3452018-04-10 16:43:47 +0200187 /*
188 * Check if there is 32 Byte chunk and if there is, transfer
189 * it in one burst, otherwise transfer on byte-by-byte basis.
190 */
191 chunk = (nbyte >= 32) ? 32 : 1;
192
193 clrsetbits_8(&ss->regs->spbfcr, SPBFCR_TXTRG | SPBFCR_RXTRG,
194 chunk == 32 ? SPBFCR_TXTRG | SPBFCR_RXTRG : 0);
195
Marek Vasutf6bdbcc2018-04-10 16:58:46 +0200196 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPTEF,
197 true, 1000, true);
198 if (ret)
199 return ret;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900200
Marek Vasut0bad3452018-04-10 16:43:47 +0200201 for (i = 0; i < chunk; i++) {
202 writeb(*tdata, &ss->regs->spdr);
203 if (dout != NULL)
204 tdata++;
205 }
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900206
Marek Vasutf6bdbcc2018-04-10 16:58:46 +0200207 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPRFF,
208 true, 1000, true);
209 if (ret)
210 return ret;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900211
Marek Vasut0bad3452018-04-10 16:43:47 +0200212 for (i = 0; i < chunk; i++) {
213 *rdata = readb(&ss->regs->spdr);
214 if (din != NULL)
215 rdata++;
216 }
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900217
Marek Vasut0bad3452018-04-10 16:43:47 +0200218 nbyte -= chunk;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900219 }
220
221 if (flags & SPI_XFER_END)
Marek Vasutd2da0452018-08-25 19:34:24 +0200222 sh_qspi_cs_deactivate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900223
224 return ret;
225}
Marek Vasutd2da0452018-08-25 19:34:24 +0200226
Lukasz Majewski76f442982020-06-04 23:11:53 +0800227#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutd2da0452018-08-25 19:34:24 +0200228static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave)
229{
230 return container_of(slave, struct sh_qspi_slave, slave);
231}
232
233int spi_cs_is_valid(unsigned int bus, unsigned int cs)
234{
235 return 1;
236}
237
238void spi_cs_activate(struct spi_slave *slave)
239{
240 struct sh_qspi_slave *ss = to_sh_qspi(slave);
241
242 sh_qspi_cs_activate(ss);
243}
244
245void spi_cs_deactivate(struct spi_slave *slave)
246{
247 struct sh_qspi_slave *ss = to_sh_qspi(slave);
248
249 sh_qspi_cs_deactivate(ss);
250}
251
Marek Vasutd2da0452018-08-25 19:34:24 +0200252struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
253 unsigned int max_hz, unsigned int mode)
254{
255 struct sh_qspi_slave *ss;
256
257 if (!spi_cs_is_valid(bus, cs))
258 return NULL;
259
260 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
261 if (!ss) {
262 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
263 return NULL;
264 }
265
266 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
267
268 /* Init SH QSPI */
269 sh_qspi_init(ss);
270
271 return &ss->slave;
272}
273
274void spi_free_slave(struct spi_slave *slave)
275{
276 struct sh_qspi_slave *spi = to_sh_qspi(slave);
277
278 free(spi);
279}
280
281int spi_claim_bus(struct spi_slave *slave)
282{
283 return 0;
284}
285
286void spi_release_bus(struct spi_slave *slave)
287{
288}
289
290int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
291 const void *dout, void *din, unsigned long flags)
292{
293 struct sh_qspi_slave *ss = to_sh_qspi(slave);
294
295 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
296}
297
298#else
299
300#include <dm.h>
301
302static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen,
303 const void *dout, void *din, unsigned long flags)
304{
305 struct udevice *bus = dev->parent;
Simon Glassfa20e932020-12-03 16:55:20 -0700306 struct sh_qspi_slave *ss = dev_get_plat(bus);
Marek Vasutd2da0452018-08-25 19:34:24 +0200307
308 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
309}
310
311static int sh_qspi_set_speed(struct udevice *dev, uint speed)
312{
313 /* This is a SPI NOR controller, do nothing. */
314 return 0;
315}
316
317static int sh_qspi_set_mode(struct udevice *dev, uint mode)
318{
319 /* This is a SPI NOR controller, do nothing. */
320 return 0;
321}
322
323static int sh_qspi_probe(struct udevice *dev)
324{
Simon Glassfa20e932020-12-03 16:55:20 -0700325 struct sh_qspi_slave *ss = dev_get_plat(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200326
327 sh_qspi_init(ss);
328
329 return 0;
330}
331
Simon Glassaad29ae2020-12-03 16:55:21 -0700332static int sh_qspi_of_to_plat(struct udevice *dev)
Marek Vasutd2da0452018-08-25 19:34:24 +0200333{
Simon Glassfa20e932020-12-03 16:55:20 -0700334 struct sh_qspi_slave *plat = dev_get_plat(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200335
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100336 plat->regs = dev_read_addr_ptr(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200337
338 return 0;
339}
340
341static const struct dm_spi_ops sh_qspi_ops = {
342 .xfer = sh_qspi_xfer,
343 .set_speed = sh_qspi_set_speed,
344 .set_mode = sh_qspi_set_mode,
345};
346
347static const struct udevice_id sh_qspi_ids[] = {
348 { .compatible = "renesas,qspi" },
349 { }
350};
351
352U_BOOT_DRIVER(sh_qspi) = {
353 .name = "sh_qspi",
354 .id = UCLASS_SPI,
355 .of_match = sh_qspi_ids,
356 .ops = &sh_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700357 .of_to_plat = sh_qspi_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700358 .plat_auto = sizeof(struct sh_qspi_slave),
Marek Vasutd2da0452018-08-25 19:34:24 +0200359 .probe = sh_qspi_probe,
360};
361#endif