blob: 72594993853bdd2048d99b3d17f88ab64d806c6e [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
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090011#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090013#include <malloc.h>
14#include <spi.h>
Marek Vasutf6bdbcc2018-04-10 16:58:46 +020015#include <wait_bit.h>
Marek Vasut97a070b2024-02-27 17:05:54 +010016#include <asm/arch/renesas.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090017#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090019
20/* SH QSPI register bit masks <REG>_<BIT> */
21#define SPCR_MSTR 0x08
22#define SPCR_SPE 0x40
23#define SPSR_SPRFF 0x80
24#define SPSR_SPTEF 0x20
25#define SPPCR_IO3FV 0x04
26#define SPPCR_IO2FV 0x02
27#define SPPCR_IO1FV 0x01
Jagan Teki3857b342015-10-23 01:38:47 +053028#define SPBDCR_RXBC0 BIT(0)
29#define SPCMD_SCKDEN BIT(15)
30#define SPCMD_SLNDEN BIT(14)
31#define SPCMD_SPNDEN BIT(13)
32#define SPCMD_SSLKP BIT(7)
33#define SPCMD_BRDV0 BIT(2)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090034#define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \
35 SPCMD_SPNDEN | SPCMD_SSLKP | \
36 SPCMD_BRDV0
37#define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \
38 SPCMD_BRDV0
Jagan Teki3857b342015-10-23 01:38:47 +053039#define SPBFCR_TXRST BIT(7)
40#define SPBFCR_RXRST BIT(6)
Marek Vasut0bad3452018-04-10 16:43:47 +020041#define SPBFCR_TXTRG 0x30
42#define SPBFCR_RXTRG 0x07
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090043
44/* SH QSPI register set */
45struct sh_qspi_regs {
Marek Vasut6a1fe372018-04-10 16:47:38 +020046 u8 spcr;
47 u8 sslp;
48 u8 sppcr;
49 u8 spsr;
50 u32 spdr;
51 u8 spscr;
52 u8 spssr;
53 u8 spbr;
54 u8 spdcr;
55 u8 spckd;
56 u8 sslnd;
57 u8 spnd;
58 u8 dummy0;
59 u16 spcmd0;
60 u16 spcmd1;
61 u16 spcmd2;
62 u16 spcmd3;
63 u8 spbfcr;
64 u8 dummy1;
65 u16 spbdcr;
66 u32 spbmul0;
67 u32 spbmul1;
68 u32 spbmul2;
69 u32 spbmul3;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090070};
71
72struct sh_qspi_slave {
Lukasz Majewski76f442982020-06-04 23:11:53 +080073#if !CONFIG_IS_ENABLED(DM_SPI)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090074 struct spi_slave slave;
Marek Vasutd2da0452018-08-25 19:34:24 +020075#endif
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090076 struct sh_qspi_regs *regs;
77};
78
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +090079static void sh_qspi_init(struct sh_qspi_slave *ss)
80{
81 /* QSPI initialize */
82 /* Set master mode only */
83 writeb(SPCR_MSTR, &ss->regs->spcr);
84
85 /* Set SSL signal level */
86 writeb(0x00, &ss->regs->sslp);
87
88 /* Set MOSI signal value when transfer is in idle state */
89 writeb(SPPCR_IO3FV|SPPCR_IO2FV, &ss->regs->sppcr);
90
91 /* Set bit rate. See 58.3.8 Quad Serial Peripheral Interface */
92 writeb(0x01, &ss->regs->spbr);
93
94 /* Disable Dummy Data Transmission */
95 writeb(0x00, &ss->regs->spdcr);
96
97 /* Set clock delay value */
98 writeb(0x00, &ss->regs->spckd);
99
100 /* Set SSL negation delay value */
101 writeb(0x00, &ss->regs->sslnd);
102
103 /* Set next-access delay value */
104 writeb(0x00, &ss->regs->spnd);
105
106 /* Set equence command */
107 writew(SPCMD_INIT2, &ss->regs->spcmd0);
108
109 /* Reset transfer and receive Buffer */
110 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
111
112 /* Clear transfer and receive Buffer control bit */
113 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
114
115 /* Set equence control method. Use equence0 only */
116 writeb(0x00, &ss->regs->spscr);
117
118 /* Enable SPI function */
119 setbits_8(&ss->regs->spcr, SPCR_SPE);
120}
121
Marek Vasutd2da0452018-08-25 19:34:24 +0200122static void sh_qspi_cs_activate(struct sh_qspi_slave *ss)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900123{
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900124 /* Set master mode only */
125 writeb(SPCR_MSTR, &ss->regs->spcr);
126
127 /* Set command */
128 writew(SPCMD_INIT1, &ss->regs->spcmd0);
129
130 /* Reset transfer and receive Buffer */
131 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
132
133 /* Clear transfer and receive Buffer control bit */
134 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
135
136 /* Set equence control method. Use equence0 only */
137 writeb(0x00, &ss->regs->spscr);
138
139 /* Enable SPI function */
140 setbits_8(&ss->regs->spcr, SPCR_SPE);
141}
142
Marek Vasutd2da0452018-08-25 19:34:24 +0200143static void sh_qspi_cs_deactivate(struct sh_qspi_slave *ss)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900144{
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900145 /* Disable SPI Function */
146 clrbits_8(&ss->regs->spcr, SPCR_SPE);
147}
148
Marek Vasutd2da0452018-08-25 19:34:24 +0200149static int sh_qspi_xfer_common(struct sh_qspi_slave *ss, unsigned int bitlen,
150 const void *dout, void *din, unsigned long flags)
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900151{
Marek Vasut0bad3452018-04-10 16:43:47 +0200152 u32 nbyte, chunk;
153 int i, ret = 0;
Marek Vasut6a1fe372018-04-10 16:47:38 +0200154 u8 dtdata = 0, drdata;
155 u8 *tdata = &dtdata, *rdata = &drdata;
156 u32 *spbmul0 = &ss->regs->spbmul0;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900157
158 if (dout == NULL && din == NULL) {
159 if (flags & SPI_XFER_END)
Marek Vasutd2da0452018-08-25 19:34:24 +0200160 sh_qspi_cs_deactivate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900161 return 0;
162 }
163
164 if (bitlen % 8) {
Pengfei Fan05d13882022-12-09 09:39:51 +0800165 log_warning("bitlen is not 8bit aligned %d", bitlen);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900166 return 1;
167 }
168
169 nbyte = bitlen / 8;
170
171 if (flags & SPI_XFER_BEGIN) {
Marek Vasutd2da0452018-08-25 19:34:24 +0200172 sh_qspi_cs_activate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900173
174 /* Set 1048576 byte */
175 writel(0x100000, spbmul0);
176 }
177
178 if (flags & SPI_XFER_END)
179 writel(nbyte, spbmul0);
180
181 if (dout != NULL)
Marek Vasut6a1fe372018-04-10 16:47:38 +0200182 tdata = (u8 *)dout;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900183
184 if (din != NULL)
185 rdata = din;
186
187 while (nbyte > 0) {
Marek Vasut0bad3452018-04-10 16:43:47 +0200188 /*
189 * Check if there is 32 Byte chunk and if there is, transfer
190 * it in one burst, otherwise transfer on byte-by-byte basis.
191 */
192 chunk = (nbyte >= 32) ? 32 : 1;
193
194 clrsetbits_8(&ss->regs->spbfcr, SPBFCR_TXTRG | SPBFCR_RXTRG,
195 chunk == 32 ? SPBFCR_TXTRG | SPBFCR_RXTRG : 0);
196
Marek Vasutf6bdbcc2018-04-10 16:58:46 +0200197 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPTEF,
198 true, 1000, true);
199 if (ret)
200 return ret;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900201
Marek Vasut0bad3452018-04-10 16:43:47 +0200202 for (i = 0; i < chunk; i++) {
203 writeb(*tdata, &ss->regs->spdr);
204 if (dout != NULL)
205 tdata++;
206 }
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900207
Marek Vasutf6bdbcc2018-04-10 16:58:46 +0200208 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPRFF,
209 true, 1000, true);
210 if (ret)
211 return ret;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900212
Marek Vasut0bad3452018-04-10 16:43:47 +0200213 for (i = 0; i < chunk; i++) {
214 *rdata = readb(&ss->regs->spdr);
215 if (din != NULL)
216 rdata++;
217 }
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900218
Marek Vasut0bad3452018-04-10 16:43:47 +0200219 nbyte -= chunk;
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900220 }
221
222 if (flags & SPI_XFER_END)
Marek Vasutd2da0452018-08-25 19:34:24 +0200223 sh_qspi_cs_deactivate(ss);
Nobuhiro Iwamatsu0ae83982013-12-18 15:31:55 +0900224
225 return ret;
226}
Marek Vasutd2da0452018-08-25 19:34:24 +0200227
Lukasz Majewski76f442982020-06-04 23:11:53 +0800228#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutd2da0452018-08-25 19:34:24 +0200229static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave)
230{
231 return container_of(slave, struct sh_qspi_slave, slave);
232}
233
234int spi_cs_is_valid(unsigned int bus, unsigned int cs)
235{
236 return 1;
237}
238
239void spi_cs_activate(struct spi_slave *slave)
240{
241 struct sh_qspi_slave *ss = to_sh_qspi(slave);
242
243 sh_qspi_cs_activate(ss);
244}
245
246void spi_cs_deactivate(struct spi_slave *slave)
247{
248 struct sh_qspi_slave *ss = to_sh_qspi(slave);
249
250 sh_qspi_cs_deactivate(ss);
251}
252
Marek Vasutd2da0452018-08-25 19:34:24 +0200253struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
254 unsigned int max_hz, unsigned int mode)
255{
256 struct sh_qspi_slave *ss;
257
258 if (!spi_cs_is_valid(bus, cs))
259 return NULL;
260
261 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
262 if (!ss) {
263 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
264 return NULL;
265 }
266
267 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
268
269 /* Init SH QSPI */
270 sh_qspi_init(ss);
271
272 return &ss->slave;
273}
274
275void spi_free_slave(struct spi_slave *slave)
276{
277 struct sh_qspi_slave *spi = to_sh_qspi(slave);
278
279 free(spi);
280}
281
282int spi_claim_bus(struct spi_slave *slave)
283{
284 return 0;
285}
286
287void spi_release_bus(struct spi_slave *slave)
288{
289}
290
291int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
292 const void *dout, void *din, unsigned long flags)
293{
294 struct sh_qspi_slave *ss = to_sh_qspi(slave);
295
296 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
297}
298
299#else
300
301#include <dm.h>
302
303static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen,
304 const void *dout, void *din, unsigned long flags)
305{
306 struct udevice *bus = dev->parent;
Simon Glassfa20e932020-12-03 16:55:20 -0700307 struct sh_qspi_slave *ss = dev_get_plat(bus);
Marek Vasutd2da0452018-08-25 19:34:24 +0200308
309 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
310}
311
312static int sh_qspi_set_speed(struct udevice *dev, uint speed)
313{
314 /* This is a SPI NOR controller, do nothing. */
315 return 0;
316}
317
318static int sh_qspi_set_mode(struct udevice *dev, uint mode)
319{
320 /* This is a SPI NOR controller, do nothing. */
321 return 0;
322}
323
324static int sh_qspi_probe(struct udevice *dev)
325{
Simon Glassfa20e932020-12-03 16:55:20 -0700326 struct sh_qspi_slave *ss = dev_get_plat(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200327
328 sh_qspi_init(ss);
329
330 return 0;
331}
332
Simon Glassaad29ae2020-12-03 16:55:21 -0700333static int sh_qspi_of_to_plat(struct udevice *dev)
Marek Vasutd2da0452018-08-25 19:34:24 +0200334{
Simon Glassfa20e932020-12-03 16:55:20 -0700335 struct sh_qspi_slave *plat = dev_get_plat(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200336
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100337 plat->regs = dev_read_addr_ptr(dev);
Marek Vasutd2da0452018-08-25 19:34:24 +0200338
339 return 0;
340}
341
342static const struct dm_spi_ops sh_qspi_ops = {
343 .xfer = sh_qspi_xfer,
344 .set_speed = sh_qspi_set_speed,
345 .set_mode = sh_qspi_set_mode,
346};
347
348static const struct udevice_id sh_qspi_ids[] = {
349 { .compatible = "renesas,qspi" },
350 { }
351};
352
353U_BOOT_DRIVER(sh_qspi) = {
354 .name = "sh_qspi",
355 .id = UCLASS_SPI,
356 .of_match = sh_qspi_ids,
357 .ops = &sh_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700358 .of_to_plat = sh_qspi_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700359 .plat_auto = sizeof(struct sh_qspi_slave),
Marek Vasutd2da0452018-08-25 19:34:24 +0200360 .probe = sh_qspi_probe,
361};
362#endif