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;
26 unsigned int max_transfer_length;
29 #define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
30 #define US_PER_SECOND 1000000UL
32 #define ESPI_MAX_CS_NUM 4
33 #define ESPI_FIFO_WIDTH_BIT 32
35 #define ESPI_EV_RNE BIT(9)
36 #define ESPI_EV_TNF BIT(8)
37 #define ESPI_EV_DON BIT(14)
38 #define ESPI_EV_TXE BIT(15)
39 #define ESPI_EV_RFCNT_SHIFT 24
40 #define ESPI_EV_RFCNT_MASK (0x3f << ESPI_EV_RFCNT_SHIFT)
42 #define ESPI_MODE_EN BIT(31) /* Enable interface */
43 #define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
44 #define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
46 #define ESPI_COM_CS(x) ((x) << 30)
47 #define ESPI_COM_TRANLEN(x) ((x) << 0)
49 #define ESPI_CSMODE_CI_INACTIVEHIGH BIT(31)
50 #define ESPI_CSMODE_CP_BEGIN_EDGCLK BIT(30)
51 #define ESPI_CSMODE_REV_MSB_FIRST BIT(29)
52 #define ESPI_CSMODE_DIV16 BIT(28)
53 #define ESPI_CSMODE_PM(x) ((x) << 24)
54 #define ESPI_CSMODE_POL_ASSERTED_LOW BIT(20)
55 #define ESPI_CSMODE_LEN(x) ((x) << 16)
56 #define ESPI_CSMODE_CSBEF(x) ((x) << 12)
57 #define ESPI_CSMODE_CSAFT(x) ((x) << 8)
58 #define ESPI_CSMODE_CSCG(x) ((x) << 3)
60 #define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
61 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
64 #define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
66 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
67 unsigned int max_hz, unsigned int mode)
69 struct fsl_spi_slave *fsl;
71 unsigned long spibrg = 0;
72 unsigned long spi_freq = 0;
75 if (!spi_cs_is_valid(bus, cs))
78 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
82 fsl->espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
84 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
86 /* Set eSPI BRG clock source */
87 get_sys_info(&sysinfo);
88 spibrg = sysinfo.freq_systembus / 2;
90 if ((spibrg / max_hz) > 32) {
91 fsl->div16 = ESPI_CSMODE_DIV16;
92 pm = spibrg / (max_hz * 16 * 2);
95 debug("Requested speed is too low: %d Hz, %ld Hz "
96 "is used.\n", max_hz, spibrg / (32 * 16));
99 pm = spibrg / (max_hz * 2);
105 spi_freq = spibrg / ((pm + 1) * 2 * 16);
107 spi_freq = spibrg / ((pm + 1) * 2);
109 /* set tx_timeout to 10 times of one espi FIFO entry go out */
110 fsl->tx_timeout = DIV_ROUND_UP((US_PER_SECOND * ESPI_FIFO_WIDTH_BIT
116 void spi_free_slave(struct spi_slave *slave)
118 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
127 int spi_claim_bus(struct spi_slave *slave)
129 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
130 ccsr_espi_t *espi = fsl->espi;
131 unsigned char pm = fsl->pm;
132 unsigned int cs = slave->cs;
133 unsigned int mode = fsl->mode;
134 unsigned int div16 = fsl->div16;
137 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, cs);
139 /* Enable eSPI interface */
140 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
141 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
143 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
144 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
146 /* Init CS mode interface */
147 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
148 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
150 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
151 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
152 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
153 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
155 /* Set eSPI BRG clock source */
156 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
157 | ESPI_CSMODE_PM(pm) | div16);
161 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
162 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
164 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
165 | ESPI_CSMODE_CI_INACTIVEHIGH);
167 /* Character bit order: msb first */
168 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
169 | ESPI_CSMODE_REV_MSB_FIRST);
171 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
172 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
173 | ESPI_CSMODE_LEN(7));
178 void spi_release_bus(struct spi_slave *slave)
183 static void fsl_espi_tx(struct fsl_spi_slave *fsl, const void *dout)
185 ccsr_espi_t *espi = fsl->espi;
186 unsigned int tmpdout, event;
190 tmpdout = *(u32 *)dout;
194 out_be32(&espi->tx, tmpdout);
195 out_be32(&espi->event, ESPI_EV_TNF);
196 debug("***spi_xfer:...%08x written\n", tmpdout);
198 tmp_tx_timeout = fsl->tx_timeout;
199 /* Wait for eSPI transmit to go out */
200 while (tmp_tx_timeout--) {
201 event = in_be32(&espi->event);
202 if (event & ESPI_EV_DON || event & ESPI_EV_TXE) {
203 out_be32(&espi->event, ESPI_EV_TXE);
209 if (tmp_tx_timeout < 0)
210 debug("***spi_xfer:...Tx timeout! event = %08x\n", event);
213 static int fsl_espi_rx(struct fsl_spi_slave *fsl, void *din, unsigned int bytes)
215 ccsr_espi_t *espi = fsl->espi;
216 unsigned int tmpdin, rx_times;
217 unsigned char *buf, *p_cursor;
222 rx_times = DIV_ROUND_UP(bytes, 4);
223 buf = (unsigned char *)malloc(4 * rx_times);
225 debug("SF: Failed to malloc memory.\n");
230 tmpdin = in_be32(&espi->rx);
231 debug("***spi_xfer:...%08x readed\n", tmpdin);
232 *(u32 *)p_cursor = tmpdin;
237 memcpy(din, buf, bytes);
240 out_be32(&espi->event, ESPI_EV_RNE);
245 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *data_out,
246 void *data_in, unsigned long flags)
248 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
249 ccsr_espi_t *espi = fsl->espi;
250 unsigned int event, rx_bytes;
251 const void *dout = NULL;
254 int num_blks, num_chunks, max_tran_len, tran_len;
256 unsigned char *buffer = NULL;
258 u8 *cmd_buf = fsl->cmd_buf;
259 size_t cmd_len = fsl->cmd_len;
260 size_t data_len = bitlen / 8;
261 size_t rx_offset = 0;
264 max_tran_len = fsl->max_transfer_length;
267 cmd_len = fsl->cmd_len = data_len;
268 memcpy(cmd_buf, data_out, cmd_len);
273 spi_cs_deactivate(slave);
276 buf_len = 2 * cmd_len + min(data_len, (size_t)max_tran_len);
277 len = cmd_len + data_len;
279 buffer = (unsigned char *)malloc(buf_len);
281 debug("SF: Failed to malloc memory.\n");
284 memcpy(buffer, cmd_buf, cmd_len);
286 memcpy(buffer + cmd_len, data_out, data_len);
288 case SPI_XFER_BEGIN | SPI_XFER_END:
290 buffer = (unsigned char *)malloc(len * 2);
292 debug("SF: Failed to malloc memory.\n");
295 memcpy(buffer, data_out, len);
301 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) len %u\n",
302 *(uint *)data_out, data_out, *(uint *)data_in, data_in, len);
304 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
305 while (num_chunks--) {
307 din = buffer + rx_offset;
309 tran_len = min(data_len, (size_t)max_tran_len);
310 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
311 num_bytes = (tran_len + cmd_len) % 4;
312 fsl->data_len = tran_len + cmd_len;
313 spi_cs_activate(slave);
315 /* Clear all eSPI events */
316 out_be32(&espi->event , 0xffffffff);
317 /* handle data in 32-bit chunks */
319 event = in_be32(&espi->event);
320 if (event & ESPI_EV_TNF) {
321 fsl_espi_tx(fsl, dout);
322 /* Set up the next iteration */
329 event = in_be32(&espi->event);
330 if (event & ESPI_EV_RNE) {
331 rf_cnt = ((event & ESPI_EV_RFCNT_MASK)
332 >> ESPI_EV_RFCNT_SHIFT);
335 else if (num_blks == 1 && rf_cnt == num_bytes)
336 rx_bytes = num_bytes;
339 if (fsl_espi_rx(fsl, din, rx_bytes)
343 din = (unsigned char *)din
349 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
350 if (*buffer == 0x0b) {
352 data_len -= tran_len;
353 *(int *)buffer += tran_len;
356 spi_cs_deactivate(slave);
363 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
365 return bus == 0 && cs < ESPI_MAX_CS_NUM;
368 void spi_cs_activate(struct spi_slave *slave)
370 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
371 ccsr_espi_t *espi = fsl->espi;
372 unsigned int com = 0;
373 size_t data_len = fsl->data_len;
375 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
376 com |= ESPI_COM_CS(slave->cs);
377 com |= ESPI_COM_TRANLEN(data_len - 1);
378 out_be32(&espi->com, com);
381 void spi_cs_deactivate(struct spi_slave *slave)
383 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
384 ccsr_espi_t *espi = fsl->espi;
386 /* clear the RXCNT and TXCNT */
387 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
388 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);