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);
255 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
256 unsigned int max_hz, unsigned int mode)
258 struct sh_qspi_slave *ss;
260 if (!spi_cs_is_valid(bus, cs))
263 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
265 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
269 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
277 void spi_free_slave(struct spi_slave *slave)
279 struct sh_qspi_slave *spi = to_sh_qspi(slave);
284 int spi_claim_bus(struct spi_slave *slave)
289 void spi_release_bus(struct spi_slave *slave)
293 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
294 const void *dout, void *din, unsigned long flags)
296 struct sh_qspi_slave *ss = to_sh_qspi(slave);
298 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
305 static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen,
306 const void *dout, void *din, unsigned long flags)
308 struct udevice *bus = dev->parent;
309 struct sh_qspi_slave *ss = dev_get_platdata(bus);
311 return sh_qspi_xfer_common(ss, bitlen, dout, din, flags);
314 static int sh_qspi_set_speed(struct udevice *dev, uint speed)
316 /* This is a SPI NOR controller, do nothing. */
320 static int sh_qspi_set_mode(struct udevice *dev, uint mode)
322 /* This is a SPI NOR controller, do nothing. */
326 static int sh_qspi_probe(struct udevice *dev)
328 struct sh_qspi_slave *ss = dev_get_platdata(dev);
335 static int sh_qspi_ofdata_to_platdata(struct udevice *dev)
337 struct sh_qspi_slave *plat = dev_get_platdata(dev);
339 plat->regs = (struct sh_qspi_regs *)dev_read_addr(dev);
344 static const struct dm_spi_ops sh_qspi_ops = {
345 .xfer = sh_qspi_xfer,
346 .set_speed = sh_qspi_set_speed,
347 .set_mode = sh_qspi_set_mode,
350 static const struct udevice_id sh_qspi_ids[] = {
351 { .compatible = "renesas,qspi" },
355 U_BOOT_DRIVER(sh_qspi) = {
358 .of_match = sh_qspi_ids,
360 .ofdata_to_platdata = sh_qspi_ofdata_to_platdata,
361 .platdata_auto_alloc_size = sizeof(struct sh_qspi_slave),
362 .probe = sh_qspi_probe,