1 // SPDX-License-Identifier: GPL-2.0
3 * SH QSPI (Quad SPI) driver
5 * Copyright (C) 2013 Renesas Electronics Corporation
6 * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
14 #include <asm/arch/rmobile.h>
16 #include <linux/bitops.h>
18 /* SH QSPI register bit masks <REG>_<BIT> */
19 #define SPCR_MSTR 0x08
21 #define SPSR_SPRFF 0x80
22 #define SPSR_SPTEF 0x20
23 #define SPPCR_IO3FV 0x04
24 #define SPPCR_IO2FV 0x02
25 #define SPPCR_IO1FV 0x01
26 #define SPBDCR_RXBC0 BIT(0)
27 #define SPCMD_SCKDEN BIT(15)
28 #define SPCMD_SLNDEN BIT(14)
29 #define SPCMD_SPNDEN BIT(13)
30 #define SPCMD_SSLKP BIT(7)
31 #define SPCMD_BRDV0 BIT(2)
32 #define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \
33 SPCMD_SPNDEN | SPCMD_SSLKP | \
35 #define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \
37 #define SPBFCR_TXRST BIT(7)
38 #define SPBFCR_RXRST BIT(6)
39 #define SPBFCR_TXTRG 0x30
40 #define SPBFCR_RXTRG 0x07
42 /* SH QSPI register set */
70 struct sh_qspi_slave {
71 #if !CONFIG_IS_ENABLED(DM_SPI)
72 struct spi_slave slave;
74 struct sh_qspi_regs *regs;
77 static void sh_qspi_init(struct sh_qspi_slave *ss)
80 /* Set master mode only */
81 writeb(SPCR_MSTR, &ss->regs->spcr);
83 /* Set SSL signal level */
84 writeb(0x00, &ss->regs->sslp);
86 /* Set MOSI signal value when transfer is in idle state */
87 writeb(SPPCR_IO3FV|SPPCR_IO2FV, &ss->regs->sppcr);
89 /* Set bit rate. See 58.3.8 Quad Serial Peripheral Interface */
90 writeb(0x01, &ss->regs->spbr);
92 /* Disable Dummy Data Transmission */
93 writeb(0x00, &ss->regs->spdcr);
95 /* Set clock delay value */
96 writeb(0x00, &ss->regs->spckd);
98 /* Set SSL negation delay value */
99 writeb(0x00, &ss->regs->sslnd);
101 /* Set next-access delay value */
102 writeb(0x00, &ss->regs->spnd);
104 /* Set equence command */
105 writew(SPCMD_INIT2, &ss->regs->spcmd0);
107 /* Reset transfer and receive Buffer */
108 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
110 /* Clear transfer and receive Buffer control bit */
111 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
113 /* Set equence control method. Use equence0 only */
114 writeb(0x00, &ss->regs->spscr);
116 /* Enable SPI function */
117 setbits_8(&ss->regs->spcr, SPCR_SPE);
120 static void sh_qspi_cs_activate(struct sh_qspi_slave *ss)
122 /* Set master mode only */
123 writeb(SPCR_MSTR, &ss->regs->spcr);
126 writew(SPCMD_INIT1, &ss->regs->spcmd0);
128 /* Reset transfer and receive Buffer */
129 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
131 /* Clear transfer and receive Buffer control bit */
132 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
134 /* Set equence control method. Use equence0 only */
135 writeb(0x00, &ss->regs->spscr);
137 /* Enable SPI function */
138 setbits_8(&ss->regs->spcr, SPCR_SPE);
141 static void sh_qspi_cs_deactivate(struct sh_qspi_slave *ss)
143 /* Disable SPI Function */
144 clrbits_8(&ss->regs->spcr, SPCR_SPE);
147 static int sh_qspi_xfer_common(struct sh_qspi_slave *ss, unsigned int bitlen,
148 const void *dout, void *din, unsigned long flags)
152 u8 dtdata = 0, drdata;
153 u8 *tdata = &dtdata, *rdata = &drdata;
154 u32 *spbmul0 = &ss->regs->spbmul0;
156 if (dout == NULL && din == NULL) {
157 if (flags & SPI_XFER_END)
158 sh_qspi_cs_deactivate(ss);
163 printf("%s: bitlen is not 8bit alined %d", __func__, bitlen);
169 if (flags & SPI_XFER_BEGIN) {
170 sh_qspi_cs_activate(ss);
172 /* Set 1048576 byte */
173 writel(0x100000, spbmul0);
176 if (flags & SPI_XFER_END)
177 writel(nbyte, spbmul0);
187 * Check if there is 32 Byte chunk and if there is, transfer
188 * it in one burst, otherwise transfer on byte-by-byte basis.
190 chunk = (nbyte >= 32) ? 32 : 1;
192 clrsetbits_8(&ss->regs->spbfcr, SPBFCR_TXTRG | SPBFCR_RXTRG,
193 chunk == 32 ? SPBFCR_TXTRG | SPBFCR_RXTRG : 0);
195 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPTEF,
200 for (i = 0; i < chunk; i++) {
201 writeb(*tdata, &ss->regs->spdr);
206 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPRFF,
211 for (i = 0; i < chunk; i++) {
212 *rdata = readb(&ss->regs->spdr);
220 if (flags & SPI_XFER_END)
221 sh_qspi_cs_deactivate(ss);
226 #if !CONFIG_IS_ENABLED(DM_SPI)
227 static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave)
229 return container_of(slave, struct sh_qspi_slave, slave);
232 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
237 void spi_cs_activate(struct spi_slave *slave)
239 struct sh_qspi_slave *ss = to_sh_qspi(slave);
241 sh_qspi_cs_activate(ss);
244 void spi_cs_deactivate(struct spi_slave *slave)
246 struct sh_qspi_slave *ss = to_sh_qspi(slave);
248 sh_qspi_cs_deactivate(ss);
251 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
252 unsigned int max_hz, unsigned int mode)
254 struct sh_qspi_slave *ss;
256 if (!spi_cs_is_valid(bus, cs))
259 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
261 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
265 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
273 void spi_free_slave(struct spi_slave *slave)
275 struct sh_qspi_slave *spi = to_sh_qspi(slave);
280 int spi_claim_bus(struct spi_slave *slave)
285 void spi_release_bus(struct spi_slave *slave)
289 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
290 const void *dout, void *din, unsigned long flags)
292 struct sh_qspi_slave *ss = to_sh_qspi(slave);
294 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
301 static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen,
302 const void *dout, void *din, unsigned long flags)
304 struct udevice *bus = dev->parent;
305 struct sh_qspi_slave *ss = dev_get_plat(bus);
307 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
310 static int sh_qspi_set_speed(struct udevice *dev, uint speed)
312 /* This is a SPI NOR controller, do nothing. */
316 static int sh_qspi_set_mode(struct udevice *dev, uint mode)
318 /* This is a SPI NOR controller, do nothing. */
322 static int sh_qspi_probe(struct udevice *dev)
324 struct sh_qspi_slave *ss = dev_get_plat(dev);
331 static int sh_qspi_of_to_plat(struct udevice *dev)
333 struct sh_qspi_slave *plat = dev_get_plat(dev);
335 plat->regs = (struct sh_qspi_regs *)dev_read_addr(dev);
340 static const struct dm_spi_ops sh_qspi_ops = {
341 .xfer = sh_qspi_xfer,
342 .set_speed = sh_qspi_set_speed,
343 .set_mode = sh_qspi_set_mode,
346 static const struct udevice_id sh_qspi_ids[] = {
347 { .compatible = "renesas,qspi" },
351 U_BOOT_DRIVER(sh_qspi) = {
354 .of_match = sh_qspi_ids,
356 .of_to_plat = sh_qspi_of_to_plat,
357 .plat_auto = sizeof(struct sh_qspi_slave),
358 .probe = sh_qspi_probe,