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+
14 #include <asm/immap_85xx.h>
16 struct fsl_spi_slave
{
17 struct spi_slave slave
;
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) | \
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
;
63 unsigned long spibrg
= 0;
66 if (!spi_cs_is_valid(bus
, cs
))
69 fsl
= spi_alloc_slave(struct fsl_spi_slave
, bus
, cs
);
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;
80 if ((spibrg
/ max_hz
) > 32) {
81 fsl
->div16
= ESPI_CSMODE_DIV16
;
82 pm
= spibrg
/ (max_hz
* 16 * 2);
85 debug("Requested speed is too low: %d Hz, %ld Hz "
86 "is used.\n", max_hz
, spibrg
/ (32 * 16));
89 pm
= spibrg
/ (max_hz
* 2);
97 void spi_free_slave(struct spi_slave
*slave
)
99 struct fsl_spi_slave
*fsl
= to_fsl_spi_slave(slave
);
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
;
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
);
142 out_be32(&espi
->csmode
[cs
], in_be32(&espi
->csmode
[cs
])
143 | ESPI_CSMODE_CP_BEGIN_EDGCLK
);
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));
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
;
173 int num_blks
, num_chunks
, max_tran_len
, tran_len
;
176 unsigned char *buffer
= NULL
;
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
;
186 cmd_len
= fsl
->cmd_len
= data_len
;
187 memcpy(cmd_buf
, data_out
, cmd_len
);
192 spi_cs_deactivate(slave
);
195 buf_len
= 2 * cmd_len
+ min(data_len
, max_tran_len
);
196 len
= cmd_len
+ data_len
;
198 buffer
= (unsigned char *)malloc(buf_len
);
200 debug("SF: Failed to malloc memory.\n");
203 memcpy(buffer
, cmd_buf
, cmd_len
);
205 memcpy(buffer
+ cmd_len
, data_out
, data_len
);
207 case SPI_XFER_BEGIN
| SPI_XFER_END
:
209 buffer
= (unsigned char *)malloc(len
* 2);
211 debug("SF: Failed to malloc memory.\n");
214 memcpy(buffer
, data_out
, len
);
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
--) {
227 din
= buffer
+ rx_offset
;
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 */
240 event
= in_be32(&espi
->event
);
241 if (event
& ESPI_EV_TNF
) {
242 tmpdout
= *(u32
*)dout
;
244 /* Set up the next iteration */
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 */
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
;
264 *(unsigned char *)din
++ = *ch
++;
266 *(u32
*) din
= tmpdin
;
270 out_be32(&espi
->event
, in_be32(&espi
->event
)
272 debug("***spi_xfer:...%08x readed\n", tmpdin
);
276 memcpy(data_in
, buffer
+ 2 * cmd_len
, tran_len
);
277 if (*buffer
== 0x0b) {
279 data_len
-= tran_len
;
280 *(int *)buffer
+= tran_len
;
283 spi_cs_deactivate(slave
);
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
);