1 // SPDX-License-Identifier: GPL-2.0+
3 * eSPI controller driver.
5 * Copyright 2010-2011 Freescale Semiconductor, Inc.
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
18 #include <asm/immap_85xx.h>
22 #include <dm/platform_data/fsl_espi.h>
24 struct fsl_spi_slave {
25 struct spi_slave slave;
36 unsigned int max_transfer_length;
39 #define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
40 #define US_PER_SECOND 1000000UL
42 /* default SCK frequency, unit: HZ */
43 #define FSL_ESPI_DEFAULT_SCK_FREQ 10000000
45 #define ESPI_MAX_CS_NUM 4
46 #define ESPI_FIFO_WIDTH_BIT 32
48 #define ESPI_EV_RNE BIT(9)
49 #define ESPI_EV_TNF BIT(8)
50 #define ESPI_EV_DON BIT(14)
51 #define ESPI_EV_TXE BIT(15)
52 #define ESPI_EV_RFCNT_SHIFT 24
53 #define ESPI_EV_RFCNT_MASK (0x3f << ESPI_EV_RFCNT_SHIFT)
55 #define ESPI_MODE_EN BIT(31) /* Enable interface */
56 #define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
57 #define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
59 #define ESPI_COM_CS(x) ((x) << 30)
60 #define ESPI_COM_TRANLEN(x) ((x) << 0)
62 #define ESPI_CSMODE_CI_INACTIVEHIGH BIT(31)
63 #define ESPI_CSMODE_CP_BEGIN_EDGCLK BIT(30)
64 #define ESPI_CSMODE_REV_MSB_FIRST BIT(29)
65 #define ESPI_CSMODE_DIV16 BIT(28)
66 #define ESPI_CSMODE_PM(x) ((x) << 24)
67 #define ESPI_CSMODE_POL_ASSERTED_LOW BIT(20)
68 #define ESPI_CSMODE_LEN(x) ((x) << 16)
69 #define ESPI_CSMODE_CSBEF(x) ((x) << 12)
70 #define ESPI_CSMODE_CSAFT(x) ((x) << 8)
71 #define ESPI_CSMODE_CSCG(x) ((x) << 3)
73 #define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
74 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
77 #define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
79 void fsl_spi_cs_activate(struct spi_slave *slave, uint cs)
81 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
82 ccsr_espi_t *espi = fsl->espi;
84 size_t data_len = fsl->data_len;
86 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
87 com |= ESPI_COM_CS(cs);
88 com |= ESPI_COM_TRANLEN(data_len - 1);
89 out_be32(&espi->com, com);
92 void fsl_spi_cs_deactivate(struct spi_slave *slave)
94 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
95 ccsr_espi_t *espi = fsl->espi;
97 /* clear the RXCNT and TXCNT */
98 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
99 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);
102 static void fsl_espi_tx(struct fsl_spi_slave *fsl, const void *dout)
104 ccsr_espi_t *espi = fsl->espi;
105 unsigned int tmpdout, event;
109 tmpdout = *(u32 *)dout;
113 out_be32(&espi->tx, tmpdout);
114 out_be32(&espi->event, ESPI_EV_TNF);
115 debug("***spi_xfer:...%08x written\n", tmpdout);
117 tmp_tx_timeout = fsl->tx_timeout;
118 /* Wait for eSPI transmit to go out */
119 while (tmp_tx_timeout--) {
120 event = in_be32(&espi->event);
121 if (event & ESPI_EV_DON || event & ESPI_EV_TXE) {
122 out_be32(&espi->event, ESPI_EV_TXE);
128 if (tmp_tx_timeout < 0)
129 debug("***spi_xfer:...Tx timeout! event = %08x\n", event);
132 static int fsl_espi_rx(struct fsl_spi_slave *fsl, void *din,
135 ccsr_espi_t *espi = fsl->espi;
136 unsigned int tmpdin, rx_times;
137 unsigned char *buf, *p_cursor;
142 rx_times = DIV_ROUND_UP(bytes, 4);
143 buf = (unsigned char *)malloc(4 * rx_times);
145 debug("SF: Failed to malloc memory.\n");
150 tmpdin = in_be32(&espi->rx);
151 debug("***spi_xfer:...%08x readed\n", tmpdin);
152 *(u32 *)p_cursor = tmpdin;
157 memcpy(din, buf, bytes);
160 out_be32(&espi->event, ESPI_EV_RNE);
165 void espi_release_bus(struct fsl_spi_slave *fsl)
167 /* Disable the SPI hardware */
168 out_be32(&fsl->espi->mode,
169 in_be32(&fsl->espi->mode) & (~ESPI_MODE_EN));
172 int espi_xfer(struct fsl_spi_slave *fsl, uint cs, unsigned int bitlen,
173 const void *data_out, void *data_in, unsigned long flags)
175 struct spi_slave *slave = &fsl->slave;
176 ccsr_espi_t *espi = fsl->espi;
177 unsigned int event, rx_bytes;
178 const void *dout = NULL;
181 int num_blks, num_chunks, max_tran_len, tran_len;
183 unsigned char *buffer = NULL;
185 u8 *cmd_buf = fsl->cmd_buf;
186 size_t cmd_len = fsl->cmd_len;
187 size_t data_len = bitlen / 8;
188 size_t rx_offset = 0;
191 max_tran_len = fsl->max_transfer_length;
195 fsl->cmd_len = cmd_len;
196 memcpy(cmd_buf, data_out, cmd_len);
201 fsl_spi_cs_deactivate(slave);
204 buf_len = 2 * cmd_len + min(data_len, (size_t)max_tran_len);
205 len = cmd_len + data_len;
207 buffer = (unsigned char *)malloc(buf_len);
209 debug("SF: Failed to malloc memory.\n");
212 memcpy(buffer, cmd_buf, cmd_len);
214 memcpy(buffer + cmd_len, data_out, data_len);
216 case SPI_XFER_BEGIN | SPI_XFER_END:
218 buffer = (unsigned char *)malloc(len * 2);
220 debug("SF: Failed to malloc memory.\n");
223 memcpy(buffer, data_out, len);
229 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) len %u\n",
230 *(uint *)data_out, data_out, *(uint *)data_in, data_in, len);
232 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
233 while (num_chunks--) {
235 din = buffer + rx_offset;
237 tran_len = min(data_len, (size_t)max_tran_len);
238 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
239 num_bytes = (tran_len + cmd_len) % 4;
240 fsl->data_len = tran_len + cmd_len;
241 fsl_spi_cs_activate(slave, cs);
243 /* Clear all eSPI events */
244 out_be32(&espi->event , 0xffffffff);
245 /* handle data in 32-bit chunks */
247 event = in_be32(&espi->event);
248 if (event & ESPI_EV_TNF) {
249 fsl_espi_tx(fsl, dout);
250 /* Set up the next iteration */
257 event = in_be32(&espi->event);
258 if (event & ESPI_EV_RNE) {
259 rf_cnt = ((event & ESPI_EV_RFCNT_MASK)
260 >> ESPI_EV_RFCNT_SHIFT);
263 else if (num_blks == 1 && rf_cnt == num_bytes)
264 rx_bytes = num_bytes;
267 if (fsl_espi_rx(fsl, din, rx_bytes)
271 din = (unsigned char *)din
277 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
278 if (*buffer == 0x0b) {
280 data_len -= tran_len;
281 *(int *)buffer += tran_len;
284 fsl_spi_cs_deactivate(slave);
291 void espi_claim_bus(struct fsl_spi_slave *fsl, unsigned int cs)
293 ccsr_espi_t *espi = fsl->espi;
294 unsigned char pm = fsl->pm;
295 unsigned int mode = fsl->mode;
296 unsigned int div16 = fsl->div16;
299 /* Enable eSPI interface */
300 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
301 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
303 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
304 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
306 /* Init CS mode interface */
307 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
308 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
310 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
311 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
312 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
313 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
315 /* Set eSPI BRG clock source */
316 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
317 | ESPI_CSMODE_PM(pm) | div16);
321 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
322 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
324 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
325 | ESPI_CSMODE_CI_INACTIVEHIGH);
327 /* Character bit order: msb first */
328 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
329 | ESPI_CSMODE_REV_MSB_FIRST);
331 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
332 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
333 | ESPI_CSMODE_LEN(7));
336 void espi_setup_slave(struct fsl_spi_slave *fsl)
340 unsigned long spibrg = 0;
341 unsigned long spi_freq = 0;
342 unsigned char pm = 0;
344 max_hz = fsl->speed_hz;
346 get_sys_info(&sysinfo);
347 spibrg = sysinfo.freq_systembus / 2;
349 if ((spibrg / max_hz) > 32) {
350 fsl->div16 = ESPI_CSMODE_DIV16;
351 pm = spibrg / (max_hz * 16 * 2);
354 debug("max_hz is too low: %d Hz, %ld Hz is used.\n",
355 max_hz, spibrg / (32 * 16));
358 pm = spibrg / (max_hz * 2);
365 spi_freq = spibrg / ((pm + 1) * 2 * 16);
367 spi_freq = spibrg / ((pm + 1) * 2);
369 /* set tx_timeout to 10 times of one espi FIFO entry go out */
370 fsl->tx_timeout = DIV_ROUND_UP((US_PER_SECOND * ESPI_FIFO_WIDTH_BIT
371 * 10), spi_freq);/* Set eSPI BRG clock source */
374 #if !CONFIG_IS_ENABLED(DM_SPI)
375 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
377 return bus == 0 && cs < ESPI_MAX_CS_NUM;
380 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
381 unsigned int max_hz, unsigned int mode)
383 struct fsl_spi_slave *fsl;
385 if (!spi_cs_is_valid(bus, cs))
388 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
392 fsl->espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
394 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
395 fsl->speed_hz = max_hz;
397 espi_setup_slave(fsl);
402 void spi_free_slave(struct spi_slave *slave)
404 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
409 int spi_claim_bus(struct spi_slave *slave)
411 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
413 espi_claim_bus(fsl, slave->cs);
418 void spi_release_bus(struct spi_slave *slave)
420 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
422 espi_release_bus(fsl);
425 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
426 void *din, unsigned long flags)
428 struct fsl_spi_slave *fsl = (struct fsl_spi_slave *)slave;
430 return espi_xfer(fsl, slave->cs, bitlen, dout, din, flags);
433 static void __espi_set_speed(struct fsl_spi_slave *fsl)
435 espi_setup_slave(fsl);
437 /* Set eSPI BRG clock source */
438 out_be32(&fsl->espi->csmode[fsl->cs],
439 in_be32(&fsl->espi->csmode[fsl->cs])
440 | ESPI_CSMODE_PM(fsl->pm) | fsl->div16);
443 static void __espi_set_mode(struct fsl_spi_slave *fsl)
446 if (fsl->mode & SPI_CPHA)
447 out_be32(&fsl->espi->csmode[fsl->cs],
448 in_be32(&fsl->espi->csmode[fsl->cs])
449 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
450 if (fsl->mode & SPI_CPOL)
451 out_be32(&fsl->espi->csmode[fsl->cs],
452 in_be32(&fsl->espi->csmode[fsl->cs])
453 | ESPI_CSMODE_CI_INACTIVEHIGH);
456 static int fsl_espi_claim_bus(struct udevice *dev)
458 struct udevice *bus = dev->parent;
459 struct fsl_spi_slave *fsl = dev_get_priv(bus);
461 espi_claim_bus(fsl, fsl->cs);
466 static int fsl_espi_release_bus(struct udevice *dev)
468 struct udevice *bus = dev->parent;
469 struct fsl_spi_slave *fsl = dev_get_priv(bus);
471 espi_release_bus(fsl);
476 static int fsl_espi_xfer(struct udevice *dev, unsigned int bitlen,
477 const void *dout, void *din, unsigned long flags)
479 struct udevice *bus = dev->parent;
480 struct fsl_spi_slave *fsl = dev_get_priv(bus);
482 return espi_xfer(fsl, fsl->cs, bitlen, dout, din, flags);
485 static int fsl_espi_set_speed(struct udevice *bus, uint speed)
487 struct fsl_spi_slave *fsl = dev_get_priv(bus);
489 debug("%s speed %u\n", __func__, speed);
490 fsl->speed_hz = speed;
492 __espi_set_speed(fsl);
497 static int fsl_espi_set_mode(struct udevice *bus, uint mode)
499 struct fsl_spi_slave *fsl = dev_get_priv(bus);
501 debug("%s mode %u\n", __func__, mode);
504 __espi_set_mode(fsl);
509 static int fsl_espi_child_pre_probe(struct udevice *dev)
511 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
512 struct udevice *bus = dev->parent;
513 struct fsl_spi_slave *fsl = dev_get_priv(bus);
515 debug("%s cs %u\n", __func__, slave_plat->cs);
516 fsl->cs = slave_plat->cs;
521 static int fsl_espi_probe(struct udevice *bus)
523 struct fsl_espi_plat *plat = dev_get_plat(bus);
524 struct fsl_spi_slave *fsl = dev_get_priv(bus);
526 fsl->espi = (ccsr_espi_t *)((u32)plat->regs_addr);
527 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
528 fsl->speed_hz = plat->speed_hz;
530 debug("%s probe done, bus-num %d.\n", bus->name, dev_seq(bus));
535 static const struct dm_spi_ops fsl_espi_ops = {
536 .claim_bus = fsl_espi_claim_bus,
537 .release_bus = fsl_espi_release_bus,
538 .xfer = fsl_espi_xfer,
539 .set_speed = fsl_espi_set_speed,
540 .set_mode = fsl_espi_set_mode,
543 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
544 static int fsl_espi_of_to_plat(struct udevice *bus)
547 struct fsl_espi_plat *plat = bus->plat;
548 const void *blob = gd->fdt_blob;
549 int node = dev_of_offset(bus);
551 addr = dev_read_addr(bus);
552 if (addr == FDT_ADDR_T_NONE)
555 plat->regs_addr = lower_32_bits(addr);
556 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
557 FSL_ESPI_DEFAULT_SCK_FREQ);
559 debug("ESPI: regs=%p, max-frequency=%d\n",
560 &plat->regs_addr, plat->speed_hz);
565 static const struct udevice_id fsl_espi_ids[] = {
566 { .compatible = "fsl,mpc8536-espi" },
571 U_BOOT_DRIVER(fsl_espi) = {
574 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
575 .of_match = fsl_espi_ids,
576 .of_to_plat = fsl_espi_of_to_plat,
578 .ops = &fsl_espi_ops,
579 .plat_auto = sizeof(struct fsl_espi_plat),
580 .priv_auto = sizeof(struct fsl_spi_slave),
581 .probe = fsl_espi_probe,
582 .child_pre_probe = fsl_espi_child_pre_probe,