1 // SPDX-License-Identifier: GPL-2.0+
3 * eSPI controller driver.
5 * Copyright 2010-2011 Freescale Semiconductor, Inc.
6 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
13 #include <asm/immap_85xx.h>
15 struct fsl_spi_slave {
16 struct spi_slave slave;
25 unsigned int max_transfer_length;
28 #define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
29 #define US_PER_SECOND 1000000UL
31 #define ESPI_MAX_CS_NUM 4
32 #define ESPI_FIFO_WIDTH_BIT 32
34 #define ESPI_EV_RNE BIT(9)
35 #define ESPI_EV_TNF BIT(8)
36 #define ESPI_EV_DON BIT(14)
37 #define ESPI_EV_TXE BIT(15)
38 #define ESPI_EV_RFCNT_SHIFT 24
39 #define ESPI_EV_RFCNT_MASK (0x3f << ESPI_EV_RFCNT_SHIFT)
41 #define ESPI_MODE_EN BIT(31) /* Enable interface */
42 #define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
43 #define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
45 #define ESPI_COM_CS(x) ((x) << 30)
46 #define ESPI_COM_TRANLEN(x) ((x) << 0)
48 #define ESPI_CSMODE_CI_INACTIVEHIGH BIT(31)
49 #define ESPI_CSMODE_CP_BEGIN_EDGCLK BIT(30)
50 #define ESPI_CSMODE_REV_MSB_FIRST BIT(29)
51 #define ESPI_CSMODE_DIV16 BIT(28)
52 #define ESPI_CSMODE_PM(x) ((x) << 24)
53 #define ESPI_CSMODE_POL_ASSERTED_LOW BIT(20)
54 #define ESPI_CSMODE_LEN(x) ((x) << 16)
55 #define ESPI_CSMODE_CSBEF(x) ((x) << 12)
56 #define ESPI_CSMODE_CSAFT(x) ((x) << 8)
57 #define ESPI_CSMODE_CSCG(x) ((x) << 3)
59 #define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
60 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
63 #define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
65 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
66 unsigned int max_hz, unsigned int mode)
68 struct fsl_spi_slave *fsl;
70 unsigned long spibrg = 0;
71 unsigned long spi_freq = 0;
74 if (!spi_cs_is_valid(bus, cs))
77 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
81 fsl->espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
83 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
85 /* Set eSPI BRG clock source */
86 get_sys_info(&sysinfo);
87 spibrg = sysinfo.freq_systembus / 2;
89 if ((spibrg / max_hz) > 32) {
90 fsl->div16 = ESPI_CSMODE_DIV16;
91 pm = spibrg / (max_hz * 16 * 2);
94 debug("Requested speed is too low: %d Hz, %ld Hz "
95 "is used.\n", max_hz, spibrg / (32 * 16));
98 pm = spibrg / (max_hz * 2);
104 spi_freq = spibrg / ((pm + 1) * 2 * 16);
106 spi_freq = spibrg / ((pm + 1) * 2);
108 /* set tx_timeout to 10 times of one espi FIFO entry go out */
109 fsl->tx_timeout = DIV_ROUND_UP((US_PER_SECOND * ESPI_FIFO_WIDTH_BIT
115 void spi_free_slave(struct spi_slave *slave)
117 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
126 int spi_claim_bus(struct spi_slave *slave)
128 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
129 ccsr_espi_t *espi = fsl->espi;
130 unsigned char pm = fsl->pm;
131 unsigned int cs = slave->cs;
132 unsigned int mode = fsl->mode;
133 unsigned int div16 = fsl->div16;
136 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, cs);
138 /* Enable eSPI interface */
139 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
140 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
142 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
143 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
145 /* Init CS mode interface */
146 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
147 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
149 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
150 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
151 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
152 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
154 /* Set eSPI BRG clock source */
155 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
156 | ESPI_CSMODE_PM(pm) | div16);
160 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
161 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
163 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
164 | ESPI_CSMODE_CI_INACTIVEHIGH);
166 /* Character bit order: msb first */
167 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
168 | ESPI_CSMODE_REV_MSB_FIRST);
170 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
171 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
172 | ESPI_CSMODE_LEN(7));
177 void spi_release_bus(struct spi_slave *slave)
182 static void fsl_espi_tx(struct fsl_spi_slave *fsl, const void *dout)
184 ccsr_espi_t *espi = fsl->espi;
185 unsigned int tmpdout, event;
189 tmpdout = *(u32 *)dout;
193 out_be32(&espi->tx, tmpdout);
194 out_be32(&espi->event, ESPI_EV_TNF);
195 debug("***spi_xfer:...%08x written\n", tmpdout);
197 tmp_tx_timeout = fsl->tx_timeout;
198 /* Wait for eSPI transmit to go out */
199 while (tmp_tx_timeout--) {
200 event = in_be32(&espi->event);
201 if (event & ESPI_EV_DON || event & ESPI_EV_TXE) {
202 out_be32(&espi->event, ESPI_EV_TXE);
208 if (tmp_tx_timeout < 0)
209 debug("***spi_xfer:...Tx timeout! event = %08x\n", event);
212 static int fsl_espi_rx(struct fsl_spi_slave *fsl, void *din, unsigned int bytes)
214 ccsr_espi_t *espi = fsl->espi;
215 unsigned int tmpdin, rx_times;
216 unsigned char *buf, *p_cursor;
221 rx_times = DIV_ROUND_UP(bytes, 4);
222 buf = (unsigned char *)malloc(4 * rx_times);
224 debug("SF: Failed to malloc memory.\n");
229 tmpdin = in_be32(&espi->rx);
230 debug("***spi_xfer:...%08x readed\n", tmpdin);
231 *(u32 *)p_cursor = tmpdin;
236 memcpy(din, buf, bytes);
239 out_be32(&espi->event, ESPI_EV_RNE);
244 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *data_out,
245 void *data_in, unsigned long flags)
247 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
248 ccsr_espi_t *espi = fsl->espi;
249 unsigned int event, rx_bytes;
250 const void *dout = NULL;
253 int num_blks, num_chunks, max_tran_len, tran_len;
255 unsigned char *buffer = NULL;
257 u8 *cmd_buf = fsl->cmd_buf;
258 size_t cmd_len = fsl->cmd_len;
259 size_t data_len = bitlen / 8;
260 size_t rx_offset = 0;
263 max_tran_len = fsl->max_transfer_length;
266 cmd_len = fsl->cmd_len = data_len;
267 memcpy(cmd_buf, data_out, cmd_len);
272 spi_cs_deactivate(slave);
275 buf_len = 2 * cmd_len + min(data_len, (size_t)max_tran_len);
276 len = cmd_len + data_len;
278 buffer = (unsigned char *)malloc(buf_len);
280 debug("SF: Failed to malloc memory.\n");
283 memcpy(buffer, cmd_buf, cmd_len);
285 memcpy(buffer + cmd_len, data_out, data_len);
287 case SPI_XFER_BEGIN | SPI_XFER_END:
289 buffer = (unsigned char *)malloc(len * 2);
291 debug("SF: Failed to malloc memory.\n");
294 memcpy(buffer, data_out, len);
300 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) len %u\n",
301 *(uint *)data_out, data_out, *(uint *)data_in, data_in, len);
303 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
304 while (num_chunks--) {
306 din = buffer + rx_offset;
308 tran_len = min(data_len, (size_t)max_tran_len);
309 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
310 num_bytes = (tran_len + cmd_len) % 4;
311 fsl->data_len = tran_len + cmd_len;
312 spi_cs_activate(slave);
314 /* Clear all eSPI events */
315 out_be32(&espi->event , 0xffffffff);
316 /* handle data in 32-bit chunks */
318 event = in_be32(&espi->event);
319 if (event & ESPI_EV_TNF) {
320 fsl_espi_tx(fsl, dout);
321 /* Set up the next iteration */
328 event = in_be32(&espi->event);
329 if (event & ESPI_EV_RNE) {
330 rf_cnt = ((event & ESPI_EV_RFCNT_MASK)
331 >> ESPI_EV_RFCNT_SHIFT);
334 else if (num_blks == 1 && rf_cnt == num_bytes)
335 rx_bytes = num_bytes;
338 if (fsl_espi_rx(fsl, din, rx_bytes)
342 din = (unsigned char *)din
348 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
349 if (*buffer == 0x0b) {
351 data_len -= tran_len;
352 *(int *)buffer += tran_len;
355 spi_cs_deactivate(slave);
362 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
364 return bus == 0 && cs < ESPI_MAX_CS_NUM;
367 void spi_cs_activate(struct spi_slave *slave)
369 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
370 ccsr_espi_t *espi = fsl->espi;
371 unsigned int com = 0;
372 size_t data_len = fsl->data_len;
374 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
375 com |= ESPI_COM_CS(slave->cs);
376 com |= ESPI_COM_TRANLEN(data_len - 1);
377 out_be32(&espi->com, com);
380 void spi_cs_deactivate(struct spi_slave *slave)
382 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
383 ccsr_espi_t *espi = fsl->espi;
385 /* clear the RXCNT and TXCNT */
386 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
387 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);