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>
17 /* SH QSPI register bit masks <REG>_<BIT> */
18 #define SPCR_MSTR 0x08
20 #define SPSR_SPRFF 0x80
21 #define SPSR_SPTEF 0x20
22 #define SPPCR_IO3FV 0x04
23 #define SPPCR_IO2FV 0x02
24 #define SPPCR_IO1FV 0x01
25 #define SPBDCR_RXBC0 BIT(0)
26 #define SPCMD_SCKDEN BIT(15)
27 #define SPCMD_SLNDEN BIT(14)
28 #define SPCMD_SPNDEN BIT(13)
29 #define SPCMD_SSLKP BIT(7)
30 #define SPCMD_BRDV0 BIT(2)
31 #define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \
32 SPCMD_SPNDEN | SPCMD_SSLKP | \
34 #define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \
36 #define SPBFCR_TXRST BIT(7)
37 #define SPBFCR_RXRST BIT(6)
38 #define SPBFCR_TXTRG 0x30
39 #define SPBFCR_RXTRG 0x07
41 /* SH QSPI register set */
69 struct sh_qspi_slave {
71 struct spi_slave slave;
73 struct sh_qspi_regs *regs;
76 static void sh_qspi_init(struct sh_qspi_slave *ss)
79 /* Set master mode only */
80 writeb(SPCR_MSTR, &ss->regs->spcr);
82 /* Set SSL signal level */
83 writeb(0x00, &ss->regs->sslp);
85 /* Set MOSI signal value when transfer is in idle state */
86 writeb(SPPCR_IO3FV|SPPCR_IO2FV, &ss->regs->sppcr);
88 /* Set bit rate. See 58.3.8 Quad Serial Peripheral Interface */
89 writeb(0x01, &ss->regs->spbr);
91 /* Disable Dummy Data Transmission */
92 writeb(0x00, &ss->regs->spdcr);
94 /* Set clock delay value */
95 writeb(0x00, &ss->regs->spckd);
97 /* Set SSL negation delay value */
98 writeb(0x00, &ss->regs->sslnd);
100 /* Set next-access delay value */
101 writeb(0x00, &ss->regs->spnd);
103 /* Set equence command */
104 writew(SPCMD_INIT2, &ss->regs->spcmd0);
106 /* Reset transfer and receive Buffer */
107 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
109 /* Clear transfer and receive Buffer control bit */
110 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
112 /* Set equence control method. Use equence0 only */
113 writeb(0x00, &ss->regs->spscr);
115 /* Enable SPI function */
116 setbits_8(&ss->regs->spcr, SPCR_SPE);
119 static void sh_qspi_cs_activate(struct sh_qspi_slave *ss)
121 /* Set master mode only */
122 writeb(SPCR_MSTR, &ss->regs->spcr);
125 writew(SPCMD_INIT1, &ss->regs->spcmd0);
127 /* Reset transfer and receive Buffer */
128 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
130 /* Clear transfer and receive Buffer control bit */
131 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
133 /* Set equence control method. Use equence0 only */
134 writeb(0x00, &ss->regs->spscr);
136 /* Enable SPI function */
137 setbits_8(&ss->regs->spcr, SPCR_SPE);
140 static void sh_qspi_cs_deactivate(struct sh_qspi_slave *ss)
142 /* Disable SPI Function */
143 clrbits_8(&ss->regs->spcr, SPCR_SPE);
146 static int sh_qspi_xfer_common(struct sh_qspi_slave *ss, unsigned int bitlen,
147 const void *dout, void *din, unsigned long flags)
151 u8 dtdata = 0, drdata;
152 u8 *tdata = &dtdata, *rdata = &drdata;
153 u32 *spbmul0 = &ss->regs->spbmul0;
155 if (dout == NULL && din == NULL) {
156 if (flags & SPI_XFER_END)
157 sh_qspi_cs_deactivate(ss);
162 printf("%s: bitlen is not 8bit alined %d", __func__, bitlen);
168 if (flags & SPI_XFER_BEGIN) {
169 sh_qspi_cs_activate(ss);
171 /* Set 1048576 byte */
172 writel(0x100000, spbmul0);
175 if (flags & SPI_XFER_END)
176 writel(nbyte, spbmul0);
186 * Check if there is 32 Byte chunk and if there is, transfer
187 * it in one burst, otherwise transfer on byte-by-byte basis.
189 chunk = (nbyte >= 32) ? 32 : 1;
191 clrsetbits_8(&ss->regs->spbfcr, SPBFCR_TXTRG | SPBFCR_RXTRG,
192 chunk == 32 ? SPBFCR_TXTRG | SPBFCR_RXTRG : 0);
194 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPTEF,
199 for (i = 0; i < chunk; i++) {
200 writeb(*tdata, &ss->regs->spdr);
205 ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPRFF,
210 for (i = 0; i < chunk; i++) {
211 *rdata = readb(&ss->regs->spdr);
219 if (flags & SPI_XFER_END)
220 sh_qspi_cs_deactivate(ss);
225 #ifndef CONFIG_DM_SPI
226 static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave)
228 return container_of(slave, struct sh_qspi_slave, slave);
231 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
236 void spi_cs_activate(struct spi_slave *slave)
238 struct sh_qspi_slave *ss = to_sh_qspi(slave);
240 sh_qspi_cs_activate(ss);
243 void spi_cs_deactivate(struct spi_slave *slave)
245 struct sh_qspi_slave *ss = to_sh_qspi(slave);
247 sh_qspi_cs_deactivate(ss);
250 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
251 unsigned int max_hz, unsigned int mode)
253 struct sh_qspi_slave *ss;
255 if (!spi_cs_is_valid(bus, cs))
258 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
260 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
264 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
272 void spi_free_slave(struct spi_slave *slave)
274 struct sh_qspi_slave *spi = to_sh_qspi(slave);
279 int spi_claim_bus(struct spi_slave *slave)
284 void spi_release_bus(struct spi_slave *slave)
288 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
289 const void *dout, void *din, unsigned long flags)
291 struct sh_qspi_slave *ss = to_sh_qspi(slave);
293 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
300 static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen,
301 const void *dout, void *din, unsigned long flags)
303 struct udevice *bus = dev->parent;
304 struct sh_qspi_slave *ss = dev_get_platdata(bus);
306 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
309 static int sh_qspi_set_speed(struct udevice *dev, uint speed)
311 /* This is a SPI NOR controller, do nothing. */
315 static int sh_qspi_set_mode(struct udevice *dev, uint mode)
317 /* This is a SPI NOR controller, do nothing. */
321 static int sh_qspi_probe(struct udevice *dev)
323 struct sh_qspi_slave *ss = dev_get_platdata(dev);
330 static int sh_qspi_ofdata_to_platdata(struct udevice *dev)
332 struct sh_qspi_slave *plat = dev_get_platdata(dev);
334 plat->regs = (struct sh_qspi_regs *)dev_read_addr(dev);
339 static const struct dm_spi_ops sh_qspi_ops = {
340 .xfer = sh_qspi_xfer,
341 .set_speed = sh_qspi_set_speed,
342 .set_mode = sh_qspi_set_mode,
345 static const struct udevice_id sh_qspi_ids[] = {
346 { .compatible = "renesas,qspi" },
350 U_BOOT_DRIVER(sh_qspi) = {
353 .of_match = sh_qspi_ids,
355 .ofdata_to_platdata = sh_qspi_ofdata_to_platdata,
356 .platdata_auto_alloc_size = sizeof(struct sh_qspi_slave),
357 .probe = sh_qspi_probe,