arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / spi / fsl_espi.c
blob7c845827627bc2a276e326f7119fab2c485fffc9
1 /*
2 * eSPI controller driver.
4 * Copyright 2010-2011 Freescale Semiconductor, Inc.
5 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
7 * SPDX-License-Identifier: GPL-2.0+
8 */
10 #include <common.h>
12 #include <malloc.h>
13 #include <spi.h>
14 #include <asm/immap_85xx.h>
16 struct fsl_spi_slave {
17 struct spi_slave slave;
18 unsigned int div16;
19 unsigned int pm;
20 unsigned int mode;
21 size_t cmd_len;
22 u8 cmd_buf[16];
23 size_t data_len;
24 unsigned int max_transfer_length;
27 #define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
29 #define ESPI_MAX_CS_NUM 4
31 #define ESPI_EV_RNE (1 << 9)
32 #define ESPI_EV_TNF (1 << 8)
34 #define ESPI_MODE_EN (1 << 31) /* Enable interface */
35 #define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
36 #define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
38 #define ESPI_COM_CS(x) ((x) << 30)
39 #define ESPI_COM_TRANLEN(x) ((x) << 0)
41 #define ESPI_CSMODE_CI_INACTIVEHIGH (1 << 31)
42 #define ESPI_CSMODE_CP_BEGIN_EDGCLK (1 << 30)
43 #define ESPI_CSMODE_REV_MSB_FIRST (1 << 29)
44 #define ESPI_CSMODE_DIV16 (1 << 28)
45 #define ESPI_CSMODE_PM(x) ((x) << 24)
46 #define ESPI_CSMODE_POL_ASSERTED_LOW (1 << 20)
47 #define ESPI_CSMODE_LEN(x) ((x) << 16)
48 #define ESPI_CSMODE_CSBEF(x) ((x) << 12)
49 #define ESPI_CSMODE_CSAFT(x) ((x) << 8)
50 #define ESPI_CSMODE_CSCG(x) ((x) << 3)
52 #define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
53 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
54 ESPI_CSMODE_CSCG(1))
56 #define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
58 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
59 unsigned int max_hz, unsigned int mode)
61 struct fsl_spi_slave *fsl;
62 sys_info_t sysinfo;
63 unsigned long spibrg = 0;
64 unsigned char pm = 0;
66 if (!spi_cs_is_valid(bus, cs))
67 return NULL;
69 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
70 if (!fsl)
71 return NULL;
73 fsl->mode = mode;
74 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
76 /* Set eSPI BRG clock source */
77 get_sys_info(&sysinfo);
78 spibrg = sysinfo.freq_systembus / 2;
79 fsl->div16 = 0;
80 if ((spibrg / max_hz) > 32) {
81 fsl->div16 = ESPI_CSMODE_DIV16;
82 pm = spibrg / (max_hz * 16 * 2);
83 if (pm > 16) {
84 pm = 16;
85 debug("Requested speed is too low: %d Hz, %ld Hz "
86 "is used.\n", max_hz, spibrg / (32 * 16));
88 } else
89 pm = spibrg / (max_hz * 2);
90 if (pm)
91 pm--;
92 fsl->pm = pm;
94 return &fsl->slave;
97 void spi_free_slave(struct spi_slave *slave)
99 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
100 free(fsl);
103 void spi_init(void)
108 int spi_claim_bus(struct spi_slave *slave)
110 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
111 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
112 unsigned char pm = fsl->pm;
113 unsigned int cs = slave->cs;
114 unsigned int mode = fsl->mode;
115 unsigned int div16 = fsl->div16;
116 int i;
118 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, cs);
120 /* Enable eSPI interface */
121 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
122 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
124 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
125 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
127 /* Init CS mode interface */
128 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
129 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
131 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
132 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
133 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
134 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
136 /* Set eSPI BRG clock source */
137 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
138 | ESPI_CSMODE_PM(pm) | div16);
140 /* Set eSPI mode */
141 if (mode & SPI_CPHA)
142 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
143 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
144 if (mode & SPI_CPOL)
145 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
146 | ESPI_CSMODE_CI_INACTIVEHIGH);
148 /* Character bit order: msb first */
149 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
150 | ESPI_CSMODE_REV_MSB_FIRST);
152 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
153 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
154 | ESPI_CSMODE_LEN(7));
156 return 0;
159 void spi_release_bus(struct spi_slave *slave)
164 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *data_out,
165 void *data_in, unsigned long flags)
167 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
168 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
169 unsigned int tmpdout, tmpdin, event;
170 const void *dout = NULL;
171 void *din = NULL;
172 int len = 0;
173 int num_blks, num_chunks, max_tran_len, tran_len;
174 int num_bytes;
175 unsigned char *ch;
176 unsigned char *buffer = NULL;
177 size_t buf_len;
178 u8 *cmd_buf = fsl->cmd_buf;
179 size_t cmd_len = fsl->cmd_len;
180 size_t data_len = bitlen / 8;
181 size_t rx_offset = 0;
183 max_tran_len = fsl->max_transfer_length;
184 switch (flags) {
185 case SPI_XFER_BEGIN:
186 cmd_len = fsl->cmd_len = data_len;
187 memcpy(cmd_buf, data_out, cmd_len);
188 return 0;
189 case 0:
190 case SPI_XFER_END:
191 if (bitlen == 0) {
192 spi_cs_deactivate(slave);
193 return 0;
195 buf_len = 2 * cmd_len + min(data_len, max_tran_len);
196 len = cmd_len + data_len;
197 rx_offset = cmd_len;
198 buffer = (unsigned char *)malloc(buf_len);
199 if (!buffer) {
200 debug("SF: Failed to malloc memory.\n");
201 return 1;
203 memcpy(buffer, cmd_buf, cmd_len);
204 if (data_in == NULL)
205 memcpy(buffer + cmd_len, data_out, data_len);
206 break;
207 case SPI_XFER_BEGIN | SPI_XFER_END:
208 len = data_len;
209 buffer = (unsigned char *)malloc(len * 2);
210 if (!buffer) {
211 debug("SF: Failed to malloc memory.\n");
212 return 1;
214 memcpy(buffer, data_out, len);
215 rx_offset = len;
216 cmd_len = 0;
217 break;
220 debug("spi_xfer: slave %u:%u dout %08X(%p) din %08X(%p) len %u\n",
221 slave->bus, slave->cs, *(uint *) dout,
222 dout, *(uint *) din, din, len);
224 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
225 while (num_chunks--) {
226 if (data_in)
227 din = buffer + rx_offset;
228 dout = buffer;
229 tran_len = min(data_len , max_tran_len);
230 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
231 num_bytes = (tran_len + cmd_len) % 4;
232 fsl->data_len = tran_len + cmd_len;
233 spi_cs_activate(slave);
235 /* Clear all eSPI events */
236 out_be32(&espi->event , 0xffffffff);
237 /* handle data in 32-bit chunks */
238 while (num_blks--) {
240 event = in_be32(&espi->event);
241 if (event & ESPI_EV_TNF) {
242 tmpdout = *(u32 *)dout;
244 /* Set up the next iteration */
245 if (len > 4) {
246 len -= 4;
247 dout += 4;
250 out_be32(&espi->tx, tmpdout);
251 out_be32(&espi->event, ESPI_EV_TNF);
252 debug("***spi_xfer:...%08x written\n", tmpdout);
255 /* Wait for eSPI transmit to get out */
256 udelay(80);
258 event = in_be32(&espi->event);
259 if (event & ESPI_EV_RNE) {
260 tmpdin = in_be32(&espi->rx);
261 if (num_blks == 0 && num_bytes != 0) {
262 ch = (unsigned char *)&tmpdin;
263 while (num_bytes--)
264 *(unsigned char *)din++ = *ch++;
265 } else {
266 *(u32 *) din = tmpdin;
267 din += 4;
270 out_be32(&espi->event, in_be32(&espi->event)
271 | ESPI_EV_RNE);
272 debug("***spi_xfer:...%08x readed\n", tmpdin);
275 if (data_in) {
276 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
277 if (*buffer == 0x0b) {
278 data_in += tran_len;
279 data_len -= tran_len;
280 *(int *)buffer += tran_len;
283 spi_cs_deactivate(slave);
286 free(buffer);
287 return 0;
290 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
292 return bus == 0 && cs < ESPI_MAX_CS_NUM;
295 void spi_cs_activate(struct spi_slave *slave)
297 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
298 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
299 unsigned int com = 0;
300 size_t data_len = fsl->data_len;
302 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
303 com |= ESPI_COM_CS(slave->cs);
304 com |= ESPI_COM_TRANLEN(data_len - 1);
305 out_be32(&espi->com, com);
308 void spi_cs_deactivate(struct spi_slave *slave)
310 ccsr_espi_t *espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
312 /* clear the RXCNT and TXCNT */
313 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
314 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);