arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / spi / sh_qspi.c
blob77ede6bba3a5d9655a7bc46be8dca39d70d2834d
1 /*
2 * SH QSPI (Quad SPI) driver
4 * Copyright (C) 2013 Renesas Electronics Corporation
5 * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
7 * SPDX-License-Identifier: GPL-2.0
8 */
10 #include <common.h>
11 #include <malloc.h>
12 #include <spi.h>
13 #include <asm/arch/rmobile.h>
14 #include <asm/io.h>
16 /* SH QSPI register bit masks <REG>_<BIT> */
17 #define SPCR_MSTR 0x08
18 #define SPCR_SPE 0x40
19 #define SPSR_SPRFF 0x80
20 #define SPSR_SPTEF 0x20
21 #define SPPCR_IO3FV 0x04
22 #define SPPCR_IO2FV 0x02
23 #define SPPCR_IO1FV 0x01
24 #define SPBDCR_RXBC0 (1 << 0)
25 #define SPCMD_SCKDEN (1 << 15)
26 #define SPCMD_SLNDEN (1 << 14)
27 #define SPCMD_SPNDEN (1 << 13)
28 #define SPCMD_SSLKP (1 << 7)
29 #define SPCMD_BRDV0 (1 << 2)
30 #define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \
31 SPCMD_SPNDEN | SPCMD_SSLKP | \
32 SPCMD_BRDV0
33 #define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \
34 SPCMD_BRDV0
35 #define SPBFCR_TXRST (1 << 7)
36 #define SPBFCR_RXRST (1 << 6)
38 /* SH QSPI register set */
39 struct sh_qspi_regs {
40 unsigned char spcr;
41 unsigned char sslp;
42 unsigned char sppcr;
43 unsigned char spsr;
44 unsigned long spdr;
45 unsigned char spscr;
46 unsigned char spssr;
47 unsigned char spbr;
48 unsigned char spdcr;
49 unsigned char spckd;
50 unsigned char sslnd;
51 unsigned char spnd;
52 unsigned char dummy0;
53 unsigned short spcmd0;
54 unsigned short spcmd1;
55 unsigned short spcmd2;
56 unsigned short spcmd3;
57 unsigned char spbfcr;
58 unsigned char dummy1;
59 unsigned short spbdcr;
60 unsigned long spbmul0;
61 unsigned long spbmul1;
62 unsigned long spbmul2;
63 unsigned long spbmul3;
66 struct sh_qspi_slave {
67 struct spi_slave slave;
68 struct sh_qspi_regs *regs;
71 static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave)
73 return container_of(slave, struct sh_qspi_slave, slave);
76 static void sh_qspi_init(struct sh_qspi_slave *ss)
78 /* QSPI initialize */
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 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
121 return 1;
124 void spi_cs_activate(struct spi_slave *slave)
126 struct sh_qspi_slave *ss = to_sh_qspi(slave);
128 /* Set master mode only */
129 writeb(SPCR_MSTR, &ss->regs->spcr);
131 /* Set command */
132 writew(SPCMD_INIT1, &ss->regs->spcmd0);
134 /* Reset transfer and receive Buffer */
135 setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
137 /* Clear transfer and receive Buffer control bit */
138 clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST);
140 /* Set equence control method. Use equence0 only */
141 writeb(0x00, &ss->regs->spscr);
143 /* Enable SPI function */
144 setbits_8(&ss->regs->spcr, SPCR_SPE);
147 void spi_cs_deactivate(struct spi_slave *slave)
149 struct sh_qspi_slave *ss = to_sh_qspi(slave);
151 /* Disable SPI Function */
152 clrbits_8(&ss->regs->spcr, SPCR_SPE);
155 void spi_init(void)
157 /* nothing to do */
160 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
161 unsigned int max_hz, unsigned int mode)
163 struct sh_qspi_slave *ss;
165 if (!spi_cs_is_valid(bus, cs))
166 return NULL;
168 ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs);
169 if (!ss) {
170 printf("SPI_error: Fail to allocate sh_qspi_slave\n");
171 return NULL;
174 ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE;
176 /* Init SH QSPI */
177 sh_qspi_init(ss);
179 return &ss->slave;
182 void spi_free_slave(struct spi_slave *slave)
184 struct sh_qspi_slave *spi = to_sh_qspi(slave);
186 free(spi);
189 int spi_claim_bus(struct spi_slave *slave)
191 return 0;
194 void spi_release_bus(struct spi_slave *slave)
198 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
199 void *din, unsigned long flags)
201 struct sh_qspi_slave *ss = to_sh_qspi(slave);
202 unsigned long nbyte;
203 int ret = 0;
204 unsigned char dtdata = 0, drdata;
205 unsigned char *tdata = &dtdata, *rdata = &drdata;
206 unsigned long *spbmul0 = &ss->regs->spbmul0;
208 if (dout == NULL && din == NULL) {
209 if (flags & SPI_XFER_END)
210 spi_cs_deactivate(slave);
211 return 0;
214 if (bitlen % 8) {
215 printf("%s: bitlen is not 8bit alined %d", __func__, bitlen);
216 return 1;
219 nbyte = bitlen / 8;
221 if (flags & SPI_XFER_BEGIN) {
222 spi_cs_activate(slave);
224 /* Set 1048576 byte */
225 writel(0x100000, spbmul0);
228 if (flags & SPI_XFER_END)
229 writel(nbyte, spbmul0);
231 if (dout != NULL)
232 tdata = (unsigned char *)dout;
234 if (din != NULL)
235 rdata = din;
237 while (nbyte > 0) {
238 while (!(readb(&ss->regs->spsr) & SPSR_SPTEF)) {
239 if (ctrlc()) {
240 puts("abort\n");
241 return 1;
243 udelay(10);
246 writeb(*tdata, (unsigned char *)(&ss->regs->spdr));
248 while ((readw(&ss->regs->spbdcr) != SPBDCR_RXBC0)) {
249 if (ctrlc()) {
250 puts("abort\n");
251 return 1;
253 udelay(1);
256 while (!(readb(&ss->regs->spsr) & SPSR_SPRFF)) {
257 if (ctrlc()) {
258 puts("abort\n");
259 return 1;
261 udelay(10);
264 *rdata = readb((unsigned char *)(&ss->regs->spdr));
266 if (dout != NULL)
267 tdata++;
268 if (din != NULL)
269 rdata++;
271 nbyte--;
274 if (flags & SPI_XFER_END)
275 spi_cs_deactivate(slave);
277 return ret;